Skip to contentSkip to conversation

kconfig: Add support for -Wimplicit-fallthrough

PATCH In review Gustavo A. R. Silva 4 participants 9 messages Nov 14, 2021 – Nov 15, 2021 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kconfig: Add support for -Wimplicit-fallthrough

Add Kconfig support for -Wimplicit-fallthrough for both GCC and Clang.

The compiler option is under configuration CC_IMPLICIT_FALLTHROUGH, which is enabled by default.

Special thanks to Nathan Chancellor who fixed the Clang bug[1][2]. This bugfix only appears in Clang 14.0.0, so older versions still contain the bug and -Wimplicit-fallthrough won't be enabled for them, for now.

This concludes a long journey and now we are finally getting rid of the unintentional fallthrough bug-class in the kernel, entirely. :)

[1] https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
[2] https://bugs.llvm.org/show_bug.cgi?id=51094

---
 Makefile     | 6 +-----
 init/Kconfig | 5 +++++
 2 files changed, 6 insertions(+), 5 deletions(-)
Makefile +1 -5
index 30c7c81d0437..f18a50daad00 100644
@@ -786,7 +786,7 @@ stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 KBUILD_CFLAGS += $(stackp-flags-y)
 
 KBUILD_CFLAGS-$(CONFIG_WERROR) += -Werror
-KBUILD_CFLAGS += $(KBUILD_CFLAGS-y)
+KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
 
 ifdef CONFIG_CC_IS_CLANG
 KBUILD_CPPFLAGS += -Qunused-arguments
@@ -798,10 +798,6 @@ KBUILD_CFLAGS += -Wno-gnu
 KBUILD_CFLAGS += -mno-global-merge
 else
 
-# Warn about unmarked fall-throughs in switch statement.
-# Disabled for clang while comment to attribute conversion happens and
-# https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
-KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)
 # gcc inanely warns about local variables called 'main'
 KBUILD_CFLAGS += -Wno-main
 endif
init/Kconfig +5 -0
index 11f8a845f259..b0582cd3e096 100644
@@ -885,6 +885,11 @@ config ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
 config CC_HAS_INT128
 	def_bool !$(cc-option,$(m64-flag) -D__SIZEOF_INT128__=0) && 64BIT
 
+config CC_IMPLICIT_FALLTHROUGH
+	string
+	default "-Wimplicit-fallthrough=5" if CC_IS_GCC
+	default "-Wimplicit-fallthrough" if CC_IS_CLANG && $(cc-option,-Wunreachable-code-fallthrough)
+
 #
 # For architectures that know their GCC __int128 support is sound
 #
Signature · 1 line
2.27.0
maintainer Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 15 lines · Gustavo A. R. Silva

Add Kconfig support for -Wimplicit-fallthrough for both GCC and Clang.

The compiler option is under configuration CC_IMPLICIT_FALLTHROUGH, which is enabled by default.

Special thanks to Nathan Chancellor who fixed the Clang bug[1][2]. This bugfix only appears in Clang 14.0.0, so older versions still contain the bug and -Wimplicit-fallthrough won't be enabled for them, for now.

This concludes a long journey and now we are finally getting rid of the unintentional fallthrough bug-class in the kernel, entirely. :)

[1] https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
[2] https://bugs.llvm.org/show_bug.cgi?id=51094

Quoted 7 lines

This appears to do the right thing with both clang-13 and clang-14.

It feels a little odd to have this in Kconfig but if it works and gets the warning enabled, then so be it.

Quoted 49 lines
---
 Makefile     | 6 +-----
 init/Kconfig | 5 +++++
 2 files changed, 6 insertions(+), 5 deletions(-)
Makefile +1 -5
index 30c7c81d0437..f18a50daad00 100644
@@ -786,7 +786,7 @@ stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 KBUILD_CFLAGS += $(stackp-flags-y)
 
 KBUILD_CFLAGS-$(CONFIG_WERROR) += -Werror
-KBUILD_CFLAGS += $(KBUILD_CFLAGS-y)
+KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
 
 ifdef CONFIG_CC_IS_CLANG
 KBUILD_CPPFLAGS += -Qunused-arguments
@@ -798,10 +798,6 @@ KBUILD_CFLAGS += -Wno-gnu
 KBUILD_CFLAGS += -mno-global-merge
 else
 
-# Warn about unmarked fall-throughs in switch statement.
-# Disabled for clang while comment to attribute conversion happens and
-# https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
-KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)
 # gcc inanely warns about local variables called 'main'
 KBUILD_CFLAGS += -Wno-main
 endif
