Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+

From: Nathan Chancellor
Date: Tue Aug 17 2021 - 00:20:38 EST

On 8/16/2021 5:56 PM, Nathan Chancellor wrote:
Clang prior to 14.0.0 warns when a fallthrough annotation is in an
unreachable spot, which can occur when IS_ENABLED(CONFIG_...) in a
conditional statement prior to the fallthrough annotation such as


which to clang looks like


if CONFIG_... is enabled due to the control flow graph. Example of the
warning in practice:

sound/core/pcm_native.c:3812:3: warning: fallthrough annotation in
unreachable code [-Wimplicit-fallthrough]

Warning on unreachable annotations makes the warning too noisy and
pointless for the kernel due to the nature of guarding some code on
configuration options so it was disabled in commit d936eb238744 ("Revert
"Makefile: Enable -Wimplicit-fallthrough for Clang"").

This has been resolved in clang 14.0.0 by moving the unreachable warning
to its own flag under -Wunreachable-code, which the kernel will most
likely never enable due to situations like this.

Enable -Wimplicit-fallthrough for clang 14+ so that issues such as the
one in commit 652b44453ea9 ("habanalabs/gaudi: fix missing code in ECC
handling") can be caught before they enter the tree.

Signed-off-by: Nathan Chancellor <nathan@xxxxxxxxxx>
Makefile | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index c19d1638da25..91a4a80409e1 100644
--- a/Makefile
+++ b/Makefile
@@ -797,11 +797,17 @@ KBUILD_CFLAGS += -Wno-gnu
# source of a reference will be _MergedGlobals and not on of the whitelisted names.
# See modpost pattern 2
KBUILD_CFLAGS += -mno-global-merge
+# Warn about unmarked fall-throughs in switch statement.
+# Clang prior to 14.0.0 warned on unreachable fallthroughs with
+# -Wimplicit-fallthrough, which is unacceptable due to IS_ENABLED().
+ifeq ($(shell test $(CONFIG_CLANG_VERSION) -ge 140000; echo $$?),0)
+KBUILD_CFLAGS += -Wimplicit-fallthrough
# Warn about unmarked fall-throughs in switch statement.
-# Disabled for clang while comment to attribute conversion happens and
-# is discussed.
KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)

base-commit: a2824f19e6065a0d3735acd9fe7155b104e7edf5

Please do not apply this patch in its current form, as it does not properly credit Gustavo for all of the hard work he has done for enabling this warning.

Additionally, there should be some time for the CI systems to update their clang-14 builds, as the recent 0day report shows.