PickyWarnings.cmake 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. if(PICKY_COMPILER)
  26. if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
  27. # https://clang.llvm.org/docs/DiagnosticsReference.html
  28. # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  29. # WPICKY_ENABLE = Options we want to enable as-is.
  30. # WPICKY_DETECT = Options we want to test first and enable if available.
  31. # Prefer the -Wextra alias with clang.
  32. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  33. set(WPICKY_ENABLE "-Wextra")
  34. else()
  35. set(WPICKY_ENABLE "-W")
  36. endif()
  37. list(APPEND WPICKY_ENABLE
  38. -Wall -pedantic
  39. )
  40. # ----------------------------------
  41. # Add new options here, if in doubt:
  42. # ----------------------------------
  43. set(WPICKY_DETECT
  44. )
  45. # Assume these options always exist with both clang and gcc.
  46. # Require clang 3.0 / gcc 2.95 or later.
  47. list(APPEND WPICKY_ENABLE
  48. -Wbad-function-cast # clang 3.0 gcc 2.95
  49. -Wconversion # clang 3.0 gcc 2.95
  50. -Winline # clang 1.0 gcc 1.0
  51. -Wmissing-declarations # clang 1.0 gcc 2.7
  52. -Wmissing-prototypes # clang 1.0 gcc 1.0
  53. -Wnested-externs # clang 1.0 gcc 2.7
  54. -Wno-long-long # clang 1.0 gcc 2.95
  55. -Wno-multichar # clang 1.0 gcc 2.95
  56. -Wpointer-arith # clang 1.0 gcc 1.4
  57. -Wshadow # clang 1.0 gcc 2.95
  58. -Wsign-compare # clang 1.0 gcc 2.95
  59. -Wundef # clang 1.0 gcc 2.95
  60. -Wunused # clang 1.1 gcc 2.95
  61. -Wwrite-strings # clang 1.0 gcc 1.4
  62. )
  63. # Always enable with clang, version dependent with gcc
  64. set(WPICKY_COMMON_OLD
  65. -Wcast-align # clang 1.0 gcc 4.2
  66. -Wdeclaration-after-statement # clang 1.0 gcc 3.4
  67. -Wempty-body # clang 3.0 gcc 4.3
  68. -Wendif-labels # clang 1.0 gcc 3.3
  69. -Wfloat-equal # clang 1.0 gcc 2.96 (3.0)
  70. -Wignored-qualifiers # clang 3.0 gcc 4.3
  71. -Wno-format-nonliteral # clang 1.0 gcc 2.96 (3.0)
  72. -Wno-sign-conversion # clang 3.0 gcc 4.3
  73. -Wno-system-headers # clang 1.0 gcc 3.0
  74. -Wstrict-prototypes # clang 1.0 gcc 3.3
  75. -Wtype-limits # clang 3.0 gcc 4.3
  76. -Wvla # clang 2.8 gcc 4.3
  77. )
  78. set(WPICKY_COMMON
  79. -Wdouble-promotion # clang 3.6 gcc 4.6 appleclang 6.3
  80. -Wenum-conversion # clang 3.2 gcc 10.0 appleclang 4.6 g++ 11.0
  81. -Wunused-const-variable # clang 3.4 gcc 6.0 appleclang 5.1
  82. )
  83. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  84. list(APPEND WPICKY_ENABLE
  85. ${WPICKY_COMMON_OLD}
  86. -Wshift-sign-overflow # clang 2.9
  87. -Wshorten-64-to-32 # clang 1.0
  88. )
  89. # Enable based on compiler version
  90. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
  91. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
  92. list(APPEND WPICKY_ENABLE
  93. ${WPICKY_COMMON}
  94. )
  95. endif()
  96. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
  97. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
  98. list(APPEND WPICKY_ENABLE
  99. -Wcomma # clang 3.9 appleclang 8.3
  100. -Wmissing-variable-declarations # clang 3.2 appleclang 4.6
  101. )
  102. endif()
  103. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
  104. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
  105. list(APPEND WPICKY_ENABLE
  106. -Wassign-enum # clang 7.0 appleclang 10.3
  107. -Wextra-semi-stmt # clang 7.0 appleclang 10.3
  108. )
  109. endif()
  110. else() # gcc
  111. list(APPEND WPICKY_DETECT
  112. ${WPICKY_COMMON}
  113. )
  114. # Enable based on compiler version
  115. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
  116. list(APPEND WPICKY_ENABLE
  117. ${WPICKY_COMMON_OLD}
  118. -Wmissing-parameter-type # gcc 4.3
  119. -Wold-style-declaration # gcc 4.3
  120. -Wstrict-aliasing=3 # gcc 4.0
  121. )
  122. endif()
  123. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW)
  124. list(APPEND WPICKY_ENABLE
  125. -Wno-pedantic-ms-format # gcc 4.5 (mingw-only)
  126. )
  127. endif()
  128. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
  129. list(APPEND WPICKY_ENABLE
  130. -Wformat=2 # clang 3.0 gcc 4.8 (clang part-default, enabling it fully causes -Wformat-nonliteral warnings)
  131. )
  132. endif()
  133. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
  134. list(APPEND WPICKY_ENABLE
  135. -Warray-bounds=2 -ftree-vrp # clang 3.0 gcc 5.0 (clang default: -Warray-bounds)
  136. )
  137. endif()
  138. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.0)
  139. list(APPEND WPICKY_ENABLE
  140. -Wduplicated-cond # gcc 6.0
  141. -Wnull-dereference # clang 3.0 gcc 6.0 (clang default)
  142. -fdelete-null-pointer-checks
  143. -Wshift-negative-value # clang 3.7 gcc 6.0 (clang default)
  144. -Wshift-overflow=2 # clang 3.0 gcc 6.0 (clang default: -Wshift-overflow)
  145. )
  146. endif()
  147. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0)
  148. list(APPEND WPICKY_ENABLE
  149. -Walloc-zero # gcc 7.0
  150. -Wduplicated-branches # gcc 7.0
  151. -Wformat-overflow=2 # gcc 7.0
  152. -Wformat-truncation=1 # gcc 7.0
  153. -Wrestrict # gcc 7.0
  154. )
  155. endif()
  156. if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
  157. list(APPEND WPICKY_ENABLE
  158. -Warith-conversion # gcc 10.0
  159. )
  160. endif()
  161. endif()
  162. #
  163. unset(WPICKY)
  164. foreach(_CCOPT ${WPICKY_ENABLE})
  165. set(WPICKY "${WPICKY} ${_CCOPT}")
  166. endforeach()
  167. foreach(_CCOPT ${WPICKY_DETECT})
  168. # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
  169. # test result in.
  170. string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
  171. # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
  172. # so test for the positive form instead
  173. string(REPLACE "-Wno-" "-W" _CCOPT_ON "${_CCOPT}")
  174. check_c_compiler_flag(${_CCOPT_ON} ${_optvarname})
  175. if(${_optvarname})
  176. set(WPICKY "${WPICKY} ${_CCOPT}")
  177. endif()
  178. endforeach()
  179. message(STATUS "Picky compiler options:${WPICKY}")
  180. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WPICKY}")
  181. endif()
  182. endif()