blib/lib/Devel/XSCover/XS.pm

filter: Statementstmt 100% 185 / 185 Branchbran 100% 86 / 86 Conditioncond 87.5% 28 / 32 MC/DCmcdc 78.9% 15 / 19 Subroutinesub 100% 22 / 22 Podpod 100% 11 / 11 totaltotal 97.7% 347 / 355 CC 66 SCAR 19.1
CC
66
Cov
99%
map
12.0
declaration_line
10.0
find_declaration
9.0
CRAP 6.7
« blib/lib/Devel/XSCover/Test.pm ↑ Summary blib/lib/Devel/XSCover/Build.pm »
1 package Devel::XSCover::XS;
2  
3 33 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 33 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 33 our $VERSION = '0.08';
11  
12 33 our ($KEYWORDS);
13  
14 33 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 33 $KEYWORDS = qr/^\s*(?:CODE|PPCODE|PREINIT|INPUT|INIT|ALIAS|OUTPUT|PROTOTYPE|C_ARGS|CLEANUP|POSTCALL|OVERLOAD|FALLBACK|ATTRS|INTERFACE|INTERFACE_MACRO|SCOPE|CASE|NO_INIT|NO_OUTPUT|PROTOTYPES|VERSIONCHECK|EXPORT_XSUB_SYMBOLS|REQUIRE|BOOT|INCLUDE|INCLUDE_COMMAND)\s*:/;
16 }
17  
18 36 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
19 36 my ($class, %args) = @_;
20 36 my $self = bless {
21 dir => '.',
22 skip => [qw(blib cover_db .git)],
23 }, $class;
24 36 $self->{$_} = $args{$_} for keys %args;
25 36 return $self;
26 }
27  
28 23 ▶sub generated_files {
Subroutine generated_files: called CC=4 SCAR=13.9
Pod documented
29 23 my ($self) = @_;
30 23 my %pair;
31 23 my $pl = File::Spec->catfile($self->{dir}, 'Makefile.PL');
32 23 ▶ if (open my $fh, '<', $pl) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $pl) 13 10 23
33 13 my $text = do { local $/; <$fh> };
34 13 close $fh;
35 13 ▶ while ($text =~ /\bXS\s*=>\s*\{(.*?)\}/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\bXS\s*=>\s*\{(.*?)\}/gsu) 3 13 16
36 3 my $body = $1;
37 3 ▶ while ($body =~ /(['"])([^'"]+)\1\s*=>\s*(['"])([^'"]+)\3/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /(['"])([^'"]+)\1\s*=>\s*(['"])([^'"]+)\3/gu) 3 3 6
38 3 $pair{ $self->_canon($2) } = $self->_canon($4);
39 }
40 }
41 }
42 23 my %skip = map { $_ => 1 } @{ $self->{skip} };
43 23 my $top = _slashes($self->{dir});
44 File::Find::find({
45 no_chdir => 1,
46 251 ▶ wanted => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
47 251 ▶ if (-d $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_) 76 175 251
48 76 my $base = (File::Spec->splitdir($_))[-1];
49 76 ▶ $File::Find::prune = 1 if $skip{$base} && _slashes($_) ne $top;
Branch: 1 branch
BranchTrueFalseTotal
if $skip{$base} and _slashes($_) ne $top 1 75 76
Condition100%3/3
$skip{$base} and _slashes($_) ne $top
!ll&&!rl&&r
7411
MC/DC100%2/2
$skip{$base} and _slashes($_) ne $top
$skip{$base} _slashes($_) ne $top
Truth table100%3/3
A and B
A= $skip{$base}B= _slashes($_) ne $top
ABresult
0X0
100
111
50 76 return;
51 }
52 175 ▶ return unless /\.xs\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\.xs\z/u 138 37 175
53 37 my $rel = _slashes(File::Spec->abs2rel($_, $self->{dir}));
54 37 (my $c = $rel) =~ s/\.xs\z/.c/;
55 37 ▶ $pair{$rel} = $c unless defined $pair{$rel};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pair{$rel} 34 3 37
56 },
57 23 }, $self->{dir});
58 15 return map { [ $_, $pair{$_} ] }
59 23 grep { -f File::Spec->catfile($self->{dir}, $pair{$_}) } sort keys %pair;
60 }
61  
62 24 ▶sub line_map {
Subroutine line_map: called CC=8 SCAR=20.8
Pod documented
63 24 my ($self, $c) = @_;
64 24 my @map;
65 24 my $own = $self->_canon($c);
66 24 my ($file, $line, $said, $dropped) = ($own, 1, 1, undef);
67 24 my %inverse;
68 24 my $n = 0;
69 24 for my $text ($self->_lines($c)) {
70 10892 $n++;
71 10892 ▶ if ($text =~ /^#line\s+(\d+)\s+"([^"]*)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^#line\s+(\d+)\s+"([^"]*)"/u) 438 10454 10892
72 438 $map[$n] = [ $own, $n ];
73 438 ($file, $line, $said) = ($self->resolve($c, $2), $1, $1);
74 438 ▶ $dropped = $file eq $own ? undef : $self->_dropped($file);
Branch: 1 branch
BranchTrueFalseTotal
$file eq $own ? : 229 209 438
75 438 next;
76 }
77 10454 ▶ $line++ while $dropped && $dropped->{$line};
Branch: 1 branch
BranchTrueFalseTotal
$dropped and $dropped->{$line} and do { ++$line } 4 10454 10458
Condition100%3/3
$dropped and $dropped->{$line}
!ll&&!rl&&r
747129834
MC/DC100%2/2
$dropped and $dropped->{$line}
$dropped $dropped->{$line}
Truth table100%3/3
A and B
A= $droppedB= $dropped->{$line}
ABresult
0X0
100
111
78 10454 ▶ $inverse{$file}{$said} //= $line if $said != $line;
Branch: 1 branch
BranchTrueFalseTotal
if $said != $line 4 10450 10454
Condition33%1/3
$inverse{$file}{$said} //= $line
l!l&&r!l&&!r
040
MC/DC0%0/2
$inverse{$file}{$said} //= $line
$inverse{$file}{$said} $line
Truth table33%1/3
A //= B
A= $inverse{$file}{$said}B= $line
ABresult
1X1
011
000
79 10454 $map[$n] = [ $file, $line++ ];
80 10454 $said++;
81 }
82 23 $self->{inverse}{$own} = \%inverse;
83 23 return \@map;
84 }
85  
86 209 ▶sub _dropped {
Subroutine _dropped: called CC=4 SCAR=13.9
87 209 my ($self, $file) = @_;
88 209 ▶ return $self->{dropped}{$file} if exists $self->{dropped}{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"dropped"}{$file} 173 36 209
89 36 my %dropped;
90 36 ▶ my $path = File::Spec->file_name_is_absolute($file) ? $file : File::Spec->catfile($self->{dir}, $file);
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($file) ? : 1 35 36
91 36 ▶ if (-f $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path) 35 1 36
92 35 require Devel::XSCover::Flow;
93 35 %dropped = %{ Devel::XSCover::Flow->new->dropped([ $self->_lines($file) ]) };
94 }
95 36 return $self->{dropped}{$file} = \%dropped;
96 }
97  
98 22 ▶sub xsubs {
Subroutine xsubs: called CC=8 SCAR=20.8
Pod documented
99 22 my ($self, $c) = @_;
100 22 my @lines = $self->_lines($c);
101 22 my $canon = $self->_canon($c);
102 22 my @xsubs;
103 22 for my $i (0 .. $#lines) {
104 10120 ▶ next unless $lines[$i] =~ /^XS(?:_EUPXS|_EXTERNAL|_INTERNAL)?\((\w+)\)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines[$i] =~ /^XS(?:_EUPXS|_EXTERNAL|_INTERNAL)?\((\w+)\)\s*$/u 9916 204 10120
105 204 my $name = $1;
106 204 ▶ next if $name =~ /^boot_/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^boot_/u 21 183 204
107 183 my %xsub = (c_name => $name, c_line => $i + 1);
108 183 for my $j ($i + 1 .. $#lines) {
109 1836 ▶ last if $lines[$j] =~ /^XS(?:_EUPXS|_EXTERNAL|_INTERNAL)?\(\w+\)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$j] =~ /^XS(?:_EUPXS|_EXTERNAL|_INTERNAL)?\(\w+\)\s*$/u 6 1830 1836
110 1830 ▶ next unless $lines[$j] =~ /^#line\s+(\d+)\s+"([^"]*)"/;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines[$j] =~ /^#line\s+(\d+)\s+"([^"]*)"/u 1651 179 1830
111 179 my $file = $self->resolve($c, $2);
112 179 ▶ next if $file eq $canon;
Branch: 1 branch
BranchTrueFalseTotal
if $file eq $canon 2 177 179
113 177 @xsub{qw(file body_line)} = ($file, $1);
114 177 last;
115 }
116 183 push @xsubs, \%xsub;
117 }
118 22 return @xsubs;
119 }
120  
121 22 ▶sub perl_names {
Subroutine perl_names: called CC=3 SCAR=11.0
Pod documented
122 22 my ($self, $c) = @_;
123 22 my %names;
124 22 for my $text ($self->_lines($c)) {
125 10433 ▶ next unless $text =~ /newXS\w*\s*\(\s*"([^"]+)"\s*,\s*(XS_\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /newXS\w*\s*\(\s*"([^"]+)"\s*,\s*(XS_\w+)/u 10233 200 10433
126 200 push @{ $names{$2} }, $1;
127 }
128 22 return \%names;
129 }
130  
131 162 ▶sub declaration_line {
Subroutine declaration_line: called CC=10 SCAR=23.0
Pod documented
132 162 my ($self, $file, $body_line, $sub) = @_;
133 162 my @lines = $self->_lines($file);
134 162 ▶ for (my $i = $body_line - 2; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 546 2 548
135 546 my $text = $lines[$i];
136 546 ▶ next if $text =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^\s*$/u 4 542 546
137 542 ▶ next if $text =~ $KEYWORDS;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /$Devel::XSCover::XS::KEYWORDS/u 176 366 542
138 366 ▶ next if $text =~ /^\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^\s/u 197 169 366
139 169 ▶ next if $text =~ /^[{}]/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^[{}]/u 4 165 169
140 165 ▶ return undef if $text =~ /^MODULE\s*=/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^MODULE\s*=/u 1 164 165
141 164 ▶ if ($text =~ /^(\w+)\s*\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^(\w+)\s*\(/u) 159 5 164
142 159 ▶ return $i + 1 if !defined $sub || $1 eq $sub;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $sub or $1 eq $sub 156 3 159
Condition100%3/3
not defined $sub or $1 eq $sub
l!l&&r!l&&!r
21543
MC/DC100%2/2
not defined $sub or $1 eq $sub
not defined $sub $1 eq $sub
Truth table100%3/3
not(A) or B
A= not defined $subB= $1 eq $sub
ABresult
1X1
011
000
143 3 return undef;
144 }
145 }
146 2 return undef;
147 }
148  
149 10 ▶sub find_declaration {
Subroutine find_declaration: called CC=9 SCAR=22.0
Pod documented
150 10 my ($self, $files, $perl_name) = @_;
151 10 my ($package, $sub) = $perl_name =~ /^(?:(.*)::)?(\w+)$/;
152 10 ▶ $package //= '';
Condition100%2/2
$package //= ""
l!l
82
MC/DC100%1/1
$package //= ""
$package
Truth table100%2/2
A
A= $package
Aresult
11
00
153 10 for my $file (@$files) {
154 18 my $current = '';
155 18 my $n = 0;
156 18 for my $text ($self->_lines($file)) {
157 513 $n++;
158 513 ▶ if ($text =~ /^MODULE\s*=\s*\S+(?:\s+PACKAGE\s*=\s*(\S+))?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^MODULE\s*=\s*\S+(?:\s+PACKAGE\s*=\s*(\S+))?/u) 11 502 513
159 11 ▶ $current = $1 // '';
Condition50%1/2
$1 // ""
l!l
110
MC/DC0%0/1
$1 // ""
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
160 11 next;
161 }
162 502 ▶ return ($file, $n) if $text =~ /^\Q$sub\E\s*\(/ && ($current eq $package || $current eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^\Q$sub\E\s*\(/u and $current eq $package || $current eq "" 5 497 502
Condition100%6/6
$text =~ /^\Q$sub\E\s*\(/u and $current eq $package || $current eq ""
!ll&&!rl&&r
49525
$current eq $package or $current eq ""
l!l&&r!l&&!r
322
MC/DC100%3/3
$text =~ /^\Q$sub\E\s*\(/u and $current eq $package || $current eq ""
$text =~ /^\Q$sub\E\s*\(/u $current eq $package $current eq ""
Truth table100%4/4
A and B or C
A= $text =~ /^\Q$sub\E\s*\(/uB= $current eq $packageC= $current eq ""
ABCresult
0XX0
1000
11X1
1011
163 }
164 }
165 5 return;
166 }
167  
168 19 ▶sub map {
Subroutine map: called CC=12 SCAR=24.9
Pod documented
169 19 my ($self, $c) = @_;
170 19 my $lines = $self->line_map($c);
171 19 my $canon = $self->_canon($c);
172 19 my %files;
173 19 for my $entry (@$lines) {
174 8286 ▶ next unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 19 8267 8286
175 8267 ▶ $files{ $entry->[0] }++ unless $entry->[0] eq $canon;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->[0] eq $canon 1758 6509 8267
176 }
177 19 my @files = sort keys %files;
178 19 my $names = $self->perl_names($c);
179 19 my %xsubs;
180 19 for my $xsub ($self->xsubs($c)) {
181 159 ▶ my @names = @{ $names->{ $xsub->{c_name} } || [] };
Condition100%2/2
$$names{$xsub->{'c_name'}} || []
l!l
1572
MC/DC100%1/1
$$names{$xsub->{'c_name'}} || []
$$names{$xsub->{'c_name'}}
Truth table100%2/2
A
A= $$names{$xsub->{'c_name'}}
Aresult
11
00
182 159 ▶ my $sub = @names ? ($names[0] =~ /(\w+)$/)[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@names ? : 157 2 159
183 159 my $line;
184 159 ▶ if ($xsub->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsub->{"file"}) 156 3 159
185 156 $line = $self->declaration_line($xsub->{file}, $xsub->{body_line}, $sub);
186 }
187 159 ▶ if (!defined $line && @names) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $line and @names) 4 155 159
Condition100%3/3
not defined $line and @names
!ll&&!rl&&r
15414
MC/DC100%2/2
not defined $line and @names
not defined $line @names
Truth table100%3/3
not(A) and B
A= not defined $lineB= @names
ABresult
0X0
100
111
188 4 my ($file, $found) = $self->find_declaration(\@files, $names[0]);
189 4 ▶ @{$xsub}{qw(file body_line)} = ($file, undef) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 2 2 4
190 4 $line = $found;
191 }
192 $xsubs{ $xsub->{c_name} } = {
193 159 %$xsub,
194 names => \@names,
195 line => $line,
196 };
197 }
198 19 ▶ return { c => $canon, inverse => $self->{inverse}{$canon} || {}, files => \@files, lines => $lines, xsubs => \%xsubs };
Condition50%1/2
$self->{'inverse'}{$canon} || {}
l!l
190
MC/DC0%0/1
$self->{'inverse'}{$canon} || {}
$self->{'inverse'}{$canon}
Truth table50%1/2
A
A= $self->{'inverse'}{$canon}
Aresult
11
00
199 }
200  
201 6 ▶sub subroutines {
Subroutine subroutines: called CC=4 SCAR=13.9
Pod documented
202 6 my ($self, $map) = @_;
203 6 my @subs;
204 6 for my $c_name (sort keys %{ $map->{xsubs} }) {
205 37 my $x = $map->{xsubs}{$c_name};
206 37 ▶ next unless defined $x->{line} && @{ $x->{names} };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x->{"line"} and @{$x->{"names"};} 4 33 37
Condition100%3/3
defined $x->{"line"} and @{$x->{"names"};}
!ll&&!rl&&r
3133
MC/DC100%2/2
defined $x->{"line"} and @{$x->{"names"};}
defined $x->{"line"} @{$x->{"names"};}
Truth table100%3/3
A and B
A= defined $x->{"line"}B= @{$x->{"names"};}
ABresult
0X0
100
111
207 33 push @subs, { c_name => $c_name, file => $x->{file}, line => $x->{line}, names => $x->{names} };
208 }
209 6 return @subs;
210 }
211  
212 19 ▶sub read {
Subroutine read: called CC=1 SCAR=0.0
Pod documented
213 19 my ($self) = @_;
214 19 return map { $self->map($_->[1]) } $self->generated_files;
215 }
216  
217 283 ▶sub _lines {
Subroutine _lines: called CC=3 SCAR=11.0
218 283 my ($self, $rel) = @_;
219 283 ▶ my $path = File::Spec->file_name_is_absolute($rel) ? $rel : File::Spec->catfile($self->{dir}, $rel);
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($rel) ? : 2 281 283
220 283 ▶ open my $fh, '<', $path or die "xs_cover: cannot read $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "xs_cover: cannot read ${path}: $!\n" 282 1 283
221 282 my @lines = <$fh>;
222 282 close $fh;
223 282 chomp @lines;
224 282 return @lines;
225 }
226  
227 621 ▶sub resolve {
Subroutine resolve: called CC=4 SCAR=13.9
Pod documented
228 621 my ($self, $c, $name) = @_;
229 621 my $canon = $self->_canon($name);
230 621 ▶ return $canon if File::Spec->file_name_is_absolute($name);
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($name) 3 618 621
231 618 ▶ return $canon if -f File::Spec->catfile($self->{dir}, $canon);
Branch: 1 branch
BranchTrueFalseTotal
if -f "File::Spec"->catfile($self->{"dir"}, $canon) 604 14 618
232 14 my ($vol, $dirs) = File::Spec->splitpath($c);
233 14 my $beside = $self->_canon(File::Spec->catpath($vol, $dirs, $name));
234 14 ▶ return -f File::Spec->catfile($self->{dir}, $beside) ? $beside : $canon;
Branch: 1 branch
BranchTrueFalseTotal
-f 'File::Spec'->catfile($self->{'dir'}, $beside) ? : 12 2 14
235 }
236  
237 706 ▶sub _canon {
Subroutine _canon: called CC=1 SCAR=0.0
238 706 my ($self, $name) = @_;
239 706 $name =~ s{^\./+}{};
240 706 return _slashes(File::Spec->canonpath($name));
241 }
242  
243 768 ▶sub _slashes {
Subroutine _slashes: called CC=2 SCAR=6.9
244 768 my ($path) = @_;
245 768 ▶ $path =~ s{\\}{/}g if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 5 763 768
246 768 return $path;
247 }
248  
249 33 1;
250  
251 __END__
« blib/lib/Devel/XSCover/Test.pm ↑ Summary blib/lib/Devel/XSCover/Build.pm »