Skip to content

Commit 7fa976d

Browse files
committed
[lldb][NFC] Move searching local variables into own function
1 parent 2e298a6 commit 7fa976d

File tree

2 files changed

+74
-39
lines changed

2 files changed

+74
-39
lines changed

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,53 @@ void ClangExpressionDeclMap::LookupInModulesDeclVendor(
11691169
}
11701170
}
11711171

1172+
bool ClangExpressionDeclMap::LookupLocalVariable(
1173+
NameSearchContext &context, ConstString name, unsigned current_id,
1174+
SymbolContext &sym_ctx, CompilerDeclContext &namespace_decl) {
1175+
ValueObjectSP valobj;
1176+
VariableSP var;
1177+
1178+
StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1179+
CompilerDeclContext compiler_decl_context =
1180+
sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext()
1181+
: CompilerDeclContext();
1182+
1183+
if (compiler_decl_context) {
1184+
// Make sure that the variables are parsed so that we have the
1185+
// declarations.
1186+
VariableListSP vars = frame->GetInScopeVariableList(true);
1187+
for (size_t i = 0; i < vars->GetSize(); i++)
1188+
vars->GetVariableAtIndex(i)->GetDecl();
1189+
1190+
// Search for declarations matching the name. Do not include imported
1191+
// decls in the search if we are looking for decls in the artificial
1192+
// namespace $__lldb_local_vars.
1193+
std::vector<CompilerDecl> found_decls =
1194+
compiler_decl_context.FindDeclByName(name, namespace_decl.IsValid());
1195+
1196+
bool variable_found = false;
1197+
for (CompilerDecl decl : found_decls) {
1198+
for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
1199+
VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1200+
if (candidate_var->GetDecl() == decl) {
1201+
var = candidate_var;
1202+
break;
1203+
}
1204+
}
1205+
1206+
if (var && !variable_found) {
1207+
variable_found = true;
1208+
valobj = ValueObjectVariable::Create(frame, var);
1209+
AddOneVariable(context, var, valobj, current_id);
1210+
context.m_found.variable = true;
1211+
}
1212+
}
1213+
if (variable_found)
1214+
return true;
1215+
}
1216+
return false;
1217+
}
1218+
11721219
void ClangExpressionDeclMap::FindExternalVisibleDecls(
11731220
NameSearchContext &context, lldb::ModuleSP module_sp,
11741221
CompilerDeclContext &namespace_decl, unsigned int current_id) {
@@ -1247,46 +1294,10 @@ void ClangExpressionDeclMap::FindExternalVisibleDecls(
12471294
bool local_var_lookup =
12481295
!namespace_decl || (namespace_decl.GetName() ==
12491296
ConstString(g_lldb_local_vars_namespace_cstr));
1250-
if (frame && local_var_lookup) {
1251-
CompilerDeclContext compiler_decl_context =
1252-
sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext()
1253-
: CompilerDeclContext();
1254-
1255-
if (compiler_decl_context) {
1256-
// Make sure that the variables are parsed so that we have the
1257-
// declarations.
1258-
VariableListSP vars = frame->GetInScopeVariableList(true);
1259-
for (size_t i = 0; i < vars->GetSize(); i++)
1260-
vars->GetVariableAtIndex(i)->GetDecl();
1261-
1262-
// Search for declarations matching the name. Do not include imported
1263-
// decls in the search if we are looking for decls in the artificial
1264-
// namespace $__lldb_local_vars.
1265-
std::vector<CompilerDecl> found_decls =
1266-
compiler_decl_context.FindDeclByName(name,
1267-
namespace_decl.IsValid());
1268-
1269-
bool variable_found = false;
1270-
for (CompilerDecl decl : found_decls) {
1271-
for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
1272-
VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1273-
if (candidate_var->GetDecl() == decl) {
1274-
var = candidate_var;
1275-
break;
1276-
}
1277-
}
1297+
if (frame && local_var_lookup)
1298+
if (LookupLocalVariable(context, name, current_id, sym_ctx, namespace_decl))
1299+
return;
12781300

1279-
if (var && !variable_found) {
1280-
variable_found = true;
1281-
valobj = ValueObjectVariable::Create(frame, var);
1282-
AddOneVariable(context, var, valobj, current_id);
1283-
context.m_found.variable = true;
1284-
}
1285-
}
1286-
if (variable_found)
1287-
return;
1288-
}
1289-
}
12901301
if (target) {
12911302
var = FindGlobalVariable(*target, module_sp, name, &namespace_decl,
12921303
nullptr);

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,30 @@ class ClangExpressionDeclMap : public ClangASTSource {
434434
void LookupInModulesDeclVendor(NameSearchContext &context, ConstString name,
435435
unsigned current_id);
436436

437+
/// Looks up a local variable.
438+
///
439+
/// \param[in] context
440+
/// The NameSearchContext that can construct Decls for this name.
441+
///
442+
/// \param[in] name
443+
/// The name of the entities that need to be found.
444+
///
445+
/// \param[in] current_id
446+
/// The ID for the current FindExternalVisibleDecls invocation,
447+
/// for logging purposes.
448+
///
449+
/// \param[in] sym_ctx
450+
/// The current SymbolContext of this frame.
451+
///
452+
/// \param[in] namespace_decl
453+
/// The parent namespace if there is one.
454+
///
455+
/// \return
456+
/// True iff a local variable was found.
457+
bool LookupLocalVariable(NameSearchContext &context, ConstString name,
458+
unsigned current_id, SymbolContext &sym_ctx,
459+
CompilerDeclContext &namespace_decl);
460+
437461
/// Given a target, find a variable that matches the given name and type.
438462
///
439463
/// \param[in] target

0 commit comments

Comments
 (0)