Movatterモバイル変換


[0]ホーム

URL:


Next:, Previous:, Up:Pragmas Accepted by GCC   [Contents][Index]


6.5.13 Diagnostic Pragmas

GCC allows the user to selectively enable or disable certain types ofdiagnostics, and change the kind of the diagnostic. For example, aproject’s policy might require that all sources compile with-Werror but certain files might have exceptions allowingspecific types of warnings. Or, a project might selectively enablediagnostics and treat them as errors depending on which preprocessormacros are defined.

#pragma GCC diagnostickindoption

Modifies the disposition of a diagnostic. Note that not alldiagnostics are modifiable; at the moment only warnings (normallycontrolled by ‘-W…’) can be controlled, and not all of them.Use-fdiagnostics-show-option to determine which diagnosticsare controllable and which option controls them.

kind is ‘error’ to treat this diagnostic as an error,‘warning’ to treat it like a warning (even if-Werror isin effect), or ‘ignored’ if the diagnostic is to be ignored.option is a double quoted string that matches the command-lineoption.

#pragma GCC diagnostic warning "-Wformat"#pragma GCC diagnostic error "-Wformat"#pragma GCC diagnostic ignored "-Wformat"

Note that these pragmas override any command-line options. GCC keepstrack of the location of each pragma, and issues diagnostics accordingto the state as of that point in the source file. Thus, pragmas occurringafter a line do not affect diagnostics caused by that line.

#pragma GCC diagnostic push
#pragma GCC diagnostic pop

Causes GCC to remember the state of the diagnostics as of eachpush, and restore to that point at eachpop. If apop has no matchingpush, the command-line options arerestored.

#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 */
#pragma GCC diagnostic ignored_attributes

Similarly to-Wno-attributes=, this pragma allows users to suppresswarnings about unknown scoped attributes (in C++11 and C23). For example,#pragma GCC diagnostic ignored_attributes "vendor::attr" disableswarning about the following declaration:

[[vendor::attr]] void f();

whereas#pragma GCC diagnostic ignored_attributes "vendor::" preventswarning about both of these declarations:

[[vendor::safe]] void f();[[vendor::unsafe]] void f2();

GCC also offers a simple mechanism for printing messages duringcompilation.

#pragma messagestring

Printsstring as a compiler message on compilation. The messageis informational only, and is neither a compilation warning nor anerror. Newlines can be included in the string by using the ‘\n’escape sequence.

#pragma message "Compiling " __FILE__ "..."

string may be parenthesized, and is printed with locationinformation. For example,

#define DO_PRAGMA(x) _Pragma (#x)#define TODO(x) DO_PRAGMA(message ("TODO - " #x))TODO(Remember to fix this)

prints ‘/tmp/file.c:4: note: #pragma message:TODO - Remember to fix this’.

#pragma GCC errormessage

Generates an error message. This pragmais considered toindicate an error in the compilation, and it will be treated as such.

Newlines can be included in the string by using the ‘\n’escape sequence. They will be displayed as newlines even if the-fmessage-length option is set to zero.

The error is only generated if the pragma is present in the code afterpre-processing has been completed. It does not matter however if thecode containing the pragma is unreachable:

#if 0#pragma GCC error "this error is not seen"#endifvoid foo (void){  return;#pragma GCC error "this error is seen"}
#pragma GCC warningmessage

This is just like ‘pragma GCC error’ except that a warningmessage is issued instead of an error message. Unless-Werror is in effect, in which case this pragma will generatean error as well.


Next:Visibility Pragmas, Previous:Weak Pragmas, Up:Pragmas Accepted by GCC   [Contents][Index]


[8]ページ先頭

©2009-2026 Movatter.jp