Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clangd gets confused by -U... in command line #97759

Closed
jpalus opened this issue Jul 4, 2024 · 3 comments
Closed

clangd gets confused by -U... in command line #97759

jpalus opened this issue Jul 4, 2024 · 3 comments
Labels
clangd duplicate Resolved as duplicate

Comments

@jpalus
Copy link

jpalus commented Jul 4, 2024

For input file test.c:

#include <stdio.h>

#ifndef MSG
#define MSG "hello world"
#endif

void hello() {
  printf(MSG);
}

and compile_commands.json:

[
  {      
    "directory": "/tmp",      
    "command": "clang -UMSG -c -o test.o test.c'",            
    "file": "test.c",            
    "output": "test.o"            
  }      
]

clangd reports false positive:

$ clangd --check=test.c
...
I[21:05:56.452] Indexing headers...
I[21:05:56.472] Building AST...
E[21:05:56.491] [undeclared_var_use] Line 8: use of undeclared identifier 'MSG'
I[21:05:56.491] Indexing AST...
I[21:05:56.492] Building inlay hints
I[21:05:56.492] Building semantic highlighting
I[21:05:56.492] Testing features at each token (may be slow in large files)
I[21:05:56.493] Found definition heuristically using nearby identifier MSG
I[21:05:56.493] Found definition heuristically using nearby identifier MSG
I[21:05:56.501] All checks completed, 1 errors
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 4, 2024

@llvm/issue-subscribers-clangd

Author: Jan Palus (jpalus)

For input file `test.c`: ```c #include <stdio.h>

#ifndef MSG
#define MSG "hello world"
#endif

void hello() {
printf(MSG);
}

and `compile_commands.json`:
```json
[
  {      
    "directory": "/tmp",      
    "command": "clang -UMSG -c -o test.o test.c'",            
    "file": "test.c",            
    "output": "test.o"            
  }      
]

clangd reports false positive:

$ clangd --check=test.c
...
I[21:05:56.452] Indexing headers...
I[21:05:56.472] Building AST...
E[21:05:56.491] [undeclared_var_use] Line 8: use of undeclared identifier 'MSG'
I[21:05:56.491] Indexing AST...
I[21:05:56.492] Building inlay hints
I[21:05:56.492] Building semantic highlighting
I[21:05:56.492] Testing features at each token (may be slow in large files)
I[21:05:56.493] Found definition heuristically using nearby identifier MSG
I[21:05:56.493] Found definition heuristically using nearby identifier MSG
I[21:05:56.501] All checks completed, 1 errors

@HighCommander4
Copy link
Collaborator

I believe this has the same underlying cause as clangd/clangd#1396: "clang first loads the preamble and then applies the builtin buffer".

Here, the #define MSG becomes part of the preamble, and the -UMSG becomes part of the builtin buffer.

A workaround is to add a non-preprocessor declaration between the includes and the #define MSG to force the preamble to end before the #define MSG:

#include <stdio.h>

struct EndPreamble {}; // dummy declaration to force the end of the preamble

#ifndef MSG
#define MSG "hello world"
#endif

void hello() {
  printf(MSG);
}

@HighCommander4
Copy link
Collaborator

Duplicate of clangd/clangd#1396

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clangd duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

4 participants