Skip to content

Commit

Permalink
Add support to build clang-tidy checks as an executable to the CMake …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
Qrox committed Apr 1, 2022
1 parent 3d0002b commit f2e2f83
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 92 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ option(DYNAMIC_LINKING
"Use dynamic linking. Or use static to remove MinGW dependency instead." "ON")
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_PLUGIN "Build Cata's custom clang-tidy checks as a 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")
Expand Down Expand Up @@ -354,7 +355,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()

Expand Down
81 changes: 5 additions & 76 deletions doc/DEVELOPER_TOOLING.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,78 +231,6 @@ and execute the following command inside the `<python3_root>/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.

Expand All @@ -314,17 +242,18 @@ 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
-DLLVM_DIR="<llvm-source-root>/build/lib/cmake/llvm"
-DClang_DIR="<llvm-source-root>/build/lib/cmake/clang"
-DLLVM_INCLUDE_DIRS="<llvm-source-root>/llvm/include"
-DCLANG_INCLUDE_DIRS="<llvm-source-root>/clang/include"
-DCATA_CLANG_TIDY_PLUGIN=ON
-DCATA_CLANG_TIDY_EXECUTABLE=ON
-DCATA_CLANG_TIDY_INCLUDE_DIR="<llvm-source-root>/clang-tools-extra/clang-tidy"
-DCATA_CHECK_CLANG_TIDY="<llvm-source-root>/clang-tools-extra/test/clang-tidy/check_clang_tidy.py -clang-tidy=<cdda-source-root>/build/tools/clang-tidy-plugin/CataAnalyzerPlugin.exe"
-DCATA_CHECK_CLANG_TIDY="<llvm-source-root>/clang-tools-extra/test/clang-tidy/check_clang_tidy.py -clang-tidy=<cdda-source-root>/build/tools/clang-tidy-plugin/CataAnalyzer.exe"
```
Next, change the directory back to the source root and run `tools/fix-compilation-database.py`
Expand All @@ -346,7 +275,7 @@ to avoid compiler errors.
```sh
python3 <llvm-source-root>/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=<llvm-source-root>/clang/lib/Headers
Expand Down
24 changes: 17 additions & 7 deletions tools/clang-tidy-plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(ExternalProject)
find_package(LLVM REQUIRED CONFIG)
find_package(Clang REQUIRED CONFIG)

add_library(CataAnalyzerPlugin MODULE
set(CataAnalyzerSrc
AlmostNeverAutoCheck.cpp
AssertCheck.cpp
CataTidyModule.cpp
Expand Down Expand Up @@ -41,7 +41,17 @@ add_library(CataAnalyzerPlugin MODULE
Utils.cpp
XYCheck.cpp)

target_include_directories(CataAnalyzerPlugin SYSTEM PRIVATE
if (CATA_CLANG_TIDY_EXECUTABLE)
set(CataAnalyzerName CataAnalyzer)
add_executable(${CataAnalyzerName} ${CataAnalyzerSrc})
target_link_libraries(${CataAnalyzerName} clangTidyMain)
add_definitions(-DCATA_CLANG_TIDY_EXECUTABLE)
else ()
set(CataAnalyzerName CataAnalyzerPlugin)
add_library(${CataAnalyzerName} MODULE ${CataAnalyzerSrc})
endif ()

target_include_directories(${CataAnalyzerName} SYSTEM PRIVATE
${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})

if ("${CATA_CLANG_TIDY_INCLUDE_DIR}" STREQUAL "")
Expand All @@ -58,21 +68,21 @@ if ("${CATA_CLANG_TIDY_INCLUDE_DIR}" STREQUAL "")
INSTALL_COMMAND ""
TEST_COMMAND "")

add_dependencies(CataAnalyzerPlugin clang-tidy-plugin-support)
target_include_directories(CataAnalyzerPlugin SYSTEM PRIVATE ${CTPS_SRC}/include)
add_dependencies(${CataAnalyzerName} clang-tidy-plugin-support)
target_include_directories(${CataAnalyzerName} SYSTEM PRIVATE ${CTPS_SRC}/include)
else ()
target_include_directories(CataAnalyzerPlugin SYSTEM PRIVATE ${CATA_CLANG_TIDY_INCLUDE_DIR})
target_include_directories(${CataAnalyzerName} SYSTEM PRIVATE ${CATA_CLANG_TIDY_INCLUDE_DIR})
endif ()

target_compile_definitions(CataAnalyzerPlugin PRIVATE ${LLVM_DEFINITIONS})
target_compile_definitions(${CataAnalyzerName} PRIVATE ${LLVM_DEFINITIONS})

# We need to turn off exceptions and RTTI to match the LLVM build.
# I feel there ought to be a way to extract these flags from the
# LLVMConfig.cmake as we have done for e.g. LLVM_INCLUDE_DIRS above, but I
# haven't found one.
if (MSVC)
else ()
target_compile_options(CataAnalyzerPlugin PRIVATE -fno-exceptions -fno-rtti)
target_compile_options(${CataAnalyzerName} PRIVATE -fno-exceptions -fno-rtti)
endif ()

configure_file(test/lit.site.cfg.in test/lit.site.cfg @ONLY)
Expand Down
11 changes: 11 additions & 0 deletions tools/clang-tidy-plugin/CataTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#include "UTF8ToLowerUpperCheck.h"
#include "XYCheck.h"

#if defined( CATA_CLANG_TIDY_EXECUTABLE )
#include "tool/ClangTidyMain.h"
#endif

namespace clang
{
namespace tidy
Expand Down Expand Up @@ -110,3 +114,10 @@ X( "cata-module", "Adds Cataclysm-DDA checks." );

} // namespace tidy
} // namespace clang

#if defined( CATA_CLANG_TIDY_EXECUTABLE )
int main( int argc, const char **argv )
{
return clang::tidy::clangTidyMain( argc, argv );
}
#endif
18 changes: 11 additions & 7 deletions tools/clang-tidy-plugin/test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ else:

check_clang_tidy += ' -std=c++14'

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')
# 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" )

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))
Expand Down
1 change: 1 addition & 0 deletions tools/clang-tidy-plugin/test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

0 comments on commit f2e2f83

Please sign in to comment.