Loose-Info.com
Last Update 2021/09/09
TOP - 各種テスト - gcc - 警告関連のオプション - -Wimplicit-fallthrough=オプション引数

-Wimplicit-fallthrough=オプション引数
switch文のcaseラベルでフォールスルーが発生する場合オプション引数で指定した条件で警告を出力

テスト概要

その1
オプション無しおよびオプション引数に0~5を指定した場合の警告出力例

その2
-Wextraオプションを指定した場合の警告出力例

その3
-Wimplicit-fallthroughオプションを指定した場合の警告出力例

実行環境

GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。

使用ファイル


sample.c
#include <stdio.h> int sampFunc(int n1, int n2) { int nRet = 0; switch (n1) { case 0: nRet = 0; break; case 1: /* fallthrough属性により警告を無効化 */ nRet = 1; __attribute__ ((fallthrough)); case 2: nRet = 2; /* test */ case 3: nRet = 3; /* falls through */ case 4: nRet = 4; /* FALLTHRU */ case 5: nRet = 5; /* FALLTHROUGH test */ case 6: if (n2 < 5) { nRet = 6; break; } else if (n2 > 8) { nRet = -1; break; } case 7: if (n2 < 5) { nRet = 7; break; } else { nRet = -1; break; } case 8: nRet = 8; case 9: nRet = 9; break; default: nRet = 10; } return nRet; } int main(void) { printf("%d\n", sampFunc(1, 2)); return 0; }

その1

オプション無しおよびオプション引数に0~5を指定した場合の警告出力例

オプション無しでコンパイルを実行
$ gcc sample.c 警告無し $

オプション引数に0を指定してコンパイルを実行
(警告を完全に無効化)
$ gcc -Wimplicit-fallthrough=0 sample.c 警告無し $

オプション引数に1を指定してコンパイルを実行
(任意のコメント(「.*」の正規表現にマッチ)がフォールスルーコメントとなり、適切な位置に該当コメントがあれば警告発生無し)
$ gcc -Wimplicit-fallthrough=1 sample.c sample.c: In function ‘sampFunc’: sample.c:33:12: warning: this statement may fall through [-Wimplicit-fallthrough=] else if (n2 > 8) case 6はn2=5,6,7,8の場合はフォールスルー発生のため警告発生 ^ sample.c:38:3: note: here case 7: ^~~~ sample.c:50:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 8; case 8はフォールスルーコメントなどが無いため警告発生 ~~~~~^~~ sample.c:51:3: note: here case 9: ^~~~ $

オプション引数に2を指定してコンパイルを実行
(正規表現「.*falls?[ \t-]*thr(ough|u).*」にマッチするコメントがフォールスルーコメントとなり、適切な位置に該当コメントがあれば警告発生無し)
$ gcc -Wimplicit-fallthrough=2 sample.c sample.c: In function ‘sampFunc’: sample.c:16:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 2; case 2はコメントが正規表現にマッチしないため警告発生 ~~~~~^~~ コメント内容 : /* test */ sample.c:18:3: note: here case 3: ^~~~ sample.c:33:12: warning: this statement may fall through [-Wimplicit-fallthrough=] else if (n2 > 8) case 6はn2=5,6,7,8の場合はフォールスルー発生 ^ sample.c:38:3: note: here case 7: ^~~~ sample.c:50:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 8; case 8はフォールスルーコメントなどが無いため警告発生 ~~~~~^~~ sample.c:51:3: note: here case 9: ^~~~ $

オプション引数に3を指定してコンパイルを実行
(GCC 8.2 Manualの正規表現にマッチするコメントがフォールスルーコメントとして機能)
$ gcc -Wimplicit-fallthrough=3 sample.c sample.c: In function ‘sampFunc’: sample.c:16:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 2; case 2はコメントが正規表現にマッチしないため警告発生 ~~~~~^~~ コメント内容 : /* test */ sample.c:18:3: note: here case 3: ^~~~ sample.c:25:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 5; case 5はコメントが正規表現にマッチしないため警告発生 ~~~~~^~~ コメント内容 : /* FALLTHROUGH test */ sample.c:27:3: note: here case 6: ^~~~ sample.c:33:12: warning: this statement may fall through [-Wimplicit-fallthrough=] else if (n2 > 8) case 6はn2=5,6,7,8の場合はフォールスルー発生 ^ sample.c:38:3: note: here case 7: ^~~~ sample.c:50:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 8; case 8はフォールスルーコメントなどが無いため警告発生 ~~~~~^~~ sample.c:51:3: note: here case 9: ^~~~ $

