blib/lib/PDL/PP/CType.pm

filter: Statementstmt 50.0% 46 / 92 Branchbran 24.0% 12 / 50 Conditioncond 38.1% 8 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 10 totaltotal 38.6% 77 / 199 CC 36 SCAR 33.2
CC
36
Cov
40%
get_copy
83.5
get_malloc
30.0
get_free
21.3
CRAP 27.6
« blib/lib/PDL/NiceSlice.pm ↑ Summary blib/lib/PDL/Transform/Cartography.pm »
1 # Represent any C type.
2 # Type contains the size of arrays, which is either constant
3 # or resolved (into an object) from resolveobj.
4  
5 package # hide from PAUSE/MetaCPAN
6 PDL::PP::CType;
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # new PDL::PP::CType(resolveobj,str)
12  
13 sub new {
14 115 ▶ my $this = bless {},shift;
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
15 115 ▶ $this->parsefrom(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 115 0 115
16 115 return $this;
17 }
18  
19 sub stripptrs {
20 199 ▶ my($this,$str) = @_;
Subroutine stripptrs: called CC=6 SCAR=22.9
Pod undocumented
21 199 ▶ $this->{WasDollar} = 1 if $str =~ s/^\$//;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ s/^\$// 0 199 199
22 199 ▶ if($str =~ s/^\s*(\w+)\s*$/$1/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^\s*(\w+)\s*$/$1/g) 115 84 199
23 115 $this->{ProtoName} = $str;
24 115 return [];
25 }
26 # Now, recall the different C syntaxes. First priority is a pointer:
27 84 ▶ return [["PTR"], @{$this->stripptrs($1)}] if $str =~ /^\s*\*(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*\*(.*)$/ 84 0 84
28 0 ▶ return $this->stripptrs($1) if $str =~ /^\s*\(.*\)\s*$/; # XXX Should try to see if a funccall.
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*\(.*\)\s*$/ 0 0 0
29 0 ▶ return [["ARR",$2], @{$this->stripptrs($1)}] if $str =~ /^(.*)\[([^]]*)\]\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^(.*)\[([^]]*)\]\s*$/ 0 0 0
30 0 Carp::confess("Invalid C type '$str'");
31 }
32  
33 # XXX Correct to *real* parsing. This is only a subset.
34 sub parsefrom {
35 115 ▶ my($this,$str) = @_;
Subroutine parsefrom: called CC=1 SCAR=0.0
Pod undocumented
36 # First, take the words in the beginning
37 115 $str =~ /^\s*((?:\w+\b\s*)+)([^[].*)$/;
38 115 @$this{qw(Base Chain)} = ($1, $this->stripptrs($2));
39 }
40  
41 sub get_decl {
42 184 ▶ my($this,$name,$opts) = @_;
Subroutine get_decl: called CC=6 SCAR=23.5
Pod undocumented
43 184 for(@{$this->{Chain}}) {
44 148 my ($type, $arg) = @$_;
45 148 ▶ if($type eq "PTR") {$name = "*$name"}
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'PTR') { } 148 0 148
elsif ($type eq 'ARR') { } 0 0 0
46 elsif($type eq "ARR") {
47 0 ▶ if($opts->{VarArrays2Ptrs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'VarArrays2Ptrs'}) { } 0 0 0
48 0 $name = "*$name";
49 } else {
50 0 $name = "($name)[$arg]";
51 }
52 0 } else { confess("Invalid decl @$_") }
53 }
54 184 ▶ $name = "*$name" if $opts->{AddIndirect};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"AddIndirect"} 0 184 184
55 184 return "$this->{Base} $name";
56 }
57  
58 # Useful when parsing argument decls
59 41 ▶sub protoname { return shift->{ProtoName} }
Subroutine protoname: called CC=1 SCAR=0.0
Pod undocumented
60  
61 sub get_copy {
62 9 ▶ my($this,$from,$to) = @_;
Subroutine get_copy: called CC=10 SCAR=44.2
Pod undocumented
63 9 ▶ return "($to) = ($from); /* CType.get_copy */" if !@{$this->{Chain}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$this->{"Chain"};} 9 0 9
64 # strdup loses portability :(
65 return "($to) = malloc(strlen($from)+1); strcpy($to,$from); /* CType.get_copy */"
66 0 ▶ if $this->{Base} =~ /^\s*char\s*$/ and @{$this->{Chain}} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"Base"} =~ /^\s*char\s*$/ and @{$this->{"Chain"};} == 1 0 0 0
Condition0%0/3
$this->{"Base"} =~ /^\s*char\s*$/ and @{$this->{"Chain"};} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"Base"} =~ /^\s*char\s*$/ and @{$this->{"Chain"};} == 1
$this->{"Base"} =~ /^\s*char\s*$/ @{$this->{"Chain"};} == 1
Truth table0%0/3
A and B
A= $this->{"Base"} =~ /^\s*char\s*$/B= @{$this->{"Chain"};} == 1
ABresult
0X0
100
111
67 0 ▶ return "($to) = newSVsv($from); /* CType.get_copy */" if $this->{Base} =~ /^\s*SV\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"Base"} =~ /^\s*SV\s*$/ 0 0 0
68 0 my $code = $this->get_malloc($to,$from);
69 0 ▶ return "($to) = ($from); /* CType.get_copy */" if !defined $code; # pointer
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 0 0
70 0 my ($deref0,$deref1,$prev,$close) = ($from,$to);
71 0 my $no = 0;
72 0 for(@{$this->{Chain}}) {
73 0 my ($type, $arg) = @$_;
74 0 ▶ if($type eq "PTR") {confess("Cannot copy pointer, must be array");}
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'PTR') { } 0 0 0
elsif ($type eq 'ARR') { } 0 0 0
75 elsif($type eq "ARR") {
76 0 $no++;
77 0 ▶ $arg = "$this->{ProtoName}_count" if $this->is_array;
Branch: 1 branch
BranchTrueFalseTotal
if $this->is_array 0 0 0
78 0 $prev .= "
79 if(!$deref0) {$deref1=0;} /* CType.get_copy */
80 else {int __malloc_ind_$no;
81 for(__malloc_ind_$no = 0;
82 __malloc_ind_$no < $arg;
83 __malloc_ind_$no ++) {";
84 0 $deref0 .= "[__malloc_ind_$no]";
85 0 $deref1 .= "[__malloc_ind_$no]";
86 0 $close .= "}}";
87 0 } else { confess("Invalid decl @$_") }
88 }
89 0 $code .= "$prev $deref1 = $deref0; $close";
90 0 return $code;
91 }
92  
93 sub get_free {
94 9 ▶ my($this,$from) = @_;
Subroutine get_free: called CC=9 SCAR=30.6
Pod undocumented
95 9 ▶ my $single_ptr = @{$this->{Chain}} == 1 && $this->{Chain}[0][0] eq 'PTR';
Condition33%1/3
@{$this->{'Chain'};} == 1 && $this->{'Chain'}[0][0] eq 'PTR'
!ll&&!rl&&r
900
MC/DC0%0/2
@{$this->{'Chain'};} == 1 && $this->{'Chain'}[0][0] eq 'PTR'
@{$this->{'Chain'};} == 1 $this->{'Chain'}[0][0] eq 'PTR'
Truth table33%1/3
A && B
A= @{$this->{'Chain'};} == 1B= $this->{'Chain'}[0][0] eq 'PTR'
ABresult
0X0
100
111
96 9 ▶ return "SvREFCNT_dec($from); /* CType.get_free */\n" if $this->{Base} =~ /^\s*SV\s*$/ and $single_ptr;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"Base"} =~ /^\s*SV\s*$/ and $single_ptr 0 9 9
Condition33%1/3
$this->{"Base"} =~ /^\s*SV\s*$/ and $single_ptr
!ll&&!rl&&r
900
MC/DC0%0/2
$this->{"Base"} =~ /^\s*SV\s*$/ and $single_ptr
$this->{"Base"} =~ /^\s*SV\s*$/ $single_ptr
Truth table33%1/3
A and B
A= $this->{"Base"} =~ /^\s*SV\s*$/B= $single_ptr
ABresult
0X0
100
111
97 9 ▶ return "free($from); /* CType.get_free */\n" if $this->{Base} =~ /^\s*char\s*$/ and $single_ptr;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"Base"} =~ /^\s*char\s*$/ and $single_ptr 0 9 9
Condition33%1/3
$this->{"Base"} =~ /^\s*char\s*$/ and $single_ptr
!ll&&!rl&&r
900
MC/DC0%0/2
$this->{"Base"} =~ /^\s*char\s*$/ and $single_ptr
$this->{"Base"} =~ /^\s*char\s*$/ $single_ptr
Truth table33%1/3
A and B
A= $this->{"Base"} =~ /^\s*char\s*$/B= $single_ptr
ABresult
0X0
100
111
98 9 ▶ return "" if !@{$this->{Chain}} or $this->{Chain}[0][0] eq 'PTR';
Branch: 1 branch
BranchTrueFalseTotal
if not @{$this->{"Chain"};} or $this->{"Chain"}[0][0] eq "PTR" 9 0 9
Condition33%1/3
not @{$this->{"Chain"};} or $this->{"Chain"}[0][0] eq "PTR"
l!l&&r!l&&!r
900
MC/DC0%0/2
not @{$this->{"Chain"};} or $this->{"Chain"}[0][0] eq "PTR"
not @{$this->{"Chain"};} $this->{"Chain"}[0][0] eq "PTR"
Truth table33%1/3
not(A) or B
A= not @{$this->{"Chain"};}B= $this->{"Chain"}[0][0] eq "PTR"
ABresult
1X1
011
000
99 0 ▶ croak("Can only free one layer!\n") if @{$this->{Chain}} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$this->{"Chain"};} > 1 0 0 0
100 0 "free($from); /* CType.get_free */\n";
101 }
102  
103 sub need_malloc {
104 0 ▶ my($this) = @_;
Subroutine need_malloc: not called CC=1 SCAR=6.9
Pod undocumented
105 0 grep /(ARR|PTR)/, map $_->[0], @{$this->{Chain}};
106 }
107  
108 # returns with the array string - undef if a pointer not needing malloc
109 sub get_malloc {
110 0 ▶ my($this,$assignto) = @_;
Subroutine get_malloc: not called CC=5 SCAR=34.0
Pod undocumented
111 0 my $str = "";
112 0 for(@{$this->{Chain}}) {
113 0 my ($type, $arg) = @$_;
114 0 ▶ if($type eq "PTR") {return}
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'PTR') { } 0 0 0
elsif ($type eq 'ARR') { } 0 0 0
115 elsif($type eq "ARR") {
116 0 ▶ $arg = "$this->{ProtoName}_count" if $this->is_array;
Branch: 1 branch
BranchTrueFalseTotal
if $this->is_array 0 0 0
117 0 $str .= "$assignto = malloc(sizeof(*$assignto) * $arg); /* CType.get_malloc */\n";
118 0 } else { confess("Invalid decl (@$_)") }
119 }
120 0 return $str;
121 }
122  
123 sub is_array {
124 161 ▶ my ($self) = @_;
Subroutine is_array: called CC=4 SCAR=14.5
Pod undocumented
125 161 @{$self->{Chain}} &&
126 10 @{$self->{Chain}[-1]} &&
127 $self->{Chain}[-1][0] eq 'ARR' &&
128 161 ▶ !$self->{Chain}[-1][1];
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"Chain"};} and @{$self->{"Chain"}[-1];} and $self->{"Chain"}[-1][0] eq "ARR" and not $self->{"Chain"}[-1][1] 0 161 161
Condition66%4/6
@{$self->{"Chain"};} and @{$self->{"Chain"}[-1];} and $self->{"Chain"}[-1][0] eq "ARR"
!ll&&!rl&&r
151100
@{$self->{"Chain"};} and @{$self->{"Chain"}[-1];}
!ll&&!rl&&r
151010
MC/DC0%0/3
@{$self->{"Chain"};} and @{$self->{"Chain"}[-1];} and $self->{"Chain"}[-1][0] eq "ARR"
@{$self->{"Chain"};} @{$self->{"Chain"}[-1];} $self->{"Chain"}[-1][0] eq "ARR"
Truth table50%2/4
A and B and C
A= @{$self->{"Chain"};}B= @{$self->{"Chain"}[-1];}C= $self->{"Chain"}[-1][0] eq "ARR"
ABCresult
0XX0
10X0
1100
1111
129 }
130  
131 3 1;
« blib/lib/PDL/NiceSlice.pm ↑ Summary blib/lib/PDL/Transform/Cartography.pm »