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