Makefile.warn 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # SPDX-License-Identifier: GPL-2.0
  2. # ==========================================================================
  3. # make W=... settings
  4. #
  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.
  7. # ==========================================================================
  8. # Default set of warnings, always enabled
  9. KBUILD_CFLAGS += -Wall
  10. KBUILD_CFLAGS += -Wextra
  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 += -Wno-frame-address
  19. KBUILD_CFLAGS += $(call cc-option, -Wno-address-of-packed-member)
  20. KBUILD_CFLAGS += -Wmissing-declarations
  21. KBUILD_CFLAGS += -Wmissing-prototypes
  22. ifneq ($(CONFIG_FRAME_WARN),0)
  23. KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
  24. endif
  25. KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds
  26. ifdef CONFIG_CC_IS_CLANG
  27. # The kernel builds with '-std=gnu11' and '-fms-extensions' so use of GNU and
  28. # Microsoft extensions is acceptable.
  29. KBUILD_CFLAGS += -Wno-gnu
  30. KBUILD_CFLAGS += -Wno-microsoft-anon-tag
  31. # Clang checks for overflow/truncation with '%p', while GCC does not:
  32. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219
  33. KBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow-non-kprintf)
  34. KBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation-non-kprintf)
  35. # Clang may emit a warning when a const variable, such as the dummy variables
  36. # in typecheck(), or const member of an aggregate type are not initialized,
  37. # which can result in unexpected behavior. However, in many audited cases of
  38. # the "field" variant of the warning, this is intentional because the field is
  39. # never used within a particular call path, the field is within a union with
  40. # other non-const members, or the containing object is not const so the field
  41. # can be modified via memcpy() / memset(). While the variable warning also gets
  42. # disabled with this same switch, there should not be too much coverage lost
  43. # because -Wuninitialized will still flag when an uninitialized const variable
  44. # is used.
  45. KBUILD_CFLAGS += $(call cc-option, -Wno-default-const-init-unsafe)
  46. else
  47. # gcc inanely warns about local variables called 'main'
  48. KBUILD_CFLAGS += -Wno-main
  49. endif
  50. # Too noisy on range checks and in macros handling both signed and unsigned.
  51. KBUILD_CFLAGS += -Wno-type-limits
  52. # These result in bogus false positives
  53. KBUILD_CFLAGS += $(call cc-option, -Wno-dangling-pointer)
  54. # Stack Variable Length Arrays (VLAs) must not be used in the kernel.
  55. # Function array parameters should, however, be usable, but -Wvla will
  56. # warn for those. Clang has no way yet to distinguish between the VLA
  57. # types, so depend on GCC for now to keep stack VLAs out of the tree.
  58. # https://github.com/llvm/llvm-project/issues/57098
  59. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217
  60. KBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=1)
  61. # disable pointer signed / unsigned warnings in gcc 4.0
  62. KBUILD_CFLAGS += -Wno-pointer-sign
  63. # In order to make sure new function cast mismatches are not introduced
  64. # in the kernel (to avoid tripping CFI checking), the kernel should be
  65. # globally built with -Wcast-function-type.
  66. KBUILD_CFLAGS += -Wcast-function-type
  67. # Currently, disable -Wstringop-overflow for GCC 11, globally.
  68. KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-option, -Wno-stringop-overflow)
  69. KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow)
  70. # Currently, disable -Wunterminated-string-initialization as broken
  71. KBUILD_CFLAGS += $(call cc-option, -Wno-unterminated-string-initialization)
  72. # The allocators already balk at large sizes, so silence the compiler
  73. # warnings for bounds checks involving those possible values. While
  74. # -Wno-alloc-size-larger-than would normally be used here, earlier versions
  75. # of gcc (<9.1) weirdly don't handle the option correctly when _other_
  76. # warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
  77. # doesn't work (as it is documented to), silently resolving to "0" prior to
  78. # version 9.1 (and producing an error more recently). Numeric values larger
  79. # than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
  80. # ignored, continuing to default to PTRDIFF_MAX. So, left with no other
  81. # choice, we must perform a versioned check to disable this warning.
  82. # https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au
  83. KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
  84. KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
  85. # Prohibit date/time macros, which would make the build non-deterministic
  86. KBUILD_CFLAGS += -Werror=date-time
  87. # enforce correct pointer usage
  88. KBUILD_CFLAGS += -Werror=incompatible-pointer-types
  89. # Require designated initializers for all marked structures
  90. KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
  91. # Warn if there is an enum types mismatch
  92. KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
  93. KBUILD_CFLAGS += -Wunused
  94. #
  95. # W=1 - warnings which may be relevant and do not occur too often
  96. #
  97. ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
  98. KBUILD_CFLAGS += -Wmissing-format-attribute
  99. KBUILD_CFLAGS += -Wmissing-include-dirs
  100. KBUILD_CFLAGS += -Wunused-const-variable
  101. KBUILD_CPPFLAGS += -Wundef
  102. KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
  103. else
  104. # Some diagnostics enabled by default are noisy.
  105. # Suppress them by using -Wno... except for W=1.
  106. KBUILD_CFLAGS += -Wno-unused-but-set-variable
  107. KBUILD_CFLAGS += -Wno-unused-const-variable
  108. KBUILD_CFLAGS += $(call cc-option, -Wno-packed-not-aligned)
  109. KBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow)
  110. ifdef CONFIG_CC_IS_GCC
  111. KBUILD_CFLAGS += -Wno-format-truncation
  112. endif
  113. KBUILD_CFLAGS += $(call cc-option, -Wno-stringop-truncation)
  114. KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
  115. ifdef CONFIG_CC_IS_CLANG
  116. # Clang before clang-16 would warn on default argument promotions.
  117. ifneq ($(call clang-min-version, 160000),y)
  118. # Disable -Wformat
  119. KBUILD_CFLAGS += -Wno-format
  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. KBUILD_CFLAGS += -Wformat-insufficient-args
  125. endif
  126. KBUILD_CFLAGS += -Wno-pointer-to-enum-cast
  127. KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
  128. KBUILD_CFLAGS += -Wno-unaligned-access
  129. KBUILD_CFLAGS += -Wno-enum-compare-conditional
  130. endif
  131. endif
  132. #
  133. # W=2 - warnings which occur quite often but may still be relevant
  134. #
  135. ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
  136. KBUILD_CFLAGS += -Wdisabled-optimization
  137. KBUILD_CFLAGS += -Wshadow
  138. KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
  139. KBUILD_CFLAGS += -Wunused-macros
  140. KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
  141. else
  142. # The following turn off the warnings enabled by -Wextra
  143. KBUILD_CFLAGS += -Wno-missing-field-initializers
  144. KBUILD_CFLAGS += -Wno-shift-negative-value
  145. ifdef CONFIG_CC_IS_CLANG
  146. KBUILD_CFLAGS += -Wno-enum-enum-conversion
  147. endif
  148. ifdef CONFIG_CC_IS_GCC
  149. KBUILD_CFLAGS += -Wno-maybe-uninitialized
  150. endif
  151. endif
  152. #
  153. # W=3 - more obscure warnings, can most likely be ignored
  154. #
  155. ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
  156. KBUILD_CFLAGS += -Wbad-function-cast
  157. KBUILD_CFLAGS += -Wcast-align
  158. KBUILD_CFLAGS += -Wcast-qual
  159. KBUILD_CFLAGS += -Wconversion
  160. KBUILD_CFLAGS += -Wpacked
  161. KBUILD_CFLAGS += -Wpadded
  162. KBUILD_CFLAGS += -Wpointer-arith
  163. KBUILD_CFLAGS += -Wredundant-decls
  164. KBUILD_CFLAGS += -Wsign-compare
  165. KBUILD_CFLAGS += -Wswitch-default
  166. KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
  167. else
  168. # The following turn off the warnings enabled by -Wextra
  169. KBUILD_CFLAGS += -Wno-sign-compare
  170. KBUILD_CFLAGS += -Wno-unused-parameter
  171. endif
  172. #
  173. # W=e and CONFIG_WERROR - error out on warnings
  174. #
  175. ifneq ($(findstring e, $(KBUILD_EXTRA_WARN))$(CONFIG_WERROR),)
  176. KBUILD_CPPFLAGS += -Werror
  177. KBUILD_AFLAGS += -Wa,--fatal-warnings
  178. KBUILD_LDFLAGS += --fatal-warnings
  179. KBUILD_USERCFLAGS += -Werror
  180. KBUILD_USERLDFLAGS += -Wl,--fatal-warnings
  181. KBUILD_RUSTFLAGS += -Dwarnings
  182. # While hostprog flags are used during build bootstrapping (thus should not
  183. # depend on CONFIG_ symbols), -Werror is disruptive and should be opted into.
  184. # Only apply -Werror to hostprogs built after the initial Kconfig stage.
  185. KBUILD_HOSTCFLAGS += -Werror
  186. KBUILD_HOSTLDFLAGS += -Wl,--fatal-warnings
  187. KBUILD_HOSTRUSTFLAGS += -Dwarnings
  188. endif