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.
This commit is contained in:
Adrian Prantl
2023-10-24 10:55:23 -07:00
committed by GitHub
parent 8a57bc0928
commit 49504674db
15 changed files with 104 additions and 61 deletions

View File

@@ -533,6 +533,12 @@ public:
/// The DeclContext, or NULL if none exists.
CompilerDeclContext GetDeclContext();
/// Get the CompilerContext for this function, if available.
///
/// \return
/// The CompilerContext, or an empty vector if none is available.
std::vector<CompilerContext> GetCompilerContext();
/// Get accessor for the type that describes the function return value type,
/// and parameter types.
///

View File

@@ -225,14 +225,16 @@ public:
virtual bool CompleteType(CompilerType &compiler_type) = 0;
virtual void ParseDeclsForContext(CompilerDeclContext decl_ctx) {}
virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) {
return CompilerDecl();
}
virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) { return {}; }
virtual CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) {
return CompilerDeclContext();
return {};
}
virtual CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) {
return CompilerDeclContext();
return {};
}
virtual std::vector<CompilerContext>
GetCompilerContextForUID(lldb::user_id_t uid) {
return {};
}
virtual uint32_t ResolveSymbolContext(const Address &so_addr,
lldb::SymbolContextItem resolve_scope,

View File

@@ -34,7 +34,7 @@ struct CompilerContext {
}
bool operator!=(const CompilerContext &rhs) const { return !(*this == rhs); }
void Dump() const;
void Dump(Stream &s) const;
CompilerContextKind kind;
ConstString name;

View File

@@ -143,8 +143,7 @@ TypeSP DWARFASTParserClang::ParseTypeFromClangModule(const SymbolContext &sc,
// If this type comes from a Clang module, recursively look in the
// DWARF section of the .pcm file in the module cache. Clang
// generates DWO skeleton units as breadcrumbs to find them.
llvm::SmallVector<CompilerContext, 4> decl_context;
die.GetDeclContext(decl_context);
std::vector<CompilerContext> decl_context = die.GetDeclContext();
TypeMap pcm_types;
// The type in the Clang module must have the same language as the current CU.
@@ -2287,7 +2286,7 @@ CompilerDecl DWARFASTParserClang::GetDeclForUIDFromDWARF(const DWARFDIE &die) {
clang::Decl *clang_decl = GetClangDeclForDIE(die);
if (clang_decl != nullptr)
return m_ast.GetCompilerDecl(clang_decl);
return CompilerDecl();
return {};
}
CompilerDeclContext
@@ -2295,7 +2294,7 @@ DWARFASTParserClang::GetDeclContextForUIDFromDWARF(const DWARFDIE &die) {
clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE(die);
if (clang_decl_ctx)
return m_ast.CreateDeclContext(clang_decl_ctx);
return CompilerDeclContext();
return {};
}
CompilerDeclContext
@@ -2304,7 +2303,7 @@ DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) {
GetClangDeclContextContainingDIE(die, nullptr);
if (clang_decl_ctx)
return m_ast.CreateDeclContext(clang_decl_ctx);
return CompilerDeclContext();
return {};
}
size_t DWARFASTParserClang::ParseChildEnumerators(

View File

@@ -373,47 +373,49 @@ std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
return result;
}
void DWARFDIE::GetDeclContext(
llvm::SmallVectorImpl<lldb_private::CompilerContext> &context) const {
std::vector<lldb_private::CompilerContext> DWARFDIE::GetDeclContext() const {
std::vector<lldb_private::CompilerContext> context;
const dw_tag_t tag = Tag();
if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
return;
return context;
DWARFDIE parent = GetParent();
if (parent)
parent.GetDeclContext(context);
context = parent.GetDeclContext();
auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
context.push_back({kind, ConstString(name)});
};
switch (tag) {
case DW_TAG_module:
context.push_back({CompilerContextKind::Module, ConstString(GetName())});
push_ctx(CompilerContextKind::Module, GetName());
break;
case DW_TAG_namespace:
context.push_back({CompilerContextKind::Namespace, ConstString(GetName())});
push_ctx(CompilerContextKind::Namespace, GetName());
break;
case DW_TAG_structure_type:
context.push_back({CompilerContextKind::Struct, ConstString(GetName())});
push_ctx(CompilerContextKind::Struct, GetName());
break;
case DW_TAG_union_type:
context.push_back({CompilerContextKind::Union, ConstString(GetName())});
push_ctx(CompilerContextKind::Union, GetName());
break;
case DW_TAG_class_type:
context.push_back({CompilerContextKind::Class, ConstString(GetName())});
push_ctx(CompilerContextKind::Class, GetName());
break;
case DW_TAG_enumeration_type:
context.push_back({CompilerContextKind::Enum, ConstString(GetName())});
push_ctx(CompilerContextKind::Enum, GetName());
break;
case DW_TAG_subprogram:
context.push_back(
{CompilerContextKind::Function, ConstString(GetPubname())});
push_ctx(CompilerContextKind::Function, GetPubname());
break;
case DW_TAG_variable:
context.push_back(
{CompilerContextKind::Variable, ConstString(GetPubname())});
push_ctx(CompilerContextKind::Variable, GetPubname());
break;
case DW_TAG_typedef:
context.push_back({CompilerContextKind::Typedef, ConstString(GetName())});
push_ctx(CompilerContextKind::Typedef, GetName());
break;
default:
break;
}
return context;
}
DWARFDIE

View File

@@ -74,7 +74,7 @@ public:
/// Return this DIE's decl context as it is needed to look up types
/// in Clang's -gmodules debug info format.
void GetDeclContext(llvm::SmallVectorImpl<CompilerContext> &context) const;
std::vector<CompilerContext> GetDeclContext() const;
// Getting attribute values from the DIE.
//

View File

@@ -1454,6 +1454,17 @@ SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
return CompilerDeclContext();
}
std::vector<CompilerContext>
SymbolFileDWARF::GetCompilerContextForUID(lldb::user_id_t type_uid) {
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
// Anytime we have a lldb::user_id_t, we must get the DIE by calling
// SymbolFileDWARF::GetDIE(). See comments inside the
// SymbolFileDWARF::GetDIE() for details.
if (DWARFDIE die = GetDIE(type_uid))
return die.GetDeclContext();
return {};
}
Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) {
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
// Anytime we have a lldb::user_id_t, we must get the DIE by calling
@@ -2715,8 +2726,7 @@ void SymbolFileDWARF::FindTypes(
if (!languages[GetLanguageFamily(*die.GetCU())])
return true;
llvm::SmallVector<CompilerContext, 4> die_context;
die.GetDeclContext(die_context);
std::vector<CompilerContext> die_context = die.GetDeclContext();
if (!contextMatches(die_context, pattern))
return true;

View File

@@ -154,6 +154,9 @@ public:
CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) override;
std::vector<CompilerContext>
GetCompilerContextForUID(lldb::user_id_t uid) override;
void ParseDeclsForContext(CompilerDeclContext decl_ctx) override;
uint32_t ResolveSymbolContext(const Address &so_addr,

View File

@@ -1379,19 +1379,25 @@ void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf,
CompilerDeclContext
SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) {
const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
if (oso_dwarf)
if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
return oso_dwarf->GetDeclContextForUID(type_uid);
return CompilerDeclContext();
return {};
}
CompilerDeclContext
SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
if (oso_dwarf)
if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
return oso_dwarf->GetDeclContextContainingUID(type_uid);
return CompilerDeclContext();
return {};
}
std::vector<CompilerContext>
SymbolFileDWARFDebugMap::GetCompilerContextForUID(lldb::user_id_t type_uid) {
const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
return oso_dwarf->GetCompilerContextForUID(type_uid);
return {};
}
void SymbolFileDWARFDebugMap::ParseDeclsForContext(

View File

@@ -93,6 +93,8 @@ public:
CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) override;
CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) override;
std::vector<CompilerContext>
GetCompilerContextForUID(lldb::user_id_t uid) override;
void ParseDeclsForContext(CompilerDeclContext decl_ctx) override;
bool CompleteType(CompilerType &compiler_type) override;

