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;

0 commit comments

Comments
 (0)