summaryrefslogtreecommitdiffstats
path: root/perl_checker
blob: d5484f5bfe7010d75ae75e0b590d2c934fe31c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/perl



sub begin {
    @exclude_calls = qw(chomp chop chr crypt hex index lc lcfirst length oct ord pack reverse rindex sprintf substr uc ucfirst pos quotemeta split study abs atan2 cos exp hex int log oct rand sin sqrt srand pop push shift splice unshift grep join map reverse sort unpack delete each exists keys values binmode close closedir dbmclose dbmopen die eof fileno flock format getc print printf read readdir rewinddir seek seekdir select syscall sysread sysseek syswrite tell telldir truncate warn write pack read syscall sysread syswrite unpack vec chdir chmod chown chroot fcntl glob ioctl link lstat mkdir open opendir readlink rename rmdir stat symlink umask unlink utime caller continue die do dump eval exit goto last next redo return sub wantarray caller import local my package use defined dump eval formline local my reset scalar undef wantarray alarm exec fork getpgrp getppid getpriority kill pipe setpgrp setpriority sleep system times wait waitpid do import no package require use bless dbmclose dbmopen package ref tie tied untie use accept bind connect getpeername getsockname getsockopt listen recv send setsockopt shutdown socket socketpair msgctl msgget msgrcv msgsnd semctl semget semop shmctl shmget shmread shmwrite endgrent endhostent endnetent endpwent getgrent getgrgid getgrnam getlogin getpwent getpwnam getpwuid setgrent setpwent endprotoent endservent gethostbyaddr gethostbyname gethostent getnetbyaddr getnetbyname getnetent getprotobyname getprotobynumber getprotoent getservbyname getservbyport getservent sethostent setnetent setprotoent setservent gmtime localtime time times abs bless chomp chr exists formline glob import lc lcfirst map my no prototype qx qw readline readpipe ref sysopen tie tied uc ucfirst untie use qq Dumper packdrake Eth);
    @exclude_calls{@exclude_calls} = undef;

    @exclude_uses = qw(globals diagnostics strict vars lib Carp Config Exporter Socket Storable Locale::GetText CORE POSIX Gtk Data::Dumper CGI Net::FTP Gtk::XmHTML Gtk::Gdk Gtk::Gdk::ImlibImage Gtk::Gdk::Pixbuf Newt Newt::Component Newt::Grid DynaLoader IO::Socket Date::Manip packdrake XML::Parser);

    while ($ARGV[0] =~ /-I(.*)/) {
	push @I, $1;
	shift @ARGV;
    }

    $NO_CW = $ARGV[0] eq "-no_cw" and shift @ARGV;
    $USES = $ARGV[0] eq "-uses" and shift @ARGV;


    @I = ('.', '..');

    foreach (@I) {
	open F, "$_/.perl_checker" or next;
	foreach (<F>) {
	    chomp;
	    push @exclude_uses, $_;
	}
    }

    unshift @INC, @I;
    @exclude_uses{@exclude_uses} = undef;

    if (!$NO_CW) {
	foreach (@ARGV) {
	    system("perl", (map {; "-I", $_ } @I), "-cw", $_);
	    $? == 0 or die "perl -cw $_ failed\n";
	}
    }
}

begin();

