如何让gcc / clang警告在switch语句中遗漏中断 [英] How to make gcc/clang warn about missing breaks in switch statements

查看:387
本文介绍了如何让gcc / clang警告在switch语句中遗漏中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以使 gcc 或 clang 警告在switch语句中遗漏中断?



具体来说,我几乎总是希望case语句以break结束,如果我不这样做,我可以让编译器抱怨。更好的是,如果它会寻找一个突破性声明或//通过评论。



是否有不同的解决方案人们用来帮助​​自己不螺丝这对吗?

解决方案

使用Clang trunk,请使用 -Wimplicit-fallthrough 。如果您使用的是C ++ 11,则可以使用 [[clang :: fallthrough]]; 语句来标记故意的漏洞(请参阅该属性的文档以获取更多信息)。该警告尚未(但)检查通过评论。这个功能不会在即将发布的3.1版本的Clang中出现,但它将(可能!)在3.2中。


$ b 编辑:属性现在是C ++ 17的一部分,名称 [[fallthrough]];


Is there any way to make gcc or clang warn about missing breaks in switch statements?

Specifically, I almost always want case statements to end with breaks, and it would be great it I could get the compiler to complain if I don't. Even better would be if it would look for either a break statement or a "// fall through" comment.

Is there a different solution people use to help themselves not screw this up?

解决方案

With Clang trunk, use -Wimplicit-fallthrough. If you're using C++11, intentional fallthrough can be marked with a [[clang::fallthrough]]; statement (see the documentation for this attribute for more information). The warning does not (yet) check for 'fall through' comments. This feature won't be in the upcoming 3.1 release of Clang, but it will (probably!) be in 3.2.

Edit: Clang's attribute is now part of C++17, under the name [[fallthrough]];.

这篇关于如何让gcc / clang警告在switch语句中遗漏中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