1

I have -Wpadded warning option on (gcc/g++) and getting these warnings for non-padded C/C++ structures as expected.

Now, I want to exclude some of the structures from that warning without having to actually pad them.

To make things even more complicated - I have "-Wall". This makes the approach with #pragma GCC diagnostics ignored -Wpadded not usable (shame, would have been very neat).

Some sort of "pragma" directive in the code is what I would expect as a solution. Thanks in advance!

Please note that there was a misspelling in my original question text! Refer to the accepted answer for the confirmation that the method with #pragma actually works!

OpalApps
  • 149
  • 13
  • 1
    https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html – Lightness Races in Orbit Jul 17 '19 at 10:36
  • @LightnessRacesinOrbit Thanks for the suggestion. Although it could have potentially worked, in my case I have "-Wall" on (not my decision), will add this to the question. The problem with -Wall is that with "#pragma GCC diagnostics push" I get "-Wunknown-pragmas".Will have to work this around somehow. But thanks to your suggestion I have found others struggling with very similar issue: https://stackoverflow.com/questions/12842306/suppress-wunknown-pragmas-warning-in-gcc. – OpalApps Jul 17 '19 at 11:00
  • You wrote `diagnostics` instead of `diagnostic`, so indeed that's an unknown pragma. – Lightness Races in Orbit Jul 17 '19 at 11:20
  • 1
    By the way, `-Wall` is good but add `-Wextra -pedantic` too for extra goodness – Lightness Races in Orbit Jul 17 '19 at 11:26

1 Answers1

3

Yes, GCC has pragmas for this:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpadded"

// Doesn't warn
struct Foo { bool x; int y; };

#pragma GCC diagnostic pop


// Warns
struct Bar { bool x; int y; };

(live demo)

Nothing about -Wall makes this complicated or in any way a problem; you just have to spell the pragma correctly.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055