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