Inline way to disable clang-tidy checks - c++

I'm trying to set up clang-tidy for a project. I'd like to be able to have clean output, and encourage the use of -fix mode where possible. However, there are individual cases where an exception is needed.
Much as it is possible to use
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-id-macro"
// Code that is being specially exempted
#pragma clang diagnostic pop
for the equivalent case where one wants to locally disable a compiler warning, is it possible to do something similar from clang-tidy?
I have tried
#pragma clang diagnostic push
#pragma clang diagnostic ignored "readability-identifier-naming"
// Code that is being specially exempted
#pragma clang diagnostic pop
and also with clang replaced with clang-tidy. Unfortunately when using clang as the pragma target and compiling with regular clang, I get the compilation warning
warning: pragma diagnostic expected option name (e.g. "-Wundef") [-Wunknown-pragmas]
and
warning: unknown pragma ignored [clang-diagnostic-unknown-pragmas]
when compiling if I use clang-tidy in place of clang. Neither make an impact on what clang-tidy itself outputs when run over the source.
This is with clang and clang-tidy 3.8 on x86_64 Linux.

Just add a comment containing the string NOLINT anywhere on the line you want clang-tidy to ignore. For example:
badcode; // NOLINT
// NOLINTNEXTLINE
badcode;
badcode; // NOLINT(cert-err-58-cpp)
See the documentation here.

Related

How to enable GCC compiler flags via preprocessor directives, rather than command line options?

I'm wondering how to activate GCC compiler flags in code, as opposed to including them in the compilation command on the command line.
Consider the following minimal example, which produces the warning -Wswitch-bool (I think by default but at least with -Wall enabled):
switch (true) {
default:
break;
}
This warning can be suppressed by providing the flag -Wno-switch-bool when compiling the program via the command line.
Is it possible to activate this flag in code? The following naive attempt doesn't work, since Wno-switch-bool is not a legal identifier (dashes are interpreted as minuses):
#define Wno-switch-bool
What is the appropriate command/syntax to use?
Note: This question is not specifically about warnings. I.e. I'm not asking about how to deactivate a specific class of warnings, which I know is best done via #pragma GCC diagnostic ignore, etc. This is a general question about how to enable GCC compiler flags in code. For instance, another example could be how I could activate -Werror, or -fno-implicit-templates via preprocessor directives (whether or not that would be ill-advised).
Is it possible to activate this flag in code?
Yes, you can use GCC's diagnostic pragmas to apply a specific pragma temporarily around a specific snippet of code, by remembering and subsequently restoring the state of the diagnostics prior to a source-local tweaking of it. E.g.:
#pragma GCC diagnostic push
// ^^^^^^^^^^^^^^^ remember the state of the diagnostics
#pragma GCC diagnostic ignored "-Wswitch-bool"
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ tweak diagnostics
switch (true) {
default:
break;
}
#pragma GCC diagnostic pop
// ^^^^^^^^^^^^^^^ restore the state of the diagnostics

#pragma(* diagnostic) when mixing Clang analyzers with a GCC compiler

I'm compiling on with GCC on Linux, but CMake is kind enough to produce a Clang compatible compilation database. This means that I can run fancy, modern Clang based tools on my codebase and those tools have perfect knowledge of how each file is to be built (flags, defines, include paths, etc.) So far so good.
But today the Clang based static analysis in my IDE started showing a Clang specific warning. I don't think it particularly matters for my question which warning it is, but it was warning: disabled expansion of recursive macro, generated by -Wdisabled-macro-expansion. This particular macro is provided by a third party, so fixing the warning isn't an option, but I would like to suppress it as it occurs several times in the file in question.
What I'm struggling with is how to suppress the warning in Clang based analysis tools without causing new warnings in the GCC build.
Adding #pragma clang diagnostic ignored "-Wdisabled-macro-expansion" suppresses the warning for Clang tools, but causes GCC to issue warning: ignoring #pragma clang diagnostic [-Wunknown-pragmas].
Similarly, adding #pragma GCC diagnostic ignored "-Wdisabled-macro-expansion" suppresses the Clang warning (because Clang tries to be compatible with GCC diagnostics), but causes GCC to issue warning: unknown option after ‘#pragma GCC diagnostic’ kind [-Wpragmas].
Wrapping either of the above with #ifdef __clang__ makes GCC happy, but doesn't suppress the Clang warning because the tooling is smart enough to know that the code isn't compiled with __clang__, but with __GNUC__ instead.
Is there a way to make a diagnostic #pragma visible to Clang tooling, but not to GCC?
the tooling is smart enough to know that the code isn't compiled with __clang__, but with __GNUC__ instead
If it's reporting a clang-only warning, but does not think that __clang__ is defined, that sounds like a problem with the tooling. If it's trying to be that clever about misrepresenting itself, you may be up a creek... but also you should be complaining to the tool author for creating this situation in the first place.
That said, you could try:
#if defined(__has_warning)
# if __has_warning("-Wdisabled-macro-expansion")
# pragma GCC diagnostic ignored "-Wdisabled-macro-expansion"
# endif
#endif
I'm not sure if this will work... it depends on how hard the tooling is pretending to not be clang (__has_warning is a clang-only extension).

