blib/lib/B/LintSubs.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 142 CC 13 SCAR 22.6
CC
13
Cov
0%
lint_gv
20.0
B::GV::lintcv
20.0
do_lint
12.0
CRAP 9.6
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2006,2007,2009 -- leonerd@leonerd.org.uk
5  
6 package B::LintSubs;
7  
8 use strict;
9 use warnings;
10 use B qw(walkoptree_slow main_root main_cv walksymtable);
11  
12 our $VERSION = '0.06';
13  
14 my $file = "unknown"; # shadows current filename
15 my $line = 0; # shadows current line number
16 my $curstash = "main"; # shadows current stash
17 my $curcv; # shadows current CV for current stash
18  
19 my %done_cv; # used to mark which subs have already been linted
20  
21 my $exitcode = 0;
22  
23 =head1 NAME
24  
25 B::LintSubs - Perl compiler backend to check sub linkage
26  
27 =head1 SYNOPSIS
28  
29 B<perl> B<-MO=LintSubs> [I<FILE>] [B<-e PROGRAM>]
30  
31 =head1 DESCRIPTION
32  
33 When using C<use strict>, subroutine names are not checked at the callsite;
34 this makes the following a perfectly valid program at compiletime, that only
35 blows up at runtime
36  
37 use strict;
38 foobar();
39  
40 When using the C<B::LintSubs> checker instead, this is detected:
41  
42 $ perl -MO=LintSubs -e 'use strict;
43 foobar();'
44 Undefined subroutine foobar called at -e line 2
45  
46 Imported functions from other modules are of course detected:
47  
48 $ perl -MO=LintSubs -e 'use strict;
49 use List::Util qw( max );
50 $_ = max( 1, 2, 3 )'
51 -e syntax OK
52  
53 In order to handle situations where external code is conditionally referenced
54 at runtime, any fully-qualified references to other functions are printed with
55 a warning, but not considered fatal. The programmer is assumed to Know What He
56 Is Doing in this case:
57  
58 $ perl -MO=LintSubs -e 'if( 1 ) { require Data::Dumper;
59 Data::Dumper::Dump( "Hello" ) }'
60 Unable to check call to Data::Dumper::Dump in foreign package at -e line 1
61 -e syntax OK
62  
63 =cut
64  
65 sub warning {
66 my $format = (@_ < 2) ? "%s" : shift;
67 warn sprintf("$format at %s line %d\n", @_, $file, $line);
68 }
69  
70 sub lint_gv
71 {
72 my $gv = shift;
73  
74 my $package = $gv->STASH->NAME;
75 my $subname = $package . "::" . $gv->NAME;
76
77 no strict 'refs';
78  
79 return if defined( &$subname );
80
81 # AUTOLOADed functions will have failed here, but can() will get them
82 my $coderef = UNIVERSAL::can( $package, $gv->NAME );
83 return if defined( $coderef );
84  
85 # If we're still failing here, it maybe means a fully-qualified function
86 # is being called at runtime in another package, that is 'require'd rather
87 # than 'use'd, so we haven't loaded it yet. We can't check this.
88  
89 if( $curstash ne $package ) {
90 # Throw a warning and hope the programmer knows what they are doing
91 warning('Unable to check call to %s in foreign package', $subname);
92 return;
93 }
94  
95 $subname =~ s/^main:://;
96 warning('Undefined subroutine %s called', $subname);
97 $exitcode = 1;
98 }
99  
100 sub B::OP::lint { }
101  
102 sub B::COP::lint {
103 my $op = shift;
104 if ($op->name eq "nextstate") {
105 $file = $op->file;
106 $line = $op->line;
107 $curstash = $op->stash->NAME;
108 }
109 }
110  
111 sub B::SVOP::lint {
112 my $op = shift;
113 if ($op->name eq "gv"
114 && $op->next->name eq "entersub")
115 {
116 lint_gv( $op->gv );
117 }
118 }
119  
120 sub B::PADOP::lint {
121 my $op = shift;
122 if ($op->name eq "gv"
123 && $op->next->name eq "entersub")
124 {
125 my $idx = $op->padix;
126 my $gv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$idx];
127 lint_gv( $gv );
128 }
129 }
130  
131 sub B::GV::lintcv {
132 my $gv = shift;
133 my $cv = $gv->CV;
134 return if !$$cv || $done_cv{$$cv}++;
135 if( $cv->FILE eq $0 ) {
136 my $root = $cv->ROOT;
137 $curcv = $cv;
138 walkoptree_slow($root, "lint") if $$root;
139 }
140 }
141  
142 sub do_lint {
143 my %search_pack;
144  
145 $curcv = main_cv;
146 walkoptree_slow(main_root, "lint") if ${main_root()};
147  
148 no strict qw( refs );
149 walksymtable(\%{"main::"}, "lintcv", sub { 1 } );
150  
151 exit( $exitcode ) if $exitcode;
152 }
153  
154 sub compile {
155 my @options = @_;
156  
157 return \&do_lint;
158 }
159  
160 # B.pm has a bug, in walkoptree_slow() tries to recurse into things that
161 # aren't actually B::OP trees. We'll have to work around it
162 my $walkoptree_old = \&B::walkoptree_slow;
163  
164 {
165 no warnings 'redefine';
166 *B::walkoptree_slow = sub {
167 my ( $op, $method, $level ) = @_;
168 return unless $op->isa( "B::OP" );
169 return $walkoptree_old->( $op, $method, $level );
170 };
171 }
172  
173 # Keep perl happy; keep Britain tidy
174 1;
175  
176 __END__