GCC 7,-Wimplicit-fallthrough 警告,以及清除它们的便携方式? [英] GCC 7, -Wimplicit-fallthrough warnings, and portable way to clear them?

查看:32
本文介绍了GCC 7,-Wimplicit-fallthrough 警告,以及清除它们的便携方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在捕获来自 GCC 7 的警告,即在 switch 语句中隐式失败.以前,我们在 Clang 下清除了它们(这就是下面评论的原因):

We are catching warnings from GCC 7 for implicit fall through in a switch statement. Previously, we cleared them under Clang (that's the reason for the comment seen below):

g++ -DNDEBUG -g2 -O3 -std=c++17 -Wall -Wextra -fPIC -c authenc.cpp
asn.cpp: In member function ‘void EncodedObjectFilter::Put(const byte*, size_t)’:
asn.cpp:359:18: warning: this statement may fall through [-Wimplicit-fallthrough=]
    m_state = BODY;  // fall through
                  ^
asn.cpp:361:3: note: here
   case BODY:
   ^~~~

GCC 手册 声明使用 __attribute__ ((fallthrough)),但它不可移植.该手册还指出......也可以添加一条贯穿注释以使警告静音",但它只提供FALLTHRU(这真的是唯一的选择吗?):

The GCC manual states to use __attribute__ ((fallthrough)), but its not portable. The manual also states "... it is also possible to add a fallthrough comment to silence the warning", but it only offer FALLTHRU (is this really the only choice?):

switch (cond)
  {
  case 1:
    bar (0);
    /* FALLTHRU */
  default:
    …
  }

是否有一种可移植的方式通过 Clang 和 GCC 的警告清除跌倒?如果有,那是什么?

Is there a portable way to clear the fall through warning for both Clang and GCC? If so, then what is it?

推荐答案

GCC 期望标记注释在自己的行中,如下所示:

GCC expects the marker comment on its own line, like this:

  m_state = BODY;
  // fall through
case BODY:

标记也必须在 case 标签之前;不能有中间的右大括号}.

The marker also has to come right before the case label; there cannot be an intervening closing brace }.

fall through 是 GCC 识别的标记之一.这不仅仅是FALLTHRU.如需完整列表,请参阅 的文档-Wimplicit-fallthrough 选项.另请参阅红帽开发人员博客上的 帖子.

fall through is among the markers recognized by GCC. It's not just FALLTHRU. For a full list, see the documentation of the -Wimplicit-fallthrough option. Also see this posting on the Red Hat Developer blog.

C++17 增加了一个 [[fallthrough]] 属性,可用于抑制此类警告.注意结尾的分号:

C++17 adds a [[fallthrough]] attribute that can be used to suppress such warnings. Note the trailing semicolon:

  m_state = BODY;
  [[fallthrough]];
case BODY:

Clang 支持 -Wimplicit-fallthrough 警告,但不将它们作为 -Wall-Wextra 的一部分启用.Clang 不识别注释标记,因此必须使用基于属性的抑制(这目前意味着 C 前端的非标准 __attribute__((fallthrough)) 构造).

Clang supports -Wimplicit-fallthrough warnings, but does not enable them as part of -Wall or -Wextra. Clang does not recognize comment markers, so the attribute-based suppression has to be used for it (which currently means the non-standard __attribute__((fallthrough)) construct for the C front end).

请注意,仅当编译器实际看到该注释时,使用标记注释抑制警告才有效.如果预处理器单独运行,则需要指示它保留注释,如 -C GCC 的选项.例如,为了避免 ccache 出现虚假警告,您需要指定 -C编译时标记,或者使用最新版本的 ccache,使用 keep_comments_cpp 选项.

Note that suppressing the warning with marker comments only works if the compiler actually sees the comment. If the preprocessor runs separately, it needs to be instructed to preserve comments, as with the -C option of GCC. For example, to avoid spurious warnings with ccache, you need to specify the -C flag when compiling, or, with recent versions of ccache, use the keep_comments_cpp option.

这篇关于GCC 7,-Wimplicit-fallthrough 警告,以及清除它们的便携方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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