| 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
| |||||||||||||||||||||||||||||
| 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'} || []
$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'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Read raw header for constant extraction | ||||||||||||||||||||||||||||
| 51 | 14 | ▶ | if (-f $header_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 14 | ▶ | open my $fh, '<', $header_path or die "Cannot read $header_path: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 162 | 56 | ▶ | next if $return_type =~ /^#/; # Skip preprocessor remnants | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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*$/
| |||||||||||||||||||||||||||||
| 185 | 56 | ▶ | return () if $params =~ /^\s*void\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | # Parse "type name" or just "type" | ||||||||||||||||||||||||||||
| 199 | 59 | ▶ | if ($part =~ /^(.+?)\s+(\w+)$/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 226 | 932 | my ($name, $value) = ($1, $2); | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # Skip common non-value defines | ||||||||||||||||||||||||||||
| 229 | 932 | ▶ | next if $value =~ /^\\/; # Line continuation | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 932 | ▶ | next if $name =~ /^_/; # Reserved names | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # Try to evaluate numeric constants | ||||||||||||||||||||||||||||
| 233 | 592 | my $numeric = $self->_try_numeric($value); | |||||||||||||||||||||||||||
| 234 | 592 | ▶ | if (defined $numeric) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 265 | 1 | return hex($1); | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | # Octal | ||||||||||||||||||||||||||||
| 269 | 637 | ▶ | if ($value =~ /^0([0-7]+)(?:U?L{0,2})?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 0 | return oct($1); | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | # Integer | ||||||||||||||||||||||||||||
| 274 | 637 | ▶ | if ($value =~ /^(-?\d+)(?:U?L{0,2})?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # Match enum blocks | ||||||||||||||||||||||||||||
| 299 | 25 | ▶ | while ($code =~ /enum\s*(\w*)\s*\{([^}]+)\}/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 307 | 55 | my ($name, $value) = ($1, $2); | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 55 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 46 | my $num = $self->_try_numeric($value); | |||||||||||||||||||||||||||
| 311 | 46 | ▶ | $counter = defined $num ? $num : $counter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||