| 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
| |||||||||||||||||||||||||||||
| 17 | 25 | ▶ | return $cmd eq 'gcc' ? "$cmd -E" : "$cmd -E"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'} || []
$opts{'define'} || {}
MC/DC100%1/1
$opts{'include'} || []
$opts{'include'}
MC/DC100%1/1
$opts{'define'} || {}
$opts{'define'}
Truth table100%2/2
A
A= $opts{'include'}
Truth table100%2/2
A
A= $opts{'define'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Read raw header for constant extraction | ||||||||||||||||||||||||||||
| 50 | 14 | ▶ | if (-f $header_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 14 | ▶ | open my $fh, '<', $header_path or die "Cannot read $header_path: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 161 | 56 | ▶ | next if $return_type =~ /^#/; # Skip preprocessor remnants | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
not defined $params or $params =~ /^\s*$/
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*$/
| |||||||||||||||||||||||||||||
| 184 | 56 | ▶ | return () if $params =~ /^\s*void\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | # Parse "type name" or just "type" | ||||||||||||||||||||||||||||
| 198 | 59 | ▶ | if ($part =~ /^(.+?)\s+(\w+)$/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 225 | 932 | my ($name, $value) = ($1, $2); | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # Skip common non-value defines | ||||||||||||||||||||||||||||
| 228 | 932 | ▶ | next if $value =~ /^\\/; # Line continuation | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 932 | ▶ | next if $name =~ /^_/; # Reserved names | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # Try to evaluate numeric constants | ||||||||||||||||||||||||||||
| 232 | 592 | my $numeric = $self->_try_numeric($value); | |||||||||||||||||||||||||||
| 233 | 592 | ▶ | if (defined $numeric) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 264 | 1 | return hex($1); | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | # Octal | ||||||||||||||||||||||||||||
| 268 | 637 | ▶ | if ($value =~ /^0([0-7]+)(?:U?L{0,2})?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 0 | return oct($1); | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | # Integer | ||||||||||||||||||||||||||||
| 273 | 637 | ▶ | if ($value =~ /^(-?\d+)(?:U?L{0,2})?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | # Match enum blocks | ||||||||||||||||||||||||||||
| 298 | 25 | ▶ | while ($code =~ /enum\s*(\w*)\s*\{([^}]+)\}/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 12 | ▶ | my ($enum_name, $body) = ($1 || 'anonymous', $2); | ||||||||||||||||||||||||||
|
Condition100%2/2
$1 || 'anonymous'
MC/DC100%1/1
$1 || 'anonymous'
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 306 | 55 | my ($name, $value) = ($1, $2); | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 55 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | 46 | my $num = $self->_try_numeric($value); | |||||||||||||||||||||||||||
| 310 | 46 | ▶ | $counter = defined $num ? $num : $counter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||