Skip to content

Commit 4950467

Browse files
Expose DWARFDIE::GetDeclContext() in lldb_private::Function. (#69981)
I need this API in the Swift plugin, but it seems generally useful enough to expose it in the main branch.
1 parent 8a57bc0 commit 4950467

File tree

15 files changed

+104
-61
lines changed

15 files changed

+104
-61
lines changed

lldb/include/lldb/Symbol/Function.h

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

536+
/// Get the CompilerContext for this function, if available.
537+
///
538+
/// \return
539+
/// The CompilerContext, or an empty vector if none is available.
540+
std::vector<CompilerContext> GetCompilerContext();
541+
536542
/// Get accessor for the type that describes the function return value type,
537543
/// and parameter types.
538544
///

lldb/include/lldb/Symbol/SymbolFile.h

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

226226
virtual bool CompleteType(CompilerType &compiler_type) = 0;
227227
virtual void ParseDeclsForContext(CompilerDeclContext decl_ctx) {}
228-
virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) {
229-
return CompilerDecl();
230-
}
228+
virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) { return {}; }
231229
virtual CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) {
232-
return CompilerDeclContext();
230+
return {};
233231
}
234232
virtual CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) {
235-
return CompilerDeclContext();
233+
return {};
234+
}
235+
virtual std::vector<CompilerContext>
236+
GetCompilerContextForUID(lldb::user_id_t uid) {
237+
return {};
236238
}
237239
virtual uint32_t ResolveSymbolContext(const Address &so_addr,
238240
lldb::SymbolContextItem resolve_scope,

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.
@@ -2287,15 +2286,15 @@ CompilerDecl DWARFASTParserClang::GetDeclForUIDFromDWARF(const DWARFDIE &die) {
22872286
clang::Decl *clang_decl = GetClangDeclForDIE(die);
22882287
if (clang_decl != nullptr)
22892288
return m_ast.GetCompilerDecl(clang_decl);
2290-
return CompilerDecl();
2289+
return {};
22912290
}
22922291

22932292
CompilerDeclContext
22942293
DWARFASTParserClang::GetDeclContextForUIDFromDWARF(const DWARFDIE &die) {
22952294
clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE(die);
22962295
if (clang_decl_ctx)
22972296
return m_ast.CreateDeclContext(clang_decl_ctx);
2298-
return CompilerDeclContext();
2297+
return {};
22992298
}
23002299

23012300
CompilerDeclContext
@@ -2304,7 +2303,7 @@ DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) {
23042303
GetClangDeclContextContainingDIE(die, nullptr);
23052304
if (clang_decl_ctx)
23062305
return m_ast.CreateDeclContext(clang_decl_ctx);
2307-
return CompilerDeclContext();
2306+
return {};
23082307
}
23092308

23102309
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
@@ -373,47 +373,49 @@ std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
373373
return result;
374374
}
375375

376-
void DWARFDIE::GetDeclContext(
377-
llvm::SmallVectorImpl<lldb_private::CompilerContext> &context) const {
376+
std::vector<lldb_private::CompilerContext> DWARFDIE::GetDeclContext() const {
377+
std::vector<lldb_private::CompilerContext> context;
378378
const dw_tag_t tag = Tag();
379379
if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
380-
return;
380+
return context;
381381
DWARFDIE parent = GetParent();
382382
if (parent)
383-
parent.GetDeclContext(context);
383+
context = parent.GetDeclContext();
384+
auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
385+
context.push_back({kind, ConstString(name)});
386+
};
384387
switch (tag) {
385388
case DW_TAG_module:
386-
context.push_back({CompilerContextKind::Module, ConstString(GetName())});
389+
push_ctx(CompilerContextKind::Module, GetName());
387390
break;
388391
case DW_TAG_namespace:
389-
context.push_back({CompilerContextKind::Namespace, ConstString(GetName())});
392+
push_ctx(CompilerContextKind::Namespace, GetName());
390393
break;
391394
case DW_TAG_structure_type:
392-
context.push_back({CompilerContextKind::Struct, ConstString(GetName())});
395+
push_ctx(CompilerContextKind::Struct, GetName());
393396
break;
394397
case DW_TAG_union_type:
395-
context.push_back({CompilerContextKind::Union, ConstString(GetName())});
398+
push_ctx(CompilerContextKind::Union, GetName());
396399
break;
397400
case DW_TAG_class_type:
398-
context.push_back({CompilerContextKind::Class, ConstString(GetName())});
401+
push_ctx(CompilerContextKind::Class, GetName());
399402
break;
400403
case DW_TAG_enumeration_type:
401-
context.push_back({CompilerContextKind::Enum, ConstString(GetName())});
404+
push_ctx(CompilerContextKind::Enum, GetName());
402405
break;
403406
case DW_TAG_subprogram:
404-
context.push_back(
405-
{CompilerContextKind::Function, ConstString(GetPubname())});
407+
push_ctx(CompilerContextKind::Function, GetPubname());
406408
break;
407409
case DW_TAG_variable:
408-
context.push_back(
409-
{CompilerContextKind::Variable, ConstString(GetPubname())});
410+
push_ctx(CompilerContextKind::Variable, GetPubname());
410411
break;
411412
case DW_TAG_typedef:
412-
context.push_back({CompilerContextKind::Typedef, ConstString(GetName())});
413+
push_ctx(CompilerContextKind::Typedef, GetName());
413414
break;
414415
default:
415416
break;
416417
}
418+
return context;
417419
}
418420

