blib/lib/Devel/XSCover/Gcov.pm

filter: Statementstmt 100% 232 / 232 Branchbran 100% 114 / 114 Conditioncond 100% 26 / 26 MC/DCmcdc 100% 16 / 16 Subroutinesub 100% 24 / 24 Podpod 100% 8 / 8 totaltotal 100% 420 / 420 CC 76 SCAR 25.2
CC
76
Cov
100%
parse
27.0
filter
13.0
run_flow
12.0
CRAP 12.5
« blib/lib/Devel/XSCover/Flow.pm ↑ Summary blib/lib/Devel/XSCover/Test.pm »
1 package Devel::XSCover::Gcov;
2  
3 43 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 43 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 43 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 43 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 43 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 43 ▶use IPC::Open3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 43 ▶use Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 43 ▶use Devel::XSCover::Flow ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 43 our $VERSION = '0.09';
17  
18 83 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
19 83 my ($class, %args) = @_;
20 83 my $self = bless {
21 dir => '.',
22 gcov => ['gcov'],
23 ignore => [],
24 generated => [],
25 xsubs => {},
26 inverse => {},
27 skip => [qw(blib cover_db .git)],
28 save => undef,
29 conditions => 0,
30 flow => '',
31 dry => 0,
32 silent => 0,
33 }, $class;
34 83 $self->{$_} = $args{$_} for keys %args;
35 83 return $self;
36 }
37  
38 43 ▶sub find_gcda {
Subroutine find_gcda: called CC=1 SCAR=0.0
Pod documented
39 43 my ($self) = @_;
40 43 my %skip = map { $_ => 1 } @{ $self->{skip} };
41 43 my $top = _slashes($self->{dir});
42 43 my @found;
43 File::Find::find({
44 no_chdir => 1,
45 581 ▶ wanted => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
46 581 ▶ if (-d $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_) 150 431 581
47 150 my $base = (File::Spec->splitdir($_))[-1];
48 150 ▶ $File::Find::prune = 1 if $skip{$base} && _slashes($_) ne $top;
Branch: 1 branch
BranchTrueFalseTotal
if $skip{$base} and _slashes($_) ne $top 11 139 150
Condition100%3/3
$skip{$base} and _slashes($_) ne $top
!ll&&!rl&&r
137211
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
49 150 return;
50 }
51 431 ▶ push @found, _slashes(File::Spec->abs2rel($_, $self->{dir})) if /\.gcda\z/;
Branch: 1 branch
BranchTrueFalseTotal
if /\.gcda\z/u 42 389 431
52 },
53 43 }, $self->{dir});
54 43 return sort @found;
55 }
56  
57 25 ▶sub run_gcov {
Subroutine run_gcov: called CC=8 SCAR=20.8
Pod documented
58 25 my ($self, $gcda) = @_;
59 25 (my $stem = $gcda) =~ s/\.gcda\z//;
60 25 my $name = (File::Spec->splitpath($stem))[2];
61 25 ▶ my @cmd = (@{ $self->{gcov} }, '-abct', ($self->{conditions} ? '--conditions' : ()), '-o', $gcda, $name);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'conditions'} ? : 1 24 25
62 25 $self->_say("xs_cover: running @cmd");
63 25 ▶ return '' if $self->{dry};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dry"} 5 20 25
64 20 my $here = Cwd::getcwd();
65 20 ▶ chdir $self->{dir} or die "xs_cover: cannot chdir to $self->{dir}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"dir"} or die qq[xs_cover: cannot chdir to $self->{"dir"}: $!\n] 19 1 20
66 19 my $text = $self->_capture(@cmd);
67 19 chdir $here;
68 19 ▶ die "xs_cover: @cmd produced nothing in $self->{dir}\n" unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 3 16 19
Condition100%3/3
defined $text and length $text
!ll&&!rl&&r
2116
MC/DC100%2/2
defined $text and length $text
defined $text length $text
Truth table100%3/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
69 16 ▶ if ($self->{save}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save"}) 14 2 16
70 14 File::Path::make_path($self->{save});
71 14 (my $flat = $stem) =~ s{[/\\]}{#}g;
72 14 my $path = File::Spec->catfile($self->{save}, "$flat.gcov.txt");
73 14 ▶ open my $fh, '>', $path or die "xs_cover: cannot write $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or die "xs_cover: cannot write ${path}: $!\n" 13 1 14
74 13 print $fh $text;
75 13 close $fh;
76 }
77 15 return $text;
78 }
79  
80 25 ▶sub run_flow {
Subroutine run_flow: called CC=12 SCAR=24.8
Pod documented
81 25 my ($self, $gcda) = @_;
82 25 ▶ my $kind = $self->{flow} or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $kind = $self->{"flow"} or return "" 12 13 25
83 12 (my $stem = $gcda) =~ s/\.gcda\z//;
84 12 my $name = (File::Spec->splitpath($stem))[2];
85 my @cmd = $kind eq 'llvm'
86 7 ? (@{ $self->{gcov} }, '--dump', '-n', '-o', $gcda, $name)
87 12 ▶ : (@{ $self->{gcov} }, '-abct', '--json-format', ($self->{conditions} ? '--conditions' : ()), '-o', $gcda, $name);
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'llvm' ? : 7 5 12
$self->{'conditions'} ? : 1 4 5
88 12 $self->_say("xs_cover: running @cmd");
89 12 ▶ return '' if $self->{dry};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dry"} 2 10 12
90 10 my $here = Cwd::getcwd();
91 10 ▶ chdir $self->{dir} or die "xs_cover: cannot chdir to $self->{dir}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"dir"} or die qq[xs_cover: cannot chdir to $self->{"dir"}: $!\n] 9 1 10
92 9 ▶ my $text = $kind eq 'llvm' ? $self->_capture_stderr(@cmd) : $self->_capture(@cmd);
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'llvm' ? : 5 4 9
93 9 chdir $here;
94 9 ▶ unless (defined $text && length $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text and length $text) 3 6 9
Condition100%3/3
defined $text and length $text
!ll&&!rl&&r
216
MC/DC100%2/2
defined $text and length $text
defined $text length $text
Truth table100%3/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
95 3 warn "xs_cover: @cmd produced no flow graph in $self->{dir}, branches come from gcov's blocks alone\n";
96 3 return '';
97 }
98 6 ▶ if ($self->{save}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save"}) 3 3 6
99 3 File::Path::make_path($self->{save});
100 3 (my $flat = $stem) =~ s{[/\\]}{#}g;
101 3 ▶ my $path = File::Spec->catfile($self->{save}, "$flat.flow." . ($kind eq 'llvm' ? 'txt' : 'json'));
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'llvm' ? : 1 2 3
102 3 ▶ open my $fh, '>', $path or die "xs_cover: cannot write $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or die "xs_cover: cannot write ${path}: $!\n" 2 1 3
103 2 print $fh $text;
104 2 close $fh;
105 }
106 5 return $text;
107 }
108  
109 18 ▶sub attach_decisions {
Subroutine attach_decisions: called CC=6 SCAR=17.9
Pod documented
110 18 my ($self, $parsed, $text) = @_;
111 18 ▶ return 0 unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 14 4 18
Condition100%3/3
defined $text and length $text
!ll&&!rl&&r
1134
MC/DC100%2/2
defined $text and length $text
defined $text length $text
Truth table100%3/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
112 4 my $flow = Devel::XSCover::Flow->new(dir => $self->{dir}, silent => $self->{silent});
113 4 ▶ my $graph = $self->{flow} eq 'llvm' ? $flow->parse_llvm($text, $parsed, $self->{xsubs}) : $flow->parse_gcc($text);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'flow'} eq 'llvm' ? : 3 1 4
114 4 my $decisions = $flow->decisions($graph, $self->{inverse});
115 4 my $n = 0;
116 4 for my $file (keys %$decisions) {
117 8 ▶ next unless $parsed->{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed->{$file} 1 7 8
118 7 $parsed->{$file}{decisions} = $decisions->{$file};
119 7 $n++;
120 }
121 4 return $n;
122 }
123  
124 48 ▶sub parse {
Subroutine parse: called CC=27 SCAR=33.0
Pod documented
125 48 my ($self, $text) = @_;
126 48 my %files;
127 48 my ($file, $pending, $block, $last_line, $decision);
128 48 for my $line (split /\n/, $text) {
129 113873 ▶ if ($line =~ /^\s*-:\s*0:Source:(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*-:\s*0:Source:(.*?)\s*$/u) 155 113718 113873
130 155 $file = $1;
131 155 ▶ $files{$file} //= { lines => {}, functions => [], blocks => [], conditions => [] };
Condition100%2/2
$files{$file} //= {"lines", {}, "functions", [], "blocks", [], "conditions", []}
l!l
1154
MC/DC100%1/1
$files{$file} //= {"lines", {}, "functions", [], "blocks", [], "conditions", []}
$files{$file}
Truth table100%2/2
A
A= $files{$file}
Aresult
11
00
132 155 ($pending, $block, $last_line, $decision) = (undef, undef, undef, undef);
133 155 next;
134 }
135 113718 ▶ next unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 4 113714 113718
136 113714 ▶ next if $line =~ /^\s*-:\s*0:/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*-:\s*0:/u 451 113263 113714
137 113263 ▶ if ($line =~ /^function\s+(\S+)\s+called\s+(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^function\s+(\S+)\s+called\s+(\d+)/u) 332 112931 113263
138 332 $pending = { name => $1, count => $2 };
139 332 next;
140 }
141 112931 ▶ if ($line =~ /^\s*\S+:\s*(\d+)-block\s+\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*\S+:\s*(\d+)-block\s+\d+/u) 2948 109983 112931
142 2948 $block = { line => $1, arms => [] };
143 2948 push @{ $files{$file}{blocks} }, $block;
144 2948 next;
145 }
146 109983 ▶ if ($line =~ /^branch\s+\d+\s+(?:taken\s+(\d+)%?|never executed)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^branch\s+\d+\s+(?:taken\s+(\d+)%?|never executed)/u) 1606 108377 109983
147 1606 ▶ my $count = $1 // 0;
Condition100%2/2
$1 // 0
l!l
1382224
MC/DC100%1/1
$1 // 0
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
148 1606 ▶ unless ($block) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block) 54 1552 1606
149 54 ▶ $block = { line => $last_line // 0, arms => [] };
Condition100%2/2
$last_line // 0
l!l
531
MC/DC100%1/1
$last_line // 0
$last_line
Truth table100%2/2
A
A= $last_line
Aresult
11
00
150 54 push @{ $files{$file}{blocks} }, $block;
151 }
152 1606 push @{ $block->{arms} }, $count;
153 1606 next;
154 }
155 108377 ▶ if ($line =~ /^condition outcomes covered (\d+)\/(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^condition outcomes covered (\d+)/(\d+)]u) 111 108266 108377
156 111 ▶ $decision = { line => $last_line // 0, covered => $1, total => $2, missing => {} };
Condition100%2/2
$last_line // 0
l!l
1101
MC/DC100%1/1
$last_line // 0
$last_line
Truth table100%2/2
A
A= $last_line
Aresult
11
00
157 111 push @{ $files{$file}{conditions} }, $decision;
158 111 next;
159 }
160 108266 ▶ if ($line =~ /^condition\s+(\d+) not covered \(([^)]+)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^condition\s+(\d+) not covered \(([^)]+)\)/u) 102 108164 108266
161 102 $decision->{missing}{$1}{$_} = 1 for split ' ', $2;
162 102 next;
163 }
164 108164 ▶ next if $line =~ /^(?:call|unconditional)\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^(?:call|unconditional)\s/u 166 107998 108164
165 107998 ▶ if ($line =~ /^\s*([^:\s]+):\s*(\d+):/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*([^:\s]+):\s*(\d+):/u) 107997 1 107998
166 107997 my ($count, $number) = ($1, $2);
167 107997 $last_line = $number;
168 107997 $block = undef;
169 107997 ▶ if ($pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending) 332 107665 107997
170 332 $pending->{line} = $number;
171 332 push @{ $files{$file}{functions} }, $pending;
172 332 $pending = undef;
173 }
174 107997 ▶ next if $count eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $count eq "-" 104773 3224 107997
175 3224 ▶ $count = 0 if $count eq '#####' || $count eq '=====';
Branch: 1 branch
BranchTrueFalseTotal
if $count eq "#####" or $count eq "=====" 644 2580 3224
Condition100%3/3
$count eq "#####" or $count eq "====="
l!l&&r!l&&!r
64222580
MC/DC100%2/2
$count eq "#####" or $count eq "====="
$count eq "#####" $count eq "====="
Truth table100%3/3
A or B
A= $count eq "#####"B= $count eq "====="
ABresult
1X1
011
000
176 3224 $count =~ s/\*\z//;
177 3224 ▶ next unless $count =~ /^\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $count =~ /^\d+\z/u 1 3223 3224
178 3223 $files{$file}{lines}{$number} = $count;
179 }
180 }
181 48 for my $f (values %files) {
182 154 $f->{blocks} = [ grep { @{ $_->{arms} } } @{ $f->{blocks} } ];
183 154 for my $d (@{ $f->{conditions} }) {
184 111 my $missing = delete $d->{missing};
185 111 ▶ $d->{terms} = [ map { { true => $missing->{$_}{true} ? 0 : 1, false => $missing->{$_}{false} ? 0 : 1 } } 0 .. int($d->{total} / 2) - 1 ];
Branch: 2 branches
BranchTrueFalseTotal
$missing->{$_}{'true'} ? : 77 44 121
$missing->{$_}{'false'} ? : 48 73 121
186 }
187 }
188 48 return \%files;
189 }
190  
191 35 ▶sub filter {
Subroutine filter: called CC=13 SCAR=25.6
Pod documented
192 35 my ($self, $files, $gcda) = @_;
193 35 my $root = Cwd::abs_path($self->{dir});
194 35 ▶ $root = File::Spec->rel2abs($self->{dir}) unless defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 1 34 35
195 35 $root = _slashes($root);
196 35 my %generated = map { $_ => 1 } @{ $self->{generated} };
197 35 my @ignore = map { qr/$_/ } @{ $self->{ignore} };
198 35 ▶ my ($vol, $beside) = defined $gcda ? File::Spec->splitpath($gcda) : ('', '');
Branch: 1 branch
BranchTrueFalseTotal
defined $gcda ? : 14 21 35
199 35 my (%kept, %aside);
200 35 for my $name (sort keys %$files) {
201 123 my $rel = $name;
202 123 ▶ if (File::Spec->file_name_is_absolute($rel)) {
Branch: 2 branches
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($rel)) { } 37 86 123
elsif (length $beside and not -f 'File::Spec'->catfile($self->{'dir'}, $rel)) { } 3 158 161
203 37 my $abs = Cwd::abs_path($rel);
204 37 ▶ $abs = $rel unless defined $abs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $abs 31 6 37
205 37 $abs = _slashes($abs);
206 37 ▶ next unless index($abs, "$root/") == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($abs, "$root/") == 0 33 4 37
207 4 $rel = File::Spec->abs2rel($abs, $root);
208 }
209 ▶ elsif (length $beside && !-f File::Spec->catfile($self->{dir}, $rel)) {
Condition100%3/3
length $beside and not -f "File::Spec"->catfile($self->{"dir"}, $rel)
!ll&&!rl&&r
7583
MC/DC100%2/2
length $beside and not -f "File::Spec"->catfile($self->{"dir"}, $rel)
length $beside not -f "File::Spec"->catfile($self->{"dir"}, $rel)
Truth table100%3/3
A and not(B)
A= length $besideB= not -f "File::Spec"->catfile($self->{"dir"}, $rel)
ABresult
0X0
100
111
210 3 my $near = File::Spec->catpath($vol, $beside, $rel);
211 3 ▶ $rel = $near if -f File::Spec->catfile($self->{dir}, $near);
Branch: 1 branch
BranchTrueFalseTotal
if -f "File::Spec"->catfile($self->{"dir"}, $near) 1 2 3
212 }
213 90 $rel =~ s{^\./+}{};
214 90 $rel = _slashes(File::Spec->canonpath($rel));
215 90 ▶ next if (File::Spec->splitpath($rel))[2] eq 'ppport.h';
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->splitpath($rel))[2] eq "ppport.h" 4 86 90
216 86 ▶ if ($generated{$rel}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($generated{$rel}) 12 74 86
217 12 $aside{$rel} = $files->{$name};
218 12 next;
219 }
220 74 ▶ next if grep { $rel =~ $_ } @ignore;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$rel =~ /$_/u;} @ignore 2 72 74
221 72 $kept{$rel} = $files->{$name};
222 }
223 35 return (\%kept, \%aside);
224 }
225  
226 13 ▶sub read {
Subroutine read: called CC=4 SCAR=13.9
Pod documented
227 13 my ($self) = @_;
228 13 my @objects;
229 13 for my $gcda ($self->find_gcda) {
230 15 my $text = $self->run_gcov($gcda);
231 15 ▶ next unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 2 13 15
232 13 my $parsed = $self->parse($text);
233 13 $self->attach_decisions($parsed, $self->run_flow($gcda));
234 13 my ($files, $generated) = $self->filter($parsed, $gcda);
235 13 push @objects, { object => $gcda, files => $files, generated => $generated };
236 }
237 13 ▶ $self->_say(sprintf 'xs_cover: read %d object%s', scalar @objects, @objects == 1 ? '' : 's');
Branch: 1 branch
BranchTrueFalseTotal
@objects == 1 ? : 7 6 13
238 13 return @objects;
239 }
240  
241 22 ▶sub _capture {
Subroutine _capture: called CC=1 SCAR=0.0
242 22 my ($self, @cmd) = @_;
243 22 return $self->_run_capture(0, @cmd);
244 }
245  
246 4 ▶sub _capture_stderr {
Subroutine _capture_stderr: called CC=1 SCAR=0.0
247 4 my ($self, @cmd) = @_;
248 4 return $self->_run_capture(1, @cmd);
249 }
250  
251 26 ▶sub _run_capture {
Subroutine _run_capture: called CC=5 SCAR=16.1
252 26 my ($self, $stderr, @cmd) = @_;
253 26 local *NULL;
254 26 open NULL, '>', File::Spec->devnull;
255 26 my ($in, $out, $err) = (undef, Symbol::gensym(), Symbol::gensym());
256 26 ▶ my $pid = eval { IPC::Open3::open3($in, $stderr ? '>&NULL' : $out, $stderr ? $err : '>&NULL', @cmd) };
Branch: 2 branches
BranchTrueFalseTotal
$stderr ? : 4 22 26
$stderr ? : 4 22 26
257 26 ▶ return undef unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 2 24 26
258 24 close $in;
259 24 ▶ my $fh = $stderr ? $err : $out;
Branch: 1 branch
BranchTrueFalseTotal
$stderr ? : 3 21 24
260 24 my $text = do { local $/; <$fh> };
261 24 close $fh;
262 24 waitpid $pid, 0;
263 24 return $text;
264 }
265  
266 260 ▶sub _slashes {
Subroutine _slashes: called CC=2 SCAR=6.9
267 260 my ($path) = @_;
268 260 ▶ $path =~ s{\\}{/}g if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 8 252 260
269 260 return $path;
270 }
271  
272 50 ▶sub _say {
Subroutine _say: called CC=2 SCAR=6.9
273 50 my ($self, $line) = @_;
274 50 ▶ print "$line\n" unless $self->{silent};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"silent"} 28 22 50
275 }
276  
277 43 1;
278  
279 __END__
« blib/lib/Devel/XSCover/Flow.pm ↑ Summary blib/lib/Devel/XSCover/Test.pm »