Skip to content

Commit 313e960

Browse files
scripts: add unsafe_blocks_report.sh
The new script can be used to print statistics about undocumented unsafe blocks in our code base. Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
1 parent 775b751 commit 313e960

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

scripts/unsafe_blocks_report.sh

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6+
CLIPPY_SAFETY_MSG="missing a safety comment"
7+
8+
CLIPPY_FILE=
9+
TMP_FILE=
10+
QUIET=0
11+
12+
ALL=1
13+
MODULE=0
14+
TOTAL=0
15+
16+
cleanup() {
17+
if [ -n "${TMP_FILE}" ]; then
18+
rm "${TMP_FILE}"
19+
fi
20+
}
21+
trap cleanup EXIT
22+
23+
function usage
24+
{
25+
echo -e "usage: $0 [OPTION...]"
26+
echo -e ""
27+
echo -e "Print statistics about undocumented unsafe blocks"
28+
echo -e ""
29+
echo -e "Generic options:"
30+
echo -e " -f, --file FILENAME Use the specified file, instead of running clippy"
31+
echo -e " -q, --quiet Print just stats without log messages"
32+
echo -e " -h, --help Print this help"
33+
echo -e ""
34+
echo -e "By default all stats are printed; to select only some of them,"
35+
echo -e "please use the following options:"
36+
echo -e " -m, --module Undocumented unsafe blocks per module"
37+
echo -e " -t, --total Total number of undocumented unsafe blocks"
38+
}
39+
40+
while [[ $# -gt 0 ]]; do
41+
case $1 in
42+
-f | --file)
43+
CLIPPY_FILE="$2"
44+
shift
45+
;;
46+
-q | --quiet)
47+
QUIET=1
48+
;;
49+
-m | --module)
50+
ALL=0
51+
MODULE=1
52+
;;
53+
-t | --total)
54+
ALL=0
55+
TOTAL=1
56+
;;
57+
-h | --help)
58+
usage
59+
exit
60+
;;
61+
-*|--*)
62+
echo "Unknown option $1"
63+
exit 1
64+
;;
65+
*)
66+
echo "Invalid parameter $1"
67+
exit 1
68+
;;
69+
esac
70+
shift
71+
done
72+
73+
log() {
74+
if [ "${QUIET}" == "0" ]; then
75+
echo -e "$@"
76+
fi
77+
}
78+
79+
clippy_unsafe_blocks_per_module() {
80+
grep -A 1 "${CLIPPY_SAFETY_MSG}" "$1" | grep "\-->" | awk '{print $2}' | \
81+
sort | uniq | cut -d':' -f1 | \
82+
awk -F'/' '{
83+
module="";
84+
for(i=1; i <= NF; i++) {
85+
module=module "/" $i;
86+
print module
87+
}
88+
}' | sort | uniq -c
89+
}
90+
91+
clippy_unsafe_blocks_total() {
92+
grep -A 1 "${CLIPPY_SAFETY_MSG}" "$1" | grep "\-->" | awk '{print $2}' | \
93+
sort | uniq | wc -l
94+
}
95+
96+
if [ -z "${CLIPPY_FILE}" ]; then
97+
log "Running \`make clippy\`..."
98+
TMP_FILE=$(mktemp)
99+
make -C "${SCRIPT_DIR}/.." clippy CLIPPY_OPTIONS="--quiet --color=never" \
100+
UNSAFE_BLOCKS=1 2>"${TMP_FILE}"
101+
CLIPPY_FILE="${TMP_FILE}"
102+
log ""
103+
fi
104+
105+
if [[ "${ALL}" == "1" || "${MODULE}" == "1" ]]; then
106+
log "Undocumented unsafe blocks per module\n"
107+
clippy_unsafe_blocks_per_module "${CLIPPY_FILE}"
108+
log ""
109+
fi
110+
111+
if [[ "${ALL}" == "1" || "${TOTAL}" == "1" ]]; then
112+
log "Total number of undocumented unsafe blocks\n"
113+
clippy_unsafe_blocks_total "${CLIPPY_FILE}"
114+
log ""
115+
fi

0 commit comments

Comments
 (0)