diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 708479f79..4a637d0d7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,7 +20,7 @@ jobs: - name: Install dependencies run: | sudo apt-get update - sudo apt-get install -y cmake build-essential zlib1g-dev libzip-dev opencl-headers ocl-icd-opencl-dev + sudo apt-get install -y cmake build-essential zlib1g-dev libzip-dev opencl-headers ocl-icd-opencl-dev clang-tidy - name: Cache CMake build uses: actions/cache@v4 @@ -35,7 +35,7 @@ jobs: - name: Configure CMake working-directory: cpp run: | - cmake . -DUSE_BACKEND=OPENCL -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS_RELEASE="-s" + cmake . -DUSE_BACKEND=OPENCL -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS_RELEASE="-s" -DUSE_CLANG_TIDY=1 - name: Build working-directory: cpp @@ -65,7 +65,7 @@ jobs: - name: Install dependencies run: | - brew install zlib libzip opencl-headers + brew install zlib libzip opencl-headers llvm - name: Cache CMake build uses: actions/cache@v4 with: @@ -81,7 +81,7 @@ jobs: - name: Configure CMake working-directory: cpp run: | - cmake . -G Ninja -DUSE_BACKEND=OPENCL -DCMAKE_BUILD_TYPE=Release + cmake . -G Ninja -DUSE_BACKEND=OPENCL -DCMAKE_BUILD_TYPE=Release -DUSE_CLANG_TIDY=1 - name: Build working-directory: cpp run: | @@ -128,7 +128,8 @@ jobs: run: | cmake . -G "Visual Studio 17 2022" -A x64 ` -DUSE_BACKEND=OPENCL ` - -DCMAKE_TOOLCHAIN_FILE="$env:VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake" + -DCMAKE_TOOLCHAIN_FILE="$env:VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake" ` + -DUSE_CLANG_TIDY=1 - name: Build working-directory: cpp diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 9adbe5eb9..f0364ddf9 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -50,6 +50,8 @@ set(USE_BIGGER_BOARDS_EXPENSIVE 0 CACHE BOOL "Allow boards up to size 50. Compil set(USE_CACHE_TENSORRT_PLAN 0 CACHE BOOL "Use TENSORRT plan cache. May use a lot of disk space. Only applies when USE_BACKEND is TENSORRT.") mark_as_advanced(USE_CACHE_TENSORRT_PLAN) +set(USE_CLANG_TIDY 1 CACHE BOOL "Run clang-tidy static analysis during build") + #--------------------------- NEURAL NET BACKEND ------------------------------------------------------------------------ message(STATUS "Building 'katago' executable for GTP engine and other tools.") @@ -610,5 +612,37 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "C endif() +if(USE_CLANG_TIDY) + message(STATUS "Setting up clang-tidy checks.") + + set(CLANG_TIDY_HINTS) + if(WIN32) + if(MSVC) + set(_vs_root "$ENV{ProgramFiles}/Microsoft Visual Studio") + if(EXISTS "${_vs_root}") + file(GLOB _vs_install_dirs LIST_DIRECTORIES true "${_vs_root}/*/*") + foreach(_vs_dir ${_vs_install_dirs}) + list(APPEND CLANG_TIDY_HINTS "${_vs_dir}/VC/Tools/Llvm/bin") + endforeach() + endif() + else() + list(APPEND CLANG_TIDY_HINTS "$ENV{ProgramFiles}/LLVM/bin") + endif() + elseif(APPLE) + set(CLANG_TIDY_HINTS /opt/homebrew/opt/llvm/bin) + endif() + + find_program(CLANG_TIDY_EXE NAMES clang-tidy HINTS ${CLANG_TIDY_HINTS} REQUIRED) + + if(MSVC) + set(CLANG_TIDY_EXTRA_ARGS "--extra-arg=/EHsc") + else() + set(CLANG_TIDY_EXTRA_ARGS "--extra-arg=-fexceptions") + endif() + + set_target_properties(katago PROPERTIES + CXX_CLANG_TIDY "${CLANG_TIDY_EXE};-checks=-*,performance-inefficient-vector-operation,performance-unnecessary-copy-initialization,readability-non-const-parameter,readability-make-member-function-const;${CLANG_TIDY_EXTRA_ARGS}") +endif() + target_include_directories(katago PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/cpp/core/global.h b/cpp/core/global.h index 455cced8a..ba4b04dc8 100644 --- a/cpp/core/global.h +++ b/cpp/core/global.h @@ -261,7 +261,7 @@ unique_ptr_void make_unique_void(T* ptr) }); } -template +template struct WrappedWithDeleter { bool assigned; T val;