229 lines · 8.1 KB · Text
RawDownload
# 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 enabledKBUILD_CFLAGS += -WallKBUILD_CFLAGS += -WextraKBUILD_CFLAGS += -WundefKBUILD_CFLAGS += -Werror=implicit-function-declarationKBUILD_CFLAGS += -Werror=implicit-intKBUILD_CFLAGS += -Werror=return-typeKBUILD_CFLAGS += -Werror=strict-prototypesKBUILD_CFLAGS += -Wno-format-securityKBUILD_CFLAGS += -Wno-trigraphsKBUILD_CFLAGS += -Wno-frame-addressKBUILD_CFLAGS += $(call cc-option, -Wno-address-of-packed-member)KBUILD_CFLAGS += -Wmissing-declarationsKBUILD_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# Clang checks for overflow/truncation with '%p', while GCC does not:# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219KBUILD_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-mainendif # Too noisy on range checks and in macros handling both signed and unsigned.KBUILD_CFLAGS += -Wno-type-limits # These result in bogus false positivesKBUILD_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=98217KBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=1) # disable pointer signed / unsigned warnings in gcc 4.0KBUILD_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 += -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 brokenKBUILD_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.auKBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-thanKBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) # Prohibit date/time macros, which would make the build non-deterministicKBUILD_CFLAGS += -Werror=date-time # enforce correct pointer usageKBUILD_CFLAGS += -Werror=incompatible-pointer-types # Require designated initializers for all marked structuresKBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) # Warn if there is an enum types mismatchKBUILD_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-attributeKBUILD_CFLAGS += -Wmissing-include-dirsKBUILD_CFLAGS += -Wunused-const-variable KBUILD_CPPFLAGS += -WundefKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 else # Some diagnostics enabled by default are noisy.# Suppress them by using -Wno... except for W=1.KBUILD_CFLAGS += -Wno-unused-but-set-variableKBUILD_CFLAGS += -Wno-unused-const-variableKBUILD_CFLAGS += $(call cc-option, -Wno-packed-not-aligned)KBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow)ifdef CONFIG_CC_IS_GCCKBUILD_CFLAGS += -Wno-format-truncationendifKBUILD_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 -WformatKBUILD_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-specifierKBUILD_CFLAGS += -Wformat-zero-length -WnonnullKBUILD_CFLAGS += -Wformat-insufficient-argsendifKBUILD_CFLAGS += -Wno-pointer-to-enum-castKBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compareKBUILD_CFLAGS += -Wno-unaligned-accessKBUILD_CFLAGS += -Wno-enum-compare-conditionalendif endif ## W=2 - warnings which occur quite often but may still be relevant#ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) KBUILD_CFLAGS += -Wdisabled-optimizationKBUILD_CFLAGS += -WshadowKBUILD_CFLAGS += $(call cc-option, -Wlogical-op)KBUILD_CFLAGS += -Wunused-macros KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 else # The following turn off the warnings enabled by -WextraKBUILD_CFLAGS += -Wno-missing-field-initializersKBUILD_CFLAGS += -Wno-shift-negative-value ifdef CONFIG_CC_IS_CLANGKBUILD_CFLAGS += -Wno-enum-enum-conversionendif ifdef CONFIG_CC_IS_GCCKBUILD_CFLAGS += -Wno-maybe-uninitializedendif endif ## W=3 - more obscure warnings, can most likely be ignored#ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) KBUILD_CFLAGS += -Wbad-function-castKBUILD_CFLAGS += -Wcast-alignKBUILD_CFLAGS += -Wcast-qualKBUILD_CFLAGS += -WconversionKBUILD_CFLAGS += -WpackedKBUILD_CFLAGS += -WpaddedKBUILD_CFLAGS += -Wpointer-arithKBUILD_CFLAGS += -Wredundant-declsKBUILD_CFLAGS += -Wsign-compareKBUILD_CFLAGS += -Wswitch-default KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 else # The following turn off the warnings enabled by -WextraKBUILD_CFLAGS += -Wno-sign-compareKBUILD_CFLAGS += -Wno-unused-parameter endif ## W=e and CONFIG_WERROR - error out on warnings#ifneq ($(findstring e, $(KBUILD_EXTRA_WARN))$(CONFIG_WERROR),) KBUILD_CPPFLAGS		+= -WerrorKBUILD_AFLAGS		+= -Wa,--fatal-warningsKBUILD_LDFLAGS		+= --fatal-warningsKBUILD_USERCFLAGS	+= -WerrorKBUILD_USERLDFLAGS	+= -Wl,--fatal-warningsKBUILD_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	+= -WerrorKBUILD_HOSTLDFLAGS	+= -Wl,--fatal-warningsKBUILD_HOSTRUSTFLAGS	+= -Dwarnings endif