C ++强制编译时错误/对交换机中隐式下降的警告 [英] C++ Force compile-time error/warning on implicit fall-through in switch
问题描述
switch
语句可以是超级有用的,但会导致一个常见的错误,程序员忘记了一个break语句:
switch(val){
case 0:
foo();
break;
case 1:
bar();
// oops
case 2:
baz();
break;
默认值:
roomba();
}
明显地, 。良好的编码风格建议,当你的下降是故意的,但有时是不够的。
我很确定这个问题的答案是没有,但是:目前(或未来提出)有任何方式能够要求编译器如果 case
没有 break;
中的至少一个,则会抛出错误(或至少警告!某些效果 // fallthru
?对于使用 switch
语句有一个防御性编程选项是很好的。
Well clang有 -Wimplicit-fallthrough
,我不知道但是通过使用 -Weverything
找到。因此,对于此代码,它会给我以下警告( 看看它现场 ): / p>
警告:切换标签之间无注释掉落[-Wimplicit-fallthrough]
情况2:
^
note:insert'[[clang :: fallthrough]];'沉默此警告
case 2:
^
[[clang :: fallthrough]];
note:insert'break;'以避免掉线
case 2:
^
break;
我可以找到这个标志的唯一文档在属性参考,说明:
clang :: fallthrough属性与
-Wimplicit-fallthrough参数一起使用,以注释切换标签之间的故意故障。它只能应用于在任何语句和下一个
开关标签之间的执行点处放置的空语句
。通常使用特定的
注释来标记这些位置,但是这个属性意味着用更多的
严格注释来替换注释,这可以由编译器检查。
并提供如何标记显式插入的示例:
case 44:// warning:unannotated fall-through
g();
[[clang :: fallthrough]];
case 55:// no warning
使用属性来标记显式下拉具有不可移植的缺点。 Visual Studio
生成错误,并且 gcc
生成以下警告:
警告:忽略语句开头的属性[-Wattributes]
这是一个问题,如果你想使用 -Werror
。
code> gcc 4.9 ,并且看起来像 gcc
不支持此警告:
错误:无法识别的命令行选项'-Wimplicit-fallthrough'
c> gcc 错误报告 Bug 7652 - -Wswitch-break:警告如果交换机情况下通过覆盖了 gcc
中的这一努力,它实际上注意到 clang
支持此功能。 / p>
我没有看到为 Visual Studio
生成此类警告的方法。
请注意, Chandler Carruth 说明 -Weverything
不是为生产使用:
这是一个疯狂的组,从字面上启用Clang中的每个警告。
不要在你的代码上使用这个。它是专门为Clang
开发人员设计的,或者用于探索存在的警告。
switch
statements can be super useful, but lead to a common bug where a programmer forgot a break statement:
switch(val) {
case 0:
foo();
break;
case 1:
bar();
// oops
case 2:
baz();
break;
default:
roomba();
}
You won't get a warning obviously since sometimes fall-through is explicitly desired. Good coding style suggests to comment when your fall-through is deliberate, but sometimes that is insufficient.
I'm pretty sure the answer to this question is no, but: is there any way currently (or proposed in the future) to be able to ask the compiler to throw an error (or at least a warning!) if your case
does not have at least one of break;
or something to the effect of // fallthru
? It would be nice to have a defensive programming option for using switch
statements.
Well clang has -Wimplicit-fallthrough
which I did not know about but found by using -Weverything
. So for this code it gives me the following warning (see it live):
warning: unannotated fall-through between switch labels [-Wimplicit-fallthrough]
case 2:
^
note: insert '[[clang::fallthrough]];' to silence this warning
case 2:
^
[[clang::fallthrough]];
note: insert 'break;' to avoid fall-through
case 2:
^
break;
The only documentation I can find for this flag is in the Attribute Reference which says:
The clang::fallthrough attribute is used along with the -Wimplicit-fallthrough argument to annotate intentional fall-through between switch labels. It can only be applied to a null statement placed at a point of execution between any statement and the next switch label. It is common to mark these places with a specific comment, but this attribute is meant to replace comments with a more strict annotation, which can be checked by the compiler.
and provides an example of how to mark explicit fall-through:
case 44: // warning: unannotated fall-through
g();
[[clang::fallthrough]];
case 55: // no warning
This use of an attribute to mark explicit fall-through has the disadvantage of not being portable. Visual Studio
generate an error and gcc
generates the following warning:
warning: attributes at the beginning of statement are ignored [-Wattributes]
which is a problem if you want to use -Werror
.
I tried this with gcc 4.9
and it looks like gcc
does not support this warning:
error: unrecognized command line option '-Wimplicit-fallthrough'
The gcc
bug report Bug 7652 - -Wswitch-break : Warn if a switch case falls through covers this effort in the gcc
and it actually notes that clang
support this feature.
I do not see a way of generating such a warning for Visual Studio
.
Note, Chandler Carruth explains that -Weverything
is not for production use:
This is an insane group that literally enables every warning in Clang. Don't use this on your code. It is intended strictly for Clang developers or for exploring what warnings exist.
but it is useful for figuring out what warnings exist.
这篇关于C ++强制编译时错误/对交换机中隐式下降的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!