1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 2a86fe353SMasahiro Yamada# ========================================================================== 3a86fe353SMasahiro Yamada# make W=... settings 4a86fe353SMasahiro Yamada# 5c77d06e7SYann Droneaud# There are four warning groups enabled by W=1, W=2, W=3, and W=e 6c77d06e7SYann Droneaud# They are independent, and can be combined like W=12 or W=123e. 7a86fe353SMasahiro Yamada# ========================================================================== 8a86fe353SMasahiro Yamada 9e88ca243SArnd Bergmann# Default set of warnings, always enabled 10e88ca243SArnd BergmannKBUILD_CFLAGS += -Wall 114f79eaa2SNathan ChancellorKBUILD_CFLAGS += -Wextra 12e88ca243SArnd BergmannKBUILD_CFLAGS += -Wundef 13e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=implicit-function-declaration 14e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=implicit-int 15e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=return-type 16e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=strict-prototypes 17e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-format-security 18e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-trigraphs 19*379b749aSThomas WeißschuhKBUILD_CFLAGS += -Wno-frame-address 20bad14b5dSLinus TorvaldsKBUILD_CFLAGS += $(call cc-option, -Wno-address-of-packed-member) 210fcb7085SArnd BergmannKBUILD_CFLAGS += -Wmissing-declarations 220fcb7085SArnd BergmannKBUILD_CFLAGS += -Wmissing-prototypes 23e88ca243SArnd Bergmann 24e88ca243SArnd Bergmannifneq ($(CONFIG_FRAME_WARN),0) 25e88ca243SArnd BergmannKBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN) 26e88ca243SArnd Bergmannendif 27e88ca243SArnd Bergmann 28e88ca243SArnd BergmannKBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds 29e88ca243SArnd Bergmann 30e88ca243SArnd Bergmannifdef CONFIG_CC_IS_CLANG 31c4781dc3SRasmus Villemoes# The kernel builds with '-std=gnu11' and '-fms-extensions' so use of GNU and 32c4781dc3SRasmus Villemoes# Microsoft extensions is acceptable. 33e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-gnu 34c4781dc3SRasmus VillemoesKBUILD_CFLAGS += -Wno-microsoft-anon-tag 35738fc998SNathan Chancellor 36738fc998SNathan Chancellor# Clang checks for overflow/truncation with '%p', while GCC does not: 37738fc998SNathan Chancellor# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219 38bad14b5dSLinus TorvaldsKBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow-non-kprintf) 39bad14b5dSLinus TorvaldsKBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation-non-kprintf) 40d0afcfebSNathan Chancellor 41d0afcfebSNathan Chancellor# Clang may emit a warning when a const variable, such as the dummy variables 42d0afcfebSNathan Chancellor# in typecheck(), or const member of an aggregate type are not initialized, 43d0afcfebSNathan Chancellor# which can result in unexpected behavior. However, in many audited cases of 44d0afcfebSNathan Chancellor# the "field" variant of the warning, this is intentional because the field is 45d0afcfebSNathan Chancellor# never used within a particular call path, the field is within a union with 46d0afcfebSNathan Chancellor# other non-const members, or the containing object is not const so the field 47d0afcfebSNathan Chancellor# can be modified via memcpy() / memset(). While the variable warning also gets 48d0afcfebSNathan Chancellor# disabled with this same switch, there should not be too much coverage lost 49d0afcfebSNathan Chancellor# because -Wuninitialized will still flag when an uninitialized const variable 50d0afcfebSNathan Chancellor# is used. 51bad14b5dSLinus TorvaldsKBUILD_CFLAGS += $(call cc-option, -Wno-default-const-init-unsafe) 52e88ca243SArnd Bergmannelse 53e88ca243SArnd Bergmann 54e88ca243SArnd Bergmann# gcc inanely warns about local variables called 'main' 55e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-main 56e88ca243SArnd Bergmannendif 57e88ca243SArnd Bergmann 58660e8991SVincent Mailhol# Too noisy on range checks and in macros handling both signed and unsigned. 59660e8991SVincent MailholKBUILD_CFLAGS += -Wno-type-limits 60660e8991SVincent Mailhol 61e88ca243SArnd Bergmann# These result in bogus false positives 62bad14b5dSLinus TorvaldsKBUILD_CFLAGS += $(call cc-option, -Wno-dangling-pointer) 63e88ca243SArnd Bergmann 645e88c48cSKees Cook# Stack Variable Length Arrays (VLAs) must not be used in the kernel. 655e88c48cSKees Cook# Function array parameters should, however, be usable, but -Wvla will 665e88c48cSKees Cook# warn for those. Clang has no way yet to distinguish between the VLA 675e88c48cSKees Cook# types, so depend on GCC for now to keep stack VLAs out of the tree. 685e88c48cSKees Cook# https://github.com/llvm/llvm-project/issues/57098 695e88c48cSKees Cook# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217 705e88c48cSKees CookKBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=1) 71e88ca243SArnd Bergmann 72e88ca243SArnd Bergmann# disable pointer signed / unsigned warnings in gcc 4.0 73e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-pointer-sign 74e88ca243SArnd Bergmann 75e88ca243SArnd Bergmann# In order to make sure new function cast mismatches are not introduced 76e88ca243SArnd Bergmann# in the kernel (to avoid tripping CFI checking), the kernel should be 77e88ca243SArnd Bergmann# globally built with -Wcast-function-type. 78*379b749aSThomas WeißschuhKBUILD_CFLAGS += -Wcast-function-type 79e88ca243SArnd Bergmann 804f79eaa2SNathan Chancellor# Currently, disable -Wstringop-overflow for GCC 11, globally. 81bad14b5dSLinus TorvaldsKBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-option, -Wno-stringop-overflow) 824f79eaa2SNathan ChancellorKBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow) 834f79eaa2SNathan Chancellor 844f79eaa2SNathan Chancellor# Currently, disable -Wunterminated-string-initialization as broken 85bad14b5dSLinus TorvaldsKBUILD_CFLAGS += $(call cc-option, -Wno-unterminated-string-initialization) 864f79eaa2SNathan Chancellor 87e88ca243SArnd Bergmann# The allocators already balk at large sizes, so silence the compiler 88e88ca243SArnd Bergmann# warnings for bounds checks involving those possible values. While 89e88ca243SArnd Bergmann# -Wno-alloc-size-larger-than would normally be used here, earlier versions 90e88ca243SArnd Bergmann# of gcc (<9.1) weirdly don't handle the option correctly when _other_ 91e88ca243SArnd Bergmann# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX 92e88ca243SArnd Bergmann# doesn't work (as it is documented to), silently resolving to "0" prior to 93e88ca243SArnd Bergmann# version 9.1 (and producing an error more recently). Numeric values larger 94e88ca243SArnd Bergmann# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently 95e88ca243SArnd Bergmann# ignored, continuing to default to PTRDIFF_MAX. So, left with no other 96e88ca243SArnd Bergmann# choice, we must perform a versioned check to disable this warning. 97e88ca243SArnd Bergmann# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au 98e88ca243SArnd BergmannKBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than 99e88ca243SArnd BergmannKBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) 100e88ca243SArnd Bergmann 101e88ca243SArnd Bergmann# Prohibit date/time macros, which would make the build non-deterministic 102e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=date-time 103e88ca243SArnd Bergmann 104e88ca243SArnd Bergmann# enforce correct pointer usage 105*379b749aSThomas WeißschuhKBUILD_CFLAGS += -Werror=incompatible-pointer-types 106e88ca243SArnd Bergmann 107e88ca243SArnd Bergmann# Require designated initializers for all marked structures 108e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) 109e88ca243SArnd Bergmann 110e88ca243SArnd Bergmann# Warn if there is an enum types mismatch 111e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Wenum-conversion) 112e88ca243SArnd Bergmann 113f5982cceSArnd BergmannKBUILD_CFLAGS += -Wunused 114f5982cceSArnd Bergmann 11564a91907SMasahiro Yamada# 11664a91907SMasahiro Yamada# W=1 - warnings which may be relevant and do not occur too often 11764a91907SMasahiro Yamada# 118e27128dbSMasahiro Yamadaifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) 119a86fe353SMasahiro Yamada 12064a91907SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-format-attribute 12164a91907SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-include-dirs 122*379b749aSThomas WeißschuhKBUILD_CFLAGS += -Wunused-const-variable 123a86fe353SMasahiro Yamada 12480b6093bSMasahiro YamadaKBUILD_CPPFLAGS += -Wundef 1256863f564SMasahiro YamadaKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 1266863f564SMasahiro Yamada 12726ea6bb1SBehan Websterelse 12826ea6bb1SBehan Webster 12964a91907SMasahiro Yamada# Some diagnostics enabled by default are noisy. 13064a91907SMasahiro Yamada# Suppress them by using -Wno... except for W=1. 131*379b749aSThomas WeißschuhKBUILD_CFLAGS += -Wno-unused-but-set-variable 132*379b749aSThomas WeißschuhKBUILD_CFLAGS += -Wno-unused-const-variable 133bad14b5dSLinus TorvaldsKBUILD_CFLAGS += $(call cc-option, -Wno-packed-not-aligned) 134bad14b5dSLinus TorvaldsKBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow) 135908dd508SArnd Bergmannifdef CONFIG_CC_IS_GCC 136*379b749aSThomas WeißschuhKBUILD_CFLAGS += -Wno-format-truncation 137908dd508SArnd Bergmannendif 138bad14b5dSLinus TorvaldsKBUILD_CFLAGS += $(call cc-option, -Wno-stringop-truncation) 13964a91907SMasahiro Yamada 140c40845e3SArnd BergmannKBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang 141c40845e3SArnd Bergmann 142076f421dSMasahiro Yamadaifdef CONFIG_CC_IS_CLANG 143b0839b28SNick Desaulniers# Clang before clang-16 would warn on default argument promotions. 14488b61e3bSNick Desaulniersifneq ($(call clang-min-version, 160000),y) 145b0839b28SNick Desaulniers# Disable -Wformat 14621f9c8a1SLinus TorvaldsKBUILD_CFLAGS += -Wno-format 147b0839b28SNick Desaulniers# Then re-enable flags that were part of the -Wformat group that aren't 148b0839b28SNick Desaulniers# problematic. 149b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier 150b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-zero-length -Wnonnull 151b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-insufficient-args 152b0839b28SNick Desaulniersendif 153*379b749aSThomas WeißschuhKBUILD_CFLAGS += -Wno-pointer-to-enum-cast 154afe956c5SNathan ChancellorKBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare 155*379b749aSThomas WeißschuhKBUILD_CFLAGS += -Wno-unaligned-access 15675b5ab13SNathan ChancellorKBUILD_CFLAGS += -Wno-enum-compare-conditional 15726ea6bb1SBehan Websterendif 15864a91907SMasahiro Yamada 15964a91907SMasahiro Yamadaendif 16064a91907SMasahiro Yamada 16164a91907SMasahiro Yamada# 16264a91907SMasahiro Yamada# W=2 - warnings which occur quite often but may still be relevant 16364a91907SMasahiro Yamada# 164e27128dbSMasahiro Yamadaifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) 16564a91907SMasahiro Yamada 16664a91907SMasahiro YamadaKBUILD_CFLAGS += -Wdisabled-optimization 16764a91907SMasahiro YamadaKBUILD_CFLAGS += -Wshadow 16864a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wlogical-op) 169*379b749aSThomas WeißschuhKBUILD_CFLAGS += -Wunused-macros 17064a91907SMasahiro Yamada 1716863f564SMasahiro YamadaKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 1726863f564SMasahiro Yamada 1732cd3271bSArnd Bergmannelse 1742cd3271bSArnd Bergmann 1752cd3271bSArnd Bergmann# The following turn off the warnings enabled by -Wextra 1762cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-missing-field-initializers 1772cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-shift-negative-value 1782cd3271bSArnd Bergmann 1798f6629c0SNathan Chancellorifdef CONFIG_CC_IS_CLANG 1808f6629c0SNathan ChancellorKBUILD_CFLAGS += -Wno-enum-enum-conversion 1818f6629c0SNathan Chancellorendif 1828f6629c0SNathan Chancellor 183c40845e3SArnd Bergmannifdef CONFIG_CC_IS_GCC 1842cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-maybe-uninitialized 1852cd3271bSArnd Bergmannendif 1862cd3271bSArnd Bergmann 18764a91907SMasahiro Yamadaendif 18864a91907SMasahiro Yamada 18964a91907SMasahiro Yamada# 19064a91907SMasahiro Yamada# W=3 - more obscure warnings, can most likely be ignored 19164a91907SMasahiro Yamada# 192e27128dbSMasahiro Yamadaifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) 19364a91907SMasahiro Yamada 19464a91907SMasahiro YamadaKBUILD_CFLAGS += -Wbad-function-cast 195095fbca0SArnd BergmannKBUILD_CFLAGS += -Wcast-align 19664a91907SMasahiro YamadaKBUILD_CFLAGS += -Wcast-qual 19764a91907SMasahiro YamadaKBUILD_CFLAGS += -Wconversion 19864a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpacked 19964a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpadded 20064a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpointer-arith 20164a91907SMasahiro YamadaKBUILD_CFLAGS += -Wredundant-decls 202a97ea93eSJoe PerchesKBUILD_CFLAGS += -Wsign-compare 20364a91907SMasahiro YamadaKBUILD_CFLAGS += -Wswitch-default 20464a91907SMasahiro Yamada 2056863f564SMasahiro YamadaKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 2066863f564SMasahiro Yamada 2072cd3271bSArnd Bergmannelse 2082cd3271bSArnd Bergmann 2092cd3271bSArnd Bergmann# The following turn off the warnings enabled by -Wextra 2102cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-sign-compare 211f5982cceSArnd BergmannKBUILD_CFLAGS += -Wno-unused-parameter 2122cd3271bSArnd Bergmann 213a86fe353SMasahiro Yamadaendif 214c77d06e7SYann Droneaud 215c77d06e7SYann Droneaud# 216e7a10929SThomas Weißschuh# W=e and CONFIG_WERROR - error out on warnings 217c77d06e7SYann Droneaud# 218e7a10929SThomas Weißschuhifneq ($(findstring e, $(KBUILD_EXTRA_WARN))$(CONFIG_WERROR),) 219c77d06e7SYann Droneaud 220f852ce05SThomas WeißschuhKBUILD_CPPFLAGS += -Werror 221ec4a3992SThomas WeißschuhKBUILD_AFLAGS += -Wa,--fatal-warnings 222ec4a3992SThomas WeißschuhKBUILD_LDFLAGS += --fatal-warnings 2233f0ff4ccSThomas WeißschuhKBUILD_USERCFLAGS += -Werror 2243f0ff4ccSThomas WeißschuhKBUILD_USERLDFLAGS += -Wl,--fatal-warnings 225592b571fSMiguel OjedaKBUILD_RUSTFLAGS += -Dwarnings 226c77d06e7SYann Droneaud 2277ded7d37SNathan Chancellor# While hostprog flags are used during build bootstrapping (thus should not 2287ded7d37SNathan Chancellor# depend on CONFIG_ symbols), -Werror is disruptive and should be opted into. 2297ded7d37SNathan Chancellor# Only apply -Werror to hostprogs built after the initial Kconfig stage. 23027758d8cSThomas WeißschuhKBUILD_HOSTCFLAGS += -Werror 23127758d8cSThomas WeißschuhKBUILD_HOSTLDFLAGS += -Wl,--fatal-warnings 23227758d8cSThomas WeißschuhKBUILD_HOSTRUSTFLAGS += -Dwarnings 2337ded7d37SNathan Chancellor 2347ded7d37SNathan Chancellorendif 235