[Lldb-commits] [lldb] d178213 - [lldb][NFC] Allow range-based for-loops on VariableList

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Mon Nov 25 06:04:31 PST 2019


Author: Raphael Isemann
Date: 2019-11-25T15:03:46+01:00
New Revision: d1782133d96d316c3bc98e33a191994794a26851

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

LOG: [lldb][NFC] Allow range-based for-loops on VariableList

Summary:
Adds support for doing range-based for-loops on LLDB's VariableList and
modernises all the index-based for-loops in LLDB where possible.

Reviewers: labath, jdoerfert

Reviewed By: labath

Subscribers: JDevlieghere, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D70668

Added: 
    

Modified: 
    lldb/include/lldb/Symbol/VariableList.h
    lldb/source/API/SBFrame.cpp
    lldb/source/API/SBModule.cpp
    lldb/source/API/SBTarget.cpp
    lldb/source/Commands/CommandObjectTarget.cpp
    lldb/source/Core/Address.cpp
    lldb/source/Core/IOHandler.cpp
    lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
    lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
    lldb/source/Symbol/Block.cpp
    lldb/source/Symbol/Variable.cpp
    lldb/source/Target/StackFrame.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Symbol/VariableList.h b/lldb/include/lldb/Symbol/VariableList.h
index 54d27583cd7b..87f98668a8a8 100644
--- a/lldb/include/lldb/Symbol/VariableList.h
+++ b/lldb/include/lldb/Symbol/VariableList.h
@@ -16,6 +16,8 @@
 namespace lldb_private {
 
 class VariableList {
+  typedef std::vector<lldb::VariableSP> collection;
+
 public:
   // Constructors and Destructors
   //  VariableList(const SymbolContext &symbol_context);
@@ -65,11 +67,15 @@ class VariableList {
   size_t GetSize() const;
   bool Empty() const { return m_variables.empty(); }
 
-protected:
-  typedef std::vector<lldb::VariableSP> collection;
   typedef collection::iterator iterator;
   typedef collection::const_iterator const_iterator;
 
+  iterator begin() { return m_variables.begin(); }
+  iterator end() { return m_variables.end(); }
+  const_iterator begin() const { return m_variables.begin(); }
+  const_iterator end() const { return m_variables.end(); }
+
+protected:
   collection m_variables;
 
 private:

diff  --git a/lldb/source/API/SBFrame.cpp b/lldb/source/API/SBFrame.cpp
index c0e272e1bcd4..af42be9ac75e 100644
--- a/lldb/source/API/SBFrame.cpp
+++ b/lldb/source/API/SBFrame.cpp
@@ -831,14 +831,12 @@ SBValueList SBFrame::GetVariables(const lldb::SBVariablesOptions &options) {
     if (stop_locker.TryLock(&process->GetRunLock())) {
       frame = exe_ctx.GetFramePtr();
       if (frame) {
-        size_t i;
         VariableList *variable_list = nullptr;
         variable_list = frame->GetVariableList(true);
         if (variable_list) {
           const size_t num_variables = variable_list->GetSize();
           if (num_variables) {
-            for (i = 0; i < num_variables; ++i) {
-              VariableSP variable_sp(variable_list->GetVariableAtIndex(i));
+            for (const VariableSP &variable_sp : *variable_list) {
               if (variable_sp) {
                 bool add_variable = false;
                 switch (variable_sp->GetScope()) {

diff  --git a/lldb/source/API/SBModule.cpp b/lldb/source/API/SBModule.cpp
index 6cc6d2628ace..7ac189bb4273 100644
--- a/lldb/source/API/SBModule.cpp
+++ b/lldb/source/API/SBModule.cpp
@@ -419,16 +419,12 @@ SBValueList SBModule::FindGlobalVariables(SBTarget &target, const char *name,
     VariableList variable_list;
     module_sp->FindGlobalVariables(ConstString(name), nullptr, max_matches,
                                    variable_list);
-    const uint32_t match_count = variable_list.GetSize();
-    if (match_count > 0) {
-      for (uint32_t i = 0; i < match_count; ++i) {
-        lldb::ValueObjectSP valobj_sp;
-        TargetSP target_sp(target.GetSP());
-        valobj_sp = ValueObjectVariable::Create(
-            target_sp.get(), variable_list.GetVariableAtIndex(i));
-        if (valobj_sp)
-          sb_value_list.Append(SBValue(valobj_sp));
-      }
+    for (const VariableSP &var_sp : variable_list) {
+      lldb::ValueObjectSP valobj_sp;
+      TargetSP target_sp(target.GetSP());
+      valobj_sp = ValueObjectVariable::Create(target_sp.get(), var_sp);
+      if (valobj_sp)
+        sb_value_list.Append(SBValue(valobj_sp));
     }
   }
 

diff  --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp
index bf444a72278a..7013e2b45e5f 100644
--- a/lldb/source/API/SBTarget.cpp
+++ b/lldb/source/API/SBTarget.cpp
@@ -1929,14 +1929,13 @@ SBValueList SBTarget::FindGlobalVariables(const char *name,
     VariableList variable_list;
     target_sp->GetImages().FindGlobalVariables(ConstString(name), max_matches,
                                                variable_list);
-    const uint32_t match_count = variable_list.GetSize();
-    if (match_count > 0) {
+    if (!variable_list.Empty()) {
       ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
       if (exe_scope == nullptr)
         exe_scope = target_sp.get();
-      for (uint32_t i = 0; i < match_count; ++i) {
-        lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
-            exe_scope, variable_list.GetVariableAtIndex(i)));
+      for (const VariableSP &var_sp : variable_list) {
+        lldb::ValueObjectSP valobj_sp(
+            ValueObjectVariable::Create(exe_scope, var_sp));
         if (valobj_sp)
           sb_value_list.Append(SBValue(valobj_sp));
       }
@@ -1961,7 +1960,6 @@ SBValueList SBTarget::FindGlobalVariables(const char *name,
     VariableList variable_list;
 
     std::string regexstr;
-    uint32_t match_count;
     switch (matchtype) {
     case eMatchTypeNormal:
       target_sp->GetImages().FindGlobalVariables(ConstString(name), max_matches,
@@ -1977,14 +1975,13 @@ SBValueList SBTarget::FindGlobalVariables(const char *name,
                                                  max_matches, variable_list);
       break;
     }
-    match_count = variable_list.GetSize();
-    if (match_count > 0) {
+    if (!variable_list.Empty()) {
       ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
       if (exe_scope == nullptr)
         exe_scope = target_sp.get();
-      for (uint32_t i = 0; i < match_count; ++i) {
-        lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
-            exe_scope, variable_list.GetVariableAtIndex(i)));
+      for (const VariableSP &var_sp : variable_list) {
+        lldb::ValueObjectSP valobj_sp(
+            ValueObjectVariable::Create(exe_scope, var_sp));
         if (valobj_sp)
           sb_value_list.Append(SBValue(valobj_sp));
       }

diff  --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp
index 90578d2b0092..d77207bb82cf 100644
--- a/lldb/source/Commands/CommandObjectTarget.cpp
+++ b/lldb/source/Commands/CommandObjectTarget.cpp
@@ -812,32 +812,29 @@ class CommandObjectTargetVariable : public CommandObjectParsed {
   void DumpGlobalVariableList(const ExecutionContext &exe_ctx,
                               const SymbolContext &sc,
                               const VariableList &variable_list, Stream &s) {
-    size_t count = variable_list.GetSize();
-    if (count > 0) {
-      if (sc.module_sp) {
-        if (sc.comp_unit) {
-          s.Printf("Global variables for %s in %s:\n",
-                   sc.comp_unit->GetPath().c_str(),
-                   sc.module_sp->GetFileSpec().GetPath().c_str());
-        } else {
-          s.Printf("Global variables for %s\n",
-                   sc.module_sp->GetFileSpec().GetPath().c_str());
-        }
-      } else if (sc.comp_unit) {
-        s.Printf("Global variables for %s\n", sc.comp_unit->GetPath().c_str());
+    if (variable_list.Empty())
+      return;
+    if (sc.module_sp) {
+      if (sc.comp_unit) {
+        s.Printf("Global variables for %s in %s:\n",
+                 sc.comp_unit->GetPath().c_str(),
+                 sc.module_sp->GetFileSpec().GetPath().c_str());
+      } else {
+        s.Printf("Global variables for %s\n",
+                 sc.module_sp->GetFileSpec().GetPath().c_str());
       }
+    } else if (sc.comp_unit) {
+      s.Printf("Global variables for %s\n", sc.comp_unit->GetPath().c_str());
+    }
 
-      for (uint32_t i = 0; i < count; ++i) {
-        VariableSP var_sp(variable_list.GetVariableAtIndex(i));
-        if (var_sp) {
-          ValueObjectSP valobj_sp(ValueObjectVariable::Create(
-              exe_ctx.GetBestExecutionContextScope(), var_sp));
+    for (VariableSP var_sp : variable_list) {
+      if (!var_sp)
+        continue;
+      ValueObjectSP valobj_sp(ValueObjectVariable::Create(
+          exe_ctx.GetBestExecutionContextScope(), var_sp));
 
-          if (valobj_sp)
-            DumpValueObject(s, var_sp, valobj_sp,
-                            var_sp->GetName().GetCString());
-        }
-      }
+      if (valobj_sp)
+        DumpValueObject(s, var_sp, valobj_sp, var_sp->GetName().GetCString());
     }
   }
 

diff  --git a/lldb/source/Core/Address.cpp b/lldb/source/Core/Address.cpp
index a3912bef5a6e..3313b5980901 100644
--- a/lldb/source/Core/Address.cpp
+++ b/lldb/source/Core/Address.cpp
@@ -712,22 +712,20 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
                                     [](Variable *) { return true; },
                                     &variable_list);
 
-          const size_t num_variables = variable_list.GetSize();
-          for (size_t var_idx = 0; var_idx < num_variables; ++var_idx) {
-            Variable *var = variable_list.GetVariableAtIndex(var_idx).get();
-            if (var && var->LocationIsValidForAddress(*this)) {
+          for (const VariableSP &var_sp : variable_list) {
+            if (var_sp && var_sp->LocationIsValidForAddress(*this)) {
               s->Indent();
               s->Printf("   Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"",
-                        var->GetID(), var->GetName().GetCString());
-              Type *type = var->GetType();
+                        var_sp->GetID(), var_sp->GetName().GetCString());
+              Type *type = var_sp->GetType();
               if (type)
                 s->Printf(", type = \"%s\"", type->GetName().GetCString());
               else
                 s->PutCString(", type = <unknown>");
               s->PutCString(", location = ");
-              var->DumpLocationForAddress(s, *this);
+              var_sp->DumpLocationForAddress(s, *this);
               s->PutCString(", decl = ");
-              var->GetDeclaration().DumpStopContext(s, false);
+              var_sp->GetDeclaration().DumpStopContext(s, false);
               s->EOL();
             }
           }

diff  --git a/lldb/source/Core/IOHandler.cpp b/lldb/source/Core/IOHandler.cpp
index 46be29f6fbf9..d11248094e05 100644
--- a/lldb/source/Core/IOHandler.cpp
+++ b/lldb/source/Core/IOHandler.cpp
@@ -3002,10 +3002,9 @@ class FrameVariablesWindowDelegate : public ValueObjectListDelegate {
         VariableList *locals = frame->GetVariableList(true);
         if (locals) {
           const DynamicValueType use_dynamic = eDynamicDontRunTarget;
-          const size_t num_locals = locals->GetSize();
-          for (size_t i = 0; i < num_locals; ++i) {
-            ValueObjectSP value_sp = frame->GetValueObjectForFrameVariable(
-                locals->GetVariableAtIndex(i), use_dynamic);
+          for (const VariableSP &local_sp : *locals) {
+            ValueObjectSP value_sp =
+                frame->GetValueObjectForFrameVariable(local_sp, use_dynamic);
             if (value_sp) {
               ValueObjectSP synthetic_value_sp = value_sp->GetSyntheticValue();
               if (synthetic_value_sp)

diff  --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
index 88534d07200f..4966ac1640fe 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
@@ -648,9 +648,7 @@ lldb::VariableSP ClangExpressionDeclMap::FindGlobalVariable(
 
   if (vars.GetSize()) {
     if (type) {
-      for (size_t i = 0; i < vars.GetSize(); ++i) {
-        VariableSP var_sp = vars.GetVariableAtIndex(i);
-
+      for (VariableSP var_sp : vars) {
         if (ClangASTContext::AreTypesSame(
                 *type, var_sp->GetType()->GetFullCompilerType()))
           return var_sp;

diff  --git a/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp b/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
index 5200749d759f..e612e15ee789 100644
--- a/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
+++ b/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
@@ -2240,8 +2240,7 @@ void RenderScriptRuntime::FindStructTypeName(Element &elem,
   // Iterate over all the global variables looking for one with a matching type
   // to the Element. We make the assumption a match exists since there needs to
   // be a global variable to reflect the struct type back into java host code.
-  for (uint32_t i = 0; i < var_list.GetSize(); ++i) {
-    const VariableSP var_sp(var_list.GetVariableAtIndex(i));
+  for (const VariableSP &var_sp : var_list) {
     if (!var_sp)
       continue;
 

diff  --git a/lldb/source/Symbol/Block.cpp b/lldb/source/Symbol/Block.cpp
index 77a4830dea7c..dad52c6502a3 100644
--- a/lldb/source/Symbol/Block.cpp
+++ b/lldb/source/Symbol/Block.cpp
@@ -406,11 +406,10 @@ Block::AppendBlockVariables(bool can_create, bool get_child_block_variables,
   uint32_t num_variables_added = 0;
   VariableList *block_var_list = GetBlockVariableList(can_create).get();
   if (block_var_list) {
-    for (size_t i = 0; i < block_var_list->GetSize(); ++i) {
-      VariableSP variable = block_var_list->GetVariableAtIndex(i);
-      if (filter(variable.get())) {
+    for (const VariableSP &var_sp : *block_var_list) {
+      if (filter(var_sp.get())) {
         num_variables_added++;
-        variable_list->AddVariable(variable);
+        variable_list->AddVariable(var_sp);
       }
     }
   }

diff  --git a/lldb/source/Symbol/Variable.cpp b/lldb/source/Symbol/Variable.cpp
index 3f3d7c198f15..427dbf459c4e 100644
--- a/lldb/source/Symbol/Variable.cpp
+++ b/lldb/source/Symbol/Variable.cpp
@@ -609,11 +609,8 @@ static void PrivateAutoComplete(
         VariableList *variable_list = frame->GetVariableList(get_file_globals);
 
         if (variable_list) {
-          const size_t num_variables = variable_list->GetSize();
-          for (size_t i = 0; i < num_variables; ++i) {
-            Variable *variable = variable_list->GetVariableAtIndex(i).get();
-            request.AddCompletion(variable->GetName().AsCString());
-          }
+          for (const VariableSP &var_sp : *variable_list)
+            request.AddCompletion(var_sp->GetName().AsCString());
         }
       }
     }
@@ -710,17 +707,15 @@ static void PrivateAutoComplete(
           if (!variable_list)
             break;
 
-          const size_t num_variables = variable_list->GetSize();
-          for (size_t i = 0; i < num_variables; ++i) {
-            Variable *variable = variable_list->GetVariableAtIndex(i).get();
+          for (VariableSP var_sp : *variable_list) {
 
-            if (!variable)
+            if (!var_sp)
               continue;
 
-            const char *variable_name = variable->GetName().AsCString();
+            const char *variable_name = var_sp->GetName().AsCString();
             if (strstr(variable_name, token.c_str()) == variable_name) {
               if (strcmp(variable_name, token.c_str()) == 0) {
-                Type *variable_type = variable->GetType();
+                Type *variable_type = var_sp->GetType();
                 if (variable_type) {
                   CompilerType variable_compiler_type(
                       variable_type->GetForwardCompilerType());

diff  --git a/lldb/source/Target/StackFrame.cpp b/lldb/source/Target/StackFrame.cpp
index 5e5a596e471d..5c6ea7a03933 100644
--- a/lldb/source/Target/StackFrame.cpp
+++ b/lldb/source/Target/StackFrame.cpp
@@ -573,8 +573,7 @@ ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
   if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
     // Check if any anonymous unions are there which contain a variable with
     // the name we need
-    for (size_t i = 0; i < variable_list->GetSize(); i++) {
-      VariableSP variable_sp = variable_list->GetVariableAtIndex(i);
+    for (const VariableSP &variable_sp : *variable_list) {
       if (!variable_sp)
         continue;
       if (!variable_sp->GetName().IsEmpty())
@@ -1529,11 +1528,9 @@ lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
              : Instruction::Operand::BuildDereference(
                    Instruction::Operand::BuildRegister(reg));
 
-  for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
-    VariableSP var_sp = variables.GetVariableAtIndex(vi);
-    if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
+  for (VariableSP var_sp : variables) {
+    if (var_sp->LocationExpression().MatchesOperand(frame, op))
       return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
-    }
   }
 
   const uint32_t current_inst =


        


More information about the lldb-commits mailing list