Skip to content

Commit 38f744a

Browse files
Merge pull request #7995 from adrian-prantl/120553412
[lldb] DWARFDIE: Follow DW_AT_specification when computing CompilerCo…
2 parents 46f63f9 + 6c8d23e commit 38f744a

File tree

6 files changed

+67
-21
lines changed

6 files changed

+67
-21
lines changed

lldb/include/lldb/Core/Module.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ class Module : public std::enable_shared_from_this<Module>,
339339
const ModuleFunctionSearchOptions &options,
340340
SymbolContextList &sc_list);
341341

342+
/// Find functions by compiler context.
343+
void FindFunctions(llvm::ArrayRef<CompilerContext> compiler_ctx,
344+
lldb::FunctionNameType name_type_mask,
345+
const ModuleFunctionSearchOptions &options,
346+
SymbolContextList &sc_list);
347+
342348
/// Find functions by name.
343349
///
344350
/// If the function is an inlined function, it will have a block,

lldb/source/Core/Module.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,23 @@ void Module::FindFunctions(ConstString name,
892892
}
893893
}
894894

895+
void Module::FindFunctions(llvm::ArrayRef<CompilerContext> compiler_ctx,
896+
FunctionNameType name_type_mask,
897+
const ModuleFunctionSearchOptions &options,
898+
SymbolContextList &sc_list) {
899+
if (compiler_ctx.empty() ||
900+
compiler_ctx.back().kind != CompilerContextKind::Function)
901+
return;
902+
ConstString name = compiler_ctx.back().name;
903+
SymbolContextList unfiltered;
904+
FindFunctions(name, CompilerDeclContext(), name_type_mask, options,
905+
unfiltered);
906+
// Filter by context.
907+
for (auto &sc : unfiltered)
908+
if (sc.function && compiler_ctx.equals(sc.function->GetCompilerContext()))
909+
sc_list.Append(sc);
910+
}
911+
895912
void Module::FindFunctions(const RegularExpression &regex,
896913
const ModuleFunctionSearchOptions &options,
897914
SymbolContextList &sc_list) {

lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -380,51 +380,63 @@ std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
380380
return result;
381381
}
382382

