Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kbuild: refactor scripts/Makefile.extrawarn

Instead of the warning-[123] magic, let's accumulate compiler options
to KBUILD_CFLAGS directly as the top Makefile does. I think this makes
it easier to understand what is going on in this file.

This commit slightly changes the behavior, I think all of which are OK.

[1] Currently, cc-option calls are needlessly evaluated. For example,
warning-3 += $(call cc-option, -Wpacked-bitfield-compat)
needs evaluating only when W=3, but it is actually evaluated for
W=1, W=2 as well. With this commit, only relevant cc-option calls
will be evaluated. This is a slight optimization.

[2] Currently, unsupported level like W=4 is checked by:
$(error W=$(KBUILD_ENABLE_EXTRA_GCC_CHECKS) is unknown)
This will no longer be checked, but I do not think it is a big
deal.

[3] Currently, 4 Clang warnings (Winitializer-overrides, Wformat,
Wsign-compare, Wformat-zero-length) are shown by any of W=1, W=2,
and W=3. With this commit, they will be warned only by W=1. I
think this is a more correct behavior since each warning belongs
to only one group.

For understanding this commit correctly:

We have 3 warning groups, W=1, W=2, and W=3. You may think W=3 has a
higher level than W=1, but they are actually independent. If you like,
you can combine them like W=13. To enable all the warnings, you can
pass W=123. It is shown by 'make help', but not noticed much. Since we
support W= combination, there should not exist intersection among the
three groups. If we enable Winitializer-overrides for W=1, we do not
need to for W=2 or W=3. This is the reason why I think the change [3]
makes sense.

