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 | 19 | KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member) |
0fcb7085 AB |
20 | KBUILD_CFLAGS += -Wmissing-declarations |
21 | KBUILD_CFLAGS += -Wmissing-prototypes | |
e88ca243 AB |
22 | |
23 | ifneq ($(CONFIG_FRAME_WARN),0) | |
24 | KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN) | |
25 | endif | |
26 | ||
27 | KBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror | |
28 | KBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y) | |
29 | KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds | |
30 | ||
31 | ifdef CONFIG_CC_IS_CLANG | |
32 | # The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable. | |
33 | KBUILD_CFLAGS += -Wno-gnu | |
34 | else | |
35 | ||
36 | # gcc inanely warns about local variables called 'main' | |
37 | KBUILD_CFLAGS += -Wno-main | |
38 | endif | |
39 | ||
40 | # These warnings generated too much noise in a regular build. | |
41 | # Use make W=1 to enable them (see scripts/Makefile.extrawarn) | |
42 | KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) | |
43 | KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) | |
44 | ||
45 | # These result in bogus false positives | |
46 | KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer) | |
47 | ||
48 | # Variable Length Arrays (VLAs) should not be used anywhere in the kernel | |
49 | KBUILD_CFLAGS += -Wvla | |
50 | ||
51 | # disable pointer signed / unsigned warnings in gcc 4.0 | |
52 | KBUILD_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. | |
57 | KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type) | |
58 | ||
e88ca243 AB |
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 | |
70 | KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than | |
71 | KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) | |
72 | ||
73 | # Prohibit date/time macros, which would make the build non-deterministic | |
74 | KBUILD_CFLAGS += -Werror=date-time | |
75 | ||
76 | # enforce correct pointer usage | |
77 | KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types) | |
78 | ||
79 | # Require designated initializers for all marked structures | |
80 | KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) | |
81 | ||
82 | # Warn if there is an enum types mismatch | |
83 | KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion) | |
84 | ||
64a91907 MY |
85 | # |
86 | # W=1 - warnings which may be relevant and do not occur too often | |
87 | # | |
e27128db | 88 | ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) |
a86fe353 | 89 | |
64a91907 | 90 | KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter |
26030cb9 | 91 | KBUILD_CFLAGS += $(call cc-option, -Wrestrict) |
64a91907 | 92 | KBUILD_CFLAGS += -Wmissing-format-attribute |
64a91907 MY |
93 | KBUILD_CFLAGS += -Wold-style-definition |
94 | KBUILD_CFLAGS += -Wmissing-include-dirs | |
95 | KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable) | |
96 | KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) | |
97 | KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned) | |
6d4ab2e9 AB |
98 | KBUILD_CFLAGS += $(call cc-option, -Wformat-overflow) |
99 | KBUILD_CFLAGS += $(call cc-option, -Wformat-truncation) | |
64a91907 | 100 | KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation) |
a86fe353 | 101 | |
80b6093b | 102 | KBUILD_CPPFLAGS += -Wundef |
6863f564 MY |
103 | KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 |
104 | ||
26ea6bb1 BW |
105 | else |
106 | ||
64a91907 MY |
107 | # Some diagnostics enabled by default are noisy. |
108 | # Suppress them by using -Wno... except for W=1. | |
2cd3271b AB |
109 | KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) |
110 | KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) | |
26030cb9 | 111 | KBUILD_CFLAGS += $(call cc-disable-warning, restrict) |
2cd3271b | 112 | KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned) |
6d4ab2e9 AB |
113 | KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow) |
114 | KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation) | |
2cd3271b | 115 | KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation) |
64a91907 | 116 | |
076f421d | 117 | ifdef CONFIG_CC_IS_CLANG |
b0839b28 | 118 | # Clang before clang-16 would warn on default argument promotions. |
88b61e3b | 119 | ifneq ($(call clang-min-version, 160000),y) |
b0839b28 | 120 | # Disable -Wformat |
21f9c8a1 | 121 | KBUILD_CFLAGS += -Wno-format |
b0839b28 ND |
122 | # Then re-enable flags that were part of the -Wformat group that aren't |
123 | # problematic. | |
124 | KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier | |
125 | KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull | |
126 | # Requires clang-12+. | |
88b61e3b | 127 | ifeq ($(call clang-min-version, 120000),y) |
b0839b28 ND |
128 | KBUILD_CFLAGS += -Wformat-insufficient-args |
129 | endif | |
130 | endif | |
82f2bc2f | 131 | KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast) |
afe956c5 | 132 | KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare |
1cf5f151 | 133 | KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access) |
21206351 | 134 | KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict) |
26ea6bb1 | 135 | endif |
64a91907 MY |
136 | |
137 | endif | |
138 | ||
139 | # | |
140 | # W=2 - warnings which occur quite often but may still be relevant | |
141 | # | |
e27128db | 142 | ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) |
64a91907 | 143 | |
64a91907 | 144 | KBUILD_CFLAGS += -Wdisabled-optimization |
64a91907 MY |
145 | KBUILD_CFLAGS += -Wshadow |
146 | KBUILD_CFLAGS += $(call cc-option, -Wlogical-op) | |
147 | KBUILD_CFLAGS += -Wmissing-field-initializers | |
355a3587 | 148 | KBUILD_CFLAGS += -Wtype-limits |
64a91907 MY |
149 | KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized) |
150 | KBUILD_CFLAGS += $(call cc-option, -Wunused-macros) | |
151 | ||
2cd3271b AB |
152 | ifdef CONFIG_CC_IS_CLANG |
153 | KBUILD_CFLAGS += -Winitializer-overrides | |
154 | endif | |
155 | ||
6863f564 MY |
156 | KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 |
157 | ||
2cd3271b AB |
158 | else |
159 | ||
160 | # The following turn off the warnings enabled by -Wextra | |
161 | KBUILD_CFLAGS += -Wno-missing-field-initializers | |
162 | KBUILD_CFLAGS += -Wno-type-limits | |
163 | KBUILD_CFLAGS += -Wno-shift-negative-value | |
164 | ||
165 | ifdef CONFIG_CC_IS_CLANG | |
166 | KBUILD_CFLAGS += -Wno-initializer-overrides | |
167 | else | |
168 | KBUILD_CFLAGS += -Wno-maybe-uninitialized | |
169 | endif | |
170 | ||
64a91907 MY |
171 | endif |
172 | ||
173 | # | |
174 | # W=3 - more obscure warnings, can most likely be ignored | |
175 | # | |
e27128db | 176 | ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) |
64a91907 MY |
177 | |
178 | KBUILD_CFLAGS += -Wbad-function-cast | |
095fbca0 | 179 | KBUILD_CFLAGS += -Wcast-align |
64a91907 MY |
180 | KBUILD_CFLAGS += -Wcast-qual |
181 | KBUILD_CFLAGS += -Wconversion | |
182 | KBUILD_CFLAGS += -Wpacked | |
183 | KBUILD_CFLAGS += -Wpadded | |
184 | KBUILD_CFLAGS += -Wpointer-arith | |
185 | KBUILD_CFLAGS += -Wredundant-decls | |
a97ea93e | 186 | KBUILD_CFLAGS += -Wsign-compare |
64a91907 MY |
187 | KBUILD_CFLAGS += -Wswitch-default |
188 | KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat) | |
189 | ||
6863f564 MY |
190 | KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 |
191 | ||
2cd3271b AB |
192 | else |
193 | ||
194 | # The following turn off the warnings enabled by -Wextra | |
195 | KBUILD_CFLAGS += -Wno-sign-compare | |
196 | ||
a86fe353 | 197 | endif |
c77d06e7 YD |
198 | |
199 | # | |
200 | # W=e - error out on warnings | |
201 | # | |
202 | ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),) | |
203 | ||
204 | KBUILD_CFLAGS += -Werror | |
205 | ||
206 | endif |