Skip to content

Commit e2b3e4e

Browse files
authored
[lldb][NFCI] Unify DW_TAG -> string conversions (llvm#90657)
The high level goal is to have 1 way of converting a DW_TAG value into a human-readable string. There are 3 ways this change accomplishes that: 1.) Changing DW_TAG_value_to_name to not create custom error strings. The way it was doing this is error-prone: Specifically, it was using a function-local static char buffer and handing out a pointer to it. Initialization of this is thread-safe, but mutating it is definitely not. Multiple threads that want to call this function could step on each others toes. The implementation in this patch sidesteps the issue by just returning a StringRef with no mention of the tag value in it. 2.) Changing all uses of DW_TAG_value_to_name to log the value of the tag since the function doesn't create a string with the value in it anymore. 3.) Removing `DWARFBaseDIE::GetTagAsCString()`. Callers should call DW_TAG_value_to_name on the tag directly.
1 parent 401ecb4 commit e2b3e4e

File tree

6 files changed

+71
-78
lines changed

6 files changed

+71
-78
lines changed

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

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,9 @@ TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
452452
log,
453453
"DWARFASTParserClang::ParseTypeFromDWARF "
454454
"(die = {0:x16}, decl_ctx = {1:p} (die "
455-
"{2:x16})) {3} name = '{4}')",
455+
"{2:x16})) {3} ({4}) name = '{5}')",
456456
die.GetOffset(), static_cast<void *>(context), context_die.GetOffset(),
457-
die.GetTagAsCString(), die.GetName());
457+
DW_TAG_value_to_name(die.Tag()), die.Tag(), die.GetName());
458458
}
459459

460460
Type *type_ptr = dwarf->GetDIEToType().lookup(die.GetDIE());
@@ -765,9 +765,10 @@ DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
765765
if (log)
766766
dwarf->GetObjectFile()->GetModule()->LogMessage(
767767
log,
768-
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} '{2}' "
768+
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' "
769769
"is Objective-C 'id' built-in type.",
770-
die.GetOffset(), die.GetTagAsCString(), die.GetName());
770+
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
771+
die.GetName());
771772
clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
772773
encoding_data_type = Type::eEncodingIsUID;
773774
attrs.type.Clear();
@@ -776,9 +777,10 @@ DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
776777
if (log)
777778
dwarf->GetObjectFile()->GetModule()->LogMessage(
778779
log,
779-
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} '{2}' "
780+
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' "
780781
"is Objective-C 'Class' built-in type.",
781-
die.GetOffset(), die.GetTagAsCString(), die.GetName());
782+
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
783+
die.GetName());
782784
clang_type = m_ast.GetBasicType(eBasicTypeObjCClass);
783785
encoding_data_type = Type::eEncodingIsUID;
784786
attrs.type.Clear();
@@ -787,9 +789,10 @@ DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
787789
if (log)
788790
dwarf->GetObjectFile()->GetModule()->LogMessage(
789791
log,
790-
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} '{2}' "
792+
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' "
791793
"is Objective-C 'selector' built-in type.",
792-
die.GetOffset(), die.GetTagAsCString(), die.GetName());
794+
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
795+
die.GetName());
793796
clang_type = m_ast.GetBasicType(eBasicTypeObjCSel);
794797
encoding_data_type = Type::eEncodingIsUID;
795798
attrs.type.Clear();
@@ -808,10 +811,10 @@ DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
808811
if (log)
809812
dwarf->GetObjectFile()->GetModule()->LogMessage(
810813
log,
811-
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} "
812-
"'{2}' is 'objc_object*', which we overrode to "
813-
"'id'.",
814-
die.GetOffset(), die.GetTagAsCString(), die.GetName());
814+
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' "
815+
"is 'objc_object*', which we overrode to 'id'.",
816+
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
817+
die.GetName());
815818
clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
816819
encoding_data_type = Type::eEncodingIsUID;
817820
attrs.type.Clear();
@@ -870,10 +873,10 @@ TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc,
870873
if (log) {
871874
dwarf->GetObjectFile()->GetModule()->LogMessage(
872875
log,
873-
"SymbolFileDWARF({0:p}) - {1:x16}}: {2} type \"{3}\" is a "
874-
"forward declaration, complete type is {4:x8}",
876+
"SymbolFileDWARF({0:p}) - {1:x16}}: {2} ({3}) type \"{4}\" is a "
877+
"forward declaration, complete type is {5:x8}",
875878
static_cast<void *>(this), die.GetOffset(),
876-
DW_TAG_value_to_name(tag), attrs.name.GetCString(),
879+
DW_TAG_value_to_name(tag), tag, attrs.name.GetCString(),
877880
type_sp->GetID());
878881
}
879882

