From 6fc18f9c0dbf685f422b0b6c59c8847654c52c30 Mon Sep 17 00:00:00 2001
From: moxian <moxian@users.noreply.github.com>
Date: Thu, 26 Dec 2024 22:49:05 -0800
Subject: [PATCH] More targed clang-tidy

---
 .github/workflows/clang-tidy.yml    |  8 ++++++--
 build-scripts/clang-tidy-wrapper.sh |  2 ++
 build-scripts/clang-tidy.sh         | 13 +++++++++----
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml
index 13c6903756e93..ea8fc5b399749 100644
--- a/.github/workflows/clang-tidy.yml
+++ b/.github/workflows/clang-tidy.yml
@@ -36,7 +36,11 @@ jobs:
       matrix:
         # To make the run finish in the run time limit, we split it up into two
         # parts: the src directory and everything else
-        subset: [ 'src', 'other' ]
+        subset: [
+            'directly-changed',
+            'indirectly-changed-src',
+            'indirectly-changed-other'
+        ]
 
     runs-on: ubuntu-22.04
     env:
@@ -90,7 +94,7 @@ jobs:
           for (const path of files) {
             console.log(path);
           }
-          fs.writeFileSync("files_changed", files.join('\n'));
+          fs.writeFileSync("files_changed", files.join('\n') + '\n');
     - uses: ammaraskar/gcc-problem-matcher@master
     - name: run clang-tidy
       if: ${{ needs.skip-duplicates.outputs.should_skip != 'true' && github.event_name != 'pull_request' || github.event.pull_request.draft == false }}
diff --git a/build-scripts/clang-tidy-wrapper.sh b/build-scripts/clang-tidy-wrapper.sh
index e7166b77d0d67..9169d48a7ffd0 100755
--- a/build-scripts/clang-tidy-wrapper.sh
+++ b/build-scripts/clang-tidy-wrapper.sh
@@ -7,7 +7,9 @@ plugin=build/tools/clang-tidy-plugin/libCataAnalyzerPlugin.so
 
 if [ -f "$plugin" ]
 then
+    set -x
     LD_PRELOAD=$plugin "$CATA_CLANG_TIDY" --enable-check-profile --store-check-profile=clang-tidy-trace "$@"
 else
+    set -x
     "$CATA_CLANG_TIDY" "$@"
 fi
diff --git a/build-scripts/clang-tidy.sh b/build-scripts/clang-tidy.sh
index 0e948953b3088..2cd4d5a398efe 100755
--- a/build-scripts/clang-tidy.sh
+++ b/build-scripts/clang-tidy.sh
@@ -42,6 +42,7 @@ cmake \
 
 if [ "$CATA_CLANG_TIDY" = "plugin" ]
 then
+    echo "Compiling clang-tidy plugin"
     make -j$num_jobs CataAnalyzerPlugin
     export PATH=$PWD/tools/clang-tidy-plugin/clang-tidy-plugin-support/bin:$PATH
     if ! which FileCheck
@@ -90,6 +91,7 @@ then
     tidyable_cpp_files=$all_cpp_files
 else
     make \
+        --silent \
         -j $num_jobs \
         ${COMPILER:+COMPILER=$COMPILER} \
         TILES=${TILES:-0} \
@@ -122,11 +124,14 @@ printf "Subset to analyze: '%s'\n" "$CATA_CLANG_TIDY_SUBSET"
 # formats are matched. Exit code 1 from grep (meaning no match) is ignored in
 # case one subset contains no file to analyze.
 case "$CATA_CLANG_TIDY_SUBSET" in
-    ( src )
-        tidyable_cpp_files=$(printf '%s\n' "$tidyable_cpp_files" | grep -E '(^|/)src/' || [[ $? == 1 ]])
+    ( directly-changed )
+        tidyable_cpp_files=$(printf '%s\n' "$tidyable_cpp_files" | grep -f ./files_changed || [[ $? == 1 ]])
         ;;
-    ( other )
-        tidyable_cpp_files=$(printf '%s\n' "$tidyable_cpp_files" | grep -Ev '(^|/)src/' || [[ $? == 1 ]])
+    ( indirectly-changed-src )
+        tidyable_cpp_files=$(printf '%s\n' "$tidyable_cpp_files" | grep -E '(^|/)src/' | grep -vf ./files_changed || [[ $? == 1 ]])
+        ;;
+    ( indirectly-changed-other )
+        tidyable_cpp_files=$(printf '%s\n' "$tidyable_cpp_files" | grep -Ev '(^|/)src/' | grep -vf ./files_changed || [[ $? == 1 ]])
         ;;
 esac