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