[Lldb-commits] [lldb] 7fa976d - [lldb][NFC] Move searching local variables into own function

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 21 03:46:46 PST 2019


Author: Raphael Isemann
Date: 2019-11-21T12:45:38+01:00
New Revision: 7fa976d57a1e2ab735212e5d9fc13cc38c4c81e9

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

LOG: [lldb][NFC] Move searching local variables into own function

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 a4ca7cb6cc7b..50e76be8f31b 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
@@ -1169,6 +1169,53 @@ void ClangExpressionDeclMap::LookupInModulesDeclVendor(
   }
 }
 
+bool ClangExpressionDeclMap::LookupLocalVariable(
+    NameSearchContext &context, ConstString name, unsigned current_id,
+    SymbolContext &sym_ctx, CompilerDeclContext &namespace_decl) {
+  ValueObjectSP valobj;
+  VariableSP var;
+
+  StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
+  CompilerDeclContext compiler_decl_context =
+      sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext()
+                               : CompilerDeclContext();
+
+  if (compiler_decl_context) {
+    // Make sure that the variables are parsed so that we have the
+    // declarations.
+    VariableListSP vars = frame->GetInScopeVariableList(true);
+    for (size_t i = 0; i < vars->GetSize(); i++)
+      vars->GetVariableAtIndex(i)->GetDecl();
+
+    // Search for declarations matching the name. Do not include imported
+    // decls in the search if we are looking for decls in the artificial
+    // namespace $__lldb_local_vars.
+    std::vector<CompilerDecl> found_decls =
+        compiler_decl_context.FindDeclByName(name, namespace_decl.IsValid());
+
+    bool variable_found = false;
+    for (CompilerDecl decl : found_decls) {
+      for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
+        VariableSP candidate_var = vars->GetVariableAtIndex(vi);
+        if (candidate_var->GetDecl() == decl) {
+          var = candidate_var;
+          break;
+        }
+      }
+
+      if (var && !variable_found) {
+        variable_found = true;
+        valobj = ValueObjectVariable::Create(frame, var);
+        AddOneVariable(context, var, valobj, current_id);
+        context.m_found.variable = true;
+      }
+    }
+    if (variable_found)
+      return true;
+  }
+  return false;
+}
+
 void ClangExpressionDeclMap::FindExternalVisibleDecls(
     NameSearchContext &context, lldb::ModuleSP module_sp,
     CompilerDeclContext &namespace_decl, unsigned int current_id) {
@@ -1247,46 +1294,10 @@ void ClangExpressionDeclMap::FindExternalVisibleDecls(
   bool local_var_lookup =
       !namespace_decl || (namespace_decl.GetName() ==
                           ConstString(g_lldb_local_vars_namespace_cstr));
-  if (frame && local_var_lookup) {
-    CompilerDeclContext compiler_decl_context =
-        sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext()
-                                 : CompilerDeclContext();
-
-    if (compiler_decl_context) {
-      // Make sure that the variables are parsed so that we have the
-      // declarations.
-      VariableListSP vars = frame->GetInScopeVariableList(true);
-      for (size_t i = 0; i < vars->GetSize(); i++)
-        vars->GetVariableAtIndex(i)->GetDecl();
-
-      // Search for declarations matching the name. Do not include imported
-      // decls in the search if we are looking for decls in the artificial
-      // namespace $__lldb_local_vars.
-      std::vector<CompilerDecl> found_decls =
-          compiler_decl_context.FindDeclByName(name,
-                                               namespace_decl.IsValid());
-
-      bool variable_found = false;
-      for (CompilerDecl decl : found_decls) {
-        for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
-          VariableSP candidate_var = vars->GetVariableAtIndex(vi);
-          if (candidate_var->GetDecl() == decl) {
-            var = candidate_var;
-            break;
-          }
-        }
+  if (frame && local_var_lookup)
+    if (LookupLocalVariable(context, name, current_id, sym_ctx, namespace_decl))
+      return;
 
-        if (var && !variable_found) {
-          variable_found = true;
-          valobj = ValueObjectVariable::Create(frame, var);
-          AddOneVariable(context, var, valobj, current_id);
-          context.m_found.variable = true;
-        }
-      }
-      if (variable_found)
-        return;
-    }
-  }
   if (target) {
     var = FindGlobalVariable(*target, module_sp, name, &namespace_decl,
                              nullptr);

diff  --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h
index 1f2677048392..2abb182e7798 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h
@@ -434,6 +434,30 @@ class ClangExpressionDeclMap : public ClangASTSource {
   void LookupInModulesDeclVendor(NameSearchContext &context, ConstString name,
                                  unsigned current_id);
 
+  /// Looks up a local variable.
+  ///
+  /// \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.
+  ///
+  /// \param[in] sym_ctx
+  ///     The current SymbolContext of this frame.
+  ///
+  /// \param[in] namespace_decl
+  ///     The parent namespace if there is one.
+  ///
+  /// \return
+  ///    True iff a local variable was found.
+  bool LookupLocalVariable(NameSearchContext &context, ConstString name,
+                           unsigned current_id, SymbolContext &sym_ctx,
+                           CompilerDeclContext &namespace_decl);
+
   /// Given a target, find a variable that matches the given name and type.
   ///
   /// \param[in] target


        


More information about the lldb-commits mailing list