📄 warning-options.html
字号:
the prior value shall be read only to determine the value to bestored.". If a program breaks these rules, the results on anyparticular implementation are entirely unpredictable. <p>Examples of code with undefined behavior are <code>a = a++;</code>, <code>a[n]= b[n++]</code> and <code>a[i++] = i;</code>. Some more complicated cases are notdiagnosed by this option, and it may give an occasional false positiveresult, but in general it has been found fairly effective at detectingthis sort of problem in programs. <p>The present implementation of this option only works for C programs. Afuture implementation may also work for C++ programs. <p>The C standard is worded confusingly, therefore there is some debateover the precise meaning of the sequence point rules in subtle cases. Links to discussions of the problem, including proposed formaldefinitions, may be found on our readings page, at<a href="http://gcc.gnu.org/readings.html">http://gcc.gnu.org/readings.html</a>. <br><dt><code>-Wreturn-type</code> <dd>Warn whenever a function is defined with a return-type that defaults to<code>int</code>. Also warn about any <code>return</code> statement with noreturn-value in a function whose return-type is not <code>void</code>. <p>For C++, a function without return type always produces a diagnosticmessage, even when <code>-Wno-return-type</code> is specified. The onlyexceptions are <code>main</code> and functions defined in system headers. <br><dt><code>-Wswitch</code> <dd>Warn whenever a <code>switch</code> statement has an index of enumeral typeand lacks a <code>case</code> for one or more of the named codes of thatenumeration. (The presence of a <code>default</code> label prevents thiswarning.) <code>case</code> labels outside the enumeration range alsoprovoke warnings when this option is used. <br><dt><code>-Wswitch-default</code> <dd>Warn whenever a <code>switch</code> statement does not have a <code>default</code>case. <br><dt><code>-Wswitch-enum</code> <dd>Warn whenever a <code>switch</code> statement has an index of enumeral typeand lacks a <code>case</code> for one or more of the named codes of thatenumeration. <code>case</code> labels outside the enumeration range alsoprovoke warnings when this option is used. <br><dt><code>-Wtrigraphs</code> <dd>Warn if any trigraphs are encountered that might change the meaning ofthe program (trigraphs within comments are not warned about). <br><dt><code>-Wunused-function</code> <dd>Warn whenever a static function is declared but not defined or anon\-inline static function is unused. <br><dt><code>-Wunused-label</code> <dd>Warn whenever a label is declared but not used. <p>To suppress this warning use the <code>unused</code> attribute(see <a href="Variable-Attributes.html#Variable%20Attributes">Variable Attributes</a>). <br><dt><code>-Wunused-parameter</code> <dd>Warn whenever a function parameter is unused aside from its declaration. <p>To suppress this warning use the <code>unused</code> attribute(see <a href="Variable-Attributes.html#Variable%20Attributes">Variable Attributes</a>). <br><dt><code>-Wunused-variable</code> <dd>Warn whenever a local variable or non-constant static variable is unusedaside from its declaration <p>To suppress this warning use the <code>unused</code> attribute(see <a href="Variable-Attributes.html#Variable%20Attributes">Variable Attributes</a>). <br><dt><code>-Wunused-value</code> <dd>Warn whenever a statement computes a result that is explicitly not used. <p>To suppress this warning cast the expression to <code>void</code>. <br><dt><code>-Wunused</code> <dd>All the above <code>-Wunused</code> options combined. <p>In order to get a warning about an unused function parameter, you musteither specify <code>-Wextra -Wunused</code> (note that <code>-Wall</code> implies<code>-Wunused</code>), or separately specify <code>-Wunused-parameter</code>. <br><dt><code>-Wuninitialized</code> <dd>Warn if an automatic variable is used without first being initialized orif a variable may be clobbered by a <code>setjmp</code> call. <p>These warnings are possible only in optimizing compilation,because they require data flow information that is computed onlywhen optimizing. If you don't specify <code>-O</code>, you simply won'tget these warnings. <p>If you want to warn about code which uses the uninitialized value of thevariable in its own initializer, use the <code>-Winit-self</code> option. <p>These warnings occur only for variables that are candidates forregister allocation. Therefore, they do not occur for a variable thatis declared <code>volatile</code>, or whose address is taken, or whose sizeis other than 1, 2, 4 or 8 bytes. Also, they do not occur forstructures, unions or arrays, even when they are in registers. <p>Note that there may be no warning about a variable that is used onlyto compute a value that itself is never used, because suchcomputations may be deleted by data flow analysis before the warningsare printed. <p>These warnings are made optional because GCC is not smartenough to see all the reasons why the code might be correctdespite appearing to have an error. Here is one example of howthis can happen: <pre class="smallexample"> { int x; switch (y) { case 1: x = 1; break; case 2: x = 4; break; case 3: x = 5; } foo (x); } </pre> <p>If the value of <code>y</code> is always 1, 2 or 3, then <code>x</code> isalways initialized, but GCC doesn't know this. Here isanother common case: <pre class="smallexample"> { int save_y; if (change_y) save_y = y, y = new_y; ... if (change_y) y = save_y; } </pre> <p>This has no bug because <code>save_y</code> is used only if it is set. <p>This option also warns when a non-volatile automatic variable might bechanged by a call to <code>longjmp</code>. These warnings as well are possibleonly in optimizing compilation. <p>The compiler sees only the calls to <code>setjmp</code>. It cannot knowwhere <code>longjmp</code> will be called; in fact, a signal handler couldcall it at any point in the code. As a result, you may get a warningeven when there is in fact no problem because <code>longjmp</code> cannotin fact be called at the place which would cause a problem. <p>Some spurious warnings can be avoided if you declare all the functionsyou use that never return as <code>noreturn</code>. See <a href="Function-Attributes.html#Function%20Attributes">Function Attributes</a>. <br><dt><code>-Wunknown-pragmas</code> <dd>Warn when a #pragma directive is encountered which is not understood byGCC. If this command line option is used, warnings will even be issuedfor unknown pragmas in system header files. This is not the case ifthe warnings were only enabled by the <code>-Wall</code> command line option. <br><dt><code>-Wstrict-aliasing</code> <dd>This option is only active when <code>-fstrict-aliasing</code> is active. It warns about code which might break the strict aliasing rules that thecompiler is using for optimization. The warning does not catch allcases, but does attempt to catch the more common pitfalls. It isincluded in <code>-Wall</code>. <br><dt><code>-Wstrict-aliasing=2</code> <dd>This option is only active when <code>-fstrict-aliasing</code> is active. It warns about all code which might break the strict aliasing rules that thecompiler is using for optimization. This warning catches all cases, butit will also give a warning for some ambiguous cases that are safe. <br><dt><code>-Wall</code> <dd>All of the above <code>-W</code> options combined. This enables all thewarnings about constructions that some users consider questionable, andthat are easy to avoid (or modify to prevent the warning), even inconjunction with macros. This also enables some language-specificwarnings described in <a href="C---Dialect-Options.html#C++%20Dialect%20Options">C++ Dialect Options</a> and<a href="Objective-C-Dialect-Options.html#Objective-C%20Dialect%20Options">Objective-C Dialect Options</a>. </dl> <p>The following <code>-W...</code> options are not implied by <code>-Wall</code>. Some of them warn about constructions that users generally do notconsider questionable, but which occasionally you might wish to checkfor; others warn about constructions that are necessary or hard to avoidin some cases, and there is no simple way to modify the code to suppressthe warning. <dl><dt><code>-Wextra</code> <dd>(This option used to be called <code>-W</code>. The older name is stillsupported, but the newer name is more descriptive.) Print extra warningmessages for these events: <ul><li>A function can return either with or without a value. (Fallingoff the end of the function body is considered returning withouta value.) For example, this function would evoke such awarning: <pre class="smallexample"> foo (a) { if (a > 0) return a; } </pre> <li>An expression-statement or the left-hand side of a comma expressioncontains no side effects. To suppress the warning, cast the unused expression to void. For example, an expression such as <code>x[i,j]</code> will cause a warning,but <code>x[(void)i,j]</code> will not. <li>An unsigned value is compared against zero with <code><</code> or <code>>=</code>. <li>A comparison like <code>x<=y<=z</code> appears; this is equivalent to<code>(x<=y ? 1 : 0) <= z</code>, which is a different interpretation fromthat of ordinary mathematical notation. <li>Storage-class specifiers like <code>static</code> are not the first things ina declaration. According to the C Standard, this usage is obsolescent. <li>The return type of a function has a type qualifier such as <code>const</code>. Such a type qualifier has no effect, since the value returned by afunction is not an lvalue. (But don't warn about the GNU extension of<code>volatile void</code> return types. That extension will be warned aboutif <code>-pedantic</code> is specified.) <li>If <code>-Wall</code> or <code>-Wunused</code> is also specified, warn about unusedarguments. <li>A comparison between signed and unsigned values could produce anincorrect result when the signed value is converted to unsigned. (But don't warn if <code>-Wno-sign-compare</code> is also specified.) <li>An aggregate has an initializer which does not initialize all members. For example, the following code would cause such a warning, because<code>x.h</code> would be implicitly initialized to zero: <pre class="smallexample"> struct s { int f, g, h; }; struct s x = { 3, 4 }; </pre> <li>A function parameter is declared without a type specifier in K&R-stylefunctions: <pre class="smallexample"> void foo(bar) { } </pre> <li>An empty body occurs in an <code>if</code> or <code>else</code> statement. <li>A pointer is compared against integer zero with <code><</code>, <code><=</code>,<code>></code>, or <code>>=</code>. <li>A variable might be changed by <code>longjmp</code> or <code>vfork</code>. <li>Any of several floating-point events that often indicate errors, such asoverflow, underflow, loss of precision, etc. <li>(C++ only)An enumerator and a non-enumerator both appear in a conditional expression. <li>(C++ only)A non-static reference or non-static <code>const</code> member appears in aclass without constructors. <li>(C++ only)Ambiguous virtual bases. <li>(C++ only)Subscripting an array which has been declared <code>register</code>. <li>(C++ only)Taking the address of a variable which has been declared <code>register</code>. <li>(C++ only)A base class is not initialized in a derived class' copy constructor. </ul> <br><dt><code>-Wno-div-by-zero</code> <dd>Do not warn about compile-time integer division by zero. Floating pointdivision by zero is not warned about, as it can be a legitimate way ofobtaining infinities and NaNs. <br><dt><code>-Wsystem-headers</code> <dd>Print warning messages for constructs found in system header files. Warnings from system headers are normally suppressed, on the assumptionthat they usually do not indicate real problems and would only make thecompiler output harder to read. Using this command line option tellsGCC to emit warnings from system headers as if they occurred in usercode. However, note that using <code>-Wall</code> in conjunction with thisoption will <em>not</em> warn about unknown pragmas in systemheaders--for that, <code>-Wunknown-pragmas</code> must also be used. <br><dt><code>-Wfloat-equal</code> <dd>Warn if floating point values are used in equality comparisons. <p>The idea behind this is that sometimes it is convenient (for theprogrammer) to consider floating-point values as approximations toinfinitely precise real numbers. If you are doing this, then you needto compute (by analyzing the code, or in some other way) the maximum orlikely maximum error that the computation introduces, and allow for itwhen performing comparisons (and when producing output, but that's adifferent problem). In particular, instead of testing for equality, youwould check to see whether the two values have ranges that overlap; andthis is done with the relational operators, so equality comparisons areprobably mistaken.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -