blib/lib/Devel/Cover/DB.pm untested

filter: Statementstmt 0.0% 0 / 576 Branchbran 0.0% 0 / 186 Conditioncond 0.0% 0 / 156 MC/DCmcdc 0.0% 0 / 92 Subroutinesub 0.0% 0 / 61 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 1109 CC 94 SCAR 38.6
CC
94
Cov
0%
uncoverable_comments
156.0
cover
156.0
objectify_cover
90.0
CRAP 47.4
1 # Copyright 2001-2026, Paul Johnson (paul@pjcj.net)
2  
3 # This software is free. It is licensed under the same terms as Perl itself.
4  
5 # The latest version of this software should be available from my homepage:
6 # https://pjcj.net
7  
8 package Devel::Cover::DB;
9  
10 use strict;
11 use warnings;
12  
13 our $VERSION = '1.52'; # VERSION
14  
15 use Devel::Cover::Criterion;
16 use Devel::Cover::DB::File;
17 use Devel::Cover::DB::Structure;
18 use Devel::Cover::DB::IO;
19  
20 use Carp;
21 use File::Find;
22 use File::Path;
23  
24 use Devel::Cover::Dumper; # For debugging
25  
26 use constant CAN_TERM_SIZE => eval { require Term::Size };
27  
28 my $DB = "cover.14"; # Version of the database
29  
30 @Devel::Cover::DB::Criteria
31 = (qw( statement branch path condition subroutine pod time ));
32 @Devel::Cover::DB::Criteria_short
33 = (qw( stmt bran path cond sub pod time ));
34 $Devel::Cover::DB::Ignore_filenames = qr/ # Used by Devel::Cover
35 (?: [\/\\]lib[\/\\](?:Storable|POSIX).pm$ )
36 | # Moose
37 (?:
38 (?:
39 reader | writer | constructor | destructor | accessor |
40 predicate | clearer | native \s delegation \s method |
41 # Template Toolkit
42 Parser\.yp
43 )
44 \s .* \s
45 (?: \( defined \s at \s .* \s line \s \d+ \) | defined \s at )
46 )
47 | # Moose
48 (?: generated \s method \s \( unknown \s origin \) )
49 | # Mouse
50 (?: (?: rw-accessor | ro-accessor ) \s for )
51 | # Template Toolkit
52 (?: Parser\.yp )
53 | # perl generated
54 (?: \/loader\/0x )
55 /x;
56  
57 sub new {
58 my $class = shift;
59 my $self = {
60 criteria => \@Devel::Cover::DB::Criteria,
61 criteria_short => \@Devel::Cover::DB::Criteria_short,
62 runs => {},
63 collected => {},
64 uncoverable_file => [],
65 @_,
66 };
67  
68 $self->{all_criteria} = [ @{ $self->{criteria} }, "total" ];
69 $self->{all_criteria_short} = [ @{ $self->{criteria_short} }, "total" ];
70 $self->{base} ||= $self->{db};
71 bless $self, $class;
72  
73 if (defined $self->{db}) {
74 $self->validate_db;
75 my $file = "$self->{db}/$DB";
76 $self->read($file) if -e $file;
77 }
78  
79 $self
80 }
81  
82 sub criteria { @{ $_[0]->{criteria} } }
83 sub criteria_short { @{ $_[0]->{criteria_short} } }
84 sub all_criteria { @{ $_[0]->{all_criteria} } }
85 sub all_criteria_short { @{ $_[0]->{all_criteria_short} } }
86  
87 sub read {
88 my $self = shift;
89 my ($file) = @_;
90  
91 my $io = Devel::Cover::DB::IO->new;
92 my $db = eval { $io->read($file) };
93 if ($@ or !$db) {
94 warn $@;
95 } else {
96 $self->{runs} = $db->{runs};
97 }
98 $self
99 }
100  
101 sub write {
102 my $self = shift;
103 $self->{db} = shift if @_;
104  
105 croak "No db specified" unless length $self->{db};
106 unless (mkdir $self->{db}) {
107 croak "Can't mkdir $self->{db}: $!\n" unless -d $self->{db};
108 }
109 chmod 0777, $self->{db} if $self->{loose_perms};
110 $self->validate_db;
111  
112 my $db = { runs => $self->{runs} };
113 my $io = Devel::Cover::DB::IO->new;
114 $io->write($db, "$self->{db}/$DB");
115 $self->{structure}->write($self->{base}) if $self->{structure};
116 $self
117 }
118  
119 sub delete {
120 my $self = shift;
121  
122 my $db = "";
123 $db = $self->{db} if ref $self;
124 $db = shift if @_;
125 $self->{db} = $db if ref $self;
126 croak "No db specified" unless length $db;
127  
128 return $self unless -d $db;
129  
130 # TODO - just delete the directory?
131 opendir DIR, $db or die "Can't opendir $db: $!";
132 my @files = map "$db/$_", map /(.*)/ && $1, grep !/^\.\.?/, readdir DIR;
133 closedir DIR or die "Can't closedir $db: $!";
134 rmtree(\@files) if @files;
135  
136 $self
137 }
138  
139 sub clean {
140 my $self = shift;
141  
142 # remove all lock files
143 my $rm_lock = sub {
144 unlink if /\.lock$/
145 };
146 find($rm_lock, $self->{db});
147 }
148  
149 sub merge_runs {
150 my $self = shift;
151 my $db = $self->{db};
152 # print STDERR "merge_runs from $db/runs/*\n";
153 # system "ls -al $db/runs";
154 return $self unless length $db;
155 opendir DIR, "$db/runs" or return $self;
156 my @runs = map "$db/runs/$_", grep !/^\.\.?/, readdir DIR;
157 closedir DIR or die "Can't closedir $db/runs: $!";
158  
159 $self->{changed_files} = {};
160  
161 # The ordering is important here. The runs need to be merged in the order
162 # they were created. We're only at a granularity of one second, but that
163 # shouldn't be a problem unless a file is altered and the coverage run
164 # created in less than a second. I think we're OK for now.
165  
166 for my $run (sort @runs) {
167 # print STDERR "Devel::Cover: merging run $run <$self->{base}>\n";
168 my $r = Devel::Cover::DB->new(base => $self->{base}, db => $run);
169 $self->merge($r);
170 }
171  
172 $self->write($db) if @runs;
173 rmtree(\@runs);
174  
175 if (keys %{ $self->{changed_files} }) {
176 my $st = Devel::Cover::DB::Structure->new(base => $self->{base});
177 $st->read_all;
178 for my $file (sort keys %{ $self->{changed_files} }) {
179 # print STDERR "dealing with changed file <$file>\n";
180 $st->delete_file($file);
181 }
182 $st->write($self->{base});
183 }
184  
185 $self->clean;
186  
187 $self
188 }
189  
190 sub validate_db {
191 my $self = shift;
192  
193 # Check validity of the db. It is valid if the $DB file is there, or if it
194 # is not there but the db directory is empty, or if there is no db
195 # directory.
196 # die if the db is invalid.
197  
198 # just warn for now
199 print STDERR "Devel::Cover: $self->{db} is an invalid database\n"
200 unless $self->is_valid;
201  
202 $self
203 }
204  
205 sub exists {
206 my $self = shift;
207 -d $self->{db}
208 }
209  
210 sub is_valid {
211 my $self = shift;
212 return 1 if !-e $self->{db};
213 return 1 if -e "$self->{db}/$DB";
214 opendir my $fh, $self->{db} or return 0;
215 my $ignore = join "|", qw(
216 runs structure debuglog digests .AppleDouble
217 );
218 for my $file (readdir $fh) {
219 next if $file eq "." || $file eq "..";
220 next if $file =~ /(?:$ignore)|(?:\.lock)$/ && -e "$self->{db}/$file";
221 warn "found $file in $self->{db}";
222 return 0;
223 }
224 closedir $fh
225 }
226  
227 sub collected {
228 my $self = shift;
229 $self->cover;
230 sort keys %{ $self->{collected} }
231 }
232  
233 sub merge {
234 my ($self, $from) = @_;
235  
236 # print STDERR "Merging ", Dumper($self), "From ", Dumper($from);
237 # print STDERR "Merging ", $self, "From ", $from, "\n";
238 # print STDERR "$_\n" for keys %{$from->{runs}};
239  
240 for my $fname (sort keys %{ $from->{runs} }) {
241 # print STDERR "fname $fname\n";
242 my $frun = $from->{runs}{$fname};
243 for my $file (sort keys %{ $frun->{digests} }) {
244 # print STDERR "file $file\n";
245 my $digest = $frun->{digests}{$file};
246 for my $name (sort keys %{ $self->{runs} }) {
247 # print STDERR "name $name\n";
248 my $run = $self->{runs}{$name};
249 # print STDERR
250 # "digests for $file: $digest, $run->{digests}{$file}\n";
251 if ( $run->{digests}{$file}
252 && $digest
253 && $run->{digests}{$file} ne $digest)
254 {
255 # File has changed. Delete old coverage instead of merging.
256 print STDOUT "Devel::Cover: Deleting old coverage for ",
257 "changed file $file\n"
258 unless $Devel::Cover::Silent;
259 delete $run->{digests}{$file};
260 delete $run->{count}{$file};
261 delete $run->{vec}{$file};
262 $self->{changed_files}{$file}++;
263 }
264 }
265 }
266 }
267  
268 _merge_hash($self->{runs}, $from->{runs});
269 _merge_hash($self->{collected}, $from->{collected});
270  
271 # TODO - determine whether, when the database gets big, it's quicker to
272 # merge into what's already there. Instead of the previous two lines we
273 # would have these:
274  
275 # _merge_hash($from->{runs}, $self->{runs});
276 # _merge_hash($from->{collected}, $self->{collected});
277 # for (keys %$self) {
278 # $from->{$_} = $self->{$_} unless $_ eq "runs" || $_ eq "collected";
279 # }
280 # $_[0] = $from;
281 }
282  
283 sub _merge_hash {
284 my ($into, $from, $noadd) = @_;
285 return unless $from;
286 for my $fkey (keys %{$from}) {
287 # print STDERR "key [$fkey]\n";
288 my $fval = $from->{$fkey};
289  
290 if (defined $into->{$fkey} and UNIVERSAL::isa($into->{$fkey}, "ARRAY")) {
291 _merge_array($into->{$fkey}, $fval, $noadd);
292 } elsif (defined $fval && UNIVERSAL::isa($fval, "HASH")) {
293 if (defined $into->{$fkey} and UNIVERSAL::isa($into->{$fkey}, "HASH")) {
294 _merge_hash($into->{$fkey}, $fval, $noadd);
295 } else {
296 $into->{$fkey} = $fval;
297 }
298 } else {
299 # A scalar (or a blessed scalar). We know there is no into
300 # array, or we would have just merged with it.
301 $into->{$fkey} = $fval;
302 }
303 }
304 }
305  
306 sub _merge_array {
307 my ($into, $from, $noadd) = @_;
308 for my $i (@$into) {
309 my $f = shift @$from;
310 if (UNIVERSAL::isa($i, "ARRAY")
311 || !defined $i && UNIVERSAL::isa($f, "ARRAY"))
312 {
313 _merge_array($i, $f || [], $noadd);
314 } elsif (UNIVERSAL::isa($i, "HASH")
315 || !defined $i && UNIVERSAL::isa($f, "HASH"))
316 {
317 _merge_hash($i, $f || {}, $noadd);
318 } elsif (UNIVERSAL::isa($i, "SCALAR")
319 || !defined $i && UNIVERSAL::isa($f, "SCALAR"))
320 {
321 $$i += $$f;
322 } else {
323 if (defined $f) {
324 $i ||= 0;
325 if (!$noadd && $f =~ /^\d+$/ && $i =~ /^\d+$/) {
326 $i += $f;
327 } elsif ($i ne $f) {
328 warn "<$i> does not match <$f> - using later value";
329 $i = $f;
330 }
331 }
332 }
333 }
334 push @$into, @$from;
335 }
336  
337 sub summary {
338 my $self = shift;
339 my ($file, $criterion, $part) = @_;
340 my $f = $self->{summary}{$file};
341 return $f unless $f && defined $criterion;
342 my $c = $f->{$criterion};
343 $c && defined $part ? $c->{$part} : $c
344 }
345  
346 sub calculate_summary {
347 my $self = shift;
348 my %options = @_;
349  
350 return if exists $self->{summary} && !$options{force};
351 my $s = $self->{summary} = {};
352  
353 my @files = $self->cover->items;
354 if (my $files = delete $options{files}) {
355 my %required_files = map { ($_ => 1) } @$files;
356 @files = grep $required_files{$_}, @files;
357 }
358  
359 for my $file (@files) {
360 $self->cover->get($file)->calculate_summary($self, $file, \%options);
361 }
362  
363 # print STDERR Dumper $self;
364  
365 for my $file (@files) {
366 $self->cover->get($file)->calculate_percentage($self, $s->{$file});
367 }
368  
369 my $t = $self->{summary}{Total};
370 for my $criterion ($self->criteria) {
371 next unless exists $t->{$criterion};
372 my $c = "Devel::Cover::\u$criterion";
373 $c->calculate_percentage($self, $t->{$criterion});
374 }
375 Devel::Cover::Criterion->calculate_percentage($self, $t->{total});
376  
377 # print STDERR Dumper $self->{summary};
378 }
379  
380 sub trimmed_file {
381 my ($f, $len) = @_;
382 substr $f, 0, 3 - $len, "..." if length $f > $len;
383 $f
384 }
385  
386 sub print_summary {
387 my $self = shift;
388 my ($files, $criteria, $opts) = @_;
389  
390 my %crit = map(($_ => 1), $self->collected);
391 my %options = $criteria ? map(($_ => 1), grep $crit{$_}, @$criteria) : %crit;
392 $options{total} = 1 if keys %options;
393  
394 my $n = keys %options;
395  
396 my $oldfh = select STDOUT;
397  
398 $options{files} = $files if $files && @$files;
399 $self->calculate_summary(%options, %$opts);
400  
401 my $format = sub {
402 my ($part, $criterion) = @_;
403 $options{$criterion} && exists $part->{$criterion}
404 ? do {
405 my $x = sprintf "%5.2f", $part->{$criterion}{percentage};
406 chop $x;
407 $x
408 }
409 : "n/a"
410 };
411  
412 my $s = $self->{summary};
413 my @files = (grep($_ ne "Total", sort keys %$s), "Total");
414 my $max = 5;
415 for (@files) { $max = length if length > $max }
416 my $width
417 = !$ENV{DEVEL_COVER_TEST_SUITE}
418 && CAN_TERM_SIZE
419 && -t STDOUT ? (Term::Size::chars(\*STDOUT))[0] : 80;
420 my $fw = $width - $n * 7 - 3;
421  
422 $fw = $max if $max < $fw;
423  
424 no warnings "uninitialized";
425 my $fmt = "%-${fw}s" . " %6s" x $n . "\n";
426 printf $fmt, "-" x $fw, ("------") x $n;
427 printf $fmt, "File",
428 map { $self->{all_criteria_short}[$_] }
429 grep { $options{ $self->{all_criteria}[$_] } }
430 (0 .. $#{ $self->{all_criteria} });
431 printf $fmt, "-" x $fw, ("------") x $n;
432  
433 for my $file (@files) {
434 printf $fmt, trimmed_file($file, $fw),
435 map { $format->($s->{$file}, $_) }
436 grep { $options{$_} } @{ $self->{all_criteria} };
437  
438 }
439  
440 printf $fmt, "-" x $fw, ("------") x $n;
441 print "\n\n";
442  
443 select $oldfh;
444 }
445  
446 sub add_statement {
447 my $self = shift;
448 my ($cc, $sc, $fc, $uc) = @_;
449 my %line;
450 for my $i (0 .. $#$fc) {
451 # print STDERR "statement: $i\n";
452 my $l = $sc->[$i];
453 unless (defined $l) {
454 # print STDERR "sc ", scalar @$sc, ", fc ", scalar @$fc, "\n";
455 # print STDERR "sc ", Dumper($sc), "fc ", Dumper($fc);
456 warn "Devel::Cover: ignoring extra statement\n";
457 return;
458 }
459 my $n = $line{$l}++;
460 no warnings "uninitialized";
461 $cc->{$l}[$n][0] += $fc->[$i];
462 $cc->{$l}[$n][1] ||= $uc->{$l}[$n][0][1];
463 }
464 # print STDERR Dumper $uc;
465 # print STDERR "cc: ", Dumper $cc;
466 }
467  
468 sub add_time {
469 my $self = shift;
470 my ($cc, $sc, $fc) = @_;
471 my %line;
472 for my $i (0 .. $#$fc) {
473 my $l = $sc->[$i];
474 unless (defined $l) {
475 # print STDERR "sc ", scalar @$sc, ", fc ", scalar @$fc, "\n";
476 # print STDERR "sc ", Dumper($sc), "fc ", Dumper($fc);
477 warn "Devel::Cover: ignoring extra statement\n";
478 return;
479 }
480 my $n = $line{$l}++;
481 $cc->{$l}[$n] ||= do { my $c; \$c };
482 no warnings "uninitialized";
483 ${ $cc->{$l}[$n] } += $fc->[$i];
484 }
485 }
486  
487 sub add_branch {
488 my $self = shift;
489 my ($cc, $sc, $fc, $uc) = @_;
490 my %line;
491 for my $i (0 .. $#$fc) {
492 my $l = $sc->[$i][0];
493 unless (defined $l) {
494 warn "Devel::Cover: ignoring extra branch\n";
495 return;
496 }
497 my $n = $line{$l}++;
498 no warnings "uninitialized";
499 if (my $a = $cc->{$l}[$n]) {
500 $a->[0][0] += $fc->[$i][0];
501 $a->[0][1] += $fc->[$i][1];
502 $a->[0][2] += $fc->[$i][2] if exists $fc->[$i][2];
503 $a->[0][3] += $fc->[$i][3] if exists $fc->[$i][3];
504 } else {
505 $cc->{$l}[$n] = [ $fc->[$i], $sc->[$i][1] ];
506 }
507 $cc->{$l}[$n][2][ $_->[0] ] ||= $_->[1] for @{ $uc->{$l}[$n] };
508 }
509 }
510  
511 sub add_subroutine {
512 my $self = shift;
513 my ($cc, $sc, $fc, $uc) = @_;
514  
515 # print STDERR "add_subroutine():\n", Dumper $cc, $sc, $fc, $uc;
516  
517 # $cc = { line_number => [ [ count, sub_name, uncoverable ], [ ... ] ], .. }
518 # $sc = [ [ line_number, sub_name ], [ ... ] ]
519 # $fc = [ count, ... ]
520 # $uc = { line_number => [ [ ??? ], [ ... ] ], ... }
521 # length @$sc == length @$fc
522  
523 my %line;
524 for my $i (0 .. $#$fc) {
525 my $l = $sc->[$i][0];
526 unless (defined $l) {
527 # print STDERR "sc ", scalar @$sc, ", fc ", scalar @$fc, "\n";
528 # print STDERR "sc ", Dumper($sc), "fc ", Dumper($fc);
529 warn "Devel::Cover: ignoring extra subroutine\n";
530 return;
531 }
532  
533 my $n = $line{$l}++;
534 if (my $a = $cc->{$l}[$n]) {
535 no warnings "uninitialized";
536 $a->[0] += $fc->[$i];
537 } else {
538 $cc->{$l}[$n] = [ $fc->[$i], $sc->[$i][1] ];
539 }
540 $cc->{$l}[$n][2] ||= $uc->{$l}[$n][0][1];
541 }
542 }
543  
544 {
545 no warnings "once";
546 *add_condition = \&add_branch;
547 *add_pod = \&add_subroutine;
548 }
549  
550 sub uncoverable_files {
551 my $self = shift;
552 my $f = ".uncoverable";
553 (@{ $self->{uncoverable_file} }, $f, glob("~/$f"))
554 }
555  
556 sub uncoverable {
557 my $self = shift;
558  
559 my $u = {}; # holds all the uncoverable information
560  
561 # First populate $u with the uncoverable information directly from the
562 # .uncoverable files. Then loop through the information converting it to
563 # the format we will use later to manage the uncoverable code. The
564 # primary changes are converting MD5 digests of lines to line numbers, and
565 # converting filenames to MD5 digests of the files.
566  
567 for my $file ($self->uncoverable_files) {
568 open my $f, "<", $file or next;
569 print STDOUT "Reading uncoverable information from $file\n"
570 unless $Devel::Cover::Silent;
571 while (<$f>) {
572 chomp;
573 my ($file, $crit, $line, $count, $type, $class, $note) = split " ", $_, 7;
574 push @{ $u->{$file}{$crit}{$line}[$count] }, [ $type, $class, $note ];
575 }
576 }
577  
578 # print STDERR Dumper $u;
579 # Now change the format of the uncoverable information
580  
581 for my $file (sort keys %$u) {
582 # print STDERR "Reading $file\n";
583 open my $fh, "<", $file or do {
584 warn "Devel::Cover: Can't open $file: $!\n";
585 next;
586 };
587 my $df = Digest::MD5->new; # MD5 digest of the file
588 my %dl; # maps MD5 digests of lines to line numbers
589 my $ln = 0; # line number
590 while (<$fh>) {
591 # print STDERR "read [$.][$_]\n";
592 $dl{ Digest::MD5->new->add($_)->hexdigest } = ++$ln;
593 $df->add($_);
594 }
595 close $fh;
596 my $f = $u->{$file};
597 # print STDERR Dumper $f;
598 for my $crit (keys %$f) {
599 my $c = $f->{$crit};
600 for my $line (keys %$c) {
601 if (exists $dl{$line}) {
602 # print STDERR
603 # "Found uncoverable $file:$crit:$line -> $dl{$line}\n";
604  
605 # Change key from the MD5 digest to the actual line number
606 $c->{ $dl{$line} } = delete $c->{$line};
607 } else {
608 warn "Devel::Cover: Can't find line for uncovered data: "
609 . "$file $crit $line\n";
610 delete $c->{$line};
611 }
612 }
613 }
614 # Change the key from the filename to the MD5 digest of the file
615 $u->{ $df->hexdigest } = delete $u->{$file};
616 }
617  
618 print STDERR Dumper $u;
619 $u
620 }
621  
622 sub add_uncoverable {
623 my $self = shift;
624 my ($adds) = @_;
625 for my $add (@$adds) {
626 my ($file, $crit, $line, $count, $type, $class, $note) = split " ", $_, 7;
627 my ($uncoverable_file) = $self->uncoverable_files;
628  
629 open my $f, "<", $file or do {
630 warn "Devel::Cover: Can't open $file: $!";
631 next;
632 };
633 while (<$f>) {
634 last if $. == $line;
635 }
636 if (defined) {
637 open my $u, ">>", $uncoverable_file
638 or die "Devel::Cover: Can't open $uncoverable_file: $!\n";
639 my $dl = Digest::MD5->new->add($_)->hexdigest;
640 print $u "$file $crit $dl $count $type $class $note\n";
641 } else {
642 warn "Devel::Cover: Can't find line $line in $file. ",
643 "Last line is $.\n";
644 }
645 close $f or die "Devel::Cover: Can't close $file: $!\n";
646 }
647 }
648  
649 sub delete_uncoverable {
650 my $self = shift;
651 }
652  
653 sub clean_uncoverable {
654 my $self = shift;
655 }
656  
657 sub uncoverable_comments {
658  
659 my $self = shift;
660 my ($uncoverable, $file, $digest) = @_;
661  
662 my $cr = join "|", @{ $self->{all_criteria} };
663 my $uc = qr/(.*)# uncoverable ($cr)(.*)/; # regex for uncoverable comments
664 my %types = (
665 branch => { true => 0, false => 1 },
666 condition => { left => 0, right => 1, false => 2 },
667 );
668  
669 # Look for uncoverable comments
670 open my $fh, "<", $file or do {
671 # The warning should have already been given ...
672 # warn "Devel::Cover: Warning: can't open $file: $!\n";
673 return;
674 };
675 my @waiting;
676 while (<$fh>) {
677 chomp;
678 # print STDERR "read [$.][$_]\n";
679 next unless /$uc/ || @waiting;
680 if ($2) {
681 my ($code, $criterion, $info) = ($1, $2, $3);
682 my ($count, $class, $note, $type) = (1, "default", "");
683  
684 if ($criterion eq "branch" || $criterion eq "condition") {
685 if ($info =~ /^\s*(\w+)(?:\s|$)/) {
686 my $t = $1;
687 $type = $types{$criterion}{$t};
688 unless (defined $type) {
689 warn "Unknown type $t found parsing "
690 . "uncoverable $criterion at $file:$.\n";
691 $type = 999; # partly magic number
692 }
693 }
694 }
695 # e.g.: count:1 | count:2,5 | count:1,4..7
696 my $c = qr/\d+(?:\.\.\d+)?/;
697 $count = $1 if $info =~ /count:($c(?:,$c)*)/;
698 my @counts = map { m/^(\d+)\.\.(\d+)$/ ? ($1 .. $2) : $_ } split m/,/,
699 $count;
700 $class = $1 if $info =~ /class:(\w+)/;
701 $note = $1 if $info =~ /note:(.+)/;
702  
703 for my $c (@counts) {
704 # no warnings "uninitialized";
705 # warn "pushing $criterion, $c - 1, $type, $class, $note";
706 push @waiting, [ $criterion, $c - 1, $type, $class, $note ];
707 }
708  
709 next unless $code =~ /\S/;
710 }
711  
712 # found what we are waiting for
713 while (my $w = shift @waiting) {
714 my ($criterion, $count, $type, $class, $note) = @$w;
715 push @{ $uncoverable->{$digest}{$criterion}{$.}[$count] },
716 [ $type, $class, $note ];
717 }
718 }
719 close $fh;
720  
721 warn scalar @waiting,
722 " unmatched uncoverable comments not found at end of $file\n"
723 if @waiting;
724  
725 # TODO - read in and merge $self->uncoverable;
726 # print Dumper $uncoverable;
727 }
728  
729 sub objectify_cover {
730 my $self = shift;
731  
732 unless (UNIVERSAL::isa($self->{cover}, "Devel::Cover::DB::Cover")) {
733 bless $self->{cover}, "Devel::Cover::DB::Cover";
734 for my $file (values %{ $self->{cover} }) {
735 bless $file, "Devel::Cover::DB::File";
736 while (my ($crit, $criterion) = each %$file) {
737 next if $crit eq "meta"; # ignore meta data
738 my $class = "Devel::Cover::" . ucfirst lc $crit;
739 bless $criterion, "Devel::Cover::DB::Criterion";
740 for my $line (values %$criterion) {
741 for my $o (@$line) {
742 die "<$crit:$o>" unless ref $o;
743 bless $o, $class;
744 bless $o, $class . "_" . $o->type if $o->can("type");
745 # print "blessed $crit, $o\n";
746 }
747 }
748 }
749 }
750 for my $r (keys %{ $self->{runs} }) {
751 if (defined $self->{runs}{$r}) {
752 bless $self->{runs}{$r}, "Devel::Cover::DB::Run";
753 } else {
754 delete $self->{runs}{$r}; # DEVEL_COVER_SELF
755 }
756 }
757 }
758  
759 unless (exists &Devel::Cover::DB::Base::items) {
760 *Devel::Cover::DB::Base::items = sub {
761 my $self = shift;
762 keys %$self
763 };
764  
765 {
766 no warnings "once";
767 *Devel::Cover::DB::Base::values = sub {
768 my $self = shift;
769 values %$self
770 };
771  
772 *Devel::Cover::DB::Base::get = sub {
773 my $self = shift;
774 my ($get) = @_;
775 $self->{$get}
776 };
777 }
778  
779 my $classes = {
780 Cover => [qw( files file )],
781 File => [qw( criteria criterion )],
782 Criterion => [qw( locations location )],
783 Location => [qw( data datum )],
784 };
785 my $base = "Devel::Cover::DB::Base";
786 while (my ($class, $functions) = each %$classes) {
787 my $c = "Devel::Cover::DB::$class";
788 no strict "refs";
789 @{"${c}::ISA"} = $base;
790 *{"${c}::$functions->[0]"} = \&{"${base}::values"};
791 *{"${c}::$functions->[1]"} = \&{"${base}::get"};
792 }
793  
794 {
795 no warnings "once";
796 *Devel::Cover::DB::File::DESTROY = sub { };
797 unless (exists &Devel::Cover::DB::File::AUTOLOAD) {
798 *Devel::Cover::DB::File::AUTOLOAD = sub {
799 # Work around a change in bleadperl from 12251 to 14899
800 my $func = $Devel::Cover::DB::AUTOLOAD || $::AUTOLOAD;
801  
802 # print STDERR "autoloading <$func>\n";
803 (my $f = $func) =~ s/.*:://;
804 carp "Undefined subroutine $f called"
805 unless grep { $_ eq $f } @{ $self->{all_criteria} },
806 @{ $self->{all_criteria_short} };
807 no strict "refs";
808 *$func = sub { shift->{$f} };
809 goto &$func
810 };
811 }
812 }
813 }
814 }
815  
816 sub cover {
817 my $self = shift;
818  
819 return $self->{cover} if $self->{cover_valid};
820  
821 my %digests; # mapping of digests to canonical filenames
822 my %files; # processed files
823 my $cover = $self->{cover} = {};
824 my $uncoverable = {};
825 my $st = $self->{_structure}
826 // Devel::Cover::DB::Structure->new(base => $self->{base})->read_all;
827  
828 # Sometimes the start value is undefined. It's not yet clear why, but it
829 # probably has something to do with the code under test forking. We'll
830 # just try to cope with that here.
831 my @runs = sort {
832 ($self->{runs}{$b}{start} || 0) <=> ($self->{runs}{$a}{start} || 0)
833 || $b cmp $a
834 } keys %{ $self->{runs} };
835 # print STDERR "runs: ", Dumper \@runs
836  
837 my %warned;
838 for my $run (@runs) {
839 last unless $st;
840  
841 my $r = $self->{runs}{$run};
842 next unless $r->{collected}; # DEVEL_COVER_SELF
843 @{ $self->{collected} }{ @{ $r->{collected} } } = ();
844 $st->add_criteria(@{ $r->{collected} });
845 my $count = $r->{count};
846 # print STDERR "run $run, count: ", Dumper $count;
847 while (my ($file, $f) = each %$count) {
848 my $digest = $r->{digests}{$file};
849 unless ($digest) {
850 print STDERR "Devel::Cover: Can't find digest for $file\n"
851 unless $Devel::Cover::Silent
852 || $file =~ $Devel::Cover::DB::Ignore_filenames
853 || ($Devel::Cover::Self_cover && $file =~ q|/Devel/Cover[./]|);
854 next;
855 }
856 # print STDERR "File: $file\n";
857 print STDERR "Devel::Cover: merging data for $file ",
858 "into $digests{$digest}\n"
859 if !$files{$file}++ && $digests{$digest};
860  
861 $self->uncoverable_comments($uncoverable, $file, $digest)
862 unless $digests{$digest};
863  
864 # Set up data structure to hold coverage being filled in
865 my $ff = $file;
866 if ($self->{prefer_lib}) {
867 $ff =~ s|^blib/||;
868 $ff = $file unless -e $ff;
869 }
870 my $cf = $cover->{ $digests{$digest} ||= $ff } ||= {};
871  
872 # print STDERR "st ", Dumper($st),
873 # "f ", Dumper($f),
874 # "uc ", Dumper($uncoverable->{$digest});
875 while (my ($criterion, $fc) = each %$f) {
876 my $get = "get_$criterion";
877 my $sc = $st->$get($digest);
878 # print STDERR "$criterion: ", Dumper $sc, $fc;
879 unless ($sc) {
880 print STDERR "Devel::Cover: Warning: can't locate ",
881 "structure for $criterion in $file\n"
882 unless $warned{$file}{$criterion}++;
883 next;
884 }
885 my $cc = $cf->{$criterion} ||= {};
886 my $add = "add_$criterion";
887 # print STDERR "$add():\n", Dumper $cc, $sc, $fc;
888 $self->$add($cc, $sc, $fc, $uncoverable->{$digest}{$criterion});
889 # print STDERR "--> $add():\n", Dumper $cc;
890 # $cc - coverage being filled in
891 # $sc - structure information
892 # $fc - coverage from this file
893 # $uc - uncoverable information
894 }
895 }
896 # print STDERR "Cover: ", Dumper $cover;
897 }
898  
899 $self->objectify_cover;
900 $self->{cover_valid} = 1;
901 $self->{cover}
902 }
903  
904 sub run_keys {
905 my $self = shift;
906 $self->cover unless $self->{cover_valid};
907 sort { $self->{runs}{$b}{start} <=> $self->{runs}{$a}{start} }
908 keys %{ $self->{runs} };
909 }
910  
911 sub runs {
912 my $self = shift;
913 $self->cover unless $self->{cover_valid};
914 @{ $self->{runs} }{ $self->run_keys }
915 }
916  
917 sub set_structure {
918 my $self = shift;
919 my ($structure) = @_;
920 $self->{_structure} = $structure;
921 }
922  
923 package Devel::Cover::DB::Run;
924  
925 our $AUTOLOAD;
926  
927 sub DESTROY { }
928  
929 sub AUTOLOAD {
930 my $func = $AUTOLOAD;
931 # print STDERR "autoloading <$func>\n";
932 (my $f = $func) =~ s/.*:://;
933 no strict "refs";
934 *$func = sub { shift->{$f} };
935 goto &$func
936 }
937  
938 1
939  
940 __END__