I want to remove/ignore a clang warning for a block of code and found multiple examples of how to use pragamas for this. For example if the warning is unused-variable
you can disable it by using:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
int a;
#pragma clang diagnostic pop
However the problem is that I do not get a warning in the output when building the repository, I only get to know which clang check it is that gives the warning... And I can not find any other questions or documentations where this is the case. This is what my output looks:
warning: Use of memory after it is freed [clang-analyzer-cplusplus.NewDelete]
I have tried hundreds of different combinations of how to ignore this but nothing works (using // NOLINT
is not a viable option). Among the things that i have tried, here are some:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winline-new-delete"
#pragma clang diagnostic ignored "-Wmost"
#pragma clang diagnostic ignored "-Weverything"
#pragma clang diagnostic ignored "clang-analyzer-cplusplus.NewDelete"
#pragma clang diagnostic ignored "-Wclang-analyzer-cplusplus.NewDelete"
#pragma clang diagnostic ignored "-clang-analyzer-cplusplus.NewDelete"
#pragma clang diagnostic ignored "-W-NewDelete"
#pragma clang diagnostic ignored "-W-new-delete"
// code
#pragma clang diagnostic pop
Note, "fixing" the code is also not an option since it is third party code.
As mentioned in the comments the #pragma clang diagnostic
approach can only be used to suppress compiler warnings. The warnings you refer to are coming from clang-static-analyzer which is now a part of clang-tidy.
The only two options to disable a specific clang-tidy check via code are the //NOLINT
and //NOLINTNEXTLINE
macros.
As you mentioned the code in question is third-party I will assume that you are not interested in analyzing it at all. Since you are using CMake this is easily done via .clang-tidy
files. You can place a .clang-tidy
file in the root directory of your project and list/configure the desired checks there like this:
Checks: '-*,cppcoreguidelines-*'
(this would enable all Cpp Core Guidelines checks).
Then in the directory where your third-party code is placed you can disable clang-tidy analysis via a .clang-tidy
file placed in that directory. As .clang-tidy
files cannot be empty or specify no checks, you can do this by "misconfiguring" a check like this:
Checks: '-*,misc-definitions-in-headers'
CheckOptions:
- { key: HeaderFileExtensions, value: "x" }
For more details on this approach see this answer.
Alternatively you can use the .clang-tidy
file in the third-party directory to disable only some checks.