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. set(_picky "")
  26. if(CURL_WERROR AND
  27. ((CMAKE_COMPILER_IS_GNUCC AND
  28. NOT DOS AND # Watt-32 headers use the '#include_next' GCC extension
  29. NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0 AND
  30. NOT CMAKE_VERSION VERSION_LESS 3.23.0) OR # to avoid check_symbol_exists() conflicting with GCC -pedantic-errors
  31. CMAKE_C_COMPILER_ID MATCHES "Clang"))
  32. list(APPEND _picky "-pedantic-errors")
  33. endif()
  34. if(APPLE AND
  35. (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
  36. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
  37. list(APPEND _picky "-Werror=partial-availability") # clang 3.6 appleclang 6.3
  38. endif()
  39. if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
  40. list(APPEND _picky "-Werror-implicit-function-declaration") # clang 1.0 gcc 2.95
  41. endif()
  42. if(PICKY_COMPILER)
  43. if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
  44. # https://clang.llvm.org/docs/DiagnosticsReference.html
  45. # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  46. # _picky_enable = Options we want to enable as-is.
  47. # _picky_detect = Options we want to test first and enable if available.
  48. # Prefer the -Wextra alias with clang.
  49. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  50. set(_picky_enable "-Wextra")
  51. else()
  52. set(_picky_enable "-W")
  53. endif()
  54. list(APPEND _picky_enable
  55. -Wall -pedantic
  56. )
  57. # ----------------------------------
  58. # Add new options here, if in doubt:
  59. # ----------------------------------
  60. set(_picky_detect
  61. )
  62. # Assume these options always exist with both clang and gcc.
  63. # Require clang 3.0 / gcc 2.95 or later.
  64. list(APPEND _picky_enable
  65. -Wbad-function-cast # clang 2.7 gcc 2.95
  66. -Wconversion # clang 2.7 gcc 2.95
  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(_picky_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: 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: It 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(_picky_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 _picky_enable
  116. ${_picky_common_old}
  117. -Wshift-sign-overflow # clang 2.9
  118. -Wshorten-64-to-32 # clang 1.0
  119. -Wformat=2 # clang 3.0 gcc 4.8
  120. )
  121. if(NOT MSVC)
  122. list(APPEND _picky_enable
  123. -Wlanguage-extension-token # clang 3.0
  124. )
  125. endif()
  126. # Enable based on compiler version
  127. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
  128. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
  129. list(APPEND _picky_enable
  130. ${_picky_common}
  131. # -Wunreachable-code-break # clang 3.5 appleclang 6.0 # Not used: Silent in "unity" builds
  132. -Wheader-guard # clang 3.4 appleclang 5.1
  133. -Wsometimes-uninitialized # clang 3.2 appleclang 4.6
  134. )
  135. endif()
  136. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
  137. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
  138. list(APPEND _picky_enable
  139. -Wcomma # clang 3.9 appleclang 8.3
  140. -Wmissing-variable-declarations # clang 3.2 appleclang 4.6
  141. )
  142. endif()
  143. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
  144. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
  145. list(APPEND _picky_enable
  146. -Wassign-enum # clang 7.0 appleclang 10.3
  147. -Wextra-semi-stmt # clang 7.0 appleclang 10.3
  148. )
  149. endif()
  150. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0) OR
  151. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 12.4))
  152. list(APPEND _picky_enable
  153. -Wimplicit-fallthrough # clang 4.0 gcc 7.0 appleclang 12.4 # We do silencing for clang 10.0 and above only
  154. )
  155. endif()
  156. else() # gcc
  157. list(APPEND _picky_detect
  158. ${_picky_common}
  159. )
  160. # Enable based on compiler version
  161. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
  162. list(APPEND _picky_enable
  163. ${_picky_common_old}
  164. -Wclobbered # gcc 4.3
  165. -Wmissing-parameter-type # gcc 4.3
  166. -Wold-style-declaration # gcc 4.3
  167. -Wstrict-aliasing=3 # gcc 4.0
  168. -Wtrampolines # gcc 4.3
  169. )
  170. endif()
  171. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW)
  172. list(APPEND _picky_enable
  173. -Wno-pedantic-ms-format # gcc 4.5 (MinGW-only)
  174. )
  175. endif()
  176. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
  177. list(APPEND _picky_enable
  178. -Wformat=2 # clang 3.0 gcc 4.8
  179. )
  180. endif()
  181. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
  182. list(APPEND _picky_enable
  183. -Warray-bounds=2 -ftree-vrp # clang 3.0 gcc 5.0 (clang default: -Warray-bounds)
  184. )
  185. endif()
  186. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.0)
  187. list(APPEND _picky_enable
  188. -Wduplicated-cond # gcc 6.0
  189. -Wnull-dereference # clang 3.0 gcc 6.0 (clang default)
  190. -fdelete-null-pointer-checks
  191. -Wshift-negative-value # clang 3.7 gcc 6.0 (clang default)
  192. -Wshift-overflow=2 # clang 3.0 gcc 6.0 (clang default: -Wshift-overflow)
  193. )
  194. endif()
  195. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0)
  196. list(APPEND _picky_enable
  197. -Walloc-zero # gcc 7.0
  198. -Wduplicated-branches # gcc 7.0
  199. -Wformat-truncation=2 # gcc 7.0
  200. -Wimplicit-fallthrough # clang 4.0 gcc 7.0
  201. -Wrestrict # gcc 7.0
  202. )
  203. endif()
  204. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
  205. list(APPEND _picky_enable
  206. -Warith-conversion # gcc 10.0
  207. )
  208. endif()
  209. endif()
  210. #
  211. foreach(_ccopt IN LISTS _picky_enable)
  212. list(APPEND _picky "${_ccopt}")
  213. endforeach()
  214. foreach(_ccopt IN LISTS _picky_detect)
  215. # Use a unique variable name 1. for meaningful log output 2. to have a fresh, undefined variable for each detection
  216. string(MAKE_C_IDENTIFIER "OPT${_ccopt}" _optvarname)
  217. # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
  218. # so test for the positive form instead
  219. string(REPLACE "-Wno-" "-W" _ccopt_on "${_ccopt}")
  220. check_c_compiler_flag(${_ccopt_on} ${_optvarname})
  221. if(${_optvarname})
  222. list(APPEND _picky "${_ccopt}")
  223. endif()
  224. endforeach()
  225. endif()
  226. endif()
  227. # clang-cl
  228. if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND MSVC)
  229. list(APPEND _picky "-Wno-language-extension-token") # Allow __int64
  230. set(_picky_tmp "")
  231. foreach(_ccopt IN LISTS _picky)
  232. # Prefix -Wall, otherwise clang-cl interprets it as an MSVC option and translates it to -Weverything
  233. if(_ccopt MATCHES "^-W" AND NOT _ccopt STREQUAL "-Wall")
  234. list(APPEND _picky_tmp ${_ccopt})
  235. else()
  236. list(APPEND _picky_tmp "/clang:${_ccopt}")
  237. endif()
  238. endforeach()
  239. set(_picky ${_picky_tmp})
  240. endif()
  241. if(_picky)
  242. string(REPLACE ";" " " _picky "${_picky}")
  243. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_picky}")
  244. message(STATUS "Picky compiler options: ${_picky}")
  245. endif()