419421
DWARFDIE

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

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

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

7979
// Getting attribute values from the DIE.
8080
//

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,17 @@ SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
14541454
return CompilerDeclContext();
14551455
}
14561456

1457+
std::vector<CompilerContext>
1458+
SymbolFileDWARF::GetCompilerContextForUID(lldb::user_id_t type_uid) {
1459+
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1460+
// Anytime we have a lldb::user_id_t, we must get the DIE by calling
1461+
// SymbolFileDWARF::GetDIE(). See comments inside the
1462+
// SymbolFileDWARF::GetDIE() for details.
1463+
if (DWARFDIE die = GetDIE(type_uid))
1464+
return die.GetDeclContext();
1465+
return {};
1466+
}
1467+
14571468
Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
14581469
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
14591470
// Anytime we have a lldb::user_id_t, we must get the DIE by calling
@@ -2715,8 +2726,7 @@ void SymbolFileDWARF::FindTypes(
27152726
if (!languages[GetLanguageFamily(*die.GetCU())])
27162727
return true;
27172728

2718-
llvm::SmallVector<CompilerContext, 4> die_context;
2719-
die.GetDeclContext(die_context);
2729+
std::vector<CompilerContext> die_context = die.GetDeclContext();
27202730
if (!contextMatches(die_context, pattern))
27212731
return true;
27222732

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

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

155155
CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) override;
156156

157+
std::vector<CompilerContext>
158+
GetCompilerContextForUID(lldb::user_id_t uid) override;
159+
157160
void ParseDeclsForContext(CompilerDeclContext decl_ctx) override;
158161

159162
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
@@ -1379,19 +1379,25 @@ void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf,
13791379
CompilerDeclContext
13801380
SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) {
13811381
const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1382-
SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1383-
if (oso_dwarf)
1382+
if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
13841383
return oso_dwarf->GetDeclContextForUID(type_uid);
1385-
return CompilerDeclContext();
1384+
return {};
13861385
}
13871386

13881387
CompilerDeclContext
13891388
SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
13901389
const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1391-
SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1392-
if (oso_dwarf)
1390+
if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
13931391
return oso_dwarf->GetDeclContextContainingUID(type_uid);
1394-
return CompilerDeclContext();
1392+
return {};
1393+
}
1394+
1395+
std::vector<CompilerContext>
1396+
SymbolFileDWARFDebugMap::GetCompilerContextForUID(lldb::user_id_t type_uid) {
1397+
const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1398+
if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
1399+
return oso_dwarf->GetCompilerContextForUID(type_uid);
1400+
return {};
13951401
}
13961402

13971403
void SymbolFileDWARFDebugMap::ParseDeclsForContext(

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

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

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

98100
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
@@ -396,6 +396,15 @@ void Function::GetDescription(Stream *s, lldb::DescriptionLevel level,
396396
s->AsRawOstream() << ", name = \"" << name << '"';
397397
if (mangled)
398398
s->AsRawOstream() << ", mangled = \"" << mangled << '"';
399+
if (level == eDescriptionLevelVerbose) {
400+
*s << ", decl_context = {";
401+
auto decl_context = GetCompilerContext();
402+
// Drop the function itself from the context chain.
403+
if (decl_context.size())
404+
decl_context.pop_back();
405+
llvm::interleaveComma(decl_context, *s, [&](auto &ctx) { ctx.Dump(*s); });
406+
*s << "}";
407+
}
399408
*s << ", range = ";
400409
Address::DumpStyle fallback_style;
401410
if (level == eDescriptionLevelVerbose)
@@ -513,13 +522,17 @@ ConstString Function::GetDisplayName() const {
513522
}
514523

515524
CompilerDeclContext Function::GetDeclContext() {
516-
ModuleSP module_sp = CalculateSymbolContextModule();
517-
518-
if (module_sp) {
525+
if (ModuleSP module_sp = CalculateSymbolContextModule())
519526
if (SymbolFile *sym_file = module_sp->GetSymbolFile())
520527
return sym_file->GetDeclContextForUID(GetID());
521-
}
522-
return CompilerDeclContext();
528+
return {};
529+
}
530+
531+
std::vector<CompilerContext> Function::GetCompilerContext() {
532+
if (ModuleSP module_sp = CalculateSymbolContextModule())
533+
if (SymbolFile *sym_file = module_sp->GetSymbolFile())
534+
return sym_file->GetCompilerContextForUID(GetID());
535+
return {};
523536
}
524537

525538
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)