update checkpatch.pl to version 0.18
[safe/jmp/linux-2.6] / scripts / checkpatch.pl
1 #!/usr/bin/perl -w
2 # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc)
5 # Licensed under the terms of the GNU GPL License version 2
6
7 use strict;
8
9 my $P = $0;
10 $P =~ s@.*/@@g;
11
12 my $V = '0.18';
13
14 use Getopt::Long qw(:config no_auto_abbrev);
15
16 my $quiet = 0;
17 my $tree = 1;
18 my $chk_signoff = 1;
19 my $chk_patch = 1;
20 my $tst_type = 0;
21 my $tst_only;
22 my $emacs = 0;
23 my $terse = 0;
24 my $file = 0;
25 my $check = 0;
26 my $summary = 1;
27 my $mailback = 0;
28 my $summary_file = 0;
29 my $root;
30 my %debug;
31 GetOptions(
32         'q|quiet+'      => \$quiet,
33         'tree!'         => \$tree,
34         'signoff!'      => \$chk_signoff,
35         'patch!'        => \$chk_patch,
36         'emacs!'        => \$emacs,
37         'terse!'        => \$terse,
38         'file!'         => \$file,
39         'subjective!'   => \$check,
40         'strict!'       => \$check,
41         'root=s'        => \$root,
42         'summary!'      => \$summary,
43         'mailback!'     => \$mailback,
44         'summary-file!' => \$summary_file,
45
46         'debug=s'       => \%debug,
47         'test-type!'    => \$tst_type,
48         'test-only=s'   => \$tst_only,
49 ) or exit;
50
51 my $exit = 0;
52
53 if ($#ARGV < 0) {
54         print "usage: $P [options] patchfile\n";
55         print "version: $V\n";
56         print "options: -q               => quiet\n";
57         print "         --no-tree        => run without a kernel tree\n";
58         print "         --terse          => one line per report\n";
59         print "         --emacs          => emacs compile window format\n";
60         print "         --file           => check a source file\n";
61         print "         --strict         => enable more subjective tests\n";
62         print "         --root           => path to the kernel tree root\n";
63         print "         --no-summary     => suppress the per-file summary\n";
64         print "         --summary-file   => include the filename in summary\n";
65         exit(1);
66 }
67
68 my $dbg_values = 0;
69 my $dbg_possible = 0;
70 for my $key (keys %debug) {
71         eval "\${dbg_$key} = '$debug{$key}';"
72 }
73
74 if ($terse) {
75         $emacs = 1;
76         $quiet++;
77 }
78
79 if ($tree) {
80         if (defined $root) {
81                 if (!top_of_kernel_tree($root)) {
82                         die "$P: $root: --root does not point at a valid tree\n";
83                 }
84         } else {
85                 if (top_of_kernel_tree('.')) {
86                         $root = '.';
87                 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
88                                                 top_of_kernel_tree($1)) {
89                         $root = $1;
90                 }
91         }
92
93         if (!defined $root) {
94                 print "Must be run from the top-level dir. of a kernel tree\n";
95                 exit(2);
96         }
97 }
98
99 my $emitted_corrupt = 0;
100
101 our $Ident       = qr{[A-Za-z_][A-Za-z\d_]*};
102 our $Storage    = qr{extern|static|asmlinkage};
103 our $Sparse     = qr{
104                         __user|
105                         __kernel|
106                         __force|
107                         __iomem|
108                         __must_check|
109                         __init_refok|
110                         __kprobes
111                 }x;
112 our $Attribute  = qr{
113                         const|
114                         __read_mostly|
115                         __kprobes|
116                         __(?:mem|cpu|dev|)(?:initdata|init)
117                   }x;
118 our $Inline     = qr{inline|__always_inline|noinline};
119 our $Member     = qr{->$Ident|\.$Ident|\[[^]]*\]};
120 our $Lval       = qr{$Ident(?:$Member)*};
121
122 our $Constant   = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
123 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
124 our $Operators  = qr{
125                         <=|>=|==|!=|
126                         =>|->|<<|>>|<|>|!|~|
127                         &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
128                   }x;
129
130 our $NonptrType;
131 our $Type;
132 our $Declare;
133
134 our $UTF8       = qr {
135         [\x09\x0A\x0D\x20-\x7E]              # ASCII
136         | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
137         |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
138         | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
139         |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
140         |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
141         | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
142         |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
143 }x;
144
145 our @typeList = (
146         qr{void},
147         qr{char},
148         qr{short},
149         qr{int},
150         qr{long},
151         qr{unsigned},
152         qr{float},
153         qr{double},
154         qr{bool},
155         qr{long\s+int},
156         qr{long\s+long},
157         qr{long\s+long\s+int},
158         qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
159         qr{struct\s+$Ident},
160         qr{union\s+$Ident},
161         qr{enum\s+$Ident},
162         qr{${Ident}_t},
163         qr{${Ident}_handler},
164         qr{${Ident}_handler_fn},
165 );
166
167 sub build_types {
168         my $all = "(?:  \n" . join("|\n  ", @typeList) . "\n)";
169         $NonptrType     = qr{
170                         \b
171                         (?:const\s+)?
172                         (?:unsigned\s+)?
173                         (?:
174                                 $all|
175                                 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
176                         )
177                         (?:\s+$Sparse|\s+const)*
178                         \b
179                   }x;
180         $Type   = qr{
181                         \b$NonptrType\b
182                         (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
183                         (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
184                   }x;
185         $Declare        = qr{(?:$Storage\s+)?$Type};
186 }
187 build_types();
188
189 $chk_signoff = 0 if ($file);
190
191 my @dep_includes = ();
192 my @dep_functions = ();
193 my $removal = "Documentation/feature-removal-schedule.txt";
194 if ($tree && -f "$root/$removal") {
195         open(REMOVE, "<$root/$removal") ||
196                                 die "$P: $removal: open failed - $!\n";
197         while (<REMOVE>) {
198                 if (/^Check:\s+(.*\S)/) {
199                         for my $entry (split(/[, ]+/, $1)) {
200                                 if ($entry =~ m@include/(.*)@) {
201                                         push(@dep_includes, $1);
202
203                                 } elsif ($entry !~ m@/@) {
204                                         push(@dep_functions, $entry);
205                                 }
206                         }
207                 }
208         }
209 }
210
211 my @rawlines = ();
212 my @lines = ();
213 my $vname;
214 for my $filename (@ARGV) {
215         if ($file) {
216                 open(FILE, "diff -u /dev/null $filename|") ||
217                         die "$P: $filename: diff failed - $!\n";
218         } else {
219                 open(FILE, "<$filename") ||
220                         die "$P: $filename: open failed - $!\n";
221         }
222         if ($filename eq '-') {
223                 $vname = 'Your patch';
224         } else {
225                 $vname = $filename;
226         }
227         while (<FILE>) {
228                 chomp;
229                 push(@rawlines, $_);
230         }
231         close(FILE);
232         if (!process($filename)) {
233                 $exit = 1;
234         }
235         @rawlines = ();
236         @lines = ();
237 }
238
239 exit($exit);
240
241 sub top_of_kernel_tree {
242         my ($root) = @_;
243
244         my @tree_check = (
245                 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
246                 "README", "Documentation", "arch", "include", "drivers",
247                 "fs", "init", "ipc", "kernel", "lib", "scripts",
248         );
249
250         foreach my $check (@tree_check) {
251                 if (! -e $root . '/' . $check) {
252                         return 0;
253                 }
254         }
255         return 1;
256 }
257
258 sub expand_tabs {
259         my ($str) = @_;
260
261         my $res = '';
262         my $n = 0;
263         for my $c (split(//, $str)) {
264                 if ($c eq "\t") {
265                         $res .= ' ';
266                         $n++;
267                         for (; ($n % 8) != 0; $n++) {
268                                 $res .= ' ';
269                         }
270                         next;
271                 }
272                 $res .= $c;
273                 $n++;
274         }
275
276         return $res;
277 }
278 sub copy_spacing {
279         (my $res = shift) =~ tr/\t/ /c;
280         return $res;
281 }
282
283 sub line_stats {
284         my ($line) = @_;
285
286         # Drop the diff line leader and expand tabs
287         $line =~ s/^.//;
288         $line = expand_tabs($line);
289
290         # Pick the indent from the front of the line.
291         my ($white) = ($line =~ /^(\s*)/);
292
293         return (length($line), length($white));
294 }
295
296 my $sanitise_quote = '';
297
298 sub sanitise_line_reset {
299         my ($in_comment) = @_;
300
301         if ($in_comment) {
302                 $sanitise_quote = '*/';
303         } else {
304                 $sanitise_quote = '';
305         }
306 }
307 sub sanitise_line {
308         my ($line) = @_;
309
310         my $res = '';
311         my $l = '';
312
313         my $qlen = 0;
314         my $off = 0;
315         my $c;
316
317         # Always copy over the diff marker.
318         $res = substr($line, 0, 1);
319
320         for ($off = 1; $off < length($line); $off++) {
321                 $c = substr($line, $off, 1);
322
323                 # Comments we are wacking completly including the begin
324                 # and end, all to $;.
325                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
326                         $sanitise_quote = '*/';
327
328                         substr($res, $off, 2, "$;$;");
329                         $off++;
330                         next;
331                 }
332                 if (substr($line, $off, 2) eq $sanitise_quote) {
333                         $sanitise_quote = '';
334                         substr($res, $off, 2, "$;$;");
335                         $off++;
336                         next;
337                 }
338
339                 # A \ in a string means ignore the next character.
340                 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
341                     $c eq "\\") {
342                         substr($res, $off, 2, 'XX');
343                         $off++;
344                         next;
345                 }
346                 # Regular quotes.
347                 if ($c eq "'" || $c eq '"') {
348                         if ($sanitise_quote eq '') {
349                                 $sanitise_quote = $c;
350
351                                 substr($res, $off, 1, $c);
352                                 next;
353                         } elsif ($sanitise_quote eq $c) {
354                                 $sanitise_quote = '';
355                         }
356                 }
357
358                 #print "SQ:$sanitise_quote\n";
359                 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
360                         substr($res, $off, 1, $;);
361                 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
362                         substr($res, $off, 1, 'X');
363                 } else {
364                         substr($res, $off, 1, $c);
365                 }
366         }
367
368         # The pathname on a #include may be surrounded by '<' and '>'.
369         if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
370                 my $clean = 'X' x length($1);
371                 $res =~ s@\<.*\>@<$clean>@;
372
373         # The whole of a #error is a string.
374         } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
375                 my $clean = 'X' x length($1);
376                 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
377         }
378
379         return $res;
380 }
381
382 sub ctx_statement_block {
383         my ($linenr, $remain, $off) = @_;
384         my $line = $linenr - 1;
385         my $blk = '';
386         my $soff = $off;
387         my $coff = $off - 1;
388         my $coff_set = 0;
389
390         my $loff = 0;
391
392         my $type = '';
393         my $level = 0;
394         my $p;
395         my $c;
396         my $len = 0;
397
398         my $remainder;
399         while (1) {
400                 #warn "CSB: blk<$blk> remain<$remain>\n";
401                 # If we are about to drop off the end, pull in more
402                 # context.
403                 if ($off >= $len) {
404                         for (; $remain > 0; $line++) {
405                                 next if ($lines[$line] =~ /^-/);
406                                 $remain--;
407                                 $loff = $len;
408                                 $blk .= $lines[$line] . "\n";
409                                 $len = length($blk);
410                                 $line++;
411                                 last;
412                         }
413                         # Bail if there is no further context.
414                         #warn "CSB: blk<$blk> off<$off> len<$len>\n";
415                         if ($off >= $len) {
416                                 last;
417                         }
418                 }
419                 $p = $c;
420                 $c = substr($blk, $off, 1);
421                 $remainder = substr($blk, $off);
422
423                 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
424                 # Statement ends at the ';' or a close '}' at the
425                 # outermost level.
426                 if ($level == 0 && $c eq ';') {
427                         last;
428                 }
429
430                 # An else is really a conditional as long as its not else if
431                 if ($level == 0 && $coff_set == 0 &&
432                                 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
433                                 $remainder =~ /^(else)(?:\s|{)/ &&
434                                 $remainder !~ /^else\s+if\b/) {
435                         $coff = $off + length($1) - 1;
436                         $coff_set = 1;
437                         #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
438                         #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
439                 }
440
441                 if (($type eq '' || $type eq '(') && $c eq '(') {
442                         $level++;
443                         $type = '(';
444                 }
445                 if ($type eq '(' && $c eq ')') {
446                         $level--;
447                         $type = ($level != 0)? '(' : '';
448
449                         if ($level == 0 && $coff < $soff) {
450                                 $coff = $off;
451                                 $coff_set = 1;
452                                 #warn "CSB: mark coff<$coff>\n";
453                         }
454                 }
455                 if (($type eq '' || $type eq '{') && $c eq '{') {
456                         $level++;
457                         $type = '{';
458                 }
459                 if ($type eq '{' && $c eq '}') {
460                         $level--;
461                         $type = ($level != 0)? '{' : '';
462
463                         if ($level == 0) {
464                                 last;
465                         }
466                 }
467                 $off++;
468         }
469         if ($off == $len) {
470                 $line++;
471                 $remain--;
472         }
473
474         my $statement = substr($blk, $soff, $off - $soff + 1);
475         my $condition = substr($blk, $soff, $coff - $soff + 1);
476
477         #warn "STATEMENT<$statement>\n";
478         #warn "CONDITION<$condition>\n";
479
480         #print "coff<$coff> soff<$off> loff<$loff>\n";
481
482         return ($statement, $condition,
483                         $line, $remain + 1, $off - $loff + 1, $level);
484 }
485
486 sub statement_lines {
487         my ($stmt) = @_;
488
489         # Strip the diff line prefixes and rip blank lines at start and end.
490         $stmt =~ s/(^|\n)./$1/g;
491         $stmt =~ s/^\s*//;
492         $stmt =~ s/\s*$//;
493
494         my @stmt_lines = ($stmt =~ /\n/g);
495
496         return $#stmt_lines + 2;
497 }
498
499 sub statement_rawlines {
500         my ($stmt) = @_;
501
502         my @stmt_lines = ($stmt =~ /\n/g);
503
504         return $#stmt_lines + 2;
505 }
506
507 sub statement_block_size {
508         my ($stmt) = @_;
509
510         $stmt =~ s/(^|\n)./$1/g;
511         $stmt =~ s/^\s*{//;
512         $stmt =~ s/}\s*$//;
513         $stmt =~ s/^\s*//;
514         $stmt =~ s/\s*$//;
515
516         my @stmt_lines = ($stmt =~ /\n/g);
517         my @stmt_statements = ($stmt =~ /;/g);
518
519         my $stmt_lines = $#stmt_lines + 2;
520         my $stmt_statements = $#stmt_statements + 1;
521
522         if ($stmt_lines > $stmt_statements) {
523                 return $stmt_lines;
524         } else {
525                 return $stmt_statements;
526         }
527 }
528
529 sub ctx_statement_full {
530         my ($linenr, $remain, $off) = @_;
531         my ($statement, $condition, $level);
532
533         my (@chunks);
534
535         # Grab the first conditional/block pair.
536         ($statement, $condition, $linenr, $remain, $off, $level) =
537                                 ctx_statement_block($linenr, $remain, $off);
538         #print "F: c<$condition> s<$statement> remain<$remain>\n";
539         push(@chunks, [ $condition, $statement ]);
540         if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
541                 return ($level, $linenr, @chunks);
542         }
543
544         # Pull in the following conditional/block pairs and see if they
545         # could continue the statement.
546         for (;;) {
547                 ($statement, $condition, $linenr, $remain, $off, $level) =
548                                 ctx_statement_block($linenr, $remain, $off);
549                 #print "C: c<$condition> s<$statement> remain<$remain>\n";
550                 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
551                 #print "C: push\n";
552                 push(@chunks, [ $condition, $statement ]);
553         }
554
555         return ($level, $linenr, @chunks);
556 }
557
558 sub ctx_block_get {
559         my ($linenr, $remain, $outer, $open, $close, $off) = @_;
560         my $line;
561         my $start = $linenr - 1;
562         my $blk = '';
563         my @o;
564         my @c;
565         my @res = ();
566
567         my $level = 0;
568         for ($line = $start; $remain > 0; $line++) {
569                 next if ($rawlines[$line] =~ /^-/);
570                 $remain--;
571
572                 $blk .= $rawlines[$line];
573                 foreach my $c (split(//, $rawlines[$line])) {
574                         ##print "C<$c>L<$level><$open$close>O<$off>\n";
575                         if ($off > 0) {
576                                 $off--;
577                                 next;
578                         }
579
580                         if ($c eq $close && $level > 0) {
581                                 $level--;
582                                 last if ($level == 0);
583                         } elsif ($c eq $open) {
584                                 $level++;
585                         }
586                 }
587
588                 if (!$outer || $level <= 1) {
589                         push(@res, $rawlines[$line]);
590                 }
591
592                 last if ($level == 0);
593         }
594
595         return ($level, @res);
596 }
597 sub ctx_block_outer {
598         my ($linenr, $remain) = @_;
599
600         my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
601         return @r;
602 }
603 sub ctx_block {
604         my ($linenr, $remain) = @_;
605
606         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
607         return @r;
608 }
609 sub ctx_statement {
610         my ($linenr, $remain, $off) = @_;
611
612         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
613         return @r;
614 }
615 sub ctx_block_level {
616         my ($linenr, $remain) = @_;
617
618         return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
619 }
620 sub ctx_statement_level {
621         my ($linenr, $remain, $off) = @_;
622
623         return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
624 }
625
626 sub ctx_locate_comment {
627         my ($first_line, $end_line) = @_;
628
629         # Catch a comment on the end of the line itself.
630         my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
631         return $current_comment if (defined $current_comment);
632
633         # Look through the context and try and figure out if there is a
634         # comment.
635         my $in_comment = 0;
636         $current_comment = '';
637         for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
638                 my $line = $rawlines[$linenr - 1];
639                 #warn "           $line\n";
640                 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
641                         $in_comment = 1;
642                 }
643                 if ($line =~ m@/\*@) {
644                         $in_comment = 1;
645                 }
646                 if (!$in_comment && $current_comment ne '') {
647                         $current_comment = '';
648                 }
649                 $current_comment .= $line . "\n" if ($in_comment);
650                 if ($line =~ m@\*/@) {
651                         $in_comment = 0;
652                 }
653         }
654
655         chomp($current_comment);
656         return($current_comment);
657 }
658 sub ctx_has_comment {
659         my ($first_line, $end_line) = @_;
660         my $cmt = ctx_locate_comment($first_line, $end_line);
661
662         ##print "LINE: $rawlines[$end_line - 1 ]\n";
663         ##print "CMMT: $cmt\n";
664
665         return ($cmt ne '');
666 }
667
668 sub cat_vet {
669         my ($vet) = @_;
670         my ($res, $coded);
671
672         $res = '';
673         while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
674                 $res .= $1;
675                 if ($2 ne '') {
676                         $coded = sprintf("^%c", unpack('C', $2) + 64);
677                         $res .= $coded;
678                 }
679         }
680         $res =~ s/$/\$/;
681
682         return $res;
683 }
684
685 my $av_preprocessor = 0;
686 my $av_pending;
687 my @av_paren_type;
688
689 sub annotate_reset {
690         $av_preprocessor = 0;
691         $av_pending = '_';
692         @av_paren_type = ('E');
693 }
694
695 sub annotate_values {
696         my ($stream, $type) = @_;
697
698         my $res;
699         my $cur = $stream;
700
701         print "$stream\n" if ($dbg_values > 1);
702
703         while (length($cur)) {
704                 @av_paren_type = ('E') if ($#av_paren_type < 0);
705                 print " <" . join('', @av_paren_type) .
706                                 "> <$type> <$av_pending>" if ($dbg_values > 1);
707                 if ($cur =~ /^(\s+)/o) {
708                         print "WS($1)\n" if ($dbg_values > 1);
709                         if ($1 =~ /\n/ && $av_preprocessor) {
710                                 $type = pop(@av_paren_type);
711                                 $av_preprocessor = 0;
712                         }
713
714                 } elsif ($cur =~ /^($Type)/) {
715                         print "DECLARE($1)\n" if ($dbg_values > 1);
716                         $type = 'T';
717
718                 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
719                         print "DEFINE($1,$2)\n" if ($dbg_values > 1);
720                         $av_preprocessor = 1;
721                         push(@av_paren_type, $type);
722                         if ($2 ne '') {
723                                 $av_pending = 'N';
724                         }
725                         $type = 'E';
726
727                 } elsif ($cur =~ /^(#\s*undef\s*$Ident)/o) {
728                         print "UNDEF($1)\n" if ($dbg_values > 1);
729                         $av_preprocessor = 1;
730                         push(@av_paren_type, $type);
731
732                 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
733                         print "PRE_START($1)\n" if ($dbg_values > 1);
734                         $av_preprocessor = 1;
735
736                         push(@av_paren_type, $type);
737                         push(@av_paren_type, $type);
738                         $type = 'E';
739
740                 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
741                         print "PRE_RESTART($1)\n" if ($dbg_values > 1);
742                         $av_preprocessor = 1;
743
744                         push(@av_paren_type, $av_paren_type[$#av_paren_type]);
745
746                         $type = 'E';
747
748                 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
749                         print "PRE_END($1)\n" if ($dbg_values > 1);
750
751                         $av_preprocessor = 1;
752
753                         # Assume all arms of the conditional end as this
754                         # one does, and continue as if the #endif was not here.
755                         pop(@av_paren_type);
756                         push(@av_paren_type, $type);
757                         $type = 'E';
758
759                 } elsif ($cur =~ /^(\\\n)/o) {
760                         print "PRECONT($1)\n" if ($dbg_values > 1);
761
762                 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
763                         print "ATTR($1)\n" if ($dbg_values > 1);
764                         $av_pending = $type;
765                         $type = 'N';
766
767                 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
768                         print "SIZEOF($1)\n" if ($dbg_values > 1);
769                         if (defined $2) {
770                                 $av_pending = 'V';
771                         }
772                         $type = 'N';
773
774                 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
775                         print "COND($1)\n" if ($dbg_values > 1);
776                         $av_pending = 'N';
777                         $type = 'N';
778
779                 } elsif ($cur =~/^(return|case|else)/o) {
780                         print "KEYWORD($1)\n" if ($dbg_values > 1);
781                         $type = 'N';
782
783                 } elsif ($cur =~ /^(\()/o) {
784                         print "PAREN('$1')\n" if ($dbg_values > 1);
785                         push(@av_paren_type, $av_pending);
786                         $av_pending = '_';
787                         $type = 'N';
788
789                 } elsif ($cur =~ /^(\))/o) {
790                         my $new_type = pop(@av_paren_type);
791                         if ($new_type ne '_') {
792                                 $type = $new_type;
793                                 print "PAREN('$1') -> $type\n"
794                                                         if ($dbg_values > 1);
795                         } else {
796                                 print "PAREN('$1')\n" if ($dbg_values > 1);
797                         }
798
799                 } elsif ($cur =~ /^($Ident)\(/o) {
800                         print "FUNC($1)\n" if ($dbg_values > 1);
801                         $av_pending = 'V';
802
803                 } elsif ($cur =~ /^($Ident|$Constant)/o) {
804                         print "IDENT($1)\n" if ($dbg_values > 1);
805                         $type = 'V';
806
807                 } elsif ($cur =~ /^($Assignment)/o) {
808                         print "ASSIGN($1)\n" if ($dbg_values > 1);
809                         $type = 'N';
810
811                 } elsif ($cur =~/^(;|{|})/) {
812                         print "END($1)\n" if ($dbg_values > 1);
813                         $type = 'E';
814
815                 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
816                         print "CLOSE($1)\n" if ($dbg_values > 1);
817                         $type = 'N';
818
819                 } elsif ($cur =~ /^($Operators)/o) {
820                         print "OP($1)\n" if ($dbg_values > 1);
821                         if ($1 ne '++' && $1 ne '--') {
822                                 $type = 'N';
823                         }
824
825                 } elsif ($cur =~ /(^.)/o) {
826                         print "C($1)\n" if ($dbg_values > 1);
827                 }
828                 if (defined $1) {
829                         $cur = substr($cur, length($1));
830                         $res .= $type x length($1);
831                 }
832         }
833
834         return $res;
835 }
836
837 sub possible {
838         my ($possible, $line) = @_;
839
840         #print "CHECK<$possible>\n";
841         if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
842             $possible ne 'goto' && $possible ne 'return' &&
843             $possible ne 'struct' && $possible ne 'enum' &&
844             $possible ne 'case' && $possible ne 'else' &&
845             $possible ne 'typedef') {
846                 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
847                 push(@typeList, $possible);
848                 build_types();
849         }
850 }
851
852 my $prefix = '';
853
854 sub report {
855         if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
856                 return 0;
857         }
858         my $line = $prefix . $_[0];
859
860         $line = (split('\n', $line))[0] . "\n" if ($terse);
861
862         push(our @report, $line);
863
864         return 1;
865 }
866 sub report_dump {
867         our @report;
868 }
869 sub ERROR {
870         if (report("ERROR: $_[0]\n")) {
871                 our $clean = 0;
872                 our $cnt_error++;
873         }
874 }
875 sub WARN {
876         if (report("WARNING: $_[0]\n")) {
877                 our $clean = 0;
878                 our $cnt_warn++;
879         }
880 }
881 sub CHK {
882         if ($check && report("CHECK: $_[0]\n")) {
883                 our $clean = 0;
884                 our $cnt_chk++;
885         }
886 }
887
888 sub process {
889         my $filename = shift;
890
891         my $linenr=0;
892         my $prevline="";
893         my $prevrawline="";
894         my $stashline="";
895         my $stashrawline="";
896
897         my $length;
898         my $indent;
899         my $previndent=0;
900         my $stashindent=0;
901
902         our $clean = 1;
903         my $signoff = 0;
904         my $is_patch = 0;
905
906         our @report = ();
907         our $cnt_lines = 0;
908         our $cnt_error = 0;
909         our $cnt_warn = 0;
910         our $cnt_chk = 0;
911
912         # Trace the real file/line as we go.
913         my $realfile = '';
914         my $realline = 0;
915         my $realcnt = 0;
916         my $here = '';
917         my $in_comment = 0;
918         my $comment_edge = 0;
919         my $first_line = 0;
920
921         my $prev_values = 'E';
922
923         # suppression flags
924         my %suppress_ifbraces;
925
926         # Pre-scan the patch sanitizing the lines.
927         # Pre-scan the patch looking for any __setup documentation.
928         #
929         my @setup_docs = ();
930         my $setup_docs = 0;
931
932         sanitise_line_reset();
933         my $line;
934         foreach my $rawline (@rawlines) {
935                 $linenr++;
936                 $line = $rawline;
937
938                 if ($rawline=~/^\+\+\+\s+(\S+)/) {
939                         $setup_docs = 0;
940                         if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
941                                 $setup_docs = 1;
942                         }
943                         #next;
944                 }
945                 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
946                         $realline=$1-1;
947                         if (defined $2) {
948                                 $realcnt=$3+1;
949                         } else {
950                                 $realcnt=1+1;
951                         }
952
953                         # Guestimate if this is a continuing comment.  Run
954                         # the context looking for a comment "edge".  If this
955                         # edge is a close comment then we must be in a comment
956                         # at context start.
957                         my $edge;
958                         for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) {
959                                 next if ($line =~ /^-/);
960                                 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
961                                 last if (defined $edge);
962                         }
963                         if (defined $edge && $edge eq '*/') {
964                                 $in_comment = 1;
965                         }
966
967                         # Guestimate if this is a continuing comment.  If this
968                         # is the start of a diff block and this line starts
969                         # ' *' then it is very likely a comment.
970                         if (!defined $edge &&
971                             $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
972                         {
973                                 $in_comment = 1;
974                         }
975
976                         ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
977                         sanitise_line_reset($in_comment);
978
979                 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
980                         # Standardise the strings and chars within the input to
981                         # simplify matching -- only bother with positive lines.
982                         $line = sanitise_line($rawline);
983                 }
984                 push(@lines, $line);
985
986                 if ($realcnt > 1) {
987                         $realcnt-- if ($line =~ /^(?:\+| |$)/);
988                 } else {
989                         $realcnt = 0;
990                 }
991
992                 #print "==>$rawline\n";
993                 #print "-->$line\n";
994
995                 if ($setup_docs && $line =~ /^\+/) {
996                         push(@setup_docs, $line);
997                 }
998         }
999
1000         $prefix = '';
1001
1002         $realcnt = 0;
1003         $linenr = 0;
1004         foreach my $line (@lines) {
1005                 $linenr++;
1006
1007                 my $rawline = $rawlines[$linenr - 1];
1008
1009 #extract the line range in the file after the patch is applied
1010                 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1011                         $is_patch = 1;
1012                         $first_line = $linenr + 1;
1013                         $realline=$1-1;
1014                         if (defined $2) {
1015                                 $realcnt=$3+1;
1016                         } else {
1017                                 $realcnt=1+1;
1018                         }
1019                         annotate_reset();
1020                         $prev_values = 'E';
1021
1022                         %suppress_ifbraces = ();
1023                         next;
1024
1025 # track the line number as we move through the hunk, note that
1026 # new versions of GNU diff omit the leading space on completely
1027 # blank context lines so we need to count that too.
1028                 } elsif ($line =~ /^( |\+|$)/) {
1029                         $realline++;
1030                         $realcnt-- if ($realcnt != 0);
1031
1032                         # Measure the line length and indent.
1033                         ($length, $indent) = line_stats($rawline);
1034
1035                         # Track the previous line.
1036                         ($prevline, $stashline) = ($stashline, $line);
1037                         ($previndent, $stashindent) = ($stashindent, $indent);
1038                         ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1039
1040                         #warn "line<$line>\n";
1041
1042                 } elsif ($realcnt == 1) {
1043                         $realcnt--;
1044                 }
1045
1046 #make up the handle for any error we report on this line
1047                 $prefix = "$filename:$realline: " if ($emacs && $file);
1048                 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1049
1050                 $here = "#$linenr: " if (!$file);
1051                 $here = "#$realline: " if ($file);
1052
1053                 # extract the filename as it passes
1054                 if ($line=~/^\+\+\+\s+(\S+)/) {
1055                         $realfile = $1;
1056                         $realfile =~ s@^[^/]*/@@;
1057
1058                         if ($realfile =~ m@include/asm/@) {
1059                                 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1060                         }
1061                         next;
1062                 }
1063
1064                 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1065
1066                 my $hereline = "$here\n$rawline\n";
1067                 my $herecurr = "$here\n$rawline\n";
1068                 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1069
1070                 $cnt_lines++ if ($realcnt != 0);
1071
1072 #check the patch for a signoff:
1073                 if ($line =~ /^\s*signed-off-by:/i) {
1074                         # This is a signoff, if ugly, so do not double report.
1075                         $signoff++;
1076                         if (!($line =~ /^\s*Signed-off-by:/)) {
1077                                 WARN("Signed-off-by: is the preferred form\n" .
1078                                         $herecurr);
1079                         }
1080                         if ($line =~ /^\s*signed-off-by:\S/i) {
1081                                 WARN("space required after Signed-off-by:\n" .
1082                                         $herecurr);
1083                         }
1084                 }
1085
1086 # Check for wrappage within a valid hunk of the file
1087                 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1088                         ERROR("patch seems to be corrupt (line wrapped?)\n" .
1089                                 $herecurr) if (!$emitted_corrupt++);
1090                 }
1091
1092 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1093                 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1094                     $rawline !~ m/^$UTF8*$/) {
1095                         my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1096
1097                         my $blank = copy_spacing($rawline);
1098                         my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1099                         my $hereptr = "$hereline$ptr\n";
1100
1101                         ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1102                 }
1103
1104 #ignore lines being removed
1105                 if ($line=~/^-/) {next;}
1106
1107 # check we are in a valid source file if not then ignore this hunk
1108                 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1109
1110 #trailing whitespace
1111                 if ($line =~ /^\+.*\015/) {
1112                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1113                         ERROR("DOS line endings\n" . $herevet);
1114
1115                 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1116                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1117                         ERROR("trailing whitespace\n" . $herevet);
1118                 }
1119 #80 column limit
1120                 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
1121                         WARN("line over 80 characters\n" . $herecurr);
1122                 }
1123
1124 # check for adding lines without a newline.
1125                 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1126                         WARN("adding a line without newline at end of file\n" . $herecurr);
1127                 }
1128
1129 # check we are in a valid source file *.[hc] if not then ignore this hunk
1130                 next if ($realfile !~ /\.[hc]$/);
1131
1132 # at the beginning of a line any tabs must come first and anything
1133 # more than 8 must use tabs.
1134                 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1135                     $rawline =~ /^\+\s*        \s*/) {
1136                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1137                         ERROR("code indent should use tabs where possible\n" . $herevet);
1138                 }
1139
1140 # check for RCS/CVS revision markers
1141                 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1142                         WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1143                 }
1144
1145 # Check for potential 'bare' types
1146                 my ($stat, $cond);
1147                 if ($realcnt && $line =~ /.\s*\S/) {
1148                         ($stat, $cond) = ctx_statement_block($linenr,
1149                                                                 $realcnt, 0);
1150                         $stat =~ s/\n./\n /g;
1151                         $cond =~ s/\n./\n /g;
1152
1153                         my $s = $stat;
1154                         $s =~ s/{.*$//s;
1155
1156                         # Ignore goto labels.
1157                         if ($s =~ /$Ident:\*$/s) {
1158
1159                         # Ignore functions being called
1160                         } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1161
1162                         # definitions in global scope can only start with types
1163                         } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
1164                                 possible($1, $s);
1165
1166                         # declarations always start with types
1167                         } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/s) {
1168                                 possible($1, $s);
1169                         }
1170
1171                         # any (foo ... *) is a pointer cast, and foo is a type
1172                         while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
1173                                 possible($1, $s);
1174                         }
1175
1176                         # Check for any sort of function declaration.
1177                         # int foo(something bar, other baz);
1178                         # void (*store_gdt)(x86_descr_ptr *);
1179                         if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1180                                 my ($name_len) = length($1);
1181
1182                                 my $ctx = $s;
1183                                 substr($ctx, 0, $name_len + 1, '');
1184                                 $ctx =~ s/\)[^\)]*$//;
1185
1186                                 for my $arg (split(/\s*,\s*/, $ctx)) {
1187                                         if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1188
1189                                                 possible($1, $s);
1190                                         }
1191                                 }
1192                         }
1193
1194                 }
1195
1196 #
1197 # Checks which may be anchored in the context.
1198 #
1199
1200 # Check for switch () and associated case and default
1201 # statements should be at the same indent.
1202                 if ($line=~/\bswitch\s*\(.*\)/) {
1203                         my $err = '';
1204                         my $sep = '';
1205                         my @ctx = ctx_block_outer($linenr, $realcnt);
1206                         shift(@ctx);
1207                         for my $ctx (@ctx) {
1208                                 my ($clen, $cindent) = line_stats($ctx);
1209                                 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1210                                                         $indent != $cindent) {
1211                                         $err .= "$sep$ctx\n";
1212                                         $sep = '';
1213                                 } else {
1214                                         $sep = "[...]\n";
1215                                 }
1216                         }
1217                         if ($err ne '') {
1218                                 ERROR("switch and case should be at the same indent\n$hereline$err");
1219                         }
1220                 }
1221
1222 # if/while/etc brace do not go on next line, unless defining a do while loop,
1223 # or if that brace on the next line is for something else
1224                 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1225                         my $pre_ctx = "$1$2";
1226
1227                         my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1228                         my $ctx_ln = $linenr + $#ctx + 1;
1229                         my $ctx_cnt = $realcnt - $#ctx - 1;
1230                         my $ctx = join("\n", @ctx);
1231
1232                         ##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1233
1234                         # Skip over any removed lines in the context following statement.
1235                         while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
1236                                 $ctx_ln++;
1237                         }
1238                         ##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1239
1240                         if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1241                                 ERROR("that open brace { should be on the previous line\n" .
1242                                         "$here\n$ctx\n$lines[$ctx_ln - 1]");
1243                         }
1244                         if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1245                             $ctx =~ /\)\s*\;\s*$/ &&
1246                             defined $lines[$ctx_ln - 1])
1247                         {
1248                                 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1249                                 if ($nindent > $indent) {
1250                                         WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1251                                                 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1252                                 }
1253                         }
1254                 }
1255
1256                 # Track the 'values' across context and added lines.
1257                 my $opline = $line; $opline =~ s/^./ /;
1258                 my $curr_values = annotate_values($opline . "\n", $prev_values);
1259                 $curr_values = $prev_values . $curr_values;
1260                 if ($dbg_values) {
1261                         my $outline = $opline; $outline =~ s/\t/ /g;
1262                         print "$linenr > .$outline\n";
1263                         print "$linenr > $curr_values\n";
1264                 }
1265                 $prev_values = substr($curr_values, -1);
1266
1267 #ignore lines not being added
1268                 if ($line=~/^[^\+]/) {next;}
1269
1270 # TEST: allow direct testing of the type matcher.
1271                 if ($tst_type && $line =~ /^.$Declare$/) {
1272                         ERROR("TEST: is type $Declare\n" . $herecurr);
1273                         next;
1274                 }
1275
1276 # check for initialisation to aggregates open brace on the next line
1277                 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1278                     $line =~ /^.\s*{/) {
1279                         ERROR("that open brace { should be on the previous line\n" . $hereprev);
1280                 }
1281
1282 #
1283 # Checks which are anchored on the added line.
1284 #
1285
1286 # check for malformed paths in #include statements (uses RAW line)
1287                 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1288                         my $path = $1;
1289                         if ($path =~ m{//}) {
1290                                 ERROR("malformed #include filename\n" .
1291                                         $herecurr);
1292                         }
1293                 }
1294
1295 # no C99 // comments
1296                 if ($line =~ m{//}) {
1297                         ERROR("do not use C99 // comments\n" . $herecurr);
1298                 }
1299                 # Remove C99 comments.
1300                 $line =~ s@//.*@@;
1301                 $opline =~ s@//.*@@;
1302
1303 #EXPORT_SYMBOL should immediately follow its function closing }.
1304                 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1305                     ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1306                         my $name = $1;
1307                         if (($prevline !~ /^}/) &&
1308                            ($prevline !~ /^\+}/) &&
1309                            ($prevline !~ /^ }/) &&
1310                            ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1311                            ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1312                            ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
1313                            ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1314                                 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1315                         }
1316                 }
1317
1318 # check for external initialisers.
1319                 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL|false)\s*;/) {
1320                         ERROR("do not initialise externals to 0 or NULL\n" .
1321                                 $herecurr);
1322                 }
1323 # check for static initialisers.
1324                 if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1325                         ERROR("do not initialise statics to 0 or NULL\n" .
1326                                 $herecurr);
1327                 }
1328
1329 # check for new typedefs, only function parameters and sparse annotations
1330 # make sense.
1331                 if ($line =~ /\btypedef\s/ &&
1332                     $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1333                     $line !~ /\b__bitwise(?:__|)\b/) {
1334                         WARN("do not add new typedefs\n" . $herecurr);
1335                 }
1336
1337 # * goes on variable not on type
1338                 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1339                         ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1340                                 $herecurr);
1341
1342                 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1343                         ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1344                                 $herecurr);
1345
1346                 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1347                         ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1348                                 $herecurr);
1349
1350                 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1351                         ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1352                                 $herecurr);
1353                 }
1354
1355 # # no BUG() or BUG_ON()
1356 #               if ($line =~ /\b(BUG|BUG_ON)\b/) {
1357 #                       print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1358 #                       print "$herecurr";
1359 #                       $clean = 0;
1360 #               }
1361
1362                 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1363                         WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1364                 }
1365
1366 # printk should use KERN_* levels.  Note that follow on printk's on the
1367 # same line do not need a level, so we use the current block context
1368 # to try and find and validate the current printk.  In summary the current
1369 # printk includes all preceeding printk's which have no newline on the end.
1370 # we assume the first bad printk is the one to report.
1371                 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1372                         my $ok = 0;
1373                         for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1374                                 #print "CHECK<$lines[$ln - 1]\n";
1375                                 # we have a preceeding printk if it ends
1376                                 # with "\n" ignore it, else it is to blame
1377                                 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1378                                         if ($rawlines[$ln - 1] !~ m{\\n"}) {
1379                                                 $ok = 1;
1380                                         }
1381                                         last;
1382                                 }
1383                         }
1384                         if ($ok == 0) {
1385                                 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1386                         }
1387                 }
1388
1389 # function brace can't be on same line, except for #defines of do while,
1390 # or if closed on same line
1391                 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
1392                     !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1393                         ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1394                 }
1395
1396 # open braces for enum, union and struct go on the same line.
1397                 if ($line =~ /^.\s*{/ &&
1398                     $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1399                         ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1400                 }
1401
1402 # check for spaces between functions and their parentheses.
1403                 while ($line =~ /($Ident)\s+\(/g) {
1404                         my $name = $1;
1405                         my $ctx_before = substr($line, 0, $-[1]);
1406                         my $ctx = "$ctx_before$name";
1407
1408                         # Ignore those directives where spaces _are_ permitted.
1409                         if ($name =~ /^(?:
1410                                 if|for|while|switch|return|case|
1411                                 volatile|__volatile__|
1412                                 __attribute__|format|__extension__|
1413                                 asm|__asm__)$/x)
1414                         {
1415
1416                         # cpp #define statements have non-optional spaces, ie
1417                         # if there is a space between the name and the open
1418                         # parenthesis it is simply not a parameter group.
1419                         } elsif ($ctx_before =~ /^.\#\s*define\s*$/) {
1420
1421                         # cpp #elif statement condition may start with a (
1422                         } elsif ($ctx =~ /^.\#\s*elif\s*$/) {
1423
1424                         # If this whole things ends with a type its most
1425                         # likely a typedef for a function.
1426                         } elsif ($ctx =~ /$Type$/) {
1427
1428                         } else {
1429                                 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1430                         }
1431                 }
1432 # Check operator spacing.
1433                 if (!($line=~/\#\s*include/)) {
1434                         my $ops = qr{
1435                                 <<=|>>=|<=|>=|==|!=|
1436                                 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1437                                 =>|->|<<|>>|<|>|=|!|~|
1438                                 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1439                         }x;
1440                         my @elements = split(/($ops|;)/, $opline);
1441                         my $off = 0;
1442
1443                         my $blank = copy_spacing($opline);
1444
1445                         for (my $n = 0; $n < $#elements; $n += 2) {
1446                                 $off += length($elements[$n]);
1447
1448                                 # Pick up the preceeding and succeeding characters.
1449                                 my $ca = substr($opline, 0, $off);
1450                                 my $cc = '';
1451                                 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1452                                         $cc = substr($opline, $off + length($elements[$n + 1]));
1453                                 }
1454                                 my $cb = "$ca$;$cc";
1455
1456                                 my $a = '';
1457                                 $a = 'V' if ($elements[$n] ne '');
1458                                 $a = 'W' if ($elements[$n] =~ /\s$/);
1459                                 $a = 'C' if ($elements[$n] =~ /$;$/);
1460                                 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1461                                 $a = 'O' if ($elements[$n] eq '');
1462                                 $a = 'E' if ($ca =~ /^\s*$/);
1463
1464                                 my $op = $elements[$n + 1];
1465
1466                                 my $c = '';
1467                                 if (defined $elements[$n + 2]) {
1468                                         $c = 'V' if ($elements[$n + 2] ne '');
1469                                         $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1470                                         $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1471                                         $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1472                                         $c = 'O' if ($elements[$n + 2] eq '');
1473                                         $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
1474                                 } else {
1475                                         $c = 'E';
1476                                 }
1477
1478                                 my $ctx = "${a}x${c}";
1479
1480                                 my $at = "(ctx:$ctx)";
1481
1482                                 my $ptr = substr($blank, 0, $off) . "^";
1483                                 my $hereptr = "$hereline$ptr\n";
1484
1485                                 # Classify operators into binary, unary, or
1486                                 # definitions (* only) where they have more
1487                                 # than one mode.
1488                                 my $op_type = substr($curr_values, $off + 1, 1);
1489                                 my $op_left = substr($curr_values, $off, 1);
1490                                 my $is_unary;
1491                                 if ($op_type eq 'T') {
1492                                         $is_unary = 2;
1493                                 } elsif ($op_left eq 'V') {
1494                                         $is_unary = 0;
1495                                 } else {
1496                                         $is_unary = 1;
1497                                 }
1498                                 #if ($op eq '-' || $op eq '&' || $op eq '*') {
1499                                 #       print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
1500                                 #}
1501
1502                                 # Ignore operators passed as parameters.
1503                                 if ($op_type ne 'V' &&
1504                                     $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1505
1506 #                               # Ignore comments
1507 #                               } elsif ($op =~ /^$;+$/) {
1508
1509                                 # ; should have either the end of line or a space or \ after it
1510                                 } elsif ($op eq ';') {
1511                                         if ($ctx !~ /.x[WEBC]/ &&
1512                                             $cc !~ /^\\/ && $cc !~ /^;/) {
1513                                                 ERROR("space required after that '$op' $at\n" . $hereptr);
1514                                         }
1515
1516                                 # // is a comment
1517                                 } elsif ($op eq '//') {
1518
1519                                 # -> should have no spaces
1520                                 } elsif ($op eq '->') {
1521                                         if ($ctx =~ /Wx.|.xW/) {
1522                                                 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
1523                                         }
1524
1525                                 # , must have a space on the right.
1526                                 } elsif ($op eq ',') {
1527                                         if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1528                                                 ERROR("space required after that '$op' $at\n" . $hereptr);
1529                                         }
1530
1531                                 # '*' as part of a type definition -- reported already.
1532                                 } elsif ($op eq '*' && $is_unary == 2) {
1533                                         #warn "'*' is part of type\n";
1534
1535                                 # unary operators should have a space before and
1536                                 # none after.  May be left adjacent to another
1537                                 # unary operator, or a cast
1538                                 } elsif ($op eq '!' || $op eq '~' ||
1539                                          ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1540                                         if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1541                                                 ERROR("space required before that '$op' $at\n" . $hereptr);
1542                                         }
1543                                         if ($op  eq '*' && $cc =~/\s*const\b/) {
1544                                                 # A unary '*' may be const
1545
1546                                         } elsif ($ctx =~ /.xW/) {
1547                                                 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1548                                         }
1549
1550                                 # unary ++ and unary -- are allowed no space on one side.
1551                                 } elsif ($op eq '++' or $op eq '--') {
1552                                         if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1553                                                 ERROR("space required one side of that '$op' $at\n" . $hereptr);
1554                                         }
1555                                         if ($ctx =~ /Wx[BE]/ ||
1556                                             ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1557                                                 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1558                                         }
1559                                         if ($ctx =~ /ExW/) {
1560                                                 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1561                                         }
1562
1563
1564                                 # << and >> may either have or not have spaces both sides
1565                                 } elsif ($op eq '<<' or $op eq '>>' or
1566                                          $op eq '&' or $op eq '^' or $op eq '|' or
1567                                          $op eq '+' or $op eq '-' or
1568                                          $op eq '*' or $op eq '/' or
1569                                          $op eq '%')
1570                                 {
1571                                         if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1572                                                 ERROR("need consistent spacing around '$op' $at\n" .
1573                                                         $hereptr);
1574                                         }
1575
1576                                 # All the others need spaces both sides.
1577                                 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1578                                         # Ignore email addresses <foo@bar>
1579                                         if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1580                                             !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1581                                                 ERROR("spaces required around that '$op' $at\n" . $hereptr);
1582                                         }
1583                                 }
1584                                 $off += length($elements[$n + 1]);
1585                         }
1586                 }
1587
1588 # check for multiple assignments
1589                 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1590                         CHK("multiple assignments should be avoided\n" . $herecurr);
1591                 }
1592
1593 ## # check for multiple declarations, allowing for a function declaration
1594 ## # continuation.
1595 ##              if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1596 ##                  $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1597 ##
1598 ##                      # Remove any bracketed sections to ensure we do not
1599 ##                      # falsly report the parameters of functions.
1600 ##                      my $ln = $line;
1601 ##                      while ($ln =~ s/\([^\(\)]*\)//g) {
1602 ##                      }
1603 ##                      if ($ln =~ /,/) {
1604 ##                              WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1605 ##                      }
1606 ##              }
1607
1608 #need space before brace following if, while, etc
1609                 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1610                     $line =~ /do{/) {
1611                         ERROR("space required before the open brace '{'\n" . $herecurr);
1612                 }
1613
1614 # closing brace should have a space following it when it has anything
1615 # on the line
1616                 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1617                         ERROR("space required after that close brace '}'\n" . $herecurr);
1618                 }
1619
1620 # check spacing on square brackets
1621                 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1622                         ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
1623                 }
1624                 if ($line =~ /\s\]/) {
1625                         ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
1626                 }
1627
1628 # check spacing on paretheses
1629                 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1630                     $line !~ /for\s*\(\s+;/) {
1631                         ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
1632                 }
1633                 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1634                     $line !~ /for\s*\(.*;\s+\)/) {
1635                         ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
1636                 }
1637
1638 #goto labels aren't indented, allow a single space however
1639                 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
1640                    !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1641                         WARN("labels should not be indented\n" . $herecurr);
1642                 }
1643
1644 # Need a space before open parenthesis after if, while etc
1645                 if ($line=~/\b(if|while|for|switch)\(/) {
1646                         ERROR("space required before the open parenthesis '('\n" . $herecurr);
1647                 }
1648
1649 # Check for illegal assignment in if conditional.
1650                 if ($line =~ /\bif\s*\(/) {
1651                         my ($s, $c) = ($stat, $cond);
1652
1653                         if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1654                                 ERROR("do not use assignment in if condition\n" . $herecurr);
1655                         }
1656
1657                         # Find out what is on the end of the line after the
1658                         # conditional.
1659                         substr($s, 0, length($c), '');
1660                         $s =~ s/\n.*//g;
1661                         $s =~ s/$;//g;  # Remove any comments
1662                         if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1663                             $c !~ /^.\#\s*if/)
1664                         {
1665                                 ERROR("trailing statements should be on next line\n" . $herecurr);
1666                         }
1667                 }
1668
1669 # Check for bitwise tests written as boolean
1670                 if ($line =~ /
1671                         (?:
1672                                 (?:\[|\(|\&\&|\|\|)
1673                                 \s*0[xX][0-9]+\s*
1674                                 (?:\&\&|\|\|)
1675                         |
1676                                 (?:\&\&|\|\|)
1677                                 \s*0[xX][0-9]+\s*
1678                                 (?:\&\&|\|\||\)|\])
1679                         )/x)
1680                 {
1681                         WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1682                 }
1683
1684 # if and else should not have general statements after it
1685                 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1686                         my $s = $1;
1687                         $s =~ s/$;//g;  # Remove any comments
1688                         if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1689                                 ERROR("trailing statements should be on next line\n" . $herecurr);
1690                         }
1691                 }
1692
1693                 # Check for }<nl>else {, these must be at the same
1694                 # indent level to be relevant to each other.
1695                 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1696                                                 $previndent == $indent) {
1697                         ERROR("else should follow close brace '}'\n" . $hereprev);
1698                 }
1699
1700                 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1701                                                 $previndent == $indent) {
1702                         my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1703
1704                         # Find out what is on the end of the line after the
1705                         # conditional.
1706                         substr($s, 0, length($c), '');
1707                         $s =~ s/\n.*//g;
1708
1709                         if ($s =~ /^\s*;/) {
1710                                 ERROR("while should follow close brace '}'\n" . $hereprev);
1711                         }
1712                 }
1713
1714 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
1715 #               if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1716 #                   print "No studly caps, use _\n";
1717 #                   print "$herecurr";
1718 #                   $clean = 0;
1719 #               }
1720
1721 #no spaces allowed after \ in define
1722                 if ($line=~/\#define.*\\\s$/) {
1723                         WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
1724                 }
1725
1726 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1727                 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
1728                         my $checkfile = "$root/include/linux/$1.h";
1729                         if (-f $checkfile && $1 ne 'irq') {
1730                                 WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1731                                         $herecurr);
1732                         }
1733                 }
1734
1735 # multi-statement macros should be enclosed in a do while loop, grab the
1736 # first statement and ensure its the whole macro if its not enclosed
1737 # in a known good container
1738                 if ($prevline =~ /\#define.*\\/ &&
1739                    $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1740                    $line !~ /(?:do\s+{|\(\{|\{)/ &&
1741                    $line !~ /^.\s*$Declare\s/) {
1742                         # Grab the first statement, if that is the entire macro
1743                         # its ok.  This may start either on the #define line
1744                         # or the one below.
1745                         my $ln = $linenr;
1746                         my $cnt = $realcnt;
1747                         my $off = 0;
1748
1749                         # If the macro starts on the define line start
1750                         # grabbing the statement after the identifier
1751                         $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1752                         ##print "1<$1> 2<$2>\n";
1753                         if (defined $2 && $2 ne '') {
1754                                 $off = length($1);
1755                                 $ln--;
1756                                 $cnt++;
1757                                 while ($lines[$ln - 1] =~ /^-/) {
1758                                         $ln--;
1759                                         $cnt++;
1760                                 }
1761                         }
1762                         my @ctx = ctx_statement($ln, $cnt, $off);
1763                         my $ctx_ln = $ln + $#ctx + 1;
1764                         my $ctx = join("\n", @ctx);
1765
1766                         # Pull in any empty extension lines.
1767                         while ($ctx =~ /\\$/ &&
1768                                $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1769                                 $ctx .= $lines[$ctx_ln - 1];
1770                                 $ctx_ln++;
1771                         }
1772
1773                         if ($ctx =~ /\\$/) {
1774                                 if ($ctx =~ /;/) {
1775                                         ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1776                                 } else {
1777                                         ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1778                                 }
1779                         }
1780                 }
1781
1782 # check for redundant bracing round if etc
1783                 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1784                         my ($level, $endln, @chunks) =
1785                                 ctx_statement_full($linenr, $realcnt, 1);
1786                         #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1787                         #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1788                         if ($#chunks > 0 && $level == 0) {
1789                                 my $allowed = 0;
1790                                 my $seen = 0;
1791                                 my $herectx = $here . "\n";
1792                                 my $ln = $linenr - 1;
1793                                 for my $chunk (@chunks) {
1794                                         my ($cond, $block) = @{$chunk};
1795
1796                                         # If the condition carries leading newlines, then count those as offsets.
1797                                         my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1798                                         my $offset = statement_rawlines($whitespace) - 1;
1799
1800                                         #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1801
1802                                         # We have looked at and allowed this specific line.
1803                                         $suppress_ifbraces{$ln + $offset} = 1;
1804
1805                                         $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
1806                                         $ln += statement_rawlines($block) - 1;
1807
1808                                         substr($block, 0, length($cond), '');
1809
1810                                         $seen++ if ($block =~ /^\s*{/);
1811
1812                                         #print "cond<$cond> block<$block> allowed<$allowed>\n";
1813                                         if (statement_lines($cond) > 1) {
1814                                                 #print "APW: ALLOWED: cond<$cond>\n";
1815                                                 $allowed = 1;
1816                                         }
1817                                         if ($block =~/\b(?:if|for|while)\b/) {
1818                                                 #print "APW: ALLOWED: block<$block>\n";
1819                                                 $allowed = 1;
1820                                         }
1821                                         if (statement_block_size($block) > 1) {
1822                                                 #print "APW: ALLOWED: lines block<$block>\n";
1823                                                 $allowed = 1;
1824                                         }
1825                                 }
1826                                 if ($seen && !$allowed) {
1827                                         WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
1828                                 }
1829                         }
1830                 }
1831                 if (!defined $suppress_ifbraces{$linenr - 1} &&
1832                                         $line =~ /\b(if|while|for|else)\b/) {
1833                         my $allowed = 0;
1834
1835                         # Check the pre-context.
1836                         if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1837                                 #print "APW: ALLOWED: pre<$1>\n";
1838                                 $allowed = 1;
1839                         }
1840
1841                         my ($level, $endln, @chunks) =
1842                                 ctx_statement_full($linenr, $realcnt, $-[0]);
1843
1844                         # Check the condition.
1845                         my ($cond, $block) = @{$chunks[0]};
1846                         #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
1847                         if (defined $cond) {
1848                                 substr($block, 0, length($cond), '');
1849                         }
1850                         if (statement_lines($cond) > 1) {
1851                                 #print "APW: ALLOWED: cond<$cond>\n";
1852                                 $allowed = 1;
1853                         }
1854                         if ($block =~/\b(?:if|for|while)\b/) {
1855                                 #print "APW: ALLOWED: block<$block>\n";
1856                                 $allowed = 1;
1857                         }
1858                         if (statement_block_size($block) > 1) {
1859                                 #print "APW: ALLOWED: lines block<$block>\n";
1860                                 $allowed = 1;
1861                         }
1862                         # Check the post-context.
1863                         if (defined $chunks[1]) {
1864                                 my ($cond, $block) = @{$chunks[1]};
1865                                 if (defined $cond) {
1866                                         substr($block, 0, length($cond), '');
1867                                 }
1868                                 if ($block =~ /^\s*\{/) {
1869                                         #print "APW: ALLOWED: chunk-1 block<$block>\n";
1870                                         $allowed = 1;
1871                                 }
1872                         }
1873                         if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1874                                 my $herectx = $here . "\n";;
1875                                 my $end = $linenr + statement_rawlines($block) - 1;
1876
1877                                 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1878                                         $herectx .= $rawlines[$ln] . "\n";;
1879                                 }
1880
1881                                 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
1882                         }
1883                 }
1884
1885 # don't include deprecated include files (uses RAW line)
1886                 for my $inc (@dep_includes) {
1887                         if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
1888                                 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1889                         }
1890                 }
1891
1892 # don't use deprecated functions
1893                 for my $func (@dep_functions) {
1894                         if ($line =~ /\b$func\b/) {
1895                                 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1896                         }
1897                 }
1898
1899 # no volatiles please
1900                 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1901                 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
1902                         WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
1903                 }
1904
1905 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1906                 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1907                         ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1908                 }
1909
1910 # warn about #if 0
1911                 if ($line =~ /^.#\s*if\s+0\b/) {
1912                         CHK("if this code is redundant consider removing it\n" .
1913                                 $herecurr);
1914                 }
1915
1916 # check for needless kfree() checks
1917                 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1918                         my $expr = $1;
1919                         if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1920                                 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1921                         }
1922                 }
1923 # check for needless usb_free_urb() checks
1924                 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1925                         my $expr = $1;
1926                         if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
1927                                 WARN("usb_free_urb(NULL) is safe this check is probabally not required\n" . $hereprev);
1928                         }
1929                 }
1930
1931 # warn about #ifdefs in C files
1932 #               if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1933 #                       print "#ifdef in C files should be avoided\n";
1934 #                       print "$herecurr";
1935 #                       $clean = 0;
1936 #               }
1937
1938 # warn about spacing in #ifdefs
1939                 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1940                         ERROR("exactly one space required after that #$1\n" . $herecurr);
1941                 }
1942
1943 # check for spinlock_t definitions without a comment.
1944                 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
1945                     $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
1946                         my $which = $1;
1947                         if (!ctx_has_comment($first_line, $linenr)) {
1948                                 CHK("$1 definition without comment\n" . $herecurr);
1949                         }
1950                 }
1951 # check for memory barriers without a comment.
1952                 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1953                         if (!ctx_has_comment($first_line, $linenr)) {
1954                                 CHK("memory barrier without comment\n" . $herecurr);
1955                         }
1956                 }
1957 # check of hardware specific defines
1958                 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1959                         CHK("architecture specific defines should be avoided\n" .  $herecurr);
1960                 }
1961
1962 # check the location of the inline attribute, that it is between
1963 # storage class and type.
1964                 if ($line =~ /\b$Type\s+$Inline\b/ ||
1965                     $line =~ /\b$Inline\s+$Storage\b/) {
1966                         ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1967                 }
1968
1969 # Check for __inline__ and __inline, prefer inline
1970                 if ($line =~ /\b(__inline__|__inline)\b/) {
1971                         WARN("plain inline is preferred over $1\n" . $herecurr);
1972                 }
1973
1974 # check for new externs in .c files.
1975                 if ($realfile =~ /\.c$/ && defined $stat &&
1976                     $stat =~ /^.\s*(?:extern\s+)?$Type\s+$Ident(\s*)\(/s)
1977                 {
1978                         my $paren_space = $1;
1979
1980                         my $s = $stat;
1981                         if (defined $cond) {
1982                                 substr($s, 0, length($cond), '');
1983                         }
1984                         if ($s =~ /^\s*;/) {
1985                                 WARN("externs should be avoided in .c files\n" .  $herecurr);
1986                         }
1987
1988                         if ($paren_space =~ /\n/) {
1989                                 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
1990                         }
1991
1992                 } elsif ($realfile =~ /\.c$/ && defined $stat &&
1993                     $stat =~ /^.\s*extern\s+/)
1994                 {
1995                         WARN("externs should be avoided in .c files\n" .  $herecurr);
1996                 }
1997
1998 # checks for new __setup's
1999                 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2000                         my $name = $1;
2001
2002                         if (!grep(/$name/, @setup_docs)) {
2003                                 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2004                         }
2005                 }
2006
2007 # check for pointless casting of kmalloc return
2008                 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2009                         WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2010                 }
2011
2012 # check for gcc specific __FUNCTION__
2013                 if ($line =~ /__FUNCTION__/) {
2014                         WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
2015                 }
2016
2017 # check for semaphores used as mutexes
2018                 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2019                         WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2020                 }
2021 # check for semaphores used as mutexes
2022                 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2023                         WARN("consider using a completion\n" . $herecurr);
2024                 }
2025 # recommend strict_strto* over simple_strto*
2026                 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2027                         WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2028                 }
2029
2030 # use of NR_CPUS is usually wrong
2031 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2032                 if ($line =~ /\bNR_CPUS\b/ &&
2033                     $line !~ /^.#\s*if\b.*\bNR_CPUS\b/ &&
2034                     $line !~ /^.#\s*define\b.*\bNR_CPUS\b/ &&
2035                     $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2036                     $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2037                     $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2038                 {
2039                         WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2040                 }
2041
2042 # check for %L{u,d,i} in strings
2043                 my $string;
2044                 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2045                         $string = substr($rawline, $-[1], $+[1] - $-[1]);
2046                         if ($string =~ /(?<!%)%L[udi]/) {
2047                                 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2048                                 last;
2049                         }
2050                 }
2051         }
2052
2053         # If we have no input at all, then there is nothing to report on
2054         # so just keep quiet.
2055         if ($#rawlines == -1) {
2056                 exit(0);
2057         }
2058
2059         # In mailback mode only produce a report in the negative, for
2060         # things that appear to be patches.
2061         if ($mailback && ($clean == 1 || !$is_patch)) {
2062                 exit(0);
2063         }
2064
2065         # This is not a patch, and we are are in 'no-patch' mode so
2066         # just keep quiet.
2067         if (!$chk_patch && !$is_patch) {
2068                 exit(0);
2069         }
2070
2071         if (!$is_patch) {
2072                 ERROR("Does not appear to be a unified-diff format patch\n");
2073         }
2074         if ($is_patch && $chk_signoff && $signoff == 0) {
2075                 ERROR("Missing Signed-off-by: line(s)\n");
2076         }
2077
2078         print report_dump();
2079         if ($summary && !($clean == 1 && $quiet == 1)) {
2080                 print "$filename " if ($summary_file);
2081                 print "total: $cnt_error errors, $cnt_warn warnings, " .
2082                         (($check)? "$cnt_chk checks, " : "") .
2083                         "$cnt_lines lines checked\n";
2084                 print "\n" if ($quiet == 0);
2085         }
2086
2087         if ($clean == 1 && $quiet == 0) {
2088                 print "$vname has no obvious style problems and is ready for submission.\n"
2089         }
2090         if ($clean == 0 && $quiet == 0) {
2091                 print "$vname has style problems, please review.  If any of these errors\n";
2092                 print "are false positives report them to the maintainer, see\n";
2093                 print "CHECKPATCH in MAINTAINERS.\n";
2094         }
2095
2096         return $clean;
2097 }