@@ -1734,11 +1737,10 @@ DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
17341737
if (log) {
17351738
dwarf->GetObjectFile()->GetModule()->LogMessage(
17361739
log,
1737-
"SymbolFileDWARF({0:p}) - {1:x16}: {2} type "
1738-
"\"{3}\" is an "
1739-
"incomplete objc type, complete type is {4:x8}",
1740+
"SymbolFileDWARF({0:p}) - {1:x16}: {2} ({3}) type \"{4}\" is an "
1741+
"incomplete objc type, complete type is {5:x8}",
17401742
static_cast<void *>(this), die.GetOffset(),
1741-
DW_TAG_value_to_name(tag), attrs.name.GetCString(),
1743+
DW_TAG_value_to_name(tag), tag, attrs.name.GetCString(),
17421744
type_sp->GetID());
17431745
}
17441746

@@ -1760,10 +1762,10 @@ DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
17601762
if (log) {
17611763
dwarf->GetObjectFile()->GetModule()->LogMessage(
17621764
log,
1763-
"SymbolFileDWARF({0:p}) - {1:x16}: {2} type \"{3}\" is a "
1765+
"SymbolFileDWARF({0:p}) - {1:x16}: {2} ({3}) type \"{4}\" is a "
17641766
"forward declaration, trying to find complete type",
17651767
static_cast<void *>(this), die.GetOffset(), DW_TAG_value_to_name(tag),
1766-
attrs.name.GetCString());
1768+
tag, attrs.name.GetCString());
17671769
}
17681770

17691771
// See if the type comes from a Clang module and if so, track down
@@ -1789,10 +1791,10 @@ DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
17891791
if (log) {
17901792
dwarf->GetObjectFile()->GetModule()->LogMessage(
17911793
log,
1792-
"SymbolFileDWARF({0:p}) - {1:x16}: {2} type \"{3}\" is a "
1793-
"forward declaration, complete type is {4:x8}",
1794+
"SymbolFileDWARF({0:p}) - {1:x16}: {2} ({3}) type \"{4}\" is a "
1795+
"forward declaration, complete type is {5:x8}",
17941796
static_cast<void *>(this), die.GetOffset(),
1795-
DW_TAG_value_to_name(tag), attrs.name.GetCString(),
1797+
DW_TAG_value_to_name(tag), tag, attrs.name.GetCString(),
17961798
type_sp->GetID());
17971799
}
17981800

