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# Another good warning that we'll want to enable eventually 58KBUILD_CFLAGS += $(call cc-disable-warning, restrict) 59 60# The allocators already balk at large sizes, so silence the compiler 61# warnings for bounds checks involving those possible values. While 62# -Wno-alloc-size-larger-than would normally be used here, earlier versions 63# of gcc (<9.1) weirdly don't handle the option correctly when _other_ 64# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX 65# doesn't work (as it is documented to), silently resolving to "0" prior to 66# version 9.1 (and producing an error more recently). Numeric values larger 67# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently 68# ignored, continuing to default to PTRDIFF_MAX. So, left with no other 69# choice, we must perform a versioned check to disable this warning. 70# https://lore.kernel.org/lkml/[email protected] 71KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than 72KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) 73 74# Prohibit date/time macros, which would make the build non-deterministic 75KBUILD_CFLAGS += -Werror=date-time 76 77# enforce correct pointer usage 78KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types) 79 80# Require designated initializers for all marked structures 81KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) 82 83# Warn if there is an enum types mismatch 84KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion) 85 86# backward compatibility 87KBUILD_EXTRA_WARN ?= $(KBUILD_ENABLE_EXTRA_GCC_CHECKS) 88 89ifeq ("$(origin W)", "command line") 90 KBUILD_EXTRA_WARN := $(W) 91endif 92 93export KBUILD_EXTRA_WARN 94 95# 96# W=1 - warnings which may be relevant and do not occur too often 97# 98ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) 99 100KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter 101KBUILD_CFLAGS += -Wmissing-declarations 102KBUILD_CFLAGS += -Wmissing-format-attribute 103KBUILD_CFLAGS += -Wmissing-prototypes 104KBUILD_CFLAGS += -Wold-style-definition 105KBUILD_CFLAGS += -Wmissing-include-dirs 106KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable) 107KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) 108KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned) 109KBUILD_CFLAGS += $(call cc-option, -Wformat-overflow) 110KBUILD_CFLAGS += $(call cc-option, -Wformat-truncation) 111KBUILD_CFLAGS += $(call cc-option, -Wstringop-overflow) 112KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation) 113 114KBUILD_CPPFLAGS += -Wundef 115KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 116 117else 118 119# Some diagnostics enabled by default are noisy. 120# Suppress them by using -Wno... except for W=1. 121KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) 122KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) 123KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned) 124KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow) 125KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation) 126KBUILD_CFLAGS += $(call cc-disable-warning, stringop-overflow) 127KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation) 128 129ifdef CONFIG_CC_IS_CLANG 130# Clang before clang-16 would warn on default argument promotions. 131ifneq ($(call clang-min-version, 160000),y) 132# Disable -Wformat 133KBUILD_CFLAGS += -Wno-format 134# Then re-enable flags that were part of the -Wformat group that aren't 135# problematic. 136KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier 137KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull 138# Requires clang-12+. 139ifeq ($(call clang-min-version, 120000),y) 140KBUILD_CFLAGS += -Wformat-insufficient-args 141endif 142endif 143KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast) 144KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare 145KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access) 146KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict) 147endif 148 149endif 150 151# 152# W=2 - warnings which occur quite often but may still be relevant 153# 154ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) 155 156KBUILD_CFLAGS += -Wdisabled-optimization 157KBUILD_CFLAGS += -Wshadow 158KBUILD_CFLAGS += $(call cc-option, -Wlogical-op) 159KBUILD_CFLAGS += -Wmissing-field-initializers 160KBUILD_CFLAGS += -Wtype-limits 161KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized) 162KBUILD_CFLAGS += $(call cc-option, -Wunused-macros) 163 164ifdef CONFIG_CC_IS_CLANG 165KBUILD_CFLAGS += -Winitializer-overrides 166endif 167 168KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 169 170else 171 172# The following turn off the warnings enabled by -Wextra 173KBUILD_CFLAGS += -Wno-missing-field-initializers 174KBUILD_CFLAGS += -Wno-type-limits 175KBUILD_CFLAGS += -Wno-shift-negative-value 176 177ifdef CONFIG_CC_IS_CLANG 178KBUILD_CFLAGS += -Wno-initializer-overrides 179else 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 200KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat) 201 202KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 203 204else 205 206# The following turn off the warnings enabled by -Wextra 207KBUILD_CFLAGS += -Wno-sign-compare 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