-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathStaticAnalyzers.cmake
More file actions
177 lines (160 loc) · 5.47 KB
/
StaticAnalyzers.cmake
File metadata and controls
177 lines (160 loc) · 5.47 KB
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
# Enable static analysis with Cppcheck
macro(enable_cppcheck CPPCHECK_OPTIONS)
find_program(CPPCHECK cppcheck)
if(CPPCHECK)
if(CMAKE_GENERATOR MATCHES ".*Visual Studio.*")
set(CPPCHECK_TEMPLATE "vs")
else()
set(CPPCHECK_TEMPLATE "gcc")
endif()
if("${CPPCHECK_OPTIONS}" STREQUAL "")
# Enable all warnings that are actionable by the user of this toolset
# style should enable the other 3, but we'll be explicit just in case
set(CMAKE_CXX_CPPCHECK
${CPPCHECK}
--template=${CPPCHECK_TEMPLATE}
--enable=style,performance,warning,portability
--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)
else()
# if the user provides a CPPCHECK_OPTIONS with a template specified, it will override this template
set(CMAKE_CXX_CPPCHECK ${CPPCHECK} --template=${CPPCHECK_TEMPLATE} ${CPPCHECK_OPTIONS})
endif()
if(WARNINGS_AS_ERRORS)
list(APPEND CMAKE_CXX_CPPCHECK --error-exitcode=2)
endif()
# C cppcheck
set(CMAKE_C_CPPCHECK ${CMAKE_CXX_CPPCHECK})
if(NOT
"${CMAKE_CXX_STANDARD}"
STREQUAL
"")
set(CMAKE_CXX_CPPCHECK ${CMAKE_CXX_CPPCHECK} --std=c++${CMAKE_CXX_STANDARD})
endif()
if(NOT
"${CMAKE_C_STANDARD}"
STREQUAL
"")
set(CMAKE_C_CPPCHECK ${CMAKE_C_CPPCHECK} --std=c${CMAKE_C_STANDARD})
endif()
else()
message(${WARNING_MESSAGE} "cppcheck requested but executable not found")
endif()
endmacro()
# Enable static analysis with clang-tidy
macro(enable_clang_tidy)
# https://github.com/ejfitzgerald/clang-tidy-cache
find_program(
CLANGTIDY_CACHE
NAMES "clang-tidy-cache"
"clang-tidy-cache-windows-amd64"
"clang-tidy-cache-linux-amd64"
"clang-tidy-cache-darwin-amd64")
if(CLANGTIDY_CACHE)
# use clang-tidy-cache if found
set($ENV{CLANG_TIDY_CACHE_BINARY} ${CLANGTIDY_CACHE})
set(CLANGTIDY
${CLANGTIDY_CACHE}
-p
"${CMAKE_CURRENT_BINARY_DIR}"
--export-fixes
"${CMAKE_CURRENT_BINARY_DIR}/clang_tidy_fixes.yaml")
else()
# otherwise use clang-tidy directly
find_program(CLANGTIDY clang-tidy)
endif()
if(CLANGTIDY)
# clang-tidy only works with clang when PCH is enabled
if((NOT
CMAKE_CXX_COMPILER_ID
MATCHES
".*Clang"
OR (NOT
CMAKE_C_COMPILER_ID
MATCHES
".*Clang"
)
)
AND ${ProjectOptions_ENABLE_PCH})
message(
${WARNING_MESSAGE}
"clang-tidy cannot be enabled with non-clang compiler and PCH, clang-tidy fails to handle gcc's PCH file. Disabling PCH..."
)
set(ProjectOptions_ENABLE_PCH OFF)
endif()
# construct the clang-tidy command line
set(CMAKE_CXX_CLANG_TIDY ${CLANGTIDY} -extra-arg=-Wno-unknown-warning-option)
# set warnings as errors
if(WARNINGS_AS_ERRORS)
list(APPEND CMAKE_CXX_CLANG_TIDY -warnings-as-errors=*)
endif()
# C clang-tidy
set(CMAKE_C_CLANG_TIDY ${CMAKE_CXX_CLANG_TIDY})
# set C++ standard
if(NOT
"${CMAKE_CXX_STANDARD}"
STREQUAL
"")
if("${CMAKE_CXX_CLANG_TIDY_DRIVER_MODE}" STREQUAL "cl")
set(CMAKE_CXX_CLANG_TIDY ${CMAKE_CXX_CLANG_TIDY} -extra-arg=/std:c++${CMAKE_CXX_STANDARD})
else()
set(CMAKE_CXX_CLANG_TIDY ${CMAKE_CXX_CLANG_TIDY} -extra-arg=-std=c++${CMAKE_CXX_STANDARD})
endif()
endif()
# set C standard
if(NOT
"${CMAKE_C_STANDARD}"
STREQUAL
"")
if("${CMAKE_C_CLANG_TIDY_DRIVER_MODE}" STREQUAL "cl")
set(CMAKE_C_CLANG_TIDY ${CMAKE_C_CLANG_TIDY} -extra-arg=/std:c${CMAKE_C_STANDARD})
else()
set(CMAKE_C_CLANG_TIDY ${CMAKE_C_CLANG_TIDY} -extra-arg=-std=c${CMAKE_C_STANDARD})
endif()
endif()
else()
message(${WARNING_MESSAGE} "clang-tidy requested but executable not found")
endif()
endmacro()
# Enable static analysis inside Visual Studio IDE
macro(enable_vs_analysis VS_ANALYSIS_RULESET)
if("${VS_ANALYSIS_RULESET}" STREQUAL "")
# See for other rulesets: C:\Program Files (x86)\Microsoft Visual Studio\20xx\xx\Team Tools\Static Analysis Tools\Rule Sets\
set(VS_ANALYSIS_RULESET "AllRules.ruleset")
endif()
if(NOT
"${CMAKE_CXX_CLANG_TIDY}"
STREQUAL
"")
set(_VS_CLANG_TIDY "true")
else()
set(_VS_CLANG_TIDY "false")
endif()
if(CMAKE_GENERATOR MATCHES "Visual Studio")
get_all_targets(_targets_list)
foreach(target IN LISTS ${_targets_list})
set_target_properties(
${target}
PROPERTIES
VS_GLOBAL_EnableMicrosoftCodeAnalysis true
VS_GLOBAL_CodeAnalysisRuleSet "${VS_ANALYSIS_RULESET}"
VS_GLOBAL_EnableClangTidyCodeAnalysis "${_VS_CLANG_TIDY}"
# TODO(disabled) This is set to false deliberately. The compiler warnings are already given in the CompilerWarnings.cmake file
# VS_GLOBAL_RunCodeAnalysis false
)
endforeach()
endif()
endmacro()
# Enable static analysis with include-what-you-use
macro(enable_include_what_you_use)
find_program(INCLUDE_WHAT_YOU_USE include-what-you-use)
if(INCLUDE_WHAT_YOU_USE)
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${INCLUDE_WHAT_YOU_USE})
else()
message(${WARNING_MESSAGE} "include-what-you-use requested but executable not found")
endif()
endmacro()