码迷,mamicode.com
首页 > 其他好文 > 详细

【转】GCC4.6编译的warning -Werror

时间:2015-06-10 13:57:21      阅读:596      评论:0      收藏:0      [点我收藏+]

标签:

原文网址:http://blog.sina.com.cn/s/blog_605f5b4f0101bct7.html

New warnings for unused variables and parameters

The behavior of -Wall has changed and now includes the new warning flags -Wunused-but-set-variable and (with -Wall -Wextra-Wunused-but-set-parameter. This may result in new warnings in code that compiled cleanly with previous versions of GCC.

For example,

void fn (void) 
 { 
 int foo; 
 foo = bar ();  
 }

Gives the following diagnostic:

warning: variable "foo" set but not used [-Wunused-but-set-variable]

Although these warnings will not result in compilation failure, often -Wall is used in conjunction with -Werror and as a result, new warnings are turned into new errors.

To fix, first see if the unused variable or parameter can be removed without changing the result or logic of the surrounding code. If not, annotate it with __attribute__((__unused__)).

As a workaround, add -Wno-error=unused-but-set-variable or -Wno-error=unused-but-set-parameter.

 

-Werror 
     Make all warnings into errors
-Wall 
-Wall turns on the following warning flags:
                 --Waddress   
          -Warray-bounds (only with -O2)  
          -Wc++11-compat  
          -Wchar-subscripts  
          -Wenum-compare (in C/ObjC; this is on by default in C++) 
          -Wimplicit-int (C and Objective-C only) 
          -Wimplicit-function-declaration (C and Objective-C only) 
          -Wcomment  
          -Wformat   
          -Wmain (only for C/ObjC and unless -ffreestanding)  
          -Wmaybe-uninitialized 
          -Wmissing-braces (only for C/ObjC) 
          -Wnonnull  
          -Wparentheses  
          -Wpointer-sign  
          -Wreorder   
          -Wreturn-type  
          -Wsequence-point  
          -Wsign-compare (only in C++)  
          -Wstrict-aliasing  
          -Wstrict-overflow=1  
          -Wswitch  
          -Wtrigraphs  
          -Wuninitialized  
          -Wunknown-pragmas  
          -Wunused-function  
          -Wunused-label     
          -Wunused-value     
          -Wunused-variable  
          -Wvolatile-register-var 
 -Wextra
This enables some extra warning flags that are not enabled by -Wall. (This option used to be called -W. The older name is still supported, but the newer name is more descriptive.)
          -Wclobbered  
          -Wempty-body  
          -Wignored-qualifiers 
          -Wmissing-field-initializers  
          -Wmissing-parameter-type (C only)  
          -Wold-style-declaration (C only)  
          -Woverride-init  
          -Wsign-compare  
          -Wtype-limits  
          -Wuninitialized  
          -Wunused-parameter (only with -Wunused or -Wall) 
      -Wunused-but-set-parameter (only with -Wunused or -Wall)  

Warn whenever a function parameter is assigned to, but otherwise unused (aside from its declaration).
To suppress this warning use the ‘unused’ attribute (see Variable Attributes).
This warning is also enabled by -Wunused together with -Wextra. 

-Wunused-but-set-parameter
Warn whenever a function parameter is assigned to, but otherwise unused (aside from its declaration).
To suppress this warning use the ‘unused’ attribute (see Variable Attributes).
This warning is also enabled by -Wunused together with -Wextra. 
-Wunused-but-set-variable
Warn whenever a local variable is assigned to, but otherwise unused (aside from its declaration). This warning is enabled by -Wall.
To suppress this warning use the ‘unused’ attribute (see Variable Attributes).
This warning is also enabled by -Wunused, which is enabled by -Wall
-Wunused-function
Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall. 
-Wunused-label
Warn whenever a label is declared but not used. This warning is enabled by -Wall.To suppress this warning use the ‘unused’ attribute (see Variable Attributes). 
-Wunused-local-typedefs (C, Objective-C, C++ and Objective-C++ only)
Warn when a typedef locally defined in a function is not used. This warning is enabled by -Wall. 
-Wunused-parameter
Warn whenever a function parameter is unused aside from its declaration.
To suppress this warning use the ‘unused’ attribute (see Variable Attributes). 



6.61.10 Diagnostic Pragmas

https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html

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 -Werror 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.

#pragma GCC diagnostic kind option
Modifies the disposition of a diagnostic. Note that not all diagnostics are modifiable; at the moment only warnings (normally controlled by ‘-W...’) can be controlled, and not all of them. Use -fdiagnostics-show-option to determine which diagnostics are 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 is in effect), or ‘ignored’ if the diagnostic is to be ignored. option is a double quoted string that matches the command-line option.

          #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 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. 

#pragma GCC diagnostic push
#pragma GCC diagnostic pop
Causes GCC to remember the state of the diagnostics as of each push, and restore to that point at each pop. If a pop has no matching push, the command-line options are restored.
          #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 */

GCC also offers a simple mechanism for printing messages during compilation.

#pragma message string
Prints string as a compiler message on compilation. The message is informational only, and is neither a compilation warning nor an error.
          #pragma message "Compiling " __FILE__ "..."

string may be parenthesized, and is printed with location information. 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’.

 

【转】GCC4.6编译的warning -Werror

标签:

原文地址:http://www.cnblogs.com/wi100sh/p/4565602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!