Debian Codegraph @

tests/bin/norminfo

raw
1 #!/usr/bin/env perl
2 #
3 # Copyright IBM Corp. 2017
4 #
5 # Usage: norminfo <coverage-data-file> [<multiplier>]
6 #
7 # Normalize coverage data file (ensure stable order), perform some sanity
8 # checks, and apply optional multiplier to execution counts.
9 #
10
11 use strict;
12 use warnings;
13
14 sub ferr($$$)
15 {
16 my ($pos, $filename, $msg) = @_;
17
18 if (defined($pos)) {
19 $pos .= ":";
20 } else {
21 $pos = "";
22 }
23
24 die("$0:$filename:$pos $msg");
25 }
26
27 sub print_sorted($$$)
28 {
29 my ($fd, $info, $multi) = @_;
30 my (%fn, %fns, %fnda, %brda, %da);
31 my ($fnf, $fnh, $brf, $brh, $lf, $lh);
32 my ($idx, $lineNo, $endLine);
33
34 while (my $line = <$fd>) {
35 $line =~ s/(^\s*|\s*$)//g;
36
37 if ($line =~ /^end_of_record$/) {
38 last;
39 } elsif ($line =~ /^FNL:(\d+),(\d+)(,(\d+))?$/) {
40 # generate backward-compatible data for the moment
41 $idx = $1;
42 $lineNo = $2;
43 $endLine = $4;
44 } elsif ($line =~ /^FNA:(\d+),([^,]+),(.+)$/) {
45 die("unexpected index $1 != $idx") unless $1 == $idx;
46 $idx = undef;
47 my $hit = $2;
48 my $fnname = $3;
49 $fn{$lineNo} = [$fnname, $endLine];
50 $fns{$fnname} = $lineNo;
51 $fnda{$fnname} = int($hit * $multi);
52 } elsif ($line =~ /^FN:(\d+),((\d+),)?(.*)$/) {
53 my ($lineno, $fnname) = ($1, $4);
54
55 if (exists($fn{$lineno})) {
56 ferr($., $info, "Duplicate FN: entry\n");
57 }
58 $fn{$lineno} = [$fnname, $3];
59 if (exists($fns{$fnname})) {
60 ferr($., $info, "Duplicate function name\n");
61 }
62 $fns{$fnname} = $lineno;
63 } elsif ($line =~ /^FNDA:(\d+),(.*)$/) {
64 my ($count, $fnname) = ($1, $2);
65
66 if (exists($fnda{$fnname})) {
67 ferr($., $info, "Duplicate FNDA: entry\n");
68 }
69 $fnda{$fnname} = int($count * $multi);
70 } elsif ($line =~ /^FNF:(\d+)$/) {
71 if (defined($fnf)) {
72 ferr($., $info, "Duplicate FNF: entry\n");
73 }
74 $fnf = $1;
75 } elsif ($line =~ /^FNH:(\d+)$/) {
76 if (defined($fnh)) {
77 ferr($., $info, "Duplicate FNH: entry\n");
78 }
79 $fnh = $1;
80 } elsif ($line =~ /^BRDA:(\d+),(e)?(\d+),(\d+),(\d+|-)$/) {
81 my ($lineno, $is_exception, $block, $branch, $count) =
82 ($1, defined($2) && $2 eq 'e', $3, $4, $5);
83
84 if (exists($brda{$lineno}->{$block}->{$branch})) {
85 ferr($., $info, "Duplicate BRDA: entry\n");
86 }
87 $count = int($count * $multi) if ($count ne "-");
88 $brda{$lineno}->{$block}->{$branch} = [$count, $is_exception];
89
90 } elsif ($line =~ /^BRF:(\d+)$/) {
91 if (defined($brf)) {
92 ferr($., $info, "Duplicate BRF: entry\n");
93 }
94 $brf = $1;
95 } elsif ($line =~ /^BRH:(\d+)$/) {
96 if (defined($brh)) {
97 ferr($., $info, "Duplicate BRH: entry\n");
98 }
99 $brh = $1;
100 } elsif ($line =~ /^DA:(\d+),(\d+)$/) {
101 my ($lineno, $count) = ($1, $2);
102
103 if (exists($da{$lineno})) {
104 ferr($., $info, "Duplicate FNDA: entry\n");
105 }
106 $da{$lineno} = int($count * $multi);
107 } elsif ($line =~ /^LF:(\d+)$/) {
108 if (defined($lf)) {
109 ferr($., $info, "Duplicate LF: entry\n");
110 }
111 $lf = $1;
112 } elsif ($line =~ /^LH:(\d+)$/) {
113 if (defined($lh)) {
114 ferr($., $info, "Duplicate LH: entry\n");
115 }
116 $lh = $1;
117 } else {
118 ferr($., $info, "Unknown line: $line\n");
119 }
120 }
121
122 # FN:<line>,<fnname>
123 foreach my $lineno (sort({ $a <=> $b } keys(%fn))) {
124 my ($fnname, $endLine) = @{$fn{$lineno}};
125 $endLine = $endLine ? '.' . $endLine : '';
126 print("FN:$lineno$endLine,$fnname\n");
127 }
128
129 # FNDA:<counts>,<fnname>
130 foreach my $fnname (keys(%fnda)) {
131 if (!exists($fns{$fnname})) {
132 ferr(undef, $info, "FNDA entry without FN: $fnname\n");
133 }
134 }
135 foreach my $fnname (sort({ $fns{$a} <=> $fns{$b} } keys(%fnda))) {
136 my $count = $fnda{$fnname};
137 print("FNDA:$count,$fnname\n");
138 }
139 # FNF:<counts>
140 print("FNF:$fnf\n") if (defined($fnf));
141 # FNH:<counts>
142 if (defined($fnh)) {
143 $fnh = 0 if ($multi == 0);
144 print("FNH:$fnh\n");
145 }
146 # BRDA:<line>,<exception><block>,<branch>,<count>
147 foreach my $lineno (sort({ $a <=> $b } keys(%brda))) {
148 my $blocks = $brda{$lineno};
149
150 foreach my $block (sort({ $a <=> $b } keys(%{$blocks}))) {
151 my $branches = $blocks->{$block};
152
153 foreach my $branch (sort({ $a <=> $b }
154 keys(%{$branches}))
155 ) {
156 my ($count, $is_exception) = @{$branches->{$branch}};
157
158 $count = "-" if ($multi == 0);
159 print("BRDA:$lineno," .
160 (defined($is_exception) && $is_exception ? 'e' : '') .
161 "$block,$branch,$count\n");
162 }
163 }
164
165 }
166 # BRF:<counts>
167 print("BRF:$brf\n") if (defined($brf));
168 # BRH:<counts>
169 if (defined($brh)) {
170 $brh = 0 if ($multi == 0);
171 print("BRH:$brh\n");
172 }
173 # DA:<line>,<counts>
174 foreach my $lineno (sort({ $a <=> $b } keys(%da))) {
175 my $count = $da{$lineno};
176
177 print("DA:$lineno,$count\n");
178 }
179 # LF:<counts>
180 print("LF:$lf\n") if (defined($lf));
181 # LH:<count>
182 if (defined($lh)) {
183 $lh = 0 if ($multi == 0);
184 print("LH:$lh\n");
185 }
186 }
187
188 sub main()
189 {
190 my $infofile = $ARGV[0];
191 my $multi = $ARGV[1];
192 # info: testname -> files
193 # files: infofile -> data
194 # data: [ starting offset, starting line ]
195 my %info;
196 my $fd;
197 my $tn = "";
198 my %allfiles;
199
200 $multi = 1 if (!defined($multi));
201 if (!defined($infofile)) {
202 $infofile = "standard input";
203 warn("$0: Reading data from standard input\n");
204 open($fd, "<&STDIN") or
205 die("$0: Could not duplicated stdin: $!\n");
206 } else {
207 open($fd, "<", $infofile) or
208 die("$0: Could not open $infofile: $!\n");
209 }
210
211 # Register starting positions of data sets
212 while (my $line = <$fd>) {
213 if ($line =~ /^TN:(.*)$/) {
214 $tn = $1;
215 } elsif ($line =~ /^SF:(.*)$/) {
216 my $sf = $1;
217 my $pos = tell($fd);
218
219 die("$0: Could not get file position: $!\n")
220 if ($pos == -1);
221 if (exists($info{$tn}->{$sf})) {
222 ferr($., $infofile, "Duplicate entry for $tn:$sf\n");
223 }
224 $info{$tn}->{$sf} = [$pos, $.];
225 $allfiles{$sf} = 1;
226 }
227 }
228
229 # Print data sets in normalized order
230 foreach my $filename (sort(keys(%allfiles))) {
231 foreach my $testname (sort(keys(%info))) {
232 my $pos = $info{$testname}->{$filename};
233 my ($cpos, $lpos) = @$pos;
234
235 next if (!defined($pos));
236
237 if (seek($fd, $cpos, 0) != 1) {
238 die("$0: Could not seek in $infofile: $!\n");
239 }
240 printf("TN:$testname\n");
241 printf("SF:$filename\n");
242
243 $. = $lpos;
244 print_sorted($fd, $infofile, $multi);
245
246 printf("end_of_record\n");
247
248 }
249 }
250 foreach my $testname (sort(keys(%info))) {
251 my $files = $info{$testname};
252
253 foreach my $filename (sort(keys(%{$files}))) {
254 }
255 }
256
257 close($fd);
258 }
259
260 main();
261 exit(0);