blib/lib/XS/JIT/Header/Parser.pm

filter: Statementstmt 89.6% 138 / 154 Branchbran 69.7% 46 / 66 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 81.8% 18 / 22 Podpod 100% 11 / 11 totaltotal 83.5% 223 / 267 CC 40 SCAR 16.6
CC
40
Cov
83%
_parse_params
10.4
_parse_enums
7.0
_try_numeric
6.2
CRAP 5.3
« blib/lib/XS/JIT/Builder.pm ↑ Summary blib/lib/XS/JIT/Header/TypeMap.pm »
1 package XS::JIT::Header::Parser;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.16';
9  
10 # Find a working C preprocessor
11 sub _find_preprocessor {
12 25 ▶ my @candidates = qw(clang cpp gcc);
Subroutine _find_preprocessor: called CC=4 SCAR=14.2
13  
14 25 for my $cmd (@candidates) {
15 50 my $output = `$cmd --version 2>/dev/null`;
16 50 ▶ if ($? == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 25 25 50
17 25 ▶ return $cmd eq 'gcc' ? "$cmd -E" : "$cmd -E";
Branch: 1 branch
BranchTrueFalseTotal
$cmd eq 'gcc' ? : 0 25 25
18 }
19 }
20  
21 0 die "No C preprocessor found. Install clang, gcc, or cpp.";
22 }
23  
24 # Create a new parser instance
25 sub new {
26 28 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
27  
28 my $self = bless {
29 include => $opts{include} || [],
30 define => $opts{define} || {},
31 28 ▶ functions => {},
Condition100%4/4
$opts{'include'} || []
l!l
1513
$opts{'define'} || {}
l!l
1513
MC/DC100%1/1
$opts{'include'} || []
$opts{'include'}
MC/DC100%1/1
$opts{'define'} || {}
$opts{'define'}
Truth table100%2/2
A
A= $opts{'include'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'define'}
Aresult
11
00
32 constants => {},
33 typedefs => {},
34 enums => {},
35 structs => {},
36 _raw_code => '',
37 _preprocessed => '',
38 }, $class;
39  
40 28 return $self;
41 }
42  
43 # Parse a header file
44 sub parse_file {
45 14 ▶ my ($self, $header_path) = @_;
Subroutine parse_file: called CC=4 SCAR=14.0
Pod documented
46  
47 14 ▶ die "Header file required" unless defined $header_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header_path 0 14 14
48  
49 # Read raw header for constant extraction
50 14 ▶ if (-f $header_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $header_path) 14 0 14
51 14 ▶ open my $fh, '<', $header_path or die "Cannot read $header_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $header_path or die "Cannot read ${header_path}: $!" 0 14 14
52 14 local $/;
53 14 $self->{_raw_code} = <$fh>;
54 14 close $fh;
55 }
56  
57 # Preprocess and parse
58 14 $self->_preprocess($header_path);
59 14 $self->_parse_functions();
60 14 $self->_parse_constants();
61 14 $self->_parse_enums();
62  
63 14 return $self;
64 }
65  
66 # Parse header content directly (for testing)
67 sub parse_string {
68 11 ▶ my ($self, $code) = @_;
Subroutine parse_string: called CC=1 SCAR=0.0
Pod documented
69  
70 11 $self->{_raw_code} = $code;
71  
72 # Write to temp file for preprocessing
73 11 my ($fh, $tmpfile) = tempfile(SUFFIX => '.h', UNLINK => 1);
74 11 print $fh $code;
75 11 close $fh;
76  
77 11 $self->_preprocess($tmpfile);
78 11 $self->_parse_functions();
79 11 $self->_parse_constants();
80 11 $self->_parse_enums();
81  
82 11 return $self;
83 }
84  
85 # Run C preprocessor
86 sub _preprocess {
87 25 ▶ my ($self, $header_path) = @_;
Subroutine _preprocess: called CC=3 SCAR=12.6
88  
89 25 my $cpp = _find_preprocessor();
90  
91 25 my @includes = map { "-I$_" } @{$self->{include}};
92 25 my @defines = map { "-D$_=$self->{define}{$_}" } keys %{$self->{define}};
93  
94 25 my $cmd = join(' ',
95 $cpp,
96 @includes,
97 @defines,
98 '-x', 'c', # Force C language
99 qq{"$header_path"},
100 '2>/dev/null'
101 );
102  
103 25 $self->{_preprocessed} = `$cmd`;
104  
105 25 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 25 25
106 0 warn "Preprocessor warning for $header_path (exit code: $?)";
107 # Try to continue with raw code if preprocessing fails
108 0 ▶ $self->{_preprocessed} = $self->{_raw_code} if $self->{_raw_code};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_raw_code"} 0 0 0
109 }
110  
111 25 return $self->{_preprocessed};
112 }
113  
114 # Parse function declarations from preprocessed code
115 sub _parse_functions {
116 25 ▶ my ($self) = @_;
Subroutine _parse_functions: called CC=6 SCAR=18.0
117  
118 25 my $code = $self->{_preprocessed};
119 25 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 25 25
120  
121 # Remove comments (shouldn't be any after preprocessing, but just in case)
122 25 $code =~ s{/\*.*?\*/}{}gs;
123 25 $code =~ s{//.*$}{}gm;
124  
125 # Function declaration pattern
126 # Matches: return_type function_name(params);
127 # Handles: const, static, inline, extern, pointers, etc.
128 25 my $func_pattern = qr/
129 (?:^|;|\}|\n) # Start of line, after semicolon, or after brace
130 \s*
131 (?:extern\s+|static\s+|inline\s+)* # Optional storage class
132 ( # Capture group 1: return type
133 (?:const\s+)? # Optional const
134 (?:unsigned\s+|signed\s+)? # Optional unsigned\/signed
135 (?:struct\s+|enum\s+|union\s+)? # Optional struct\/enum\/union
136 \w+ # Type name
137 (?:\s+\w+)* # Additional type words (long long, etc.)
138 \s*\** # Optional pointer(s)
139 )
140 \s+
141 (\w+) # Capture group 2: function name
142 \s*
143 \( # Opening paren
144 ([^)]*?) # Capture group 3: parameters
145 \) # Closing paren
146 \s*
147 (?:__attribute__\s*\(\([^)]*\)\))? # Optional GCC attributes
148 \s*; # Semicolon
149 /xm;
150  
151 25 ▶ while ($code =~ /$func_pattern/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /$func_pattern/g) 75 25 100
152 75 my ($return_type, $name, $params) = ($1, $2, $3);
153  
154 # Clean up return type
155 75 $return_type =~ s/^\s+//;
156 75 $return_type =~ s/\s+$//;
157 75 $return_type =~ s/\s+/ /g;
158  
159 # Skip if it looks like a macro or type definition
160 75 ▶ next if $name =~ /^_/; # Skip internal/reserved names
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^_/ 19 56 75
161 56 ▶ next if $return_type =~ /^#/; # Skip preprocessor remnants
Branch: 1 branch
BranchTrueFalseTotal
if $return_type =~ /^#/ 0 56 56
162  
163 # Parse parameters
164 56 my @parsed_params = $self->_parse_params($params);
165  
166 $self->{functions}{$name} = {
167 name => $name,
168 return_type => $return_type,
169 params => \@parsed_params,
170 59 param_types => [ map { $_->{type} } @parsed_params ],
171 56 ▶ param_names => [ map { $_->{name} } @parsed_params ],
Branch: 1 branch
BranchTrueFalseTotal
$params =~ /\.\.\./ ? : 0 56 56
172 is_variadic => ($params =~ /\.\.\./ ? 1 : 0),
173 };
174 }
175  
176 25 return $self->{functions};
177 }
178  
179 # Parse parameter list
180 sub _parse_params {
181 56 ▶ my ($self, $params) = @_;
Subroutine _parse_params: called CC=9 SCAR=23.4
182  
183 56 ▶ return () if !defined $params || $params =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $params or $params =~ /^\s*$/ 0 56 56
Condition33%1/3
not defined $params or $params =~ /^\s*$/
l!l&&r!l&&!r
0056
MC/DC0%0/2
not defined $params or $params =~ /^\s*$/
not defined $params $params =~ /^\s*$/
Truth table33%1/3
not(A) or B
A= not defined $paramsB= $params =~ /^\s*$/
ABresult
1X1
011
000
184 56 ▶ return () if $params =~ /^\s*void\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $params =~ /^\s*void\s*$/ 26 30 56
185  
186 30 my @result;
187 30 my @parts = split /,/, $params;
188  
189 30 my $idx = 0;
190 30 for my $part (@parts) {
191 59 $part =~ s/^\s+//;
192 59 $part =~ s/\s+$//;
193  
194 # Skip variadic
195 59 ▶ next if $part eq '...';
Branch: 1 branch
BranchTrueFalseTotal
if $part eq "..." 0 59 59
196  
197 # Parse "type name" or just "type"
198 59 ▶ if ($part =~ /^(.+?)\s+(\w+)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($part =~ /^(.+?)\s+(\w+)$/) { } 58 1 59
elsif ($part =~ /^(.+?)\s*(\*+)\s*(\w+)$/) { } 1 0 1
elsif ($part =~ /^(.+?)$/) { } 0 0 0
199 58 push @result, { type => $1, name => $2, index => $idx };
200 }
201 elsif ($part =~ /^(.+?)\s*(\*+)\s*(\w+)$/) {
202 # Handle "type *name" or "type * name"
203 1 push @result, { type => "$1$2", name => $3, index => $idx };
204 }
205 elsif ($part =~ /^(.+?)$/) {
206 # Just a type, no name
207 0 push @result, { type => $1, name => "arg$idx", index => $idx };
208 }
209  
210 59 $idx++;
211 }
212  
213 30 return @result;
214 }
215  
216 # Parse #define constants from raw (non-preprocessed) code
217 sub _parse_constants {
218 25 ▶ my ($self) = @_;
Subroutine _parse_constants: called CC=6 SCAR=18.0
219  
220 25 my $code = $self->{_raw_code};
221 25 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 25 25
222  
223 # Match #define NAME value (excluding function-like macros)
224 25 ▶ while ($code =~ /^\s*#\s*define\s+(\w+)(?!\s*\()[ \t]+(.+?)\s*$/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^\s*#\s*define\s+(\w+)(?!\s*\()[ \t]+(.+?)\s*$/gm) 932 25 957
225 932 my ($name, $value) = ($1, $2);
226  
227 # Skip common non-value defines
228 932 ▶ next if $value =~ /^\\/; # Line continuation
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\\/ 0 932 932
229 932 ▶ next if $name =~ /^_/; # Reserved names
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^_/ 340 592 932
230  
231 # Try to evaluate numeric constants
232 592 my $numeric = $self->_try_numeric($value);
233 592 ▶ if (defined $numeric) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $numeric) { } 256 336 592
234 256 $self->{constants}{$name} = {
235 name => $name,
236 value => $numeric,
237 raw => $value,
238 };
239 }
240 else {
241 # Store as string constant
242 336 $self->{constants}{$name} = {
243 name => $name,
244 value => $value,
245 raw => $value,
246 string => 1,
247 };
248 }
249 }
250  
251 25 return $self->{constants};
252 }
253  
254 # Try to convert a value to a number
255 sub _try_numeric {
256 638 ▶ my ($self, $value) = @_;
Subroutine _try_numeric: called CC=6 SCAR=18.2
257  
258 # Strip trailing C comments (e.g., "3.14159 /* pi */")
259 638 $value =~ s{/\*.*?\*/}{}g;
260 638 $value =~ s{\s+$}{};
261  
262 # Hex
263 638 ▶ if ($value =~ /^0x([0-9a-fA-F]+)(?:U?L{0,2})?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^0x([0-9a-fA-F]+)(?:U?L{0,2})?$/) 1 637 638
264 1 return hex($1);
265 }
266  
267 # Octal
268 637 ▶ if ($value =~ /^0([0-7]+)(?:U?L{0,2})?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^0([0-7]+)(?:U?L{0,2})?$/) 0 637 637
269 0 return oct($1);
270 }
271  
272 # Integer
273 637 ▶ if ($value =~ /^(-?\d+)(?:U?L{0,2})?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(-?\d+)(?:U?L{0,2})?$/) 80 557 637
274 80 return $1 + 0;
275 }
276  
277 # Float (with optional trailing whitespace/suffix)
278 557 ▶ if ($value =~ /^(-?\d+\.?\d*(?:[eE][+-]?\d+)?)[fFlL]?\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(-?\d+\.?\d*(?:[eE][+-]?\d+)?)[fFlL]?\s*$/) 181 376 557
279 181 return $1 + 0.0;
280 }
281  
282 # Simple expression like (1 << 4)
283 376 ▶ if ($value =~ /^\(?\s*(\d+)\s*<<\s*(\d+)\s*\)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\(?\s*(\d+)\s*<<\s*(\d+)\s*\)?$/) 0 376 376
284 0 return $1 << $2;
285 }
286  
287 376 return undef;
288 }
289  
290 # Parse enum definitions
291 sub _parse_enums {
292 25 ▶ my ($self) = @_;
Subroutine _parse_enums: called CC=7 SCAR=19.5
293  
294 25 my $code = $self->{_raw_code};
295 25 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 25 25
296  
297 # Match enum blocks
298 25 ▶ while ($code =~ /enum\s*(\w*)\s*\{([^}]+)\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /enum\s*(\w*)\s*\{([^}]+)\}/g) 12 25 37
299 12 ▶ my ($enum_name, $body) = ($1 || 'anonymous', $2);
Condition100%2/2
$1 || 'anonymous'
l!l
48
MC/DC100%1/1
$1 || 'anonymous'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
300  
301 12 my $counter = 0;
302 12 my @values;
303  
304 # Parse enum values
305 12 ▶ while ($body =~ /(\w+)(?:\s*=\s*([^,}]+))?/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /(\w+)(?:\s*=\s*([^,}]+))?/g) 55 12 67
306 55 my ($name, $value) = ($1, $2);
307  
308 55 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 46 9 55
309 46 my $num = $self->_try_numeric($value);
310 46 ▶ $counter = defined $num ? $num : $counter;
Branch: 1 branch
BranchTrueFalseTotal
defined $num ? : 6 40 46
311 }
312  
313 55 push @values, { name => $name, value => $counter };
314  
315 # Also add to constants
316 55 $self->{constants}{$name} = {
317 name => $name,
318 value => $counter,
319 enum => $enum_name,
320 };
321  
322 55 $counter++;
323 }
324  
325 12 $self->{enums}{$enum_name} = \@values;
326 }
327  
328 25 return $self->{enums};
329 }
330  
331 # Accessors
332 0 ▶sub functions { return %{shift->{functions}} }
Subroutine functions: not called CC=1 SCAR=6.9
Pod documented
333 36 ▶sub function { my ($self, $name) = @_; return $self->{functions}{$name} }
Subroutine function: called CC=1 SCAR=0.0
Pod documented
334 0 ▶sub constants { return %{shift->{constants}} }
Subroutine constants: not called CC=1 SCAR=6.9
Pod documented
335 27 ▶sub constant { my ($self, $name) = @_; return $self->{constants}{$name} }
Subroutine constant: called CC=1 SCAR=0.0
Pod documented
336 0 ▶sub enums { return %{shift->{enums}} }
Subroutine enums: not called CC=1 SCAR=6.9
Pod documented
337 0 ▶sub enum { my ($self, $name) = @_; return $self->{enums}{$name} }
Subroutine enum: not called CC=1 SCAR=6.9
Pod documented
338  
339 # Get list of function names
340 sub function_names {
341 5 ▶ my ($self) = @_;
Subroutine function_names: called CC=1 SCAR=0.0
Pod documented
342 5 return sort keys %{$self->{functions}};
343 }
344  
345 # Get list of constant names
346 sub constant_names {
347 4 ▶ my ($self) = @_;
Subroutine constant_names: called CC=1 SCAR=0.0
Pod documented
348 4 return sort keys %{$self->{constants}};
349 }
350  
351 4 1;
352  
353 __END__
« blib/lib/XS/JIT/Builder.pm ↑ Summary blib/lib/XS/JIT/Header/TypeMap.pm »