#!/usr/bin/env bash # Author: Guoliang Chen, a Senior Digital Design Engineer # Prove every formatting style safe over real trees: for each preset, format # a scratch copy of the trees in place, count verification errors and # crashes, re-check the output for idempotency, and format every A-output # with B and then A again to measure cross-preset stability. # # Usage: tools/style_sweep.sh -b lucisv -w workdir [-s "lucisv google ..."] tree... # # Never writes to the trees themselves -- only to copies under workdir. # Every lucisv run goes through tools/guard.sh. Prints one TSV row per # preset and one per ordered pair; exit 1 if any verification error, crash # or idempotency violation was seen. # See docs/features/format-styles/README.md. set -u here=$(cd "$(dirname "$0")" && pwd) lucisv= work= styles="lucisv google lowrisc ansi microsoft nxp auto" while [ $# -gt 0 ]; do case $1 in -b) lucisv=$2; shift 2 ;; -w) work=$2; shift 2 ;; -s) styles=$2; shift 2 ;; -*) echo "usage: $0 -b lucisv -w workdir [-s styles] tree..." >&2; exit 2 ;; *) break ;; esac done [ -x "$lucisv" ] && [ -n "$work" ] && [ $# -gt 0 ] || { echo "usage: $0 -b lucisv -w workdir [-s styles] tree..." >&2; exit 2; } flags="--no-cache --no-config --no-gitignore" run() { "$here/guard.sh" -t 1200 -- "$lucisv" $flags "$@"; } # The sources: every .v/.sv/.vh/.svh of every tree (the walk's # EXTENSIONS), symlinks followed as the walk follows them, paths kept, # nothing else. rm -rf "$work" && mkdir -p "$work/src" for tree in "$@"; do dest="$work/src/$(basename "$tree")" (cd "$tree" && find -L . -type f \( -name '*.v' -o -name '*.sv' -o -name '*.vh' -o -name '*.svh' \) -not -path '*/.git/*' | while IFS= read -r f; do mkdir -p "$dest/$(dirname "$f")"; cp "$f" "$dest/$f"; done) done files=$(find "$work/src" -type f | wc -l | tr -d ' ') status=0 printf 'style\tfiles\tparse-errors\tverification-errors\tcrashes\tnot-idempotent\n' for s in $styles; do cp -R "$work/src" "$work/$s" run --style "$s" "$work/$s" >/dev/null 2>"$work/$s.log" parse=$(grep -c ': parse error\|: lex error' "$work/$s.log") verr=$(grep -c 'verification failed' "$work/$s.log") crash=$(grep -ci 'panic\|guard.sh' "$work/$s.log") run --check --style "$s" "$work/$s" >"$work/$s.check" 2>/dev/null again=$(grep -c '^Would reformat' "$work/$s.check") printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$s" "$files" "$parse" "$verr" "$crash" "$again" [ "$verr" -eq 0 ] && [ "$crash" -eq 0 ] && [ "$again" -eq 0 ] || status=1 done printf '\nA\tB\tfiles where A(B(A(x))) != A(x)\n' for a in $styles; do for b in $styles; do [ "$a" = "$b" ] && continue rm -rf "$work/pair" && cp -R "$work/$a" "$work/pair" run --style "$b" "$work/pair" >/dev/null 2>&1 run --style "$a" "$work/pair" >/dev/null 2>&1 diff -rq "$work/$a" "$work/pair" >"$work/$a-$b.diff" printf '%s\t%s\t%s\n' "$a" "$b" "$(wc -l <"$work/$a-$b.diff" | tr -d ' ')" done done rm -rf "$work/pair" exit $status # Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com