PickyWarnings.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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(APPLE AND
  34. (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
  35. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
  36. set(WPICKY "${WPICKY} -Werror=partial-availability") # clang 3.6 appleclang 6.3
  37. endif()
  38. if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
  39. set(WPICKY "${WPICKY} -Werror-implicit-function-declaration") # clang 1.0 gcc 2.95
  40. endif()
  41. if(PICKY_COMPILER)
  42. if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
  43. # https://clang.llvm.org/docs/DiagnosticsReference.html
  44. # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  45. # WPICKY_ENABLE = Options we want to enable as-is.
  46. # WPICKY_DETECT = Options we want to test first and enable if available.
  47. # Prefer the -Wextra alias with clang.
  48. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  49. set(WPICKY_ENABLE "-Wextra")
  50. else()
  51. set(WPICKY_ENABLE "-W")
  52. endif()
  53. list(APPEND WPICKY_ENABLE
  54. -Wall -pedantic
  55. )
  56. # ----------------------------------
  57. # Add new options here, if in doubt:
  58. # ----------------------------------
  59. set(WPICKY_DETECT
  60. )
  61. # Assume these options always exist with both clang and gcc.
  62. # Require clang 3.0 / gcc 2.95 or later.
  63. list(APPEND WPICKY_ENABLE
  64. -Wbad-function-cast # clang 2.7 gcc 2.95
  65. -Wconversion # clang 2.7 gcc 2.95
  66. -Winline # clang 1.0 gcc 1.0
  67. -Wmissing-declarations # clang 1.0 gcc 2.7
  68. -Wmissing-prototypes # clang 1.0 gcc 1.0
  69. -Wnested-externs # clang 1.0 gcc 2.7
  70. -Wno-long-long # clang 1.0 gcc 2.95
  71. -Wno-multichar # clang 1.0 gcc 2.95
  72. -Wpointer-arith # clang 1.0 gcc 1.4
  73. -Wshadow # clang 1.0 gcc 2.95
  74. -Wsign-compare # clang 1.0 gcc 2.95
  75. -Wundef # clang 1.0 gcc 2.95
  76. -Wunused # clang 1.1 gcc 2.95
  77. -Wwrite-strings # clang 1.0 gcc 1.4
  78. )
  79. # Always enable with clang, version dependent with gcc
  80. set(WPICKY_COMMON_OLD
  81. -Waddress # clang 2.7 gcc 4.3
  82. -Wattributes # clang 2.7 gcc 4.1
  83. -Wcast-align # clang 1.0 gcc 4.2
  84. -Wdeclaration-after-statement # clang 1.0 gcc 3.4
  85. -Wdiv-by-zero # clang 2.7 gcc 4.1
  86. -Wempty-body # clang 2.7 gcc 4.3
  87. -Wendif-labels # clang 1.0 gcc 3.3
  88. -Wfloat-equal # clang 1.0 gcc 2.96 (3.0)
  89. -Wformat-security # clang 2.7 gcc 4.1
  90. -Wignored-qualifiers # clang 2.8 gcc 4.3
  91. -Wmissing-field-initializers # clang 2.7 gcc 4.1
  92. -Wmissing-noreturn # clang 2.7 gcc 4.1
  93. -Wno-format-nonliteral # clang 1.0 gcc 2.96 (3.0)
  94. -Wno-system-headers # clang 1.0 gcc 3.0
  95. # -Wpadded # clang 2.9 gcc 4.1 # Not used because we cannot change public structs
  96. -Wold-style-definition # clang 2.7 gcc 3.4
  97. -Wredundant-decls # clang 2.7 gcc 4.1
  98. -Wsign-conversion # clang 2.9 gcc 4.3
  99. -Wno-error=sign-conversion # FIXME
  100. -Wstrict-prototypes # clang 1.0 gcc 3.3
  101. # -Wswitch-enum # clang 2.7 gcc 4.1 # Not used because this basically disallows default case
  102. -Wtype-limits # clang 2.7 gcc 4.3
  103. -Wunreachable-code # clang 2.7 gcc 4.1
  104. # -Wunused-macros # clang 2.7 gcc 4.1 # Not practical
  105. -Wunused-parameter # clang 2.7 gcc 4.1
  106. -Wvla # clang 2.8 gcc 4.3
  107. )
  108. set(WPICKY_COMMON
  109. -Wdouble-promotion # clang 3.6 gcc 4.6 appleclang 6.3
  110. -Wenum-conversion # clang 3.2 gcc 10.0 appleclang 4.6 g++ 11.0
  111. -Wpragmas # clang 3.5 gcc 4.1 appleclang 6.0
  112. -Wunused-const-variable # clang 3.4 gcc 6.0 appleclang 5.1
  113. )
  114. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  115. list(APPEND WPICKY_ENABLE
  116. ${WPICKY_COMMON_OLD}
  117. -Wshift-sign-overflow # clang 2.9
  118. -Wshorten-64-to-32 # clang 1.0
  119. -Wlanguage-extension-token # clang 3.0
  120. -Wformat=2 # clang 3.0 gcc 4.8
  121. )
  122. # Enable based on compiler version
  123. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
  124. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
  125. list(APPEND WPICKY_ENABLE
  126. ${WPICKY_COMMON}
  127. -Wunreachable-code-break # clang 3.5 appleclang 6.0
  128. -Wheader-guard # clang 3.4 appleclang 5.1
  129. -Wsometimes-uninitialized # clang 3.2 appleclang 4.6
  130. )
  131. endif()
  132. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
  133. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
  134. list(APPEND WPICKY_ENABLE
  135. -Wcomma # clang 3.9 appleclang 8.3
  136. -Wmissing-variable-declarations # clang 3.2 appleclang 4.6
  137. )
  138. endif()
  139. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
  140. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
  141. list(APPEND WPICKY_ENABLE
  142. -Wassign-enum # clang 7.0 appleclang 10.3
  143. -Wextra-semi-stmt # clang 7.0 appleclang 10.3
  144. )
  145. endif()
  146. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0) OR
  147. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 12.4))
  148. list(APPEND WPICKY_ENABLE
  149. -Wimplicit-fallthrough # clang 4.0 gcc 7.0 appleclang 12.4 # we have silencing markup for clang 10.0 and above only
  150. )
  151. endif()
  152. else() # gcc
  153. list(APPEND WPICKY_DETECT
  154. ${WPICKY_COMMON}
  155. )
  156. # Enable based on compiler version
  157. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
  158. list(APPEND WPICKY_ENABLE
  159. ${WPICKY_COMMON_OLD}
  160. -Wclobbered # gcc 4.3
  161. -Wmissing-parameter-type # gcc 4.3
  162. -Wold-style-declaration # gcc 4.3
  163. -Wstrict-aliasing=3 # gcc 4.0
  164. -Wtrampolines # gcc 4.3
  165. )
  166. endif()
  167. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW)
  168. list(APPEND WPICKY_ENABLE
  169. -Wno-pedantic-ms-format # gcc 4.5 (mingw-only)
  170. )
  171. endif()
  172. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
  173. list(APPEND WPICKY_ENABLE
  174. -Wformat=2 # clang 3.0 gcc 4.8
  175. )
  176. endif()
  177. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
  178. list(APPEND WPICKY_ENABLE
  179. -Warray-bounds=2 -ftree-vrp # clang 3.0 gcc 5.0 (clang default: -Warray-bounds)
  180. )
  181. endif()
  182. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.0)
  183. list(APPEND WPICKY_ENABLE
  184. -Wduplicated-cond # gcc 6.0
  185. -Wnull-dereference # clang 3.0 gcc 6.0 (clang default)
  186. -fdelete-null-pointer-checks
  187. -Wshift-negative-value # clang 3.7 gcc 6.0 (clang default)
  188. -Wshift-overflow=2 # clang 3.0 gcc 6.0 (clang default: -Wshift-overflow)
  189. )
  190. endif()
  191. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0)
  192. list(APPEND WPICKY_ENABLE
  193. -Walloc-zero # gcc 7.0
  194. -Wduplicated-branches # gcc 7.0
  195. -Wformat-overflow=2 # gcc 7.0
  196. -Wformat-truncation=2 # gcc 7.0
  197. -Wimplicit-fallthrough # clang 4.0 gcc 7.0
  198. -Wrestrict # gcc 7.0
  199. )
  200. endif()
  201. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
  202. list(APPEND WPICKY_ENABLE
  203. -Warith-conversion # gcc 10.0
  204. )
  205. endif()
  206. endif()
  207. #
  208. foreach(_CCOPT IN LISTS WPICKY_ENABLE)
  209. set(WPICKY "${WPICKY} ${_CCOPT}")
  210. endforeach()
  211. foreach(_CCOPT IN LISTS WPICKY_DETECT)
  212. # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
  213. # test result in.
  214. string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
  215. # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
  216. # so test for the positive form instead
  217. string(REPLACE "-Wno-" "-W" _CCOPT_ON "${_CCOPT}")
  218. check_c_compiler_flag(${_CCOPT_ON} ${_optvarname})
  219. if(${_optvarname})
  220. set(WPICKY "${WPICKY} ${_CCOPT}")
  221. endif()
  222. endforeach()
  223. endif()
  224. endif()
  225. if(WPICKY)
  226. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WPICKY}")
  227. message(STATUS "Picky compiler options:${WPICKY}")
  228. endif()