[clang] [clang-repl] Address error recovery fixing infinite loop while parsing (PR #127569)

Anutosh Bhat via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 17 21:57:32 PST 2025


https://github.com/anutosh491 created https://github.com/llvm/llvm-project/pull/127569

I was interested in recovering error for some use cases.

Before the change
```
clang-repl> void foo() { int x = 5;
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
fatal error: too many errors emitted, stopping now [-ferror-limit=]
```

After the change
```
clang-repl> void foo() { int x = 5;
<<< inputs >>>:1:1: error: expected '}'
input_line_3:1:12: note: to match this '{'
    1 | void foo() { int x = 5;
      |            ^
error: Parsing failed.
```

Some other use cases could be the following

Before the change
```
clang-repl> void (*test)() = [](){ if }
In file included from <<< inputs >>>:1:
input_line_1:1:27: error: expected '(' after 'if'
    1 | void (*test)() = [](){ if }
      |                           ^
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
<<< inputs >>>:1:1: error: expected expression
fatal error: too many errors emitted, stopping now [-ferror-limit=]
```

After the change 
```
clang-repl> void (*test)() = [](){ if }
In file included from <<< inputs >>>:1:
input_line_1:1:27: error: expected '(' after 'if'
    1 | void (*test)() = [](){ if }
      |                           ^
<<< inputs >>>:1:1: error: expected '}'
input_line_1:1:22: note: to match this '{'
    1 | void (*test)() = [](){ if }
      |                      ^
input_line_1:1:28: error: expected ';' after top level declarator
    1 | void (*test)() = [](){ if }
      |                            ^
      |                            ;
error: Parsing failed.
```

>From 5bda5cb946e8056a43e487e66487632729d37aee Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Tue, 18 Feb 2025 11:23:30 +0530
Subject: [PATCH] Address error recovery fixing infinite loop while parsing

---
 clang/lib/Parse/ParseStmt.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index cd4504630f871..a044c8acab927 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -1253,7 +1253,7 @@ StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
 
   bool LastIsError = false;
   while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
-         Tok.isNot(tok::eof)) {
+         Tok.isNot(tok::eof) && Tok.isNot(tok::annot_repl_input_end)) {
     if (Tok.is(tok::annot_pragma_unused)) {
       HandlePragmaUnused();
       continue;



More information about the cfe-commits mailing list