checkpatch: ignore SI unit CamelCase variants like "_uV"
[cascardo/linux.git] / scripts / checkpatch.pl
1 #!/usr/bin/perl -w
2 # (c) 2001, Dave Jones. (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
7
8 use strict;
9
10 my $P = $0;
11 $P =~ s@.*/@@g;
12
13 my $V = '0.32';
14
15 use Getopt::Long qw(:config no_auto_abbrev);
16
17 my $quiet = 0;
18 my $tree = 1;
19 my $chk_signoff = 1;
20 my $chk_patch = 1;
21 my $tst_only;
22 my $emacs = 0;
23 my $terse = 0;
24 my $file = 0;
25 my $check = 0;
26 my $summary = 1;
27 my $mailback = 0;
28 my $summary_file = 0;
29 my $show_types = 0;
30 my $fix = 0;
31 my $root;
32 my %debug;
33 my %ignore_type = ();
34 my @ignore = ();
35 my $help = 0;
36 my $configuration_file = ".checkpatch.conf";
37 my $max_line_length = 80;
38
39 sub help {
40         my ($exitcode) = @_;
41
42         print << "EOM";
43 Usage: $P [OPTION]... [FILE]...
44 Version: $V
45
46 Options:
47   -q, --quiet                quiet
48   --no-tree                  run without a kernel tree
49   --no-signoff               do not check for 'Signed-off-by' line
50   --patch                    treat FILE as patchfile (default)
51   --emacs                    emacs compile window format
52   --terse                    one line per report
53   -f, --file                 treat FILE as regular source file
54   --subjective, --strict     enable more subjective tests
55   --ignore TYPE(,TYPE2...)   ignore various comma separated message types
56   --max-line-length=n        set the maximum line length, if exceeded, warn
57   --show-types               show the message "types" in the output
58   --root=PATH                PATH to the kernel tree root
59   --no-summary               suppress the per-file summary
60   --mailback                 only produce a report in case of warnings/errors
61   --summary-file             include the filename in summary
62   --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
63                              'values', 'possible', 'type', and 'attr' (default
64                              is all off)
65   --test-only=WORD           report only warnings/errors containing WORD
66                              literally
67   --fix                      EXPERIMENTAL - may create horrible results
68                              If correctable single-line errors exist, create
69                              "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
70                              with potential errors corrected to the preferred
71                              checkpatch style
72   -h, --help, --version      display this help and exit
73
74 When FILE is - read standard input.
75 EOM
76
77         exit($exitcode);
78 }
79
80 my $conf = which_conf($configuration_file);
81 if (-f $conf) {
82         my @conf_args;
83         open(my $conffile, '<', "$conf")
84             or warn "$P: Can't find a readable $configuration_file file $!\n";
85
86         while (<$conffile>) {
87                 my $line = $_;
88
89                 $line =~ s/\s*\n?$//g;
90                 $line =~ s/^\s*//g;
91                 $line =~ s/\s+/ /g;
92
93                 next if ($line =~ m/^\s*#/);
94                 next if ($line =~ m/^\s*$/);
95
96                 my @words = split(" ", $line);
97                 foreach my $word (@words) {
98                         last if ($word =~ m/^#/);
99                         push (@conf_args, $word);
100                 }
101         }
102         close($conffile);
103         unshift(@ARGV, @conf_args) if @conf_args;
104 }
105
106 GetOptions(
107         'q|quiet+'      => \$quiet,
108         'tree!'         => \$tree,
109         'signoff!'      => \$chk_signoff,
110         'patch!'        => \$chk_patch,
111         'emacs!'        => \$emacs,
112         'terse!'        => \$terse,
113         'f|file!'       => \$file,
114         'subjective!'   => \$check,
115         'strict!'       => \$check,
116         'ignore=s'      => \@ignore,
117         'show-types!'   => \$show_types,
118         'max-line-length=i' => \$max_line_length,
119         'root=s'        => \$root,
120         'summary!'      => \$summary,
121         'mailback!'     => \$mailback,
122         'summary-file!' => \$summary_file,
123         'fix!'          => \$fix,
124         'debug=s'       => \%debug,
125         'test-only=s'   => \$tst_only,
126         'h|help'        => \$help,
127         'version'       => \$help
128 ) or help(1);
129
130 help(0) if ($help);
131
132 my $exit = 0;
133
134 if ($#ARGV < 0) {
135         print "$P: no input files\n";
136         exit(1);
137 }
138
139 @ignore = split(/,/, join(',',@ignore));
140 foreach my $word (@ignore) {
141         $word =~ s/\s*\n?$//g;
142         $word =~ s/^\s*//g;
143         $word =~ s/\s+/ /g;
144         $word =~ tr/[a-z]/[A-Z]/;
145
146         next if ($word =~ m/^\s*#/);
147         next if ($word =~ m/^\s*$/);
148
149         $ignore_type{$word}++;
150 }
151
152 my $dbg_values = 0;
153 my $dbg_possible = 0;
154 my $dbg_type = 0;
155 my $dbg_attr = 0;
156 for my $key (keys %debug) {
157         ## no critic
158         eval "\${dbg_$key} = '$debug{$key}';";
159         die "$@" if ($@);
160 }
161
162 my $rpt_cleaners = 0;
163
164 if ($terse) {
165         $emacs = 1;
166         $quiet++;
167 }
168
169 if ($tree) {
170         if (defined $root) {
171                 if (!top_of_kernel_tree($root)) {
172                         die "$P: $root: --root does not point at a valid tree\n";
173                 }
174         } else {
175                 if (top_of_kernel_tree('.')) {
176                         $root = '.';
177                 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
178                                                 top_of_kernel_tree($1)) {
179                         $root = $1;
180                 }
181         }
182
183         if (!defined $root) {
184                 print "Must be run from the top-level dir. of a kernel tree\n";
185                 exit(2);
186         }
187 }
188
189 my $emitted_corrupt = 0;
190
191 our $Ident      = qr{
192                         [A-Za-z_][A-Za-z\d_]*
193                         (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
194                 }x;
195 our $Storage    = qr{extern|static|asmlinkage};
196 our $Sparse     = qr{
197                         __user|
198                         __kernel|
199                         __force|
200                         __iomem|
201                         __must_check|
202                         __init_refok|
203                         __kprobes|
204                         __ref|
205                         __rcu
206                 }x;
207
208 # Notes to $Attribute:
209 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
210 our $Attribute  = qr{
211                         const|
212                         __percpu|
213                         __nocast|
214                         __safe|
215                         __bitwise__|
216                         __packed__|
217                         __packed2__|
218                         __naked|
219                         __maybe_unused|
220                         __always_unused|
221                         __noreturn|
222                         __used|
223                         __cold|
224                         __noclone|
225                         __deprecated|
226                         __read_mostly|
227                         __kprobes|
228                         __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
229                         ____cacheline_aligned|
230                         ____cacheline_aligned_in_smp|
231                         ____cacheline_internodealigned_in_smp|
232                         __weak
233                   }x;
234 our $Modifier;
235 our $Inline     = qr{inline|__always_inline|noinline};
236 our $Member     = qr{->$Ident|\.$Ident|\[[^]]*\]};
237 our $Lval       = qr{$Ident(?:$Member)*};
238
239 our $Int_type   = qr{(?i)llu|ull|ll|lu|ul|l|u};
240 our $Binary     = qr{(?i)0b[01]+$Int_type?};
241 our $Hex        = qr{(?i)0x[0-9a-f]+$Int_type?};
242 our $Int        = qr{[0-9]+$Int_type?};
243 our $Float_hex  = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
244 our $Float_dec  = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
245 our $Float_int  = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
246 our $Float      = qr{$Float_hex|$Float_dec|$Float_int};
247 our $Constant   = qr{$Float|$Binary|$Hex|$Int};
248 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
249 our $Compare    = qr{<=|>=|==|!=|<|>};
250 our $Arithmetic = qr{\+|-|\*|\/|%};
251 our $Operators  = qr{
252                         <=|>=|==|!=|
253                         =>|->|<<|>>|<|>|!|~|
254                         &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
255                   }x;
256
257 our $NonptrType;
258 our $Type;
259 our $Declare;
260
261 our $NON_ASCII_UTF8     = qr{
262         [\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
263         |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
264         | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
265         |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
266         |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
267         | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
268         |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
269 }x;
270
271 our $UTF8       = qr{
272         [\x09\x0A\x0D\x20-\x7E]              # ASCII
273         | $NON_ASCII_UTF8
274 }x;
275
276 our $typeTypedefs = qr{(?x:
277         (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
278         atomic_t
279 )};
280
281 our $logFunctions = qr{(?x:
282         printk(?:_ratelimited|_once|)|
283         [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
284         WARN(?:_RATELIMIT|_ONCE|)|
285         panic|
286         MODULE_[A-Z_]+
287 )};
288
289 our $signature_tags = qr{(?xi:
290         Signed-off-by:|
291         Acked-by:|
292         Tested-by:|
293         Reviewed-by:|
294         Reported-by:|
295         Suggested-by:|
296         To:|
297         Cc:
298 )};
299
300 our @typeList = (
301         qr{void},
302         qr{(?:unsigned\s+)?char},
303         qr{(?:unsigned\s+)?short},
304         qr{(?:unsigned\s+)?int},
305         qr{(?:unsigned\s+)?long},
306         qr{(?:unsigned\s+)?long\s+int},
307         qr{(?:unsigned\s+)?long\s+long},
308         qr{(?:unsigned\s+)?long\s+long\s+int},
309         qr{unsigned},
310         qr{float},
311         qr{double},
312         qr{bool},
313         qr{struct\s+$Ident},
314         qr{union\s+$Ident},
315         qr{enum\s+$Ident},
316         qr{${Ident}_t},
317         qr{${Ident}_handler},
318         qr{${Ident}_handler_fn},
319 );
320 our @modifierList = (
321         qr{fastcall},
322 );
323
324 our $allowed_asm_includes = qr{(?x:
325         irq|
326         memory
327 )};
328 # memory.h: ARM has a custom one
329
330 sub build_types {
331         my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
332         my $all = "(?x:  \n" . join("|\n  ", @typeList) . "\n)";
333         $Modifier       = qr{(?:$Attribute|$Sparse|$mods)};
334         $NonptrType     = qr{
335                         (?:$Modifier\s+|const\s+)*
336                         (?:
337                                 (?:typeof|__typeof__)\s*\([^\)]*\)|
338                                 (?:$typeTypedefs\b)|
339                                 (?:${all}\b)
340                         )
341                         (?:\s+$Modifier|\s+const)*
342                   }x;
343         $Type   = qr{
344                         $NonptrType
345                         (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
346                         (?:\s+$Inline|\s+$Modifier)*
347                   }x;
348         $Declare        = qr{(?:$Storage\s+)?$Type};
349 }
350 build_types();
351
352
353 our $Typecast   = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
354
355 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
356 # requires at least perl version v5.10.0
357 # Any use must be runtime checked with $^V
358
359 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
360 our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
361 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
362
363 sub deparenthesize {
364         my ($string) = @_;
365         return "" if (!defined($string));
366         $string =~ s@^\s*\(\s*@@g;
367         $string =~ s@\s*\)\s*$@@g;
368         $string =~ s@\s+@ @g;
369         return $string;
370 }
371
372 $chk_signoff = 0 if ($file);
373
374 my @rawlines = ();
375 my @lines = ();
376 my @fixed = ();
377 my $vname;
378 for my $filename (@ARGV) {
379         my $FILE;
380         if ($file) {
381                 open($FILE, '-|', "diff -u /dev/null $filename") ||
382                         die "$P: $filename: diff failed - $!\n";
383         } elsif ($filename eq '-') {
384                 open($FILE, '<&STDIN');
385         } else {
386                 open($FILE, '<', "$filename") ||
387                         die "$P: $filename: open failed - $!\n";
388         }
389         if ($filename eq '-') {
390                 $vname = 'Your patch';
391         } else {
392                 $vname = $filename;
393         }
394         while (<$FILE>) {
395                 chomp;
396                 push(@rawlines, $_);
397         }
398         close($FILE);
399         if (!process($filename)) {
400                 $exit = 1;
401         }
402         @rawlines = ();
403         @lines = ();
404         @fixed = ();
405 }
406
407 exit($exit);
408
409 sub top_of_kernel_tree {
410         my ($root) = @_;
411
412         my @tree_check = (
413                 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
414                 "README", "Documentation", "arch", "include", "drivers",
415                 "fs", "init", "ipc", "kernel", "lib", "scripts",
416         );
417
418         foreach my $check (@tree_check) {
419                 if (! -e $root . '/' . $check) {
420                         return 0;
421                 }
422         }
423         return 1;
424 }
425
426 sub parse_email {
427         my ($formatted_email) = @_;
428
429         my $name = "";
430         my $address = "";
431         my $comment = "";
432
433         if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
434                 $name = $1;
435                 $address = $2;
436                 $comment = $3 if defined $3;
437         } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
438                 $address = $1;
439                 $comment = $2 if defined $2;
440         } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
441                 $address = $1;
442                 $comment = $2 if defined $2;
443                 $formatted_email =~ s/$address.*$//;
444                 $name = $formatted_email;
445                 $name = trim($name);
446                 $name =~ s/^\"|\"$//g;
447                 # If there's a name left after stripping spaces and
448                 # leading quotes, and the address doesn't have both
449                 # leading and trailing angle brackets, the address
450                 # is invalid. ie:
451                 #   "joe smith joe@smith.com" bad
452                 #   "joe smith <joe@smith.com" bad
453                 if ($name ne "" && $address !~ /^<[^>]+>$/) {
454                         $name = "";
455                         $address = "";
456                         $comment = "";
457                 }
458         }
459
460         $name = trim($name);
461         $name =~ s/^\"|\"$//g;
462         $address = trim($address);
463         $address =~ s/^\<|\>$//g;
464
465         if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
466                 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
467                 $name = "\"$name\"";
468         }
469
470         return ($name, $address, $comment);
471 }
472
473 sub format_email {
474         my ($name, $address) = @_;
475
476         my $formatted_email;
477
478         $name = trim($name);
479         $name =~ s/^\"|\"$//g;
480         $address = trim($address);
481
482         if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
483                 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
484                 $name = "\"$name\"";
485         }
486
487         if ("$name" eq "") {
488                 $formatted_email = "$address";
489         } else {
490                 $formatted_email = "$name <$address>";
491         }
492
493         return $formatted_email;
494 }
495
496 sub which_conf {
497         my ($conf) = @_;
498
499         foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
500                 if (-e "$path/$conf") {
501                         return "$path/$conf";
502                 }
503         }
504
505         return "";
506 }
507
508 sub expand_tabs {
509         my ($str) = @_;
510
511         my $res = '';
512         my $n = 0;
513         for my $c (split(//, $str)) {
514                 if ($c eq "\t") {
515                         $res .= ' ';
516                         $n++;
517                         for (; ($n % 8) != 0; $n++) {
518                                 $res .= ' ';
519                         }
520                         next;
521                 }
522                 $res .= $c;
523                 $n++;
524         }
525
526         return $res;
527 }
528 sub copy_spacing {
529         (my $res = shift) =~ tr/\t/ /c;
530         return $res;
531 }
532
533 sub line_stats {
534         my ($line) = @_;
535
536         # Drop the diff line leader and expand tabs
537         $line =~ s/^.//;
538         $line = expand_tabs($line);
539
540         # Pick the indent from the front of the line.
541         my ($white) = ($line =~ /^(\s*)/);
542
543         return (length($line), length($white));
544 }
545
546 my $sanitise_quote = '';
547
548 sub sanitise_line_reset {
549         my ($in_comment) = @_;
550
551         if ($in_comment) {
552                 $sanitise_quote = '*/';
553         } else {
554                 $sanitise_quote = '';
555         }
556 }
557 sub sanitise_line {
558         my ($line) = @_;
559
560         my $res = '';
561         my $l = '';
562
563         my $qlen = 0;
564         my $off = 0;
565         my $c;
566
567         # Always copy over the diff marker.
568         $res = substr($line, 0, 1);
569
570         for ($off = 1; $off < length($line); $off++) {
571                 $c = substr($line, $off, 1);
572
573                 # Comments we are wacking completly including the begin
574                 # and end, all to $;.
575                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
576                         $sanitise_quote = '*/';
577
578                         substr($res, $off, 2, "$;$;");
579                         $off++;
580                         next;
581                 }
582                 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
583                         $sanitise_quote = '';
584                         substr($res, $off, 2, "$;$;");
585                         $off++;
586                         next;
587                 }
588                 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
589                         $sanitise_quote = '//';
590
591                         substr($res, $off, 2, $sanitise_quote);
592                         $off++;
593                         next;
594                 }
595
596                 # A \ in a string means ignore the next character.
597                 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
598                     $c eq "\\") {
599                         substr($res, $off, 2, 'XX');
600                         $off++;
601                         next;
602                 }
603                 # Regular quotes.
604                 if ($c eq "'" || $c eq '"') {
605                         if ($sanitise_quote eq '') {
606                                 $sanitise_quote = $c;
607
608                                 substr($res, $off, 1, $c);
609                                 next;
610                         } elsif ($sanitise_quote eq $c) {
611                                 $sanitise_quote = '';
612                         }
613                 }
614
615                 #print "c<$c> SQ<$sanitise_quote>\n";
616                 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
617                         substr($res, $off, 1, $;);
618                 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
619                         substr($res, $off, 1, $;);
620                 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
621                         substr($res, $off, 1, 'X');
622                 } else {
623                         substr($res, $off, 1, $c);
624                 }
625         }
626
627         if ($sanitise_quote eq '//') {
628                 $sanitise_quote = '';
629         }
630
631         # The pathname on a #include may be surrounded by '<' and '>'.
632         if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
633                 my $clean = 'X' x length($1);
634                 $res =~ s@\<.*\>@<$clean>@;
635
636         # The whole of a #error is a string.
637         } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
638                 my $clean = 'X' x length($1);
639                 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
640         }
641
642         return $res;
643 }
644
645 sub get_quoted_string {
646         my ($line, $rawline) = @_;
647
648         return "" if ($line !~ m/(\"[X]+\")/g);
649         return substr($rawline, $-[0], $+[0] - $-[0]);
650 }
651
652 sub ctx_statement_block {
653         my ($linenr, $remain, $off) = @_;
654         my $line = $linenr - 1;
655         my $blk = '';
656         my $soff = $off;
657         my $coff = $off - 1;
658         my $coff_set = 0;
659
660         my $loff = 0;
661
662         my $type = '';
663         my $level = 0;
664         my @stack = ();
665         my $p;
666         my $c;
667         my $len = 0;
668
669         my $remainder;
670         while (1) {
671                 @stack = (['', 0]) if ($#stack == -1);
672
673                 #warn "CSB: blk<$blk> remain<$remain>\n";
674                 # If we are about to drop off the end, pull in more
675                 # context.
676                 if ($off >= $len) {
677                         for (; $remain > 0; $line++) {
678                                 last if (!defined $lines[$line]);
679                                 next if ($lines[$line] =~ /^-/);
680                                 $remain--;
681                                 $loff = $len;
682                                 $blk .= $lines[$line] . "\n";
683                                 $len = length($blk);
684                                 $line++;
685                                 last;
686                         }
687                         # Bail if there is no further context.
688                         #warn "CSB: blk<$blk> off<$off> len<$len>\n";
689                         if ($off >= $len) {
690                                 last;
691                         }
692                         if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
693                                 $level++;
694                                 $type = '#';
695                         }
696                 }
697                 $p = $c;
698                 $c = substr($blk, $off, 1);
699                 $remainder = substr($blk, $off);
700
701                 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
702
703                 # Handle nested #if/#else.
704                 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
705                         push(@stack, [ $type, $level ]);
706                 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
707                         ($type, $level) = @{$stack[$#stack - 1]};
708                 } elsif ($remainder =~ /^#\s*endif\b/) {
709                         ($type, $level) = @{pop(@stack)};
710                 }
711
712                 # Statement ends at the ';' or a close '}' at the
713                 # outermost level.
714                 if ($level == 0 && $c eq ';') {
715                         last;
716                 }
717
718                 # An else is really a conditional as long as its not else if
719                 if ($level == 0 && $coff_set == 0 &&
720                                 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
721                                 $remainder =~ /^(else)(?:\s|{)/ &&
722                                 $remainder !~ /^else\s+if\b/) {
723                         $coff = $off + length($1) - 1;
724                         $coff_set = 1;
725                         #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
726                         #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
727                 }
728
729                 if (($type eq '' || $type eq '(') && $c eq '(') {
730                         $level++;
731                         $type = '(';
732                 }
733                 if ($type eq '(' && $c eq ')') {
734                         $level--;
735                         $type = ($level != 0)? '(' : '';
736
737                         if ($level == 0 && $coff < $soff) {
738                                 $coff = $off;
739                                 $coff_set = 1;
740                                 #warn "CSB: mark coff<$coff>\n";
741                         }
742                 }
743                 if (($type eq '' || $type eq '{') && $c eq '{') {
744                         $level++;
745                         $type = '{';
746                 }
747                 if ($type eq '{' && $c eq '}') {
748                         $level--;
749                         $type = ($level != 0)? '{' : '';
750
751                         if ($level == 0) {
752                                 if (substr($blk, $off + 1, 1) eq ';') {
753                                         $off++;
754                                 }
755                                 last;
756                         }
757                 }
758                 # Preprocessor commands end at the newline unless escaped.
759                 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
760                         $level--;
761                         $type = '';
762                         $off++;
763                         last;
764                 }
765                 $off++;
766         }
767         # We are truly at the end, so shuffle to the next line.
768         if ($off == $len) {
769                 $loff = $len + 1;
770                 $line++;
771                 $remain--;
772         }
773
774         my $statement = substr($blk, $soff, $off - $soff + 1);
775         my $condition = substr($blk, $soff, $coff - $soff + 1);
776
777         #warn "STATEMENT<$statement>\n";
778         #warn "CONDITION<$condition>\n";
779
780         #print "coff<$coff> soff<$off> loff<$loff>\n";
781
782         return ($statement, $condition,
783                         $line, $remain + 1, $off - $loff + 1, $level);
784 }
785
786 sub statement_lines {
787         my ($stmt) = @_;
788
789         # Strip the diff line prefixes and rip blank lines at start and end.
790         $stmt =~ s/(^|\n)./$1/g;
791         $stmt =~ s/^\s*//;
792         $stmt =~ s/\s*$//;
793
794         my @stmt_lines = ($stmt =~ /\n/g);
795
796         return $#stmt_lines + 2;
797 }
798
799 sub statement_rawlines {
800         my ($stmt) = @_;
801
802         my @stmt_lines = ($stmt =~ /\n/g);
803
804         return $#stmt_lines + 2;
805 }
806
807 sub statement_block_size {
808         my ($stmt) = @_;
809
810         $stmt =~ s/(^|\n)./$1/g;
811         $stmt =~ s/^\s*{//;
812         $stmt =~ s/}\s*$//;
813         $stmt =~ s/^\s*//;
814         $stmt =~ s/\s*$//;
815
816         my @stmt_lines = ($stmt =~ /\n/g);
817         my @stmt_statements = ($stmt =~ /;/g);
818
819         my $stmt_lines = $#stmt_lines + 2;
820         my $stmt_statements = $#stmt_statements + 1;
821
822         if ($stmt_lines > $stmt_statements) {
823                 return $stmt_lines;
824         } else {
825                 return $stmt_statements;
826         }
827 }
828
829 sub ctx_statement_full {
830         my ($linenr, $remain, $off) = @_;
831         my ($statement, $condition, $level);
832
833         my (@chunks);
834
835         # Grab the first conditional/block pair.
836         ($statement, $condition, $linenr, $remain, $off, $level) =
837                                 ctx_statement_block($linenr, $remain, $off);
838         #print "F: c<$condition> s<$statement> remain<$remain>\n";
839         push(@chunks, [ $condition, $statement ]);
840         if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
841                 return ($level, $linenr, @chunks);
842         }
843
844         # Pull in the following conditional/block pairs and see if they
845         # could continue the statement.
846         for (;;) {
847                 ($statement, $condition, $linenr, $remain, $off, $level) =
848                                 ctx_statement_block($linenr, $remain, $off);
849                 #print "C: c<$condition> s<$statement> remain<$remain>\n";
850                 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
851                 #print "C: push\n";
852                 push(@chunks, [ $condition, $statement ]);
853         }
854
855         return ($level, $linenr, @chunks);
856 }
857
858 sub ctx_block_get {
859         my ($linenr, $remain, $outer, $open, $close, $off) = @_;
860         my $line;
861         my $start = $linenr - 1;
862         my $blk = '';
863         my @o;
864         my @c;
865         my @res = ();
866
867         my $level = 0;
868         my @stack = ($level);
869         for ($line = $start; $remain > 0; $line++) {
870                 next if ($rawlines[$line] =~ /^-/);
871                 $remain--;
872
873                 $blk .= $rawlines[$line];
874
875                 # Handle nested #if/#else.
876                 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
877                         push(@stack, $level);
878                 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
879                         $level = $stack[$#stack - 1];
880                 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
881                         $level = pop(@stack);
882                 }
883
884                 foreach my $c (split(//, $lines[$line])) {
885                         ##print "C<$c>L<$level><$open$close>O<$off>\n";
886                         if ($off > 0) {
887                                 $off--;
888                                 next;
889                         }
890
891                         if ($c eq $close && $level > 0) {
892                                 $level--;
893                                 last if ($level == 0);
894                         } elsif ($c eq $open) {
895                                 $level++;
896                         }
897                 }
898
899                 if (!$outer || $level <= 1) {
900                         push(@res, $rawlines[$line]);
901                 }
902
903                 last if ($level == 0);
904         }
905
906         return ($level, @res);
907 }
908 sub ctx_block_outer {
909         my ($linenr, $remain) = @_;
910
911         my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
912         return @r;
913 }
914 sub ctx_block {
915         my ($linenr, $remain) = @_;
916
917         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
918         return @r;
919 }
920 sub ctx_statement {
921         my ($linenr, $remain, $off) = @_;
922
923         my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
924         return @r;
925 }
926 sub ctx_block_level {
927         my ($linenr, $remain) = @_;
928
929         return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
930 }
931 sub ctx_statement_level {
932         my ($linenr, $remain, $off) = @_;
933
934         return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
935 }
936
937 sub ctx_locate_comment {
938         my ($first_line, $end_line) = @_;
939
940         # Catch a comment on the end of the line itself.
941         my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
942         return $current_comment if (defined $current_comment);
943
944         # Look through the context and try and figure out if there is a
945         # comment.
946         my $in_comment = 0;
947         $current_comment = '';
948         for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
949                 my $line = $rawlines[$linenr - 1];
950                 #warn "           $line\n";
951                 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
952                         $in_comment = 1;
953                 }
954                 if ($line =~ m@/\*@) {
955                         $in_comment = 1;
956                 }
957                 if (!$in_comment && $current_comment ne '') {
958                         $current_comment = '';
959                 }
960                 $current_comment .= $line . "\n" if ($in_comment);
961                 if ($line =~ m@\*/@) {
962                         $in_comment = 0;
963                 }
964         }
965
966         chomp($current_comment);
967         return($current_comment);
968 }
969 sub ctx_has_comment {
970         my ($first_line, $end_line) = @_;
971         my $cmt = ctx_locate_comment($first_line, $end_line);
972
973         ##print "LINE: $rawlines[$end_line - 1 ]\n";
974         ##print "CMMT: $cmt\n";
975
976         return ($cmt ne '');
977 }
978
979 sub raw_line {
980         my ($linenr, $cnt) = @_;
981
982         my $offset = $linenr - 1;
983         $cnt++;
984
985         my $line;
986         while ($cnt) {
987                 $line = $rawlines[$offset++];
988                 next if (defined($line) && $line =~ /^-/);
989                 $cnt--;
990         }
991
992         return $line;
993 }
994
995 sub cat_vet {
996         my ($vet) = @_;
997         my ($res, $coded);
998
999         $res = '';
1000         while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1001                 $res .= $1;
1002                 if ($2 ne '') {
1003                         $coded = sprintf("^%c", unpack('C', $2) + 64);
1004                         $res .= $coded;
1005                 }
1006         }
1007         $res =~ s/$/\$/;
1008
1009         return $res;
1010 }
1011
1012 my $av_preprocessor = 0;
1013 my $av_pending;
1014 my @av_paren_type;
1015 my $av_pend_colon;
1016
1017 sub annotate_reset {
1018         $av_preprocessor = 0;
1019         $av_pending = '_';
1020         @av_paren_type = ('E');
1021         $av_pend_colon = 'O';
1022 }
1023
1024 sub annotate_values {
1025         my ($stream, $type) = @_;
1026
1027         my $res;
1028         my $var = '_' x length($stream);
1029         my $cur = $stream;
1030
1031         print "$stream\n" if ($dbg_values > 1);
1032
1033         while (length($cur)) {
1034                 @av_paren_type = ('E') if ($#av_paren_type < 0);
1035                 print " <" . join('', @av_paren_type) .
1036                                 "> <$type> <$av_pending>" if ($dbg_values > 1);
1037                 if ($cur =~ /^(\s+)/o) {
1038                         print "WS($1)\n" if ($dbg_values > 1);
1039                         if ($1 =~ /\n/ && $av_preprocessor) {
1040                                 $type = pop(@av_paren_type);
1041                                 $av_preprocessor = 0;
1042                         }
1043
1044                 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1045                         print "CAST($1)\n" if ($dbg_values > 1);
1046                         push(@av_paren_type, $type);
1047                         $type = 'c';
1048
1049                 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1050                         print "DECLARE($1)\n" if ($dbg_values > 1);
1051                         $type = 'T';
1052
1053                 } elsif ($cur =~ /^($Modifier)\s*/) {
1054                         print "MODIFIER($1)\n" if ($dbg_values > 1);
1055                         $type = 'T';
1056
1057                 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1058                         print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1059                         $av_preprocessor = 1;
1060                         push(@av_paren_type, $type);
1061                         if ($2 ne '') {
1062                                 $av_pending = 'N';
1063                         }
1064                         $type = 'E';
1065
1066                 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1067                         print "UNDEF($1)\n" if ($dbg_values > 1);
1068                         $av_preprocessor = 1;
1069                         push(@av_paren_type, $type);
1070
1071                 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1072                         print "PRE_START($1)\n" if ($dbg_values > 1);
1073                         $av_preprocessor = 1;
1074
1075                         push(@av_paren_type, $type);
1076                         push(@av_paren_type, $type);
1077                         $type = 'E';
1078
1079                 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1080                         print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1081                         $av_preprocessor = 1;
1082
1083                         push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1084
1085                         $type = 'E';
1086
1087                 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1088                         print "PRE_END($1)\n" if ($dbg_values > 1);
1089
1090                         $av_preprocessor = 1;
1091
1092                         # Assume all arms of the conditional end as this
1093                         # one does, and continue as if the #endif was not here.
1094                         pop(@av_paren_type);
1095                         push(@av_paren_type, $type);
1096                         $type = 'E';
1097
1098                 } elsif ($cur =~ /^(\\\n)/o) {
1099                         print "PRECONT($1)\n" if ($dbg_values > 1);
1100
1101                 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1102                         print "ATTR($1)\n" if ($dbg_values > 1);
1103                         $av_pending = $type;
1104                         $type = 'N';
1105
1106                 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1107                         print "SIZEOF($1)\n" if ($dbg_values > 1);
1108                         if (defined $2) {
1109                                 $av_pending = 'V';
1110                         }
1111                         $type = 'N';
1112
1113                 } elsif ($cur =~ /^(if|while|for)\b/o) {
1114                         print "COND($1)\n" if ($dbg_values > 1);
1115                         $av_pending = 'E';
1116                         $type = 'N';
1117
1118                 } elsif ($cur =~/^(case)/o) {
1119                         print "CASE($1)\n" if ($dbg_values > 1);
1120                         $av_pend_colon = 'C';
1121                         $type = 'N';
1122
1123                 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1124                         print "KEYWORD($1)\n" if ($dbg_values > 1);
1125                         $type = 'N';
1126
1127                 } elsif ($cur =~ /^(\()/o) {
1128                         print "PAREN('$1')\n" if ($dbg_values > 1);
1129                         push(@av_paren_type, $av_pending);
1130                         $av_pending = '_';
1131                         $type = 'N';
1132
1133                 } elsif ($cur =~ /^(\))/o) {
1134                         my $new_type = pop(@av_paren_type);
1135                         if ($new_type ne '_') {
1136                                 $type = $new_type;
1137                                 print "PAREN('$1') -> $type\n"
1138                                                         if ($dbg_values > 1);
1139                         } else {
1140                                 print "PAREN('$1')\n" if ($dbg_values > 1);
1141                         }
1142
1143                 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1144                         print "FUNC($1)\n" if ($dbg_values > 1);
1145                         $type = 'V';
1146                         $av_pending = 'V';
1147
1148                 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1149                         if (defined $2 && $type eq 'C' || $type eq 'T') {
1150                                 $av_pend_colon = 'B';
1151                         } elsif ($type eq 'E') {
1152                                 $av_pend_colon = 'L';
1153                         }
1154                         print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1155                         $type = 'V';
1156
1157                 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1158                         print "IDENT($1)\n" if ($dbg_values > 1);
1159                         $type = 'V';
1160
1161                 } elsif ($cur =~ /^($Assignment)/o) {
1162                         print "ASSIGN($1)\n" if ($dbg_values > 1);
1163                         $type = 'N';
1164
1165                 } elsif ($cur =~/^(;|{|})/) {
1166                         print "END($1)\n" if ($dbg_values > 1);
1167                         $type = 'E';
1168                         $av_pend_colon = 'O';
1169
1170                 } elsif ($cur =~/^(,)/) {
1171                         print "COMMA($1)\n" if ($dbg_values > 1);
1172                         $type = 'C';
1173
1174                 } elsif ($cur =~ /^(\?)/o) {
1175                         print "QUESTION($1)\n" if ($dbg_values > 1);
1176                         $type = 'N';
1177
1178                 } elsif ($cur =~ /^(:)/o) {
1179                         print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1180
1181                         substr($var, length($res), 1, $av_pend_colon);
1182                         if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1183                                 $type = 'E';
1184                         } else {
1185                                 $type = 'N';
1186                         }
1187                         $av_pend_colon = 'O';
1188
1189                 } elsif ($cur =~ /^(\[)/o) {
1190                         print "CLOSE($1)\n" if ($dbg_values > 1);
1191                         $type = 'N';
1192
1193                 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1194                         my $variant;
1195
1196                         print "OPV($1)\n" if ($dbg_values > 1);
1197                         if ($type eq 'V') {
1198                                 $variant = 'B';
1199                         } else {
1200                                 $variant = 'U';
1201                         }
1202
1203                         substr($var, length($res), 1, $variant);
1204                         $type = 'N';
1205
1206                 } elsif ($cur =~ /^($Operators)/o) {
1207                         print "OP($1)\n" if ($dbg_values > 1);
1208                         if ($1 ne '++' && $1 ne '--') {
1209                                 $type = 'N';
1210                         }
1211
1212                 } elsif ($cur =~ /(^.)/o) {
1213                         print "C($1)\n" if ($dbg_values > 1);
1214                 }
1215                 if (defined $1) {
1216                         $cur = substr($cur, length($1));
1217                         $res .= $type x length($1);
1218                 }
1219         }
1220
1221         return ($res, $var);
1222 }
1223
1224 sub possible {
1225         my ($possible, $line) = @_;
1226         my $notPermitted = qr{(?:
1227                 ^(?:
1228                         $Modifier|
1229                         $Storage|
1230                         $Type|
1231                         DEFINE_\S+
1232                 )$|
1233                 ^(?:
1234                         goto|
1235                         return|
1236                         case|
1237                         else|
1238                         asm|__asm__|
1239                         do|
1240                         \#|
1241                         \#\#|
1242                 )(?:\s|$)|
1243                 ^(?:typedef|struct|enum)\b
1244             )}x;
1245         warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1246         if ($possible !~ $notPermitted) {
1247                 # Check for modifiers.
1248                 $possible =~ s/\s*$Storage\s*//g;
1249                 $possible =~ s/\s*$Sparse\s*//g;
1250                 if ($possible =~ /^\s*$/) {
1251
1252                 } elsif ($possible =~ /\s/) {
1253                         $possible =~ s/\s*$Type\s*//g;
1254                         for my $modifier (split(' ', $possible)) {
1255                                 if ($modifier !~ $notPermitted) {
1256                                         warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1257                                         push(@modifierList, $modifier);
1258                                 }
1259                         }
1260
1261                 } else {
1262                         warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1263                         push(@typeList, $possible);
1264                 }
1265                 build_types();
1266         } else {
1267                 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1268         }
1269 }
1270
1271 my $prefix = '';
1272
1273 sub show_type {
1274        return !defined $ignore_type{$_[0]};
1275 }
1276
1277 sub report {
1278         if (!show_type($_[1]) ||
1279             (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
1280                 return 0;
1281         }
1282         my $line;
1283         if ($show_types) {
1284                 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1285         } else {
1286                 $line = "$prefix$_[0]: $_[2]\n";
1287         }
1288         $line = (split('\n', $line))[0] . "\n" if ($terse);
1289
1290         push(our @report, $line);
1291
1292         return 1;
1293 }
1294 sub report_dump {
1295         our @report;
1296 }
1297
1298 sub ERROR {
1299         if (report("ERROR", $_[0], $_[1])) {
1300                 our $clean = 0;
1301                 our $cnt_error++;
1302                 return 1;
1303         }
1304         return 0;
1305 }
1306 sub WARN {
1307         if (report("WARNING", $_[0], $_[1])) {
1308                 our $clean = 0;
1309                 our $cnt_warn++;
1310                 return 1;
1311         }
1312         return 0;
1313 }
1314 sub CHK {
1315         if ($check && report("CHECK", $_[0], $_[1])) {
1316                 our $clean = 0;
1317                 our $cnt_chk++;
1318                 return 1;
1319         }
1320         return 0;
1321 }
1322
1323 sub check_absolute_file {
1324         my ($absolute, $herecurr) = @_;
1325         my $file = $absolute;
1326
1327         ##print "absolute<$absolute>\n";
1328
1329         # See if any suffix of this path is a path within the tree.
1330         while ($file =~ s@^[^/]*/@@) {
1331                 if (-f "$root/$file") {
1332                         ##print "file<$file>\n";
1333                         last;
1334                 }
1335         }
1336         if (! -f _)  {
1337                 return 0;
1338         }
1339
1340         # It is, so see if the prefix is acceptable.
1341         my $prefix = $absolute;
1342         substr($prefix, -length($file)) = '';
1343
1344         ##print "prefix<$prefix>\n";
1345         if ($prefix ne ".../") {
1346                 WARN("USE_RELATIVE_PATH",
1347                      "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1348         }
1349 }
1350
1351 sub trim {
1352         my ($string) = @_;
1353
1354         $string =~ s/(^\s+|\s+$)//g;
1355
1356         return $string;
1357 }
1358
1359 sub tabify {
1360         my ($leading) = @_;
1361
1362         my $source_indent = 8;
1363         my $max_spaces_before_tab = $source_indent - 1;
1364         my $spaces_to_tab = " " x $source_indent;
1365
1366         #convert leading spaces to tabs
1367         1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1368         #Remove spaces before a tab
1369         1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1370
1371         return "$leading";
1372 }
1373
1374 sub pos_last_openparen {
1375         my ($line) = @_;
1376
1377         my $pos = 0;
1378
1379         my $opens = $line =~ tr/\(/\(/;
1380         my $closes = $line =~ tr/\)/\)/;
1381
1382         my $last_openparen = 0;
1383
1384         if (($opens == 0) || ($closes >= $opens)) {
1385                 return -1;
1386         }
1387
1388         my $len = length($line);
1389
1390         for ($pos = 0; $pos < $len; $pos++) {
1391                 my $string = substr($line, $pos);
1392                 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1393                         $pos += length($1) - 1;
1394                 } elsif (substr($line, $pos, 1) eq '(') {
1395                         $last_openparen = $pos;
1396                 } elsif (index($string, '(') == -1) {
1397                         last;
1398                 }
1399         }
1400
1401         return $last_openparen + 1;
1402 }
1403
1404 sub process {
1405         my $filename = shift;
1406
1407         my $linenr=0;
1408         my $prevline="";
1409         my $prevrawline="";
1410         my $stashline="";
1411         my $stashrawline="";
1412
1413         my $length;
1414         my $indent;
1415         my $previndent=0;
1416         my $stashindent=0;
1417
1418         our $clean = 1;
1419         my $signoff = 0;
1420         my $is_patch = 0;
1421
1422         my $in_header_lines = 1;
1423         my $in_commit_log = 0;          #Scanning lines before patch
1424
1425         my $non_utf8_charset = 0;
1426
1427         our @report = ();
1428         our $cnt_lines = 0;
1429         our $cnt_error = 0;
1430         our $cnt_warn = 0;
1431         our $cnt_chk = 0;
1432
1433         # Trace the real file/line as we go.
1434         my $realfile = '';
1435         my $realline = 0;
1436         my $realcnt = 0;
1437         my $here = '';
1438         my $in_comment = 0;
1439         my $comment_edge = 0;
1440         my $first_line = 0;
1441         my $p1_prefix = '';
1442
1443         my $prev_values = 'E';
1444
1445         # suppression flags
1446         my %suppress_ifbraces;
1447         my %suppress_whiletrailers;
1448         my %suppress_export;
1449         my $suppress_statement = 0;
1450
1451         my %camelcase = ();
1452
1453         # Pre-scan the patch sanitizing the lines.
1454         # Pre-scan the patch looking for any __setup documentation.
1455         #
1456         my @setup_docs = ();
1457         my $setup_docs = 0;
1458
1459         sanitise_line_reset();
1460         my $line;
1461         foreach my $rawline (@rawlines) {
1462                 $linenr++;
1463                 $line = $rawline;
1464
1465                 push(@fixed, $rawline) if ($fix);
1466
1467                 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1468                         $setup_docs = 0;
1469                         if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1470                                 $setup_docs = 1;
1471                         }
1472                         #next;
1473                 }
1474                 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1475                         $realline=$1-1;
1476                         if (defined $2) {
1477                                 $realcnt=$3+1;
1478                         } else {
1479                                 $realcnt=1+1;
1480                         }
1481                         $in_comment = 0;
1482
1483                         # Guestimate if this is a continuing comment.  Run
1484                         # the context looking for a comment "edge".  If this
1485                         # edge is a close comment then we must be in a comment
1486                         # at context start.
1487                         my $edge;
1488                         my $cnt = $realcnt;
1489                         for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1490                                 next if (defined $rawlines[$ln - 1] &&
1491                                          $rawlines[$ln - 1] =~ /^-/);
1492                                 $cnt--;
1493                                 #print "RAW<$rawlines[$ln - 1]>\n";
1494                                 last if (!defined $rawlines[$ln - 1]);
1495                                 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1496                                     $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1497                                         ($edge) = $1;
1498                                         last;
1499                                 }
1500                         }
1501                         if (defined $edge && $edge eq '*/') {
1502                                 $in_comment = 1;
1503                         }
1504
1505                         # Guestimate if this is a continuing comment.  If this
1506                         # is the start of a diff block and this line starts
1507                         # ' *' then it is very likely a comment.
1508                         if (!defined $edge &&
1509                             $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1510                         {
1511                                 $in_comment = 1;
1512                         }
1513
1514                         ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1515                         sanitise_line_reset($in_comment);
1516
1517                 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1518                         # Standardise the strings and chars within the input to
1519                         # simplify matching -- only bother with positive lines.
1520                         $line = sanitise_line($rawline);
1521                 }
1522                 push(@lines, $line);
1523
1524                 if ($realcnt > 1) {
1525                         $realcnt-- if ($line =~ /^(?:\+| |$)/);
1526                 } else {
1527                         $realcnt = 0;
1528                 }
1529
1530                 #print "==>$rawline\n";
1531                 #print "-->$line\n";
1532
1533                 if ($setup_docs && $line =~ /^\+/) {
1534                         push(@setup_docs, $line);
1535                 }
1536         }
1537
1538         $prefix = '';
1539
1540         $realcnt = 0;
1541         $linenr = 0;
1542         foreach my $line (@lines) {
1543                 $linenr++;
1544
1545                 my $rawline = $rawlines[$linenr - 1];
1546
1547 #extract the line range in the file after the patch is applied
1548                 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1549                         $is_patch = 1;
1550                         $first_line = $linenr + 1;
1551                         $realline=$1-1;
1552                         if (defined $2) {
1553                                 $realcnt=$3+1;
1554                         } else {
1555                                 $realcnt=1+1;
1556                         }
1557                         annotate_reset();
1558                         $prev_values = 'E';
1559
1560                         %suppress_ifbraces = ();
1561                         %suppress_whiletrailers = ();
1562                         %suppress_export = ();
1563                         $suppress_statement = 0;
1564                         next;
1565
1566 # track the line number as we move through the hunk, note that
1567 # new versions of GNU diff omit the leading space on completely
1568 # blank context lines so we need to count that too.
1569                 } elsif ($line =~ /^( |\+|$)/) {
1570                         $realline++;
1571                         $realcnt-- if ($realcnt != 0);
1572
1573                         # Measure the line length and indent.
1574                         ($length, $indent) = line_stats($rawline);
1575
1576                         # Track the previous line.
1577                         ($prevline, $stashline) = ($stashline, $line);
1578                         ($previndent, $stashindent) = ($stashindent, $indent);
1579                         ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1580
1581                         #warn "line<$line>\n";
1582
1583                 } elsif ($realcnt == 1) {
1584                         $realcnt--;
1585                 }
1586
1587                 my $hunk_line = ($realcnt != 0);
1588
1589 #make up the handle for any error we report on this line
1590                 $prefix = "$filename:$realline: " if ($emacs && $file);
1591                 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1592
1593                 $here = "#$linenr: " if (!$file);
1594                 $here = "#$realline: " if ($file);
1595
1596                 # extract the filename as it passes
1597                 if ($line =~ /^diff --git.*?(\S+)$/) {
1598                         $realfile = $1;
1599                         $realfile =~ s@^([^/]*)/@@;
1600                         $in_commit_log = 0;
1601                 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1602                         $realfile = $1;
1603                         $realfile =~ s@^([^/]*)/@@;
1604                         $in_commit_log = 0;
1605
1606                         $p1_prefix = $1;
1607                         if (!$file && $tree && $p1_prefix ne '' &&
1608                             -e "$root/$p1_prefix") {
1609                                 WARN("PATCH_PREFIX",
1610                                      "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1611                         }
1612
1613                         if ($realfile =~ m@^include/asm/@) {
1614                                 ERROR("MODIFIED_INCLUDE_ASM",
1615                                       "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1616                         }
1617                         next;
1618                 }
1619
1620                 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1621
1622                 my $hereline = "$here\n$rawline\n";
1623                 my $herecurr = "$here\n$rawline\n";
1624                 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1625
1626                 $cnt_lines++ if ($realcnt != 0);
1627
1628 # Check for incorrect file permissions
1629                 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1630                         my $permhere = $here . "FILE: $realfile\n";
1631                         if ($realfile !~ m@scripts/@ &&
1632                             $realfile !~ /\.(py|pl|awk|sh)$/) {
1633                                 ERROR("EXECUTE_PERMISSIONS",
1634                                       "do not set execute permissions for source files\n" . $permhere);
1635                         }
1636                 }
1637
1638 # Check the patch for a signoff:
1639                 if ($line =~ /^\s*signed-off-by:/i) {
1640                         $signoff++;
1641                         $in_commit_log = 0;
1642                 }
1643
1644 # Check signature styles
1645                 if (!$in_header_lines &&
1646                     $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
1647                         my $space_before = $1;
1648                         my $sign_off = $2;
1649                         my $space_after = $3;
1650                         my $email = $4;
1651                         my $ucfirst_sign_off = ucfirst(lc($sign_off));
1652
1653                         if ($sign_off !~ /$signature_tags/) {
1654                                 WARN("BAD_SIGN_OFF",
1655                                      "Non-standard signature: $sign_off\n" . $herecurr);
1656                         }
1657                         if (defined $space_before && $space_before ne "") {
1658                                 if (WARN("BAD_SIGN_OFF",
1659                                          "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1660                                     $fix) {
1661                                         $fixed[$linenr - 1] =
1662                                             "$ucfirst_sign_off $email";
1663                                 }
1664                         }
1665                         if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
1666                                 if (WARN("BAD_SIGN_OFF",
1667                                          "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1668                                     $fix) {
1669                                         $fixed[$linenr - 1] =
1670                                             "$ucfirst_sign_off $email";
1671                                 }
1672
1673                         }
1674                         if (!defined $space_after || $space_after ne " ") {
1675                                 if (WARN("BAD_SIGN_OFF",
1676                                          "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1677                                     $fix) {
1678                                         $fixed[$linenr - 1] =
1679                                             "$ucfirst_sign_off $email";
1680                                 }
1681                         }
1682
1683                         my ($email_name, $email_address, $comment) = parse_email($email);
1684                         my $suggested_email = format_email(($email_name, $email_address));
1685                         if ($suggested_email eq "") {
1686                                 ERROR("BAD_SIGN_OFF",
1687                                       "Unrecognized email address: '$email'\n" . $herecurr);
1688                         } else {
1689                                 my $dequoted = $suggested_email;
1690                                 $dequoted =~ s/^"//;
1691                                 $dequoted =~ s/" </ </;
1692                                 # Don't force email to have quotes
1693                                 # Allow just an angle bracketed address
1694                                 if ("$dequoted$comment" ne $email &&
1695                                     "<$email_address>$comment" ne $email &&
1696                                     "$suggested_email$comment" ne $email) {
1697                                         WARN("BAD_SIGN_OFF",
1698                                              "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1699                                 }
1700                         }
1701                 }
1702
1703 # Check for wrappage within a valid hunk of the file
1704                 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1705                         ERROR("CORRUPTED_PATCH",
1706                               "patch seems to be corrupt (line wrapped?)\n" .
1707                                 $herecurr) if (!$emitted_corrupt++);
1708                 }
1709
1710 # Check for absolute kernel paths.
1711                 if ($tree) {
1712                         while ($line =~ m{(?:^|\s)(/\S*)}g) {
1713                                 my $file = $1;
1714
1715                                 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1716                                     check_absolute_file($1, $herecurr)) {
1717                                         #
1718                                 } else {
1719                                         check_absolute_file($file, $herecurr);
1720                                 }
1721                         }
1722                 }
1723
1724 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1725                 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1726                     $rawline !~ m/^$UTF8*$/) {
1727                         my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1728
1729                         my $blank = copy_spacing($rawline);
1730                         my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1731                         my $hereptr = "$hereline$ptr\n";
1732
1733                         CHK("INVALID_UTF8",
1734                             "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1735                 }
1736
1737 # Check if it's the start of a commit log
1738 # (not a header line and we haven't seen the patch filename)
1739                 if ($in_header_lines && $realfile =~ /^$/ &&
1740                     $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1741                         $in_header_lines = 0;
1742                         $in_commit_log = 1;
1743                 }
1744
1745 # Check if there is UTF-8 in a commit log when a mail header has explicitly
1746 # declined it, i.e defined some charset where it is missing.
1747                 if ($in_header_lines &&
1748                     $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1749                     $1 !~ /utf-8/i) {
1750                         $non_utf8_charset = 1;
1751                 }
1752
1753                 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1754                     $rawline =~ /$NON_ASCII_UTF8/) {
1755                         WARN("UTF8_BEFORE_PATCH",
1756                             "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1757                 }
1758
1759 # ignore non-hunk lines and lines being removed
1760                 next if (!$hunk_line || $line =~ /^-/);
1761
1762 #trailing whitespace
1763                 if ($line =~ /^\+.*\015/) {
1764                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1765                         ERROR("DOS_LINE_ENDINGS",
1766                               "DOS line endings\n" . $herevet);
1767
1768                 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1769                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1770                         if (ERROR("TRAILING_WHITESPACE",
1771                                   "trailing whitespace\n" . $herevet) &&
1772                             $fix) {
1773                                 $fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/;
1774                         }
1775
1776                         $rpt_cleaners = 1;
1777                 }
1778
1779 # check for Kconfig help text having a real description
1780 # Only applies when adding the entry originally, after that we do not have
1781 # sufficient context to determine whether it is indeed long enough.
1782                 if ($realfile =~ /Kconfig/ &&
1783                     $line =~ /.\s*config\s+/) {
1784                         my $length = 0;
1785                         my $cnt = $realcnt;
1786                         my $ln = $linenr + 1;
1787                         my $f;
1788                         my $is_start = 0;
1789                         my $is_end = 0;
1790                         for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
1791                                 $f = $lines[$ln - 1];
1792                                 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1793                                 $is_end = $lines[$ln - 1] =~ /^\+/;
1794
1795                                 next if ($f =~ /^-/);
1796
1797                                 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1798                                         $is_start = 1;
1799                                 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1800                                         $length = -1;
1801                                 }
1802
1803                                 $f =~ s/^.//;
1804                                 $f =~ s/#.*//;
1805                                 $f =~ s/^\s+//;
1806                                 next if ($f =~ /^$/);
1807                                 if ($f =~ /^\s*config\s/) {
1808                                         $is_end = 1;
1809                                         last;
1810                                 }
1811                                 $length++;
1812                         }
1813                         WARN("CONFIG_DESCRIPTION",
1814                              "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1815                         #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1816                 }
1817
1818 # discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1819                 if ($realfile =~ /Kconfig/ &&
1820                     $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1821                         WARN("CONFIG_EXPERIMENTAL",
1822                              "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1823                 }
1824
1825                 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1826                     ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1827                         my $flag = $1;
1828                         my $replacement = {
1829                                 'EXTRA_AFLAGS' =>   'asflags-y',
1830                                 'EXTRA_CFLAGS' =>   'ccflags-y',
1831                                 'EXTRA_CPPFLAGS' => 'cppflags-y',
1832                                 'EXTRA_LDFLAGS' =>  'ldflags-y',
1833                         };
1834
1835                         WARN("DEPRECATED_VARIABLE",
1836                              "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1837                 }
1838
1839 # check we are in a valid source file if not then ignore this hunk
1840                 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1841
1842 #line length limit
1843                 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1844                     $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1845                     !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1846                     $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1847                     $length > $max_line_length)
1848                 {
1849                         WARN("LONG_LINE",
1850                              "line over $max_line_length characters\n" . $herecurr);
1851                 }
1852
1853 # Check for user-visible strings broken across lines, which breaks the ability
1854 # to grep for the string.  Limited to strings used as parameters (those
1855 # following an open parenthesis), which almost completely eliminates false
1856 # positives, as well as warning only once per parameter rather than once per
1857 # line of the string.  Make an exception when the previous string ends in a
1858 # newline (multiple lines in one string constant) or \n\t (common in inline
1859 # assembly to indent the instruction on the following line).
1860                 if ($line =~ /^\+\s*"/ &&
1861                     $prevline =~ /"\s*$/ &&
1862                     $prevline =~ /\(/ &&
1863                     $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1864                         WARN("SPLIT_STRING",
1865                              "quoted string split across lines\n" . $hereprev);
1866                 }
1867
1868 # check for spaces before a quoted newline
1869                 if ($rawline =~ /^.*\".*\s\\n/) {
1870                         if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1871                                  "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
1872                             $fix) {
1873                                 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
1874                         }
1875
1876                 }
1877
1878 # check for adding lines without a newline.
1879                 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1880                         WARN("MISSING_EOF_NEWLINE",
1881                              "adding a line without newline at end of file\n" . $herecurr);
1882                 }
1883
1884 # Blackfin: use hi/lo macros
1885                 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1886                         if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1887                                 my $herevet = "$here\n" . cat_vet($line) . "\n";
1888                                 ERROR("LO_MACRO",
1889                                       "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1890                         }
1891                         if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1892                                 my $herevet = "$here\n" . cat_vet($line) . "\n";
1893                                 ERROR("HI_MACRO",
1894                                       "use the HI() macro, not (... >> 16)\n" . $herevet);
1895                         }
1896                 }
1897
1898 # check we are in a valid source file C or perl if not then ignore this hunk
1899                 next if ($realfile !~ /\.(h|c|pl)$/);
1900
1901 # at the beginning of a line any tabs must come first and anything
1902 # more than 8 must use tabs.
1903                 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1904                     $rawline =~ /^\+\s*        \s*/) {
1905                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1906                         $rpt_cleaners = 1;
1907                         if (ERROR("CODE_INDENT",
1908                                   "code indent should use tabs where possible\n" . $herevet) &&
1909                             $fix) {
1910                                 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
1911                         }
1912                 }
1913
1914 # check for space before tabs.
1915                 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1916                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1917                         if (WARN("SPACE_BEFORE_TAB",
1918                                 "please, no space before tabs\n" . $herevet) &&
1919                             $fix) {
1920                                 $fixed[$linenr - 1] =~
1921                                     s/(^\+.*) +\t/$1\t/;
1922                         }
1923                 }
1924
1925 # check for && or || at the start of a line
1926                 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1927                         CHK("LOGICAL_CONTINUATIONS",
1928                             "Logical continuations should be on the previous line\n" . $hereprev);
1929                 }
1930
1931 # check multi-line statement indentation matches previous line
1932                 if ($^V && $^V ge 5.10.0 &&
1933                     $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1934                         $prevline =~ /^\+(\t*)(.*)$/;
1935                         my $oldindent = $1;
1936                         my $rest = $2;
1937
1938                         my $pos = pos_last_openparen($rest);
1939                         if ($pos >= 0) {
1940                                 $line =~ /^(\+| )([ \t]*)/;
1941                                 my $newindent = $2;
1942
1943                                 my $goodtabindent = $oldindent .
1944                                         "\t" x ($pos / 8) .
1945                                         " "  x ($pos % 8);
1946                                 my $goodspaceindent = $oldindent . " "  x $pos;
1947
1948                                 if ($newindent ne $goodtabindent &&
1949                                     $newindent ne $goodspaceindent) {
1950
1951                                         if (CHK("PARENTHESIS_ALIGNMENT",
1952                                                 "Alignment should match open parenthesis\n" . $hereprev) &&
1953                                             $fix && $line =~ /^\+/) {
1954                                                 $fixed[$linenr - 1] =~
1955                                                     s/^\+[ \t]*/\+$goodtabindent/;
1956                                         }
1957                                 }
1958                         }
1959                 }
1960
1961                 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
1962                         if (CHK("SPACING",
1963                                 "No space is necessary after a cast\n" . $hereprev) &&
1964                             $fix) {
1965                                 $fixed[$linenr - 1] =~
1966                                     s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
1967                         }
1968                 }
1969
1970                 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1971                     $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1972                     $rawline =~ /^\+[ \t]*\*/) {
1973                         WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1974                              "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
1975                 }
1976
1977                 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1978                     $prevrawline =~ /^\+[ \t]*\/\*/ &&          #starting /*
1979                     $prevrawline !~ /\*\/[ \t]*$/ &&            #no trailing */
1980                     $rawline !~ /^\+[ \t]*\*/) {                #no leading *
1981                         WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1982                              "networking block comments start with * on subsequent lines\n" . $hereprev);
1983                 }
1984
1985                 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1986                     $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&       #trailing */
1987                     $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&      #inline /*...*/
1988                     $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&       #trailing **/
1989                     $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {    #non blank */
1990                         WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1991                              "networking block comments put the trailing */ on a separate line\n" . $herecurr);
1992                 }
1993
1994 # check for spaces at the beginning of a line.
1995 # Exceptions:
1996 #  1) within comments
1997 #  2) indented preprocessor commands
1998 #  3) hanging labels
1999                 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
2000                         my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2001                         if (WARN("LEADING_SPACE",
2002                                  "please, no spaces at the start of a line\n" . $herevet) &&
2003                             $fix) {
2004                                 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2005                         }
2006                 }
2007
2008 # check we are in a valid C source file if not then ignore this hunk
2009                 next if ($realfile !~ /\.(h|c)$/);
2010
2011 # discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2012                 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2013                         WARN("CONFIG_EXPERIMENTAL",
2014                              "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2015                 }
2016
2017 # check for RCS/CVS revision markers
2018                 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2019                         WARN("CVS_KEYWORD",
2020                              "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2021                 }
2022
2023 # Blackfin: don't use __builtin_bfin_[cs]sync
2024                 if ($line =~ /__builtin_bfin_csync/) {
2025                         my $herevet = "$here\n" . cat_vet($line) . "\n";
2026                         ERROR("CSYNC",
2027                               "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2028                 }
2029                 if ($line =~ /__builtin_bfin_ssync/) {
2030                         my $herevet = "$here\n" . cat_vet($line) . "\n";
2031                         ERROR("SSYNC",
2032                               "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2033                 }
2034
2035 # check for old HOTPLUG __dev<foo> section markings
2036                 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2037                         WARN("HOTPLUG_SECTION",
2038                              "Using $1 is unnecessary\n" . $herecurr);
2039                 }
2040
2041 # Check for potential 'bare' types
2042                 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2043                     $realline_next);
2044 #print "LINE<$line>\n";
2045                 if ($linenr >= $suppress_statement &&
2046                     $realcnt && $line =~ /.\s*\S/) {
2047                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2048                                 ctx_statement_block($linenr, $realcnt, 0);
2049                         $stat =~ s/\n./\n /g;
2050                         $cond =~ s/\n./\n /g;
2051
2052 #print "linenr<$linenr> <$stat>\n";
2053                         # If this statement has no statement boundaries within
2054                         # it there is no point in retrying a statement scan
2055                         # until we hit end of it.
2056                         my $frag = $stat; $frag =~ s/;+\s*$//;
2057                         if ($frag !~ /(?:{|;)/) {
2058 #print "skip<$line_nr_next>\n";
2059                                 $suppress_statement = $line_nr_next;
2060                         }
2061
2062                         # Find the real next line.
2063                         $realline_next = $line_nr_next;
2064                         if (defined $realline_next &&
2065                             (!defined $lines[$realline_next - 1] ||
2066                              substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2067                                 $realline_next++;
2068                         }
2069
2070                         my $s = $stat;
2071                         $s =~ s/{.*$//s;
2072
2073                         # Ignore goto labels.
2074                         if ($s =~ /$Ident:\*$/s) {
2075
2076                         # Ignore functions being called
2077                         } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2078
2079                         } elsif ($s =~ /^.\s*else\b/s) {
2080
2081                         # declarations always start with types
2082                         } 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) {
2083                                 my $type = $1;
2084                                 $type =~ s/\s+/ /g;
2085                                 possible($type, "A:" . $s);
2086
2087                         # definitions in global scope can only start with types
2088                         } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2089                                 possible($1, "B:" . $s);
2090                         }
2091
2092                         # any (foo ... *) is a pointer cast, and foo is a type
2093                         while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2094                                 possible($1, "C:" . $s);
2095                         }
2096
2097                         # Check for any sort of function declaration.
2098                         # int foo(something bar, other baz);
2099                         # void (*store_gdt)(x86_descr_ptr *);
2100                         if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2101                                 my ($name_len) = length($1);
2102
2103                                 my $ctx = $s;
2104                                 substr($ctx, 0, $name_len + 1, '');
2105                                 $ctx =~ s/\)[^\)]*$//;
2106
2107                                 for my $arg (split(/\s*,\s*/, $ctx)) {
2108                                         if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2109
2110                                                 possible($1, "D:" . $s);
2111                                         }
2112                                 }
2113                         }
2114
2115                 }
2116
2117 #
2118 # Checks which may be anchored in the context.
2119 #
2120
2121 # Check for switch () and associated case and default
2122 # statements should be at the same indent.
2123                 if ($line=~/\bswitch\s*\(.*\)/) {
2124                         my $err = '';
2125                         my $sep = '';
2126                         my @ctx = ctx_block_outer($linenr, $realcnt);
2127                         shift(@ctx);
2128                         for my $ctx (@ctx) {
2129                                 my ($clen, $cindent) = line_stats($ctx);
2130                                 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2131                                                         $indent != $cindent) {
2132                                         $err .= "$sep$ctx\n";
2133                                         $sep = '';
2134                                 } else {
2135                                         $sep = "[...]\n";
2136                                 }
2137                         }
2138                         if ($err ne '') {
2139                                 ERROR("SWITCH_CASE_INDENT_LEVEL",
2140                                       "switch and case should be at the same indent\n$hereline$err");
2141                         }
2142                 }
2143
2144 # if/while/etc brace do not go on next line, unless defining a do while loop,
2145 # or if that brace on the next line is for something else
2146                 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2147                         my $pre_ctx = "$1$2";
2148
2149                         my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
2150
2151                         if ($line =~ /^\+\t{6,}/) {
2152                                 WARN("DEEP_INDENTATION",
2153                                      "Too many leading tabs - consider code refactoring\n" . $herecurr);
2154                         }
2155
2156                         my $ctx_cnt = $realcnt - $#ctx - 1;
2157                         my $ctx = join("\n", @ctx);
2158
2159                         my $ctx_ln = $linenr;
2160                         my $ctx_skip = $realcnt;
2161
2162                         while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2163                                         defined $lines[$ctx_ln - 1] &&
2164                                         $lines[$ctx_ln - 1] =~ /^-/)) {
2165                                 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2166                                 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2167                                 $ctx_ln++;
2168                         }
2169
2170                         #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2171                         #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2172
2173                         if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2174                                 ERROR("OPEN_BRACE",
2175                                       "that open brace { should be on the previous line\n" .
2176                                         "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2177                         }
2178                         if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2179                             $ctx =~ /\)\s*\;\s*$/ &&
2180                             defined $lines[$ctx_ln - 1])
2181                         {
2182                                 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2183                                 if ($nindent > $indent) {
2184                                         WARN("TRAILING_SEMICOLON",
2185                                              "trailing semicolon indicates no statements, indent implies otherwise\n" .
2186                                                 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2187                                 }
2188                         }
2189                 }
2190
2191 # Check relative indent for conditionals and blocks.
2192                 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2193                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2194                                 ctx_statement_block($linenr, $realcnt, 0)
2195                                         if (!defined $stat);
2196                         my ($s, $c) = ($stat, $cond);
2197
2198                         substr($s, 0, length($c), '');
2199
2200                         # Make sure we remove the line prefixes as we have
2201                         # none on the first line, and are going to readd them
2202                         # where necessary.
2203                         $s =~ s/\n./\n/gs;
2204
2205                         # Find out how long the conditional actually is.
2206                         my @newlines = ($c =~ /\n/gs);
2207                         my $cond_lines = 1 + $#newlines;
2208
2209                         # We want to check the first line inside the block
2210                         # starting at the end of the conditional, so remove:
2211                         #  1) any blank line termination
2212                         #  2) any opening brace { on end of the line
2213                         #  3) any do (...) {
2214                         my $continuation = 0;
2215                         my $check = 0;
2216                         $s =~ s/^.*\bdo\b//;
2217                         $s =~ s/^\s*{//;
2218                         if ($s =~ s/^\s*\\//) {
2219                                 $continuation = 1;
2220                         }
2221                         if ($s =~ s/^\s*?\n//) {
2222                                 $check = 1;
2223                                 $cond_lines++;
2224                         }
2225
2226                         # Also ignore a loop construct at the end of a
2227                         # preprocessor statement.
2228                         if (($prevline =~ /^.\s*#\s*define\s/ ||
2229                             $prevline =~ /\\\s*$/) && $continuation == 0) {
2230                                 $check = 0;
2231                         }
2232
2233                         my $cond_ptr = -1;
2234                         $continuation = 0;
2235                         while ($cond_ptr != $cond_lines) {
2236                                 $cond_ptr = $cond_lines;
2237
2238                                 # If we see an #else/#elif then the code
2239                                 # is not linear.
2240                                 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2241                                         $check = 0;
2242                                 }
2243
2244                                 # Ignore:
2245                                 #  1) blank lines, they should be at 0,
2246                                 #  2) preprocessor lines, and
2247                                 #  3) labels.
2248                                 if ($continuation ||
2249                                     $s =~ /^\s*?\n/ ||
2250                                     $s =~ /^\s*#\s*?/ ||
2251                                     $s =~ /^\s*$Ident\s*:/) {
2252                                         $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2253                                         if ($s =~ s/^.*?\n//) {
2254                                                 $cond_lines++;
2255                                         }
2256                                 }
2257                         }
2258
2259                         my (undef, $sindent) = line_stats("+" . $s);
2260                         my $stat_real = raw_line($linenr, $cond_lines);
2261
2262                         # Check if either of these lines are modified, else
2263                         # this is not this patch's fault.
2264                         if (!defined($stat_real) ||
2265                             $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2266                                 $check = 0;
2267                         }
2268                         if (defined($stat_real) && $cond_lines > 1) {
2269                                 $stat_real = "[...]\n$stat_real";
2270                         }
2271
2272                         #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";
2273
2274                         if ($check && (($sindent % 8) != 0 ||
2275                             ($sindent <= $indent && $s ne ''))) {
2276                                 WARN("SUSPECT_CODE_INDENT",
2277                                      "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2278                         }
2279                 }
2280
2281                 # Track the 'values' across context and added lines.
2282                 my $opline = $line; $opline =~ s/^./ /;
2283                 my ($curr_values, $curr_vars) =
2284                                 annotate_values($opline . "\n", $prev_values);
2285                 $curr_values = $prev_values . $curr_values;
2286                 if ($dbg_values) {
2287                         my $outline = $opline; $outline =~ s/\t/ /g;
2288                         print "$linenr > .$outline\n";
2289                         print "$linenr > $curr_values\n";
2290                         print "$linenr >  $curr_vars\n";
2291                 }
2292                 $prev_values = substr($curr_values, -1);
2293
2294 #ignore lines not being added
2295                 next if ($line =~ /^[^\+]/);
2296
2297 # TEST: allow direct testing of the type matcher.
2298                 if ($dbg_type) {
2299                         if ($line =~ /^.\s*$Declare\s*$/) {
2300                                 ERROR("TEST_TYPE",
2301                                       "TEST: is type\n" . $herecurr);
2302                         } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2303                                 ERROR("TEST_NOT_TYPE",
2304                                       "TEST: is not type ($1 is)\n". $herecurr);
2305                         }
2306                         next;
2307                 }
2308 # TEST: allow direct testing of the attribute matcher.
2309                 if ($dbg_attr) {
2310                         if ($line =~ /^.\s*$Modifier\s*$/) {
2311                                 ERROR("TEST_ATTR",
2312                                       "TEST: is attr\n" . $herecurr);
2313                         } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2314                                 ERROR("TEST_NOT_ATTR",
2315                                       "TEST: is not attr ($1 is)\n". $herecurr);
2316                         }
2317                         next;
2318                 }
2319
2320 # check for initialisation to aggregates open brace on the next line
2321                 if ($line =~ /^.\s*{/ &&
2322                     $prevline =~ /(?:^|[^=])=\s*$/) {
2323                         ERROR("OPEN_BRACE",
2324                               "that open brace { should be on the previous line\n" . $hereprev);
2325                 }
2326
2327 #
2328 # Checks which are anchored on the added line.
2329 #
2330
2331 # check for malformed paths in #include statements (uses RAW line)
2332                 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2333                         my $path = $1;
2334                         if ($path =~ m{//}) {
2335                                 ERROR("MALFORMED_INCLUDE",
2336                                       "malformed #include filename\n" . $herecurr);
2337                         }
2338                         if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2339                                 ERROR("UAPI_INCLUDE",
2340                                       "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2341                         }
2342                 }
2343
2344 # no C99 // comments
2345                 if ($line =~ m{//}) {
2346                         if (ERROR("C99_COMMENTS",
2347                                   "do not use C99 // comments\n" . $herecurr) &&
2348                             $fix) {
2349                                 my $line = $fixed[$linenr - 1];
2350                                 if ($line =~ /\/\/(.*)$/) {
2351                                         my $comment = trim($1);
2352                                         $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2353                                 }
2354                         }
2355                 }
2356                 # Remove C99 comments.
2357                 $line =~ s@//.*@@;
2358                 $opline =~ s@//.*@@;
2359
2360 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2361 # the whole statement.
2362 #print "APW <$lines[$realline_next - 1]>\n";
2363                 if (defined $realline_next &&
2364                     exists $lines[$realline_next - 1] &&
2365                     !defined $suppress_export{$realline_next} &&
2366                     ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2367                      $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2368                         # Handle definitions which produce identifiers with
2369                         # a prefix:
2370                         #   XXX(foo);
2371                         #   EXPORT_SYMBOL(something_foo);
2372                         my $name = $1;
2373                         if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
2374                             $name =~ /^${Ident}_$2/) {
2375 #print "FOO C name<$name>\n";
2376                                 $suppress_export{$realline_next} = 1;
2377
2378                         } elsif ($stat !~ /(?:
2379                                 \n.}\s*$|
2380                                 ^.DEFINE_$Ident\(\Q$name\E\)|
2381                                 ^.DECLARE_$Ident\(\Q$name\E\)|
2382                                 ^.LIST_HEAD\(\Q$name\E\)|
2383                                 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2384                                 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
2385                             )/x) {
2386 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2387                                 $suppress_export{$realline_next} = 2;
2388                         } else {
2389                                 $suppress_export{$realline_next} = 1;
2390                         }
2391                 }
2392                 if (!defined $suppress_export{$linenr} &&
2393                     $prevline =~ /^.\s*$/ &&
2394                     ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2395                      $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2396 #print "FOO B <$lines[$linenr - 1]>\n";
2397                         $suppress_export{$linenr} = 2;
2398                 }
2399                 if (defined $suppress_export{$linenr} &&
2400                     $suppress_export{$linenr} == 2) {
2401                         WARN("EXPORT_SYMBOL",
2402                              "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2403                 }
2404
2405 # check for global initialisers.
2406                 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
2407                         ERROR("GLOBAL_INITIALISERS",
2408                               "do not initialise globals to 0 or NULL\n" .
2409                                 $herecurr);
2410                 }
2411 # check for static initialisers.
2412                 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2413                         ERROR("INITIALISED_STATIC",
2414                               "do not initialise statics to 0 or NULL\n" .
2415                                 $herecurr);
2416                 }
2417
2418 # check for static const char * arrays.
2419                 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
2420                         WARN("STATIC_CONST_CHAR_ARRAY",
2421                              "static const char * array should probably be static const char * const\n" .
2422                                 $herecurr);
2423                }
2424
2425 # check for static char foo[] = "bar" declarations.
2426                 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
2427                         WARN("STATIC_CONST_CHAR_ARRAY",
2428                              "static char array declaration should probably be static const char\n" .
2429                                 $herecurr);
2430                }
2431
2432 # check for declarations of struct pci_device_id
2433                 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
2434                         WARN("DEFINE_PCI_DEVICE_TABLE",
2435                              "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
2436                 }
2437
2438 # check for new typedefs, only function parameters and sparse annotations
2439 # make sense.
2440                 if ($line =~ /\btypedef\s/ &&
2441                     $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
2442                     $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
2443                     $line !~ /\b$typeTypedefs\b/ &&
2444                     $line !~ /\b__bitwise(?:__|)\b/) {
2445                         WARN("NEW_TYPEDEFS",
2446                              "do not add new typedefs\n" . $herecurr);
2447                 }
2448
2449 # * goes on variable not on type
2450                 # (char*[ const])
2451                 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2452                         #print "AA<$1>\n";
2453                         my ($ident, $from, $to) = ($1, $2, $2);
2454
2455                         # Should start with a space.
2456                         $to =~ s/^(\S)/ $1/;
2457                         # Should not end with a space.
2458                         $to =~ s/\s+$//;
2459                         # '*'s should not have spaces between.
2460                         while ($to =~ s/\*\s+\*/\*\*/) {
2461                         }
2462
2463 ##                      print "1: from<$from> to<$to> ident<$ident>\n";
2464                         if ($from ne $to) {
2465                                 if (ERROR("POINTER_LOCATION",
2466                                           "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
2467                                     $fix) {
2468                                         my $sub_from = $ident;
2469                                         my $sub_to = $ident;
2470                                         $sub_to =~ s/\Q$from\E/$to/;
2471                                         $fixed[$linenr - 1] =~
2472                                             s@\Q$sub_from\E@$sub_to@;
2473                                 }
2474                         }
2475                 }
2476                 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2477                         #print "BB<$1>\n";
2478                         my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
2479
2480                         # Should start with a space.
2481                         $to =~ s/^(\S)/ $1/;
2482                         # Should not end with a space.
2483                         $to =~ s/\s+$//;
2484                         # '*'s should not have spaces between.
2485                         while ($to =~ s/\*\s+\*/\*\*/) {
2486                         }
2487                         # Modifiers should have spaces.
2488                         $to =~ s/(\b$Modifier$)/$1 /;
2489
2490 ##                      print "2: from<$from> to<$to> ident<$ident>\n";
2491                         if ($from ne $to && $ident !~ /^$Modifier$/) {
2492                                 if (ERROR("POINTER_LOCATION",
2493                                           "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
2494                                     $fix) {
2495
2496                                         my $sub_from = $match;
2497                                         my $sub_to = $match;
2498                                         $sub_to =~ s/\Q$from\E/$to/;
2499                                         $fixed[$linenr - 1] =~
2500                                             s@\Q$sub_from\E@$sub_to@;
2501                                 }
2502                         }
2503                 }
2504
2505 # # no BUG() or BUG_ON()
2506 #               if ($line =~ /\b(BUG|BUG_ON)\b/) {
2507 #                       print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2508 #                       print "$herecurr";
2509 #                       $clean = 0;
2510 #               }
2511
2512                 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
2513                         WARN("LINUX_VERSION_CODE",
2514                              "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
2515                 }
2516
2517 # check for uses of printk_ratelimit
2518                 if ($line =~ /\bprintk_ratelimit\s*\(/) {
2519                         WARN("PRINTK_RATELIMITED",
2520 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
2521                 }
2522
2523 # printk should use KERN_* levels.  Note that follow on printk's on the
2524 # same line do not need a level, so we use the current block context
2525 # to try and find and validate the current printk.  In summary the current
2526 # printk includes all preceding printk's which have no newline on the end.
2527 # we assume the first bad printk is the one to report.
2528                 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
2529                         my $ok = 0;
2530                         for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2531                                 #print "CHECK<$lines[$ln - 1]\n";
2532                                 # we have a preceding printk if it ends
2533                                 # with "\n" ignore it, else it is to blame
2534                                 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2535                                         if ($rawlines[$ln - 1] !~ m{\\n"}) {
2536                                                 $ok = 1;
2537                                         }
2538                                         last;
2539                                 }
2540                         }
2541                         if ($ok == 0) {
2542                                 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2543                                      "printk() should include KERN_ facility level\n" . $herecurr);
2544                         }
2545                 }
2546
2547                 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2548                         my $orig = $1;
2549                         my $level = lc($orig);
2550                         $level = "warn" if ($level eq "warning");
2551                         my $level2 = $level;
2552                         $level2 = "dbg" if ($level eq "debug");
2553                         WARN("PREFER_PR_LEVEL",
2554                              "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
2555                 }
2556
2557                 if ($line =~ /\bpr_warning\s*\(/) {
2558                         WARN("PREFER_PR_LEVEL",
2559                              "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2560                 }
2561
2562                 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2563                         my $orig = $1;
2564                         my $level = lc($orig);
2565                         $level = "warn" if ($level eq "warning");
2566                         $level = "dbg" if ($level eq "debug");
2567                         WARN("PREFER_DEV_LEVEL",
2568                              "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2569                 }
2570
2571 # function brace can't be on same line, except for #defines of do while,
2572 # or if closed on same line
2573                 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2574                     !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
2575                         ERROR("OPEN_BRACE",
2576                               "open brace '{' following function declarations go on the next line\n" . $herecurr);
2577                 }
2578
2579 # open braces for enum, union and struct go on the same line.
2580                 if ($line =~ /^.\s*{/ &&
2581                     $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
2582                         ERROR("OPEN_BRACE",
2583                               "open brace '{' following $1 go on the same line\n" . $hereprev);
2584                 }
2585
2586 # missing space after union, struct or enum definition
2587                 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2588                         if (WARN("SPACING",
2589                                  "missing space after $1 definition\n" . $herecurr) &&
2590                             $fix) {
2591                                 $fixed[$linenr - 1] =~
2592                                     s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2593                         }
2594                 }
2595
2596 # check for spacing round square brackets; allowed:
2597 #  1. with a type on the left -- int [] a;
2598 #  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2599 #  3. inside a curly brace -- = { [0...10] = 5 }
2600                 while ($line =~ /(.*?\s)\[/g) {
2601                         my ($where, $prefix) = ($-[1], $1);
2602                         if ($prefix !~ /$Type\s+$/ &&
2603                             ($where != 0 || $prefix !~ /^.\s+$/) &&
2604                             $prefix !~ /[{,]\s+$/) {
2605                                 if (ERROR("BRACKET_SPACE",
2606                                           "space prohibited before open square bracket '['\n" . $herecurr) &&
2607                                     $fix) {
2608                                     $fixed[$linenr - 1] =~
2609                                         s/^(\+.*?)\s+\[/$1\[/;
2610                                 }
2611                         }
2612                 }
2613
2614 # check for spaces between functions and their parentheses.
2615                 while ($line =~ /($Ident)\s+\(/g) {
2616                         my $name = $1;
2617                         my $ctx_before = substr($line, 0, $-[1]);
2618                         my $ctx = "$ctx_before$name";
2619
2620                         # Ignore those directives where spaces _are_ permitted.
2621                         if ($name =~ /^(?:
2622                                 if|for|while|switch|return|case|
2623                                 volatile|__volatile__|
2624                                 __attribute__|format|__extension__|
2625                                 asm|__asm__)$/x)
2626                         {
2627                         # cpp #define statements have non-optional spaces, ie
2628                         # if there is a space between the name and the open
2629                         # parenthesis it is simply not a parameter group.
2630                         } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
2631
2632                         # cpp #elif statement condition may start with a (
2633                         } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
2634
2635                         # If this whole things ends with a type its most
2636                         # likely a typedef for a function.
2637                         } elsif ($ctx =~ /$Type$/) {
2638
2639                         } else {
2640                                 if (WARN("SPACING",
2641                                          "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2642                                              $fix) {
2643                                         $fixed[$linenr - 1] =~
2644                                             s/\b$name\s+\(/$name\(/;
2645                                 }
2646                         }
2647                 }
2648
2649 # Check operator spacing.
2650                 if (!($line=~/\#\s*include/)) {
2651                         my $fixed_line = "";
2652                         my $line_fixed = 0;
2653
2654                         my $ops = qr{
2655                                 <<=|>>=|<=|>=|==|!=|
2656                                 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2657                                 =>|->|<<|>>|<|>|=|!|~|
2658                                 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2659                                 \?|:
2660                         }x;
2661                         my @elements = split(/($ops|;)/, $opline);
2662
2663 ##                      print("element count: <" . $#elements . ">\n");
2664 ##                      foreach my $el (@elements) {
2665 ##                              print("el: <$el>\n");
2666 ##                      }
2667
2668                         my @fix_elements = ();
2669                         my $off = 0;
2670
2671                         foreach my $el (@elements) {
2672                                 push(@fix_elements, substr($rawline, $off, length($el)));
2673                                 $off += length($el);
2674                         }
2675
2676                         $off = 0;
2677
2678                         my $blank = copy_spacing($opline);
2679
2680                         for (my $n = 0; $n < $#elements; $n += 2) {
2681
2682                                 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2683
2684 ##                              print("n: <$n> good: <$good>\n");
2685
2686                                 $off += length($elements[$n]);
2687
2688                                 # Pick up the preceding and succeeding characters.
2689                                 my $ca = substr($opline, 0, $off);
2690                                 my $cc = '';
2691                                 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2692                                         $cc = substr($opline, $off + length($elements[$n + 1]));
2693                                 }
2694                                 my $cb = "$ca$;$cc";
2695
2696                                 my $a = '';
2697                                 $a = 'V' if ($elements[$n] ne '');
2698                                 $a = 'W' if ($elements[$n] =~ /\s$/);
2699                                 $a = 'C' if ($elements[$n] =~ /$;$/);
2700                                 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2701                                 $a = 'O' if ($elements[$n] eq '');
2702                                 $a = 'E' if ($ca =~ /^\s*$/);
2703
2704                                 my $op = $elements[$n + 1];
2705
2706                                 my $c = '';
2707                                 if (defined $elements[$n + 2]) {
2708                                         $c = 'V' if ($elements[$n + 2] ne '');
2709                                         $c = 'W' if ($elements[$n + 2] =~ /^\s/);
2710                                         $c = 'C' if ($elements[$n + 2] =~ /^$;/);
2711                                         $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2712                                         $c = 'O' if ($elements[$n + 2] eq '');
2713                                         $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
2714                                 } else {
2715                                         $c = 'E';
2716                                 }
2717
2718                                 my $ctx = "${a}x${c}";
2719
2720                                 my $at = "(ctx:$ctx)";
2721
2722                                 my $ptr = substr($blank, 0, $off) . "^";
2723                                 my $hereptr = "$hereline$ptr\n";
2724
2725                                 # Pull out the value of this operator.
2726                                 my $op_type = substr($curr_values, $off + 1, 1);
2727
2728                                 # Get the full operator variant.
2729                                 my $opv = $op . substr($curr_vars, $off, 1);
2730
2731                                 # Ignore operators passed as parameters.
2732                                 if ($op_type ne 'V' &&
2733                                     $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2734
2735 #                               # Ignore comments
2736 #                               } elsif ($op =~ /^$;+$/) {
2737
2738                                 # ; should have either the end of line or a space or \ after it
2739                                 } elsif ($op eq ';') {
2740                                         if ($ctx !~ /.x[WEBC]/ &&
2741                                             $cc !~ /^\\/ && $cc !~ /^;/) {
2742                                                 if (ERROR("SPACING",
2743                                                           "space required after that '$op' $at\n" . $hereptr)) {
2744                                                         $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2745                                                         $line_fixed = 1;
2746                                                 }
2747                                         }
2748
2749                                 # // is a comment
2750                                 } elsif ($op eq '//') {
2751
2752                                 # No spaces for:
2753                                 #   ->
2754                                 #   :   when part of a bitfield
2755                                 } elsif ($op eq '->' || $opv eq ':B') {
2756                                         if ($ctx =~ /Wx.|.xW/) {
2757                                                 if (ERROR("SPACING",
2758                                                           "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2759                                                         $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2760                                                         $line_fixed = 1;
2761                                                         if (defined $fix_elements[$n + 2]) {
2762                                                                 $fix_elements[$n + 2] =~ s/^\s+//;
2763                                                         }
2764                                                 }
2765                                         }
2766
2767                                 # , must have a space on the right.
2768                                 } elsif ($op eq ',') {
2769                                         if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2770                                                 if (ERROR("SPACING",
2771                                                           "space required after that '$op' $at\n" . $hereptr)) {
2772                                                         $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2773                                                         $line_fixed = 1;
2774                                                 }
2775                                         }
2776
2777                                 # '*' as part of a type definition -- reported already.
2778                                 } elsif ($opv eq '*_') {
2779                                         #warn "'*' is part of type\n";
2780
2781                                 # unary operators should have a space before and
2782                                 # none after.  May be left adjacent to another
2783                                 # unary operator, or a cast
2784                                 } elsif ($op eq '!' || $op eq '~' ||
2785                                          $opv eq '*U' || $opv eq '-U' ||
2786                                          $opv eq '&U' || $opv eq '&&U') {
2787                                         if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2788                                                 if (ERROR("SPACING",
2789                                                           "space required before that '$op' $at\n" . $hereptr)) {
2790                                                         $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
2791                                                         $line_fixed = 1;
2792                                                 }
2793                                         }
2794                                         if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
2795                                                 # A unary '*' may be const
2796
2797                                         } elsif ($ctx =~ /.xW/) {
2798                                                 if (ERROR("SPACING",
2799                                                           "space prohibited after that '$op' $at\n" . $hereptr)) {
2800                                                         $fixed_line =~ s/\s+$//;
2801                                                         $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2802                                                         $line_fixed = 1;
2803                                                         if (defined $fix_elements[$n + 2]) {
2804                                                                 $fix_elements[$n + 2] =~ s/^\s+//;
2805                                                         }
2806                                                 }
2807                                         }
2808
2809                                 # unary ++ and unary -- are allowed no space on one side.
2810                                 } elsif ($op eq '++' or $op eq '--') {
2811                                         if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2812                                                 if (ERROR("SPACING",
2813                                                           "space required one side of that '$op' $at\n" . $hereptr)) {
2814                                                         $fixed_line =~ s/\s+$//;
2815                                                         $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2816                                                         $line_fixed = 1;
2817                                                 }
2818                                         }
2819                                         if ($ctx =~ /Wx[BE]/ ||
2820                                             ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2821                                                 if (ERROR("SPACING",
2822                                                           "space prohibited before that '$op' $at\n" . $hereptr)) {
2823                                                         $fixed_line =~ s/\s+$//;
2824                                                         $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2825                                                         $line_fixed = 1;
2826                                                 }
2827                                         }
2828                                         if ($ctx =~ /ExW/) {
2829                                                 if (ERROR("SPACING",
2830                                                           "space prohibited after that '$op' $at\n" . $hereptr)) {
2831                                                         $fixed_line =~ s/\s+$//;
2832                                                         $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2833                                                         $line_fixed = 1;
2834                                                         if (defined $fix_elements[$n + 2]) {
2835                                                                 $fix_elements[$n + 2] =~ s/^\s+//;
2836                                                         }
2837                                                 }
2838                                         }
2839
2840                                 # << and >> may either have or not have spaces both sides
2841                                 } elsif ($op eq '<<' or $op eq '>>' or
2842                                          $op eq '&' or $op eq '^' or $op eq '|' or
2843                                          $op eq '+' or $op eq '-' or
2844                                          $op eq '*' or $op eq '/' or
2845                                          $op eq '%')
2846                                 {
2847                                         if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2848                                                 if (ERROR("SPACING",
2849                                                           "need consistent spacing around '$op' $at\n" . $hereptr)) {
2850                                                         $fixed_line =~ s/\s+$//;
2851                                                         $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2852                                                         $line_fixed = 1;
2853                                                 }
2854                                         }
2855
2856                                 # A colon needs no spaces before when it is
2857                                 # terminating a case value or a label.
2858                                 } elsif ($opv eq ':C' || $opv eq ':L') {
2859                                         if ($ctx =~ /Wx./) {
2860                                                 if (ERROR("SPACING",
2861                                                           "space prohibited before that '$op' $at\n" . $hereptr)) {
2862                                                         $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2863                                                         $line_fixed = 1;
2864                                                 }
2865                                         }
2866
2867                                 # All the others need spaces both sides.
2868                                 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2869                                         my $ok = 0;
2870
2871                                         # Ignore email addresses <foo@bar>
2872                                         if (($op eq '<' &&
2873                                              $cc =~ /^\S+\@\S+>/) ||
2874                                             ($op eq '>' &&
2875                                              $ca =~ /<\S+\@\S+$/))
2876                                         {
2877                                                 $ok = 1;
2878                                         }
2879
2880                                         # Ignore ?:
2881                                         if (($opv eq ':O' && $ca =~ /\?$/) ||
2882                                             ($op eq '?' && $cc =~ /^:/)) {
2883                                                 $ok = 1;
2884                                         }
2885
2886                                         if ($ok == 0) {
2887                                                 if (ERROR("SPACING",
2888                                                           "spaces required around that '$op' $at\n" . $hereptr)) {
2889                                                         $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2890                                                         $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
2891                                                         $line_fixed = 1;
2892                                                 }
2893                                         }
2894                                 }
2895                                 $off += length($elements[$n + 1]);
2896
2897 ##                              print("n: <$n> GOOD: <$good>\n");
2898
2899                                 $fixed_line = $fixed_line . $good;
2900                         }
2901
2902                         if (($#elements % 2) == 0) {
2903                                 $fixed_line = $fixed_line . $fix_elements[$#elements];
2904                         }
2905
2906                         if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
2907                                 $fixed[$linenr - 1] = $fixed_line;
2908                         }
2909
2910
2911                 }
2912
2913 # check for whitespace before a non-naked semicolon
2914                 if ($line =~ /^\+.*\S\s+;/) {
2915                         if (WARN("SPACING",
2916                                  "space prohibited before semicolon\n" . $herecurr) &&
2917                             $fix) {
2918                                 1 while $fixed[$linenr - 1] =~
2919                                     s/^(\+.*\S)\s+;/$1;/;
2920                         }
2921                 }
2922
2923 # check for multiple assignments
2924                 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
2925                         CHK("MULTIPLE_ASSIGNMENTS",
2926                             "multiple assignments should be avoided\n" . $herecurr);
2927                 }
2928
2929 ## # check for multiple declarations, allowing for a function declaration
2930 ## # continuation.
2931 ##              if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2932 ##                  $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2933 ##
2934 ##                      # Remove any bracketed sections to ensure we do not
2935 ##                      # falsly report the parameters of functions.
2936 ##                      my $ln = $line;
2937 ##                      while ($ln =~ s/\([^\(\)]*\)//g) {
2938 ##                      }
2939 ##                      if ($ln =~ /,/) {
2940 ##                              WARN("MULTIPLE_DECLARATION",
2941 ##                                   "declaring multiple variables together should be avoided\n" . $herecurr);
2942 ##                      }
2943 ##              }
2944
2945 #need space before brace following if, while, etc
2946                 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2947                     $line =~ /do{/) {
2948                         if (ERROR("SPACING",
2949                                   "space required before the open brace '{'\n" . $herecurr) &&
2950                             $fix) {
2951                                 $fixed[$linenr - 1] =~
2952                                     s/^(\+.*(?:do|\))){/$1 {/;
2953                         }
2954                 }
2955
2956 ## # check for blank lines before declarations
2957 ##              if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
2958 ##                  $prevrawline =~ /^.\s*$/) {
2959 ##                      WARN("SPACING",
2960 ##                           "No blank lines before declarations\n" . $hereprev);
2961 ##              }
2962 ##
2963
2964 # closing brace should have a space following it when it has anything
2965 # on the line
2966                 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2967                         ERROR("SPACING",
2968                               "space required after that close brace '}'\n" . $herecurr);
2969                 }
2970
2971 # check spacing on square brackets
2972                 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2973                         if (ERROR("SPACING",
2974                                   "space prohibited after that open square bracket '['\n" . $herecurr) &&
2975                             $fix) {
2976                                 $fixed[$linenr - 1] =~
2977                                     s/\[\s+/\[/;
2978                         }
2979                 }
2980                 if ($line =~ /\s\]/) {
2981                         if (ERROR("SPACING",
2982                                   "space prohibited before that close square bracket ']'\n" . $herecurr) &&
2983                             $fix) {
2984                                 $fixed[$linenr - 1] =~
2985                                     s/\s+\]/\]/;
2986                         }
2987                 }
2988
2989 # check spacing on parentheses
2990                 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2991                     $line !~ /for\s*\(\s+;/) {
2992                         if (ERROR("SPACING",
2993                                   "space prohibited after that open parenthesis '('\n" . $herecurr) &&
2994                             $fix) {
2995                                 $fixed[$linenr - 1] =~
2996                                     s/\(\s+/\(/;
2997                         }
2998                 }
2999                 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3000                     $line !~ /for\s*\(.*;\s+\)/ &&
3001                     $line !~ /:\s+\)/) {
3002                         if (ERROR("SPACING",
3003                                   "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3004                             $fix) {
3005                                 $fixed[$linenr - 1] =~
3006                                     s/\s+\)/\)/;
3007                         }
3008                 }
3009
3010 #goto labels aren't indented, allow a single space however
3011                 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3012                    !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3013                         if (WARN("INDENTED_LABEL",
3014                                  "labels should not be indented\n" . $herecurr) &&
3015                             $fix) {
3016                                 $fixed[$linenr - 1] =~
3017                                     s/^(.)\s+/$1/;
3018                         }
3019                 }
3020
3021 # Return is not a function.
3022                 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3023                         my $spacing = $1;
3024                         my $value = $2;
3025
3026                         # Flatten any parentheses
3027                         $value =~ s/\(/ \(/g;
3028                         $value =~ s/\)/\) /g;
3029                         while ($value =~ s/\[[^\[\]]*\]/1/ ||
3030                                $value !~ /(?:$Ident|-?$Constant)\s*
3031                                              $Compare\s*
3032                                              (?:$Ident|-?$Constant)/x &&
3033                                $value =~ s/\([^\(\)]*\)/1/) {
3034                         }
3035 #print "value<$value>\n";
3036                         if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
3037                                 ERROR("RETURN_PARENTHESES",
3038                                       "return is not a function, parentheses are not required\n" . $herecurr);
3039
3040                         } elsif ($spacing !~ /\s+/) {
3041                                 ERROR("SPACING",
3042                                       "space required before the open parenthesis '('\n" . $herecurr);
3043                         }
3044                 }
3045 # Return of what appears to be an errno should normally be -'ve
3046                 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3047                         my $name = $1;
3048                         if ($name ne 'EOF' && $name ne 'ERROR') {
3049                                 WARN("USE_NEGATIVE_ERRNO",
3050                                      "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3051                         }
3052                 }
3053
3054 # Need a space before open parenthesis after if, while etc
3055                 if ($line =~ /\b(if|while|for|switch)\(/) {
3056                         if (ERROR("SPACING",
3057                                   "space required before the open parenthesis '('\n" . $herecurr) &&
3058                             $fix) {
3059                                 $fixed[$linenr - 1] =~
3060                                     s/\b(if|while|for|switch)\(/$1 \(/;
3061                         }
3062                 }
3063
3064 # Check for illegal assignment in if conditional -- and check for trailing
3065 # statements after the conditional.
3066                 if ($line =~ /do\s*(?!{)/) {
3067                         ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3068                                 ctx_statement_block($linenr, $realcnt, 0)
3069                                         if (!defined $stat);
3070                         my ($stat_next) = ctx_statement_block($line_nr_next,
3071                                                 $remain_next, $off_next);
3072                         $stat_next =~ s/\n./\n /g;
3073                         ##print "stat<$stat> stat_next<$stat_next>\n";
3074
3075                         if ($stat_next =~ /^\s*while\b/) {
3076                                 # If the statement carries leading newlines,
3077                                 # then count those as offsets.
3078                                 my ($whitespace) =
3079                                         ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3080                                 my $offset =
3081                                         statement_rawlines($whitespace) - 1;
3082
3083                                 $suppress_whiletrailers{$line_nr_next +
3084                                                                 $offset} = 1;
3085                         }
3086                 }
3087                 if (!defined $suppress_whiletrailers{$linenr} &&
3088                     $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3089                         my ($s, $c) = ($stat, $cond);
3090
3091                         if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3092                                 ERROR("ASSIGN_IN_IF",
3093                                       "do not use assignment in if condition\n" . $herecurr);
3094                         }
3095
3096                         # Find out what is on the end of the line after the
3097                         # conditional.
3098                         substr($s, 0, length($c), '');
3099                         $s =~ s/\n.*//g;
3100                         $s =~ s/$;//g;  # Remove any comments
3101                         if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3102                             $c !~ /}\s*while\s*/)
3103                         {
3104                                 # Find out how long the conditional actually is.
3105                                 my @newlines = ($c =~ /\n/gs);
3106                                 my $cond_lines = 1 + $#newlines;
3107                                 my $stat_real = '';
3108
3109                                 $stat_real = raw_line($linenr, $cond_lines)
3110                                                         . "\n" if ($cond_lines);
3111                                 if (defined($stat_real) && $cond_lines > 1) {
3112                                         $stat_real = "[...]\n$stat_real";
3113                                 }
3114
3115                                 ERROR("TRAILING_STATEMENTS",
3116                                       "trailing statements should be on next line\n" . $herecurr . $stat_real);
3117                         }
3118                 }
3119
3120 # Check for bitwise tests written as boolean
3121                 if ($line =~ /
3122                         (?:
3123                                 (?:\[|\(|\&\&|\|\|)
3124                                 \s*0[xX][0-9]+\s*
3125                                 (?:\&\&|\|\|)
3126                         |
3127                                 (?:\&\&|\|\|)
3128                                 \s*0[xX][0-9]+\s*
3129                                 (?:\&\&|\|\||\)|\])
3130                         )/x)
3131                 {
3132                         WARN("HEXADECIMAL_BOOLEAN_TEST",
3133                              "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
3134                 }
3135
3136 # if and else should not have general statements after it
3137                 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3138                         my $s = $1;
3139                         $s =~ s/$;//g;  # Remove any comments
3140                         if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3141                                 ERROR("TRAILING_STATEMENTS",
3142                                       "trailing statements should be on next line\n" . $herecurr);
3143                         }
3144                 }
3145 # if should not continue a brace
3146                 if ($line =~ /}\s*if\b/) {
3147                         ERROR("TRAILING_STATEMENTS",
3148                               "trailing statements should be on next line\n" .
3149                                 $herecurr);
3150                 }
3151 # case and default should not have general statements after them
3152                 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3153                     $line !~ /\G(?:
3154                         (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
3155                         \s*return\s+
3156                     )/xg)
3157                 {
3158                         ERROR("TRAILING_STATEMENTS",
3159                               "trailing statements should be on next line\n" . $herecurr);
3160                 }
3161
3162                 # Check for }<nl>else {, these must be at the same
3163                 # indent level to be relevant to each other.
3164                 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3165                                                 $previndent == $indent) {
3166                         ERROR("ELSE_AFTER_BRACE",
3167                               "else should follow close brace '}'\n" . $hereprev);
3168                 }
3169
3170                 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3171                                                 $previndent == $indent) {
3172                         my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3173
3174                         # Find out what is on the end of the line after the
3175                         # conditional.
3176                         substr($s, 0, length($c), '');
3177                         $s =~ s/\n.*//g;
3178
3179                         if ($s =~ /^\s*;/) {
3180                                 ERROR("WHILE_AFTER_BRACE",
3181                                       "while should follow close brace '}'\n" . $hereprev);
3182                         }
3183                 }
3184
3185 #Specific variable tests
3186                 while ($line =~ m{($Constant|$Lval)}g) {
3187                         my $var = $1;
3188
3189 #gcc binary extension
3190                         if ($var =~ /^$Binary$/) {
3191                                 WARN("GCC_BINARY_CONSTANT",
3192                                      "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
3193                         }
3194
3195 #CamelCase
3196                         if ($var !~ /^$Constant$/ &&
3197                             $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
3198 #Ignore Page<foo> variants
3199                             $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
3200 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
3201                             $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
3202                             !defined $camelcase{$var}) {
3203                                 $camelcase{$var} = 1;
3204                                 CHK("CAMELCASE",
3205                                     "Avoid CamelCase: <$var>\n" . $herecurr);
3206                         }
3207                 }
3208
3209 #no spaces allowed after \ in define
3210                 if ($line=~/\#\s*define.*\\\s$/) {
3211                         WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3212                              "Whitepspace after \\ makes next lines useless\n" . $herecurr);
3213                 }
3214
3215 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
3216                 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
3217                         my $file = "$1.h";
3218                         my $checkfile = "include/linux/$file";
3219                         if (-f "$root/$checkfile" &&
3220                             $realfile ne $checkfile &&
3221                             $1 !~ /$allowed_asm_includes/)
3222                         {
3223                                 if ($realfile =~ m{^arch/}) {
3224                                         CHK("ARCH_INCLUDE_LINUX",
3225                                             "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3226                                 } else {
3227                                         WARN("INCLUDE_LINUX",
3228                                              "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
3229                                 }
3230                         }
3231                 }
3232
3233 # multi-statement macros should be enclosed in a do while loop, grab the
3234 # first statement and ensure its the whole macro if its not enclosed
3235 # in a known good container
3236                 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3237                     $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
3238                         my $ln = $linenr;
3239                         my $cnt = $realcnt;
3240                         my ($off, $dstat, $dcond, $rest);
3241                         my $ctx = '';
3242                         ($dstat, $dcond, $ln, $cnt, $off) =
3243                                 ctx_statement_block($linenr, $realcnt, 0);
3244                         $ctx = $dstat;
3245                         #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
3246                         #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
3247
3248                         $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
3249                         $dstat =~ s/$;//g;
3250                         $dstat =~ s/\\\n.//g;
3251                         $dstat =~ s/^\s*//s;
3252                         $dstat =~ s/\s*$//s;
3253
3254                         # Flatten any parentheses and braces
3255                         while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3256                                $dstat =~ s/\{[^\{\}]*\}/1/ ||
3257                                $dstat =~ s/\[[^\[\]]*\]/1/)
3258                         {
3259                         }
3260
3261                         # Flatten any obvious string concatentation.
3262                         while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3263                                $dstat =~ s/$Ident\s*("X*")/$1/)
3264                         {
3265                         }
3266
3267                         my $exceptions = qr{
3268                                 $Declare|
3269                                 module_param_named|
3270                                 MODULE_PARM_DESC|
3271                                 DECLARE_PER_CPU|
3272                                 DEFINE_PER_CPU|
3273                                 __typeof__\(|
3274                                 union|
3275                                 struct|
3276                                 \.$Ident\s*=\s*|
3277                                 ^\"|\"$
3278                         }x;
3279                         #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
3280                         if ($dstat ne '' &&
3281                             $dstat !~ /^(?:$Ident|-?$Constant),$/ &&                    # 10, // foo(),
3282                             $dstat !~ /^(?:$Ident|-?$Constant);$/ &&                    # foo();
3283                             $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&          # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
3284                             $dstat !~ /^'X'$/ &&                                        # character constants
3285                             $dstat !~ /$exceptions/ &&
3286                             $dstat !~ /^\.$Ident\s*=/ &&                                # .foo =
3287                             $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&          # stringification #foo
3288                             $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&       # do {...} while (...); // do {...} while (...)
3289                             $dstat !~ /^for\s*$Constant$/ &&                            # for (...)
3290                             $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&   # for (...) bar()
3291                             $dstat !~ /^do\s*{/ &&                                      # do {...
3292                             $dstat !~ /^\({/)                                           # ({...
3293                         {
3294                                 $ctx =~ s/\n*$//;
3295                                 my $herectx = $here . "\n";
3296                                 my $cnt = statement_rawlines($ctx);
3297
3298                                 for (my $n = 0; $n < $cnt; $n++) {
3299                                         $herectx .= raw_line($linenr, $n) . "\n";
3300                                 }
3301
3302                                 if ($dstat =~ /;/) {
3303                                         ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3304                                               "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3305                                 } else {
3306                                         ERROR("COMPLEX_MACRO",
3307                                               "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3308                                 }
3309                         }
3310
3311 # check for line continuations outside of #defines, preprocessor #, and asm
3312
3313                 } else {
3314                         if ($prevline !~ /^..*\\$/ &&
3315                             $line !~ /^\+\s*\#.*\\$/ &&         # preprocessor
3316                             $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&   # asm
3317                             $line =~ /^\+.*\\$/) {
3318                                 WARN("LINE_CONTINUATIONS",
3319                                      "Avoid unnecessary line continuations\n" . $herecurr);
3320                         }
3321                 }
3322
3323 # do {} while (0) macro tests:
3324 # single-statement macros do not need to be enclosed in do while (0) loop,
3325 # macro should not end with a semicolon
3326                 if ($^V && $^V ge 5.10.0 &&
3327                     $realfile !~ m@/vmlinux.lds.h$@ &&
3328                     $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3329                         my $ln = $linenr;
3330                         my $cnt = $realcnt;
3331                         my ($off, $dstat, $dcond, $rest);
3332                         my $ctx = '';
3333                         ($dstat, $dcond, $ln, $cnt, $off) =
3334                                 ctx_statement_block($linenr, $realcnt, 0);
3335                         $ctx = $dstat;
3336
3337                         $dstat =~ s/\\\n.//g;
3338
3339                         if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3340                                 my $stmts = $2;
3341                                 my $semis = $3;
3342
3343                                 $ctx =~ s/\n*$//;
3344                                 my $cnt = statement_rawlines($ctx);
3345                                 my $herectx = $here . "\n";
3346
3347                                 for (my $n = 0; $n < $cnt; $n++) {
3348                                         $herectx .= raw_line($linenr, $n) . "\n";
3349                                 }
3350
3351                                 if (($stmts =~ tr/;/;/) == 1 &&
3352                                     $stmts !~ /^\s*(if|while|for|switch)\b/) {
3353                                         WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3354                                              "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3355                                 }
3356                                 if (defined $semis && $semis ne "") {
3357                                         WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3358                                              "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3359                                 }
3360                         }
3361                 }
3362
3363 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3364 # all assignments may have only one of the following with an assignment:
3365 #       .
3366 #       ALIGN(...)
3367 #       VMLINUX_SYMBOL(...)
3368                 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
3369                         WARN("MISSING_VMLINUX_SYMBOL",
3370                              "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
3371                 }
3372
3373 # check for redundant bracing round if etc
3374                 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3375                         my ($level, $endln, @chunks) =
3376                                 ctx_statement_full($linenr, $realcnt, 1);
3377                         #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3378                         #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3379                         if ($#chunks > 0 && $level == 0) {
3380                                 my @allowed = ();
3381                                 my $allow = 0;
3382                                 my $seen = 0;
3383                                 my $herectx = $here . "\n";
3384                                 my $ln = $linenr - 1;
3385                                 for my $chunk (@chunks) {
3386                                         my ($cond, $block) = @{$chunk};
3387
3388                                         # If the condition carries leading newlines, then count those as offsets.
3389                                         my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3390                                         my $offset = statement_rawlines($whitespace) - 1;
3391
3392                                         $allowed[$allow] = 0;
3393                                         #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3394
3395                                         # We have looked at and allowed this specific line.
3396                                         $suppress_ifbraces{$ln + $offset} = 1;
3397
3398                                         $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
3399                                         $ln += statement_rawlines($block) - 1;
3400
3401                                         substr($block, 0, length($cond), '');
3402
3403                                         $seen++ if ($block =~ /^\s*{/);
3404
3405                                         #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
3406                                         if (statement_lines($cond) > 1) {
3407                                                 #print "APW: ALLOWED: cond<$cond>\n";
3408                                                 $allowed[$allow] = 1;
3409                                         }
3410                                         if ($block =~/\b(?:if|for|while)\b/) {
3411                                                 #print "APW: ALLOWED: block<$block>\n";
3412                                                 $allowed[$allow] = 1;
3413                                         }
3414                                         if (statement_block_size($block) > 1) {
3415                                                 #print "APW: ALLOWED: lines block<$block>\n";
3416                                                 $allowed[$allow] = 1;
3417                                         }
3418                                         $allow++;
3419                                 }
3420                                 if ($seen) {
3421                                         my $sum_allowed = 0;
3422                                         foreach (@allowed) {
3423                                                 $sum_allowed += $_;
3424                                         }
3425                                         if ($sum_allowed == 0) {
3426                                                 WARN("BRACES",
3427                                                      "braces {} are not necessary for any arm of this statement\n" . $herectx);
3428                                         } elsif ($sum_allowed != $allow &&
3429                                                  $seen != $allow) {
3430                                                 CHK("BRACES",
3431                                                     "braces {} should be used on all arms of this statement\n" . $herectx);
3432                                         }
3433                                 }
3434                         }
3435                 }
3436                 if (!defined $suppress_ifbraces{$linenr - 1} &&
3437                                         $line =~ /\b(if|while|for|else)\b/) {
3438                         my $allowed = 0;
3439
3440                         # Check the pre-context.
3441                         if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3442                                 #print "APW: ALLOWED: pre<$1>\n";
3443                                 $allowed = 1;
3444                         }
3445
3446                         my ($level, $endln, @chunks) =
3447                                 ctx_statement_full($linenr, $realcnt, $-[0]);
3448
3449                         # Check the condition.
3450                         my ($cond, $block) = @{$chunks[0]};
3451                         #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
3452                         if (defined $cond) {
3453                                 substr($block, 0, length($cond), '');
3454                         }
3455                         if (statement_lines($cond) > 1) {
3456                                 #print "APW: ALLOWED: cond<$cond>\n";
3457                                 $allowed = 1;
3458                         }
3459                         if ($block =~/\b(?:if|for|while)\b/) {
3460                                 #print "APW: ALLOWED: block<$block>\n";
3461                                 $allowed = 1;
3462                         }
3463                         if (statement_block_size($block) > 1) {
3464                                 #print "APW: ALLOWED: lines block<$block>\n";
3465                                 $allowed = 1;
3466                         }
3467                         # Check the post-context.
3468                         if (defined $chunks[1]) {
3469                                 my ($cond, $block) = @{$chunks[1]};
3470                                 if (defined $cond) {
3471                                         substr($block, 0, length($cond), '');
3472                                 }
3473                                 if ($block =~ /^\s*\{/) {
3474                                         #print "APW: ALLOWED: chunk-1 block<$block>\n";
3475                                         $allowed = 1;
3476                                 }
3477                         }
3478                         if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3479                                 my $herectx = $here . "\n";
3480                                 my $cnt = statement_rawlines($block);
3481
3482                                 for (my $n = 0; $n < $cnt; $n++) {
3483                                         $herectx .= raw_line($linenr, $n) . "\n";
3484                                 }
3485
3486                                 WARN("BRACES",
3487                                      "braces {} are not necessary for single statement blocks\n" . $herectx);
3488                         }
3489                 }
3490
3491 # check for unnecessary blank lines around braces
3492                 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
3493                         CHK("BRACES",
3494                             "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3495                 }
3496                 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
3497                         CHK("BRACES",
3498                             "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3499                 }
3500
3501 # no volatiles please
3502                 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3503                 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
3504                         WARN("VOLATILE",
3505                              "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
3506                 }
3507
3508 # warn about #if 0
3509                 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
3510                         CHK("REDUNDANT_CODE",
3511                             "if this code is redundant consider removing it\n" .
3512                                 $herecurr);
3513                 }
3514
3515 # check for needless "if (<foo>) fn(<foo>)" uses
3516                 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3517                         my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3518                         if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3519                                 WARN('NEEDLESS_IF',
3520                                      "$1(NULL) is safe this check is probably not required\n" . $hereprev);
3521                         }
3522                 }
3523
3524 # prefer usleep_range over udelay
3525                 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
3526                         # ignore udelay's < 10, however
3527                         if (! ($1 < 10) ) {
3528                                 CHK("USLEEP_RANGE",
3529                                     "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
3530                         }
3531                 }
3532
3533 # warn about unexpectedly long msleep's
3534                 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3535                         if ($1 < 20) {
3536                                 WARN("MSLEEP",
3537                                      "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
3538                         }
3539                 }
3540
3541 # check for comparisons of jiffies
3542                 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3543                         WARN("JIFFIES_COMPARISON",
3544                              "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3545                 }
3546
3547 # check for comparisons of get_jiffies_64()
3548                 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3549                         WARN("JIFFIES_COMPARISON",
3550                              "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3551                 }
3552
3553 # warn about #ifdefs in C files
3554 #               if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
3555 #                       print "#ifdef in C files should be avoided\n";
3556 #                       print "$herecurr";
3557 #                       $clean = 0;
3558 #               }
3559
3560 # warn about spacing in #ifdefs
3561                 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3562                         if (ERROR("SPACING",
3563                                   "exactly one space required after that #$1\n" . $herecurr) &&
3564                             $fix) {
3565                                 $fixed[$linenr - 1] =~
3566                                     s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3567                         }
3568
3569                 }
3570
3571 # check for spinlock_t definitions without a comment.
3572                 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3573                     $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
3574                         my $which = $1;
3575                         if (!ctx_has_comment($first_line, $linenr)) {
3576                                 CHK("UNCOMMENTED_DEFINITION",
3577                                     "$1 definition without comment\n" . $herecurr);
3578                         }
3579                 }
3580 # check for memory barriers without a comment.
3581                 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3582                         if (!ctx_has_comment($first_line, $linenr)) {
3583                                 CHK("MEMORY_BARRIER",
3584                                     "memory barrier without comment\n" . $herecurr);
3585                         }
3586                 }
3587 # check of hardware specific defines
3588                 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
3589                         CHK("ARCH_DEFINES",
3590                             "architecture specific defines should be avoided\n" .  $herecurr);
3591                 }
3592
3593 # Check that the storage class is at the beginning of a declaration
3594                 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
3595                         WARN("STORAGE_CLASS",
3596                              "storage class should be at the beginning of the declaration\n" . $herecurr)
3597                 }
3598
3599 # check the location of the inline attribute, that it is between
3600 # storage class and type.
3601                 if ($line =~ /\b$Type\s+$Inline\b/ ||
3602                     $line =~ /\b$Inline\s+$Storage\b/) {
3603                         ERROR("INLINE_LOCATION",
3604                               "inline keyword should sit between storage class and type\n" . $herecurr);
3605                 }
3606
3607 # Check for __inline__ and __inline, prefer inline
3608                 if ($line =~ /\b(__inline__|__inline)\b/) {
3609                         WARN("INLINE",
3610                              "plain inline is preferred over $1\n" . $herecurr);
3611                 }
3612
3613 # Check for __attribute__ packed, prefer __packed
3614                 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3615                         WARN("PREFER_PACKED",
3616                              "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3617                 }
3618
3619 # Check for __attribute__ aligned, prefer __aligned
3620                 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3621                         WARN("PREFER_ALIGNED",
3622                              "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3623                 }
3624
3625 # Check for __attribute__ format(printf, prefer __printf
3626                 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3627                         WARN("PREFER_PRINTF",
3628                              "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3629                 }
3630
3631 # Check for __attribute__ format(scanf, prefer __scanf
3632                 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3633                         WARN("PREFER_SCANF",
3634                              "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3635                 }
3636
3637 # check for sizeof(&)
3638                 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3639                         WARN("SIZEOF_ADDRESS",
3640                              "sizeof(& should be avoided\n" . $herecurr);
3641                 }
3642
3643 # check for sizeof without parenthesis
3644                 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3645                         WARN("SIZEOF_PARENTHESIS",
3646                              "sizeof $1 should be sizeof($1)\n" . $herecurr);
3647                 }
3648
3649 # check for line continuations in quoted strings with odd counts of "
3650                 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3651                         WARN("LINE_CONTINUATIONS",
3652                              "Avoid line continuations in quoted strings\n" . $herecurr);
3653                 }
3654
3655 # check for struct spinlock declarations
3656                 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3657                         WARN("USE_SPINLOCK_T",
3658                              "struct spinlock should be spinlock_t\n" . $herecurr);
3659                 }
3660
3661 # check for seq_printf uses that could be seq_puts
3662                 if ($line =~ /\bseq_printf\s*\(/) {
3663                         my $fmt = get_quoted_string($line, $rawline);
3664                         if ($fmt !~ /[^\\]\%/) {
3665                                 WARN("PREFER_SEQ_PUTS",
3666                                      "Prefer seq_puts to seq_printf\n" . $herecurr);
3667                         }
3668                 }
3669
3670 # Check for misused memsets
3671                 if ($^V && $^V ge 5.10.0 &&
3672                     defined $stat &&
3673                     $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3674
3675                         my $ms_addr = $2;
3676                         my $ms_val = $7;
3677                         my $ms_size = $12;
3678
3679                         if ($ms_size =~ /^(0x|)0$/i) {
3680                                 ERROR("MEMSET",
3681                                       "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3682                         } elsif ($ms_size =~ /^(0x|)1$/i) {
3683                                 WARN("MEMSET",
3684                                      "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3685                         }
3686                 }
3687
3688 # typecasts on min/max could be min_t/max_t
3689                 if ($^V && $^V ge 5.10.0 &&
3690                     defined $stat &&
3691                     $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3692                         if (defined $2 || defined $7) {
3693                                 my $call = $1;
3694                                 my $cast1 = deparenthesize($2);
3695                                 my $arg1 = $3;
3696                                 my $cast2 = deparenthesize($7);
3697                                 my $arg2 = $8;
3698                                 my $cast;
3699
3700                                 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3701                                         $cast = "$cast1 or $cast2";
3702                                 } elsif ($cast1 ne "") {
3703                                         $cast = $cast1;
3704                                 } else {
3705                                         $cast = $cast2;
3706                                 }
3707                                 WARN("MINMAX",
3708                                      "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3709                         }
3710                 }
3711
3712 # check usleep_range arguments
3713                 if ($^V && $^V ge 5.10.0 &&
3714                     defined $stat &&
3715                     $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3716                         my $min = $1;
3717                         my $max = $7;
3718                         if ($min eq $max) {
3719                                 WARN("USLEEP_RANGE",
3720                                      "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3721                         } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3722                                  $min > $max) {
3723                                 WARN("USLEEP_RANGE",
3724                                      "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3725                         }
3726                 }
3727
3728 # check for new externs in .c files.
3729                 if ($realfile =~ /\.c$/ && defined $stat &&
3730                     $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
3731                 {
3732                         my $function_name = $1;
3733                         my $paren_space = $2;
3734
3735                         my $s = $stat;
3736                         if (defined $cond) {
3737                                 substr($s, 0, length($cond), '');
3738                         }
3739                         if ($s =~ /^\s*;/ &&
3740                             $function_name ne 'uninitialized_var')
3741                         {
3742                                 WARN("AVOID_EXTERNS",
3743                                      "externs should be avoided in .c files\n" .  $herecurr);
3744                         }
3745
3746                         if ($paren_space =~ /\n/) {
3747                                 WARN("FUNCTION_ARGUMENTS",
3748                                      "arguments for function declarations should follow identifier\n" . $herecurr);
3749                         }
3750
3751                 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3752                     $stat =~ /^.\s*extern\s+/)
3753                 {
3754                         WARN("AVOID_EXTERNS",
3755                              "externs should be avoided in .c files\n" .  $herecurr);
3756                 }
3757
3758 # checks for new __setup's
3759                 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3760                         my $name = $1;
3761
3762                         if (!grep(/$name/, @setup_docs)) {
3763                                 CHK("UNDOCUMENTED_SETUP",
3764                                     "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
3765                         }
3766                 }
3767
3768 # check for pointless casting of kmalloc return
3769                 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
3770                         WARN("UNNECESSARY_CASTS",
3771                              "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
3772                 }
3773
3774 # alloc style
3775 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3776                 if ($^V && $^V ge 5.10.0 &&
3777                     $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3778                         CHK("ALLOC_SIZEOF_STRUCT",
3779                             "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3780                 }
3781
3782 # check for krealloc arg reuse
3783                 if ($^V && $^V ge 5.10.0 &&
3784                     $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3785                         WARN("KREALLOC_ARG_REUSE",
3786                              "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3787                 }
3788
3789 # check for alloc argument mismatch
3790                 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3791                         WARN("ALLOC_ARRAY_ARGS",
3792                              "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3793                 }
3794
3795 # check for multiple semicolons
3796                 if ($line =~ /;\s*;\s*$/) {
3797                         WARN("ONE_SEMICOLON",
3798                              "Statements terminations use 1 semicolon\n" . $herecurr);
3799                 }
3800
3801 # check for switch/default statements without a break;
3802                 if ($^V && $^V ge 5.10.0 &&
3803                     defined $stat &&
3804                     $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3805                         my $ctx = '';
3806                         my $herectx = $here . "\n";
3807                         my $cnt = statement_rawlines($stat);
3808                         for (my $n = 0; $n < $cnt; $n++) {
3809                                 $herectx .= raw_line($linenr, $n) . "\n";
3810                         }
3811                         WARN("DEFAULT_NO_BREAK",
3812                              "switch default: should use break\n" . $herectx);
3813                 }
3814
3815 # check for gcc specific __FUNCTION__
3816                 if ($line =~ /__FUNCTION__/) {
3817                         WARN("USE_FUNC",
3818                              "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
3819                 }
3820
3821 # check for use of yield()
3822                 if ($line =~ /\byield\s*\(\s*\)/) {
3823                         WARN("YIELD",
3824                              "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
3825                 }
3826
3827 # check for comparisons against true and false
3828                 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3829                         my $lead = $1;
3830                         my $arg = $2;
3831                         my $test = $3;
3832                         my $otype = $4;
3833                         my $trail = $5;
3834                         my $op = "!";
3835
3836                         ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3837
3838                         my $type = lc($otype);
3839                         if ($type =~ /^(?:true|false)$/) {
3840                                 if (("$test" eq "==" && "$type" eq "true") ||
3841                                     ("$test" eq "!=" && "$type" eq "false")) {
3842                                         $op = "";
3843                                 }
3844
3845                                 CHK("BOOL_COMPARISON",
3846                                     "Using comparison to $otype is error prone\n" . $herecurr);
3847
3848 ## maybe suggesting a correct construct would better
3849 ##                                  "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3850
3851                         }
3852                 }
3853
3854 # check for semaphores initialized locked
3855                 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3856                         WARN("CONSIDER_COMPLETION",
3857                              "consider using a completion\n" . $herecurr);
3858                 }
3859
3860 # recommend kstrto* over simple_strto* and strict_strto*
3861                 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
3862                         WARN("CONSIDER_KSTRTO",
3863                              "$1 is obsolete, use k$3 instead\n" . $herecurr);
3864                 }
3865
3866 # check for __initcall(), use device_initcall() explicitly please
3867                 if ($line =~ /^.\s*__initcall\s*\(/) {
3868                         WARN("USE_DEVICE_INITCALL",
3869                              "please use device_initcall() instead of __initcall()\n" . $herecurr);
3870                 }
3871
3872 # check for various ops structs, ensure they are const.
3873                 my $struct_ops = qr{acpi_dock_ops|
3874                                 address_space_operations|
3875                                 backlight_ops|
3876                                 block_device_operations|
3877                                 dentry_operations|
3878                                 dev_pm_ops|
3879                                 dma_map_ops|
3880                                 extent_io_ops|
3881                                 file_lock_operations|
3882                                 file_operations|
3883                                 hv_ops|
3884                                 ide_dma_ops|
3885                                 intel_dvo_dev_ops|
3886                                 item_operations|
3887                                 iwl_ops|
3888                                 kgdb_arch|
3889                                 kgdb_io|
3890                                 kset_uevent_ops|
3891                                 lock_manager_operations|
3892                                 microcode_ops|
3893                                 mtrr_ops|
3894                                 neigh_ops|
3895                                 nlmsvc_binding|
3896                                 pci_raw_ops|
3897                                 pipe_buf_operations|
3898                                 platform_hibernation_ops|
3899                                 platform_suspend_ops|
3900                                 proto_ops|
3901                                 rpc_pipe_ops|
3902                                 seq_operations|
3903                                 snd_ac97_build_ops|
3904                                 soc_pcmcia_socket_ops|
3905                                 stacktrace_ops|
3906                                 sysfs_ops|
3907                                 tty_operations|
3908                                 usb_mon_operations|
3909                                 wd_ops}x;
3910                 if ($line !~ /\bconst\b/ &&
3911                     $line =~ /\bstruct\s+($struct_ops)\b/) {
3912                         WARN("CONST_STRUCT",
3913                              "struct $1 should normally be const\n" .
3914                                 $herecurr);
3915                 }
3916
3917 # use of NR_CPUS is usually wrong
3918 # ignore definitions of NR_CPUS and usage to define arrays as likely right
3919                 if ($line =~ /\bNR_CPUS\b/ &&
3920                     $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3921                     $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
3922                     $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3923                     $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3924                     $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
3925                 {
3926                         WARN("NR_CPUS",
3927                              "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
3928                 }
3929
3930 # check for %L{u,d,i} in strings
3931                 my $string;
3932                 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3933                         $string = substr($rawline, $-[1], $+[1] - $-[1]);
3934                         $string =~ s/%%/__/g;
3935                         if ($string =~ /(?<!%)%L[udi]/) {
3936                                 WARN("PRINTF_L",
3937                                      "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
3938                                 last;
3939                         }
3940                 }
3941
3942 # whine mightly about in_atomic
3943                 if ($line =~ /\bin_atomic\s*\(/) {
3944                         if ($realfile =~ m@^drivers/@) {
3945                                 ERROR("IN_ATOMIC",
3946                                       "do not use in_atomic in drivers\n" . $herecurr);
3947                         } elsif ($realfile !~ m@^kernel/@) {
3948                                 WARN("IN_ATOMIC",
3949                                      "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
3950                         }
3951                 }
3952
3953 # check for lockdep_set_novalidate_class
3954                 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3955                     $line =~ /__lockdep_no_validate__\s*\)/ ) {
3956                         if ($realfile !~ m@^kernel/lockdep@ &&
3957                             $realfile !~ m@^include/linux/lockdep@ &&
3958                             $realfile !~ m@^drivers/base/core@) {
3959                                 ERROR("LOCKDEP",
3960                                       "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
3961                         }
3962                 }
3963
3964                 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3965                     $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
3966                         WARN("EXPORTED_WORLD_WRITABLE",
3967                              "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3968                 }
3969         }
3970
3971         # If we have no input at all, then there is nothing to report on
3972         # so just keep quiet.
3973         if ($#rawlines == -1) {
3974                 exit(0);
3975         }
3976
3977         # In mailback mode only produce a report in the negative, for
3978         # things that appear to be patches.
3979         if ($mailback && ($clean == 1 || !$is_patch)) {
3980                 exit(0);
3981         }
3982
3983         # This is not a patch, and we are are in 'no-patch' mode so
3984         # just keep quiet.
3985         if (!$chk_patch && !$is_patch) {
3986                 exit(0);
3987         }
3988
3989         if (!$is_patch) {
3990                 ERROR("NOT_UNIFIED_DIFF",
3991                       "Does not appear to be a unified-diff format patch\n");
3992         }
3993         if ($is_patch && $chk_signoff && $signoff == 0) {
3994                 ERROR("MISSING_SIGN_OFF",
3995                       "Missing Signed-off-by: line(s)\n");
3996         }
3997
3998         print report_dump();
3999         if ($summary && !($clean == 1 && $quiet == 1)) {
4000                 print "$filename " if ($summary_file);
4001                 print "total: $cnt_error errors, $cnt_warn warnings, " .
4002                         (($check)? "$cnt_chk checks, " : "") .
4003                         "$cnt_lines lines checked\n";
4004                 print "\n" if ($quiet == 0);
4005         }
4006
4007         if ($quiet == 0) {
4008
4009                 if ($^V lt 5.10.0) {
4010                         print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4011                         print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4012                 }
4013
4014                 # If there were whitespace errors which cleanpatch can fix
4015                 # then suggest that.
4016                 if ($rpt_cleaners) {
4017                         print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4018                         print "      scripts/cleanfile\n\n";
4019                         $rpt_cleaners = 0;
4020                 }
4021         }
4022
4023         if ($quiet == 0 && keys %ignore_type) {
4024             print "NOTE: Ignored message types:";
4025             foreach my $ignore (sort keys %ignore_type) {
4026                 print " $ignore";
4027             }
4028             print "\n\n";
4029         }
4030
4031         if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4032                 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4033                 my $linecount = 0;
4034                 my $f;
4035
4036                 open($f, '>', $newfile)
4037                     or die "$P: Can't open $newfile for write\n";
4038                 foreach my $fixed_line (@fixed) {
4039                         $linecount++;
4040                         if ($file) {
4041                                 if ($linecount > 3) {
4042                                         $fixed_line =~ s/^\+//;
4043                                         print $f $fixed_line. "\n";
4044                                 }
4045                         } else {
4046                                 print $f $fixed_line . "\n";
4047                         }
4048                 }
4049                 close($f);
4050
4051                 if (!$quiet) {
4052                         print << "EOM";
4053 Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4054
4055 Do _NOT_ trust the results written to this file.
4056 Do _NOT_ submit these changes without inspecting them for correctness.
4057
4058 This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4059 No warranties, expressed or implied...
4060
4061 EOM
4062                 }
4063         }
4064
4065         if ($clean == 1 && $quiet == 0) {
4066                 print "$vname has no obvious style problems and is ready for submission.\n"
4067         }
4068         if ($clean == 0 && $quiet == 0) {
4069                 print << "EOM";
4070 $vname has style problems, please review.
4071
4072 If any of these errors are false positives, please report
4073 them to the maintainer, see CHECKPATCH in MAINTAINERS.
4074 EOM
4075         }
4076
4077         return $clean;
4078 }