-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmlb-coverage
executable file
·167 lines (133 loc) · 4.4 KB
/
mlb-coverage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
#
# mlb-coverage - compile and run an SML program defined in a MLB file
# using MLton and print a code coverage report
#
# Chris Cannam, 2015-2018. MIT licence
logfile=mlb-coverage.log
usage () {
cat 1>&2 <<EOF
Usage:
$(basename $0) program.mlb [args...]
print coverage summary for running program.mlb with supplied args
$(basename $0) -f file.sml program.mlb [args...]
print annotation of only file.sml showing which lines were not executed
The program's standard output will be redirected to $logfile.
This will silently clobber any existing content in that file.
If the args are followed by a double-dash ("--"), all subsequent
arguments will be passed to the MLton compiler. If a further "--"
is found, arguments after that will be passed as object files to the
MLton compiler (previous args will go before the .mlb argument).
This is likely to be a fragile script, dependent both on specific
behaviour from MLton and its profiler and on specific shell features
and supporting tools.
Example:
$(basename $0) -f src/tricky.sml program.mlb data.txt -- -default-ann 'allowFFI true' -- export.o
EOF
exit 2
}
mlb="$1"; shift
srcfile=""
if [ "$mlb" = "-f" ]; then
srcfile="$1"; shift
mlb="$1"; shift
fi
declare -a mlton_args
declare -a object_args
declare -a program_args
while [ "$#" -gt 0 -a "$1" != "--" ]; do
program_args[${#program_args[@]}]="$1"
shift
done
if [ "$1" = "--" ]; then
shift
while [ "$#" -gt 0 -a "$1" != "--" ]; do
mlton_args[${#mlton_args[@]}]="$1"
shift
done
if [ "$1" = "--" ]; then
shift
fi
while [ "$#" -gt 0 ]; do
object_args[${#object_args[@]}]="$1"
shift
done
fi
if [ -z "$mlb" ]; then usage; fi
set -e
mydir=$(dirname "$0")
. "$mydir/smlbuild-include.sh"
PROGRAM=$(get_outfile "$mlb")
set -u
rm -f mlmon.out
echo "+++ Compiling program $PROGRAM.mlb..." 1>&2
set +u # or else bash on osx erroneously (I think?) complains the arrays
# are undefined when they are simply empty
mlton -profile count -profile-branch true -profile-val true "${mlton_args[@]}" "$PROGRAM.mlb" "${object_args[@]}"
echo "+++ Executing program $PROGRAM..." 1>&2
if ./"$PROGRAM" "${program_args[@]}" >"$logfile" ; then
echo "+++ Program completed: stdout is in $logfile" 1>&2
else
echo "--- Program returned exit code $?: stdout is in $logfile" 1>&2
fi
set -u
tmpfile=/tmp/"$$"_cov
trap "rm -f $tmpfile" 0
# Mangle the output of mlprof into a series of lines of the form
# filename,lineno,yes (or no)
# indicating whether the given line of the given file has been
# executed.
# Mlprof sometimes outputs more than one result for a given source
# line; we want to remove these duplicates because we use the line
# count to calculate our %ages, and also if it outputs both "yes" and
# "no", we want to keep only the "yes". The "sort -r | perl" business
# does that, by sorting on filename and line and then with "yes"
# before "no", and then keeping only the first in any sequence of
# lines with a common filename and line.
mlprof -raw true -show-line true "$PROGRAM" mlmon.out |
grep '\.sml: [0-9]' |
sed 's|^.* \([A-Za-z][^ ]*\.sml\)|\1|' |
sed 's|: | |' |
awk '{ print $1","$2","$4 }' |
sed 's|(0)|no|g' |
sed 's|([0-9,]*)|yes|g' |
sort -r |
perl -e 'while (<>) { ($f, $n, $b) = split /,/; next if ($f eq $pf and $n eq $pn); print; $pf = $f; $pn = $n }' > "$tmpfile"
summarise_for() {
what=$(canonicalise "$1")
yes=$(fgrep "$what" "$tmpfile" | grep ",yes$" | wc -l | sed 's/ //g')
no=$(fgrep "$what" "$tmpfile" | grep ",no$" | wc -l | sed 's/ //g')
total=$(($yes + $no))
if [ "$total" = "0" ]; then
echo " --% $what (0/0)"
else
percent=$(((100 * $yes) / $total))
if [ "$percent" = 100 ]; then
echo " 100% $what ($yes/$total)"
elif [ "$percent" -lt 10 ]; then
echo " $percent% $what ($yes/$total)"
else
echo " $percent% $what ($yes/$total)"
fi
fi
}
if [ "$srcfile" = "" ]; then
summarise_for "sml"
expand_arg -u "$mlb" | grep -v '^/' | grep -v '\.sig$' | LANG=C LC_ALL=C sort |
while read x; do
summarise_for "$x" ;
done
else
# A monumentally inefficient way to show the lines lacking
# coverage from a given source file
cat -n "$srcfile" |
sed 's|^ *||' |
while read x; do
n=${x%%[^0-9]*}
if grep -q "$srcfile,$n,no" "$tmpfile" ;
then echo " ### $x";
else echo " $x";
fi;
done | \
grep -C2 '^ ###'
fi