while (@ARGV) {
    $prev_ARGV = $ARGV;
    $ARGV = shift @ARGV;

    $package = 'main';
    $. = 0;
    open F, $ARGV or die "can't open file $ARGV\n";
    while (<F>) {
	chop;

	if ($. == 1 && $ARGV =~ /\.pm$/ && !/^package\b/) {
	    warn_(qq(module $ARGV does not have "package xxxx;" on its first line), info());
	}

	if (/^# perl_checker: RE-EXPORT-ALL/) {
	    push @re_exported, $package;
	}

	if (/^=(head|begin)/ .. /^=cut/) {
          die "missing =cut in $prev_ARGV\n" if $. == 1;
          next;
        }
	/^\s*#/ and next;

	$explicit_no_warning = /#-#/;

	if (/^\s*require\s+([\w:]+);/) {
	    add_use($1);
	} elsif (my ($r) = /^\s*require (.*)/) {
	    $r =~ /'(.*)'/ or err("<<$_>> should be formatted like <<require 'PACKAGE.pm'>>", info());
	    $r =~ m|'([^/]*)\.pm'| and add_use($1);
	    next;
	}

	syntax_warnings();
	drop_strings_and_regexps();
	syntax_warnings_after_removing_strings_and_regexps();
	get_exports();

    
	while (/(^|[^\$@\w:>\\]) # first a char
	        ((\w|:)*\w)      # function name
	        (?=\()         # a open parenthesis (zero-width lookahead)
	    /gx) {
	    my $f = $2;
	    add_call($f) if !/$f\(s\)/; # special case xxxx(s) excluded cuz' of strings containing it
	}
	while (/&([\w:]+)/g) { add_call($1); }
	
	
	/^use\s+([\w:]+);/ and add_use($1);
	if (my ($p, $v) = /^use\s([\w:]+)\s+(.*)/) {
	    $v =~ /^qw\((.*?)\)/ ? add_use($p, $1) : err("<<$_>> should be written <<use $p qw(:A :B ...)>>", info())
	      if !exists $exclude_uses{$p};
	}
	if (my ($p, $v) = /^\s*(\S+)->import\((.*)\)/) {
	    $v =~ /^qw\((.*?)\)/ ? add_use($p, $1) : err("<<$_>> should be written << $p->import(qw(:A :B ...))>>", info());
	}
	/^\s*sub\s+(\w+)/ and $sub{$package}->{$1} = 1;	
    }
    close F;
}

$explicit_no_warning = 0;
end();

sub drop_strings_and_regexps {
    s/"[^"]*"/"foo"/g;  #"
    s/'[^']*'/'foo'/g;  #'
    s/\bqq\([^)]*\)/'foo'/g;
    s/\bm\b([\/|!,])(.*?)\1/m!foo!/g; # m// operator: "m/re/", "m|re|", ...
    s/\bs\b([\/|!,])(.*?)\1(.*?)\1/s!foo!bar!/g; # s/// operator: "s/re/.../", "s|re|...|", ...
    s|\(/.*?/|(/foo/|g; # m// operator: "(/re/"
    s{([!=]~|\bif\b)\s*/.*?/}{$1 /foo/}g; # m// operator: "if /re/", "=~ /re/", "!~ /re/"
    
    s/#.*//;
}

sub get_exports {
    if (/^\s*package ([\w:]+)/) {
        $package = $1;
        $path{$package} ||= ($ARGV =~ m|(.*/)|)[0];
    }
    
    
    if (/\@EXPORT = qw\((.*)\)/) {
        @{$export{$package}}{split ' ', $1} = undef;
    }
    if (/\@EXPORT_OK = qw\((.*)\)/) {
        @{$export_ok{$package}}{split ' ', $1} = undef;
        $export_info{$package} = info();
    }
    if (/%EXPORT_TAGS = \((\w+) => \[ \@EXPORT_OK \]\)/) {
        $export_tags{$package}->{$1} = $export_ok{$package};
    }
    if (/%EXPORT_TAGS = \(\s*$/ .. /^\);$/) {
        /\s+(\w+)\s+.*?qw\((.*)\)/;
        @{$export_tags{$package}->{$1}}{split ' ', $2} = undef;
        $export_tags_info{$package}->{$1} = info();
    }
}

sub syntax_warnings {

    if (/\W("\$\w+")/ && !/qq\(/ && !/^\s*case "\$/ && !/\b[A-Z]\w*\s+"\$\w+"/) {
	warn_(qq($1 is better written without the double quotes), info());
    }

    if (/^\s*my\b[^;]*=\s*(\(\)|undef);/) {
        warn_(q(no need to initialize variables, it's done by default), info());
    }
    
    if (/[^#\s)}\]]\s+\)/) {
        warn_(q(spurious space before closing parenthesis), info());
    }
    
    if (/;\s*\}/) {
        warn_(q(spurious ";" before closing block), info());
    }
    
    if (/(.*\s\{)[^\s{}]/ && !/\{[\d,]+\}/ && !/Usage:/) { #- eg of Usage: cmd {foo|bar}
        warn_(qq(missing space after "$1"), info());
    }
    
    if (/\{\s[^{}]*[^\s{}]\}/) {
        warn_(qq(missing space before "}$'"), info());
    }

    if (/(\$\w+)\s*=\s+.*\s+((if\s+!|unless)\s*\1);/) {
	warn_(qq("$1 = ... $2" can be written "$1 ||= ..."), info());
    }

    if (my ($infix, $cond) = /\S\s+(if|unless|foreach)\s+(\([^,]*\));?$/) {
	if (remove_parentheses($cond) eq '') {
	    warn_(qq(''... $1 ($2)'' can be written ''... $1 $2''), info());
	}
    }

    if (my ($cond) = /\bunless\s+(\(.*?\))\s+{/ ? $1 : /\bunless\s+([^;]*);?$/) {
	if (remove_funcalls($cond) =~ /(!|&&|\|\|)/) {
	    warn_(qq(don't use "unless" when the condition is complex, use "if" instead), info());
	}
    }
    
    if (/%_\W/ || /[^@]\$_{/) { #-#
        err(q(do not use %_), info()); #-#
    }
    if (/\$_\s*=~/) {
        warn_(q(``$_ =~ /regexp/'' can be written ``/regexp/''), info());
    }
    if (/\$_\s*!~/) {
        warn_(q(``$_ !~ /regexp/'' can be written ``!/regexp/''), info());
    }
    if (/my \$\w+\s*=~/ || /my \(\$\w+\)\s*=~/) {
        err(q(applying =~ on a new initialized variable is wrong), info());
    }
    if (/[}>]{(['"])([a-z][a-z0-9_]*)\1}/i) {
        warn_(qq(``{$1$2$1}'' can be written ``{$2}''), info())
          if $2 ne 'x' && $2 ne 'y'; 
          # special case for {'y'} otherwise the emacs mode goes wild
          # special case for {'x'} to have the same as {'y'} (since they usually go together)
    }
    if (/%\$\w+(->|\{)/ || /%\{([^{}]|\{[^{}]*\})*\}(->|\{)/ ||
        /@\$\w+->/      || /@\{([^{}]|\{[^{}]*\})*\}->/) {
        err(q(bad expression, tell pixel@mandrakesoft.com), info());
    }
    
    if (/\blength(\s*|\()\@/) {
        err(q(never use ``length @l'', it returns the length of the string int(@l)), info());
    }
    
    if (/\blocal\b.*\|\|=/) {
        err(q(never use ``local $var ||= <val>'', write ``local $var = $var || <val>''), info());
    }
    
    if (/\bsub\b\s*\{[^{}]*\$_(?!\[)/) {
        warn_(q(using $_ in a sub is generally bad news), info()) if !/local\s+\$_/;
    }
    
    if ((my ($op) = /([<>]{2})/) && (/[+-]\s*[\w\$]+\s*[<>]{2}/ || /[<>]{2}\s*[\w\$]+\s*[+-]/) && !/\<\<[A-Z]/) { # don't warn for here-document alike
        err(qq(parentheses needed around operator $op), info());
    }
    if (/=.*:\s*\$\w+\s*=/) {
        err(q(do not use ``cond ? $v1 = XX1 : $v2 = XX2'' which is parsed as ``(cond ? $v1 = XX1 : $v2) = XX2''), info());
    }

    if (my ($f, $s) = /(N_?)\((.*)/) {
	$s =~ s/\\"//g;
	$s =~ s/"([^"]*)($|".*)/$1/ or err(qq($f must be used as $f("xxx") for xgettext to work correctly (hint: use translate instead)), info());
	$s =~ s/\\[\$@]//g;
	$s !~ /[\$@]/ or err(qq(don't use interpolated translated string, use %s or %d instead), info());
    }    

    if (my (undef, $stash) = /\bopen(\(|\s+)(\w+),/) {
	if ($stash !~ /^(STDIN|STDOUT|STDERR)$/ && (/\local \*(\w+)/ ? $1 : $stash_localised) ne $stash) {
	    err(qq(you must localize $stash ("local *$stash;" must be on the previous line)), info());
	}
    }
    ($stash_localised) = /\blocal \*(\w+)/;

    if (/\bwhile \(</ && !/\blocal \$_;/ && !$underscore_localised) {
	err(q(expression while (<FILEHANDLE>) ... modifies $_, localize it first (it must be on the previous line)), info());
    }
    $underscore_localised = /\blocal \$_;/;

    if (/\bfor\s+my/ || /\bfor\b\s*\([^;]*$/ && !/\bfor\b\s*\(\$[^),\s]*\)/) { #- for is allowed with the meaning of "with" in Pascal
	err(q(use "foreach" instead of "for" (these are synonyms, but foreach has been chosen to be the default)), info());
    }
}

sub syntax_warnings_after_removing_strings_and_regexps {
    local $_ = $_;
    s/\bqw\([^)]*\)/'foo'/g;

    if (my ($before, $f, $pkg, $after) = /(.*[^a-zA-Z0-9_:@*%&\$])((\w+::)+\w+)\s*([^a-zA-Z0-9_(: {].*)/) {
	if ($before !~ /(sub|use|package|require|new|->)\s*$/ && $after !~ /^->/) {
	    $pkg =~ s/::$//;
	    warn_(qq($f must be written $f() otherwise it will not work if "use $pkg" is replaced by "require $pkg"), info());
	}
    }

    if (/\}->([{\[])/) {
	warn_(qq("}->$2" is better written "}$2"), info());
    }

    if (/[\w>}]\[[^\]\[]*\]->([{\[])/) {
	warn_(qq("]->$2" is better written "]$2"), info());
    }

    if (/([\$@%]\w+=)[^~]/) {
        warn_(qq(missing space before "=" in $1), info());
    }
    
    if (/\(\s+[^#\s({\[]/) {
        warn_(q(spurious space after opening parenthesis), info());
    }

    if (/;([^\s\\].{0,10})/ && !/^sub/ && !/;-\)/ && !/^\s+;;$/) {
        warn_(qq(missing space after ";$1"), info());
    }
}

sub add_call {
    $explicit_no_warning and return;
    $_[0] =~ /^\d/ and return;
    exists $exclude_calls{$_[0]} and return;
    $call{$package}->{$_[0]} = 1;
    $call_info{$package}->{$_[0]} = info()
}

sub add_use {
    my ($p, $op) = @_;
    exists $exclude_uses{$p} and return;

    warn_("uses $p", info()) if $USES;

    my $already = $use_info{$package}->{$p};
#    warn_("reused " . $already->{file} . " " . $already->{line}, info()) if $already && $ARGV ne $already->{file};

    $use{$package}->{$p} ||= []; # create it

    foreach (split ' ', $op) {
	if (/^:(.*)/) {
	    push @{$use_tags{$package}->{$p}}, $1;
	} else {
	    push @{$use{$package}->{$p}}, $_;
	}
    }
	    
    $use_info{$package}->{$p} = info();

    $path{$p} and return;
    my ($path, $f) = "$p.pm" =~ /(.*::)?(.*)/;
    $path =~ s|::|/|g;
    
    $path{$p} = updirs($path{$package}, (($package =~ tr/://) / 2));
    foreach ($path{$p}, @INC) {
	$_ .= "/" if !m|/$|;
	my $file = "$_$path$f";
	if (-e $file) {
	    push @ARGV, $file;
	    $file2package{$file} = $p;
	    return;
	}
    }
    err("can't find package $p", info());
}

#sub add_usevar {
#    length $_[0] <= 3 and return;
#    $usevar{$package}->{$_[0]} = 1;
#    $usevar_info{$package}->{$_[0]} = info();
#}

sub info { { file => $ARGV, line => $. } }

sub updirs {
    my ($p, $i) = @_;
    for (; $i && $p =~ s|[^/]+/^||; $i--) {}
    for (; $i ; $i--) { $p .= "../"; }
    $p;
}


sub err {
    return if $explicit_no_warning;

    $ERR = 1;
    &warn_;
}
sub warn_ {
    return if $explicit_no_warning;

    my ($m, $i) = @_;
    if ($i) {
	 print STDERR "$i->{file}:$i->{line}: $m\n";
    } else {
	 print STDERR "$m\n";
    }
}

sub end {
    foreach my $package (reverse @re_exported) {
	my $h = $export{$package} ||= {};
	%$h = (%$h, 
	       (map { %{$export{$_}} } keys %{$use{$package}}),
	       (map { %$_ } map { values %{$export_tags{$_}} } keys %{$use{$package}}),
	       );
    }

#    while (my ($package, $vars) = each %usevar) {
#	 my @l = grep { ! $defvar{$package}->{$_} } keys %$vars;
#	 @l and print "$package: uses vars (", join(' ', @l), ")";
#    }

    while (my ($package, $calls) = each %call) {
        here: foreach my $func_verbatim (keys %$calls) {

	    my ($pkg, $func) = do {
		if ($func_verbatim =~ /(.*)::(.*)/) {
		    exists $exclude_uses{$1} and next;
		    exists $use{$package}->{$1} or err("you must <<use $1>> for function $2", $call_info{$package}->{$func_verbatim});
		    $1, $2;
		} else {
		    $package, $func_verbatim;
		}
	    };
	    $sub{$pkg}->{$func} and next; # defined in own package
	    
	    foreach my $k (keys %{$use{$pkg}}) {
		exists $export{$k}->{$func} and next here; # ok, exported by $k

		foreach (@{$use_tags{$pkg}->{$k}}) {
		    exists $export_tags{$k}->{$_}->{$func} and next here; # ok, exported by $k in tag $_
		}
	    }
	    err("$func_verbatim undefined", $call_info{$package}->{$func_verbatim}) if $func_verbatim ne 'q';
	}
    }
    $ERR and exit $ERR;
}

sub remove_parentheses {
    my ($s) = @_;
    while ($s =~ s/\([^()]*\)//g) {}
    $s;
}

sub remove_funcalls {
    my ($s) = @_;
    my $r = '';
    while (my ($prev, $next) = $s =~ /^(.*?\w+)(\(.*)/) {
	$r .= "$prev()";
	$s = $next;
	while ($s =~ /^\(/) {
	    $s =~ s/\([^()]*\)//g or return $r;
	}
    }
    "$r$s";
}