Reputation: 1063
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.
Upvotes: 5
Views: 6902
Reputation: 2663
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.
Upvotes: 4
Reputation: 9173
Best approach that comes to my mind is a little dependent to your build system. Let's assume that you have a sample code like this:
Main file:
int main()
{
// ...
int *a = new int;
*a = 10;
delete a;
if (::rand() < 10) {
std::cout << *a; //<-- clang tidy warning here
}
//...
}
This creates the clang tidy warning in the mentioned line. Now what You can do? Separate your code into 2 files:
File A:
void foo() {
int *a = new int;
*a = 10;
delete a;
if (::rand() < 10) {
std::cout << *a; //<-- clang tidy warning here
}
}
Main file:
int main()
{
// ...
foo();
//...
}
And now disable cland-tidy
for File A. This is a sample, But I guess you understand my general idea. You cannot disable clang-tidy
because it is in another library and you cannot touch that code for several reasons. //NOLINT
does not work on function too. So just create a wrapper in a single file for using that library and disable clang-tidy
in that wrapper file for whole file.
Upvotes: 0