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 19a79be02bSLinus TorvaldsKBUILD_CFLAGS += $(call cc-option, -Wno-frame-address) 20e88ca243SArnd BergmannKBUILD_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_CPPFLAGS-$(CONFIG_WERROR) += -Werror 29e88ca243SArnd BergmannKBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y) 30e88ca243SArnd BergmannKBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds 31e88ca243SArnd Bergmann 32e88ca243SArnd Bergmannifdef CONFIG_CC_IS_CLANG 33e88ca243SArnd Bergmann# The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable. 34e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-gnu 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 38738fc998SNathan ChancellorKBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow-non-kprintf) 39738fc998SNathan ChancellorKBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation-non-kprintf) 40*d0afcfebSNathan Chancellor 41*d0afcfebSNathan Chancellor# Clang may emit a warning when a const variable, such as the dummy variables 42*d0afcfebSNathan Chancellor# in typecheck(), or const member of an aggregate type are not initialized, 43*d0afcfebSNathan Chancellor# which can result in unexpected behavior. However, in many audited cases of 44*d0afcfebSNathan Chancellor# the "field" variant of the warning, this is intentional because the field is 45*d0afcfebSNathan Chancellor# never used within a particular call path, the field is within a union with 46*d0afcfebSNathan Chancellor# other non-const members, or the containing object is not const so the field 47*d0afcfebSNathan Chancellor# can be modified via memcpy() / memset(). While the variable warning also gets 48*d0afcfebSNathan Chancellor# disabled with this same switch, there should not be too much coverage lost 49*d0afcfebSNathan Chancellor# because -Wuninitialized will still flag when an uninitialized const variable 50*d0afcfebSNathan Chancellor# is used. 51*d0afcfebSNathan ChancellorKBUILD_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 58e88ca243SArnd Bergmann# These result in bogus false positives 59e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-dangling-pointer) 60e88ca243SArnd Bergmann 61e88ca243SArnd Bergmann# Stack Variable Length Arrays (VLAs) must not be used in the kernel. 62e88ca243SArnd Bergmann# Function array parameters should, however, be usable, but -Wvla will 63e88ca243SArnd Bergmann# warn for those. Clang has no way yet to distinguish between the VLA 64e88ca243SArnd Bergmann# types, so depend on GCC for now to keep stack VLAs out of the tree. 65e88ca243SArnd Bergmann# https://github.com/llvm/llvm-project/issues/57098 66e88ca243SArnd Bergmann# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217 67e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=1) 68e88ca243SArnd Bergmann 69e88ca243SArnd Bergmann# disable pointer signed / unsigned warnings in gcc 4.0 70e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-pointer-sign 71e88ca243SArnd Bergmann 724f79eaa2SNathan Chancellor# In order to make sure new function cast mismatches are not introduced 734f79eaa2SNathan Chancellor# in the kernel (to avoid tripping CFI checking), the kernel should be 744f79eaa2SNathan Chancellor# globally built with -Wcast-function-type. 754f79eaa2SNathan ChancellorKBUILD_CFLAGS += $(call cc-option, -Wcast-function-type) 764f79eaa2SNathan Chancellor 774f79eaa2SNathan Chancellor# Currently, disable -Wstringop-overflow for GCC 11, globally. 784f79eaa2SNathan ChancellorKBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-option, -Wno-stringop-overflow) 79e88ca243SArnd BergmannKBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow) 80e88ca243SArnd Bergmann 81e88ca243SArnd Bergmann# Currently, disable -Wunterminated-string-initialization as broken 82e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-unterminated-string-initialization) 83e88ca243SArnd Bergmann 84e88ca243SArnd Bergmann# The allocators already balk at large sizes, so silence the compiler 85e88ca243SArnd Bergmann# warnings for bounds checks involving those possible values. While 86e88ca243SArnd Bergmann# -Wno-alloc-size-larger-than would normally be used here, earlier versions 87e88ca243SArnd Bergmann# of gcc (<9.1) weirdly don't handle the option correctly when _other_ 88e88ca243SArnd Bergmann# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX 89e88ca243SArnd Bergmann# doesn't work (as it is documented to), silently resolving to "0" prior to 90e88ca243SArnd Bergmann# version 9.1 (and producing an error more recently). Numeric values larger 91e88ca243SArnd Bergmann# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently 92e88ca243SArnd Bergmann# ignored, continuing to default to PTRDIFF_MAX. So, left with no other 93e88ca243SArnd Bergmann# choice, we must perform a versioned check to disable this warning. 94e88ca243SArnd Bergmann# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au 95e88ca243SArnd BergmannKBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than 96e88ca243SArnd BergmannKBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) 97e88ca243SArnd Bergmann 98e88ca243SArnd Bergmann# Prohibit date/time macros, which would make the build non-deterministic 99e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=date-time 100e88ca243SArnd Bergmann 101e88ca243SArnd Bergmann# enforce correct pointer usage 102e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types) 103e88ca243SArnd Bergmann 104e88ca243SArnd Bergmann# Require designated initializers for all marked structures 105f5982cceSArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) 106f5982cceSArnd Bergmann 10764a91907SMasahiro Yamada# Warn if there is an enum types mismatch 10864a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option,-Wenum-conversion) 10964a91907SMasahiro Yamada 110e27128dbSMasahiro YamadaKBUILD_CFLAGS += -Wunused 111a86fe353SMasahiro Yamada 11264a91907SMasahiro Yamada# 11364a91907SMasahiro Yamada# W=1 - warnings which may be relevant and do not occur too often 11464a91907SMasahiro Yamada# 115a86fe353SMasahiro Yamadaifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) 11680b6093bSMasahiro Yamada 1176863f564SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-format-attribute 1186863f564SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-include-dirs 11926ea6bb1SBehan WebsterKBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) 12026ea6bb1SBehan Webster 12164a91907SMasahiro YamadaKBUILD_CPPFLAGS += -Wundef 12264a91907SMasahiro YamadaKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 1232cd3271bSArnd Bergmann 1242cd3271bSArnd Bergmannelse 1252cd3271bSArnd Bergmann 1266d4ab2e9SArnd Bergmann# Some diagnostics enabled by default are noisy. 127908dd508SArnd Bergmann# Suppress them by using -Wno... except for W=1. 1286d4ab2e9SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-unused-but-set-variable) 129908dd508SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-unused-const-variable) 1302cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-packed-not-aligned) 13164a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow) 132c40845e3SArnd Bergmannifdef CONFIG_CC_IS_GCC 133c40845e3SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation) 134076f421dSMasahiro Yamadaendif 135b0839b28SNick DesaulniersKBUILD_CFLAGS += $(call cc-option, -Wno-stringop-truncation) 13688b61e3bSNick Desaulniers 137b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang 13821f9c8a1SLinus Torvalds 139b0839b28SNick Desaulniersifdef CONFIG_CC_IS_CLANG 140b0839b28SNick Desaulniers# Clang before clang-16 would warn on default argument promotions. 141b0839b28SNick Desaulniersifneq ($(call clang-min-version, 160000),y) 142b0839b28SNick Desaulniers# Disable -Wformat 143b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wno-format 14488b61e3bSNick Desaulniers# Then re-enable flags that were part of the -Wformat group that aren't 145b0839b28SNick Desaulniers# problematic. 146b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier 147b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-zero-length -Wnonnull 14882f2bc2fSNathan Chancellor# Requires clang-12+. 149afe956c5SNathan Chancellorifeq ($(call clang-min-version, 120000),y) 1501cf5f151SNathan ChancellorKBUILD_CFLAGS += -Wformat-insufficient-args 15175b5ab13SNathan Chancellorendif 15226ea6bb1SBehan Websterendif 15364a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wno-pointer-to-enum-cast) 15464a91907SMasahiro YamadaKBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare 15564a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wno-unaligned-access) 15664a91907SMasahiro YamadaKBUILD_CFLAGS += -Wno-enum-compare-conditional 15764a91907SMasahiro Yamadaendif 15864a91907SMasahiro Yamada 159e27128dbSMasahiro Yamadaendif 16064a91907SMasahiro Yamada 16164a91907SMasahiro Yamada# 16264a91907SMasahiro Yamada# W=2 - warnings which occur quite often but may still be relevant 16364a91907SMasahiro Yamada# 16464a91907SMasahiro Yamadaifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) 16564a91907SMasahiro Yamada 1666863f564SMasahiro YamadaKBUILD_CFLAGS += -Wdisabled-optimization 1676863f564SMasahiro YamadaKBUILD_CFLAGS += -Wshadow 1682cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wlogical-op) 1692cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wunused-macros) 1702cd3271bSArnd Bergmann 1712cd3271bSArnd BergmannKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 1722cd3271bSArnd Bergmann 1732cd3271bSArnd Bergmannelse 1742cd3271bSArnd Bergmann 1758f6629c0SNathan Chancellor# The following turn off the warnings enabled by -Wextra 1768f6629c0SNathan ChancellorKBUILD_CFLAGS += -Wno-missing-field-initializers 1778f6629c0SNathan ChancellorKBUILD_CFLAGS += -Wno-type-limits 1788f6629c0SNathan ChancellorKBUILD_CFLAGS += -Wno-shift-negative-value 179c40845e3SArnd Bergmann 1802cd3271bSArnd Bergmannifdef CONFIG_CC_IS_CLANG 1812cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-enum-enum-conversion 1822cd3271bSArnd Bergmannendif 18364a91907SMasahiro Yamada 18464a91907SMasahiro Yamadaifdef CONFIG_CC_IS_GCC 18564a91907SMasahiro YamadaKBUILD_CFLAGS += -Wno-maybe-uninitialized 18664a91907SMasahiro Yamadaendif 18764a91907SMasahiro Yamada 188e27128dbSMasahiro Yamadaendif 18964a91907SMasahiro Yamada 19064a91907SMasahiro Yamada# 191095fbca0SArnd Bergmann# W=3 - more obscure warnings, can most likely be ignored 19264a91907SMasahiro Yamada# 19364a91907SMasahiro Yamadaifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) 19464a91907SMasahiro Yamada 19564a91907SMasahiro YamadaKBUILD_CFLAGS += -Wbad-function-cast 19664a91907SMasahiro YamadaKBUILD_CFLAGS += -Wcast-align 19764a91907SMasahiro YamadaKBUILD_CFLAGS += -Wcast-qual 198a97ea93eSJoe PerchesKBUILD_CFLAGS += -Wconversion 19964a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpacked 20064a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpadded 2016863f564SMasahiro YamadaKBUILD_CFLAGS += -Wpointer-arith 2026863f564SMasahiro YamadaKBUILD_CFLAGS += -Wredundant-decls 2032cd3271bSArnd BergmannKBUILD_CFLAGS += -Wsign-compare 2042cd3271bSArnd BergmannKBUILD_CFLAGS += -Wswitch-default 2052cd3271bSArnd Bergmann 2062cd3271bSArnd BergmannKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 207f5982cceSArnd Bergmann 2082cd3271bSArnd Bergmannelse 209a86fe353SMasahiro Yamada 210c77d06e7SYann Droneaud# The following turn off the warnings enabled by -Wextra 211c77d06e7SYann DroneaudKBUILD_CFLAGS += -Wno-sign-compare 212c77d06e7SYann DroneaudKBUILD_CFLAGS += -Wno-unused-parameter 213c77d06e7SYann Droneaud 214c77d06e7SYann Droneaudendif 215c77d06e7SYann Droneaud 216c77d06e7SYann Droneaud# 217c77d06e7SYann Droneaud# W=e - error out on warnings 218c77d06e7SYann Droneaud# 219ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),) 220 221KBUILD_CFLAGS += -Werror 222 223endif 224