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) 200fcb7085SArnd BergmannKBUILD_CFLAGS += -Wmissing-declarations 210fcb7085SArnd BergmannKBUILD_CFLAGS += -Wmissing-prototypes 22e88ca243SArnd Bergmann 23e88ca243SArnd Bergmannifneq ($(CONFIG_FRAME_WARN),0) 24e88ca243SArnd BergmannKBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN) 25e88ca243SArnd Bergmannendif 26e88ca243SArnd Bergmann 27e88ca243SArnd BergmannKBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror 28e88ca243SArnd BergmannKBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y) 29e88ca243SArnd BergmannKBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds 30e88ca243SArnd Bergmann 31e88ca243SArnd Bergmannifdef CONFIG_CC_IS_CLANG 32e88ca243SArnd Bergmann# The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable. 33e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-gnu 34e88ca243SArnd Bergmannelse 35e88ca243SArnd Bergmann 36e88ca243SArnd Bergmann# gcc inanely warns about local variables called 'main' 37e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-main 38e88ca243SArnd Bergmannendif 39e88ca243SArnd Bergmann 40e88ca243SArnd Bergmann# These result in bogus false positives 41e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer) 42e88ca243SArnd Bergmann 43e88ca243SArnd Bergmann# Variable Length Arrays (VLAs) should not be used anywhere in the kernel 44e88ca243SArnd BergmannKBUILD_CFLAGS += -Wvla 45e88ca243SArnd Bergmann 46e88ca243SArnd Bergmann# disable pointer signed / unsigned warnings in gcc 4.0 47e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-pointer-sign 48e88ca243SArnd Bergmann 49e88ca243SArnd Bergmann# In order to make sure new function cast mismatches are not introduced 50e88ca243SArnd Bergmann# in the kernel (to avoid tripping CFI checking), the kernel should be 51e88ca243SArnd Bergmann# globally built with -Wcast-function-type. 52e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wcast-function-type) 53e88ca243SArnd Bergmann 54e88ca243SArnd Bergmann# The allocators already balk at large sizes, so silence the compiler 55e88ca243SArnd Bergmann# warnings for bounds checks involving those possible values. While 56e88ca243SArnd Bergmann# -Wno-alloc-size-larger-than would normally be used here, earlier versions 57e88ca243SArnd Bergmann# of gcc (<9.1) weirdly don't handle the option correctly when _other_ 58e88ca243SArnd Bergmann# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX 59e88ca243SArnd Bergmann# doesn't work (as it is documented to), silently resolving to "0" prior to 60e88ca243SArnd Bergmann# version 9.1 (and producing an error more recently). Numeric values larger 61e88ca243SArnd Bergmann# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently 62e88ca243SArnd Bergmann# ignored, continuing to default to PTRDIFF_MAX. So, left with no other 63e88ca243SArnd Bergmann# choice, we must perform a versioned check to disable this warning. 64e88ca243SArnd Bergmann# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au 65e88ca243SArnd BergmannKBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than 66e88ca243SArnd BergmannKBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) 67e88ca243SArnd Bergmann 68e88ca243SArnd Bergmann# Prohibit date/time macros, which would make the build non-deterministic 69e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=date-time 70e88ca243SArnd Bergmann 71e88ca243SArnd Bergmann# enforce correct pointer usage 72e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types) 73e88ca243SArnd Bergmann 74e88ca243SArnd Bergmann# Require designated initializers for all marked structures 75e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) 76e88ca243SArnd Bergmann 77e88ca243SArnd Bergmann# Warn if there is an enum types mismatch 78e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Wenum-conversion) 79e88ca243SArnd Bergmann 80*dce4aab8SKees Cook# Explicitly clear padding bits during variable initialization 81*dce4aab8SKees CookKBUILD_CFLAGS += $(call cc-option,-fzero-init-padding-bits=all) 82*dce4aab8SKees Cook 83f5982cceSArnd BergmannKBUILD_CFLAGS += -Wextra 84f5982cceSArnd BergmannKBUILD_CFLAGS += -Wunused 85f5982cceSArnd Bergmann 8664a91907SMasahiro Yamada# 8764a91907SMasahiro Yamada# W=1 - warnings which may be relevant and do not occur too often 8864a91907SMasahiro Yamada# 89e27128dbSMasahiro Yamadaifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) 90a86fe353SMasahiro Yamada 9164a91907SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-format-attribute 9264a91907SMasahiro YamadaKBUILD_CFLAGS += -Wmissing-include-dirs 9364a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) 94a86fe353SMasahiro Yamada 9580b6093bSMasahiro YamadaKBUILD_CPPFLAGS += -Wundef 966863f564SMasahiro YamadaKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 976863f564SMasahiro Yamada 9826ea6bb1SBehan Websterelse 9926ea6bb1SBehan Webster 10064a91907SMasahiro Yamada# Some diagnostics enabled by default are noisy. 10164a91907SMasahiro Yamada# Suppress them by using -Wno... except for W=1. 1022cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) 1032cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) 1042cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned) 1056d4ab2e9SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, format-overflow) 106908dd508SArnd Bergmannifdef CONFIG_CC_IS_GCC 1076d4ab2e9SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, format-truncation) 108908dd508SArnd Bergmannelse 109908dd508SArnd Bergmann# Clang checks for overflow/truncation with '%p', while GCC does not: 110908dd508SArnd Bergmann# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219 111908dd508SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, format-overflow-non-kprintf) 112908dd508SArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, format-truncation-non-kprintf) 113908dd508SArnd Bergmannendif 1142cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation) 11564a91907SMasahiro Yamada 116c40845e3SArnd BergmannKBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang 117c40845e3SArnd Bergmann 118076f421dSMasahiro Yamadaifdef CONFIG_CC_IS_CLANG 119b0839b28SNick Desaulniers# Clang before clang-16 would warn on default argument promotions. 12088b61e3bSNick Desaulniersifneq ($(call clang-min-version, 160000),y) 121b0839b28SNick Desaulniers# Disable -Wformat 12221f9c8a1SLinus TorvaldsKBUILD_CFLAGS += -Wno-format 123b0839b28SNick Desaulniers# Then re-enable flags that were part of the -Wformat group that aren't 124b0839b28SNick Desaulniers# problematic. 125b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier 126b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-zero-length -Wnonnull 127b0839b28SNick Desaulniers# Requires clang-12+. 12888b61e3bSNick Desaulniersifeq ($(call clang-min-version, 120000),y) 129b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wformat-insufficient-args 130b0839b28SNick Desaulniersendif 131b0839b28SNick Desaulniersendif 13282f2bc2fSNathan ChancellorKBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast) 133afe956c5SNathan ChancellorKBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare 1341cf5f151SNathan ChancellorKBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access) 13575b5ab13SNathan ChancellorKBUILD_CFLAGS += -Wno-enum-compare-conditional 13675b5ab13SNathan ChancellorKBUILD_CFLAGS += -Wno-enum-enum-conversion 13726ea6bb1SBehan Websterendif 13864a91907SMasahiro Yamada 13964a91907SMasahiro Yamadaendif 14064a91907SMasahiro Yamada 14164a91907SMasahiro Yamada# 14264a91907SMasahiro Yamada# W=2 - warnings which occur quite often but may still be relevant 14364a91907SMasahiro Yamada# 144e27128dbSMasahiro Yamadaifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) 14564a91907SMasahiro Yamada 14664a91907SMasahiro YamadaKBUILD_CFLAGS += -Wdisabled-optimization 14764a91907SMasahiro YamadaKBUILD_CFLAGS += -Wshadow 14864a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wlogical-op) 14964a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wunused-macros) 15064a91907SMasahiro Yamada 1516863f564SMasahiro YamadaKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 1526863f564SMasahiro Yamada 1532cd3271bSArnd Bergmannelse 1542cd3271bSArnd Bergmann 1552cd3271bSArnd Bergmann# The following turn off the warnings enabled by -Wextra 1562cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-missing-field-initializers 1572cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-type-limits 1582cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-shift-negative-value 1592cd3271bSArnd Bergmann 160c40845e3SArnd Bergmannifdef CONFIG_CC_IS_GCC 1612cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-maybe-uninitialized 1622cd3271bSArnd Bergmannendif 1632cd3271bSArnd Bergmann 16464a91907SMasahiro Yamadaendif 16564a91907SMasahiro Yamada 16664a91907SMasahiro Yamada# 16764a91907SMasahiro Yamada# W=3 - more obscure warnings, can most likely be ignored 16864a91907SMasahiro Yamada# 169e27128dbSMasahiro Yamadaifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) 17064a91907SMasahiro Yamada 17164a91907SMasahiro YamadaKBUILD_CFLAGS += -Wbad-function-cast 172095fbca0SArnd BergmannKBUILD_CFLAGS += -Wcast-align 17364a91907SMasahiro YamadaKBUILD_CFLAGS += -Wcast-qual 17464a91907SMasahiro YamadaKBUILD_CFLAGS += -Wconversion 17564a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpacked 17664a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpadded 17764a91907SMasahiro YamadaKBUILD_CFLAGS += -Wpointer-arith 17864a91907SMasahiro YamadaKBUILD_CFLAGS += -Wredundant-decls 179a97ea93eSJoe PerchesKBUILD_CFLAGS += -Wsign-compare 18064a91907SMasahiro YamadaKBUILD_CFLAGS += -Wswitch-default 18164a91907SMasahiro Yamada 1826863f564SMasahiro YamadaKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 1836863f564SMasahiro Yamada 1842cd3271bSArnd Bergmannelse 1852cd3271bSArnd Bergmann 1862cd3271bSArnd Bergmann# The following turn off the warnings enabled by -Wextra 1872cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-sign-compare 188f5982cceSArnd BergmannKBUILD_CFLAGS += -Wno-unused-parameter 1892cd3271bSArnd Bergmann 190a86fe353SMasahiro Yamadaendif 191c77d06e7SYann Droneaud 192c77d06e7SYann Droneaud# 193c77d06e7SYann Droneaud# W=e - error out on warnings 194c77d06e7SYann Droneaud# 195c77d06e7SYann Droneaudifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),) 196c77d06e7SYann Droneaud 197c77d06e7SYann DroneaudKBUILD_CFLAGS += -Werror 198c77d06e7SYann Droneaud 199c77d06e7SYann Droneaudendif 200