Skip to content

Commit 6305529

Browse files
committed
[Parse] Add back TrailingSemiLoc to Expr, Stmt, and Decl
1 parent 0705506 commit 6305529

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,8 @@ class alignas(1 << DeclAlignInBits) Decl {
744744
/// Returns the source range of the entire declaration.
745745
SourceRange getSourceRange() const;
746746

747+
SourceLoc TrailingSemiLoc;
748+
747749
LLVM_ATTRIBUTE_DEPRECATED(
748750
void dump() const LLVM_ATTRIBUTE_USED,
749751
"only for use within the debugger");

include/swift/AST/Expr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,8 @@ class alignas(8) Expr {
500500
SourceLoc getLoc() const { return (SUBEXPR)->getLoc(); } \
501501
SourceRange getSourceRange() const { return (SUBEXPR)->getSourceRange(); }
502502

503+
SourceLoc TrailingSemiLoc;
504+
503505
/// getSemanticsProvidingExpr - Find the smallest subexpression
504506
/// which obeys the property that evaluating it is exactly
505507
/// equivalent to evaluating this expression.

include/swift/AST/Stmt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class alignas(8) Stmt {
7878
SourceLoc getEndLoc() const;
7979

8080
SourceRange getSourceRange() const;
81+
SourceLoc TrailingSemiLoc;
8182

8283
/// isImplicit - Determines whether this statement was implicitly-generated,
8384
/// rather than explicitly written in the AST.

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,11 +2733,14 @@ static ParserStatus parseDeclItem(Parser &P,
27332733
.fixItInsert(endOfPrevious, ";");
27342734
}
27352735

2736-
ParserStatus Status = P.parseDecl(Options, handler);
2737-
if (Status.isError())
2736+
auto Result = P.parseDecl(Options, handler);
2737+
if (Result.isParseError())
27382738
P.skipUntilDeclRBrace(tok::semi, tok::pound_endif);
2739-
PreviousHadSemi = P.consumeIf(tok::semi);
2740-
return Status;
2739+
SourceLoc SemiLoc;
2740+
PreviousHadSemi = P.consumeIf(tok::semi, SemiLoc);
2741+
if (PreviousHadSemi && Result.isNonNull())
2742+
Result.get()->TrailingSemiLoc = SemiLoc;
2743+
return Result;
27412744
}
27422745

27432746
/// \brief Parse the members in a struct/class/enum/protocol/extension.

lib/Parse/ParseStmt.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,18 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
297297
PreviousHadSemi = false;
298298
if (isStartOfDecl()
299299
&& Tok.isNot(tok::pound_if, tok::pound_sourceLocation)) {
300-
ParseDeclOptions options = IsTopLevel ? PD_AllowTopLevel : PD_Default;
301-
ParserStatus Status =
302-
parseDecl(options, [&](Decl *D) { TmpDecls.push_back(D); });
303-
if (Status.isError()) {
300+
ParserResult<Decl> DeclResult =
301+
parseDecl(IsTopLevel ? PD_AllowTopLevel : PD_Default,
302+
[&](Decl *D) {TmpDecls.push_back(D);});
303+
if (DeclResult.isParseError()) {
304304
NeedParseErrorRecovery = true;
305-
if (Status.hasCodeCompletion() && IsTopLevel &&
305+
if (DeclResult.hasCodeCompletion() && IsTopLevel &&
306306
isCodeCompletionFirstPass()) {
307307
consumeDecl(BeginParserPosition, None, IsTopLevel);
308-
return Status;
308+
return DeclResult;
309309
}
310310
}
311+
Result = DeclResult.getPtrOrNull();
311312

312313
for (Decl *D : TmpDecls)
313314
Entries.push_back(D);
@@ -412,9 +413,16 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
412413
Entries.push_back(Result);
413414
}
414415

415-
if (!NeedParseErrorRecovery && !PreviousHadSemi && Tok.is(tok::semi)) {
416-
consumeToken();
416+
if (!NeedParseErrorRecovery && Tok.is(tok::semi)) {
417417
PreviousHadSemi = true;
418+
if (Expr *E = Result.dyn_cast<Expr*>())
419+
E->TrailingSemiLoc = consumeToken(tok::semi);
420+
else if (Stmt *S = Result.dyn_cast<Stmt*>())
421+
S->TrailingSemiLoc = consumeToken(tok::semi);
422+
else if (Decl *D = Result.dyn_cast<Decl*>())
423+
D->TrailingSemiLoc = consumeToken(tok::semi);
424+
else
425+
assert(!Result && "Unsupported AST node");
418426
}
419427

420428
if (NeedParseErrorRecovery) {

0 commit comments

Comments
 (0)