| 1 | package App::FindCallers; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Find the callers of a given function in a directory tree | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use File::Find; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use PPI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub report { | ||||||||||||||||||||||||||
|
Subroutine
report: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 0 | my ($filename, $f, $nestlevel) = @_; | |||||||||||||||||||||||||||
| 15 | 0 | my $indent = " " x $nestlevel; | |||||||||||||||||||||||||||
| 16 | 0 | my $message = "Called from"; | |||||||||||||||||||||||||||
| 17 | 0 | ▶ | if ($nestlevel) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 0 | $message = "Defined in" | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | 0 | my $location = $filename . ":" . $f->line_number; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | printf "%s%s %s() in %s\n", $indent, $message, $f->name, $location; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | sub find_in_file { | ||||||||||||||||||||||||||
|
Subroutine
find_in_file: called CC=8 SCAR=21.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 1 | my ($function, $filename, $cb) = @_; | |||||||||||||||||||||||||||
| 27 | 1 | ▶ | $cb ||= \&report; | ||||||||||||||||||||||||||
|
Condition50%1/2
$cb ||= \&App::FindCallers::report
MC/DC0%0/1
$cb ||= \&App::FindCallers::report
$cb
Truth table50%1/2
A
A= $cb
| |||||||||||||||||||||||||||||
| 28 | 1 | my $document = PPI::Document->new($filename); | |||||||||||||||||||||||||||
| 29 | 1 | ▶ | unless ($document) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | say "Failed to parse $filename " . PPI::Document->errstr; | |||||||||||||||||||||||||||
| 31 | 0 | return; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 1 | $document->index_locations; | |||||||||||||||||||||||||||
| 34 | 68 | ▶ | my $references = $document->find(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 35 | 68 | ▶ | $_[1]->isa('PPI::Token::Word') and $_[1]->content eq $function | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 1 | }); | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | return unless $references; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | for my $f (@$references) { | |||||||||||||||||||||||||||
| 39 | 3 | my $nestlevel = 0; | |||||||||||||||||||||||||||
| 40 | 3 | ▶ | while ($f = $f->parent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | # XXX this makes skip the declaration of said sub, | ||||||||||||||||||||||||||||
| 42 | # but also makes it not detect recursive calls | ||||||||||||||||||||||||||||
| 43 | 14 | ▶ | if ($f->isa('PPI::Statement::Sub') and $f->name ne $function) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$f->isa("PPI::Statement::Sub") and $f->name ne $function
MC/DC100%2/2
$f->isa("PPI::Statement::Sub") and $f->name ne $function
$f->isa("PPI::Statement::Sub")
$f->name ne $function
Truth table100%3/3
A and B
A= $f->isa("PPI::Statement::Sub")B= $f->name ne $function
| |||||||||||||||||||||||||||||
| 44 | 4 | $cb->($filename, $f, $nestlevel); | |||||||||||||||||||||||||||
| 45 | 4 | $nestlevel++; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub main { | ||||||||||||||||||||||||||
|
Subroutine
main: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 0 | my ($funcname, $directory, $cb) = @_; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | $directory ||= '.'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$directory ||= "."
MC/DC0%0/1
$directory ||= "."
$directory
Truth table0%0/2
A
A= $directory
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | find({ wanted => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (/\.p[lm]$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | find_in_file $funcname, $_; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 0 | }, follow => 1, no_chdir => 1 }, $directory); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | 1; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | __END__ | ||||||||||||||||||||||||||||