Concise way to disable specific warning instances in Clang

Suppose there is some warning in my code, e.g. that Clang has added padding to a struct. I am find with that particular instance and I want to mark it as "Noted; don't warn me about this instance again".
Is there a way to do this that isn't insanely verbose (i.e. #pragma clang diagnostic push etc)? Ideally something like a comment on the same line as the warning, something like this:
// clang(-Wno-padded)
To be clear, I only want to suppress one specific instance of the warning (which normally requires #pragma diagnostic push/pop), not all warnings in the file.
As described in the Controlling Diagnostics via Pragmas article it would be:
#pragma clang diagnostic ignored "-Wpadded"
If you want to suppress a warning in a certain chunk of code (be it a single line of code or multiple statements) then you need to utilize the push / pop mechanism:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpadded"
// your code for which the warning gets suppressed
#pragma clang diagnostic pop
// not suppressed here
If you have some include file where you can put a macro definition like this:
#define DO_PRAGMA(x) _Pragma(#x)
#define NOWARN(warnoption, ...) \
DO_PRAGMA(GCC diagnostic push) \
DO_PRAGMA(GCC diagnostic ignored #warnoption) \
__VA_ARGS__ \
DO_PRAGMA(GCC diagnostic pop)
Then you can disable a warning within your code like this:
NOWARN(-Wpadded,
// your code for which the warning gets suppressed
)
Example: https://godbolt.org/z/oW87ej
Slightly off-topic note:
gcc does not allow GCC diagnostic .... pragmas within expressions. So something like this:
#define MY_MYCRO(type) NOWARN(-Wpadded, sizeof(struct{char c; type t;}))
int myval = MY_MYCRO(int);
will produce a error in gcc and won't compile. Note: Using clang diagnostic .... pragmas will not produce an error in gcc (but also doesn't disable the warning in gcc).

how to disable the warning (seems not one common warning )for this code sample in c++/gcc compile?

how to disable the warning for this code sample in c++/gcc compile?
the warning seems it isn't a common warning. So how to disable it?
is it similar with followed code?
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable" //I don't know ignore what for the code in pic.
#include "subfolder/ClassTwo.h"
#pragma GCC diagnostic pop
but ignore what?
I tried one answer but pop one error:
for
#pragma GCC diagnostic push
it pop:
warning: expected [error|warning|ignored] after â#pragma GCC diagnosticâ
It is strange to see this warning popping up in Boost. The compiler flag to disable this warning is -Wno-reorder, as per gcc's man page.
In your case, you can use:
#pragma GCC diagnostic ignored "-Wreorder"

In GCC, how can I mute the '-fpermissive' warning?

I am including a file from a third-party library that raises an error that can be downgraded to a warning with -fpermissive. But because I do not want to "pollute" my compilation log with these warnings, I want to completely disable this messages.
So far, I set the -fpermissive option with a diagnostic pragma when including the file; something like:
#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-fpermissive"
#include <third-party-file.h>
#pragma GCC diagnostic pop
Since GCC usually provides both a "positive" and "negative" version of the -f flags, I thought about ignoring the "no-permissive" feature:
#pragma GCC diagnostic ignored "-fno-permissive"
#include <third-party-file.h>
But there does not seem to be a "negative" version of the -fpermissive flag (I am using GCC 4.6.3; but even the version 4.7.0 does not have it).
Can I mimic this behavior?
tldr: You cannot turn off the fpermissive output after GCC 4.7.
Just posting this here so it has more visibility: unfortunately, zwol's answer (while well-intentioned, and potentially helpful to those with older GCC versions) does not work for more recent versions of GCC. From GCC 4.8 and beyond, you cannot turn off the fpermissive output. o11c in his comment to the OP helpfully provides the following bug which tracks this:
Bug 81787. [5/6/7/8 Regression] #pragma GCC diagnostic warning "-fpermissive" no longer
Note that it is in the state "RESOLVED INVALID", so the inability to turn it off is the expected behavior and there are no plans to change it.
One of these ought to do what you wanted:
#pragma GCC diagnostic ignored "-fpermissive"
or
#pragma GCC diagnostic ignored "-pedantic"
"ignored" is how you squelch a diagnostic entirely, and the inverse of -fpermissive is -pedantic, for historical reasons.