Skip to content

Commit a40b694

Browse files
author
David Ungar
committed
Use token start locations
1 parent d84d541 commit a40b694

File tree

5 files changed

+26
-40
lines changed

5 files changed

+26
-40
lines changed

include/swift/Parse/Parser.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -815,13 +815,14 @@ class Parser {
815815
/// Returns the proper location for a missing right brace, parenthesis, etc.
816816
SourceLoc getLocForMissingMatchingToken() const;
817817

818-
/// When encountering an error or a missing matching token (e.g. '}') return
819-
/// its location. This value should be right at the end of the last token in
818+
/// When encountering an error or a missing matching token (e.g. '}'), return
819+
/// the location to use for it. This value should be at the last token in
820820
/// the ASTNode being parsed so that it nests within any enclosing nodes, and,
821821
/// for ASTScope lookups, it does not preceed any identifiers to be looked up.
822-
/// The latter case obtaines when the parsing an interpolated string literal
823-
/// because there may be identifiers to be looked up and they must precede the
824-
/// location of a missing close brace.
822+
/// However, the latter case does not hold when parsing an interpolated
823+
/// string literal because there may be identifiers to be looked up in the
824+
/// literal and their locations will not precede the location of a missing
825+
/// close brace.
825826
SourceLoc getErrorOrMissingLoc() const;
826827

827828
/// Parse a comma separated list of some elements.

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3655,7 +3655,7 @@ class Verifier : public ASTWalker {
36553655
return;
36563656
// FIXME: This is not working well for decl parents.
36573657
// But it must work when using lazy ASTScopes for IterableDeclContexts
3658-
if (!Ctx.LangOpts.LazyASTScopes || !isa<IterableDeclContext>(D))
3658+
if (!isa<IterableDeclContext>(D))
36593659
return;
36603660
} else if (Stmt *S = Parent.getAsStmt()) {
36613661
Enclosing = S->getSourceRange();

lib/AST/Expr.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,13 +2228,7 @@ VarDecl * TapExpr::getVar() const {
22282228
}
22292229

22302230
SourceLoc TapExpr::getEndLoc() const {
2231-
// Before LazyASTScopes, was:
2232-
// return SubExpr ? SubExpr->getSourceRange() : SourceRange();
2233-
22342231
// Include the body in the range, assuming the body follows the SubExpr.
2235-
// ASTScopes needs the body in the range in order to do lookups into the
2236-
// expressions in interpolated strings.
2237-
22382232
// Also, be (perhaps overly) defensive about null pointers & invalid
22392233
// locations.
22402234
if (auto *const b = getBody()) {

lib/Parse/ParseExpr.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,11 +1913,6 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
19131913

19141914
// The start location of the entire string literal.
19151915
SourceLoc Loc = Tok.getLoc();
1916-
// Put the close brace of the of the body of the AppendingExpr just inside
1917-
// of the literal so that it doesn't go past where a missing close brace
1918-
// for an enclosing IterableTypeDecl will be added.
1919-
// (See \c parseMatchingToken.)
1920-
SourceLoc EndLoc = Loc.getAdvancedLoc(Tok.getLength() - 1);
19211916

19221917
StringRef OpenDelimiterStr, OpenQuoteStr, CloseQuoteStr, CloseDelimiterStr;
19231918
unsigned DelimiterLength = Tok.getCustomDelimiterLen();
@@ -1996,7 +1991,8 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
19961991
llvm::SaveAndRestore<Token> SavedTok(Tok);
19971992
llvm::SaveAndRestore<ParsedTrivia> SavedLeadingTrivia(LeadingTrivia);
19981993
llvm::SaveAndRestore<ParsedTrivia> SavedTrailingTrivia(TrailingTrivia);
1999-
// For errors, we need the real PreviousLoc
1994+
// For errors, we need the real PreviousLoc, i.e. the start of the
1995+
// whole InterpolatedStringLiteral.
20001996
llvm::SaveAndRestore<SourceLoc> SavedPreviousLoc(PreviousLoc);
20011997

20021998
// We're not in a place where an interpolation would be valid.
@@ -2036,8 +2032,9 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
20362032
Status = parseStringSegments(Segments, EntireTok, InterpolationVar,
20372033
Stmts, LiteralCapacity, InterpolationCount);
20382034

2039-
// See the definition of \c EndLoc above.
2040-
auto Body = BraceStmt::create(Context, Loc, Stmts, EndLoc,
2035+
// At this point, PreviousLoc points to the last token parsed within
2036+
// the body, so use that for the brace statement location.
2037+
auto Body = BraceStmt::create(Context, Loc, Stmts, PreviousLoc,
20412038
/*implicit=*/false);
20422039
AppendingExpr = new (Context) TapExpr(nullptr, Body);
20432040
}

lib/Parse/Parser.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -988,30 +988,24 @@ bool Parser::parseMatchingToken(tok K, SourceLoc &TokLoc, Diag<> ErrorDiag,
988988
}
989989

990990
SourceLoc Parser::getLocForMissingMatchingToken() const {
991-
// The right brace, parenthesis, etc. must include the whole of the previous
992-
// token in order so that an unexpanded lazy \c IterableTypeScope includes its
993-
// contents.
991+
// At present, use the same location whether it's an error or whether
992+
// the matching token is missing.
993+
// Both cases supply a location for something the user didn't type.
994994
return getErrorOrMissingLoc();
995995
}
996996

997997
SourceLoc Parser::getErrorOrMissingLoc() const {
998-
// LazyASTScopes require that the ends of types, etc. which are at the end of
999-
// \c IterableTypeContext \c Decls, such as \c StructDecl not be beyond the
1000-
// end of the enclosing \c Decl.
1001-
1002-
// The missing right brace, parenthesis, etc. must include the whole of the
1003-
// previous token in order so that an unexpanded lazy \c IterableTypeScope
1004-
// includes its contents.
1005-
1006-
auto const PreviousTok = Lexer::getTokenAtLocation(Context.SourceMgr,
1007-
PreviousLoc);
1008-
// If it's a string literal, it might be an InterpolatedStringLiteral.
1009-
// In that case the missing close paren, etc. needs to go at the end
1010-
// because there might be things to be looked up for ASTScope in the
1011-
// middle.
1012-
return PreviousTok.getKind() != tok::string_literal
1013-
? PreviousLoc
1014-
: PreviousLoc.getAdvancedLoc(PreviousTok.getLength() - 1);
998+
// The next token might start a new enclosing construct,
999+
// and SourceLoc's are always at the start of a token (for example, for
1000+
// fixits, so use the previous token's SourceLoc and allow a subnode to end
1001+
// right at the same place as its supernode.
1002+
1003+
// The tricky case is when the previous token is an InterpolatedStringLiteral.
1004+
// Then, there will be names in scope whose SourceLoc is *after* the
1005+
// the location of a missing close brace.
1006+
// ASTScope tree creation will have to cope.
1007+
1008+
return PreviousLoc;
10151009
}
10161010

10171011
static SyntaxKind getListElementKind(SyntaxKind ListKind) {

0 commit comments

Comments
 (0)