オプション引数に4を指定してコンパイルを実行
(GCC 8.2 Manualの正規表現にマッチするコメントがフォールスルーコメントとして機能)
$ gcc -Wimplicit-fallthrough=4 sample.c sample.c: In function ‘sampFunc’: sample.c:16:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 2; case 2はコメントが正規表現にマッチしないため警告発生 ~~~~~^~~ コメント内容 : /* test */ sample.c:18:3: note: here case 3: ^~~~ sample.c:19:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 3; case 3はコメントが正規表現にマッチしないため警告発生 ~~~~~^~~ コメント内容 : /* falls through */ sample.c:21:3: note: here case 4: ^~~~ sample.c:25:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 5; case 5はコメントが正規表現にマッチしないため警告発生 ~~~~~^~~ コメント内容 : /* FALLTHROUGH test */ sample.c:27:3: note: here case 6: ^~~~ sample.c:33:12: warning: this statement may fall through [-Wimplicit-fallthrough=] else if (n2 > 8) case 6はn2=5,6,7,8の場合はフォールスルー発生 ^ sample.c:38:3: note: here case 7: ^~~~ sample.c:50:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 8; case 8はフォールスルーコメントなどが無いため警告発生 ~~~~~^~~ sample.c:51:3: note: here case 9: ^~~~ $

オプション引数に5を指定してコンパイルを実行
(フォールスルーコメントは無効。属性のみが警告を抑制)
$ gcc -Wimplicit-fallthrough=5 sample.c sample.c: In function ‘sampFunc’: sample.c:16:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 2; ~~~~~^~~ sample.c:18:3: note: here case 3: ^~~~ sample.c:19:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 3; ~~~~~^~~ sample.c:21:3: note: here case 4: ^~~~ sample.c:22:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 4; ~~~~~^~~ sample.c:24:3: note: here case 5: ^~~~ sample.c:25:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 5; ~~~~~^~~ sample.c:27:3: note: here case 6: ^~~~ sample.c:33:12: warning: this statement may fall through [-Wimplicit-fallthrough=] else if (n2 > 8) ^ sample.c:38:3: note: here case 7: ^~~~ sample.c:50:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 8; ~~~~~^~~ sample.c:51:3: note: here case 9: ^~~~ $ いかなるフォールスルーコメントも無効となるため、case 1(属性の指定)以外のフォールスルー発生個所では警告発生

その2

-Wextraオプションを指定した場合の警告出力例

-Wextraオプションを指定してコンパイルを実行
$ gcc -Wextra sample.c sample.c: In function ‘sampFunc’: sample.c:16:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 2; ~~~~~^~~ sample.c:18:3: note: here case 3: ^~~~ sample.c:25:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 5; ~~~~~^~~ sample.c:27:3: note: here case 6: ^~~~ sample.c:33:12: warning: this statement may fall through [-Wimplicit-fallthrough=] else if (n2 > 8) ^ sample.c:38:3: note: here case 7: ^~~~ sample.c:50:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 8; ~~~~~^~~ sample.c:51:3: note: here case 9: ^~~~ $ -Wimplicit-fallthrough=3オプションが含まれるためオプション引数「3」の場合と同じ出力内容

その3

-Wimplicit-fallthroughオプションを指定した場合の警告出力例

-Wimplicit-fallthroughオプションを指定してコンパイルを実行
$ gcc -Wimplicit-fallthrough sample.c sample.c: In function ‘sampFunc’: sample.c:16:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 2; ~~~~~^~~ sample.c:18:3: note: here case 3: ^~~~ sample.c:25:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 5; ~~~~~^~~ sample.c:27:3: note: here case 6: ^~~~ sample.c:33:12: warning: this statement may fall through [-Wimplicit-fallthrough=] else if (n2 > 8) ^ sample.c:38:3: note: here case 7: ^~~~ sample.c:50:9: warning: this statement may fall through [-Wimplicit-fallthrough=] nRet = 8; ~~~~~^~~ sample.c:51:3: note: here case 9: ^~~~ $ -Wimplicit-fallthrough=3オプションと同じ出力