Selectively disable GCC warnings for only part of a translation unit
Solution 1
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 */
Solution 2
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.
Solution 3
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.
Solution 4
This is an expansion to Matt Joiner's answer.
If you don't want to spawn pragmas all over your code, you can use the _Pragma operator:
#ifdef __GNUC__
# define DIAGNOSTIC_ERROR(w) _Pragma("GCC diagnostic error \"" w "\"")
# define DIAGNOSTIC_IGNORE(w) _Pragma("GCC diagnostic ignore \"" w "\"")
# define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
# define DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
#endif
// (...)
DIAGNOSTIC_ERROR("-Wuninitialized")
foo(a); // Error
DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE("-Wuninitialized")
foo(a); // No error
DIAGNOSTIC_POP
foo(a); // Error
Jon-Eric
Updated on November 14, 2021Comments
-
Jon-Eric over 2 years
What's the closest GCC equivalent to this MSVC preprocessor code?
#pragma warning( push ) // Save the current warning state. #pragma warning( disable : 4723 ) // C4723: potential divide by 0 // Code which would generate warning 4723. #pragma warning( pop ) // Restore warnings to previous state.
We have code in commonly included headers which we do not want to generate a specific warning for. However, we want files which include those headers to continue to generate that warning (if the project has that warning enabled).
-
Admin over 14 yearsDo you know what and where the rationale for not adding this feature might be? (I couldn't find it.) I find the warning push-disable-pop to be useful.
-
chaos over 14 yearsI don't really imagine that "not adding features" to gcc tends to have a rationale so much as an absence of anyone submitting a working patch.
-
Bob Murphy over 14 yearsIt's not that nobody is willing to do the work for this kind of fine-grained warning control in gcc, or submit the code - I know of one major Silicon Valley corporation that already did this, and another that would have been delighted to pay somebody to do it and get the code into the stream. Rather, per a discussion with a guy who (as one of the gdb maintainers) is plugged into this stuff, the gcc maintainers have a philosophy: "If there's a warning, it's a bug, and you need to fix it." So (imo) it's a religious argument, and they control the code so they win.
-
Tom over 14 yearsTo add to Bob's comment, the GCC developers have a history of disliking the
#pragma
directive, so anything that is GCC-specific is probably more likely to be implemented as an__attribute__((foo))
. -
Spudd86 about 14 yearsnew gcc (>=4.4) has
#pragma GCC push_options
so you can muck about with more than just diagnostics... gcc.gnu.org/onlinedocs/gcc/… -
Dave Johansen over 12 yearsThe push and pop functionality was added in gcc 4.6 ( gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Diagnostic-Pragmas.html ).
-
Nestor over 12 yearsGreat tip. If using LLVM, add the -isystem flag under "Other C Flags" in the "Apple LLVM Compiler - Language" section.
-
Lorenzo B over 11 years@Tom Thanks for sharing. I cannot understand where to use your solution. Can you say a little bit more?
-
Dan over 10 yearsYou'll probably want to push twice if your going to pop twice.
-
Matt Joiner over 10 years@Dan: Read the manual, and comment. Note the origin of the example.
-
Trevor Boyd Smith about 6 yearsFYI for older versions like 4.4.7 you can still use the
#pragma GCC diagnostic [error|warning|ignored]
but thepop
is not implemented/supported. -
TheExcitedEngineer over 2 yearsIs there a way to disable all warnings for a piece of code via this method?