Skip to content

Commit b37f9e0

Browse files
authored
[lldb] Move DWARFDeclContext functions from DWARFDebugInfoEntry to DW… (#95227)
…ARFDIE This puts them closer to the other two functions doing something very similar. I've tried to stick to the original logic of the functions as much as possible, though I did apply some easy simplifications. The changes in DWARFDeclContext.h are there to make the unit tests produce more useful error messages.
1 parent 65f746e commit b37f9e0

File tree

11 files changed

+95
-114
lines changed

11 files changed

+95
-114
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,7 @@ DWARFASTParserClang::ConstructDemangledNameFromDWARF(const DWARFDIE &die) {
24612461
std::vector<clang::ParmVarDecl *> param_decls;
24622462
StreamString sstr;
24632463

2464-
DWARFDeclContext decl_ctx = SymbolFileDWARF::GetDWARFDeclContext(die);
2464+
DWARFDeclContext decl_ctx = die.GetDWARFDeclContext();
24652465
sstr << decl_ctx.GetQualifiedName();
24662466

24672467
clang::DeclContext *containing_decl_ctx =

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

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,64 @@ std::vector<CompilerContext> DWARFDIE::GetTypeLookupContext() const {
505505
return context;
506506
}
507507

508+
static DWARFDeclContext GetDWARFDeclContextImpl(DWARFDIE die) {
509+
DWARFDeclContext dwarf_decl_ctx;
510+
while (die) {
511+
const dw_tag_t tag = die.Tag();
512+
if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
513+
break;
514+
dwarf_decl_ctx.AppendDeclContext(tag, die.GetName());
515+
DWARFDIE parent_decl_ctx_die = die.GetParentDeclContextDIE();
516+
if (parent_decl_ctx_die == die)
517+
break;
518+
die = parent_decl_ctx_die;
519+
}
520+
return dwarf_decl_ctx;
521+
}
522+
523+
DWARFDeclContext DWARFDIE::GetDWARFDeclContext() const {
524+
return GetDWARFDeclContextImpl(*this);
525+
}
526+
527+
static DWARFDIE GetParentDeclContextDIEImpl(DWARFDIE die) {
528+
DWARFDIE orig_die = die;
529+
while (die) {
530+
// If this is the original DIE that we are searching for a declaration for,
531+
// then don't look in the cache as we don't want our own decl context to be
532+
// our decl context...
533+
if (die != orig_die) {
534+
switch (die.Tag()) {
535+
case DW_TAG_compile_unit:
536+
case DW_TAG_partial_unit:
537+
case DW_TAG_namespace:
538+
case DW_TAG_structure_type:
539+
case DW_TAG_union_type:
540+
case DW_TAG_class_type:
541+
return die;
542+
543+
default:
544+
break;
545+
}
546+
}
547+
548+
if (DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification)) {
549+
if (DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE())
550+
return decl_ctx_die;
551+
}
552+
553+
if (DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin)) {
554+
if (DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE())
555+
return decl_ctx_die;
556+
}
557+
558+
die = die.GetParent();
559+
}
560+
return DWARFDIE();
561+
}
562+
508563
DWARFDIE
509564
DWARFDIE::GetParentDeclContextDIE() const {
510-
if (IsValid())
511-
return m_die->GetParentDeclContextDIE(m_cu);
512-
else
513-
return DWARFDIE();
565+
return GetParentDeclContextDIEImpl(*this);
514566
}
515567

