Cannot temporarily disable unknown-pragmas warning in GCC - gcc

I cannot make the method to temporarily disable warnings in GCC (see How to disable GCC warnings for a few lines of code) work, at least not for the "unknown-pragmas" warning.
Compiling this code ...
#pragma comment(user,"This should depend on the command line options")
#pragma GCC diagnostic warning "-Wunknown-pragmas"
#pragma comment(user,"This should cause a warning")
#pragma GCC diagnostic error "-Wunknown-pragmas"
#pragma comment(user,"This should cause an error")
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma comment(user,"This should be ignored")
... produces either no warning/error (except that the linker complais about a missing main), or when using -Wall or just -Wunknown-pragmas it produces one warning for each of the comment pragmas.
The behaviour that I would have expected is that each comment should have caused exactly what the comment says.
I think I can back my expectation with the documentation:
At the moment only warnings (normally controlled by ‘-W...’) can be controlled, and not all of them. Use -fdiagnostics-show-option to determine which diagnostics are controllable and which option controls them.
The warnings I get show as
warning: ignoring #pragma comment [-Wunknown-pragmas]
and as the part in brackets tells us,
this diagnostic is controllable
and the option -Wunknown-pragmas controls it
Hence my code should work.
So what am I doing wrong?
version info:
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609

This is a long-standing missing feature in the GCC C++ front end:
C++ preprocessor ignores #pragma GCC diagnostic
Warnings generated by preprocessing cannot be controlled using programs in g++. Unlike the C front end, pragmas are processed only after the preprocessing phase in the C++ front end.

Related

GCC diagnostic ignored "-Wuninitialized" doesn't prevent build error

I am using GCC 12.1.0 with -Wall and -Werror enabled.
When building my code I want to ignore the following warning:
error: use of uninitialized value '' [CWE-457] [-Werror=analyzer-use-of-uninitialized-value]
I added the following pragma statements to my code around the block producing the error, however the build error still occurs.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
// code producing warning
#pragma GCC diagnostic pop
Is there an option that I am supposed to use instead of -Wuninitizalized, or is there another means of ignoring the build error?

How can I ignore all warnings from included headers in clang/gcc?

I want to use #pragma clang diagnostic push to ignore warnings from some included header files. So I write this:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wall"
#include "SpinGenApi/SpinnakerGenApi.h"
#include "Spinnaker.h"
#pragma clang diagnostic pop
But still the compiler prints warnings when compiling the file with this code. What do I need to change so that all warnings are ignored for these headers?
The answer seems to be in this case that -Wall contrary to its name did not include the warnings which I wanted to suppress. I added #pragma clang diagnostic ignored "-Weverything" as well and they went away.

How to disable GCC warning about the #warning directive being a GCC extension?

When using the #warning directive, rather than one warning I get two - the one I generate, plus an additional
warning: #warning is a GCC extension [enabled by default].
I can suppress the #warning directive's result itself, with -Wno-cpp, but that's the opposite of what I want.
I can work around this by instead using #pragma message() but that seems to render the #warning directive rather pointless - is there no way to suppress this warning warning?
Ah, it seems the -pedantic option enables this warning, and there does not seem to be a specific override for it, as there is with most other GCC warnings. Removing -pedantic gets rid of the warning about a warning.
#ifdef __GNUC__
#warning "no warning here!"
#endif
will compile with or without gcc, even with -pedantic, but it will still give the warning, (and fail if warnings are escalated to errors).
UPDATE:
According to this related question, there is no clean solution to selectively disable the warnings about the gnu-specific #pragma s
Actually, the following should work. With that you can suppress/ignore the gcc warnings.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcpp"
...
#pragma GCC diagnostic pop

Suppress -Wunknown-pragmas warning in GCC

I try to ignore warnings coming from some 3rd party header files like this:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC diagnostic ignored "-Wreorder"
#include <some_3rd_party_header.h>
#pragma GCC diagnostic pop
This approach seems to work in general, but not for the unknown pragma warnings (I still get them).
Why does it work for other warnings but not for this one? Can anyone confirm this behaviour?
I'm using g++ (version 4.7.1) with -Wall and -std=c++0x under Debian.
I've run into this annoyance, too. According to the GCC manpage -Wall turns on -Wunknown-pragmas for you, so just manually disable it using -Wno-unknown-pragmas after -Wall.
There is a GCC feature request to make this work using #pragma GCC diagnostic:
C++ preprocessor ignores #pragma GCC diagnostic

GCC warning flag for Semantic Issue

I'd like to silence Semantic Issue warning but I don't know the warning flag for this type.
Do you know the flag name?
ie: #pragma GCC diagnostic ignored "-Wsequence-point"
#pragma GCC diagnostic can only be used with options shown by -fdiagnostics-show-option
Compile with -fdiagnostics-show-option to see whether there is some [-Wfoo] at the end of the warning message.

Resources