|
| 1 | +#!/usr/bin/env bash |
| 2 | +# lint-tidy-diff.sh — clang-tidy scoped to the lines this commit changes. |
| 3 | +# |
| 4 | +# `make lint-tidy` (the full sweep) checks the entire LINT_SRCS tree, which |
| 5 | +# currently surfaces ~5,100 pre-existing findings unrelated to any given |
| 6 | +# commit (see Makefile.cbm's `lint-tidy` target). That is fine for a manual |
| 7 | +# deep audit but makes the pre-commit hook unusable: it blocks every commit, |
| 8 | +# including ones that touch files with zero findings on the touched lines. |
| 9 | +# |
| 10 | +# This script runs the same clang-tidy binary/config against the SAME staged |
| 11 | +# .c files, but passes clang-tidy's own `-line-filter` so only lines the |
| 12 | +# commit actually added/modified can produce a diagnostic. Pre-existing |
| 13 | +# findings on untouched lines — in the same file or elsewhere — are not |
| 14 | +# reported. |
| 15 | +# |
| 16 | +# Invoked by `make lint-tidy-diff` (scripts/hooks/pre-commit), which supplies |
| 17 | +# the resolved clang-tidy binary, then LINT_SRCS, then `--`, then the |
| 18 | +# compiler flags — the same argv shape as the `lint-tidy` target itself. |
| 19 | +# |
| 20 | +# Known limitation: only staged *.c files that are members of LINT_SRCS are |
| 21 | +# checked (matching the full sweep's own scope: vendored code and tests/ are |
| 22 | +# never clang-tidy'd). clang-tidy reports a header's diagnostics under |
| 23 | +# whatever path it was #include-d with, which is not reliably the same |
| 24 | +# string as its path in the working tree, so a commit that touches only a |
| 25 | +# header is not covered here even if a .c file including it also changed; |
| 26 | +# run `make lint-tidy` for a full pass in that case. |
| 27 | + |
| 28 | +set -euo pipefail |
| 29 | + |
| 30 | +ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 31 | +cd "$ROOT" |
| 32 | + |
| 33 | +if [ "$#" -lt 1 ]; then |
| 34 | + echo "usage: lint-tidy-diff.sh <clang-tidy-bin> [LINT_SRCS...] -- [CFLAGS...]" >&2 |
| 35 | + exit 2 |
| 36 | +fi |
| 37 | + |
| 38 | +CLANG_TIDY="$1" |
| 39 | +shift |
| 40 | + |
| 41 | +LINT_SRCS=() |
| 42 | +while [ "$#" -gt 0 ] && [ "$1" != "--" ]; do |
| 43 | + LINT_SRCS+=("$1") |
| 44 | + shift |
| 45 | +done |
| 46 | +[ "$#" -gt 0 ] && shift # drop the `--` separator |
| 47 | +CFLAGS=("$@") |
| 48 | + |
| 49 | +# bash 3.2 (macOS default, see scripts/check-lsp-originality.sh) has neither |
| 50 | +# `mapfile` nor associative arrays — read loop + linear membership check. |
| 51 | +is_lint_src() { |
| 52 | + local needle="$1" f |
| 53 | + [ "${#LINT_SRCS[@]}" -eq 0 ] && return 1 |
| 54 | + for f in "${LINT_SRCS[@]}"; do |
| 55 | + [ "$f" = "$needle" ] && return 0 |
| 56 | + done |
| 57 | + return 1 |
| 58 | +} |
| 59 | + |
| 60 | +# Staged .c files this commit actually changed (added/copied/modified/ |
| 61 | +# renamed — a delete has no new lines to check) that are members of |
| 62 | +# LINT_SRCS. |
| 63 | +CHANGED=() |
| 64 | +while IFS= read -r f; do |
| 65 | + [ -n "$f" ] && is_lint_src "$f" && CHANGED+=("$f") |
| 66 | +done < <(git diff --cached --name-only --diff-filter=ACMR -- '*.c') |
| 67 | + |
| 68 | +if [ "${#CHANGED[@]}" -eq 0 ]; then |
| 69 | + echo "lint-tidy-diff: no staged changes to a linted .c file, skipping clang-tidy" |
| 70 | + exit 0 |
| 71 | +fi |
| 72 | + |
| 73 | +# For each changed file, turn its staged hunks into a clang-tidy line-filter |
| 74 | +# range: `@@ -a,b +c,d @@` -> lines [c, c+d-1]. Zero-context (-U0) so a range |
| 75 | +# never includes untouched, pre-existing lines. Pure-deletion hunks (d == 0) |
| 76 | +# add no new lines and are skipped. |
| 77 | +LINE_FILTER='[' |
| 78 | +FILTER_SEP='' |
| 79 | +TARGETS=() |
| 80 | +for f in "${CHANGED[@]}"; do |
| 81 | + [ -f "$f" ] || continue # renamed away / not present in the working tree |
| 82 | + ranges='' |
| 83 | + range_sep='' |
| 84 | + while IFS= read -r hunk; do |
| 85 | + if [[ "$hunk" =~ ^@@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+)(,([0-9]+))?\ @@ ]]; then |
| 86 | + new_start="${BASH_REMATCH[2]}" |
| 87 | + new_count="${BASH_REMATCH[4]:-1}" |
| 88 | + [ "$new_count" -eq 0 ] && continue |
| 89 | + new_end=$((new_start + new_count - 1)) |
| 90 | + ranges+="${range_sep}[${new_start},${new_end}]" |
| 91 | + range_sep=',' |
| 92 | + fi |
| 93 | + done < <(git diff --cached -U0 --diff-filter=ACMR -- "$f" | grep -E '^@@ ') |
| 94 | + [ -z "$ranges" ] && continue |
| 95 | + |
| 96 | + LINE_FILTER+="${FILTER_SEP}{\"name\":\"${f}\",\"lines\":[${ranges}]}" |
| 97 | + FILTER_SEP=',' |
| 98 | + TARGETS+=("$f") |
| 99 | +done |
| 100 | +LINE_FILTER+=']' |
| 101 | + |
| 102 | +if [ "${#TARGETS[@]}" -eq 0 ]; then |
| 103 | + echo "lint-tidy-diff: staged .c changes are deletion-only, skipping clang-tidy" |
| 104 | + exit 0 |
| 105 | +fi |
| 106 | + |
| 107 | +echo "lint-tidy-diff: ${#TARGETS[@]} changed file(s), diff-scoped clang-tidy" |
| 108 | +"$CLANG_TIDY" --quiet "-line-filter=${LINE_FILTER}" "${TARGETS[@]}" -- "${CFLAGS[@]}" |
0 commit comments