[Lldb-commits] [lldb] c34478f - [lldb][NFC] Move ClangExpressionDeclMap's persistent decl search into its own function

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Wed Nov 20 05:18:07 PST 2019


Author: Raphael Isemann
Date: 2019-11-20T14:17:35+01:00
New Revision: c34478f5f6c7ef1ae8fb3605fbdec0634d543fed

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

LOG: [lldb][NFC] Move ClangExpressionDeclMap's persistent decl search into its own function

Searching persistent decls is a small subset of the things
FindExternalVisibleDecls does. It should be its own function instead
of being encapsulated in this `do { } while(false);` pattern.

Added: 
    

Modified: 
    lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
    lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
index f8e448ffcb6f..da106f4f8f44 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
@@ -771,19 +771,64 @@ void ClangExpressionDeclMap::FindExternalVisibleDecls(
   ClangASTSource::FindExternalVisibleDecls(context);
 }
 
+void ClangExpressionDeclMap::MaybeRegisterFunctionBody(
+    FunctionDecl *copied_function_decl) {
+  if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) {
+    clang::DeclGroupRef decl_group_ref(copied_function_decl);
+    m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
+  }
+}
+
+void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context,
+                                                  const ConstString name,
+                                                  unsigned int current_id) {
+  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
+  Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
+  if (!target)
+    return;
+
+  ClangASTContext *scratch_clang_ast_context =
+      target->GetScratchClangASTContext();
+
+  if (!scratch_clang_ast_context)
+    return;
+
+  ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
+
+  if (!scratch_ast_context)
+    return;
+
+  NamedDecl *persistent_decl =
+      m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
+
+  if (!persistent_decl)
+    return;
+
+  Decl *parser_persistent_decl = CopyDecl(persistent_decl);
+
+  if (!parser_persistent_decl)
+    return;
+
+  NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
+
+  if (!parser_named_decl)
+    return;
+
+  if (clang::FunctionDecl *parser_function_decl =
+          llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
+    MaybeRegisterFunctionBody(parser_function_decl);
+  }
+
+  LLDB_LOGF(log, "  CEDM::FEVD[%u] Found persistent decl %s", current_id,
+            name.GetCString());
+
+  context.AddNamedDecl(parser_named_decl);
+}
 void ClangExpressionDeclMap::FindExternalVisibleDecls(
     NameSearchContext &context, lldb::ModuleSP module_sp,
     CompilerDeclContext &namespace_decl, 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));
 
   SymbolContextList sc_list;
@@ -802,51 +847,8 @@ void ClangExpressionDeclMap::FindExternalVisibleDecls(
                                       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 = CopyDecl(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);
-      }
-
-      LLDB_LOGF(log, "  CEDM::FEVD[%u] Found persistent decl %s", current_id,
-                name.GetCString());
-
-      context.AddNamedDecl(parser_named_decl);
-    } while (false);
-  }
+  if (!namespace_decl)
+    SearchPersistenDecls(context, name, current_id);
 
   if (name.GetCString()[0] == '$' && !namespace_decl) {
     static ConstString g_lldb_class_name("$__lldb_class");

diff  --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h
index 060067c35725..4b80248ceac5 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h
@@ -372,6 +372,24 @@ class ClangExpressionDeclMap : public ClangASTSource {
   /// from persistent variables.
   uint64_t GetParserID() { return (uint64_t) this; }
 
+  /// Should be called on all copied functions.
+  void MaybeRegisterFunctionBody(clang::FunctionDecl *copied_function_decl);
+
+  /// Searches the persistent decls of the target for entities with the
+  /// given name.
+  ///
+  /// \param[in] context
+  ///     The NameSearchContext that can construct Decls for this name.
+  ///
+  /// \param[in] name
+  ///     The name of the entities that need to be found.
+  ///
+  /// \param[in] current_id
+  ///     The ID for the current FindExternalVisibleDecls invocation,
+  ///     for logging purposes.
+  void SearchPersistenDecls(NameSearchContext &context, const ConstString name,
+                            unsigned int current_id);
+
   /// Given a target, find a variable that matches the given name and type.
   ///
   /// \param[in] target


        


More information about the lldb-commits mailing list