1 # SPDX-License-Identifier: GPL-2.0 1 # SPDX-License-Identifier: GPL-2.0 2 # ============================================ 2 # ========================================================================== 3 # make W=... settings 3 # make W=... settings 4 # 4 # 5 # There are four warning groups enabled by W=1 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 li 6 # They are independent, and can be combined like W=12 or W=123e. 7 # ============================================ 7 # ========================================================================== 8 8 9 # Default set of warnings, always enabled 9 # Default set of warnings, always enabled 10 KBUILD_CFLAGS += -Wall 10 KBUILD_CFLAGS += -Wall 11 KBUILD_CFLAGS += -Wundef 11 KBUILD_CFLAGS += -Wundef 12 KBUILD_CFLAGS += -Werror=implicit-function-dec 12 KBUILD_CFLAGS += -Werror=implicit-function-declaration 13 KBUILD_CFLAGS += -Werror=implicit-int 13 KBUILD_CFLAGS += -Werror=implicit-int 14 KBUILD_CFLAGS += -Werror=return-type 14 KBUILD_CFLAGS += -Werror=return-type 15 KBUILD_CFLAGS += -Werror=strict-prototypes 15 KBUILD_CFLAGS += -Werror=strict-prototypes 16 KBUILD_CFLAGS += -Wno-format-security 16 KBUILD_CFLAGS += -Wno-format-security 17 KBUILD_CFLAGS += -Wno-trigraphs 17 KBUILD_CFLAGS += -Wno-trigraphs 18 KBUILD_CFLAGS += $(call cc-disable-warning,fra 18 KBUILD_CFLAGS += $(call cc-disable-warning,frame-address,) 19 KBUILD_CFLAGS += $(call cc-disable-warning, ad 19 KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member) 20 KBUILD_CFLAGS += -Wmissing-declarations 20 KBUILD_CFLAGS += -Wmissing-declarations 21 KBUILD_CFLAGS += -Wmissing-prototypes 21 KBUILD_CFLAGS += -Wmissing-prototypes 22 22 23 ifneq ($(CONFIG_FRAME_WARN),0) 23 ifneq ($(CONFIG_FRAME_WARN),0) 24 KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_ 24 KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN) 25 endif 25 endif 26 26 27 KBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror 27 KBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror 28 KBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y) 28 KBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y) 29 KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += 29 KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds 30 30 31 ifdef CONFIG_CC_IS_CLANG 31 ifdef CONFIG_CC_IS_CLANG 32 # The kernel builds with '-std=gnu11' so use o 32 # The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable. 33 KBUILD_CFLAGS += -Wno-gnu 33 KBUILD_CFLAGS += -Wno-gnu 34 else 34 else 35 35 36 # gcc inanely warns about local variables call 36 # gcc inanely warns about local variables called 'main' 37 KBUILD_CFLAGS += -Wno-main 37 KBUILD_CFLAGS += -Wno-main 38 endif 38 endif 39 39 >> 40 # These warnings generated too much noise in a regular build. >> 41 # Use make W=1 to enable them (see scripts/Makefile.extrawarn) >> 42 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) >> 43 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) >> 44 40 # These result in bogus false positives 45 # These result in bogus false positives 41 KBUILD_CFLAGS += $(call cc-disable-warning, da 46 KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer) 42 47 43 # Variable Length Arrays (VLAs) should not be 48 # Variable Length Arrays (VLAs) should not be used anywhere in the kernel 44 KBUILD_CFLAGS += -Wvla 49 KBUILD_CFLAGS += -Wvla 45 50 46 # disable pointer signed / unsigned warnings i 51 # disable pointer signed / unsigned warnings in gcc 4.0 47 KBUILD_CFLAGS += -Wno-pointer-sign 52 KBUILD_CFLAGS += -Wno-pointer-sign 48 53 49 # In order to make sure new function cast mism 54 # In order to make sure new function cast mismatches are not introduced 50 # in the kernel (to avoid tripping CFI checkin 55 # in the kernel (to avoid tripping CFI checking), the kernel should be 51 # globally built with -Wcast-function-type. 56 # globally built with -Wcast-function-type. 52 KBUILD_CFLAGS += $(call cc-option, -Wcast-func 57 KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type) 53 58 54 # The allocators already balk at large sizes, 59 # The allocators already balk at large sizes, so silence the compiler 55 # warnings for bounds checks involving those p 60 # warnings for bounds checks involving those possible values. While 56 # -Wno-alloc-size-larger-than would normally b 61 # -Wno-alloc-size-larger-than would normally be used here, earlier versions 57 # of gcc (<9.1) weirdly don't handle the optio 62 # of gcc (<9.1) weirdly don't handle the option correctly when _other_ 58 # warnings are produced (?!). Using -Walloc-si 63 # warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX 59 # doesn't work (as it is documented to), silen 64 # doesn't work (as it is documented to), silently resolving to "0" prior to 60 # version 9.1 (and producing an error more rec 65 # version 9.1 (and producing an error more recently). Numeric values larger 61 # than PTRDIFF_MAX also don't work prior to ve 66 # than PTRDIFF_MAX also don't work prior to version 9.1, which are silently 62 # ignored, continuing to default to PTRDIFF_MA 67 # ignored, continuing to default to PTRDIFF_MAX. So, left with no other 63 # choice, we must perform a versioned check to 68 # choice, we must perform a versioned check to disable this warning. 64 # https://lore.kernel.org/lkml/20210824115859. 69 # https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au 65 KBUILD_CFLAGS-$(call gcc-min-version, 90100) + 70 KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than 66 KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_C 71 KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) 67 72 68 # Prohibit date/time macros, which would make 73 # Prohibit date/time macros, which would make the build non-deterministic 69 KBUILD_CFLAGS += -Werror=date-time 74 KBUILD_CFLAGS += -Werror=date-time 70 75 71 # enforce correct pointer usage 76 # enforce correct pointer usage 72 KBUILD_CFLAGS += $(call cc-option,-Werror=inco 77 KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types) 73 78 74 # Require designated initializers for all mark 79 # Require designated initializers for all marked structures 75 KBUILD_CFLAGS += $(call cc-option,-Werror=desi 80 KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) 76 81 77 # Warn if there is an enum types mismatch 82 # Warn if there is an enum types mismatch 78 KBUILD_CFLAGS += $(call cc-option,-Wenum-conve 83 KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion) 79 84 80 KBUILD_CFLAGS += -Wextra << 81 KBUILD_CFLAGS += -Wunused << 82 << 83 # 85 # 84 # W=1 - warnings which may be relevant and do 86 # W=1 - warnings which may be relevant and do not occur too often 85 # 87 # 86 ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) 88 ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) 87 89 >> 90 KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter >> 91 KBUILD_CFLAGS += $(call cc-option, -Wrestrict) 88 KBUILD_CFLAGS += -Wmissing-format-attribute 92 KBUILD_CFLAGS += -Wmissing-format-attribute >> 93 KBUILD_CFLAGS += -Wold-style-definition 89 KBUILD_CFLAGS += -Wmissing-include-dirs 94 KBUILD_CFLAGS += -Wmissing-include-dirs >> 95 KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable) 90 KBUILD_CFLAGS += $(call cc-option, -Wunused-co 96 KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) >> 97 KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned) >> 98 KBUILD_CFLAGS += $(call cc-option, -Wformat-overflow) >> 99 KBUILD_CFLAGS += $(call cc-option, -Wformat-truncation) >> 100 KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation) 91 101 92 KBUILD_CPPFLAGS += -Wundef 102 KBUILD_CPPFLAGS += -Wundef 93 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 103 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 94 104 95 else 105 else 96 106 97 # Some diagnostics enabled by default are nois 107 # Some diagnostics enabled by default are noisy. 98 # Suppress them by using -Wno... except for W= 108 # Suppress them by using -Wno... except for W=1. 99 KBUILD_CFLAGS += $(call cc-disable-warning, un 109 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) 100 KBUILD_CFLAGS += $(call cc-disable-warning, un 110 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) >> 111 KBUILD_CFLAGS += $(call cc-disable-warning, restrict) 101 KBUILD_CFLAGS += $(call cc-disable-warning, pa 112 KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned) 102 KBUILD_CFLAGS += $(call cc-disable-warning, fo 113 KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow) 103 ifdef CONFIG_CC_IS_GCC << 104 KBUILD_CFLAGS += $(call cc-disable-warning, fo 114 KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation) 105 else << 106 # Clang checks for overflow/truncation with '% << 107 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id << 108 KBUILD_CFLAGS += $(call cc-disable-warning, fo << 109 KBUILD_CFLAGS += $(call cc-disable-warning, fo << 110 endif << 111 KBUILD_CFLAGS += $(call cc-disable-warning, st 115 KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation) 112 116 113 KBUILD_CFLAGS += -Wno-override-init # alias fo 117 KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang 114 118 115 ifdef CONFIG_CC_IS_CLANG 119 ifdef CONFIG_CC_IS_CLANG 116 # Clang before clang-16 would warn on default 120 # Clang before clang-16 would warn on default argument promotions. 117 ifneq ($(call clang-min-version, 160000),y) 121 ifneq ($(call clang-min-version, 160000),y) 118 # Disable -Wformat 122 # Disable -Wformat 119 KBUILD_CFLAGS += -Wno-format 123 KBUILD_CFLAGS += -Wno-format 120 # Then re-enable flags that were part of the - 124 # Then re-enable flags that were part of the -Wformat group that aren't 121 # problematic. 125 # problematic. 122 KBUILD_CFLAGS += -Wformat-extra-args -Wformat- 126 KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier 123 KBUILD_CFLAGS += -Wformat-zero-length -Wnonnul 127 KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull 124 # Requires clang-12+. 128 # Requires clang-12+. 125 ifeq ($(call clang-min-version, 120000),y) 129 ifeq ($(call clang-min-version, 120000),y) 126 KBUILD_CFLAGS += -Wformat-insufficient-args 130 KBUILD_CFLAGS += -Wformat-insufficient-args 127 endif 131 endif 128 endif 132 endif 129 KBUILD_CFLAGS += $(call cc-disable-warning, po 133 KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast) 130 KBUILD_CFLAGS += -Wno-tautological-constant-ou 134 KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare 131 KBUILD_CFLAGS += $(call cc-disable-warning, un 135 KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access) >> 136 KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict) 132 KBUILD_CFLAGS += -Wno-enum-compare-conditional 137 KBUILD_CFLAGS += -Wno-enum-compare-conditional 133 KBUILD_CFLAGS += -Wno-enum-enum-conversion 138 KBUILD_CFLAGS += -Wno-enum-enum-conversion 134 endif 139 endif 135 140 136 endif 141 endif 137 142 138 # 143 # 139 # W=2 - warnings which occur quite often but m 144 # W=2 - warnings which occur quite often but may still be relevant 140 # 145 # 141 ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) 146 ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) 142 147 143 KBUILD_CFLAGS += -Wdisabled-optimization 148 KBUILD_CFLAGS += -Wdisabled-optimization 144 KBUILD_CFLAGS += -Wshadow 149 KBUILD_CFLAGS += -Wshadow 145 KBUILD_CFLAGS += $(call cc-option, -Wlogical-o 150 KBUILD_CFLAGS += $(call cc-option, -Wlogical-op) >> 151 KBUILD_CFLAGS += -Wmissing-field-initializers >> 152 KBUILD_CFLAGS += -Wtype-limits >> 153 KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized) 146 KBUILD_CFLAGS += $(call cc-option, -Wunused-ma 154 KBUILD_CFLAGS += $(call cc-option, -Wunused-macros) 147 155 148 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 156 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 149 157 150 else 158 else 151 159 152 # The following turn off the warnings enabled 160 # The following turn off the warnings enabled by -Wextra 153 KBUILD_CFLAGS += -Wno-missing-field-initialize 161 KBUILD_CFLAGS += -Wno-missing-field-initializers 154 KBUILD_CFLAGS += -Wno-type-limits 162 KBUILD_CFLAGS += -Wno-type-limits 155 KBUILD_CFLAGS += -Wno-shift-negative-value 163 KBUILD_CFLAGS += -Wno-shift-negative-value 156 164 157 ifdef CONFIG_CC_IS_GCC 165 ifdef CONFIG_CC_IS_GCC 158 KBUILD_CFLAGS += -Wno-maybe-uninitialized 166 KBUILD_CFLAGS += -Wno-maybe-uninitialized 159 endif 167 endif 160 168 161 endif 169 endif 162 170 163 # 171 # 164 # W=3 - more obscure warnings, can most likely 172 # W=3 - more obscure warnings, can most likely be ignored 165 # 173 # 166 ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) 174 ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) 167 175 168 KBUILD_CFLAGS += -Wbad-function-cast 176 KBUILD_CFLAGS += -Wbad-function-cast 169 KBUILD_CFLAGS += -Wcast-align 177 KBUILD_CFLAGS += -Wcast-align 170 KBUILD_CFLAGS += -Wcast-qual 178 KBUILD_CFLAGS += -Wcast-qual 171 KBUILD_CFLAGS += -Wconversion 179 KBUILD_CFLAGS += -Wconversion 172 KBUILD_CFLAGS += -Wpacked 180 KBUILD_CFLAGS += -Wpacked 173 KBUILD_CFLAGS += -Wpadded 181 KBUILD_CFLAGS += -Wpadded 174 KBUILD_CFLAGS += -Wpointer-arith 182 KBUILD_CFLAGS += -Wpointer-arith 175 KBUILD_CFLAGS += -Wredundant-decls 183 KBUILD_CFLAGS += -Wredundant-decls 176 KBUILD_CFLAGS += -Wsign-compare 184 KBUILD_CFLAGS += -Wsign-compare 177 KBUILD_CFLAGS += -Wswitch-default 185 KBUILD_CFLAGS += -Wswitch-default >> 186 KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat) 178 187 179 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 188 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 180 189 181 else 190 else 182 191 183 # The following turn off the warnings enabled 192 # The following turn off the warnings enabled by -Wextra 184 KBUILD_CFLAGS += -Wno-sign-compare 193 KBUILD_CFLAGS += -Wno-sign-compare 185 KBUILD_CFLAGS += -Wno-unused-parameter << 186 194 187 endif 195 endif 188 196 189 # 197 # 190 # W=e - error out on warnings 198 # W=e - error out on warnings 191 # 199 # 192 ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),) 200 ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),) 193 201 194 KBUILD_CFLAGS += -Werror 202 KBUILD_CFLAGS += -Werror 195 203 196 endif 204 endif
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.