162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci# ==========================================================================
362306a36Sopenharmony_ci# make W=... settings
462306a36Sopenharmony_ci#
562306a36Sopenharmony_ci# There are four warning groups enabled by W=1, W=2, W=3, and W=e
662306a36Sopenharmony_ci# They are independent, and can be combined like W=12 or W=123e.
762306a36Sopenharmony_ci# ==========================================================================
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci# Default set of warnings, always enabled
1062306a36Sopenharmony_ciKBUILD_CFLAGS += -Wall
1162306a36Sopenharmony_ciKBUILD_CFLAGS += -Wundef
1262306a36Sopenharmony_ciKBUILD_CFLAGS += -Werror=implicit-function-declaration
1362306a36Sopenharmony_ciKBUILD_CFLAGS += -Werror=implicit-int
1462306a36Sopenharmony_ciKBUILD_CFLAGS += -Werror=return-type
1562306a36Sopenharmony_ciKBUILD_CFLAGS += -Werror=strict-prototypes
1662306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-format-security
1762306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-trigraphs
1862306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning,frame-address,)
1962306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciifneq ($(CONFIG_FRAME_WARN),0)
2262306a36Sopenharmony_ciKBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
2362306a36Sopenharmony_ciendif
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciKBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror
2662306a36Sopenharmony_ciKBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y)
2762306a36Sopenharmony_ciKBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciifdef CONFIG_CC_IS_CLANG
3062306a36Sopenharmony_ci# The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable.
3162306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-gnu
3262306a36Sopenharmony_cielse
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci# gcc inanely warns about local variables called 'main'
3562306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-main
3662306a36Sopenharmony_ciendif
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci# These warnings generated too much noise in a regular build.
3962306a36Sopenharmony_ci# Use make W=1 to enable them (see scripts/Makefile.extrawarn)
4062306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
4162306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci# These result in bogus false positives
4462306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci# Variable Length Arrays (VLAs) should not be used anywhere in the kernel
4762306a36Sopenharmony_ciKBUILD_CFLAGS += -Wvla
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci# disable pointer signed / unsigned warnings in gcc 4.0
5062306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-pointer-sign
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci# In order to make sure new function cast mismatches are not introduced
5362306a36Sopenharmony_ci# in the kernel (to avoid tripping CFI checking), the kernel should be
5462306a36Sopenharmony_ci# globally built with -Wcast-function-type.
5562306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wcast-function-type)
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci# The allocators already balk at large sizes, so silence the compiler
5862306a36Sopenharmony_ci# warnings for bounds checks involving those possible values. While
5962306a36Sopenharmony_ci# -Wno-alloc-size-larger-than would normally be used here, earlier versions
6062306a36Sopenharmony_ci# of gcc (<9.1) weirdly don't handle the option correctly when _other_
6162306a36Sopenharmony_ci# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
6262306a36Sopenharmony_ci# doesn't work (as it is documented to), silently resolving to "0" prior to
6362306a36Sopenharmony_ci# version 9.1 (and producing an error more recently). Numeric values larger
6462306a36Sopenharmony_ci# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
6562306a36Sopenharmony_ci# ignored, continuing to default to PTRDIFF_MAX. So, left with no other
6662306a36Sopenharmony_ci# choice, we must perform a versioned check to disable this warning.
6762306a36Sopenharmony_ci# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au
6862306a36Sopenharmony_ciKBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
6962306a36Sopenharmony_ciKBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci# Prohibit date/time macros, which would make the build non-deterministic
7262306a36Sopenharmony_ciKBUILD_CFLAGS += -Werror=date-time
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci# enforce correct pointer usage
7562306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci# Require designated initializers for all marked structures
7862306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci# Warn if there is an enum types mismatch
8162306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci# backward compatibility
8462306a36Sopenharmony_ciKBUILD_EXTRA_WARN ?= $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ciifeq ("$(origin W)", "command line")
8762306a36Sopenharmony_ci  KBUILD_EXTRA_WARN := $(W)
8862306a36Sopenharmony_ciendif
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciexport KBUILD_EXTRA_WARN
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#
9362306a36Sopenharmony_ci# W=1 - warnings which may be relevant and do not occur too often
9462306a36Sopenharmony_ci#
9562306a36Sopenharmony_ciifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciKBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter
9862306a36Sopenharmony_ciKBUILD_CFLAGS += -Wmissing-declarations
9962306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wrestrict)
10062306a36Sopenharmony_ciKBUILD_CFLAGS += -Wmissing-format-attribute
10162306a36Sopenharmony_ciKBUILD_CFLAGS += -Wmissing-prototypes
10262306a36Sopenharmony_ciKBUILD_CFLAGS += -Wold-style-definition
10362306a36Sopenharmony_ciKBUILD_CFLAGS += -Wmissing-include-dirs
10462306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable)
10562306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
10662306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned)
10762306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wformat-overflow)
10862306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wformat-truncation)
10962306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wstringop-overflow)
11062306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation)
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciKBUILD_CPPFLAGS += -Wundef
11362306a36Sopenharmony_ciKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cielse
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci# Some diagnostics enabled by default are noisy.
11862306a36Sopenharmony_ci# Suppress them by using -Wno... except for W=1.
11962306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
12062306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
12162306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, restrict)
12262306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
12362306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
12462306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
12562306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, stringop-overflow)
12662306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciifdef CONFIG_CC_IS_CLANG
12962306a36Sopenharmony_ci# Clang before clang-16 would warn on default argument promotions.
13062306a36Sopenharmony_ciifneq ($(call clang-min-version, 160000),y)
13162306a36Sopenharmony_ci# Disable -Wformat
13262306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-format
13362306a36Sopenharmony_ci# Then re-enable flags that were part of the -Wformat group that aren't
13462306a36Sopenharmony_ci# problematic.
13562306a36Sopenharmony_ciKBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier
13662306a36Sopenharmony_ciKBUILD_CFLAGS += -Wformat-zero-length -Wnonnull
13762306a36Sopenharmony_ci# Requires clang-12+.
13862306a36Sopenharmony_ciifeq ($(call clang-min-version, 120000),y)
13962306a36Sopenharmony_ciKBUILD_CFLAGS += -Wformat-insufficient-args
14062306a36Sopenharmony_ciendif
14162306a36Sopenharmony_ciendif
14262306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
14362306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
14462306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access)
14562306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict)
14662306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-enum-compare-conditional
14762306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-enum-enum-conversion
14862306a36Sopenharmony_ciendif
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ciendif
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci#
15362306a36Sopenharmony_ci# W=2 - warnings which occur quite often but may still be relevant
15462306a36Sopenharmony_ci#
15562306a36Sopenharmony_ciifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ciKBUILD_CFLAGS += -Wdisabled-optimization
15862306a36Sopenharmony_ciKBUILD_CFLAGS += -Wshadow
15962306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
16062306a36Sopenharmony_ciKBUILD_CFLAGS += -Wmissing-field-initializers
16162306a36Sopenharmony_ciKBUILD_CFLAGS += -Wtype-limits
16262306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized)
16362306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciifdef CONFIG_CC_IS_CLANG
16662306a36Sopenharmony_ciKBUILD_CFLAGS += -Winitializer-overrides
16762306a36Sopenharmony_ciendif
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ciKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_cielse
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci# The following turn off the warnings enabled by -Wextra
17462306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-missing-field-initializers
17562306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-type-limits
17662306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-shift-negative-value
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ciifdef CONFIG_CC_IS_CLANG
17962306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-initializer-overrides
18062306a36Sopenharmony_cielse
18162306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-maybe-uninitialized
18262306a36Sopenharmony_ciendif
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ciendif
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci#
18762306a36Sopenharmony_ci# W=3 - more obscure warnings, can most likely be ignored
18862306a36Sopenharmony_ci#
18962306a36Sopenharmony_ciifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ciKBUILD_CFLAGS += -Wbad-function-cast
19262306a36Sopenharmony_ciKBUILD_CFLAGS += -Wcast-align
19362306a36Sopenharmony_ciKBUILD_CFLAGS += -Wcast-qual
19462306a36Sopenharmony_ciKBUILD_CFLAGS += -Wconversion
19562306a36Sopenharmony_ciKBUILD_CFLAGS += -Wpacked
19662306a36Sopenharmony_ciKBUILD_CFLAGS += -Wpadded
19762306a36Sopenharmony_ciKBUILD_CFLAGS += -Wpointer-arith
19862306a36Sopenharmony_ciKBUILD_CFLAGS += -Wredundant-decls
19962306a36Sopenharmony_ciKBUILD_CFLAGS += -Wsign-compare
20062306a36Sopenharmony_ciKBUILD_CFLAGS += -Wswitch-default
20162306a36Sopenharmony_ciKBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat)
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ciKBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_cielse
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci# The following turn off the warnings enabled by -Wextra
20862306a36Sopenharmony_ciKBUILD_CFLAGS += -Wno-sign-compare
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ciendif
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci#
21362306a36Sopenharmony_ci# W=e - error out on warnings
21462306a36Sopenharmony_ci#
21562306a36Sopenharmony_ciifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),)
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ciKBUILD_CFLAGS += -Werror
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ciendif
220