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 11e88ca243SArnd BergmannKBUILD_CFLAGS += -Wundef 12e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=implicit-function-declaration 13e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=implicit-int 14e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=return-type 15e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=strict-prototypes 16e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-format-security 17e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-trigraphs 18e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning,frame-address,) 19e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member) 20e88ca243SArnd Bergmann 21e88ca243SArnd Bergmannifneq ($(CONFIG_FRAME_WARN),0) 22e88ca243SArnd BergmannKBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN) 23e88ca243SArnd Bergmannendif 24e88ca243SArnd Bergmann 25e88ca243SArnd BergmannKBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror 26e88ca243SArnd BergmannKBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y) 27e88ca243SArnd BergmannKBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds 28e88ca243SArnd Bergmann 29e88ca243SArnd Bergmannifdef CONFIG_CC_IS_CLANG 30e88ca243SArnd Bergmann# The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable. 31e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-gnu 32e88ca243SArnd Bergmannelse 33e88ca243SArnd Bergmann 34e88ca243SArnd Bergmann# gcc inanely warns about local variables called 'main' 35e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-main 36e88ca243SArnd Bergmannendif 37e88ca243SArnd Bergmann 38e88ca243SArnd Bergmann# These warnings generated too much noise in a regular build. 39e88ca243SArnd Bergmann# Use make W=1 to enable them (see scripts/Makefile.extrawarn) 40e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) 41e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) 42e88ca243SArnd Bergmann 43e88ca243SArnd Bergmann# These result in bogus false positives 44e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer) 45e88ca243SArnd Bergmann 46e88ca243SArnd Bergmann# Variable Length Arrays (VLAs) should not be used anywhere in the kernel 47e88ca243SArnd BergmannKBUILD_CFLAGS += -Wvla 48e88ca243SArnd Bergmann 49e88ca243SArnd Bergmann# disable pointer signed / unsigned warnings in gcc 4.0 50e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-pointer-sign 51e88ca243SArnd Bergmann 52e88ca243SArnd Bergmann# In order to make sure new function cast mismatches are not introduced 53e88ca243SArnd Bergmann# in the kernel (to avoid tripping CFI checking), the kernel should be 54e88ca243SArnd Bergmann# globally built with -Wcast-function-type. 55e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wcast-function-type) 56e88ca243SArnd Bergmann 57e88ca243SArnd Bergmann# Another good warning that we'll want to enable eventually 58e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, restrict) 59e88ca243SArnd Bergmann 60e88ca243SArnd Bergmann# The allocators already balk at large sizes, so silence the compiler 61e88ca243SArnd Bergmann# warnings for bounds checks involving those possible values. While 62e88ca243SArnd Bergmann# -Wno-alloc-size-larger-than would normally be used here, earlier versions 63e88ca243SArnd Bergmann# of gcc (<9.1) weirdly don't handle the option correctly when _other_ 64e88ca243SArnd Bergmann# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX 65e88ca243SArnd Bergmann# doesn't work (as it is documented to), silently resolving to "0" prior to 66e88ca243SArnd Bergmann# version 9.1 (and producing an error more recently). Numeric values larger 67e88ca243SArnd Bergmann# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently 68e88ca243SArnd Bergmann# ignored, continuing to default to PTRDIFF_MAX. So, left with no other 69e88ca243SArnd Bergmann# choice, we must perform a versioned check to disable this warning. 70e88ca243SArnd Bergmann# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au 71e88ca243SArnd BergmannKBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than 72e88ca243SArnd BergmannKBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) 73e88ca243SArnd Bergmann 74e88ca243SArnd Bergmann# Prohibit date/time macros, which would make the build non-deterministic 75e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=date-time 76e88ca243SArnd Bergmann 77e88ca243SArnd Bergmann# enforce correct pointer usage 78e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types) 79e88ca243SArnd Bergmann 80e88ca243SArnd Bergmann# Require designated initializers for all marked structures 81e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) 82e88ca243SArnd Bergmann 83e88ca243SArnd Bergmann# Warn if there is an enum types mismatch 84e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Wenum-conversion) 85e88ca243SArnd Bergmann 86e27128dbSMasahiro Yamada# backward compatibility 87e27128dbSMasahiro YamadaKBUILD_EXTRA_WARN ?= $(KBUILD_ENABLE_EXTRA_GCC_CHECKS) 88e27128dbSMasahiro Yamada 89a86fe353SMasahiro Yamadaifeq ("$(origin W)", "command line") 90e27128dbSMasahiro Yamada KBUILD_EXTRA_WARN := $(W) 91a86fe353SMasahiro Yamadaendif 92a86fe353SMasahiro Yamada 93e27128dbSMasahiro Yamadaexport KBUILD_EXTRA_WARN 94e27128dbSMasahiro Yamada 9564a91907SMasahiro Yamada# 9664a91907SMasahiro Yamada# W=1 - warnings which may be relevant and do not occur too often 9764a91907SMasahiro Yamada# 98e27128dbSMasahiro Yamadaifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) 99a86fe353SMasahiro Yamada 10064a91907SMasahiro YamadaKBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter 10164a91907SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-declarations 10264a91907SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-format-attribute 10364a91907SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-prototypes 10464a91907SMasahiro YamadaKBUILD_CFLAGS += -Wold-style-definition 10564a91907SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-include-dirs 10664a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable) 10764a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) 10864a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned) 109*6d4ab2e9SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wformat-overflow) 110*6d4ab2e9SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wformat-truncation) 111*6d4ab2e9SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wstringop-overflow) 11264a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation) 113a86fe353SMasahiro Yamada 11480b6093bSMasahiro YamadaKBUILD_CPPFLAGS += -Wundef 1156863f564SMasahiro YamadaKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 1166863f564SMasahiro Yamada 11726ea6bb1SBehan Websterelse 11826ea6bb1SBehan Webster 11964a91907SMasahiro Yamada# Some diagnostics enabled by default are noisy. 12064a91907SMasahiro Yamada# Suppress them by using -Wno... except for W=1. 1212cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) 1222cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) 1232cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned) 124*6d4ab2e9SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, format-overflow) 125*6d4ab2e9SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, format-truncation) 126*6d4ab2e9SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, stringop-overflow) 1272cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation) 12864a91907SMasahiro Yamada 129076f421dSMasahiro Yamadaifdef CONFIG_CC_IS_CLANG 130b0839b28SNick Desaulniers# Clang before clang-16 would warn on default argument promotions. 13188b61e3bSNick Desaulniersifneq ($(call clang-min-version, 160000),y) 132b0839b28SNick Desaulniers# Disable -Wformat 13321f9c8a1SLinus TorvaldsKBUILD_CFLAGS += -Wno-format 134b0839b28SNick Desaulniers# Then re-enable flags that were part of the -Wformat group that aren't 135b0839b28SNick Desaulniers# problematic. 136b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier 137b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-zero-length -Wnonnull 138b0839b28SNick Desaulniers# Requires clang-12+. 13988b61e3bSNick Desaulniersifeq ($(call clang-min-version, 120000),y) 140b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-insufficient-args 141b0839b28SNick Desaulniersendif 142b0839b28SNick Desaulniersendif 14382f2bc2fSNathan ChancellorKBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast) 144afe956c5SNathan ChancellorKBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare 1451cf5f151SNathan ChancellorKBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access) 14621206351SSami TolvanenKBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict) 14726ea6bb1SBehan Websterendif 14864a91907SMasahiro Yamada 14964a91907SMasahiro Yamadaendif 15064a91907SMasahiro Yamada 15164a91907SMasahiro Yamada# 15264a91907SMasahiro Yamada# W=2 - warnings which occur quite often but may still be relevant 15364a91907SMasahiro Yamada# 154e27128dbSMasahiro Yamadaifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) 15564a91907SMasahiro Yamada 15664a91907SMasahiro YamadaKBUILD_CFLAGS += -Wdisabled-optimization 15764a91907SMasahiro YamadaKBUILD_CFLAGS += -Wshadow 15864a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wlogical-op) 15964a91907SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-field-initializers 160355a3587SRikard FalkebornKBUILD_CFLAGS += -Wtype-limits 16164a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized) 16264a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wunused-macros) 16364a91907SMasahiro Yamada 1642cd3271bSArnd Bergmannifdef CONFIG_CC_IS_CLANG 1652cd3271bSArnd BergmannKBUILD_CFLAGS += -Winitializer-overrides 1662cd3271bSArnd Bergmannendif 1672cd3271bSArnd Bergmann 1686863f564SMasahiro YamadaKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 1696863f564SMasahiro Yamada 1702cd3271bSArnd Bergmannelse 1712cd3271bSArnd Bergmann 1722cd3271bSArnd Bergmann# The following turn off the warnings enabled by -Wextra 1732cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-missing-field-initializers 1742cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-type-limits 1752cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-shift-negative-value 1762cd3271bSArnd Bergmann 1772cd3271bSArnd Bergmannifdef CONFIG_CC_IS_CLANG 1782cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-initializer-overrides 1792cd3271bSArnd Bergmannelse 1802cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-maybe-uninitialized 1812cd3271bSArnd Bergmannendif 1822cd3271bSArnd Bergmann 18364a91907SMasahiro Yamadaendif 18464a91907SMasahiro Yamada 18564a91907SMasahiro Yamada# 18664a91907SMasahiro Yamada# W=3 - more obscure warnings, can most likely be ignored 18764a91907SMasahiro Yamada# 188e27128dbSMasahiro Yamadaifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) 18964a91907SMasahiro Yamada 19064a91907SMasahiro YamadaKBUILD_CFLAGS += -Wbad-function-cast 191095fbca0SArnd BergmannKBUILD_CFLAGS += -Wcast-align 19264a91907SMasahiro YamadaKBUILD_CFLAGS += -Wcast-qual 19364a91907SMasahiro YamadaKBUILD_CFLAGS += -Wconversion 19464a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpacked 19564a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpadded 19664a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpointer-arith 19764a91907SMasahiro YamadaKBUILD_CFLAGS += -Wredundant-decls 198a97ea93eSJoe PerchesKBUILD_CFLAGS += -Wsign-compare 19964a91907SMasahiro YamadaKBUILD_CFLAGS += -Wswitch-default 20064a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat) 20164a91907SMasahiro Yamada 2026863f564SMasahiro YamadaKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 2036863f564SMasahiro Yamada 2042cd3271bSArnd Bergmannelse 2052cd3271bSArnd Bergmann 2062cd3271bSArnd Bergmann# The following turn off the warnings enabled by -Wextra 2072cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-sign-compare 2082cd3271bSArnd Bergmann 209a86fe353SMasahiro Yamadaendif 210c77d06e7SYann Droneaud 211c77d06e7SYann Droneaud# 212c77d06e7SYann Droneaud# W=e - error out on warnings 213c77d06e7SYann Droneaud# 214c77d06e7SYann Droneaudifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),) 215c77d06e7SYann Droneaud 216c77d06e7SYann DroneaudKBUILD_CFLAGS += -Werror 217c77d06e7SYann Droneaud 218c77d06e7SYann Droneaudendif 219