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