Install/FrozenShit/Typemaps.pm

filter: Statementstmt 58.0% 217 / 374 Branchbran 42.1% 75 / 178 Conditioncond 30.6% 19 / 62 MC/DCmcdc 5.0% 2 / 40 Subroutinesub 54.5% 18 / 33 Podpod 94.7% 18 / 19 totaltotal 49.4% 349 / 706 CC 124 SCAR 29.3
CC
124
Cov
51%
as_string
56.0
validate
56.0
_parse
26.7
CRAP 18.8
« Install/Deps.pm ↑ Summary Install/FrozenShit/ParseXS/Utilities.pm »
1 package
2 XS::Install::FrozenShit::Typemaps;
3 1 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = '3.57';
7  
8 1 require XS::Install::FrozenShit::ParseXS;
9 1 require XS::Install::FrozenShit::ParseXS::Constants;
10 1 require XS::Install::FrozenShit::Typemaps::InputMap;
11 1 require XS::Install::FrozenShit::Typemaps::OutputMap;
12 1 require XS::Install::FrozenShit::Typemaps::Type;
13  
14 =head1 NAME
15  
16 XS::Install::FrozenShit::Typemaps - Read/Write/Modify Perl/XS typemap files
17  
18 =head1 SYNOPSIS
19  
20 # read/create file
21 my $typemap = XS::Install::FrozenShit::Typemaps->new(file => 'typemap');
22 # alternatively create an in-memory typemap
23 # $typemap = XS::Install::FrozenShit::Typemaps->new();
24 # alternatively create an in-memory typemap by parsing a string
25 # $typemap = XS::Install::FrozenShit::Typemaps->new(string => $sometypemap);
26  
27 # add a mapping
28 $typemap->add_typemap(ctype => 'NV', xstype => 'T_NV');
29 $typemap->add_inputmap(
30 xstype => 'T_NV', code => '$var = ($type)SvNV($arg);'
31 );
32 $typemap->add_outputmap(
33 xstype => 'T_NV', code => 'sv_setnv($arg, (NV)$var);'
34 );
35 $typemap->add_string(string => $typemapstring);
36 # will be parsed and merged
37  
38 # remove a mapping (same for remove_typemap and remove_outputmap...)
39 $typemap->remove_inputmap(xstype => 'SomeType');
40  
41 # save a typemap to a file
42 $typemap->write(file => 'anotherfile.map');
43  
44 # merge the other typemap into this one
45 $typemap->merge(typemap => $another_typemap);
46  
47 =head1 DESCRIPTION
48  
49 This module can read, modify, create and write Perl XS typemap files. If you don't know
50 what a typemap is, please confer the L<perlxstut> and L<perlxs> manuals.
51  
52 The module is not entirely round-trip safe: For example it currently simply strips all comments.
53 The order of entries in the maps is, however, preserved.
54  
55 We check for duplicate entries in the typemap, but do not check for missing
56 C<TYPEMAP> entries for C<INPUTMAP> or C<OUTPUTMAP> entries since these might be hidden
57 in a different typemap.
58  
59 =head1 METHODS
60  
61 =cut
62  
63 =head2 new
64  
65 Returns a new typemap object. Takes an optional C<file> parameter.
66 If set, the given file will be read. If the file doesn't exist, an empty typemap
67 is returned.
68  
69 Alternatively, if the C<string> parameter is given, the supplied
70 string will be parsed instead of a file.
71  
72 =cut
73  
74 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
75 3 my $class = shift;
76 3 my %args = @_;
77  
78 3 ▶ if (defined $args{file} and defined $args{string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"file"} and defined $args{"string"}) 0 3 3
Condition66%2/3
defined $args{"file"} and defined $args{"string"}
!ll&&!rl&&r
120
MC/DC0%0/2
defined $args{"file"} and defined $args{"string"}
defined $args{"file"} defined $args{"string"}
Truth table66%2/3
A and B
A= defined $args{"file"}B= defined $args{"string"}
ABresult
0X0
100
111
79 0 die("Cannot handle both 'file' and 'string' arguments to constructor");
80 }
81  
82 3 my $self = bless {
83 file => undef,
84 %args,
85 typemap_section => [],
86 typemap_lookup => {},
87 input_section => [],
88 input_lookup => {},
89 output_section => [],
90 output_lookup => {},
91 } => $class;
92  
93 3 $self->_init();
94  
95 3 return $self;
96 }
97  
98 3 ▶sub _init {
Subroutine _init: called CC=5 SCAR=17.3
99 3 my $self = shift;
100 3 ▶ if (defined $self->{string}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'string'}) { } 0 3 3
elsif (defined $self->{'file'} and -e $self->{'file'}) { } 2 2 4
101 0 $self->_parse(\($self->{string}), $self->{lineno_offset}, $self->{fake_filename});
102 0 delete $self->{string};
103 }
104 ▶ elsif (defined $self->{file} and -e $self->{file}) {
Condition66%2/3
defined $self->{"file"} and -e $self->{"file"}
!ll&&!rl&&r
102
MC/DC50%1/2
defined $self->{"file"} and -e $self->{"file"}
defined $self->{"file"} -e $self->{"file"}
Truth table66%2/3
A and B
A= defined $self->{"file"}B= -e $self->{"file"}
ABresult
0X0
100
111
105 open my $fh, '<', $self->{file}
106 or die "Cannot open typemap file '"
107 2 ▶ . $self->{file} . "' for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"file"} or die q[Cannot open typemap file '] . $self->{"file"} . "' for reading: $!" 2 0 2
108 2 local $/ = undef;
109 2 my $string = <$fh>;
110 2 $self->_parse(\$string, $self->{lineno_offset}, $self->{file});
111 }
112 }
113  
114  
115 =head2 file
116  
117 Get/set the file that the typemap is written to when the
118 C<write> method is called.
119  
120 =cut
121  
122 0 ▶sub file {
Subroutine file: not called CC=2 SCAR=17.9
Pod documented
123 0 ▶ $_[0]->{file} = $_[1] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
124 $_[0]->{file}
125 0 }
126  
127 =head2 add_typemap
128  
129 Add a C<TYPEMAP> entry to the typemap.
130  
131 Required named arguments: The C<ctype> (e.g. C<ctype =E<gt> 'double'>)
132 and the C<xstype> (e.g. C<xstype =E<gt> 'T_NV'>).
133  
134 Optional named arguments: C<replace =E<gt> 1> forces removal/replacement of
135 existing C<TYPEMAP> entries of the same C<ctype>. C<skip =E<gt> 1>
136 triggers a I<"first come first serve"> logic by which new entries that conflict
137 with existing entries are silently ignored.
138  
139 As an alternative to the named parameters usage, you may pass in
140 an C<XS::Install::FrozenShit::Typemaps::Type> object as first argument, a copy of which will be
141 added to the typemap. In that case, only the C<replace> or C<skip> named parameters
142 may be used after the object. Example:
143  
144 $map->add_typemap($type_obj, replace => 1);
145  
146 =cut
147  
148 134 ▶sub add_typemap {
Subroutine add_typemap: called CC=9 SCAR=31.1
Pod documented
149 134 my $self = shift;
150 134 my $type;
151 my %args;
152  
153 134 ▶ if ((@_ % 2) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 1) { } 134 0 134
154 134 my $orig = shift;
155 134 $type = $orig->new();
156 134 %args = @_;
157 }
158 else {
159 0 %args = @_;
160 0 my $ctype = $args{ctype};
161 0 ▶ die("Need ctype argument") if not defined $ctype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctype 0 0 0
162 0 my $xstype = $args{xstype};
163 0 ▶ die("Need xstype argument") if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 0 0
164  
165 $type = XS::Install::FrozenShit::Typemaps::Type->new(
166 xstype => $xstype,
167 0 'prototype' => $args{'prototype'},
168 ctype => $ctype,
169 );
170 }
171  
172 134 ▶ if ($args{skip} and $args{replace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip"} and $args{"replace"}) 0 134 134
Condition33%1/3
$args{"skip"} and $args{"replace"}
!ll&&!rl&&r
13400
MC/DC0%0/2
$args{"skip"} and $args{"replace"}
$args{"skip"} $args{"replace"}
Truth table33%1/3
A and B
A= $args{"skip"}B= $args{"replace"}
ABresult
0X0
100
111
173 0 die("Cannot use both 'skip' and 'replace'");
174 }
175  
176 134 ▶ if ($args{replace}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'replace'}) { } 134 0 134
elsif ($args{'skip'}) { } 0 0 0
177 134 $self->remove_typemap(ctype => $type->ctype);
178 }
179 elsif ($args{skip}) {
180 0 ▶ return() if exists $self->{typemap_lookup}{$type->ctype};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"typemap_lookup"}{$type->ctype} 0 0 0
181 }
182 else {
183 0 $self->validate(typemap_xstype => $type->xstype, ctype => $type->ctype);
184 }
185  
186 # store
187 134 push @{$self->{typemap_section}}, $type;
188 # remember type for lookup, too.
189 134 $self->{typemap_lookup}{$type->tidy_ctype} = $#{$self->{typemap_section}};
190  
191 134 return 1;
192 }
193  
194 =head2 add_inputmap
195  
196 Add an C<INPUT> entry to the typemap.
197  
198 Required named arguments:
199 The C<xstype> (e.g. C<xstype =E<gt> 'T_NV'>)
200 and the C<code> to associate with it for input.
201  
202 Optional named arguments: C<replace =E<gt> 1> forces removal/replacement of
203 existing C<INPUT> entries of the same C<xstype>. C<skip =E<gt> 1>
204 triggers a I<"first come first serve"> logic by which new entries that conflict
205 with existing entries are silently ignored.
206  
207 As an alternative to the named parameters usage, you may pass in
208 an C<XS::Install::FrozenShit::Typemaps::InputMap> object as first argument, a copy of which will be
209 added to the typemap. In that case, only the C<replace> or C<skip> named parameters
210 may be used after the object. Example:
211  
212 $map->add_inputmap($type_obj, replace => 1);
213  
214 =cut
215  
216 100 ▶sub add_inputmap {
Subroutine add_inputmap: called CC=9 SCAR=31.1
Pod documented
217 100 my $self = shift;
218 100 my $input;
219 my %args;
220  
221 100 ▶ if ((@_ % 2) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 1) { } 100 0 100
222 100 my $orig = shift;
223 100 $input = $orig->new();
224 100 %args = @_;
225 }
226 else {
227 0 %args = @_;
228 0 my $xstype = $args{xstype};
229 0 ▶ die("Need xstype argument") if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 0 0
230 0 my $code = $args{code};
231 0 ▶ die("Need code argument") if not defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 0 0
232  
233 0 $input = XS::Install::FrozenShit::Typemaps::InputMap->new(
234 xstype => $xstype,
235 code => $code,
236 );
237 }
238  
239 100 ▶ if ($args{skip} and $args{replace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip"} and $args{"replace"}) 0 100 100
Condition33%1/3
$args{"skip"} and $args{"replace"}
!ll&&!rl&&r
10000
MC/DC0%0/2
$args{"skip"} and $args{"replace"}
$args{"skip"} $args{"replace"}
Truth table33%1/3
A and B
A= $args{"skip"}B= $args{"replace"}
ABresult
0X0
100
111
240 0 die("Cannot use both 'skip' and 'replace'");
241 }
242  
243 100 ▶ if ($args{replace}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'replace'}) { } 100 0 100
elsif ($args{'skip'}) { } 0 0 0
244 100 $self->remove_inputmap(xstype => $input->xstype);
245 }
246 elsif ($args{skip}) {
247 0 ▶ return() if exists $self->{input_lookup}{$input->xstype};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"input_lookup"}{$input->xstype} 0 0 0
248 }
249 else {
250 0 $self->validate(inputmap_xstype => $input->xstype);
251 }
252  
253 # store
254 100 push @{$self->{input_section}}, $input;
255 # remember type for lookup, too.
256 100 $self->{input_lookup}{$input->xstype} = $#{$self->{input_section}};
257  
258 100 return 1;
259 }
260  
261 =head2 add_outputmap
262  
263 Add an C<OUTPUT> entry to the typemap.
264 Works exactly the same as C<add_inputmap>.
265  
266 =cut
267  
268 100 ▶sub add_outputmap {
Subroutine add_outputmap: called CC=9 SCAR=31.1
Pod documented
269 100 my $self = shift;
270 100 my $output;
271 my %args;
272  
273 100 ▶ if ((@_ % 2) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 1) { } 100 0 100
274 100 my $orig = shift;
275 100 $output = $orig->new();
276 100 %args = @_;
277 }
278 else {
279 0 %args = @_;
280 0 my $xstype = $args{xstype};
281 0 ▶ die("Need xstype argument") if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 0 0
282 0 my $code = $args{code};
283 0 ▶ die("Need code argument") if not defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 0 0
284  
285 0 $output = XS::Install::FrozenShit::Typemaps::OutputMap->new(
286 xstype => $xstype,
287 code => $code,
288 );
289 }
290  
291 100 ▶ if ($args{skip} and $args{replace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip"} and $args{"replace"}) 0 100 100
Condition33%1/3
$args{"skip"} and $args{"replace"}
!ll&&!rl&&r
10000
MC/DC0%0/2
$args{"skip"} and $args{"replace"}
$args{"skip"} $args{"replace"}
Truth table33%1/3
A and B
A= $args{"skip"}B= $args{"replace"}
ABresult
0X0
100
111
292 0 die("Cannot use both 'skip' and 'replace'");
293 }
294  
295 100 ▶ if ($args{replace}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'replace'}) { } 100 0 100
elsif ($args{'skip'}) { } 0 0 0
296 100 $self->remove_outputmap(xstype => $output->xstype);
297 }
298 elsif ($args{skip}) {
299 0 ▶ return() if exists $self->{output_lookup}{$output->xstype};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"output_lookup"}{$output->xstype} 0 0 0
300 }
301 else {
302 0 $self->validate(outputmap_xstype => $output->xstype);
303 }
304  
305 # store
306 100 push @{$self->{output_section}}, $output;
307 # remember type for lookup, too.
308 100 $self->{output_lookup}{$output->xstype} = $#{$self->{output_section}};
309  
310 100 return 1;
311 }
312  
313 =head2 add_string
314  
315 Parses a string as a typemap and merge it into the typemap object.
316  
317 Required named argument: C<string> to specify the string to parse.
318  
319 =cut
320  
321 0 ▶sub add_string {
Subroutine add_string: not called CC=2 SCAR=17.9
Pod documented
322 0 my $self = shift;
323 0 my %args = @_;
324 0 ▶ die("Need 'string' argument") if not defined $args{string};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"string"} 0 0 0
325  
326 # no, this is not elegant.
327 0 my $other = XS::Install::FrozenShit::Typemaps->new(string => $args{string});
328 0 $self->merge(typemap => $other);
329 }
330  
331 =head2 remove_typemap
332  
333 Removes a C<TYPEMAP> entry from the typemap.
334  
335 Required named argument: C<ctype> to specify the entry to remove from the typemap.
336  
337 Alternatively, you may pass a single C<XS::Install::FrozenShit::Typemaps::Type> object.
338  
339 =cut
340  
341 134 ▶sub remove_typemap {
Subroutine remove_typemap: called CC=3 SCAR=11.3
Pod documented
342 134 my $self = shift;
343 134 my $ctype;
344 134 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 134 0 134
345 134 my %args = @_;
346 134 $ctype = $args{ctype};
347 134 ▶ die("Need ctype argument") if not defined $ctype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctype 0 134 134
348 134 $ctype = tidy_type($ctype);
349 }
350 else {
351 0 $ctype = $_[0]->tidy_ctype;
352 }
353  
354 134 return $self->_remove($ctype, $self->{typemap_section}, $self->{typemap_lookup});
355 }
356  
357 =head2 remove_inputmap
358  
359 Removes an C<INPUT> entry from the typemap.
360  
361 Required named argument: C<xstype> to specify the entry to remove from the typemap.
362  
363 Alternatively, you may pass a single C<XS::Install::FrozenShit::Typemaps::InputMap> object.
364  
365 =cut
366  
367 100 ▶sub remove_inputmap {
Subroutine remove_inputmap: called CC=3 SCAR=11.4
Pod documented
368 100 my $self = shift;
369 100 my $xstype;
370 100 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 100 0 100
371 100 my %args = @_;
372 100 $xstype = $args{xstype};
373 100 ▶ die("Need xstype argument") if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 100 100
374 }
375 else {
376 0 $xstype = $_[0]->xstype;
377 }
378
379 100 return $self->_remove($xstype, $self->{input_section}, $self->{input_lookup});
380 }
381  
382 =head2 remove_outputmap
383  
384 Removes an C<OUTPUT> entry from the typemap.
385  
386 Required named argument: C<xstype> to specify the entry to remove from the typemap.
387  
388 Alternatively, you may pass a single C<XS::Install::FrozenShit::Typemaps::OutputMap> object.
389  
390 =cut
391  
392 100 ▶sub remove_outputmap {
Subroutine remove_outputmap: called CC=3 SCAR=11.4
Pod documented
393 100 my $self = shift;
394 100 my $xstype;
395 100 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 100 0 100
396 100 my %args = @_;
397 100 $xstype = $args{xstype};
398 100 ▶ die("Need xstype argument") if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 100 100
399 }
400 else {
401 0 $xstype = $_[0]->xstype;
402 }
403
404 100 return $self->_remove($xstype, $self->{output_section}, $self->{output_lookup});
405 }
406  
407 334 ▶sub _remove {
Subroutine _remove: called CC=4 SCAR=13.9
408 334 my $self = shift;
409 334 my $rm = shift;
410 334 my $array = shift;
411 334 my $lookup = shift;
412  
413 # Just fetch the index of the item from the lookup table
414 334 my $index = $lookup->{$rm};
415 334 ▶ return() if not defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 330 4 334
416  
417 # Nuke the item from storage
418 4 splice(@$array, $index, 1);
419  
420 # Decrement the storage position of all items thereafter
421 4 foreach my $key (keys %$lookup) {
422 228 ▶ if ($lookup->{$key} > $index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup->{$key} > $index) 132 96 228
423 132 $lookup->{$key}--;
424 }
425 }
426 4 return();
427 }
428  
429 =head2 get_typemap
430  
431 Fetches an entry of the TYPEMAP section of the typemap.
432  
433 Mandatory named arguments: The C<ctype> of the entry.
434  
435 Returns the C<XS::Install::FrozenShit::Typemaps::Type>
436 object for the entry if found.
437  
438 =cut
439  
440 14 ▶sub get_typemap {
Subroutine get_typemap: called CC=4 SCAR=14.2
441 14 my $self = shift;
442 14 ▶ die("Need named parameters, got uneven number") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 14 14
443  
444 14 my %args = @_;
445 14 my $ctype = $args{ctype};
446 14 ▶ die("Need ctype argument") if not defined $ctype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctype 0 14 14
447 14 $ctype = tidy_type($ctype);
448  
449 14 my $index = $self->{typemap_lookup}{$ctype};
450 14 ▶ return() if not defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 14 14
451 14 return $self->{typemap_section}[$index];
452 }
453  
454 =head2 get_inputmap
455  
456 Fetches an entry of the INPUT section of the
457 typemap.
458  
459 Mandatory named arguments: The C<xstype> of the
460 entry or the C<ctype> of the typemap that can be used to find
461 the C<xstype>. To wit, the following pieces of code
462 are equivalent:
463  
464 my $type = $typemap->get_typemap(ctype => $ctype)
465 my $input_map = $typemap->get_inputmap(xstype => $type->xstype);
466  
467 my $input_map = $typemap->get_inputmap(ctype => $ctype);
468  
469 Returns the C<XS::Install::FrozenShit::Typemaps::InputMap>
470 object for the entry if found.
471  
472 =cut
473  
474 4 ▶sub get_inputmap {
Subroutine get_inputmap: called CC=10 SCAR=30.7
Pod documented
475 4 my $self = shift;
476 4 ▶ die("Need named parameters, got uneven number") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 4 4
477  
478 4 my %args = @_;
479 4 my $xstype = $args{xstype};
480 4 my $ctype = $args{ctype};
481 4 ▶ die("Need xstype or ctype argument")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype or defined $ctype 0 4 4
Condition33%1/3
defined $xstype or defined $ctype
l!l&&r!l&&!r
400
MC/DC0%0/2
defined $xstype or defined $ctype
defined $xstype defined $ctype
Truth table33%1/3
A or B
A= defined $xstypeB= defined $ctype
ABresult
1X1
011
000
482 if not defined $xstype
483 and not defined $ctype;
484 4 ▶ die("Need xstype OR ctype arguments, not both")
Branch: 1 branch
BranchTrueFalseTotal
if defined $xstype and defined $ctype 0 4 4
Condition33%1/3
defined $xstype and defined $ctype
!ll&&!rl&&r
040
MC/DC0%0/2
defined $xstype and defined $ctype
defined $xstype defined $ctype
Truth table33%1/3
A and B
A= defined $xstypeB= defined $ctype
ABresult
0X0
100
111
485 if defined $xstype and defined $ctype;
486  
487 4 ▶ if (defined $ctype) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctype) 0 4 4
488 0 my $tm = $self->get_typemap(ctype => $ctype);
489 0 ▶ $xstype = $tm && $tm->xstype;
Condition0%0/3
$tm && $tm->xstype
!ll&&!rl&&r
000
MC/DC0%0/2
$tm && $tm->xstype
$tm $tm->xstype
Truth table0%0/3
A && B
A= $tmB= $tm->xstype
ABresult
0X0
100
111
490 0 ▶ return() if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 0 0
491 }
492  
493 4 my $index = $self->{input_lookup}{$xstype};
494 4 ▶ return() if not defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 4 4
495 4 return $self->{input_section}[$index];
496 }
497  
498 =head2 get_outputmap
499  
500 Fetches an entry of the OUTPUT section of the
501 typemap.
502  
503 Mandatory named arguments: The C<xstype> of the
504 entry or the C<ctype> of the typemap that can be used to
505 resolve the C<xstype>. (See above for an example.)
506  
507 Returns the C<XS::Install::FrozenShit::Typemaps::InputMap>
508 object for the entry if found.
509  
510 =cut
511  
512 4 ▶sub get_outputmap {
Subroutine get_outputmap: called CC=10 SCAR=24.6
Pod documented
513 4 my $self = shift;
514 4 ▶ die("Need named parameters, got uneven number") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 4 4
515  
516 4 my %args = @_;
517 4 my $xstype = $args{xstype};
518 4 my $ctype = $args{ctype};
519 4 ▶ die("Need xstype or ctype argument")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype or defined $ctype 0 4 4
Condition66%2/3
defined $xstype or defined $ctype
l!l&&r!l&&!r
220
MC/DC0%0/2
defined $xstype or defined $ctype
defined $xstype defined $ctype
Truth table66%2/3
A or B
A= defined $xstypeB= defined $ctype
ABresult
1X1
011
000
520 if not defined $xstype
521 and not defined $ctype;
522 4 ▶ die("Need xstype OR ctype arguments, not both")
Branch: 1 branch
BranchTrueFalseTotal
if defined $xstype and defined $ctype 0 4 4
Condition66%2/3
defined $xstype and defined $ctype
!ll&&!rl&&r
220
MC/DC0%0/2
defined $xstype and defined $ctype
defined $xstype defined $ctype
Truth table66%2/3
A and B
A= defined $xstypeB= defined $ctype
ABresult
0X0
100
111
523 if defined $xstype and defined $ctype;
524  
525 4 ▶ if (defined $ctype) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctype) 2 2 4
526 2 my $tm = $self->get_typemap(ctype => $ctype);
527 2 ▶ $xstype = $tm && $tm->xstype;
Condition33%1/3
$tm && $tm->xstype
!ll&&!rl&&r
002
MC/DC0%0/2
$tm && $tm->xstype
$tm $tm->xstype
Truth table33%1/3
A && B
A= $tmB= $tm->xstype
ABresult
0X0
100
111
528 2 ▶ return() if not defined $xstype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xstype 0 2 2
529 }
530  
531 4 my $index = $self->{output_lookup}{$xstype};
532 4 ▶ return() if not defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 4 4
533 4 return $self->{output_section}[$index];
534 }
535  
536 =head2 write
537  
538 Write the typemap to a file. Optionally takes a C<file> argument. If given, the
539 typemap will be written to the specified file. If not, the typemap is written
540 to the currently stored file name (see L</file> above, this defaults to the file
541 it was read from if any).
542  
543 =cut
544  
545 0 ▶sub write {
Subroutine write: not called CC=4 SCAR=30.0
Pod documented
546 0 my $self = shift;
547 0 my %args = @_;
548 0 ▶ my $file = defined $args{file} ? $args{file} : $self->file();
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'file'} ? : 0 0 0
549 0 ▶ die("write() needs a file argument (or set the file name of the typemap using the 'file' method)")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
550 if not defined $file;
551  
552 0 ▶ open my $fh, '>', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Cannot open typemap file '${file}' for writing: $!" 0 0 0
553 or die "Cannot open typemap file '$file' for writing: $!";
554 0 print $fh $self->as_string();
555 0 close $fh;
556 }
557  
558 =head2 as_string
559  
560 Generates and returns the string form of the typemap.
561  
562 =cut
563  
564 0 ▶sub as_string {
Subroutine as_string: not called CC=7 SCAR=40.3
Pod documented
565 0 my $self = shift;
566 0 my $typemap = $self->{typemap_section};
567 0 my @code;
568 0 push @code, "TYPEMAP\n";
569 0 foreach my $entry (@$typemap) {
570 # type kind proto
571 # /^(.*?\S)\s+(\S+)\s*($XS::Install::FrozenShit::ParseXS::Constants::PrototypeRegexp*)$/o
572 0 ▶ push @code, $entry->ctype . "\t" . $entry->xstype
Branch: 1 branch
BranchTrueFalseTotal
$entry->proto ne '' ? : 0 0 0
573 . ($entry->proto ne '' ? "\t".$entry->proto : '') . "\n";
574 }
575  
576 0 my $input = $self->{input_section};
577 0 ▶ if (@$input) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$input) 0 0 0
578 0 push @code, "\nINPUT\n";
579 0 foreach my $entry (@$input) {
580 0 push @code, $entry->xstype, "\n", $entry->code, "\n";
581 }
582 }
583  
584 0 my $output = $self->{output_section};
585 0 ▶ if (@$output) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$output) 0 0 0
586 0 push @code, "\nOUTPUT\n";
587 0 foreach my $entry (@$output) {
588 0 push @code, $entry->xstype, "\n", $entry->code, "\n";
589 }
590 }
591 0 return join '', @code;
592 }
593  
594 =head2 as_embedded_typemap
595  
596 Generates and returns the string form of the typemap with the
597 appropriate prefix around it for verbatim inclusion into an
598 XS file as an embedded typemap. This will return a string like
599  
600 TYPEMAP: <<END_OF_TYPEMAP
601 ... typemap here (see as_string) ...
602 END_OF_TYPEMAP
603  
604 The method takes care not to use a HERE-doc end marker that
605 appears in the typemap string itself.
606  
607 =cut
608  
609 0 ▶sub as_embedded_typemap {
Subroutine as_embedded_typemap: not called CC=3 SCAR=24.8
Pod documented
610 0 my $self = shift;
611 0 my $string = $self->as_string;
612  
613 0 my @ident_cand = qw(END_TYPEMAP END_OF_TYPEMAP END);
614 0 my $icand = 0;
615 0 my $cand_suffix = "";
616 0 ▶ while ($string =~ /^\Q$ident_cand[$icand]$cand_suffix\E\s*$/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\Q$ident_cand[$icand]$cand_suffix\E\s*$/m) 0 0 0
617 0 $icand++;
618 0 ▶ if ($icand == @ident_cand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($icand == @ident_cand) 0 0 0
619 0 $icand = 0;
620 0 ++$cand_suffix;
621 }
622 }
623  
624 0 my $marker = "$ident_cand[$icand]$cand_suffix";
625 0 return "TYPEMAP: <<$marker;\n$string\n$marker\n";
626 }
627  
628 =head2 merge
629  
630 Merges a given typemap into the object. Note that a failed merge
631 operation leaves the object in an inconsistent state so clone it if necessary.
632  
633 Mandatory named arguments: Either C<typemap =E<gt> $another_typemap_obj>
634 or C<file =E<gt> $path_to_typemap_file> but not both.
635  
636 Optional arguments: C<replace =E<gt> 1> to force replacement
637 of existing typemap entries without warning or C<skip =E<gt> 1>
638 to skip entries that exist already in the typemap.
639  
640 =cut
641  
642 2 ▶sub merge {
Subroutine merge: called CC=11 SCAR=26.5
643 2 my $self = shift;
644 2 my %args = @_;
645  
646 2 ▶ if (exists $args{typemap} and exists $args{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'typemap'} and exists $args{'file'}) { } 0 2 2
elsif (not exists $args{'typemap'} || exists $args{'file'}) { } 0 2 2
Condition33%1/3
exists $args{"typemap"} and exists $args{"file"}
!ll&&!rl&&r
200
MC/DC0%0/2
exists $args{"typemap"} and exists $args{"file"}
exists $args{"typemap"} exists $args{"file"}
Truth table33%1/3
A and B
A= exists $args{"typemap"}B= exists $args{"file"}
ABresult
0X0
100
111
647 0 die("Need {file} OR {typemap} argument. Not both!");
648 }
649 ▶ elsif (not exists $args{typemap} and not exists $args{file}) {
Condition33%1/3
exists $args{'typemap'} || exists $args{'file'}
l!l&&r!l&&!r
020
MC/DC0%0/2
exists $args{'typemap'} || exists $args{'file'}
exists $args{'typemap'} exists $args{'file'}
Truth table33%1/3
A || B
A= exists $args{'typemap'}B= exists $args{'file'}
ABresult
1X1
011
000
650 0 die("Need {file} or {typemap} argument!");
651 }
652  
653 2 my @params;
654 2 ▶ push @params, 'replace' => $args{replace} if exists $args{replace};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"replace"} 2 0 2
655 2 ▶ push @params, 'skip' => $args{skip} if exists $args{skip};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"skip"} 0 2 2
656  
657 2 my $typemap = $args{typemap};
658 2 ▶ if (not defined $typemap) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $typemap) 2 0 2
659 2 $typemap = ref($self)->new(file => $args{file}, @params);
660 }
661  
662 # FIXME breaking encapsulation. Add accessor code.
663 2 foreach my $entry (@{$typemap->{typemap_section}}) {
664 67 $self->add_typemap( $entry, @params );
665 }
666  
667 2 foreach my $entry (@{$typemap->{input_section}}) {
668 50 $self->add_inputmap( $entry, @params );
669 }
670  
671 2 foreach my $entry (@{$typemap->{output_section}}) {
672 50 $self->add_outputmap( $entry, @params );
673 }
674  
675 2 return 1;
676 }
677  
678 =head2 is_empty
679  
680 Returns a bool indicating whether this typemap is entirely empty.
681  
682 =cut
683  
684 0 ▶sub is_empty {
Subroutine is_empty: not called CC=3 SCAR=24.8
Pod documented
685 0 my $self = shift;
686  
687 0 return @{ $self->{typemap_section} } == 0
688 0 && @{ $self->{input_section} } == 0
689 0 ▶ && @{ $self->{output_section} } == 0;
Condition0%0/6
@{$self->{'typemap_section'};} == 0 && @{$self->{'input_section'};} == 0 && @{$self->{'output_section'};} == 0
!ll&&!rl&&r
000
@{$self->{"typemap_section"};} == 0 and @{$self->{"input_section"};} == 0
!ll&&!rl&&r
000
MC/DC0%0/3
@{$self->{'typemap_section'};} == 0 && @{$self->{'input_section'};} == 0 && @{$self->{'output_section'};} == 0
@{$self->{"typemap_section"};} == 0 @{$self->{"input_section"};} == 0 @{$self->{'output_section'};} == 0
Truth table0%0/4
A and B && C
A= @{$self->{"typemap_section"};} == 0B= @{$self->{"input_section"};} == 0C= @{$self->{'output_section'};} == 0
ABCresult
0XX0
10X0
1100
1111
690 }
691  
692 =head2 list_mapped_ctypes
693  
694 Returns a list of the C types that are mappable by
695 this typemap object.
696  
697 =cut
698  
699 0 ▶sub list_mapped_ctypes {
Subroutine list_mapped_ctypes: not called CC=1 SCAR=6.9
Pod documented
700 0 my $self = shift;
701 0 return sort keys %{ $self->{typemap_lookup} };
702 }
703  
704 =head2 _get_typemap_hash
705  
706 Returns a hash mapping the C types to the XS types:
707  
708 {
709 'char **' => 'T_PACKEDARRAY',
710 'bool_t' => 'T_IV',
711 'AV *' => 'T_AVREF',
712 'InputStream' => 'T_IN',
713 'double' => 'T_DOUBLE',
714 # ...
715 }
716  
717 This is documented because it is used by C<XS::Install::FrozenShit::ParseXS>,
718 but it's not intended for general consumption. May be removed
719 at any time.
720  
721 =cut
722  
723 0 ▶sub _get_typemap_hash {
Subroutine _get_typemap_hash: not called CC=2 SCAR=17.9
724 0 my $self = shift;
725 0 my $lookup = $self->{typemap_lookup};
726 0 my $storage = $self->{typemap_section};
727  
728 0 my %rv;
729 0 foreach my $ctype (keys %$lookup) {
730 0 $rv{$ctype} = $storage->[ $lookup->{$ctype} ]->xstype;
731 }
732  
733 0 return \%rv;
734 }
735  
736 =head2 _get_inputmap_hash
737  
738 Returns a hash mapping the XS types (identifiers) to the
739 corresponding INPUT code:
740  
741 {
742 'T_CALLBACK' => ' $var = make_perl_cb_$type($arg)
743 ',
744 'T_OUT' => ' $var = IoOFP(sv_2io($arg))
745 ',
746 'T_REF_IV_PTR' => ' if (sv_isa($arg, \\"${ntype}\\")) {
747 # ...
748 }
749  
750 This is documented because it is used by C<XS::Install::FrozenShit::ParseXS>,
751 but it's not intended for general consumption. May be removed
752 at any time.
753  
754 =cut
755  
756 0 ▶sub _get_inputmap_hash {
Subroutine _get_inputmap_hash: not called CC=2 SCAR=17.9
757 0 my $self = shift;
758 0 my $lookup = $self->{input_lookup};
759 0 my $storage = $self->{input_section};
760  
761 0 my %rv;
762 0 foreach my $xstype (keys %$lookup) {
763 0 $rv{$xstype} = $storage->[ $lookup->{$xstype} ]->code;
764  
765 # Squash trailing whitespace to one line break
766 # This isn't strictly necessary, but makes the output more similar
767 # to the original XS::Install::FrozenShit::ParseXS.
768 0 $rv{$xstype} =~ s/\s*\z/\n/;
769 }
770  
771 0 return \%rv;
772 }
773  
774  
775 =head2 _get_outputmap_hash
776  
777 Returns a hash mapping the XS types (identifiers) to the
778 corresponding OUTPUT code:
779  
780 {
781 'T_CALLBACK' => ' sv_setpvn($arg, $var.context.value().chp(),
782 $var.context.value().size());
783 ',
784 'T_OUT' => ' {
785 GV *gv = (GV *)sv_newmortal();
786 gv_init_pvn(gv, gv_stashpvs("$Package",1),
787 "__ANONIO__",10,0);
788 if ( do_open(gv, "+>&", 3, FALSE, 0, 0, $var) )
789 sv_setsv(
790 $arg,
791 sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1))
792 );
793 else
794 $arg = &PL_sv_undef;
795 }
796 ',
797 # ...
798 }
799  
800 This is documented because it is used by C<XS::Install::FrozenShit::ParseXS>,
801 but it's not intended for general consumption. May be removed
802 at any time.
803  
804 =cut
805  
806 0 ▶sub _get_outputmap_hash {
Subroutine _get_outputmap_hash: not called CC=2 SCAR=17.9
807 0 my $self = shift;
808 0 my $lookup = $self->{output_lookup};
809 0 my $storage = $self->{output_section};
810  
811 0 my %rv;
812 0 foreach my $xstype (keys %$lookup) {
813 0 $rv{$xstype} = $storage->[ $lookup->{$xstype} ]->code;
814  
815 # Squash trailing whitespace to one line break
816 # This isn't strictly necessary, but makes the output more similar
817 # to the original XS::Install::FrozenShit::ParseXS.
818 0 $rv{$xstype} =~ s/\s*\z/\n/;
819 }
820  
821 0 return \%rv;
822 }
823  
824 =head2 _get_prototype_hash
825  
826 Returns a hash mapping the C types of the typemap to their
827 corresponding prototypes.
828  
829 {
830 'char **' => '$',
831 'bool_t' => '$',
832 'AV *' => '$',
833 'InputStream' => '$',
834 'double' => '$',
835 # ...
836 }
837  
838 This is documented because it is used by C<XS::Install::FrozenShit::ParseXS>,
839 but it's not intended for general consumption. May be removed
840 at any time.
841  
842 =cut
843  
844 0 ▶sub _get_prototype_hash {
Subroutine _get_prototype_hash: not called CC=3 SCAR=24.8
845 0 my $self = shift;
846 0 my $lookup = $self->{typemap_lookup};
847 0 my $storage = $self->{typemap_section};
848  
849 0 my %rv;
850 0 foreach my $ctype (keys %$lookup) {
851 0 ▶ $rv{$ctype} = $storage->[ $lookup->{$ctype} ]->proto || '$';
Condition0%0/2
$$storage[$lookup->{$ctype}]->proto || '$'
l!l
00
MC/DC0%0/1
$$storage[$lookup->{$ctype}]->proto || '$'
$$storage[$lookup->{$ctype}]->proto
Truth table0%0/2
A
A= $$storage[$lookup->{$ctype}]->proto
Aresult
11
00
852 }
853  
854 0 return \%rv;
855 }
856  
857  
858  
859 # make sure that the provided types wouldn't collide with what's
860 # in the object already.
861 0 ▶sub validate {
Subroutine validate: not called CC=7 SCAR=40.3
Pod undocumented
862 0 my $self = shift;
863 0 my %args = @_;
864  
865 0 ▶ if ( exists $args{ctype}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"ctype"} and exists $self->{"typemap_lookup"}{tidy_type($args{"ctype"})}) 0 0 0
Condition0%0/3
exists $args{"ctype"} and exists $self->{"typemap_lookup"}{tidy_type($args{"ctype"})}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"ctype"} and exists $self->{"typemap_lookup"}{tidy_type($args{"ctype"})}
exists $args{"ctype"} exists $self->{"typemap_lookup"}{tidy_type($args{"ctype"})}
Truth table0%0/3
A and B
A= exists $args{"ctype"}B= exists $self->{"typemap_lookup"}{tidy_type($args{"ctype"})}
ABresult
0X0
100
111
866 and exists $self->{typemap_lookup}{tidy_type($args{ctype})} )
867 {
868 0 die("Multiple definition of ctype '$args{ctype}' in TYPEMAP section");
869 }
870  
871 0 ▶ if ( exists $args{inputmap_xstype}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"inputmap_xstype"} and exists $self->{"input_lookup"}{$args{"inputmap_xstype"}}) 0 0 0
Condition0%0/3
exists $args{"inputmap_xstype"} and exists $self->{"input_lookup"}{$args{"inputmap_xstype"}}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"inputmap_xstype"} and exists $self->{"input_lookup"}{$args{"inputmap_xstype"}}
exists $args{"inputmap_xstype"} exists $self->{"input_lookup"}{$args{"inputmap_xstype"}}
Truth table0%0/3
A and B
A= exists $args{"inputmap_xstype"}B= exists $self->{"input_lookup"}{$args{"inputmap_xstype"}}
ABresult
0X0
100
111
872 and exists $self->{input_lookup}{$args{inputmap_xstype}} )
873 {
874 0 die("Multiple definition of xstype '$args{inputmap_xstype}' in INPUTMAP section");
875 }
876  
877 0 ▶ if ( exists $args{outputmap_xstype}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"outputmap_xstype"} and exists $self->{"output_lookup"}{$args{"outputmap_xstype"}}) 0 0 0
Condition0%0/3
exists $args{"outputmap_xstype"} and exists $self->{"output_lookup"}{$args{"outputmap_xstype"}}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"outputmap_xstype"} and exists $self->{"output_lookup"}{$args{"outputmap_xstype"}}
exists $args{"outputmap_xstype"} exists $self->{"output_lookup"}{$args{"outputmap_xstype"}}
Truth table0%0/3
A and B
A= exists $args{"outputmap_xstype"}B= exists $self->{"output_lookup"}{$args{"outputmap_xstype"}}
ABresult
0X0
100
111
878 and exists $self->{output_lookup}{$args{outputmap_xstype}} )
879 {
880 0 die("Multiple definition of xstype '$args{outputmap_xstype}' in OUTPUTMAP section");
881 }
882  
883 0 return 1;
884 }
885  
886 =head2 clone
887  
888 Creates and returns a clone of a full typemaps object.
889  
890 Takes named parameters: If C<shallow> is true,
891 the clone will share the actual individual type/input/outputmap objects,
892 but not share their storage. Use with caution. Without C<shallow>,
893 the clone will be fully independent.
894  
895 =cut
896  
897 0 ▶sub clone {
Subroutine clone: not called CC=2 SCAR=17.9
Pod documented
898 0 my $proto = shift;
899 0 my %args = @_;
900  
901 0 my $self;
902 0 ▶ if ($args{shallow}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'shallow'}) { } 0 0 0
903 $self = bless( {
904 %$proto,
905 0 typemap_section => [@{$proto->{typemap_section}}],
906 0 typemap_lookup => {%{$proto->{typemap_lookup}}},
907 0 input_section => [@{$proto->{input_section}}],
908 0 input_lookup => {%{$proto->{input_lookup}}},
909 0 output_section => [@{$proto->{output_section}}],
910 0 output_lookup => {%{$proto->{output_lookup}}},
911 } => ref($proto) );
912 }
913 else {
914 $self = bless( {
915 %$proto,
916 0 typemap_section => [map $_->new, @{$proto->{typemap_section}}],
917 0 typemap_lookup => {%{$proto->{typemap_lookup}}},
918 0 input_section => [map $_->new, @{$proto->{input_section}}],
919 0 input_lookup => {%{$proto->{input_lookup}}},
920 0 output_section => [map $_->new, @{$proto->{output_section}}],
921 0 output_lookup => {%{$proto->{output_lookup}}},
922 } => ref($proto) );
923 }
924  
925 0 return $self;
926 }
927  
928 =head2 tidy_type
929  
930 Function to (heuristically) canonicalize a C type in terms of white space.
931 Works to some degree with C++ types. For example,
932  
933 $halfway_canonical_type = tidy_type(' int * * const * ');
934  
935 returns C<'int ** const *'>.
936  
937 Moved from C<XS::Install::FrozenShit::ParseXS>.
938  
939 =cut
940  
941 361 ▶sub tidy_type {
Subroutine tidy_type: called CC=2 SCAR=7.2
Pod documented
942 361 local $_ = shift;
943  
944 # for templated C++ types, do some bit of flawed canonicalization
945 # wrt. templates at least
946 361 ▶ if (/[<>]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/[<>]/) 0 361 361
947 0 s/\s*([<>])\s*/$1/g;
948 0 s/>>/> >/g;
949 }
950  
951 # rationalise any '*' by joining them into bunches and removing whitespace
952 361 s#\s*(\*+)\s*#$1#g;
953 361 s#(\*+)# $1 #g ;
954  
955 # trim leading & trailing whitespace
956 361 s/^\s+//; s/\s+$//;
957  
958 # change multiple whitespace into a single space
959 361 s/\s+/ /g;
960  
961 361 $_;
962 }
963  
964  
965  
966 2 ▶sub _parse {
Subroutine _parse: called CC=25 SCAR=32.8
967 2 my $self = shift;
968 2 my $stringref = shift;
969 2 my $lineno_offset = shift;
970 2 ▶ $lineno_offset = 0 if not defined $lineno_offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lineno_offset 2 0 2
971 2 my $filename = shift;
972 2 ▶ $filename = '<string>' if not defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 2 2
973  
974 2 my $replace = $self->{replace};
975 2 my $skip = $self->{skip};
976 2 ▶ die "Can only replace OR skip" if $replace and $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $replace and $skip 0 2 2
Condition33%1/3
$replace and $skip
!ll&&!rl&&r
020
MC/DC0%0/2
$replace and $skip
$replace $skip
Truth table33%1/3
A and B
A= $replaceB= $skip
ABresult
0X0
100
111
977 2 my @add_params;
978 2 ▶ push @add_params, replace => 1 if $replace;
Branch: 1 branch
BranchTrueFalseTotal
if $replace 2 0 2
979 2 ▶ push @add_params, skip => 1 if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 2 2
980  
981 # TODO comments should round-trip, currently ignoring
982 # TODO order of sections, multiple sections of same type
983 # Heavily influenced by XS::Install::FrozenShit::ParseXS
984 2 my $section = 'typemap';
985 2 my $lineno = $lineno_offset;
986 2 my $junk = "";
987 2 my $current = \$junk;
988 2 my @input_expr;
989 my @output_expr;
990 2 ▶ while ($$stringref =~ /^(.*)$/gcm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$stringref =~ /^(.*)$/cgm) 564 2 566
991 564 local $_ = $1;
992 564 ++$lineno;
993 564 chomp;
994 564 ▶ next if /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ 18 546 564
995 546 ▶ if (/^INPUT\s*$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^INPUT\s*$/) { } 2 544 546
elsif (/^OUTPUT\s*$/) { } 2 542 544
elsif (/^TYPEMAP\s*$/) { } 1 541 542
996 2 $section = 'input';
997 2 $current = \$junk;
998 2 next;
999 }
1000 elsif (/^OUTPUT\s*$/) {
1001 2 $section = 'output';
1002 2 $current = \$junk;
1003 2 next;
1004 }
1005 elsif (/^TYPEMAP\s*$/) {
1006 1 $section = 'typemap';
1007 1 $current = \$junk;
1008 1 next;
1009 }
1010
1011 541 ▶ if ($section eq 'typemap') {
Branch: 4 branches
BranchTrueFalseTotal
if ($section eq 'typemap') { } 77 464 541
elsif (/^\s/) { } 355 109 464
elsif ($_ eq '') { } 9 100 109
elsif ($section eq 'input') { } 50 50 100
1012 77 my $line = $_;
1013 77 s/^\s+//; s/\s+$//;
1014 77 ▶ next if $_ eq '' or /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" or /^#/ 10 67 77
Condition66%2/3
$_ eq "" or /^#/
l!l&&r!l&&!r
10067
MC/DC50%1/2
$_ eq "" or /^#/
$_ eq "" /^#/
Truth table66%2/3
A or B
A= $_ eq ""B= /^#/
ABresult
1X1
011
000
1015 67 ▶ my($type, $kind, $proto) = /^(.*?\S)\s+(\S+)\s*($XS::Install::FrozenShit::ParseXS::Constants::PrototypeRegexp*)$/o
Branch: 1 branch
BranchTrueFalseTotal
() = /^(.*?\S)\s+(\S+)\s*($XS::Install::FrozenShit::ParseXS::Constants::PrototypeRegexp*)$/o or warn("Warning: File '${filename}' Line $lineno '${line}' TYPEMAP entry needs 2 or 3 columns\n"), next 67 0 67
1016 or warn("Warning: File '$filename' Line $lineno '$line' TYPEMAP entry needs 2 or 3 columns\n"),
1017 next;
1018  
1019 67 ▶ if (length($proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $proto) 0 67 67
1020 0 ▶ warn("Warning: File '$filename' Line $lineno '$line' Invalid prototype '$proto'\n")
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_proto_string($proto) 0 0 0
1021 unless _valid_proto_string($proto);
1022 }
1023 $self->add_typemap(
1024 67 ▶ XS::Install::FrozenShit::Typemaps::Type->new(
Branch: 1 branch
BranchTrueFalseTotal
length $proto ? : 0 67 67
1025 xstype => $kind,
1026 (length($proto) ? (prototype => $proto) : ()),
1027 ctype => $type
1028 ),
1029 @add_params
1030 );
1031 } elsif (/^\s/) {
1032 355 s/\s+$//;
1033 355 ▶ $$current .= $$current eq '' ? $_ : "\n".$_;
Branch: 1 branch
BranchTrueFalseTotal
$$current eq '' ? : 100 255 355
1034 } elsif ($_ eq '') {
1035 9 next;
1036 } elsif ($section eq 'input') {
1037 50 s/\s+$//;
1038 50 push @input_expr, {xstype => $_, code => ''};
1039 50 $current = \$input_expr[-1]{code};
1040 } else { # output section
1041 50 s/\s+$//;
1042 50 push @output_expr, {xstype => $_, code => ''};
1043 50 $current = \$output_expr[-1]{code};
1044 }
1045  
1046 } # end while lines
1047  
1048 2 foreach my $inexpr (@input_expr) {
1049 50 $self->add_inputmap( XS::Install::FrozenShit::Typemaps::InputMap->new(%$inexpr), @add_params );
1050 }
1051 2 foreach my $outexpr (@output_expr) {
1052 50 $self->add_outputmap( XS::Install::FrozenShit::Typemaps::OutputMap->new(%$outexpr), @add_params );
1053 }
1054  
1055 2 return 1;
1056 }
1057  
1058 # taken from XS::Install::FrozenShit::ParseXS
1059 0 ▶sub _valid_proto_string {
Subroutine _valid_proto_string: not called CC=2 SCAR=17.9
1060 0 my $string = shift;
1061 0 ▶ if ($string =~ /^$XS::Install::FrozenShit::ParseXS::Constants::PrototypeRegexp+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^$XS::Install::FrozenShit::ParseXS::Constants::PrototypeRegexp+$/o) 0 0 0
1062 0 return $string;
1063 }
1064  
1065 0 return 0 ;
1066 }
1067  
1068 # taken from XS::Install::FrozenShit::ParseXS (C_string)
1069 0 ▶sub _escape_backslashes {
Subroutine _escape_backslashes: not called CC=1 SCAR=6.9
1070 0 my $string = shift;
1071 0 $string =~ s[\\][\\\\]g;
1072 0 $string;
1073 }
1074  
1075 =head1 CAVEATS
1076  
1077 Inherits some evil code from C<XS::Install::FrozenShit::ParseXS>.
1078  
1079 =head1 SEE ALSO
1080  
1081 The parser is heavily inspired from the one in L<XS::Install::FrozenShit::ParseXS>.
1082  
1083 For details on typemaps: L<perlxstut>, L<perlxs>.
1084  
1085 =head1 AUTHOR
1086  
1087 Steffen Mueller C<<smueller@cpan.org>>
1088  
1089 =head1 COPYRIGHT & LICENSE
1090  
1091 Copyright 2009, 2010, 2011, 2012, 2013 Steffen Mueller
1092  
1093 This program is free software; you can redistribute it and/or
1094 modify it under the same terms as Perl itself.
1095  
1096 =cut
1097  
1098 1 1;
1099  
« Install/Deps.pm ↑ Summary Install/FrozenShit/ParseXS/Utilities.pm »