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