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