mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-02-15 16:45:05 -05:00
W=2 builds are heavily polluted by the -Wtype-limits warning.
Here are some W=12 statistics on Linux v6.19-rc1 for an x86_64
defconfig (with just CONFIG_WERROR set to "n") using gcc 14.3.1:
Warning name count percent
-------------------------------------------------
-Wlogical-op 2 0.00 %
-Wmaybe-uninitialized 138 0.20 %
-Wunused-macros 869 1.24 %
-Wmissing-field-initializers 1418 2.02 %
-Wshadow 2234 3.19 %
-Wtype-limits 65378 93.35 %
-------------------------------------------------
Total 70039 100.00 %
As we can see, -Wtype-limits represents the vast majority of all
warnings. The reason behind this is that these warnings appear in
some common header files, meaning that some unique warnings are
repeated tens of thousands of times (once per header inclusion).
Add to this the fact that each warning is coupled with a dozen lines
detailing some macro expansion. The end result is that the W=2 output
is just too bloated and painful to use.
Three years ago, I proposed in [1] modifying one such header to
silence that noise. Because the code was not faulty, Linus rejected
the idea and instead suggested simply removing that warning.
At that time, I could not bring myself to send such a patch because,
despite its problems, -Wtype-limits would still catch the below bug:
unsigned int ret;
ret = check();
if (ret < 0)
error();
Meanwhile, based on another suggestion from Linus, I added a new check
to sparse [2] that would catch the above bug without the useless spam.
With this, remove gcc's -Wtype-limits. People who still want to catch
incorrect comparisons between unsigned integers and zero can now use
sparse instead.
On a side note, clang also has a -Wtype-limits warning but:
* it is not enabled in the kernel at the moment because, contrary to
gcc, clang did not include it under -Wextra.
* it does not warn if the code results from a macro expansion. So,
if activated, it would not cause as much spam as gcc does.
* -Wtype-limits is split into four sub-warnings [3] meaning that if
it were to be activated, we could select which one to keep.
So there is no present need to explicitly disable -Wtype-limits in
clang.
[1] linux/bits.h: GENMASK_INPUT_CHECK: reduce W=2 noise by 31% treewide
Link: https://lore.kernel.org/all/20220308141201.2343757-1-mailhol.vincent@wanadoo.fr/
[2] Warn about "unsigned value that used to be signed against zero"
Link: https://lore.kernel.org/all/20250921061337.3047616-1-mailhol@kernel.org/
[3] clang's -Wtype-limits
Link: https://clang.llvm.org/docs/DiagnosticsReference.html#wtype-limits
Reviewed-by: Nicolas Schier <nsc@kernel.org>
Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
Link: https://patch.msgid.link/20251220-remove_wtype-limits-v3-1-24b170af700e@kernel.org
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
238 lines
8.6 KiB
Makefile
238 lines
8.6 KiB
Makefile
# SPDX-License-Identifier: GPL-2.0
|
|
# ==========================================================================
|
|
# make W=... settings
|
|
#
|
|
# There are four warning groups enabled by W=1, W=2, W=3, and W=e
|
|
# They are independent, and can be combined like W=12 or W=123e.
|
|
# ==========================================================================
|
|
|
|
# Default set of warnings, always enabled
|
|
KBUILD_CFLAGS += -Wall
|
|
KBUILD_CFLAGS += -Wextra
|
|
KBUILD_CFLAGS += -Wundef
|
|
KBUILD_CFLAGS += -Werror=implicit-function-declaration
|
|
KBUILD_CFLAGS += -Werror=implicit-int
|
|
KBUILD_CFLAGS += -Werror=return-type
|
|
KBUILD_CFLAGS += -Werror=strict-prototypes
|
|
KBUILD_CFLAGS += -Wno-format-security
|
|
KBUILD_CFLAGS += -Wno-trigraphs
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-frame-address)
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-address-of-packed-member)
|
|
KBUILD_CFLAGS += -Wmissing-declarations
|
|
KBUILD_CFLAGS += -Wmissing-prototypes
|
|
|
|
ifneq ($(CONFIG_FRAME_WARN),0)
|
|
KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
|
|
endif
|
|
|
|
KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds
|
|
|
|
ifdef CONFIG_CC_IS_CLANG
|
|
# The kernel builds with '-std=gnu11' and '-fms-extensions' so use of GNU and
|
|
# Microsoft extensions is acceptable.
|
|
KBUILD_CFLAGS += -Wno-gnu
|
|
KBUILD_CFLAGS += -Wno-microsoft-anon-tag
|
|
|
|
# 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-option, -Wno-format-overflow-non-kprintf)
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation-non-kprintf)
|
|
|
|
# Clang may emit a warning when a const variable, such as the dummy variables
|
|
# in typecheck(), or const member of an aggregate type are not initialized,
|
|
# which can result in unexpected behavior. However, in many audited cases of
|
|
# the "field" variant of the warning, this is intentional because the field is
|
|
# never used within a particular call path, the field is within a union with
|
|
# other non-const members, or the containing object is not const so the field
|
|
# can be modified via memcpy() / memset(). While the variable warning also gets
|
|
# disabled with this same switch, there should not be too much coverage lost
|
|
# because -Wuninitialized will still flag when an uninitialized const variable
|
|
# is used.
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-default-const-init-unsafe)
|
|
else
|
|
|
|
# gcc inanely warns about local variables called 'main'
|
|
KBUILD_CFLAGS += -Wno-main
|
|
endif
|
|
|
|
# Too noisy on range checks and in macros handling both signed and unsigned.
|
|
KBUILD_CFLAGS += -Wno-type-limits
|
|
|
|
# These result in bogus false positives
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-dangling-pointer)
|
|
|
|
# Stack Variable Length Arrays (VLAs) must not be used in the kernel.
|
|
# Function array parameters should, however, be usable, but -Wvla will
|
|
# warn for those. Clang has no way yet to distinguish between the VLA
|
|
# types, so depend on GCC for now to keep stack VLAs out of the tree.
|
|
# https://github.com/llvm/llvm-project/issues/57098
|
|
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217
|
|
KBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=1)
|
|
|
|
# disable pointer signed / unsigned warnings in gcc 4.0
|
|
KBUILD_CFLAGS += -Wno-pointer-sign
|
|
|
|
# In order to make sure new function cast mismatches are not introduced
|
|
# in the kernel (to avoid tripping CFI checking), the kernel should be
|
|
# globally built with -Wcast-function-type.
|
|
KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type)
|
|
|
|
# Currently, disable -Wstringop-overflow for GCC 11, globally.
|
|
KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-option, -Wno-stringop-overflow)
|
|
KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow)
|
|
|
|
# Currently, disable -Wunterminated-string-initialization as broken
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-unterminated-string-initialization)
|
|
|
|
# The allocators already balk at large sizes, so silence the compiler
|
|
# warnings for bounds checks involving those possible values. While
|
|
# -Wno-alloc-size-larger-than would normally be used here, earlier versions
|
|
# of gcc (<9.1) weirdly don't handle the option correctly when _other_
|
|
# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
|
|
# doesn't work (as it is documented to), silently resolving to "0" prior to
|
|
# version 9.1 (and producing an error more recently). Numeric values larger
|
|
# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
|
|
# ignored, continuing to default to PTRDIFF_MAX. So, left with no other
|
|
# choice, we must perform a versioned check to disable this warning.
|
|
# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au
|
|
KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
|
|
KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
|
|
|
|
# Prohibit date/time macros, which would make the build non-deterministic
|
|
KBUILD_CFLAGS += -Werror=date-time
|
|
|
|
# enforce correct pointer usage
|
|
KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
|
|
|
|
# Require designated initializers for all marked structures
|
|
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 += -Wunused
|
|
|
|
#
|
|
# W=1 - warnings which may be relevant and do not occur too often
|
|
#
|
|
ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
|
|
|
|
KBUILD_CFLAGS += -Wmissing-format-attribute
|
|
KBUILD_CFLAGS += -Wmissing-include-dirs
|
|
KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
|
|
|
|
KBUILD_CPPFLAGS += -Wundef
|
|
KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
|
|
|
|
else
|
|
|
|
# Some diagnostics enabled by default are noisy.
|
|
# Suppress them by using -Wno... except for W=1.
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-unused-but-set-variable)
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-unused-const-variable)
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-packed-not-aligned)
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow)
|
|
ifdef CONFIG_CC_IS_GCC
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation)
|
|
endif
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-stringop-truncation)
|
|
|
|
KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
|
|
|
|
ifdef CONFIG_CC_IS_CLANG
|
|
# Clang before clang-16 would warn on default argument promotions.
|
|
ifneq ($(call clang-min-version, 160000),y)
|
|
# Disable -Wformat
|
|
KBUILD_CFLAGS += -Wno-format
|
|
# Then re-enable flags that were part of the -Wformat group that aren't
|
|
# problematic.
|
|
KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier
|
|
KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull
|
|
# Requires clang-12+.
|
|
ifeq ($(call clang-min-version, 120000),y)
|
|
KBUILD_CFLAGS += -Wformat-insufficient-args
|
|
endif
|
|
endif
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-pointer-to-enum-cast)
|
|
KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
|
|
KBUILD_CFLAGS += $(call cc-option, -Wno-unaligned-access)
|
|
KBUILD_CFLAGS += -Wno-enum-compare-conditional
|
|
endif
|
|
|
|
endif
|
|
|
|
#
|
|
# W=2 - warnings which occur quite often but may still be relevant
|
|
#
|
|
ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
|
|
|
|
KBUILD_CFLAGS += -Wdisabled-optimization
|
|
KBUILD_CFLAGS += -Wshadow
|
|
KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
|
|
KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
|
|
|
|
KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
|
|
|
|
else
|
|
|
|
# The following turn off the warnings enabled by -Wextra
|
|
KBUILD_CFLAGS += -Wno-missing-field-initializers
|
|
KBUILD_CFLAGS += -Wno-shift-negative-value
|
|
|
|
ifdef CONFIG_CC_IS_CLANG
|
|
KBUILD_CFLAGS += -Wno-enum-enum-conversion
|
|
endif
|
|
|
|
ifdef CONFIG_CC_IS_GCC
|
|
KBUILD_CFLAGS += -Wno-maybe-uninitialized
|
|
endif
|
|
|
|
endif
|
|
|
|
#
|
|
# W=3 - more obscure warnings, can most likely be ignored
|
|
#
|
|
ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
|
|
|
|
KBUILD_CFLAGS += -Wbad-function-cast
|
|
KBUILD_CFLAGS += -Wcast-align
|
|
KBUILD_CFLAGS += -Wcast-qual
|
|
KBUILD_CFLAGS += -Wconversion
|
|
KBUILD_CFLAGS += -Wpacked
|
|
KBUILD_CFLAGS += -Wpadded
|
|
KBUILD_CFLAGS += -Wpointer-arith
|
|
KBUILD_CFLAGS += -Wredundant-decls
|
|
KBUILD_CFLAGS += -Wsign-compare
|
|
KBUILD_CFLAGS += -Wswitch-default
|
|
|
|
KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
|
|
|
|
else
|
|
|
|
# The following turn off the warnings enabled by -Wextra
|
|
KBUILD_CFLAGS += -Wno-sign-compare
|
|
KBUILD_CFLAGS += -Wno-unused-parameter
|
|
|
|
endif
|
|
|
|
#
|
|
# W=e and CONFIG_WERROR - error out on warnings
|
|
#
|
|
ifneq ($(findstring e, $(KBUILD_EXTRA_WARN))$(CONFIG_WERROR),)
|
|
|
|
KBUILD_CPPFLAGS += -Werror
|
|
KBUILD_AFLAGS += -Wa,--fatal-warnings
|
|
KBUILD_LDFLAGS += --fatal-warnings
|
|
KBUILD_USERCFLAGS += -Werror
|
|
KBUILD_USERLDFLAGS += -Wl,--fatal-warnings
|
|
KBUILD_RUSTFLAGS += -Dwarnings
|
|
|
|
# While hostprog flags are used during build bootstrapping (thus should not
|
|
# depend on CONFIG_ symbols), -Werror is disruptive and should be opted into.
|
|
# Only apply -Werror to hostprogs built after the initial Kconfig stage.
|
|
KBUILD_HOSTCFLAGS += -Werror
|
|
KBUILD_HOSTLDFLAGS += -Wl,--fatal-warnings
|
|
KBUILD_HOSTRUSTFLAGS += -Dwarnings
|
|
|
|
endif
|