xref: /curl/CMake/PickyWarnings.cmake (revision 11d27cf3)
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###########################################################################
24include(CheckCCompilerFlag)
25
26unset(WPICKY)
27
28if(CURL_WERROR AND
29   ((CMAKE_COMPILER_IS_GNUCC AND
30     NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0 AND
31     NOT CMAKE_VERSION VERSION_LESS 3.23.0) OR  # check_symbol_exists() incompatible with GCC -pedantic-errors in earlier CMake versions
32   CMAKE_C_COMPILER_ID MATCHES "Clang"))
33  set(WPICKY "${WPICKY} -pedantic-errors")
34endif()
35
36if(PICKY_COMPILER)
37  if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
38
39    # https://clang.llvm.org/docs/DiagnosticsReference.html
40    # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
41
42    # WPICKY_ENABLE = Options we want to enable as-is.
43    # WPICKY_DETECT = Options we want to test first and enable if available.
44
45    # Prefer the -Wextra alias with clang.
46    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
47      set(WPICKY_ENABLE "-Wextra")
48    else()
49      set(WPICKY_ENABLE "-W")
50    endif()
51
52    list(APPEND WPICKY_ENABLE
53      -Wall -pedantic
54    )
55
56    # ----------------------------------
57    # Add new options here, if in doubt:
58    # ----------------------------------
59    set(WPICKY_DETECT
60    )
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 WPICKY_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
81    # Always enable with clang, version dependent with gcc
82    set(WPICKY_COMMON_OLD
83      -Waddress                            # clang  2.7  gcc  4.3
84      -Wattributes                         # clang  2.7  gcc  4.1
85      -Wcast-align                         # clang  1.0  gcc  4.2
86      -Wdeclaration-after-statement        # clang  1.0  gcc  3.4
87      -Wdiv-by-zero                        # clang  2.7  gcc  4.1
88      -Wempty-body                         # clang  2.7  gcc  4.3
89      -Wendif-labels                       # clang  1.0  gcc  3.3
90      -Wfloat-equal                        # clang  1.0  gcc  2.96 (3.0)
91      -Wformat-security                    # clang  2.7  gcc  4.1
92      -Wignored-qualifiers                 # clang  2.8  gcc  4.3
93      -Wmissing-field-initializers         # clang  2.7  gcc  4.1
94      -Wmissing-noreturn                   # clang  2.7  gcc  4.1
95      -Wno-format-nonliteral               # clang  1.0  gcc  2.96 (3.0)
96      -Wno-system-headers                  # clang  1.0  gcc  3.0
97    # -Wpadded                             # clang  2.9  gcc  4.1               # Not used because we cannot change public structs
98      -Wold-style-definition               # clang  2.7  gcc  3.4
99      -Wredundant-decls                    # clang  2.7  gcc  4.1
100      -Wsign-conversion                    # clang  2.9  gcc  4.3
101        -Wno-error=sign-conversion                                              # FIXME
102      -Wstrict-prototypes                  # clang  1.0  gcc  3.3
103    # -Wswitch-enum                        # clang  2.7  gcc  4.1               # Not used because this basically disallows default case
104      -Wtype-limits                        # clang  2.7  gcc  4.3
105      -Wunreachable-code                   # clang  2.7  gcc  4.1
106    # -Wunused-macros                      # clang  2.7  gcc  4.1               # Not practical
107      -Wunused-parameter                   # clang  2.7  gcc  4.1
108      -Wvla                                # clang  2.8  gcc  4.3
109    )
110
111    set(WPICKY_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
118    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
119      list(APPEND WPICKY_ENABLE
120        ${WPICKY_COMMON_OLD}
121        -Wshift-sign-overflow              # clang  2.9
122        -Wshorten-64-to-32                 # clang  1.0
123        -Wlanguage-extension-token         # clang  3.0
124        -Wformat=2                         # clang  3.0  gcc  4.8
125      )
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 WPICKY_ENABLE
130          ${WPICKY_COMMON}
131          -Wunreachable-code-break         # clang  3.5            appleclang  6.0
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 WPICKY_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 WPICKY_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 WPICKY_ENABLE
153          -Wimplicit-fallthrough           # clang  4.0  gcc  7.0  appleclang 12.4  # we have silencing markup for clang 10.0 and above only
154        )
155      endif()
156    else()  # gcc
157      list(APPEND WPICKY_DETECT
158        ${WPICKY_COMMON}
159      )
160      # Enable based on compiler version
161      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
162        list(APPEND WPICKY_ENABLE
163          ${WPICKY_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 WPICKY_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 WPICKY_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 WPICKY_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 WPICKY_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 WPICKY_ENABLE
197          -Walloc-zero                     #             gcc  7.0
198          -Wduplicated-branches            #             gcc  7.0
199          -Wformat-overflow=2              #             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 WPICKY_ENABLE
207          -Warith-conversion               #             gcc 10.0
208        )
209      endif()
210    endif()
211
212    #
213
214    foreach(_CCOPT IN LISTS WPICKY_ENABLE)
215      set(WPICKY "${WPICKY} ${_CCOPT}")
216    endforeach()
217
218    foreach(_CCOPT IN LISTS WPICKY_DETECT)
219      # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
220      # test result in.
221      string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
222      # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
223      # so test for the positive form instead
224      string(REPLACE "-Wno-" "-W" _CCOPT_ON "${_CCOPT}")
225      check_c_compiler_flag(${_CCOPT_ON} ${_optvarname})
226      if(${_optvarname})
227        set(WPICKY "${WPICKY} ${_CCOPT}")
228      endif()
229    endforeach()
230  endif()
231endif()
232
233if(WPICKY)
234  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WPICKY}")
235  message(STATUS "Picky compiler options:${WPICKY}")
236endif()
237