GCC does not honor 'pragma GCC diagnostic' to silence warnings
This appears to be a bug in gcc
at least. The following code:
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC diagnostic ignored "-Wuninitialized"
int fn(void) {
#pragma xyzzy
int x;
return x;
}
int main (void) {
return fn();
}
has no problems ignoring the uninitialised x
value but still complains about the pragma (without the uninitialized
pragma, it generates a warning for x
as you'd expect).
If you change the command line options to be -Wall -Wno-unknown-pragmas
, then it ignores it just fine. That's okay for your specific case since you want it applied over your entire translation unit but it won't allow the fine-grained control that you would get from the #pragma
method (if it worked).
I went to raise a bug report on GCC but found that it already exists (#53431).
While that specific bug has to do with -Wundef
, a snippet in one of the comments indicates that it probably applies to all variants affecting the preprocessor (slightly modified for emphasis):
The C++ parser lexes (and preprocesses) before handling the pragmas, whereas the C parser processes the pragmas as it sees them.
We must somehow parse these pragmas also in
cp/parser.c:631
. Maybe one can do something similar to what we do forcp_parser_initial_pragma
, but within the loop and only handling pragma diagnostic. Surely, it will need some trial and error to get it right. If any of you wants to give it a try and need some help, just ask here or in the mailing list.
That explains why we don't see the same problem with -Wuninitialized
, because it's detected during later stages of the compilation process, after the pragmas have been activated at the end of preprocessing.
So, if you want to see it fixed in a more timely manner (it was raised over three years ago), I'd suggest (as I have) hassling the GCC bugzilla site to try get some exposure.
g++ complains of #pragma region if I use #pragma GCC diagnostic
I have faked #pragma region as follows.
#ifndef __PRAGMAREGION__ // Sign commands
... code goes here ...
#endif // Sign commands
VSCode does collapse/expand in the same way as #pragma region; and the GCC compiler is fine with it.
Not perfect... but the VSCode collapse feature is too important to lose.
How to disable GCC warnings for a few lines of code
It appears this can be done. I'm unable to determine the version of GCC that it was added, but it was sometime before June 2010.
Here's an example:
#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 */
GCC ignores diagnostic pragmas while Clang doesn't
It seems like it's related to the scoping. When I move push/pop #pragma
s outside of a variable's block, GCC behaves well. For example:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
uint8_t hour = p[8];
uint8_t min = p[9];
uint8_t sec = p[10];
uint16_t msec = (p[11] * 4);
#pragma GCC diagnostic pop
should be changed to the:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
{
uint8_t hour = p[8];
uint8_t min = p[9];
uint8_t sec = p[10];
uint16_t msec = (p[11] * 4);
}
#pragma GCC diagnostic pop
Then both Clang and GCC work fine.
Ignoring note: offset of packed bit-field without using -Wno-packed-bitfield-compat
Just ran into a similar issue. It seems that gcc just doesn't like bitfields that cross the width of the type (as two
does in the example)?
If you change all the types to uint16_t
, gcc accepts:
union pack_it_in {
struct
{
uint16_t zero : 3;
uint16_t one : 2;
uint16_t two : 6;
uint16_t three : 4;
uint16_t four : 1;
} __attribute__((packed)) u8_2;
uint16_t u16;
};
The layout is what you want it to be, even if the types of these members may not be.
CUDA compiler warning about unrecognized GCC pragma
Try this one:
-Xcudafe "--diag_suppress=unrecognized_gcc_pragma"
Related Topics
Windows Named Pipe Support in Linux
Check Whether Iterator Belongs to a List
How to Get Size of Check and Gap in Check Box
Round to Nearest Multiple of a Number
Checking for Duplicates in a Vector
Hash Function for User Defined Class. How to Make Friends?
Std::Set Iterator Automatically Const
Remote Debugging C++ Application with Eclipse Gui
Why Can Template Instances Not Be Deduced in 'Std::Reference_Wrapper'S
Constructor as a Function Try Block - Exception Aborts Program
Do You Prefer Explicit Namespaces or 'Using' in C++
What Is a C++11 Extension [-Wc++11-Extensions]
Std::Locale Breakage on MACos 10.6 with Lang=En_Us.Utf-8
How to Create a Temporary Text File in C++
Why Does My Program Run Way Faster When I Enable Profiling