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