Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selectively disable GCC warnings for only part of a translation unit

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

like image 833
Jon-Eric Avatar asked Jun 08 '09 14:06

Jon-Eric


People also ask

How do I stop a GCC warning?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

How do you stop warnings in Cmake?

cmake:48 (include) CMakeLists. txt:208 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. You can disable the warning like this when you are configuring your build.

What is werror?

-Werror= Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors.


2 Answers

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 */ 
like image 104
Matt Joiner Avatar answered Sep 30 '22 03:09

Matt Joiner


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.

like image 37
chaos Avatar answered Sep 30 '22 01:09

chaos