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.
Unified · Split
| @@ -1,14 +1,9 @@ | ||
| 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) |
| @@ -17,58 +12,69 @@ ifeq ("$(origin W)", "command line") · 2 unmodified lines | ||
| 17 | 12 | export KBUILD_ENABLE_EXTRA_GCC_CHECKS := $(W) |
| 18 | 13 | endif |
| 19 | 14 | |
| 20 | −ifdef KBUILD_ENABLE_EXTRA_GCC_CHECKS | |
| 21 | −warning- := $(empty) | |
| 15 | +# | |
| 16 | +# W=1 - warnings which may be relevant and do not occur too often | |
| 17 | +# | |
| 18 | +ifneq ($(findstring 1, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)),) | |
| 22 | 19 | |
| 23 | −warning-1 := -Wextra -Wunused -Wno-unused-parameter | |
| 24 | −warning-1 += -Wmissing-declarations | |
| 25 | −warning-1 += -Wmissing-format-attribute | |
| 26 | −warning-1 += -Wmissing-prototypes | |
| 27 | −warning-1 += -Wold-style-definition | |
| 28 | −warning-1 += -Wmissing-include-dirs | |
| 29 | −warning-1 += $(call cc-option, -Wunused-but-set-variable) | |
| 30 | −warning-1 += $(call cc-option, -Wunused-const-variable) | |
| 31 | −warning-1 += $(call cc-option, -Wpacked-not-aligned) | |
| 32 | −warning-1 += $(call cc-option, -Wstringop-truncation) | |
| 20 | +KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter | |
| 21 | +KBUILD_CFLAGS += -Wmissing-declarations | |
| 22 | +KBUILD_CFLAGS += -Wmissing-format-attribute | |
| 23 | +KBUILD_CFLAGS += -Wmissing-prototypes | |
| 24 | +KBUILD_CFLAGS += -Wold-style-definition | |
| 25 | +KBUILD_CFLAGS += -Wmissing-include-dirs | |
| 26 | +KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable) | |
| 27 | +KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) | |
| 28 | +KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned) | |
| 29 | +KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation) | |
| 33 | 30 | # The following turn off the warnings enabled by -Wextra |
| 34 | −warning-1 += -Wno-missing-field-initializers | |
| 35 | −warning-1 += -Wno-sign-compare | |
| 36 | − | |
| 37 | −warning-2 += -Wcast-align | |
| 38 | −warning-2 += -Wdisabled-optimization | |
| 39 | −warning-2 += -Wnested-externs | |
| 40 | −warning-2 += -Wshadow | |
| 41 | −warning-2 += $(call cc-option, -Wlogical-op) | |
| 42 | −warning-2 += -Wmissing-field-initializers | |
| 43 | −warning-2 += -Wsign-compare | |
| 44 | −warning-2 += $(call cc-option, -Wmaybe-uninitialized) | |
| 45 | −warning-2 += $(call cc-option, -Wunused-macros) | |
| 46 | − | |
| 47 | −warning-3 := -Wbad-function-cast | |
| 48 | −warning-3 += -Wcast-qual | |
| 49 | −warning-3 += -Wconversion | |
| 50 | −warning-3 += -Wpacked | |
| 51 | −warning-3 += -Wpadded | |
| 52 | −warning-3 += -Wpointer-arith | |
| 53 | −warning-3 += -Wredundant-decls | |
| 54 | −warning-3 += -Wswitch-default | |
| 55 | −warning-3 += $(call cc-option, -Wpacked-bitfield-compat) | |
| 56 | − | |
| 57 | −warning := $(warning-$(findstring 1, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS))) | |
| 58 | −warning += $(warning-$(findstring 2, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS))) | |
| 59 | −warning += $(warning-$(findstring 3, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS))) | |
| 60 | − | |
| 61 | −ifeq ("$(strip $(warning))","") | |
| 62 | − $(error W=$(KBUILD_ENABLE_EXTRA_GCC_CHECKS) is unknown) | |
| 63 | −endif | |
| 31 | +KBUILD_CFLAGS += -Wno-missing-field-initializers | |
| 32 | +KBUILD_CFLAGS += -Wno-sign-compare | |
| 64 | 33 | |
| 65 | −KBUILD_CFLAGS += $(warning) | |
| 66 | 34 | else |
| 67 | 35 | |
| 36 | +# Some diagnostics enabled by default are noisy. | |
| 37 | +# Suppress them by using -Wno... except for W=1. | |
| 38 | + | |
| 68 | 39 | ifdef CONFIG_CC_IS_CLANG |
| 69 | 40 | KBUILD_CFLAGS += -Wno-initializer-overrides |
| 70 | 41 | KBUILD_CFLAGS += -Wno-format |
| 71 | 42 | KBUILD_CFLAGS += -Wno-sign-compare |
| 72 | 43 | KBUILD_CFLAGS += -Wno-format-zero-length |
| 73 | 44 | endif |
| 45 | + | |
| 46 | +endif | |
| 47 | + | |
| 48 | +# | |
| 49 | +# W=2 - warnings which occur quite often but may still be relevant | |
| 50 | +# | |
| 51 | +ifneq ($(findstring 2, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)),) | |
| 52 | + | |
| 53 | +KBUILD_CFLAGS += -Wcast-align | |
| 54 | +KBUILD_CFLAGS += -Wdisabled-optimization | |
| 55 | +KBUILD_CFLAGS += -Wnested-externs | |
| 56 | +KBUILD_CFLAGS += -Wshadow | |
| 57 | +KBUILD_CFLAGS += $(call cc-option, -Wlogical-op) | |
| 58 | +KBUILD_CFLAGS += -Wmissing-field-initializers | |
| 59 | +KBUILD_CFLAGS += -Wsign-compare | |
| 60 | +KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized) | |
| 61 | +KBUILD_CFLAGS += $(call cc-option, -Wunused-macros) | |
| 62 | + | |
| 63 | +endif | |
| 64 | + | |
| 65 | +# | |
| 66 | +# W=3 - more obscure warnings, can most likely be ignored | |
| 67 | +# | |
| 68 | +ifneq ($(findstring 3, $(KBUILD_ENABLE_EXTRA_GCC_CHECKS)),) | |
| 69 | + | |
| 70 | +KBUILD_CFLAGS += -Wbad-function-cast | |
| 71 | +KBUILD_CFLAGS += -Wcast-qual | |
| 72 | +KBUILD_CFLAGS += -Wconversion | |
| 73 | +KBUILD_CFLAGS += -Wpacked | |
| 74 | +KBUILD_CFLAGS += -Wpadded | |
| 75 | +KBUILD_CFLAGS += -Wpointer-arith | |
| 76 | +KBUILD_CFLAGS += -Wredundant-decls | |
| 77 | +KBUILD_CFLAGS += -Wswitch-default | |
| 78 | +KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat) | |
| 79 | + | |
| 74 | 80 | endif |