@@ -1836,10 +1838,10 @@ DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
18361838
if (log) {
18371839
dwarf->GetObjectFile()->GetModule()->LogMessage(
18381840
log,
1839-
"SymbolFileDWARF({0:p}) - {1:x16}: {2} type \"{3}\" "
1841+
"SymbolFileDWARF({0:p}) - {1:x16}: {2} ({3}) type \"{4}\" "
18401842
"clang::ClassTemplateDecl failed to return a decl.",
18411843
static_cast<void *>(this), die.GetOffset(),
1842-
DW_TAG_value_to_name(tag), attrs.name.GetCString());
1844+
DW_TAG_value_to_name(tag), tag, attrs.name.GetCString());
18431845
}
18441846
return TypeSP();
18451847
}
@@ -3018,11 +3020,11 @@ void DWARFASTParserClang::ParseSingleMember(
30183020
this_field_info.bit_offset)))) {
30193021
ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
30203022
objfile->GetModule()->ReportWarning(
3021-
"{0:x16}: {1} bitfield named \"{2}\" has invalid "
3022-
"bit offset ({3:x8}) member will be ignored. Please file a bug "
3023+
"{0:x16}: {1} ({2}) bitfield named \"{3}\" has invalid "
3024+
"bit offset ({4:x8}) member will be ignored. Please file a bug "
30233025
"against the "
3024-
"compiler and include the preprocessed output for {4}\n",
3025-
die.GetID(), DW_TAG_value_to_name(tag), attrs.name,
3026+
"compiler and include the preprocessed output for {5}\n",
3027+
die.GetID(), DW_TAG_value_to_name(tag), tag, attrs.name,
30263028
this_field_info.bit_offset, GetUnitName(parent_die).c_str());
30273029
return;
30283030
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ dw_tag_t DWARFBaseDIE::Tag() const {
3535
return llvm::dwarf::DW_TAG_null;
3636
}
3737

38-
const char *DWARFBaseDIE::GetTagAsCString() const {
39-
return DW_TAG_value_to_name(Tag());
40-
}
41-
4238
const char *DWARFBaseDIE::GetAttributeValueAsString(const dw_attr_t attr,
4339
const char *fail_value) const {
4440
if (IsValid())

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ class DWARFBaseDIE {
8585
// Accessing information about a DIE
8686
dw_tag_t Tag() const;
8787

88-
const char *GetTagAsCString() const;
89-
9088
dw_offset_t GetOffset() const;
9189

9290
// Get the LLDB user ID for this DIE. This is often just the DIE offset,

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@
1515
namespace lldb_private::plugin {
1616
namespace dwarf {
1717

18-
const char *DW_TAG_value_to_name(uint32_t val) {
19-
static char invalid[100];
20-
21-
if (val == 0)
22-
return "NULL";
18+
llvm::StringRef DW_TAG_value_to_name(dw_tag_t tag) {
19+
static constexpr llvm::StringLiteral s_unknown_tag_name("<unknown DW_TAG>");
20+
if (llvm::StringRef tag_name = llvm::dwarf::TagString(tag); !tag_name.empty())
21+
return tag_name;
2322

24-
llvm::StringRef llvmstr = llvm::dwarf::TagString(val);
25-
if (llvmstr.empty()) {
26-
snprintf(invalid, sizeof(invalid), "Unknown DW_TAG constant: 0x%x", val);
27-
return invalid;
28-
}
29-
return llvmstr.data();
23+
return s_unknown_tag_name;
3024
}
3125

3226
const char *DW_AT_value_to_name(uint32_t val) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace dwarf {
1717

1818
typedef uint32_t DRC_class; // Holds DRC_* class bitfields
1919

20-
const char *DW_TAG_value_to_name(uint32_t val);
20+
llvm::StringRef DW_TAG_value_to_name(dw_tag_t tag);
2121

2222
const char *DW_AT_value_to_name(uint32_t val);
2323

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

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,8 +1555,10 @@ Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
15551555
Log *log = GetLog(DWARFLog::DebugInfo);
15561556
if (log)
15571557
GetObjectFile()->GetModule()->LogMessage(
1558-
log, "SymbolFileDWARF::ResolveTypeUID (die = {0:x16}) {1} '{2}'",
1559-
die.GetOffset(), die.GetTagAsCString(), die.GetName());
1558+
log,
1559+
"SymbolFileDWARF::ResolveTypeUID (die = {0:x16}) {1} ({2}) '{3}'",
1560+
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
1561+
die.GetName());
15601562

15611563
// We might be coming in in the middle of a type tree (a class within a
15621564
// class, an enum within a class), so parse any needed parent DIEs before
@@ -1572,11 +1574,10 @@ Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die,
15721574
if (log)
15731575
GetObjectFile()->GetModule()->LogMessage(
15741576
log,
1575-
"SymbolFileDWARF::ResolveTypeUID (die = {0:x16}) "
1576-
"{1} '{2}' "
1577-
"resolve parent forward type for {3:x16})",
1578-
die.GetOffset(), die.GetTagAsCString(), die.GetName(),
1579-
decl_ctx_die.GetOffset());
1577+
"SymbolFileDWARF::ResolveTypeUID (die = {0:x16}) {1} ({2}) "
1578+
"'{3}' resolve parent forward type for {4:x16})",
1579+
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
1580+
die.GetName(), decl_ctx_die.GetOffset());
15801581
} break;
15811582

15821583
default:
@@ -1644,9 +1645,9 @@ bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
16441645
Log *log = GetLog(DWARFLog::DebugInfo | DWARFLog::TypeCompletion);
16451646
if (log)
16461647
GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
1647-
log, "{0:x8}: {1} '{2}' resolving forward declaration...",
1648-
dwarf_die.GetID(), dwarf_die.GetTagAsCString(),
1649-
type->GetName().AsCString());
1648+
log, "{0:x8}: {1} ({2}) '{3}' resolving forward declaration...",
1649+
dwarf_die.GetID(), DW_TAG_value_to_name(dwarf_die.Tag()),
1650+
dwarf_die.Tag(), type->GetName().AsCString());
16501651
assert(compiler_type);
16511652
if (DWARFASTParser *dwarf_ast = GetDWARFParser(*dwarf_die.GetCU()))
16521653
return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type);
@@ -1665,8 +1666,9 @@ Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
16651666
return type;
16661667

16671668
GetObjectFile()->GetModule()->ReportError(
1668-
"Parsing a die that is being parsed die: {0:x16}: {1} {2}",
1669-
die.GetOffset(), die.GetTagAsCString(), die.GetName());
1669+
"Parsing a die that is being parsed die: {0:x16}: {1} ({2}) {3}",
1670+
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
1671+
die.GetName());
16701672

