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