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