516568
bool DWARFDIE::IsStructUnionOrClass() const {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class DWARFDIE : public DWARFBaseDIE {
8686
/// using a full or partial CompilerContext array.
8787
std::vector<CompilerContext> GetTypeLookupContext() const;
8888

89+
DWARFDeclContext GetDWARFDeclContext() const;
90+
8991
// Getting attribute values from the DIE.
9092
//
9193
// GetAttributeValueAsXXX() functions should only be used if you are

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

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -610,79 +610,6 @@ void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable(
610610
}
611611
}
612612

613-
DWARFDeclContext
614-
DWARFDebugInfoEntry::GetDWARFDeclContextStatic(const DWARFDebugInfoEntry *die,
615-
DWARFUnit *cu) {
616-
DWARFDeclContext dwarf_decl_ctx;
617-
for (;;) {
618-
const dw_tag_t tag = die->Tag();
619-
if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
620-
return dwarf_decl_ctx;
621-
dwarf_decl_ctx.AppendDeclContext(tag, die->GetName(cu));
622-
DWARFDIE parent_decl_ctx_die = die->GetParentDeclContextDIE(cu);
623-
if (!parent_decl_ctx_die || parent_decl_ctx_die.GetDIE() == die)
624-
return dwarf_decl_ctx;
625-
if (parent_decl_ctx_die.Tag() == DW_TAG_compile_unit ||
626-
parent_decl_ctx_die.Tag() == DW_TAG_partial_unit)
627-
return dwarf_decl_ctx;
628-
die = parent_decl_ctx_die.GetDIE();
629-
cu = parent_decl_ctx_die.GetCU();
630-
}
631-
}
632-
633-
DWARFDeclContext DWARFDebugInfoEntry::GetDWARFDeclContext(DWARFUnit *cu) const {
634-
return GetDWARFDeclContextStatic(this, cu);
635-
}
636-
637-
DWARFDIE
638-
DWARFDebugInfoEntry::GetParentDeclContextDIE(DWARFUnit *cu) const {
639-
DWARFAttributes attributes = GetAttributes(cu, Recurse::yes);
640-
return GetParentDeclContextDIE(cu, attributes);
641-
}
642-
643-
DWARFDIE
644-
DWARFDebugInfoEntry::GetParentDeclContextDIE(
645-
DWARFUnit *cu, const DWARFAttributes &attributes) const {
646-
DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this));
647-
648-
while (die) {
649-
// If this is the original DIE that we are searching for a declaration for,
650-
// then don't look in the cache as we don't want our own decl context to be
651-
// our decl context...
652-
if (die.GetDIE() != this) {
653-
switch (die.Tag()) {
654-
case DW_TAG_compile_unit:
655-
case DW_TAG_partial_unit:
656-
case DW_TAG_namespace:
657-
case DW_TAG_structure_type:
658-
case DW_TAG_union_type:
659-
case DW_TAG_class_type:
660-
return die;
661-
662-
default:
663-
break;
664-
}
665-
}
666-
667-
DWARFDIE spec_die = attributes.FormValueAsReference(DW_AT_specification);
668-
if (spec_die) {
669-
DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE();
670-
if (decl_ctx_die)
671-
return decl_ctx_die;
672-
}
673-
674-
DWARFDIE abs_die = attributes.FormValueAsReference(DW_AT_abstract_origin);
675-
if (abs_die) {
676-
DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE();
677-
if (decl_ctx_die)
678-
return decl_ctx_die;
679-
}
680-
681-
die = die.GetParent();
682-
}
683-
return DWARFDIE();
684-
}
685-
686613
lldb::offset_t DWARFDebugInfoEntry::GetFirstAttributeOffset() const {
687614
return GetOffset() + llvm::getULEB128Size(m_abbr_idx);
688615
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,6 @@ class DWARFDebugInfoEntry {
157157
return HasChildren() ? this + 1 : nullptr;
158158
}
159159

160-
DWARFDeclContext GetDWARFDeclContext(DWARFUnit *cu) const;
161-
162-
DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu) const;
163-
DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu,
164-
const DWARFAttributes &attributes) const;
165-
166160
void SetSiblingIndex(uint32_t idx) { m_sibling_idx = idx; }
167161
void SetParentIndex(uint32_t idx) { m_parent_idx = idx; }
168162