init/Kconfig +5 -0
index 11f8a845f259..b0582cd3e096 100644
@@ -885,6 +885,11 @@ config ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
 config CC_HAS_INT128
 	def_bool !$(cc-option,$(m64-flag) -D__SIZEOF_INT128__=0) && 64BIT
 
+config CC_IMPLICIT_FALLTHROUGH
+	string
+	default "-Wimplicit-fallthrough=5" if CC_IS_GCC
+	default "-Wimplicit-fallthrough" if CC_IS_CLANG && $(cc-option,-Wunreachable-code-fallthrough)
+
 #
 # For architectures that know their GCC __int128 support is sound
 #
Signature · 1 line
2.27.0
#2 lore raw
Quoted 3 lines · Nathan Chancellor

It feels a little odd to have this in Kconfig but if it works and gets the warning enabled, then so be it.

We've actually been actively moving more and more of the compiler flags configuration to Kconfig time.

The Kconfig language makes it fairly easy and natural to do these days, and in some cases the compiler flags end up having a number of other dependencies (not in this case, but look at things like CC_HAS_KASAN_GENERIC etc, where the existence of some compiler flag ends up then also affecting whether other options can be enabled).

So the whole "move it to config time" ends up being the direction we've been going, and the only really unusual case here is that it ends up being done as a string config rather than as a boolean.

And that's just because the different compilers also have different flags in this case ;(

Linus

#3 lore raw
In reply to Nathan Chancellor
Quoted 24 lines · Nathan Chancellor
Quoted 15 lines · Gustavo A. R. Silva

Add Kconfig support for -Wimplicit-fallthrough for both GCC and Clang.

The compiler option is under configuration CC_IMPLICIT_FALLTHROUGH, which is enabled by default.

Special thanks to Nathan Chancellor who fixed the Clang bug[1][2]. This bugfix only appears in Clang 14.0.0, so older versions still contain the bug and -Wimplicit-fallthrough won't be enabled for them, for now.

This concludes a long journey and now we are finally getting rid of the unintentional fallthrough bug-class in the kernel, entirely. :)

[1] https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
[2] https://bugs.llvm.org/show_bug.cgi?id=51094

Quoted 7 lines
Quoted 4 lines

This appears to do the right thing with both clang-13 and clang-14.

Thanks, Nathan.

--
Gustavo

Quoted 53 lines

It feels a little odd to have this in Kconfig but if it works and gets the warning enabled, then so be it.

Quoted 49 lines
---
 Makefile     | 6 +-----
 init/Kconfig | 5 +++++
 2 files changed, 6 insertions(+), 5 deletions(-)
Makefile +1 -5
index 30c7c81d0437..f18a50daad00 100644
@@ -786,7 +786,7 @@ stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 KBUILD_CFLAGS += $(stackp-flags-y)
 
 KBUILD_CFLAGS-$(CONFIG_WERROR) += -Werror
-KBUILD_CFLAGS += $(KBUILD_CFLAGS-y)
+KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
 
 ifdef CONFIG_CC_IS_CLANG
 KBUILD_CPPFLAGS += -Qunused-arguments
@@ -798,10 +798,6 @@ KBUILD_CFLAGS += -Wno-gnu
 KBUILD_CFLAGS += -mno-global-merge
 else
 
-# Warn about unmarked fall-throughs in switch statement.
-# Disabled for clang while comment to attribute conversion happens and
-# https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
-KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)
 # gcc inanely warns about local variables called 'main'
 KBUILD_CFLAGS += -Wno-main
 endif
init/Kconfig +5 -0
index 11f8a845f259..b0582cd3e096 100644
@@ -885,6 +885,11 @@ config ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
 config CC_HAS_INT128
 	def_bool !$(cc-option,$(m64-flag) -D__SIZEOF_INT128__=0) && 64BIT
 
+config CC_IMPLICIT_FALLTHROUGH
+	string
+	default "-Wimplicit-fallthrough=5" if CC_IS_GCC
+	default "-Wimplicit-fallthrough" if CC_IS_CLANG && $(cc-option,-Wunreachable-code-fallthrough)
+
 #
 # For architectures that know their GCC __int128 support is sound
 #
Signature · 1 line
2.27.0
#4 lore raw
In reply to Nathan Chancellor
Quoted 24 lines · Nathan Chancellor
Quoted 15 lines · Gustavo A. R. Silva

Add Kconfig support for -Wimplicit-fallthrough for both GCC and Clang.

The compiler option is under configuration CC_IMPLICIT_FALLTHROUGH, which is enabled by default.

Special thanks to Nathan Chancellor who fixed the Clang bug[1][2]. This bugfix only appears in Clang 14.0.0, so older versions still contain the bug and -Wimplicit-fallthrough won't be enabled for them, for now.

This concludes a long journey and now we are finally getting rid of the unintentional fallthrough bug-class in the kernel, entirely. :)

