Sepia/Xref.pm

filter: Statementstmt 19.1% 67 / 350 Branchbran 3.1% 5 / 158 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 25.8% 15 / 58 Podpod 26.1% 11 / 42 totaltotal 13.8% 98 / 709 CC 106 SCAR 41.6
CC
106
Cov
13%
process
272.0
guess_module_file
156.0
unmention
90.0
CRAP 64.2
« Sepia.pm ↑ Summary Sepia/Debug.pm »
1 package Sepia::Xref;
2  
3 =head1 NAME
4  
5 Sepia::Xref - Generates cross reference database for use by Perl programs.
6  
7 =head1 SYNOPSIS
8  
9 use Sepia::Xref qw(rebuild defs callers);
10  
11 rebuild;
12 for (defs 'foo') {
13 printf "%s:%d: sub %s\::foo() defined\n", @{$_}[0..2];
14 }
15  
16 for (callers 'foo') {
17 printf "%s:%d: sub foo() called by %s\::%s().\n", @{$_}[0..3];
18 }
19  
20 =head1 DESCRIPTION
21  
22 C<Sepia::Xref> is intended as a programmatic interface to the
23 information supplied by L<B::Xref>. It is intended to be a component
24 for interactive Perl development, with other packages providing a
25 friendly interface to the raw information it extracts. C<B::Xref>
26 could be seen as an example of this sort of user-level tool, if it
27 weren't for the fact that this module was created later, and stole
28 most of its code.
29  
30 =cut
31  
32 # use Sepia '_apropos_re';
33 1 require Sepia;
34 1 ▶BEGIN { *_apropos_re = *Sepia::_apropos_re; }
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use B qw(peekop class comppadlist main_start svref_2object walksymtable
Subroutine BEGIN: called CC=1 SCAR=0.0
40 OPpLVAL_INTRO SVf_POK OPpOUR_INTRO OPf_MOD OPpDEREF_HV OPpDEREF_AV
41 1 cstring);
42 # stupid warnings...
43 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 =head2 Variables
46  
47 =over
48  
49 =item C<%call>
50  
51 A map of subs to call locations and callers
52  
53 =item C<%callby>
54  
55 A map of subs to subs called.
56  
57 =item C<%var_use>
58  
59 A map of global/package variables to uses.
60  
61 =item C<%var_def>
62  
63 A map of global/package variables to definitions (usually empty, since
64 it only picks up local (...) declarations.
65  
66 =back
67  
68 =cut
69  
70 1 our %call;
71 1 our %callby;
72 1 our %var_def;
73 1 our %var_use;
74  
75 1 require Exporter;
76 1 our @ISA = 'Exporter';
77 1 my @most = qw(redefined forget rebuild callers callees
78 var_defs var_uses
79 var_apropos);
80 1 our @EXPORT_OK = (@most,
81 qw(xref_definitions xref_object xref_main
82 %call %callby %var_use %var_def));
83  
84 1 our %EXPORT_TAGS =
85 (':all' => \@EXPORT_OK,
86 ':most' => \@most);
87  
88 ######################################################################
89 ## Xref state variables:
90  
91 1 ▶sub UNKNOWN { ["?", "?", "?"] }
Subroutine UNKNOWN: called CC=1 SCAR=0.0
Pod undocumented
92  
93 1 my @pad; # lexicals in current pad
94 # as ["(lexical)", type, name]
95 1 my @padval;
96 1 our %done; # keyed by $$op: set when each $op is done
97 1 my $top = UNKNOWN; # shadows top element of stack as
98 # [pack, type, name] (pack can be "(lexical)")
99 1 our $file; # shadows current filename
100 1 my $line; # shadows current line number
101 1 our $subname; # shadows current sub name
102 1 our @todo = (); # List of CVs that need processing
103 1 my $lastclass; # last bareword seen after entersub.
104  
105 1 our $DEBUG = 0;
106 0 ▶sub dprint {
Subroutine dprint: not called CC=2 SCAR=17.9
Pod undocumented
107 0 my $type = shift;
108 0 my $res = "@_";
109 0 $res =~ s/%//g; # XXX: work around EPL's misuse of (message)
110 0 ▶ print STDERR "@_" if $DEBUG =~ /$type/;
Branch: 1 branch
BranchTrueFalseTotal
if $Sepia::Xref::DEBUG =~ /$type/ 0 0 0
111 }
112  
113 1 my %code = (intro => "i", used => "",
114 subdef => "s", subused => "&",
115 formdef => "f", meth => "->");
116  
117 =head2 Functions
118  
119 =over 4
120  
121 =item C<guess_module_file($pack, $ofile)>
122  
123 XXX: it turns out that rooting around trying to figure out the file
124 ourselves is more reliable than what we grab from the op. Are we
125 doing this wrong?
126  
127 =cut
128  
129 0 ▶sub guess_module_file {
Subroutine guess_module_file: not called CC=12 SCAR=50.5
Pod documented
130 0 my ($pack, $ofile) = @_;
131 0 my $file;
132  
133 # XXX: is this why we get the bogus defs?
134 0 ▶ return undef if $ofile =~ /Exporter\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ofile =~ /Exporter\.pm$/ 0 0 0
135 # Try for standard translation in %INC:
136 0 (my $fn = $pack) =~ s/::/\//g;
137 0 ▶ return unless $fn; # stupid warnings...
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 0 0
138 0 ▶ if (exists $INC{"$fn.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $INC{"$fn.pm"}) 0 0 0
139 0 return $INC{"$fn.pm"};
140 }
141  
142 # Try what they told us:
143 0 chomp $ofile;
144 0 ▶ return $ofile if -f $ofile;
Branch: 1 branch
BranchTrueFalseTotal
if -f $ofile 0 0 0
145  
146 # Try "parent" packages:
147 0 ▶ while ($fn =~ s|/?[^/]+$|| && !$file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn =~ s[/?[^/]+$][] and not $file) 0 0 0
Condition0%0/3
$fn =~ s[/?[^/]+$][] and not $file
!ll&&!rl&&r
000
MC/DC0%0/2
$fn =~ s[/?[^/]+$][] and not $file
$fn =~ s[/?[^/]+$][] not $file
Truth table0%0/3
A and not(B)
A= $fn =~ s[/?[^/]+$][]B= not $file
ABresult
0X0
100
111
148 0 ▶ $file ||= $INC{"$fn.pm"};
Condition0%0/3
$file ||= $INC{"$fn.pm"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$file ||= $INC{"$fn.pm"}
$file $INC{"$fn.pm"}
Truth table0%0/3
A ||= B
A= $fileB= $INC{"$fn.pm"}
ABresult
1X1
011
000
149 }
150  
151 0 ▶ if ($file && $file !~ /^\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file and not $file =~ m[^/]) 0 0 0
Condition0%0/3
$file and not $file =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/2
$file and not $file =~ m[^/]
$file not $file =~ m[^/]
Truth table0%0/3
A and not(B)
A= $fileB= not $file =~ m[^/]
ABresult
0X0
100
111
152 0 $file = abs_path($file);
153 }
154  
155 0 ▶ if (!$file || !-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file and -f $file) 0 0 0
Condition0%0/3
$file and -f $file
!ll&&!rl&&r
000
MC/DC0%0/2
$file and -f $file
$file -f $file
Truth table0%0/3
A and B
A= $fileB= -f $file
ABresult
0X0
100
111
156 0 undef $file;
157 }
158 0 $file;
159 }
160  
161 # XXX: should weed through the code below so it only generates decent
162 # package names, but this will fix it for now.
163 0 ▶sub realpack {
Subroutine realpack: not called CC=5 SCAR=34.0
Pod undocumented
164 0 my $p = shift;
165 0 ▶ if (!defined $p || $p eq '?' || $p eq '(method)') {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $p or $p eq '?' or $p eq '(method)') { } 0 0 0
elsif ($p eq '') { } 0 0 0
Condition0%0/6
not defined $p or $p eq "?" or $p eq "(method)"
l!l&&r!l&&!r
000
not defined $p or $p eq "?"
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $p or $p eq "?" or $p eq "(method)"
not defined $p $p eq "?" $p eq "(method)"
Truth table0%0/4
not(A) or B or C
A= not defined $pB= $p eq "?"C= $p eq "(method)"
ABCresult
1XX1
01X1
0011
0000
166 0 return undef;
167 } elsif ($p eq '') {
168 0 return 'main';
169 } else {
170 0 return $p;
171 }
172 }
173  
174 # Turn a possibly-qualified name into a package and basename.
175 0 ▶sub split_name {
Subroutine split_name: not called CC=3 SCAR=24.8
Pod undocumented
176 0 local $_ = shift;
177 0 my ($p, $s);
178 0 ▶ if (/^(.*)::(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(.*)::(.+)$/) { } 0 0 0
179 0 ($p, $s) = ($1, $2);
180 } else {
181 0 ($p, $s) = ('main', $_);
182 }
183 0 ▶ undef $s if $s eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "?" 0 0 0
184 0 ($p, $s);
185 }
186  
187 0 ▶sub process {
Subroutine process: not called CC=16 SCAR=56.1
Pod undocumented
188 0 my ($var, $event) = @_;
189 0 my ($pack, $type, $name) = @$var;
190 0 $pack = realpack($pack);
191 0 dprint 'loud', "Processing $event: @$var ($subname)";
192 0 ▶ if ($type eq "*") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "*") 0 0 0
193 0 ▶ if ($event eq "used" || $event eq 'set') {
Branch: 3 branches
BranchTrueFalseTotal
if ($event eq 'used' or $event eq 'set') { } 0 0 0
elsif ($event eq 'subused') { } 0 0 0
elsif ($event eq 'meth') { } 0 0 0
Condition0%0/3
$event eq "used" or $event eq "set"
l!l&&r!l&&!r
000
MC/DC0%0/2
$event eq "used" or $event eq "set"
$event eq "used" $event eq "set"
Truth table0%0/3
A or B
A= $event eq "used"B= $event eq "set"
ABresult
1X1
011
000
194 0 return;
195 } elsif ($event eq "subused") {
196 0 $type = "&";
197 } elsif ($event eq "meth") {
198 0 $type = '->';
199 }
200 }
201 0 $type =~ s/(.)\*$/$1/g;
202 0 $file = guess_module_file($pack, $file);
203  
204 0 ▶ if (($type eq '&' || $type eq '->') && $subname ne '(definitions)') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq '&' || $type eq '->' and $Sepia::Xref::subname ne '(definitions)') { } 0 0 0
Condition0%0/6
$type eq "&" || $type eq "->" and $Sepia::Xref::subname ne "(definitions)"
!ll&&!rl&&r
000
$type eq "&" or $type eq "->"
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "&" || $type eq "->" and $Sepia::Xref::subname ne "(definitions)"
$type eq "&" $type eq "->" $Sepia::Xref::subname ne "(definitions)"
Truth table0%0/5
A or B and C
A= $type eq "&"B= $type eq "->"C= $Sepia::Xref::subname ne "(definitions)"
ABCresult
00X0
1X00
0100
1X11
0111
205 # Handle caller/callee relations
206 0 my ($spack, $sname) = split_name($subname);
207  
208 0 $call{$name}{$pack}{$subname} = 1;
209 0 ▶ $callby{$sname}{$spack}{"$pack\::$name"} = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (do { $type eq 's' or $Sepia::Xref::subname eq '(definitions)' }) { } 0 0 0
elsif (not $name =~ /^[\x00-\x1f^] | ^\d+$ | ^[\W_]$ | ^(?:ENV|INC|STD(?:IN|OUT|ERR)|SIG)$ /x and realpack($pack)) { } 0 0 0
210 ▶ } elsif ($type eq 's' || $subname eq '(definitions)') {
Condition0%0/3
$type eq "s" or $Sepia::Xref::subname eq "(definitions)"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "s" or $Sepia::Xref::subname eq "(definitions)"
$type eq "s" $Sepia::Xref::subname eq "(definitions)"
Truth table0%0/3
A or B
A= $type eq "s"B= $Sepia::Xref::subname eq "(definitions)"
ABresult
1X1
011
000
211 # definition
212 ▶ } elsif ($name !~ /^[\x00-\x1f^] | ^\d+$ | ^[\W_]$
Condition0%0/3
not $name =~ /^[\x00-\x1f^] | ^\d+$ | ^[\W_]$ | ^(?:ENV|INC|STD(?:IN|OUT|ERR)|SIG)$ /x and realpack($pack)
!ll&&!rl&&r
000
MC/DC0%0/2
not $name =~ /^[\x00-\x1f^] | ^\d+$ | ^[\W_]$ | ^(?:ENV|INC|STD(?:IN|OUT|ERR)|SIG)$ /x and realpack($pack)
not $name =~ /^[\x00-\x1f^] | ^\d+$ | ^[\W_]$ | ^(?:ENV|INC|STD(?:IN|OUT|ERR)|SIG)$ /x realpack($pack)
Truth table0%0/3
not(A) and B
A= not $name =~ /^[\x00-\x1f^] | ^\d+$ | ^[\W_]$ | ^(?:ENV|INC|STD(?:IN|OUT|ERR)|SIG)$ /xB= realpack($pack)
ABresult
0X0
100
111
213 | ^(?:ENV|INC|STD(?:IN|OUT|ERR)|SIG)$ /x
214 && realpack($pack)) {
215 # Variables, but ignore specials and lexicals
216 0 my ($spack, $sname) = split_name($subname);
217 0 ▶ if ($event eq 'intro') {
Branch: 2 branches
BranchTrueFalseTotal
if ($event eq 'intro') { } 0 0 0
elsif ($event eq 'used' or $event eq 'set') { } 0 0 0
218 0 $var_def{$name}{$pack} =
219 { file => $file,
220 package => $spack,
221 line => $line,
222 sub => $sname,
223 };
224 ▶ } elsif ($event eq 'used' || $event eq 'set') {
Condition0%0/3
$event eq "used" or $event eq "set"
l!l&&r!l&&!r
000
MC/DC0%0/2
$event eq "used" or $event eq "set"
$event eq "used" $event eq "set"
Truth table0%0/3
A or B
A= $event eq "used"B= $event eq "set"
ABresult
1X1
011
000
225 0 push @{$var_use{$name}{$pack}},
226 { file => $file,
227 package => $spack,
228 line => $line,
229 sub => $sname,
230 assign => ($event eq 'set'),
231 };
232 } else {
233 0 dprint 'ignore', "Ignoring var event $event";
234 }
235 } else {
236 0 dprint 'ignore', "Ignoring $type event $event";
237 }
238 }
239  
240 0 ▶sub load_pad {
Subroutine load_pad: not called CC=8 SCAR=42.8
Pod undocumented
241 0 my $padlist = shift;
242 0 my ($namelistav, $vallistav, @namelist, $ix);
243 0 @pad = ();
244 0 @padval = ();
245 0 ▶ return if class($padlist) eq "SPECIAL";
Branch: 1 branch
BranchTrueFalseTotal
if Sepia::Xref::class($padlist) eq "SPECIAL" 0 0 0
246 0 ($namelistav,$vallistav) = $padlist->ARRAY;
247 0 @namelist = $namelistav->ARRAY;
248 0 ▶ for ($ix = 1; $ix < @namelist; $ix++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @namelist) 0 0 0
249 0 my $namesv = $namelist[$ix];
250 0 ▶ next if class($namesv) eq "SPECIAL";
Branch: 1 branch
BranchTrueFalseTotal
if Sepia::Xref::class($namesv) eq "SPECIAL" 0 0 0
251 0 my ($type, $name) = $namesv->PV =~ /^(.)([^\0]*)(\0.*)?$/;
252 0 $pad[$ix] = [undef, $type, $name];
253 }
254 0 ▶ if ($Config{useithreads}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Sepia::Xref::Config{"useithreads"}) 0 0 0
255 0 my (@vallist);
256 0 @vallist = $vallistav->ARRAY;
257 0 ▶ for ($ix = 1; $ix < @vallist; $ix++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @vallist) 0 0 0
258 0 my $valsv = $vallist[$ix];
259 0 ▶ next unless class($valsv) eq "GV";
Branch: 1 branch
BranchTrueFalseTotal
unless Sepia::Xref::class($valsv) eq "GV" 0 0 0
260 # these pad GVs don't have corresponding names, so same @pad
261 # array can be used without collisions
262  
263 # XXX: for some reason, on 5.10 $valsv->STASH can be a
264 # B::SPECIAL, which doesn't have a name.
265  
266 # XXX: this segfaults on 5.10 for some reason while
267 # traversing File::Find::contract_name from main
268  
269 0 ▶ next if ref $valsv->STASH eq 'B::SPECIAL';
Branch: 1 branch
BranchTrueFalseTotal
if ref $valsv->STASH eq "B::SPECIAL" 0 0 0
270 0 $pad[$ix] = [$valsv->STASH->NAME, "*", $valsv->NAME];
271 }
272 }
273 0 @padval = $vallistav->ARRAY;
274 }
275  
276 0 ▶sub xref {
Subroutine xref: not called CC=10 SCAR=44.8
Pod undocumented
277 0 my $start = shift;
278 0 my $op;
279 0 ▶ for ($op = $start; $$op; $op = $op->next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$op) 0 0 0
280 0 ▶ last if $done{$$op}++;
Branch: 1 branch
BranchTrueFalseTotal
if $Sepia::Xref::done{$$op}++ 0 0 0
281 0 my $opname = $op->name;
282 0 ▶ if ($opname =~ /^(or|and|mapwhile|grepwhile|range|cond_expr)$/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($opname =~ /^(or|and|mapwhile|grepwhile|range|cond_expr)$/) { } 0 0 0
elsif ($opname eq 'match' or $opname eq 'subst') { } 0 0 0
elsif ($opname eq 'substcont') { } 0 0 0
elsif ($opname eq 'enterloop') { } 0 0 0
elsif ($opname eq 'subst') { } 0 0 0
283 0 xref($op->other);
284 ▶ } elsif ($opname eq "match" || $opname eq "subst") {
Condition0%0/3
$opname eq "match" or $opname eq "subst"
l!l&&r!l&&!r
000
MC/DC0%0/2
$opname eq "match" or $opname eq "subst"
$opname eq "match" $opname eq "subst"
Truth table0%0/3
A or B
A= $opname eq "match"B= $opname eq "subst"
ABresult
1X1
011
000
285 0 xref($op->pmreplstart);
286 } elsif ($opname eq "substcont") {
287 0 xref($op->other->pmreplstart);
288 0 $op = $op->other;
289 0 redo;
290 } elsif ($opname eq "enterloop") {
291 0 xref($op->redoop);
292 0 xref($op->nextop);
293 0 xref($op->lastop);
294 } elsif ($opname eq "subst") {
295 0 xref($op->pmreplstart);
296 } else {
297 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
298 0 my $ppname = "pp_$opname";
299 0 ▶ &$ppname($op) if defined(&$ppname);
Branch: 1 branch
BranchTrueFalseTotal
if defined &$ppname 0 0 0
300 }
301 }
302 }
303  
304 0 ▶sub xref_cv {
Subroutine xref_cv: not called CC=2 SCAR=17.9
Pod undocumented
305 0 my $cv = shift;
306 0 my $pack = $cv->GV->STASH->NAME;
307 0 ▶ local $subname = ($pack eq "main" ? "" : "$pack\::") . $cv->GV->NAME;
Branch: 1 branch
BranchTrueFalseTotal
$pack eq 'main' ? : 0 0 0
308 0 load_pad($cv->PADLIST);
309 0 xref($cv->START);
310 }
311  
312 0 ▶sub xref_object {
Subroutine xref_object: not called CC=1 SCAR=6.9
Pod undocumented
313 0 my $cvref = shift;
314 0 local (@todo, %done);
315 0 my $cv = svref_2object($cvref);
316 0 xref_cv($cv);
317 0 dprint 'todo', "todo = (@todo)";
318 0 my $gv = $cv->GV;
319 0 process([$gv->STASH->NAME, '&', $gv->NAME], 'subdef');
320 }
321  
322 0 ▶sub xref_main {
Subroutine xref_main: not called CC=2 SCAR=17.9
Pod undocumented
323 0 $subname = "(main)";
324 0 load_pad(comppadlist);
325 0 xref(main_start);
326 0 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Sepia::Xref::todo) 0 0 0
327 0 xref_cv(shift @todo);
328 }
329 }
330  
331 0 ▶sub pp_pushmark {
Subroutine pp_pushmark: not called CC=6 SCAR=37.4
Pod undocumented
332 0 my $op = shift;
333 0 my ($class, $meth);
334 0 ▶ if (($class = $op->next)->name eq 'const') {
Branch: 1 branch
BranchTrueFalseTotal
if (($class = $op->next)->name eq "const") 0 0 0
335 0 my $sv = $class->sv;
336 0 my $classname;
337 # constant could be in the pad (under useithreads)
338 0 ▶ if (class($sv) ne "SPECIAL" && $sv->FLAGS & SVf_POK) {
Branch: 1 branch
BranchTrueFalseTotal
if (Sepia::Xref::class($sv) ne 'SPECIAL' and $sv->FLAGS & 1024) { } 0 0 0
Condition0%0/3
Sepia::Xref::class($sv) ne "SPECIAL" and $sv->FLAGS & 1024
!ll&&!rl&&r
000
MC/DC0%0/2
Sepia::Xref::class($sv) ne "SPECIAL" and $sv->FLAGS & 1024
Sepia::Xref::class($sv) ne "SPECIAL" $sv->FLAGS & 1024
Truth table0%0/3
A and B
A= Sepia::Xref::class($sv) ne "SPECIAL"B= $sv->FLAGS & 1024
ABresult
0X0
100
111
339 0 $classname = $sv->PV;
340 } else {
341 0 my $pv = $padval[$class->targ];
342 0 ▶ if (class($pv) =~ /^PV/ && class($sv) eq 'SPECIAL'
Branch: 1 branch
BranchTrueFalseTotal
if (Sepia::Xref::class($pv) =~ /^PV/ and Sepia::Xref::class($sv) eq "SPECIAL") 0 0 0
Condition0%0/3
Sepia::Xref::class($pv) =~ /^PV/ and Sepia::Xref::class($sv) eq "SPECIAL"
!ll&&!rl&&r
000
MC/DC0%0/2
Sepia::Xref::class($pv) =~ /^PV/ and Sepia::Xref::class($sv) eq "SPECIAL"
Sepia::Xref::class($pv) =~ /^PV/ Sepia::Xref::class($sv) eq "SPECIAL"
Truth table0%0/3
A and B
A= Sepia::Xref::class($pv) =~ /^PV/B= Sepia::Xref::class($sv) eq "SPECIAL"
ABresult
0X0
100
111
343 ## bareword flag -- should use this?
344 # && ($op->private & 64)
345 ) {
346 0 $classname = $pv->PV;
347 }
348 }
349 0 $lastclass = $classname;
350 }
351 }
352  
353 0 ▶sub pp_nextstate {
Subroutine pp_nextstate: not called CC=2 SCAR=17.9
Pod undocumented
354 0 my $op = shift;
355 0 $file = $op->file;
356 0 ▶ die "pp_nextstate: $file" if $file =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
if $Sepia::Xref::file =~ /::/ 0 0 0
357 0 $line = $op->line;
358 0 $top = UNKNOWN;
359 }
360  
361 0 ▶sub use_type($) {
Subroutine use_type: not called CC=4 SCAR=30.0
Pod undocumented
362 0 my ($op) = @_;
363 0 ▶ if ($op->private & (OPpLVAL_INTRO | OPpOUR_INTRO)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->private & 192) { } 0 0 0
elsif ($op->flags & 32 and not $op->private & 48) { } 0 0 0
364 0 'intro';
365 ▶ } elsif ($op->flags & OPf_MOD
Condition0%0/3
$op->flags & 32 and not $op->private & 48
!ll&&!rl&&r
000
MC/DC0%0/2
$op->flags & 32 and not $op->private & 48
$op->flags & 32 not $op->private & 48
Truth table0%0/3
A and not(B)
A= $op->flags & 32B= not $op->private & 48
ABresult
0X0
100
111
366 && !($op->private & (OPpDEREF_HV | OPpDEREF_AV))) {
367 0 'set';
368 } else {
369 0 'used';
370 }
371 }
372  
373 0 ▶sub pp_padsv {
Subroutine pp_padsv: not called CC=1 SCAR=6.9
Pod undocumented
374 0 my $op = shift;
375 0 $top = $pad[$op->targ];
376 # process($top, $op->private & OPpLVAL_INTRO ? "intro" : "used");
377 }
378  
379 0 ▶sub pp_padav { pp_padsv(@_) }
Subroutine pp_padav: not called CC=1 SCAR=6.9
Pod undocumented
380 0 ▶sub pp_padhv { pp_padsv(@_) }
Subroutine pp_padhv: not called CC=1 SCAR=6.9
Pod undocumented
381  
382 0 ▶sub deref {
Subroutine deref: not called CC=1 SCAR=6.9
Pod undocumented
383 0 my ($op, $var, $as) = @_;
384 0 $var->[1] = $as . $var->[1];
385 0 process($var, use_type $op);
386 }
387  
388 0 ▶sub pp_rv2cv { deref(shift, $top, "&"); }
Subroutine pp_rv2cv: not called CC=1 SCAR=6.9
Pod undocumented
389 0 ▶sub pp_rv2hv { deref(shift, $top, "%"); }
Subroutine pp_rv2hv: not called CC=1 SCAR=6.9
Pod undocumented
390 0 ▶sub pp_rv2sv { deref(shift, $top, "\$"); }
Subroutine pp_rv2sv: not called CC=1 SCAR=6.9
Pod undocumented
391 0 ▶sub pp_rv2av { deref(shift, $top, "\@"); }
Subroutine pp_rv2av: not called CC=1 SCAR=6.9
Pod undocumented
392 0 ▶sub pp_rv2gv { deref(shift, $top, "*"); }
Subroutine pp_rv2gv: not called CC=1 SCAR=6.9
Pod undocumented
393  
394 0 ▶sub pp_gvsv {
Subroutine pp_gvsv: not called CC=3 SCAR=24.8
Pod undocumented
395 0 my $op = shift;
396 0 my $gv;
397 0 ▶ if ($Config{useithreads}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Sepia::Xref::Config{'useithreads'}) { } 0 0 0
398 0 $top = $pad[$op->padix];
399 0 ▶ $top = UNKNOWN unless $top;
Branch: 1 branch
BranchTrueFalseTotal
unless $top 0 0 0
400 0 $top->[1] = '$';
401 }
402 else {
403 0 $gv = $op->gv;
404 0 $top = [$gv->STASH->NAME, '$', $gv->SAFENAME];
405 }
406 0 process($top, use_type $op);
407 }
408  
409 0 ▶sub pp_gv {
Subroutine pp_gv: not called CC=3 SCAR=24.8
Pod undocumented
410 0 my $op = shift;
411 0 my $gv;
412 0 ▶ if ($Config{useithreads}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Sepia::Xref::Config{'useithreads'}) { } 0 0 0
413 0 $top = $pad[$op->padix];
414 0 ▶ $top = UNKNOWN unless $top;
Branch: 1 branch
BranchTrueFalseTotal
unless $top 0 0 0
415 0 $top->[1] = '*';
416 }
417 else {
418 0 $gv = $op->gv;
419 0 $top = [$gv->STASH->NAME, "*", $gv->SAFENAME];
420 }
421 0 process($top, use_type $op);
422 }
423  
424 0 ▶sub pp_method {
Subroutine pp_method: not called CC=2 SCAR=17.9
Pod undocumented
425 0 my $op = shift;
426 0 ▶ $top = [$lastclass || "(method)", "->".$top->[1], $top->[2]];
Condition0%0/2
$lastclass || '(method)'
l!l
00
MC/DC0%0/1
$lastclass || '(method)'
$lastclass
Truth table0%0/2
A
A= $lastclass
Aresult
11
00
427 0 dprint 'method', "pp_method($top->[1])";
428 0 undef $lastclass;
429 }
430  
431 0 ▶sub pp_method_named {
Subroutine pp_method_named: not called CC=5 SCAR=30.5
Pod undocumented
432 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
433 0 my $op = shift;
434 0 my $sv = $op->sv;
435 0 my $pviv = $padval[$op->targ];
436 0 ▶ if ($pviv && class($pviv) =~ /^PV/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pviv and Sepia::Xref::class($pviv) =~ /^PV/) { } 0 0 0
Condition0%0/3
$pviv and Sepia::Xref::class($pviv) =~ /^PV/
!ll&&!rl&&r
000
MC/DC0%0/2
$pviv and Sepia::Xref::class($pviv) =~ /^PV/
$pviv Sepia::Xref::class($pviv) =~ /^PV/
Truth table0%0/3
A and B
A= $pvivB= Sepia::Xref::class($pviv) =~ /^PV/
ABresult
0X0
100
111
437 0 my $name = $pviv->PV;
438 0 dprint 'method_named', $op->targ.": $name";
439 0 ▶ undef $top->[2] if $top->[2] eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $top->[2] eq "?" 0 0 0
440 0 ▶ $top = [$lastclass || "(method)", '->', $name];
Condition0%0/2
$lastclass || '(method)'
l!l
00
MC/DC0%0/1
$lastclass || '(method)'
$lastclass
Truth table0%0/2
A
A= $lastclass
Aresult
11
00
441 0 undef $lastclass;
442 } else {
443 0 dprint 'method_named', "method_named: wtf: sizeof padval = ".@padval;
444 }
445 }
446  
447 0 ▶sub pp_entersub {
Subroutine pp_entersub: not called CC=2 SCAR=17.9
Pod undocumented
448 0 my $op = shift;
449 0 ▶ if ($top->[1] =~ /^(?:m$|->)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top->[1] =~ /^(?:m$|->)/) { } 0 0 0
450 0 dprint 'method', "call to (@$top) from $subname";
451 0 process($top, "meth");
452 } else {
453 0 process($top, "subused");
454 }
455 0 undef $lastclass;
456 0 $top = UNKNOWN;
457 }
458  
459 #
460 # Stuff for cross referencing definitions of variables and subs
461 #
462  
463 0 ▶sub B::GV::xref {
Subroutine xref: not called CC=4 SCAR=30.0
464 0 my $gv = shift;
465 0 my $cv = $gv->CV;
466 0 $file = $gv->FILE;
467 # XXX: sometimes the "file" is a module. Why?
468 0 $line = $gv->LINE;
469 0 ▶ if ($$cv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$cv) 0 0 0
470 #return if $done{$$cv}++;
471 0 process([$gv->STASH->NAME, "&", $gv->NAME], "subdef");
472 0 push(@todo, $cv);
473 }
474 0 my $form = $gv->FORM;
475 0 ▶ if ($$form) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$form) 0 0 0
476 0 ▶ return if $done{$$form}++;
Branch: 1 branch
BranchTrueFalseTotal
if $Sepia::Xref::done{$$form}++ 0 0 0
477 0 process([$gv->STASH->NAME, "", $gv->NAME], "formdef");
478 }
479 }
480  
481 ## Exclude all pragmatic modules (lowercase first letter) and the
482 ## following problematic things, which tend to cause more harm than
483 ## good when they get xref'd:
484 1 my %exclude;
485 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
486 undef $exclude{"$_\::"}
487 1 for qw(B O AutoLoader DynaLoader XSLoader Config DB VMS
488 FileHandle Exporter Carp PerlIO::Layer);
489 }
490  
491 0 ▶sub xref_exclude {
Subroutine xref_exclude: not called CC=2 SCAR=17.9
Pod undocumented
492 0 my $x = shift;
493 0 ▶ $x =~ /^[a-z]/ || exists $exclude{$x};
Branch: 1 branch
BranchTrueFalseTotal
$x =~ /^[a-z]/ or exists $exclude{$x} 0 0 0
494 }
495  
496 0 ▶sub xref_definitions {
Subroutine xref_definitions: not called CC=1 SCAR=2.2
Pod undocumented
497 0 my ($pack, %exclude);
498 0 $subname = "(definitions)";
499 1 ▶ no strict qw(vars refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
500 0 ▶ walksymtable(\%{"main::"}, "xref", sub { !xref_exclude($_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
501 }
502  
503 =item C<rebuild()>
504  
505 Rebuild the Xref database.
506  
507 =cut
508  
509 0 ▶sub rebuild {
Subroutine rebuild: not called CC=1 SCAR=6.9
Pod documented
510 0 %call = (); %callby = ();
511 0 %var_def = (); %var_use = ();
512 0 local (@todo, %done);
513 0 xref_definitions;
514 0 xref_main;
515 0 1;
516 }
517  
518 0 ▶sub unmention {
Subroutine unmention: not called CC=9 SCAR=45.0
Pod undocumented
519 0 my ($h, $K, $V, $pack) = @_;
520 0 dprint 'unmention', "Unmentioning $K => $V";
521 0 ▶ while (my ($k, $v) = each %$h) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$h) 0 0 0
522 0 ▶ while (my ($k2, $v2) = each %$v) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$v) 0 0 0
523 0 ▶ if (ref $v2 eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v2 eq 'ARRAY') { } 0 0 0
524 $v->{$k2} = [grep {
525 0 $_->{$K} ne $V || !$pack || $pack ne $_->{package}
526 0 ▶ } @$v2];
Condition0%0/6
$_->{$K} ne $V || !$pack || $pack ne $_->{'package'}
l!l&&r!l&&!r
000
$_->{$K} ne $V or not $pack
l!l&&r!l&&!r
000
MC/DC0%0/3
$_->{$K} ne $V || !$pack || $pack ne $_->{'package'}
$_->{$K} ne $V not $pack $pack ne $_->{'package'}
Truth table0%0/4
A or not(B) || C
A= $_->{$K} ne $VB= not $packC= $pack ne $_->{'package'}
ABCresult
1XX1
01X1
0011
0000
527 0 ▶ delete $v->{$k2} unless @{$v->{$k2}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$v->{$k2};} 0 0 0
528 } else {
529 0 ▶ delete $v->{$k2} if $k2 eq $V;
Branch: 1 branch
BranchTrueFalseTotal
if $k2 eq $V 0 0 0
530 }
531 }
532 0 ▶ delete $h->{$k} unless keys %{$h->{$k}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$h{$k};} 0 0 0
533 }
534 }
535  
536 0 ▶sub unmention_sub {
Subroutine unmention_sub: not called CC=3 SCAR=24.8
Pod undocumented
537 0 my ($h, $sub, $pack) = @_;
538 0 dprint 'unmention', "Unmentioning $pack\::$sub";
539 0 ▶ if ($pack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pack) { } 0 0 0
540 0 delete $h->{$sub}{$pack};
541 0 ▶ delete $h->{$sub} unless keys %{$h->{$sub}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$h{$sub};} 0 0 0
542 } else {
543 0 delete $h->{$sub};
544 }
545 }
546  
547 =item C<forget($func [, $mod])>
548  
549 Forget that C<$func> was defined.
550  
551 =cut
552  
553 0 ▶sub forget {
Subroutine forget: not called CC=1 SCAR=6.9
Pod documented
554 0 my ($obj, $pack) = @_;
555 0 unmention_sub \%callby, @_;
556 0 unmention \%call, 'sub', @_;
557 0 unmention \%var_use, 'sub', @_;
558 0 unmention \%var_def, 'sub', @_;
559 }
560  
561 =item C<redefined($func [, $pack])>
562  
563 Recompute xref info for C<$func>, or C<$pack::$func> if C<$pack> given.
564  
565 =cut
566  
567 0 ▶sub redefined {
Subroutine redefined: not called CC=3 SCAR=20.0
Pod documented
568 0 forget @_;
569 {
570 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
571 0 my ($sub, $pack) = @_;
572 0 ▶ $pack ||= 'main';
Condition0%0/2
$pack ||= "main"
l!l
00
MC/DC0%0/1
$pack ||= "main"
$pack
Truth table0%0/2
A
A= $pack
Aresult
11
00
573 0 ▶ $sub = $pack eq 'main' ? $sub : "$pack\::$sub";
Branch: 1 branch
BranchTrueFalseTotal
$pack eq 'main' ? : 0 0 0
574 0 local $subname = '(definitions)';
575 0 xref_object \&$sub;
576 }
577 }
578  
579 ######################################################################
580 # Apropos and definition-finding:
581  
582 sub _ret_list
583 4 ▶{
Subroutine _ret_list: called CC=5 SCAR=16.3
584 4 my ($h, $sub, $mod) = @_;
585 4 ▶ if ($sub =~ /^(.*)::([^:]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub =~ /^(.*)::([^:]+)$/) 2 2 4
586 2 $sub = $2;
587 2 $mod = $1;
588 }
589 4 $h = $h->{$sub};
590 4 my @r;
591 4 ▶ if ($mod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod) { } 2 2 4
592 0 @r = keys %{$h->{$mod}};
593 } else {
594 # @r = map { @$_ } values %$h;
595 2 my %h;
596 2 @h{keys %$_} = 1 for values %$h;
597 2 @r = keys %h;
598 }
599 4 @r = sort @r;
600 4 ▶ return wantarray ? @r : \@r;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
601 }
602  
603 sub _var_ret_list
604 0 ▶{
Subroutine _var_ret_list: not called CC=7 SCAR=40.3
605 0 my ($h, $v, $mod, $assign) = @_;
606 0 ▶ if ($v =~ /^(.*)::([^:]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^(.*)::([^:]+)$/) 0 0 0
607 0 $mod = $1;
608 0 $v = $2;
609 }
610 0 $h = $h->{$v};
611 0 my @r;
612 0 ▶ if ($mod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod) { } 0 0 0
613 0 ▶ @r = exists $h->{$mod} ? @{$h->{$mod}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $h->{$mod} ? : 0 0 0
614 } else {
615 ## XXX: Need to revisit when this is/isn't an array!
616 0 ▶ @r = map { ref $_ eq 'ARRAY' ? @$_ : $_ } values %$h;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
617 }
618 0 ▶ @r = grep $_->{assign}, @r if $assign;
Branch: 1 branch
BranchTrueFalseTotal
if $assign 0 0 0
619 0 @r = map { [@{$_}{qw(file line sub package)}] } @r;
620 0 ▶ return wantarray ? @r : \@r;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
621 }
622  
623 =item C<callers($func)>
624  
625 List callers of C<$func>.
626  
627 =cut
628  
629 2 ▶sub callers {
Subroutine callers: called CC=1 SCAR=0.0
Pod documented
630 2 _ret_list \%call, @_;
631 }
632  
633 =item C<callees($func)>
634  
635 List callees of C<$func>.
636  
637 =cut
638  
639 2 ▶sub callees {
Subroutine callees: called CC=1 SCAR=0.0
Pod documented
640 2 _ret_list \%callby, @_;
641 }
642  
643 =item C<var_defs($var)>
644  
645 Find locations where C<$var> is defined.
646  
647 =cut
648  
649 0 ▶sub var_defs {
Subroutine var_defs: not called CC=1 SCAR=6.9
Pod documented
650 0 return _var_ret_list \%var_def, @_;
651 }
652  
653 =item C<var_uses($var)>
654  
655 Find locations where C<$var> is used.
656  
657 =cut
658  
659 0 ▶sub var_uses {
Subroutine var_uses: not called CC=1 SCAR=6.9
Pod documented
660 0 return _var_ret_list \%var_use, @_;
661 }
662  
663 =item C<var_assigns($var)>
664  
665 Find locations where C<$var> is assigned to.
666  
667 =cut
668  
669 0 ▶sub var_assigns {
Subroutine var_assigns: not called CC=1 SCAR=6.9
Pod documented
670 0 my ($v, $pack) = @_;
671 0 return _var_ret_list \%var_use, $v, $pack, 1;
672 }
673  
674 =item C<file_modules($file)>
675  
676 List the modules defined in file C<$file>.
677  
678 =cut
679  
680 0 ▶sub file_modules {
Subroutine file_modules: not called CC=2 SCAR=17.9
Pod documented
681 0 my $file = shift;
682 0 eval {
683 0 require Module::Info;
684 0 my $mod = Module::Info->new_from_file(abs_path($file));
685 0 ▶ if ( $mod ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod) 0 0 0
686 0 return $mod->packages_inside();
687 }
688 }
689 }
690  
691 =item C<var_apropos($expr)>
692  
693 Find variables matching C<$expr>.
694  
695 =cut
696  
697 0 ▶sub _apropos {
Subroutine _apropos: not called CC=8 SCAR=42.8
698 0 my ($h, $re, $mod) = @_;
699 0 my @r = do {
700 0 ▶ if($re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($re) { } 0 0 0
701 0 $re = _apropos_re($re);
702 0 sort grep /$re/, keys %$h;
703 } else {
704 0 sort keys %$h;
705 }
706 };
707 0 ▶ if ($mod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod) 0 0 0
708 0 $mod = _apropos_re($mod);
709 0 my %r;
710 0 for (@r) {
711 0 my $sn = $_;
712 0 for (keys %{$h->{$_}}) {
713 0 ▶ $r{$_ eq 'main' ? $sn : "$_\::$sn"} = 1 if /$mod/;
Branch: 2 branches
BranchTrueFalseTotal
if /$mod/ 0 0 0
$_ eq 'main' ? : 0 0 0
714 }
715 }
716 0 @r = sort keys %r;
717 }
718 0 ▶ return wantarray ? @r : \@r;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
719 }
720  
721 0 ▶sub var_apropos {
Subroutine var_apropos: not called CC=1 SCAR=6.9
Pod documented
722 0 _apropos \%var_use, @_;
723 }
724  
725 1 1;
726  
727 __END__
« Sepia.pm ↑ Summary Sepia/Debug.pm »