Skip to content

Commit 00e81f8

Browse files
committed
[llvm-debuginfo-analyzer] Remove LVScope::Children container
Remove the `LVScope::Children` container and use `llvm::concat()` instead to return a view over the types, symbols, and sub-scopes contained in a given `LVScope`.
1 parent 560fccc commit 00e81f8

File tree

3 files changed

+37
-45
lines changed

3 files changed

+37
-45
lines changed

llvm/include/llvm/DebugInfo/LogicalView/Core/LVScope.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSCOPE_H
1515
#define LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSCOPE_H
1616

17+
#include "llvm/ADT/STLExtras.h"
1718
#include "llvm/DebugInfo/LogicalView/Core/LVElement.h"
1819
#include "llvm/DebugInfo/LogicalView/Core/LVLocation.h"
1920
#include "llvm/DebugInfo/LogicalView/Core/LVSort.h"
@@ -94,6 +95,11 @@ class LLVM_ABI LVScope : public LVElement {
9495
LVProperties<LVScopeKind> Kinds;
9596
LVProperties<Property> Properties;
9697
static LVScopeDispatch Dispatch;
98+
// Empty containers used in `getChildren()` in case there is no Types,
99+
// Symbols, or Scopes.
100+
static const LVTypes EmptyTypes;
101+
static const LVSymbols EmptySymbols;
102+
static const LVScopes EmptyScopes;
97103

98104
// Size in bits if this scope represents also a compound type.
99105
uint32_t BitSize = 0;
@@ -128,14 +134,6 @@ class LLVM_ABI LVScope : public LVElement {
128134
std::unique_ptr<LVLines> Lines;
129135
std::unique_ptr<LVLocations> Ranges;
130136

131-
// Vector of elements (types, scopes and symbols).
132-
// It is the union of (*Types, *Symbols and *Scopes) to be used for
133-
// the following reasons:
134-
// - Preserve the order the logical elements are read in.
135-
// - To have a single container with all the logical elements, when
136-
// the traversal does not require any specific element kind.
137-
std::unique_ptr<LVElements> Children;
138-
139137
// Resolve the template parameters/arguments relationship.
140138
void resolveTemplate();
141139
void printEncodedArgs(raw_ostream &OS, bool Full) const;
@@ -213,7 +211,11 @@ class LLVM_ABI LVScope : public LVElement {
213211
const LVScopes *getScopes() const { return Scopes.get(); }
214212
const LVSymbols *getSymbols() const { return Symbols.get(); }
215213
const LVTypes *getTypes() const { return Types.get(); }
216-
const LVElements *getChildren() const { return Children.get(); }
214+
auto getChildren() const {
215+
return llvm::concat<LVElement *const>(Types ? *Types : EmptyTypes,
216+
Symbols ? *Symbols : EmptySymbols,
217+
Scopes ? *Scopes : EmptyScopes);
218+
}
217219

218220
void addElement(LVElement *Element);
219221
void addElement(LVLine *Line);
@@ -222,7 +224,6 @@ class LLVM_ABI LVScope : public LVElement {
222224
void addElement(LVType *Type);
223225
void addObject(LVLocation *Location);
224226
void addObject(LVAddress LowerAddress, LVAddress UpperAddress);
225-
void addToChildren(LVElement *Element);
226227

227228
// Add the missing elements from the given 'Reference', which is the
228229
// scope associated with any DW_AT_specification, DW_AT_abstract_origin.

llvm/lib/DebugInfo/LogicalView/Core/LVScope.cpp

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,9 @@ LVScopeDispatch LVScope::Dispatch = {
107107
{LVScopeKind::IsTryBlock, &LVScope::getIsTryBlock},
108108
{LVScopeKind::IsUnion, &LVScope::getIsUnion}};
109109

110-
void LVScope::addToChildren(LVElement *Element) {
111-
if (!Children)
112-
Children = std::make_unique<LVElements>();
113-
Children->push_back(Element);
114-
}
110+
const LVTypes LVScope::EmptyTypes{};
111+
const LVSymbols LVScope::EmptySymbols{};
112+
const LVScopes LVScope::EmptyScopes{};
115113

116114
void LVScope::addElement(LVElement *Element) {
117115
assert(Element && "Invalid element.");
@@ -175,7 +173,6 @@ void LVScope::addElement(LVScope *Scope) {
175173

176174
// Add it to parent.
177175
Scopes->push_back(Scope);
178-
addToChildren(Scope);
179176
Scope->setParent(this);
180177

181178
// Notify the reader about the new element being added.
@@ -202,7 +199,6 @@ void LVScope::addElement(LVSymbol *Symbol) {
202199

203200
// Add it to parent.
204201
Symbols->push_back(Symbol);
205-
addToChildren(Symbol);
206202
Symbol->setParent(this);
207203

208204
// Notify the reader about the new element being added.
@@ -229,7 +225,6 @@ void LVScope::addElement(LVType *Type) {
229225

230226
// Add it to parent.
231227
Types->push_back(Type);
232-
addToChildren(Type);
233228
Type->setParent(this);
234229

235230
// Notify the reader about the new element being added.
@@ -277,15 +272,12 @@ bool LVScope::removeElement(LVElement *Element) {
277272
if (Element->getIsLine())
278273
return RemoveElement(Lines);
279274

280-
if (RemoveElement(Children)) {
281-
if (Element->getIsSymbol())
282-
return RemoveElement(Symbols);
283-
if (Element->getIsType())
284-
return RemoveElement(Types);
285-
if (Element->getIsScope())
286-
return RemoveElement(Scopes);
287-
llvm_unreachable("Invalid element.");
288-
}
275+
if (Element->getIsSymbol())
276+
return RemoveElement(Symbols);
277+
if (Element->getIsType())
278+
return RemoveElement(Types);
279+
if (Element->getIsScope())
280+
return RemoveElement(Scopes);
289281

290282
return false;
291283
}
@@ -356,8 +348,8 @@ void LVScope::updateLevel(LVScope *Parent, bool Moved) {
356348
setLevel(Parent->getLevel() + 1);
357349

358350
// Update the children.
359-
if (Children)
360-
for (LVElement *Element : *Children)
351+
if (auto Elements = getChildren(); Elements.begin() != Elements.end())
352+
for (LVElement *Element : Elements)
361353
Element->updateLevel(this, Moved);
362354

363355
// Update any lines.
@@ -374,8 +366,8 @@ void LVScope::resolve() {
374366
LVElement::resolve();
375367

376368
// Resolve the children.
377-
if (Children)
378-
for (LVElement *Element : *Children) {
369+
if (auto Elements = getChildren(); Elements.begin() != Elements.end())
370+
for (LVElement *Element : Elements) {
379371
if (getIsGlobalReference())
380372
// If the scope is a global reference, mark all its children as well.
381373
Element->setIsGlobalReference();
@@ -633,8 +625,9 @@ Error LVScope::doPrint(bool Split, bool Match, bool Print, raw_ostream &OS,
633625
options().getPrintFormatting() &&
634626
getLevel() < options().getOutputLevel()) {
635627
// Print the children.
636-
if (Children)
637-
for (const LVElement *Element : *Children) {
628+
if (const auto Elements = getChildren();
629+
Elements.begin() != Elements.end())
630+
for (const LVElement *Element : Elements) {
638631
if (Match && !Element->getHasPattern())
639632
continue;
640633
if (Error Err =
@@ -692,7 +685,6 @@ void LVScope::sort() {
692685
Traverse(Parent->Symbols, SortFunction);
693686
Traverse(Parent->Scopes, SortFunction);
694687
Traverse(Parent->Ranges, compareRange);
695-
Traverse(Parent->Children, SortFunction);
696688

697689
if (Parent->Scopes)
698690
for (LVScope *Scope : *Parent->Scopes)
@@ -978,8 +970,8 @@ bool LVScope::equals(const LVScopes *References, const LVScopes *Targets) {
978970
void LVScope::report(LVComparePass Pass) {
979971
getComparator().printItem(this, Pass);
980972
getComparator().push(this);
981-
if (Children)
982-
for (LVElement *Element : *Children)
973+
if (auto Elements = getChildren(); Elements.begin() != Elements.end())
974+
for (LVElement *Element : Elements)
983975
Element->report(Pass);
984976

985977
if (Lines)
@@ -1656,8 +1648,9 @@ void LVScopeCompileUnit::printMatchedElements(raw_ostream &OS,
16561648
// Print the view for the matched scopes.
16571649
for (const LVScope *Scope : MatchedScopes) {
16581650
Scope->print(OS);
1659-
if (const LVElements *Elements = Scope->getChildren())
1660-
for (LVElement *Element : *Elements)
1651+
if (const auto Elements = Scope->getChildren();
1652+
Elements.begin() != Elements.end())
1653+
for (LVElement *Element : Elements)
16611654
Element->print(OS);
16621655
}
16631656
}

llvm/unittests/DebugInfo/LogicalView/DWARFReaderTest.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,12 @@ void checkUnspecifiedParameters(LVReader *Reader) {
163163
LVPublicNames::const_iterator IterNames = PublicNames.cbegin();
164164
LVScope *Function = (*IterNames).first;
165165
EXPECT_EQ(Function->getName(), "foo_printf");
166-
const LVElements *Elements = Function->getChildren();
167-
ASSERT_NE(Elements, nullptr);
166+
const auto Elements = Function->getChildren();
168167
// foo_printf is a variadic function whose prototype is
169168
// `int foo_printf(const char *, ...)`, where the '...' is represented by a
170169
// DW_TAG_unspecified_parameters, i.e. we expect to find at least one child
171170
// for which getIsUnspecified() returns true.
172-
EXPECT_TRUE(llvm::any_of(*Elements, [](const LVElement *elt) {
171+
EXPECT_TRUE(llvm::any_of(Elements, [](const LVElement *elt) {
173172
return elt->getIsSymbol() &&
174173
static_cast<const LVSymbol *>(elt)->getIsUnspecified();
175174
}));
@@ -183,10 +182,9 @@ void checkScopeModule(LVReader *Reader) {
183182
EXPECT_EQ(Root->getFileFormatName(), "Mach-O 64-bit x86-64");
184183
EXPECT_EQ(Root->getName(), DwarfClangModule);
185184

186-
ASSERT_NE(CompileUnit->getChildren(), nullptr);
187-
LVElement *FirstChild = *(CompileUnit->getChildren()->begin());
188-
EXPECT_EQ(FirstChild->getIsScope(), 1);
189-
LVScopeModule *Module = static_cast<LVScopeModule *>(FirstChild);
185+
ASSERT_NE(CompileUnit->getScopes(), nullptr);
186+
LVElement *FirstScope = *(CompileUnit->getScopes()->begin());
187+
LVScopeModule *Module = static_cast<LVScopeModule *>(FirstScope);
190188
EXPECT_EQ(Module->getIsModule(), 1);
191189
EXPECT_EQ(Module->getName(), "DebugModule");
192190
}

0 commit comments

Comments
 (0)