[all-commits] [llvm/llvm-project] 9ed4a9: [clang] Expose unreachable fallthrough annotation ...

Nathan Chancellor via All-commits all-commits at lists.llvm.org
Mon Aug 16 17:16:08 PDT 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 9ed4a94d6451046a51ef393cd62f00710820a7e8
      https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
  Author: Nathan Chancellor <nathan at kernel.org>
  Date:   2021-08-16 (Mon, 16 Aug 2021)

  Changed paths:
    M clang/include/clang/Basic/DiagnosticGroups.td
    M clang/include/clang/Basic/DiagnosticSemaKinds.td
    M clang/lib/Sema/AnalysisBasedWarnings.cpp
    M clang/test/SemaCXX/P30636.cpp
    M clang/test/SemaCXX/switch-implicit-fallthrough.cpp

  Log Message:
  -----------
  [clang] Expose unreachable fallthrough annotation warning

The Linux kernel has a macro called IS_ENABLED(), which evaluates to a
constant 1 or 0 based on Kconfig selections, allowing C code to be
unconditionally enabled or disabled at build time. For example:

int foo(struct *a, int b) {
    switch (b) {
    case 1:
        if (a->flag || !IS_ENABLED(CONFIG_64BIT))
            return 1;
        __attribute__((fallthrough));
    case 2:
        return 2;
    default:
        return 3;
    }
}

There is an unreachable warning about the fallthrough annotation in the
first case because !IS_ENABLED(CONFIG_64BIT) can be evaluated to 1,
which looks like

        return 1;
        __attribute__((fallthrough));

to clang.

This type of warning is pointless for the Linux kernel because it does
this trick all over the place due to the sheer number of configuration
options that it has.

Add -Wunreachable-code-fallthrough, enabled under -Wunreachable-code, so
that projects that want to warn on unreachable code get this warning but
projects that do not care about unreachable code can still use
-Wimplicit-fallthrough without having to make changes to their code
base.

Fixes PR51094.

Reviewed By: aaron.ballman, nickdesaulniers

Differential Revision: https://reviews.llvm.org/D107933




More information about the All-commits mailing list