View File

@@ -396,6 +396,15 @@ void Function::GetDescription(Stream *s, lldb::DescriptionLevel level,
s->AsRawOstream() << ", name = \"" << name << '"';
if (mangled)
s->AsRawOstream() << ", mangled = \"" << mangled << '"';
if (level == eDescriptionLevelVerbose) {
*s << ", decl_context = {";
auto decl_context = GetCompilerContext();
// Drop the function itself from the context chain.
if (decl_context.size())
decl_context.pop_back();
llvm::interleaveComma(decl_context, *s, [&](auto &ctx) { ctx.Dump(*s); });
*s << "}";
}
*s << ", range = ";
Address::DumpStyle fallback_style;
if (level == eDescriptionLevelVerbose)
@@ -513,13 +522,17 @@ ConstString Function::GetDisplayName() const {
}
CompilerDeclContext Function::GetDeclContext() {
ModuleSP module_sp = CalculateSymbolContextModule();
if (module_sp) {
if (ModuleSP module_sp = CalculateSymbolContextModule())
if (SymbolFile *sym_file = module_sp->GetSymbolFile())
return sym_file->GetDeclContextForUID(GetID());
}
return CompilerDeclContext();
return {};
}
std::vector<CompilerContext> Function::GetCompilerContext() {
if (ModuleSP module_sp = CalculateSymbolContextModule())
if (SymbolFile *sym_file = module_sp->GetSymbolFile())
return sym_file->GetCompilerContextForUID(GetID());
return {};
}
Type *Function::GetType() {

View File

@@ -64,49 +64,49 @@ bool lldb_private::contextMatches(llvm::ArrayRef<CompilerContext> context_chain,
return true;
}
void CompilerContext::Dump() const {
void CompilerContext::Dump(Stream &s) const {
switch (kind) {
default:
printf("Invalid");
s << "Invalid";
break;
case CompilerContextKind::TranslationUnit:
printf("TranslationUnit");
s << "TranslationUnit";
break;
case CompilerContextKind::Module:
printf("Module");
s << "Module";
break;
case CompilerContextKind::Namespace:
printf("Namespace");
s << "Namespace";
break;
case CompilerContextKind::Class:
printf("Class");
s << "Class";
break;
case CompilerContextKind::Struct:
printf("Structure");
s << "Structure";
break;
case CompilerContextKind::Union:
printf("Union");
s << "Union";
break;
case CompilerContextKind::Function:
printf("Function");
s << "Function";
break;
case CompilerContextKind::Variable:
printf("Variable");
s << "Variable";
break;
case CompilerContextKind::Enum:
printf("Enumeration");
s << "Enumeration";
break;
case CompilerContextKind::Typedef:
printf("Typedef");
s << "Typedef";
break;
case CompilerContextKind::AnyModule:
printf("AnyModule");
s << "AnyModule";
break;
case CompilerContextKind::AnyType:
printf("AnyType");
s << "AnyType";
break;
}
printf("(\"%s\")\n", name.GetCString());
s << "(" << name << ")";
}
class TypeAppendVisitor {

View File

@@ -82,7 +82,7 @@
// FULL-MANGLED-METHOD-DAG: name = "sbar::foo(int)", mangled = "_ZN4sbar3fooEi"
// CONTEXT: Found 1 functions:
// CONTEXT-DAG: name = "bar::foo()", mangled = "_ZN3bar3fooEv"
// CONTEXT-DAG: name = "bar::foo()", mangled = "_ZN3bar3fooEv", decl_context = {Namespace(bar)}
// EMPTY: Found 0 functions:

View File

@@ -26,7 +26,7 @@ Struct s2;
// CHECK-ANON-S1: CXXRecordDecl {{.*}} imported in A struct
StructB s3;
// CHECK-ANON-S2: CXXRecordDecl {{.*}} imported in A.B {{.*}} struct
// CHECK-ANON-S2: CXXRecordDecl {{.*}} imported in A.B {{.*}}struct
// CHECK-ANON-S2: -FieldDecl {{.*}} in A.B anon_field_b 'int'
Nested s4;

View File

@@ -322,10 +322,10 @@ std::vector<CompilerContext> parseCompilerContext() {
}
result.push_back({kind, ConstString{value}});
}
outs() << "Search context: {\n";
for (auto entry: result)
entry.Dump();
outs() << "}\n";
outs() << "Search context: {";
lldb_private::StreamString s;
llvm::interleaveComma(result, s, [&](auto &ctx) { ctx.Dump(s); });
outs() << s.GetString().str() << "}\n";
return result;
}