[clang] [clang-repl] Remove invalid TopLevelStmtDecl from TU on parse failure (PR #153945)

via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 18 01:27:47 PDT 2025


================
@@ -91,6 +93,53 @@ TEST_F(InterpreterTest, IncrementalInputTopLevelDecls) {
   EXPECT_EQ("var2", DeclToString(*R2DeclRange.begin()));
 }
 
+TEST_F(InterpreterTest, BadIncludeDoesNotCorruptTU) {
----------------
devajithvs wrote:

The state of the AST before and after the failed statement will not be identical (and asserts on AST dump). I could not find a way to emit/dump AST on a failed statement.

```
int i = 10;
#include "interp-bad-include.h"
int j = 20;
```

cat "test.C" | bin/clang-repl -Xcc -Xclang -Xcc -ast-dump

I get:
```
...
...
TranslationUnitDecl 0x5af994c521d0 prev 0x5af994c26c18 <<invalid sloc>> <invalid sloc>
`-VarDecl 0x5af994c52250 <input_line_1:1:1, col:9> col:5 i 'int' cinit
  `-IntegerLiteral 0x5af994c522b8 <col:9> 'int' 10
In file included from <<< inputs >>>:1:
In file included from input_line_2:1:
./interp-bad-include.h:1:1: error: use of undeclared identifier 'error_here'
    1 | error_here; // expected-error {{use of undeclared identifier}}
      | ^~~~~~~~~~
error: Parsing failed.
TranslationUnitDecl 0x5af994c52438 prev 0x5af994c52328 <<invalid sloc>> <invalid sloc>
`-VarDecl 0x5af994c524b8 <input_line_3:1:1, col:9> col:5 j 'int' cinit
  `-IntegerLiteral 0x5af994c52520 <col:9> 'int' 20
```

There are no dumps after the failed statement. Do you know of another way to dump the AST (to trigger the assertion)?


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


More information about the cfe-commits mailing list