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

via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 17 21:58:02 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Anutosh Bhat (anutosh491)

<details>
<summary>Changes</summary>

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.
```

---
Full diff: https://github.com/llvm/llvm-project/pull/127569.diff


1 Files Affected:

- (modified) clang/lib/Parse/ParseStmt.cpp (+1-1) 


``````````diff
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;

``````````

</details>


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


More information about the cfe-commits mailing list