[lldb][NFC] Add safe Decl->CompilerDecl conversion function TypeSystemClang
authorRaphael Isemann <teemperor@gmail.com>
Fri, 31 Jan 2020 08:22:25 +0000 (09:22 +0100)
committerRaphael Isemann <teemperor@gmail.com>
Fri, 31 Jan 2020 08:44:24 +0000 (09:44 +0100)
This adds a conversion function from clang::Decl to CompilerDecl. It checks
that the TypeSystemClang in the CompilerDecl actually fits to the clang::Decl
AST during creation, thus preventing the creation of CompilerDecl instances with
inconsistent state.

lldb/include/lldb/Symbol/TypeSystemClang.h
lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp
lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCDeclVendor.cpp
lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp
lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
lldb/source/Symbol/TypeSystemClang.cpp

index 9d4d5e5..fd8276d 100644 (file)
@@ -407,6 +407,16 @@ public:
       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
           &vbase_offsets);
 
+  /// Creates a CompilerDecl from the given Decl with the current
+  /// TypeSystemClang instance as its typesystem.
+  /// The Decl has to come from the ASTContext of this
+  /// TypeSystemClang.
+  CompilerDecl GetCompilerDecl(clang::Decl *decl) {
+    assert(&decl->getASTContext() == &getASTContext() &&
+           "CreateCompilerDecl for Decl from wrong ASTContext?");
+    return CompilerDecl(this, decl);
+  }
+
   // CompilerDecl override functions
   ConstString DeclGetName(void *opaque_decl) override;
 
index c6eb451..cc3b4ed 100644 (file)
@@ -384,7 +384,7 @@ ClangModulesDeclVendorImpl::FindDecls(ConstString name, bool append,
     if (num_matches >= max_matches)
       return num_matches;
 
-    decls.push_back(CompilerDecl(m_ast_context.get(), named_decl));
+    decls.push_back(m_ast_context->GetCompilerDecl(named_decl));
     ++num_matches;
   }
 
index bdc5c31..2701f5b 100644 (file)
@@ -583,7 +583,7 @@ uint32_t AppleObjCDeclVendor::FindDecls(ConstString name, bool append,
                    current_id, result_iface_type.getAsString(), isa_value);
         }
 
-        decls.push_back(CompilerDecl(&m_ast_ctx, result_iface_decl));
+        decls.push_back(m_ast_ctx.GetCompilerDecl(result_iface_decl));
         ret++;
         break;
       } else {
@@ -626,7 +626,7 @@ uint32_t AppleObjCDeclVendor::FindDecls(ConstString name, bool append,
                new_iface_type.getAsString(), (uint64_t)isa);
     }
 
-    decls.push_back(CompilerDecl(&m_ast_ctx, iface_decl));
+    decls.push_back(m_ast_ctx.GetCompilerDecl(iface_decl));
     ret++;
     break;
   } while (false);
index 468603c..39d4b37 100644 (file)
@@ -2182,7 +2182,7 @@ void DWARFASTParserClang::EnsureAllDIEsInDeclContextHaveBeenParsed(
 CompilerDecl DWARFASTParserClang::GetDeclForUIDFromDWARF(const DWARFDIE &die) {
   clang::Decl *clang_decl = GetClangDeclForDIE(die);
   if (clang_decl != nullptr)
-    return CompilerDecl(&m_ast, clang_decl);
+    return m_ast.GetCompilerDecl(clang_decl);
   return CompilerDecl();
 }
 
index d06d542..806f7b6 100644 (file)
@@ -1334,7 +1334,7 @@ void PdbAstBuilder::ParseDeclsForContext(clang::DeclContext &context) {
 }
 
 CompilerDecl PdbAstBuilder::ToCompilerDecl(clang::Decl &decl) {
-  return {&m_clang, &decl};
+  return m_clang.GetCompilerDecl(&decl);
 }
 
 CompilerType PdbAstBuilder::ToCompilerType(clang::QualType qt) {
index 16fda8c..04a9dae 100644 (file)
@@ -636,7 +636,7 @@ lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
   if (!decl)
     return CompilerDecl();
 
-  return CompilerDecl(clang_ast_ctx, decl);
+  return clang_ast_ctx->GetCompilerDecl(decl);
 }
 
 lldb_private::CompilerDeclContext
index d1bb2fe..77e89eb 100644 (file)
@@ -4129,7 +4129,7 @@ TypeSystemClang::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
               else
                 kind = lldb::eMemberFunctionKindInstanceMethod;
               clang_type = GetType(cxx_method_decl->getType());
-              clang_decl = CompilerDecl(this, cxx_method_decl);
+              clang_decl = GetCompilerDecl(cxx_method_decl);
             }
           }
         }
@@ -4155,7 +4155,7 @@ TypeSystemClang::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
             clang::ObjCMethodDecl *objc_method_decl =
                 method_iter->getCanonicalDecl();
             if (objc_method_decl) {
-              clang_decl = CompilerDecl(this, objc_method_decl);
+              clang_decl = GetCompilerDecl(objc_method_decl);
               name = objc_method_decl->getSelector().getAsString();
               if (objc_method_decl->isClassMethod())
                 kind = lldb::eMemberFunctionKindStaticMethod;
@@ -4185,7 +4185,7 @@ TypeSystemClang::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
               clang::ObjCMethodDecl *objc_method_decl =
                   method_iter->getCanonicalDecl();
               if (objc_method_decl) {
-                clang_decl = CompilerDecl(this, objc_method_decl);
+                clang_decl = GetCompilerDecl(objc_method_decl);
                 name = objc_method_decl->getSelector().getAsString();
                 if (objc_method_decl->isClassMethod())
                   kind = lldb::eMemberFunctionKindStaticMethod;
@@ -8980,14 +8980,14 @@ std::vector<CompilerDecl> TypeSystemClang::DeclContextFindDeclByName(
                 IdentifierInfo *ii = nd->getIdentifier();
                 if (ii != nullptr &&
                     ii->getName().equals(name.AsCString(nullptr)))
-                  found_decls.push_back(CompilerDecl(this, nd));
+                  found_decls.push_back(GetCompilerDecl(nd));
               }
             }
           } else if (clang::NamedDecl *nd =
                          llvm::dyn_cast<clang::NamedDecl>(child)) {
             IdentifierInfo *ii = nd->getIdentifier();
             if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
-              found_decls.push_back(CompilerDecl(this, nd));
+              found_decls.push_back(GetCompilerDecl(nd));
           }
         }
       }