mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-18 09:06:43 +00:00

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
21 lines
547 B
C++
21 lines
547 B
C++
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wimplicit-fallthrough -Wunreachable-code-fallthrough %s
|
|
// expected-no-diagnostics
|
|
|
|
template<bool param>
|
|
int fallthrough_template(int i)
|
|
{
|
|
switch (i) {
|
|
case 1:
|
|
if (param)
|
|
return 3;
|
|
[[clang::fallthrough]]; // no warning here, for an unreachable annotation (in the fallthrough_template<true> case) in a template function
|
|
case 2:
|
|
return 4;
|
|
default:
|
|
return 5;
|
|
}
|
|
}
|
|
|
|
template int fallthrough_template<true>(int);
|
|
|