@@ -172,9 +166,6 @@ class DWARFDebugInfoEntry {
172166
bool IsGlobalOrStaticScopeVariable() const;
173167

174168
protected:
175-
static DWARFDeclContext
176-
GetDWARFDeclContextStatic(const DWARFDebugInfoEntry *die, DWARFUnit *cu);
177-
178169
// Up to 2TB offset within the .debug_info/.debug_types
179170
dw_offset_t m_offset : DW_DIE_OFFSET_MAX_BITSIZE;
180171
// How many to subtract from "this" to get the parent. If zero this die has no

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212
using namespace lldb_private::dwarf;
1313
using namespace lldb_private::plugin::dwarf;
1414

15-
/// Returns the name of `entry` if it has one, or the appropriate "anonymous
16-
/// {namespace, class, struct, union}".
17-
static const char *GetName(DWARFDeclContext::Entry entry) {
18-
if (entry.name != nullptr)
19-
return entry.name;
20-
if (entry.tag == DW_TAG_namespace)
15+
const char *DWARFDeclContext::Entry::GetName() const {
16+
if (name != nullptr)
17+
return name;
18+
if (tag == DW_TAG_namespace)
2119
return "(anonymous namespace)";
22-
if (entry.tag == DW_TAG_class_type)
20+
if (tag == DW_TAG_class_type)
2321
return "(anonymous class)";
24-
if (entry.tag == DW_TAG_structure_type)
22+
if (tag == DW_TAG_structure_type)
2523
return "(anonymous struct)";
26-
if (entry.tag == DW_TAG_union_type)
24+
if (tag == DW_TAG_union_type)
2725
return "(anonymous union)";
2826
return "(anonymous)";
2927
}
@@ -46,7 +44,7 @@ const char *DWARFDeclContext::GetQualifiedName() const {
4644
llvm::raw_string_ostream string_stream(m_qualified_name);
4745
llvm::interleave(
4846
llvm::reverse(m_entries), string_stream,
49-
[&](auto entry) { string_stream << GetName(entry); }, "::");
47+
[&](auto entry) { string_stream << entry.GetName(); }, "::");
5048
}
5149
}
5250
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDECLCONTEXT_H
1010
#define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDECLCONTEXT_H
1111

12-
#include "lldb/Utility/ConstString.h"
1312
#include "DWARFDefines.h"
13+
#include "lldb/Utility/ConstString.h"
14+
#include "llvm/ADT/StringExtras.h"
1415

1516
#include <cassert>
1617
#include <string>
@@ -38,6 +39,10 @@ class DWARFDeclContext {
3839
return false;
3940
}
4041

42+
/// Returns the name of this entry if it has one, or the appropriate
43+
/// "anonymous {namespace, class, struct, union}".
44+
const char *GetName() const;
45+
4146
// Test operator
4247
explicit operator bool() const { return tag != 0; }
4348

@@ -83,6 +88,17 @@ class DWARFDeclContext {
8388
m_qualified_name.clear();
8489
}
8590

