Skip to content

Commit a4a12b1

Browse files
raiden00plxiaoxiang781216
authored andcommitted
tools/nxstyle: add a script to check a whole tree at once
nxstyle checks one file per invocation. nxstyle_sweep.sh runs it over the directories given, or the whole repository, and collects what it reports. Signed-off-by: raiden00pl <raiden00@railab.me> Assisted-by: Claude Code
1 parent 44dac09 commit a4a12b1

2 files changed

Lines changed: 214 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
====================
2+
``nxstyle_sweep.sh``
3+
====================
4+
5+
Run ``nxstyle`` over whole directories, or over the repository, and collect
6+
what it reports. Usage:
7+
8+
.. code:: console
9+
10+
$ tools/nxstyle_sweep.sh [options] [<dir> ...]
11+
12+
-o <file> Diagnostics (default: nxstyle-errors.txt)
13+
-f <file> Failing files, worst first (default: nxstyle-files.txt)
14+
-l List failing files instead of writing the reports
15+
-s Summarise the diagnostics by message
16+
-b <path> Use an existing nxstyle binary
17+
-j <n> Number of parallel checks
18+
-a Check every file, not only those tracked by git
19+
-h Show this help
20+
21+
Options must precede the directories, which default to the whole repository
22+
and are relative to its top. The exit status is non-zero if anything was
23+
reported.
24+
25+
See also ``nxstyle``.

