Original URL: http://blog.csdn.net/lizzywu/article/details/9419145
GCC warnings at each level
Overrides
Variable (code) level from top to bottom: Specify a variable warning
int a __attribute__ ((unused));
Specifies that the variable is unused. Even if the variable is not used, a warning output is ignored at compile time.
File-level: diagnostics (Ignore/warn) in source code file
Syntax:
#pragma GCC diagnostic [error|warning|ignored] "-w< Warning Options >
Diagnostics-Ignore: (Turn off warning)
#pragma gcc diagnostic ignored "-wunused"
#pragma gcc Diagnostic ignored "-wunused-parameter"
Diagnostics-Warning: (Open warning)
#pragma gcc diagnostic warning "-wunused"
#pragma gcc diagnostic warning "-wunused-parameter"
Diagnostics-Error: (Turn on warning-upgrade to error)
# pragma gcc diagnostic error "-wunused"
#pragma gcc diagnostic error "-wunused-parameter"
Usage:
Closes the warning at the beginning of the file and then opens the warning at the end of the file, which ignores the specified warning in the file.
Project-level: command line/compile parameter specifies
Warning:
gcc main.c-wall ignored:
gcc mian.c-wall-wno-unused-parameter//Open All warning, However, the-unused-parameter warning
option format:-w[no-]< warning option is ignored,
such as:-wno-unused-parameter # no-indicates that the warning is ignored during diagnostics
Source: https://github.com/zodiac1111/note/blob/master/note/gcc/gcc-ignored-warning.markdown#%E6%96%87%E4%BB%B6%E7% Ba%a7%e5%9c%a8%e6%ba%90%e4%bb%a3%e7%a0%81%e6%96%87%e4%bb%b6%e4%b8%ad%e8%af%8a%e6%96%ad%e5%bf%bd%e7%95%a5%e8%ad %a6%e5%91%8a
6.59.10 Diagnostic Pragmas
GCC allows the user to selectively enable or disable certain types of diagnostics, and the kind of the diagnostic. For example, a project's policy might require that all sources compile with but -Werror certain files might has exceptions Allowing specific types of warnings. Or, a project might selectively enable diagnostics and treat them as errors depending on which preprocessor macros is def ined.
-
-
#pragma GCC diagnostic
kind option
-
Modifies the disposition of a diagnostic. Note that not all diagnostics is modifiable; At the moment-warnings (normally controlled by ' -W...') can is controlled, and not all of them. Use -fdiagnostics-show-optionTo determine which diagnostics is controllable and which option controls them.
kindIs " error to treat" diagnostic as an "error," to warning treat it like a warning (even if are -Werror in effect), or ' ' If the diagnostic is to be ignored. is option 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 all pragma, and issues diagnostics according to the state as of CE file. Thus, pragmas occurring after a line does not affect diagnostics caused by.
-
-
#pragma GCC diagnostic push
-
-
#pragma GCC diagnostic pop
-
-
Causes GCC
push
to remember the state of the diagnostics as of each, and a restore to that point at each
pop
. If a
pop
has no matching
push
, the command-line options is 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 as string a compiler message on compilation. The message is informational only, and was neither a compilation warning nor an error.
#pragma message "compiling" __file__ "..."
stringMay was parenthesized, and is printed with the 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 .
Source: http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
Example
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-wunused-label"
....
#pragma GCC diagnostic POPs
"Go" gcc warnings at all levels #pragma gcc diagnostic ignored "-wunused-parameter"--good