blib/lib/FFI/TinyCC.pm

filter: Statementstmt 79.7% 161 / 202 Branchbran 42.1% 27 / 64 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 89.7% 35 / 39 Podpod 100% 15 / 15 totaltotal 71.3% 239 / 335 CC 36 SCAR 23.3
CC
36
Cov
69%
detect_sysinclude_path
56.2
__ANON__
6.0
__ANON__
6.0
CRAP 10.3
↑ Summary
1 package FFI::TinyCC;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use FFI::Platypus;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use FFI::Platypus::Memory qw( malloc free );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use File::ShareDir::Dist qw( dist_share );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Tiny C Compiler for FFI
14 23 our $VERSION = '0.31'; # VERSION
15  
16  
17 sub _dlext ()
18 25 ▶{
Subroutine _dlext: called CC=2 SCAR=7.6
19 25 ▶ $^O eq 'MSWin32' ? 'dll' : $Config{dlext};
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 25 25
20 }
21  
22 23 our $ffi = FFI::Platypus->new;
23 23 $ffi->lib(
24 File::Spec->catfile(dist_share( 'FFI-TinyCC' ), 'libtcc.' . _dlext)
25 );
26  
27 $ffi->custom_type( tcc_t => {
28 141 ▶ perl_to_native => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 $_[0]->{handle},
30 141 },
31
32 34 ▶ native_to_perl => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 {
34 34 handle => $_[0],
35 relocate => 0,
36 error => [],
37 };
38 },
39  
40 23 });
41  
42 23 do {
43 23 my %output_type = qw(
44 memory 0
45 exe 1
46 dll 2
47 obj 3
48 );
49  
50 $ffi->custom_type( output_t => {
51 native_type => 'int',
52 14 ▶ perl_to_native => sub { $output_type{$_[0]} },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 23 });
54 };
55  
56 23 $ffi->type('int' => 'error_t');
57 23 $ffi->type('(opaque,string)->void' => 'error_handler_t');
58  
59 23 $ffi->attach([tcc_new => '_new'] => [] => 'tcc_t');
60 23 $ffi->attach([tcc_delete => '_delete'] => ['tcc_t'] => 'void');
61 23 $ffi->attach([tcc_set_error_func => '_set_error_func'] => ['tcc_t', 'opaque', 'error_handler_t'] => 'void');
62 23 $ffi->attach([tcc_add_symbol => '_add_symbol'] => ['tcc_t', 'string', 'opaque'] => 'int');
63 23 $ffi->attach([tcc_get_symbol => '_get_symbol'] => ['tcc_t', 'string'] => 'opaque');
64 23 $ffi->attach([tcc_relocate => '_relocate'] => ['tcc_t', 'opaque'] => 'int');
65 23 $ffi->attach([tcc_run => '_run'] => ['tcc_t', 'int', 'opaque'] => 'int');
66  
67 sub _method ($;@)
68 230 ▶{
Subroutine _method: called CC=2 SCAR=7.2
69 230 my($name, @args) = @_;
70 230 $ffi->attach(["tcc_$name" => "_$name"] => ['tcc_t', @args] => 'error_t');
71 230 eval '# line '. __LINE__ . ' "' . __FILE__ . qq("\n) .qq{
72 sub $name
73 13 ▶ {
Subroutine add_file: called CC=2 SCAR=7.3
Subroutine add_include_path: called CC=2 SCAR=7.3
Subroutine add_library: called CC=2 SCAR=7.3
Subroutine add_library_path: called CC=2 SCAR=7.3
Subroutine add_sysinclude_path: called CC=2 SCAR=7.3
Subroutine compile_string: called CC=2 SCAR=7.3
Subroutine output_file: called CC=2 SCAR=7.3
Subroutine set_lib_path: not called CC=2 SCAR=7.3
Subroutine set_options: called CC=2 SCAR=7.3
Subroutine set_output_type: called CC=2 SCAR=7.3
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
74 0 my \$r = _$name (\@_);
75 0 ▶ die FFI::TinyCC::Exception->new(\$_[0]) if \$r == -1;
Branch: 10 branches
BranchTrueFalseTotal
if $r == -1 0 13 13
if $r == -1 0 1 1
if $r == -1 0 2 2
if $r == -1 0 1 1
if $r == -1 0 6 6
if $r == -1 1 19 20
if $r == -1 0 10 10
if $r == -1 0 0 0
if $r == -1 0 9 9
if $r == -1 0 14 14
76 0 \$_[0];
77 }
78 };
79 230 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 230 230
80 }
81  
82  
83 sub new
84 34 ▶{
Subroutine new: called CC=3 SCAR=12.2
Pod documented
85 34 my($class, %opt) = @_;
86  
87 34 my $self = bless _new(), $class;
88
89 1 ▶ $self->{error_cb} = $ffi->closure(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
90 1 push @{ $self->{error} }, $_[1];
91 34 });
92 34 _set_error_func($self, undef, $self->{error_cb});
93
94 34 ▶ if($^O eq 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 34 34
95 {
96 0 require File::Basename;
97 0 require File::Spec;
98 0 my $path = File::Spec->catdir(File::Basename::dirname($ffi->lib), 'lib');
99 0 $self->add_library_path($path);
100 }
101
102 34 ▶ $self->{no_free_store} = 1 if $opt{_no_free_store};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"_no_free_store"} 0 34 34
103
104 34 $self;
105 }
106  
107 sub _error
108 0 ▶{
Subroutine _error: not called CC=1 SCAR=6.9
109 0 my($self, $msg) = @_;
110 0 push @{ $self->{error} }, $msg;
111 0 $self;
112 }
113  
114 23 ▶if(defined ${^GLOBAL_PHASE})
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${^GLOBAL_PHASE}) { } 23 0 23
115 {
116 *DESTROY = sub
117 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
118 0 my($self) = @_;
119 0 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 0 0
120 0 _delete($self);
121 # TODO: should we do this?
122 0 free($self->{store});
123 }
124 23 }
125 else
126 {
127 0 require Devel::GlobalDestruction;
128 *DESTROY = sub
129 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
130 0 my($self) = @_;
131 0 ▶ return if Devel::GlobalDestruction::in_global_destruction();
Branch: 1 branch
BranchTrueFalseTotal
if Devel::GlobalDestruction::in_global_destruction() 0 0 0
132 0 _delete($self);
133 # TODO: should we do this?
134 0 free($self->{store});
135 }
136 0 }
137  
138  
139 23 _method set_options => qw( string );
140  
141  
142 23 _method add_file => qw( string );
143  
144  
145 23 _method compile_string => qw( string );
146  
147  
148 sub add_symbol
149 1 ▶{
Subroutine add_symbol: called CC=2 SCAR=7.0
Pod documented
150 1 my($self, $name, $ptr) = @_;
151 1 my $r;
152 1 $r = _add_symbol($self, $name, $ptr);
153 1 ▶ die FFI::TinyCC::Exception->new($self) if $r == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $r == -1 0 1 1
154 1 $self;
155 }
156  
157  
158 sub detect_sysinclude_path
159 1 ▶{
Subroutine detect_sysinclude_path: called CC=11 SCAR=40.3
Pod documented
160 1 my($self) = @_;
161
162 1 my @path_list;
163
164 1 ▶ if($^O eq 'MSWin32')
Branch: 3 branches
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 1 1
elsif ($FFI::TinyCC::Config{'incpth'}) { } 1 0 1
elsif ($FFI::TinyCC::Config{'ccname'} eq 'gcc') { } 0 0 0
165 {
166 0 require File::Spec;
167 0 push @path_list, File::Spec->catdir(dist_share('Alien-TinyCC'), 'include');
168 }
169 elsif($Config{incpth})
170 {
171 1 require Alien::TinyCC;
172 1 require File::Spec;
173 1 push @path_list, File::Spec->catdir(Alien::TinyCC->libtcc_library_path, qw( tcc include ));
174 1 push @path_list, split /\s+/, $Config{incpth};
175 }
176 elsif($Config{ccname} eq 'gcc')
177 {
178 0 require File::Temp;
179 0 my($fh, $filename) = File::Temp::tempfile( "tryXXXX", SUFFIX => '.c', UNLINK => 1 );
180 0 close $fh;
181
182 0 my @lines = `$Config{cpprun} -v $filename 2>&1`;
183
184 0 ▶ shift @lines while defined $lines[0] && $lines[0] !~ /^#include </;
Branch: 1 branch
BranchTrueFalseTotal
defined $lines[0] and not $lines[0] =~ /^#include </ and do { shift @lines } 0 0 0
Condition0%0/3
defined $lines[0] and not $lines[0] =~ /^#include </
!ll&&!rl&&r
000
MC/DC0%0/2
defined $lines[0] and not $lines[0] =~ /^#include </
defined $lines[0] not $lines[0] =~ /^#include </
Truth table0%0/3
A and not(B)
A= defined $lines[0]B= not $lines[0] =~ /^#include </
ABresult
0X0
100
111
185 0 shift @lines;
186 0 ▶ pop @lines while defined $lines[-1] && $lines[-1] !~ /^End of search /;
Branch: 1 branch
BranchTrueFalseTotal
defined $lines[-1] and not $lines[-1] =~ /^End of search / and do { pop @lines } 0 0 0
Condition0%0/3
defined $lines[-1] and not $lines[-1] =~ /^End of search /
!ll&&!rl&&r
000
MC/DC0%0/2
defined $lines[-1] and not $lines[-1] =~ /^End of search /
defined $lines[-1] not $lines[-1] =~ /^End of search /
Truth table0%0/3
A and not(B)
A= defined $lines[-1]B= not $lines[-1] =~ /^End of search /
ABresult
0X0
100
111
187 0 pop @lines;
188
189 0 ▶ croak "Cannot detect sysinclude path" unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 0 0
190
191 0 require Alien::TinyCC;
192 0 require File::Spec;
193
194 0 push @path_list, File::Spec->catdir(Alien::TinyCC->libtcc_library_path, qw( tcc include ));
195 0 push @path_list, map { chomp; s/^ //; $_ } @lines;
196 }
197 else
198 {
199 0 croak "Cannot detect sysinclude path";
200 }
201
202 1 ▶ croak "Cannot detect sysinclude path" unless grep { -d $_ } @path_list;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {-d $_;} @path_list 0 1 1
203
204 1 $self->add_sysinclude_path($_) for @path_list;
205
206 1 @path_list;
207 }
208  
209  
210  
211 23 _method add_include_path => qw( string );
212  
213  
214 23 _method add_sysinclude_path => qw( string );
215  
216  
217 23 _method set_lib_path => qw( string );
218  
219  
220 23 $ffi->attach([tcc_define_symbol=>'define_symbol'] => ['tcc_t', 'string', 'string'] => 'void');
221  
222  
223 23 $ffi->attach([tcc_undefine_symbol=>'undefine_symbol'] => ['tcc_t', 'string', 'string'] => 'void');
224  
225  
226 23 _method set_output_type => qw( output_t );
227  
228  
229 23 _method add_library => qw( string );
230  
231  
232 23 _method add_library_path => qw( string );
233  
234  
235 sub run
236 14 ▶{
Subroutine run: called CC=3 SCAR=11.1
Pod documented
237 14 my($self, @args) = @_;
238
239 14 ▶ croak "unable to use run method after get_symbol" if $self->{relocate};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"relocate"} 0 14 14
240
241 14 my $argc = scalar @args;
242 14 my @c_strings = map { "$_\0" } @args;
243 14 my $ptrs = pack 'P' x $argc, @c_strings;
244 14 my $argv = unpack('L!', pack('P', $ptrs));
245  
246 14 my $r = _run($self, $argc, $argv);
247 14 ▶ die FFI::TinyCC::Exception->new($self) if $r == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $r == -1 0 14 14
248 14 $r;
249 }
250  
251  
252 sub get_symbol
253 4 ▶{
Subroutine get_symbol: called CC=3 SCAR=11.1
Pod documented
254 4 my($self, $symbol_name) = @_;
255
256 4 ▶ unless($self->{relocate})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"relocate"}) 4 0 4
257 {
258 4 my $size = _relocate($self, undef);
259 4 $self->{store} = malloc($size);
260 4 my $r = _relocate($self, $self->{store});
261 4 ▶ die FFI::TinyCC::Exception->new($self) if $r == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $r == -1 0 4 4
262 4 $self->{relocate} = 1;
263 }
264 4 _get_symbol($self, $symbol_name);
265 }
266  
267  
268 23 _method output_file => qw( string );
269  
270 package
271 FFI::TinyCC::Exception;
272  
273 23 ▶use overload '""' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=2 SCAR=7.4
274 2 my $self = shift;
275 2 ▶ if(@{ $self->{fault} } == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'fault'};} == 2) { } 2 0 2
276 {
277 join(' ', $self->as_string,
278 at => $self->{fault}->[0],
279 2 line => $self->{fault}->[1],
280 );
281 }
282 else
283 {
284 0 $self->as_string . "\n";
285 }
286 23 };
287 23 ▶use overload fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
288  
289 sub new
290 1 ▶{
Subroutine new: called CC=4 SCAR=14.0
291 1 my($class, $tcc) = @_;
292
293 1 my @errors = @{ $tcc->{error} };
294 1 $tcc->{errors} = [];
295 1 my @stack;
296 my @fault;
297
298 1 my $i=2;
299 1 ▶ while(my @frame = caller($i++))
Branch: 1 branch
BranchTrueFalseTotal
if (my(@frame) = caller $i++) 1 1 2
300 {
301 1 push @stack, \@frame;
302 1 ▶ if(@fault == 0 && $frame[0] !~ /^FFI::TinyCC/)
Branch: 1 branch
BranchTrueFalseTotal
if (@fault == 0 and not $frame[0] =~ /^FFI::TinyCC/) 1 0 1
Condition33%1/3
@fault == 0 and not $frame[0] =~ /^FFI::TinyCC/
!ll&&!rl&&r
001
MC/DC0%0/2
@fault == 0 and not $frame[0] =~ /^FFI::TinyCC/
@fault == 0 not $frame[0] =~ /^FFI::TinyCC/
Truth table33%1/3
A and not(B)
A= @fault == 0B= not $frame[0] =~ /^FFI::TinyCC/
ABresult
0X0
100
111
303 {
304 1 @fault = ($frame[1], $frame[2]);
305 }
306 }
307
308 1 my $self = bless {
309 errors => \@errors,
310 stack => \@stack,
311 fault => \@fault,
312 }, $class;
313
314 1 $self;
315 }
316  
317 2 ▶sub errors { shift->{errors} }
Subroutine errors: called CC=1 SCAR=0.0
318  
319 sub as_string
320 2 ▶{
Subroutine as_string: called CC=1 SCAR=0.0
321 2 my($self) = @_;
322 2 join "\n", @{ $self->{errors} };
323 }
324  
325 23 1;
326  
327 __END__
↑ Summary