File tree Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -230,7 +230,22 @@ class Parser {
230
230
// Cut off parsing by acting as if we reached the end-of-file.
231
231
Tok.setKind (tok::eof);
232
232
}
233
-
233
+
234
+ // / Use this to assert that the parser has advanced the lexing location, e.g.
235
+ // / before a specific parser function has returned.
236
+ class AssertParserMadeProgressBeforeLeavingScopeRAII {
237
+ Parser &P;
238
+ SourceLoc InitialLoc;
239
+ public:
240
+ AssertParserMadeProgressBeforeLeavingScopeRAII (Parser &parser) : P(parser) {
241
+ InitialLoc = P.Tok .getLoc ();
242
+ }
243
+ ~AssertParserMadeProgressBeforeLeavingScopeRAII () {
244
+ assert (InitialLoc != P.Tok .getLoc () &&
245
+ " parser did not make progress, this can result in infinite loop" );
246
+ }
247
+ };
248
+
234
249
// / A RAII object for temporarily changing CurDeclContext.
235
250
class ContextChange {
236
251
protected:
Original file line number Diff line number Diff line change @@ -36,6 +36,9 @@ using namespace swift::syntax;
36
36
// / isStartOfStmt - Return true if the current token starts a statement.
37
37
// /
38
38
bool Parser::isStartOfStmt () {
39
+ // This needs to be kept in sync with `Parser::parseStmt()`. If a new token
40
+ // kind is accepted here as start of statement, it should also be handled in
41
+ // `Parser::parseStmt()`.
39
42
switch (Tok.getKind ()) {
40
43
default : return false ;
41
44
case tok::kw_return:
@@ -565,6 +568,8 @@ static ParserResult<Stmt> recoverFromInvalidCase(Parser &P) {
565
568
}
566
569
567
570
ParserResult<Stmt> Parser::parseStmt () {
571
+ AssertParserMadeProgressBeforeLeavingScopeRAII apmp (*this );
572
+
568
573
SyntaxParsingContext LocalContext (SyntaxContext, SyntaxContextKind::Stmt);
569
574
570
575
// Note that we're parsing a statement.
@@ -588,7 +593,9 @@ ParserResult<Stmt> Parser::parseStmt() {
588
593
if (isContextualYieldKeyword ()) {
589
594
Tok.setKind (tok::kw_yield);
590
595
}
591
-
596
+
597
+ // This needs to handle everything that `Parser::isStartOfStmt()` accepts as
598
+ // start of statement.
592
599
switch (Tok.getKind ()) {
593
600
case tok::pound_line:
594
601
case tok::pound_sourceLocation:
You can’t perform that action at this time.
0 commit comments