@@ -107,10 +107,22 @@ 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::getChildren () const {
115
+ LVElements Elements (getUnsortedChildren ());
116
+ if (LVSortFunction SortFunction = getSortFunction ()) {
117
+ llvm::stable_sort (Elements, SortFunction);
118
+ } else {
119
+ // No specific sort function, sort by `LVObject::ID` which replicates the
120
+ // order in which the elements were created.
121
+ llvm::stable_sort (Elements, [](const LVObject *LHS, const LVObject *RHS) {
122
+ return LHS.ID < RHS.ID ;
123
+ });
124
+ }
125
+ return Elements;
114
126
}
115
127
116
128
void LVScope::addElement (LVElement *Element) {
@@ -175,7 +187,6 @@ void LVScope::addElement(LVScope *Scope) {
175
187
176
188
// Add it to parent.
177
189
Scopes->push_back (Scope);
178
- addToChildren (Scope);
179
190
Scope->setParent (this );
180
191
181
192
// Notify the reader about the new element being added.
@@ -202,7 +213,6 @@ void LVScope::addElement(LVSymbol *Symbol) {
202
213
203
214
// Add it to parent.
204
215
Symbols->push_back (Symbol);
205
- addToChildren (Symbol);
206
216
Symbol->setParent (this );
207
217
208
218
// Notify the reader about the new element being added.
@@ -229,7 +239,6 @@ void LVScope::addElement(LVType *Type) {
229
239
230
240
// Add it to parent.
231
241
Types->push_back (Type);
232
- addToChildren (Type);
233
242
Type->setParent (this );
234
243
235
244
// Notify the reader about the new element being added.
@@ -277,15 +286,12 @@ bool LVScope::removeElement(LVElement *Element) {
277
286
if (Element->getIsLine ())
278
287
return RemoveElement (Lines);
279
288
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
- }
289
+ if (Element->getIsSymbol ())
290
+ return RemoveElement (Symbols);
291
+ if (Element->getIsType ())
292
+ return RemoveElement (Types);
293
+ if (Element->getIsScope ())
294
+ return RemoveElement (Scopes);
289
295
290
296
return false ;
291
297
}
@@ -356,9 +362,8 @@ void LVScope::updateLevel(LVScope *Parent, bool Moved) {
356
362
setLevel (Parent->getLevel () + 1 );
357
363
358
364
// Update the children.
359
- if (Children)
360
- for (LVElement *Element : *Children)
361
- Element->updateLevel (this , Moved);
365
+ for (LVElement *Element : getChildren ())
366
+ Element->updateLevel (this , Moved);
362
367
363
368
// Update any lines.
364
369
if (Lines)
@@ -374,13 +379,12 @@ void LVScope::resolve() {
374
379
LVElement::resolve ();
375
380
376
381
// 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
- }
382
+ for (LVElement *Element : getChildren ()) {
383
+ if (getIsGlobalReference ())
384
+ // If the scope is a global reference, mark all its children as well.
385
+ Element->setIsGlobalReference ();
386
+ Element->resolve ();
387
+ }
384
388
}
385
389
386
390
void LVScope::resolveName () {
@@ -633,14 +637,13 @@ Error LVScope::doPrint(bool Split, bool Match, bool Print, raw_ostream &OS,
633
637
options ().getPrintFormatting () &&
634
638
getLevel () < options ().getOutputLevel ()) {
635
639
// 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
- }
640
+ for (const LVElement *Element : getChildren ()) {
641
+ if (Match && !Element->getHasPattern ())
642
+ continue ;
643
+ if (Error Err =
644
+ Element->doPrint (Split, Match, Print, *StreamSplit, Full))
645
+ return Err;
646
+ }
644
647
645
648
// Print the line records.
646
649
if (Lines)
@@ -692,7 +695,6 @@ void LVScope::sort() {
692
695
Traverse (Parent->Symbols , SortFunction);
693
696
Traverse (Parent->Scopes , SortFunction);
694
697
Traverse (Parent->Ranges , compareRange);
695
- Traverse (Parent->Children , SortFunction);
696
698
697
699
if (Parent->Scopes )
698
700
for (LVScope *Scope : *Parent->Scopes )
@@ -978,9 +980,8 @@ bool LVScope::equals(const LVScopes *References, const LVScopes *Targets) {
978
980
void LVScope::report (LVComparePass Pass) {
979
981
getComparator ().printItem (this , Pass);
980
982
getComparator ().push (this );
981
- if (Children)
982
- for (LVElement *Element : *Children)
983
- Element->report (Pass);
983
+ for (LVElement *Element : getChildren ())
984
+ Element->report (Pass);
984
985
985
986
if (Lines)
986
987
for (LVLine *Line : *Lines)
@@ -1656,9 +1657,8 @@ void LVScopeCompileUnit::printMatchedElements(raw_ostream &OS,
1656
1657
// Print the view for the matched scopes.
1657
1658
for (const LVScope *Scope : MatchedScopes) {
1658
1659
Scope->print (OS);
1659
- if (const LVElements *Elements = Scope->getChildren ())
1660
- for (LVElement *Element : *Elements)
1661
- Element->print (OS);
1660
+ for (LVElement *Element : Scope->getChildren ())
1661
+ Element->print (OS);
1662
1662
}
1663
1663
}
1664
1664
0 commit comments