[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
This commit is contained in:
Raphael Isemann
2019-11-25 15:03:46 +01:00
parent d9c9a4e48d
commit d1782133d9
12 changed files with 66 additions and 87 deletions

View File

@@ -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 @@ public:
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:

View File

@@ -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()) {

View File

@@ -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));
}
}

View File

@@ -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));
}

View File

@@ -812,32 +812,29 @@ protected:
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());
}
}

View File

@@ -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();
}
}

View File

@@ -3002,10 +3002,9 @@ public:
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)

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}
}
}

View File

@@ -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());

View File

@@ -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 =