Issue 171440
Summary [clang-repl] Implicit FunctionDecl created on error is not removed by CleanUpPTU
Labels new issue
Assignees
Reporter anutosh491
    Through https://github.com/llvm/llvm-project/pull/169989 we were able to skip CodeGen for top-level decls when diagnostics report errors and as a result of this, we could have these cases fixed.

```
$ ./clang-repl   --Xcc=-x --Xcc=c --Xcc=-std=c23
clang-repl> printf("10\n");
In file included from <<< inputs >>>:1:
input_line_1: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("10\n");
      | ^
input_line_1:1:1: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
error: Parsing failed.
clang-repl> #include <stdio.h>
clang-repl> printf("10\n");
10

$ ./clang-repl   --Xcc=-x --Xcc=c   --Xcc=-std=c23 --Xcc=-fno-builtin
clang-repl> printf("10\n");
In file included from <<< inputs >>>:1:
input_line_1:1:1: error: use of undeclared identifier 'printf'
    1 | printf("10\n");
      | ^~~~~~
error: Parsing failed.
clang-repl> #include <stdio.h>
clang-repl> printf("10\n");
10

$ ./clang-repl --Xcc=-x --Xcc=c   --Xcc=-std=c17
clang-repl> printf("10\n");
In file included from <<< inputs >>>:1:
input_line_1: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("10\n");
      | ^
input_line_1:1:1: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
error: Parsing failed.
clang-repl> #include <stdio.h>
clang-repl> printf("10\n");
10
```

The c23 and c17 case would make use of [LazilyCreateBuiltin](https://github.com/llvm/llvm-project/blob/27660022743c560d266bb9c0c83454f663a2d7ad/clang/lib/Sema/SemaDecl.cpp#L2410). But I notice the case that involves an implicit function definition and ends up calling [ImplicitlyDefineFunction](https://github.com/llvm/llvm-project/blob/27660022743c560d266bb9c0c83454f663a2d7ad/clang/lib/Sema/SemaDecl.cpp#L16942) because we disallow builtins would end up like this
```
anutosh491@vv-nuc:/build/anutosh491/llvm-project/build/bin$ ./clang-repl   --Xcc=-x --Xcc=c   --Xcc=-std=c17 --Xcc=-fno-builtin
clang-repl> printf("10\n");
In file included from <<< inputs >>>:1:
input_line_1:1:1: error: call to undeclared function 'printf'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    1 | printf("10\n");
      | ^
error: Parsing failed.
clang-repl> #include <stdio.h>
In file included from <<< inputs >>>:1:
In file included from input_line_2:1:
/usr/include/stdio.h:356:12: error: conflicting types for 'printf'
  356 | extern int printf (const char *__restrict __format, ...);
 |            ^
input_line_1:1:1: note: previous implicit declaration is here
    1 | printf("10\n");
      | ^
error: Parsing failed.
```

Problem : When a call to an undeclared function occurs in C17 mode, Clang emits an implicit-function-declaration diagnostic and also creates an implicit `FunctionDecl`; `CleanUpPTU` fails to remove this implicitly-created FunctionDecl, allowing it to leak into later `PTUs` and causing redefinition errors when `<stdio.h>` is included.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to