【问题标题】:clang not detecting switch fallthroughs叮当声未检测到开关故障
【发布时间】:2026-01-04 04:15:02
【问题描述】:

我想让 clang 捕捉到我无意中的“switch”语句失败。这个 shell 脚本演示了我的失败;我在脚本本身之后显示输出。我做错了什么?

#!/bin/sh

cat <<EOD > 1.c
#include <stdio.h>
int main(void)
{
  int etwas=5;
  switch(etwas)
  {
    case 5:
    {
      printf("It's 5.\n");
    }
    case 6:
    {
      printf("It's 6.\n");
    }
    default:
    {
      printf("It's something else\n");
      break;
    }
  }
  return 0;
}
EOD

rm -f 1
clang --version
echo === demonstrating that unrecognized warning names are not allowed ===
clang -Wnonsense-warning -Werror -Wall 1.c -o 1
echo === The real compile follows. ===
clang -Wimplicit-fallthrough 1.c -o 1
echo === The execution follows. ===
./1

输出:

FreeBSD clang version 4.0.0 (tags/RELEASE_400/final 297347) (based on LLVM 4.0.0)
Target: x86_64-unknown-freebsd11.1
Thread model: posix
InstalledDir: /usr/bin
=== demonstrating that unrecognized warning names are not allowed ===
error: unknown warning option '-Wnonsense-warning'
      [-Werror,-Wunknown-warning-option]
=== The real compile follows. ===
=== The execution follows. ===
It's 5.
It's 6.
It's something else

【问题讨论】:

  • 你遇到了什么问题?
  • 又是什么问题? -Wnonsense-warning 似乎不是基于输出的 clang 选项。
  • @Pablo:问题与 -Wnonsense-warning 没有直接关系。我想通过显示 clang 抱怨无意义的警告名称来表明 clang 确实识别 -Wimplicit-fallthrough(而不是简单地忽略它不知道的任何警告)。
  • 奇怪的是,当我编译为 C 时,我无法在失败时发出警告。如果我将其更改为 C++,它会正确发出警告,但前提是我将其设置为 C++11@987654322 @ (C++03 doesn't warn)。
  • 我得到与@Cornstalks 相同的结果(使用Apple LLVM v9.0.0)——使用--std=c++11 编译并出现错误。我建议他将他的评论转化为答案。

标签: c clang fall-through


【解决方案1】:

显然,-Wimplicit-fallthrough 应该只在 C++11 模式(而不是 C 模式或 C++03)中生效,这是一个有意的设计决定。

邮件列表讨论中表达的基本原理似乎是这样的:

  1. 他们不希望在 C 中默认启用它,因为它会惹恼 C 编码人员,并且(在 C2x 之前)没有商定的方式来指示代码中的故意失败;
  2. 他们确实希望在指定-Wextra 时在C++11 中启用它,因此他们希望-Wextra 隐含-Wimplicit-fallthrough。但是他们的前端不支持 -Wextra 暗示不同的选项,具体取决于我们是否处于 C++11 模式;
  3. 实现 (1) 和 (2) 的最简单方法就是在 C 中完全禁用它

【讨论】:

  • 好的,他们不希望在 C 中默认启用它。有没有办法在 C 中启用它(使用 clang)? -Wextra -Wimplicit-fallthrough 不会这样做。
  • @BillEvansatMariposa 我不知道; clang 开发人员确实读过 SO,所以也许他们中的一个会稍后出现并发表评论
  • FWIW -std=c2x -Wimplicit-fallthrough[[fallthrough]]; 现在可以在 C 中使用 clang