You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
6.0 KiB
HTML

<html lang="en">
<head>
<title>Diagnostic Pragmas - Using the GNU Compiler Collection (GCC)</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Using the GNU Compiler Collection (GCC)">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Pragmas.html#Pragmas" title="Pragmas">
<link rel="prev" href="Weak-Pragmas.html#Weak-Pragmas" title="Weak Pragmas">
<link rel="next" href="Visibility-Pragmas.html#Visibility-Pragmas" title="Visibility Pragmas">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
Copyright (C) 1988-2015 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Funding Free Software'', the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below). A copy of the license is included in the section entitled
``GNU Free Documentation License''.
(a) The FSF's Front-Cover Text is:
A GNU Manual
(b) The FSF's Back-Cover Text is:
You have freedom to copy and modify this GNU Manual, like GNU
software. Copies published by the Free Software Foundation raise
funds for GNU development.-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<a name="Diagnostic-Pragmas"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Visibility-Pragmas.html#Visibility-Pragmas">Visibility Pragmas</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Weak-Pragmas.html#Weak-Pragmas">Weak Pragmas</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Pragmas.html#Pragmas">Pragmas</a>
<hr>
</div>
<h4 class="subsection">6.60.10 Diagnostic Pragmas</h4>
<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><span class="option">-Werror</span></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.
<dl>
<dt><code>#pragma GCC diagnostic </code><var>kind</var> <var>option</var><dd><a name="index-pragma_002c-diagnostic-4178"></a>
Modifies the disposition of a diagnostic. Note that not all
diagnostics are modifiable; at the moment only warnings (normally
controlled by &lsquo;<samp><span class="samp">-W...</span></samp>&rsquo;) can be controlled, and not all of them.
Use <samp><span class="option">-fdiagnostics-show-option</span></samp> to determine which diagnostics
are controllable and which option controls them.
<p><var>kind</var> is &lsquo;<samp><span class="samp">error</span></samp>&rsquo; to treat this diagnostic as an error,
&lsquo;<samp><span class="samp">warning</span></samp>&rsquo; to treat it like a warning (even if <samp><span class="option">-Werror</span></samp> is
in effect), or &lsquo;<samp><span class="samp">ignored</span></samp>&rsquo; if the diagnostic is to be ignored.
<var>option</var> is a double quoted string that matches the command-line
option.
<pre class="smallexample"> #pragma GCC diagnostic warning "-Wformat"
#pragma GCC diagnostic error "-Wformat"
#pragma GCC diagnostic ignored "-Wformat"
</pre>
<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.
<br><dt><code>#pragma GCC diagnostic push</code><dt><code>#pragma GCC diagnostic pop</code><dd>
Causes GCC to remember the state of the diagnostics as of each
<code>push</code>, and restore to that point at each <code>pop</code>. If a
<code>pop</code> has no matching <code>push</code>, the command-line options are
restored.
<pre class="smallexample"> #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>
</dl>
<p>GCC also offers a simple mechanism for printing messages during
compilation.
<dl>
<dt><code>#pragma message </code><var>string</var><dd><a name="index-pragma_002c-diagnostic-4179"></a>
Prints <var>string</var> as a compiler message on compilation. The message
is informational only, and is neither a compilation warning nor an error.
<pre class="smallexample"> #pragma message "Compiling " __FILE__ "..."
</pre>
<p><var>string</var> may be parenthesized, and is printed with location
information. For example,
<pre class="smallexample"> #define DO_PRAGMA(x) _Pragma (#x)
#define TODO(x) DO_PRAGMA(message ("TODO - " #x))
TODO(Remember to fix this)
</pre>
<p class="noindent">prints &lsquo;<samp><span class="samp">/tmp/file.c:4: note: #pragma message:
TODO - Remember to fix this</span></samp>&rsquo;.
</dl>
</body></html>