xref: /linux/scripts/Makefile.extrawarn (revision 68f715a820b02f965e2afc584a6cb542843cbc98)
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, address-of-packed-member)
20KBUILD_CFLAGS += -Wmissing-declarations
21KBUILD_CFLAGS += -Wmissing-prototypes
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# The allocators already balk at large sizes, so silence the compiler
60# warnings for bounds checks involving those possible values. While
61# -Wno-alloc-size-larger-than would normally be used here, earlier versions
62# of gcc (<9.1) weirdly don't handle the option correctly when _other_
63# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
64# doesn't work (as it is documented to), silently resolving to "0" prior to
65# version 9.1 (and producing an error more recently). Numeric values larger
66# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
67# ignored, continuing to default to PTRDIFF_MAX. So, left with no other
68# choice, we must perform a versioned check to disable this warning.
69# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au
70KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
71KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
72
73# Prohibit date/time macros, which would make the build non-deterministic
74KBUILD_CFLAGS += -Werror=date-time
75
76# enforce correct pointer usage
77KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
78
79# Require designated initializers for all marked structures
80KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
81
82# Warn if there is an enum types mismatch
83KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
84
85#
86# W=1 - warnings which may be relevant and do not occur too often
87#
88ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
89
90KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter
91KBUILD_CFLAGS += $(call cc-option, -Wrestrict)
92KBUILD_CFLAGS += -Wmissing-format-attribute
93KBUILD_CFLAGS += -Wold-style-definition
94KBUILD_CFLAGS += -Wmissing-include-dirs
95KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable)
96KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
97KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned)
98KBUILD_CFLAGS += $(call cc-option, -Wformat-overflow)
99KBUILD_CFLAGS += $(call cc-option, -Wformat-truncation)
100KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation)
101
102KBUILD_CPPFLAGS += -Wundef
103KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
104
105else
106
107# Some diagnostics enabled by default are noisy.
108# Suppress them by using -Wno... except for W=1.
109KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
110KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
111KBUILD_CFLAGS += $(call cc-disable-warning, restrict)
112KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
113KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
114KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
115KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
116
117ifdef CONFIG_CC_IS_CLANG
118# Clang before clang-16 would warn on default argument promotions.
119ifneq ($(call clang-min-version, 160000),y)
120# Disable -Wformat
121KBUILD_CFLAGS += -Wno-format
122# Then re-enable flags that were part of the -Wformat group that aren't
123# problematic.
124KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier
125KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull
126# Requires clang-12+.
127ifeq ($(call clang-min-version, 120000),y)
128KBUILD_CFLAGS += -Wformat-insufficient-args
129endif
130endif
131KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
132KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
133KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access)
134KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict)
135endif
136
137endif
138
139#
140# W=2 - warnings which occur quite often but may still be relevant
141#
142ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
143
144KBUILD_CFLAGS += -Wdisabled-optimization
145KBUILD_CFLAGS += -Wshadow
146KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
147KBUILD_CFLAGS += -Wmissing-field-initializers
148KBUILD_CFLAGS += -Wtype-limits
149KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized)
150KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
151
152ifdef CONFIG_CC_IS_CLANG
153KBUILD_CFLAGS += -Winitializer-overrides
154endif
155
156KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
157
158else
159
160# The following turn off the warnings enabled by -Wextra
161KBUILD_CFLAGS += -Wno-missing-field-initializers
162KBUILD_CFLAGS += -Wno-type-limits
163KBUILD_CFLAGS += -Wno-shift-negative-value
164
165ifdef CONFIG_CC_IS_CLANG
166KBUILD_CFLAGS += -Wno-initializer-overrides
167else
168KBUILD_CFLAGS += -Wno-maybe-uninitialized
169endif
170
171endif
172
173#
174# W=3 - more obscure warnings, can most likely be ignored
175#
176ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
177
178KBUILD_CFLAGS += -Wbad-function-cast
179KBUILD_CFLAGS += -Wcast-align
180KBUILD_CFLAGS += -Wcast-qual
181KBUILD_CFLAGS += -Wconversion
182KBUILD_CFLAGS += -Wpacked
183KBUILD_CFLAGS += -Wpadded
184KBUILD_CFLAGS += -Wpointer-arith
185KBUILD_CFLAGS += -Wredundant-decls
186KBUILD_CFLAGS += -Wsign-compare
187KBUILD_CFLAGS += -Wswitch-default
188KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat)
189
190KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
191
192else
193
194# The following turn off the warnings enabled by -Wextra
195KBUILD_CFLAGS += -Wno-sign-compare
196
197endif
198
199#
200# W=e - error out on warnings
201#
202ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),)
203
204KBUILD_CFLAGS += -Werror
205
206endif
207