blib/lib/Devel/StatProfiler/SourceMap.pm

filter: Statementstmt 84.1% 90 / 107 Branchbran 54.3% 25 / 46 Conditioncond 65.0% 13 / 20 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 90.0% 18 / 20 Podpod 0.0% 0 / 10 totaltotal 69.9% 149 / 213 CC 37 SCAR 16.4
CC
37
Cov
74%
get_reverse_mapping
12.0
add_file_mapping
7.3
end_file_mapping
7.3
CRAP 5.2
« blib/lib/Devel/StatProfiler/Utils.pm ↑ Summary blib/lib/Devel/StatProfiler/Html.pm »
1 package Devel::StatProfiler::SourceMap;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use Devel::StatProfiler::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 check_serializer
8 read_data
9 state_dir
10 utf8_sha1_hex
11 write_data_any
12 24 );
13 24 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 24 my ($SEREAL_ENCODER, $SEREAL_DECODER);
17  
18 1966 ▶sub _check_serializer {
Subroutine _check_serializer: called CC=3 SCAR=16.7
19 1966 my ($serializer) = @_;
20  
21 1966 ▶ if ($serializer eq 'storable') {
Branch: 2 branches
BranchTrueFalseTotal
if ($serializer eq 'storable') { } 1966 0 1966
elsif ($serializer eq 'sereal') { } 0 0 0
22 1966 require Storable;
23 } elsif ($serializer eq 'sereal') {
24 0 require Sereal;
25  
26 0 $SEREAL_ENCODER = Sereal::Encoder->new({
27 dedupe_strings => 1,
28 });
29 0 $SEREAL_DECODER = Sereal::Decoder->new;
30 } else {
31 0 die "Unsupported serializer format '$serializer'";
32 }
33 }
34  
35 1966 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
36 1966 my ($class, %opts) = @_;
37 my $self = bless {
38 map => {},
39 reverse_map => {},
40 current_map => undef,
41 current_file => undef,
42 current_mapping => undef,
43 ignore_mapping => 0,
44 root_dir => $opts{root_directory},
45 shard => $opts{shard},
46 1966 ▶ serializer => $opts{serializer} || 'storable',
Condition100%2/2
$opts{'serializer'} || 'storable'
l!l
101956
MC/DC100%1/1
$opts{'serializer'} || 'storable'
$opts{'serializer'}
Truth table100%2/2
A
A= $opts{'serializer'}
Aresult
11
00
47 }, $class;
48  
49 1966 _check_serializer($self->{serializer});
50 1966 check_serializer($self->{serializer});
51  
52 1966 return $self;
53 }
54  
55 8 ▶sub start_file_mapping {
Subroutine start_file_mapping: called CC=3 SCAR=11.8
Pod undocumented
56 8 my ($self, $physical_file) = @_;
57 8 ▶ die "Causal failure for '$physical_file'" if $self->{current_map};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"current_map"} 0 8 8
58  
59 8 ▶ if ($self->{map}{$physical_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"map"}{$physical_file}) 0 8 8
60 0 $self->{ignore_mapping} = 1;
61 0 return;
62 }
63  
64 8 $self->{ignore_mapping} = 0;
65 8 $self->{current_map} = [1, $physical_file, 1];
66 8 $self->{current_file} = $physical_file;
67 8 $self->{current_mapping} = [$self->{current_map}];
68 }
69  
70 8 ▶sub end_file_mapping {
Subroutine end_file_mapping: called CC=7 SCAR=19.8
Pod undocumented
71 8 my ($self, $physical_line) = @_;
72 my ($current_map, $current_file, $current_mapping) =
73 8 @{$self}{qw(current_map current_file current_mapping)};
74 8 $self->{current_map} = $self->{current_file} = $self->{current_mapping} = undef;
75  
76 8 ▶ return if $self->{ignore_mapping};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ignore_mapping"} 0 8 8
77  
78 8 ▶ return if $current_map &&
Branch: 1 branch
BranchTrueFalseTotal
if $current_map and $current_map->[0] == 1 and $current_map->[1] eq $current_file and $current_map->[2] == 1 1 7 8
Condition66%6/9
$current_map and $current_map->[0] == 1 and $current_map->[1] eq $current_file and $current_map->[2] == 1
!ll&&!rl&&r
701
$current_map and $current_map->[0] == 1 and $current_map->[1] eq $current_file
!ll&&!rl&&r
701
$current_map and $current_map->[0] == 1
!ll&&!rl&&r
071
MC/DC25%1/4
$current_map and $current_map->[0] == 1 and $current_map->[1] eq $current_file and $current_map->[2] == 1
$current_map $current_map->[0] == 1 $current_map->[1] eq $current_file $current_map->[2] == 1
Truth table40%2/5
A and B and C and D
A= $current_mapB= $current_map->[0] == 1C= $current_map->[1] eq $current_fileD= $current_map->[2] == 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
79 $current_map->[0] == 1 &&
80 $current_map->[1] eq $current_file &&
81 $current_map->[2] == 1;
82  
83 7 for my $entry (@$current_mapping) {
84 14 $self->{reverse_map}{$entry->[1]}{$current_file} = 1;
85 }
86  
87 # add last line
88 7 push @$current_mapping, [$physical_line + 1, undef, $physical_line + 1];
89  
90 7 $self->{map}{$current_file} = _encode($self, $current_mapping);
91 }
92  
93 10 ▶sub add_file_mapping {
Subroutine add_file_mapping: called CC=7 SCAR=19.9
Pod undocumented
94 10 my ($self, $physical_line, $mapped_file, $mapped_line) = @_;
95 10 ▶ return if $self->{ignore_mapping};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ignore_mapping"} 0 10 10
96  
97 10 ▶ die "Causal failure for '$self->{current_file}'" unless $self->{current_map};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"current_map"} 0 10 10
98  
99 10 my ($st, $en) = (substr($mapped_file, 0, 1), substr($mapped_file, -1, 1));
100 10 ▶ if ($st eq $en && ($st eq '"' || $st eq "'")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st eq $en and $st eq "\"" || $st eq "'") 4 6 10
Condition50%3/6
$st eq $en and $st eq "\"" || $st eq "'"
!ll&&!rl&&r
604
$st eq "\"" or $st eq "'"
l!l&&r!l&&!r
400
MC/DC33%1/3
$st eq $en and $st eq "\"" || $st eq "'"
$st eq $en $st eq "\"" $st eq "'"
Truth table50%2/4
A and B or C
A= $st eq $enB= $st eq "\""C= $st eq "'"
ABCresult
0XX0
1000
11X1
1011
101 4 $mapped_file = substr($mapped_file, 1, -1);
102 }
103  
104 10 ▶ if ($physical_line == $self->{current_map}[0] + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($physical_line == $self->{'current_map'}[0] + 1) { } 3 7 10
105 3 $self->{current_map} = [$physical_line, $mapped_file, 0 + $mapped_line];
106 3 $self->{current_mapping}->[-1] = $self->{current_map};
107 } else {
108 7 $self->{current_map} = [$physical_line, $mapped_file, 0 + $mapped_line];
109 7 push @{$self->{current_mapping}}, $self->{current_map};
110 }
111 }
112  
113 7999 ▶sub add_sources_from_reader {
Subroutine add_sources_from_reader: called CC=5 SCAR=16.1
Pod undocumented
114 7999 my ($self, $r) = @_;
115  
116 7999 my $source_code = $r->get_source_code;
117 7999 for my $name (keys %$source_code) {
118 281 ▶ next unless $source_code->{$name} =~ /^#line\s+\d+\s+/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_code->{$name} =~ /^#line\s+\d+\s+/m 275 6 281
119  
120 6 my $eval_name = 'eval:' . utf8_sha1_hex($source_code->{$name});
121  
122 6 ▶ next if $self->{map}{$eval_name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"map"}{$eval_name} 0 6 6
123  
124 6 $self->start_file_mapping($eval_name);
125  
126 6 ▶ while ($source_code->{$name} =~ /^#line\s+(\d+)\s+(.+?)$/mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_code->{$name} =~ /^#line\s+(\d+)\s+(.+?)$/gm) 6 6 12
127 6 my $line = substr($source_code->{$name}, 0, pos($source_code->{$name})) =~ tr/\n/\n/;
128 6 $self->add_file_mapping($line + 2, $2, $1);
129 }
130  
131 # a\nb\n -> 2 newlines, 2 lines, a\nb -> 1 newline, still 2 lines
132 $self->end_file_mapping($source_code->{$name} =~ tr/\n/\n/ +
133 6 (substr($source_code->{$name}, -1) ne "\n"));
134 }
135 }
136  
137 1945 ▶sub _save {
Subroutine _save: called CC=3 SCAR=11.0
138 1945 my ($self, $state_dir, $is_part) = @_;
139  
140 1945 ▶ $state_dir //= state_dir($self);
Condition66%2/3
$state_dir //= state_dir($self)
l!l&&r!l&&!r
194230
MC/DC0%0/2
$state_dir //= state_dir($self)
$state_dir state_dir($self)
Truth table66%2/3
A //= B
A= $state_dirB= state_dir($self)
ABresult
1X1
011
000
141 1945 File::Path::mkpath($state_dir);
142  
143 write_data_any($is_part, $self, $state_dir, 'sourcemap', $self->{map})
144 1945 ▶ if %{$self->{map}};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->{"map"};} 2 1943 1945
145 }
146  
147 1942 ▶sub save_part { $_[0]->_save($_[1], 1) }
Subroutine save_part: called CC=1 SCAR=0.0
Pod undocumented
148 3 ▶sub save_merged { $_[0]->_save(undef, 0) }
Subroutine save_merged: called CC=1 SCAR=0.0
Pod undocumented
149  
150 10 ▶sub load_and_merge {
Subroutine load_and_merge: called CC=2 SCAR=6.9
Pod undocumented
151 10 my ($self, @files) = @_;
152  
153 10 $self->_load_and_merge($_) for @files;
154 }
155  
156 2 ▶sub _load_and_merge {
Subroutine _load_and_merge: called CC=6 SCAR=17.9
157 2 my ($self, $file) = @_;
158 2 my $data = read_data($self->{serializer}, $file);
159  
160 # Backwards compatibility for releases before 0.47
161 2 for my $value (values %$data) {
162 # use aliasing to modify in-place
163 4 ▶ $value = _encode($self, $value) if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 4 4
164 }
165  
166 2 for my $key (keys %$data) {
167 4 $self->{map}{$key} = $data->{$key};
168  
169 4 for my $entry (@{_decode($self, $data->{$key})}) {
170 10 ▶ $self->{reverse_map}{$entry->[1]}{$key} = 1
Branch: 1 branch
BranchTrueFalseTotal
if $entry->[1] 6 4 10
171 if $entry->[1]; # skip sentinel entry for last line
172 }
173 }
174 }
175  
176 0 ▶sub get_mapping {
Subroutine get_mapping: not called CC=1 SCAR=6.9
Pod undocumented
177 0 my ($self, $file) = @_;
178  
179 0 return _decode($self, $self->{map}{$file});
180 }
181  
182 0 ▶sub get_reverse_mapping {
Subroutine get_reverse_mapping: not called CC=3 SCAR=24.8
Pod undocumented
183 0 my ($self, $file) = @_;
184  
185 0 ▶ $file = $2 if $file =~ m{^qeval:([0-9a-f]+)/(.+)$};
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[^qeval:([0-9a-f]+)/(.+)$] 0 0 0
186  
187 0 ▶ return unless $self->{reverse_map}{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"reverse_map"}{$file} 0 0 0
188 0 return (keys %{$self->{reverse_map}{$file}})[0];
189 }
190  
191 7 ▶sub _encode {
Subroutine _encode: called CC=3 SCAR=15.1
192 7 my ($self, $rows) = @_;
193  
194 7 ▶ if ($self->{serializer} eq 'storable') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'serializer'} eq 'storable') { } 7 0 7
elsif ($self->{'serializer'} eq 'sereal') { } 0 0 0
195 7 return Storable::nfreeze($rows);
196 } elsif ($self->{serializer} eq 'sereal') {
197 0 return $SEREAL_ENCODER->encode($rows);
198 } else {
199 0 die "Unsupported serializer format '$self->{serializer}'";
200 }
201 }
202  
203 15 ▶sub _decode {
Subroutine _decode: called CC=4 SCAR=17.9
204 15 my ($self, $blob) = @_;
205  
206 15 ▶ return undef unless defined $blob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $blob 0 15 15
207 15 ▶ if ($self->{serializer} eq 'storable') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'serializer'} eq 'storable') { } 15 0 15
elsif ($self->{'serializer'} eq 'sereal') { } 0 0 0
208 15 return Storable::thaw($blob);
209 } elsif ($self->{serializer} eq 'sereal') {
210 0 return $SEREAL_DECODER->decode($blob);
211 } else {
212 0 die "Unsupported serializer format '$self->{serializer}'";
213 }
214 }
215  
216 24 1;
217  
218 __END__
« blib/lib/Devel/StatProfiler/Utils.pm ↑ Summary blib/lib/Devel/StatProfiler/Html.pm »