@@ -107,10 +107,16 @@ LVScopeDispatch LVScope::Dispatch = {
107
107
{LVScopeKind::IsTryBlock, &LVScope::getIsTryBlock},
108
108
{LVScopeKind::IsUnion, &LVScope::getIsUnion}};
109
109
110
- void LVScope::addToChildren (LVElement *Element) {
111
- if (!Children)
112
- Children = std::make_unique<LVElements>();
113
- Children->push_back (Element);
110
+ const LVTypes LVScope::EmptyTypes{};
111
+ const LVSymbols LVScope::EmptySymbols{};
112
+ const LVScopes LVScope::EmptyScopes{};
113
+
114
+ LVElements LVScope::getSortedChildren (LVSortFunction SortFunction) const {
115
+ const auto UnsortedChildren = getChildren ();
116
+ LVElements Elements{UnsortedChildren.begin (), UnsortedChildren.end ()};
117
+ if (SortFunction)
118
+ llvm::stable_sort (Elements, SortFunction);
119
+ return Elements;
114
120
}
115
121
116
122
void LVScope::addElement (LVElement *Element) {
@@ -175,7 +181,6 @@ void LVScope::addElement(LVScope *Scope) {
175
181
176
182
// Add it to parent.
177
183
Scopes->push_back (Scope);
178
- addToChildren (Scope);
179
184
Scope->setParent (this );
180
185
181
186
// Notify the reader about the new element being added.
@@ -202,7 +207,6 @@ void LVScope::addElement(LVSymbol *Symbol) {
202
207
203
208
// Add it to parent.
204
209
Symbols->push_back (Symbol);
205
- addToChildren (Symbol);
206
210
Symbol->setParent (this );
207
211
208
212
// Notify the reader about the new element being added.
@@ -229,7 +233,6 @@ void LVScope::addElement(LVType *Type) {
229
233
230
234
// Add it to parent.
231
235
Types->push_back (Type);
232
- addToChildren (Type);
233
236
Type->setParent (this );
234
237
235
238
// Notify the reader about the new element being added.
@@ -277,15 +280,12 @@ bool LVScope::removeElement(LVElement *Element) {
277
280
if (Element->getIsLine ())
278
281
return RemoveElement (Lines);
279
282
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
- }
283
+ if (Element->getIsSymbol ())
284
+ return RemoveElement (Symbols);
285
+ if (Element->getIsType ())
286
+ return RemoveElement (Types);
287
+ if (Element->getIsScope ())
288
+ return RemoveElement (Scopes);
289
289
290
290
return false ;
291
291
}
@@ -356,9 +356,8 @@ void LVScope::updateLevel(LVScope *Parent, bool Moved) {
356
356
setLevel (Parent->getLevel () + 1 );
357
357
358
358
// Update the children.
359
- if (Children)
360
- for (LVElement *Element : *Children)
361
- Element->updateLevel (this , Moved);
359
+ for (LVElement *Element : getChildren ())
360
+ Element->updateLevel (this , Moved);
362
361
363
362
// Update any lines.
364
363
if (Lines)
@@ -374,13 +373,12 @@ void LVScope::resolve() {
374
373
LVElement::resolve ();
375
374
376
375
// Resolve the children.
377
- if (Children)
378
- for (LVElement *Element : *Children) {
379
- if (getIsGlobalReference ())
380
- // If the scope is a global reference, mark all its children as well.
381
- Element->setIsGlobalReference ();
382
- Element->resolve ();
383
- }
376
+ for (LVElement *Element : getChildren ()) {
377
+ if (getIsGlobalReference ())
378
+ // If the scope is a global reference, mark all its children as well.
379
+ Element->setIsGlobalReference ();
380
+ Element->resolve ();
381
+ }
384
382
}
385
383
386
384
void LVScope::resolveName () {
@@ -633,14 +631,13 @@ Error LVScope::doPrint(bool Split, bool Match, bool Print, raw_ostream &OS,
633
631
options ().getPrintFormatting () &&
634
632
getLevel () < options ().getOutputLevel ()) {
635
633
// Print the children.
636
- if (Children)
637
- for (const LVElement *Element : *Children) {
638
- if (Match && !Element->getHasPattern ())
639
- continue ;
640
- if (Error Err =
641
- Element->doPrint (Split, Match, Print, *StreamSplit, Full))
642
- return Err;
643
- }
634
+ for (const LVElement *Element : getSortedChildren ()) {
635
+ if (Match && !Element->getHasPattern ())
636
+ continue ;
637
+ if (Error Err =
638
+ Element->doPrint (Split, Match, Print, *StreamSplit, Full))
639
+ return Err;
640
+ }
644
641
645
642
// Print the line records.
646
643
if (Lines)
@@ -692,7 +689,6 @@ void LVScope::sort() {
692
689
Traverse (Parent->Symbols , SortFunction);
693
690
Traverse (Parent->Scopes , SortFunction);
694
691
Traverse (Parent->Ranges , compareRange);
695
- Traverse (Parent->Children , SortFunction);
696
692
697
693
if (Parent->Scopes )
698
694
for (LVScope *Scope : *Parent->Scopes )
@@ -978,9 +974,8 @@ bool LVScope::equals(const LVScopes *References, const LVScopes *Targets) {
978
974
void LVScope::report (LVComparePass Pass) {
979
975
getComparator ().printItem (this , Pass);
980
976
getComparator ().push (this );
981
- if (Children)
982
- for (LVElement *Element : *Children)
983
- Element->report (Pass);
977
+ for (LVElement *Element : getSortedChildren ())
978
+ Element->report (Pass);
984
979
985
980
if (Lines)
986
981
for (LVLine *Line : *Lines)
@@ -1656,9 +1651,8 @@ void LVScopeCompileUnit::printMatchedElements(raw_ostream &OS,
1656
1651
// Print the view for the matched scopes.
1657
1652
for (const LVScope *Scope : MatchedScopes) {
1658
1653
Scope->print (OS);
1659
- if (const LVElements *Elements = Scope->getChildren ())
1660
- for (LVElement *Element : *Elements)
1661
- Element->print (OS);
1654
+ for (LVElement *Element : Scope->getSortedChildren ())
1655
+ Element->print (OS);
1662
1656
}
1663
1657
}
1664
1658
0 commit comments