[Lldb-commits] [lldb] 8e12904 - [lldb/DWARF] Refactor DWARFDIE::Get{Decl, TypeLookup}Context (#93291)
via lldb-commits
lldb-commits at lists.llvm.org
Wed May 29 05:19:54 PDT 2024
Author: Pavel Labath
Date: 2024-05-29T14:19:49+02:00
New Revision: 8e1290432adf33a7aeca65a53d1faa7577ed0e66
URL: https://github.com/llvm/llvm-project/commit/8e1290432adf33a7aeca65a53d1faa7577ed0e66
DIFF: https://github.com/llvm/llvm-project/commit/8e1290432adf33a7aeca65a53d1faa7577ed0e66.diff
LOG: [lldb/DWARF] Refactor DWARFDIE::Get{Decl,TypeLookup}Context (#93291)
After a bug (the bug is that the functions don't handle DW_AT_signature,
aka type units) led me to one of these similar-but-different functions,
I started to realize that most of the differences between these two
functions are actually bugs.
As a first step towards merging them, this patch rewrites both of them
to follow the same pattern, while preserving all of their differences.
The main change is that GetTypeLookupContext now also uses a `seen` list
to avoid reference loops (currently that's not necessary because the
function strictly follows parent links, but that will change with
DW_AT_signatures).
I've also optimized both functions to avoid recursion by starting contruction
with the deepest scope first (and then reversing it).
Added:
Modified:
lldb/include/lldb/Symbol/Type.h
lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
lldb/source/Symbol/Type.cpp
lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
Removed:
################################################################################
diff --git a/lldb/include/lldb/Symbol/Type.h b/lldb/include/lldb/Symbol/Type.h
index 7aa0852676e46..c6f30cde81867 100644
--- a/lldb/include/lldb/Symbol/Type.h
+++ b/lldb/include/lldb/Symbol/Type.h
@@ -62,6 +62,8 @@ struct CompilerContext {
CompilerContextKind kind;
ConstString name;
};
+llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
+ const CompilerContext &rhs);
/// Match \p context_chain against \p pattern, which may contain "Any"
/// kinds. The \p context_chain should *not* contain any "Any" kinds.
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
index 4884374ef9472..03e289bbf3300 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
@@ -13,6 +13,7 @@
#include "DWARFDebugInfoEntry.h"
#include "DWARFDeclContext.h"
#include "DWARFUnit.h"
+#include "lldb/Symbol/Type.h"
#include "llvm/ADT/iterator.h"
@@ -379,108 +380,118 @@ std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
return result;
}
-static std::vector<lldb_private::CompilerContext>
-GetDeclContextImpl(llvm::SmallSet<lldb::user_id_t, 4> &seen, DWARFDIE die) {
- std::vector<lldb_private::CompilerContext> context;
+static void GetDeclContextImpl(DWARFDIE die,
+ llvm::SmallSet<lldb::user_id_t, 4> &seen,
+ std::vector<CompilerContext> &context) {
// Stop if we hit a cycle.
- if (!die || !seen.insert(die.GetID()).second)
- return context;
-
- // Handle outline member function DIEs by following the specification.
- if (DWARFDIE spec = die.GetReferencedDIE(DW_AT_specification))
- return GetDeclContextImpl(seen, spec);
-
- // Get the parent context chain.
- context = GetDeclContextImpl(seen, die.GetParent());
+ while (die && seen.insert(die.GetID()).second) {
+ // Handle outline member function DIEs by following the specification.
+ if (DWARFDIE spec = die.GetReferencedDIE(DW_AT_specification)) {
+ die = spec;
+ continue;
+ }
- // Add this DIE's contribution at the end of the chain.
- auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
- context.push_back({kind, ConstString(name)});
- };
- switch (die.Tag()) {
- case DW_TAG_module:
- push_ctx(CompilerContextKind::Module, die.GetName());
- break;
- case DW_TAG_namespace:
- push_ctx(CompilerContextKind::Namespace, die.GetName());
- break;
- case DW_TAG_structure_type:
- push_ctx(CompilerContextKind::Struct, die.GetName());
- break;
- case DW_TAG_union_type:
- push_ctx(CompilerContextKind::Union, die.GetName());
- break;
- case DW_TAG_class_type:
- push_ctx(CompilerContextKind::Class, die.GetName());
- break;
- case DW_TAG_enumeration_type:
- push_ctx(CompilerContextKind::Enum, die.GetName());
- break;
- case DW_TAG_subprogram:
- push_ctx(CompilerContextKind::Function, die.GetName());
- break;
- case DW_TAG_variable:
- push_ctx(CompilerContextKind::Variable, die.GetPubname());
- break;
- case DW_TAG_typedef:
- push_ctx(CompilerContextKind::Typedef, die.GetName());
- break;
- default:
- break;
+ // Add this DIE's contribution at the end of the chain.
+ auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
+ context.push_back({kind, ConstString(name)});
+ };
+ switch (die.Tag()) {
+ case DW_TAG_module:
+ push_ctx(CompilerContextKind::Module, die.GetName());
+ break;
+ case DW_TAG_namespace:
+ push_ctx(CompilerContextKind::Namespace, die.GetName());
+ break;
+ case DW_TAG_structure_type:
+ push_ctx(CompilerContextKind::Struct, die.GetName());
+ break;
+ case DW_TAG_union_type:
+ push_ctx(CompilerContextKind::Union, die.GetName());
+ break;
+ case DW_TAG_class_type:
+ push_ctx(CompilerContextKind::Class, die.GetName());
+ break;
+ case DW_TAG_enumeration_type:
+ push_ctx(CompilerContextKind::Enum, die.GetName());
+ break;
+ case DW_TAG_subprogram:
+ push_ctx(CompilerContextKind::Function, die.GetName());
+ break;
+ case DW_TAG_variable:
+ push_ctx(CompilerContextKind::Variable, die.GetPubname());
+ break;
+ case DW_TAG_typedef:
+ push_ctx(CompilerContextKind::Typedef, die.GetName());
+ break;
+ default:
+ break;
+ }
+ // Now process the parent.
+ die = die.GetParent();
}
- return context;
}
-std::vector<lldb_private::CompilerContext> DWARFDIE::GetDeclContext() const {
+std::vector<CompilerContext> DWARFDIE::GetDeclContext() const {
llvm::SmallSet<lldb::user_id_t, 4> seen;
- return GetDeclContextImpl(seen, *this);
+ std::vector<CompilerContext> context;
+ GetDeclContextImpl(*this, seen, context);
+ std::reverse(context.begin(), context.end());
+ return context;
}
-std::vector<lldb_private::CompilerContext>
-DWARFDIE::GetTypeLookupContext() const {
- std::vector<lldb_private::CompilerContext> context;
- // If there is no name, then there is no need to look anything up for this
- // DIE.
- const char *name = GetName();
- if (!name || !name[0])
- return context;
- const dw_tag_t tag = Tag();
- if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
- return context;
- DWARFDIE parent = GetParent();
- if (parent)
- context = parent.GetTypeLookupContext();
- auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
- context.push_back({kind, ConstString(name)});
- };
- switch (tag) {
- case DW_TAG_namespace:
- push_ctx(CompilerContextKind::Namespace, name);
- break;
- case DW_TAG_structure_type:
- push_ctx(CompilerContextKind::Struct, name);
- break;
- case DW_TAG_union_type:
- push_ctx(CompilerContextKind::Union, name);
- break;
- case DW_TAG_class_type:
- push_ctx(CompilerContextKind::Class, name);
- break;
- case DW_TAG_enumeration_type:
- push_ctx(CompilerContextKind::Enum, name);
- break;
- case DW_TAG_variable:
- push_ctx(CompilerContextKind::Variable, GetPubname());
- break;
- case DW_TAG_typedef:
- push_ctx(CompilerContextKind::Typedef, name);
- break;
- case DW_TAG_base_type:
- push_ctx(CompilerContextKind::Builtin, name);
- break;
- default:
- break;
+static void GetTypeLookupContextImpl(DWARFDIE die,
+ llvm::SmallSet<lldb::user_id_t, 4> &seen,
+ std::vector<CompilerContext> &context) {
+ // Stop if we hit a cycle.
+ while (die && seen.insert(die.GetID()).second) {
+ // If there is no name, then there is no need to look anything up for this
+ // DIE.
+ const char *name = die.GetName();
+ if (!name || !name[0])
+ return;
+
+ // Add this DIE's contribution at the end of the chain.
+ auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
+ context.push_back({kind, ConstString(name)});
+ };
+ switch (die.Tag()) {
+ case DW_TAG_namespace:
+ push_ctx(CompilerContextKind::Namespace, die.GetName());
+ break;
+ case DW_TAG_structure_type:
+ push_ctx(CompilerContextKind::Struct, die.GetName());
+ break;
+ case DW_TAG_union_type:
+ push_ctx(CompilerContextKind::Union, die.GetName());
+ break;
+ case DW_TAG_class_type:
+ push_ctx(CompilerContextKind::Class, die.GetName());
+ break;
+ case DW_TAG_enumeration_type:
+ push_ctx(CompilerContextKind::Enum, die.GetName());
+ break;
+ case DW_TAG_variable:
+ push_ctx(CompilerContextKind::Variable, die.GetPubname());
+ break;
+ case DW_TAG_typedef:
+ push_ctx(CompilerContextKind::Typedef, die.GetName());
+ break;
+ case DW_TAG_base_type:
+ push_ctx(CompilerContextKind::Builtin, name);
+ break;
+ default:
+ break;
+ }
+ // Now process the parent.
+ die = die.GetParent();
}
+}
+
+std::vector<CompilerContext> DWARFDIE::GetTypeLookupContext() const {
+ llvm::SmallSet<lldb::user_id_t, 4> seen;
+ std::vector<CompilerContext> context;
+ GetTypeLookupContextImpl(*this, seen, context);
+ std::reverse(context.begin(), context.end());
return context;
}
diff --git a/lldb/source/Symbol/Type.cpp b/lldb/source/Symbol/Type.cpp
index 6bf69c2ded287..585808ace15ce 100644
--- a/lldb/source/Symbol/Type.cpp
+++ b/lldb/source/Symbol/Type.cpp
@@ -36,6 +36,13 @@
using namespace lldb;
using namespace lldb_private;
+llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os,
+ const CompilerContext &rhs) {
+ StreamString lldb_stream;
+ rhs.Dump(lldb_stream);
+ return os << lldb_stream.GetString();
+}
+
bool lldb_private::contextMatches(llvm::ArrayRef<CompilerContext> context_chain,
llvm::ArrayRef<CompilerContext> pattern) {
auto ctx = context_chain.begin();
diff --git a/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp b/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
index 20742ea512309..bea07dfa27cc6 100644
--- a/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
+++ b/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
@@ -10,6 +10,8 @@
#include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
#include "TestingSupport/Symbol/YAMLModuleTester.h"
#include "lldb/Core/dwarf.h"
+#include "lldb/Symbol/Type.h"
+#include "lldb/lldb-private-enumerations.h"
#include "llvm/ADT/STLExtras.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
@@ -187,3 +189,72 @@ TEST(DWARFDIETest, PeekName) {
dw_offset_t fifth_die_offset = 26;
EXPECT_EQ(unit->PeekDIEName(fifth_die_offset), "NameType2");
}
+
+TEST(DWARFDIETest, GetContext) {
+ const char *yamldata = R"(
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_386
+DWARF:
+ debug_abbrev:
+ - ID: 0
+ Table:
+ - Code: 0x1
+ Tag: DW_TAG_compile_unit
+ Children: DW_CHILDREN_yes
+ Attributes:
+ - Attribute: DW_AT_language
+ Form: DW_FORM_data2
+ - Code: 0x2
+ Tag: DW_TAG_namespace
+ Children: DW_CHILDREN_yes
+ Attributes:
+ - Attribute: DW_AT_name
+ Form: DW_FORM_string
+ - Code: 0x3
+ Tag: DW_TAG_structure_type
+ Children: DW_CHILDREN_no
+ Attributes:
+ - Attribute: DW_AT_name
+ Form: DW_FORM_string
+ debug_info:
+ - Version: 4
+ AddrSize: 8
+ Entries:
+ - AbbrCode: 0x1
+ Values:
+ - Value: 0x000000000000000C
+ - AbbrCode: 0x2
+ Values:
+ - CStr: NAMESPACE
+ - AbbrCode: 0x3
+ Values:
+ - CStr: STRUCT
+ - AbbrCode: 0x0
+ - AbbrCode: 0x0
+)";
+
+ YAMLModuleTester t(yamldata);
+ auto *symbol_file =
+ llvm::cast<SymbolFileDWARF>(t.GetModule()->GetSymbolFile());
+ DWARFUnit *unit = symbol_file->DebugInfo().GetUnitAtIndex(0);
+ ASSERT_TRUE(unit);
+
+ auto make_namespace = [](llvm::StringRef name) {
+ return CompilerContext(CompilerContextKind::Namespace, ConstString(name));
+ };
+ auto make_struct = [](llvm::StringRef name) {
+ return CompilerContext(CompilerContextKind::Struct, ConstString(name));
+ };
+ DWARFDIE struct_die = unit->DIE().GetFirstChild().GetFirstChild();
+ ASSERT_TRUE(struct_die);
+ EXPECT_THAT(
+ struct_die.GetDeclContext(),
+ testing::ElementsAre(make_namespace("NAMESPACE"), make_struct("STRUCT")));
+ EXPECT_THAT(
+ struct_die.GetTypeLookupContext(),
+ testing::ElementsAre(make_namespace("NAMESPACE"), make_struct("STRUCT")));
+}
More information about the lldb-commits
mailing list