Should I use "#pragma GCC ..." or "#pragma clang ..." in Xcode - gcc

Which of these should I use to ignore a warning?
#pragma clang diagnostic ignored "-W<warning>"
#pragma GCC diagnostic ignored "-W<warning>"
Both seems to work for me, however which one is the correct to use?
Should I always use the one matching the compiler I'm using?

Generally, you should prefer #pragma GCC in cases where the pragma is GCC-specific, or is equally applicable to GCC, Clang, and other compilers which try to be GCC-compatible (such as ICC). Use #pragma clang in cases where the pragma is in some way Clang-specific (such as a diagnostic option which doesn't exist in GCC).

Related

How to suppress GCC compiler warning: inline variables are only available with -std=c++1z or -std=gnu++1z

I am using an inline global variable which works well for the purpose of it.
class MyClass {
public:
void Func() {
}
}
inline MyClass myClass; // global inline variable
Above works well for my purpose but I get a warning when my code compiles on gcc with compiler below C++17. Following is the warning
warning: inline variables are only available with -std=c++1z or
-std=gnu++1z
Question:
How can I suppress the warning on gcc?
I tried to suppress the warning by using a #pragma like below
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wc++17-extensions"
inline MyClass myClass;
#pragma GCC diagnostic pop
Above #pragma technique works on clang, but looks like GCC to not understand the #pragma? I just want to brute force suppress the warning on GCC. How can I do that?
Looks like gcc warning options list does not even mention about this?
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
I am using gcc (GCC) 7.3.0
To understand why you cannot suppress this warning, look at what happens if the code is compiled with a compiler that does not support inline variables. (Inline variable support started with gcc 7.) Older versions of gcc process your code and spit out error: 'myClass' declared as an 'inline' variable. Not a warning, but an unsuppressible error. Hard stop; object code not produced.
Newer versions of gcc are able to be more understanding and helpful, but at the same time they have an obligation to maintain some degree of compatibility with older compilers. These newer compilers can recognize this C++17 feature, and it's been determined that ignoring "inline" downgrades the error to a warning (compilation does not necessarily need to stop). Furthermore, the message was given information about how to resolve this situation (assuming the code is correct). At the same time, this warning is still essentially the error produced by older versions of gcc, just given a makeover to make it more user-friendly. It cannot be suppressed any more than the old error could. Your choices are to write valid pre-17 code or to enable C++17 features.

Cannot temporarily disable unknown-pragmas warning in 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.

pragma ignored in g++ and clang

I would like to disable specific known warnings in C++ code coming from a library header when compiling my own code. There are clang and gcc specific methods for disabling the warnings. The way this is done is almost identical.
For clang:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-local-typedefs"
#include <library.h>
#pragma clang diagnostic pop
For gcc:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <library.h>
#pragma GCC diagnostic pop
Is there a clean way to suppress these warnings that is portable across clang and GCC?

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

Resources