Files
ConformalLabpp/scripts/quality/clang-format.sh
Tarik Moussa f1d77aa293
Some checks failed
C++ Tests / test-fast (pull_request) Successful in 2m26s
API Docs / doc-build (pull_request) Successful in 55s
Markdown link check / check (pull_request) Successful in 49s
C++ Tests / test-cgal (pull_request) Failing after 12m24s
quality: add code-style + CGAL-convention checkers (local-only)
Closes the gap "no code-quality / convention gate" from the structural
review.  Three new artefacts, all local-only (CI promotion deferred
until the existing tree is 100 % clean under each):

1. .clang-format — project's existing style mechanically captured
   (4-space indent, opening brace on new line for class/struct/function,
   left-aligned pointer/reference modifiers, aligned `using = ...` blocks,
   100-col loose limit, no include re-ordering — matches code/include/
   today).

2. scripts/quality/clang-format.sh — drift detector.  Dry-run mode by
   default (always exits 0); --strict to fail on drift; --fix to apply
   suggested changes in place.  Skips code/deps/ and macOS-duplicate
   files.

3. scripts/quality/cgal-conventions.py — checker for the CGAL idioms
   that clang-format/clang-tidy cannot express:
     CGAL-1  include-guard format `CGAL_<DIRS>_<FILE>_H`
     CGAL-2  every public header has a `\\file` Doxygen brief
     CGAL-3  no nested namespaces beyond the allowed set
             (CGAL::parameters, CGAL::Conformal_map, internal_np, IO)
     CGAL-4  named-parameter tag types end in `_t`; value object does not
     CGAL-5  no `using namespace ...` at file scope (header leakage)
     CGAL-6  no #define beyond CGAL_* / include-guard

   Result on the current tree: 6 CGAL public headers, 0 violations.
   The checker therefore doubles as documentation of the conventions
   we already follow.

Both are wired into scripts/quality/run-all.sh's fast subset (~5 s
combined wall time).  README.md updated to split the gates into a
"style/convention" group (cheap, run-on-every-commit material) and a
"correctness/quality" group (slow, run-before-tag material).

The reviewer-facing locked-vs-flexible.md gains another " Closed"
row documenting both gates and the 0-violation baseline.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 08:37:12 +02:00

97 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# scripts/quality/clang-format.sh
#
# Verify every source file under code/{include,src,tests} matches the
# project's `.clang-format` policy. Runs in dry-run mode by default —
# only reports diffs; never edits files.
#
# Pass `--fix` to apply the suggested formatting in place.
#
# Local-only. Promotion to CI is intended once the existing tree is
# 100 %-conformant; today we report drift but don't fail (the script
# exits non-zero only with `--strict`).
#
# Usage:
# bash scripts/quality/clang-format.sh # dry-run, exit 0 always
# bash scripts/quality/clang-format.sh --strict # dry-run, exit 1 on drift
# bash scripts/quality/clang-format.sh --fix # apply changes
#
# Exit codes:
# 0 no drift, or drift but --strict not set
# 1 drift detected AND --strict (or fixes applied AND --fix)
# 2 prerequisite missing
set -uo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$ROOT"
command -v clang-format >/dev/null 2>&1 || {
echo "FAIL: clang-format not in PATH." >&2
echo " macOS: brew install clang-format" >&2
echo " Linux: sudo apt install clang-format" >&2
exit 2
}
STRICT=0
FIX=0
for arg in "$@"; do
case "$arg" in
--strict) STRICT=1 ;;
--fix) FIX=1 ;;
*) echo "Unknown arg: $arg" >&2; exit 2 ;;
esac
done
FILES_LIST="$(find code/include code/src code/tests \
\( -name "*.h" -o -name "*.hpp" -o -name "*.cpp" \) \
-type f 2>/dev/null \
| grep -v "^code/deps/" \
| grep -v " 2\." \
| sort)"
n_total=0
n_drift=0
drift_list=""
while IFS= read -r f; do
[ -z "$f" ] && continue
n_total=$((n_total + 1))
if [ "$FIX" -eq 1 ]; then
clang-format -i "$f"
else
# --dry-run + -Werror sets non-zero exit when changes would be made.
if ! clang-format --dry-run -Werror "$f" >/dev/null 2>&1; then
n_drift=$((n_drift + 1))
drift_list="$drift_list $f
"
fi
fi
done <<EOF
$FILES_LIST
EOF
echo "clang-format ($(clang-format --version | head -1))"
echo "Scanned $n_total source files."
if [ "$FIX" -eq 1 ]; then
echo "FIX mode: applied formatting in place."
exit 0
fi
if [ "$n_drift" -gt 0 ]; then
echo
echo "DRIFT: $n_drift file(s) do not match .clang-format policy:"
printf "%s" "$drift_list"
echo
echo "Recovery:"
echo " bash scripts/quality/clang-format.sh --fix # apply"
echo " git diff # review"
if [ "$STRICT" -eq 1 ]; then
exit 1
fi
echo " (--strict not set → exiting 0 anyway)"
exit 0
fi
echo "OK: every file matches .clang-format policy."
exit 0