blib/lib/ExtUtils/Constant/Base.pm

filter: Statementstmt 92.4% 365 / 395 Branchbran 79.4% 143 / 180 Conditioncond 66.6% 108 / 162 MC/DCmcdc 34.9% 29 / 83 Subroutinesub 61.1% 22 / 36 Podpod 36.6% 11 / 30 totaltotal 76.5% 678 / 886 CC 164 SCAR 30.0
CC
164
Cov
84%
dump_names
35.6
switch_clause
31.3
normalise_items
25.2
CRAP 20.1
« blib/lib/ExtUtils/Constant.pm ↑ Summary blib/lib/ExtUtils/Constant/XS.pm »
1 package ExtUtils::Constant::Base;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Text::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use ExtUtils::Constant::Utils qw(C_stringify perl_stringify);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 $VERSION = '0.07';
9  
10 1 ▶use constant is_perl56 => ($] < 5.007 && $] > 5.005_50);
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
$] < 5.007 && $] > 5.0055
!ll&&!rl&&r
100
MC/DC0%0/2
$] < 5.007 && $] > 5.0055
$] < 5.007 $] > 5.0055
Truth table33%1/3
A && B
A= $] < 5.007B= $] > 5.0055
ABresult
0X0
100
111
11  
12  
13 =head1 NAME
14  
15 ExtUtils::Constant::Base - base class for ExtUtils::Constant objects
16  
17 =head1 SYNOPSIS
18  
19 require ExtUtils::Constant::Base;
20 @ISA = 'ExtUtils::Constant::Base';
21  
22 =head1 DESCRIPTION
23  
24 ExtUtils::Constant::Base provides a base implementation of methods to
25 generate C code to give fast constant value lookup by named string. Currently
26 it's mostly used ExtUtils::Constant::XS, which generates the lookup code
27 for the constant() subroutine found in many XS modules.
28  
29 =head1 USAGE
30  
31 ExtUtils::Constant::Base exports no subroutines. The following methods are
32 available
33  
34 =over 4
35  
36 =cut
37  
38 0 ▶sub valid_type {
Subroutine valid_type: not called CC=1 SCAR=6.9
Pod undocumented
39 # Default to assuming that you don't need different types of return data.
40 0 1;
41 }
42 0 ▶sub default_type {
Subroutine default_type: not called CC=1 SCAR=6.9
Pod documented
43 0 '';
44 }
45  
46 =item header
47  
48 A method returning a scalar containing definitions needed, typically for a
49 C header file.
50  
51 =cut
52  
53 0 ▶sub header {
Subroutine header: not called CC=1 SCAR=6.9
Pod documented
54 0 ''
55 }
56  
57 # This might actually be a return statement. Note that you are responsible
58 # for any space you might need before your value, as it lets to perform
59 # "tricks" such as "return KEY_" and have strings appended.
60 sub assignment_clause_for_type;
61 # In which case this might be an empty string
62 0 ▶sub return_statement_for_type {undef};
Subroutine return_statement_for_type: not called CC=1 SCAR=6.9
Pod undocumented
63 sub return_statement_for_notdef;
64 sub return_statement_for_notfound;
65  
66 # "#if 1" is true to a C pre-processor
67 0 ▶sub macro_from_name {
Subroutine macro_from_name: not called CC=1 SCAR=6.9
Pod undocumented
68 0 1;
69 }
70  
71 0 ▶sub macro_from_item {
Subroutine macro_from_item: not called CC=1 SCAR=6.9
Pod undocumented
72 0 1;
73 }
74  
75 143 ▶sub macro_to_ifdef {
Subroutine macro_to_ifdef: called CC=6 SCAR=18.6
Pod undocumented
76 143 my ($self, $macro) = @_;
77 143 ▶ if (ref $macro) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $macro) 4 139 143
78 4 return $macro->[0];
79 }
80 139 ▶ if (defined $macro && $macro ne "" && $macro ne "1") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $macro and $macro ne "" and $macro ne 1) 115 24 139
Condition50%3/6
defined $macro and $macro ne "" and $macro ne 1
!ll&&!rl&&r
024115
defined $macro and $macro ne ""
!ll&&!rl&&r
00139
MC/DC33%1/3
defined $macro and $macro ne "" and $macro ne 1
defined $macro $macro ne "" $macro ne 1
Truth table50%2/4
A and B and C
A= defined $macroB= $macro ne ""C= $macro ne 1
ABCresult
0XX0
10X0
1100
1111
81 115 ▶ return $macro ? "#ifdef $macro\n" : "#if 0\n";
Branch: 1 branch
BranchTrueFalseTotal
$macro ? : 115 0 115
82 }
83 24 return "";
84 }
85  
86 21 ▶sub macro_to_ifndef {
Subroutine macro_to_ifndef: called CC=6 SCAR=20.9
Pod undocumented
87 21 my ($self, $macro) = @_;
88 21 ▶ if (ref $macro) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $macro) 1 20 21
89 # Can't invert these stylishly, so "bodge it"
90 1 return "$macro->[0]#else\n";
91 }
92 20 ▶ if (defined $macro && $macro ne "" && $macro ne "1") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $macro and $macro ne "" and $macro ne 1) 20 0 20
Condition33%2/6
defined $macro and $macro ne "" and $macro ne 1
!ll&&!rl&&r
0020
defined $macro and $macro ne ""
!ll&&!rl&&r
0020
MC/DC0%0/3
defined $macro and $macro ne "" and $macro ne 1
defined $macro $macro ne "" $macro ne 1
Truth table25%1/4
A and B and C
A= defined $macroB= $macro ne ""C= $macro ne 1
ABCresult
0XX0
10X0
1100
1111
93 20 ▶ return $macro ? "#ifndef $macro\n" : "#if 1\n";
Branch: 1 branch
BranchTrueFalseTotal
$macro ? : 20 0 20
94 }
95 0 croak "Can't generate an ifndef for unconditional code";
96 }
97  
98 119 ▶sub macro_to_endif {
Subroutine macro_to_endif: called CC=5 SCAR=16.4
Pod undocumented
99 119 my ($self, $macro) = @_;
100  
101 119 ▶ if (ref $macro) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $macro) 3 116 119
102 3 return $macro->[1];
103 }
104 116 ▶ if (defined $macro && $macro ne "" && $macro ne "1") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $macro and $macro ne "" and $macro ne 1) 96 20 116
Condition50%3/6
defined $macro and $macro ne "" and $macro ne 1
!ll&&!rl&&r
02096
defined $macro and $macro ne ""
!ll&&!rl&&r
00116
MC/DC33%1/3
defined $macro and $macro ne "" and $macro ne 1
defined $macro $macro ne "" $macro ne 1
Truth table50%2/4
A and B and C
A= defined $macroB= $macro ne ""C= $macro ne 1
ABCresult
0XX0
10X0
1100
1111
105 96 return "#endif\n";
106 }
107 20 return "";
108 }
109  
110 107 ▶sub name_param {
Subroutine name_param: called CC=1 SCAR=0.0
Pod undocumented
111 107 'name';
112 }
113  
114 # This is possibly buggy, in that it's not mandatory (below, in the main
115 # C_constant parameters, but is expected to exist here, if it's needed)
116 # Buggy because if you're definitely pure 8 bit only, and will never be
117 # presented with your constants in utf8, the default form of C_constant can't
118 # be told not to do the utf8 version.
119  
120 11 ▶sub is_utf8_param {
Subroutine is_utf8_param: called CC=1 SCAR=0.0
Pod undocumented
121 11 'utf8';
122 }
123  
124 0 ▶sub memEQ {
Subroutine memEQ: not called CC=1 SCAR=6.9
Pod undocumented
125 0 "!memcmp";
126 }
127  
128 =item memEQ_clause args_hashref
129  
130 A method to return a suitable C C<if> statement to check whether I<name>
131 is equal to the C variable C<name>. If I<checked_at> is defined, then it
132 is used to avoid C<memEQ> for short names, or to generate a comment to
133 highlight the position of the character in the C<switch> statement.
134  
135 If i<checked_at> is a reference to a scalar, then instead it gives
136 the characters pre-checked at the beginning, (and the number of chars by
137 which the C variable name has been advanced. These need to be chopped from
138 the front of I<name>).
139  
140 =cut
141  
142 71 ▶sub memEQ_clause {
Subroutine memEQ_clause: called CC=24 SCAR=32.0
Pod documented
143 # if (memEQ(name, "thingy", 6)) {
144 # Which could actually be a character comparison or even ""
145 71 my ($self, $args) = @_;
146 71 my ($name, $checked_at, $indent) = @{$args}{qw(name checked_at indent)};
147 71 ▶ $indent = ' ' x ($indent || 4);
Condition50%1/2
$indent || 4
l!l
710
MC/DC0%0/1
$indent || 4
$indent
Truth table50%1/2
A
A= $indent
Aresult
11
00
148 71 my $front_chop;
149 71 ▶ if (ref $checked_at) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $checked_at) 14 57 71
150 # regexp won't work on 5.6.1 without use utf8; in turn that won't work
151 # on 5.005_03.
152 14 substr ($name, 0, length $$checked_at,) = '';
153 14 $front_chop = C_stringify ($$checked_at);
154 14 undef $checked_at;
155 }
156 71 my $len = length $name;
157  
158 71 ▶ if ($len < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 2) 8 63 71
159 8 ▶ return $indent . "{\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $checked_at and $checked_at == 0 or $len == 0 5 3 8
Condition66%4/6
defined $checked_at and $checked_at == 0 or $len == 0
l!l&&r!l&&!r
503
defined $checked_at and $checked_at == 0
!ll&&!rl&&r
305
MC/DC33%1/3
defined $checked_at and $checked_at == 0 or $len == 0
defined $checked_at $checked_at == 0 $len == 0
Truth table40%2/5
A and B or C
A= defined $checked_atB= $checked_at == 0C= $len == 0
ABCresult
11X1
0X11
1011
0X00
1000
160 if (defined $checked_at and $checked_at == 0) or $len == 0;
161 # We didn't switch, drop through to the code for the 2 character string
162 3 $checked_at = 1;
163 }
164  
165 66 my $name_param = $self->name_param;
166  
167 66 ▶ if ($len < 3 and defined $checked_at) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 3 and defined $checked_at) 23 43 66
Condition100%3/3
$len < 3 and defined $checked_at
!ll&&!rl&&r
291423
MC/DC100%2/2
$len < 3 and defined $checked_at
$len < 3 defined $checked_at
Truth table100%3/3
A and B
A= $len < 3B= defined $checked_at
ABresult
0X0
100
111
168 23 my $check;
169 23 ▶ if ($checked_at == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($checked_at == 1) { } 23 0 23
elsif ($checked_at == 0) { } 0 0 0
170 23 $check = 0;
171 } elsif ($checked_at == 0) {
172 0 $check = 1;
173 }
174 23 ▶ if (defined $check) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check) 23 0 23
175 23 my $char = C_stringify (substr $name, $check, 1);
176 # Placate 5.005 with a break in the string. I can't see a good way of
177 # getting it to not take [ as introducing an array lookup, even with
178 # ${name_param}[$check]
179 23 return $indent . "if ($name_param" . "[$check] == '$char') {\n";
180 }
181 }
182 43 ▶ if (($len == 2 and !defined $checked_at)
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 2 and not defined $checked_at or $len == 3 and defined $checked_at and $checked_at == 2) 20 23 43
Condition91%11/12
$len == 2 and not defined $checked_at or $len == 3 and defined $checked_at and $checked_at == 2
l!l&&r!l&&!r
14623
$len == 2 and not defined $checked_at
!ll&&!rl&&r
29014
$len == 3 and defined $checked_at and $checked_at == 2
!ll&&!rl&&r
1856
$len == 3 and defined $checked_at
!ll&&!rl&&r
17111
MC/DC80%4/5
$len == 2 and not defined $checked_at or $len == 3 and defined $checked_at and $checked_at == 2
$len == 2 not defined $checked_at $len == 3 defined $checked_at $checked_at == 2
Truth table55%5/9
A and not(B) or C and D and E
A= $len == 2B= not defined $checked_atC= $len == 3D= defined $checked_atE= $checked_at == 2
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
183 or ($len == 3 and defined ($checked_at) and $checked_at == 2)) {
184 20 my $char1 = C_stringify (substr $name, 0, 1);
185 20 my $char2 = C_stringify (substr $name, 1, 1);
186 20 return $indent .
187 "if ($name_param" . "[0] == '$char1' && $name_param" . "[1] == '$char2') {\n";
188 }
189 23 ▶ if (($len == 3 and defined ($checked_at) and $checked_at == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 3 and defined $checked_at and $checked_at == 1) 5 18 23
Condition83%5/6
$len == 3 and defined $checked_at and $checked_at == 1
!ll&&!rl&&r
1805
$len == 3 and defined $checked_at
!ll&&!rl&&r
1715
MC/DC66%2/3
$len == 3 and defined $checked_at and $checked_at == 1
$len == 3 defined $checked_at $checked_at == 1
Truth table75%3/4
A and B and C
A= $len == 3B= defined $checked_atC= $checked_at == 1
ABCresult
0XX0
10X0
1100
1111
190 5 my $char1 = C_stringify (substr $name, 0, 1);
191 5 my $char2 = C_stringify (substr $name, 2, 1);
192 5 return $indent .
193 "if ($name_param" . "[0] == '$char1' && $name_param" . "[2] == '$char2') {\n";
194 }
195  
196 18 my $pointer = '^';
197 18 ▶ my $have_checked_last = defined ($checked_at) && $len == $checked_at + 1;
Condition100%3/3
defined $checked_at && $len == $checked_at + 1
!ll&&!rl&&r
873
MC/DC100%2/2
defined $checked_at && $len == $checked_at + 1
defined $checked_at $len == $checked_at + 1
Truth table100%3/3
A && B
A= defined $checked_atB= $len == $checked_at + 1
ABresult
0X0
100
111
198 18 ▶ if ($have_checked_last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_checked_last) 3 15 18
199 # Checked at the last character, so no need to memEQ it.
200 3 $pointer = C_stringify (chop $name);
201 3 $len--;
202 }
203  
204 18 $name = C_stringify ($name);
205 18 my $memEQ = $self->memEQ();
206 18 my $body = $indent . "if ($memEQ($name_param, \"$name\", $len)) {\n";
207 # Put a little ^ under the letter we checked at
208 # Screws up for non printable and non-7 bit stuff, but that's too hard to
209 # get right.
210 18 ▶ if (defined $checked_at) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $checked_at) { } 10 8 18
elsif (defined $front_chop) { } 2 6 8
211 10 $body .= $indent . "/* " . (' ' x length $memEQ)
212 . (' ' x length $name_param)
213 . (' ' x $checked_at) . $pointer
214 . (' ' x ($len - $checked_at + length $len)) . " */\n";
215 } elsif (defined $front_chop) {
216 2 $body .= $indent . "/* $front_chop"
217 . (' ' x ($len + 1 + length $len)) . " */\n";
218 }
219 18 return $body;
220 }
221  
222 =item dump_names arg_hashref, ITEM...
223  
224 An internal function to generate the embedded perl code that will regenerate
225 the constant subroutines. I<default_type>, I<types> and I<ITEM>s are the
226 same as for C_constant. I<indent> is treated as number of spaces to indent
227 by. If C<declare_types> is true a C<$types> is always declared in the perl
228 code generated, if defined and false never declared, and if undefined C<$types>
229 is only declared if the values in I<types> as passed in cannot be inferred from
230 I<default_types> and the I<ITEM>s.
231  
232 =cut
233  
234 7 ▶sub dump_names {
Subroutine dump_names: called CC=28 SCAR=35.7
Pod documented
235 7 my ($self, $args, @items) = @_;
236 my ($default_type, $what, $indent, $declare_types)
237 7 = @{$args}{qw(default_type what indent declare_types)};
238 7 ▶ $indent = ' ' x ($indent || 0);
Condition50%1/2
$indent || 0
l!l
07
MC/DC0%0/1
$indent || 0
$indent
Truth table50%1/2
A
A= $indent
Aresult
11
00
239  
240 7 my $result;
241 7 my (@simple, @complex, %used_types);
242 7 foreach (@items) {
243 71 my $type;
244 71 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 71 0 71
245 71 ▶ $type = $_->{type} || $default_type;
Condition33%1/3
$_->{'type'} || $default_type
l!l&&r!l&&!r
7100
MC/DC0%0/2
$_->{'type'} || $default_type
$_->{'type'} $default_type
Truth table33%1/3
A || B
A= $_->{'type'}B= $default_type
ABresult
1X1
011
000
246 71 ▶ if ($_->{utf8}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"utf8"}) 11 60 71
247 # For simplicity always skip the bytes case, and reconstitute this entry
248 # from its utf8 twin.
249 11 ▶ next if $_->{utf8} eq 'no';
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"utf8"} eq "no" 5 6 11
250 # Copy the hashref, as we don't want to mess with the caller's hashref.
251 6 $_ = {%$_};
252 6 unless (is_perl56) {
253 6 utf8::decode ($_->{name});
254 } else {
255 $_->{name} = pack 'U*', unpack 'U0U*', $_->{name};
256 }
257 6 delete $_->{utf8};
258 }
259 } else {
260 0 $_ = {name=>$_};
261 0 $type = $default_type;
262 }
263 66 $used_types{$type}++;
264 66 ▶ if ($type eq $default_type
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq $default_type and length $_->{'name'} and length $_->{'name'} == $_->{'name'} =~ tr/0-9A-Z_a-z// and not defined $_->{'macro'} and not defined $_->{'value'} and not defined $_->{'default'} and not defined $_->{'pre'} and not defined $_->{'post'} and not defined $_->{'def_pre'} and not defined $_->{'def_post'} and not defined $_->{'weight'}) { } 43 23 66
Condition73%22/30
$type eq $default_type and length $_->{"name"} and length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z// and not defined $_->{"macro"} and not defined $_->{"value"} and not defined $_->{"default"} and not defined $_->{"pre"} and not defined $_->{"post"} and not defined $_->{"def_pre"} and not defined $_->{"def_post"} and not defined $_->{"weight"}
!ll&&!rl&&r
23043
$type eq $default_type and length $_->{"name"} and length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z// and not defined $_->{"macro"} and not defined $_->{"value"} and not defined $_->{"default"} and not defined $_->{"pre"} and not defined $_->{"post"} and not defined $_->{"def_pre"} and not defined $_->{"def_post"}
!ll&&!rl&&r
23043
$type eq $default_type and length $_->{"name"} and length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z// and not defined $_->{"macro"} and not defined $_->{"value"} and not defined $_->{"default"} and not defined $_->{"pre"} and not defined $_->{"post"} and not defined $_->{"def_pre"}
!ll&&!rl&&r
23043
$type eq $default_type and length $_->{"name"} and length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z// and not defined $_->{"macro"} and not defined $_->{"value"} and not defined $_->{"default"} and not defined $_->{"pre"} and not defined $_->{"post"}
!ll&&!rl&&r
23043
$type eq $default_type and length $_->{"name"} and length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z// and not defined $_->{"macro"} and not defined $_->{"value"} and not defined $_->{"default"} and not defined $_->{"pre"}
!ll&&!rl&&r
23043
$type eq $default_type and length $_->{"name"} and length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z// and not defined $_->{"macro"} and not defined $_->{"value"} and not defined $_->{"default"}
!ll&&!rl&&r
22143
$type eq $default_type and length $_->{"name"} and length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z// and not defined $_->{"macro"} and not defined $_->{"value"}
!ll&&!rl&&r
22044
$type eq $default_type and length $_->{"name"} and length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z// and not defined $_->{"macro"}
!ll&&!rl&&r
22044
$type eq $default_type and length $_->{"name"} and length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z//
!ll&&!rl&&r
21144
$type eq $default_type and length $_->{"name"}
!ll&&!rl&&r
21045
MC/DC27%3/11
$type eq $default_type and length $_->{"name"} and length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z// and not defined $_->{"macro"} and not defined $_->{"value"} and not defined $_->{"default"} and not defined $_->{"pre"} and not defined $_->{"post"} and not defined $_->{"def_pre"} and not defined $_->{"def_post"} and not defined $_->{"weight"}
$type eq $default_type length $_->{"name"} length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z// not defined $_->{"macro"} not defined $_->{"value"} not defined $_->{"default"} not defined $_->{"pre"} not defined $_->{"post"} not defined $_->{"def_pre"} not defined $_->{"def_post"} not defined $_->{"weight"}
Truth table33%4/12
A and B and C and not(D) and not(E) and not(F) and not(G) and not(H) and not(I) and not(J) and not(K)
A= $type eq $default_typeB= length $_->{"name"}C= length $_->{"name"} == $_->{"name"} =~ tr/0-9A-Z_a-z//D= not defined $_->{"macro"}E= not defined $_->{"value"}F= not defined $_->{"default"}G= not defined $_->{"pre"}H= not defined $_->{"post"}I= not defined $_->{"def_pre"}J= not defined $_->{"def_post"}K= not defined $_->{"weight"}
ABCDEFGHIJKresult
0XXXXXXXXXX0
10XXXXXXXXX0
110XXXXXXXX0
1110XXXXXXX0
11110XXXXXX0
111110XXXXX0
1111110XXXX0
11111110XXX0
111111110XX0
1111111110X0
111111111100
111111111111
265 # grr 5.6.1
266 and length $_->{name}
267 and length $_->{name} == ($_->{name} =~ tr/A-Za-z0-9_//)
268 and !defined ($_->{macro}) and !defined ($_->{value})
269 and !defined ($_->{default}) and !defined ($_->{pre})
270 and !defined ($_->{post}) and !defined ($_->{def_pre})
271 and !defined ($_->{def_post}) and !defined ($_->{weight})) {
272 # It's the default type, and the name consists only of A-Za-z0-9_
273 43 push @simple, $_->{name};
274 } else {
275 23 push @complex, $_;
276 }
277 }
278  
279 7 ▶ if (!defined $declare_types) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $declare_types) 0 7 7
280 # Do they pass in any types we weren't already using?
281 0 foreach (keys %$what) {
282 0 ▶ next if $used_types{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $used_types{$_} 0 0 0
283 0 $declare_types++; # Found one in $what that wasn't used.
284 0 last; # And one is enough to terminate this loop
285 }
286 }
287 7 ▶ if ($declare_types) {
Branch: 1 branch
BranchTrueFalseTotal
if ($declare_types) 7 0 7
288 7 $result = $indent . 'my $types = {map {($_, 1)} qw('
289 . join (" ", sort keys %$what) . ")};\n";
290 }
291 7 local $Text::Wrap::huge = 'overflow';
292 7 local $Text::Wrap::columns = 80;
293 7 $result .= wrap ($indent . "my \@names = (qw(",
294 $indent . " ", join (" ", sort @simple) . ")");
295 7 ▶ if (@complex) {
Branch: 1 branch
BranchTrueFalseTotal
if (@complex) 3 4 7
296 3 foreach my $item (sort {$a->{name} cmp $b->{name}} @complex) {
297 23 my $name = perl_stringify $item->{name};
298 23 my $line = ",\n$indent {name=>\"$name\"";
299 23 ▶ $line .= ", type=>\"$item->{type}\"" if defined $item->{type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->{"type"} 23 0 23
300 23 foreach my $thing (qw (macro value default pre post def_pre def_post)) {
301 161 my $value = $item->{$thing};
302 161 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 27 134 161
303 27 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 3 24 27
304 $line .= ", $thing=>[\""
305 3 . join ('", "', map {perl_stringify $_} @$value) . '"]';
306 } else {
307 24 $line .= ", $thing=>\"" . perl_stringify($value) . "\"";
308 }
309 }
310 }
311 23 $line .= "}";
312 # Ensure that the enclosing C comment doesn't end
313 # by turning */ into *" . "/
314 23 $line =~ s!\*\/!\*" . "/!gs;
315 # gcc -Wall doesn't like finding /* inside a comment
316 23 $line =~ s!\/\*!/" . "\*!gs;
317 23 $result .= $line;
318 }
319 }
320 7 $result .= ");\n";
321  
322 7 $result;
323 }
324  
325 =item assign arg_hashref, VALUE...
326  
327 A method to return a suitable assignment clause. If I<type> is aggregate
328 (eg I<PVN> expects both pointer and length) then there should be multiple
329 I<VALUE>s for the components. I<pre> and I<post> if defined give snippets
330 of C code to proceed and follow the assignment. I<pre> will be at the start
331 of a block, so variables may be defined in it.
332  
333 =cut
334 # Hmm. value undef to do NOTDEF? value () to do NOTFOUND?
335  
336 74 ▶sub assign {
Subroutine assign: called CC=10 SCAR=24.4
Pod documented
337 74 my $self = shift;
338 74 my $args = shift;
339 my ($indent, $type, $pre, $post, $item)
340 74 = @{$args}{qw(indent type pre post item)};
341 74 ▶ $post ||= '';
Condition50%1/2
$post ||= ""
l!l
074
MC/DC0%0/1
$post ||= ""
$post
Truth table50%1/2
A
A= $post
Aresult
11
00
342 74 my $clause;
343 my $close;
344 74 ▶ if ($pre) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre) 1 73 74
345 1 chomp $pre;
346 1 $close = "$indent}\n";
347 1 $clause = $indent . "{\n";
348 1 $indent .= " ";
349 1 $clause .= "$indent$pre";
350 1 ▶ $clause .= ";" unless $pre =~ /;$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pre =~ /;$/ 0 1 1
351 1 $clause .= "\n";
352 }
353 74 ▶ confess "undef \$type" unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 74 74
354 74 ▶ confess "Can't generate code for type $type"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->valid_type($type) 0 74 74
355 unless $self->valid_type($type);
356  
357 74 $clause .= join '', map {"$indent$_\n"}
358 $self->assignment_clause_for_type({type=>$type,item=>$item}, @_);
359 74 chomp $post;
360 74 ▶ if (length $post) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $post) 0 74 74
361 0 $clause .= "$post";
362 0 ▶ $clause .= ";" unless $post =~ /;$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $post =~ /;$/ 0 0 0
363 0 $clause .= "\n";
364 }
365 74 my $return = $self->return_statement_for_type($type);
366 74 ▶ $clause .= "$indent$return\n" if defined $return;
Branch: 1 branch
BranchTrueFalseTotal
if defined $return 74 0 74
367 74 ▶ $clause .= $close if $close;
Branch: 1 branch
BranchTrueFalseTotal
if $close 1 73 74
368 74 return $clause;
369 }
370  
371 =item return_clause arg_hashref, ITEM
372  
373 A method to return a suitable C<#ifdef> clause. I<ITEM> is a hashref
374 (as passed to C<C_constant> and C<match_clause>. I<indent> is the number
375 of spaces to indent, defaulting to 6.
376  
377 =cut
378  
379 73 ▶sub return_clause {
Subroutine return_clause: called CC=11 SCAR=24.7
Pod documented
380  
381 ##ifdef thingy
382 # *iv_return = thingy;
383 # return PERL_constant_ISIV;
384 ##else
385 # return PERL_constant_NOTDEF;
386 ##endif
387 73 my ($self, $args, $item) = @_;
388 73 my $indent = $args->{indent};
389  
390 my ($name, $value, $default, $pre, $post, $def_pre, $def_post, $type)
391 73 = @$item{qw (name value default pre post def_pre def_post type)};
392 73 ▶ $value = $name unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 53 20 73
393 73 my $macro = $self->macro_from_item($item);
394 73 ▶ $indent = ' ' x ($indent || 6);
Condition50%1/2
$indent || 6
l!l
730
MC/DC0%0/1
$indent || 6
$indent
Truth table50%1/2
A
A= $indent
Aresult
11
00
395 73 ▶ unless (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 73 73
396 # use Data::Dumper; print STDERR Dumper ($item);
397 0 confess "undef \$type";
398 }
399  
400 ##ifdef thingy
401 73 my $clause = $self->macro_to_ifdef($macro);
402  
403 # *iv_return = thingy;
404 # return PERL_constant_ISIV;
405 73 ▶ $clause
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 1 72 73
406 .= $self->assign ({indent=>$indent, type=>$type, pre=>$pre, post=>$post,
407 item=>$item}, ref $value ? @$value : $value);
408  
409 73 ▶ if (defined $macro && $macro ne "" && $macro ne "1") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $macro and $macro ne "" and $macro ne 1) 57 16 73
Condition50%3/6
defined $macro and $macro ne "" and $macro ne 1
!ll&&!rl&&r
01657
defined $macro and $macro ne ""
!ll&&!rl&&r
0073
MC/DC33%1/3
defined $macro and $macro ne "" and $macro ne 1
defined $macro $macro ne "" $macro ne 1
Truth table50%2/4
A and B and C
A= defined $macroB= $macro ne ""C= $macro ne 1
ABCresult
0XX0
10X0
1100
1111
410 ##else
411 57 $clause .= "#else\n";
412  
413 # return PERL_constant_NOTDEF;
414 57 ▶ if (!defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $default) { } 56 1 57
415 56 my $notdef = $self->return_statement_for_notdef();
416 56 ▶ $clause .= "$indent$notdef\n" if defined $notdef;
Branch: 1 branch
BranchTrueFalseTotal
if defined $notdef 56 0 56
417 } else {
418 1 ▶ my @default = ref $default ? @$default : $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default ? : 1 0 1
419 1 $type = shift @default;
420 1 $clause .= $self->assign ({indent=>$indent, type=>$type, pre=>$pre,
421 post=>$post, item=>$item}, @default);
422 }
423 }
424 ##endif
425 73 $clause .= $self->macro_to_endif($macro);
426  
427 73 return $clause;
428 }
429  
430 71 ▶sub match_clause {
Subroutine match_clause: called CC=11 SCAR=24.0
Pod undocumented
431 # $offset defined if we have checked an offset.
432 71 my ($self, $args, $item) = @_;
433 71 my ($offset, $indent) = @{$args}{qw(checked_at indent)};
434 71 ▶ $indent = ' ' x ($indent || 4);
Condition100%2/2
$indent || 4
l!l
6011
MC/DC100%1/1
$indent || 4
$indent
Truth table100%2/2
A
A= $indent
Aresult
11
00
435 71 my $body = '';
436 71 my ($no, $yes, $either, $name, $inner_indent);
437 71 ▶ if (ref $item eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 11 60 71
438 11 ($yes, $no) = @$item;
439 11 ▶ $either = $yes || $no;
Condition66%2/3
$yes || $no
l!l&&r!l&&!r
740
MC/DC0%0/2
$yes || $no
$yes $no
Truth table66%2/3
A || B
A= $yesB= $no
ABresult
1X1
011
000
440 11 ▶ confess "$item is $either expecting hashref in [0] || [1]"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $either eq "HASH" 0 11 11
441 unless ref $either eq 'HASH';
442 11 $name = $either->{name};
443 } else {
444 confess "$item->{name} has utf8 flag '$item->{utf8}', should be false"
445 60 ▶ if $item->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"utf8"} 0 60 60
446 60 $name = $item->{name};
447 60 $inner_indent = $indent;
448 }
449  
450 71 $body .= $self->memEQ_clause ({name => $name, checked_at => $offset,
451 indent => length $indent});
452 # If we've been presented with an arrayref for $item, then the user string
453 # contains in the range 128-255, and we need to check whether it was utf8
454 # (or not).
455 # In the worst case we have two named constants, where one's name happens
456 # encoded in UTF8 happens to be the same byte sequence as the second's
457 # encoded in (say) ISO-8859-1.
458 # In this case, $yes and $no both have item hashrefs.
459 71 ▶ if ($yes) {
Branch: 2 branches
BranchTrueFalseTotal
if ($yes) { } 7 64 71
elsif ($no) { } 4 60 64
460 7 $body .= $indent . " if (" . $self->is_utf8_param . ") {\n";
461 } elsif ($no) {
462 4 $body .= $indent . " if (!" . $self->is_utf8_param . ") {\n";
463 }
464 71 ▶ if ($either) {
Branch: 1 branch
BranchTrueFalseTotal
if ($either) { } 11 60 71
465 11 $body .= $self->return_clause ({indent=>4 + length $indent}, $either);
466 11 ▶ if ($yes and $no) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yes and $no) 2 9 11
Condition100%3/3
$yes and $no
!ll&&!rl&&r
452
MC/DC100%2/2
$yes and $no
$yes $no
Truth table100%3/3
A and B
A= $yesB= $no
ABresult
0X0
100
111
467 2 $body .= $indent . " } else {\n";
468 2 $body .= $self->return_clause ({indent=>4 + length $indent}, $no);
469 }
470 11 $body .= $indent . " }\n";
471 } else {
472 60 $body .= $self->return_clause ({indent=>2 + length $indent}, $item);
473 }
474 71 $body .= $indent . "}\n";
475 }
476  
477  
478 =item switch_clause arg_hashref, NAMELEN, ITEMHASH, ITEM...
479  
480 An internal method to generate a suitable C<switch> clause, called by
481 C<C_constant> I<ITEM>s are in the hash ref format as given in the description
482 of C<C_constant>, and must all have the names of the same length, given by
483 I<NAMELEN>. I<ITEMHASH> is a reference to a hash, keyed by name, values being
484 the hashrefs in the I<ITEM> list. (No parameters are modified, and there can
485 be keys in the I<ITEMHASH> that are not in the list of I<ITEM>s without
486 causing problems - the hash is passed in to save generating it afresh for
487 each call).
488  
489 =cut
490  
491 14 ▶sub switch_clause {
Subroutine switch_clause: called CC=30 SCAR=34.4
Pod documented
492 14 my ($self, $args, $namelen, $items, @items) = @_;
493 14 my ($indent, $comment) = @{$args}{qw(indent comment)};
494 14 ▶ $indent = ' ' x ($indent || 2);
Condition50%1/2
$indent || 2
l!l
140
MC/DC0%0/1
$indent || 2
$indent
Truth table50%1/2
A
A= $indent
Aresult
11
00
495  
496 14 local $Text::Wrap::huge = 'overflow';
497 14 local $Text::Wrap::columns = 80;
498  
499 14 my @names = sort map {$_->{name}} @items;
500 14 my $leader = $indent . '/* ';
501 14 my $follower = ' ' x length $leader;
502 14 my $body = $indent . "/* Names all of length $namelen. */\n";
503 14 ▶ if (defined $comment) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment) 10 4 14
504 10 $body = wrap ($leader, $follower, $comment) . "\n";
505 10 $leader = $follower;
506 }
507 14 my @safe_names = @names;
508 14 foreach (@safe_names) {
509 60 ▶ confess sprintf "Name '$_' is length %d, not $namelen", length
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ == $namelen 0 60 60
510 unless length == $namelen;
511 # Argh. 5.6.1
512 # next unless tr/A-Za-z0-9_//c;
513 60 ▶ next if tr/A-Za-z0-9_// == length;
Branch: 1 branch
BranchTrueFalseTotal
if tr/0-9A-Z_a-z// == length $_ 49 11 60
514 11 $_ = '"' . perl_stringify ($_) . '"';
515 # Ensure that the enclosing C comment doesn't end
516 # by turning */ into *" . "/
517 11 s!\*\/!\*"."/!gs;
518 # gcc -Wall doesn't like finding /* inside a comment
519 11 s!\/\*!/"."\*!gs;
520 }
521 14 $body .= wrap ($leader, $follower, join (" ", @safe_names) . " */") . "\n";
522 # Figure out what to switch on.
523 # (RMS, Spread of jump table, Position, Hashref)
524 14 my @best = (1e38, ~0);
525 # Prefer the last character over the others. (As it lets us shorten the
526 # memEQ clause at no cost).
527 14 foreach my $i ($namelen - 1, 0 .. ($namelen - 2)) {
528 49 my ($min, $max) = (~0, 0);
529 49 my %spread;
530 49 if (is_perl56) {
531 # Need proper Unicode preserving hash keys for bytes in range 128-255
532 # here too, for some reason. grr 5.6.1 yet again.
533 tie %spread, 'ExtUtils::Constant::Aaargh56Hash';
534 }
535 49 foreach (@names) {
536 176 my $char = substr $_, $i, 1;
537 176 my $ord = ord $char;
538 176 ▶ confess "char $ord is out of range" if $ord > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $ord > 255 0 176 176
539 176 ▶ $max = $ord if $ord > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $ord > $max 118 58 176
540 176 ▶ $min = $ord if $ord < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $ord < $min 56 120 176
541 176 push @{$spread{$char}}, $_;
542 # warn "$_ $char";
543 }
544 # I'm going to pick the character to split on that minimises the root
545 # mean square of the number of names in each case. Normally this should
546 # be the one with the most keys, but it may pick a 7 where the 8 has
547 # one long linear search. I'm not sure if RMS or just sum of squares is
548 # actually better.
549 # $max and $min are for the tie-breaker if the root mean squares match.
550 # Assuming that the compiler may be building a jump table for the
551 # switch() then try to minimise the size of that jump table.
552 # Finally use < not <= so that if it still ties the earliest part of
553 # the string wins. Because if that passes but the memEQ fails, it may
554 # only need the start of the string to bin the choice.
555 # I think. But I'm micro-optimising. :-)
556 # OK. Trump that. Now favour the last character of the string, before the
557 # rest.
558 49 my $ss;
559 49 $ss += @$_ * @$_ foreach values %spread;
560 49 my $rms = sqrt ($ss / keys %spread);
561 49 ▶ if ($rms < $best[0] || ($rms == $best[0] && ($max - $min) < $best[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rms < $best[0] or $rms == $best[0] and $max - $min < $best[1]) 22 27 49
Condition100%6/6
$rms < $best[0] or $rms == $best[0] and $max - $min < $best[1]
l!l&&r!l&&!r
16627
$rms == $best[0] and $max - $min < $best[1]
!ll&&!rl&&r
10176
MC/DC100%3/3
$rms < $best[0] or $rms == $best[0] and $max - $min < $best[1]
$rms < $best[0] $rms == $best[0] $max - $min < $best[1]
Truth table100%4/4
A or B and C
A= $rms < $best[0]B= $rms == $best[0]C= $max - $min < $best[1]
ABCresult
1XX1
0111
00X0
0100
562 22 @best = ($rms, $max - $min, $i, \%spread);
563 }
564 }
565 14 ▶ confess "Internal error. Failed to pick a switch point for @names"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $best[2] 0 14 14
566 unless defined $best[2];
567 # use Data::Dumper; print Dumper (@best);
568 14 my ($offset, $best) = @best[2,3];
569 14 $body .= $indent . "/* Offset $offset gives the best switch position. */\n";
570  
571 14 ▶ my $do_front_chop = $offset == 0 && $namelen > 2;
Condition100%3/3
$offset == 0 && $namelen > 2
!ll&&!rl&&r
1112
MC/DC100%2/2
$offset == 0 && $namelen > 2
$offset == 0 $namelen > 2
Truth table100%3/3
A && B
A= $offset == 0B= $namelen > 2
ABresult
0X0
100
111
572 14 ▶ if ($do_front_chop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_front_chop) { } 2 12 14
573 2 $body .= $indent . "switch (*" . $self->name_param() . "++) {\n";
574 } else {
575 12 $body .= $indent . "switch (" . $self->name_param() . "[$offset]) {\n";
576 }
577 14 foreach my $char (sort keys %$best) {
578 54 ▶ confess sprintf "'$char' is %d bytes long, not 1", length $char
Branch: 1 branch
BranchTrueFalseTotal
if length $char != 1 0 54 54
579 if length ($char) != 1;
580 54 ▶ confess sprintf "char %#X is out of range", ord $char if ord ($char) > 255;
Branch: 1 branch
BranchTrueFalseTotal
if ord $char > 255 0 54 54
581 54 $body .= $indent . "case '" . C_stringify ($char) . "':\n";
582 54 foreach my $thisone (sort {
583 # Deal with the case of an item actually being an array ref to 1 or 2
584 # hashrefs. Don't assign to $a or $b, as they're aliases to the
585 # original
586 7 ▶ my $l = ref $a eq 'ARRAY' ? ($a->[0] || $->[1]) : $a;
Branch: 1 branch
BranchTrueFalseTotal
ref $a eq 'ARRAY' ? : 1 6 7
Condition33%1/3
$ExtUtils::Constant::Base::a->[0] or $- > [1]
l!l&&r!l&&!r
100
MC/DC0%0/2
$ExtUtils::Constant::Base::a->[0] or $- > [1]
$ExtUtils::Constant::Base::a->[0] $- > [1]
Truth table33%1/3
A or B
A= $ExtUtils::Constant::Base::a->[0]B= $- > [1]
ABresult
1X1
011
000
587 7 ▶ my $r = ref $b eq 'ARRAY' ? ($b->[0] || $->[1]) : $b;
Branch: 1 branch
BranchTrueFalseTotal
ref $b eq 'ARRAY' ? : 1 6 7
Condition33%1/3
$ExtUtils::Constant::Base::b->[0] or $- > [1]
l!l&&r!l&&!r
100
MC/DC0%0/2
$ExtUtils::Constant::Base::b->[0] or $- > [1]
$ExtUtils::Constant::Base::b->[0] $- > [1]
Truth table33%1/3
A or B
A= $ExtUtils::Constant::Base::b->[0]B= $- > [1]
ABresult
1X1
011
000
588 # Sort by weight first
589 ($r->{weight} || 0) <=> ($l->{weight} || 0)
590 # Sort equal weights by name
591 7 ▶ or $l->{name} cmp $r->{name}}
Condition57%4/7
($r->{'weight'} || 0) <=> ($l->{'weight'} || 0) || $l->{'name'} cmp $r->{'name'}
l!l&&r!l&&!r
061
$r->{'weight'} || 0
l!l
07
$l->{'weight'} || 0
l!l
07
MC/DC50%1/2
($r->{'weight'} || 0) <=> ($l->{'weight'} || 0) || $l->{'name'} cmp $r->{'name'}
$r->{'weight'} $l->{'name'} cmp $r->{'name'}
MC/DC0%0/1
$l->{'weight'} || 0
$l->{'weight'}
Truth table66%2/3
A || B
A= $r->{'weight'}B= $l->{'name'} cmp $r->{'name'}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $l->{'weight'}
Aresult
11
00
592 # If this looks evil, maybe it is. $items is a
593 # hashref, and we're doing a hash slice on it
594 54 @{$items}{@{$best->{$char}}}) {
595 # warn "You are here";
596 60 ▶ if ($do_front_chop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_front_chop) { } 14 46 60
597 14 $body .= $self->match_clause ({indent => 2 + length $indent,
598 checked_at => \$char}, $thisone);
599 } else {
600 46 $body .= $self->match_clause ({indent => 2 + length $indent,
601 checked_at => $offset}, $thisone);
602 }
603 }
604 54 $body .= $indent . " break;\n";
605 }
606 14 $body .= $indent . "}\n";
607 14 return $body;
608 }
609  
610 17 ▶sub C_constant_return_type {
Subroutine C_constant_return_type: called CC=1 SCAR=0.0
Pod undocumented
611 17 "static int";
612 }
613  
614 0 ▶sub C_constant_prefix_param {
Subroutine C_constant_prefix_param: not called CC=1 SCAR=6.9
Pod undocumented
615 0 '';
616 }
617  
618 0 ▶sub C_constant_prefix_param_defintion {
Subroutine C_constant_prefix_param_defintion: not called CC=1 SCAR=6.9
Pod undocumented
619 0 '';
620 }
621  
622 17 ▶sub name_param_definition {
Subroutine name_param_definition: called CC=1 SCAR=0.0
Pod undocumented
623 17 "const char *" . $_[0]->name_param;
624 }
625  
626 14 ▶sub namelen_param {
Subroutine namelen_param: called CC=1 SCAR=0.0
Pod undocumented
627 14 'len';
628 }
629  
630 0 ▶sub namelen_param_definition {
Subroutine namelen_param_definition: not called CC=1 SCAR=6.9
Pod undocumented
631 0 'size_t ' . $_[0]->namelen_param;
632 }
633  
634 0 ▶sub C_constant_other_params {
Subroutine C_constant_other_params: not called CC=1 SCAR=6.9
Pod undocumented
635 0 '';
636 }
637  
638 0 ▶sub C_constant_other_params_defintion {
Subroutine C_constant_other_params_defintion: not called CC=1 SCAR=6.9
Pod undocumented
639 0 '';
640 }
641  
642 =item params WHAT
643  
644 An "internal" method, subject to change, currently called to allow an
645 overriding class to cache information that will then be passed into all
646 the C<*param*> calls. (Yes, having to read the source to make sense of this is
647 considered a known bug). I<WHAT> is be a hashref of types the constant
648 function will return. In ExtUtils::Constant::XS this method is used to
649 returns a hashref keyed IV NV PV SV to show which combination of pointers will
650 be needed in the C argument list generated by
651 C_constant_other_params_definition and C_constant_other_params
652  
653 =cut
654  
655 0 ▶sub params {
Subroutine params: not called CC=1 SCAR=6.9
Pod documented
656 0 '';
657 }
658  
659  
660 =item dogfood arg_hashref, ITEM...
661  
662 An internal function to generate the embedded perl code that will regenerate
663 the constant subroutines. Parameters are the same as for C_constant.
664  
665 Currently the base class does nothing and returns an empty string.
666  
667 =cut
668  
669 0 ▶sub dogfood {
Subroutine dogfood: not called CC=1 SCAR=6.9
Pod documented
670 0 ''
671 }
672  
673 =item normalise_items args, default_type, seen_types, seen_items, ITEM...
674  
675 Convert the items to a normalised form. For 8 bit and Unicode values converts
676 the item to an array of 1 or 2 items, both 8 bit and UTF-8 encoded.
677  
678 =cut
679  
680 sub normalise_items
681 8 ▶{
Subroutine normalise_items: called CC=22 SCAR=32.3
Pod documented
682 8 my $self = shift;
683 8 my $args = shift;
684 8 my $default_type = shift;
685 8 my $what = shift;
686 8 my $items = shift;
687 8 my @new_items;
688 8 foreach my $orig (@_) {
689 91 my ($name, $item);
690 91 ▶ if (ref $orig) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $orig) { } 37 54 91
691 # Make a copy which is a normalised version of the ref passed in.
692 37 $name = $orig->{name};
693 37 my ($type, $macro, $value) = @$orig{qw (type macro value)};
694 37 ▶ $type ||= $default_type;
Condition66%2/3
$type ||= $default_type
l!l&&r!l&&!r
3520
MC/DC0%0/2
$type ||= $default_type
$type $default_type
Truth table66%2/3
A ||= B
A= $typeB= $default_type
ABresult
1X1
011
000
695 37 $what->{$type} = 1;
696 37 $item = {name=>$name, type=>$type};
697  
698 37 ▶ undef $macro if defined $macro and $macro eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $macro and $macro eq $name 0 37 37
Condition66%2/3
defined $macro and $macro eq $name
!ll&&!rl&&r
21160
MC/DC0%0/2
defined $macro and $macro eq $name
defined $macro $macro eq $name
Truth table66%2/3
A and B
A= defined $macroB= $macro eq $name
ABresult
0X0
100
111
699 37 ▶ $item->{macro} = $macro if defined $macro;
Branch: 1 branch
BranchTrueFalseTotal
if defined $macro 16 21 37
700 37 ▶ undef $value if defined $value and $value eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and $value eq $name 0 37 37
Condition66%2/3
defined $value and $value eq $name
!ll&&!rl&&r
15220
MC/DC0%0/2
defined $value and $value eq $name
defined $value $value eq $name
Truth table66%2/3
A and B
A= defined $valueB= $value eq $name
ABresult
0X0
100
111
701 37 ▶ $item->{value} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 22 15 37
702 37 foreach my $key (qw(default pre post def_pre def_post weight
703 not_constant)) {
704 259 my $value = $orig->{$key};
705 259 ▶ $item->{$key} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 4 255 259
706 # warn "$key $value";
707 }
708 } else {
709 54 $name = $orig;
710 54 $item = {name=>$name, type=>$default_type};
711 54 $what->{$default_type} = 1;
712 }
713 warn +(ref ($self) || $self)
714 . "doesn't know how to handle values of type $_ used in macro $name"
715 91 ▶ unless $self->valid_type ($item->{type});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->valid_type($item->{"type"}) 0 91 91
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
716 # tr///c is broken on 5.6.1 for utf8, so my original tr/\0-\177//c
717 # doesn't work. Upgrade to 5.8
718 # if ($name !~ tr/\0-\177//c || $] < 5.005_50) {
719 91 ▶ if ($name !~ /[[:^ascii:]]/ || $] < 5.005_50
Branch: 1 branch
BranchTrueFalseTotal
if (not $name =~ /[[:^ascii:]]/ or $] < 5.0055 or $args->{'disable_utf8_duplication'}) { } 85 6 91
Condition83%5/6
not $name =~ /[[:^ascii:]]/ or $] < "5.0055" or $args->{"disable_utf8_duplication"}
l!l&&r!l&&!r
8416
not $name =~ /[[:^ascii:]]/ or $] < "5.0055"
l!l&&r!l&&!r
8407
MC/DC66%2/3
not $name =~ /[[:^ascii:]]/ or $] < "5.0055" or $args->{"disable_utf8_duplication"}
not $name =~ /[[:^ascii:]]/ $] < "5.0055" $args->{"disable_utf8_duplication"}
Truth table75%3/4
not(A) or B or C
A= not $name =~ /[[:^ascii:]]/B= $] < "5.0055"C= $args->{"disable_utf8_duplication"}
ABCresult
1XX1
01X1
0011
0000
720 || $args->{disable_utf8_duplication}) {
721 # No characters outside 7 bit ASCII.
722 85 ▶ if (exists $items->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $items->{$name}) 0 85 85
723 0 die "Multiple definitions for macro $name";
724 }
725 85 $items->{$name} = $item;
726 } else {
727 # No characters outside 8 bit. This is hardest.
728 6 ▶ if (exists $items->{$name} and ref $items->{$name} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $items->{$name} and ref $items->{$name} ne "ARRAY") 0 6 6
Condition66%2/3
exists $items->{$name} and ref $items->{$name} ne "ARRAY"
!ll&&!rl&&r
510
MC/DC0%0/2
exists $items->{$name} and ref $items->{$name} ne "ARRAY"
exists $items->{$name} ref $items->{$name} ne "ARRAY"
Truth table66%2/3
A and B
A= exists $items->{$name}B= ref $items->{$name} ne "ARRAY"
ABresult
0X0
100
111
729 0 confess "Unexpected ASCII definition for macro $name";
730 }
731 # Again, 5.6.1 tr broken, so s/5\.6.*/5\.8\.0/;
732 # if ($name !~ tr/\0-\377//c) {
733 6 ▶ if ($name =~ tr/\0-\377// == length $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ tr/\000-\377// == length $name) 5 1 6
734 # if ($] < 5.007) {
735 # $name = pack "C*", unpack "U*", $name;
736 # }
737 5 $item->{utf8} = 'no';
738 5 $items->{$name}[1] = $item;
739 5 push @new_items, $item;
740 # Copy item, to create the utf8 variant.
741 5 $item = {%$item};
742 }
743 # Encode the name as utf8 bytes.
744 6 unless (is_perl56) {
745 6 utf8::encode($name);
746 } else {
747 # warn "Was >$name< " . length ${name};
748 $name = pack 'C*', unpack 'C*', $name . pack 'U*';
749 # warn "Now '${name}' " . length ${name};
750 }
751 6 ▶ if ($items->{$name}[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($items->{$name}[0]) 0 6 6
752 0 die "Multiple definitions for macro $name";
753 }
754 6 $item->{utf8} = 'yes';
755 6 $item->{name} = $name;
756 6 $items->{$name}[0] = $item;
757 # We have need for the utf8 flag.
758 6 $what->{''} = 1;
759 }
760 91 push @new_items, $item;
761 }
762 8 @new_items;
763 }
764  
765 =item C_constant arg_hashref, ITEM...
766  
767 A function that returns a B<list> of C subroutine definitions that return
768 the value and type of constants when passed the name by the XS wrapper.
769 I<ITEM...> gives a list of constant names. Each can either be a string,
770 which is taken as a C macro name, or a reference to a hash with the following
771 keys
772  
773 =over 8
774  
775 =item name
776  
777 The name of the constant, as seen by the perl code.
778  
779 =item type
780  
781 The type of the constant (I<IV>, I<NV> etc)
782  
783 =item value
784  
785 A C expression for the value of the constant, or a list of C expressions if
786 the type is aggregate. This defaults to the I<name> if not given.
787  
788 =item macro
789  
790 The C pre-processor macro to use in the C<#ifdef>. This defaults to the
791 I<name>, and is mainly used if I<value> is an C<enum>. If a reference an
792 array is passed then the first element is used in place of the C<#ifdef>
793 line, and the second element in place of the C<#endif>. This allows
794 pre-processor constructions such as
795  
796 #if defined (foo)
797 #if !defined (bar)
798 ...
799 #endif
800 #endif
801  
802 to be used to determine if a constant is to be defined.
803  
804 A "macro" 1 signals that the constant is always defined, so the C<#if>/C<#endif>
805 test is omitted.
806  
807 =item default
808  
809 Default value to use (instead of C<croak>ing with "your vendor has not
810 defined...") to return if the macro isn't defined. Specify a reference to
811 an array with type followed by value(s).
812  
813 =item pre
814  
815 C code to use before the assignment of the value of the constant. This allows
816 you to use temporary variables to extract a value from part of a C<struct>
817 and return this as I<value>. This C code is places at the start of a block,
818 so you can declare variables in it.
819  
820 =item post
821  
822 C code to place between the assignment of value (to a temporary) and the
823 return from the function. This allows you to clear up anything in I<pre>.
824 Rarely needed.
825  
826 =item def_pre
827  
828 =item def_post
829  
830 Equivalents of I<pre> and I<post> for the default value.
831  
832 =item utf8
833  
834 Generated internally. Is zero or undefined if name is 7 bit ASCII,
835 "no" if the name is 8 bit (and so should only match if SvUTF8() is false),
836 "yes" if the name is utf8 encoded.
837  
838 The internals automatically clone any name with characters 128-255 but none
839 256+ (ie one that could be either in bytes or utf8) into a second entry
840 which is utf8 encoded.
841  
842 =item weight
843  
844 Optional sorting weight for names, to determine the order of
845 linear testing when multiple names fall in the same case of a switch clause.
846 Higher comes earlier, undefined defaults to zero.
847  
848 =back
849  
850 In the argument hashref, I<package> is the name of the package, and is only
851 used in comments inside the generated C code. I<subname> defaults to
852 C<constant> if undefined.
853  
854 I<default_type> is the type returned by C<ITEM>s that don't specify their
855 type. It defaults to the value of C<default_type()>. I<types> should be given
856 either as a comma separated list of types that the C subroutine I<subname>
857 will generate or as a reference to a hash. I<default_type> will be added to
858 the list if not present, as will any types given in the list of I<ITEM>s. The
859 resultant list should be the same list of types that C<XS_constant> is
860 given. [Otherwise C<XS_constant> and C<C_constant> may differ in the number of
861 parameters to the constant function. I<indent> is currently unused and
862 ignored. In future it may be used to pass in information used to change the C
863 indentation style used.] The best way to maintain consistency is to pass in a
864 hash reference and let this function update it.
865  
866 I<breakout> governs when child functions of I<subname> are generated. If there
867 are I<breakout> or more I<ITEM>s with the same length of name, then the code
868 to switch between them is placed into a function named I<subname>_I<len>, for
869 example C<constant_5> for names 5 characters long. The default I<breakout> is
870 3. A single C<ITEM> is always inlined.
871  
872 =cut
873  
874 # The parameter now BREAKOUT was previously documented as:
875 #
876 # I<NAMELEN> if defined signals that all the I<name>s of the I<ITEM>s are of
877 # this length, and that the constant name passed in by perl is checked and
878 # also of this length. It is used during recursion, and should be C<undef>
879 # unless the caller has checked all the lengths during code generation, and
880 # the generated subroutine is only to be called with a name of this length.
881 #
882 # As you can see it now performs this function during recursion by being a
883 # scalar reference.
884  
885 17 ▶sub C_constant {
Subroutine C_constant: called CC=20 SCAR=30.0
Pod documented
886 17 my ($self, $args, @items) = @_;
887 my ($package, $subname, $default_type, $what, $indent, $breakout) =
888 17 @{$args}{qw(package subname default_type types indent breakout)};
889 17 ▶ $package ||= 'Foo';
Condition50%1/2
$package ||= "Foo"
l!l
170
MC/DC0%0/1
$package ||= "Foo"
$package
Truth table50%1/2
A
A= $package
Aresult
11
00
890 17 ▶ $subname ||= 'constant';
Condition100%2/2
$subname ||= "constant"
l!l
161
MC/DC100%1/1
$subname ||= "constant"
$subname
Truth table100%2/2
A
A= $subname
Aresult
11
00
891 # I'm not using this. But a hashref could be used for full formatting without
892 # breaking this API
893 # $indent ||= 0;
894  
895 17 my ($namelen, $items);
896 17 ▶ if (ref $breakout) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $breakout) { } 10 7 17
897 # We are called recursively. We trust @items to be normalised, $what to
898 # be a hashref, and pinch %$items from our parent to save recalculation.
899 10 ($namelen, $items) = @$breakout;
900 } else {
901 7 $items = {};
902 7 if (is_perl56) {
903 # Need proper Unicode preserving hash keys.
904 require ExtUtils::Constant::Aaargh56Hash;
905 tie %$items, 'ExtUtils::Constant::Aaargh56Hash';
906 }
907 7 ▶ $breakout ||= 3;
Condition50%1/2
$breakout ||= 3
l!l
07
MC/DC0%0/1
$breakout ||= 3
$breakout
Truth table50%1/2
A
A= $breakout
Aresult
11
00
908 7 ▶ $default_type ||= $self->default_type();
Condition66%2/3
$default_type ||= $self->default_type
l!l&&r!l&&!r
610
MC/DC0%0/2
$default_type ||= $self->default_type
$default_type $self->default_type
Truth table66%2/3
A ||= B
A= $default_typeB= $self->default_type
ABresult
1X1
011
000
909 7 ▶ if (!ref $what) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $what) 1 6 7
910 # Convert line of the form IV,UV,NV to hash
911 0 ▶ $what = {map {$_ => 1} split /,\s*/, ($what || '')};
Condition50%1/2
$what || ''
l!l
01
MC/DC0%0/1
$what || ''
$what
Truth table50%1/2
A
A= $what
Aresult
11
00
912 # Figure out what types we're dealing with, and assign all unknowns to the
913 # default type
914 }
915 7 @items = $self->normalise_items ({}, $default_type, $what, $items, @items);
916 # use Data::Dumper; print Dumper @items;
917 }
918 17 my $params = $self->params ($what);
919  
920 # Probably "static int"
921 17 my ($body, @subs);
922 17 $body = $self->C_constant_return_type($params) . "\n$subname ("
923 # Eg "pTHX_ "
924 . $self->C_constant_prefix_param_defintion($params)
925 # Probably "const char *name"
926 . $self->name_param_definition($params);
927 # Something like ", STRLEN len"
928 17 ▶ $body .= ", " . $self->namelen_param_definition($params)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $namelen 7 10 17
929 unless defined $namelen;
930 17 $body .= $self->C_constant_other_params_defintion($params);
931 17 $body .= ") {\n";
932  
933 17 ▶ if (defined $namelen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namelen) { } 10 7 17
934 # We are a child subroutine. Print the simple description
935 10 my $comment = 'When generated this function returned values for the list'
936 . ' of names given here. However, subsequent manual editing may have'
937 . ' added or removed some.';
938 10 $body .= $self->switch_clause ({indent=>2, comment=>$comment},
939 $namelen, $items, @items);
940 } else {
941 # We are the top level.
942 7 $body .= " /* Initially switch on the length of the name. */\n";
943 7 $body .= $self->dogfood ({package => $package, subname => $subname,
944 default_type => $default_type, what => $what,
945 indent => $indent, breakout => $breakout},
946 @items);
947 7 $body .= ' switch ('.$self->namelen_param().") {\n";
948 # Need to group names of the same length
949 7 my @by_length;
950 7 foreach (@items) {
951 71 push @{$by_length[length $_->{name}]}, $_;
952 }
953 7 foreach my $i (0 .. $#by_length) {
954 37 ▶ next unless $by_length[$i]; # None of this length
Branch: 1 branch
BranchTrueFalseTotal
unless $by_length[$i] 12 25 37
955 25 $body .= " case $i:\n";
956 25 ▶ if (@{$by_length[$i]} == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$by_length[$i];} == 1) { } 11 14 25
elsif (@{$by_length[$i];} < $breakout) { } 4 10 14
957 11 my $only_thing = $by_length[$i]->[0];
958 11 ▶ if ($only_thing->{utf8}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($only_thing->{'utf8'}) { } 4 7 11
959 4 ▶ if ($only_thing->{utf8} eq 'yes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($only_thing->{'utf8'} eq 'yes') { } 2 2 4
960 # With utf8 on flag item is passed in element 0
961 2 $body .= $self->match_clause (undef, [$only_thing]);
962 } else {
963 # With utf8 off flag item is passed in element 1
964 2 $body .= $self->match_clause (undef, [undef, $only_thing]);
965 }
966 } else {
967 7 $body .= $self->match_clause (undef, $only_thing);
968 }
969 14 } elsif (@{$by_length[$i]} < $breakout) {
970 $body .= $self->switch_clause ({indent=>4},
971 4 $i, $items, @{$by_length[$i]});
972 } else {
973 # Only use the minimal set of parameters actually needed by the types
974 # of the names of this length.
975 10 my $what = {};
976 10 foreach (@{$by_length[$i]}) {
977 52 $what->{$_->{type}} = 1;
978 52 ▶ $what->{''} = 1 if $_->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"utf8"} 7 45 52
979 }
980 10 $params = $self->params ($what);
981 push @subs, $self->C_constant ({package=>$package,
982 subname=>"${subname}_$i",
983 default_type => $default_type,
984 types => $what, indent => $indent,
985 breakout => [$i, $items]},
986 10 @{$by_length[$i]});
987 10 $body .= " return ${subname}_$i ("
988 # Eg "aTHX_ "
989 . $self->C_constant_prefix_param($params)
990 # Probably "name"
991 . $self->name_param($params);
992 10 $body .= $self->C_constant_other_params($params);
993 10 $body .= ");\n";
994 }
995 25 $body .= " break;\n";
996 }
997 7 $body .= " }\n";
998 }
999 17 my $notfound = $self->return_statement_for_notfound();
1000 17 ▶ $body .= " $notfound\n" if $notfound;
Branch: 1 branch
BranchTrueFalseTotal
if $notfound 17 0 17
1001 17 $body .= "}\n";
1002 17 return (@subs, $body);
1003 }
1004  
1005 1 1;
1006 __END__
« blib/lib/ExtUtils/Constant.pm ↑ Summary blib/lib/ExtUtils/Constant/XS.pm »