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

1
  • If the headers are installed to /usr/include or what have you gcc doesn't generate warnings for them by default.
    – Spudd86
    Jun 15, 2010 at 15:29

4 Answers 4

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 */
5
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.

5
  • 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.
    – Roger Pate
    Nov 17, 2009 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, 2009 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, 2010 at 1: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, 2010 at 21:04
  • 10
    new gcc (>=4.4) has #pragma GCC push_options so you can muck about with more than just diagnostics... gcc.gnu.org/onlinedocs/gcc/…
    – Spudd86
    Jun 15, 2010 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.

2
  • 1
    Great tip. If using LLVM, add the -isystem flag under "Other C Flags" in the "Apple LLVM Compiler - Language" section.
    – Nestor
    Mar 14, 2012 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 7, 2013 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.