blib/lib/PDL/PP/CType.pm

filter: Statementstmt 90.2% 83 / 92 Branchbran 76.0% 38 / 50 Conditioncond 80.9% 17 / 21 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 10 totaltotal 79.9% 159 / 199 CC 36 SCAR 18.7
CC
36
Cov
85%
get_copy
10.3
get_free
9.1
get_decl
6.3
CRAP 6.5
« blib/lib/PDL/PP/PdlParObj.pm ↑ Summary blib/lib/PDL/Char.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 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # new PDL::PP::CType(resolveobj,str)
12  
13 215 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
14 215 my $this = bless {},shift;
15 215 ▶ $this->parsefrom(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 215 0 215
16 215 return $this;
17 }
18  
19 367 ▶sub stripptrs {
Subroutine stripptrs: called CC=6 SCAR=18.3
Pod undocumented
20 367 my($this,$str) = @_;
21 367 ▶ $this->{WasDollar} = 1 if $str =~ s/^\$//;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ s/^\$// 0 367 367
22 367 ▶ if($str =~ s/^\s*(\w+)\s*$/$1/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^\s*(\w+)\s*$/$1/g) 215 152 367
23 215 $this->{ProtoName} = $str;
24 215 return [];
25 }
26 # Now, recall the different C syntaxes. First priority is a pointer:
27 152 ▶ return [["PTR"], @{$this->stripptrs($1)}] if $str =~ /^\s*\*(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\s*\*(.*)$/ 146 6 152
28 6 ▶ 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 6 6
29 6 ▶ return [["ARR",$2], @{$this->stripptrs($1)}] if $str =~ /^(.*)\[([^]]*)\]\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^(.*)\[([^]]*)\]\s*$/ 6 0 6
30 0 Carp::confess("Invalid C type '$str'");
31 }
32  
33 # XXX Correct to *real* parsing. This is only a subset.
34 215 ▶sub parsefrom {
Subroutine parsefrom: called CC=1 SCAR=0.0
Pod undocumented
35 215 my($this,$str) = @_;
36 # First, take the words in the beginning
37 215 $str =~ /^\s*((?:\w+\b\s*)+)([^[].*)$/;
38 215 @$this{qw(Base Chain)} = ($1, $this->stripptrs($2));
39 }
40  
41 394 ▶sub get_decl {
Subroutine get_decl: called CC=6 SCAR=18.4
Pod undocumented
42 394 my($this,$name,$opts) = @_;
43 394 for(@{$this->{Chain}}) {
44 294 my ($type, $arg) = @$_;
45 294 ▶ if($type eq "PTR") {$name = "*$name"}
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'PTR') { } 281 13 294
elsif ($type eq 'ARR') { } 13 0 13
46 elsif($type eq "ARR") {
47 13 ▶ if($opts->{VarArrays2Ptrs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'VarArrays2Ptrs'}) { } 13 0 13
48 13 $name = "*$name";
49 } else {
50 0 $name = "($name)[$arg]";
51 }
52 0 } else { confess("Invalid decl @$_") }
53 }
54 394 ▶ $name = "*$name" if $opts->{AddIndirect};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"AddIndirect"} 18 376 394
55 394 return "$this->{Base} $name";
56 }
57  
58 # Useful when parsing argument decls
59 85 ▶sub protoname { return shift->{ProtoName} }
Subroutine protoname: called CC=1 SCAR=0.0
Pod undocumented
60  
61 34 ▶sub get_copy {
Subroutine get_copy: called CC=10 SCAR=23.3
Pod undocumented
62 34 my($this,$from,$to) = @_;
63 34 ▶ return "($to) = ($from); /* CType.get_copy */" if !@{$this->{Chain}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$this->{"Chain"};} 27 7 34
64 # strdup loses portability :(
65 return "($to) = malloc(strlen($from)+1); strcpy($to,$from); /* CType.get_copy */"
66 7 ▶ if $this->{Base} =~ /^\s*char\s*$/ and @{$this->{Chain}} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"Base"} =~ /^\s*char\s*$/ and @{$this->{"Chain"};} == 1 2 5 7
Condition66%2/3
$this->{"Base"} =~ /^\s*char\s*$/ and @{$this->{"Chain"};} == 1
!ll&&!rl&&r
502
MC/DC50%1/2
$this->{"Base"} =~ /^\s*char\s*$/ and @{$this->{"Chain"};} == 1
$this->{"Base"} =~ /^\s*char\s*$/ @{$this->{"Chain"};} == 1
Truth table66%2/3
A and B
A= $this->{"Base"} =~ /^\s*char\s*$/B= @{$this->{"Chain"};} == 1
ABresult
0X0
100
111
67 5 ▶ return "($to) = newSVsv($from); /* CType.get_copy */" if $this->{Base} =~ /^\s*SV\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"Base"} =~ /^\s*SV\s*$/ 1 4 5
68 4 my $code = $this->get_malloc($to,$from);
69 4 ▶ return "($to) = ($from); /* CType.get_copy */" if !defined $code; # pointer
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 3 1 4
70 1 my ($deref0,$deref1,$prev,$close) = ($from,$to);
71 1 my $no = 0;
72 1 for(@{$this->{Chain}}) {
73 1 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 1 1
elsif ($type eq 'ARR') { } 1 0 1
75 elsif($type eq "ARR") {
76 1 $no++;
77 1 ▶ $arg = "$this->{ProtoName}_count" if $this->is_array;
Branch: 1 branch
BranchTrueFalseTotal
if $this->is_array 1 0 1
78 1 $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 1 $deref0 .= "[__malloc_ind_$no]";
85 1 $deref1 .= "[__malloc_ind_$no]";
86 1 $close .= "}}";
87 0 } else { confess("Invalid decl @$_") }
88 }
89 1 $code .= "$prev $deref1 = $deref0; $close";
90 1 return $code;
91 }
92  
93 34 ▶sub get_free {
Subroutine get_free: called CC=9 SCAR=22.1
Pod undocumented
94 34 my($this,$from) = @_;
95 34 ▶ my $single_ptr = @{$this->{Chain}} == 1 && $this->{Chain}[0][0] eq 'PTR';
Condition100%3/3
@{$this->{'Chain'};} == 1 && $this->{'Chain'}[0][0] eq 'PTR'
!ll&&!rl&&r
2914
MC/DC100%2/2
@{$this->{'Chain'};} == 1 && $this->{'Chain'}[0][0] eq 'PTR'
@{$this->{'Chain'};} == 1 $this->{'Chain'}[0][0] eq 'PTR'
Truth table100%3/3
A && B
A= @{$this->{'Chain'};} == 1B= $this->{'Chain'}[0][0] eq 'PTR'
ABresult
0X0
100
111
96 34 ▶ 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 1 33 34
Condition66%2/3
$this->{"Base"} =~ /^\s*SV\s*$/ and $single_ptr
!ll&&!rl&&r
3301
MC/DC50%1/2
$this->{"Base"} =~ /^\s*SV\s*$/ and $single_ptr
$this->{"Base"} =~ /^\s*SV\s*$/ $single_ptr
Truth table66%2/3
A and B
A= $this->{"Base"} =~ /^\s*SV\s*$/B= $single_ptr
ABresult
0X0
100
111
97 33 ▶ 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 2 31 33
Condition66%2/3
$this->{"Base"} =~ /^\s*char\s*$/ and $single_ptr
!ll&&!rl&&r
3102
MC/DC50%1/2
$this->{"Base"} =~ /^\s*char\s*$/ and $single_ptr
$this->{"Base"} =~ /^\s*char\s*$/ $single_ptr
Truth table66%2/3
A and B
A= $this->{"Base"} =~ /^\s*char\s*$/B= $single_ptr
ABresult
0X0
100
111
98 31 ▶ 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" 30 1 31
Condition100%3/3
not @{$this->{"Chain"};} or $this->{"Chain"}[0][0] eq "PTR"
l!l&&r!l&&!r
2731
MC/DC100%2/2
not @{$this->{"Chain"};} or $this->{"Chain"}[0][0] eq "PTR"
not @{$this->{"Chain"};} $this->{"Chain"}[0][0] eq "PTR"
Truth table100%3/3
not(A) or B
A= not @{$this->{"Chain"};}B= $this->{"Chain"}[0][0] eq "PTR"
ABresult
1X1
011
000
99 1 ▶ croak("Can only free one layer!\n") if @{$this->{Chain}} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$this->{"Chain"};} > 1 0 1 1
100 1 "free($from); /* CType.get_free */\n";
101 }
102  
103 0 ▶sub need_malloc {
Subroutine need_malloc: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my($this) = @_;
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 4 ▶sub get_malloc {
Subroutine get_malloc: called CC=5 SCAR=16.4
Pod undocumented
110 4 my($this,$assignto) = @_;
111 4 my $str = "";
112 4 for(@{$this->{Chain}}) {
113 4 my ($type, $arg) = @$_;
114 4 ▶ if($type eq "PTR") {return}
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'PTR') { } 3 1 4
elsif ($type eq 'ARR') { } 1 0 1
115 elsif($type eq "ARR") {
116 1 ▶ $arg = "$this->{ProtoName}_count" if $this->is_array;
Branch: 1 branch
BranchTrueFalseTotal
if $this->is_array 1 0 1
117 1 $str .= "$assignto = malloc(sizeof(*$assignto) * $arg); /* CType.get_malloc */\n";
118 0 } else { confess("Invalid decl (@$_)") }
119 }
120 1 return $str;
121 }
122  
123 537 ▶sub is_array {
Subroutine is_array: called CC=4 SCAR=13.9
Pod undocumented
124 537 my ($self) = @_;
125 537 @{$self->{Chain}} &&
126 131 @{$self->{Chain}[-1]} &&
127 $self->{Chain}[-1][0] eq 'ARR' &&
128 537 ▶ !$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] 53 484 537
Condition83%5/6
@{$self->{"Chain"};} and @{$self->{"Chain"}[-1];} and $self->{"Chain"}[-1][0] eq "ARR"
!ll&&!rl&&r
4067853
@{$self->{"Chain"};} and @{$self->{"Chain"}[-1];}
!ll&&!rl&&r
4060131
MC/DC66%2/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 table75%3/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 5 1;
« blib/lib/PDL/PP/PdlParObj.pm ↑ Summary blib/lib/PDL/Char.pm »