Author: Raphael Isemann
Date: 2019-12-28T22:45:23+01:00
New Revision: 36fb199ecaa5c75bd339f530277cd6d1f32033b3

URL: 
https://github.com/llvm/llvm-project/commit/36fb199ecaa5c75bd339f530277cd6d1f32033b3
DIFF: 
https://github.com/llvm/llvm-project/commit/36fb199ecaa5c75bd339f530277cd6d1f32033b3.diff

LOG: [lldb][NFC] Remove GetASTContext call in ClangPersistentVariables

We try to build a CompilerType from the persistent decls so we need
a ClangASTContext. With this patch the ClangPersistentVariables store
the associated ClangASTContext of the persistent decls (which is
always the scratch ClangASTContext) and no longer call GetASTContext
to map back from clang::ASTContext to ClangASTContext.

Added: 
    

Modified: 
    lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
    lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp
    lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h

Removed: 
    


################################################################################
diff  --git 
a/lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp 
b/lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
index 1caac9e11cbd..5e6a1ac2a083 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
@@ -448,12 +448,17 @@ void ASTResultSynthesizer::RecordPersistentDecl(NamedDecl 
*D) {
 }
 
 void ASTResultSynthesizer::CommitPersistentDecls() {
+  PersistentExpressionState *state =
+      m_target.GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC);
+  auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state);
+  ClangASTContext *scratch_ctx = ClangASTContext::GetScratch(m_target);
+
   for (clang::NamedDecl *decl : m_decls) {
     StringRef name = decl->getName();
     ConstString name_cs(name.str().c_str());
 
     Decl *D_scratch = m_target.GetClangASTImporter()->DeportDecl(
-        &ClangASTContext::GetScratch(m_target)->getASTContext(), decl);
+        &scratch_ctx->getASTContext(), decl);
 
     if (!D_scratch) {
       Log 
*log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
@@ -471,10 +476,8 @@ void ASTResultSynthesizer::CommitPersistentDecls() {
     }
 
     if (NamedDecl *NamedDecl_scratch = dyn_cast<NamedDecl>(D_scratch))
-      llvm::cast<ClangPersistentVariables>(
-          m_target.GetPersistentExpressionStateForLanguage(
-              lldb::eLanguageTypeC))
-          ->RegisterPersistentDecl(name_cs, NamedDecl_scratch);
+      persistent_vars->RegisterPersistentDecl(name_cs, NamedDecl_scratch,
+                                              scratch_ctx);
   }
 }
 

diff  --git 
a/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp 
b/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp
index 24dd705e37b1..7423f623efb2 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp
@@ -68,38 +68,36 @@ llvm::Optional<CompilerType>
 ClangPersistentVariables::GetCompilerTypeFromPersistentDecl(
     ConstString type_name) {
   CompilerType compiler_type;
-  if (clang::TypeDecl *tdecl = llvm::dyn_cast_or_null<clang::TypeDecl>(
-          GetPersistentDecl(type_name))) {
-    compiler_type.SetCompilerType(
-        ClangASTContext::GetASTContext(&tdecl->getASTContext()),
-        reinterpret_cast<lldb::opaque_compiler_type_t>(
-            const_cast<clang::Type *>(tdecl->getTypeForDecl())));
-    return compiler_type;
+
+  PersistentDecl p = m_persistent_decls.lookup(type_name.GetCString());
+
+  if (p.m_decl == nullptr)
+    return llvm::None;
+
+  if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {
+    opaque_compiler_type_t t = static_cast<opaque_compiler_type_t>(
+        const_cast<clang::Type *>(tdecl->getTypeForDecl()));
+    return CompilerType(p.m_context, t);
   }
   return llvm::None;
 }
 
 void ClangPersistentVariables::RegisterPersistentDecl(ConstString name,
-                                                      clang::NamedDecl *decl) {
-  m_persistent_decls.insert(
-      std::pair<const char *, clang::NamedDecl *>(name.GetCString(), decl));
+                                                      clang::NamedDecl *decl,
+                                                      ClangASTContext *ctx) {
+  PersistentDecl p = {decl, ctx};
+  m_persistent_decls.insert(std::make_pair(name.GetCString(), p));
 
   if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
     for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
-      m_persistent_decls.insert(std::pair<const char *, clang::NamedDecl *>(
-          ConstString(enumerator_decl->getNameAsString()).GetCString(),
-          enumerator_decl));
+      p = {enumerator_decl, ctx};
+      m_persistent_decls.insert(std::make_pair(
+          ConstString(enumerator_decl->getNameAsString()).GetCString(), p));
     }
   }
 }
 
 clang::NamedDecl *
 ClangPersistentVariables::GetPersistentDecl(ConstString name) {
-  PersistentDeclMap::const_iterator i =
-      m_persistent_decls.find(name.GetCString());
-
-  if (i == m_persistent_decls.end())
-    return nullptr;
-  else
-    return i->second;
+  return m_persistent_decls.lookup(name.GetCString()).m_decl;
 }

diff  --git 
a/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h 
b/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h
index 95e6c3ac963d..434196b35fd5 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h
@@ -62,7 +62,8 @@ class ClangPersistentVariables : public 
PersistentExpressionState {
   llvm::Optional<CompilerType>
   GetCompilerTypeFromPersistentDecl(ConstString type_name) override;
 
-  void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl);
+  void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl,
+                              ClangASTContext *ctx);
 
   clang::NamedDecl *GetPersistentDecl(ConstString name);
 
@@ -80,7 +81,14 @@ class ClangPersistentVariables : public 
PersistentExpressionState {
   // The counter used by GetNextPersistentVariableName
   uint32_t m_next_persistent_variable_id = 0;
 
-  typedef llvm::DenseMap<const char *, clang::NamedDecl *> PersistentDeclMap;
+  struct PersistentDecl {
+    /// The persistent decl.
+    clang::NamedDecl *m_decl = nullptr;
+    /// The ClangASTContext for the ASTContext of m_decl.
+    ClangASTContext *m_context = nullptr;
+  };
+
+  typedef llvm::DenseMap<const char *, PersistentDecl> PersistentDeclMap;
   PersistentDeclMap
       m_persistent_decls; ///< Persistent entities declared by the user.
 


        
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to