Skip to content

Commit 45b279e

Browse files
committed
feat: support the Visual Studio IDE code analysis
1 parent 3edb28b commit 45b279e

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ project_options(
3535
ENABLE_CACHE
3636
ENABLE_CPPCHECK
3737
ENABLE_CLANG_TIDY
38+
ENABLE_VS_ANALYSIS
3839
# ENABLE_CONAN
3940
# ENABLE_IPO
4041
# ENABLE_DOXYGEN
@@ -51,7 +52,6 @@ project_options(
5152
# ENABLE_USER_LINKER
5253
# ENABLE_BUILD_WITH_TIME_TRACE
5354
# ENABLE_UNITY
54-
# CONAN_OPTIONS
5555
)
5656
```
5757

@@ -156,6 +156,7 @@ It accepts the following named flags:
156156
- `ENABLE_CACHE`: Enable cache if available
157157
- `ENABLE_CPPCHECK`: Enable static analysis with Cppcheck
158158
- `ENABLE_CLANG_TIDY`: Enable static analysis with clang-tidy
159+
- `ENABLE_VS_ANALYSIS`: Enable Visual Studio IDE code analysis if the generator is Visual Studio.
159160
- `ENABLE_CONAN`: Use Conan for dependency management
160161
- `ENABLE_IPO`: Enable Interprocedural Optimization (Link Time Optimization, LTO) in the release build
161162
- `ENABLE_COVERAGE`: Enable coverage reporting for gcc/clang
@@ -181,6 +182,7 @@ It gets the following named parameters that can have different values in front o
181182
- `GCC_WARNINGS`: Override the defaults for the GCC warnings
182183
- `CUDA_WARNINGS`: Override the defaults for the CUDA warnings
183184
- `CPPCHECK_WARNINGS`: Override the defaults for the options passed to cppcheck
185+
- `VS_ANALYSIS_RULESET`: Override the defaults for the code analysis rule set in Visual Studio.
184186
- `CONAN_OPTIONS`: Extra Conan options
185187

186188
## `run_vcpkg` function

src/DynamicProjectOptions.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ set(options
6767
"ENABLE_CACHE\;${MAKEFILE_OR_NINJA}\;${MAKEFILE_OR_NINJA}\;Enable ccache on Unix"
6868
"WARNINGS_AS_ERRORS\;OFF\;ON\;Treat warnings as Errors"
6969
"ENABLE_CLANG_TIDY\;OFF\;${MAKEFILE_OR_NINJA}\;Enable clang-tidy analysis during compilation"
70+
"ENABLE_VS_ANALYSIS\;ON\;ON\;Enable Visual Studio IDE code analysis if the generator is Visual Studio."
7071
"ENABLE_CONAN\;OFF\;OFF\;Automatically integrate Conan for package management"
7172
"ENABLE_COVERAGE\;OFF\;OFF\;Analyze and report on coverage"
7273
"ENABLE_SANITIZER_ADDRESS\;OFF\;${SUPPORTS_ASAN}\;Make memory errors into hard runtime errors (windows/linux/macos)"

src/Index.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ macro(project_options)
5050
ENABLE_COVERAGE
5151
ENABLE_CPPCHECK
5252
ENABLE_CLANG_TIDY
53+
ENABLE_VS_ANALYSIS
5354
ENABLE_INCLUDE_WHAT_YOU_USE
5455
ENABLE_CACHE
5556
ENABLE_PCH
@@ -65,7 +66,7 @@ macro(project_options)
6566
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
6667
ENABLE_SANITIZER_THREAD
6768
ENABLE_SANITIZER_MEMORY)
68-
set(oneValueArgs DOXYGEN_THEME)
69+
set(oneValueArgs DOXYGEN_THEME VS_ANALYSIS_RULESET)
6970
set(multiValueArgs
7071
MSVC_WARNINGS
7172
CLANG_WARNINGS
@@ -161,6 +162,10 @@ macro(project_options)
161162
enable_clang_tidy()
162163
endif()
163164

165+
if(${ProjectOptions_ENABLE_VS_ANALYSIS})
166+
enable_vs_analysis("${ProjectOptions_VS_ANALYSIS_RULESET}")
167+
endif()
168+
164169
if(${ProjectOptions_ENABLE_INCLUDE_WHAT_YOU_USE})
165170
enable_include_what_you_use()
166171
endif()

src/StaticAnalyzers.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,34 @@ macro(enable_clang_tidy)
7474
endif()
7575
endmacro()
7676

77+
macro(enable_vs_analysis VS_ANALYSIS_RULESET)
78+
if("${VS_ANALYSIS_RULESET}" STREQUAL "")
79+
# See for other rulesets: C:\Program Files (x86)\Microsoft Visual Studio\20xx\xx\Team Tools\Static Analysis Tools\Rule Sets\
80+
set(VS_ANALYSIS_RULESET "AllRules.ruleset")
81+
endif()
82+
if(NOT
83+
"${CMAKE_CXX_CLANG_TIDY}"
84+
STREQUAL
85+
"")
86+
set(_VS_CLANG_TIDY "true")
87+
else()
88+
set(_VS_CLANG_TIDY "false")
89+
endif()
90+
if(CMAKE_GENERATOR MATCHES "Visual Studio")
91+
get_all_targets(_targets_list)
92+
foreach(target IN LISTS ${_targets_list})
93+
set_target_properties(
94+
${target}
95+
PROPERTIES
96+
VS_GLOBAL_EnableMicrosoftCodeAnalysis true
97+
VS_GLOBAL_CodeAnalysisRuleSet "${VS_ANALYSIS_RULESET}"
98+
VS_GLOBAL_EnableClangTidyCodeAnalysis "${_VS_CLANG_TIDY}"
99+
# This is set to false deliberately. The compiler warnings are already given in the CompilerWarnings.cmake file
100+
VS_GLOBAL_RunCodeAnalysis false)
101+
endforeach()
102+
endif()
103+
endmacro()
104+
77105
macro(enable_include_what_you_use)
78106
find_program(INCLUDE_WHAT_YOU_USE include-what-you-use)
79107
if(INCLUDE_WHAT_YOU_USE)

0 commit comments

Comments
 (0)