如何告诉 gcc 在 switch/case 语句上不间断地发出警告(或失败)?

发布于 2024-12-09 07:01:15 字数 429 浏览 5 评论 0原文

我有一个复杂的 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 cases. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风月客 2024-12-16 07:01:15

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

千鲤 2024-12-16 07:01:15

此检查可在 Cppcheck 中找到,这是一个针对 C 和 C++ 代码的免费静态分析器。该检查当前标记为“实验性”,因此您需要使用 --experimental 命令行开关将其打开。

此检查针对非空 case 子句发出警告,该子句会在没有控制流语句(例如 breakcontinuereturn 等的情况下进入下一个 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 next case.

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.

等风来 2024-12-16 07:01:15

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/

一曲琵琶半遮面シ 2024-12-16 07:01:15

我刚刚查看了 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

盛装女皇 2024-12-16 07:01:15

您可以为 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 before case.

囚我心虐我身 2024-12-16 07:01:15

简短的回答是否定的,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文