-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcoverage.sh
More file actions
78 lines (66 loc) · 2.46 KB
/
Copy pathcoverage.sh
File metadata and controls
78 lines (66 loc) · 2.46 KB
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
#!/bin/bash
## First, get the overall coverage data
cov_data=$(gcovr -r . -s | grep "[lb][a-z]*:")
l_per=$(echo "$cov_data" | grep lines | cut -d" " -f2 | rev | cut -c2- | rev)
l_abs=$(echo "$cov_data" | grep lines | cut -d" " -f3 | cut -c2-)
b_per=$(echo "$cov_data" | grep branch | cut -d" " -f2 | rev | cut -c2- | rev)
b_abs=$(echo "$cov_data" | grep branch | cut -d" " -f3 | cut -c2-)
covered_times_of_line=0
## Second, get the covered times of specific line in the file if three arguments are provided
if [ $# -eq 3 ]; then
relative_path=$1
line_no=$2
line_content=$3 # already stripped
# check if relative_path exists and is a file
if [ ! -f "$relative_path" ]; then
echo "File $relative_path does not exist"
exit 1
fi
# generate gcov file
src_dir=$(dirname "$relative_path")
basename=$(basename "$relative_path")
cd "$src_dir"
output=$(gcov -r -b -o . "$basename" 2>&1)
gcov_file="$basename.gcov"
if [ ! -f "$gcov_file" ]; then
echo "GCOV file $gcov_file not found"
exit 1
fi
# The line_no is not accurate, we should locate the exact line by looking around the given line_no using the line_content
search_range=30
lower_bound=$((line_no - search_range))
upper_bound=$((line_no + search_range))
covered_times_of_line=$(awk -v lb="$lower_bound" -v ub="$upper_bound" -v want="$line_content" '
BEGIN {
printed = 0
# trim and collapse whitespaces
gsub(/^[ \t]+|[ \t]+$/, "", want)
gsub(/[ \t]+/, " ", want)
}
{
p1 = index($0, ":")
p2 = index(substr($0, p1 + 1), ":")
if (!p1 || !p2) next
p2 += p1
count = substr($0, 1, p1 - 1)
lineno = substr($0, p1 + 1, p2 - p1 - 1) + 0
code = substr($0, p2 + 1)
if (lineno >= lb && lineno <= ub) {
gsub(/^[ \t]+|[ \t]+$/, "", code)
gsub(/[ \t]+/, " ", code)
if (code == want) {
gsub(/^[ \t]+/, "", count)
printed = 1
if (count ~ /^[-#]/) print 0
else print count + 0
exit
}
}
if (lineno > ub) {
exit
}
}
END { if (!printed) print 0 }
' "$gcov_file")
fi
echo "$l_per,$l_abs,$b_per,$b_abs,$covered_times_of_line"