[Lldb-commits] [lldb] 4c8b6fa - [lldb][NFCI] TypeSystemClang::GetTypeForIdentifier should take a StringRef

Alex Langford via lldb-commits lldb-commits at lists.llvm.org
Fri Jun 16 10:49:09 PDT 2023


Author: Alex Langford
Date: 2023-06-16T10:44:55-07:00
New Revision: 4c8b6fae4437f9a5b17ddc146592aec0e4c78e02

URL: https://github.com/llvm/llvm-project/commit/4c8b6fae4437f9a5b17ddc146592aec0e4c78e02
DIFF: https://github.com/llvm/llvm-project/commit/4c8b6fae4437f9a5b17ddc146592aec0e4c78e02.diff

LOG: [lldb][NFCI] TypeSystemClang::GetTypeForIdentifier should take a StringRef

This method just takes its ConstString parameter and gets a StringRef
out of it. Let's just pass in a StringRef directly.

This also cleans up some logic in the callers to be a little easier to
read and to avoid unnecessary ConstString creation.

Differential Revision: https://reviews.llvm.org/D153054

Added: 
    

Modified: 
    lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
    lldb/source/Plugins/RegisterTypeBuilder/RegisterTypeBuilderClang.cpp
    lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp
    lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/Language/ObjC/NSDictionary.cpp b/lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
