[clang] [clang-repl] Extend the C support. (PR #89804)

Vassil Vassilev via cfe-commits cfe-commits at lists.llvm.org
Sun May 19 23:33:18 PDT 2024


================
@@ -2282,7 +2282,8 @@ void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
 
     // Remove this name from our lexical scope, and warn on it if we haven't
     // already.
-    IdResolver.RemoveDecl(D);
+    if (!PP.isIncrementalProcessingEnabled())
+      IdResolver.RemoveDecl(D);
----------------
vgvassilev wrote:

No, this change makes sure that we preserve the state of the `IdResolver` across partial translation units (PTU). That is, when we see another incremental output, we still take into account the declarations that came from previous PTUs. Eg:

```
[repl] int i = 12;
[repl] printf("%d\n", i); // without this patch we report we do not know what `i` is...
```

To your explicit question this patch produces:

```
bin/clang-repl -Xcc -xc
clang-repl> { int i = 12; }
clang-repl> printf("%d", i); // Fine?
In file included from <<< inputs >>>:1:
input_line_2:1:1: error: call to undeclared library function 'printf' with type 'int (const char *, ...)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    1 | printf("%d", i); // Fine?
      | ^
input_line_2:1:1: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
input_line_2:1:14: error: reference to local variable 'i' declared in enclosing context
    1 | printf("%d", i); // Fine?
      |              ^
input_line_1:1:7: note: 'i' declared here
    1 | { int i = 12; }
      |       ^
error: Parsing failed.
clang-repl> %quit
```

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


More information about the cfe-commits mailing list