From 228110c3b51e47f7b881c67453a27feded274f7f Mon Sep 17 00:00:00 2001 From: Johannes Schneider Date: Fri, 9 May 2025 13:41:24 +0200 Subject: [PATCH] Use a clang-tidy configuration file (#488) * Improve GitHub workflow for clang-tidy * Improve clang-tidy configuration * Further improve clang-tidy ignore list of warnings * Add cert warnings to clang-tidy --- .clang-tidy | 57 ++++++++++++++++ .github/workflows/static_code_analysis.yaml | 76 +++++++++++++-------- 2 files changed, 106 insertions(+), 27 deletions(-) create mode 100644 .clang-tidy diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..be6409c --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,57 @@ +Checks: + - cert-* + - clang-analyzer-* + - bugprone-* + - performance-* + - readability-* + - modernize-* + - cppcoreguidelines-* + - misc-* + - '-altera-unroll-loops' + - '-bugprone-easily-swappable-parameters' + - '-bugprone-sizeof-expression' + - '-cppcoreguidelines-avoid-do-while' + - '-cppcoreguidelines-macro-usage' + - '-cppcoreguidelines-no-malloc' + - '-cppcoreguidelines-owning-memory' + - '-cppcoreguidelines-pro-bounds-array-to-pointer-decay' + - '-cppcoreguidelines-pro-bounds-constant-array-index' + - '-cppcoreguidelines-pro-bounds-pointer-arithmetic' + - '-cppcoreguidelines-pro-type-union-access' + - '-cppcoreguidelines-pro-type-vararg' + - '-llvmlibc-implementation-in-namespace' + - '-llvmlibc-restrict-system-libc-headers' + - '-misc-const-correctness' + - '-misc-definitions-in-headers' + - '-misc-no-recursion' + - '-misc-static-assert' + - '-misc-unused-parameters' + - '-modernize-use-auto' + - '-modernize-use-nodiscard' + - '-modernize-use-using' + - '-modernize-use-trailing-return-type' + - '-performance-enum-size' + - '-readability-braces-around-statements' + - '-readability-function-cognitive-complexity' + - '-readability-implicit-bool-conversion' + - '-readability-simplify-boolean-expr' + - '-readability-static-accessed-through-instance' + - '-readability-identifier-naming' + - '-readability-identifier-length' + +HeaderFilterRegex: '.*\.(cpp|h)$' +FormatStyle: file + +CheckOptions: + - key: 'modernize-loop-convert.MaxCopySize' + value: '16' + - key: 'readability-identifier-naming.VariableCase' + value: 'camelBack' + - key: 'readability-identifier-naming.ClassCase' + value: 'CamelCase' + - key: 'readability-identifier-naming.FunctionCase' + value: 'camelBack' + - key: 'cppcoreguidelines-avoid-magic-numbers.IgnoreEnums' + value: '1' + - key: 'cppcoreguidelines-avoid-magic-numbers.IgnoreOctalLiterals' + value: '1' diff --git a/.github/workflows/static_code_analysis.yaml b/.github/workflows/static_code_analysis.yaml index d933ce4..873c97c 100644 --- a/.github/workflows/static_code_analysis.yaml +++ b/.github/workflows/static_code_analysis.yaml @@ -1,4 +1,4 @@ -name: Static code analysis (clang-tidy) +name: Static code analysis (clang-tidy) on: push: @@ -12,26 +12,39 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Install dependencies + # Cache Vulkan SDK + - name: Cache Vulkan SDK + id: cache-vulkan + uses: actions/cache@v4 + with: + path: vulkan_sdk + key: vulkan-sdk-1.4.309.0 + + # Install system dependencies + - name: Install system dependencies run: | sudo apt update - sudo apt install -y clang-15 clang-tidy-15 cmake ninja-build libc++-15-dev libc++abi-15-dev + sudo apt install -y clang-15 clang-tidy-15 cmake parallel libc++-15-dev libc++abi-15-dev - - name: Set clang-tidy version - run: echo "CLANG_TIDY=clang-tidy-15" >> $GITHUB_ENV - - - name: Prepare Vulkan SDK + # Download Vulkan SDK only if not cached + - name: Download Vulkan SDK + if: steps.cache-vulkan.outputs.cache-hit != 'true' run: | curl -LS -o vulkansdk.tar.xz https://sdk.lunarg.com/sdk/download/1.4.309.0/linux/vulkansdk-linux-x86_64-1.4.309.0.tar.xz mkdir -p vulkan_sdk tar xf vulkansdk.tar.xz -C vulkan_sdk - export VULKAN_SDK=$GITHUB_WORKSPACE/vulkan_sdk/1.4.309.0/x86_64 - echo "VULKAN_SDK=$VULKAN_SDK" >> $GITHUB_ENV - echo "PATH=$VULKAN_SDK/bin:$PATH" >> $GITHUB_ENV - echo "LD_LIBRARY_PATH=$VULKAN_SDK/lib:$LD_LIBRARY_PATH" >> $GITHUB_ENV - echo "VK_ICD_FILENAMES=$VULKAN_SDK/etc/vulkan/icd.d" >> $GITHUB_ENV - echo "VK_LAYER_PATH=$VULKAN_SDK/etc/vulkan/layer.d" >> $GITHUB_ENV + # Set environment variables for Clang and Vulkan SDK + - name: Set environment variables + run: | + echo "CLANG_TIDY=clang-tidy-15" >> $GITHUB_ENV + echo "VULKAN_SDK=${GITHUB_WORKSPACE}/vulkan_sdk/1.4.309.0/x86_64" >> $GITHUB_ENV + echo "PATH=${GITHUB_WORKSPACE}/vulkan_sdk/1.4.309.0/x86_64/bin:$PATH" >> $GITHUB_ENV + echo "LD_LIBRARY_PATH=${GITHUB_WORKSPACE}/vulkan_sdk/1.4.309.0/x86_64/lib:$LD_LIBRARY_PATH" >> $GITHUB_ENV + echo "VK_ICD_FILENAMES=${GITHUB_WORKSPACE}/vulkan_sdk/1.4.309.0/x86_64/etc/vulkan/icd.d" >> $GITHUB_ENV + echo "VK_LAYER_PATH=${GITHUB_WORKSPACE}/vulkan_sdk/1.4.309.0/x86_64/etc/vulkan/layer.d" >> $GITHUB_ENV + + # Configure the project with CMake - name: Configure with CMake run: | cmake -S . -B build \ @@ -40,18 +53,27 @@ jobs: -DCMAKE_C_COMPILER=clang-15 \ -DVMA_BUILD_SAMPLES=YES - - name: Run Clang-Tidy + # List files to analyze + - name: Check the files found for clang-tidy run: | - find . -name '*.cpp' | xargs clang-tidy-15 -checks='*, \ - -modernize-use-trailing-return-type, \ - -cppcoreguidelines-macro-usage, \ - -modernize-use-auto, \ - -modernize-use-using \ - -modernize-use-nodiscard \ - -altera-unroll-loops \ - -misc-definitions-in-headers \ - -cppcoreguidelines-pro-bounds-pointer-arithmetic \ - -readability-function-cognitive-complexity \ - -llvmlibc-restrict-system-libc-headers \ - -cppcoreguidelines-pro-type-union-access' \ - -header-filter='.*vk_mem_alloc\.h' -p build || true + find src include \ + -path '*/_deps/*' -prune -o \ + -path '*/build/*' -prune -o \ + \( -name '*.cpp' -o -name '*.hpp' \) -print + + # Run clang-tidy in parallel + - name: Run clang-tidy + run: | + find src include \ + -path '*/_deps/*' -prune -o \ + -path '*/build/*' -prune -o \ + \( -name '*.cpp' -o -name '*.hpp' \) -print0 | + parallel -0 clang-tidy -p build {} | + tee output || true + + # Summarize warnings + - name: Summarize clang-tidy warnings + run: | + grep -hEo '\[[a-z0-9]+-[a-z0-9-]+\]' output \ + | sort | uniq -c | sort -nr \ + | sed 's/[][]//g' || true