xref: /linux-6.15/scripts/Makefile.extrawarn (revision 2cd3271b)
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 += -Wundef
12KBUILD_CFLAGS += -Werror=implicit-function-declaration
13KBUILD_CFLAGS += -Werror=implicit-int
14KBUILD_CFLAGS += -Werror=return-type
15KBUILD_CFLAGS += -Werror=strict-prototypes
16KBUILD_CFLAGS += -Wno-format-security
17KBUILD_CFLAGS += -Wno-trigraphs
18KBUILD_CFLAGS += $(call cc-disable-warning,frame-address,)
19KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
20KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
21KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
22
23ifneq ($(CONFIG_FRAME_WARN),0)
24KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
25endif
26
27KBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror
28KBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y)
29KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds
30
31ifdef CONFIG_CC_IS_CLANG
32# The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable.
33KBUILD_CFLAGS += -Wno-gnu
34else
35
36# gcc inanely warns about local variables called 'main'
37KBUILD_CFLAGS += -Wno-main
38endif
39
40# These warnings generated too much noise in a regular build.
41# Use make W=1 to enable them (see scripts/Makefile.extrawarn)
42KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
43KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
44
45# These result in bogus false positives
46KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
47
48# Variable Length Arrays (VLAs) should not be used anywhere in the kernel
49KBUILD_CFLAGS += -Wvla
50
51# disable pointer signed / unsigned warnings in gcc 4.0
52KBUILD_CFLAGS += -Wno-pointer-sign
53
54# In order to make sure new function cast mismatches are not introduced
55# in the kernel (to avoid tripping CFI checking), the kernel should be
56# globally built with -Wcast-function-type.
57KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type)
58
59# We'll want to enable this eventually, but it's not going away for 5.7 at least
60KBUILD_CFLAGS += $(call cc-disable-warning, stringop-overflow)
61
62# Another good warning that we'll want to enable eventually
63KBUILD_CFLAGS += $(call cc-disable-warning, restrict)
64
65# The allocators already balk at large sizes, so silence the compiler
66# warnings for bounds checks involving those possible values. While
67# -Wno-alloc-size-larger-than would normally be used here, earlier versions
68# of gcc (<9.1) weirdly don't handle the option correctly when _other_
69# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
70# doesn't work (as it is documented to), silently resolving to "0" prior to
71# version 9.1 (and producing an error more recently). Numeric values larger
72# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
73# ignored, continuing to default to PTRDIFF_MAX. So, left with no other
74# choice, we must perform a versioned check to disable this warning.
75# https://lore.kernel.org/lkml/[email protected]
76KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
77KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
78
79# Prohibit date/time macros, which would make the build non-deterministic
80KBUILD_CFLAGS += -Werror=date-time
81
82# enforce correct pointer usage
83KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
84
85# Require designated initializers for all marked structures
86KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
87
88# Warn if there is an enum types mismatch
89KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
90
91# backward compatibility
92KBUILD_EXTRA_WARN ?= $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)
93
94ifeq ("$(origin W)", "command line")
95  KBUILD_EXTRA_WARN := $(W)
96endif
97
98export KBUILD_EXTRA_WARN
99
100#
101# W=1 - warnings which may be relevant and do not occur too often
102#
103ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
104
105KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter
106KBUILD_CFLAGS += -Wmissing-declarations
107KBUILD_CFLAGS += -Wmissing-format-attribute
108KBUILD_CFLAGS += -Wmissing-prototypes
109KBUILD_CFLAGS += -Wold-style-definition
110KBUILD_CFLAGS += -Wmissing-include-dirs
111KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable)
112KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
113KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned)
114KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation)
115
116KBUILD_CPPFLAGS += -Wundef
117KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
118
119else
120
121# Some diagnostics enabled by default are noisy.
122# Suppress them by using -Wno... except for W=1.
123KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
124KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
125KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
126KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
127
128ifdef CONFIG_CC_IS_CLANG
129# Clang before clang-16 would warn on default argument promotions.
130ifneq ($(call clang-min-version, 160000),y)
131# Disable -Wformat
132KBUILD_CFLAGS += -Wno-format
133# Then re-enable flags that were part of the -Wformat group that aren't
134# problematic.
135KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier
136KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull
137# Requires clang-12+.
138ifeq ($(call clang-min-version, 120000),y)
139KBUILD_CFLAGS += -Wformat-insufficient-args
140endif
141endif
142KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
143KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
144KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access)
145KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict)
146endif
147
148endif
149
150#
151# W=2 - warnings which occur quite often but may still be relevant
152#
153ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
154
155KBUILD_CFLAGS += -Wdisabled-optimization
156KBUILD_CFLAGS += -Wshadow
157KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
158KBUILD_CFLAGS += -Wmissing-field-initializers
159KBUILD_CFLAGS += -Wtype-limits
160KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized)
161KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
162
163ifdef CONFIG_CC_IS_CLANG
164KBUILD_CFLAGS += -Winitializer-overrides
165endif
166
167KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
168
169else
170
171# The following turn off the warnings enabled by -Wextra
172KBUILD_CFLAGS += -Wno-missing-field-initializers
173KBUILD_CFLAGS += -Wno-type-limits
174KBUILD_CFLAGS += -Wno-shift-negative-value
175
176ifdef CONFIG_CC_IS_CLANG
177KBUILD_CFLAGS += -Wno-initializer-overrides
178else
179KBUILD_CFLAGS += -Wno-maybe-uninitialized
180endif
181
182endif
183
184#
185# W=3 - more obscure warnings, can most likely be ignored
186#
187ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
188
189KBUILD_CFLAGS += -Wbad-function-cast
190KBUILD_CFLAGS += -Wcast-align
191KBUILD_CFLAGS += -Wcast-qual
192KBUILD_CFLAGS += -Wconversion
193KBUILD_CFLAGS += -Wpacked
194KBUILD_CFLAGS += -Wpadded
195KBUILD_CFLAGS += -Wpointer-arith
196KBUILD_CFLAGS += -Wredundant-decls
197KBUILD_CFLAGS += -Wsign-compare
198KBUILD_CFLAGS += -Wswitch-default
199KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat)
200
201KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
202
203else
204
205# The following turn off the warnings enabled by -Wextra
206KBUILD_CFLAGS += -Wno-sign-compare
207
208endif
209
210#
211# W=e - error out on warnings
212#
213ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),)
214
215KBUILD_CFLAGS += -Werror
216
217endif
218