Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concise way to disable specific warning instances in Clang

Tags:

c++

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.

like image 921
Timmmm Avatar asked Jan 24 '18 15:01

Timmmm


People also ask

How do I supress a warning in GCC?

To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused.

Does Clang define __ GNU C __?

(GNU C is a language, GCC is a compiler for that language.Clang defines __GNUC__ / __GNUC_MINOR__ / __GNUC_PATCHLEVEL__ according to the version of gcc that it claims full compatibility with.

How do I turn off compiler warnings in GCC?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.


2 Answers

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).

like image 64
T S Avatar answered Oct 12 '22 23:10

T S


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
like image 45
Ron Avatar answered Oct 12 '22 22:10

Ron