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