extrawarn: enable format and stringop overflow warnings in W=1
[platform/kernel/linux-starfive.git] / scripts / Makefile.extrawarn
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 += -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,)
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
57 # Another good warning that we'll want to enable eventually
58 KBUILD_CFLAGS += $(call cc-disable-warning, restrict)
59
60 # The allocators already balk at large sizes, so silence the compiler
61 # warnings for bounds checks involving those possible values. While
62 # -Wno-alloc-size-larger-than would normally be used here, earlier versions
63 # of gcc (<9.1) weirdly don't handle the option correctly when _other_
64 # warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
65 # doesn't work (as it is documented to), silently resolving to "0" prior to
66 # version 9.1 (and producing an error more recently). Numeric values larger
67 # than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
68 # ignored, continuing to default to PTRDIFF_MAX. So, left with no other
69 # choice, we must perform a versioned check to disable this warning.
70 # https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au
71 KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
72 KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
73
74 # Prohibit date/time macros, which would make the build non-deterministic
75 KBUILD_CFLAGS += -Werror=date-time
76
77 # enforce correct pointer usage
78 KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
79
80 # Require designated initializers for all marked structures
81 KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
82
83 # Warn if there is an enum types mismatch
84 KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
85
86 # backward compatibility
87 KBUILD_EXTRA_WARN ?= $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)
88
89 ifeq ("$(origin W)", "command line")
90   KBUILD_EXTRA_WARN := $(W)
91 endif
92
93 export KBUILD_EXTRA_WARN
94
95 #
96 # W=1 - warnings which may be relevant and do not occur too often
97 #
98 ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
99
100 KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter
101 KBUILD_CFLAGS += -Wmissing-declarations
102 KBUILD_CFLAGS += -Wmissing-format-attribute
103 KBUILD_CFLAGS += -Wmissing-prototypes
104 KBUILD_CFLAGS += -Wold-style-definition
105 KBUILD_CFLAGS += -Wmissing-include-dirs
106 KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable)
107 KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
108 KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned)
109 KBUILD_CFLAGS += $(call cc-option, -Wformat-overflow)
110 KBUILD_CFLAGS += $(call cc-option, -Wformat-truncation)
111 KBUILD_CFLAGS += $(call cc-option, -Wstringop-overflow)
112 KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation)
113
114 KBUILD_CPPFLAGS += -Wundef
115 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
116
117 else
118
119 # Some diagnostics enabled by default are noisy.
120 # Suppress them by using -Wno... except for W=1.
121 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
122 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
123 KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
124 KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
125 KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
126 KBUILD_CFLAGS += $(call cc-disable-warning, stringop-overflow)
127 KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
128
129 ifdef CONFIG_CC_IS_CLANG
130 # Clang before clang-16 would warn on default argument promotions.
131 ifneq ($(call clang-min-version, 160000),y)
132 # Disable -Wformat
133 KBUILD_CFLAGS += -Wno-format
134 # Then re-enable flags that were part of the -Wformat group that aren't
135 # problematic.
136 KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier
137 KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull
138 # Requires clang-12+.
139 ifeq ($(call clang-min-version, 120000),y)
140 KBUILD_CFLAGS += -Wformat-insufficient-args
141 endif
142 endif
143 KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
144 KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
145 KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access)
146 KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict)
147 endif
148
149 endif
150
151 #
152 # W=2 - warnings which occur quite often but may still be relevant
153 #
154 ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
155
156 KBUILD_CFLAGS += -Wdisabled-optimization
157 KBUILD_CFLAGS += -Wshadow
158 KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
159 KBUILD_CFLAGS += -Wmissing-field-initializers
160 KBUILD_CFLAGS += -Wtype-limits
161 KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized)
162 KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
163
164 ifdef CONFIG_CC_IS_CLANG
165 KBUILD_CFLAGS += -Winitializer-overrides
166 endif
167
168 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
169
170 else
171
172 # The following turn off the warnings enabled by -Wextra
173 KBUILD_CFLAGS += -Wno-missing-field-initializers
174 KBUILD_CFLAGS += -Wno-type-limits
175 KBUILD_CFLAGS += -Wno-shift-negative-value
176
177 ifdef CONFIG_CC_IS_CLANG
178 KBUILD_CFLAGS += -Wno-initializer-overrides
179 else
180 KBUILD_CFLAGS += -Wno-maybe-uninitialized
181 endif
182
183 endif
184
185 #
186 # W=3 - more obscure warnings, can most likely be ignored
187 #
188 ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
189
190 KBUILD_CFLAGS += -Wbad-function-cast
191 KBUILD_CFLAGS += -Wcast-align
192 KBUILD_CFLAGS += -Wcast-qual
193 KBUILD_CFLAGS += -Wconversion
194 KBUILD_CFLAGS += -Wpacked
195 KBUILD_CFLAGS += -Wpadded
196 KBUILD_CFLAGS += -Wpointer-arith
197 KBUILD_CFLAGS += -Wredundant-decls
198 KBUILD_CFLAGS += -Wsign-compare
199 KBUILD_CFLAGS += -Wswitch-default
200 KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat)
201
202 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
203
204 else
205
206 # The following turn off the warnings enabled by -Wextra
207 KBUILD_CFLAGS += -Wno-sign-compare
208
209 endif
210
211 #
212 # W=e - error out on warnings
213 #
214 ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),)
215
216 KBUILD_CFLAGS += -Werror
217
218 endif