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