blib/lib/Inline/Filters.pm

filter: Statementstmt 80.6% 71 / 88 Branchbran 70.8% 17 / 24 Conditioncond 45.4% 5 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 78.5% 11 / 14 Podpod 100% 9 / 9 totaltotal 74.3% 113 / 152 CC 17 SCAR 16.0
CC
17
Cov
76%
Preprocess
9.7
_skip_quoted
5.8
_strip_comments
5.0
CRAP 5.0
↑ Summary Makefile.PL »
1 package Inline::Filters;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 our $VERSION = "0.19";
5 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 #============================================================================
8 # Object Interface
9 #============================================================================
10 sub new {
11 4 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
12 4 return bless { filter => shift, coderef => shift }, $class;
13 }
14  
15 sub filter {
16 4 ▶ my ($self, $o, $code) = @_;
Subroutine filter: called CC=1 SCAR=0.0
Pod documented
17 4 return $self->{coderef}->($o, $code);
18 }
19  
20 #============================================================================
21 # Strip POD
22 #============================================================================
23 sub Strip_POD {
24 2 ▶ my $ilsm = shift;
Subroutine Strip_POD: called CC=1 SCAR=0.0
Pod documented
25 2 my $code = shift;
26 2 $code =~ s/^=\w+[^\n]*\n\n(.*?)(^=cut\n\n|\Z)//gsm;
27 2 return $code;
28 }
29  
30 #============================================================================
31 # Strip comments in various languages
32 #============================================================================
33 sub _skip_quoted {
34 4 ▶ my ($text, $index, $closer) = @_;
Subroutine _skip_quoted: called CC=5 SCAR=17.5
35 4 ▶ for (my $i=$index+1; $i<length $text; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $text) 80 0 80
36 80 my $p = substr($text, $i-1, 1);
37 80 my $c = substr($text, $i, length($closer));
38 80 ▶ return $i if ($c eq $closer and ($p ne '\\' or length($closer)>1));
Branch: 1 branch
BranchTrueFalseTotal
if $c eq $closer and $p ne "\\" || length $closer > 1 4 76 80
Condition50%3/6
$c eq $closer and $p ne "\\" || length $closer > 1
!ll&&!rl&&r
7640
$p ne "\\" or length $closer > 1
l!l&&r!l&&!r
400
MC/DC0%0/3
$c eq $closer and $p ne "\\" || length $closer > 1
$c eq $closer $p ne "\\" length $closer > 1
Truth table25%1/4
A and B or C
A= $c eq $closerB= $p ne "\\"C= length $closer > 1
ABCresult
0XX0
1000
11X1
1011
39 }
40 0 return $index; # must not have been a string
41 }
42  
43 sub _strip_comments {
44 2 ▶ my ($txt, $opn, $cls, @quotes) = @_;
Subroutine _strip_comments: called CC=5 SCAR=16.1
45 2 my $i = -1;
46 2 ▶ while (++$i < length $txt) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i < length $txt) 359 2 361
47 359 my $closer;
48 359 ▶ if (grep {my $r=substr($txt,$i,length($_)) eq $_; $closer=$_ if $r; $r}
Branch: 2 branches
BranchTrueFalseTotal
if (grep {my $r = substr($txt, $i, length $_) eq $_; $closer = $_ if $r; $r;} @quotes) 4 355 359
if $r 4 355 359
49 @quotes) {
50 4 $i = _skip_quoted($txt, $i, $closer);
51 4 next;
52 }
53 355 ▶ if (substr($txt, $i, length($opn)) eq $opn) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($txt, $i, length $opn) eq $opn) 3 352 355
54 3 my $e = index($txt, $cls, $i) + length($cls);
55 3 substr($txt, $i, $e-$i) =~ s/[^\n]/ /g;
56 3 $i--;
57 3 next;
58 }
59 }
60 2 return $txt;
61 }
62  
63 # Note: strips both C and C++ comments because so many compilers accept
64 # both styles for C programs. Perhaps a --strict parameter?
65 sub Strip_C_Comments {
66 1 ▶ my $ilsm = shift;
Subroutine Strip_C_Comments: called CC=1 SCAR=0.0
Pod documented
67 1 my $code = shift;
68 1 $code = _strip_comments($code, '//', "\n", '"');
69 1 $code = _strip_comments($code, '/*', '*/', '"');
70 1 return $code;
71 }
72  
73 sub Strip_CPP_Comments {
74 0 ▶ my $ilsm = shift;
Subroutine Strip_CPP_Comments: not called CC=1 SCAR=6.9
Pod documented
75 0 my $code = shift;
76 0 $code = _strip_comments($code, '//', "\n", '"');
77 0 $code = _strip_comments($code, '/*', '*/', '"');
78 0 return $code;
79 }
80  
81 sub Strip_Python_Comments {
82 0 ▶ my $ilsm = shift;
Subroutine Strip_Python_Comments: not called CC=1 SCAR=6.9
Pod documented
83 0 my $code = shift;
84 0 $code = _strip_comments($code, '#', "\n", '"', '"""', '\'');
85 0 return $code;
86 }
87  
88 sub Strip_TCL_Comments {
89 0 ▶ my $ilsm = shift;
Subroutine Strip_TCL_Comments: not called CC=1 SCAR=6.9
Pod documented
90 0 my $code = shift;
91  
92 0 return $code;
93 }
94  
95 #============================================================================
96 # Preprocess C and C++
97 #============================================================================
98 sub Preprocess {
99 1 ▶ my $ilsm = shift;
Subroutine Preprocess: called CC=8 SCAR=22.8
Pod documented
100 1 my $code = shift;
101  
102 1 my @inc_array;
103 1 ▶ if (defined($ilsm->{ILSM}{MAKEFILE}{INC})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ilsm->{'ILSM'}{'MAKEFILE'}{'INC'}) { } 1 0 1
104 1 ▶ if (ref($ilsm->{ILSM}{MAKEFILE}{INC} eq 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($ilsm->{'ILSM'}{'MAKEFILE'}{'INC'} eq 'ARRAY')) { } 0 1 1
105 0 @inc_array = @{$ilsm->{ILSM}{MAKEFILE}{INC}};
106 }
107 else {
108 1 @inc_array = ($ilsm->{ILSM}{MAKEFILE}{INC});
109 }
110 }
111 else {
112 0 @inc_array = ();
113 }
114  
115 1 my $cppflags = q{};
116 1 ▶ if (defined $ilsm->{CONFIG}->{CPPFLAGS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ilsm->{"CONFIG"}{"CPPFLAGS"}) 0 1 1
117 0 $cppflags = $ilsm->{CONFIG}->{CPPFLAGS};
118 }
119 my $cpp = join ' ', ($Config{cpprun},
120 $Config{cppflags},
121 1 $cppflags,
122 "-I$Config{archlibexp}/CORE",
123 @inc_array
124 );
125  
126 1 my $tmpfile = File::Spec->catfile($ilsm->{API}{build_dir}, "Filters$$.c");
127 1 $ilsm->mkpath($ilsm->{API}{build_dir});
128 1 my ($CSRC, $PROCESSED);
129 1 ▶ open $CSRC, ">", $tmpfile or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $CSRC, ">", $tmpfile or die $! 0 1 1
130 1 print $CSRC $code;
131 1 close $CSRC;
132 1 ▶ open $PROCESSED, "$cpp \"$tmpfile\" |" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $PROCESSED, qq[$cpp "$tmpfile" |] or die $! 0 1 1
133 1 $code = join '', <$PROCESSED>;
134 1 close $PROCESSED;
135  
136 # default yes, will remove Filters*.c files, this is the same config option used by Inline::C(PP);
137 # must disable when using gdb to debug Inline::C(PP) code
138 1 ▶ if ((not defined $ilsm->{CONFIG}->{CLEAN_AFTER_BUILD}) or $ilsm->{CONFIG}->{CLEAN_AFTER_BUILD}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ilsm->{"CONFIG"}{"CLEAN_AFTER_BUILD"} or $ilsm->{"CONFIG"}{"CLEAN_AFTER_BUILD"}) 1 0 1
Condition33%1/3
not defined $ilsm->{"CONFIG"}{"CLEAN_AFTER_BUILD"} or $ilsm->{"CONFIG"}{"CLEAN_AFTER_BUILD"}
l!l&&r!l&&!r
010
MC/DC0%0/2
not defined $ilsm->{"CONFIG"}{"CLEAN_AFTER_BUILD"} or $ilsm->{"CONFIG"}{"CLEAN_AFTER_BUILD"}
not defined $ilsm->{"CONFIG"}{"CLEAN_AFTER_BUILD"} $ilsm->{"CONFIG"}{"CLEAN_AFTER_BUILD"}
Truth table33%1/3
not(A) or B
A= not defined $ilsm->{"CONFIG"}{"CLEAN_AFTER_BUILD"}B= $ilsm->{"CONFIG"}{"CLEAN_AFTER_BUILD"}
ABresult
1X1
011
000
139 1 unlink $tmpfile;
140 }
141  
142 1 return $code;
143 }
144  
145 #============================================================================
146 # Returns a list of key, value pairs; a filter and its code reference.
147 #============================================================================
148 3 my %filters =
149 (
150 ALL => [
151 Strip_POD => \&Strip_POD,
152 Preprocess => \&Preprocess,
153 ],
154 C => [
155 Strip_Comments => \&Strip_C_Comments,
156 ],
157 CPP => [
158 Strip_Comments => \&Strip_CPP_Comments,
159 ],
160 JAVA => [
161 Strip_Comments => \&Strip_CPP_Comments,
162 ],
163 );
164  
165 sub get_filters {
166 3 ▶ my $language = shift;
Subroutine get_filters: called CC=2 SCAR=7.0
Pod documented
167 3 my ($all, $lang) = @filters{ALL => $language};
168 3 ▶ $lang ||= [];
Condition50%1/2
$lang ||= []
l!l
30
MC/DC0%0/1
$lang ||= []
$lang
Truth table50%1/2
A
A= $lang
Aresult
11
00
169 3 return (@$all, @$lang);
170 }
171  
172 3 1;
↑ Summary Makefile.PL »