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