Skip to content

Commit 136879a

Browse files
[lldb][NFC] Simplify DWARRFDeclContext::GetQualifiedName (llvm#74788)
This commit factors out the logic building each component of a qualified name into its own function so that it may be reused by a future commit, while also simplifying the logic of assembling these pieces together by using llvm::interleave. (cherry picked from commit 06d6af7)
1 parent 5f7e6f9 commit 136879a

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,27 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "DWARFDeclContext.h"
10+
#include "llvm/Support/raw_ostream.h"
1011

1112
using namespace lldb_private::dwarf;
1213
using namespace lldb_private::plugin::dwarf;
1314

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)
21+
return "(anonymous namespace)";
22+
if (entry.tag == DW_TAG_class_type)
23+
return "(anonymous class)";
24+
if (entry.tag == DW_TAG_structure_type)
25+
return "(anonymous struct)";
26+
if (entry.tag == DW_TAG_union_type)
27+
return "(anonymous union)";
28+
return "(anonymous)";
29+
}
30+
1431
const char *DWARFDeclContext::GetQualifiedName() const {
1532
if (m_qualified_name.empty()) {
1633
// The declaration context array for a class named "foo" in namespace
@@ -26,26 +43,10 @@ const char *DWARFDeclContext::GetQualifiedName() const {
2643
m_qualified_name.append(m_entries[0].name);
2744
}
2845
} else {
29-
collection::const_reverse_iterator pos;
30-
collection::const_reverse_iterator begin = m_entries.rbegin();
31-
collection::const_reverse_iterator end = m_entries.rend();
32-
for (pos = begin; pos != end; ++pos) {
33-
if (pos != begin)
34-
m_qualified_name.append("::");
35-
if (pos->name == nullptr) {
36-
if (pos->tag == DW_TAG_namespace)
37-
m_qualified_name.append("(anonymous namespace)");
38-
else if (pos->tag == DW_TAG_class_type)
39-
m_qualified_name.append("(anonymous class)");
40-
else if (pos->tag == DW_TAG_structure_type)
41-
m_qualified_name.append("(anonymous struct)");
42-
else if (pos->tag == DW_TAG_union_type)
43-
m_qualified_name.append("(anonymous union)");
44-
else
45-
m_qualified_name.append("(anonymous)");
46-
} else
47-
m_qualified_name.append(pos->name);
48-
}
46+
llvm::raw_string_ostream string_stream(m_qualified_name);
47+
llvm::interleave(
48+
llvm::reverse(m_entries), string_stream,
49+
[&](auto entry) { string_stream << GetName(entry); }, "::");
4950
}
5051
}
5152
}

0 commit comments

Comments
 (0)