name: "Clang-Tidy"

on:
  pull_request:
    types: [opened, edited, synchronize, reopened]
    paths:
      - ".github/automation/x64/**"
      - ".github/workflows/clang-tidy.yml"
      - "cmake/**"
      - "examples/**"
      - "include/**"
      - "src/common/**"
      - "src/cpu/*"
      - "src/cpu/gemm/**"
      - "src/cpu/matmul/**"
      - "src/cpu/reorder/**"
      - "src/cpu/rnn/**"
      - "src/cpu/x64/**"
      - "src/gpu/*"
      - "src/gpu/intel/**"
      - "src/graph/**"
      - "tests/**"
      - "CMakeLists.txt"

## Declare default permissions as read only.
permissions: read-all

# Kill stale checks
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  pr-clang-tidy:
    name: Clang-Tidy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout oneDNN
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0

      - name: Install clang
        run: |
          sudo apt-get update
          sudo apt-get install -y clang libomp-dev ocl-icd-libopencl1 ocl-icd-opencl-dev

      - name: Configure oneDNN
        run: .github/automation/x64/build_linters.sh
        env:
          ONEDNN_ACTION: configure

      - name: Check source files
        run: |
          echo -e "Checking Clang-Tidy $(clang-tidy --version)\n"
          touch clang-tidy.log
          for file in $(git diff --name-only ${{ github.event.pull_request.head.sha }} ${{ github.event.pull_request.base.sha }} | grep -E '\.cpp$'); 
          do
            if grep -q "$file" "build/compile_commands.json"; then
              echo -e "\nAnalyzing $file"
              clang-tidy -p build $file 2>&1 | tee -a clang-tidy.log
            else
              echo "Skipped $file as it's not built in x64 OpenMP/OpenCL configuration."
            fi
          done
          grep -i -E "warning:|error:" clang-tidy.log | sort -u
          grep -q -i -E "warning:|error:" clang-tidy.log && exit 1 || true