index 702d196a7dda9..2e927eb8d8569 100644
--- a/lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
+++ b/lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
@@ -65,32 +65,30 @@ NSDictionary_Additionals::GetAdditionalSynthetics() {
 
 static CompilerType GetLLDBNSPairType(TargetSP target_sp) {
   CompilerType compiler_type;
-
   TypeSystemClangSP scratch_ts_sp =
       ScratchTypeSystemClang::GetForTarget(*target_sp);
 
-  if (scratch_ts_sp) {
-    ConstString g_lldb_autogen_nspair("__lldb_autogen_nspair");
-
-    compiler_type = scratch_ts_sp->GetTypeForIdentifier<clang::CXXRecordDecl>(
-        g_lldb_autogen_nspair);
-
-    if (!compiler_type) {
-      compiler_type = scratch_ts_sp->CreateRecordType(
-          nullptr, OptionalClangModuleID(), lldb::eAccessPublic,
-          g_lldb_autogen_nspair.GetCString(), clang::TTK_Struct,
-          lldb::eLanguageTypeC);
-
-      if (compiler_type) {
-        TypeSystemClang::StartTagDeclarationDefinition(compiler_type);
-        CompilerType id_compiler_type =
-            scratch_ts_sp->GetBasicType(eBasicTypeObjCID);
-        TypeSystemClang::AddFieldToRecordType(
-            compiler_type, "key", id_compiler_type, lldb::eAccessPublic, 0);
-        TypeSystemClang::AddFieldToRecordType(
-            compiler_type, "value", id_compiler_type, lldb::eAccessPublic, 0);
-        TypeSystemClang::CompleteTagDeclarationDefinition(compiler_type);
-      }
+  if (!scratch_ts_sp)
+    return compiler_type;
+
+  static constexpr llvm::StringLiteral g_lldb_autogen_nspair("__lldb_autogen_nspair");
+
+  compiler_type = scratch_ts_sp->GetTypeForIdentifier<clang::CXXRecordDecl>(g_lldb_autogen_nspair);
+
+  if (!compiler_type) {
+    compiler_type = scratch_ts_sp->CreateRecordType(
+        nullptr, OptionalClangModuleID(), lldb::eAccessPublic,
+        g_lldb_autogen_nspair, clang::TTK_Struct, lldb::eLanguageTypeC);
+
+    if (compiler_type) {
+      TypeSystemClang::StartTagDeclarationDefinition(compiler_type);
+      CompilerType id_compiler_type =
+          scratch_ts_sp->GetBasicType(eBasicTypeObjCID);
+      TypeSystemClang::AddFieldToRecordType(
+          compiler_type, "key", id_compiler_type, lldb::eAccessPublic, 0);
+      TypeSystemClang::AddFieldToRecordType(
+          compiler_type, "value", id_compiler_type, lldb::eAccessPublic, 0);
+      TypeSystemClang::CompleteTagDeclarationDefinition(compiler_type);
     }
   }
   return compiler_type;

diff  --git a/lldb/source/Plugins/RegisterTypeBuilder/RegisterTypeBuilderClang.cpp b/lldb/source/Plugins/RegisterTypeBuilder/RegisterTypeBuilderClang.cpp
index 49348ed697014..aeb54ef9ee24b 100644
--- a/lldb/source/Plugins/RegisterTypeBuilder/RegisterTypeBuilderClang.cpp
+++ b/lldb/source/Plugins/RegisterTypeBuilder/RegisterTypeBuilderClang.cpp
@@ -48,7 +48,7 @@ CompilerType RegisterTypeBuilderClang::GetRegisterType(
   // See if we have made this type before and can reuse it.
   CompilerType fields_type =
       type_system->GetTypeForIdentifier<clang::CXXRecordDecl>(
-          ConstString(register_type_name.c_str()));
+          register_type_name);
 
   if (!fields_type) {
     // In most ABI, a change of field type means a change in storage unit.

diff  --git a/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp b/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp
index b1a882465c404..5efa5bccb85f5 100644
--- a/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp
+++ b/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp
@@ -407,8 +407,8 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
     // symbols in PDB for types with const or volatile modifiers, but we need
     // to create only one declaration for them all.
     Type::ResolveState type_resolve_state;
-    CompilerType clang_type = m_ast.GetTypeForIdentifier<clang::CXXRecordDecl>(
-        ConstString(name), decl_context);
+    CompilerType clang_type =
+        m_ast.GetTypeForIdentifier<clang::CXXRecordDecl>(name, decl_context);
     if (!clang_type.IsValid()) {
       auto access = GetAccessibilityForUdt(*udt);
 
@@ -479,8 +479,8 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
     uint64_t bytes = enum_type->getLength();
 
     // Check if such an enum already exists in the current context
-    CompilerType ast_enum = m_ast.GetTypeForIdentifier<clang::EnumDecl>(
-        ConstString(name), decl_context);
+    CompilerType ast_enum =
+        m_ast.GetTypeForIdentifier<clang::EnumDecl>(name, decl_context);
     if (!ast_enum.IsValid()) {
       auto underlying_type_up = enum_type->getUnderlyingType();
       if (!underlying_type_up)
@@ -557,8 +557,7 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
 
     // Check if such a typedef already exists in the current context
     CompilerType ast_typedef =
-        m_ast.GetTypeForIdentifier<clang::TypedefNameDecl>(ConstString(name),
-                                                           decl_ctx);
+        m_ast.GetTypeForIdentifier<clang::TypedefNameDecl>(name, decl_ctx);
     if (!ast_typedef.IsValid()) {
       CompilerType target_ast_type = target_type->GetFullCompilerType();
 

diff  --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
index bafae2e8a1da5..a3ddc5323b6e1 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
@@ -252,31 +252,29 @@ class TypeSystemClang : public TypeSystem {
 
   template <typename RecordDeclType>
   CompilerType
-  GetTypeForIdentifier(ConstString type_name,
+  GetTypeForIdentifier(llvm::StringRef type_name,
                        clang::DeclContext *decl_context = nullptr) {
     CompilerType compiler_type;
-
-    if (type_name.GetLength()) {
-      clang::ASTContext &ast = getASTContext();
-      if (!decl_context)
-        decl_context = ast.getTranslationUnitDecl();
-
-      clang::IdentifierInfo &myIdent = ast.Idents.get(type_name.GetCString());
-      clang::DeclarationName myName =
-          ast.DeclarationNames.getIdentifier(&myIdent);
-
-      clang::DeclContext::lookup_result result = decl_context->lookup(myName);
-
-      if (!result.empty()) {
-        clang::NamedDecl *named_decl = *result.begin();
-        if (const RecordDeclType *record_decl =
-                llvm::dyn_cast<RecordDeclType>(named_decl))
-          compiler_type =
-              CompilerType(weak_from_this(),
-                           clang::QualType(record_decl->getTypeForDecl(), 0)
-                               .getAsOpaquePtr());
-      }
-    }
+    if (type_name.empty())
+      return compiler_type;
+
+    clang::ASTContext &ast = getASTContext();
+    if (!decl_context)
+      decl_context = ast.getTranslationUnitDecl();
+
+    clang::IdentifierInfo &myIdent = ast.Idents.get(type_name);
+    clang::DeclarationName myName =
+        ast.DeclarationNames.getIdentifier(&myIdent);
+    clang::DeclContext::lookup_result result = decl_context->lookup(myName);
+    if (result.empty())
+      return compiler_type;
+
+    clang::NamedDecl *named_decl = *result.begin();
+    if (const RecordDeclType *record_decl =
+            llvm::dyn_cast<RecordDeclType>(named_decl))
+      compiler_type = CompilerType(
+          weak_from_this(),
+          clang::QualType(record_decl->getTypeForDecl(), 0).getAsOpaquePtr());
 
     return compiler_type;
   }


        


More information about the lldb-commits mailing list