at commit 2a31b537

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