Skip to contentSkip to conversation

Enable more warnings by default

PATCH Superseded Arnd Bergmann 4 participants 8 messages Apr 4, 2024 – Apr 9, 2024 Kbuild

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Enable more warnings by default

From: Arnd Bergmann <[email protected]>

I think these can now get merged into linux-next. I reordered these slightly from my previous submission to skip the -Wcast-function-type-strict warnings but instead include the -Wformat-truncation enablement, based on what got merged so far.

We are still missing the "kallsyms: rework symbol lookup return codes" and "firmware: dmi-id: add a release callback function" patches, but I expect them to get merged through the respective trees soon.

There is still an open question about the order of patches in the coming merge window: if some of the other patches are also queued for 6.10 rather than 6.9, this series has to come after those to avoid regressions. Maybe they can go into some kind of 'kbuild/late' branch that gets merged in the second half of the merge window?

Arnd

Arnd Bergmann (4):
kbuild: turn on -Wextra by default
kbuild: remove redundant extra warning flags
kbuild: turn on -Wrestrict by default
kbuild: enable -Wformat-truncation on clang

 scripts/Makefile.extrawarn | 29 +++++++++++------------------
 1 file changed, 11 insertions(+), 18 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/4 kbuild: turn on -Wextra by default

From: Arnd Bergmann <[email protected]>

The -Wextra option controls a number of different warnings that differ slightly by compiler version. Some are useful in general, others are better left at W=1 or higher. Based on earlier work, the ones that should be disabled by default are left for the higher warning levels already, and a lot of the useful ones have no remaining output when enabled.

Move the -Wextra option up into the set of default-enabled warnings and just rely on the individual ones getting disabled as needed.

The -Wunused warning was always grouped with this, so turn it on by default as well, except for the -Wunused-parameter warning that really has no value at all for the kernel since many interfaces have intentionally unused arguments.

---
 scripts/Makefile.extrawarn | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/Makefile.extrawarn +4 -1
index c5af566e911a..c247552c192c 100644
@@ -82,12 +82,14 @@ KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
 # Warn if there is an enum types mismatch
 KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
 
+KBUILD_CFLAGS += -Wextra
+KBUILD_CFLAGS += -Wunused
+
 #
 # W=1 - warnings which may be relevant and do not occur too often
 #
 ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
 
-KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter
 KBUILD_CFLAGS += $(call cc-option, -Wrestrict)
 KBUILD_CFLAGS += -Wmissing-format-attribute
 KBUILD_CFLAGS += -Wold-style-definition
@@ -190,6 +192,7 @@ else
 
 # The following turn off the warnings enabled by -Wextra
 KBUILD_CFLAGS += -Wno-sign-compare
+KBUILD_CFLAGS += -Wno-unused-parameter
 
 endif
 
Signature · 1 line
2.39.2
maintainer Acked-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] kbuild: turn on -Wextra by default

Quoted 12 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The -Wextra option controls a number of different warnings that differ slightly by compiler version. Some are useful in general, others are better left at W=1 or higher. Based on earlier work, the ones that should be disabled by default are left for the higher warning levels already, and a lot of the useful ones have no remaining output when enabled.

Move the -Wextra option up into the set of default-enabled warnings and just rely on the individual ones getting disabled as needed.

Quoted 6 lines

The -Wunused warning was always grouped with this, so turn it on by default as well, except for the -Wunused-parameter warning that really has no value at all for the kernel since many interfaces have intentionally unused arguments.

I have not done any LLVM builds with this change but if this is going to be in -next for a little bit, we should be able to get any regressions handled quickly.

I am in favor of more warnings but I am a little nervous this will make compiler upgrades (or tracking their mainline) even more difficult. I do not have a feeling for how often warnings are added to -Wall and -Wextra so this may be unfounded but the kernel's -Werror use complicates this in my opinion. I can engage with the clang folks to try and be given a heads up when a warning is going to be added to -Wextra, it would be good to have someone do something similar for GCC, so that those upgrades do not cause something like this change to be rolled back.

It is easy enough to back out of this if necessary though, so:

Quoted 35 lines
---
 scripts/Makefile.extrawarn | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/Makefile.extrawarn +4 -1
index c5af566e911a..c247552c192c 100644
@@ -82,12 +82,14 @@ KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
 # Warn if there is an enum types mismatch
 KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
 
+KBUILD_CFLAGS += -Wextra
+KBUILD_CFLAGS += -Wunused
+
 #
 # W=1 - warnings which may be relevant and do not occur too often
 #
 ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
 
-KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter
 KBUILD_CFLAGS += $(call cc-option, -Wrestrict)
 KBUILD_CFLAGS += -Wmissing-format-attribute
 KBUILD_CFLAGS += -Wold-style-definition
@@ -190,6 +192,7 @@ else
 
 # The following turn off the warnings enabled by -Wextra
 KBUILD_CFLAGS += -Wno-sign-compare
+KBUILD_CFLAGS += -Wno-unused-parameter
 
 endif
 
Signature · 1 line
2.39.2
#3 lore raw

Re: [PATCH 1/4] kbuild: turn on -Wextra by default

Quoted 7 lines · Nathan Chancellor
Quoted 1 line · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

I have not done any LLVM builds with this change but if this is going to be in -next for a little bit, we should be able to get any regressions handled quickly.

Quoted 8 lines

