From 3ce77807fc7992e29dbfa958f1f0a0b77d0998f7 Mon Sep 17 00:00:00 2001 From: abeimler Date: Mon, 27 Jun 2022 22:34:34 +0200 Subject: [PATCH 1/5] feat: add disable static analysis macros (#133) --- src/StaticAnalyzers.cmake | 29 +++++++++++++++++++++++++++++ test/CMakeLists.txt | 1 + 2 files changed, 30 insertions(+) diff --git a/src/StaticAnalyzers.cmake b/src/StaticAnalyzers.cmake index c7840882..e5349eeb 100644 --- a/src/StaticAnalyzers.cmake +++ b/src/StaticAnalyzers.cmake @@ -158,3 +158,32 @@ macro(enable_include_what_you_use) message(${WARNING_MESSAGE} "include-what-you-use requested but executable not found") endif() endmacro() + + +# Disable clang-tidy for target +macro(target_disable_clang_tidy TARGET) + set_target_properties(${TARGET} PROPERTIES C_CLANG_TIDY "") + set_target_properties(${TARGET} PROPERTIES CXX_CLANG_TIDY "") +endmacro() + +# Disable cppcheck for target +macro(target_disable_cpp_check TARGET) + set_target_properties(${TARGET} PROPERTIES C_CPPCHECK "") + set_target_properties(${TARGET} PROPERTIES CXX_CPPCHECK "") +endmacro() + +# Disable static analysis for target +macro(target_disable_static_analysis TARGET) + target_disable_clang_tidy(${TARGET}) + target_disable_cpp_check(${TARGET}) + + if(CMAKE_GENERATOR MATCHES "Visual Studio") + set_target_properties( + ${TARGET} + PROPERTIES + VS_GLOBAL_EnableMicrosoftCodeAnalysis false + VS_GLOBAL_CodeAnalysisRuleSet "" + VS_GLOBAL_EnableClangTidyCodeAnalysis "" + ) + endif() +endmacro() \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f451dec1..124368e3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -81,6 +81,7 @@ set(DEPENDENCIES_CONFIGURED fmt Eigen3 docopt) foreach(DEPENDENCY ${DEPENDENCIES_CONFIGURED}) find_package(${DEPENDENCY} CONFIG REQUIRED) endforeach() +target_disable_static_analysis(Eigen3::Eigen) target_link_system_libraries( main From 9335aa72c0bd29a1eddff7e71aa03c45fda54504 Mon Sep 17 00:00:00 2001 From: Alex Beimler Date: Thu, 30 Jun 2022 16:01:55 +0200 Subject: [PATCH 2/5] feat: add target_disable_vs_analysis macro --- src/StaticAnalyzers.cmake | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/StaticAnalyzers.cmake b/src/StaticAnalyzers.cmake index e5349eeb..c43123a5 100644 --- a/src/StaticAnalyzers.cmake +++ b/src/StaticAnalyzers.cmake @@ -172,11 +172,8 @@ macro(target_disable_cpp_check TARGET) set_target_properties(${TARGET} PROPERTIES CXX_CPPCHECK "") endmacro() -# Disable static analysis for target -macro(target_disable_static_analysis TARGET) - target_disable_clang_tidy(${TARGET}) - target_disable_cpp_check(${TARGET}) - +# Disable vs analysis for target +macro(target_disable_vs_analysis TARGET) if(CMAKE_GENERATOR MATCHES "Visual Studio") set_target_properties( ${TARGET} @@ -186,4 +183,11 @@ macro(target_disable_static_analysis TARGET) VS_GLOBAL_EnableClangTidyCodeAnalysis "" ) endif() -endmacro() \ No newline at end of file +endmacro() + +# Disable static analysis for target +macro(target_disable_static_analysis TARGET) + target_disable_clang_tidy(${TARGET}) + target_disable_cpp_check(${TARGET}) + target_disable_vs_analysis(${TARGET}) +endmacro() From 2d680c9bda89a8713b7cbec152bbc46211218aa9 Mon Sep 17 00:00:00 2001 From: abeimler Date: Sun, 3 Jul 2022 22:33:45 +0200 Subject: [PATCH 3/5] fix: add checks for target_disable_static_analysis --- src/StaticAnalyzers.cmake | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/StaticAnalyzers.cmake b/src/StaticAnalyzers.cmake index c43123a5..1f9f8103 100644 --- a/src/StaticAnalyzers.cmake +++ b/src/StaticAnalyzers.cmake @@ -162,14 +162,20 @@ endmacro() # Disable clang-tidy for target macro(target_disable_clang_tidy TARGET) - set_target_properties(${TARGET} PROPERTIES C_CLANG_TIDY "") - set_target_properties(${TARGET} PROPERTIES CXX_CLANG_TIDY "") + find_program(CLANGTIDY clang-tidy) + if(CLANGTIDY) + set_target_properties(${TARGET} PROPERTIES C_CLANG_TIDY "") + set_target_properties(${TARGET} PROPERTIES CXX_CLANG_TIDY "") + endif() endmacro() # Disable cppcheck for target macro(target_disable_cpp_check TARGET) - set_target_properties(${TARGET} PROPERTIES C_CPPCHECK "") - set_target_properties(${TARGET} PROPERTIES CXX_CPPCHECK "") + find_program(CPPCHECK cppcheck) + if(CPPCHECK) + set_target_properties(${TARGET} PROPERTIES C_CPPCHECK "") + set_target_properties(${TARGET} PROPERTIES CXX_CPPCHECK "") + endif() endmacro() # Disable vs analysis for target @@ -187,7 +193,9 @@ endmacro() # Disable static analysis for target macro(target_disable_static_analysis TARGET) - target_disable_clang_tidy(${TARGET}) - target_disable_cpp_check(${TARGET}) + if(NOT CMAKE_GENERATOR MATCHES "Visual Studio") + target_disable_clang_tidy(${TARGET}) + target_disable_cpp_check(${TARGET}) + endif() target_disable_vs_analysis(${TARGET}) endmacro() From 9305474df6db280d34e77ba66154b957b7e19ee2 Mon Sep 17 00:00:00 2001 From: abeimler Date: Sun, 3 Jul 2022 23:50:28 +0200 Subject: [PATCH 4/5] fix: add new test library for static analysis(#133) --- test/CMakeLists.txt | 7 ++++++- test/libs/CMakeLists.txt | 4 ++++ test/libs/mythirdpartylib/CMakeLists.txt | 11 ++++++++++ test/libs/mythirdpartylib/include/Foo.hpp | 25 +++++++++++++++++++++++ test/libs/mythirdpartylib/src/Foo.cpp | 24 ++++++++++++++++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 test/libs/CMakeLists.txt create mode 100644 test/libs/mythirdpartylib/CMakeLists.txt create mode 100644 test/libs/mythirdpartylib/include/Foo.hpp create mode 100644 test/libs/mythirdpartylib/src/Foo.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 124368e3..c43767dc 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -81,7 +81,6 @@ set(DEPENDENCIES_CONFIGURED fmt Eigen3 docopt) foreach(DEPENDENCY ${DEPENDENCIES_CONFIGURED}) find_package(${DEPENDENCY} CONFIG REQUIRED) endforeach() -target_disable_static_analysis(Eigen3::Eigen) target_link_system_libraries( main @@ -89,6 +88,8 @@ target_link_system_libraries( fmt::fmt Eigen3::Eigen) +add_subdirectory(libs) + ## tests enable_testing() add_test(NAME main COMMAND main) @@ -116,6 +117,10 @@ target_link_system_libraries( PRIVATE fmt::fmt Eigen3::Eigen) +target_link_system_libraries( + lib2 + PRIVATE + mythirdpartylib) # package everything automatically package_project( diff --git a/test/libs/CMakeLists.txt b/test/libs/CMakeLists.txt new file mode 100644 index 00000000..26b66ab0 --- /dev/null +++ b/test/libs/CMakeLists.txt @@ -0,0 +1,4 @@ +include(GenerateExportHeader) + +add_subdirectory(mythirdpartylib) +target_disable_static_analysis(mythirdpartylib) diff --git a/test/libs/mythirdpartylib/CMakeLists.txt b/test/libs/mythirdpartylib/CMakeLists.txt new file mode 100644 index 00000000..ddc7e17b --- /dev/null +++ b/test/libs/mythirdpartylib/CMakeLists.txt @@ -0,0 +1,11 @@ + +add_library(mythirdpartylib STATIC src/Foo.cpp) +generate_export_header(mythirdpartylib) + +target_include_directories(mythirdpartylib + PUBLIC $ + $ +) +target_include_directories(mythirdpartylib + PUBLIC $ +) \ No newline at end of file diff --git a/test/libs/mythirdpartylib/include/Foo.hpp b/test/libs/mythirdpartylib/include/Foo.hpp new file mode 100644 index 00000000..24621a36 --- /dev/null +++ b/test/libs/mythirdpartylib/include/Foo.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +#include "mythirdpartylib_export.h" + +namespace mythirdpartylib { + +class MYTHIRDPARTYLIB_EXPORT Foo { +public: + Foo() = default; + + /*implicit*/ Foo(int a) : m_a(a) {} + + int a() const { return m_a; } + + void update(bool b, bool c, bool d); + void bad(std::vector& v); + +private: + int m_a; +}; + +} \ No newline at end of file diff --git a/test/libs/mythirdpartylib/src/Foo.cpp b/test/libs/mythirdpartylib/src/Foo.cpp new file mode 100644 index 00000000..f83ced37 --- /dev/null +++ b/test/libs/mythirdpartylib/src/Foo.cpp @@ -0,0 +1,24 @@ +#include "Foo.hpp" + +namespace mythirdpartylib { + +void Foo::update(bool b, bool c, bool d) { + int e = b + d; + m_a = e; +} + +void Foo::bad(std::vector& v) { + std::string val = "hello"; + int index = -1; // bad, plus should use gsl::index + for (int i = 0; i < v.size(); ++i) { + if (v[i] == val) { + index = i; + break; + } + } +} + +static Foo foo (5); +static Foo bar = 42; + +} \ No newline at end of file From 19a9c6d9cc2393fae9b4519410b02189a524f809 Mon Sep 17 00:00:00 2001 From: Alex Beimler Date: Mon, 4 Jul 2022 00:02:57 +0200 Subject: [PATCH 5/5] Update cspell.config.yaml * add "mythirdpartylib" (#133) --- cspell.config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/cspell.config.yaml b/cspell.config.yaml index 537b2ad4..0aa60cb0 100644 --- a/cspell.config.yaml +++ b/cspell.config.yaml @@ -110,3 +110,4 @@ words: - "shlib" - "vcpkg" - cppdbg + - mythirdpartylib