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