2
0

PickyWarnings.cmake 12 KB

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