[Lldb-commits] [lldb] r263864 - Handle any persistent Decl in the Clang expression parser, not just types.

Sean Callanan via lldb-commits lldb-commits at lists.llvm.org
Fri Mar 18 17:51:44 PDT 2016


Author: spyffe
Date: Fri Mar 18 19:51:43 2016
New Revision: 263864

URL: http://llvm.org/viewvc/llvm-project?rev=263864&view=rev
Log:
Handle any persistent Decl in the Clang expression parser, not just types.

Persistent decls have traditionally only been types.  However, we want to
be able to persist more things, like functions and global variables.  This
changes some of the nomenclature and the lookup rules to make this possible.

<rdar://problem/22864976>

Modified:
    lldb/trunk/source/Commands/CommandObjectMemory.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h

Modified: lldb/trunk/source/Commands/CommandObjectMemory.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectMemory.cpp?rev=263864&r1=263863&r2=263864&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectMemory.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectMemory.cpp Fri Mar 18 19:51:43 2016
@@ -540,7 +540,7 @@ protected:
             {
                 if (ClangPersistentVariables *persistent_vars = llvm::dyn_cast_or_null<ClangPersistentVariables>(target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC)))
                 {
-                    clang::TypeDecl *tdecl = persistent_vars->GetPersistentType(ConstString(lookup_type_name));
+                    clang::TypeDecl *tdecl = llvm::dyn_cast_or_null<clang::TypeDecl>(persistent_vars->GetPersistentDecl(ConstString(lookup_type_name)));
 
                     if (tdecl)
                     {

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp?rev=263864&r1=263863&r2=263864&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp Fri Mar 18 19:51:43 2016
@@ -464,7 +464,7 @@ ASTResultSynthesizer::MaybeRecordPersist
                                                                  D);
 
     if (TypeDecl *TypeDecl_scratch = dyn_cast<TypeDecl>(D_scratch))
-        llvm::cast<ClangPersistentVariables>(m_target.GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC))->RegisterPersistentType(name_cs, TypeDecl_scratch);
+        llvm::cast<ClangPersistentVariables>(m_target.GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC))->RegisterPersistentDecl(name_cs, TypeDecl_scratch);
 }
 
 void

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp?rev=263864&r1=263863&r2=263864&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp Fri Mar 18 19:51:43 2016
@@ -877,6 +877,17 @@ ClangExpressionDeclMap::FindExternalVisi
                                                   unsigned int current_id)
 {
     assert (m_ast_context);
+    
+    std::function<void (clang::FunctionDecl *)> MaybeRegisterFunctionBody =
+    [this](clang::FunctionDecl *copied_function_decl)
+    {
+        if (copied_function_decl->getBody() && m_parser_vars->m_code_gen)
+        {
+            DeclGroupRef decl_group_ref(copied_function_decl);
+            m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
+        }
+    };
+
 
     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
 
@@ -902,6 +913,52 @@ ClangExpressionDeclMap::FindExternalVisi
     SymbolContext sym_ctx;
     if (frame != nullptr)
         sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock);
+    
+    // Try the persistent decls, which take precedence over all else.
+    if (!namespace_decl)
+    {
+        do
+        {
+            if (!target)
+                break;
+            
+            ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext();
+            
+            if (!scratch_clang_ast_context)
+                break;
+            
+            ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
+            
+            if (!scratch_ast_context)
+                break;
+            
+            NamedDecl *persistent_decl = m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
+            
+            if (!persistent_decl)
+                break;
+            
+            Decl *parser_persistent_decl = m_ast_importer_sp->CopyDecl(m_ast_context, scratch_ast_context, persistent_decl);
+            
+            if (!parser_persistent_decl)
+                break;
+            
+            NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
+            
+            if (!parser_named_decl)
+                break;
+            
+            if (clang::FunctionDecl *parser_function_decl = llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl))
+            {
+                MaybeRegisterFunctionBody(parser_function_decl);
+            }
+            
+            if (log)
+                log->Printf("  CEDM::FEVD[%u] Found persistent decl %s", current_id, name.GetCString());
+            
+            context.AddNamedDecl(parser_named_decl);
+        } while (0);
+    }
+    
     if (name_unique_cstr[0] == '$' && !namespace_decl)
     {
         static ConstString g_lldb_class_name ("$__lldb_class");
@@ -1153,42 +1210,6 @@ ClangExpressionDeclMap::FindExternalVisi
         if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1))
             return;
 