383-
std::vector<lldb_private::CompilerContext> DWARFDIE::GetDeclContext() const {
383+
static std::vector<lldb_private::CompilerContext>
384+
GetDeclContextImpl(llvm::SmallSet<lldb::user_id_t, 4> &seen, DWARFDIE die) {
384385
std::vector<lldb_private::CompilerContext> context;
385-
const dw_tag_t tag = Tag();
386-
if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
386+
// Stop if we hit a cycle.
387+
if (!die || !seen.insert(die.GetID()).second)
387388
return context;
388-
DWARFDIE parent = GetParent();
389-
if (parent)
390-
context = parent.GetDeclContext();
389+
390+
// Handle outline member function DIEs by following the specification.
391+
if (DWARFDIE spec = die.GetReferencedDIE(DW_AT_specification))
392+
return GetDeclContextImpl(seen, spec);
393+
394+
// Get the parent context chain.
395+
context = GetDeclContextImpl(seen, die.GetParent());
396+
397+
// Add this DIE's contribution at the end of the chain.
391398
auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
392399
context.push_back({kind, ConstString(name)});
393400
};
394-
switch (tag) {
401+
switch (die.Tag()) {
395402
case DW_TAG_module:
396-
push_ctx(CompilerContextKind::Module, GetName());
403+
push_ctx(CompilerContextKind::Module, die.GetName());
397404
break;
398405
case DW_TAG_namespace:
399-
push_ctx(CompilerContextKind::Namespace, GetName());
406+
push_ctx(CompilerContextKind::Namespace, die.GetName());
400407
break;
401408
case DW_TAG_structure_type:
402-
push_ctx(CompilerContextKind::Struct, GetName());
409+
push_ctx(CompilerContextKind::Struct, die.GetName());
403410
break;
404411
case DW_TAG_union_type:
405-
push_ctx(CompilerContextKind::Union, GetName());
412+
push_ctx(CompilerContextKind::Union, die.GetName());
406413
break;
407414
case DW_TAG_class_type:
408-
push_ctx(CompilerContextKind::Class, GetName());
415+
push_ctx(CompilerContextKind::Class, die.GetName());
409416
break;
410417
case DW_TAG_enumeration_type:
411-
push_ctx(CompilerContextKind::Enum, GetName());
418+
push_ctx(CompilerContextKind::Enum, die.GetName());
412419
break;
413420
case DW_TAG_subprogram:
414-
push_ctx(CompilerContextKind::Function, GetPubname());
421+
push_ctx(CompilerContextKind::Function, die.GetName());
415422
break;
416423
case DW_TAG_variable:
417-
push_ctx(CompilerContextKind::Variable, GetPubname());
424+
push_ctx(CompilerContextKind::Variable, die.GetPubname());
418425
break;
419426
case DW_TAG_typedef:
420-
push_ctx(CompilerContextKind::Typedef, GetName());
427+
push_ctx(CompilerContextKind::Typedef, die.GetName());
421428
break;
422429
default:
423430
break;
424431
}
425432
return context;
426433
}
427434

435+
std::vector<lldb_private::CompilerContext> DWARFDIE::GetDeclContext() const {
436+
llvm::SmallSet<lldb::user_id_t, 4> seen;
437+
return GetDeclContextImpl(seen, *this);
438+
}
439+
428440
std::vector<lldb_private::CompilerContext>
429441
DWARFDIE::GetTypeLookupContext() const {
430442
std::vector<lldb_private::CompilerContext> context;

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,11 +2489,12 @@ void SymbolFileDWARF::FindFunctions(const Module::LookupInfo &lookup_info,
24892489

24902490
Module::LookupInfo no_tp_lookup_info(lookup_info);
24912491
no_tp_lookup_info.SetLookupName(ConstString(name_no_template_params));
2492-
m_index->GetFunctions(no_tp_lookup_info, *this, parent_decl_ctx, [&](DWARFDIE die) {
2493-
if (resolved_dies.insert(die.GetDIE()).second)
2494-
ResolveFunction(die, include_inlines, sc_list);
2495-
return true;
2496-
});
2492+
m_index->GetFunctions(no_tp_lookup_info, *this, parent_decl_ctx,
2493+
[&](DWARFDIE die) {
2494+
if (resolved_dies.insert(die.GetDIE()).second)
2495+
ResolveFunction(die, include_inlines, sc_list);
2496+
return true;
2497+
});
24972498
}
24982499
}
24992500

lldb/test/Shell/SymbolFile/DWARF/x86/find-basic-function.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
// RUN: FileCheck --check-prefix=FULL-MANGLED-METHOD %s
3535
// RUN: lldb-test symbols --name=foo --context=context --find=function --function-flags=base %t | \
3636
// RUN: FileCheck --check-prefix=CONTEXT %s
37+
// RUN: lldb-test symbols --compiler-context=Struct:sbar,Function:foo -language=c++ -find=function -function-flags=method %t | \
38+
// RUN: FileCheck --check-prefix=COMPILER-CONTEXT %s
3739
// RUN: lldb-test symbols --name=not_there --find=function %t | \
3840
// RUN: FileCheck --check-prefix=EMPTY %s
3941

@@ -84,6 +86,10 @@
8486
// CONTEXT: Found 1 functions:
8587
// CONTEXT-DAG: name = "bar::foo()", mangled = "_ZN3bar3fooEv", decl_context = {Namespace(bar)}
8688

89+
// COMPILER-CONTEXT: Found 2 functions:
90+
// COMPILER-CONTEXT-DAG: name = "sbar::foo()", mangled = "_ZN4sbar3fooEv"
91+
// COMPILER-CONTEXT-DAG: name = "sbar::foo(int)", mangled = "_ZN4sbar3fooEi"
92+
8793
// EMPTY: Found 0 functions:
8894

8995
void foo() {}

lldb/tools/lldb-test/lldb-test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ static lldb::DescriptionLevel GetDescriptionLevel() {
466466
Error opts::symbols::findFunctions(lldb_private::Module &Module) {
467467
SymbolFile &Symfile = *Module.GetSymbolFile();
468468
SymbolContextList List;
469+
auto compiler_context = parseCompilerContext();
469470
if (!File.empty()) {
470471
assert(Line != 0);
471472

@@ -498,6 +499,9 @@ Error opts::symbols::findFunctions(lldb_private::Module &Module) {
498499
assert(RE.IsValid());
499500
List.Clear();
500501
Symfile.FindFunctions(RE, true, List);
502+
} else if (!compiler_context.empty()) {
503+
List.Clear();
504+
Module.FindFunctions(compiler_context, getFunctionNameFlags(), {}, List);
501505
} else {
502506
Expected<CompilerDeclContext> ContextOr = getDeclContext(Symfile);
503507
if (!ContextOr)

0 commit comments

Comments
 (0)