xref: /openbmc/linux/scripts/Makefile.extrawarn (revision 44ad3baf1cca483e418b6aadf2d3994f69e0f16a)
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)
20
21ifneq ($(CONFIG_FRAME_WARN),0)
22KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
23endif
24
25KBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror
26KBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y)
27KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds
28
29ifdef CONFIG_CC_IS_CLANG
30# The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable.
31KBUILD_CFLAGS += -Wno-gnu
32
33# Clang may emit a warning when a const variable, such as the dummy variables
34# in typecheck(), or const member of an aggregate type are not initialized,
35# which can result in unexpected behavior. However, in many audited cases of
36# the "field" variant of the warning, this is intentional because the field is
37# never used within a particular call path, the field is within a union with
38# other non-const members, or the containing object is not const so the field
39# can be modified via memcpy() / memset(). While the variable warning also gets
40# disabled with this same switch, there should not be too much coverage lost
41# because -Wuninitialized will still flag when an uninitialized const variable
42# is used.
43KBUILD_CFLAGS += $(call cc-disable-warning, default-const-init-unsafe)
44else
45
46# gcc inanely warns about local variables called 'main'
47KBUILD_CFLAGS += -Wno-main
48endif
49
50# These warnings generated too much noise in a regular build.
51# Use make W=1 to enable them (see scripts/Makefile.extrawarn)
52KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
53KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
54
55# These result in bogus false positives
56KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
57
58# Variable Length Arrays (VLAs) should not be used anywhere in the kernel
59KBUILD_CFLAGS += -Wvla
60
61# disable pointer signed / unsigned warnings in gcc 4.0
62KBUILD_CFLAGS += -Wno-pointer-sign
63
64# In order to make sure new function cast mismatches are not introduced
65# in the kernel (to avoid tripping CFI checking), the kernel should be
66# globally built with -Wcast-function-type.
67KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type)
68
69# The allocators already balk at large sizes, so silence the compiler
70# warnings for bounds checks involving those possible values. While
71# -Wno-alloc-size-larger-than would normally be used here, earlier versions
72# of gcc (<9.1) weirdly don't handle the option correctly when _other_
73# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
74# doesn't work (as it is documented to), silently resolving to "0" prior to
75# version 9.1 (and producing an error more recently). Numeric values larger
76# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
77# ignored, continuing to default to PTRDIFF_MAX. So, left with no other
78# choice, we must perform a versioned check to disable this warning.
79# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au
80KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
81KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
82
83# Prohibit date/time macros, which would make the build non-deterministic
84KBUILD_CFLAGS += -Werror=date-time
85
86# enforce correct pointer usage
87KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
88
89# Require designated initializers for all marked structures
90KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
91
92# Warn if there is an enum types mismatch
93KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
94
95# backward compatibility
96KBUILD_EXTRA_WARN ?= $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)
97
98ifeq ("$(origin W)", "command line")
99  KBUILD_EXTRA_WARN := $(W)
100endif
101
102export KBUILD_EXTRA_WARN
103
104#
105# W=1 - warnings which may be relevant and do not occur too often
106#
107ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
108
109KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter
110KBUILD_CFLAGS += -Wmissing-declarations
111KBUILD_CFLAGS += $(call cc-option, -Wrestrict)
112KBUILD_CFLAGS += -Wmissing-format-attribute
113KBUILD_CFLAGS += -Wmissing-prototypes
114KBUILD_CFLAGS += -Wold-style-definition
115KBUILD_CFLAGS += -Wmissing-include-dirs
116KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable)
117KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
118KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned)
119KBUILD_CFLAGS += $(call cc-option, -Wformat-overflow)
120KBUILD_CFLAGS += $(call cc-option, -Wformat-truncation)
121KBUILD_CFLAGS += $(call cc-option, -Wstringop-overflow)
122KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation)
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-disable-warning, unused-but-set-variable)
132KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
133KBUILD_CFLAGS += $(call cc-disable-warning, restrict)
134KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
135KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
136KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
137KBUILD_CFLAGS += $(call cc-disable-warning, stringop-overflow)
138KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
139
140ifdef CONFIG_CC_IS_CLANG
141# Clang before clang-16 would warn on default argument promotions.
142ifneq ($(call clang-min-version, 160000),y)
143# Disable -Wformat
144KBUILD_CFLAGS += -Wno-format
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+.
150ifeq ($(call clang-min-version, 120000),y)
151KBUILD_CFLAGS += -Wformat-insufficient-args
152endif
153endif
154KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
155KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
156KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access)
157KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict)
158KBUILD_CFLAGS += -Wno-enum-compare-conditional
159endif
160
161endif
162
163#
164# W=2 - warnings which occur quite often but may still be relevant
165#
166ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
167
168KBUILD_CFLAGS += -Wdisabled-optimization
169KBUILD_CFLAGS += -Wshadow
170KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
171KBUILD_CFLAGS += -Wmissing-field-initializers
172KBUILD_CFLAGS += -Wtype-limits
173KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized)
174KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
175
176ifdef CONFIG_CC_IS_CLANG
177KBUILD_CFLAGS += -Winitializer-overrides
178endif
179
180KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
181
182else
183
184# The following turn off the warnings enabled by -Wextra
185KBUILD_CFLAGS += -Wno-missing-field-initializers
186KBUILD_CFLAGS += -Wno-type-limits
187KBUILD_CFLAGS += -Wno-shift-negative-value
188
189ifdef CONFIG_CC_IS_CLANG
190KBUILD_CFLAGS += -Wno-enum-enum-conversion
191endif
192
193ifdef CONFIG_CC_IS_CLANG
194KBUILD_CFLAGS += -Wno-initializer-overrides
195else
196KBUILD_CFLAGS += -Wno-maybe-uninitialized
197endif
198
199endif
200
201#
202# W=3 - more obscure warnings, can most likely be ignored
203#
204ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
205
206KBUILD_CFLAGS += -Wbad-function-cast
207KBUILD_CFLAGS += -Wcast-align
208KBUILD_CFLAGS += -Wcast-qual
209KBUILD_CFLAGS += -Wconversion
210KBUILD_CFLAGS += -Wpacked
211KBUILD_CFLAGS += -Wpadded
212KBUILD_CFLAGS += -Wpointer-arith
213KBUILD_CFLAGS += -Wredundant-decls
214KBUILD_CFLAGS += -Wsign-compare
215KBUILD_CFLAGS += -Wswitch-default
216KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat)
217
218KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
219
220else
221
222# The following turn off the warnings enabled by -Wextra
223KBUILD_CFLAGS += -Wno-sign-compare
224
225endif
226
227#
228# W=e - error out on warnings
229#
230ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),)
231
232KBUILD_CFLAGS += -Werror
233
234endif
235