| 1 | package Const::Introspect::C; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use 5.020; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use experimental qw( signatures postderef ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Ref::Util qw( is_plain_arrayref ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Text::ParseWords (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Path::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Capture::Tiny qw( capture capture_merged ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Const::Introspect::C::Constant; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Data::Section::Simple (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Template (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use FFI::Platypus 1.00; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use FFI::Build; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # ABSTRACT: Find and evaluate C/C++ constants for use in Perl | ||||||||||||||||||||||||||||
| 18 | 2 | our $VERSION = '0.01'; # VERSION | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | has headers => ( | ||||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||
| 23 | 5 | ▶ | isa => sub { die "headers should be a plain array ref" unless is_plain_arrayref($_[0]) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | has lang => ( | ||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||
| 30 | 5 | ▶ | isa => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | die "lang should be one of c or c++" unless $_[0] =~ /^c(|\+\+)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||
| 33 | 2 | default => 'c', | |||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has cc => ( | ||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||
| 39 | 5 | ▶ | default => sub { [Text::ParseWords::shellwords($Config{cc})] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 2 | ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | has ppflags => ( | ||||||||||||||||||||||||||||
| 44 | is => 'ro', | ||||||||||||||||||||||||||||
| 45 | lazy => 1, | ||||||||||||||||||||||||||||
| 46 | 1 | ▶ | default => sub ($self) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 1 | ['-dM', '-E', '-x' => $self->lang]; | |||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | 2 | ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | has cflags => ( | ||||||||||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||||||||||
| 54 | 5 | ▶ | default => sub { [Text::ParseWords::shellwords($Config{ccflags})] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 2 | ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | has extra_cflags => ( | ||||||||||||||||||||||||||||
| 59 | is => 'ro', | ||||||||||||||||||||||||||||
| 60 | 2 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 2 | ); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | has source => ( | ||||||||||||||||||||||||||||
| 65 | is => 'ro', | ||||||||||||||||||||||||||||
| 66 | lazy => 1, | ||||||||||||||||||||||||||||
| 67 | 1 | ▶ | default => sub ($self) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 68 | 1 | ▶ | my $p = Path::Tiny->tempfile( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | TEMPLATE => 'const-introspect-c-XXXXXX', | ||||||||||||||||||||||||||||
| 70 | SUFFIX => $self->lang eq 'c' ? '.c' : '.cxx', | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | 1 | my $fh = $p->openw_utf8; | |||||||||||||||||||||||||||
| 73 | 1 | say $fh "#include <$_>" for $self->headers->@*; | |||||||||||||||||||||||||||
| 74 | 1 | close $fh; | |||||||||||||||||||||||||||
| 75 | 1 | $p; | |||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||
| 77 | 2 | ); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | has filter => ( | ||||||||||||||||||||||||||||
| 81 | is => 'ro', | ||||||||||||||||||||||||||||
| 82 | 5 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 5 | qr/^[^_]/; | |||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||
| 85 | 2 | ); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | has diag => ( | ||||||||||||||||||||||||||||
| 89 | is => 'ro', | ||||||||||||||||||||||||||||
| 90 | 5 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 2 | ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub get_macro_constants ($self) | ||||||||||||||||||||||||||
|
Subroutine
get_macro_constants: called CC=11 SCAR=24.4
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | my @cmd = ( | ||||||||||||||||||||||||||||
| 97 | 1 | map { $_->@* } | |||||||||||||||||||||||||||
| 98 | $self->cc, | ||||||||||||||||||||||||||||
| 99 | $self->ppflags, | ||||||||||||||||||||||||||||
| 100 | $self->cflags, | ||||||||||||||||||||||||||||
| 101 | $self->extra_cflags, | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | push @cmd, $self->source->stringify; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | ▶ | my($out, $err, $ret, $sig) = capture { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | 1 | system @cmd; | |||||||||||||||||||||||||||
| 108 | 1 | ($? >> 8,$? & 127); | |||||||||||||||||||||||||||
| 109 | 1 | }; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | if($ret != 0 || $sig != 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ret != 0 or $sig != 0
MC/DC0%0/2
$ret != 0 or $sig != 0
$ret != 0
$sig != 0
Truth table33%1/3
A or B
A= $ret != 0B= $sig != 0
| |||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||
| 113 | 0 | push $self->diag->@*, $err; | |||||||||||||||||||||||||||
| 114 | # TODO: class exception here | ||||||||||||||||||||||||||||
| 115 | 0 | die "command: @cmd failed"; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | my $filter = $self->filter; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | my @macros; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | foreach my $line (split /\n/, $out) | |||||||||||||||||||||||||||
| 123 | { | ||||||||||||||||||||||||||||
| 124 | 409 | ▶ | if($line =~ /^#define\s+(\S+)\s+(.*)\s*$/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | 409 | my $name = $1; | |||||||||||||||||||||||||||
| 127 | 409 | my $value = $2; | |||||||||||||||||||||||||||
| 128 | 409 | ▶ | next if $name =~ /[()]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 398 | ▶ | next unless $name =~ $filter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 7 | ▶ | if($value =~ /^-?([1-9][0-9]*|0[0-7]*)$/) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 132 | { | ||||||||||||||||||||||||||||
| 133 | 3 | push @macros, Const::Introspect::C::Constant->new( | |||||||||||||||||||||||||||
| 134 | c => $self, | ||||||||||||||||||||||||||||
| 135 | name => $name, | ||||||||||||||||||||||||||||
| 136 | raw_value => $value, | ||||||||||||||||||||||||||||
| 137 | value => int $value, | ||||||||||||||||||||||||||||
| 138 | type => 'int', | ||||||||||||||||||||||||||||
| 139 | ) | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | elsif($value =~ /^"([a-z_0-9]+)"$/i) | ||||||||||||||||||||||||||||
| 142 | { | ||||||||||||||||||||||||||||
| 143 | 1 | push @macros, Const::Introspect::C::Constant->new( | |||||||||||||||||||||||||||
| 144 | c => $self, | ||||||||||||||||||||||||||||
| 145 | name => $name, | ||||||||||||||||||||||||||||
| 146 | raw_value => $value, | ||||||||||||||||||||||||||||
| 147 | value => $1, | ||||||||||||||||||||||||||||
| 148 | type => 'string', | ||||||||||||||||||||||||||||
| 149 | ) | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | elsif($value =~ /^([0-9]+\.[0-9]+)([Ff]{0,1})$/) | ||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||
| 153 | 2 | ▶ | push @macros, Const::Introspect::C::Constant->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | c => $self, | ||||||||||||||||||||||||||||
| 155 | name => $name, | ||||||||||||||||||||||||||||
| 156 | raw_value => $value, | ||||||||||||||||||||||||||||
| 157 | value => $1, | ||||||||||||||||||||||||||||
| 158 | type => $2 ? 'float' : 'double', | ||||||||||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | else | ||||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||
| 163 | 1 | push @macros, Const::Introspect::C::Constant->new( | |||||||||||||||||||||||||||
| 164 | c => $self, | ||||||||||||||||||||||||||||
| 165 | name => $name, | ||||||||||||||||||||||||||||
| 166 | raw_value => $value, | ||||||||||||||||||||||||||||
| 167 | ); | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | else | ||||||||||||||||||||||||||||
| 171 | { | ||||||||||||||||||||||||||||
| 172 | 0 | warn "unable to parse line: $line"; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 1 | @macros; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 6 | ▶ | sub get_single ($self, $name) | ||||||||||||||||||||||||||
|
Subroutine
get_single: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||
| 182 | 6 | Const::Introspect::C::Constant->new( | |||||||||||||||||||||||||||
| 183 | c => $self, | ||||||||||||||||||||||||||||
| 184 | name => $name, | ||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 28 | ▶ | sub _tt ($self, $name, %args) | ||||||||||||||||||||||||||
|
Subroutine
_tt: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||
| 190 | { | ||||||||||||||||||||||||||||
| 191 | 28 | state $cache; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 28 | ▶ | my $template = $cache->{$name} //= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$cache->{$name} //= do {
state $dss;
$dss //= "Data::Section::Simple"->new(__PACKAGE__);
$dss->get_data_section($name) // die("no such template: $name")
}
MC/DC0%0/2
$cache->{$name} //= do {
state $dss;
$dss //= "Data::Section::Simple"->new(__PACKAGE__);
$dss->get_data_section($name) // die("no such template: $name")
}
$cache->{$name}
do {
state $dss;
$dss //= "Data::Section::Simple"->new(__PACKAGE__);
$dss->get_data_section($name) // die("no such template: $name")
}
Truth table66%2/3
A //= B
A= $cache->{$name}B= do {
state $dss;
$dss //= "Data::Section::Simple"->new(__PACKAGE__);
$dss->get_data_section($name) // die("no such template: $name")
}
| |||||||||||||||||||||||||||||
| 194 | 4 | state $dss; | |||||||||||||||||||||||||||
| 195 | 4 | ▶ | $dss //= Data::Section::Simple->new(__PACKAGE__); | ||||||||||||||||||||||||||
|
Condition66%2/3
$dss //= "Data::Section::Simple"->new(__PACKAGE__)
MC/DC0%0/2
$dss //= "Data::Section::Simple"->new(__PACKAGE__)
$dss
"Data::Section::Simple"->new(__PACKAGE__)
Truth table66%2/3
A //= B
A= $dssB= "Data::Section::Simple"->new(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 196 | 4 | ▶ | $dss->get_data_section($name) // die "no such template: $name"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$dss->get_data_section($name) // die("no such template: $name")
MC/DC0%0/1
$dss->get_data_section($name) // die("no such template: $name")
$dss->get_data_section($name)
Truth table50%1/2
A
A= $dss->get_data_section($name)
| |||||||||||||||||||||||||||||
| 197 | }; | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 28 | state $tt; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 28 | ▶ | $tt //= Template->new; | ||||||||||||||||||||||||||
|
Condition66%2/3
$tt //= "Template"->new
MC/DC0%0/2
$tt //= "Template"->new
$tt
"Template"->new
Truth table66%2/3
A //= B
A= $ttB= "Template"->new
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 28 | my $output = ''; | |||||||||||||||||||||||||||
| 204 | 28 | $args{self} = $self; | |||||||||||||||||||||||||||
| 205 | 28 | ▶ | $tt->process(\$template, \%args, \$output) || die $tt->error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 28 | $output; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # give a unique name for each lib | ||||||||||||||||||||||||||||
| 210 | 28 | ▶ | sub _lib_name ($self, $name) | ||||||||||||||||||||||||||
|
Subroutine
_lib_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 211 | { | ||||||||||||||||||||||||||||
| 212 | 28 | state $counter = 0; | |||||||||||||||||||||||||||
| 213 | 28 | $counter++; | |||||||||||||||||||||||||||
| 214 | 28 | join '', $name, $$, $counter; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 28 | ▶ | sub _build_from_template ($self, $name1, $name2, %args) | ||||||||||||||||||||||||||
|
Subroutine
_build_from_template: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 218 | { | ||||||||||||||||||||||||||||
| 219 | 28 | ▶ | my $source = Path::Tiny->tempfile( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | TEMPLATE => "$name1-XXXXXX", | ||||||||||||||||||||||||||||
| 221 | SUFFIX => $self->lang eq 'c' ? '.c' : '.cxx', | ||||||||||||||||||||||||||||
| 222 | ); | ||||||||||||||||||||||||||||
| 223 | 28 | $source->spew_utf8( | |||||||||||||||||||||||||||
| 224 | $self->_tt( | ||||||||||||||||||||||||||||
| 225 | "$name1.c.tt", | ||||||||||||||||||||||||||||
| 226 | %args, | ||||||||||||||||||||||||||||
| 227 | ) | ||||||||||||||||||||||||||||
| 228 | ); | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 28 | my $libname = $self->_lib_name($name2); | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 28 | my $build = FFI::Build->new( | |||||||||||||||||||||||||||
| 233 | $libname, | ||||||||||||||||||||||||||||
| 234 | cflags => $self->extra_cflags, | ||||||||||||||||||||||||||||
| 235 | export => [$name1 =~ s/-/_/gr], | ||||||||||||||||||||||||||||
| 236 | source => ["$source"], | ||||||||||||||||||||||||||||
| 237 | ); | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 28 | ▶ | my($out, $lib, $error) = capture_merged { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 240 | 28 | local $@ = ''; | |||||||||||||||||||||||||||
| 241 | 28 | my $lib = eval { $build->build }; | |||||||||||||||||||||||||||
| 242 | 28 | ($lib, $@) | |||||||||||||||||||||||||||
| 243 | 28 | }; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 28 | ▶ | push $self->diag->@*, $out | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | if $out eq ''; | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 28 | ▶ | die $error if $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 26 | my $ffi = FFI::Platypus->new( | |||||||||||||||||||||||||||
| 251 | api => 1, | ||||||||||||||||||||||||||||
| 252 | lib => [$lib->path], | ||||||||||||||||||||||||||||
| 253 | ); | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 26 | ($ffi, $build) | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 17 | ▶ | sub compute_expression_type ($self, $expression) | ||||||||||||||||||||||||||
|
Subroutine
compute_expression_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 259 | { | ||||||||||||||||||||||||||||
| 260 | 17 | my($ffi, $build) = $self->_build_from_template( | |||||||||||||||||||||||||||
| 261 | 'compute-expression-type', | ||||||||||||||||||||||||||||
| 262 | 'cet', | ||||||||||||||||||||||||||||
| 263 | expression => $expression, | ||||||||||||||||||||||||||||
| 264 | ); | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 15 | my $type = $ffi->function( 'compute_expression_type' => [] => 'string' ) | |||||||||||||||||||||||||||
| 267 | ->call; | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 15 | $build->clean; | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 15 | $type; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 11 | ▶ | sub compute_expression_value ($self, $type, $expression) | ||||||||||||||||||||||||||
|
Subroutine
compute_expression_value: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 276 | { | ||||||||||||||||||||||||||||
| 277 | 11 | my $ctype = $type; | |||||||||||||||||||||||||||
| 278 | 11 | ▶ | $ctype = 'const char *' if $type eq 'string'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 11 | ▶ | $ctype = 'void *' if $type eq 'pointer'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 11 | my $ffitype = $type; | |||||||||||||||||||||||||||
| 281 | 11 | ▶ | $ffitype = 'opaque' if $type eq 'pointer'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 11 | my($ffi, $build) = $self->_build_from_template( | |||||||||||||||||||||||||||
| 284 | 'compute-expression-value', | ||||||||||||||||||||||||||||
| 285 | 'cev', | ||||||||||||||||||||||||||||
| 286 | ctype => $ctype, | ||||||||||||||||||||||||||||
| 287 | expression => $expression, | ||||||||||||||||||||||||||||
| 288 | ); | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 11 | my $value = $ffi->function( 'compute_expression_value' => [] => $ffitype ) | |||||||||||||||||||||||||||
| 291 | ->call; | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 11 | $build->clean; | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 11 | $value; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 2 | ▶ | no Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 2 | 1; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =pod | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =head1 NAME | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | Const::Introspect::C - Find and evaluate C/C++ constants for use in Perl | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =head1 VERSION | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | version 0.01 | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | use Const::Introspect::C; | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | my $c = Const::Introspect::C->new( | ||||||||||||||||||||||||||||
| 320 | headers => ['foo.h'], | ||||||||||||||||||||||||||||
| 321 | ); | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | foreach my $const ($c->get_macro_constants) | ||||||||||||||||||||||||||||
| 324 | { | ||||||||||||||||||||||||||||
| 325 | # const isa Const::Introspect::C::Constant | ||||||||||||||||||||||||||||
| 326 | say "name = ", $const->name; | ||||||||||||||||||||||||||||
| 327 | # type is one of: int, long, pointer, string, | ||||||||||||||||||||||||||||
| 328 | # float, double or "other" | ||||||||||||||||||||||||||||
| 329 | say "type = ", $const->type; | ||||||||||||||||||||||||||||
| 330 | say "value = ", $const->value; | ||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | B<Note>: This is an early release, expect some interface changes in the near future. | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | This module provides an interface for finding C/C++ constant style macros, and can | ||||||||||||||||||||||||||||
| 338 | compute their types and values. It can also be used to compute the values of | ||||||||||||||||||||||||||||
| 339 | enumerated type constants, although this module doesn't have a way of finding | ||||||||||||||||||||||||||||
| 340 | the names (For that try something like L<Clang::CastXML>). | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =head1 PROPERTIES | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =head2 headers | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | List of C/C++ header files. | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =head2 lang | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | The programming language. Should be one of C<c> or C<c++>. The default is C<c>. | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =head2 cc | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | The C compiler. The default is the C compiler used by Perl itself, | ||||||||||||||||||||||||||||
| 355 | automatically split on the appropriate whitespace. | ||||||||||||||||||||||||||||
| 356 | This should be a array reference, so C<['clang']> and not C<'clang'>. | ||||||||||||||||||||||||||||
| 357 | This allows for C<cc> with spaces in it. | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head2 ppflags | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | The C pre-processor flags. This may change in the future, or on some platforms, but as of | ||||||||||||||||||||||||||||
| 362 | this writing this is C<-dM -E -x c> for C and C<-dM -E -x c++> for C++. This must be an | ||||||||||||||||||||||||||||
| 363 | array reference. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =head2 cflags | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | C compiler flags. This is what Perl uses by default. This must be an array reference. | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =head2 extra_cflags | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | Extra Compiler flags. This is an empty array by default. This allows the caller to provide additional | ||||||||||||||||||||||||||||
| 372 | library specific flags, like C<-I>. | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =head2 source | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | C source file. This is an instance of L<Path::Tiny> and it is created on-the-fly. You shouldn't | ||||||||||||||||||||||||||||
| 377 | need to specify this explicitly. | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =head2 filter | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | Filter regular expression that all macro names must match. This is C<^[^_]> by default, which means | ||||||||||||||||||||||||||||
| 382 | all macros starting with an underscore are skipped. | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | =head2 diag | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | List of diagnostic failures. | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head1 METHODS | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =head2 get_macro_constants | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | my @const = $c->get_macro_constants; | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | This generates the source file, runs the pre-processor, parses the macros as well as possible and | ||||||||||||||||||||||||||||
| 395 | returns the result as a list of L<Const::Introspect::C::Constant> instances. | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =head2 get_single | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | my $const = $c->get_single($name); | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | Get a single constant by the name of C<$name>. Returns an instance of | ||||||||||||||||||||||||||||
| 402 | L<Const::Introspect::C>. This is most useful for getting the integer | ||||||||||||||||||||||||||||
| 403 | values for named enumerated values. | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =head2 compute_expression_type | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | my $type = $c->compute_expression_type($expression); | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | This attempts to compute the type of the C C<$expression>. It should | ||||||||||||||||||||||||||||
| 410 | return one of C<int>, C<long>, C<string>, C<float>, C<double>, or C<other>. | ||||||||||||||||||||||||||||
| 411 | If the type cannot be determined then C<other> will be returned, and | ||||||||||||||||||||||||||||
| 412 | often indicates a code macro that doesn't have a corresponding | ||||||||||||||||||||||||||||
| 413 | constant. | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =head2 compute_expression_value | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | my $value = $c->compute_expression_value($type, $expression); | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | This method attempts to compute the value of the given C C<$expression> of | ||||||||||||||||||||||||||||
| 420 | the given C<$type>. C<$type> should be one of C<int>, C<long>, C<string>, | ||||||||||||||||||||||||||||
| 421 | C<float>, or C<double>. | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | If you do not know the expression type, you can try to compute the type | ||||||||||||||||||||||||||||
| 424 | using C<compute_expression_type> above. | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =head1 CAVEATS | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | This modules requires the C pre-processor for macro constants, and for many constants | ||||||||||||||||||||||||||||
| 429 | requires a compiler to compute the type and value. The techniques used by this module | ||||||||||||||||||||||||||||
| 430 | work with C<clang> and C<gcc>, but they probably don't work with other compilers. | ||||||||||||||||||||||||||||
| 431 | Patches welcome to support other compilers. | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | This module can tell you the value of pointer constants, but there is not much utility | ||||||||||||||||||||||||||||
| 434 | to the value of non C<NULL> values. | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | Graham Ollis <plicease@cpan.org> | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | This software is copyright (c) 2020 by Graham Ollis. | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 445 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =cut | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | __DATA__ | ||||||||||||||||||||||||||||