Imagine being a guest contributor opening a C project similar to the following example code, in an editor using clangd to lint the code:
Makefile:
all:
$(CC) -c -o example.o example.c -DFOO=1
example.c:
int foo = FOO;
int bar = TYPO;
With clangd's default configuration, undeclared_var_use errors will be indicated for both FOO
and for TYPO
. Where TYPO
is intended to illustrate a real error, while the first one is a false positive as FOO
will exist at compile time.
example.c|1 col 11-13 error| clangd: undeclared_var_use|Use of undeclared identifier 'FOO'
example.c|2 col 11-14 error| clangd: undeclared_var_use|Use of undeclared identifier 'TYPO'
Please also consider a larger code base where these symbols could be used in multiple places.
There is the, on some level, similar question: Concise way to disable specific warning instances in Clang, which hints about using #pragma clang diagnostic ignored …
directives. Suppressing warnings is one thing, errors however is another thing. Suppressing the latter neither seems like a reasonable thing to do, is not documented nor does it appear to work when attempting it.
Another similar question is: Make clangd aware of macros given from the compiler, which has an answer requiring to add and maintain a compile_commands.json file. A file which must necessarily live inside the project tree, and can thus be used to avoid the problem this question is asking only when such changes can be made to the codebase.
One thing which is possible to do, is to suppress all occurrences of the error by configuring clangd as:
User's config.yaml (or project's .clangd if there is one):
Diagnostics:
Suppress: undeclared_var_use
While doing so arguable is an improvement, it is not fully satisfactory as it hides real errors. Can one somehow make clangd suppress only the FOO
error? (Random sources suggest putting the problematic variable within parenthesis as argument to the suppress directive, but that seems to be nothing but an unsubstantiated rumour.)
A related bonus question; Is there any documentation for the the diagnostic codes which Suppress takes as arguments, or where could one find them in llvm:s source code? (GitHub issue #649 has an answer for the first part. Searching for the literal strings (e.g. undeclared_var_use) yield surprisingly few results.)
Versions of clangd used debian packaged 1:14.0-55.6 and self-built 17.0.0, commit cd22e7c.
Edit after accepting answer: Stroke out the misconception that compile_commands.json needs to live in the same directory as the source code.