[1] https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
[2] https://bugs.llvm.org/show_bug.cgi?id=51094

Quoted 7 lines
Quoted 1 line

This appears to do the right thing with both clang-13 and clang-14.

Now that I gave this a look for the GCC side, I think it is wrong.

-Wimplicit-fallthrough=5 was under cc-option because it was only available in GCC 7.x and newer so the build is now broken for GCC 5.x and 6.x:

gcc: error: unrecognized command line option '-Wimplicit-fallthrough=5'; did you mean '-Wno-fallthrough'?

I think this needs to be added (I can send a formal patch tomorrow unless someone wants to beat me to it):

init/Kconfig +1 -1
index 036b750e8d8a..85882c317235 100644
@@ -887,7 +887,7 @@ config CC_HAS_INT128
 
 config CC_IMPLICIT_FALLTHROUGH
 	string
-	default "-Wimplicit-fallthrough=5" if CC_IS_GCC
+	default "-Wimplicit-fallthrough=5" if $(cc-option,-Wimplicit-fallthrough=5)
 	default "-Wimplicit-fallthrough" if CC_IS_CLANG && $(cc-option,-Wunreachable-code-fallthrough)
 
 #

Cheers,
Nathan

Quoted 56 lines

It feels a little odd to have this in Kconfig but if it works and gets the warning enabled, then so be it.

Quoted 49 lines
---
 Makefile     | 6 +-----
 init/Kconfig | 5 +++++
 2 files changed, 6 insertions(+), 5 deletions(-)
Makefile +1 -5
index 30c7c81d0437..f18a50daad00 100644
@@ -786,7 +786,7 @@ stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 KBUILD_CFLAGS += $(stackp-flags-y)
 
 KBUILD_CFLAGS-$(CONFIG_WERROR) += -Werror
-KBUILD_CFLAGS += $(KBUILD_CFLAGS-y)
+KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
 
 ifdef CONFIG_CC_IS_CLANG
 KBUILD_CPPFLAGS += -Qunused-arguments
@@ -798,10 +798,6 @@ KBUILD_CFLAGS += -Wno-gnu
 KBUILD_CFLAGS += -mno-global-merge
 else
 
-# Warn about unmarked fall-throughs in switch statement.
-# Disabled for clang while comment to attribute conversion happens and
-# https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
-KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)
 # gcc inanely warns about local variables called 'main'
 KBUILD_CFLAGS += -Wno-main
 endif
init/Kconfig +5 -0
index 11f8a845f259..b0582cd3e096 100644
@@ -885,6 +885,11 @@ config ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
 config CC_HAS_INT128
 	def_bool !$(cc-option,$(m64-flag) -D__SIZEOF_INT128__=0) && 64BIT
 
+config CC_IMPLICIT_FALLTHROUGH
+	string
+	default "-Wimplicit-fallthrough=5" if CC_IS_GCC
+	default "-Wimplicit-fallthrough" if CC_IS_CLANG && $(cc-option,-Wunreachable-code-fallthrough)
+
 #
 # For architectures that know their GCC __int128 support is sound
 #
Signature · 1 line
2.27.0
#5 lore raw
Quoted 27 lines · Nathan Chancellor
Quoted 24 lines · Nathan Chancellor
Quoted 15 lines · Gustavo A. R. Silva

Add Kconfig support for -Wimplicit-fallthrough for both GCC and Clang.

The compiler option is under configuration CC_IMPLICIT_FALLTHROUGH, which is enabled by default.

Special thanks to Nathan Chancellor who fixed the Clang bug[1][2]. This bugfix only appears in Clang 14.0.0, so older versions still contain the bug and -Wimplicit-fallthrough won't be enabled for them, for now.

This concludes a long journey and now we are finally getting rid of the unintentional fallthrough bug-class in the kernel, entirely. :)

[1] https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
[2] https://bugs.llvm.org/show_bug.cgi?id=51094

Quoted 7 lines
Quoted 1 line

This appears to do the right thing with both clang-13 and clang-14.

Quoted 8 lines

Now that I gave this a look for the GCC side, I think it is wrong.

-Wimplicit-fallthrough=5 was under cc-option because it was only available in GCC 7.x and newer so the build is now broken for GCC 5.x and 6.x:

gcc: error: unrecognized command line option '-Wimplicit-fallthrough=5'; did you mean '-Wno-fallthrough'?

I'll send a patch for this right away. Thanks for the report, Nathan! :)

--
Gustavo

Quoted 77 lines

I think this needs to be added (I can send a formal patch tomorrow unless someone wants to beat me to it):

