views:

4117

answers:

3

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. However we want files which include those headers to continue to generate that warning (if the project has that warning enabled).

+7  A: 

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.

chaos
Do 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.
Roger Pate
I 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.
chaos
It'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.
Bob Murphy
To 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))`.
Tom
new gcc (>=4.4) has `#pragma GCC push_options` so you can muck about with more than just diagnostics...http://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html
Spudd86
+3  A: 

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.

Tom
+3  A: 

This is possible in GCC since around June 2010 (whatever version that corresponds with).

Here's an example:

#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 */
Matt Joiner

related questions