Skip to content

Commit c7c0ea7

Browse files
facontidavideclaude
andcommitted
Add clang-tidy pre-commit hook and fix clang-tidy warnings
- Add run_clang_tidy_hook.sh for pre-commit integration - Skips if clangd-21 not installed - Skips if compile_commands.json not found - Runs only on modified files - Fails on errors - Fix clang-tidy warnings in exception tracking code: - Use anonymous namespace instead of static for tick_stack_ - Add deleted move operations to TickStackGuard Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 664d4b2 commit c7c0ea7

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

.pre-commit-config.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ repos:
4343
- id: clang-format
4444
args: ['-fallback-style=none', '-i']
4545

46-
# C++ static analysis (installs clang-tidy automatically via pip)
47-
# - repo: https://github.com/mxmlnrdr/clang_tidy_hook
48-
# rev: v0.3.1
49-
# hooks:
50-
# - id: clang-tidy
51-
# args:
52-
# - --config-file=.clang-tidy
53-
# - -p=build
46+
# C++ static analysis via clangd-21 (skips if not installed)
47+
- repo: local
48+
hooks:
49+
- id: clang-tidy
50+
name: clang-tidy
51+
entry: ./run_clang_tidy_hook.sh
52+
language: script
53+
files: \.(cpp|hpp|h)$
54+
exclude: ^3rdparty/|^include/behaviortree_cpp/contrib/|^include/behaviortree_cpp/scripting/
5455

5556
# Spell check
5657
- repo: https://github.com/codespell-project/codespell

run_clang_tidy_hook.sh

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

src/tree_node.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020

2121
namespace BT
2222
{
23-
23+
namespace
24+
{
2425
// Thread-local stack tracking the current tick hierarchy.
2526
// Used to build a backtrace when an exception is thrown during tick().
26-
static thread_local std::vector<const TreeNode*> tick_stack_;
27+
thread_local std::vector<const TreeNode*> tick_stack_;
2728

2829
// RAII guard to push/pop nodes from the tick stack
2930
class TickStackGuard
@@ -39,6 +40,8 @@ class TickStackGuard
3940
}
4041
TickStackGuard(const TickStackGuard&) = delete;
4142
TickStackGuard& operator=(const TickStackGuard&) = delete;
43+
TickStackGuard(TickStackGuard&&) = delete;
44+
TickStackGuard& operator=(TickStackGuard&&) = delete;
4245

4346
// Build a backtrace from the current tick stack
4447
static std::vector<TickBacktraceEntry> buildBacktrace()
@@ -52,6 +55,7 @@ class TickStackGuard
5255
return backtrace;
5356
}
5457
};
58+
} // namespace
5559

5660
struct TreeNode::PImpl
5761
{

0 commit comments

Comments
 (0)