init/Kconfig +1 -1
index 036b750e8d8a..85882c317235 100644
@@ -887,7 +887,7 @@ config CC_HAS_INT128
 
 config CC_IMPLICIT_FALLTHROUGH
 	string
-	default "-Wimplicit-fallthrough=5" if CC_IS_GCC
+	default "-Wimplicit-fallthrough=5" if $(cc-option,-Wimplicit-fallthrough=5)
 	default "-Wimplicit-fallthrough" if CC_IS_CLANG && $(cc-option,-Wunreachable-code-fallthrough)
 
 #

Cheers,
Nathan

Quoted 56 lines

It feels a little odd to have this in Kconfig but if it works and gets the warning enabled, then so be it.

Quoted 49 lines
---
 Makefile     | 6 +-----
 init/Kconfig | 5 +++++
 2 files changed, 6 insertions(+), 5 deletions(-)
Makefile +1 -5
index 30c7c81d0437..f18a50daad00 100644
@@ -786,7 +786,7 @@ stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 KBUILD_CFLAGS += $(stackp-flags-y)
 
 KBUILD_CFLAGS-$(CONFIG_WERROR) += -Werror
-KBUILD_CFLAGS += $(KBUILD_CFLAGS-y)
+KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
 
 ifdef CONFIG_CC_IS_CLANG
 KBUILD_CPPFLAGS += -Qunused-arguments
@@ -798,10 +798,6 @@ KBUILD_CFLAGS += -Wno-gnu
 KBUILD_CFLAGS += -mno-global-merge
 else
 
-# Warn about unmarked fall-throughs in switch statement.
-# Disabled for clang while comment to attribute conversion happens and
-# https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
-KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)
 # gcc inanely warns about local variables called 'main'
 KBUILD_CFLAGS += -Wno-main
 endif
init/Kconfig +5 -0
index 11f8a845f259..b0582cd3e096 100644
@@ -885,6 +885,11 @@ config ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
 config CC_HAS_INT128
 	def_bool !$(cc-option,$(m64-flag) -D__SIZEOF_INT128__=0) && 64BIT
 
+config CC_IMPLICIT_FALLTHROUGH
+	string
+	default "-Wimplicit-fallthrough=5" if CC_IS_GCC
+	default "-Wimplicit-fallthrough" if CC_IS_CLANG && $(cc-option,-Wunreachable-code-fallthrough)
+
 #
 # For architectures that know their GCC __int128 support is sound
 #
Signature · 1 line
2.27.0
#6 lore raw
Quoted 31 lines · Gustavo A. R. Silva
Quoted 29 lines · Nathan Chancellor
Quoted 24 lines · Nathan Chancellor
Quoted 15 lines · Gustavo A. R. Silva

Add Kconfig support for -Wimplicit-fallthrough for both GCC and Clang.

The compiler option is under configuration CC_IMPLICIT_FALLTHROUGH, which is enabled by default.

Special thanks to Nathan Chancellor who fixed the Clang bug[1][2]. This bugfix only appears in Clang 14.0.0, so older versions still contain the bug and -Wimplicit-fallthrough won't be enabled for them, for now.

This concludes a long journey and now we are finally getting rid of the unintentional fallthrough bug-class in the kernel, entirely. :)

[1] https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
[2] https://bugs.llvm.org/show_bug.cgi?id=51094

Quoted 7 lines
Quoted 1 line

This appears to do the right thing with both clang-13 and clang-14.

Now that I gave this a look for the GCC side, I think it is wrong.

Quoted 8 lines
Quoted 6 lines

-Wimplicit-fallthrough=5 was under cc-option because it was only available in GCC 7.x and newer so the build is now broken for GCC 5.x and 6.x:

gcc: error: unrecognized command line option '-Wimplicit-fallthrough=5'; did you mean '-Wno-fallthrough'?

I'll send a patch for this right away. Thanks for the report, Nathan! :)

Please use a subject prefix different than "kconfig:"

I want to see "kconfig:' only for changes in scripts/kconfig/.

--
Best Regards
Masahiro Yamada

#7 lore raw
Quoted 7 lines · Masahiro Yamada
Quoted 1 line · Gustavo A. R. Silva

Please use a subject prefix different than "kconfig:"

I want to see "kconfig:' only for changes in scripts/kconfig/.

How about kbuild for this case, instead?

Thanks
--
Gustavo

#8 lore raw
Quoted 11 lines · Gustavo A. R. Silva
Quoted 7 lines · Masahiro Yamada
Quoted 1 line · Gustavo A. R. Silva

Please use a subject prefix different than "kconfig:"

I want to see "kconfig:' only for changes in scripts/kconfig/.

How about kbuild for this case, instead?

Sounds good to me.

Signature · 2 lines
Best Regards
Masahiro Yamada