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