File Coverage

lib/XS/TCC/Parser.pm
Criterion Covered Total %
statement 57 64 89.0
branch 11 22 50.0
condition 4 6 66.6
subroutine 4 4 100.0
pod 0 1 0.0
total 76 97 78.3


line stmt bran cond sub pod time code
1             package XS::TCC::Parser;
2 3     3   15 use strict;
  3         6  
  3         89  
3 3     3   14 use warnings;
  3         6  
  3         2784  
4              
5             # These regular expressions were derived from Regexp::Common v0.01.
6             my $RE_comment_C = q{(?:(?:\/\*)(?:(?:(?!\*\/)[\s\S])*)(?:\*\/))};
7             my $RE_comment_Cpp = q{(?:\/\*(?:(?!\*\/)[\s\S])*\*\/|\/\/[^\n]*\n)};
8             my $RE_quoted = (q{(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")}
9             .q{|(?:\')(?:[^\\\']*(?:\\.[^\\\']*)*)(?:\'))});
10             my $RE_balanced_brackets;
11             $RE_balanced_brackets =
12             qr'(?:[{]((?:(?>[^{}]+)|(??{$RE_balanced_brackets}))*)[}])';
13             my $RE_balanced_parens;
14             $RE_balanced_parens =
15             qr'(?:[(]((?:(?>[^()]+)|(??{$RE_balanced_parens}))*)[)])';
16              
17              
18             sub _normalize_type {
19             # Normalize a type for lookup in a typemap.
20 16     16   27 my($type) = @_;
21              
22             # Remove "extern".
23             # But keep "static", "inline", "typedef", etc,
24             # to cause desirable typemap misses.
25 16         27 $type =~ s/\bextern\b//g;
26              
27             # Whitespace: only single spaces, none leading or trailing.
28 16         64 $type =~ s/\s+/ /g;
29 16         35 $type =~ s/^\s//; $type =~ s/\s$//;
  16         42  
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 16         23 $type =~ s/\*\s\*/\*\*/g;
35 16         27 $type =~ s/(?<=[^ \*])\*/ \*/g;
36              
37 16         42 return $type;
38             }
39              
40             sub extract_function_metadata {
41 9     9 0 2256 my ($code) = @_;
42              
43 9         43 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 9         111 $code =~ s/$RE_comment_C/ /go;
51 9         112 $code =~ s/$RE_comment_Cpp/ /go;
52 9         22 $code =~ s/^\#.*(\\\n.*)*//mgo;
53             #$code =~ s/$RE_quoted/\"\"/go; # Buggy, if included.
54 9         61 $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 9         41 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 9         29 my $sp = qr{[ \t]|\n(?![ \t]*\n)};
71              
72 9         114 my $re_type = qr {(
73             (?: \w+ $sp* )+? # words
74             (?: \* $sp* )* # stars
75             )}xo;
76              
77 9         66 my $re_identifier = qr{ (\w+) $sp* }xo;
78 9         378 while( $code =~ m{
79             $re_plausible_place_to_begin_a_declaration
80             ( $re_type $re_identifier $RE_balanced_parens $sp* (\;|\{) )
81             }xgo)
82             {
83 7         48 my($type, $identifier, $args, $what) = ($2,$3,$4,$5);
84 7 50       33 $args = "" if $args =~ /^\s+$/;
85              
86 7         14 my $need_threading_context = 0;
87 7         19 my $is_decl = $what eq ';';
88 7         10 my $function = $identifier;
89 7         20 my $return_type = _normalize_type($type);
90 7         36 my @arguments = split ',', $args;
91              
92             #goto RESYNC if $is_decl && !$self->{data}{AUTOWRAP};
93 7 50       31 goto RESYNC if exists $results->{functions}{$function};
94             #goto RESYNC if !defined $self->{data}{typeconv}{valid_rtypes}{$return_type};
95              
96 7         11 my(@arg_names,@arg_types);
97 7         14 my $dummy_name = 'arg1';
98              
99 7         10 my $argno = 0;
100 7         16 foreach my $arg (@arguments) {
101             # recognize threading context passing as part of first arg
102 9 100 100     69 if ($argno++ == 0 and $arg =~ s/^\s*pTHX_?\s*//) {
103 1         2 $need_threading_context = 1;
104 1 50       7 next if $arg !~ /\S/;
105             }
106              
107 9         15 my $arg_no_space = $arg;
108 9         42 $arg_no_space =~ s/\s+//g;
109              
110             # If $arg_no_space is 'void', there will be no identifier.
111 9 50       289 if( my($type, $identifier) =
    0          
112             $arg =~ /^\s*$re_type(?:$re_identifier)?\s*$/o )
113             {
114 9         13 my $arg_name = $identifier;
115 9         18 my $arg_type = _normalize_type($type);
116              
117 9 50 33     36 if((!defined $arg_name) && ($arg_no_space ne 'void')) {
118 0 0       0 goto RESYNC if !$is_decl;
119 0         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 9 50       25 defined($arg_name) ? push(@arg_names,$arg_name)
128             : push(@arg_names, '');
129 9 50       17 if($arg_name) {push(@arg_types,$arg_type)}
  9         28  
  0         0  
130             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         0 push(@arg_names,'...');
134 0         0 push(@arg_types,'...');
135             }
136             else {
137 0         0 goto RESYNC;
138             }
139             }
140              
141             # Commit.
142 7         10 push @{$results->{function_names}}, $function;
  7         19  
143 7         27 $results->{functions}{$function}{return_type}= $return_type;
144 7         29 $results->{functions}{$function}{arg_names} = [@arg_names];
145 7         25 $results->{functions}{$function}{arg_types} = [@arg_types];
146 7 100       21 $results->{functions}{$function}{need_threading_context} = $need_threading_context if $need_threading_context;
147              
148 7         39 next;
149              
150 0         0 RESYNC: # Skip the rest of the current line, and continue.
151             $code =~ /\G[^\n]*\n/gc;
152             }
153              
154 9         55 return $results;
155             }
156              
157             __END__