Commit | Line | Data |
---|---|---|
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 |
10 | KBUILD_CFLAGS += -Wall | |
11 | KBUILD_CFLAGS += -Wundef | |
12 | KBUILD_CFLAGS += -Werror=implicit-function-declaration | |
13 | KBUILD_CFLAGS += -Werror=implicit-int | |
14 | KBUILD_CFLAGS += -Werror=return-type | |
15 | KBUILD_CFLAGS += -Werror=strict-prototypes | |
16 | KBUILD_CFLAGS += -Wno-format-security | |
17 | KBUILD_CFLAGS += -Wno-trigraphs | |
18 | KBUILD_CFLAGS += $(call cc-disable-warning,frame-address,) | |
e88ca243 AB |
19 | KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member) |
20 | ||
21 | ifneq ($(CONFIG_FRAME_WARN),0) | |
22 | KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN) | |
23 | endif | |
24 | ||
25 | KBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror | |
26 | KBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y) | |
27 | KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds | |
28 | ||
29 | ifdef CONFIG_CC_IS_CLANG | |
30 | # The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable. | |
31 | KBUILD_CFLAGS += -Wno-gnu | |
32 | else | |
33 | ||
34 | # gcc inanely warns about local variables called 'main' | |
35 | KBUILD_CFLAGS += -Wno-main | |
36 | endif | |
37 | ||
38 | # These warnings generated too much noise in a regular build. | |
39 | # Use make W=1 to enable them (see scripts/Makefile.extrawarn) | |
40 | KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) | |
41 | KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) | |
42 | ||
43 | # These result in bogus false positives | |
44 | KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer) | |
45 | ||
46 | # Variable Length Arrays (VLAs) should not be used anywhere in the kernel | |
47 | KBUILD_CFLAGS += -Wvla | |
48 | ||
49 | # disable pointer signed / unsigned warnings in gcc 4.0 | |
50 | KBUILD_CFLAGS += -Wno-pointer-sign | |
51 | ||
52 | # In order to make sure new function cast mismatches are not introduced | |
53 | # in the kernel (to avoid tripping CFI checking), the kernel should be | |
54 | # globally built with -Wcast-function-type. | |
55 | KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type) | |
56 | ||
e88ca243 AB |
57 | # The allocators already balk at large sizes, so silence the compiler |
58 | # warnings for bounds checks involving those possible values. While | |
59 | # -Wno-alloc-size-larger-than would normally be used here, earlier versions | |
60 | # of gcc (<9.1) weirdly don't handle the option correctly when _other_ | |
61 | # warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX | |
62 | # doesn't work (as it is documented to), silently resolving to "0" prior to | |
63 | # version 9.1 (and producing an error more recently). Numeric values larger | |
64 | # than PTRDIFF_MAX also don't work prior to version 9.1, which are silently | |
65 | # ignored, continuing to default to PTRDIFF_MAX. So, left with no other | |
66 | # choice, we must perform a versioned check to disable this warning. | |
67 | # https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au | |
68 | KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than | |
69 | KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) | |
70 | ||
71 | # Prohibit date/time macros, which would make the build non-deterministic | |
72 | KBUILD_CFLAGS += -Werror=date-time | |
73 | ||
74 | # enforce correct pointer usage | |
75 | KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types) | |
76 | ||
77 | # Require designated initializers for all marked structures | |
78 | KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) | |
79 | ||
80 | # Warn if there is an enum types mismatch | |
81 | KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion) | |
82 | ||
e27128db MY |
83 | # backward compatibility |
84 | KBUILD_EXTRA_WARN ?= $(KBUILD_ENABLE_EXTRA_GCC_CHECKS) | |
85 | ||
a86fe353 | 86 | ifeq ("$(origin W)", "command line") |
e27128db | 87 | KBUILD_EXTRA_WARN := $(W) |
a86fe353 MY |
88 | endif |
89 | ||
e27128db MY |
90 | export KBUILD_EXTRA_WARN |
91 | ||
64a91907 MY |
92 | # |
93 | # W=1 - warnings which may be relevant and do not occur too often | |
94 | # | |
e27128db | 95 | ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) |
a86fe353 | 96 | |
64a91907 MY |
97 | KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter |
98 | KBUILD_CFLAGS += -Wmissing-declarations | |
26030cb9 | 99 | KBUILD_CFLAGS += $(call cc-option, -Wrestrict) |
64a91907 MY |
100 | KBUILD_CFLAGS += -Wmissing-format-attribute |
101 | KBUILD_CFLAGS += -Wmissing-prototypes | |
102 | KBUILD_CFLAGS += -Wold-style-definition | |
103 | KBUILD_CFLAGS += -Wmissing-include-dirs | |
104 | KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable) | |
105 | KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) | |
106 | KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned) | |
6d4ab2e9 AB |
107 | KBUILD_CFLAGS += $(call cc-option, -Wformat-overflow) |
108 | KBUILD_CFLAGS += $(call cc-option, -Wformat-truncation) | |
109 | KBUILD_CFLAGS += $(call cc-option, -Wstringop-overflow) | |
64a91907 | 110 | KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation) |
a86fe353 | 111 | |
80b6093b | 112 | KBUILD_CPPFLAGS += -Wundef |
6863f564 MY |
113 | KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 |
114 | ||
26ea6bb1 BW |
115 | else |
116 | ||
64a91907 MY |
117 | # Some diagnostics enabled by default are noisy. |
118 | # Suppress them by using -Wno... except for W=1. | |
2cd3271b AB |
119 | KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) |
120 | KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) | |
26030cb9 | 121 | KBUILD_CFLAGS += $(call cc-disable-warning, restrict) |
2cd3271b | 122 | KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned) |
6d4ab2e9 AB |
123 | KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow) |
124 | KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation) | |
125 | KBUILD_CFLAGS += $(call cc-disable-warning, stringop-overflow) | |
2cd3271b | 126 | KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation) |
64a91907 | 127 | |
076f421d | 128 | ifdef CONFIG_CC_IS_CLANG |
b0839b28 | 129 | # Clang before clang-16 would warn on default argument promotions. |
88b61e3b | 130 | ifneq ($(call clang-min-version, 160000),y) |
b0839b28 | 131 | # Disable -Wformat |
21f9c8a1 | 132 | KBUILD_CFLAGS += -Wno-format |
b0839b28 ND |
133 | # Then re-enable flags that were part of the -Wformat group that aren't |
134 | # problematic. | |
135 | KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier | |
136 | KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull | |
137 | # Requires clang-12+. | |
88b61e3b | 138 | ifeq ($(call clang-min-version, 120000),y) |
b0839b28 ND |
139 | KBUILD_CFLAGS += -Wformat-insufficient-args |
140 | endif | |
141 | endif | |
82f2bc2f | 142 | KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast) |
afe956c5 | 143 | KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare |
1cf5f151 | 144 | KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access) |
21206351 | 145 | KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict) |
26ea6bb1 | 146 | endif |
64a91907 MY |
147 | |
148 | endif | |
149 | ||
150 | # | |
151 | # W=2 - warnings which occur quite often but may still be relevant | |
152 | # | |
e27128db | 153 | ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) |
64a91907 | 154 | |
64a91907 | 155 | KBUILD_CFLAGS += -Wdisabled-optimization |
64a91907 MY |
156 | KBUILD_CFLAGS += -Wshadow |
157 | KBUILD_CFLAGS += $(call cc-option, -Wlogical-op) | |
158 | KBUILD_CFLAGS += -Wmissing-field-initializers | |
355a3587 | 159 | KBUILD_CFLAGS += -Wtype-limits |
64a91907 MY |
160 | KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized) |
161 | KBUILD_CFLAGS += $(call cc-option, -Wunused-macros) | |
162 | ||
2cd3271b AB |
163 | ifdef CONFIG_CC_IS_CLANG |
164 | KBUILD_CFLAGS += -Winitializer-overrides | |
165 | endif | |
166 | ||
6863f564 MY |
167 | KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 |
168 | ||
2cd3271b AB |
169 | else |
170 | ||
171 | # The following turn off the warnings enabled by -Wextra | |
172 | KBUILD_CFLAGS += -Wno-missing-field-initializers | |
173 | KBUILD_CFLAGS += -Wno-type-limits | |
174 | KBUILD_CFLAGS += -Wno-shift-negative-value | |
175 | ||
176 | ifdef CONFIG_CC_IS_CLANG | |
177 | KBUILD_CFLAGS += -Wno-initializer-overrides | |
178 | else | |
179 | KBUILD_CFLAGS += -Wno-maybe-uninitialized | |
180 | endif | |
181 | ||
64a91907 MY |
182 | endif |
183 | ||
184 | # | |
185 | # W=3 - more obscure warnings, can most likely be ignored | |
186 | # | |
e27128db | 187 | ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) |
64a91907 MY |
188 | |
189 | KBUILD_CFLAGS += -Wbad-function-cast | |
095fbca0 | 190 | KBUILD_CFLAGS += -Wcast-align |
64a91907 MY |
191 | KBUILD_CFLAGS += -Wcast-qual |
192 | KBUILD_CFLAGS += -Wconversion | |
193 | KBUILD_CFLAGS += -Wpacked | |
194 | KBUILD_CFLAGS += -Wpadded | |
195 | KBUILD_CFLAGS += -Wpointer-arith | |
196 | KBUILD_CFLAGS += -Wredundant-decls | |
a97ea93e | 197 | KBUILD_CFLAGS += -Wsign-compare |
64a91907 MY |
198 | KBUILD_CFLAGS += -Wswitch-default |
199 | KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat) | |
200 | ||
6863f564 MY |
201 | KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 |
202 | ||
2cd3271b AB |
203 | else |
204 | ||
205 | # The following turn off the warnings enabled by -Wextra | |
206 | KBUILD_CFLAGS += -Wno-sign-compare | |
207 | ||
a86fe353 | 208 | endif |
c77d06e7 YD |
209 | |
210 | # | |
211 | # W=e - error out on warnings | |
212 | # | |
213 | ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),) | |
214 | ||
215 | KBUILD_CFLAGS += -Werror | |
216 | ||
217 | endif |