@@ -662,8 +662,9 @@ class NodeAdder
662
662
NullablePtr<ASTScopeImpl> visitTopLevelCodeDecl (TopLevelCodeDecl *d,
663
663
ASTScopeImpl *p,
664
664
ScopeCreator &scopeCreator) {
665
- return scopeCreator.ifUniqueConstructExpandAndInsert <TopLevelCodeScope>(p,
666
- d);
665
+ ASTScopeAssert (endLoc.hasValue (), " TopLevelCodeDecl in wrong place?" );
666
+ return scopeCreator.ifUniqueConstructExpandAndInsert <TopLevelCodeScope>(
667
+ p, d, *endLoc);
667
668
}
668
669
669
670
#pragma mark special-case creation
@@ -708,9 +709,14 @@ class NodeAdder
708
709
}
709
710
}
710
711
712
+ SourceLoc endLocForBraceStmt = bs->getEndLoc ();
713
+ if (endLoc.hasValue ())
714
+ endLocForBraceStmt = *endLoc;
715
+
711
716
auto maybeBraceScope =
712
717
scopeCreator.ifUniqueConstructExpandAndInsert <BraceStmtScope>(
713
- p, bs, std::move (localFuncsAndTypes), std::move (localVars));
718
+ p, bs, std::move (localFuncsAndTypes), std::move (localVars),
719
+ endLocForBraceStmt);
714
720
if (auto *s = scopeCreator.getASTContext ().Stats )
715
721
++s->getFrontendCounters ().NumBraceStmtASTScopes ;
716
722
@@ -989,12 +995,17 @@ ASTSourceFileScope::expandAScopeThatCreatesANewInsertionPoint(
989
995
ASTScopeAssert (SF, " Must already have a SourceFile." );
990
996
ArrayRef<Decl *> decls = SF->getTopLevelDecls ();
991
997
// Assume that decls are only added at the end, in source order
998
+ Optional<SourceLoc> endLoc = None;
999
+ if (!decls.empty ())
1000
+ endLoc = decls.back ()->getEndLoc ();
1001
+
992
1002
std::vector<ASTNode> newNodes (decls.begin (), decls.end ());
993
1003
insertionPoint =
994
1004
scopeCreator.addSiblingsToScopeTree (insertionPoint,
995
1005
scopeCreator.sortBySourceRange (
996
1006
scopeCreator.cull (newNodes)),
997
- None);
1007
+ endLoc);
1008
+
998
1009
// Too slow to perform all the time:
999
1010
// ASTScopeAssert(scopeCreator->containsAllDeclContextsFromAST(),
1000
1011
// "ASTScope tree missed some DeclContexts or made some up");
@@ -1123,7 +1134,7 @@ BraceStmtScope::expandAScopeThatCreatesANewInsertionPoint(
1123
1134
scopeCreator.sortBySourceRange (
1124
1135
scopeCreator.cull (
1125
1136
stmt->getElements ())),
1126
- stmt-> getEndLoc () );
1137
+ endLoc );
1127
1138
if (auto *s = scopeCreator.getASTContext ().Stats )
1128
1139
++s->getFrontendCounters ().NumBraceStmtASTScopeExpansions ;
1129
1140
return {
@@ -1137,7 +1148,7 @@ TopLevelCodeScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
1137
1148
1138
1149
if (auto *body =
1139
1150
scopeCreator
1140
- .addToScopeTreeAndReturnInsertionPoint (decl->getBody (), this , None )
1151
+ .addToScopeTreeAndReturnInsertionPoint (decl->getBody (), this , endLoc )
1141
1152
.getPtrOrNull ())
1142
1153
return {body, " So next top level code scope and put its decls in its body "
1143
1154
" under a guard statement scope (etc) from the last top level "
0 commit comments