PreProcessor.pm

filter: Statementstmt 12.1% 12 / 99 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 27.2% 3 / 11 Podpod 0.0% 0 / 6 totaltotal 6.7% 15 / 222 CC 44 SCAR 51.9
CC
44
Cov
6%
new
380.0
_just_get_a_line
132.0
get_next_line
90.0
CRAP 179.1
« DesignUnit.pm ↑ Summary CLI.pm »
1 package Hardware::Vhdl::Automake::PreProcessor;
2
3 # file line-reader, which can delegate preprocessing to a preprocessor module specified in the first line of the file
4 # to do:
5 # check first line for PreProcessor selector, and attempt to auto-load it and use it
6
7 #use AutoLoader; # don't import AUTOLOAD, define our own
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 1 our $file_slurp_limit = 16000; # the size of the chunks of file we read at a time
13
14 1 our $VERSION = "1.00";
15
16 =for notes
17 For a 'delegate' style preprocessor, the module must provide the following class methods:
18 Class->pp_style() - must return the string 'delegate'
19 Class->new(sourcefile => $filename, includepaths => [$path1, $path2...], defines => {name1 => 'value1'...} )
20 and the following object methods:
21 get_next_line() - returns a line and its newline at the end, or the last line (perhaps without a newline) or undef if there are no more lines
22 linenum() - returns the line number of the last line fetched
23 sourcefile() - returns the source file of the last line fetched
24 files_used() - returns a list of all the source files used
25
26 For a 'wholefile_with_synclines' style preprocessor, the module must provide the following class methods:
27 Class->pp_style() - must return the string 'wholefile_with_synclines'
28 $tempfile = Class->preprocess(sourcefile => $filename, includepaths => [$path1, $path2...], defines => {name1 => 'value1'...} )
29 This should preprocess the file and return a filename for the output file, which will be deleted later
30 =cut
31
32 0 ▶sub new {
Subroutine new: not called CC=19 SCAR=59.4
Pod undocumented
33 0 my $class = shift;
34 0 my %args = @_;
35 0 my $self = {
36 ungot => '',
37 line => '',
38 linenum => 0,
39 source => undef,
40 endat => undef,
41 delegate => undef,
42 firstline => 1,
43 no_processing => 0,
44 };
45
46 0 ▶ if ( exists $args{no_processing} && $args{no_processing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'no_processing'} and $args{'no_processing'}) { } 0 0 0
Condition0%0/3
exists $args{"no_processing"} and $args{"no_processing"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"no_processing"} and $args{"no_processing"}
exists $args{"no_processing"} $args{"no_processing"}
Truth table0%0/3
A and B
A= exists $args{"no_processing"}B= $args{"no_processing"}
ABresult
0X0
100
111
47 0 $self->{no_processing} = 1;
48 0 for my $optname (qw/preprocessor/) {
49 0 ▶ croak "argument $optname to $class is not allowed if no_processing option is set" if exists $args{$optname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$optname} 0 0 0
50 }
51 } else {
52 0 for my $optname (qw/sourcestring startat endat linenum/) {
53 0 ▶ croak "argument $optname to $class is not allowed unless no_processing option is set" if exists $args{$optname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$optname} 0 0 0
54 }
55 }
56
57 0 ▶ if ( defined $args{sourcestring} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'sourcestring'}) { } 0 0 0
elsif (defined $args{'sourcefile'}) { } 0 0 0
58 0 $self->{sourcestring} = $args{sourcestring};
59 0 ▶ if (defined $self->{endat}) { $self->{sourcestring} = substr $self->{sourcestring}, 0, $self->{endat} }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"endat"}) 0 0 0
60 0 ▶ if (defined $self->{startat}) { $self->{sourcestring} = substr $self->{sourcestring}, $self->{startat} }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"startat"}) 0 0 0
61 0 $self->{source} = '<passed string>';
62 } elsif ( defined $args{sourcefile} ) {
63 0 my $fhi;
64 0 ▶ -f $args{sourcefile} || croak "File '$args{sourcefile}' does not exist\n";
Branch: 1 branch
BranchTrueFalseTotal
-f $args{"sourcefile"} or croak(qq[File '$args{"sourcefile"}' does not exist\n]) 0 0 0
65 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
66 0 $self->{fhi} = $fhi;
67 0 binmode $self->{fhi};
68 0 $self->{filebuf} = '';
69 0 $self->{source} = $args{sourcefile};
70 0 $self->{files_used}{ $args{sourcefile} } = undef;
71 } else {
72 0 ▶ croak "No source code specified" unless defined $self->{source};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"source"} 0 0 0
73 }
74 0 ▶ if (defined $args{startat} && defined $self->{fhi}) { seek $self->{fhi}, $args{startat}, 0 }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"startat"} and defined $self->{"fhi"}) 0 0 0
Condition0%0/3
defined $args{"startat"} and defined $self->{"fhi"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"startat"} and defined $self->{"fhi"}
defined $args{"startat"} defined $self->{"fhi"}
Truth table0%0/3
A and B
A= defined $args{"startat"}B= defined $self->{"fhi"}
ABresult
0X0
100
111
75 0 ▶ if (defined $args{endat}) { $self->{endat} = $args{endat}; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"endat"}) 0 0 0
76 0 ▶ if (defined $args{linenum}) { $self->{linenum} = $args{linenum}-1; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"linenum"}) 0 0 0
77 0 bless $self, $class;
78
79 0 ▶ $self->_select_preprocessor($args{preprocessor}) if ( defined $args{preprocessor} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"preprocessor"} 0 0 0
80
81 0 $self;
82 }
83
84 0 ▶sub linenum {
Subroutine linenum: not called CC=2 SCAR=17.9
Pod undocumented
85 # returns the line number of the last line fetched
86 0 ▶ defined $_[0]->{delegate} ? $_[0]->{delegate}->linenum : $_[0]->{linenum};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'delegate'} ? : 0 0 0
87 }
88
89 0 ▶sub sourcefile {
Subroutine sourcefile: not called CC=2 SCAR=17.9
Pod undocumented
90 # returns the source file of the last line fetched
91 0 ▶ defined $_[0]->{delegate} ? $_[0]->{delegate}->sourcefile : $_[0]->{source};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'delegate'} ? : 0 0 0
92 }
93
94 0 ▶sub files_used {
Subroutine files_used: not called CC=2 SCAR=17.9
Pod undocumented
95 # returns a list of all the source files used
96 0 ▶ defined $_[0]->{delegate} ? $_[0]->{delegate}->files_used : $_[0]->{source};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'delegate'} ? : 0 0 0
97 }
98
99 0 ▶sub unget {
Subroutine unget: not called CC=1 SCAR=6.9
Pod undocumented
100 0 my $self = shift;
101 0 $self->{ungot} = $_[0] . $self->{ungot};
102 }
103
104 0 ▶sub get_next_line {
Subroutine get_next_line: not called CC=9 SCAR=45.0
Pod undocumented
105 # returns a line and its newline at the end, or the last line (perhaps without a newline) or undef
106 0 my $self = shift;
107 0 ▶ return $self->{delegate}->get_next_line if defined $self->{delegate};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"delegate"} 0 0 0
108
109 0 ▶ if ($self->{ungot} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ungot'} ne '') { } 0 0 0
110 0 ▶ if ( $self->{ungot} =~ m/^(.*?(\015\012?|\012\015?))(.*)$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ungot'} =~ /^(.*?(\015\012?|\012\015?))(.*)$/s) { } 0 0 0
111 0 $self->{line} = $1;
112 0 $self->{ungot} = $3;
113 } else {
114 0 $self->{line} = $self->{ungot}."\n";
115 0 $self->{ungot} = '';
116 }
117 } else {
118 0 $self->_just_get_a_line;
119 0 ▶ if ( defined $self->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"line"}) 0 0 0
120 0 $self->{linenum}++;
121 }
122 }
123
124 # check for preprocessor selection in first line of file
125 #~ if ($self->{firstline}) {
126 #~ print "First line is $self->{line}\n";
127 #~ print "Preprocessing is " . ($self->{no_processing} ? 'disabled' : 'enabled') . "\n";
128 #~ }
129 0 ▶ if ($self->{firstline} && !$self->{no_processing} && $self->{line} =~ m/ --< \s* use \s+ preprocess[oe]r \s+ (\S+) \s* >-- /xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"firstline"} and not $self->{"no_processing"} and $self->{"line"} =~ / --< \s* use \s+ preprocess[oe]r \s+ (\S+) \s* >-- /msx) 0 0 0
Condition0%0/6
$self->{"firstline"} and not $self->{"no_processing"} and $self->{"line"} =~ / --< \s* use \s+ preprocess[oe]r \s+ (\S+) \s* >-- /msx
!ll&&!rl&&r
000
$self->{"firstline"} and not $self->{"no_processing"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"firstline"} and not $self->{"no_processing"} and $self->{"line"} =~ / --< \s* use \s+ preprocess[oe]r \s+ (\S+) \s* >-- /msx
$self->{"firstline"} not $self->{"no_processing"} $self->{"line"} =~ / --< \s* use \s+ preprocess[oe]r \s+ (\S+) \s* >-- /msx
Truth table0%0/4
A and not(B) and C
A= $self->{"firstline"}B= not $self->{"no_processing"}C= $self->{"line"} =~ / --< \s* use \s+ preprocess[oe]r \s+ (\S+) \s* >-- /msx
ABCresult
0XX0
10X0
1100
1111
130 0 my $ppname = $1;
131 #~ print "Preprocessor '$ppname' requested\n";
132 0 $self->{firstline} = 0;
133 0 $self->_select_preprocessor($ppname);
134 0 return $self->get_next_line;
135 }
136 0 $self->{firstline} = 0;
137
138 #print "# got line $self->{linenum}: '$self->{line}'\n";
139 0 ▶ wantarray ? ( $self->{line}, $self->{source}, $self->{linenum} ) : $self->{line};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
140 }
141
142 0 ▶sub _select_preprocessor {
Subroutine _select_preprocessor: not called CC=5 SCAR=34.0
143 0 my ($self, $pp) = @_;
144 0 my $ppclass = 'Hardware::Vhdl::Automake::PreProcessor::'.$pp;
145 #print "# preprocessor $pp requested!\n";
146 0 my $ppstyle;
147 0 eval "
148 require $ppclass;
149 #eval { import $ppclass; };
150 \$ppstyle = $ppclass->pp_style();
151 ";
152 0 ▶ die "Could not load preprocessor plugin '$pp':$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
153
154 0 ▶ if ($ppstyle eq 'delegate') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ppstyle eq 'delegate') { } 0 0 0
elsif ($ppstyle eq 'wholefile_with_synclines') { } 0 0 0
155 0 $self->{delegate} = $ppclass->new(sourcefile => $self->{source});
156 #print "# Delegating to preprocessor plugin '$pp'\n";
157 } elsif ($ppstyle eq 'wholefile_with_synclines') {
158 0 my $ppfile;
159 0 eval "\$ppfile = $ppclass->preprocess(sourcefile => \$self->{source});";
160 0 ▶ die "Preprocessor plugin '$pp' failed: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
161 0 $self->{delegate} = Hardware::Vhdl::Automake::PreProcessor->new(no_processing => 1, sourcefile => $ppfile, delete_source => 1);
162 } else {
163 0 die ref($self)." does not recognise preprocessor plugin style '$ppstyle'\n";
164 }
165 }
166
167 0 ▶sub _just_get_a_line {
Subroutine _just_get_a_line: not called CC=11 SCAR=48.8
168 0 my $self = shift;
169
170 0 $self->{line} = undef; # default return value
171 0 ▶ my $bufname = exists $self->{sourcestring} ? 'sourcestring' : 'filebuf';
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'sourcestring'} ? : 0 0 0
172
173 # 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.
174
175 GET_LINE: {
176 0 my $rem;
177 0 ▶ if (defined $self->{endat}) { $rem = $self->{endat} - tell($self->{fhi}) } # REMainding bytes we are allowed to read from the file
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"endat"}) 0 0 0
178 0 ▶ if ( $self->{$bufname} =~ m/^(.*?(\015\012?|\012\015?))(.*)$/s ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{$bufname} =~ /^(.*?(\015\012?|\012\015?))(.*)$/s) { } 0 0 0
elsif (exists $self->{'fhi'} and not eof $self->{'fhi'} and !defined($rem) || $rem > 0) { } 0 0 0
179 0 $self->{line} = $1;
180 0 $self->{$bufname} = $3;
181 ▶ } elsif ( exists $self->{fhi} && !eof $self->{fhi} && (!defined $rem || ($rem > 0))) {
Condition0%0/9
exists $self->{"fhi"} and not eof $self->{"fhi"} and !defined($rem) || $rem > 0
!ll&&!rl&&r
000
exists $self->{"fhi"} and not eof $self->{"fhi"}
!ll&&!rl&&r
000
not defined $rem or $rem > 0
l!l&&r!l&&!r
000
MC/DC0%0/4
exists $self->{"fhi"} and not eof $self->{"fhi"} and !defined($rem) || $rem > 0
exists $self->{"fhi"} not eof $self->{"fhi"} not defined $rem $rem > 0
Truth table0%0/5
A and not(B) and not(C) or D
A= exists $self->{"fhi"}B= not eof $self->{"fhi"}C= not defined $remD= $rem > 0
ABCDresult
0XXX0
10XX0
11000
111X1
11011
182 0 local $/ = \$file_slurp_limit;
183 0 ▶ if (defined $rem && $file_slurp_limit > $rem) { $/ = \$rem }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rem and $Hardware::Vhdl::Automake::PreProcessor::file_slurp_limit > $rem) 0 0 0
Condition0%0/3
defined $rem and $Hardware::Vhdl::Automake::PreProcessor::file_slurp_limit > $rem
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rem and $Hardware::Vhdl::Automake::PreProcessor::file_slurp_limit > $rem
defined $rem $Hardware::Vhdl::Automake::PreProcessor::file_slurp_limit > $rem
Truth table0%0/3
A and B
A= defined $remB= $Hardware::Vhdl::Automake::PreProcessor::file_slurp_limit > $rem
ABresult
0X0
100
111
184 0 $self->{$bufname} .= readline $self->{fhi};
185 0 redo GET_LINE;
186 } else {
187 0 $self->{line} = $self->{$bufname};
188 0 $self->{$bufname} = '';
189 }
190 }
191 0 ▶ $self->{line} = undef if $self->{line} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"line"} eq "" 0 0 0
192 }
193
194 1 1;
« DesignUnit.pm ↑ Summary CLI.pm »