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