coverage 2.36 KB
Newer Older
1 2 3 4 5
#!/bin/sh
#
# Calculate coverage of source files
#

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
gcovflags=-n

usage ()
{
    cat <<EOF
usage: coverage [OPTIONS] [output-files]
    where OPTIONS are:
    --output      generate .gcov files
    --help -h -?  print this message
EOF
    exit $1
}

while true; do
case "$1" in
--output ) gcovflags= ; shift ;;
'-?' | -h | --help ) usage ;;
- ) shift ; break ;;
-* ) usage 1 ;;
* ) break ;;
esac
done

29
{
30
for f in *.bb
31
do
32
    test "$f" = "*.bb" && { echo "run 'make check' first"; exit 1; }
33
    gcov $gcovflags "$f" 2>/dev/null
34
done
35
for f in $@
36
do
37
    gcov $gcovflags "$f" 2>/dev/null
38
done
39 40
} |
awk '
41 42 43
BEGIN {
'"$(for f in "$@"; do echo 'include["'$f'"] = 1;'; done)"'
}
44 45 46 47 48 49 50 51 52 53

/^File/ {
    if (sub(/^File `\/home\/ppessi\/sofia-proxy\/(proxy\/..\/)?/, "")) {
	sub(/.$/, "");
	file=$0;
	if (file ~ /torture/) file = "";
    }
    else
	file = "";
}
54 55
file ~ /\.[hc]$/ && /Lines executed/ {
	sub(/Lines executed:/, "");
56
	covered = int($3*$1/100 + 0.5);
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71
#	printf "%-5u %-5u %-5u %-7s %s\n", $3, covered, $3 - covered, $1, file;
	if ($3 == 0) {
		/* skip */
        }
	else if (!lines[file]) {
          	names[i++] = file; lines[file] = $3; coverage[file] = covered;
		coverage[file] = $3*$1/100;
        }
	else if (coverage[file] < covered) {
		lines[file] = $3; coverage[file] = covered;
		coverage[file] = $3*$1/100;
        }
}

72
/torture/ { next; }
73 74 75
/test/ { next; }
/\/usr\/include/ { next; }

76
/(source )?lines executed in file (.*\/)?[-A-Za-z_0-9]+\.[ch]/ {
77
        file = $0;
Pekka Pessi's avatar
Pekka Pessi committed
78
	sub(/.* in file /, "", file);
79 80 81
	sub(/.*\//, "", file);
	covered = int($3*$1/100 + 0.5);
	#printf "%-5u %-5u %-5u %-7s %s\n", $3, covered, $3 - covered, $1, file;
82 83 84 85

	if ($3 == 0) {
		/* skip */
        }
86
	else if (!file in lines) {
87 88
		lines[file] = $3; coverage[file] = $3*$1/100;
        }
89
	else if (coverage[file] <= $3*$1/100) {
90 91 92 93 94
		lines[file] = $3; coverage[file] = $3*$1/100;
        }
}

END {
95 96 97
    for (file in lines) {
        if (!(file in include))
	    continue;
98 99
	l = lines[file]; c = coverage[file];
	covered = int(c + 0.5);
100
        total += l; total_coverage += c;
101
	printf "%-5u %-5u %-5u %6.2f%%  %s\n",
102 103 104
		l, covered, l - covered, 100.0 * c / l, file;
    }
    if (total) {
Pekka Pessi's avatar
Pekka Pessi committed
105 106 107 108
      module = ENVIRON["PWD"];
      gsub(/\/+$/, "", module);
      gsub(/.*\//, "", module);
      printf "%-5u %-5u %-5u %6.2f%%  %s in %s\n",
109
	total, total_coverage, total - total_coverage,
Pekka Pessi's avatar
Pekka Pessi committed
110
	100.0 * total_coverage / total, "TOTAL", module;
111 112
  }
}
Pekka Pessi's avatar
Pekka Pessi committed
113 114
' |
sort -n