xref: /linux/scripts/Makefile.extrawarn (revision c04022dccb2f9cf2b1cfe65807149500d1fc080a)
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-option, -Wno-frame-address)
20KBUILD_CFLAGS += $(call cc-option, -Wno-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_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds
29
30ifdef CONFIG_CC_IS_CLANG
31# The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable.
32KBUILD_CFLAGS += -Wno-gnu
33
34# Clang checks for overflow/truncation with '%p', while GCC does not:
35# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219
36KBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow-non-kprintf)
37KBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation-non-kprintf)
38
39# Clang may emit a warning when a const variable, such as the dummy variables
40# in typecheck(), or const member of an aggregate type are not initialized,
41# which can result in unexpected behavior. However, in many audited cases of
42# the "field" variant of the warning, this is intentional because the field is
43# never used within a particular call path, the field is within a union with
44# other non-const members, or the containing object is not const so the field
45# can be modified via memcpy() / memset(). While the variable warning also gets
46# disabled with this same switch, there should not be too much coverage lost
47# because -Wuninitialized will still flag when an uninitialized const variable
48# is used.
49KBUILD_CFLAGS += $(call cc-option, -Wno-default-const-init-unsafe)
50else
51
52# gcc inanely warns about local variables called 'main'
53KBUILD_CFLAGS += -Wno-main
54endif
55
56# These result in bogus false positives
57KBUILD_CFLAGS += $(call cc-option, -Wno-dangling-pointer)
58
59# Stack Variable Length Arrays (VLAs) must not be used in the kernel.
60# Function array parameters should, however, be usable, but -Wvla will
61# warn for those. Clang has no way yet to distinguish between the VLA
62# types, so depend on GCC for now to keep stack VLAs out of the tree.
63# https://github.com/llvm/llvm-project/issues/57098
64# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217
65KBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=1)
66
67# disable pointer signed / unsigned warnings in gcc 4.0
68KBUILD_CFLAGS += -Wno-pointer-sign
69
70# In order to make sure new function cast mismatches are not introduced
71# in the kernel (to avoid tripping CFI checking), the kernel should be
72# globally built with -Wcast-function-type.
73KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type)
74
75# Currently, disable -Wstringop-overflow for GCC 11, globally.
76KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-option, -Wno-stringop-overflow)
77KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow)
78
79# Currently, disable -Wunterminated-string-initialization as broken
80KBUILD_CFLAGS += $(call cc-option, -Wno-unterminated-string-initialization)
81
82# The allocators already balk at large sizes, so silence the compiler
83# warnings for bounds checks involving those possible values. While
84# -Wno-alloc-size-larger-than would normally be used here, earlier versions
85# of gcc (<9.1) weirdly don't handle the option correctly when _other_
86# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
87# doesn't work (as it is documented to), silently resolving to "0" prior to
88# version 9.1 (and producing an error more recently). Numeric values larger
89# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
90# ignored, continuing to default to PTRDIFF_MAX. So, left with no other
91# choice, we must perform a versioned check to disable this warning.
92# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au
93KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
94KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
95
96# Prohibit date/time macros, which would make the build non-deterministic
97KBUILD_CFLAGS += -Werror=date-time
98
99# enforce correct pointer usage
100KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
101
102# Require designated initializers for all marked structures
103KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
104
105# Warn if there is an enum types mismatch
106KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
107
108KBUILD_CFLAGS += -Wunused
109
110#
111# W=1 - warnings which may be relevant and do not occur too often
112#
113ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
114
115KBUILD_CFLAGS += -Wmissing-format-attribute
116KBUILD_CFLAGS += -Wmissing-include-dirs
117KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
118
119KBUILD_CPPFLAGS += -Wundef
120KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
121
122else
123
124# Some diagnostics enabled by default are noisy.
125# Suppress them by using -Wno... except for W=1.
126KBUILD_CFLAGS += $(call cc-option, -Wno-unused-but-set-variable)
127KBUILD_CFLAGS += $(call cc-option, -Wno-unused-const-variable)
128KBUILD_CFLAGS += $(call cc-option, -Wno-packed-not-aligned)
129KBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow)
130ifdef CONFIG_CC_IS_GCC
131KBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation)
132endif
133KBUILD_CFLAGS += $(call cc-option, -Wno-stringop-truncation)
134
135KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
136
137ifdef CONFIG_CC_IS_CLANG
138# Clang before clang-16 would warn on default argument promotions.
139ifneq ($(call clang-min-version, 160000),y)
140# Disable -Wformat
141KBUILD_CFLAGS += -Wno-format
142# Then re-enable flags that were part of the -Wformat group that aren't
143# problematic.
144KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier
145KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull
146# Requires clang-12+.
147ifeq ($(call clang-min-version, 120000),y)
148KBUILD_CFLAGS += -Wformat-insufficient-args
149endif
150endif
151KBUILD_CFLAGS += $(call cc-option, -Wno-pointer-to-enum-cast)
152KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
153KBUILD_CFLAGS += $(call cc-option, -Wno-unaligned-access)
154KBUILD_CFLAGS += -Wno-enum-compare-conditional
155endif
156
157endif
158
159#
160# W=2 - warnings which occur quite often but may still be relevant
161#
162ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
163
164KBUILD_CFLAGS += -Wdisabled-optimization
165KBUILD_CFLAGS += -Wshadow
166KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
167KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
168
169KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
170
171else
172
173# The following turn off the warnings enabled by -Wextra
174KBUILD_CFLAGS += -Wno-missing-field-initializers
175KBUILD_CFLAGS += -Wno-type-limits
176KBUILD_CFLAGS += -Wno-shift-negative-value
177
178ifdef CONFIG_CC_IS_CLANG
179KBUILD_CFLAGS += -Wno-enum-enum-conversion
180endif
181
182ifdef CONFIG_CC_IS_GCC
183KBUILD_CFLAGS += -Wno-maybe-uninitialized
184endif
185
186endif
187
188#
189# W=3 - more obscure warnings, can most likely be ignored
190#
191ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
192
193KBUILD_CFLAGS += -Wbad-function-cast
194KBUILD_CFLAGS += -Wcast-align
195KBUILD_CFLAGS += -Wcast-qual
196KBUILD_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