1# SPDX-License-Identifier: GPL-2.0 2# ========================================================================== 3# make W=... settings 4# 5# There are four warning groups enabled by W=1, W=2, W=3, and W=e 6# They are independent, and can be combined like W=12 or W=123e. 7# ========================================================================== 8 9# Default set of warnings, always enabled 10KBUILD_CFLAGS += -Wall 11KBUILD_CFLAGS += -Wextra 12KBUILD_CFLAGS += -Wundef 13KBUILD_CFLAGS += -Werror=implicit-function-declaration 14KBUILD_CFLAGS += -Werror=implicit-int 15KBUILD_CFLAGS += -Werror=return-type 16KBUILD_CFLAGS += -Werror=strict-prototypes 17KBUILD_CFLAGS += -Wno-format-security 18KBUILD_CFLAGS += -Wno-trigraphs 19KBUILD_CFLAGS += $(call cc-disable-warning, frame-address) 20KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member) 21KBUILD_CFLAGS += -Wmissing-declarations 22KBUILD_CFLAGS += -Wmissing-prototypes 23 24ifneq ($(CONFIG_FRAME_WARN),0) 25KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN) 26endif 27 28KBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror 29KBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y) 30KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds 31 32ifdef CONFIG_CC_IS_CLANG 33# The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable. 34KBUILD_CFLAGS += -Wno-gnu 35 36# Clang checks for overflow/truncation with '%p', while GCC does not: 37# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219 38KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow-non-kprintf) 39KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation-non-kprintf) 40 41# Clang may emit a warning when a const variable, such as the dummy variables 42# in typecheck(), or const member of an aggregate type are not initialized, 43# which can result in unexpected behavior. However, in many audited cases of 44# the "field" variant of the warning, this is intentional because the field is 45# never used within a particular call path, the field is within a union with 46# other non-const members, or the containing object is not const so the field 47# can be modified via memcpy() / memset(). While the variable warning also gets 48# disabled with this same switch, there should not be too much coverage lost 49# because -Wuninitialized will still flag when an uninitialized const variable 50# is used. 51KBUILD_CFLAGS += $(call cc-disable-warning, default-const-init-unsafe) 52else 53 54# gcc inanely warns about local variables called 'main' 55KBUILD_CFLAGS += -Wno-main 56endif 57 58# These result in bogus false positives 59KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer) 60 61# Variable Length Arrays (VLAs) should not be used anywhere in the kernel 62KBUILD_CFLAGS += -Wvla 63 64# disable pointer signed / unsigned warnings in gcc 4.0 65KBUILD_CFLAGS += -Wno-pointer-sign 66 67# In order to make sure new function cast mismatches are not introduced 68# in the kernel (to avoid tripping CFI checking), the kernel should be 69# globally built with -Wcast-function-type. 70KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type) 71 72# Currently, disable -Wstringop-overflow for GCC 11, globally. 73KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-disable-warning, stringop-overflow) 74KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow) 75 76# Currently, disable -Wunterminated-string-initialization as broken 77KBUILD_CFLAGS += $(call cc-disable-warning, unterminated-string-initialization) 78 79# The allocators already balk at large sizes, so silence the compiler 80# warnings for bounds checks involving those possible values. While 81# -Wno-alloc-size-larger-than would normally be used here, earlier versions 82# of gcc (<9.1) weirdly don't handle the option correctly when _other_ 83# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX 84# doesn't work (as it is documented to), silently resolving to "0" prior to 85# version 9.1 (and producing an error more recently). Numeric values larger 86# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently 87# ignored, continuing to default to PTRDIFF_MAX. So, left with no other 88# choice, we must perform a versioned check to disable this warning. 89# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au 90KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than 91KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) 92 93# Prohibit date/time macros, which would make the build non-deterministic 94KBUILD_CFLAGS += -Werror=date-time 95 96# enforce correct pointer usage 97KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types) 98 99# Require designated initializers for all marked structures 100KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) 101 102# Warn if there is an enum types mismatch 103KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion) 104 105KBUILD_CFLAGS += -Wunused 106 107# 108# W=1 - warnings which may be relevant and do not occur too often 109# 110ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) 111 112KBUILD_CFLAGS += -Wmissing-format-attribute 113KBUILD_CFLAGS += -Wmissing-include-dirs 114KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) 115 116KBUILD_CPPFLAGS += -Wundef 117KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 118 119else 120 121# Some diagnostics enabled by default are noisy. 122# Suppress them by using -Wno... except for W=1. 123KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) 124KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) 125KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned) 126KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow) 127ifdef CONFIG_CC_IS_GCC 128KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation) 129endif 130KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation) 131 132KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang 133 134ifdef CONFIG_CC_IS_CLANG 135# Clang before clang-16 would warn on default argument promotions. 136ifneq ($(call clang-min-version, 160000),y) 137# Disable -Wformat 138KBUILD_CFLAGS += -Wno-format 139# Then re-enable flags that were part of the -Wformat group that aren't 140# problematic. 141KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier 142KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull 143# Requires clang-12+. 144ifeq ($(call clang-min-version, 120000),y) 145KBUILD_CFLAGS += -Wformat-insufficient-args 146endif 147endif 148KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast) 149KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare 150KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access) 151KBUILD_CFLAGS += -Wno-enum-compare-conditional 152endif 153 154endif 155 156# 157# W=2 - warnings which occur quite often but may still be relevant 158# 159ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) 160 161KBUILD_CFLAGS += -Wdisabled-optimization 162KBUILD_CFLAGS += -Wshadow 163KBUILD_CFLAGS += $(call cc-option, -Wlogical-op) 164KBUILD_CFLAGS += $(call cc-option, -Wunused-macros) 165 166KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 167 168else 169 170# The following turn off the warnings enabled by -Wextra 171KBUILD_CFLAGS += -Wno-missing-field-initializers 172KBUILD_CFLAGS += -Wno-type-limits 173KBUILD_CFLAGS += -Wno-shift-negative-value 174 175ifdef CONFIG_CC_IS_CLANG 176KBUILD_CFLAGS += -Wno-enum-enum-conversion 177endif 178 179ifdef CONFIG_CC_IS_GCC 180KBUILD_CFLAGS += -Wno-maybe-uninitialized 181endif 182 183endif 184 185# 186# W=3 - more obscure warnings, can most likely be ignored 187# 188ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) 189 190KBUILD_CFLAGS += -Wbad-function-cast 191KBUILD_CFLAGS += -Wcast-align 192KBUILD_CFLAGS += -Wcast-qual 193KBUILD_CFLAGS += -Wconversion 194KBUILD_CFLAGS += -Wpacked 195KBUILD_CFLAGS += -Wpadded 196KBUILD_CFLAGS += -Wpointer-arith 197KBUILD_CFLAGS += -Wredundant-decls 198KBUILD_CFLAGS += -Wsign-compare 199KBUILD_CFLAGS += -Wswitch-default 200 201KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 202 203else 204 205# The following turn off the warnings enabled by -Wextra 206KBUILD_CFLAGS += -Wno-sign-compare 207KBUILD_CFLAGS += -Wno-unused-parameter 208 209endif 210 211# 212# W=e - error out on warnings 213# 214ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),) 215 216KBUILD_CFLAGS += -Werror 217 218endif 219