| 1 | package B::Lint; | ||
| 2 | use if $] > 5.017, 'deprecate'; | ||
| 3 | |||
| 4 | our $VERSION = '1.20'; ## no critic | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | B::Lint - Perl lint | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | perl -MO=Lint[,OPTIONS] foo.pl | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | The B::Lint module is equivalent to an extended version of the B<-w> | ||
| 17 | option of B<perl>. It is named after the program F<lint> which carries | ||
| 18 | out a similar process for C programs. | ||
| 19 | |||
| 20 | =head1 OPTIONS AND LINT CHECKS | ||
| 21 | |||
| 22 | Option words are separated by commas (not whitespace) and follow the | ||
| 23 | usual conventions of compiler backend options. Following any options | ||
| 24 | (indicated by a leading B<->) come lint check arguments. Each such | ||
| 25 | argument (apart from the special B<all> and B<none> options) is a | ||
| 26 | word representing one possible lint check (turning on that check) or | ||
| 27 | is B<no-foo> (turning off that check). Before processing the check | ||
| 28 | arguments, a standard list of checks is turned on. Later options | ||
| 29 | override earlier ones. Available options are: | ||
| 30 | |||
| 31 | =over 8 | ||
| 32 | |||
| 33 | =item B<magic-diamond> | ||
| 34 | |||
| 35 | Produces a warning whenever the magic C<E<lt>E<gt>> readline is | ||
| 36 | used. Internally it uses perl's two-argument open which itself treats | ||
| 37 | filenames with special characters specially. This could allow | ||
| 38 | interestingly named files to have unexpected effects when reading. | ||
| 39 | |||
| 40 | % touch 'rm *|' | ||
| 41 | % perl -pe 1 | ||
| 42 | |||
| 43 | The above creates a file named C<rm *|>. When perl opens it with | ||
| 44 | C<E<lt>E<gt>> it actually executes the shell program C<rm *>. This | ||
| 45 | makes C<E<lt>E<gt>> dangerous to use carelessly. | ||
| 46 | |||
| 47 | =item B<context> | ||
| 48 | |||
| 49 | Produces a warning whenever an array is used in an implicit scalar | ||
| 50 | context. For example, both of the lines | ||
| 51 | |||
| 52 | $foo = length(@bar); | ||
| 53 | $foo = @bar; | ||
| 54 | |||
| 55 | will elicit a warning. Using an explicit B<scalar()> silences the | ||
| 56 | warning. For example, | ||
| 57 | |||
| 58 | $foo = scalar(@bar); | ||
| 59 | |||
| 60 | =item B<implicit-read> and B<implicit-write> | ||
| 61 | |||
| 62 | These options produce a warning whenever an operation implicitly | ||
| 63 | reads or (respectively) writes to one of Perl's special variables. | ||
| 64 | For example, B<implicit-read> will warn about these: | ||
| 65 | |||
| 66 | /foo/; | ||
| 67 | |||
| 68 | and B<implicit-write> will warn about these: | ||
| 69 | |||
| 70 | s/foo/bar/; | ||
| 71 | |||
| 72 | Both B<implicit-read> and B<implicit-write> warn about this: | ||
| 73 | |||
| 74 | for (@a) { ... } | ||
| 75 | |||
| 76 | =item B<bare-subs> | ||
| 77 | |||
| 78 | This option warns whenever a bareword is implicitly quoted, but is also | ||
| 79 | the name of a subroutine in the current package. Typical mistakes that it will | ||
| 80 | trap are: | ||
| 81 | |||
| 82 | use constant foo => 'bar'; | ||
| 83 | @a = ( foo => 1 ); | ||
| 84 | $b{foo} = 2; | ||
| 85 | |||
| 86 | Neither of these will do what a naive user would expect. | ||
| 87 | |||
| 88 | =item B<dollar-underscore> | ||
| 89 | |||
| 90 | This option warns whenever C<$_> is used either explicitly anywhere or | ||
| 91 | as the implicit argument of a B<print> statement. | ||
| 92 | |||
| 93 | =item B<private-names> | ||
| 94 | |||
| 95 | This option warns on each use of any variable, subroutine or | ||
| 96 | method name that lives in a non-current package but begins with | ||
| 97 | an underscore ("_"). Warnings aren't issued for the special case | ||
| 98 | of the single character name "_" by itself (e.g. C<$_> and C<@_>). | ||
| 99 | |||
| 100 | =item B<undefined-subs> | ||
| 101 | |||
| 102 | This option warns whenever an undefined subroutine is invoked. | ||
| 103 | This option will only catch explicitly invoked subroutines such | ||
| 104 | as C<foo()> and not indirect invocations such as C<&$subref()> | ||
| 105 | or C<$obj-E<gt>meth()>. Note that some programs or modules delay | ||
| 106 | definition of subs until runtime by means of the AUTOLOAD | ||
| 107 | mechanism. | ||
| 108 | |||
| 109 | =item B<regexp-variables> | ||
| 110 | |||
| 111 | This option warns whenever one of the regexp variables C<$`>, C<$&> or C<$'> | ||
| 112 | is used. Any occurrence of any of these variables in your | ||
| 113 | program can slow your whole program down. See L<perlre> for | ||
| 114 | details. | ||
| 115 | |||
| 116 | =item B<all> | ||
| 117 | |||
| 118 | Turn all warnings on. | ||
| 119 | |||
| 120 | =item B<none> | ||
| 121 | |||
| 122 | Turn all warnings off. | ||
| 123 | |||
| 124 | =back | ||
| 125 | |||
| 126 | =head1 NON LINT-CHECK OPTIONS | ||
| 127 | |||
| 128 | =over 8 | ||
| 129 | |||
| 130 | =item B<-u Package> | ||
| 131 | |||
| 132 | Normally, Lint only checks the main code of the program together | ||
| 133 | with all subs defined in package main. The B<-u> option lets you | ||
| 134 | include other package names whose subs are then checked by Lint. | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | =head1 EXTENDING LINT | ||
| 139 | |||
| 140 | Lint can be extended by with plugins. Lint uses L<Module::Pluggable> | ||
| 141 | to find available plugins. Plugins are expected but not required to | ||
| 142 | inform Lint of which checks they are adding. | ||
| 143 | |||
| 144 | The C<< B::Lint->register_plugin( MyPlugin => \@new_checks ) >> method | ||
| 145 | adds the list of C<@new_checks> to the list of valid checks. If your | ||
| 146 | module wasn't loaded by L<Module::Pluggable> then your class name is | ||
| 147 | added to the list of plugins. | ||
| 148 | |||
| 149 | You must create a C<match( \%checks )> method in your plugin class or one | ||
| 150 | of its parents. It will be called on every op as a regular method call | ||
| 151 | with a hash ref of checks as its parameter. | ||
| 152 | |||
| 153 | The class methods C<< B::Lint->file >> and C<< B::Lint->line >> contain | ||
| 154 | the current filename and line number. | ||
| 155 | |||
| 156 | package Sample; | ||
| 157 | use B::Lint; | ||
| 158 | B::Lint->register_plugin( Sample => [ 'good_taste' ] ); | ||
| 159 | |||
| 160 | sub match { | ||
| 161 | my ( $op, $checks_href ) = shift @_; | ||
| 162 | if ( $checks_href->{good_taste} ) { | ||
| 163 | ... | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | =head1 TODO | ||
| 168 | |||
| 169 | =over | ||
| 170 | |||
| 171 | =item while(<FH>) stomps $_ | ||
| 172 | |||
| 173 | =item strict oo | ||
| 174 | |||
| 175 | =item unchecked system calls | ||
| 176 | |||
| 177 | =item more tests, validate against older perls | ||
| 178 | |||
| 179 | =back | ||
| 180 | |||
| 181 | =head1 BUGS | ||
| 182 | |||
| 183 | This is only a very preliminary version. | ||
| 184 | |||
| 185 | =head1 AUTHOR | ||
| 186 | |||
| 187 | Malcolm Beattie, mbeattie@sable.ox.ac.uk. | ||
| 188 | |||
| 189 | =head1 ACKNOWLEDGEMENTS | ||
| 190 | |||
| 191 | Sebastien Aperghis-Tramoni - bug fixes | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | use strict; | ||
| 196 | use B qw( walkoptree_slow | ||
| 197 | main_root main_cv walksymtable parents | ||
| 198 | OPpOUR_INTRO | ||
| 199 | OPf_WANT_VOID OPf_WANT_LIST OPf_WANT OPf_STACKED SVf_POK SVf_ROK ); | ||
| 200 | use Carp 'carp'; | ||
| 201 | |||
| 202 | # The current M::P doesn't know about .pmc files. | ||
| 203 | use Module::Pluggable ( require => 1 ); | ||
| 204 | |||
| 205 | use List::Util 'first'; | ||
| 206 | ## no critic Prototypes | ||
| 207 | sub any (&@) { my $test = shift @_; $test->() and return 1 for @_; return 0 } | ||
| 208 | |||
| 209 | BEGIN { | ||
| 210 | |||
| 211 | # Import or create some constants from B. B doesn't provide | ||
| 212 | # everything I need so some things like OPpCONST_BARE are defined | ||
| 213 | # here. | ||
| 214 | for my $sym ( qw( begin_av check_av init_av end_av ), | ||
| 215 | [ 'OPpCONST_BARE' => 64 ] ) | ||
| 216 | { | ||
| 217 | my $val; | ||
| 218 | ( $sym, $val ) = @$sym if ref $sym; | ||
| 219 | |||
| 220 | if ( any { $sym eq $_ } @B::EXPORT_OK, @B::EXPORT ) { | ||
| 221 | B->import($sym); | ||
| 222 | } | ||
| 223 | else { | ||
| 224 | require constant; | ||
| 225 | constant->import( $sym => $val ); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | my $file = "unknown"; # shadows current filename | ||
| 231 | my $line = 0; # shadows current line number | ||
| 232 | my $curstash = "main"; # shadows current stash | ||
| 233 | my $curcv; # shadows current B::CV for pad lookups | ||
| 234 | |||
| 235 | sub file {$file} | ||
| 236 | sub line {$line} | ||
| 237 | sub curstash {$curstash} | ||
| 238 | sub curcv {$curcv} | ||
| 239 | |||
| 240 | # Lint checks | ||
| 241 | my %check; | ||
| 242 | my %implies_ok_context; | ||
| 243 | |||
| 244 | map( $implies_ok_context{$_}++, | ||
| 245 | qw(scalar av2arylen aelem aslice helem hslice | ||
| 246 | keys values hslice defined undef delete) ); | ||
| 247 | |||
| 248 | # Lint checks turned on by default | ||
| 249 | my @default_checks | ||
| 250 | = qw(context magic_diamond undefined_subs regexp_variables); | ||
| 251 | |||
| 252 | my %valid_check; | ||
| 253 | |||
| 254 | # All valid checks | ||
| 255 | for my $check ( | ||
| 256 | qw(context implicit_read implicit_write dollar_underscore | ||
| 257 | private_names bare_subs undefined_subs regexp_variables | ||
| 258 | magic_diamond ) | ||
| 259 | ) | ||
| 260 | { | ||
| 261 | $valid_check{$check} = __PACKAGE__; | ||
| 262 | } | ||
| 263 | |||
| 264 | # Debugging options | ||
| 265 | my ($debug_op); | ||
| 266 | |||
| 267 | my %done_cv; # used to mark which subs have already been linted | ||
| 268 | my @extra_packages; # Lint checks mainline code and all subs which are | ||
| 269 | # in main:: or in one of these packages. | ||
| 270 | |||
| 271 | sub warning { | ||
| 272 | my $format = ( @_ < 2 ) ? "%s" : shift @_; | ||
| 273 | warn sprintf( "$format at %s line %d\n", @_, $file, $line ); | ||
| 274 | return undef; ## no critic undef | ||
| 275 | } | ||
| 276 | |||
| 277 | # This gimme can't cope with context that's only determined | ||
| 278 | # at runtime via dowantarray(). | ||
| 279 | sub gimme { | ||
| 280 | my $op = shift @_; | ||
| 281 | my $flags = $op->flags; | ||
| 282 | if ( $flags & OPf_WANT ) { | ||
| 283 | return ( ( $flags & OPf_WANT ) == OPf_WANT_LIST ? 1 : 0 ); | ||
| 284 | } | ||
| 285 | return undef; ## no critic undef | ||
| 286 | } | ||
| 287 | |||
| 288 | my @plugins = __PACKAGE__->plugins; | ||
| 289 | |||
| 290 | sub inside_grepmap { | ||
| 291 | |||
| 292 | # A boolean function to be used while inside a B::walkoptree_slow | ||
| 293 | # call. If we are in the EXPR part of C<grep EXPR, ...> or C<grep | ||
| 294 | # { EXPR } ...>, this returns true. | ||
| 295 | return any { $_->name =~ m/\A(?:grep|map)/xms } @{ parents() }; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub inside_foreach_modifier { | ||
| 299 | |||
| 300 | # TODO: use any() | ||
| 301 | |||
| 302 | # A boolean function to be used while inside a B::walkoptree_slow | ||
| 303 | # call. If we are in the EXPR part of C<EXPR foreach ...> this | ||
| 304 | # returns true. | ||
| 305 | for my $ancestor ( @{ parents() } ) { | ||
| 306 | next unless $ancestor->name eq 'leaveloop'; | ||
| 307 | |||
| 308 | my $first = $ancestor->first; | ||
| 309 | next unless $first->name eq 'enteriter'; | ||
| 310 | |||
| 311 | next if $first->redoop->name =~ m/\A(?:next|db|set)state\z/xms; | ||
| 312 | |||
| 313 | return 1; | ||
| 314 | } | ||
| 315 | return 0; | ||
| 316 | } | ||
| 317 | |||
| 318 | for ( | ||
| 319 | [qw[ B::PADOP::gv_harder gv padix]], | ||
| 320 | [qw[ B::SVOP::sv_harder sv targ]], | ||
| 321 | [qw[ B::METHOP::sv_harder meth_sv targ]], | ||
| 322 | [qw[ B::SVOP::gv_harder gv padix]] | ||
| 323 | ) | ||
| 324 | { | ||
| 325 | |||
| 326 | # I'm generating some functions here because they're mostly | ||
| 327 | # similar. It's all for compatibility with threaded | ||
| 328 | # perl. Perhaps... this code should inspect $Config{usethreads} | ||
| 329 | # and generate a *specific* function. I'm leaving it generic for | ||
| 330 | # the moment. | ||
| 331 | # | ||
| 332 | # In threaded perl SVs and GVs aren't used directly in the optrees | ||
| 333 | # like they are in non-threaded perls. The ops that would use a SV | ||
| 334 | # or GV keep an index into the subroutine's scratchpad. I'm | ||
| 335 | # currently ignoring $cv->DEPTH and that might be at my peril. | ||
| 336 | |||
| 337 | my ( $subname, $attr, $pad_attr ) = @$_; | ||
| 338 | my $target = do { ## no critic strict | ||
| 339 | no strict 'refs'; | ||
| 340 | \*$subname; | ||
| 341 | }; | ||
| 342 | *$target = sub { | ||
| 343 | my ($op) = @_; | ||
| 344 | |||
| 345 | my $elt; | ||
| 346 | if ( not $op->isa('B::PADOP') ) { | ||
| 347 | $elt = $op->$attr; | ||
| 348 | } | ||
| 349 | return $elt if eval { $elt->isa('B::SV') }; | ||
| 350 | |||
| 351 | my $ix = $op->$pad_attr; | ||
| 352 | my @entire_pad = $curcv->PADLIST->ARRAY; | ||
| 353 | my @elts = map +( $_->ARRAY )[$ix], @entire_pad; | ||
| 354 | ($elt) = first { | ||
| 355 | eval { $_->isa('B::SV') } ? $_ : (); | ||
| 356 | } | ||
| 357 | @elts[ 0, reverse 1 .. $#elts ]; | ||
| 358 | return $elt; | ||
| 359 | }; | ||
| 360 | } | ||
| 361 | |||
| 362 | sub B::OP::lint { | ||
| 363 | my ($op) = @_; | ||
| 364 | |||
| 365 | # This is a fallback ->lint for all the ops where I haven't | ||
| 366 | # defined something more specific. Nothing happens here. | ||
| 367 | |||
| 368 | # Call all registered plugins | ||
| 369 | my $m; | ||
| 370 | $m = $_->can('match'), $op->$m( \%check ) for @plugins; | ||
| 371 | return; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub B::COP::lint { | ||
| 375 | my ($op) = @_; | ||
| 376 | |||
| 377 | # nextstate ops sit between statements. Whenever I see one I | ||
| 378 | # update the current info on file, line, and stash. This code also | ||
| 379 | # updates it when it sees a dbstate or setstate op. I have no idea | ||
| 380 | # what those are but having seen them mentioned together in other | ||
| 381 | # parts of the perl I think they're kind of equivalent. | ||
| 382 | if ( $op->name =~ m/\A(?:next|db|set)state\z/ ) { | ||
| 383 | $file = $op->file; | ||
| 384 | $line = $op->line; | ||
| 385 | $curstash = $op->stash->NAME; | ||
| 386 | } | ||
| 387 | |||
| 388 | # Call all registered plugins | ||
| 389 | my $m; | ||
| 390 | $m = $_->can('match'), $op->$m( \%check ) for @plugins; | ||
| 391 | return; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub B::UNOP::lint { | ||
| 395 | my ($op) = @_; | ||
| 396 | |||
| 397 | my $opname = $op->name; | ||
| 398 | |||
| 399 | CONTEXT: { | ||
| 400 | |||
| 401 | # Check arrays and hashes in scalar or void context where | ||
| 402 | # scalar() hasn't been used. | ||
| 403 | |||
| 404 | next | ||
| 405 | unless $check{context} | ||
| 406 | and $opname =~ m/\Arv2[ah]v\z/xms | ||
| 407 | and not gimme($op); | ||
| 408 | |||
| 409 | my ( $parent, $gparent ) = @{ parents() }[ 0, 1 ]; | ||
| 410 | my $pname = $parent->name; | ||
| 411 | |||
| 412 | next if $implies_ok_context{$pname}; | ||
| 413 | |||
| 414 | # Three special cases to deal with: "foreach (@foo)", "delete | ||
| 415 | # $a{$b}", and "exists $a{$b}" null out the parent so we have to | ||
| 416 | # check for a parent of pp_null and a grandparent of | ||
| 417 | # pp_enteriter, pp_delete, pp_exists | ||
| 418 | |||
| 419 | next | ||
| 420 | if $pname eq "null" | ||
| 421 | and $gparent->name =~ m/\A(?:delete|enteriter|exists)\z/xms; | ||
| 422 | |||
| 423 | # our( @bar ); would also trigger this error so I exclude | ||
| 424 | # that. | ||
| 425 | next | ||
| 426 | if $op->private & OPpOUR_INTRO | ||
| 427 | and ( $op->flags & OPf_WANT ) == OPf_WANT_VOID; | ||
| 428 | |||
| 429 | warning 'Implicit scalar context for %s in %s', | ||
| 430 | $opname eq "rv2av" ? "array" : "hash", $parent->desc; | ||
| 431 | } | ||
| 432 | |||
| 433 | PRIVATE_NAMES: { | ||
| 434 | |||
| 435 | # Looks for calls to methods with names that begin with _ and | ||
| 436 | # that aren't visible within the current package. Maybe this | ||
| 437 | # should look at @ISA. | ||
| 438 | next | ||
| 439 | unless $check{private_names} | ||
| 440 | and $opname =~ m/\Amethod/xms; | ||
| 441 | |||
| 442 | my $methop = $op->first; | ||
| 443 | next unless $methop->name eq "const"; | ||
| 444 | |||
| 445 | my $method = $methop->sv_harder->PV; | ||
| 446 | next | ||
| 447 | unless $method =~ m/\A_/xms | ||
| 448 | and not defined &{"$curstash\::$method"}; | ||
| 449 | |||
| 450 | warning q[Illegal reference to private method name '%s'], $method; | ||
| 451 | } | ||
| 452 | |||
| 453 | # Call all registered plugins | ||
| 454 | my $m; | ||
| 455 | $m = $_->can('match'), $op->$m( \%check ) for @plugins; | ||
| 456 | return; | ||
| 457 | } | ||
| 458 | |||
| 459 | sub B::PMOP::lint { | ||
| 460 | my ($op) = @_; | ||
| 461 | |||
| 462 | IMPLICIT_READ: { | ||
| 463 | |||
| 464 | # Look for /.../ that doesn't use =~ to bind to something. | ||
| 465 | next | ||
| 466 | unless $check{implicit_read} | ||
| 467 | and $op->name eq "match" | ||
| 468 | and not( $op->flags & OPf_STACKED | ||
| 469 | or inside_grepmap() ); | ||
| 470 | warning 'Implicit match on $_'; | ||
| 471 | } | ||
| 472 | |||
| 473 | IMPLICIT_WRITE: { | ||
| 474 | |||
| 475 | # Look for s/.../.../ that doesn't use =~ to bind to | ||
| 476 | # something. | ||
| 477 | next | ||
| 478 | unless $check{implicit_write} | ||
| 479 | and $op->name eq "subst" | ||
| 480 | and not $op->flags & OPf_STACKED; | ||
| 481 | warning 'Implicit substitution on $_'; | ||
| 482 | } | ||
| 483 | |||
| 484 | # Call all registered plugins | ||
| 485 | my $m; | ||
| 486 | $m = $_->can('match'), $op->$m( \%check ) for @plugins; | ||
| 487 | return; | ||
| 488 | } | ||
| 489 | |||
| 490 | sub B::LOOP::lint { | ||
| 491 | my ($op) = @_; | ||
| 492 | |||
| 493 | IMPLICIT_FOO: { | ||
| 494 | |||
| 495 | # Look for C<for ( ... )>. | ||
| 496 | next | ||
| 497 | unless ( $check{implicit_read} or $check{implicit_write} ) | ||
| 498 | and $op->name eq "enteriter"; | ||
| 499 | |||
| 500 | my $last = $op->last; | ||
| 501 | next | ||
| 502 | unless $last->name eq "gv" | ||
| 503 | and $last->gv_harder->NAME eq "_" | ||
| 504 | and $op->redoop->name =~ m/\A(?:next|db|set)state\z/xms; | ||
| 505 | |||
| 506 | warning 'Implicit use of $_ in foreach'; | ||
| 507 | } | ||
| 508 | |||
| 509 | # Call all registered plugins | ||
| 510 | my $m; | ||
| 511 | $m = $_->can('match'), $op->$m( \%check ) for @plugins; | ||
| 512 | return; | ||
| 513 | } | ||
| 514 | |||
| 515 | # In threaded vs non-threaded perls you'll find that threaded perls | ||
| 516 | # use PADOP in place of SVOPs so they can do lookups into the | ||
| 517 | # scratchpad to find things. I suppose this is so a optree can be | ||
| 518 | # shared between threads and all symbol table muckery will just get | ||
| 519 | # written to a scratchpad. | ||
| 520 | *B::METHOP::lint = *B::PADOP::lint = *B::PADOP::lint = \&B::SVOP::lint; | ||
| 521 | |||
| 522 | sub B::SVOP::lint { | ||
| 523 | my ($op) = @_; | ||
| 524 | |||
| 525 | MAGIC_DIAMOND: { | ||
| 526 | next | ||
| 527 | unless $check{magic_diamond} | ||
| 528 | and parents()->[0]->name eq 'readline' | ||
| 529 | and $op->gv_harder->NAME eq 'ARGV'; | ||
| 530 | |||
| 531 | warning 'Use of <>'; | ||
| 532 | } | ||
| 533 | |||
| 534 | BARE_SUBS: { | ||
| 535 | next | ||
| 536 | unless $check{bare_subs} | ||
| 537 | and $op->name eq 'const' | ||
| 538 | and $op->private & OPpCONST_BARE; | ||
| 539 | |||
| 540 | my $sv = $op->sv_harder; | ||
| 541 | next unless $sv->FLAGS & SVf_POK; | ||
| 542 | |||
| 543 | my $sub = $sv->PV; | ||
| 544 | my $subname = "$curstash\::$sub"; | ||
| 545 | |||
| 546 | # I want to skip over things that were declared with the | ||
| 547 | # constant pragma. Well... sometimes. Hmm. I want to ignore | ||
| 548 | # C<<use constant FOO => ...>> but warn on C<<FOO => ...>> | ||
| 549 | # later. The former is typical declaration syntax and the | ||
| 550 | # latter would be an error. | ||
| 551 | # | ||
| 552 | # Skipping over both could be handled by looking if | ||
| 553 | # $constant::declared{$subname} is true. | ||
| 554 | |||
| 555 | # Check that it's a function. | ||
| 556 | next | ||
| 557 | unless exists &{"$curstash\::$sub"}; | ||
| 558 | |||
| 559 | warning q[Bare sub name '%s' interpreted as string], $sub; | ||
| 560 | } | ||
| 561 | |||
| 562 | PRIVATE_NAMES: { | ||
| 563 | next unless $check{private_names}; | ||
| 564 | |||
| 565 | my $opname = $op->name; | ||
| 566 | if ( $opname =~ m/\Agv(?:sv)?\z/xms ) { | ||
| 567 | |||
| 568 | # Looks for uses of variables and stuff that are named | ||
| 569 | # private and we're not in the same package. | ||
| 570 | my $gv = $op->gv_harder; | ||
| 571 | my $name = $gv->NAME; | ||
| 572 | next | ||
| 573 | unless $name =~ m/\A_./xms | ||
| 574 | and $gv->STASH->NAME ne $curstash; | ||
| 575 | |||
| 576 | warning q[Illegal reference to private name '%s'], $name; | ||
| 577 | } | ||
| 578 | elsif ( $opname eq "method_named" ) { | ||
| 579 | my $method = $op->sv_harder->PV; | ||
| 580 | next unless $method =~ m/\A_./xms; | ||
| 581 | |||
| 582 | warning q[Illegal reference to private method name '%s'], $method; | ||
| 583 | } | ||
| 584 | } | ||
| 585 | |||
| 586 | DOLLAR_UNDERSCORE: { | ||
| 587 | |||
| 588 | # Warn on uses of $_ with a few exceptions. I'm not warning on | ||
| 589 | # $_ inside grep, map, or statement modifier foreach because | ||
| 590 | # they localize $_ and it'd be impossible to use these | ||
| 591 | # features without getting warnings. | ||
| 592 | |||
| 593 | next | ||
| 594 | unless $check{dollar_underscore} | ||
| 595 | and $op->name eq "gvsv" | ||
| 596 | and $op->gv_harder->NAME eq "_" | ||
| 597 | and not( inside_grepmap | ||
| 598 | or inside_foreach_modifier ); | ||
| 599 | |||
| 600 | warning 'Use of $_'; | ||
| 601 | } | ||
| 602 | |||
| 603 | REGEXP_VARIABLES: { | ||
| 604 | |||
| 605 | # Look for any uses of $`, $&, or $'. | ||
| 606 | next | ||
| 607 | unless $check{regexp_variables} | ||
| 608 | and $op->name eq "gvsv"; | ||
| 609 | |||
| 610 | my $name = $op->gv_harder->NAME; | ||
| 611 | next unless $name =~ m/\A[\&\'\`]\z/xms; | ||
| 612 | |||
| 613 | warning 'Use of regexp variable $%s', $name; | ||
| 614 | } | ||
| 615 | |||
| 616 | UNDEFINED_SUBS: { | ||
| 617 | |||
| 618 | # Look for calls to functions that either don't exist or don't | ||
| 619 | # have a definition. | ||
| 620 | next | ||
| 621 | unless $check{undefined_subs} | ||
| 622 | and $op->name eq "gv" | ||
| 623 | and $op->next->name eq "entersub"; | ||
| 624 | |||
| 625 | my $gv = $op->gv_harder; | ||
| 626 | my $cv = $gv->FLAGS & SVf_ROK ? $gv->RV : undef; | ||
| 627 | my $subname = ($cv || $gv)->STASH->NAME . "::" | ||
| 628 | . ($cv ? $cv->NAME_HEK || $cv->GV->NAME : $gv->NAME); | ||
| 629 | |||
| 630 | no strict 'refs'; ## no critic strict | ||
| 631 | if ( not exists &$subname ) { | ||
| 632 | $subname =~ s/\Amain:://; | ||
| 633 | warning q[Nonexistent subroutine '%s' called], $subname; | ||
| 634 | } | ||
| 635 | elsif ( not defined &$subname ) { | ||
| 636 | $subname =~ s/\A\&?main:://; | ||
| 637 | warning q[Undefined subroutine '%s' called], $subname; | ||
| 638 | } | ||
| 639 | } | ||
| 640 | |||
| 641 | # Call all registered plugins | ||
| 642 | my $m; | ||
| 643 | $m = $_->can('match'), $op->$m( \%check ) for @plugins; | ||
| 644 | return; | ||
| 645 | } | ||
| 646 | |||
| 647 | sub B::GV::lintcv { | ||
| 648 | |||
| 649 | # Example: B::svref_2object( \ *A::Glob )->lintcv | ||
| 650 | |||
| 651 | my $gv = shift @_; | ||
| 652 | my $cv = $gv->CV; | ||
| 653 | return unless $cv->can('lintcv'); | ||
| 654 | $cv->lintcv; | ||
| 655 | return; | ||
| 656 | } | ||
| 657 | |||
| 658 | sub B::CV::lintcv { | ||
| 659 | |||
| 660 | # Example: B::svref_2object( \ &foo )->lintcv | ||
| 661 | |||
| 662 | # Write to the *global* $ | ||
| 663 | $curcv = shift @_; | ||
| 664 | |||
| 665 | #warn sprintf("lintcv: %s::%s (done=%d)\n", | ||
| 666 | # $gv->STASH->NAME, $gv->NAME, $done_cv{$$curcv});#debug | ||
| 667 | return unless ref($curcv) and $$curcv and not $done_cv{$$curcv}++; | ||
| 668 | my $root = $curcv->ROOT; | ||
| 669 | |||
| 670 | #warn " root = $root (0x$$root)\n";#debug | ||
| 671 | walkoptree_slow( $root, "lint" ) if $$root; | ||
| 672 | return; | ||
| 673 | } | ||
| 674 | |||
| 675 | sub do_lint { | ||
| 676 | my %search_pack; | ||
| 677 | |||
| 678 | # Copy to the global $curcv for use in pad lookups. | ||
| 679 | $curcv = main_cv; | ||
| 680 | walkoptree_slow( main_root, "lint" ) if ${ main_root() }; | ||
| 681 | |||
| 682 | # Do all the miscellaneous non-sub blocks. | ||
| 683 | for my $av ( begin_av, init_av, check_av, end_av ) { | ||
| 684 | next unless eval { $av->isa('B::AV') }; | ||
| 685 | for my $cv ( $av->ARRAY ) { | ||
| 686 | next unless ref($cv) and $cv->FILE eq $0; | ||
| 687 | $cv->lintcv; | ||
| 688 | } | ||
| 689 | } | ||
| 690 | |||
| 691 | walksymtable( | ||
| 692 | \%main::, | ||
| 693 | sub { | ||
| 694 | if ( $_[0]->FILE eq $0 ) { $_[0]->lintcv } | ||
| 695 | }, | ||
| 696 | sub {1} | ||
| 697 | ); | ||
| 698 | return; | ||
| 699 | } | ||
| 700 | |||
| 701 | sub compile { | ||
| 702 | my @options = @_; | ||
| 703 | |||
| 704 | # Turn on default lint checks | ||
| 705 | for my $opt (@default_checks) { | ||
| 706 | $check{$opt} = 1; | ||
| 707 | } | ||
| 708 | |||
| 709 | OPTION: | ||
| 710 | while ( my $option = shift @options ) { | ||
| 711 | my ( $opt, $arg ); | ||
| 712 | unless ( ( $opt, $arg ) = $option =~ m/\A-(.)(.*)/xms ) { | ||
| 713 | unshift @options, $option; | ||
| 714 | last OPTION; | ||
| 715 | } | ||
| 716 | |||
| 717 | if ( $opt eq "-" && $arg eq "-" ) { | ||
| 718 | shift @options; | ||
| 719 | last OPTION; | ||
| 720 | } | ||
| 721 | elsif ( $opt eq "D" ) { | ||
| 722 | $arg ||= shift @options; | ||
| 723 | foreach my $arg ( split //, $arg ) { | ||
| 724 | if ( $arg eq "o" ) { | ||
| 725 | B->debug(1); | ||
| 726 | } | ||
| 727 | elsif ( $arg eq "O" ) { | ||
| 728 | $debug_op = 1; | ||
| 729 | } | ||
| 730 | } | ||
| 731 | } | ||
| 732 | elsif ( $opt eq "u" ) { | ||
| 733 | $arg ||= shift @options; | ||
| 734 | push @extra_packages, $arg; | ||
| 735 | } | ||
| 736 | } | ||
| 737 | |||
| 738 | foreach my $opt ( @default_checks, @options ) { | ||
| 739 | $opt =~ tr/-/_/; | ||
| 740 | if ( $opt eq "all" ) { | ||
| 741 | %check = %valid_check; | ||
| 742 | } | ||
| 743 | elsif ( $opt eq "none" ) { | ||
| 744 | %check = (); | ||
| 745 | } | ||
| 746 | else { | ||
| 747 | if ( $opt =~ s/\Ano_//xms ) { | ||
| 748 | $check{$opt} = 0; | ||
| 749 | } | ||
| 750 | else { | ||
| 751 | $check{$opt} = 1; | ||
| 752 | } | ||
| 753 | carp "No such check: $opt" | ||
| 754 | unless defined $valid_check{$opt}; | ||
| 755 | } | ||
| 756 | } | ||
| 757 | |||
| 758 | # Remaining arguments are things to check. So why aren't I | ||
| 759 | # capturing them or something? I don't know. | ||
| 760 | |||
| 761 | return \&do_lint; | ||
| 762 | } | ||
| 763 | |||
| 764 | sub register_plugin { | ||
| 765 | my ( undef, $plugin, $new_checks ) = @_; | ||
| 766 | |||
| 767 | # Allow the user to be lazy and not give us a name. | ||
| 768 | $plugin = caller unless defined $plugin; | ||
| 769 | |||
| 770 | # Register the plugin's named checks, if any. | ||
| 771 | for my $check ( eval {@$new_checks} ) { | ||
| 772 | if ( not defined $check ) { | ||
| 773 | carp 'Undefined value in checks.'; | ||
| 774 | next; | ||
| 775 | } | ||
| 776 | if ( exists $valid_check{$check} ) { | ||
| 777 | carp | ||
| 778 | "$check is already registered as a $valid_check{$check} feature."; | ||
| 779 | next; | ||
| 780 | } | ||
| 781 | |||
| 782 | $valid_check{$check} = $plugin; | ||
| 783 | } | ||
| 784 | |||
| 785 | # Register a non-Module::Pluggable loaded module. @plugins already | ||
| 786 | # contains whatever M::P found on disk. The user might load a | ||
| 787 | # plugin manually from some arbitrary namespace and ask for it to | ||
| 788 | # be registered. | ||
| 789 | if ( not any { $_ eq $plugin } @plugins ) { | ||
| 790 | push @plugins, $plugin; | ||
| 791 | } | ||
| 792 | |||
| 793 | return; | ||
| 794 | } | ||
| 795 | |||
| 796 | 1; |