如何告诉 gcc 在 switch/case 语句上不间断地发出警告(或失败)?
我有一个复杂的 switch
语句,并且我忘记在其中一个 case
的末尾添加 break
。这是相当合法的,因此我陷入了下一个案例
。
如果我忽略添加 break
语句,有没有办法让 gcc 发出警告(甚至更好,失败)?
我意识到有很多有效的用例(并且我经常在代码中使用它们),如 这个问题,所以显然这样的警告(或失败)需要一个简单的豁免,以便我可以轻松地说,“我确实想在这里失败。”
有什么办法告诉 gcc 这样做吗?
I have a complicated switch
statement, and I forgot to put a break
at the end of one of the case
s. This is quite legal, and as a result I had a fall-through to the next case
.
Is there any way to have gcc warn (or even better, fail) if I neglect to put a break
statement?
I realize that there are many valid use cases (and I use them often in my code), as exemplified in this question, so obviously such a warning (or failure) would need a simple waiver so that I could easily say, "I do want to fall-through here."
Is there any way to tell gcc to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
http://gcc 有关于此类功能 (-Wswitch-break) 的讨论.gnu.org/bugzilla/show_bug.cgi?id=7652。但好像还没有实施
There's a discussion about such a feature (-Wswitch-break) at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7652. But it doesn't seem to be implemented yet
此检查可在 Cppcheck 中找到,这是一个针对 C 和 C++ 代码的免费静态分析器。该检查当前标记为“实验性”,因此您需要使用
--experimental
命令行开关将其打开。此检查针对非空 case 子句发出警告,该子句会在没有控制流语句(例如
break
、continue
、return
等的情况下进入下一个 case) ,除非在下一个case
之前有一个带有诸如//fall through
之类措辞的注释。您可以通过查看
switchFallThroughCase
测试用例。This check is available in Cppcheck, a free static analyser for C and C++ code. The check is currently marked "experimental", so you will need to use the
--experimental
command line switch to turn it on.This check warns against a nonempty case clause that falls through to the next case without a control flow statement such as
break
,continue
,return
, etc, unless there is a comment with wording such as// fall through
immediately preceding the nextcase
.You can get an idea for the kinds of constructs this handles by having a look at the
switchFallThroughCase
test cases in the source code.GCC 7 使用
-Wextra
或-Wimplicit-fallthrough(=[1-5])?
启用警告:https://developers.redhat.com/blog/2017/03/10/wimplicit-fallthrough-in-gcc-7/GCC 7 has a warning enabled with
-Wextra
or-Wimplicit-fallthrough(=[1-5])?
: https://developers.redhat.com/blog/2017/03/10/wimplicit-fallthrough-in-gcc-7/我刚刚查看了 gcc 选项,没有一个至少会给您一个通知。
有 -Wswitch、-Wswitch-default 和 -Wswitch-enum ( http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options ),但它们都不适合您。
我最好的选择是使用“else if”语句
I just went through gcc options, and there is none that will at least give you a notice.
There are -Wswitch, -Wswitch-default and -Wswitch-enum ( http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options ), but none of them will work for you.
my best bet would be to use 'else if' statements
您可以为 grep/perl/emacs/etc 构建一个正则表达式来查找
case
之前没有break
的所有位置。You could construct a regexp for grep/perl/emacs/etc to find all places where there's no
break
beforecase
.简短的回答是否定的,gcc 中没有这样的标志来做到这一点。 Switch case 更常用于失败,因此这就是为什么在 gcc 中使用这样的标志没有意义。
Short answer is no, there is no such flag in gcc to do that. Switch case is used for the fall through more often so that is why it does not make sense to have such a flag in gcc.