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

213 satır
8.8KB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Diagnostic Pragmas (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Diagnostic Pragmas (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Diagnostic Pragmas (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Pragmas.html#Pragmas" rel="up" title="Pragmas">
  30. <link href="Visibility-Pragmas.html#Visibility-Pragmas" rel="next" title="Visibility Pragmas">
  31. <link href="Weak-Pragmas.html#Weak-Pragmas" rel="prev" title="Weak Pragmas">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Diagnostic-Pragmas"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Visibility-Pragmas.html#Visibility-Pragmas" accesskey="n" rel="next">Visibility Pragmas</a>, Previous: <a href="Weak-Pragmas.html#Weak-Pragmas" accesskey="p" rel="prev">Weak Pragmas</a>, Up: <a href="Pragmas.html#Pragmas" accesskey="u" rel="up">Pragmas</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="Diagnostic-Pragmas-1"></a>
  68. <h4 class="subsection">6.62.13 Diagnostic Pragmas</h4>
  69. <p>GCC allows the user to selectively enable or disable certain types of
  70. diagnostics, and change the kind of the diagnostic. For example, a
  71. project&rsquo;s policy might require that all sources compile with
  72. <samp>-Werror</samp> but certain files might have exceptions allowing
  73. specific types of warnings. Or, a project might selectively enable
  74. diagnostics and treat them as errors depending on which preprocessor
  75. macros are defined.
  76. </p>
  77. <dl compact="compact">
  78. <dt><code>#pragma GCC diagnostic <var>kind</var> <var>option</var></code></dt>
  79. <dd><a name="index-pragma_002c-diagnostic"></a>
  80. <p>Modifies the disposition of a diagnostic. Note that not all
  81. diagnostics are modifiable; at the moment only warnings (normally
  82. controlled by &lsquo;<samp>-W&hellip;</samp>&rsquo;) can be controlled, and not all of them.
  83. Use <samp>-fdiagnostics-show-option</samp> to determine which diagnostics
  84. are controllable and which option controls them.
  85. </p>
  86. <p><var>kind</var> is &lsquo;<samp>error</samp>&rsquo; to treat this diagnostic as an error,
  87. &lsquo;<samp>warning</samp>&rsquo; to treat it like a warning (even if <samp>-Werror</samp> is
  88. in effect), or &lsquo;<samp>ignored</samp>&rsquo; if the diagnostic is to be ignored.
  89. <var>option</var> is a double quoted string that matches the command-line
  90. option.
  91. </p>
  92. <div class="smallexample">
  93. <pre class="smallexample">#pragma GCC diagnostic warning &quot;-Wformat&quot;
  94. #pragma GCC diagnostic error &quot;-Wformat&quot;
  95. #pragma GCC diagnostic ignored &quot;-Wformat&quot;
  96. </pre></div>
  97. <p>Note that these pragmas override any command-line options. GCC keeps
  98. track of the location of each pragma, and issues diagnostics according
  99. to the state as of that point in the source file. Thus, pragmas occurring
  100. after a line do not affect diagnostics caused by that line.
  101. </p>
  102. </dd>
  103. <dt><code>#pragma GCC diagnostic push</code></dt>
  104. <dt><code>#pragma GCC diagnostic pop</code></dt>
  105. <dd>
  106. <p>Causes GCC to remember the state of the diagnostics as of each
  107. <code>push</code>, and restore to that point at each <code>pop</code>. If a
  108. <code>pop</code> has no matching <code>push</code>, the command-line options are
  109. restored.
  110. </p>
  111. <div class="smallexample">
  112. <pre class="smallexample">#pragma GCC diagnostic error &quot;-Wuninitialized&quot;
  113. foo(a); /* error is given for this one */
  114. #pragma GCC diagnostic push
  115. #pragma GCC diagnostic ignored &quot;-Wuninitialized&quot;
  116. foo(b); /* no diagnostic for this one */
  117. #pragma GCC diagnostic pop
  118. foo(c); /* error is given for this one */
  119. #pragma GCC diagnostic pop
  120. foo(d); /* depends on command-line options */
  121. </pre></div>
  122. </dd>
  123. </dl>
  124. <p>GCC also offers a simple mechanism for printing messages during
  125. compilation.
  126. </p>
  127. <dl compact="compact">
  128. <dt><code>#pragma message <var>string</var></code></dt>
  129. <dd><a name="index-pragma_002c-diagnostic-1"></a>
  130. <p>Prints <var>string</var> as a compiler message on compilation. The message
  131. is informational only, and is neither a compilation warning nor an
  132. error. Newlines can be included in the string by using the &lsquo;<samp>\n</samp>&rsquo;
  133. escape sequence.
  134. </p>
  135. <div class="smallexample">
  136. <pre class="smallexample">#pragma message &quot;Compiling &quot; __FILE__ &quot;...&quot;
  137. </pre></div>
  138. <p><var>string</var> may be parenthesized, and is printed with location
  139. information. For example,
  140. </p>
  141. <div class="smallexample">
  142. <pre class="smallexample">#define DO_PRAGMA(x) _Pragma (#x)
  143. #define TODO(x) DO_PRAGMA(message (&quot;TODO - &quot; #x))
  144. TODO(Remember to fix this)
  145. </pre></div>
  146. <p>prints &lsquo;<samp>/tmp/file.c:4: note: #pragma message:
  147. TODO - Remember to fix this</samp>&rsquo;.
  148. </p>
  149. </dd>
  150. <dt><code>#pragma GCC error <var>message</var></code></dt>
  151. <dd><a name="index-pragma_002c-diagnostic-2"></a>
  152. <p>Generates an error message. This pragma <em>is</em> considered to
  153. indicate an error in the compilation, and it will be treated as such.
  154. </p>
  155. <p>Newlines can be included in the string by using the &lsquo;<samp>\n</samp>&rsquo;
  156. escape sequence. They will be displayed as newlines even if the
  157. <samp>-fmessage-length</samp> option is set to zero.
  158. </p>
  159. <p>The error is only generated if the pragma is present in the code after
  160. pre-processing has been completed. It does not matter however if the
  161. code containing the pragma is unreachable:
  162. </p>
  163. <div class="smallexample">
  164. <pre class="smallexample">#if 0
  165. #pragma GCC error &quot;this error is not seen&quot;
  166. #endif
  167. void foo (void)
  168. {
  169. return;
  170. #pragma GCC error &quot;this error is seen&quot;
  171. }
  172. </pre></div>
  173. </dd>
  174. <dt><code>#pragma GCC warning <var>message</var></code></dt>
  175. <dd><a name="index-pragma_002c-diagnostic-3"></a>
  176. <p>This is just like &lsquo;<samp>pragma GCC error</samp>&rsquo; except that a warning
  177. message is issued instead of an error message. Unless
  178. <samp>-Werror</samp> is in effect, in which case this pragma will generate
  179. an error as well.
  180. </p>
  181. </dd>
  182. </dl>
  183. <hr>
  184. <div class="header">
  185. <p>
  186. Next: <a href="Visibility-Pragmas.html#Visibility-Pragmas" accesskey="n" rel="next">Visibility Pragmas</a>, Previous: <a href="Weak-Pragmas.html#Weak-Pragmas" accesskey="p" rel="prev">Weak Pragmas</a>, Up: <a href="Pragmas.html#Pragmas" accesskey="u" rel="up">Pragmas</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  187. </div>
  188. </body>
  189. </html>