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