Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Masahiro Yamada | a86fe35 | 2014-04-14 18:27:10 +0900 | [diff] [blame] | 2 | # ========================================================================== |
Masahiro Yamada | a86fe35 | 2014-04-14 18:27:10 +0900 | [diff] [blame] | 3 | # make W=... settings |
| 4 | # |
Yann Droneaud | c77d06e | 2022-04-08 10:46:07 +0200 | [diff] [blame] | 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. |
Masahiro Yamada | a86fe35 | 2014-04-14 18:27:10 +0900 | [diff] [blame] | 7 | # ========================================================================== |
| 8 | |
Arnd Bergmann | e88ca24 | 2023-08-11 16:03:20 +0200 | [diff] [blame] | 9 | # Default set of warnings, always enabled |
| 10 | KBUILD_CFLAGS += -Wall |
| 11 | KBUILD_CFLAGS += -Wundef |
| 12 | KBUILD_CFLAGS += -Werror=implicit-function-declaration |
| 13 | KBUILD_CFLAGS += -Werror=implicit-int |
| 14 | KBUILD_CFLAGS += -Werror=return-type |
| 15 | KBUILD_CFLAGS += -Werror=strict-prototypes |
| 16 | KBUILD_CFLAGS += -Wno-format-security |
| 17 | KBUILD_CFLAGS += -Wno-trigraphs |
| 18 | KBUILD_CFLAGS += $(call cc-disable-warning,frame-address,) |
Arnd Bergmann | e88ca24 | 2023-08-11 16:03:20 +0200 | [diff] [blame] | 19 | KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member) |
Arnd Bergmann | 0fcb708 | 2023-11-23 12:05:06 +0100 | [diff] [blame] | 20 | KBUILD_CFLAGS += -Wmissing-declarations |
| 21 | KBUILD_CFLAGS += -Wmissing-prototypes |
Arnd Bergmann | e88ca24 | 2023-08-11 16:03:20 +0200 | [diff] [blame] | 22 | |
| 23 | ifneq ($(CONFIG_FRAME_WARN),0) |
| 24 | KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN) |
| 25 | endif |
| 26 | |
| 27 | KBUILD_CPPFLAGS-$(CONFIG_WERROR) += -Werror |
| 28 | KBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y) |
| 29 | KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds |
| 30 | |
| 31 | ifdef CONFIG_CC_IS_CLANG |
| 32 | # The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable. |
| 33 | KBUILD_CFLAGS += -Wno-gnu |
| 34 | else |
| 35 | |
| 36 | # gcc inanely warns about local variables called 'main' |
| 37 | KBUILD_CFLAGS += -Wno-main |
| 38 | endif |
| 39 | |
Arnd Bergmann | e88ca24 | 2023-08-11 16:03:20 +0200 | [diff] [blame] | 40 | # These result in bogus false positives |
| 41 | KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer) |
| 42 | |
| 43 | # Variable Length Arrays (VLAs) should not be used anywhere in the kernel |
| 44 | KBUILD_CFLAGS += -Wvla |
| 45 | |
| 46 | # disable pointer signed / unsigned warnings in gcc 4.0 |
| 47 | KBUILD_CFLAGS += -Wno-pointer-sign |
| 48 | |
| 49 | # In order to make sure new function cast mismatches are not introduced |
| 50 | # in the kernel (to avoid tripping CFI checking), the kernel should be |
| 51 | # globally built with -Wcast-function-type. |
| 52 | KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type) |
| 53 | |
Arnd Bergmann | e88ca24 | 2023-08-11 16:03:20 +0200 | [diff] [blame] | 54 | # The allocators already balk at large sizes, so silence the compiler |
| 55 | # warnings for bounds checks involving those possible values. While |
| 56 | # -Wno-alloc-size-larger-than would normally be used here, earlier versions |
| 57 | # of gcc (<9.1) weirdly don't handle the option correctly when _other_ |
| 58 | # warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX |
| 59 | # doesn't work (as it is documented to), silently resolving to "0" prior to |
| 60 | # version 9.1 (and producing an error more recently). Numeric values larger |
| 61 | # than PTRDIFF_MAX also don't work prior to version 9.1, which are silently |
| 62 | # ignored, continuing to default to PTRDIFF_MAX. So, left with no other |
| 63 | # choice, we must perform a versioned check to disable this warning. |
| 64 | # https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au |
| 65 | KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than |
| 66 | KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH) |
| 67 | |
| 68 | # Prohibit date/time macros, which would make the build non-deterministic |
| 69 | KBUILD_CFLAGS += -Werror=date-time |
| 70 | |
| 71 | # enforce correct pointer usage |
| 72 | KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types) |
| 73 | |
| 74 | # Require designated initializers for all marked structures |
| 75 | KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init) |
| 76 | |
| 77 | # Warn if there is an enum types mismatch |
| 78 | KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion) |
| 79 | |
Arnd Bergmann | f5982cc | 2024-04-15 14:20:32 +0200 | [diff] [blame] | 80 | KBUILD_CFLAGS += -Wextra |
| 81 | KBUILD_CFLAGS += -Wunused |
| 82 | |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 83 | # |
| 84 | # W=1 - warnings which may be relevant and do not occur too often |
| 85 | # |
Masahiro Yamada | e27128d | 2019-09-01 01:25:55 +0900 | [diff] [blame] | 86 | ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) |
Masahiro Yamada | a86fe35 | 2014-04-14 18:27:10 +0900 | [diff] [blame] | 87 | |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 88 | KBUILD_CFLAGS += -Wmissing-format-attribute |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 89 | KBUILD_CFLAGS += -Wmissing-include-dirs |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 90 | KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable) |
Masahiro Yamada | a86fe35 | 2014-04-14 18:27:10 +0900 | [diff] [blame] | 91 | |
Masahiro Yamada | 80b6093 | 2022-12-06 13:07:31 +0900 | [diff] [blame] | 92 | KBUILD_CPPFLAGS += -Wundef |
Masahiro Yamada | 6863f56 | 2019-09-07 11:52:36 +0900 | [diff] [blame] | 93 | KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1 |
| 94 | |
Behan Webster | 26ea6bb | 2014-07-31 21:08:25 -0700 | [diff] [blame] | 95 | else |
| 96 | |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 97 | # Some diagnostics enabled by default are noisy. |
| 98 | # Suppress them by using -Wno... except for W=1. |
Arnd Bergmann | 2cd3271 | 2023-08-11 16:03:21 +0200 | [diff] [blame] | 99 | KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) |
| 100 | KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable) |
| 101 | KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned) |
Arnd Bergmann | 6d4ab2e | 2023-08-11 16:03:23 +0200 | [diff] [blame] | 102 | KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow) |
Arnd Bergmann | 908dd50 | 2024-04-15 14:20:35 +0200 | [diff] [blame] | 103 | ifdef CONFIG_CC_IS_GCC |
Arnd Bergmann | 6d4ab2e | 2023-08-11 16:03:23 +0200 | [diff] [blame] | 104 | KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation) |
Arnd Bergmann | 908dd50 | 2024-04-15 14:20:35 +0200 | [diff] [blame] | 105 | else |
| 106 | # Clang checks for overflow/truncation with '%p', while GCC does not: |
| 107 | # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219 |
| 108 | KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow-non-kprintf) |
| 109 | KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation-non-kprintf) |
| 110 | endif |
Arnd Bergmann | 2cd3271 | 2023-08-11 16:03:21 +0200 | [diff] [blame] | 111 | KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation) |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 112 | |
Arnd Bergmann | c40845e | 2024-03-26 15:47:16 +0100 | [diff] [blame] | 113 | KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang |
| 114 | |
Masahiro Yamada | 076f421 | 2018-10-30 22:26:33 +0900 | [diff] [blame] | 115 | ifdef CONFIG_CC_IS_CLANG |
Nick Desaulniers | b0839b28 | 2022-09-01 10:59:13 -0700 | [diff] [blame] | 116 | # Clang before clang-16 would warn on default argument promotions. |
Nick Desaulniers | 88b61e3 | 2022-09-19 10:08:28 -0700 | [diff] [blame] | 117 | ifneq ($(call clang-min-version, 160000),y) |
Nick Desaulniers | b0839b28 | 2022-09-01 10:59:13 -0700 | [diff] [blame] | 118 | # Disable -Wformat |
Linus Torvalds | 21f9c8a | 2022-08-11 08:40:01 -0700 | [diff] [blame] | 119 | KBUILD_CFLAGS += -Wno-format |
Nick Desaulniers | b0839b28 | 2022-09-01 10:59:13 -0700 | [diff] [blame] | 120 | # Then re-enable flags that were part of the -Wformat group that aren't |
| 121 | # problematic. |
| 122 | KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier |
| 123 | KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull |
| 124 | # Requires clang-12+. |
Nick Desaulniers | 88b61e3 | 2022-09-19 10:08:28 -0700 | [diff] [blame] | 125 | ifeq ($(call clang-min-version, 120000),y) |
Nick Desaulniers | b0839b28 | 2022-09-01 10:59:13 -0700 | [diff] [blame] | 126 | KBUILD_CFLAGS += -Wformat-insufficient-args |
| 127 | endif |
| 128 | endif |
Nathan Chancellor | 82f2bc2 | 2020-03-11 12:41:21 -0700 | [diff] [blame] | 129 | KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast) |
Nathan Chancellor | afe956c | 2020-03-26 12:41:55 -0700 | [diff] [blame] | 130 | KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare |
Nathan Chancellor | 1cf5f15 | 2022-02-02 16:05:16 -0700 | [diff] [blame] | 131 | KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access) |
Nathan Chancellor | 75b5ab1 | 2024-03-05 15:12:47 -0700 | [diff] [blame] | 132 | KBUILD_CFLAGS += -Wno-enum-compare-conditional |
| 133 | KBUILD_CFLAGS += -Wno-enum-enum-conversion |
Behan Webster | 26ea6bb | 2014-07-31 21:08:25 -0700 | [diff] [blame] | 134 | endif |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 135 | |
| 136 | endif |
| 137 | |
| 138 | # |
| 139 | # W=2 - warnings which occur quite often but may still be relevant |
| 140 | # |
Masahiro Yamada | e27128d | 2019-09-01 01:25:55 +0900 | [diff] [blame] | 141 | ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),) |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 142 | |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 143 | KBUILD_CFLAGS += -Wdisabled-optimization |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 144 | KBUILD_CFLAGS += -Wshadow |
| 145 | KBUILD_CFLAGS += $(call cc-option, -Wlogical-op) |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 146 | KBUILD_CFLAGS += $(call cc-option, -Wunused-macros) |
| 147 | |
Masahiro Yamada | 6863f56 | 2019-09-07 11:52:36 +0900 | [diff] [blame] | 148 | KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2 |
| 149 | |
Arnd Bergmann | 2cd3271 | 2023-08-11 16:03:21 +0200 | [diff] [blame] | 150 | else |
| 151 | |
| 152 | # The following turn off the warnings enabled by -Wextra |
| 153 | KBUILD_CFLAGS += -Wno-missing-field-initializers |
| 154 | KBUILD_CFLAGS += -Wno-type-limits |
| 155 | KBUILD_CFLAGS += -Wno-shift-negative-value |
| 156 | |
Arnd Bergmann | c40845e | 2024-03-26 15:47:16 +0100 | [diff] [blame] | 157 | ifdef CONFIG_CC_IS_GCC |
Arnd Bergmann | 2cd3271 | 2023-08-11 16:03:21 +0200 | [diff] [blame] | 158 | KBUILD_CFLAGS += -Wno-maybe-uninitialized |
| 159 | endif |
| 160 | |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 161 | endif |
| 162 | |
| 163 | # |
| 164 | # W=3 - more obscure warnings, can most likely be ignored |
| 165 | # |
Masahiro Yamada | e27128d | 2019-09-01 01:25:55 +0900 | [diff] [blame] | 166 | ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),) |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 167 | |
| 168 | KBUILD_CFLAGS += -Wbad-function-cast |
Arnd Bergmann | 095fbca | 2020-10-26 23:03:13 +0100 | [diff] [blame] | 169 | KBUILD_CFLAGS += -Wcast-align |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 170 | KBUILD_CFLAGS += -Wcast-qual |
| 171 | KBUILD_CFLAGS += -Wconversion |
| 172 | KBUILD_CFLAGS += -Wpacked |
| 173 | KBUILD_CFLAGS += -Wpadded |
| 174 | KBUILD_CFLAGS += -Wpointer-arith |
| 175 | KBUILD_CFLAGS += -Wredundant-decls |
Joe Perches | a97ea93 | 2020-07-21 21:57:43 -0700 | [diff] [blame] | 176 | KBUILD_CFLAGS += -Wsign-compare |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 177 | KBUILD_CFLAGS += -Wswitch-default |
Masahiro Yamada | 64a9190 | 2019-09-01 01:25:54 +0900 | [diff] [blame] | 178 | |
Masahiro Yamada | 6863f56 | 2019-09-07 11:52:36 +0900 | [diff] [blame] | 179 | KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3 |
| 180 | |
Arnd Bergmann | 2cd3271 | 2023-08-11 16:03:21 +0200 | [diff] [blame] | 181 | else |
| 182 | |
| 183 | # The following turn off the warnings enabled by -Wextra |
| 184 | KBUILD_CFLAGS += -Wno-sign-compare |
Arnd Bergmann | f5982cc | 2024-04-15 14:20:32 +0200 | [diff] [blame] | 185 | KBUILD_CFLAGS += -Wno-unused-parameter |
Arnd Bergmann | 2cd3271 | 2023-08-11 16:03:21 +0200 | [diff] [blame] | 186 | |
Masahiro Yamada | a86fe35 | 2014-04-14 18:27:10 +0900 | [diff] [blame] | 187 | endif |
Yann Droneaud | c77d06e | 2022-04-08 10:46:07 +0200 | [diff] [blame] | 188 | |
| 189 | # |
| 190 | # W=e - error out on warnings |
| 191 | # |
| 192 | ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),) |
| 193 | |
| 194 | KBUILD_CFLAGS += -Werror |
| 195 | |
| 196 | endif |