Generate.pm

filter: Statementstmt 94.3% 150 / 159 Branchbran 79.0% 49 / 62 Conditioncond 88.0% 22 / 25 MC/DCmcdc 68.7% 11 / 16 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 1 totaltotal 89.0% 260 / 292 CC 47 SCAR 21.5
CC
47
Cov
90%
import
20.1
_mangle_exporter
10.6
_gen_map_rhash
8.3
CRAP 8.6
↑ Summary Generate/Dualvar.pm »
1 package Constant::Generate;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 our $VERSION = '0.17';
5  
6 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Constant::Generate::Dualvar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #these two functions produce reverse mapping, one for simple constants, and
12 #one for bitfields
13  
14 7 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 CONST_BITFLAG => 1,
16 CONST_SIMPLE => 2,
17 CONST_STRING => 3
18 7 };
19  
20 5 ▶sub _gen_bitfield_fn {
Subroutine _gen_bitfield_fn: called CC=1 SCAR=0.0
21 7 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 my ($name,$rhash) = @_;
23 5 ▶ *{$name} = sub($) {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 8 my $flag = $_[0];
25 join("|",
26 8 @{$rhash}{(
27 8 grep($flag & $_, keys %$rhash)
28 )}
29 );
30 5 };
31 }
32  
33 4 ▶sub _gen_int_fn {
Subroutine _gen_int_fn: called CC=1 SCAR=0.0
34 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 my ($name,$rhash) = @_;
36 4 ▶ *{$name} = sub ($) { $rhash->{$_[0] + 0} || "" };
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$$rhash{$_[0] + 0} or "" 8 0 8
37 }
38  
39 1 ▶sub _gen_str_fn {
Subroutine _gen_str_fn: called CC=1 SCAR=0.0
40 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 my ($name,$rhash) = @_;
42 1 ▶ *{$name} = sub ($) { $rhash->{ $_[0] } || "" };
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$$rhash{$_[0]} or "" 1 0 1
43 }
44  
45  
46 11 ▶sub _gen_integer_syms {
Subroutine _gen_integer_syms: called CC=2 SCAR=6.9
47 11 my ($uarr, $symhash, $start) = @_;
48 11 foreach my $sym (@$uarr) {
49 22 $symhash->{$sym} = $start;
50 22 $start++;
51 }
52 }
53  
54 4 ▶sub _gen_bitflag_syms {
Subroutine _gen_bitflag_syms: called CC=2 SCAR=6.9
55 4 my ($uarr,$symhash,$start) = @_;
56 4 foreach my $sym (@$uarr) {
57 11 $symhash->{$sym} = 1 << $start;
58 11 $start++;
59 }
60 }
61  
62 1 ▶sub _gen_string_syms {
Subroutine _gen_string_syms: called CC=2 SCAR=6.9
63 1 my ($uarr,$symhash,$prefix) = @_;
64 1 foreach my $sym (@$uarr) {
65 3 $symhash->{$sym} = $sym;
66 }
67 }
68  
69 49 ▶sub _gen_constant {
Subroutine _gen_constant: called CC=2 SCAR=6.9
70 49 my ($pkg,$name,@values) = @_;
71 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 49 my $fqname = $pkg . "::$name";
73 49 ▶ if(@values == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values == 1) { } 47 2 49
74 47 my $value = $values[0];
75 0 ▶ *{$fqname} = sub () { $value };
Subroutine __ANON__: not called CC=1 SCAR=0.4
76 } else {
77 2 ▶ *{$fqname} = sub () { @values };
Subroutine __ANON__: called CC=1 SCAR=0.0
78 }
79 }
80  
81 10 ▶sub _gen_map_rhash {
Subroutine _gen_map_rhash: called CC=7 SCAR=21.1
82 10 my ($symhash, $prefix, $display_prefix) = @_;
83 10 my (%maphash,%rhash);
84 10 ▶ if($prefix && $display_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix and $display_prefix) { } 0 10 10
Condition66%2/3
$prefix and $display_prefix
!ll&&!rl&&r
820
MC/DC0%0/2
$prefix and $display_prefix
$prefix $display_prefix
Truth table66%2/3
A and B
A= $prefixB= $display_prefix
ABresult
0X0
100
111
85 0 ▶ while (my ($sym,$val) = each %$symhash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$symhash) 0 0 0
86 0 $maphash{$prefix.$sym} = $val;
87 }
88 } else {
89 10 %maphash = %$symhash;
90 }
91
92 #Check for duplicate constants pointing to the same value
93 10 ▶ while (my ($sym,$val) = each %maphash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %maphash) 27 10 37
94 27 push @{$rhash{$val}}, $sym;
95 }
96 10 ▶ while (my ($val,$syms) = each %rhash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %rhash) 27 10 37
97 27 ▶ if(@$syms > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$syms > 1) { } 0 27 27
98 0 $rhash{$val} = sprintf("(%s)", join(",", @$syms));
99 } else {
100 27 $rhash{$val} = $syms->[0];
101 }
102 }
103 10 return \%rhash;
104 }
105  
106 20 ▶sub _mangle_exporter {
Subroutine _mangle_exporter: called CC=10 SCAR=23.6
107 20 my ($pkg, $symlist, $tag,
108 $uspec_export, $uspec_export_ok, $uspec_export_tags) = @_;
109
110 20 my @emap = (
111 [$uspec_export, \my $my_export, 'EXPORT', 'ARRAY'],
112 [$uspec_export_ok, \my $my_export_ok, 'EXPORT_OK', 'ARRAY'],
113 [$uspec_export_tags, \my $my_export_tags, 'EXPORT_TAGS', 'HASH', \$tag]
114 );
115
116 20 foreach (@emap) {
117 60 my ($uspec,$myspec,$pvar,$vtype,$depvar) = @$_;
118 60 ▶ if(!$uspec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uspec) 52 8 60
119 52 next;
120 }
121 8 ▶ if (defined $depvar && !$$depvar) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $depvar and not $$depvar) 0 8 8
Condition66%2/3
defined $depvar and not $$depvar
!ll&&!rl&&r
440
MC/DC0%0/2
defined $depvar and not $$depvar
defined $depvar not $$depvar
Truth table66%2/3
A and not(B)
A= defined $depvarB= not $$depvar
ABresult
0X0
100
111
122 0 next;
123 }
124 8 ▶ if(ref $uspec) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uspec) { } 0 8 8
125 0 $$myspec = $uspec;
126 } else {
127 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
128 8 ▶ if(!defined ($$myspec = *{$pkg."::$pvar"}{$vtype})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($$myspec = *{$pkg . "::$pvar";}{$vtype})) 0 8 8
129 0 confess "Requested mangling of $pvar, but $pvar not yet declared!";
130 }
131 }
132 }
133
134 20 ▶ if($uspec_export_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uspec_export_ok) 2 18 20
135 2 push @$my_export_ok, @$symlist;
136 }
137 20 ▶ if($uspec_export) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uspec_export) 2 18 20
138 2 push @$my_export, @$symlist;
139 }
140 20 ▶ if($uspec_export_tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uspec_export_tags) 4 16 20
141 4 $my_export_tags->{$tag} = [ @$symlist ];
142 }
143 #Verify the required variables
144 }
145  
146 7 my $FN_CONST_TBL = {
147 CONST_BITFLAG, \&_gen_bitflag_syms,
148 CONST_SIMPLE, \&_gen_integer_syms,
149 CONST_STRING, \&_gen_string_syms
150 };
151  
152 7 my $FN_RMAP_TBL = {
153 CONST_BITFLAG, \&_gen_bitfield_fn,
154 CONST_SIMPLE, \&_gen_int_fn,
155 CONST_STRING, \&_gen_str_fn,
156 };
157  
158 20 ▶sub utype2const {
Subroutine utype2const: called CC=5 SCAR=16.2
Pod undocumented
159 20 my $utype = shift;
160 20 ▶ if(!$utype || $utype =~ /int/i) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $utype or $utype =~ /int/i) { } 13 7 20
elsif ($utype =~ /bit/i) { } 6 1 7
elsif ($utype =~ /str/i) { } 1 0 1
Condition100%3/3
not $utype or $utype =~ /int/i
l!l&&r!l&&!r
1127
MC/DC100%2/2
not $utype or $utype =~ /int/i
not $utype $utype =~ /int/i
Truth table100%3/3
not(A) or B
A= not $utypeB= $utype =~ /int/i
ABresult
1X1
011
000
161 13 return CONST_SIMPLE;
162 } elsif ($utype =~ /bit/i) {
163 6 return CONST_BITFLAG;
164 } elsif ($utype =~ /str/i) {
165 1 return CONST_STRING;
166 } else {
167 0 die "Unrecognized type '$utype'";
168 }
169 }
170  
171 259 ▶sub _getopt(\%$) {
Subroutine _getopt: called CC=3 SCAR=11.0
172 259 my ($h,$opt) = @_;
173 259 ▶ foreach ($opt,"-$opt") { return delete $h->{$_} if exists $h->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
if exists $h->{$_} 43 459 502
174 }
175  
176 20 ▶sub import {
Subroutine import: called CC=20 SCAR=30.0
177 20 my ($cls,$symspecs,%opts) = @_;
178 20 ▶ return 1 unless $symspecs;
Branch: 1 branch
BranchTrueFalseTotal
unless $symspecs 0 20 20
179
180 20 my $reqpkg = caller();
181 20 my $type = utype2const(_getopt(%opts, "type"));
182
183 #Determine our tag for %EXPORT_TAGS and reverse mapping
184
185 20 my $mapname = _getopt(%opts, "mapname");
186 20 my $export_tag = _getopt(%opts, "tag");
187 20 ▶ my $prefix = _getopt(%opts, "prefix") || "";
Condition100%2/2
&_getopt(\%opts, 'prefix') || ''
l!l
515
MC/DC100%1/1
&_getopt(\%opts, 'prefix') || ''
&_getopt(\%opts, 'prefix')
Truth table100%2/2
A
A= &_getopt(\%opts, 'prefix')
Aresult
11
00
188 20 my $display_prefix = _getopt(%opts, "show_prefix");
189 20 ▶ my $start = _getopt(%opts, "start_at") || 0;
Condition100%2/2
&_getopt(\%opts, 'start_at') || 0
l!l
416
MC/DC100%1/1
&_getopt(\%opts, 'start_at') || 0
&_getopt(\%opts, 'start_at')
Truth table100%2/2
A
A= &_getopt(\%opts, 'start_at')
Aresult
11
00
190 20 ▶ my $stringy = _getopt(%opts, "stringy_vars")
Condition100%3/3
&_getopt(\%opts, 'stringy_vars') || &_getopt(\%opts, 'dualvar')
l!l&&r!l&&!r
1613
MC/DC100%2/2
&_getopt(\%opts, 'stringy_vars') || &_getopt(\%opts, 'dualvar')
&_getopt(\%opts, 'stringy_vars') &_getopt(\%opts, 'dualvar')
Truth table100%3/3
A || B
A= &_getopt(\%opts, 'stringy_vars')B= &_getopt(\%opts, 'dualvar')
ABresult
1X1
011
000
191 || _getopt(%opts, "dualvar");
192
193 20 my $listname = _getopt(%opts, "allvalues");
194 20 my $symsname = _getopt(%opts, "allsyms");
195
196 20 ▶ if((!$mapname) && $export_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $mapname and $export_tag) 4 16 20
Condition100%3/3
not $mapname and $export_tag
!ll&&!rl&&r
6104
MC/DC100%2/2
not $mapname and $export_tag
not $mapname $export_tag
Truth table100%3/3
not(A) and B
A= not $mapnameB= $export_tag
ABresult
0X0
100
111
197 4 $mapname = $export_tag . "_to_str";
198 }
199
200 #Generate the values.
201 20 my %symhash;
202 #Initial value
203
204 ref $symspecs eq 'HASH' ? %symhash = %$symspecs :
205 20 ▶ $FN_CONST_TBL->{$type}->($symspecs, \%symhash, $start);
Branch: 1 branch
BranchTrueFalseTotal
ref $symspecs eq 'HASH' ? : 4 16 20
206
207 #tie it all together
208
209 20 ▶ while (my ($symname,$symval) = each %symhash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %symhash) 47 20 67
210 47 ▶ if($stringy && looks_like_number($symval)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stringy and &looks_like_number($symval)) 13 34 47
Condition66%2/3
$stringy and &looks_like_number($symval)
!ll&&!rl&&r
34013
MC/DC50%1/2
$stringy and &looks_like_number($symval)
$stringy &looks_like_number($symval)
Truth table66%2/3
A and B
A= $stringyB= &looks_like_number($symval)
ABresult
0X0
100
111
211
212 13 ▶ my $dv_name = $display_prefix ? $prefix . $symname : $symname;
Branch: 1 branch
BranchTrueFalseTotal
$display_prefix ? : 0 13 13
213
214 13 $symval = Constant::Generate::Dualvar::CG_dualvar(
215 $symval, $dv_name);
216 }
217 47 _gen_constant($reqpkg, $prefix.$symname, $symval);
218 }
219
220 #After we have determined values for all the symbols, we can establish our
221 #reverse mappings, if so requested
222 20 ▶ if($mapname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapname) 10 10 20
223 10 my $rhash = _gen_map_rhash(\%symhash, $prefix, $display_prefix);
224 10 $FN_RMAP_TBL->{$type}->($reqpkg."::$mapname", $rhash);
225 }
226
227 20 ▶ if($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 5 15 20
228 5 foreach my $usym (keys %symhash) {
229 11 my $v = delete $symhash{$usym};
230 11 $symhash{$prefix.$usym} = $v;
231 }
232 }
233
234 20 my $auto_export = _getopt(%opts, "export");
235 20 my $auto_export_ok = _getopt(%opts, "export_ok");
236 20 my $h_etags = _getopt(%opts, "export_tags");
237
238 20 my @symlist = keys %symhash;
239
240 20 ▶ if($listname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($listname) 1 19 20
241 1 my %tmp = reverse %symhash;
242 1 _gen_constant($reqpkg, $listname, keys %tmp);
243 1 push @symlist, $listname;
244 }
245 20 ▶ if($symsname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symsname) 1 19 20
246 1 _gen_constant($reqpkg, $symsname, keys %symhash);
247 1 push @symlist, $symsname;
248 }
249
250 20 ▶ push @symlist, $mapname if $mapname;
Branch: 1 branch
BranchTrueFalseTotal
if $mapname 10 10 20
251 20 ▶ _mangle_exporter($reqpkg, \@symlist,
Condition100%3/3
$h_etags || $export_tag
l!l&&r!l&&!r
2216
MC/DC100%2/2
$h_etags || $export_tag
$h_etags $export_tag
Truth table100%3/3
A || B
A= $h_etagsB= $export_tag
ABresult
1X1
011
000
252 $export_tag,
253 $auto_export, $auto_export_ok, $h_etags || $export_tag);
254  
255 20 ▶ if(%opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opts) 0 20 20
256 0 die "Unknown keys " . join(",", keys %opts);
257 }
258 }
259  
260 __END__
↑ Summary Generate/Dualvar.pm »