Makefile.extrawarn 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 += $(call cc-disable-warning, frame-address)
  19. KBUILD_CFLAGS += $(call cc-disable-warning, 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_CPPFLAGS-$(CONFIG_WERROR) += -Werror
  26. KBUILD_CPPFLAGS += $(KBUILD_CPPFLAGS-y)
  27. KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds
  28. ifdef CONFIG_CC_IS_CLANG
  29. # The kernel builds with '-std=gnu11' so use of GNU extensions is acceptable.
  30. KBUILD_CFLAGS += -Wno-gnu
  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-disable-warning, format-overflow-non-kprintf)
  34. KBUILD_CFLAGS += $(call cc-disable-warning, 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-disable-warning, default-const-init-unsafe)
  46. else
  47. # gcc inanely warns about local variables called 'main'
  48. KBUILD_CFLAGS += -Wno-main
  49. endif
  50. # These result in bogus false positives
  51. KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
  52. # Variable Length Arrays (VLAs) should not be used anywhere in the kernel
  53. KBUILD_CFLAGS += -Wvla
  54. # disable pointer signed / unsigned warnings in gcc 4.0
  55. KBUILD_CFLAGS += -Wno-pointer-sign
  56. # In order to make sure new function cast mismatches are not introduced
  57. # in the kernel (to avoid tripping CFI checking), the kernel should be
  58. # globally built with -Wcast-function-type.
  59. KBUILD_CFLAGS += $(call cc-option, -Wcast-function-type)
  60. # Currently, disable -Wstringop-overflow for GCC 11, globally.
  61. KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-disable-warning, stringop-overflow)
  62. KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow)
  63. # Currently, disable -Wunterminated-string-initialization as broken
  64. KBUILD_CFLAGS += $(call cc-disable-warning, unterminated-string-initialization)
  65. # The allocators already balk at large sizes, so silence the compiler
  66. # warnings for bounds checks involving those possible values. While
  67. # -Wno-alloc-size-larger-than would normally be used here, earlier versions
  68. # of gcc (<9.1) weirdly don't handle the option correctly when _other_
  69. # warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
  70. # doesn't work (as it is documented to), silently resolving to "0" prior to
  71. # version 9.1 (and producing an error more recently). Numeric values larger
  72. # than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
  73. # ignored, continuing to default to PTRDIFF_MAX. So, left with no other
  74. # choice, we must perform a versioned check to disable this warning.
  75. # https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au
  76. KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
  77. KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
  78. # Prohibit date/time macros, which would make the build non-deterministic
  79. KBUILD_CFLAGS += -Werror=date-time
  80. # enforce correct pointer usage
  81. KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
  82. # Require designated initializers for all marked structures
  83. KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
  84. # Warn if there is an enum types mismatch
  85. KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
  86. # Explicitly clear padding bits during variable initialization
  87. KBUILD_CFLAGS += $(call cc-option,-fzero-init-padding-bits=all)
  88. KBUILD_CFLAGS += -Wunused
  89. #
  90. # W=1 - warnings which may be relevant and do not occur too often
  91. #
  92. ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
  93. KBUILD_CFLAGS += -Wmissing-format-attribute
  94. KBUILD_CFLAGS += -Wmissing-include-dirs
  95. KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
  96. KBUILD_CPPFLAGS += -Wundef
  97. KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
  98. else
  99. # Some diagnostics enabled by default are noisy.
  100. # Suppress them by using -Wno... except for W=1.
  101. KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
  102. KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
  103. KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
  104. KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
  105. ifdef CONFIG_CC_IS_GCC
  106. KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
  107. endif
  108. KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
  109. KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
  110. ifdef CONFIG_CC_IS_CLANG
  111. # Clang before clang-16 would warn on default argument promotions.
  112. ifneq ($(call clang-min-version, 160000),y)
  113. # Disable -Wformat
  114. KBUILD_CFLAGS += -Wno-format
  115. # Then re-enable flags that were part of the -Wformat group that aren't
  116. # problematic.
  117. KBUILD_CFLAGS += -Wformat-extra-args -Wformat-invalid-specifier
  118. KBUILD_CFLAGS += -Wformat-zero-length -Wnonnull
  119. # Requires clang-12+.
  120. ifeq ($(call clang-min-version, 120000),y)
  121. KBUILD_CFLAGS += -Wformat-insufficient-args
  122. endif
  123. endif
  124. KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
  125. KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
  126. KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access)
  127. KBUILD_CFLAGS += -Wno-enum-compare-conditional
  128. endif
  129. endif
  130. #
  131. # W=2 - warnings which occur quite often but may still be relevant
  132. #
  133. ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
  134. KBUILD_CFLAGS += -Wdisabled-optimization
  135. KBUILD_CFLAGS += -Wshadow
  136. KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
  137. KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
  138. KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
  139. else
  140. # The following turn off the warnings enabled by -Wextra
  141. KBUILD_CFLAGS += -Wno-missing-field-initializers
  142. KBUILD_CFLAGS += -Wno-type-limits
  143. KBUILD_CFLAGS += -Wno-shift-negative-value
  144. ifdef CONFIG_CC_IS_CLANG
  145. KBUILD_CFLAGS += -Wno-enum-enum-conversion
  146. endif
  147. ifdef CONFIG_CC_IS_GCC
  148. KBUILD_CFLAGS += -Wno-maybe-uninitialized
  149. endif
  150. endif
  151. #
  152. # W=3 - more obscure warnings, can most likely be ignored
  153. #
  154. ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
  155. KBUILD_CFLAGS += -Wbad-function-cast
  156. KBUILD_CFLAGS += -Wcast-align
  157. KBUILD_CFLAGS += -Wcast-qual
  158. KBUILD_CFLAGS += -Wconversion
  159. KBUILD_CFLAGS += -Wpacked
  160. KBUILD_CFLAGS += -Wpadded
  161. KBUILD_CFLAGS += -Wpointer-arith
  162. KBUILD_CFLAGS += -Wredundant-decls
  163. KBUILD_CFLAGS += -Wsign-compare
  164. KBUILD_CFLAGS += -Wswitch-default
  165. KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
  166. else
  167. # The following turn off the warnings enabled by -Wextra
  168. KBUILD_CFLAGS += -Wno-sign-compare
  169. KBUILD_CFLAGS += -Wno-unused-parameter
  170. endif
  171. #
  172. # W=e - error out on warnings
  173. #
  174. ifneq ($(findstring e, $(KBUILD_EXTRA_WARN)),)
  175. KBUILD_CFLAGS += -Werror
  176. endif