@@ -107,10 +107,16 @@ 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);
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;
114120}
115121
116122void LVScope::addElement (LVElement *Element) {
@@ -175,7 +181,6 @@ void LVScope::addElement(LVScope *Scope) {
175181
176182 // Add it to parent.
177183 Scopes->push_back (Scope);
178- addToChildren (Scope);
179184 Scope->setParent (this );
180185
181186 // Notify the reader about the new element being added.
@@ -202,7 +207,6 @@ void LVScope::addElement(LVSymbol *Symbol) {
202207
203208 // Add it to parent.
204209 Symbols->push_back (Symbol);
205- addToChildren (Symbol);
206210 Symbol->setParent (this );
207211
208212 // Notify the reader about the new element being added.
@@ -229,7 +233,6 @@ void LVScope::addElement(LVType *Type) {
229233
230234 // Add it to parent.
231235 Types->push_back (Type);
232- addToChildren (Type);
233236 Type->setParent (this );
234237
235238 // Notify the reader about the new element being added.
@@ -277,15 +280,12 @@ bool LVScope::removeElement(LVElement *Element) {
277280 if (Element->getIsLine ())
278281 return RemoveElement (Lines);
279282
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);
289289
290290 return false ;
291291}
@@ -356,9 +356,8 @@ void LVScope::updateLevel(LVScope *Parent, bool Moved) {
356356 setLevel (Parent->getLevel () + 1 );
357357
358358 // 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);
362361
363362 // Update any lines.
364363 if (Lines)
@@ -374,13 +373,12 @@ void LVScope::resolve() {
374373 LVElement::resolve ();
375374
376375 // 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+ }
384382}
385383
386384void LVScope::resolveName () {
@@ -633,14 +631,13 @@ Error LVScope::doPrint(bool Split, bool Match, bool Print, raw_ostream &OS,
633631 options ().getPrintFormatting () &&
634632 getLevel () < options ().getOutputLevel ()) {
635633 // 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+ }
644641
645642 // Print the line records.
646643 if (Lines)
@@ -692,7 +689,6 @@ void LVScope::sort() {
692689 Traverse (Parent->Symbols , SortFunction);
693690 Traverse (Parent->Scopes , SortFunction);
694691 Traverse (Parent->Ranges , compareRange);
695- Traverse (Parent->Children , SortFunction);
696692
697693 if (Parent->Scopes )
698694 for (LVScope *Scope : *Parent->Scopes )
@@ -978,9 +974,8 @@ bool LVScope::equals(const LVScopes *References, const LVScopes *Targets) {
978974void LVScope::report (LVComparePass Pass) {
979975 getComparator ().printItem (this , Pass);
980976 getComparator ().push (this );
981- if (Children)
982- for (LVElement *Element : *Children)
983- Element->report (Pass);
977+ for (LVElement *Element : getSortedChildren ())
978+ Element->report (Pass);
984979
985980 if (Lines)
986981 for (LVLine *Line : *Lines)
@@ -1656,9 +1651,8 @@ void LVScopeCompileUnit::printMatchedElements(raw_ostream &OS,
16561651 // Print the view for the matched scopes.
16571652 for (const LVScope *Scope : MatchedScopes) {
16581653 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);
16621656 }
16631657 }
16641658
0 commit comments