Skip to content

Commit 409f3c9

Browse files
committed
Expose DWARFDIE::GetDeclContext() in lldb_private::Function.
I need this API in the Swift plugin, but it seems generally useful enough to expose it in the main branch. (cherry picked from commit b60b12b) Conflicts: lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
1 parent 0169bfc commit 409f3c9

File tree

15 files changed

+101
-64
lines changed

15 files changed

+101
-64
lines changed

lldb/include/lldb/Symbol/Function.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,12 @@ class Function : public UserID, public SymbolContextScope {
547547
/// The DeclContext, or NULL if none exists.
548548
CompilerDeclContext GetDeclContext();
549549

550+
/// Get the CompilerContext for this function, if available.
551+
///
552+
/// \return
553+
/// The CompilerContext, or an empty vector if none is available.
554+
std::vector<CompilerContext> GetCompilerContext();
555+
550556
/// Get accessor for the type that describes the function return value type,
551557
/// and parameter types.
552558
///

lldb/include/lldb/Symbol/SymbolFile.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,16 @@ class SymbolFile : public PluginInterface {
228228

229229
virtual bool CompleteType(CompilerType &compiler_type) = 0;
230230
virtual void ParseDeclsForContext(CompilerDeclContext decl_ctx) {}
231-
virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) {
232-
return CompilerDecl();
233-
}
231+
virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) { return {}; }
234232
virtual CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) {
235-
return CompilerDeclContext();
233+
return {};
236234
}
237235
virtual CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) {
238-
return CompilerDeclContext();
236+
return {};
237+
}
238+
virtual std::vector<CompilerContext>
239+
GetCompilerContextForUID(lldb::user_id_t uid) {
240+
return {};
239241
}
240242
virtual void
241243
GetDeclContextForUID(llvm::SmallVectorImpl<CompilerContext> &context,

lldb/include/lldb/Symbol/Type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct CompilerContext {
3434
}
3535
bool operator!=(const CompilerContext &rhs) const { return !(*this == rhs); }
3636

37-
void Dump() const;
37+
void Dump(Stream &s) const;
3838

3939
CompilerContextKind kind;
4040
ConstString name;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ TypeSP DWARFASTParserClang::ParseTypeFromClangModule(const SymbolContext &sc,
143143
// If this type comes from a Clang module, recursively look in the
144144
// DWARF section of the .pcm file in the module cache. Clang
145145
// generates DWO skeleton units as breadcrumbs to find them.
146-
llvm::SmallVector<CompilerContext, 4> decl_context;
147-
die.GetDeclContext(decl_context);
146+
std::vector<CompilerContext> decl_context = die.GetDeclContext();
148147
TypeMap pcm_types;
149148

150149
// The type in the Clang module must have the same language as the current CU.
@@ -2276,15 +2275,15 @@ CompilerDecl DWARFASTParserClang::GetDeclForUIDFromDWARF(const DWARFDIE &die) {
22762275
clang::Decl *clang_decl = GetClangDeclForDIE(die);
22772276
if (clang_decl != nullptr)
22782277
return m_ast.GetCompilerDecl(clang_decl);
2279-
return CompilerDecl();
2278+
return {};
22802279
}
22812280

22822281
CompilerDeclContext
22832282
DWARFASTParserClang::GetDeclContextForUIDFromDWARF(const DWARFDIE &die) {
22842283
clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE(die);
22852284
if (clang_decl_ctx)
22862285
return m_ast.CreateDeclContext(clang_decl_ctx);
2287-
return CompilerDeclContext();
2286+
return {};
22882287
}
22892288

22902289
CompilerDeclContext
@@ -2293,7 +2292,7 @@ DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) {
22932292
GetClangDeclContextContainingDIE(die, nullptr);
22942293
if (clang_decl_ctx)
22952294
return m_ast.CreateDeclContext(clang_decl_ctx);
2296-
return CompilerDeclContext();
2295+
return {};
22972296
}
22982297

22992298
size_t DWARFASTParserClang::ParseChildEnumerators(

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

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

383-
void DWARFDIE::GetDeclContext(
384-
llvm::SmallVectorImpl<lldb_private::CompilerContext> &context) const {
383+
std::vector<lldb_private::CompilerContext> DWARFDIE::GetDeclContext() const {
384+
std::vector<lldb_private::CompilerContext> context;
385385
const dw_tag_t tag = Tag();
386386
if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
387-
return;
387+
return context;
388388
DWARFDIE parent = GetParent();
389389
if (parent)
390-
parent.GetDeclContext(context);
390+
context = parent.GetDeclContext();
391+
auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
392+
context.push_back({kind, ConstString(name)});
393+
};
391394
switch (tag) {
392395
case DW_TAG_module:
393-
context.push_back({CompilerContextKind::Module, ConstString(GetName())});
396+
push_ctx(CompilerContextKind::Module, GetName());
394397
break;
395398
case DW_TAG_namespace:
396-
context.push_back({CompilerContextKind::Namespace, ConstString(GetName())});
399+
push_ctx(CompilerContextKind::Namespace, GetName());
397400
break;
398401
case DW_TAG_structure_type:
399-
context.push_back({CompilerContextKind::Struct, ConstString(GetName())});
402+
push_ctx(CompilerContextKind::Struct, GetName());
400403
break;
401404
case DW_TAG_union_type:
402-
context.push_back({CompilerContextKind::Union, ConstString(GetName())});
405+
push_ctx(CompilerContextKind::Union, GetName());
403406
break;
404407
case DW_TAG_class_type:
405-
context.push_back({CompilerContextKind::Class, ConstString(GetName())});
408+
push_ctx(CompilerContextKind::Class, GetName());
406409
break;
407410
case DW_TAG_enumeration_type:
408-
context.push_back({CompilerContextKind::Enum, ConstString(GetName())});
411+
push_ctx(CompilerContextKind::Enum, GetName());
409412
break;
410413
case DW_TAG_subprogram:
411-
context.push_back(
412-
{CompilerContextKind::Function, ConstString(GetPubname())});
414+
push_ctx(CompilerContextKind::Function, GetPubname());
413415
break;
414416
case DW_TAG_variable:
415-
context.push_back(
416-
{CompilerContextKind::Variable, ConstString(GetPubname())});
417+
push_ctx(CompilerContextKind::Variable, GetPubname());
417418
break;
418419
case DW_TAG_typedef:
419-
context.push_back({CompilerContextKind::Typedef, ConstString(GetName())});
420+
push_ctx(CompilerContextKind::Typedef, GetName());
420421
break;
421422
default:
422423
break;
423424
}
425+
return context;
424426
}
425427

426428
DWARFDIE

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class DWARFDIE : public DWARFBaseDIE {
7676

7777
/// Return this DIE's decl context as it is needed to look up types
7878
/// in Clang's -gmodules debug info format.
79-
void GetDeclContext(llvm::SmallVectorImpl<CompilerContext> &context) const;
79+
std::vector<CompilerContext> GetDeclContext() const;
8080

8181
// Getting attribute values from the DIE.
8282
//

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,10 +1466,15 @@ SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
14661466
return CompilerDeclContext();
14671467
}
14681468

1469-
void SymbolFileDWARF::GetDeclContextForUID(
1470-
llvm::SmallVectorImpl<CompilerContext> &context, lldb::user_id_t type_uid) {
1469+
std::vector<CompilerContext>
1470+
SymbolFileDWARF::GetCompilerContextForUID(lldb::user_id_t type_uid) {
1471+
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1472+
// Anytime we have a lldb::user_id_t, we must get the DIE by calling
1473+
// SymbolFileDWARF::GetDIE(). See comments inside the
1474+
// SymbolFileDWARF::GetDIE() for details.
14711475
if (DWARFDIE die = GetDIE(type_uid))
1472-
return die.GetDeclContext(context);
1476+
return die.GetDeclContext();
1477+
return {};
14731478
}
14741479

14751480
Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
@@ -2646,8 +2651,7 @@ void SymbolFileDWARF::FindTypes(
26462651
if (!languages[GetLanguageFamily(*die.GetCU())])
26472652
return true;
26482653

2649-
llvm::SmallVector<CompilerContext, 4> die_context;
2650-
die.GetDeclContext(die_context);
2654+
std::vector<CompilerContext> die_context = die.GetDeclContext();
26512655
if (!contextMatches(die_context, pattern))
26522656
return true;
26532657

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ class SymbolFileDWARF : public SymbolFileCommon {
161161

162162
CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) override;
163163

164+
std::vector<CompilerContext>
165+
GetCompilerContextForUID(lldb::user_id_t uid) override;
166+
164167
void ParseDeclsForContext(CompilerDeclContext decl_ctx) override;
165168

166169
uint32_t ResolveSymbolContext(const Address &so_addr,

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,19 +1366,25 @@ void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf,
13661366
CompilerDeclContext
13671367
SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) {
13681368
const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1369-
SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1370-
if (oso_dwarf)
1369+
if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
13711370
return oso_dwarf->GetDeclContextForUID(type_uid);
1372-
return CompilerDeclContext();
1371+
return {};
13731372
}
13741373

13751374
CompilerDeclContext
13761375
SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
13771376
const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1378-
SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1379-
if (oso_dwarf)
1377+
if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
13801378
return oso_dwarf->GetDeclContextContainingUID(type_uid);
1381-
return CompilerDeclContext();
1379+
return {};
1380+
}
1381+
1382+
std::vector<CompilerContext>
1383+
SymbolFileDWARFDebugMap::GetCompilerContextForUID(lldb::user_id_t type_uid) {
1384+
const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1385+
if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
1386+
return oso_dwarf->GetCompilerContextForUID(type_uid);
1387+
return {};
13821388
}
13831389

13841390
void SymbolFileDWARFDebugMap::ParseDeclsForContext(

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class SymbolFileDWARFDebugMap : public SymbolFileCommon {
9494

9595
CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) override;
9696
CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) override;
97+
std::vector<CompilerContext>
98+
GetCompilerContextForUID(lldb::user_id_t uid) override;
9799
void ParseDeclsForContext(CompilerDeclContext decl_ctx) override;
98100

99101
bool CompleteType(CompilerType &compiler_type) override;

lldb/source/Symbol/Function.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,15 @@ void Function::GetDescription(Stream *s, lldb::DescriptionLevel level,
377377
s->AsRawOstream() << ", name = \"" << name << '"';
378378
if (mangled)
379379
s->AsRawOstream() << ", mangled = \"" << mangled << '"';
380+
if (level == eDescriptionLevelVerbose) {
381+
*s << ", decl_context = {";
382+
auto decl_context = GetCompilerContext();
383+
// Drop the function itself from the context chain.
384+
if (decl_context.size())
385+
decl_context.pop_back();
386+
llvm::interleaveComma(decl_context, *s, [&](auto &ctx) { ctx.Dump(*s); });
387+
*s << "}";
388+
}
380389
*s << ", range = ";
381390
Address::DumpStyle fallback_style;
382391
if (level == eDescriptionLevelVerbose)
@@ -496,13 +505,17 @@ ConstString Function::GetDisplayName(const SymbolContext *sc) const {
496505
}
497506

498507
CompilerDeclContext Function::GetDeclContext() {
499-
ModuleSP module_sp = CalculateSymbolContextModule();
500-
501-
if (module_sp) {
508+
if (ModuleSP module_sp = CalculateSymbolContextModule())
502509
if (SymbolFile *sym_file = module_sp->GetSymbolFile())
503510
return sym_file->GetDeclContextForUID(GetID());
504-
}
505-
return CompilerDeclContext();
511+
return {};
512+
}
513+
514+
std::vector<CompilerContext> Function::GetCompilerContext() {
515+
if (ModuleSP module_sp = CalculateSymbolContextModule())
516+
if (SymbolFile *sym_file = module_sp->GetSymbolFile())
517+
return sym_file->GetCompilerContextForUID(GetID());
518+
return {};
506519
}
507520

508521
Type *Function::GetType() {

lldb/source/Symbol/Type.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,49 +64,49 @@ bool lldb_private::contextMatches(llvm::ArrayRef<CompilerContext> context_chain,
6464
return true;
6565
}
6666

67-
void CompilerContext::Dump() const {
67+
void CompilerContext::Dump(Stream &s) const {
6868
switch (kind) {
6969
default:
70-
printf("Invalid");
70+
s << "Invalid";
7171
break;
7272
case CompilerContextKind::TranslationUnit:
73-
printf("TranslationUnit");
73+
s << "TranslationUnit";
7474
break;
7575
case CompilerContextKind::Module:
76-
printf("Module");
76+
s << "Module";
7777
break;
7878
case CompilerContextKind::Namespace:
79-
printf("Namespace");
79+
s << "Namespace";
8080
break;
8181
case CompilerContextKind::Class:
82-
printf("Class");
82+
s << "Class";
8383
break;
8484
case CompilerContextKind::Struct:
85-
printf("Structure");
85+
s << "Structure";
8686
break;
8787
case CompilerContextKind::Union:
88-
printf("Union");
88+
s << "Union";
8989
break;
9090
case CompilerContextKind::Function:
91-
printf("Function");
91+
s << "Function";
9292
break;
9393
case CompilerContextKind::Variable:
94-
printf("Variable");
94+
s << "Variable";
9595
break;
9696
case CompilerContextKind::Enum:
97-
printf("Enumeration");
97+
s << "Enumeration";
9898
break;
9999
case CompilerContextKind::Typedef:
100-
printf("Typedef");
100+
s << "Typedef";
101101
break;
102102
case CompilerContextKind::AnyModule:
103-
printf("AnyModule");
103+
s << "AnyModule";
104104
break;
105105
case CompilerContextKind::AnyType:
106-
printf("AnyType");
106+
s << "AnyType";
107107
break;
108108
}
109-
printf("(\"%s\")\n", name.GetCString());
109+
s << "(" << name << ")";
110110
}
111111

112112
class TypeAppendVisitor {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
// FULL-MANGLED-METHOD-DAG: name = "sbar::foo(int)", mangled = "_ZN4sbar3fooEi"
8383

8484
// CONTEXT: Found 1 functions:
85-
// CONTEXT-DAG: name = "bar::foo()", mangled = "_ZN3bar3fooEv"
85+
// CONTEXT-DAG: name = "bar::foo()", mangled = "_ZN3bar3fooEv", decl_context = {Namespace(bar)}
8686

8787
// EMPTY: Found 0 functions:
8888

lldb/test/Shell/SymbolFile/DWARF/x86/module-ownership.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
// CHECK-ANON-S1: CXXRecordDecl {{.*}} imported in A struct
2727

2828
StructB s3;
29-
// CHECK-ANON-S2: CXXRecordDecl {{.*}} imported in A.B {{.*}} struct
29+
// CHECK-ANON-S2: CXXRecordDecl {{.*}} imported in A.B {{.*}}struct
3030
// CHECK-ANON-S2: -FieldDecl {{.*}} in A.B anon_field_b 'int'
3131

3232
Nested s4;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ std::vector<CompilerContext> parseCompilerContext() {
322322
}
323323
result.push_back({kind, ConstString{value}});
324324
}
325-
outs() << "Search context: {\n";
326-
for (auto entry: result)
327-
entry.Dump();
328-
outs() << "}\n";
325+
outs() << "Search context: {";
326+
lldb_private::StreamString s;
327+
llvm::interleaveComma(result, s, [&](auto &ctx) { ctx.Dump(s); });
328+
outs() << s.GetString().str() << "}\n";
329329

330330
return result;
331331
}

0 commit comments

Comments
 (0)