Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

367 lines
9.1 KiB

  1. package B::Lint;
  2. =head1 NAME
  3. B::Lint - Perl lint
  4. =head1 SYNOPSIS
  5. perl -MO=Lint[,OPTIONS] foo.pl
  6. =head1 DESCRIPTION
  7. The B::Lint module is equivalent to an extended version of the B<-w>
  8. option of B<perl>. It is named after the program B<lint> which carries
  9. out a similar process for C programs.
  10. =head1 OPTIONS AND LINT CHECKS
  11. Option words are separated by commas (not whitespace) and follow the
  12. usual conventions of compiler backend options. Following any options
  13. (indicated by a leading B<->) come lint check arguments. Each such
  14. argument (apart from the special B<all> and B<none> options) is a
  15. word representing one possible lint check (turning on that check) or
  16. is B<no-foo> (turning off that check). Before processing the check
  17. arguments, a standard list of checks is turned on. Later options
  18. override earlier ones. Available options are:
  19. =over 8
  20. =item B<context>
  21. Produces a warning whenever an array is used in an implicit scalar
  22. context. For example, both of the lines
  23. $foo = length(@bar);
  24. $foo = @bar;
  25. will elicit a warning. Using an explicit B<scalar()> silences the
  26. warning. For example,
  27. $foo = scalar(@bar);
  28. =item B<implicit-read> and B<implicit-write>
  29. These options produce a warning whenever an operation implicitly
  30. reads or (respectively) writes to one of Perl's special variables.
  31. For example, B<implicit-read> will warn about these:
  32. /foo/;
  33. and B<implicit-write> will warn about these:
  34. s/foo/bar/;
  35. Both B<implicit-read> and B<implicit-write> warn about this:
  36. for (@a) { ... }
  37. =item B<dollar-underscore>
  38. This option warns whenever $_ is used either explicitly anywhere or
  39. as the implicit argument of a B<print> statement.
  40. =item B<private-names>
  41. This option warns on each use of any variable, subroutine or
  42. method name that lives in a non-current package but begins with
  43. an underscore ("_"). Warnings aren't issued for the special case
  44. of the single character name "_" by itself (e.g. $_ and @_).
  45. =item B<undefined-subs>
  46. This option warns whenever an undefined subroutine is invoked.
  47. This option will only catch explicitly invoked subroutines such
  48. as C<foo()> and not indirect invocations such as C<&$subref()>
  49. or C<$obj-E<gt>meth()>. Note that some programs or modules delay
  50. definition of subs until runtime by means of the AUTOLOAD
  51. mechanism.
  52. =item B<regexp-variables>
  53. This option warns whenever one of the regexp variables $', $& or
  54. $' is used. Any occurrence of any of these variables in your
  55. program can slow your whole program down. See L<perlre> for
  56. details.
  57. =item B<all>
  58. Turn all warnings on.
  59. =item B<none>
  60. Turn all warnings off.
  61. =back
  62. =head1 NON LINT-CHECK OPTIONS
  63. =over 8
  64. =item B<-u Package>
  65. Normally, Lint only checks the main code of the program together
  66. with all subs defined in package main. The B<-u> option lets you
  67. include other package names whose subs are then checked by Lint.
  68. =back
  69. =head1 BUGS
  70. This is only a very preliminary version.
  71. =head1 AUTHOR
  72. Malcolm Beattie, mbeattie@sable.ox.ac.uk.
  73. =cut
  74. use strict;
  75. use B qw(walkoptree_slow main_root walksymtable svref_2object parents);
  76. # Constants (should probably be elsewhere)
  77. sub G_ARRAY () { 1 }
  78. sub OPf_LIST () { 1 }
  79. sub OPf_KNOW () { 2 }
  80. sub OPf_STACKED () { 64 }
  81. my $file = "unknown"; # shadows current filename
  82. my $line = 0; # shadows current line number
  83. my $curstash = "main"; # shadows current stash
  84. # Lint checks
  85. my %check;
  86. my %implies_ok_context;
  87. BEGIN {
  88. map($implies_ok_context{$_}++,
  89. qw(pp_scalar pp_av2arylen pp_aelem pp_aslice pp_helem pp_hslice
  90. pp_keys pp_values pp_hslice pp_defined pp_undef pp_delete));
  91. }
  92. # Lint checks turned on by default
  93. my @default_checks = qw(context);
  94. my %valid_check;
  95. # All valid checks
  96. BEGIN {
  97. map($valid_check{$_}++,
  98. qw(context implicit_read implicit_write dollar_underscore
  99. private_names undefined_subs regexp_variables));
  100. }
  101. # Debugging options
  102. my ($debug_op);
  103. my %done_cv; # used to mark which subs have already been linted
  104. my @extra_packages; # Lint checks mainline code and all subs which are
  105. # in main:: or in one of these packages.
  106. sub warning {
  107. my $format = (@_ < 2) ? "%s" : shift;
  108. warn sprintf("$format at %s line %d\n", @_, $file, $line);
  109. }
  110. # This gimme can't cope with context that's only determined
  111. # at runtime via dowantarray().
  112. sub gimme {
  113. my $op = shift;
  114. my $flags = $op->flags;
  115. if ($flags & OPf_KNOW) {
  116. return(($flags & OPf_LIST) ? 1 : 0);
  117. }
  118. return undef;
  119. }
  120. sub B::OP::lint {}
  121. sub B::COP::lint {
  122. my $op = shift;
  123. if ($op->ppaddr eq "pp_nextstate") {
  124. $file = $op->filegv->SV->PV;
  125. $line = $op->line;
  126. $curstash = $op->stash->NAME;
  127. }
  128. }
  129. sub B::UNOP::lint {
  130. my $op = shift;
  131. my $ppaddr = $op->ppaddr;
  132. if ($check{context} && ($ppaddr eq "pp_rv2av" || $ppaddr eq "pp_rv2hv")) {
  133. my $parent = parents->[0];
  134. my $pname = $parent->ppaddr;
  135. return if gimme($op) || $implies_ok_context{$pname};
  136. # Two special cases to deal with: "foreach (@foo)" and "delete $a{$b}"
  137. # null out the parent so we have to check for a parent of pp_null and
  138. # a grandparent of pp_enteriter or pp_delete
  139. if ($pname eq "pp_null") {
  140. my $gpname = parents->[1]->ppaddr;
  141. return if $gpname eq "pp_enteriter" || $gpname eq "pp_delete";
  142. }
  143. warning("Implicit scalar context for %s in %s",
  144. $ppaddr eq "pp_rv2av" ? "array" : "hash", $parent->desc);
  145. }
  146. if ($check{private_names} && $ppaddr eq "pp_method") {
  147. my $methop = $op->first;
  148. if ($methop->ppaddr eq "pp_const") {
  149. my $method = $methop->sv->PV;
  150. if ($method =~ /^_/ && !defined(&{"$curstash\::$method"})) {
  151. warning("Illegal reference to private method name $method");
  152. }
  153. }
  154. }
  155. }
  156. sub B::PMOP::lint {
  157. my $op = shift;
  158. if ($check{implicit_read}) {
  159. my $ppaddr = $op->ppaddr;
  160. if ($ppaddr eq "pp_match" && !($op->flags & OPf_STACKED)) {
  161. warning('Implicit match on $_');
  162. }
  163. }
  164. if ($check{implicit_write}) {
  165. my $ppaddr = $op->ppaddr;
  166. if ($ppaddr eq "pp_subst" && !($op->flags & OPf_STACKED)) {
  167. warning('Implicit substitution on $_');
  168. }
  169. }
  170. }
  171. sub B::LOOP::lint {
  172. my $op = shift;
  173. if ($check{implicit_read} || $check{implicit_write}) {
  174. my $ppaddr = $op->ppaddr;
  175. if ($ppaddr eq "pp_enteriter") {
  176. my $last = $op->last;
  177. if ($last->ppaddr eq "pp_gv" && $last->gv->NAME eq "_") {
  178. warning('Implicit use of $_ in foreach');
  179. }
  180. }
  181. }
  182. }
  183. sub B::GVOP::lint {
  184. my $op = shift;
  185. if ($check{dollar_underscore} && $op->ppaddr eq "pp_gvsv"
  186. && $op->gv->NAME eq "_")
  187. {
  188. warning('Use of $_');
  189. }
  190. if ($check{private_names}) {
  191. my $ppaddr = $op->ppaddr;
  192. my $gv = $op->gv;
  193. if (($ppaddr eq "pp_gv" || $ppaddr eq "pp_gvsv")
  194. && $gv->NAME =~ /^_./ && $gv->STASH->NAME ne $curstash)
  195. {
  196. warning('Illegal reference to private name %s', $gv->NAME);
  197. }
  198. }
  199. if ($check{undefined_subs}) {
  200. if ($op->ppaddr eq "pp_gv" && $op->next->ppaddr eq "pp_entersub") {
  201. my $gv = $op->gv;
  202. my $subname = $gv->STASH->NAME . "::" . $gv->NAME;
  203. no strict 'refs';
  204. if (!defined(&$subname)) {
  205. $subname =~ s/^main:://;
  206. warning('Undefined subroutine %s called', $subname);
  207. }
  208. }
  209. }
  210. if ($check{regexp_variables} && $op->ppaddr eq "pp_gvsv") {
  211. my $name = $op->gv->NAME;
  212. if ($name =~ /^[&'`]$/) {
  213. warning('Use of regexp variable $%s', $name);
  214. }
  215. }
  216. }
  217. sub B::GV::lintcv {
  218. my $gv = shift;
  219. my $cv = $gv->CV;
  220. #warn sprintf("lintcv: %s::%s (done=%d)\n",
  221. # $gv->STASH->NAME, $gv->NAME, $done_cv{$$cv});#debug
  222. return if !$$cv || $done_cv{$$cv}++;
  223. my $root = $cv->ROOT;
  224. #warn " root = $root (0x$$root)\n";#debug
  225. walkoptree_slow($root, "lint") if $$root;
  226. }
  227. sub do_lint {
  228. my %search_pack;
  229. walkoptree_slow(main_root, "lint") if ${main_root()};
  230. # Now do subs in main
  231. no strict qw(vars refs);
  232. my $sym;
  233. local(*glob);
  234. while (($sym, *glob) = each %{"main::"}) {
  235. #warn "Trying $sym\n";#debug
  236. svref_2object(\*glob)->EGV->lintcv unless $sym =~ /::$/;
  237. }
  238. # Now do subs in non-main packages given by -u options
  239. map { $search_pack{$_} = 1 } @extra_packages;
  240. walksymtable(\%{"main::"}, "lintcv", sub {
  241. my $package = shift;
  242. $package =~ s/::$//;
  243. #warn "Considering $package\n";#debug
  244. return exists $search_pack{$package};
  245. });
  246. }
  247. sub compile {
  248. my @options = @_;
  249. my ($option, $opt, $arg);
  250. # Turn on default lint checks
  251. for $opt (@default_checks) {
  252. $check{$opt} = 1;
  253. }
  254. OPTION:
  255. while ($option = shift @options) {
  256. if ($option =~ /^-(.)(.*)/) {
  257. $opt = $1;
  258. $arg = $2;
  259. } else {
  260. unshift @options, $option;
  261. last OPTION;
  262. }
  263. if ($opt eq "-" && $arg eq "-") {
  264. shift @options;
  265. last OPTION;
  266. } elsif ($opt eq "D") {
  267. $arg ||= shift @options;
  268. foreach $arg (split(//, $arg)) {
  269. if ($arg eq "o") {
  270. B->debug(1);
  271. } elsif ($arg eq "O") {
  272. $debug_op = 1;
  273. }
  274. }
  275. } elsif ($opt eq "u") {
  276. $arg ||= shift @options;
  277. push(@extra_packages, $arg);
  278. }
  279. }
  280. foreach $opt (@default_checks, @options) {
  281. $opt =~ tr/-/_/;
  282. if ($opt eq "all") {
  283. %check = %valid_check;
  284. }
  285. elsif ($opt eq "none") {
  286. %check = ();
  287. }
  288. else {
  289. if ($opt =~ s/^no-//) {
  290. $check{$opt} = 0;
  291. }
  292. else {
  293. $check{$opt} = 1;
  294. }
  295. warn "No such check: $opt\n" unless defined $valid_check{$opt};
  296. }
  297. }
  298. # Remaining arguments are things to check
  299. return \&do_lint;
  300. }
  301. 1;