2
0

PickyWarnings.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # SPDX-License-Identifier: curl
  22. #
  23. ###########################################################################
  24. include(CheckCCompilerFlag)
  25. unset(WPICKY)
  26. if(CURL_WERROR AND
  27. ((CMAKE_COMPILER_IS_GNUCC AND
  28. NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0 AND
  29. NOT CMAKE_VERSION VERSION_LESS 3.23.0) OR # check_symbol_exists() incompatible with GCC -pedantic-errors in earlier CMake versions
  30. CMAKE_C_COMPILER_ID MATCHES "Clang"))
  31. set(WPICKY "${WPICKY} -pedantic-errors")
  32. endif()
  33. if(PICKY_COMPILER)
  34. if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
  35. # https://clang.llvm.org/docs/DiagnosticsReference.html
  36. # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  37. # WPICKY_ENABLE = Options we want to enable as-is.
  38. # WPICKY_DETECT = Options we want to test first and enable if available.
  39. # Prefer the -Wextra alias with clang.
  40. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  41. set(WPICKY_ENABLE "-Wextra")
  42. else()
  43. set(WPICKY_ENABLE "-W")
  44. endif()
  45. list(APPEND WPICKY_ENABLE
  46. -Wall -pedantic
  47. )
  48. # ----------------------------------
  49. # Add new options here, if in doubt:
  50. # ----------------------------------
  51. set(WPICKY_DETECT
  52. )
  53. # Assume these options always exist with both clang and gcc.
  54. # Require clang 3.0 / gcc 2.95 or later.
  55. list(APPEND WPICKY_ENABLE
  56. -Wbad-function-cast # clang 2.7 gcc 2.95
  57. -Wconversion # clang 2.7 gcc 2.95
  58. -Winline # clang 1.0 gcc 1.0
  59. -Wmissing-declarations # clang 1.0 gcc 2.7
  60. -Wmissing-prototypes # clang 1.0 gcc 1.0
  61. -Wnested-externs # clang 1.0 gcc 2.7
  62. -Wno-long-long # clang 1.0 gcc 2.95
  63. -Wno-multichar # clang 1.0 gcc 2.95
  64. -Wpointer-arith # clang 1.0 gcc 1.4
  65. -Wshadow # clang 1.0 gcc 2.95
  66. -Wsign-compare # clang 1.0 gcc 2.95
  67. -Wundef # clang 1.0 gcc 2.95
  68. -Wunused # clang 1.1 gcc 2.95
  69. -Wwrite-strings # clang 1.0 gcc 1.4
  70. )
  71. # Always enable with clang, version dependent with gcc
  72. set(WPICKY_COMMON_OLD
  73. -Waddress # clang 2.7 gcc 4.3
  74. -Wattributes # clang 2.7 gcc 4.1
  75. -Wcast-align # clang 1.0 gcc 4.2
  76. -Wdeclaration-after-statement # clang 1.0 gcc 3.4
  77. -Wdiv-by-zero # clang 2.7 gcc 4.1
  78. -Wempty-body # clang 2.7 gcc 4.3
  79. -Wendif-labels # clang 1.0 gcc 3.3
  80. -Wfloat-equal # clang 1.0 gcc 2.96 (3.0)
  81. -Wformat-security # clang 2.7 gcc 4.1
  82. -Wignored-qualifiers # clang 2.8 gcc 4.3
  83. -Wmissing-field-initializers # clang 2.7 gcc 4.1
  84. -Wmissing-noreturn # clang 2.7 gcc 4.1
  85. -Wno-format-nonliteral # clang 1.0 gcc 2.96 (3.0)
  86. -Wno-system-headers # clang 1.0 gcc 3.0
  87. # -Wpadded # clang 2.9 gcc 4.1 # Not used because we cannot change public structs
  88. -Wold-style-definition # clang 2.7 gcc 3.4
  89. -Wredundant-decls # clang 2.7 gcc 4.1
  90. -Wsign-conversion # clang 2.9 gcc 4.3
  91. -Wno-error=sign-conversion # FIXME
  92. -Wstrict-prototypes # clang 1.0 gcc 3.3
  93. # -Wswitch-enum # clang 2.7 gcc 4.1 # Not used because this basically disallows default case
  94. -Wtype-limits # clang 2.7 gcc 4.3
  95. -Wunreachable-code # clang 2.7 gcc 4.1
  96. # -Wunused-macros # clang 2.7 gcc 4.1 # Not practical
  97. -Wunused-parameter # clang 2.7 gcc 4.1
  98. -Wvla # clang 2.8 gcc 4.3
  99. )
  100. set(WPICKY_COMMON
  101. -Wdouble-promotion # clang 3.6 gcc 4.6 appleclang 6.3
  102. -Wenum-conversion # clang 3.2 gcc 10.0 appleclang 4.6 g++ 11.0
  103. -Wpragmas # clang 3.5 gcc 4.1 appleclang 6.0
  104. -Wunused-const-variable # clang 3.4 gcc 6.0 appleclang 5.1
  105. )
  106. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  107. list(APPEND WPICKY_ENABLE
  108. ${WPICKY_COMMON_OLD}
  109. -Wshift-sign-overflow # clang 2.9
  110. -Wshorten-64-to-32 # clang 1.0
  111. -Wlanguage-extension-token # clang 3.0
  112. -Wformat=2 # clang 3.0 gcc 4.8
  113. )
  114. # Enable based on compiler version
  115. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
  116. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
  117. list(APPEND WPICKY_ENABLE
  118. ${WPICKY_COMMON}
  119. -Wunreachable-code-break # clang 3.5 appleclang 6.0
  120. -Wheader-guard # clang 3.4 appleclang 5.1
  121. -Wsometimes-uninitialized # clang 3.2 appleclang 4.6
  122. )
  123. endif()
  124. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
  125. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
  126. list(APPEND WPICKY_ENABLE
  127. -Wcomma # clang 3.9 appleclang 8.3
  128. -Wmissing-variable-declarations # clang 3.2 appleclang 4.6
  129. )
  130. endif()
  131. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
  132. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
  133. list(APPEND WPICKY_ENABLE
  134. -Wassign-enum # clang 7.0 appleclang 10.3
  135. -Wextra-semi-stmt # clang 7.0 appleclang 10.3
  136. )
  137. endif()
  138. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0) OR
  139. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 12.4))
  140. list(APPEND WPICKY_ENABLE
  141. -Wimplicit-fallthrough # clang 4.0 gcc 7.0 appleclang 12.4 # we have silencing markup for clang 10.0 and above only
  142. )
  143. endif()
  144. else() # gcc
  145. list(APPEND WPICKY_DETECT
  146. ${WPICKY_COMMON}
  147. )
  148. # Enable based on compiler version
  149. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
  150. list(APPEND WPICKY_ENABLE
  151. ${WPICKY_COMMON_OLD}
  152. -Wclobbered # gcc 4.3
  153. -Wmissing-parameter-type # gcc 4.3
  154. -Wold-style-declaration # gcc 4.3
  155. -Wstrict-aliasing=3 # gcc 4.0
  156. -Wtrampolines # gcc 4.3
  157. )
  158. endif()
  159. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW)
  160. list(APPEND WPICKY_ENABLE
  161. -Wno-pedantic-ms-format # gcc 4.5 (mingw-only)
  162. )
  163. endif()
  164. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
  165. list(APPEND WPICKY_ENABLE
  166. -Wformat=2 # clang 3.0 gcc 4.8
  167. )
  168. endif()
  169. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
  170. list(APPEND WPICKY_ENABLE
  171. -Warray-bounds=2 -ftree-vrp # clang 3.0 gcc 5.0 (clang default: -Warray-bounds)
  172. )
  173. endif()
  174. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.0)
  175. list(APPEND WPICKY_ENABLE
  176. -Wduplicated-cond # gcc 6.0
  177. -Wnull-dereference # clang 3.0 gcc 6.0 (clang default)
  178. -fdelete-null-pointer-checks
  179. -Wshift-negative-value # clang 3.7 gcc 6.0 (clang default)
  180. -Wshift-overflow=2 # clang 3.0 gcc 6.0 (clang default: -Wshift-overflow)
  181. )
  182. endif()
  183. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0)
  184. list(APPEND WPICKY_ENABLE
  185. -Walloc-zero # gcc 7.0
  186. -Wduplicated-branches # gcc 7.0
  187. -Wformat-overflow=2 # gcc 7.0
  188. -Wformat-truncation=2 # gcc 7.0
  189. -Wimplicit-fallthrough # clang 4.0 gcc 7.0
  190. -Wrestrict # gcc 7.0
  191. )
  192. endif()
  193. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
  194. list(APPEND WPICKY_ENABLE
  195. -Warith-conversion # gcc 10.0
  196. )
  197. endif()
  198. endif()
  199. #
  200. foreach(_CCOPT IN LISTS WPICKY_ENABLE)
  201. set(WPICKY "${WPICKY} ${_CCOPT}")
  202. endforeach()
  203. foreach(_CCOPT IN LISTS WPICKY_DETECT)
  204. # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
  205. # test result in.
  206. string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
  207. # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
  208. # so test for the positive form instead
  209. string(REPLACE "-Wno-" "-W" _CCOPT_ON "${_CCOPT}")
  210. check_c_compiler_flag(${_CCOPT_ON} ${_optvarname})
  211. if(${_optvarname})
  212. set(WPICKY "${WPICKY} ${_CCOPT}")
  213. endif()
  214. endforeach()
  215. endif()
  216. endif()
  217. if(WPICKY)
  218. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WPICKY}")
  219. message(STATUS "Picky compiler options:${WPICKY}")
  220. endif()