diff --git a/CMakeLists.txt b/CMakeLists.txt index dfc1533d737c..7631cd2f84fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ option(USE_PCH_HEADER "Use precompiled PCH header." "OFF") option(JSON_FORMAT "Build JSON formatter" "OFF") option(CATA_CCACHE "Try to find and build with ccache" "ON") option(CATA_CLANG_TIDY_PLUGIN "Build Cata's custom clang-tidy plugin" "OFF") +option(CATA_CLANG_TIDY_EXECUTABLE "Build Cata's custom clang-tidy checks as an executable" "OFF") set(CATA_CLANG_TIDY_INCLUDE_DIR "" CACHE STRING "Path to internal clang-tidy headers required for plugin (e.g. ClangTidy.h)") set(CATA_CHECK_CLANG_TIDY "" CACHE STRING "Path to check_clang_tidy.py for plugin tests") set(GIT_BINARY "" CACHE STRING "Git binary name or path.") @@ -353,7 +354,7 @@ add_subdirectory(tests) if (JSON_FORMAT) add_subdirectory(tools/format) endif() -if (CATA_CLANG_TIDY_PLUGIN) +if (CATA_CLANG_TIDY_PLUGIN OR CATA_CLANG_TIDY_EXECUTABLE) add_subdirectory(tools/clang-tidy-plugin) endif() diff --git a/doc/DEVELOPER_TOOLING.md b/doc/DEVELOPER_TOOLING.md index 05dc810f8d3e..92047d0503fd 100644 --- a/doc/DEVELOPER_TOOLING.md +++ b/doc/DEVELOPER_TOOLING.md @@ -237,78 +237,6 @@ and execute the following command inside the `/Scripts` directory pip install path/to/your/downloaded/file.whl ``` -Currently, the CDDA source is still building the custom checks as a plugin, -which unfortunately is not supported on Windows, so the following patch needs to -be applied before the custom checks can be built as an executable. - -```patch -diff --git a/tools/clang-tidy-plugin/CMakeLists.txt b/tools/clang-tidy-plugin/CMakeLists.txt -index cf0c237645..540d3e29a5 100644 ---- a/tools/clang-tidy-plugin/CMakeLists.txt -+++ b/tools/clang-tidy-plugin/CMakeLists.txt -@@ -4,7 +4,7 @@ include(ExternalProject) - find_package(LLVM REQUIRED CONFIG) - find_package(Clang REQUIRED CONFIG) - --add_library(CataAnalyzerPlugin MODULE -+add_executable(CataAnalyzerPlugin - AlmostNeverAutoCheck.cpp - AssertCheck.cpp - CataTidyModule.cpp -@@ -56,6 +56,11 @@ else () - target_include_directories(CataAnalyzerPlugin SYSTEM PRIVATE ${CATA_CLANG_TIDY_INCLUDE_DIR}) - endif () - -+target_link_libraries( -+ CataAnalyzerPlugin -+ clangTidyMain -+ ) -+ - target_compile_definitions(CataAnalyzerPlugin PRIVATE ${LLVM_DEFINITIONS}) - - # We need to turn off exceptions and RTTI to match the LLVM build. -diff --git a/tools/clang-tidy-plugin/CataTidyModule.cpp b/tools/clang-tidy-plugin/CataTidyModule.cpp -index b7cb4df22c..a83db0c60e 100644 ---- a/tools/clang-tidy-plugin/CataTidyModule.cpp -+++ b/tools/clang-tidy-plugin/CataTidyModule.cpp -@@ -18,6 +18,7 @@ - #include "TestFilenameCheck.h" - #include "TestsMustRestoreGlobalStateCheck.h" - #include "TextStyleCheck.h" -+#include "tool/ClangTidyMain.h" - #include "TranslatorCommentsCheck.h" - #include "UnsequencedCallsCheck.h" - #include "UnusedStaticsCheck.h" -@@ -80,3 +81,8 @@ X( "cata-module", "Adds Cataclysm-DDA checks." ); - - } // namespace tidy - } // namespace clang -+ -+int main( int argc, const char **argv ) -+{ -+ return clang::tidy::clangTidyMain( argc, argv ); -+} -diff --git a/tools/clang-tidy-plugin/test/lit.cfg b/tools/clang-tidy-plugin/test/lit.cfg -index 496804316a..43beb49653 100644 ---- a/tools/clang-tidy-plugin/test/lit.cfg -+++ b/tools/clang-tidy-plugin/test/lit.cfg -@@ -17,11 +17,13 @@ else: - config.plugin_build_root, 'clang-tidy-plugin-support', 'bin', - 'check_clang_tidy.py') - --cata_include = os.path.join( config.cata_source_dir, "src" ) -+cata_include = os.path.join( config.cata_source_dir, "./src" ) - - cata_plugin = os.path.join( - config.plugin_build_root, 'libCataAnalyzerPlugin.so') - -+cata_plugin = '' -+ - config.substitutions.append(('%check_clang_tidy', check_clang_tidy)) - config.substitutions.append(('%cata_include', cata_include)) - config.substitutions.append(('%cata_plugin', cata_plugin)) -``` - The next step is to run CMake to generate the compilation database. The compilation database contains compiler flags that clang-tidy uses to check the source files. @@ -320,7 +248,8 @@ if you built it with the instructions in the previous section. Then add the following CMake options to generate the compilation database (substitute values inside `<>` with the actual paths), and build the CDDA source and the custom clang-tidy executable with `mingw32-make`. In this tutorial we -run CMake and `mingw32-make` in the `build` subdirectory. +run CMake and `mingw32-make` in the `build` subdirectory. Note that `DCATA_CLANG_TIDY_EXECUTABLE` +is defined instead of `DCATA_CLANG_TIDY_PLUGIN`. ```sh -DCMAKE_EXPORT_COMPILE_COMMANDS=ON @@ -328,9 +257,9 @@ run CMake and `mingw32-make` in the `build` subdirectory. -DClang_DIR="/build/lib/cmake/clang" -DLLVM_INCLUDE_DIRS="/llvm/include" -DCLANG_INCLUDE_DIRS="/clang/include" --DCATA_CLANG_TIDY_PLUGIN=ON +-DCATA_CLANG_TIDY_EXECUTABLE=ON -DCATA_CLANG_TIDY_INCLUDE_DIR="/clang-tools-extra/clang-tidy" --DCATA_CHECK_CLANG_TIDY="/clang-tools-extra/test/clang-tidy/check_clang_tidy.py -clang-tidy=/build/tools/clang-tidy-plugin/CataAnalyzerPlugin.exe" +-DCATA_CHECK_CLANG_TIDY="/clang-tools-extra/test/clang-tidy/check_clang_tidy.py -clang-tidy=/build/tools/clang-tidy-plugin/CataAnalyzer.exe" ``` Next, change the directory back to the source root, and run `tools/fix-compilation-database.py` @@ -352,7 +281,7 @@ to avoid compiler errors. ```sh python3 /clang-tools-extra/clang-tidy/tool/run-clang-tidy.py \ - -clang-tidy-binary=build/tools/clang-tidy-plugin/CataAnalyzerPlugin.exe \ + -clang-tidy-binary=build/tools/clang-tidy-plugin/CataAnalyzer.exe \ -p=build "\.cpp$" \ -extra-arg=-target -extra-arg=x86_64-pc-windows-gnu -extra-arg=-pthread -extra-arg=-DSDL_DISABLE_ANALYZE_MACROS \ -extra-arg=-isystem -extra-arg=/clang/lib/Headers diff --git a/tools/clang-tidy-plugin/CataTidyModule.cpp b/tools/clang-tidy-plugin/CataTidyModule.cpp index 9c9b93faf99e..acc04404778b 100644 --- a/tools/clang-tidy-plugin/CataTidyModule.cpp +++ b/tools/clang-tidy-plugin/CataTidyModule.cpp @@ -38,10 +38,12 @@ #include "XYCheck.h" #if defined( CATA_CLANG_TIDY_EXECUTABLE ) -#include +#include "tool/ClangTidyMain.h" #endif -namespace clang::tidy +namespace clang +{ +namespace tidy { namespace cata { @@ -113,7 +115,8 @@ class CataModule : public ClangTidyModule static ClangTidyModuleRegistry::Add X( "cata-module", "Adds Cataclysm-DDA checks." ); -} // namespace clang::tidy +} // namespace tidy +} // namespace clang #if defined( CATA_CLANG_TIDY_EXECUTABLE ) int main( int argc, const char **argv ) diff --git a/tools/clang-tidy-plugin/test/lit.cfg b/tools/clang-tidy-plugin/test/lit.cfg index 9be5d3f26080..571f75df9bd3 100644 --- a/tools/clang-tidy-plugin/test/lit.cfg +++ b/tools/clang-tidy-plugin/test/lit.cfg @@ -18,10 +18,17 @@ else: check_clang_tidy += ' -std=c++17' -cata_include = os.path.join( config.cata_source_dir, "src" ) +# Note: backlash (`\`) in the path seems to cause it to not work properly +# on Windows, therefore the relative path should start with `./` and use `/` +# to delimit directories instead of using multiple arguments to `os.path.join`. +cata_include = os.path.join( config.cata_source_dir, "./src" ) +test_include = os.path.join( config.cata_source_dir, "./tools/clang-tidy-plugin/test-include" ) -cata_plugin = os.path.join( - config.plugin_build_root, 'libCataAnalyzerPlugin.so') +if config.cata_clang_tidy_executable == "ON": + cata_plugin = '' +else: + cata_plugin = os.path.join( + config.plugin_build_root, 'libCataAnalyzerPlugin.so') config.substitutions.append(('%check_clang_tidy', check_clang_tidy)) config.substitutions.append(('%cata_include', cata_include)) diff --git a/tools/clang-tidy-plugin/test/lit.site.cfg.in b/tools/clang-tidy-plugin/test/lit.site.cfg.in index 029fb178021e..4f055f3394ec 100644 --- a/tools/clang-tidy-plugin/test/lit.site.cfg.in +++ b/tools/clang-tidy-plugin/test/lit.site.cfg.in @@ -3,6 +3,7 @@ import os config.cata_source_dir = "@CMAKE_SOURCE_DIR@" config.plugin_build_root = "@CMAKE_CURRENT_BINARY_DIR@" config.cata_check_clang_tidy = "@CATA_CHECK_CLANG_TIDY@" +config.cata_clang_tidy_executable = "@CATA_CLANG_TIDY_EXECUTABLE@" lit_config.load_config( config, os.path.join("@CMAKE_CURRENT_SOURCE_DIR@", "test", "lit.cfg"))