cclangfall-through

clang not detecting switch fallthroughs


I want clang to catch my inadvertent 'switch' statement fallthroughs. This shell script demonstrates my failure; I show the output after the script itself. What am I doing wrong?

#!/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

The output:

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

Solution

  • Apparently, it is an intentional design decision that -Wimplicit-fallthrough should only take effect in C++11 mode (and not in C mode, or C++03).

    The rationale expressed in the mailing list discussion appears to be something like:

    1. They don't want it enabled by default in C because it will annoy C coders, and (prior to C2x) there was no agreed-on way to indicate intentional fallthrough in the code;
    2. They do want it enabled in C++11 when -Wextra is specified, so they want -Wextra to imply -Wimplicit-fallthrough. But their front end doesn't support having -Wextra imply different options depending on whether we're in C++11 mode or not;
    3. The easiest way to achieve (1) and (2) is just to disable it entirely in C