-        do
-        {
-            if (!target)
-                break;
-
-            ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext();
-
-            if (!scratch_clang_ast_context)
-                break;
-
-            ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
-
-            if (!scratch_ast_context)
-                break;
-
-            TypeDecl *ptype_type_decl = m_parser_vars->m_persistent_vars->GetPersistentType(name);
-
-            if (!ptype_type_decl)
-                break;
-
-            Decl *parser_ptype_decl = m_ast_importer_sp->CopyDecl(m_ast_context, scratch_ast_context, ptype_type_decl);
-
-            if (!parser_ptype_decl)
-                break;
-
-            TypeDecl *parser_ptype_type_decl = dyn_cast<TypeDecl>(parser_ptype_decl);
-
-            if (!parser_ptype_type_decl)
-                break;
-
-            if (log)
-                log->Printf("  CEDM::FEVD[%u] Found persistent type %s", current_id, name.GetCString());
-
-            context.AddNamedDecl(parser_ptype_type_decl);
-        } while (0);
-
         ExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name));
 
         if (pvar_sp)
@@ -1544,11 +1565,7 @@ ClangExpressionDeclMap::FindExternalVisi
                                 break;
                             }
                             
-                            if (copied_function_decl->getBody() && m_parser_vars->m_code_gen)
-                            {
-                                DeclGroupRef decl_group_ref(copied_function_decl);
-                                m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
-                            }
+                            MaybeRegisterFunctionBody(copied_function_decl);
                             
                             context.AddNamedDecl(copied_function_decl);
                             

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp?rev=263864&r1=263863&r2=263864&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp Fri Mar 18 19:51:43 2016
@@ -14,6 +14,8 @@
 #include "lldb/Core/StreamString.h"
 #include "lldb/Core/Value.h"
 
+#include "clang/AST/Decl.h"
+
 #include "llvm/ADT/StringMap.h"
 
 using namespace lldb;
@@ -66,18 +68,26 @@ ClangPersistentVariables::GetNextPersist
 }
 
 void
-ClangPersistentVariables::RegisterPersistentType (const ConstString &name,
-                                                  clang::TypeDecl *type_decl)
+ClangPersistentVariables::RegisterPersistentDecl (const ConstString &name,
+                                                  clang::NamedDecl *decl)
 {
-    m_persistent_types.insert(std::pair<const char*, clang::TypeDecl*>(name.GetCString(), type_decl));
+    m_persistent_decls.insert(std::pair<const char*, clang::NamedDecl*>(name.GetCString(), decl));
+    
+    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));
+        }
+    }
 }
 
-clang::TypeDecl *
-ClangPersistentVariables::GetPersistentType (const ConstString &name)
+clang::NamedDecl *
+ClangPersistentVariables::GetPersistentDecl (const ConstString &name)
 {
-    PersistentTypeMap::const_iterator i = m_persistent_types.find(name.GetCString());
+    PersistentDeclMap::const_iterator i = m_persistent_decls.find(name.GetCString());
     
-    if (i == m_persistent_types.end())
+    if (i == m_persistent_decls.end())
         return NULL;
     else
         return i->second;

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h?rev=263864&r1=263863&r2=263864&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h Fri Mar 18 19:51:43 2016
@@ -74,11 +74,11 @@ public:
     LookupSymbol (const ConstString &name) override { return LLDB_INVALID_ADDRESS; }
 
     void
-    RegisterPersistentType (const ConstString &name,
-                            clang::TypeDecl *tag_decl);
+    RegisterPersistentDecl (const ConstString &name,
+                            clang::NamedDecl *decl);
     
-    clang::TypeDecl *
-    GetPersistentType (const ConstString &name);
+    clang::NamedDecl *
+    GetPersistentDecl (const ConstString &name);
     
     void
     AddHandLoadedClangModule(ClangModulesDeclVendor::ModuleID module)
@@ -94,8 +94,8 @@ public:
 private:
     uint32_t                                                m_next_persistent_variable_id;  ///< The counter used by GetNextResultName().
     
-    typedef llvm::DenseMap<const char *, clang::TypeDecl *> PersistentTypeMap;
-    PersistentTypeMap                                       m_persistent_types;             ///< The persistent types declared by the user.
+    typedef llvm::DenseMap<const char *, clang::NamedDecl *>    PersistentDeclMap;
+    PersistentDeclMap                                           m_persistent_decls;         ///< Persistent entities declared by the user.
     
     ClangModulesDeclVendor::ModuleVector                    m_hand_loaded_clang_modules;    ///< These are Clang modules we hand-loaded; these are the highest-
                                                                                             ///< priority source for macros.




More information about the lldb-commits mailing list