| 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; | ||
| 9 | |||
| 10 | use v5.20.0; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | our $VERSION; | ||
| 15 | |||
| 16 | BEGIN { | ||
| 17 | our $VERSION = '1.52'; # VERSION | ||
| 18 | } | ||
| 19 | |||
| 20 | use DynaLoader (); | ||
| 21 | our @ISA = "DynaLoader"; | ||
| 22 | |||
| 23 | use Devel::Cover::DB (); | ||
| 24 | use Devel::Cover::DB::Digests (); | ||
| 25 | use Devel::Cover::Inc (); | ||
| 26 | |||
| 27 | BEGIN { $VERSION //= $Devel::Cover::Inc::VERSION } | ||
| 28 | |||
| 29 | use B qw( main_cv main_root ppname walksymtable ); | ||
| 30 | use B::Deparse (); | ||
| 31 | |||
| 32 | use Config qw( %Config ); | ||
| 33 | use Cwd qw( abs_path getcwd ); | ||
| 34 | use File::Spec (); | ||
| 35 | |||
| 36 | use Devel::Cover::Util qw( remove_contained_paths ); | ||
| 37 | |||
| 38 | BEGIN { | ||
| 39 | # Use Pod::Coverage if it is available | ||
| 40 | eval "use Pod::Coverage 0.06"; | ||
| 41 | # If there is any error other than a failure to locate, report it | ||
| 42 | die $@ if $@ && $@ !~ m/Can't locate Pod\/Coverage.+pm in \@INC/; | ||
| 43 | |||
| 44 | # We'll prefer Pod::Coverage::CountParents | ||
| 45 | eval "use Pod::Coverage::CountParents"; | ||
| 46 | die $@ if $@ && $@ !~ m/Can't locate Pod\/Coverage.+pm in \@INC/; | ||
| 47 | } | ||
| 48 | |||
| 49 | # $SIG{__DIE__} = \&Carp::confess; | ||
| 50 | # sub Pod::Coverage::TRACE_ALL () { 1 } | ||
| 51 | |||
| 52 | my $Initialised; # import() has been called | ||
| 53 | |||
| 54 | my $Dir; # Directory in which coverage will be | ||
| 55 | # collected | ||
| 56 | my $DB = "cover_db"; # DB name | ||
| 57 | my $Merge = 1; # Merge databases | ||
| 58 | my $Summary = 1; # Output coverage summary | ||
| 59 | my $Subs_only = 0; # Coverage only for sub bodies | ||
| 60 | my $Self_cover_run = 0; # Covering Devel::Cover now | ||
| 61 | my $Loose_perms = 0; # Use loose permissions in the cover DB | ||
| 62 | |||
| 63 | my @Ignore; # Packages to ignore | ||
| 64 | my @Inc; # Original @INC to ignore | ||
| 65 | my @Select; # Packages to select | ||
| 66 | my @Ignore_re; # Packages to ignore | ||
| 67 | my @Inc_re; # Original @INC to ignore | ||
| 68 | my @Select_re; # Packages to select | ||
| 69 | |||
| 70 | my $Pod | ||
| 71 | = $INC{"Pod/Coverage/CountParents.pm"} ? "Pod::Coverage::CountParents" | ||
| 72 | : $INC{"Pod/Coverage.pm"} ? "Pod::Coverage" | ||
| 73 | : ""; # Type of pod coverage available | ||
| 74 | my %Pod; # Pod coverage data | ||
| 75 | |||
| 76 | my @Cvs; # All the Cvs we want to cover | ||
| 77 | my %Cvs; # All the Cvs we want to cover | ||
| 78 | my @Subs; # All the subs we want to cover | ||
| 79 | my $Cv; ## no critic (ProhibitUnusedVariables) # Cv we are looking in | ||
| 80 | my $Sub_name; # Name of the sub we are looking in | ||
| 81 | my $Sub_count; # Count for multiple subs on same line | ||
| 82 | |||
| 83 | my $Coverage; # Raw coverage data | ||
| 84 | my $Structure; # Structure of the files | ||
| 85 | my $Digests; # Digests of the files | ||
| 86 | |||
| 87 | my %Criteria; # Names of coverage criteria | ||
| 88 | my %Coverage; # Coverage criteria to collect | ||
| 89 | my %Coverage_options; # Options for overage criteria | ||
| 90 | |||
| 91 | my %Run; # Data collected from the run | ||
| 92 | |||
| 93 | my $Const_right = qr/^(?:const|s?refgen|gelem|die|undef|bless|anon(?:list|hash)| | ||
| 94 | emptyavhv|scalar|return|last|next|redo|goto)$/x; | ||
| 95 | |||
| 96 | # constant ops | ||
| 97 | |||
| 98 | our $File; # Last filename we saw. (localised) | ||
| 99 | our $Line; # Last line number we saw. (localised) | ||
| 100 | our $Collect; # Whether or not we are collecting | ||
| 101 | # coverage data. We make two passes | ||
| 102 | # over conditions. (localised) | ||
| 103 | our %Files; # Whether we are interested in files | ||
| 104 | # Used in runops function | ||
| 105 | our $Replace_ops; # Whether we are replacing ops | ||
| 106 | our $Silent; # Output nothing. Can be used anywhere | ||
| 107 | our $Ignore_covered_err; # Don't flag an error when uncoverable | ||
| 108 | # code is covered. | ||
| 109 | our $Self_cover; # Coverage of Devel::Cover | ||
| 110 | |||
| 111 | BEGIN { | ||
| 112 | ($File, $Line, $Collect) = ("", 0, 1); | ||
| 113 | $Silent = ($ENV{HARNESS_PERL_SWITCHES} || "") =~ /Devel::Cover/ | ||
| 114 | || ($ENV{PERL5OPT} || "") =~ /Devel::Cover/; | ||
| 115 | *OUT = $ENV{DEVEL_COVER_DEBUG} ? *STDERR : *STDOUT; | ||
| 116 | |||
| 117 | if ($^X =~ /(apache2|httpd)$/) { | ||
| 118 | # mod_perl < 2.0.8 | ||
| 119 | @Inc = @Devel::Cover::Inc::Inc; | ||
| 120 | } else { | ||
| 121 | # Can't get @INC via eval `` in taint mode, revert to default value | ||
| 122 | if (${^TAINT}) { | ||
| 123 | @Inc = @Devel::Cover::Inc::Inc; | ||
| 124 | } else { | ||
| 125 | eval { | ||
| 126 | local %ENV = %ENV; | ||
| 127 | # Clear *PERL* variables, but keep PERL5?LIB for local::lib | ||
| 128 | # environments | ||
| 129 | /perl/i and not /^PERL5?LIB$/ and delete $ENV{$_} for keys %ENV; | ||
| 130 | my $cmd = "$^X -MData::Dumper -e " . '"print Dumper \@INC"'; | ||
| 131 | my $VAR1; | ||
| 132 | # print STDERR "Running [$cmd]\n"; | ||
| 133 | eval `$cmd`; | ||
| 134 | @Inc = @$VAR1; | ||
| 135 | }; | ||
| 136 | if ($@) { | ||
| 137 | print STDERR __PACKAGE__, ": Error getting \@INC: $@\n", | ||
| 138 | "Reverting to default value for Inc.\n"; | ||
| 139 | @Inc = @Devel::Cover::Inc::Inc; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | @Inc = map { -d() ? ($_ eq "." ? $_ : Cwd::abs_path($_)) : () } @Inc; | ||
| 145 | |||
| 146 | @Inc = remove_contained_paths(getcwd, @Inc); | ||
| 147 | |||
| 148 | @Ignore = ("/Devel/Cover[./]") unless $Self_cover = $ENV{DEVEL_COVER_SELF}; | ||
| 149 | # $^P = 0x004 | 0x010 | 0x100 | 0x200; | ||
| 150 | # $^P = 0x004 | 0x100 | 0x200; | ||
| 151 | $^P |= 0x004 | 0x100; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub version { $VERSION } | ||
| 155 | |||
| 156 | if (0 && $Config{useithreads}) { | ||
| 157 | eval "use threads"; | ||
| 158 | |||
| 159 | no warnings "redefine"; | ||
| 160 | |||
| 161 | my $original_join; | ||
| 162 | BEGIN { $original_join = \&threads::join } | ||
| 163 | # print STDERR "original_join: $original_join\n"; | ||
| 164 | |||
| 165 | # $original_join = sub { print STDERR "j\n" }; | ||
| 166 | |||
| 167 | # sub threads::join | ||
| 168 | *threads::join = sub { | ||
| 169 | # print STDERR "threads::join- ", \&threads::join, "\n"; | ||
| 170 | # print STDERR "original_join- $original_join\n"; | ||
| 171 | my $self = shift; | ||
| 172 | print STDERR "(joining thread ", $self->tid, ")\n"; | ||
| 173 | my @ret = $original_join->($self, @_); | ||
| 174 | print STDERR "(returning <@ret>)\n"; | ||
| 175 | @ret | ||
| 176 | }; | ||
| 177 | |||
| 178 | my $original_destroy; | ||
| 179 | BEGIN { $original_destroy = \&threads::DESTROY } | ||
| 180 | |||
| 181 | *threads::DESTROY = sub { | ||
| 182 | my $self = shift; | ||
| 183 | print STDERR "(destroying thread ", $self->tid, ")\n"; | ||
| 184 | $original_destroy->($self, @_); | ||
| 185 | }; | ||
| 186 | |||
| 187 | # print STDERR "threads::join: ", \&threads::join, "\n"; | ||
| 188 | |||
| 189 | my $new = \&threads::new; | ||
| 190 | *threads::new = *threads::create = sub { | ||
| 191 | my $class = shift; | ||
| 192 | my $sub = shift; | ||
| 193 | my $wantarray = wantarray; | ||
| 194 | |||
| 195 | $new->( | ||
| 196 | $class, | ||
| 197 | sub { | ||
| 198 | print STDERR "Starting thread\n"; | ||
| 199 | set_coverage(keys %Coverage); | ||
| 200 | my $ret = [ $sub->(@_) ]; | ||
| 201 | print STDERR "Ending thread\n"; | ||
| 202 | report() if $Initialised; | ||
| 203 | print STDERR "Ended thread\n"; | ||
| 204 | $wantarray ? @{$ret} : $ret->[0]; | ||
| 205 | }, | ||
| 206 | @_, | ||
| 207 | ); | ||
| 208 | }; | ||
| 209 | } | ||
| 210 | |||
| 211 | { | ||
| 212 | |||
| 213 | sub check { | ||
| 214 | return unless $Initialised; | ||
| 215 | |||
| 216 | check_files(); | ||
| 217 | |||
| 218 | set_coverage(keys %Coverage); | ||
| 219 | my @coverage = get_coverage(); | ||
| 220 | %Coverage = map { $_ => 1 } @coverage; | ||
| 221 | |||
| 222 | delete $Coverage{path}; # not done yet | ||
| 223 | my $nopod = ""; | ||
| 224 | if (!$Pod && exists $Coverage{pod}) { | ||
| 225 | delete $Coverage{pod}; # Pod::Coverage unavailable | ||
| 226 | $nopod = <<EOM; | ||
| 227 | Pod coverage is unavailable. Please install Pod::Coverage from CPAN. | ||
| 228 | EOM | ||
| 229 | } | ||
| 230 | |||
| 231 | set_coverage(keys %Coverage); | ||
| 232 | @coverage = get_coverage(); | ||
| 233 | my $last = pop @coverage || ""; | ||
| 234 | |||
| 235 | print OUT __PACKAGE__, " $VERSION: Collecting coverage data for ", | ||
| 236 | join(", ", @coverage), @coverage ? " and " : "", "$last.\n", $nopod, | ||
| 237 | $Subs_only ? " Collecting for subroutines only.\n" : "", | ||
| 238 | $ENV{MOD_PERL} ? " Collecting under $ENV{MOD_PERL}\n" : "", | ||
| 239 | "Selecting packages matching:", join("\n ", "", @Select), "\n", | ||
| 240 | "Ignoring packages matching:", join("\n ", "", @Ignore), "\n", | ||
| 241 | "Ignoring packages in:", join("\n ", "", @Inc), "\n" | ||
| 242 | unless $Silent; | ||
| 243 | |||
| 244 | populate_run(); | ||
| 245 | } | ||
| 246 | |||
| 247 | no warnings "void"; # avoid "Too late to run CHECK block" warning | ||
| 248 | CHECK { check } | ||
| 249 | } | ||
| 250 | |||
| 251 | { | ||
| 252 | my $run_end = 0; | ||
| 253 | |||
| 254 | sub first_end { | ||
| 255 | # print STDERR "**** END 1 - $run_end\n"; | ||
| 256 | set_last_end() unless $run_end++ | ||
| 257 | } | ||
| 258 | |||
| 259 | my $run_init = 0; | ||
| 260 | |||
| 261 | sub first_init { | ||
| 262 | # print STDERR "**** INIT 1 - $run_init\n"; | ||
| 263 | collect_inits() unless $run_init++ | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | sub last_end { | ||
| 268 | # print STDERR "**** END 2 - [$Initialised]\n"; | ||
| 269 | report() if $Initialised; | ||
| 270 | # print STDERR "**** END 2 - ended\n"; | ||
| 271 | } | ||
| 272 | |||
| 273 | { | ||
| 274 | no warnings "void"; # avoid "Too late to run ... block" warning | ||
| 275 | INIT { } # dummy sub to make sure PL_initav is set up and populated | ||
| 276 | END { } # dummy sub to make sure PL_endav is set up and populated | ||
| 277 | CHECK { set_first_init_and_end() } # we really want to be first | ||
| 278 | } | ||
| 279 | |||
| 280 | sub CLONE { | ||
| 281 | print STDERR <<EOM; | ||
| 282 | |||
| 283 | Unfortunately, Devel::Cover does not yet work with threads. I have done | ||
| 284 | some work in this area, but there is still more to be done. | ||
| 285 | |||
| 286 | EOM | ||
| 287 | require POSIX; | ||
| 288 | POSIX::_exit(1); | ||
| 289 | } | ||
| 290 | |||
| 291 | $Replace_ops = !$Self_cover; | ||
| 292 | |||
| 293 | sub import { | ||
| 294 | return if $Initialised; | ||
| 295 | |||
| 296 | my $class = shift; | ||
| 297 | |||
| 298 | # Die tainting | ||
| 299 | # Anyone using this module can do worse things than messing with tainting | ||
| 300 | my $options = ($ENV{DEVEL_COVER_OPTIONS} || "") =~ /(.*)/ ? $1 : ""; | ||
| 301 | my @o = (@_, split /,/, $options); | ||
| 302 | defined or $_ = "" for @o; | ||
| 303 | # print STDERR __PACKAGE__, ": Parsing options from [@o]\n"; | ||
| 304 | |||
| 305 | my $blib = -d "blib"; | ||
| 306 | @Inc = () if "@o" =~ /-inc /; | ||
| 307 | @Ignore = () if "@o" =~ /-ignore /; | ||
| 308 | @Select = () if "@o" =~ /-select /; | ||
| 309 | while (@o) { | ||
| 310 | local $_ = shift @o; | ||
| 311 | /^-silent/ && do { $Silent = shift @o; next }; | ||
| 312 | /^-dir/ && do { $Dir = shift @o; next }; | ||
| 313 | /^-db/ && do { $DB = shift @o; next }; | ||
| 314 | /^-loose_perms/ && do { $Loose_perms = shift @o; next }; | ||
| 315 | /^-merge/ && do { $Merge = shift @o; next }; | ||
| 316 | /^-summary/ && do { $Summary = shift @o; next }; | ||
| 317 | /^-blib/ && do { $blib = shift @o; next }; | ||
| 318 | /^-subs_only/ && do { $Subs_only = shift @o; next }; | ||
| 319 | /^-replace_ops/ && do { $Replace_ops = shift @o; next }; | ||
| 320 | /^-coverage/ | ||
| 321 | && do { $Coverage{ +shift @o } = 1 while @o && $o[0] !~ /^[-+]/; next }; | ||
| 322 | /^[-+]ignore/ | ||
| 323 | && do { push @Ignore, shift @o while @o && $o[0] !~ /^[-+]/; next }; | ||
| 324 | /^[-+]inc/ && do { push @Inc, shift @o while @o && $o[0] !~ /^[-+]/; next }; | ||
| 325 | /^[-+]select/ | ||
| 326 | && do { push @Select, shift @o while @o && $o[0] !~ /^[-+]/; next }; | ||
| 327 | warn __PACKAGE__ . ": Unknown option $_ ignored\n"; | ||
| 328 | } | ||
| 329 | |||
| 330 | if ($blib) { | ||
| 331 | eval "use blib"; | ||
| 332 | for (@INC) { $_ = $1 if ref $_ ne 'CODE' && /(.*)/ } # Die tainting | ||
| 333 | push @Ignore, "^t/", '\\.t$', '^test\\.pl$'; | ||
| 334 | } | ||
| 335 | |||
| 336 | my $ci = $^O eq "MSWin32"; | ||
| 337 | @Select_re = map qr/$_/, @Select; | ||
| 338 | @Ignore_re = map qr/$_/, @Ignore; | ||
| 339 | @Inc_re = map $ci ? qr/^\Q$_\//i : qr/^\Q$_\//, @Inc; | ||
| 340 | |||
| 341 | bootstrap Devel::Cover $VERSION; | ||
| 342 | |||
| 343 | if (defined $Dir) { | ||
| 344 | $Dir = $1 if $Dir =~ /(.*)/; # Die tainting | ||
| 345 | } else { | ||
| 346 | $Dir = $1 if Cwd::getcwd() =~ /(.*)/; | ||
| 347 | } | ||
| 348 | |||
| 349 | $DB = File::Spec->rel2abs($DB, $Dir); | ||
| 350 | unless (mkdir $DB) { | ||
| 351 | my $err = $!; | ||
| 352 | die "Can't mkdir $DB as EUID $>: $err" unless -d $DB; | ||
| 353 | } | ||
| 354 | chmod 0777, $DB if $Loose_perms; | ||
| 355 | $DB = $1 if abs_path($DB) =~ /(.*)/; ## no critic (CaptureWithoutTest) | ||
| 356 | Devel::Cover::DB->delete($DB) unless $Merge; | ||
| 357 | |||
| 358 | %Files = (); # start gathering file information from scratch | ||
| 359 | |||
| 360 | for my $c (Devel::Cover::DB->new->criteria) { | ||
| 361 | my $func = "coverage_$c"; | ||
| 362 | no strict "refs"; | ||
| 363 | $Criteria{$c} = $func->(); | ||
| 364 | } | ||
| 365 | |||
| 366 | for (keys %Coverage) { | ||
| 367 | my @c = split /-/; | ||
| 368 | if (@c > 1) { | ||
| 369 | $Coverage{ shift @c } = \@c; | ||
| 370 | delete $Coverage{$_}; | ||
| 371 | } | ||
| 372 | delete $Coverage{$_} unless length; | ||
| 373 | } | ||
| 374 | %Coverage = (all => 1) unless keys %Coverage; | ||
| 375 | # print STDERR "Coverage: ", Dumper \%Coverage; | ||
| 376 | %Coverage_options = %Coverage; | ||
| 377 | |||
| 378 | $Initialised = 1; | ||
| 379 | |||
| 380 | if ($ENV{MOD_PERL}) { | ||
| 381 | eval "BEGIN {}"; | ||
| 382 | check(); | ||
| 383 | set_first_init_and_end(); | ||
| 384 | } | ||
| 385 | } | ||
| 386 | |||
| 387 | sub populate_run { | ||
| 388 | my $self = shift; | ||
| 389 | |||
| 390 | $Run{OS} = $^O; | ||
| 391 | $Run{perl} = sprintf "%vd", $^V; | ||
| 392 | $Run{dir} = $Dir; | ||
| 393 | $Run{run} = $0; | ||
| 394 | $Run{name} = $Dir; | ||
| 395 | $Run{version} = "unknown"; | ||
| 396 | |||
| 397 | my $mymeta = "$Dir/MYMETA.json"; | ||
| 398 | if (-e $mymeta) { | ||
| 399 | eval { | ||
| 400 | require CPAN::Meta; | ||
| 401 | my $json = CPAN::Meta->load_file($mymeta)->as_struct; | ||
| 402 | $Run{$_} = $json->{$_} for qw( name version abstract ); | ||
| 403 | } | ||
| 404 | } elsif ($Dir =~ m|.*/([^/]+)$|) { | ||
| 405 | my $filename = $1; | ||
| 406 | eval { | ||
| 407 | require CPAN::DistnameInfo; | ||
| 408 | my $dinfo = CPAN::DistnameInfo->new($filename); | ||
| 409 | $Run{name} = $dinfo->dist; | ||
| 410 | $Run{version} = $dinfo->version; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | $Run{start} = get_elapsed() / 1e6; | ||
| 415 | } | ||
| 416 | |||
| 417 | sub cover_names_to_val { | ||
| 418 | my $val = 0; | ||
| 419 | for my $c (@_) { | ||
| 420 | if (exists $Criteria{$c}) { | ||
| 421 | $val |= $Criteria{$c}; | ||
| 422 | } elsif ($c eq "all" || $c eq "none") { | ||
| 423 | my $func = "coverage_$c"; | ||
| 424 | no strict "refs"; | ||
| 425 | $val |= $func->(); | ||
| 426 | } else { | ||
| 427 | warn __PACKAGE__ . qq(: Unknown coverage criterion "$c" ignored.\n); | ||
| 428 | } | ||
| 429 | } | ||
| 430 | $val; | ||
| 431 | } | ||
| 432 | |||
| 433 | sub set_coverage { set_criteria(cover_names_to_val(@_)) } | ||
| 434 | sub add_coverage { add_criteria(cover_names_to_val(@_)) } | ||
| 435 | sub remove_coverage { remove_criteria(cover_names_to_val(@_)) } | ||
| 436 | |||
| 437 | sub get_coverage { | ||
| 438 | return unless defined wantarray; | ||
| 439 | my @names; | ||
| 440 | my $val = get_criteria(); | ||
| 441 | for my $c (sort keys %Criteria) { | ||
| 442 | push @names, $c if $val & $Criteria{$c}; | ||
| 443 | } | ||
| 444 | return wantarray ? @names : "@names"; | ||
| 445 | } | ||
| 446 | |||
| 447 | { | ||
| 448 | |||
| 449 | my %File_cache; | ||
| 450 | |||
| 451 | # Recursion in normalised_file() is bad. It can happen if a call from the sub | ||
| 452 | # evals something which wants to load a new module. This has happened with | ||
| 453 | # the Storable backend. I don't think it happens with the JSON backend. | ||
| 454 | my $Normalising; | ||
| 455 | |||
| 456 | sub normalised_file { | ||
| 457 | my ($file) = @_; | ||
| 458 | |||
| 459 | return $File_cache{$file} if exists $File_cache{$file}; | ||
| 460 | return $file if $Normalising; | ||
| 461 | $Normalising = 1; | ||
| 462 | |||
| 463 | my $f = $file; | ||
| 464 | $file =~ s/ \(autosplit into .*\)$//; | ||
| 465 | $file =~ s/^\(eval in .*\) //; | ||
| 466 | # print STDERR "file is <$file>\ncoverage: ", Dumper coverage(0); | ||
| 467 | if ( exists coverage(0)->{module} | ||
| 468 | && exists coverage(0)->{module}{$file} | ||
| 469 | && !File::Spec->file_name_is_absolute($file)) | ||
| 470 | { | ||
| 471 | my $m = coverage(0)->{module}{$file}; | ||
| 472 | # print STDERR "Loaded <$file> <$m->[0]> from <$m->[1]> "; | ||
| 473 | $file = File::Spec->rel2abs($file, $m->[1]); | ||
| 474 | # print STDERR "as <$file> "; | ||
| 475 | } | ||
| 476 | |||
| 477 | my $inc; | ||
| 478 | $inc ||= $file =~ $_ for @Inc_re; | ||
| 479 | # warn "inc for [$file] is [$inc] @Inc_re"; | ||
| 480 | if ($inc && ($^O eq "MSWin32" || $^O eq "cygwin")) { | ||
| 481 | # Windows' Cwd::_win32_cwd() calls eval which will recurse back | ||
| 482 | # here if we call abs_path, so we just assume it's normalised. | ||
| 483 | # warn "giving up on getting normalised filename from <$file>\n"; | ||
| 484 | } else { | ||
| 485 | # print STDERR "getting abs_path <$file> "; | ||
| 486 | if (-e $file) { # Windows likes the file to exist | ||
| 487 | my $abs; | ||
| 488 | $abs = abs_path($file) unless -l $file; # leave symbolic links | ||
| 489 | # print STDERR "giving <$abs> "; | ||
| 490 | $file = $abs if defined $abs; | ||
| 491 | } | ||
| 492 | } | ||
| 493 | # print STDERR "finally <$file> <$Dir>\n"; | ||
| 494 | |||
| 495 | $file =~ s|\\|/|g if $^O eq "MSWin32"; | ||
| 496 | $file =~ s|^\Q$Dir\E/|| if defined $Dir; | ||
| 497 | |||
| 498 | $Digests ||= Devel::Cover::DB::Digests->new(db => $DB); | ||
| 499 | $file = $Digests->canonical_file($file); | ||
| 500 | |||
| 501 | # print STDERR "File: $f => $file\n"; | ||
| 502 | |||
| 503 | $Normalising = 0; | ||
| 504 | $File_cache{$f} = $file | ||
| 505 | } | ||
| 506 | |||
| 507 | } | ||
| 508 | |||
| 509 | sub get_location { | ||
| 510 | my ($op) = @_; | ||
| 511 | |||
| 512 | # print STDERR "get_location ", $op, "\n"; | ||
| 513 | # use Carp "cluck"; cluck("from here"); | ||
| 514 | return unless $op->can("file"); # How does this happen? | ||
| 515 | $File = $op->file; | ||
| 516 | $Line = $op->line; | ||
| 517 | # print STDERR "$File:$Line\n"; | ||
| 518 | |||
| 519 | # If there's an eval, get the real filename. Enabled from $^P & 0x100. | ||
| 520 | while ($File =~ /^\(eval \d+\)\[(.*):(\d+)\]/) { | ||
| 521 | ($File, $Line) = ($1, $2); | ||
| 522 | } | ||
| 523 | $File = normalised_file($File); | ||
| 524 | |||
| 525 | if (!exists $Run{vec}{$File} && $Run{collected}) { | ||
| 526 | my %vec; | ||
| 527 | @vec{ @{ $Run{collected} } } = (); | ||
| 528 | delete $vec{time}; | ||
| 529 | $vec{subroutine}++ if exists $vec{pod}; | ||
| 530 | @{ $Run{vec}{$File}{$_} }{ "vec", "size" } = ("", 0) for keys %vec; | ||
| 531 | } | ||
| 532 | } | ||
| 533 | |||
| 534 | sub use_file { | ||
| 535 | my ($file) = @_; | ||
| 536 | |||
| 537 | state $find_filename = qr/ | ||
| 538 | (?:^\(eval\s \d+\)\[(.+):\d+\]) | | ||
| 539 | (?:^\(eval\sin\s\w+\)\s(.+)) | | ||
| 540 | (?:\(defined\sat\s(.+)\sline\s\d+\)) | | ||
| 541 | (?:\[from\s(.+)\sline\s\d+\]) | ||
| 542 | /x; | ||
| 543 | |||
| 544 | return 0 unless $file && $find_filename; # global destruction, probably | ||
| 545 | |||
| 546 | # print STDERR "use_file($file)\n"; | ||
| 547 | |||
| 548 | # die "bad file" unless length $file; | ||
| 549 | |||
| 550 | # If you call your file something that matches $find_filename then things | ||
| 551 | # might go awry. But it would be silly to do that, so don't. This little | ||
| 552 | # optimisation provides a reasonable speedup. | ||
| 553 | return $Files{$file} if exists $Files{$file}; | ||
| 554 | |||
| 555 | # just don't call your filenames 0 | ||
| 556 | while ($file =~ $find_filename) { $file = $1 || $2 || $3 || $4 } | ||
| 557 | $file =~ s/ \(autosplit into .*\)$//; | ||
| 558 | |||
| 559 | # print STDERR "==> use_file($file)\n"; | ||
| 560 | |||
| 561 | return $Files{$file} if exists $Files{$file}; | ||
| 562 | return 0 | ||
| 563 | if $file =~ /\(eval \d+\)/ | ||
| 564 | || $file =~ /^\.\.[\/\\]\.\.[\/\\]lib[\/\\](?:Storable|POSIX).pm$/; | ||
| 565 | |||
| 566 | my $f = normalised_file($file); | ||
| 567 | |||
| 568 | # print STDERR "checking <$file> <$f>\n"; | ||
| 569 | # print STDERR "checking <$file> <$f> against ", | ||
| 570 | # "select(@Select_re), ignore(@Ignore_re), inc(@Inc_re)\n"; | ||
| 571 | |||
| 572 | for (@Select_re) { return $Files{$file} = 1 if $f =~ $_ } | ||
| 573 | for (@Ignore_re) { return $Files{$file} = 0 if $f =~ $_ } | ||
| 574 | for (@Inc_re) { return $Files{$file} = 0 if $f =~ $_ } | ||
| 575 | |||
| 576 | # system "pwd; ls -l '$file'"; | ||
| 577 | $Files{$file} = -e $file ? 1 : 0; | ||
| 578 | print STDERR __PACKAGE__ . qq(: Can't find file "$file" (@_): ignored.\n) | ||
| 579 | unless $Files{$file} | ||
| 580 | || $Silent | ||
| 581 | || $file =~ $Devel::Cover::DB::Ignore_filenames; | ||
| 582 | |||
| 583 | add_cvs(); # add CVs now in case of symbol table manipulation | ||
| 584 | $Files{$file} | ||
| 585 | } | ||
| 586 | |||
| 587 | sub check_file { | ||
| 588 | my ($cv) = @_; | ||
| 589 | |||
| 590 | return unless ref($cv) eq "B::CV"; | ||
| 591 | |||
| 592 | my $op = $cv->START; | ||
| 593 | return unless ref($op) eq "B::COP"; | ||
| 594 | |||
| 595 | my $file = $op->file; | ||
| 596 | my $use = use_file($file); | ||
| 597 | # printf STDERR "%6s $file\n", $use ? "use" : "ignore"; | ||
| 598 | |||
| 599 | $use | ||
| 600 | } | ||
| 601 | |||
| 602 | sub B::GV::find_cv { | ||
| 603 | my $cv = $_[0]->CV; | ||
| 604 | return unless $$cv; | ||
| 605 | |||
| 606 | # print STDERR "find_cv $$cv\n" if check_file($cv); | ||
| 607 | $Cvs{$cv} ||= $cv if check_file($cv); | ||
| 608 | if ( $cv->can("PADLIST") | ||
| 609 | && $cv->PADLIST->can("ARRAY") | ||
| 610 | && $cv->PADLIST->ARRAY | ||
| 611 | && $cv->PADLIST->ARRAY->can("ARRAY")) | ||
| 612 | { | ||
| 613 | $Cvs{$_} ||= $_ | ||
| 614 | for grep ref eq "B::CV" && check_file($_), $cv->PADLIST->ARRAY->ARRAY; | ||
| 615 | } | ||
| 616 | } | ||
| 617 | |||
| 618 | sub sub_info { | ||
| 619 | my ($cv) = @_; | ||
| 620 | my ($name, $start) = ("--unknown--", 0); | ||
| 621 | my $gv = $cv->GV; | ||
| 622 | if ($gv && !$gv->isa("B::SPECIAL")) { | ||
| 623 | return unless $gv->can("SAFENAME"); | ||
| 624 | $name = $gv->SAFENAME; | ||
| 625 | # print STDERR "--[$name]--\n"; | ||
| 626 | $name =~ s/(__ANON__)\[.+:\d+\]/$1/ if defined $name; | ||
| 627 | } | ||
| 628 | # my $op = sub { my ($t, $o) = @_; print "$t\n"; $o->debug }; | ||
| 629 | my $root = $cv->ROOT; | ||
| 630 | # $op->(root => $root); | ||
| 631 | if ($root->can("first")) { | ||
| 632 | my $lineseq = $root->first; | ||
| 633 | # $op->(lineseq => $lineseq); | ||
| 634 | if ($lineseq->can("first")) { | ||
| 635 | # normal case | ||
| 636 | $start = $lineseq->first; | ||
| 637 | # $op->(start => $start); | ||
| 638 | # signatures | ||
| 639 | if ($start->name eq "null" && $start->can("first")) { | ||
| 640 | my $lineseq2 = $start->first; | ||
| 641 | # $op->(lineseq2 => $lineseq2); | ||
| 642 | if ($lineseq2->name eq "lineseq" && $lineseq2->can("first")) { | ||
| 643 | my $cop = $lineseq2->first; | ||
| 644 | # $op->(cop => $cop); | ||
| 645 | $start = $cop if $cop->name eq "nextstate"; | ||
| 646 | } | ||
| 647 | } | ||
| 648 | } elsif ($lineseq->name eq "nextstate") { | ||
| 649 | # completely empty sub - sub empty { } | ||
| 650 | $start = $lineseq; | ||
| 651 | } | ||
| 652 | } | ||
| 653 | ($name, $start) | ||
| 654 | } | ||
| 655 | |||
| 656 | sub add_cvs { | ||
| 657 | $Cvs{$_} ||= $_ for grep check_file($_), B::main_cv->PADLIST->ARRAY->ARRAY; | ||
| 658 | } | ||
| 659 | |||
| 660 | sub check_files { | ||
| 661 | # print STDERR "Checking files\n"; | ||
| 662 | |||
| 663 | add_cvs(); | ||
| 664 | |||
| 665 | my %seen_pkg; | ||
| 666 | my %seen_cv; | ||
| 667 | |||
| 668 | walksymtable(\%main::, "find_cv", sub { !$seen_pkg{ $_[0] }++ }); | ||
| 669 | |||
| 670 | my $l = sub { | ||
| 671 | my ($cv) = @_; | ||
| 672 | my $line = 0; | ||
| 673 | my ($name, $start) = sub_info($cv); | ||
| 674 | if ($start) { | ||
| 675 | local ($Line, $File); | ||
| 676 | get_location($start); | ||
| 677 | $line = $Line; | ||
| 678 | # print STDERR "$name - $File:$Line\n"; | ||
| 679 | } | ||
| 680 | $line = 0 unless defined $line; | ||
| 681 | $name = '' unless defined $name; | ||
| 682 | ($line, $name) | ||
| 683 | }; | ||
| 684 | |||
| 685 | # print Dumper \%Cvs; | ||
| 686 | |||
| 687 | @Cvs = map $_->[0], | ||
| 688 | sort { $a->[1] <=> $b->[1] || $a->[2] cmp $b->[2] } map [ $_, $l->($_) ], | ||
| 689 | grep !$seen_cv{$$_}++, values %Cvs; | ||
| 690 | |||
| 691 | # Hack to bump up the refcount of the subs. If we don't do this then the | ||
| 692 | # subs in some modules don't seem to be around when we get to looking at | ||
| 693 | # them. I'm not sure why this is, and it seems to me that this hack could | ||
| 694 | # affect the order of destruction, but I've not seen any problems. Yet. | ||
| 695 | @Subs = map $_->object_2svref, @Cvs; | ||
| 696 | } | ||
| 697 | |||
| 698 | my %Seen; | ||
| 699 | |||
| 700 | sub report { | ||
| 701 | local $@; | ||
| 702 | eval { _report() }; | ||
| 703 | if ($@) { | ||
| 704 | print STDERR <<"EOM" unless $Silent; | ||
| 705 | Devel::Cover: Oops, it looks like something went wrong writing the coverage. | ||
| 706 | It's possible that more bad things may happen but we'll try to | ||
| 707 | carry on anyway as if nothing happened. At a minimum you'll | ||
| 708 | probably find that you are missing coverage. If you're | ||
| 709 | interested, the problem was: | ||
| 710 | |||
| 711 | $@ | ||
| 712 | |||
| 713 | EOM | ||
| 714 | } | ||
| 715 | return unless $Self_cover; | ||
| 716 | $Self_cover_run = 1; | ||
| 717 | _report(); | ||
| 718 | } | ||
| 719 | |||
| 720 | sub _report { | ||
| 721 | local @SIG{qw(__DIE__ __WARN__)}; | ||
| 722 | # $SIG{__DIE__} = \&Carp::confess; | ||
| 723 | |||
| 724 | $Run{finish} = get_elapsed() / 1e6; | ||
| 725 | |||
| 726 | die "Devel::Cover::import() not run: " | ||
| 727 | . "did you require instead of use Devel::Cover?\n" | ||
| 728 | unless defined $Dir; | ||
| 729 | |||
| 730 | my @collected = get_coverage(); | ||
| 731 | return unless @collected; | ||
| 732 | set_coverage("none") unless $Self_cover; | ||
| 733 | |||
| 734 | my ($starting_dir) = Cwd::getcwd() =~ /(.*)/; | ||
| 735 | chdir $Dir or die __PACKAGE__ . ": Can't chdir $Dir: $!\n"; | ||
| 736 | |||
| 737 | $Run{collected} = \@collected; | ||
| 738 | $Structure = Devel::Cover::DB::Structure->new(base => $DB, | ||
| 739 | loose_perms => $Loose_perms); | ||
| 740 | $Structure->read_all; | ||
| 741 | $Structure->add_criteria(@collected); | ||
| 742 | # print STDERR "Start structure: ", Dumper $Structure; | ||
| 743 | |||
| 744 | # print STDERR "Processing cover data\n@Inc\n"; | ||
| 745 | $Coverage = coverage(1) || die "No coverage data available.\n"; | ||
| 746 | # print STDERR Dumper $Coverage; | ||
| 747 | |||
| 748 | check_files(); | ||
| 749 | |||
| 750 | unless ($Subs_only) { | ||
| 751 | get_cover(main_cv, main_root); | ||
| 752 | get_cover_progress("BEGIN block", | ||
| 753 | B::begin_av()->isa("B::AV") ? B::begin_av()->ARRAY : ()); | ||
| 754 | if (exists &B::check_av) { | ||
| 755 | get_cover_progress("CHECK block", | ||
| 756 | B::check_av()->isa("B::AV") ? B::check_av()->ARRAY : ()); | ||
| 757 | } | ||
| 758 | # get_ends includes INIT blocks | ||
| 759 | get_cover_progress("END/INIT block", | ||
| 760 | get_ends()->isa("B::AV") ? get_ends()->ARRAY : ()); | ||
| 761 | } | ||
| 762 | # print STDERR "--- @Cvs\n"; | ||
| 763 | get_cover_progress("CV", @Cvs); | ||
| 764 | |||
| 765 | my %files; | ||
| 766 | $files{$_}++ for keys %{ $Run{count} }, keys %{ $Run{vec} }; | ||
| 767 | for my $file (sort keys %files) { | ||
| 768 | # print STDERR "looking at $file\n"; | ||
| 769 | unless (use_file($file)) { | ||
| 770 | # print STDERR "deleting $file\n"; | ||
| 771 | delete $Run{count}->{$file}; | ||
| 772 | delete $Run{vec}->{$file}; | ||
| 773 | $Structure->delete_file($file); | ||
| 774 | next; | ||
| 775 | } | ||
| 776 | |||
| 777 | # $Structure->add_digest($file, \%Run); | ||
| 778 | |||
| 779 | for my $run (keys %{ $Run{vec}{$file} }) { | ||
| 780 | delete $Run{vec}{$file}{$run} unless $Run{vec}{$file}{$run}{size}; | ||
| 781 | } | ||
| 782 | |||
| 783 | $Structure->store_counts($file); | ||
| 784 | } | ||
| 785 | |||
| 786 | # print STDERR "End structure: ", Dumper $Structure; | ||
| 787 | |||
| 788 | my $run = time . ".$$." . sprintf "%05d", rand 2**16; | ||
| 789 | my $cover = Devel::Cover::DB->new( | ||
| 790 | base => $DB, | ||
| 791 | runs => { $run => \%Run }, | ||
| 792 | structure => $Structure, | ||
| 793 | loose_perms => $Loose_perms | ||
| 794 | ); | ||
| 795 | |||
| 796 | my $dbrun = "$DB/runs"; | ||
| 797 | unless (mkdir $dbrun) { | ||
| 798 | die "Can't mkdir $dbrun $!" unless -d $dbrun; | ||
| 799 | } | ||
| 800 | chmod 0777, $dbrun if $Loose_perms; | ||
| 801 | $dbrun .= "/$run"; | ||
| 802 | |||
| 803 | print OUT __PACKAGE__, ": Writing coverage database to $dbrun\n" | ||
| 804 | unless $Silent; | ||
| 805 | $cover->write($dbrun); | ||
| 806 | $Digests->write; | ||
| 807 | $cover->print_summary if $Summary && !$Silent; | ||
| 808 | |||
| 809 | if ($Self_cover && !$Self_cover_run) { | ||
| 810 | $cover->delete; | ||
| 811 | delete $Run{vec}; | ||
| 812 | } | ||
| 813 | chdir $starting_dir if $starting_dir; | ||
| 814 | } | ||
| 815 | |||
| 816 | sub add_subroutine_cover { | ||
| 817 | my ($op) = @_; | ||
| 818 | |||
| 819 | get_location($op); | ||
| 820 | return unless $File; | ||
| 821 | |||
| 822 | # print STDERR "Subroutine $Sub_name $File:$Line: ", $op->name, "\n"; | ||
| 823 | |||
| 824 | my $key = get_key($op); | ||
| 825 | my $val = $Coverage->{statement}{$key} || 0; | ||
| 826 | my ($n, $new) = $Structure->add_count("subroutine"); | ||
| 827 | # print STDERR "******* subroutine $n - $new\n"; | ||
| 828 | $Structure->add_subroutine($File, [ $Line, $Sub_name ]) if $new; | ||
| 829 | $Run{count}{$File}{subroutine}[$n] += $val; | ||
| 830 | my $vec = $Run{vec}{$File}{subroutine}; | ||
| 831 | vec($vec->{vec}, $n, 1) = $val ? 1 : 0; | ||
| 832 | $vec->{size} = $n + 1; | ||
| 833 | } | ||
| 834 | |||
| 835 | sub add_statement_cover { | ||
| 836 | my ($op) = @_; | ||
| 837 | |||
| 838 | get_location($op); | ||
| 839 | return unless $File; | ||
| 840 | |||
| 841 | # print STDERR "Stmt $File:$Line: $op $$op ", $op->name, "\n"; | ||
| 842 | |||
| 843 | $Run{digests}{$File} ||= $Structure->set_file($File); | ||
| 844 | my $key = get_key($op); | ||
| 845 | my $val = $Coverage->{statement}{$key} || 0; | ||
| 846 | my ($n, $new) = $Structure->add_count("statement"); | ||
| 847 | # print STDERR "Stmt $File:$Line - $n, $new\n"; | ||
| 848 | $Structure->add_statement($File, $Line) if $new; | ||
| 849 | $Run{count}{$File}{statement}[$n] += $val; | ||
| 850 | my $vec = $Run{vec}{$File}{statement}; | ||
| 851 | vec($vec->{vec}, $n, 1) = $val ? 1 : 0; | ||
| 852 | $vec->{size} = $n + 1; | ||
| 853 | no warnings "uninitialized"; | ||
| 854 | $Run{count}{$File}{time}[$n] += $Coverage->{time}{$key} | ||
| 855 | if $Coverage{time} | ||
| 856 | && exists $Coverage->{time} | ||
| 857 | && exists $Coverage->{time}{$key}; | ||
| 858 | } | ||
| 859 | |||
| 860 | sub add_branch_cover { | ||
| 861 | return unless $Collect && $Coverage{branch}; | ||
| 862 | |||
| 863 | my ($op, $type, $text, $file, $line) = @_; | ||
| 864 | |||
| 865 | # return unless $Seen{branch}{$$op}++; | ||
| 866 | |||
| 867 | $text =~ s/^\s+//; | ||
| 868 | $text =~ s/\s+$//; | ||
| 869 | |||
| 870 | my $key = get_key($op); | ||
| 871 | my $c = $Coverage->{condition}{$key}; | ||
| 872 | |||
| 873 | no warnings "uninitialized"; | ||
| 874 | # warn "add_branch_cover $File:$Line [$type][@{[join ', ', @$c]}]\n"; | ||
| 875 | |||
| 876 | if ( $type eq "and" | ||
| 877 | || $type eq "or" | ||
| 878 | || ($type eq "elsif" && !exists $Coverage->{branch}{$key})) | ||
| 879 | { | ||
| 880 | # and => this could also be a plain if with no else or elsif | ||
| 881 | # or => this could also be an unless with no else or elsif | ||
| 882 | # elsif => no subsequent elsifs or elses | ||
| 883 | # True path taken if not short circuited. | ||
| 884 | # False path taken if short circuited. | ||
| 885 | $c = [ $c->[1] + $c->[2], $c->[3] ]; | ||
| 886 | # print STDERR "branch $type [@$c]\n"; | ||
| 887 | } else { | ||
| 888 | $c = $Coverage->{branch}{$key} || [ 0, 0 ]; | ||
| 889 | } | ||
| 890 | |||
| 891 | my ($n, $new) = $Structure->add_count("branch"); | ||
| 892 | $Structure->add_branch($file, [ $line, { text => $text } ]) if $new; | ||
| 893 | my $ccount = $Run{count}{$file}; | ||
| 894 | if (exists $ccount->{branch}[$n]) { | ||
| 895 | $ccount->{branch}[$n][$_] += $c->[$_] for 0 .. $#$c; | ||
| 896 | } else { | ||
| 897 | $ccount->{branch}[$n] = $c; | ||
| 898 | my $vec = $Run{vec}{$File}{branch}; | ||
| 899 | vec($vec->{vec}, $vec->{size}++, 1) = $_ ||= 0 ? 1 : 0 for @$c; | ||
| 900 | } | ||
| 901 | |||
| 902 | # warn "branch $type %x [@$c] => [@{$ccount->{branch}[$n]}]\n", $$op; | ||
| 903 | } | ||
| 904 | |||
| 905 | sub add_condition_cover { | ||
| 906 | my ($op, $strop, $left, $right) = @_; | ||
| 907 | |||
| 908 | return unless $Collect && $Coverage{condition}; | ||
| 909 | |||
| 910 | my $key = get_key($op); | ||
| 911 | # warn "Condition cover $$op from $File:$Line\n"; | ||
| 912 | # print STDERR "left: [$left]\nright: [$right]\n"; | ||
| 913 | # use Carp "cluck"; cluck("from here"); | ||
| 914 | |||
| 915 | my $type = $op->name; | ||
| 916 | $type =~ s/assign$//; | ||
| 917 | $type = "or" if $type eq "dor"; | ||
| 918 | |||
| 919 | my $c = $Coverage->{condition}{$key}; | ||
| 920 | |||
| 921 | no warnings "uninitialized"; | ||
| 922 | |||
| 923 | my $count; | ||
| 924 | |||
| 925 | if ($type eq "or" || $type eq "and") { | ||
| 926 | my $r = $op->first->sibling; | ||
| 927 | my $name = $r->name; | ||
| 928 | $name = $r->first->name if $name eq "sassign"; | ||
| 929 | # TODO - exec? any others? | ||
| 930 | # print STDERR "Name [$name]", Dumper $c; | ||
| 931 | if ($c->[5] || $name =~ $Const_right) { | ||
| 932 | $c = [ $c->[3], $c->[1] + $c->[2] ]; | ||
| 933 | $count = 2; | ||
| 934 | # print STDERR "Special short circuit\n"; | ||
| 935 | } else { | ||
| 936 | @$c = @{$c}[ $type eq "or" ? (3, 2, 1) : (3, 1, 2) ]; | ||
| 937 | $count = 3; | ||
| 938 | } | ||
| 939 | # print STDERR "$type 3 $name [", join(",", @$c), "] $File:$Line\n"; | ||
| 940 | } elsif ($type eq "xor") { | ||
| 941 | # !l&&!r l&&!r l&&r !l&&r | ||
| 942 | @$c = @{$c}[ 3, 2, 4, 1 ]; | ||
| 943 | $count = 4; | ||
| 944 | } else { | ||
| 945 | die qq(Unknown type "$type" for conditional); | ||
| 946 | } | ||
| 947 | |||
| 948 | my $structure = { | ||
| 949 | type => "${type}_${count}", | ||
| 950 | op => $strop, | ||
| 951 | left => $left, | ||
| 952 | right => $right, | ||
| 953 | }; | ||
| 954 | |||
| 955 | my ($n, $new) = $Structure->add_count("condition"); | ||
| 956 | $Structure->add_condition($File, [ $Line, $structure ]) if $new; | ||
| 957 | my $ccount = $Run{count}{$File}; | ||
| 958 | if (exists $ccount->{condition}[$n]) { | ||
| 959 | $ccount->{condition}[$n][$_] += $c->[$_] for 0 .. $#$c; | ||
| 960 | } else { | ||
| 961 | $ccount->{condition}[$n] = $c; | ||
| 962 | my $vec = $Run{vec}{$File}{condition}; | ||
| 963 | vec($vec->{vec}, $vec->{size}++, 1) = $_ ||= 0 ? 1 : 0 for @$c; | ||
| 964 | } | ||
| 965 | } | ||
| 966 | |||
| 967 | { | ||
| 968 | no warnings "once"; | ||
| 969 | *is_scope = \&B::Deparse::is_scope; | ||
| 970 | *is_state = \&B::Deparse::is_state; | ||
| 971 | *is_ifelse_cont = \&B::Deparse::is_ifelse_cont; | ||
| 972 | } | ||
| 973 | |||
| 974 | my %Original; | ||
| 975 | { | ||
| 976 | |||
| 977 | BEGIN { | ||
| 978 | $Original{deparse} = \&B::Deparse::deparse; | ||
| 979 | $Original{logop} = \&B::Deparse::logop; | ||
| 980 | $Original{logassignop} = \&B::Deparse::logassignop; | ||
| 981 | $Original{binop} = \&B::Deparse::binop; | ||
| 982 | $Original{const_dumper} = \&B::Deparse::const_dumper; | ||
| 983 | $Original{const} = \&B::Deparse::const if defined &B::Deparse::const; | ||
| 984 | } | ||
| 985 | |||
| 986 | sub const_dumper { | ||
| 987 | no warnings "redefine"; | ||
| 988 | |||
| 989 | local *B::Deparse::deparse = $Original{deparse}; | ||
| 990 | local *B::Deparse::logop = $Original{logop}; | ||
| 991 | local *B::Deparse::logassignop = $Original{logassignop}; | ||
| 992 | local *B::Deparse::binop = $Original{binop}; | ||
| 993 | local *B::Deparse::const_dumper = $Original{const_dumper}; | ||
| 994 | local *B::Deparse::const = $Original{const} if $Original{const}; | ||
| 995 | |||
| 996 | $Original{const_dumper}->(@_); | ||
| 997 | } | ||
| 998 | |||
| 999 | sub const { | ||
| 1000 | no warnings "redefine"; | ||
| 1001 | |||
| 1002 | local *B::Deparse::deparse = $Original{deparse}; | ||
| 1003 | local *B::Deparse::logop = $Original{logop}; | ||
| 1004 | local *B::Deparse::logassignop = $Original{logassignop}; | ||
| 1005 | local *B::Deparse::binop = $Original{binop}; | ||
| 1006 | local *B::Deparse::const_dumper = $Original{const_dumper}; | ||
| 1007 | |||
| 1008 | $Original{const}->(@_); | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | sub deparse { | ||
| 1012 | my $self = shift; | ||
| 1013 | my ($op, $cx) = @_; | ||
| 1014 | |||
| 1015 | my $deparse; | ||
| 1016 | |||
| 1017 | if ($Collect) { | ||
| 1018 | my $class = B::class($op); | ||
| 1019 | my $null = $class eq "NULL"; | ||
| 1020 | |||
| 1021 | my $name = $op->can("name") ? $op->name : "Unknown"; | ||
| 1022 | |||
| 1023 | # print STDERR "$class:$name ($$op) at $File:$Line\n"; | ||
| 1024 | # print STDERR "[$Seen{statement}{$$op}] [$Seen{other}{$$op}]\n"; | ||
| 1025 | # use Carp "cluck"; cluck("from here"); | ||
| 1026 | |||
| 1027 | return "" if $name eq "padrange"; | ||
| 1028 | |||
| 1029 | unless ($Seen{statement}{$$op} || $Seen{other}{$$op}) { | ||
| 1030 | # Collect everything under here | ||
| 1031 | local ($File, $Line) = ($File, $Line); | ||
| 1032 | # print STDERR "Collecting $$op under $File:$Line\n"; | ||
| 1033 | no warnings "redefine"; | ||
| 1034 | my $use_dumper = $class eq "SVOP" && $name eq "const"; | ||
| 1035 | local $self->{use_dumper} = 1 if $use_dumper; | ||
| 1036 | require Data::Dumper if $use_dumper; | ||
| 1037 | $deparse = eval { local $^W; $Original{deparse}->($self, @_) }; | ||
| 1038 | $deparse =~ s/^\010+//mg if defined $deparse; | ||
| 1039 | $deparse = "Deparse error: $@" if $@; | ||
| 1040 | # print STDERR "Collected $$op under $File:$Line\n"; | ||
| 1041 | # print STDERR "Collect Deparse $op $$op => <$deparse>\n"; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | # Get the coverage on this op | ||
| 1045 | |||
| 1046 | if ($class eq "COP" && $Coverage{statement}) { | ||
| 1047 | # print STDERR "COP $$op, seen [$Seen{statement}{$$op}]\n"; | ||
| 1048 | my $nnnext = ""; | ||
| 1049 | eval { | ||
| 1050 | my $next = $op->next; | ||
| 1051 | my $nnext = $next && $next->next; | ||
| 1052 | $nnnext = $nnext && $nnext->next; | ||
| 1053 | }; | ||
| 1054 | # print STDERR "COP $$op, ", $next, " -> ", $nnext, | ||
| 1055 | # " -> ", $nnnext, "\n"; | ||
| 1056 | if ($nnnext) { | ||
| 1057 | add_statement_cover($op) unless $Seen{statement}{$$op}++; | ||
| 1058 | } | ||
| 1059 | } elsif (!$null | ||
| 1060 | && $name eq "null" | ||
| 1061 | && ppname($op->targ) eq "pp_nextstate" | ||
| 1062 | && $Coverage{statement}) | ||
| 1063 | { | ||
| 1064 | # If the current op is null, but it was nextstate, we can still | ||
| 1065 | # get at the file and line number, but we need to get dirty | ||
| 1066 | |||
| 1067 | bless $op, "B::COP"; | ||
| 1068 | # print STDERR "null $$op, seen [$Seen{statement}{$$op}]\n"; | ||
| 1069 | add_statement_cover($op) unless $Seen{statement}{$$op}++; | ||
| 1070 | bless $op, "B::$class"; | ||
| 1071 | } elsif ($Seen{other}{$$op}++) { | ||
| 1072 | # print STDERR "seen [$Seen{other}{$$op}]\n"; | ||
| 1073 | return "" # Only report on each op once | ||
| 1074 | } elsif ($name eq "cond_expr") { | ||
| 1075 | local ($File, $Line) = ($File, $Line); | ||
| 1076 | my $cond = $op->first; | ||
| 1077 | my $true = $cond->sibling; | ||
| 1078 | my $false = $true->sibling; | ||
| 1079 | if (!( | ||
| 1080 | $cx < 1 | ||
| 1081 | && $self->{'expand'} < 7 | ||
| 1082 | && ( | ||
| 1083 | B::class($false) eq "NULL" # sub: empty else optimised to NULL | ||
| 1084 | || $false->name eq "null" # main: empty else optimised to null op | ||
| 1085 | || ((is_scope($true) && $true->name ne "null") | ||
| 1086 | && (is_scope($false) || is_ifelse_cont($false))) | ||
| 1087 | ) | ||
| 1088 | )) | ||
| 1089 | { | ||
| 1090 | { local $Collect; $cond = $self->deparse($cond, 8) } | ||
| 1091 | add_branch_cover($op, "if", "$cond ? :", $File, $Line); | ||
| 1092 | } else { | ||
| 1093 | { local $Collect; $cond = $self->deparse($cond, 1) } | ||
| 1094 | add_branch_cover($op, "if", "if ($cond) { }", $File, $Line); | ||
| 1095 | while (B::class($false) ne "NULL" && is_ifelse_cont($false)) { | ||
| 1096 | my $newop = $false->first; | ||
| 1097 | my $newcond = $newop->first; | ||
| 1098 | my $newtrue = $newcond->sibling; | ||
| 1099 | if ($newcond->name eq "lineseq") { | ||
| 1100 | # lineseq to ensure correct line numbers in elsif() | ||
| 1101 | # Bug #37302 fixed by change #33710 | ||
| 1102 | $newcond = $newcond->first->sibling; | ||
| 1103 | } | ||
| 1104 | # last in chain is OP_AND => no else | ||
| 1105 | $false = $newtrue->sibling; | ||
| 1106 | { local $Collect; $newcond = $self->deparse($newcond, 1) } | ||
| 1107 | add_branch_cover($newop, "elsif", "elsif ($newcond) { }", | ||
| 1108 | $File, $Line); | ||
| 1109 | } | ||
| 1110 | } | ||
| 1111 | } | ||
| 1112 | } else { | ||
| 1113 | local ($File, $Line) = ($File, $Line); | ||
| 1114 | # print STDERR "Starting plain deparse at $File:$Line\n"; | ||
| 1115 | $deparse = eval { local $^W; $Original{deparse}->($self, @_) }; | ||
| 1116 | $deparse = "" unless defined $deparse; | ||
| 1117 | $deparse =~ s/^\010+//mg; | ||
| 1118 | $deparse = "Deparse error: $@" if $@; | ||
| 1119 | # print STDERR "Ending plain deparse at $File:$Line\n"; | ||
| 1120 | # print STDERR "Deparse => <$deparse>\n"; | ||
| 1121 | } | ||
| 1122 | |||
| 1123 | # print STDERR "Returning [$deparse]\n"; | ||
| 1124 | $deparse | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | sub logop { ## no critic (ProhibitManyArgs) # B::Deparse API | ||
| 1128 | my $self = shift; | ||
| 1129 | my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_; | ||
| 1130 | my $left = $op->first; | ||
| 1131 | my $right = $op->first->sibling; | ||
| 1132 | # print STDERR "left [$left], right [$right]\n"; | ||
| 1133 | my ($file, $line) = ($File, $Line); | ||
| 1134 | |||
| 1135 | $blockname &&= $self->keyword($blockname); | ||
| 1136 | if ($cx < 1 && is_scope($right) && $blockname && $self->{expand} < 7) { | ||
| 1137 | # print STDERR 'if ($a) {$b}', "\n"; | ||
| 1138 | # if ($a) {$b} | ||
| 1139 | $left = $self->deparse($left, 1); | ||
| 1140 | $right = $self->deparse($right, 0); | ||
| 1141 | add_branch_cover($op, $lowop, "$blockname ($left)", $file, $line) | ||
| 1142 | unless $Seen{branch}{$$op}++; | ||
| 1143 | return "$blockname ($left) {\n\t$right\n\b}\cK" | ||
| 1144 | } elsif ($cx < 1 && $blockname && !$self->{parens} && $self->{expand} < 7) { | ||
| 1145 | # print STDERR '$b if $a', "\n"; | ||
| 1146 | # $b if $a | ||
| 1147 | $right = $self->deparse($right, 1); | ||
| 1148 | $left = $self->deparse($left, 1); | ||
| 1149 | add_branch_cover($op, $lowop, "$blockname $left", $file, $line) | ||
| 1150 | unless $Seen{branch}{$$op}++; | ||
| 1151 | return "$right $blockname $left" | ||
| 1152 | } elsif ($cx > $lowprec && $highop) { | ||
| 1153 | # print STDERR '$a && $b', "\n"; | ||
| 1154 | # $a && $b | ||
| 1155 | { | ||
| 1156 | local $Collect; | ||
| 1157 | $left = $self->deparse_binop_left($op, $left, $highprec); | ||
| 1158 | $right = $self->deparse_binop_right($op, $right, $highprec); | ||
| 1159 | } | ||
| 1160 | # print STDERR "left [$left], right [$right]\n"; | ||
| 1161 | add_condition_cover($op, $highop, $left, $right) | ||
| 1162 | unless $Seen{condition}{$$op}++; | ||
| 1163 | return $self->maybe_parens("$left $highop $right", $cx, $highprec) | ||
| 1164 | } else { | ||
| 1165 | # print STDERR '$a and $b', "\n"; | ||
| 1166 | # $a and $b | ||
| 1167 | $left = $self->deparse_binop_left($op, $left, $lowprec); | ||
| 1168 | $right = $self->deparse_binop_right($op, $right, $lowprec); | ||
| 1169 | add_condition_cover($op, $lowop, $left, $right) | ||
| 1170 | unless $Seen{condition}{$$op}++; | ||
| 1171 | return $self->maybe_parens("$left $lowop $right", $cx, $lowprec) | ||
| 1172 | } | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | sub logassignop { | ||
| 1176 | my $self = shift; | ||
| 1177 | my ($op, $cx, $opname) = @_; | ||
| 1178 | my $left = $op->first; | ||
| 1179 | my $right = $op->first->sibling->first; # skip sassign | ||
| 1180 | $left = $self->deparse($left, 7); | ||
| 1181 | $right = $self->deparse($right, 7); | ||
| 1182 | add_condition_cover($op, $opname, $left, $right); | ||
| 1183 | return $self->maybe_parens("$left $opname $right", $cx, 7); | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | sub binop { | ||
| 1187 | my $self = shift; | ||
| 1188 | my ($op, $cx, $opname, $prec, $flags) = (@_, 0); | ||
| 1189 | if ($] >= 5.041012 | ||
| 1190 | && ($opname eq "xor" || $opname eq "^^" && !$Seen{condition}{$$op}++)) | ||
| 1191 | { | ||
| 1192 | my $left = $op->first; | ||
| 1193 | my $right = $op->last; | ||
| 1194 | { | ||
| 1195 | local $Collect; | ||
| 1196 | $left = $self->deparse_binop_left($op, $left, $prec); | ||
| 1197 | $right = $self->deparse_binop_right($op, $right, $prec); | ||
| 1198 | } | ||
| 1199 | # print STDERR "opname [$opname], left [$left], right [$right]\n"; | ||
| 1200 | add_condition_cover($op, $opname, $left, $right); | ||
| 1201 | } | ||
| 1202 | $Original{binop}->($self, $op, $cx, $opname, $prec, $flags) | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | } | ||
| 1206 | |||
| 1207 | sub get_cover { | ||
| 1208 | my $deparse = B::Deparse->new; | ||
| 1209 | |||
| 1210 | my $cv = $deparse->{curcv} = shift; | ||
| 1211 | |||
| 1212 | ($Sub_name, my $start) = sub_info($cv); | ||
| 1213 | |||
| 1214 | # warn "get_cover: <$Sub_name>\n"; | ||
| 1215 | return unless defined $Sub_name; # Only happens within Safe.pm, AFAIK | ||
| 1216 | # return unless length $Sub_name; # Only happens with Self_cover, AFAIK | ||
| 1217 | |||
| 1218 | get_location($start) if $start; | ||
| 1219 | # print STDERR "[[$File:$Line]]\n"; | ||
| 1220 | # return unless length $File; | ||
| 1221 | return if length $File && !use_file($File); | ||
| 1222 | |||
| 1223 | return if !$Self_cover_run && $File =~ /Devel\/Cover/; | ||
| 1224 | return if $Self_cover_run && $File !~ /Devel\/Cover/; | ||
| 1225 | return | ||
| 1226 | if $Self_cover_run && $File =~ /Devel\/Cover\.pm$/ && $Sub_name eq "import"; | ||
| 1227 | |||
| 1228 | # printf STDERR "getting cover for $Sub_name ($start), %x\n", $$cv; | ||
| 1229 | |||
| 1230 | if ($start) { | ||
| 1231 | no warnings "uninitialized"; | ||
| 1232 | if ( $File eq $Structure->get_file | ||
| 1233 | && $Line == $Structure->get_line | ||
| 1234 | && $Sub_name eq "__ANON__" | ||
| 1235 | && $Structure->get_sub_name eq "__ANON__") | ||
| 1236 | { | ||
| 1237 | # Merge instances of anonymous subs into one | ||
| 1238 | # TODO - multiple anonymous subs on the same line | ||
| 1239 | } else { | ||
| 1240 | my $count = $Sub_count->{$File}{$Line}{$Sub_name}++; | ||
| 1241 | $Structure->set_subroutine($Sub_name, $File, $Line, $count); | ||
| 1242 | add_subroutine_cover($start) | ||
| 1243 | if $Coverage{subroutine} || $Coverage{pod}; # pod requires subs | ||
| 1244 | } | ||
| 1245 | } | ||
| 1246 | |||
| 1247 | if ($Pod && $Coverage{pod}) { | ||
| 1248 | my $gv = $cv->GV; | ||
| 1249 | if ($gv && !$gv->isa("B::SPECIAL")) { | ||
| 1250 | my $stash = $gv->STASH; | ||
| 1251 | my $pkg = $stash->NAME; | ||
| 1252 | my $file = $cv->FILE; | ||
| 1253 | my %opts; | ||
| 1254 | $Run{digests}{$File} ||= $Structure->set_file($File); | ||
| 1255 | if (ref $Coverage_options{pod}) { | ||
| 1256 | my $p; | ||
| 1257 | for (@{ $Coverage_options{pod} }) { | ||
| 1258 | if (/^package|(?:also_)?private|trustme|pod_from|nocp$/) { | ||
| 1259 | $opts{ $p = $_ } = []; | ||
| 1260 | } elsif ($p) { | ||
| 1261 | push @{ $opts{$p} }, $_; | ||
| 1262 | } | ||
| 1263 | } | ||
| 1264 | for my $p (qw( private also_private trustme )) { | ||
| 1265 | next unless exists $opts{$p}; | ||
| 1266 | $_ = qr/$_/ for @{ $opts{$p} }; | ||
| 1267 | } | ||
| 1268 | } | ||
| 1269 | $Pod = "Pod::Coverage" if delete $opts{nocp}; | ||
| 1270 | # print STDERR "$Pod, $File:$Line ($Sub_name) [$file($pkg)]", | ||
| 1271 | # Dumper \%opts; | ||
| 1272 | if ($Pod{$pkg} ||= $Pod->new(package => $pkg, %opts)) { | ||
| 1273 | # print STDERR Dumper $Pod{$file}; | ||
| 1274 | my $covered; | ||
| 1275 | for ($Pod{$pkg}->covered) { | ||
| 1276 | $covered = 1, last if $_ eq $Sub_name; | ||
| 1277 | } | ||
| 1278 | unless ($covered) { | ||
| 1279 | for ($Pod{$pkg}->uncovered) { | ||
| 1280 | $covered = 0, last if $_ eq $Sub_name; | ||
| 1281 | } | ||
| 1282 | } | ||
| 1283 | # print STDERR "covered ", $covered // "undef", "\n"; | ||
| 1284 | if (defined $covered) { | ||
| 1285 | my ($n, $new) = $Structure->add_count("pod"); | ||
| 1286 | $Structure->add_pod($File, [ $Line, $Sub_name ]) if $new; | ||
| 1287 | $Run{count}{$File}{pod}[$n] += $covered; | ||
| 1288 | my $vec = $Run{vec}{$File}{pod}; | ||
| 1289 | vec($vec->{vec}, $n, 1) = $covered ? 1 : 0; | ||
| 1290 | $vec->{size} = $n + 1; | ||
| 1291 | } | ||
| 1292 | } | ||
| 1293 | } | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | # my $dd = @_ && ref $_[0] | ||
| 1297 | # ? $deparse->deparse($_[0], 0) | ||
| 1298 | # : $deparse->deparse_sub($cv, 0); | ||
| 1299 | # print STDERR "get_cover: <$Sub_name>\n"; | ||
| 1300 | # print STDERR "[[$File:$Line]]\n"; | ||
| 1301 | # print STDERR "<$dd>\n"; | ||
| 1302 | |||
| 1303 | no warnings "redefine"; | ||
| 1304 | |||
| 1305 | local *B::Deparse::deparse = \&deparse; | ||
| 1306 | local *B::Deparse::logop = \&logop; | ||
| 1307 | local *B::Deparse::logassignop = \&logassignop; | ||
| 1308 | local *B::Deparse::binop = \&binop; | ||
| 1309 | local *B::Deparse::const_dumper = \&const_dumper; | ||
| 1310 | local *B::Deparse::const = \&const if $Original{const}; | ||
| 1311 | |||
| 1312 | my $de = @_ | ||
| 1313 | && ref $_[0] ? $deparse->deparse($_[0], 0) : $deparse->deparse_sub($cv, 0); | ||
| 1314 | |||
| 1315 | # print STDERR "<$de>\n"; | ||
| 1316 | $de | ||
| 1317 | } | ||
| 1318 | |||
| 1319 | sub _report_progress { | ||
| 1320 | my ($msg, $code, @items) = @_; | ||
| 1321 | if ($Silent) { | ||
| 1322 | $code->($_) for @items; | ||
| 1323 | return; | ||
| 1324 | } | ||
| 1325 | my $tot = @items || 1; | ||
| 1326 | my $prog = sub { | ||
| 1327 | my ($n) = @_; | ||
| 1328 | print OUT "\r" . __PACKAGE__ . ": " . int(100 * $n / $tot) . "% "; | ||
| 1329 | }; | ||
| 1330 | my ($old_pipe, $n, $start) = ($|, 0, time); | ||
| 1331 | $|++; | ||
| 1332 | print OUT __PACKAGE__, ": $msg\n"; | ||
| 1333 | my $is_interactive = -t *OUT; | ||
| 1334 | for (@items) { | ||
| 1335 | $prog->($n++) if $is_interactive; | ||
| 1336 | $code->($_); | ||
| 1337 | } | ||
| 1338 | $prog->($n || 1); | ||
| 1339 | print OUT __PACKAGE__ . ": Done " if !$is_interactive; | ||
| 1340 | print OUT "- " . (time - $start) . "s taken\n"; | ||
| 1341 | $| = $old_pipe; | ||
| 1342 | } | ||
| 1343 | |||
| 1344 | sub get_cover_progress { | ||
| 1345 | my ($type, @cvs) = @_; | ||
| 1346 | _report_progress("getting $type coverage", sub { get_cover($_) }, @cvs); | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | " | ||
| 1350 | We have normality, I repeat we have normality. | ||
| 1351 | Anything you still can’t cope with is therefore your own problem. | ||
| 1352 | " | ||
| 1353 | |||
| 1354 | __END__ |