ideasman42
ideasman42

Reputation: 48198

Why does GCC's diagnostic pragma fail for some warning types?

I've noticed only some warnings are supported by GCC's diagnostic pragma.

This works:

#pragma GCC diagnostic error "-Wconversion"

this fails:

#pragma GCC diagnostic error "-Wframe-larger-than=32"

... with the error:

error: unknown option after '#pragma GCC diagnostic' kind [-Werror=pragmas]
#pragma GCC diagnostic error "-Wframe-larger-than"

..both of these arguments work with GCC when passed as command line arguments.


Is there any documentation for which warnings are supported by the GCC diagnostic pragma?

Upvotes: 5

Views: 9943

Answers (2)

Parham Alvani
Parham Alvani

Reputation: 2440

This is the best thing that I've found:

GCC manual at this:

pragma GCC diagnostic kind option: Modifies the disposition of a diagnostic. Note that not all diagnostics are modifiable; 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.

And the GCC manual at this:

Right now, only the C++ front end can honor these options. .... -fdiagnostics-show-option: This option instructs the diagnostic machinery to add text to each diagnostic emitted, which indicates which command line option directly controls that diagnostic, when such an option is known to the diagnostic machinery.

https://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Language-Independent-Options.html

Upvotes: 5

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37954

I suspect that you have found a bug in GCC for this specific option.

With the following basic example (see it live):

#include <stdio.h>

int main(void)
{
    int i = 4;

    printf("%d\n", i);

    return 0;
}

which is compiled with -Wframe-larger-than=2 there is clearly a warning message:

warning: the frame size of 16 bytes is larger than 2 bytes [-Wframe-larger-than=]

However, with a combination of -Werror= (i.e., the full flag is -Werror=frame-larger-than=2), it behaves in a strange way:

error: the frame size of 16 bytes is larger than 1 bytes [-Werror=frame-larger-than=]

What is even more strange is that it looks like that value is completely ignored, as -Werror=frame-larger-than=64 still produces the same error, despite that threshold value is satisfied (i.e. 16 < 64)

(Side note: the GCC version is 4.9.0)

I believe that the -Werror= option handling is somehow connected with #pragma GCC diagnostic error, as the following seems to work:

#include <stdio.h>

#pragma GCC diagnostic error "-Wframe-larger-than="
int main(void)
{
    int i = 4;

    printf("%d\n", i);

    return 0;
}

returning an error as:

error: the frame size of 16 bytes is larger than 1 bytes [-Werror=frame-larger-than=]

but it fails to cooperate with any value, like:

#pragma GCC diagnostic error "-Wframe-larger-than=2"

produces:

warning: unknown option after '#pragma GCC diagnostic' kind [-Wpragmas]

Upvotes: 3

Related Questions