summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/diagnostic-pragmas.html
blob: c5403c4baae6747d5d523ee1a7e75872f0c2f3cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<div class="subsection-level-extent" id="Diagnostic-Pragmas"> <div class="nav-panel"> <p> Next: <a href="visibility-pragmas" accesskey="n" rel="next">Visibility Pragmas</a>, Previous: <a href="weak-pragmas" accesskey="p" rel="prev">Weak Pragmas</a>, Up: <a href="pragmas" accesskey="u" rel="up">Pragmas Accepted by GCC</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div>  <h1 class="subsection" id="Diagnostic-Pragmas-1"><span>6.62.12 Diagnostic Pragmas<a class="copiable-link" href="#Diagnostic-Pragmas-1"> ¶</a></span></h1> <p>GCC allows the user to selectively enable or disable certain types of diagnostics, and change the kind of the diagnostic. For example, a project’s policy might require that all sources compile with <samp class="option">-Werror</samp> but certain files might have exceptions allowing specific types of warnings. Or, a project might selectively enable diagnostics and treat them as errors depending on which preprocessor macros are defined. </p> <dl class="table"> <dt>
<span><code class="code">#pragma GCC diagnostic <var class="var">kind</var> <var class="var">option</var></code><a class="copiable-link" href="#index-pragma_002c-diagnostic"> ¶</a></span>
</dt> <dd> <p>Modifies the disposition of a diagnostic. Note that not all diagnostics are modifiable; at the moment only warnings (normally controlled by ‘<samp class="samp">-W…</samp>’) can be controlled, and not all of them. Use <samp class="option">-fdiagnostics-show-option</samp> to determine which diagnostics are controllable and which option controls them. </p> <p><var class="var">kind</var> is ‘<samp class="samp">error</samp>’ to treat this diagnostic as an error, ‘<samp class="samp">warning</samp>’ to treat it like a warning (even if <samp class="option">-Werror</samp> is in effect), or ‘<samp class="samp">ignored</samp>’ if the diagnostic is to be ignored. <var class="var">option</var> is a double quoted string that matches the command-line option. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#pragma GCC diagnostic warning "-Wformat"
#pragma GCC diagnostic error "-Wformat"
#pragma GCC diagnostic ignored "-Wformat"</pre>
</div> <p>Note that these pragmas override any command-line options. GCC keeps track of the location of each pragma, and issues diagnostics according to the state as of that point in the source file. Thus, pragmas occurring after a line do not affect diagnostics caused by that line. </p> </dd> <dt><code class="code">#pragma GCC diagnostic push</code></dt> <dt><code class="code">#pragma GCC diagnostic pop</code></dt> <dd> <p>Causes GCC to remember the state of the diagnostics as of each <code class="code">push</code>, and restore to that point at each <code class="code">pop</code>. If a <code class="code">pop</code> has no matching <code class="code">push</code>, the command-line options are restored. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#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 */</pre>
</div> </dd> <dt><code class="code">#pragma GCC diagnostic ignored_attributes</code></dt> <dd> <p>Similarly to <samp class="option">-Wno-attributes=</samp>, this pragma allows users to suppress warnings about unknown scoped attributes (in C++11 and C2X). For example, <code class="code">#pragma GCC diagnostic ignored_attributes "vendor::attr"</code> disables warning about the following declaration: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">[[vendor::attr]] void f();</pre>
</div> <p>whereas <code class="code">#pragma GCC diagnostic ignored_attributes "vendor::"</code> prevents warning about both of these declarations: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">[[vendor::safe]] void f();
[[vendor::unsafe]] void f2();</pre>
</div> </dd> </dl> <p>GCC also offers a simple mechanism for printing messages during compilation. </p> <dl class="table"> <dt>
<span><code class="code">#pragma message <var class="var">string</var></code><a class="copiable-link" href="#index-pragma_002c-diagnostic-1"> ¶</a></span>
</dt> <dd> <p>Prints <var class="var">string</var> as a compiler message on compilation. The message is informational only, and is neither a compilation warning nor an error. Newlines can be included in the string by using the ‘<samp class="samp">\n</samp>’ escape sequence. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#pragma message "Compiling " __FILE__ "..."</pre>
</div> <p><var class="var">string</var> may be parenthesized, and is printed with location information. For example, </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define DO_PRAGMA(x) _Pragma (#x)
#define TODO(x) DO_PRAGMA(message ("TODO - " #x))

TODO(Remember to fix this)</pre>
</div> <p>prints ‘<samp class="samp">/tmp/file.c:4: note: #pragma message: TODO - Remember to fix this</samp>’. </p> </dd> <dt>
<span><code class="code">#pragma GCC error <var class="var">message</var></code><a class="copiable-link" href="#index-pragma_002c-diagnostic-2"> ¶</a></span>
</dt> <dd>
<p>Generates an error message. This pragma <em class="emph">is</em> considered to indicate an error in the compilation, and it will be treated as such. </p> <p>Newlines can be included in the string by using the ‘<samp class="samp">\n</samp>’ escape sequence. They will be displayed as newlines even if the <samp class="option">-fmessage-length</samp> option is set to zero. </p> <p>The error is only generated if the pragma is present in the code after pre-processing has been completed. It does not matter however if the code containing the pragma is unreachable: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#if 0
#pragma GCC error "this error is not seen"
#endif
void foo (void)
{
  return;
#pragma GCC error "this error is seen"
}</pre>
</div> </dd> <dt>
<span><code class="code">#pragma GCC warning <var class="var">message</var></code><a class="copiable-link" href="#index-pragma_002c-diagnostic-3"> ¶</a></span>
</dt> <dd>
<p>This is just like ‘<samp class="samp">pragma GCC error</samp>’ except that a warning message is issued instead of an error message. Unless <samp class="option">-Werror</samp> is in effect, in which case this pragma will generate an error as well. </p> </dd> </dl> </div>  <div class="nav-panel"> <p> Next: <a href="visibility-pragmas">Visibility Pragmas</a>, Previous: <a href="weak-pragmas">Weak Pragmas</a>, Up: <a href="pragmas">Pragmas Accepted by GCC</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
  <p class="_attribution-p">
    &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
    <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Diagnostic-Pragmas.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Diagnostic-Pragmas.html</a>
  </p>
</div>