Selectively disable GCC warnings for only part of a translation unit

c, c++, compiler-warnings, gcc, pragma

Solution

Selectively disabling warnings is possible in GCC while the push/pop features are available since version 4.6.

Here's the example from the documentation:[*]

#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 imbalanced `pop` at the end is documented as well:

If a `pop` has no matching `push`, the command-line options are restored.

Problem

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

Original source

Related problems