The documentation says -Winitializer-overrides is enabled by default.
(https://clang.llvm.org/docs/DiagnosticsReference.html#winitializer-overrides)
We negate it by passing -Wno-initializer-overrides for the normal
build, but we do not do that for W=1. This means, W=1 effectively
enables -Winitializer-overrides by the clang's default. The same for
the other three.

Add comments in case people are confused with the code.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Nathan Chancellor <natechancellor@gmail.com>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
Acked-by: Nick Desaulniers <ndesaulniers@google.com>
Acked-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>

+56 -50
+56 -50
scripts/Makefile.extrawarn
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 2 # ========================================================================== 3 - # 4 3 # make W=... settings 5 4 # 6 - # W=1 - warnings that may be relevant and does not occur too often 7 - # W=2 - warnings that occur quite often but may still be relevant 8 - # W=3 - the more obscure warnings, can most likely be ignored 9 - # 10 - # $(call cc-option, -W...) handles gcc -W.. options which 11 - # are not supported by all versions of the compiler 5 + # There are three warning groups enabled by W=1, W=2, W=3. 6 + # They are independent, and can be combined like W=12 or W=123. 12 7 # ========================================================================== 13 8 14 9 KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned) ··· 12 17 export KBUILD_ENABLE_EXTRA_GCC_CHECKS := $(W) 13 18 endif 14 19 15 - ifdef KBUILD_ENABLE_EXTRA_GCC_CHECKS 16 - warning- := $(empty) 20 + # 21 + # W=1 - warnings which may be relevant and do not occur too often 22 + # 23 + ifneq ($(findstring 1, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)),) 17 24 18 - warning-1 := -Wextra -Wunused -Wno-unused-parameter 19 - warning-1 += -Wmissing-declarations 20 - warning-1 += -Wmissing-format-attribute 21 - warning-1 += -Wmissing-prototypes 22 - warning-1 += -Wold-style-definition 23 - warning-1 += -Wmissing-include-dirs 24 - warning-1 += $(call cc-option, -Wunused-but-set-variable) 25 - warning-1 += $(call cc-option, -Wunused-const-variable) 26 - warning-1 += $(call cc-option, -Wpacked-not-aligned) 27 - warning-1 += $(call cc-option, -Wstringop-truncation) 25 + KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter 26 + KBUILD_CFLAGS += -Wmissing-declarations 27 + KBUILD_CFLAGS += -Wmissing-format-attribute 28 + KBUILD_CFLAGS += -Wmissing-prototypes 29 + KBUILD_CFLAGS += -Wold-style-definition 30 + KBUILD_CFLAGS += -Wmissing-include-dirs 31 + KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable) 32 + KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) 33 + KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned) 34 + KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation) 28 35 # The following turn off the warnings enabled by -Wextra 29 - warning-1 += -Wno-missing-field-initializers 30 - warning-1 += -Wno-sign-compare 36 + KBUILD_CFLAGS += -Wno-missing-field-initializers 37 + KBUILD_CFLAGS += -Wno-sign-compare 31 38 32 - warning-2 += -Wcast-align 33 - warning-2 += -Wdisabled-optimization 34 - warning-2 += -Wnested-externs 35 - warning-2 += -Wshadow 36 - warning-2 += $(call cc-option, -Wlogical-op) 37 - warning-2 += -Wmissing-field-initializers 38 - warning-2 += -Wsign-compare 39 - warning-2 += $(call cc-option, -Wmaybe-uninitialized) 40 - warning-2 += $(call cc-option, -Wunused-macros) 41 - 42 - warning-3 := -Wbad-function-cast 43 - warning-3 += -Wcast-qual 44 - warning-3 += -Wconversion 45 - warning-3 += -Wpacked 46 - warning-3 += -Wpadded 47 - warning-3 += -Wpointer-arith 48 - warning-3 += -Wredundant-decls 49 - warning-3 += -Wswitch-default 50 - warning-3 += $(call cc-option, -Wpacked-bitfield-compat) 51 - 52 - warning := $(warning-$(findstring 1, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS))) 53 - warning += $(warning-$(findstring 2, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS))) 54 - warning += $(warning-$(findstring 3, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS))) 55 - 56 - ifeq ("$(strip $(warning))","") 57 - $(error W=$(KBUILD_ENABLE_EXTRA_GCC_CHECKS) is unknown) 58 - endif 59 - 60 - KBUILD_CFLAGS += $(warning) 61 39 else 40 + 41 + # Some diagnostics enabled by default are noisy. 42 + # Suppress them by using -Wno... except for W=1. 62 43 63 44 ifdef CONFIG_CC_IS_CLANG 64 45 KBUILD_CFLAGS += -Wno-initializer-overrides ··· 42 71 KBUILD_CFLAGS += -Wno-sign-compare 43 72 KBUILD_CFLAGS += -Wno-format-zero-length 44 73 endif 74 + 75 + endif 76 + 77 + # 78 + # W=2 - warnings which occur quite often but may still be relevant 79 + # 80 + ifneq ($(findstring 2, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)),) 81 + 82 + KBUILD_CFLAGS += -Wcast-align 83 + KBUILD_CFLAGS += -Wdisabled-optimization 84 + KBUILD_CFLAGS += -Wnested-externs 85 + KBUILD_CFLAGS += -Wshadow 86 + KBUILD_CFLAGS += $(call cc-option, -Wlogical-op) 87 + KBUILD_CFLAGS += -Wmissing-field-initializers 88 + KBUILD_CFLAGS += -Wsign-compare 89 + KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized) 90 + KBUILD_CFLAGS += $(call cc-option, -Wunused-macros) 91 + 92 + endif 93 + 94 + # 95 + # W=3 - more obscure warnings, can most likely be ignored 96 + # 97 + ifneq ($(findstring 3, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)),) 98 + 99 + KBUILD_CFLAGS += -Wbad-function-cast 100 + KBUILD_CFLAGS += -Wcast-qual 101 + KBUILD_CFLAGS += -Wconversion 102 + KBUILD_CFLAGS += -Wpacked 103 + KBUILD_CFLAGS += -Wpadded 104 + KBUILD_CFLAGS += -Wpointer-arith 105 + KBUILD_CFLAGS += -Wredundant-decls 106 + KBUILD_CFLAGS += -Wswitch-default 107 + KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat) 108 + 45 109 endif