File Coverage

blib/lib/XS/TCC/Parser.pm
Criterion Covered Total %
statement 57 64 89.0
branch 12 22 54.5
condition 4 6 66.6
subroutine 4 4 100.0
pod 0 1 0.0
total 77 97 79.3


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