Debian Codegraph @

scripts/analyzeInfoFiles

raw
1 #!/usr/bin/env perl
2
3 # a bit of a hack:
4 # Look through a set of .info files
5 # - assumption is that all the files are for an identical code base
6 # Find places where the coverpoints in the files differ:
7 # - one file contains source that is not in the other
8 # - source from one file contains coverpoints not found in the other
9
10 use strict;
11 use warnings;
12 use Getopt::Long;
13 use FindBin;
14
15 use lib "$FindBin::RealBin/../lib";
16 use lib "$FindBin::RealBin/../../../lib"; # path from 'support-scripts'
17
18 use lcovutil;
19
20 our ($keep_going, $dontDrop, $compact, $sortBySize, $reportAgreeBlocks,
21 $verbose);
22
23 our @infoFileArray;
24
25 package Region;
26
27 sub new
28 {
29 my ($class, $start, $finish, $in, $out) = @_;
30
31 my $self = [$start, $finish, $in, $out];
32 bless $self, $class;
33 return $self;
34 }
35
36 sub start
37 {
38 my $self = shift;
39 return $self->[0];
40 }
41
42 sub finish
43 {
44 my $self = shift;
45 return $self->[1];
46 }
47
48 sub size
49 {
50 my $self = shift;
51 return $self->[1] - $self->[0] + 1;
52 }
53
54 sub consistent
55 {
56 my $self = shift;
57 return !(scalar(@{$self->[2]}) && scalar(@{$self->[3]}));
58 }
59
60 sub in
61 {
62 my $self = shift;
63 return $self->[2];
64 }
65
66 sub out
67 {
68 my $self = shift;
69 return $self->[3];
70 }
71
72 sub buildCodeKey
73 {
74 my $self = shift;
75 my $code = $self->[2];
76 my $notcode = $self->[3];
77 my $key = '';
78 for (my $i = 0; $i < $main::numInfoFiles; ++$i) {
79 #my $name = $main::infoFileArray[$i];
80 my $char = $i ~~ @{$code} ? '1' : ($i ~~ @$notcode ? '_' : '.');
81 $key .= $char;
82 }
83 return $key;
84 }
85
86 sub toNames
87 {
88 my $list = shift;
89 my @rtn;
90 foreach my $idx (@$list) {
91 push(@rtn, $main::infoFileArray[$idx]);
92 }
93 return @rtn;
94 }
95
96 sub print
97 {
98 my ($self, $dest) = @_;
99
100 my ($first, $last, $code, $nonCode) = @$self;
101
102 print($dest "$first : $last:");
103
104 if ($main::compact) {
105 print($dest ' ', $self->buildCodeKey(), ' ', $self->size(), " line",
106 $self->size() == 1 ? '' : 's', "\n");
107 } else {
108 print($dest " ", $self->size(), " line",
109 $self->size() == 1 ? '' : 's', "\n");
110 if (%$code) {
111 if ($main::verbose) {
112 print($dest "\tcode:\n\t\t" .
113 join("\n\t\t", toNames($code)) . "\n");
114 } else {
115 print($dest "\tcode: " . join(' ', @$code) . "\n");
116 }
117 }
118 if (%$nonCode) {
119 if ($main::verbose) {
120 print($dest "\tnot code:\n\t\t" .
121 join("\n\t\t", toNames($nonCode)) . "\n");
122 } else {
123 print($dest "\tnot code: " . join(' ', @$nonCode) . "\n");
124 }
125 }
126 }
127 }
128
129 package FileData;
130
131 sub new
132 {
133 my ($class, $filename) = @_;
134
135 # Data: filename, [TraceInfo, # info for this file
136 # list of [traceInfo, infoFileIndex] for each .info file containing this file
137 # list of regions in order: [first, last, [in], [out]]
138 my $self = [$filename, [], []];
139 bless $self, $class;
140
141 return $self;
142 }
143
144 sub name
145 {
146 my $self = shift;
147 return $self->[0];
148 }
149
150 sub traces
151 {
152 # return list of [TraceInfo, infoFileIndex] for every .info file containing
153 # this source file
154 my $self = shift;
155 return $self->[1];
156 }
157
158 sub regions
159 {
160 # return list of [first last in out] for every identified region
161 my $self = shift;
162 return $self->[2];
163 }
164
165 sub regionsBySize
166 {
167 my $self = shift;
168 return
169 sort({ $b->size() <=> $a->size() or
170 $a->start() <=> $b->start() } @{$self->regions()});
171 }
172
173 sub totalRegionSize
174 {
175 my $self = shift;
176 my $size = 0;
177 foreach my $r (@{$self->regions()}) {
178 $size += $r->size();
179 }
180 return $size;
181 }
182
183 sub printCodeGroups
184 {
185 my ($code, $nonCode, $dest) = @_;
186 if ($main::compact) {
187 print($dest ' ', buildCodeKey($code, $nonCode, $main::numInfoFiles),
188 "\n");
189 } else {
190 print($dest "\n");
191 if (%$code) {
192 if ($main::verbose) {
193 print($dest "\tcode:\n\t\t" .
194 join("\n\t\t", hashToNames($code)) . "\n");
195 } else {
196 print($dest "\tcode: " . join(' ', sort keys(%$code)) . "\n");
197 }
198 }
199 if (%$nonCode) {
200 if ($main::verbose) {
201 print($dest "\tnot code:\n\t\t" .
202 join("\n\t\t", hashToNames($nonCode)) . "\n");
203 } else {
204 print($dest "\tnot code: " .
205 join(' ', sort keys(%$nonCode)) . "\n");
206 }
207 }
208 }
209 }
210
211 sub print
212 {
213 my ($self, $dest) = @_;
214 $dest = \*STDOUT unless defined($dest);
215
216 my $title =
217 $self->name() . ":\n total: " . $self->totalRegionSize() . "\n";
218
219 foreach
220 my $r ($main::sortBySize ? $self->regionsBySize() : @{$self->regions()})
221 {
222 print($dest $title);
223 $r->print($dest);
224 $title = '';
225 }
226 return $title eq ''; # return non-zero if I printed something
227 }
228
229 sub checkLineCoverageConsistency
230 {
231 my $self = shift;
232
233 my $traces = $self->traces();
234 return 1
235 if (scalar(@$traces) < 2); # nothing to check..only one set of data
236
237 # first, collect line coverage data for all the .info files
238 # does everyone agree that this line is code or is not code?
239 my @lineCovData;
240 foreach my $t (@$traces) {
241 my ($traceInfo, $idx) = @$t;
242 my $d = $traceInfo->sum();
243 push(@lineCovData, [$d, $idx]);
244 }
245 my $srcfile = $self->name();
246 my $consistent = 1;
247
248 my $numLines;
249 if (-f $srcfile) {
250 ($numLines) = split(' ', `wc -l $srcfile`);
251 } else {
252 # hard way: look through all the data to find the highest line number
253 $numLines = 0;
254 foreach my $lineData (@lineCovData) {
255 my @lines = sort $lineData->[0]->keylist();
256 my $largest = $lines[-1];
257 $numLines = $largest
258 if $largest > $numLines;
259 }
260 }
261 my $currentGroupStart;
262 my %currentCodeGroup;
263 my %currentNonCodeGroup;
264
265 for (my $lineNo = 1; $lineNo < $numLines; ++$lineNo) {
266 my %codeGroup;
267 my %nonCodeGroup;
268 foreach my $lineData (@lineCovData) {
269 my ($d, $infoFileIdx) = @$lineData;
270 if (defined($d->value($lineNo))) {
271 $codeGroup{$infoFileIdx} = 1;
272 } else {
273 $nonCodeGroup{$infoFileIdx} = 1;
274 }
275 }
276 if (!defined($currentGroupStart)) {
277 $currentGroupStart = $lineNo;
278 %currentNonCodeGroup = %nonCodeGroup;
279 %currentCodeGroup = %codeGroup;
280 next;
281 }
282 if (!%codeGroup ||
283 !%nonCodeGroup) {
284 # everyone agrees that this is is is not code..
285 if (scalar(%currentCodeGroup) &&
286 scalar(%currentNonCodeGroup)) {
287
288 # there was disagreement before, but now we agree
289 my $region = Region->new($currentGroupStart,
290 $lineNo - 1,
291 [sort(keys %currentCodeGroup)],
292 [sort(keys %currentNonCodeGroup)]);
293 push(@{$self->regions()}, $region);
294
295 #print($title, ' ');
296 #$title = '';
297 #$region->print(\*STDOUT);
298 $currentGroupStart = $lineNo;
299 $consistent = 0;
300 }
301 %currentCodeGroup = %codeGroup;
302 %currentNonCodeGroup = %nonCodeGroup;
303 } else {
304 # we have some disagreement about whether this is code or not
305 # - is the 'in' and 'out' group the same as it was before?
306 if (!(hashIsSame(\%currentCodeGroup, \%codeGroup) &&
307 hashIsSame(\%currentNonCodeGroup, \%nonCodeGroup))) {
308 if (defined($main::reportAgreeBlocks) ||
309 (%currentCodeGroup &&
310 %currentNonCodeGroup)
311 ) {
312
313 my $region =
314 Region->new($currentGroupStart,
315 $lineNo - 1,
316 [sort(keys %currentCodeGroup)],
317 [sort(keys %currentNonCodeGroup)]);
318 push(@{$self->regions()}, $region);
319 #print($title, ' ');
320 #$title = '';
321 #$region->print(\*STDOUT);
322 }
323 %currentNonCodeGroup = %nonCodeGroup;
324 %currentCodeGroup = %codeGroup;
325 $currentGroupStart = $lineNo;
326 $consistent = 0;
327 }
328 }
329 }
330 if (%currentCodeGroup &&
331 %currentNonCodeGroup) {
332 $consistent = 0;
333 }
334 # got to end of file..
335 if ($reportAgreeBlocks || (%currentCodeGroup && %currentNonCodeGroup)) {
336 my $region = Region->new($currentGroupStart,
337 $numLines,
338 [sort(keys %currentCodeGroup)],
339 [sort(keys %currentNonCodeGroup)]);
340 push(@{$self->regions()}, $region);
341 #print($title, " ");
342 #$region->print(\*STDOUT);
343 }
344 return $consistent;
345 }
346
347 sub hashIsSame
348 {
349 my ($hash1, $hash2) = @_;
350
351 while (my ($k, $v) = each(%$hash1)) {
352 return 0 unless exists($hash2->{$k});
353 }
354 while (my ($k, $v) = each(%$hash2)) {
355 return 0 unless exists($hash1->{$k});
356 }
357 return 1;
358 }
359
360 package main;
361
362 my $help;
363
364 my $err = !GetOptions("verbose|v" => \$verbose,
365 'substitute=s' => \@lcovutil::file_subst_patterns,
366 'exclude=s' => \@lcovutil::exclude_file_patterns,
367 'include=s' => \@lcovutil::include_file_patterns,
368 "keep-going" => \$keep_going,
369 "drop" => \$dontDrop,
370 "all" => \$reportAgreeBlocks,
371 "compact" => \$compact,
372 "sort" => \$sortBySize,
373 "help|h" => \$help,);
374
375 if ($err) {
376 print(STDERR "$0: invalid argument:\n");
377 }
378 if ($err || $help) {
379 if (!$err) {
380 print("Check for consistency in set of .info files\n");
381 }
382 my $dest = $help ? \*STDOUT : \*STDERR;
383 print($dest <<EOF);
384 Usage:
385 $0 \\
386 [--include glob] [--exclude glob] [--substitute regexp] \\
387 [--all] [--drop] [--keep-going] [--compact] [--sort] \\
388 [--verbose] [--help] infoFile infoFile ... \\
389
390 infoFile : .info file (which must end in ".info") or data file
391 containing names of .info files.
392 Data file comment character is '#'
393 --include glob : glob match source filename to check
394 --exclude glob : glob match source filename to skip
395 --substitute regexp : munge source file path when reading .info files
396 --keep-going : do not stop after mismatch found
397 --all : print all regions (not just regions conflicting votes)
398 --drop : ignore .info file if it does not contain some source
399 file - continue to check consistency in the .info files
400 which do contain the file
401 --compact : compact printing of source code region
402 --sort : sort by region size
403 --help : print this message
404 --verbose : be chatty
405
406 Check for:
407 - Is every source file present in every .info file ?
408 i.e. was some source file simply not present/not processed
409 in some .info files?
410 - No further properties are checked for this file, if the
411 --drop flag is present.
412 - if --keep-going flag not set, stop after printing names of
413 missing files.
414 - For every source file that is not dropped:
415 - Is the version ID the same in all the .info files?
416 - Do all the .info files agree about the status of every line?
417 - Do they all say the line is code or is not code?
418 Or do some .info files say one thing and some another?
419 - For every contiguous block with the same "code/not code" votes,
420 Print the start/end of the block and the vote
421 - If the --all flag is set: print the start/end of blocks
422 with unanimous votes as well.
423 - Note: these checks require that the script be able to access
424 the source code
425 - If it cannot find the source file, then it prints a message.
426 Die unless --keep-going flag is set.
427
428 EOF
429 exit($err);
430 }
431 lcovutil::munge_file_patterns(); # used for exclude/include
432
433 foreach my $infoFile (@ARGV) {
434 if ($infoFile =~ /\.info(\.gz)?$/) {
435 push(@infoFileArray, $infoFile);
436 } else {
437 open(INPUT, "<", $infoFile) or
438 die("Error: unable to opend $infoFile: $!\n");
439 while (<INPUT>) {
440 chomp($_);
441 next
442 if ($_ eq '' ||
443 $_ =~ /\s*#/); # comment character
444 push(@infoFileArray, $_);
445 }
446 close(INPUT);
447 }
448 }
449
450 my %infoFiles;
451 my %sourceFiles;
452
453 our $numInfoFiles = scalar(@infoFileArray);
454
455 my $status = 0;
456
457 my $idx = -1;
458 print("Info file mapping:\n");
459 foreach my $f (@infoFileArray) {
460 $idx++;
461 print(" $idx: $f\n");
462 }
463 print("\n\n");
464
465 $idx = -1;
466 foreach my $f (@infoFileArray) {
467 my $info = TraceFile->load($f);
468 $infoFiles{$f} = [$info, ++$idx];
469 # and collect list of source files that appear in each
470 foreach my $src ($info->files()) {
471 my $sourceFileData;
472 if (!exists($sourceFiles{$src})) {
473 $sourceFileData = FileData->new($src);
474 $sourceFiles{$src} = $sourceFileData;
475 } else {
476 $sourceFileData = $sourceFiles{$src};
477 }
478
479 my $traces = $sourceFileData->traces();
480 my $fInfo = $info->data($src)
481 ; # the TraceInfo for this source, in this .info file
482 if (@$traces) {
483 # this isn't the first time we see this source file - check for version mismatch
484 my $fv = $fInfo->version();
485 my $v = $traces->[0]->[0]->version();
486 if ((defined($fv) && !defined($v)) ||
487 (!defined($fv) && defined($v)) ||
488 (defined($fv) && defined($v) && $fv ne $v)) {
489 # versions don't match - so don't bother to check for matching
490 # line coverage data
491 print("Error: version mismatch for $src between:\n\t" .
492 $traces->[0]->[1] .
493 ": " . ($v ? $v : 'undef') . "\n\t" . $f . ": " .
494 ($fv ? $fv : 'undef') . "\n");
495 $status = 1;
496 # version mismatch - so don't bother checking data for this file
497 next;
498 }
499 }
500 push(@$traces, [$fInfo, $idx]);
501 }
502 }
503
504 # we have a list of all the source files now...
505 # check that all of them appear in every .info file we read
506 my %missing;
507 foreach my $filename (sort keys %sourceFiles) {
508 my $data = $sourceFiles{$filename};
509 my $traces = $data->traces();
510 if (scalar(@$traces) != $numInfoFiles) {
511 $status = 1;
512 my %names; # which info files DO contain this source file?
513 foreach my $d (@$traces) {
514 $names{$d->[1]} = 1;
515 }
516 print("Error: Source file '$filename' missing from:\n")
517 if $verbose;
518 my $idx = -1;
519 foreach my $f (@infoFileArray) {
520 ++$idx;
521 next if exists($names{$idx});
522 print("\t$f\n")
523 if $verbose;
524 if (exists($missing{$f})) {
525 push(@{$missing{$f}}, $filename);
526 } else {
527 $missing{$f} = [$filename];
528 }
529 }
530 }
531 }
532
533 if (%missing) {
534 print("Files missing from .info data:\n");
535
536 foreach my $f (sort keys %missing) {
537 print("\t$f:\n");
538 foreach my $src (@{$missing{$f}}) {
539 print("\t\t$src\n");
540 # and don't look at this file
541 delete $sourceFiles{$src} unless $dontDrop;
542 }
543 }
544 }
545
546 exit($status) unless defined($keep_going) || $status == 0;
547
548 # now go through the source files to check that line coverpoints are the same
549 # in all of them
550 foreach my $srcfile (sort keys %sourceFiles) {
551
552 my $srcData = $sourceFiles{$srcfile};
553 if (!$srcData->checkLineCoverageConsistency()) {
554 $status = 1;
555 }
556 }
557
558 # now sort the data and print it..
559 my @fileOrder =
560 $sortBySize ?
561 sort({ $sourceFiles{$b}->totalRegionSize()
562 <=> $sourceFiles{$a}->totalRegionSize() or
563 $a cmp $b } keys(%sourceFiles)) :
564 sort(keys %sourceFiles);
565
566 foreach my $srcFile (@fileOrder) {
567 my $printed = $sourceFiles{$srcFile}->print(\*STDOUT);
568 print("\n") if $printed;
569 }
570
571 exit($status);