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

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