| 1 | package XS::TCC::Parser; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # These regular expressions were derived from Regexp::Common v0.01. | ||||||||||||||||||||||||||||
| 6 | 2 | my $RE_comment_C = q{(?:(?:\/\*)(?:(?:(?!\*\/)[\s\S])*)(?:\*\/))}; | |||||||||||||||||||||||||||
| 7 | 2 | my $RE_comment_Cpp = q{(?:\/\*(?:(?!\*\/)[\s\S])*\*\/|\/\/[^\n]*\n)}; | |||||||||||||||||||||||||||
| 8 | 2 | my $RE_quoted = (q{(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")} | |||||||||||||||||||||||||||
| 9 | .q{|(?:\')(?:[^\\\']*(?:\\.[^\\\']*)*)(?:\'))}); | ||||||||||||||||||||||||||||
| 10 | 2 | my $RE_balanced_brackets; | |||||||||||||||||||||||||||
| 11 | $RE_balanced_brackets = | ||||||||||||||||||||||||||||
| 12 | 2 | qr'(?:[{]((?:(?>[^{}]+)|(??{$RE_balanced_brackets}))*)[}])'; | |||||||||||||||||||||||||||
| 13 | 2 | my $RE_balanced_parens; | |||||||||||||||||||||||||||
| 14 | $RE_balanced_parens = | ||||||||||||||||||||||||||||
| 15 | 2 | qr'(?:[(]((?:(?>[^()]+)|(??{$RE_balanced_parens}))*)[)])'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub _normalize_type { | ||||||||||||||||||||||||||||
| 19 | # Normalize a type for lookup in a typemap. | ||||||||||||||||||||||||||||
| 20 | 5 | ▶ | my($type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_normalize_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Remove "extern". | ||||||||||||||||||||||||||||
| 23 | # But keep "static", "inline", "typedef", etc, | ||||||||||||||||||||||||||||
| 24 | # to cause desirable typemap misses. | ||||||||||||||||||||||||||||
| 25 | 5 | $type =~ s/\bextern\b//g; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Whitespace: only single spaces, none leading or trailing. | ||||||||||||||||||||||||||||
| 28 | 5 | $type =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 29 | 5 | $type =~ s/^\s//; $type =~ s/\s$//; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Adjacent "derivative characters" are not separated by whitespace, | ||||||||||||||||||||||||||||
| 32 | # but _are_ separated from the adjoining text. | ||||||||||||||||||||||||||||
| 33 | # [ Is really only * (and not ()[]) needed??? ] | ||||||||||||||||||||||||||||
| 34 | 5 | $type =~ s/\*\s\*/\*\*/g; | |||||||||||||||||||||||||||
| 35 | 5 | $type =~ s/(?<=[^ \*])\*/ \*/g; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 5 | return $type; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub extract_function_metadata { | ||||||||||||||||||||||||||||
| 41 | 3 | ▶ | my ($code) = @_; | ||||||||||||||||||||||||||
|
Subroutine
extract_function_metadata: called CC=16 SCAR=32.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | my $results = { | |||||||||||||||||||||||||||
| 44 | function_names => [], | ||||||||||||||||||||||||||||
| 45 | functions => {}, | ||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # First, we crush out anything potentially confusing. | ||||||||||||||||||||||||||||
| 49 | # The order of these _does_ matter. | ||||||||||||||||||||||||||||
| 50 | 3 | $code =~ s/$RE_comment_C/ /go; | |||||||||||||||||||||||||||
| 51 | 3 | $code =~ s/$RE_comment_Cpp/ /go; | |||||||||||||||||||||||||||
| 52 | 3 | $code =~ s/^\#.*(\\\n.*)*//mgo; | |||||||||||||||||||||||||||
| 53 | #$code =~ s/$RE_quoted/\"\"/go; # Buggy, if included. | ||||||||||||||||||||||||||||
| 54 | 3 | $code =~ s/$RE_balanced_brackets/{ }/go; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # The decision of what is an acceptable declaration was originally | ||||||||||||||||||||||||||||
| 57 | # derived from Inline::C::grammar.pm version 0.30 (Inline 0.43). | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | my $re_plausible_place_to_begin_a_declaration = qr { | |||||||||||||||||||||||||||
| 60 | # The beginning of a line, possibly indented. | ||||||||||||||||||||||||||||
| 61 | # (Accepting indentation allows for C code to be aligned with | ||||||||||||||||||||||||||||
| 62 | # its surrounding perl, and for backwards compatibility with | ||||||||||||||||||||||||||||
| 63 | # Inline 0.43). | ||||||||||||||||||||||||||||
| 64 | (?m: ^ ) \s* | ||||||||||||||||||||||||||||
| 65 | }xo; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Instead of using \s , we dont tolerate blank lines. | ||||||||||||||||||||||||||||
| 68 | # This matches user expectation better than allowing arbitrary | ||||||||||||||||||||||||||||
| 69 | # vertical whitespace. | ||||||||||||||||||||||||||||
| 70 | 3 | my $sp = qr{[ \t]|\n(?![ \t]*\n)}; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | my $re_type = qr {( | |||||||||||||||||||||||||||
| 73 | (?: \w+ $sp* )+? # words | ||||||||||||||||||||||||||||
| 74 | (?: \* $sp* )* # stars | ||||||||||||||||||||||||||||
| 75 | )}xo; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | my $re_identifier = qr{ (\w+) $sp* }xo; | |||||||||||||||||||||||||||
| 78 | 3 | ▶ | while( $code =~ m{ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | $re_plausible_place_to_begin_a_declaration | ||||||||||||||||||||||||||||
| 80 | ( $re_type $re_identifier $RE_balanced_parens $sp* (\;|\{) ) | ||||||||||||||||||||||||||||
| 81 | }xgo) | ||||||||||||||||||||||||||||
| 82 | { | ||||||||||||||||||||||||||||
| 83 | 2 | my($type, $identifier, $args, $what) = ($2,$3,$4,$5); | |||||||||||||||||||||||||||
| 84 | 2 | ▶ | $args = "" if $args =~ /^\s+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | my $need_threading_context = 0; | |||||||||||||||||||||||||||
| 87 | 2 | my $is_decl = $what eq ';'; | |||||||||||||||||||||||||||
| 88 | 2 | my $function = $identifier; | |||||||||||||||||||||||||||
| 89 | 2 | my $return_type = _normalize_type($type); | |||||||||||||||||||||||||||
| 90 | 2 | my @arguments = split ',', $args; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | #goto RESYNC if $is_decl && !$self->{data}{AUTOWRAP}; | ||||||||||||||||||||||||||||
| 93 | 2 | ▶ | goto RESYNC if exists $results->{functions}{$function}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | #goto RESYNC if !defined $self->{data}{typeconv}{valid_rtypes}{$return_type}; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | my(@arg_names,@arg_types); | |||||||||||||||||||||||||||
| 97 | 2 | my $dummy_name = 'arg1'; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | my $argno = 0; | |||||||||||||||||||||||||||
| 100 | 2 | foreach my $arg (@arguments) { | |||||||||||||||||||||||||||
| 101 | # recognize threading context passing as part of first arg | ||||||||||||||||||||||||||||
| 102 | 3 | ▶ | if ($argno++ == 0 and $arg =~ s/^\s*pTHX_?\s*//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$argno++ == 0 and $arg =~ s/^\s*pTHX_?\s*//
MC/DC0%0/2
$argno++ == 0 and $arg =~ s/^\s*pTHX_?\s*//
$argno++ == 0
$arg =~ s/^\s*pTHX_?\s*//
Truth table66%2/3
A and B
A= $argno++ == 0B= $arg =~ s/^\s*pTHX_?\s*//
| |||||||||||||||||||||||||||||
| 103 | 0 | $need_threading_context = 1; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | next if $arg !~ /\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 3 | my $arg_no_space = $arg; | |||||||||||||||||||||||||||
| 108 | 3 | $arg_no_space =~ s/\s+//g; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # If $arg_no_space is 'void', there will be no identifier. | ||||||||||||||||||||||||||||
| 111 | 3 | ▶ | if( my($type, $identifier) = | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 112 | $arg =~ /^\s*$re_type(?:$re_identifier)?\s*$/o ) | ||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 3 | my $arg_name = $identifier; | |||||||||||||||||||||||||||
| 115 | 3 | my $arg_type = _normalize_type($type); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 3 | ▶ | if((!defined $arg_name) && ($arg_no_space ne 'void')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $arg_name and $arg_no_space ne "void"
MC/DC0%0/2
not defined $arg_name and $arg_no_space ne "void"
not defined $arg_name
$arg_no_space ne "void"
Truth table33%1/3
not(A) and B
A= not defined $arg_nameB= $arg_no_space ne "void"
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | goto RESYNC if !$is_decl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | $arg_name = $dummy_name++; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | #goto RESYNC if ((!defined | ||||||||||||||||||||||||||||
| 122 | # $self->{data}{typeconv}{valid_types}{$arg_type}) && ($arg_no_space ne 'void')); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Push $arg_name onto @arg_names iff it's defined. Otherwise ($arg_no_space | ||||||||||||||||||||||||||||
| 125 | # was 'void'), push the empty string onto @arg_names (to avoid uninitialized | ||||||||||||||||||||||||||||
| 126 | # warnings emanating from C.pm). | ||||||||||||||||||||||||||||
| 127 | 3 | ▶ | defined($arg_name) ? push(@arg_names,$arg_name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | : push(@arg_names, ''); | ||||||||||||||||||||||||||||
| 129 | 3 | ▶ | if($arg_name) {push(@arg_types,$arg_type)} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | else {push(@arg_types,'')} # $arg_no_space was 'void' - this push() avoids 'uninitialized' warnings from C.pm | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | elsif($arg =~ /^\s*\.\.\.\s*$/) { | ||||||||||||||||||||||||||||
| 133 | 0 | push(@arg_names,'...'); | |||||||||||||||||||||||||||
| 134 | 0 | push(@arg_types,'...'); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | 0 | goto RESYNC; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Commit. | ||||||||||||||||||||||||||||
| 142 | 2 | push @{$results->{function_names}}, $function; | |||||||||||||||||||||||||||
| 143 | 2 | $results->{functions}{$function}{return_type}= $return_type; | |||||||||||||||||||||||||||
| 144 | 2 | $results->{functions}{$function}{arg_names} = [@arg_names]; | |||||||||||||||||||||||||||
| 145 | 2 | $results->{functions}{$function}{arg_types} = [@arg_types]; | |||||||||||||||||||||||||||
| 146 | 2 | ▶ | $results->{functions}{$function}{need_threading_context} = $need_threading_context if $need_threading_context; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | next; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | RESYNC: # Skip the rest of the current line, and continue. | |||||||||||||||||||||||||||
| 151 | $code =~ /\G[^\n]*\n/gc; | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 3 | return $results; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||