PreProcessor/Cish.pm

filter: Statementstmt 7.4% 24 / 321 Branchbran 0.0% 0 / 150 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 20.0% 7 / 35 Podpod 0.0% 0 / 13 totaltotal 5.4% 31 / 571 CC 88 SCAR 59.0
CC
88
Cov
5%
get_next_line
1190.0
_just_get_a_line
132.0
_macro_replace_string
110.0
CRAP 364.1
« SourceFile.pm ↑ Summary UnitName.pm »
1 package Hardware::Vhdl::Automake::PreProcessor::Cish;
2
3 # VHDL file preprocessor
4 # to do:
5 # #define and #undef of functions, replace in code
6
7 1 ▶use Math::Expression;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec::Functions qw/rel2abs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 1 our $file_slurp_limit = 16000;
16 1 our $VERSION = "1.00";
17
18 0 ▶sub pp_style { 'delegate' }
Subroutine pp_style: not called CC=1 SCAR=6.9
Pod undocumented
19
20 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
21 0 my $class = shift;
22 0 my %args = @_;
23 0 my $self = [
24 {
25 files_used => {},
26 macros => {
27 '__LINE__' => { search => '__LINE__' },
28 '__FILE__' => { search => '__FILE__' },
29 },
30 ungot => '',
31 arithenv => undef,
32 },
33 {
34 line => '',
35 copyon => 1,
36 linenum => 0,
37 source => undef,
38 incsection => undef,
39 endat => undef,
40 ifstack => [],
41 }
42 ];
43 0 my $perm = $self->[0];
44 0 my $state = $self->[1];
45 0 ▶ if ( exists $args{sourcefile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'sourcefile'}) { } 0 0 0
46 0 my $fhi;
47 0 ▶ open $fhi, "<$args{sourcefile}" || croak "Could not read '$args{sourcefile}'\n";
Condition0%0/3
"<$args{'sourcefile'}" || croak("Could not read '$args{'sourcefile'}'\n")
l!l&&r!l&&!r
000
MC/DC0%0/2
"<$args{'sourcefile'}" || croak("Could not read '$args{'sourcefile'}'\n")
"<$args{'sourcefile'}" croak("Could not read '$args{'sourcefile'}'\n")
Truth table0%0/3
A || B
A= "<$args{'sourcefile'}"B= croak("Could not read '$args{'sourcefile'}'\n")
ABresult
1X1
011
000
48 0 $state->{fhi} = $fhi;
49 0 binmode $state->{fhi};
50 0 $state->{filebuf} = '';
51 0 $state->{source} = $args{sourcefile};
52 0 $perm->{files_used}{ $args{sourcefile} } = undef;
53 } else {
54 0 ▶ croak "No source code specified" unless defined $state->{source};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state->{"source"} 0 0 0
55 }
56
57 0 ▶ if ( defined $args{incsection} && $args{incsection} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"incsection"} and $args{"incsection"} ne "") 0 0 0
Condition0%0/3
defined $args{"incsection"} and $args{"incsection"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"incsection"} and $args{"incsection"} ne ""
defined $args{"incsection"} $args{"incsection"} ne ""
Truth table0%0/3
A and B
A= defined $args{"incsection"}B= $args{"incsection"} ne ""
ABresult
0X0
100
111
58 0 $state->{incsection} = $args{incsection};
59 0 $state->{incsection_found} = 0;
60 0 $state->{copyon} = 0;
61 }
62 #print "## new PreProcessor::Cish is reading $state->{source}\n";
63 0 bless $self, $class;
64 0 $self->update_macro_re;
65 0 $self;
66 }
67
68 0 ▶sub files_used {
Subroutine files_used: not called CC=1 SCAR=6.9
Pod undocumented
69 0 keys %{ $_[0]->[0]{files_used} };
70 }
71
72 0 ▶sub linenum {
Subroutine linenum: not called CC=1 SCAR=6.9
Pod undocumented
73 # returns the line number of the last line fetched
74 0 $_[0]->[-1]{linenum};
75 }
76
77 0 ▶sub sourcefile {
Subroutine sourcefile: not called CC=1 SCAR=6.9
Pod undocumented
78 # returns the source file of the last line fetched
79 0 $_[0]->[-1]{source};
80 }
81
82 0 ▶sub unget {
Subroutine unget: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my $perm = shift->[0];
84 0 $perm->{ungot} = $_[0] . $perm->{ungot};
85 }
86
87 0 ▶sub _just_get_a_line {
Subroutine _just_get_a_line: not called CC=11 SCAR=48.8
88 0 my $self = shift;
89 0 my $state = $self->[-1];
90 0 $state->{line} = undef; # default return value
91 0 ▶ my $bufname = exists $state->{sourcestring} ? 'sourcestring' : 'filebuf';
Branch: 1 branch
BranchTrueFalseTotal
exists $state->{'sourcestring'} ? : 0 0 0
92
93 # Just what is used as a newline may vary from OS to OS. Unix traditionally uses \012, one type of DOSish I/O uses \015\012, and Mac OS uses \015.
94
95 GET_LINE: {
96 0 my $rem;
97 0 ▶ if (defined $state->{endat}) { $rem = $state->{endat} - tell($state->{fhi}) } # REMainding bytes we are allowed to read from the file
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state->{"endat"}) 0 0 0
98 0 ▶ if ( $state->{$bufname} =~ m/^(.*?(\015\012?|\012\015?))(.*)$/s ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state->{$bufname} =~ /^(.*?(\015\012?|\012\015?))(.*)$/s) { } 0 0 0
elsif (exists $state->{'fhi'} and not eof $state->{'fhi'} and !defined($rem) || $rem > 0) { } 0 0 0
99 0 $state->{line} = $1;
100 0 $state->{$bufname} = $3;
101 ▶ } elsif ( exists $state->{fhi} && !eof $state->{fhi} && (!defined $rem || ($rem > 0))) {
Condition0%0/9
exists $state->{"fhi"} and not eof $state->{"fhi"} and !defined($rem) || $rem > 0
!ll&&!rl&&r
000
exists $state->{"fhi"} and not eof $state->{"fhi"}
!ll&&!rl&&r
000
not defined $rem or $rem > 0
l!l&&r!l&&!r
000
MC/DC0%0/4
exists $state->{"fhi"} and not eof $state->{"fhi"} and !defined($rem) || $rem > 0
exists $state->{"fhi"} not eof $state->{"fhi"} not defined $rem $rem > 0
Truth table0%0/5
A and not(B) and not(C) or D
A= exists $state->{"fhi"}B= not eof $state->{"fhi"}C= not defined $remD= $rem > 0
ABCDresult
0XXX0
10XX0
11000
111X1
11011
102 0 local $/ = \$file_slurp_limit;
103 0 ▶ if (defined $rem && $file_slurp_limit > $rem) { $/ = \$rem }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rem and $Hardware::Vhdl::Automake::PreProcessor::Cish::file_slurp_limit > $rem) 0 0 0
Condition0%0/3
defined $rem and $Hardware::Vhdl::Automake::PreProcessor::Cish::file_slurp_limit > $rem
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rem and $Hardware::Vhdl::Automake::PreProcessor::Cish::file_slurp_limit > $rem
defined $rem $Hardware::Vhdl::Automake::PreProcessor::Cish::file_slurp_limit > $rem
Truth table0%0/3
A and B
A= defined $remB= $Hardware::Vhdl::Automake::PreProcessor::Cish::file_slurp_limit > $rem
ABresult
0X0
100
111
104 0 $state->{$bufname} .= readline $state->{fhi};
105 0 redo GET_LINE;
106 } else {
107 0 $state->{line} = $state->{$bufname};
108 0 $state->{$bufname} = '';
109 }
110 }
111 0 ▶ $state->{line} = undef if $state->{line} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"line"} eq "" 0 0 0
112 }
113
114 0 ▶sub macro_define {
Subroutine macro_define: not called CC=3 SCAR=24.8
Pod undocumented
115 0 my ($self, $macname, $macdef) = @_;
116 0 my $perm = $self->[0];
117 0 ▶ $macdef = '' if !defined $macdef;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $macdef 0 0 0
118 0 ▶ if (exists $perm->{macros}{$macname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $perm->{"macros"}{$macname}) 0 0 0
119 0 carp "Macro '$macname', defined at $perm->{macros}{$macname}{defined_in} line $perm->{macros}{$macname}{linenum}, was redefined";
120 }
121 0 $perm->{macros}{$macname} = { search => $macname, replace => $macdef, defined_in => $self->[-1]{source}, defined_line => $self->[-1]{linenum} };
122 0 $self->update_macro_re;
123 }
124
125 0 ▶sub macro_define_func {
Subroutine macro_define_func: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my ($self, $macname, $fargs, $macdef) = @_;
127 0 $fargs =~ s/\s+//g;
128 0 my @args = split(',', $fargs);
129 0 my $arg_re = join '|', @args;
130 #print "# macro function define: name='$macname', arg re='$arg_re', definition='$macdef'\n";
131 $self->[0]{macros}{$macname.'('.scalar(@args)} = {
132 search => $macname.'(',
133 arg_re => qr/^(.*?)\b($arg_re)\b(.*)$/s,
134 0 arg_index => { map { $args[$_] => $_ } 0..$#args },
135 replace => $macdef,
136 defined_in => $self->[-1]{source},
137 defined_line => $self->[-1]{linenum}
138 0 };
139 0 $self->update_macro_re;
140 }
141
142 0 ▶sub macro_undefine {
Subroutine macro_undefine: not called CC=1 SCAR=6.9
Pod undocumented
143 0 my ($self, $macname) = @_;
144 0 my $perm = $self->[0];
145 0 delete $perm->{macros}{$macname};
146 0 $self->update_macro_re;
147 }
148
149 0 ▶sub macro_undefine_func {
Subroutine macro_undefine_func: not called CC=1 SCAR=6.9
Pod undocumented
150 0 my ($self, $macname, $args) = @_;
151 0 print "# macro function undef: name='$macname', args='$args'\n";
152 0 my @args = split(',', $args);
153 0 delete $self->[0]{macros}{$macname.'('.scalar(@args)};
154 0 $self->update_macro_re;
155 }
156
157 0 ▶sub update_macro_re {
Subroutine update_macro_re: not called CC=3 SCAR=24.8
Pod undocumented
158 0 my $perm = shift->[0];
159 0 my @macnames;
160 my %macfuncnames;
161 0 for my $search (map { $perm->{macros}{$_}{search} } keys %{$perm->{macros}}) {
162 0 ▶ if (substr($search, -1) eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($search, -1) eq '(') { } 0 0 0
163 0 $macfuncnames{quotemeta($search)} = undef;
164 } else {
165 0 push @macnames, $search."\\b";
166 }
167 }
168 0 my $macro_re = join '|', @macnames, keys %macfuncnames;
169 #print "# macro regexp = /^(.*?)\\b($macro_re)(.*)\$/s\n";
170 0 $perm->{macro_re} = qr/^ (.*?) ( " | \b(?:$macro_re) ) (.*) $/xs;
171 }
172
173 0 ▶sub macro_replace {
Subroutine macro_replace: not called CC=1 SCAR=6.9
Pod undocumented
174 # do macro expansion on the non-quoted parts of $state->{line}
175 0 my $self = shift;
176 0 my $perm = $self->[0];
177 0 my $state = $self->[-1];
178 0 my $out = $self->_macro_replace_string($state->{line}, []);
179 #~ if ( $out =~ m/^(.*?(\015\012?|\012\015?))(.*)$/s ) {
180 #~ # deal with multi-line output from macro replacement
181 #~ # TBD: doesn't this mean that lines after the first one get processed more than once?
182 #~ $state->{line} = $1;
183 #~ $perm->{ungot} = $3;
184 #~ } else
185 {
186 0 $state->{line} = $out;
187 }
188 }
189
190 0 ▶sub _extract_args {
Subroutine _extract_args: not called CC=8 SCAR=42.8
191 0 my ($self, $line) = @_;
192 # Looks for a comma-separated list of args, followed by a ')'. If found, return the stuff after the ')' and the list of args
193 # if no closing bracket found, returns $line only.
194 # Currently needs to find the closing bracket on the current line
195 0 my $in=$line; # text yet to be processed: we nibble this from the left
196 0 my $out=''; # accumulator for bits that have been nibbled and may be added to argument list
197 0 my $bd=0; # bracket depth
198 0 my @args; # arguments found so far
199 #print "#> in = '$in'\n";
200 0 ▶ while ($in =~ m/^ (.*?) ( [\(\)\",] ) (.*) $/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /^ (.*?) ( [\(\)\",] ) (.*) $/msx) 0 0 0
201 0 $out .= $1; # prematch
202 0 my $g = $2; # significant char: bracket, quote or comma
203 0 $in = $3; # postmatch
204 0 ▶ if ($g eq ',') {
Branch: 3 branches
BranchTrueFalseTotal
if ($g eq ',') { } 0 0 0
elsif ($g eq ')') { } 0 0 0
elsif ($g eq '(') { } 0 0 0
205 0 ▶ if ($bd==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bd == 0) { } 0 0 0
206 # a comma, not inside a bracket: what we've found before this must be an argument
207 0 push @args, $out;
208 0 $out = '';
209 } else {
210 # a comma, from inside a bracket: just add it to the current argument string
211 0 $out .= $g;
212 }
213 } elsif ($g eq ')') {
214 0 ▶ if ($bd==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bd == 0) { } 0 0 0
215 # a closing bracket, not inside a bracket: must be the end of the argument list
216 0 return $in, @args, $out;
217 } else {
218 # a closing bracket, from inside a bracket: decrease the bracket depth count
219 0 $out .= $g;
220 0 $bd--;
221 }
222 } elsif ($g eq '(') {
223 # an opening bracket: increase the bracket depth count
224 0 $out .= $g;
225 0 $bd++;
226 } else {
227 # must have matched a double-quote character
228 0 ▶ if ( $in =~ /^ (
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /^ ( .*? # as little text as possible, then... (?<!\\) # no single backslash before... (?:\\\\)* # as many pairs of backslashes as possible " # then a dquote char ) (.*) # Capture all that follows $/msx) { } 0 0 0
229 .*? # as little text as possible, then...
230 (?<!\\) # no single backslash before...
231 (?:\\\\)* # as many pairs of backslashes as possible
232 " # then a dquote char
233 )
234 (.*) # Capture all that follows
235 $/xms ) {
236 #print "# after '$out' found quoted: \"$1, text after='$2'\n";
237 0 $out .= $g . $1;
238 0 $in = $2;
239 } else {
240 # closing quote not found
241 #print "# closing quote not found: args='".join("', '", @args)."', out = '$out', input = '$in'\n";
242 0 $out .= $g;
243 0 $in = '';
244 }
245 }
246 ##print "#> args='".join("', '", @args)."', out = '$out', input = '$in'\n";
247 }
248 # if we get here, we've failed to find a closing bracket
249 #print "## no closing bracket found: bd=$bd, out='$out', input='$in'\n";
250 0 return $line;
251 }
252
253 0 ▶sub _macro_replace_string {
Subroutine _macro_replace_string: not called CC=10 SCAR=47.0
254 0 my $self = shift;
255 0 my $perm = $self->[0];
256 0 my ($line, $inmac) = @_;
257 0 my $out = '';
258 0 ▶ while ($line =~ $perm->{macro_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$perm->{"macro_re"}/) 0 0 0
259 0 $out .= $1;
260 0 my $macro = $2;
261 0 $line = $3;
262 0 ▶ if ($macro eq '"') {
Branch: 2 branches
BranchTrueFalseTotal
if ($macro eq '"') { } 0 0 0
elsif (substr($macro, -1) eq '(') { } 0 0 0
263 0 $out .= $macro;
264 0 ▶ if ( $line =~ /^( .*? (?<!\\) (?:\\\\)* ") (.*) $/xs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^( .*? (?<!\\) (?:\\\\)* ") (.*) $/sx) { } 0 0 0
265 #print "# found quoted: \"$1, rem='$2'\n";
266 0 $macro = $1;
267 0 $line = $2;
268 } else {
269 # closing quote not found
270 #print "# _macro_replace_string: closing quote not found: out = '$out', line = '$line'\n";
271 0 $macro = $line;
272 0 $line = '';
273 }
274 } elsif (substr($macro, -1) eq '(') {
275 # this is a function macro: we need to count the args to it before we know what to look up
276 #print "# about to _extract_args: '$line' . '$quoted' . '$self->[-1]{line}'\n";
277 0 my ($post, @args) = $self->_extract_args( $line );
278 0 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 0 0 0
279 0 my $mackey = $macro . scalar @args;
280 #print "# found possible function macro '$mackey'\n";
281 0 ▶ if (exists $perm->{macros}{$mackey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $perm->{"macros"}{$mackey}) 0 0 0
282 0 ▶ unless (grep {$_ eq $mackey} @$inmac) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $mackey;} @$inmac) 0 0 0
283 0 $macro = $perm->{macros}{$mackey}{replace};
284 0 my $argsreplaced = '';
285 0 ▶ while ($macro =~ $perm->{macros}{$mackey}{arg_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($macro =~ /$perm->{"macros"}{$mackey}{"arg_re"}/) 0 0 0
286 0 $argsreplaced .= $1;
287 0 my $argname = $2;
288 0 $argsreplaced .= $args[$perm->{macros}{$mackey}{arg_index}{$argname}];
289 0 $macro = $3;
290 }
291 0 $argsreplaced .= $macro;
292 0 push @$inmac, $mackey;
293 0 $macro = $self->_macro_replace_string($argsreplaced, $inmac);
294 0 pop @$inmac;
295 0 $line = $post;
296 }
297 }
298 } else {
299 0 print "# found function macro '$macro', argc=?\n";
300 }
301 } else {
302 # this is a simple text-replace macro
303 0 ▶ unless (grep {$_ eq $macro} @$inmac) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $macro;} @$inmac) 0 0 0
304 0 push @$inmac, $macro;
305 0 $macro = $self->_macro_replace_string($perm->{macros}{$macro}{replace}, $inmac);
306 0 pop @$inmac;
307 }
308 }
309 0 $out .= $macro;
310 }
311 0 $out . $line;
312 }
313
314 0 ▶sub _macro_eval {
Subroutine _macro_eval: not called CC=2 SCAR=17.9
315 0 my $self = shift;
316 0 my $perm = $self->[0];
317 0 my ($expr) = @_;
318 #print "# evaluating expression '$expr'\n";
319 0 ▶ if (!defined $perm->{arithenv}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $perm->{"arithenv"}) 0 0 0
320 0 $perm->{arithenv} = Math::Expression->new;
321 0 ▶ my $setfunc = sub { $_[0]->{PrintErrFunc}("use of ':=' in expressions to set variables is not allowed: use #evaldef") };
Subroutine __ANON__: not called CC=1 SCAR=6.9
322 $perm->{arithenv}->SetOpt(
323 VarSetFun => $setfunc,
324 VarSetScalar => $setfunc,
325 0 ▶ VarIsDefFun => sub { exists $perm->{macros}{$_[1]} ? 1 : 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
exists $perm->{'macros'}{$_[1]} ? : 0 0 0
326 0 ▶ VarGetFun => sub { exists $perm->{macros}{$_[1]} ? $perm->{macros}{$_[1]}{replace} : '' },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
exists $perm->{'macros'}{$_[1]} ? : 0 0 0
327 0 ▶ FuncEval => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
328 0 my ($ae, $fname, @arglist) = @_;
329 0 ▶ return $arglist[$#arglist] ? 0 : 1 if $fname eq 'not';
Branch: 2 branches
BranchTrueFalseTotal
if $fname eq "not" 0 0 0
$arglist[$#arglist] ? : 0 0 0
330 0 ▶ return time if $fname eq 'time';
Branch: 1 branch
BranchTrueFalseTotal
if $fname eq "time" 0 0 0
331 0 $ae->FuncValue($fname, @arglist); # let Math::Expression deal with the functions it knows
332 },
333 0 ▶ PrintErrFunc => sub { die sprintf(shift(@_)."\n", @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
334 0 );
335 }
336 0 my $tree = $perm->{arithenv}->Parse($expr);
337 0 $perm->{arithenv}->EvalToScalar($tree);
338 }
339
340 0 ▶sub _pp_if {
Subroutine _pp_if: not called CC=2 SCAR=17.9
341 0 my ($cond, $state, $ifstack) = @_;
342 0 ▶ if ( $state->{copyon} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{'copyon'}) { } 0 0 0
343 0 push @$ifstack, $cond;
344 0 $state->{copyon} = $cond;
345 } else {
346 0 push @$ifstack, 2;
347 }
348 }
349
350 0 ▶sub _pp_else {
Subroutine _pp_else: not called CC=4 SCAR=30.0
351 0 my ($cond, $state, $ifstack) = @_;
352 0 ▶ if (@$ifstack==0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$ifstack == 0) { } 0 0 0
elsif ($cond and $ifstack->[-1] == 0) { } 0 0 0
353 0 croak "'#else' without '#if'\n";
354 ▶ } elsif ($cond && $ifstack->[-1]==0) {
Condition0%0/3
$cond and $ifstack->[-1] == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$cond and $ifstack->[-1] == 0
$cond $ifstack->[-1] == 0
Truth table0%0/3
A and B
A= $condB= $ifstack->[-1] == 0
ABresult
0X0
100
111
355 0 $ifstack->[-1] = 1;
356 0 $state->{copyon} = 1;
357 } else {
358 0 $state->{copyon} = 0;
359 }
360 }
361
362 0 ▶sub _pp_cond_def {
Subroutine _pp_cond_def: not called CC=4 SCAR=30.0
363 0 my ($self,$macname,$neg) = @_;
364 0 my $cond = 0;
365 0 ▶ if ( exists $self->[0]{macros}{$macname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[0]{'macros'}{$macname}) { } 0 0 0
366 0 $cond = 1;
367 }
368 else {
369 0 my $len = length($macname)+1;
370 0 ▶ if ( defined(first { substr($_, 0, $len) eq "$macname(" } keys %{$self->[0]{macros}}) ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (defined &first(sub { substr($_, 0, $len) eq "$macname("; } , keys %{$$self[0]{"macros"};})) 0 0 0
371 0 $cond = 1;
372 }
373 }
374 0 ▶ $cond = 1 - $cond if $neg;
Branch: 1 branch
BranchTrueFalseTotal
if $neg 0 0 0
375 0 $cond;
376 }
377
378 0 ▶sub _pp_cond_expr {
Subroutine _pp_cond_expr: not called CC=3 SCAR=24.8
379 0 my ($self, $expr, $neg) = @_;
380 0 ▶ my $cond = $self->_macro_eval($expr) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_macro_eval($expr) ? : 0 0 0
381 0 ▶ $cond = 1 - $cond if $neg;
Branch: 1 branch
BranchTrueFalseTotal
if $neg 0 0 0
382 0 $cond;
383 }
384
385 0 ▶sub get_next_line {
Subroutine get_next_line: not called CC=34 SCAR=70.8
Pod undocumented
386 # returns a line and its newline at the end, or the last line (perhaps without a newline) or undef
387 0 my $self = shift;
388 0 my $perm = $self->[0];
389 0 my $state = $self->[-1];
390 0 my $ifstack = $state->{ifstack};
391 0 my $done = 0;
392
393 0 ▶ if ($perm->{ungot} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($perm->{"ungot"} ne "") 0 0 0
394 0 ▶ if ( $perm->{ungot} =~ m/^(.*?(\015\012?|\012\015?))(.*)$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perm->{'ungot'} =~ /^(.*?(\015\012?|\012\015?))(.*)$/s) { } 0 0 0
395 0 $state->{line} = $1;
396 0 $perm->{ungot} = $3;
397 } else {
398 0 $state->{line} = $perm->{ungot}."\n";
399 0 $perm->{ungot} = '';
400 }
401 0 $done = 1;
402 }
403
404 0 ▶ while ( !$done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 0 0
405 0 $done = $state->{copyon};
406 0 $self->_just_get_a_line;
407 0 ▶ if ( defined $state->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state->{'line'}) { } 0 0 0
408 0 $state->{linenum}++;
409
410 0 ▶ if ( $state->{line} =~ /^ \s* \#if(n?)def \s+ (\w+) \s* $/x ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($state->{'line'} =~ /^ \s* \#if(n?)def \s+ (\w+) \s* $/x) { } 0 0 0
elsif ($state->{'line'} =~ /^ \s* \#(if|ifn|unless) \s+ (.*) $/x) { } 0 0 0
elsif ($state->{'line'} =~ /^ \s* \#elsif(n?)def \s+ (\w+) \s* $/x) { } 0 0 0
elsif ($state->{'line'} =~ /^ \s* \#elsif(n?) \s+ (.*) $/x) { } 0 0 0
elsif ($state->{'line'} =~ /^ \s* \#else \s* $/x) { } 0 0 0
elsif ($state->{'line'} =~ /^ \s* \#endif \s* $/x) { } 0 0 0
411 0 my ($neg, $macname) = ($1, $2);
412 0 &_pp_if($self->_pp_cond_def($macname, $neg), $state, $ifstack);
413 0 $done = 0;
414 } elsif ( $state->{line} =~ /^ \s* \#(if|ifn|unless) \s+ (.*) $/x ) {
415 0 my ($neg, $expr) = ($1 ne 'if', $2);
416 0 &_pp_if($self->_pp_cond_expr($expr, $neg), $state, $ifstack);
417 0 $done = 0;
418 } elsif ( $state->{line} =~ /^ \s* \#elsif(n?)def \s+ (\w+) \s* $/x ) {
419 0 my ($neg, $macname) = ($1, $2);
420 0 &_pp_else($self->_pp_cond_def($macname, $neg), $state, $ifstack);
421 0 $done = 0;
422 } elsif ( $state->{line} =~ /^ \s* \#elsif(n?) \s+ (.*) $/x ) {
423 0 my ($neg, $macname) = ($1, $2);
424 0 &_pp_else($self->_pp_cond_expr($macname, $neg), $state, $ifstack);
425 0 $done = 0;
426 } elsif ( $state->{line} =~ /^ \s* \#else \s* $/x ) {
427 0 &_pp_else(1, $state, $ifstack);
428 0 $done = 0;
429 } elsif ( $state->{line} =~ /^ \s* \#endif \s* $/x ) {
430 0 my $cond = 1;
431 0 ▶ if (@$ifstack==0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ifstack == 0) 0 0 0
432 0 croak "'#endif' without '#if'\n";
433 }
434 0 $state->{copyon} = $ifstack->[-1]<2;
435 0 pop @$ifstack;
436 0 $done = 0;
437 }
438 0 ▶ if ( $state->{copyon} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{'copyon'}) { } 0 0 0
439 0 ▶ if ( $state->{line} =~ /^ \s* \#(define|evaldef) \s+ (\w+) (?: \s+ (.*?) )? \s* $/x ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($state->{'line'} =~ /^ \s* \#(define|evaldef) \s+ (\w+) (?: \s+ (.*?) )? \s* $/x) { } 0 0 0
elsif ($state->{'line'} =~ /^ \s* \#define \s+ (\w+) \( \s* ( \w+ (?: \s* , \s* \w+ )* ) \s* \) (?: \s+ (.*?) )? \s* $/x) { } 0 0 0
elsif ($state->{'line'} =~ /^ \s* \#undef \s+ (\w+) \s* $/x) { } 0 0 0
elsif ($state->{'line'} =~ /^ \s* \#undef \s+ (\w+) \( \s* ( \w+ (?: \s* , \s* \w+ )* ) \s* \) \s* $/x) { } 0 0 0
elsif ($state->{'line'} =~ /^ \s* \#include \s+ \" (.+) \" (?: \s+ ([A-Za-z][A-Za-z0-9_]*))? \s* $/x) { } 0 0 0
elsif ($state->{'line'} =~ /^ \s* \#section_(start|end) \s+ ([A-Za-z][A-Za-z0-9_]*) \s* $/x) { } 0 0 0
elsif ($state->{'line'} =~ /$perm->{'macro_re'}/) { } 0 0 0
440 0 my ($deftype, $macname, $macdef) = ($1, $2, $3);
441 0 $self->_get_continued_command(\$macdef);
442 0 ▶ if ($deftype eq 'evaldef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deftype eq "evaldef") 0 0 0
443 0 $macdef = $self->_macro_eval($macdef);
444 }
445 0 ▶ if (defined $macdef) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $macdef) { } 0 0 0
446 0 $self->macro_define($macname, $macdef);
447 } else {
448 0 $self->macro_undefine($macname);
449 }
450 0 $done = 0;
451 } elsif ( $state->{line} =~ /^ \s* \#define \s+ (\w+) \( \s* ( \w+ (?: \s* , \s* \w+ )* ) \s* \) (?: \s+ (.*?) )? \s* $/x ) {
452 0 my ($macname, $fargs, $macdef) = ($1, $2, $3);
453 0 $self->_get_continued_command(\$macdef);
454 0 $self->macro_define_func($macname, $fargs, $macdef);
455 0 $done = 0;
456 } elsif ( $state->{line} =~ /^ \s* \#undef \s+ (\w+) \s* $/x ) {
457 0 my $macname = $1;
458 0 $self->macro_undefine($macname);
459 0 $done = 0;
460 } elsif ( $state->{line} =~ /^ \s* \#undef \s+ (\w+) \( \s* ( \w+ (?: \s* , \s* \w+ )* ) \s* \) \s* $/x ) {
461 0 $self->macro_undefine_func($1, $2);
462 0 $done = 0;
463 } elsif ( $state->{line} =~ /^ \s* \#include \s+ \" (.+) \" (?: \s+ ([A-Za-z][A-Za-z0-9_]*))? \s* $/x ) {
464 0 my $isf = $1;
465 0 my $incsec = $2;
466 0 ▶ die "filename is undef" unless defined $isf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $isf 0 0 0
467 0 $isf = rel2abs( $isf, dirname( $state->{source} ) );
468 0 ▶ croak "File '$isf' does not exist" unless -f $isf;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $isf 0 0 0
469 #print "## preprocessor will #include $isf\n";
470 0 $perm->{files_used}{$isf} = undef;
471 0 my $newpp = new( ref $self, sourcefile => $isf, incsection => $incsec );
472 0 $state = $newpp->[1];
473 0 $ifstack = $state->{ifstack};
474 0 push @$self, $state;
475 #print "## preprocessor is including $state->{source}\n";
476 0 $done = 0;
477 } elsif ($state->{line} =~ /^ \s* \#section_(start|end) \s+ ([A-Za-z][A-Za-z0-9_]*) \s* $/x ) {
478 0 my ($secend, $label) = ($1 eq 'end', $2);
479 #print "## found section end, label = '$label'\n";
480 0 $done = 0;
481 0 ▶ if ($secend && $state->{incsection}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secend and $state->{"incsection"}) 0 0 0
Condition0%0/3
$secend and $state->{"incsection"}
!ll&&!rl&&r
000
MC/DC0%0/2
$secend and $state->{"incsection"}
$secend $state->{"incsection"}
Truth table0%0/3
A and B
A= $secendB= $state->{"incsection"}
ABresult
0X0
100
111
482 0 $state->{copyon} = 0;
483 }
484 } elsif ($state->{line} =~ $perm->{macro_re}) {
485 # ordinary line, not a PP directive - has macro names in it thay may need expanding
486 0 $perm->{macros}{__LINE__}{replace} = $state->{linenum};
487 0 $perm->{macros}{__FILE__}{replace} = '"'.$state->{source}.'"';
488 0 $self->macro_replace;
489 }
490 } else {
491 0 $done = 0;
492 0 ▶ if ($state->{line} =~ /^ \s* \#section_start \s+ ([A-Za-z][A-Za-z0-9_]*) \s* $/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"line"} =~ /^ \s* \#section_start \s+ ([A-Za-z][A-Za-z0-9_]*) \s* $/x) 0 0 0
493 0 my $label = $1;
494 #print "## found section start, label = '$label'\n";
495 0 $done = 0;
496 0 ▶ if (defined $state->{incsection} && $label eq $state->{incsection}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state->{"incsection"} and $label eq $state->{"incsection"}) 0 0 0
Condition0%0/3
defined $state->{"incsection"} and $label eq $state->{"incsection"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $state->{"incsection"} and $label eq $state->{"incsection"}
defined $state->{"incsection"} $label eq $state->{"incsection"}
Truth table0%0/3
A and B
A= defined $state->{"incsection"}B= $label eq $state->{"incsection"}
ABresult
0X0
100
111
497 0 $state->{incsection_found} = 1;
498 0 $state->{copyon} = 1;
499 }
500 }
501 }
502 0 ▶ if ( $done && @$self > 2 && $state->{line} !~ m/(\015|\012)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done and @$self > 2 and not $state->{"line"} =~ /(\015|\012)$/) 0 0 0
Condition0%0/6
$done and @$self > 2 and not $state->{"line"} =~ /(\015|\012)$/
!ll&&!rl&&r
000
$done and @$self > 2
!ll&&!rl&&r
000
MC/DC0%0/3
$done and @$self > 2 and not $state->{"line"} =~ /(\015|\012)$/
$done @$self > 2 not $state->{"line"} =~ /(\015|\012)$/
Truth table0%0/4
A and B and not(C)
A= $doneB= @$self > 2C= not $state->{"line"} =~ /(\015|\012)$/
ABCresult
0XX0
10X0
1100
1111
503 # last line of this file, without a newline, but it's an included file so add a newline.
504 0 $state->{line} .= "\n";
505 }
506 } else { # $state->{line} is undef
507 0 ▶ if ( @$self > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$self > 2) { } 0 0 0
508 # already returned last line of this file, but it's an included file so drop back to the parent (including) file
509 #print "## preprocessor has finished including $state->{source}\n";
510 # discard the current state
511 0 pop @$self;
512 # restore the state from the stack
513 0 $state = $self->[-1];
514 0 $ifstack = $state->{ifstack};
515 #print "## preprocessor is returning to $state->{source}\n";
516 0 $done = 0;
517 } else {
518 #print "## preprocessor has come to the end of $state->{source}\n";
519 0 $done = 1;
520 }
521 }
522 }
523
524 #print "# got line $state->{linenum}: '$state->{line}'\n";
525 0 ▶ wantarray ? ( $state->{line}, $state->{source}, $state->{linenum} ) : $state->{line};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
526 }
527
528 0 ▶sub _get_continued_command {
Subroutine _get_continued_command: not called CC=5 SCAR=34.0
529 0 my ($self, $cmdref) = @_;
530 0 ▶ $$cmdref = '' if !defined $$cmdref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$cmdref 0 0 0
531 0 my $state = $self->[-1];
532 0 ▶ while ($$cmdref =~ /^ (.*?) \s* \\$/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$cmdref =~ /^ (.*?) \s* \\$/x) 0 0 0
533 0 $$cmdref = $1;
534 0 $self->_just_get_a_line;
535 0 ▶ last if !defined $state->{line};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state->{"line"} 0 0 0
536 0 $state->{linenum}++;
537 0 ▶ if ( $state->{line} =~ /^ \s* (.*?) \s* $/xs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"line"} =~ /^ \s* (.*?) \s* $/sx) 0 0 0
538 0 $$cmdref .= ' '.$1;
539 }
540 }
541 }
542
543 1 1;
« SourceFile.pm ↑ Summary UnitName.pm »