93

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jon-Eric
  • 16,977
  • 9
  • 65
  • 97
  • If the headers are installed to /usr/include or what have you gcc doesn't generate warnings for them by default. – Spudd86 Jun 15 '10 at 15:29

4 Answers4

101

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 */
Pavel P
  • 15,789
  • 11
  • 79
  • 128
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
36

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
  • 122,029
  • 33
  • 303
  • 309
  • 1
    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. –  Nov 17 '09 at 17:36
  • 1
    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 Nov 18 '09 at 14:45
  • 14
    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 Jan 22 '10 at 01:26
  • 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 Jan 23 '10 at 21:04
  • 10
    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 Jun 15 '10 at 13:52
33

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
  • 10,689
  • 4
  • 41
  • 50
  • 1
    Great tip. If using LLVM, add the -isystem flag under "Other C Flags" in the "Apple LLVM Compiler - Language" section. – Nestor Mar 14 '12 at 10:46
  • @Tom Thanks for sharing. I cannot understand where to use your solution. Can you say a little bit more? – Lorenzo B Mar 07 '13 at 12:27
1

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
Not a real meerkat
  • 5,604
  • 1
  • 24
  • 55