Skip to content

Instantly share code, notes, and snippets.

@tosihisa
Created May 3, 2012 12:47
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tosihisa/2585404 to your computer and use it in GitHub Desktop.
Save tosihisa/2585404 to your computer and use it in GitHub Desktop.
gcc の警告オプションを調べてみた。
#!/usr/bin/gawk -f
#http://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html
BEGIN {
gcc_cmd="gcc -Q --help=warnings,^joined,^undocumented";
w_lv[0] = "";
w_lv[1] = "-Wall";
w_lv[2] = "-Wall -Wextra";
for(i = 0;i < 3;i++){
cmdln = sprintf("%s %s",gcc_cmd,w_lv[i]);
while(cmdln | getline > 0){
if($1 ~ /\-W/){
gsub(/^\-/,"",$1);
res = "\"_\"";
if(NF > 1){
res="\"x\"";
if($2 ~ /enabled/){
res="\"o\"";
}
}
w_opts[$1] = w_opts[$1] "," res;
}
}
close(cmdln);
}
for(w_key in w_opts){
printf("\"%s\"%s\r\n",w_key,w_opts[w_key]);
}
}

前置き

gcc 4.3 以降では、gcc -Q --help=warnings を実行すると、警告一覧が出てきます。
これを使って、-Wall,-Wextra を入れる/入れないでどの様に変化するか確認してみました。

結果

Debian squeeze gcc 4.4.5 の結果です。 これは gcc_warn_query.awk の出力を markdown 記法に変更したものです。gcc_warn_query.awk は、警告オプションの有無をCSV形式(しかもDOS改行)で出力します。

結論を先に書くと、gcc -Q --help=warnings の結果は、多分実際の警告オプションと合っていないと思います。

具体的には、-Wunused-function は、未使用関数(C言語の場合、static な関数ですがどこからも呼ばれていない)を見つけた場合に警告しますが、通常ではこの警告は出ないからです。
とは言え、警告オプション一覧が得られるのはありがたいです。-Wall が実は警告全てを有効にする訳ではないのは以前から知られていますが、改めて有効にすべき警告を見なおしたほうが良いでしょう。
浮動小数点演算を使用しているならば、-Wfloat-equal は指定したほうが良いでしょう。浮動小数点は、数学的に一致しているように見えて、一致していない場合があるからです。
また、条件コンパイルを多用している場合は、-Wundef を指定しておくと、シンボルが無いことによる意図しない条件コンパイルに気づくかも知れません。

いくつか見慣れない警告オプションもあります。時間を見て調べたいと思っています。

警告オプション -Wなし -Wall -Wall と -Wextra
-Wselector x x x
-Wundeclared-selector x x x
-Wampersand _ _ _
-Wformat-contains-nul x o o
-Walign-commons _ _ _
-Wunused-value o o o
-Werror-implicit-function-declaration _ _ _
-Wformat-extra-args x o o
-Wnon-template-friend o o o
-Wconversion x x x
-Waliasing _ _ _
-Wformat-y2k x x x
-Wpointer-to-int-cast o o o
-Wsystem-headers x x x
-Wmissing-parameter-type o o o
-Wabi x x x
-Weffc++ x x x
-Wunknown-pragmas _ _ _
-Wcast-align x x x
-Wstrict-overflow _ _ _
-Wcomment _ _ _
-Wuninitialized x o o
-Wsign-compare o o o
-Wundef _ _ _
-Wc++-compat x x x
-Wmissing-include-dirs _ _ _
-Wdeprecated o o o
-Wunused-macros _ _ _
-Wmissing-noreturn x x x
-Wmissing-braces x o o
-Wcomments _ _ _
-Wformat-zero-length x o o
-Wenum-compare o o o
-Wextra _ _ _
-Wcharacter-truncation _ _ _
-Wstrict-null-sentinel _ _ _
-Wunused-label o o o
-Wmissing-format-attribute x x x
-Wint-to-pointer-cast o o o
-Wimplicit-int x o o
-Wdeprecated-declarations o o o
-Wbad-function-cast x x x
-Wtabs _ _ _
-Wtraditional x x x
-Woverlength-strings o o o
-Wcast-qual x x x
-Wall _ _ _
-Wunused-parameter o o o
-Winvalid-offsetof o o o
-Wpacked x x x
-Wold-style-definition x x x
-Wsequence-point x o o
-Wstrict-selector-match x x x
-Wimplicit-interface _ _ _
-Wdiv-by-zero o o o
-Wsync-nand o o o
-Wunused x o o
-Wtype-limits o o o
-Wparentheses x o o
-Wdisabled-optimization x x x
-Wmultichar _ _ _
-Wswitch-default x x x
-Wtrigraphs _ _ _
-Waddress x o o
-Wfloat-equal x x x
-Wignored-qualifiers o o o
-Wchar-subscripts x o o
-Wendif-labels _ _ _
-Warray-bounds x o o
-Warray-temporaries _ _ _
-Wcoverage-mismatch x x x
-Wstrict-aliasing _ _ _
-Wnon-virtual-dtor x x x
-Wold-style-cast x x x
-Wmissing-declarations x x x
-Wnonnull x o o
-Wc++0x-compat x x x
-Wmissing-field-initializers o o o
-Wtraditional-conversion x x x
-Wattributes o o o
-Wsynth x x x
-Wunused-result o o o
-Wpointer-arith x x x
-Wswitch x o o
-Wintrinsics-std _ _ _
-Wshadow x x x
-Wpragmas o o o
-Wreturn-type x o o
-Wpadded x x x
-Winline x x x
-Wunderflow _ _ _
-Wold-style-declaration o o o
-Wwrite-strings x x x
-Wswitch-enum x x x
-Winit-self x x x
-Wempty-body o o o
-Wdeclaration-after-statement x x x
-Wintrinsic-shadow _ _ _
-Wformat-nonliteral x x x
-Wredundant-decls x x x
-Wstack-protector x x x
-Woverride-init o o o
-Wvariadic-macros _ _ _
-Wstrict-prototypes x x x
-Wsign-promo x x x
-Winvalid-pch _ _ _
-Wline-truncation _ _ _
-Wimplicit-function-declaration o o o
-Waggregate-return x x x
-Wvolatile-register-var x o o
-Wunsafe-loop-optimizations x x x
-Wmain o o o
-Wsurprising _ _ _
-Wmudflap o o o
-Wunreachable-code x x x
-Wprotocol o o o
-Wformat-security x x x
-Wvla o o o
-Woverflow o o o
-Wnested-externs x x x
-Wassign-intercept x x x
-Wlogical-op x x x
-Wformat _ _ _
-Wreorder x x x
-Wunused-function o o o
-Wlong-long o o o
-Wctor-dtor-privacy x x x
-Wpointer-sign o o o
-Wmissing-prototypes x x x
-Wbuiltin-macro-redefined _ _ _
-Wclobbered o o o
-Wunused-variable o o o
-Wpmf-conversions o o o
-Woverloaded-virtual x x x
-Wpacked-bitfield-compat o o o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment