Selectively disable GCC warnings for only part of a translation unit? Selectively disable GCC warnings for only part of a translation unit? c c

Selectively disable GCC warnings for only part of a translation unit?


This is possible in GCC since version 4.6, or around June 2010 in the trunk.

Here's an example:

#pragma GCC diagnostic push#pragma GCC diagnostic error "-Wuninitialized"    foo(a);         /* error is given for this one */#pragma GCC diagnostic push#pragma GCC diagnostic ignored "-Wuninitialized"    foo(b);         /* no diagnostic for this one */#pragma GCC diagnostic pop    foo(c);         /* error is given for this one */#pragma GCC diagnostic pop    foo(d);         /* depends on command line options */


The closest thing is the GCC diagnostic pragma, #pragma GCC diagnostic [warning|error|ignored] "-Wwhatever". It isn't very close to what you want, and see the link for details and caveats.


I've done something similar. For third-party code, I didn't want to see any warnings at all. So, rather than specify -I/path/to/libfoo/include, I used -isystem /path/to/libfoo/include. This makes the compiler treat those header files as "system headers" for the purpose of warnings, and so long as you don't enable -Wsystem-headers, you're mostly safe. I've still seen a few warnings leak out of there, but it cuts down on most of the junk.

Note that this only helps you if you can isolate the offending code by include-directory. If it's just a subset of your own project, or intermixed with other code, you're out of luck.