xref: /linux/scripts/Makefile.extrawarn (revision 36ec807b627b4c0a0a382f0ae48eac7187d14b2b)
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 += -Wextra
12e88ca243SArnd BergmannKBUILD_CFLAGS += -Wundef
13e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=implicit-function-declaration
14e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=implicit-int
15e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=return-type
16e88ca243SArnd BergmannKBUILD_CFLAGS += -Werror=strict-prototypes
17e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-format-security
18e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-trigraphs
19e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-frame-address)
200fcb7085SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-address-of-packed-member)
210fcb7085SArnd BergmannKBUILD_CFLAGS += -Wmissing-declarations
22e88ca243SArnd BergmannKBUILD_CFLAGS += -Wmissing-prototypes
23e88ca243SArnd Bergmann
24e88ca243SArnd Bergmannifneq ($(CONFIG_FRAME_WARN),0)
25e88ca243SArnd BergmannKBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
26e88ca243SArnd Bergmannendif
27e88ca243SArnd Bergmann
28e88ca243SArnd BergmannKBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds
29e88ca243SArnd Bergmann
30e88ca243SArnd Bergmannifdef CONFIG_CC_IS_CLANG
31e88ca243SArnd Bergmann# The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable.
32e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-gnu
33e88ca243SArnd Bergmann
34e88ca243SArnd Bergmann# Clang checks for overflow/truncation with '%p', while GCC does not:
35e88ca243SArnd Bergmann# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219
36e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow-non-kprintf)
37e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation-non-kprintf)
38e88ca243SArnd Bergmann
39e88ca243SArnd Bergmann# Clang may emit a warning when a const variable, such as the dummy variables
40e88ca243SArnd Bergmann# in typecheck(), or const member of an aggregate type are not initialized,
41e88ca243SArnd Bergmann# which can result in unexpected behavior. However, in many audited cases of
42e88ca243SArnd Bergmann# the "field" variant of the warning, this is intentional because the field is
43e88ca243SArnd Bergmann# never used within a particular call path, the field is within a union with
44e88ca243SArnd Bergmann# other non-const members, or the containing object is not const so the field
45e88ca243SArnd Bergmann# can be modified via memcpy() / memset(). While the variable warning also gets
46e88ca243SArnd Bergmann# disabled with this same switch, there should not be too much coverage lost
47e88ca243SArnd Bergmann# because -Wuninitialized will still flag when an uninitialized const variable
48e88ca243SArnd Bergmann# is used.
49e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-default-const-init-unsafe)
50e88ca243SArnd Bergmannelse
51e88ca243SArnd Bergmann
52e88ca243SArnd Bergmann# gcc inanely warns about local variables called 'main'
53e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-main
54e88ca243SArnd Bergmannendif
55e88ca243SArnd Bergmann
56e88ca243SArnd Bergmann# These result in bogus false positives
57e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-dangling-pointer)
58e88ca243SArnd Bergmann
59e88ca243SArnd Bergmann# Stack Variable Length Arrays (VLAs) must not be used in the kernel.
60e88ca243SArnd Bergmann# Function array parameters should, however, be usable, but -Wvla will
61e88ca243SArnd Bergmann# warn for those. Clang has no way yet to distinguish between the VLA
62e88ca243SArnd Bergmann# types, so depend on GCC for now to keep stack VLAs out of the tree.
63e88ca243SArnd Bergmann# https://github.com/llvm/llvm-project/issues/57098
64e88ca243SArnd Bergmann# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217
65e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=1)
66e88ca243SArnd Bergmann
67e88ca243SArnd Bergmann# disable pointer signed / unsigned warnings in gcc 4.0
68e88ca243SArnd BergmannKBUILD_CFLAGS += -Wno-pointer-sign
69e88ca243SArnd Bergmann
70e88ca243SArnd Bergmann# In order to make sure new function cast mismatches are not introduced
71e88ca243SArnd Bergmann# in the kernel (to avoid tripping CFI checking), the kernel should be
72e88ca243SArnd Bergmann# globally built with -Wcast-function-type.
73e88ca243SArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wcast-function-type)
74e88ca243SArnd Bergmann
75e88ca243SArnd Bergmann# Currently, disable -Wstringop-overflow for GCC 11, globally.
76e88ca243SArnd BergmannKBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-option, -Wno-stringop-overflow)
77e88ca243SArnd BergmannKBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow)
78e88ca243SArnd Bergmann
79e88ca243SArnd Bergmann# Currently, disable -Wunterminated-string-initialization as broken
80f5982cceSArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-unterminated-string-initialization)
81f5982cceSArnd Bergmann
82f5982cceSArnd Bergmann# The allocators already balk at large sizes, so silence the compiler
8364a91907SMasahiro Yamada# warnings for bounds checks involving those possible values. While
8464a91907SMasahiro Yamada# -Wno-alloc-size-larger-than would normally be used here, earlier versions
8564a91907SMasahiro Yamada# of gcc (<9.1) weirdly don't handle the option correctly when _other_
86e27128dbSMasahiro Yamada# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
87a86fe353SMasahiro Yamada# doesn't work (as it is documented to), silently resolving to "0" prior to
8864a91907SMasahiro Yamada# version 9.1 (and producing an error more recently). Numeric values larger
8964a91907SMasahiro Yamada# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
9064a91907SMasahiro Yamada# ignored, continuing to default to PTRDIFF_MAX. So, left with no other
91a86fe353SMasahiro Yamada# choice, we must perform a versioned check to disable this warning.
9280b6093bSMasahiro Yamada# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au
936863f564SMasahiro YamadaKBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
946863f564SMasahiro YamadaKBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
9526ea6bb1SBehan Webster
9626ea6bb1SBehan Webster# Prohibit date/time macros, which would make the build non-deterministic
9764a91907SMasahiro YamadaKBUILD_CFLAGS += -Werror=date-time
9864a91907SMasahiro Yamada
992cd3271bSArnd Bergmann# enforce correct pointer usage
1002cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
1012cd3271bSArnd Bergmann
1026d4ab2e9SArnd Bergmann# Require designated initializers for all marked structures
103*908dd508SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
1046d4ab2e9SArnd Bergmann
105*908dd508SArnd Bergmann# Warn if there is an enum types mismatch
106*908dd508SArnd BergmannKBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
107*908dd508SArnd Bergmann
108*908dd508SArnd BergmannKBUILD_CFLAGS += -Wunused
109*908dd508SArnd Bergmann
110*908dd508SArnd Bergmann#
1112cd3271bSArnd Bergmann# W=1 - warnings which may be relevant and do not occur too often
11264a91907SMasahiro Yamada#
113c40845e3SArnd Bergmannifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
114c40845e3SArnd Bergmann
115076f421dSMasahiro YamadaKBUILD_CFLAGS += -Wmissing-format-attribute
116b0839b28SNick DesaulniersKBUILD_CFLAGS += -Wmissing-include-dirs
11788b61e3bSNick DesaulniersKBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
118b0839b28SNick Desaulniers
11921f9c8a1SLinus TorvaldsKBUILD_CPPFLAGS += -Wundef
120b0839b28SNick DesaulniersKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
121b0839b28SNick Desaulniers
122b0839b28SNick Desaulnierselse
123b0839b28SNick Desaulniers
124b0839b28SNick Desaulniers# Some diagnostics enabled by default are noisy.
12588b61e3bSNick Desaulniers# Suppress them by using -Wno... except for W=1.
126b0839b28SNick DesaulniersKBUILD_CFLAGS += $(call cc-option, -Wno-unused-but-set-variable)
127b0839b28SNick DesaulniersKBUILD_CFLAGS += $(call cc-option, -Wno-unused-const-variable)
128b0839b28SNick DesaulniersKBUILD_CFLAGS += $(call cc-option, -Wno-packed-not-aligned)
12982f2bc2fSNathan ChancellorKBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow)
130afe956c5SNathan Chancellorifdef CONFIG_CC_IS_GCC
1311cf5f151SNathan ChancellorKBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation)
13275b5ab13SNathan Chancellorendif
13375b5ab13SNathan ChancellorKBUILD_CFLAGS += $(call cc-option, -Wno-stringop-truncation)
13426ea6bb1SBehan Webster
13564a91907SMasahiro YamadaKBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
13664a91907SMasahiro Yamada
13764a91907SMasahiro Yamadaifdef CONFIG_CC_IS_CLANG
13864a91907SMasahiro Yamada# Clang before clang-16 would warn on default argument promotions.
13964a91907SMasahiro Yamadaifneq ($(call clang-min-version, 160000),y)
14064a91907SMasahiro Yamada# Disable -Wformat
141e27128dbSMasahiro YamadaKBUILD_CFLAGS += -Wno-format
14264a91907SMasahiro Yamada# Then re-enable flags that were part of the -Wformat group that aren't
14364a91907SMasahiro Yamada# problematic.
14464a91907SMasahiro YamadaKBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier
14564a91907SMasahiro YamadaKBUILD_CFLAGS += -Wformat-zero-length -Wnonnull
14664a91907SMasahiro Yamada# Requires clang-12+.
14764a91907SMasahiro Yamadaifeq ($(call clang-min-version, 120000),y)
1486863f564SMasahiro YamadaKBUILD_CFLAGS += -Wformat-insufficient-args
1496863f564SMasahiro Yamadaendif
1502cd3271bSArnd Bergmannendif
1512cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-pointer-to-enum-cast)
1522cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
1532cd3271bSArnd BergmannKBUILD_CFLAGS += $(call cc-option, -Wno-unaligned-access)
1542cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-enum-compare-conditional
1552cd3271bSArnd Bergmannendif
1562cd3271bSArnd Bergmann
157c40845e3SArnd Bergmannendif
1582cd3271bSArnd Bergmann
1592cd3271bSArnd Bergmann#
1602cd3271bSArnd Bergmann# W=2 - warnings which occur quite often but may still be relevant
16164a91907SMasahiro Yamada#
16264a91907SMasahiro Yamadaifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
16364a91907SMasahiro Yamada
16464a91907SMasahiro YamadaKBUILD_CFLAGS += -Wdisabled-optimization
16564a91907SMasahiro YamadaKBUILD_CFLAGS += -Wshadow
166e27128dbSMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
16764a91907SMasahiro YamadaKBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
16864a91907SMasahiro Yamada
169095fbca0SArnd BergmannKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
17064a91907SMasahiro Yamada
17164a91907SMasahiro Yamadaelse
17264a91907SMasahiro Yamada
17364a91907SMasahiro Yamada# The following turn off the warnings enabled by -Wextra
17464a91907SMasahiro YamadaKBUILD_CFLAGS += -Wno-missing-field-initializers
17564a91907SMasahiro YamadaKBUILD_CFLAGS += -Wno-type-limits
176a97ea93eSJoe PerchesKBUILD_CFLAGS += -Wno-shift-negative-value
17764a91907SMasahiro Yamada
17864a91907SMasahiro Yamadaifdef CONFIG_CC_IS_CLANG
1796863f564SMasahiro YamadaKBUILD_CFLAGS += -Wno-enum-enum-conversion
1806863f564SMasahiro Yamadaendif
1812cd3271bSArnd Bergmann
1822cd3271bSArnd Bergmannifdef CONFIG_CC_IS_GCC
1832cd3271bSArnd BergmannKBUILD_CFLAGS += -Wno-maybe-uninitialized
1842cd3271bSArnd Bergmannendif
185f5982cceSArnd Bergmann
1862cd3271bSArnd Bergmannendif
187a86fe353SMasahiro Yamada
188c77d06e7SYann Droneaud#
189c77d06e7SYann Droneaud# W=3 - more obscure warnings, can most likely be ignored
190c77d06e7SYann Droneaud#
191c77d06e7SYann Droneaudifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
192c77d06e7SYann Droneaud
193c77d06e7SYann DroneaudKBUILD_CFLAGS += -Wbad-function-cast
194c77d06e7SYann DroneaudKBUILD_CFLAGS += -Wcast-align
195c77d06e7SYann DroneaudKBUILD_CFLAGS += -Wcast-qual
196c77d06e7SYann DroneaudKBUILD_CFLAGS += -Wconversion
197KBUILD_CFLAGS += -Wpacked
198KBUILD_CFLAGS += -Wpadded
199KBUILD_CFLAGS += -Wpointer-arith
200KBUILD_CFLAGS += -Wredundant-decls
201KBUILD_CFLAGS += -Wsign-compare
202KBUILD_CFLAGS += -Wswitch-default
203
204KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
205
206else
207
208# The following turn off the warnings enabled by -Wextra
209KBUILD_CFLAGS += -Wno-sign-compare
210KBUILD_CFLAGS += -Wno-unused-parameter
211
212endif
213
214#
215# W=e and CONFIG_WERROR - error out on warnings
216#
217ifneq ($(findstring e, $(KBUILD_EXTRA_WARN))$(CONFIG_WERROR),)
218
219KBUILD_CPPFLAGS		+= -Werror
220KBUILD_AFLAGS		+= -Wa,--fatal-warnings
221KBUILD_LDFLAGS		+= --fatal-warnings
222KBUILD_USERCFLAGS	+= -Werror
223KBUILD_USERLDFLAGS	+= -Wl,--fatal-warnings
224KBUILD_RUSTFLAGS	+= -Dwarnings
225
226# While hostprog flags are used during build bootstrapping (thus should not
227# depend on CONFIG_ symbols), -Werror is disruptive and should be opted into.
228# Only apply -Werror to hostprogs built after the initial Kconfig stage.
229KBUILD_HOSTCFLAGS	+= -Werror
230KBUILD_HOSTLDFLAGS	+= -Wl,--fatal-warnings
231KBUILD_HOSTRUSTFLAGS	+= -Dwarnings
232
233endif
234