Home > Enterprise >  Why is gcc not ignoring comment as error when using #pragma GCC diagnostic ignored "-Wcomment&q
Why is gcc not ignoring comment as error when using #pragma GCC diagnostic ignored "-Wcomment&q

Time:02-03

I have some comments that have some macro examples. To make them more readable, I end the line with a \ to continue the macro on the next line. I'd rather not put a space after the \ for other reasons. Our compiler has -Wall -Werror set, so I want to temporarily disable this error. Example code follows:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcomment"
// boo \
// hoo
#pragma GCC diagnostic pop

Demo

Unfortunately, the compiler is still complaining about this error. We're using 7.5, but this still doesn't work under the latest 12.2.

The docs say:

#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.

What it doesn't say is how -fdiagnostics-show-option can determine which diagnostics are controllable, which is annoying.

What am I doing wrong?

CodePudding user response:

The splicing together of lines when one ends in \ happens in the second phase of translation. The determination of what is a comment happens in the third phase. At this point, gcc has all the information it needs to emit that warning. Why wait?

The #pragma lines are processed in phase 4, too late to stop the warning in question. Maybe the warning could be delayed, but given that not all diagnostics are modifiable, I don't see priority given to delaying a warning merely to subject it to a #pragma.


What it doesn't say is how -fdiagnostics-show-option can determine which diagnostics are controllable, which is annoying.

I think the documentation is misleading. I do not see how this option can be used to determine which diagnostics are controllable, other than trial and error. What I see it helping with is determining "which option controls them" (sort of—this option is on by default). This option controls the display of the command line options controlling the warnings that are emitted.

For example, with -fdiagnostics-show-option (the default):

warning: multi-line comment [-Wcomment]

With -fno-diagnostics-show-option (negating the default):

warning: multi-line comment

So this is useful for determining the command line option for a particular warning, which can in turn be used to try to ignore the warning. If the #pramga works, then the warning is one that can be controlled via #pragma (not exactly a mind-blowing deduction).

  • Related