Makefile.extrawarn 6.7 KB

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