22

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.

Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • 1
    `#pragma` is the way to do it, I am afraid. The compiler is not **allowed** to interpret e.g. a comment the way you outlined, as that would affect fully-conforming programs in a non-standard way. – DevSolar Jan 25 '18 at 10:58
  • 1
    I don't see why it wouldn't be allowed to enable or disable *warnings* based on comments. The standard has nothing to say about warnings at all. As long as it never emits an error due to parsing a comment it wouldn't affect compilation at all. – Timmmm Jul 10 '19 at 15:55

2 Answers2

36

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
martinus
  • 17,736
  • 15
  • 72
  • 92
Ron
  • 14,674
  • 4
  • 34
  • 47
  • That does it for everything after that line in the file though. Not what I want. I just want to suppress *one instance* of the warning. – Timmmm Jan 25 '18 at 10:50
  • Ok but I already mentioned this possibility in the question. I wanted a more concise way to do it (I'm pretty sure it is impossible at the moment, but worth asking). – Timmmm Jan 25 '18 at 12:46
  • @Timmmm Unfortunately those are the only available options. – Ron Jan 25 '18 at 13:08
  • @Timmmm: As I commented above, `#pragma` is the way reserved by the language standard to allow for implementation-defined behaviour. A comment, for example, **must** be ignored by the compiler, because otherwise the compiler would not be standard-compliant. So you have to do compiler-specific things via `#pragma`. – DevSolar Jan 25 '18 at 14:03
  • Well... sure, but I would be happy with non standard-compliant behaviour. – Timmmm Jan 25 '18 at 14:52
  • 1
    @Timmmm For some context, Visual Studio does provide what you're looking for. In fact they provide [both alternatives](https://stackoverflow.com/a/25447795). – wardw Jan 25 '19 at 20:31
8

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

T S
  • 1,656
  • 18
  • 26