From 8540426216ae81a583be6670c56275a538cb17ad Mon Sep 17 00:00:00 2001 From: Lai-YT <381xvmvbib@gmail.com> Date: Thu, 22 Feb 2024 23:13:19 +0800 Subject: [PATCH] Run clang tidy on diff with CI Added a script for CI execution. This script is not intended for local user execution due to the following reasons: 1. `clang-tidy-diff` supports exit with a failure code when there are errors from version 17 or 18 onward. (The exact version is uncertain. For more details, please refer to https://github.com/llvm/llvm-project/issues/65000.) 2. `bear` has different command line interfaces between version <= 2.4.x and onward. (For more details, please refer to https://github.com/rizsotto/Bear?tab=readme-ov-file#how-to-use.) The script may not function properly if any of these issues arise. In the CI environment, we ensure that the versions of these dependencies are up-to-date. Specifically, the version of `clang-tidy-diff` is specified as `18`, and the version of `Bear` on Ubuntu 22.04 is 3.0.x. --- .github/workflows/ci.yml | 9 +++++++++ .gitignore | 3 +++ scripts/check-tidy.sh | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100755 scripts/check-tidy.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae3c8bdd..3e09d24b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,6 +17,15 @@ jobs: run: | sudo apt-get install -q -y clang-format scripts/check-format.sh + - name: static checks + run: | + sudo add-apt-repository -y 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main' + wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - + sudo apt-get update + sudo apt-get install -q -y bear clang-tidy-18 + CLANG_TIDY_DIFF=clang-tidy-diff-18.py + scripts/check-tidy.sh + test: runs-on: ubuntu-22.04 strategy: diff --git a/.gitignore b/.gitignore index 2677be12..f87af72a 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,6 @@ venv.bak/ # VS Code configs .vscode + +# Compilation database +compile_commands.json diff --git a/scripts/check-tidy.sh b/scripts/check-tidy.sh new file mode 100755 index 00000000..271cb9f8 --- /dev/null +++ b/scripts/check-tidy.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env sh + +# +# Runs clang-tidy on the diff between main and HEAD. +# + +set -eu + +CLANG_TIDY_DIFF=${CLANG_TIDY_DIFF:-clang-tidy-diff.py} + +command -v bear >/dev/null 2>&1 || ( + echo "error: bear is required to generate compile_commands.json; see https://github.com/rizsotto/Bear" + exit 1 +) + +# number of jobs to run +NPROCS=1 +# Find available processor numbers based on different OS. +OS="$(uname -s)" +case $OS in +'Linux') + NPROCS="$(grep -c ^processor /proc/cpuinfo)" + ;; +'Darwin') + NPROCS="$(sysctl -n hw.ncpu)" + ;; +esac + +# Run a clean build to generate compile_commands.json. +make clean +bear -- make -j"${NPROCS}" +git diff -U0 main..HEAD | ${CLANG_TIDY_DIFF} -p1 -j"${NPROCS}"