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