-Wxxx, -w and #pragma GCC diagnostic warnings

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok,
So I've run into some strangeness when using
various diagnostic warnings.
I'm using avr-gcc version 4.3.4 on linux
but I'm seeing the same
behavior on the gcc 386 native compiler on linux version 4.4.5

A couple of things.
First, whenever -w is used on the commandline,
#pragma GCC diagnostic warning "-Wparentheses"
does not work.
It is like -w cannot be overridden by pragmas.

Ok, so even though it is not documented, I might buy
that this behavior was desired so that there could always be a way to
silence all warnings.

But here is another one.

If I use -Wall on the command line I get the expected warning, but if I use

#pragma GCC diagnostic warning "-Wall"

I don't.

I would have expected -Wall in the pragma to behave like
the commandline option.

Here is my test code:

#include 
#pragma GCC diagnostic warning "-Wparentheses"
//#pragma GCC diagnostic warning "-Wall"

int q;

int
main(int argc, char *argv[])
{
char z;
        z = 3;
        printf("Percent-d\n", z);

        if(q = z)
                printf("True\n");
        return(0);
}

Try compiling it with
avr-gcc bs.c
You will see the warning, then with
avr-gcc -w bs.c
You will see the warning is suppressed.

Then modify the code to turn on off the -Wparentheses pragma
so there are no pragmas enabled and compile it with
avr-gcc -Wall bs.c
and notice that you get the warning.
Now turn on the -Wall pragma and compile it with
avr-gcc bs.c
and notice that you get no warning.

It seems like the -Wall pragma should behave like the
-Wall commandline argument.
Have I missed something?

--- bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From the GCC manual (http://gcc.gnu.org/onlinedocs/gcc-4.3.5/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas):

"[...] 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."

(I cannot get -fdiagnostics-show-option to do anything though, but didn't spend much time trying ...)

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What I noticed is that if you use -fdiagnostics-show-option and also
use the pragma the diagnostic output/warning output string includes the
warning option that controls that option.
Example:
avr-gcc -fdiagnostics-show-option bs.c

now produces:
bs.c:15: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

This is also true if you have no pragmas
and use:
avr-gcc -Wall -fdiagnostics-show-option bs.c

You will get the same output as above.

Not quite what I expected based on the documentation.

So I guess what -fdiagnostics-show-option really does is
show you the warning flag that enabled the particular warning
so you can selectively disable it if you want to
which is actually quite useful.

The documentation for this commandline option could
use a bit of updating for clarity.

--- bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, it somewhat makes sense. The idea behind that #pragma is that
you can enable a relatively high warning level on the commandline
(possibly accompanied by -Werror), and then disable some warnings
where the developer says "this is OK, it's intended that way" at a
fine-grained level within the code. By using -fdiagnostics-show-option,
you get a hint *what* to specify in the pragma.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.