I am in favor of more warnings but I am a little nervous this will make compiler upgrades (or tracking their mainline) even more difficult. I do not have a feeling for how often warnings are added to -Wall and -Wextra so this may be unfounded but the kernel's -Werror use complicates this in my opinion. I can engage with the clang folks to try and be given a heads up when a warning is going to be added to -Wextra, it would be good to have someone do something similar for GCC, so that those upgrades do not cause something like this change to be rolled back.

My impression is that most new warnings get added to -Wall rather than -Wextra anyway. If they are added to -Wextra, it's still easy enough to turn them off individually unless W=1 is set.

Quoted 3 lines

It is easy enough to back out of this if necessary though, so:

Thanks!

Arnd

#4 lore raw

PATCH 2/4 kbuild: remove redundant extra warning flags

In reply to Arnd Bergmann

From: Arnd Bergmann <[email protected]>

There is no point in turning individual options off and then on again, or vice versa, as the last one always wins. Now that -Wextra always gets passed first, remove all the redundant lines about warnings that are implied by either -Wall or -Wextra, and keep only the last one that disables it in some configurations.

This should not have any effect but keep the Makefile more readable and the command line shorter.

---
 scripts/Makefile.extrawarn | 16 ----------------
 1 file changed, 16 deletions(-)
scripts/Makefile.extrawarn +0 -16
index c247552c192c..17b00d85f6aa 100644
@@ -37,11 +37,6 @@ else
 KBUILD_CFLAGS += -Wno-main
 endif
 
-# These warnings generated too much noise in a regular build.
-# Use make W=1 to enable them (see scripts/Makefile.extrawarn)
-KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
-KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
-
 # These result in bogus false positives
 KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
 
@@ -90,16 +85,8 @@ KBUILD_CFLAGS += -Wunused
 #
 ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
 
-KBUILD_CFLAGS += $(call cc-option, -Wrestrict)
 KBUILD_CFLAGS += -Wmissing-format-attribute
-KBUILD_CFLAGS += -Wold-style-definition
 KBUILD_CFLAGS += -Wmissing-include-dirs
-KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable)
-KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
-KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned)
-KBUILD_CFLAGS += $(call cc-option, -Wformat-overflow)
-KBUILD_CFLAGS += $(call cc-option, -Wformat-truncation)
-KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation)
 
 KBUILD_CPPFLAGS += -Wundef
 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
@@ -150,9 +137,6 @@ ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
 KBUILD_CFLAGS += -Wdisabled-optimization
 KBUILD_CFLAGS += -Wshadow
 KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
-KBUILD_CFLAGS += -Wmissing-field-initializers
-KBUILD_CFLAGS += -Wtype-limits
-KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized)
 KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
 
 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
Signature · 1 line
2.39.2
#5 lore raw

PATCH 3/4 kbuild: turn on -Wrestrict by default

In reply to Arnd Bergmann

From: Arnd Bergmann <[email protected]>

All known -Wrestrict warnings are addressed now, so don't disable the warning any more.

---
 scripts/Makefile.extrawarn | 1 -
 1 file changed, 1 deletion(-)
scripts/Makefile.extrawarn +0 -1
index 17b00d85f6aa..fbe4d7144860 100644
@@ -97,7 +97,6 @@ else
 # Suppress them by using -Wno... except for W=1.
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
-KBUILD_CFLAGS += $(call cc-disable-warning, restrict)
 KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
Signature · 1 line
2.39.2
#6 lore raw

PATCH 4/4 kbuild: enable -Wformat-truncation on clang

In reply to Arnd Bergmann

From: Arnd Bergmann <[email protected]>

This warning option still produces output on gcc but is now clean when building with clang, so enable it conditionally on the compiler for now.

As far as I can tell, the remaining warnings with gcc are the result of analysing the code more deeply across inlining, while clang only does this within a function.

---
 scripts/Makefile.extrawarn | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/Makefile.extrawarn +7 -0
index fbe4d7144860..5063b847a658 100644
@@ -99,7 +99,14 @@ KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
+ifdef CONFIG_CC_IS_GCC
 KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
+else
+# Clang checks for overflow/truncation with '%p', while GCC does not:
+# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219
+KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow-non-kprintf)
+KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation-non-kprintf)
+endif
 KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
 
 KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
Signature · 1 line
2.39.2
Reviewed-by on the v1 series
#7 lore raw
In reply to Arnd Bergmann

Hi,

Quoted 23 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

I think these can now get merged into linux-next. I reordered these slightly from my previous submission to skip the

(quoted diff) +0 -2
-Wcast-function-type-strict warnings but instead include the
-Wformat-truncation enablement, based on what got merged so

far.

We are still missing the "kallsyms: rework symbol lookup return codes" and "firmware: dmi-id: add a release callback function" patches, but I expect them to get merged through the respective trees soon.

There is still an open question about the order of patches in the coming merge window: if some of the other patches are also queued for 6.10 rather than 6.9, this series has to come after those to avoid regressions. Maybe they can go into some kind of 'kbuild/late' branch that gets merged in the second half of the merge window?

Arnd

Quoted 12 lines

Arnd Bergmann (4):
kbuild: turn on -Wextra by default
kbuild: remove redundant extra warning flags
kbuild: turn on -Wrestrict by default
kbuild: enable -Wformat-truncation on clang

 scripts/Makefile.extrawarn | 29 +++++++++++------------------
 1 file changed, 11 insertions(+), 18 deletions(-)
Signature · 1 line
2.39.2

No warnings for me on Clang-18 with this series applied :)

Is there some kbuild documentation somewhere that should be updated/created to reflect which warnings the kernel has enabled by default?

Thanks
Justin