16711673
} else
16721674
return type;
@@ -3134,9 +3136,9 @@ SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(const DWARFDIE &die) {
31343136
if (log) {
31353137
GetObjectFile()->GetModule()->LogMessage(
31363138
log,
3137-
"SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag={0}, "
3138-
"name='{1}')",
3139-
DW_TAG_value_to_name(tag), die.GetName());
3139+
"SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag={0} "
3140+
"({1}), name='{2}')",
3141+
DW_TAG_value_to_name(tag), tag, die.GetName());
31403142
}
31413143

31423144
// Get the type system that we are looking to find a type for. We will
@@ -3184,10 +3186,10 @@ SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(const DWARFDIE &die) {
31843186
GetObjectFile()->GetModule()->LogMessage(
31853187
log,
31863188
"SymbolFileDWARF::"
3187-
"FindDefinitionTypeForDWARFDeclContext(tag={0}, "
3188-
"name='{1}') ignoring die={2:x16} ({3})",
3189-
DW_TAG_value_to_name(tag), die.GetName(), type_die.GetOffset(),
3190-
type_die.GetName());
3189+
"FindDefinitionTypeForDWARFDeclContext(tag={0} ({1}), "
3190+
"name='{2}') ignoring die={3:x16} ({4})",
3191+
DW_TAG_value_to_name(tag), tag, die.GetName(),
3192+
type_die.GetOffset(), type_die.GetName());
31913193
}
31923194
return true;
31933195
}
@@ -3197,9 +3199,9 @@ SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(const DWARFDIE &die) {
31973199
GetObjectFile()->GetModule()->LogMessage(
31983200
log,
31993201
"SymbolFileDWARF::"
3200-
"FindDefinitionTypeForDWARFDeclContext(tag={0}, "
3201-
"name='{1}') trying die={2:x16} ({3})",
3202-
DW_TAG_value_to_name(tag), die.GetName(), type_die.GetOffset(),
3202+
"FindDefinitionTypeForDWARFDeclContext(tag={0} ({1}), name='{2}') "
3203+
"trying die={3:x16} ({4})",
3204+
DW_TAG_value_to_name(tag), tag, die.GetName(), type_die.GetOffset(),
32033205
type_dwarf_decl_ctx.GetQualifiedName());
32043206
}
32053207

@@ -3650,8 +3652,8 @@ VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc,
36503652
StreamString strm;
36513653
location->DumpLocation(&strm, eDescriptionLevelFull, nullptr);
36523654
GetObjectFile()->GetModule()->ReportError(
3653-
"{0:x16}: {1} has an invalid location: {2}", die.GetOffset(),
3654-
die.GetTagAsCString(), strm.GetData());
3655+
"{0:x16}: {1} ({2}) has an invalid location: {3}", die.GetOffset(),
3656+
DW_TAG_value_to_name(die.Tag()), die.Tag(), strm.GetData());
36553657
}
36563658
if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
36573659
is_static_lifetime = true;
@@ -3839,19 +3841,20 @@ void SymbolFileDWARF::ParseAndAppendGlobalVariable(
38393841
variable_list_sp = sc.comp_unit->GetVariableList(false);
38403842
} else {
38413843
GetObjectFile()->GetModule()->ReportError(
3842-
"parent {0:x8} {1} with no valid compile unit in "
3843-
"symbol context for {2:x8} {3}.\n",
3844-
sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(), die.GetID(),
3845-
die.GetTagAsCString());
3844+
"parent {0:x8} {1} ({2}) with no valid compile unit in "
3845+
"symbol context for {3:x8} {4} ({5}).\n",
3846+
sc_parent_die.GetID(), DW_TAG_value_to_name(sc_parent_die.Tag()),
3847+
sc_parent_die.Tag(), die.GetID(), DW_TAG_value_to_name(die.Tag()),
3848+
die.Tag());
38463849
return;
38473850
}
38483851
break;
38493852

38503853
default:
38513854
GetObjectFile()->GetModule()->ReportError(
38523855
"didn't find appropriate parent DIE for variable list for {0:x8} "
3853-
"{1}.\n",
3854-
die.GetID(), die.GetTagAsCString());
3856+
"{1} ({2}).\n",
3857+
die.GetID(), DW_TAG_value_to_name(die.Tag()), die.Tag());
38553858
return;
38563859
}
38573860

0 commit comments

Comments
 (0)