tools/nxstyle_sweep.sh

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#!/usr/bin/env bash
2+
############################################################################
3+
# tools/nxstyle_sweep.sh
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
# Licensed to the Apache Software Foundation (ASF) under one or more
8+
# contributor license agreements. See the NOTICE file distributed with
9+
# this work for additional information regarding copyright ownership. The
10+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
11+
# "License"); you may not use this file except in compliance with the
12+
# License. You may obtain a copy of the License at
13+
#
14+
# http://www.apache.org/licenses/LICENSE-2.0
15+
#
16+
# Unless required by applicable law or agreed to in writing, software
17+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19+
# License for the specific language governing permissions and limitations
20+
# under the License.
21+
#
22+
############################################################################
23+
24+
# Run nxstyle over the whole repository, or over a subdirectory, and collect
25+
# what it reports.
26+
#
27+
# Usage: tools/nxstyle_sweep.sh [options] [<dir> ...]
28+
#
29+
# -o <file> Write the diagnostics to <file> (default: nxstyle-errors.txt)
30+
# -f <file> Write the failing file names, with a count each and worst
31+
# first, to <file> (default: nxstyle-files.txt)
32+
# -l List failing files on stdout instead of writing the reports
33+
# -s Summarise the diagnostics by message on stdout
34+
# -b <path> Use an existing nxstyle binary rather than building one
35+
# -j <n> Run <n> checks in parallel (default: number of CPUs)
36+
# -a Check every file, not only those tracked by git
37+
# -h Show this help
38+
#
39+
# With no directory given the whole repository is checked. Paths are taken
40+
# relative to the top of the repository, as nxstyle verifies the path
41+
# recorded in each file header.
42+
#
43+
# Examples:
44+
# tools/nxstyle_sweep.sh
45+
# tools/nxstyle_sweep.sh -s arch/arm/src/stm32h7
46+
# tools/nxstyle_sweep.sh -l drivers | head
47+
# tools/nxstyle_sweep.sh -o /tmp/errors.txt arch drivers
48+
49+
set -u
50+
51+
tooldir=$(cd "$(dirname "$0")" && pwd)
52+
topdir=$(cd "$tooldir/.." && pwd)
53+
54+
outfile=nxstyle-errors.txt
55+
filefile=nxstyle-files.txt
56+
listonly=0
57+
summary=0
58+
nxstyle=
59+
jobs=$( (nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4) )
60+
allfiles=0
61+
tmpdir=
62+
63+
usage()
64+
{
65+
# Print the block of comments that follows the licence header
66+
67+
awk '/^#####/ { sep++; next }
68+
sep < 2 { next }
69+
/^#/ { line = $0; sub(/^# ?/, "", line);
70+
print line; started = 1; next }
71+
started { exit }' "$0"
72+
exit "${1:-1}"
73+
}
74+
75+
cleanup()
76+
{
77+
if [ -n "$tmpdir" ]; then
78+
rm -rf "$tmpdir"
79+
fi
80+
}
81+
82+
while getopts ":o:f:b:j:lsah" opt; do
83+
case $opt in
84+
o) outfile=$OPTARG ;;
85+
f) filefile=$OPTARG ;;
86+
b) nxstyle=$OPTARG ;;
87+
j) jobs=$OPTARG ;;
88+
l) listonly=1 ;;
89+
s) summary=1 ;;
90+
a) allfiles=1 ;;
91+
h) usage 0 ;;
92+
*) usage ;;
93+
esac
94+
done
95+
shift $((OPTIND - 1))
96+
97+
trap cleanup EXIT
98+
tmpdir=$(mktemp -d)
99+
100+
# Build nxstyle unless an existing binary was given
101+
102+
if [ -z "$nxstyle" ]; then
103+
nxstyle=$tmpdir/nxstyle
104+
if ! ${CC:-cc} -O2 -o "$nxstyle" "$topdir/tools/nxstyle.c"; then
105+
echo "ERROR: failed to build nxstyle" >&2
106+
exit 1
107+
fi
108+
fi
109+
110+
cd "$topdir" || exit 1
111+
112+
# Collect the files to check. Only the sources that nxstyle understands are
113+
# of interest, and by default only those that are tracked by git.
114+
115+
listing=$tmpdir/files
116+
: > "$listing"
117+
118+
if [ $# -eq 0 ]; then
119+
set -- .
120+
fi
121+
122+
for dir in "$@"; do
123+
if [ ! -d "$dir" ]; then
124+
echo "ERROR: no such directory: $dir" >&2
125+
exit 1
126+
fi
127+
128+
if [ "$allfiles" -eq 0 ] && git rev-parse --git-dir >/dev/null 2>&1; then
129+
git ls-files -- "$dir/*.c" "$dir/*.h" >> "$listing"
130+
else
131+
find "$dir" -name '*.c' -o -name '*.h' >> "$listing"
132+
fi
133+
done
134+
135+
sort -u "$listing" -o "$listing"
136+
total=$(wc -l < "$listing")
137+
138+
if [ "$total" -eq 0 ]; then
139+
echo "No C sources found" >&2
140+
exit 1
141+
fi
142+
143+
# Check each file, keeping the output of one file together. Writing to a
144+
# file per source and concatenating afterwards avoids the interleaving that
145+
# a shared pipe would produce.
146+
147+
results=$tmpdir/results
148+
mkdir -p "$results"
149+
export nxstyle results
150+
151+
# shellcheck disable=SC2016
152+
xargs -a "$listing" -P "$jobs" -n 1 sh -c '
153+
out=$("$nxstyle" "$1" 2>&1)
154+
if [ -n "$out" ]; then
155+
printf "%s\n" "$out" > "$results/$(printf "%s" "$1" | tr / _)"
156+
fi
157+
exit 0
158+
' sh
159+
160+
collected=$tmpdir/all
161+
cat "$results"/* 2>/dev/null | sed "s#^$topdir/##" |
162+
sort -t: -k1,1 -k2,2n > "$collected"
163+
164+
ndiag=$(wc -l < "$collected")
165+
nfail=$(cut -d: -f1 "$collected" | sort -u | wc -l)
166+
167+
if [ "$listonly" -eq 1 ]; then
168+
cut -d: -f1 "$collected" | sort | uniq -c | sort -rn |
169+
awk '{printf "%6d %s\n", $1, $2}'
170+
else
171+
cp "$collected" "$outfile"
172+
cut -d: -f1 "$collected" | sort | uniq -c | sort -rn |
173+
awk '{printf "%6d %s\n", $1, $2}' > "$filefile"
174+
echo "$outfile: $ndiag diagnostics"
175+
echo "$filefile: $nfail files"
176+
fi
177+
178+
if [ "$summary" -eq 1 ]; then
179+
echo
180+
echo "Diagnostics by message:"
181+
sed 's/.*: \(error\|warning\|info\): //' "$collected" |
182+
sort | uniq -c | sort -rn | awk '{$1=$1; printf "%6d ", $1;
183+
$1=""; sub(/^ /, ""); print}'
184+
fi
185+
186+
echo
187+
echo "Checked $total files, $nfail failed"
188+
189+
[ "$ndiag" -eq 0 ]

0 commit comments

Comments
 (0)