-
Notifications
You must be signed in to change notification settings - Fork 3
/
StaticAnalyzers.cmake
307 lines (263 loc) · 9.19 KB
/
StaticAnalyzers.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
phi_include_guard()
include(CMakeParseArguments)
# cppcheck
find_program(PHI_CPPCHECK_EXECUTABLE cppcheck)
# Extract version from command "cppcheck --version"
if(PHI_CPPCHECK_EXECUTABLE)
execute_process(
COMMAND ${PHI_CPPCHECK_EXECUTABLE} --version
OUTPUT_VARIABLE CPPCHECK_VERSION
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
# CPPCHECK_VERSION sample: "Cppcheck 2.7"
string(REGEX REPLACE "Cppcheck ([.0-9]+).*" "\\1" CPPCHECK_VERSION "${CPPCHECK_VERSION}")
# CPPCHECK_VERSION sample: "2.7"
# Extract version components
string(REPLACE "." ";" cppcheck_version_parts "${CPPCHECK_VERSION}")
list(LENGTH cppcheck_version_parts CPPCHECK_VERSION_COUNT)
if(CPPCHECK_VERSION_COUNT GREATER 0)
list(GET cppcheck_version_parts 0 CPPCHECK_VERSION_MAJOR)
else()
set(CPPCHECK_VERSION_MAJOR 0)
endif()
if(CPPCHECK_VERSION_COUNT GREATER 1)
list(GET cppcheck_version_parts 1 CPPCHECK_VERSION_MINOR)
else()
set(CPPCHECK_VERSION_MINOR 0)
endif()
if(CPPCHECK_VERSION_COUNT GREATER 2)
list(GET cppcheck_version_parts 2 CPPCHECK_VERSION_PATCH)
else()
set(CPPCHECK_VERSION_PATCH 0)
endif()
phi_log("Found CppCheck version ${CPPCHECK_VERSION} at \"${PHI_CPPCHECK_EXECUTABLE}\"")
else()
phi_log("CppCheck not found")
endif()
# clang-tidy
find_program(
PHI_CLANG_TIDY_EXECUTABLE
NAMES clang-tidy
clang-tidy-15
clang-tidy-14
clang-tidy-13
clang-tidy-12
clang-tidy-11
clang-tidy-10
clang-tidy-9
clang-tidy-8
clang-tidy-7
clang-tidy-6.0
clang-tidy-5.0
clang-tidy-4.0
clang-tidy-3.9
clang-tidy-3.8
clang-tidy-3.7
clang-tidy-3.6
clang-tidy-3.5
clang-tidy-3.4
clang-tidy-3.3)
if(PHI_CLANG_TIDY_EXECUTABLE)
# Extract version info
execute_process(
COMMAND ${PHI_CLANG_TIDY_EXECUTABLE} --version
OUTPUT_VARIABLE CLANG_TIDY_VERSION
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
# CLANG_TIDY_VERSION sample: "LLVM (http://llvm.org/): LLVM version 14.0.6 Optimized build.
# Default target: x86_64-pc-linux-gnu Host CPU: skylake"
string(REGEX REPLACE ".*LLVM version ([.0-9]+).*" "\\1" CLANG_TIDY_VERSION
"${CLANG_TIDY_VERSION}")
# Extract version components
string(REPLACE "." ";" clang_tidy_version_parts "${CLANG_TIDY_VERSION}")
list(LENGTH clang_tidy_version_parts CLANG_TIDY_VERSION_COUNT)
if(CLANG_TIDY_VERSION_COUNT GREATER 0)
list(GET clang_tidy_version_parts 0 CLANG_TIDY_VERSION_MAJOR)
else()
set(CLANG_TIDY_VERSION_MAJOR 0)
endif()
if(CLANG_TIDY_VERSION_COUNT GREATER 1)
list(GET clang_tidy_version_parts 1 CLANG_TIDY_VERSION_MINOR)
else()
set(CLANG_TIDY_VERSION_MINOR 0)
endif()
if(CLANG_TIDY_VERSION_COUNT GREATER 2)
list(GET clang_tidy_version_parts 2 CLANG_TIDY_VERSION_PATCH)
else()
set(CLANG_TIDY_VERSION_PATCH 0)
endif()
phi_log("Found clang-tidy version ${CLANG_TIDY_VERSION} at \"${PHI_CLANG_TIDY_EXECUTABLE}\"")
else()
phi_log("clang-tidy not found")
endif()
# include-what-you-use
find_program(PHI_IWYU_EXECUTABLE NAMES include-what-you-use iwyu)
if(PHI_IWYU_EXECUTABLE)
# Extract version info
execute_process(
COMMAND ${PHI_IWYU_EXECUTABLE} --version
OUTPUT_VARIABLE IWYU_VERSION
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
# IWYU_VERSION sample: "include-what-you-use 0.18 (git:abd5d2b) based on clang version 14.0.6
# (https://github.com/llvm/llvm-project.git f28c006a5895fc0e329fe15fead81e37457cb1d1)"
string(REGEX REPLACE "include-what-you-use ([.0-9]+).*" "\\1" IWYU_VERSION "${IWYU_VERSION}")
# Extract version components
string(REPLACE "." ";" iwyu_version_parts "${IWYU_VERSION}")
list(LENGTH iwyu_version_parts IWYU_VERSION_COUNT)
if(IWYU_VERSION_COUNT GREATER 0)
list(GET iwyu_version_parts 0 IWYU_VERSION_MAJOR)
else()
set(IWYU_VERSION_MAJOR 0)
endif()
if(IWYU_VERSION_COUNT GREATER 1)
list(GET iwyu_version_parts 1 IWYU_VERSION_MINOR)
else()
set(IWYU_VERSION_MINOR 0)
endif()
if(IWYU_VERSION_COUNT GREATER 2)
list(GET iwyu_version_parts 2 IWYU_VERSION_PATCH)
else()
set(IWYU_VERSION_PATCH 0)
endif()
phi_log("Found include-what-you-use version ${IWYU_VERSION} at \"${PHI_IWYU_EXECUTABLE}\"")
else()
phi_log("include-what-you-use not found")
endif()
# TODO: Support MSVC analysis
function(phi_target_use_static_analyzers)
cmake_parse_arguments(sa "CLANG_TIDY;CPPCHECK;IWYU;ALL;SUPPORTED;WARNINGS_AS_ERRORS" "TARGET"
"CLANG_TIDY_OPTIONS;CPPCHECK_OPTIONS;IWYU_OPTIONS" ${ARGN})
# Check for require arguments
if(NOT DEFINED sa_TARGET)
phi_error(
"phi_target_use_static_analyzers: No target defined. You must specify it by using TARGET")
endif()
if(NOT TARGET ${sa_TARGET})
phi_error("phi_target_use_static_analyzers: The target \"${sa_TARGET}\" doesn't seem to exist")
endif()
# Check that we enable atleast one
if(NOT sa_CLANG_TIDY
AND NOT sa_CPPCHECK
AND NOT sa_IWYU
AND NOT sa_ALL
AND NOT sa_SUPPORTED)
phi_error(
"phi_target_use_static_analyzers: No static analyzer enabled. Specify CLANG_TIDY, CPPCHECK, IWYU, ALL or SUPPORTED to enable some."
)
endif()
# Check that ALL and SUPPORTED are mutually exclusive
if(sa_ALL AND sa_SUPPORTED)
phi_error("phi_target_use_static_analyzers: Only specify ALL or SUPPORTED not both")
endif()
# Check that ALL is mutually exclusive to all explicit options
if(sa_ALL
AND (sa_CLANG_TIDY
OR sa_CPPCHECK
OR sa_IWYU))
phi_error(
"phi_target_use_static_analyzers: Only specify ALL or one/multiple of CLANG_TIDY, CPPCHECK, IWYU but not both."
)
endif()
# Check that SUPPORTED is mutually exclusive to all explicit options
if(sa_SUPPORTED
AND (sa_CLANG_TIDY
OR sa_CPPCHECK
OR sa_IWYU))
phi_error(
"phi_target_use_static_analyzers: Only specify SUPPORTED or one/multiple of CLANG_TIDY, CPPCHECK, IWYU but not both."
)
endif()
# For ALL simply set all to true
if(sa_ALL)
set(sa_CLANG_TIDY 1)
set(sa_CPPCHECK 1)
set(sa_IWYU 1)
endif()
# For SUPPORTED only enable the ones which where found
if(sa_SUPPORTED)
# clang-tidy
if(PHI_CLANG_TIDY_EXECUTABLE)
set(sa_CLANG_TIDY 1)
endif()
# cppcheck
if(PHI_CPPCHECK_EXECUTABLE)
set(sa_CPPCHECK 1)
endif()
# include-what-you-use
if(PHI_IWYU_EXECUTABLE)
set(sa_IWYU 1)
endif()
endif()
# Enable clang-tidy for the specified target if requested
if(sa_CLANG_TIDY)
if(NOT PHI_CLANG_TIDY_EXECUTABLE)
phi_error(
"phi_target_use_static_analyzers: clang-tidy was explicitly enabled for target \"${sa_TARGET}\" but seems to be missing"
)
endif()
set(ct_cmdline ${PHI_CLANG_TIDY_EXECUTABLE} -header-filter=.*
-extra-arg=-Wno-unknown-warning-option)
# Optionally enable warnings as errors
if(sa_WARNINGS_AS_ERRORS)
list(APPEND ct_cmdline -warnings-as-errors=*)
endif()
# Optionally add user options
if(DEFINED sa_CLANG_TIDY_OPTIONS)
list(APPEND ct_cmdline ${sa_CLANG_TIDY_OPTIONS})
endif()
# Set target property
set_target_properties(${sa_TARGET} PROPERTIES CXX_CLANG_TIDY "${ct_cmdline}")
endif()
# Enable cppcheck for the specified target if requested
if(sa_CPPCHECK)
if(NOT PHI_CPPCHECK_EXECUTABLE)
phi_error(
"phi_target_use_static_analyzers: cppcheck was explicitly enabled for target \"${sa_TARGET}\" but seems to be missing"
)
endif()
if(CMAKE_GENERATOR MATCHES ".*Visual Studio.*")
set(CPPCHECK_TEMPLATE "vs")
else()
set(CPPCHECK_TEMPLATE "gcc")
endif()
set(cc_cmdline ${PHI_CPPCHECK_EXECUTABLE})
# Optionally add user options
if(DEFINED sa_CPPCHECK_OPTIONS)
# if the user provides a CPPCHECK_OPTIONS with a template specified, it will override this
# template
list(APPEND cc_cmdline --template=${CPPCHECK_TEMPLATE} ${sa_CPPCHECK_OPTIONS})
else()
# Provide a "reasonable" standard config
list(
APPEND
cc_cmdline
--template=${CPPCHECK_TEMPLATE}
--enable=all
--inline-suppr
# We cannot act on a bug/missing feature of cppcheck
--suppress=internalAstError
# if a file does not have an internalAstError, we get an unmatchedSuppression error
--suppress=unmatchedSuppression
--inconclusive)
endif()
# Optionally enable warnings as errors
if(sa_WARNINGS_AS_ERRORS)
list(APPEND cc_cmdline --error-exitcode=2)
endif()
# Set target property
set_target_properties(${sa_TARGET} PROPERTIES CXX_CPPCHECK "${cc_cmdline}")
endif()
# Enable include-what-you-use for the specified target if requested
if(sa_IWYU)
if(NOT PHI_IWYU_EXECUTABLE)
phi_error(
"phi_target_use_static_analyzers: include-what-you-use was explicitly enabled for target \"${sa_TARGET}\" but seems to be missing"
)
endif()
set(iwyu_cmdline ${PHI_IWYU_EXECUTABLE})
# Optionally add user options
if(DEFINED sa_IWYU_OPTIONS)
list(APPEND iwyu_cmdline ${sa_IWYU_OPTIONS})
endif()
# Set target property
set_target_properties(${sa_TARGET} PROPERTIES CXX_INCLUDE_WHAT_YOU_USE ${iwyu_cmdline})
endif()
endfunction()