91+
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
92+
const DWARFDeclContext &ctx) {
93+
OS << "DWARFDeclContext{";
94+
llvm::ListSeparator LS;
95+
for (const Entry &e : ctx.m_entries) {
96+
OS << LS << "{" << DW_TAG_value_to_name(e.tag) << ", " << e.GetName()
97+
<< "}";
98+
}
99+
return OS << "}";
100+
}
101+
86102
protected:
87103
typedef std::vector<Entry> collection;
88104
collection m_entries;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ void DWARFIndex::GetFullyQualifiedType(
121121
bool DWARFIndex::GetFullyQualifiedTypeImpl(
122122
const DWARFDeclContext &context, DWARFDIE die,
123123
llvm::function_ref<bool(DWARFDIE die)> callback) {
124-
DWARFDeclContext dwarf_decl_ctx =
125-
die.GetDIE()->GetDWARFDeclContext(die.GetCU());
124+
DWARFDeclContext dwarf_decl_ctx = die.GetDWARFDeclContext();
126125
if (dwarf_decl_ctx == context)
127126
return callback(die);
128127
return true;

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,7 +3080,7 @@ SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(const DWARFDIE &die) {
30803080
template_params = dwarf_ast->GetDIEClassTemplateParams(die);
30813081
}
30823082

3083-
const DWARFDeclContext die_dwarf_decl_ctx = GetDWARFDeclContext(die);
3083+
const DWARFDeclContext die_dwarf_decl_ctx = die.GetDWARFDeclContext();
30843084
m_index->GetFullyQualifiedType(die_dwarf_decl_ctx, [&](DWARFDIE type_die) {
30853085
// Make sure type_die's language matches the type system we are
30863086
// looking for. We don't want to find a "Foo" type from Java if we
@@ -3109,7 +3109,7 @@ SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(const DWARFDIE &die) {
31093109
}
31103110

31113111
if (log) {
3112-
DWARFDeclContext type_dwarf_decl_ctx = GetDWARFDeclContext(type_die);
3112+
DWARFDeclContext type_dwarf_decl_ctx = type_die.GetDWARFDeclContext();
31133113
GetObjectFile()->GetModule()->LogMessage(
31143114
log,
31153115
"SymbolFileDWARF::"
@@ -3535,8 +3535,9 @@ VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
35353535
if ((parent_tag == DW_TAG_compile_unit ||
35363536
parent_tag == DW_TAG_partial_unit) &&
35373537
Language::LanguageIsCPlusPlus(GetLanguage(*die.GetCU())))
3538-
mangled =
3539-
GetDWARFDeclContext(die).GetQualifiedNameAsConstString().GetCString();
3538+
mangled = die.GetDWARFDeclContext()
3539+
.GetQualifiedNameAsConstString()
3540+
.GetCString();
35403541
}
35413542

35423543
if (tag == DW_TAG_formal_parameter)
@@ -4361,14 +4362,6 @@ SymbolFileDWARF::GetContainingDeclContext(const DWARFDIE &die) {
43614362
return CompilerDeclContext();
43624363
}
43634364

4364-
DWARFDeclContext SymbolFileDWARF::GetDWARFDeclContext(const DWARFDIE &die) {
4365-
if (!die.IsValid())
4366-
return {};
4367-
DWARFDeclContext dwarf_decl_ctx =
4368-
die.GetDIE()->GetDWARFDeclContext(die.GetCU());
4369-
return dwarf_decl_ctx;
4370-
}
4371-
43724365
LanguageType SymbolFileDWARF::LanguageTypeFromDWARF(uint64_t val) {
43734366
// Note: user languages between lo_user and hi_user must be handled
43744367
// explicitly here.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,6 @@ class SymbolFileDWARF : public SymbolFileCommon {
296296

297297
static CompilerDeclContext GetContainingDeclContext(const DWARFDIE &die);
298298

299-
static DWARFDeclContext GetDWARFDeclContext(const DWARFDIE &die);
300-
301299
static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val);
302300

303301
static lldb::LanguageType GetLanguage(DWARFUnit &unit);

lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "Plugins/SymbolFile/DWARF/DWARFDIE.h"
1010
#include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
11+
#include "Plugins/SymbolFile/DWARF/DWARFDeclContext.h"
1112
#include "TestingSupport/Symbol/YAMLModuleTester.h"
1213
#include "lldb/Core/dwarf.h"
1314
#include "lldb/Symbol/Type.h"
@@ -19,6 +20,7 @@
1920
using namespace lldb;
2021
using namespace lldb_private;
2122
using namespace lldb_private::plugin::dwarf;
23+
using namespace lldb_private::dwarf;
2224

2325
TEST(DWARFDIETest, ChildIteration) {
2426
// Tests DWARFDIE::child_iterator.
@@ -257,6 +259,9 @@ TEST(DWARFDIETest, GetContext) {
257259
EXPECT_THAT(
258260
struct_die.GetTypeLookupContext(),
259261
testing::ElementsAre(make_namespace("NAMESPACE"), make_struct("STRUCT")));
262+
EXPECT_THAT(struct_die.GetDWARFDeclContext(),
263+
DWARFDeclContext({{DW_TAG_structure_type, "STRUCT"},
264+
{DW_TAG_namespace, "NAMESPACE"}}));
260265
}
261266

262267
TEST(DWARFDIETest, GetContextInFunction) {

0 commit comments

Comments
 (0)