blob: 1d13cecc7cc7808610e635ddc03476cf92b3a8c1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001# SPDX-License-Identifier: GPL-2.0
Masahiro Yamadaa86fe352014-04-14 18:27:10 +09002# ==========================================================================
Masahiro Yamadaa86fe352014-04-14 18:27:10 +09003# make W=... settings
4#
Yann Droneaudc77d06e2022-04-08 10:46:07 +02005# 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.
Masahiro Yamadaa86fe352014-04-14 18:27:10 +09007# ==========================================================================
8
Arnd Bergmanne88ca242023-08-11 16:03:20 +02009# 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,)
Arnd Bergmanne88ca242023-08-11 16:03:20 +020019KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
Arnd Bergmann0fcb7082023-11-23 12:05:06 +010020KBUILD_CFLAGS += -Wmissing-declarations
21KBUILD_CFLAGS += -Wmissing-prototypes
Arnd Bergmanne88ca242023-08-11 16:03:20 +020022
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
Arnd Bergmanne88ca242023-08-11 16:03:20 +020040# 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
Arnd Bergmanne88ca242023-08-11 16:03:20 +020054# 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
Arnd Bergmannf5982cc2024-04-15 14:20:32 +020080KBUILD_CFLAGS += -Wextra
81KBUILD_CFLAGS += -Wunused
82
Masahiro Yamada64a91902019-09-01 01:25:54 +090083#
84# W=1 - warnings which may be relevant and do not occur too often
85#
Masahiro Yamadae27128d2019-09-01 01:25:55 +090086ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
Masahiro Yamadaa86fe352014-04-14 18:27:10 +090087
Masahiro Yamada64a91902019-09-01 01:25:54 +090088KBUILD_CFLAGS += -Wmissing-format-attribute
Masahiro Yamada64a91902019-09-01 01:25:54 +090089KBUILD_CFLAGS += -Wmissing-include-dirs
Masahiro Yamada64a91902019-09-01 01:25:54 +090090KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
Masahiro Yamadaa86fe352014-04-14 18:27:10 +090091
Masahiro Yamada80b60932022-12-06 13:07:31 +090092KBUILD_CPPFLAGS += -Wundef
Masahiro Yamada6863f562019-09-07 11:52:36 +090093KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
94
Behan Webster26ea6bb2014-07-31 21:08:25 -070095else
96
Masahiro Yamada64a91902019-09-01 01:25:54 +090097# Some diagnostics enabled by default are noisy.
98# Suppress them by using -Wno... except for W=1.
Arnd Bergmann2cd32712023-08-11 16:03:21 +020099KBUILD_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)
Arnd Bergmann6d4ab2e2023-08-11 16:03:23 +0200102KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
Arnd Bergmann908dd502024-04-15 14:20:35 +0200103ifdef CONFIG_CC_IS_GCC
Arnd Bergmann6d4ab2e2023-08-11 16:03:23 +0200104KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
Arnd Bergmann908dd502024-04-15 14:20:35 +0200105else
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
Arnd Bergmann2cd32712023-08-11 16:03:21 +0200111KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
Masahiro Yamada64a91902019-09-01 01:25:54 +0900112
Arnd Bergmannc40845e2024-03-26 15:47:16 +0100113KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
114
Masahiro Yamada076f4212018-10-30 22:26:33 +0900115ifdef CONFIG_CC_IS_CLANG
Nick Desaulniersb0839b282022-09-01 10:59:13 -0700116# Clang before clang-16 would warn on default argument promotions.
Nick Desaulniers88b61e32022-09-19 10:08:28 -0700117ifneq ($(call clang-min-version, 160000),y)
Nick Desaulniersb0839b282022-09-01 10:59:13 -0700118# Disable -Wformat
Linus Torvalds21f9c8a2022-08-11 08:40:01 -0700119KBUILD_CFLAGS += -Wno-format
Nick Desaulniersb0839b282022-09-01 10:59:13 -0700120# 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+.
Nick Desaulniers88b61e32022-09-19 10:08:28 -0700125ifeq ($(call clang-min-version, 120000),y)
Nick Desaulniersb0839b282022-09-01 10:59:13 -0700126KBUILD_CFLAGS += -Wformat-insufficient-args
127endif
128endif
Nathan Chancellor82f2bc22020-03-11 12:41:21 -0700129KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
Nathan Chancellorafe956c2020-03-26 12:41:55 -0700130KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
Nathan Chancellor1cf5f152022-02-02 16:05:16 -0700131KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access)
Nathan Chancellor75b5ab12024-03-05 15:12:47 -0700132KBUILD_CFLAGS += -Wno-enum-compare-conditional
133KBUILD_CFLAGS += -Wno-enum-enum-conversion
Behan Webster26ea6bb2014-07-31 21:08:25 -0700134endif
Masahiro Yamada64a91902019-09-01 01:25:54 +0900135
136endif
137
138#
139# W=2 - warnings which occur quite often but may still be relevant
140#
Masahiro Yamadae27128d2019-09-01 01:25:55 +0900141ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
Masahiro Yamada64a91902019-09-01 01:25:54 +0900142
Masahiro Yamada64a91902019-09-01 01:25:54 +0900143KBUILD_CFLAGS += -Wdisabled-optimization
Masahiro Yamada64a91902019-09-01 01:25:54 +0900144KBUILD_CFLAGS += -Wshadow
145KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
Masahiro Yamada64a91902019-09-01 01:25:54 +0900146KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
147
Masahiro Yamada6863f562019-09-07 11:52:36 +0900148KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
149
Arnd Bergmann2cd32712023-08-11 16:03:21 +0200150else
151
152# The following turn off the warnings enabled by -Wextra
153KBUILD_CFLAGS += -Wno-missing-field-initializers
154KBUILD_CFLAGS += -Wno-type-limits
155KBUILD_CFLAGS += -Wno-shift-negative-value
156
Arnd Bergmannc40845e2024-03-26 15:47:16 +0100157ifdef CONFIG_CC_IS_GCC
Arnd Bergmann2cd32712023-08-11 16:03:21 +0200158KBUILD_CFLAGS += -Wno-maybe-uninitialized
159endif
160
Masahiro Yamada64a91902019-09-01 01:25:54 +0900161endif
162
163#
164# W=3 - more obscure warnings, can most likely be ignored
165#
Masahiro Yamadae27128d2019-09-01 01:25:55 +0900166ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
Masahiro Yamada64a91902019-09-01 01:25:54 +0900167
168KBUILD_CFLAGS += -Wbad-function-cast
Arnd Bergmann095fbca2020-10-26 23:03:13 +0100169KBUILD_CFLAGS += -Wcast-align
Masahiro Yamada64a91902019-09-01 01:25:54 +0900170KBUILD_CFLAGS += -Wcast-qual
171KBUILD_CFLAGS += -Wconversion
172KBUILD_CFLAGS += -Wpacked
173KBUILD_CFLAGS += -Wpadded
174KBUILD_CFLAGS += -Wpointer-arith
175KBUILD_CFLAGS += -Wredundant-decls
Joe Perchesa97ea932020-07-21 21:57:43 -0700176KBUILD_CFLAGS += -Wsign-compare
Masahiro Yamada64a91902019-09-01 01:25:54 +0900177KBUILD_CFLAGS += -Wswitch-default
Masahiro Yamada64a91902019-09-01 01:25:54 +0900178
Masahiro Yamada6863f562019-09-07 11:52:36 +0900179KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
180
Arnd Bergmann2cd32712023-08-11 16:03:21 +0200181else
182
183# The following turn off the warnings enabled by -Wextra
184KBUILD_CFLAGS += -Wno-sign-compare
Arnd Bergmannf5982cc2024-04-15 14:20:32 +0200185KBUILD_CFLAGS += -Wno-unused-parameter
Arnd Bergmann2cd32712023-08-11 16:03:21 +0200186
Masahiro Yamadaa86fe352014-04-14 18:27:10 +0900187endif
Yann Droneaudc77d06e2022-04-08 10:46:07 +0200188
189#
190# W=e - error out on warnings
191#
192ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),)
193
194KBUILD_CFLAGS += -Werror
195
196endif