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
9KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
10
11# backward compatibility
12KBUILD_EXTRA_WARN ?= $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)
13
14ifeq ("$(origin W)", "command line")
15  KBUILD_EXTRA_WARN := $(W)
16endif
17
18export KBUILD_EXTRA_WARN
19
20#
21# W=1 - warnings which may be relevant and do not occur too often
22#
23ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
24
25KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter
26KBUILD_CFLAGS += -Wmissing-declarations
27KBUILD_CFLAGS += -Wmissing-format-attribute
28KBUILD_CFLAGS += -Wmissing-prototypes
29KBUILD_CFLAGS += -Wold-style-definition
30KBUILD_CFLAGS += -Wmissing-include-dirs
31KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable)
32KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
33KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned)
34KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation)
35# The following turn off the warnings enabled by -Wextra
36KBUILD_CFLAGS += -Wno-missing-field-initializers
37KBUILD_CFLAGS += -Wno-sign-compare
38KBUILD_CFLAGS += -Wno-type-limits
39KBUILD_CFLAGS += -Wno-shift-negative-value
40
41KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
42
43else
44
45# Some diagnostics enabled by default are noisy.
46# Suppress them by using -Wno... except for W=1.
47
48ifdef CONFIG_CC_IS_CLANG
49KBUILD_CFLAGS += -Wno-initializer-overrides
50# Clang before clang-16 would warn on default argument promotions.
51ifneq ($(call clang-min-version, 160000),y)
52# Disable -Wformat
53KBUILD_CFLAGS += -Wno-format
54# Then re-enable flags that were part of the -Wformat group that aren't
55# problematic.
56KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier
57KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull
58# Requires clang-12+.
59ifeq ($(call clang-min-version, 120000),y)
60KBUILD_CFLAGS += -Wformat-insufficient-args
61endif
62endif
63KBUILD_CFLAGS += -Wno-sign-compare
64KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
65KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
66KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access)
67KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict)
68endif
69
70endif
71
72#
73# W=2 - warnings which occur quite often but may still be relevant
74#
75ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
76
77KBUILD_CFLAGS += -Wdisabled-optimization
78KBUILD_CFLAGS += -Wshadow
79KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
80KBUILD_CFLAGS += -Wmissing-field-initializers
81KBUILD_CFLAGS += -Wtype-limits
82KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized)
83KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
84
85KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
86
87endif
88
89#
90# W=3 - more obscure warnings, can most likely be ignored
91#
92ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
93
94KBUILD_CFLAGS += -Wbad-function-cast
95KBUILD_CFLAGS += -Wcast-align
96KBUILD_CFLAGS += -Wcast-qual
97KBUILD_CFLAGS += -Wconversion
98KBUILD_CFLAGS += -Wpacked
99KBUILD_CFLAGS += -Wpadded
100KBUILD_CFLAGS += -Wpointer-arith
101KBUILD_CFLAGS += -Wredundant-decls
102KBUILD_CFLAGS += -Wsign-compare
103KBUILD_CFLAGS += -Wswitch-default
104KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat)
105
106KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
107
108endif
109
110#
111# W=e - error out on warnings
112#
113ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),)
114
115KBUILD_CFLAGS += -Werror
116
117endif
118