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