-
Notifications
You must be signed in to change notification settings - Fork 825
Expand file tree
/
Copy pathrun_clang_tidy_hook.sh
More file actions
executable file
·57 lines (48 loc) · 1.3 KB
/
run_clang_tidy_hook.sh
File metadata and controls
executable file
·57 lines (48 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# Pre-commit hook wrapper for clang-tidy.
# - Skips if clangd-21 is not installed
# - Skips if compile_commands.json not found
# - Runs only on files passed as arguments (modified files)
# - Fails if clang-tidy reports any errors
script_dir=${0%/*}
build_dir="${script_dir}/build"
if ! command -v clangd-21 &> /dev/null; then
echo "Skipping clang-tidy: clangd-21 not installed"
exit 0
fi
if [ ! -f "${build_dir}/compile_commands.json" ]; then
echo "Skipping clang-tidy: compile_commands.json not found (run cmake first)"
exit 0
fi
if [ $# -eq 0 ]; then
echo "No files to check"
exit 0
fi
# Filter to only .cpp, .hpp, .h files and exclude 3rdparty/contrib/scripting
files=()
for f in "$@"; do
case "$f" in
3rdparty/*|*/contrib/*|*/scripting/*|*_WIN.cpp) continue ;;
*.cpp|*.hpp|*.h) files+=("$f") ;;
esac
done
if [ ${#files[@]} -eq 0 ]; then
exit 0
fi
echo "Running clang-tidy on ${#files[@]} file(s)..."
failed=0
for f in "${files[@]}"; do
echo " Checking: $f"
output=$(clangd-21 \
--log=error \
--clang-tidy \
--compile-commands-dir="$build_dir" \
--check-locations=false \
--check="$f" 2>&1)
# Check for error lines (starts with E[)
if echo "$output" | grep -q "^E\["; then
echo "$output" | grep "^E\["
failed=1
fi
done
exit $failed