PickyWarnings.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. -Winline # clang 1.0 gcc 1.0
  68. -Wmissing-declarations # clang 1.0 gcc 2.7
  69. -Wmissing-prototypes # clang 1.0 gcc 1.0
  70. -Wnested-externs # clang 1.0 gcc 2.7
  71. -Wno-long-long # clang 1.0 gcc 2.95
  72. -Wno-multichar # clang 1.0 gcc 2.95
  73. -Wpointer-arith # clang 1.0 gcc 1.4
  74. -Wshadow # clang 1.0 gcc 2.95
  75. -Wsign-compare # clang 1.0 gcc 2.95
  76. -Wundef # clang 1.0 gcc 2.95
  77. -Wunused # clang 1.1 gcc 2.95
  78. -Wwrite-strings # clang 1.0 gcc 1.4
  79. )
  80. # Always enable with clang, version dependent with gcc
  81. set(_picky_common_old
  82. -Waddress # clang 2.7 gcc 4.3
  83. -Wattributes # clang 2.7 gcc 4.1
  84. -Wcast-align # clang 1.0 gcc 4.2
  85. -Wdeclaration-after-statement # clang 1.0 gcc 3.4
  86. -Wdiv-by-zero # clang 2.7 gcc 4.1
  87. -Wempty-body # clang 2.7 gcc 4.3
  88. -Wendif-labels # clang 1.0 gcc 3.3
  89. -Wfloat-equal # clang 1.0 gcc 2.96 (3.0)
  90. -Wformat-security # clang 2.7 gcc 4.1
  91. -Wignored-qualifiers # clang 2.8 gcc 4.3
  92. -Wmissing-field-initializers # clang 2.7 gcc 4.1
  93. -Wmissing-noreturn # clang 2.7 gcc 4.1
  94. -Wno-format-nonliteral # clang 1.0 gcc 2.96 (3.0)
  95. -Wno-system-headers # clang 1.0 gcc 3.0
  96. # -Wpadded # clang 2.9 gcc 4.1 # Not used: We cannot change public structs
  97. -Wold-style-definition # clang 2.7 gcc 3.4
  98. -Wredundant-decls # clang 2.7 gcc 4.1
  99. -Wsign-conversion # clang 2.9 gcc 4.3
  100. -Wno-error=sign-conversion # FIXME
  101. -Wstrict-prototypes # clang 1.0 gcc 3.3
  102. # -Wswitch-enum # clang 2.7 gcc 4.1 # Not used: It basically disallows default case
  103. -Wtype-limits # clang 2.7 gcc 4.3
  104. -Wunreachable-code # clang 2.7 gcc 4.1
  105. # -Wunused-macros # clang 2.7 gcc 4.1 # Not practical
  106. -Wunused-parameter # clang 2.7 gcc 4.1
  107. -Wvla # clang 2.8 gcc 4.3
  108. )
  109. set(_picky_common
  110. -Wdouble-promotion # clang 3.6 gcc 4.6 appleclang 6.3
  111. -Wenum-conversion # clang 3.2 gcc 10.0 appleclang 4.6 g++ 11.0
  112. -Wpragmas # clang 3.5 gcc 4.1 appleclang 6.0
  113. -Wunused-const-variable # clang 3.4 gcc 6.0 appleclang 5.1
  114. )
  115. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  116. list(APPEND _picky_enable
  117. ${_picky_common_old}
  118. -Wshift-sign-overflow # clang 2.9
  119. -Wshorten-64-to-32 # clang 1.0
  120. -Wformat=2 # clang 3.0 gcc 4.8
  121. )
  122. if(NOT MSVC)
  123. list(APPEND _picky_enable
  124. -Wlanguage-extension-token # clang 3.0
  125. )
  126. endif()
  127. # Enable based on compiler version
  128. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
  129. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
  130. list(APPEND _picky_enable
  131. ${_picky_common}
  132. # -Wunreachable-code-break # clang 3.5 appleclang 6.0 # Not used: Silent in "unity" builds
  133. -Wheader-guard # clang 3.4 appleclang 5.1
  134. -Wsometimes-uninitialized # clang 3.2 appleclang 4.6
  135. )
  136. endif()
  137. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
  138. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
  139. list(APPEND _picky_enable
  140. -Wcomma # clang 3.9 appleclang 8.3
  141. -Wmissing-variable-declarations # clang 3.2 appleclang 4.6
  142. )
  143. endif()
  144. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
  145. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
  146. list(APPEND _picky_enable
  147. -Wassign-enum # clang 7.0 appleclang 10.3
  148. -Wextra-semi-stmt # clang 7.0 appleclang 10.3
  149. )
  150. endif()
  151. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0) OR
  152. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 12.4))
  153. list(APPEND _picky_enable
  154. -Wimplicit-fallthrough # clang 4.0 gcc 7.0 appleclang 12.4 # We do silencing for clang 10.0 and above only
  155. )
  156. endif()
  157. else() # gcc
  158. list(APPEND _picky_detect
  159. ${_picky_common}
  160. )
  161. # Enable based on compiler version
  162. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
  163. list(APPEND _picky_enable
  164. ${_picky_common_old}
  165. -Wclobbered # gcc 4.3
  166. -Wmissing-parameter-type # gcc 4.3
  167. -Wold-style-declaration # gcc 4.3
  168. -Wstrict-aliasing=3 # gcc 4.0
  169. -Wtrampolines # gcc 4.3
  170. )
  171. endif()
  172. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW)
  173. list(APPEND _picky_enable
  174. -Wno-pedantic-ms-format # gcc 4.5 (MinGW-only)
  175. )
  176. endif()
  177. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
  178. list(APPEND _picky_enable
  179. -Wformat=2 # clang 3.0 gcc 4.8
  180. )
  181. endif()
  182. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
  183. list(APPEND _picky_enable
  184. -Warray-bounds=2 -ftree-vrp # clang 3.0 gcc 5.0 (clang default: -Warray-bounds)
  185. )
  186. endif()
  187. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.0)
  188. list(APPEND _picky_enable
  189. -Wduplicated-cond # gcc 6.0
  190. -Wnull-dereference # clang 3.0 gcc 6.0 (clang default)
  191. -fdelete-null-pointer-checks
  192. -Wshift-negative-value # clang 3.7 gcc 6.0 (clang default)
  193. -Wshift-overflow=2 # clang 3.0 gcc 6.0 (clang default: -Wshift-overflow)
  194. )
  195. endif()
  196. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0)
  197. list(APPEND _picky_enable
  198. -Walloc-zero # gcc 7.0
  199. -Wduplicated-branches # gcc 7.0
  200. -Wformat-truncation=2 # gcc 7.0
  201. -Wimplicit-fallthrough # clang 4.0 gcc 7.0
  202. -Wrestrict # gcc 7.0
  203. )
  204. endif()
  205. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
  206. list(APPEND _picky_enable
  207. -Warith-conversion # gcc 10.0
  208. )
  209. endif()
  210. endif()
  211. #
  212. foreach(_ccopt IN LISTS _picky_enable)
  213. list(APPEND _picky "${_ccopt}")
  214. endforeach()
  215. foreach(_ccopt IN LISTS _picky_detect)
  216. # Use a unique variable name 1. for meaningful log output 2. to have a fresh, undefined variable for each detection
  217. string(MAKE_C_IDENTIFIER "OPT${_ccopt}" _optvarname)
  218. # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
  219. # so test for the positive form instead
  220. string(REPLACE "-Wno-" "-W" _ccopt_on "${_ccopt}")
  221. check_c_compiler_flag(${_ccopt_on} ${_optvarname})
  222. if(${_optvarname})
  223. list(APPEND _picky "${_ccopt}")
  224. endif()
  225. endforeach()
  226. endif()
  227. endif()
  228. # clang-cl
  229. if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND MSVC)
  230. list(APPEND _picky "-Wno-language-extension-token") # Allow __int64
  231. set(_picky_tmp "")
  232. foreach(_ccopt IN LISTS _picky)
  233. # Prefix -Wall, otherwise clang-cl interprets it as an MSVC option and translates it to -Weverything
  234. if(_ccopt MATCHES "^-W" AND NOT _ccopt STREQUAL "-Wall")
  235. list(APPEND _picky_tmp ${_ccopt})
  236. else()
  237. list(APPEND _picky_tmp "/clang:${_ccopt}")
  238. endif()
  239. endforeach()
  240. set(_picky ${_picky_tmp})
  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()