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