net/9p/trans_fd.c:p9_trans_fd_init(): module_init functions should return 0 on success
[linux-2.6-block.git] / scripts / checkpatch.pl
CommitLineData
0a920b5b
AW
1#!/usr/bin/perl -w
2# (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
00df344f 3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
0a920b5b
AW
4# (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc)
5# Licensed under the terms of the GNU GPL License version 2
6
7use strict;
8
9my $P = $0;
00df344f 10$P =~ s@.*/@@g;
0a920b5b 11
cf655043 12my $V = '0.15';
0a920b5b
AW
13
14use Getopt::Long qw(:config no_auto_abbrev);
15
16my $quiet = 0;
17my $tree = 1;
18my $chk_signoff = 1;
19my $chk_patch = 1;
653d4876 20my $tst_type = 0;
6c72ffaa 21my $emacs = 0;
8905a67c 22my $terse = 0;
6c72ffaa
AW
23my $file = 0;
24my $check = 0;
8905a67c
AW
25my $summary = 1;
26my $mailback = 0;
13214adf 27my $summary_file = 0;
6c72ffaa 28my $root;
c2fdda0d 29my %debug;
0a920b5b 30GetOptions(
6c72ffaa 31 'q|quiet+' => \$quiet,
0a920b5b
AW
32 'tree!' => \$tree,
33 'signoff!' => \$chk_signoff,
34 'patch!' => \$chk_patch,
6c72ffaa 35 'emacs!' => \$emacs,
8905a67c 36 'terse!' => \$terse,
6c72ffaa
AW
37 'file!' => \$file,
38 'subjective!' => \$check,
39 'strict!' => \$check,
40 'root=s' => \$root,
8905a67c
AW
41 'summary!' => \$summary,
42 'mailback!' => \$mailback,
13214adf
AW
43 'summary-file!' => \$summary_file,
44
c2fdda0d 45 'debug=s' => \%debug,
13214adf 46 'test-type!' => \$tst_type,
0a920b5b
AW
47) or exit;
48
49my $exit = 0;
50
51if ($#ARGV < 0) {
00df344f 52 print "usage: $P [options] patchfile\n";
0a920b5b 53 print "version: $V\n";
13214adf
AW
54 print "options: -q => quiet\n";
55 print " --no-tree => run without a kernel tree\n";
56 print " --terse => one line per report\n";
57 print " --emacs => emacs compile window format\n";
58 print " --file => check a source file\n";
59 print " --strict => enable more subjective tests\n";
60 print " --root => path to the kernel tree root\n";
61 print " --no-summary => suppress the per-file summary\n";
62 print " --summary-file => include the filename in summary\n";
0a920b5b
AW
63 exit(1);
64}
65
c2fdda0d
AW
66my $dbg_values = 0;
67my $dbg_possible = 0;
68for my $key (keys %debug) {
69 eval "\${dbg_$key} = '$debug{$key}';"
70}
71
8905a67c
AW
72if ($terse) {
73 $emacs = 1;
74 $quiet++;
75}
76
6c72ffaa
AW
77if ($tree) {
78 if (defined $root) {
79 if (!top_of_kernel_tree($root)) {
80 die "$P: $root: --root does not point at a valid tree\n";
81 }
82 } else {
83 if (top_of_kernel_tree('.')) {
84 $root = '.';
85 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
86 top_of_kernel_tree($1)) {
87 $root = $1;
88 }
89 }
90
91 if (!defined $root) {
92 print "Must be run from the top-level dir. of a kernel tree\n";
93 exit(2);
94 }
0a920b5b
AW
95}
96
6c72ffaa
AW
97my $emitted_corrupt = 0;
98
99our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
100our $Storage = qr{extern|static|asmlinkage};
101our $Sparse = qr{
102 __user|
103 __kernel|
104 __force|
105 __iomem|
106 __must_check|
107 __init_refok|
cf655043 108 __kprobes
6c72ffaa
AW
109 }x;
110our $Attribute = qr{
111 const|
112 __read_mostly|
113 __kprobes|
114 __(?:mem|cpu|dev|)(?:initdata|init)
115 }x;
116our $Inline = qr{inline|__always_inline|noinline};
6c72ffaa
AW
117our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
118our $Lval = qr{$Ident(?:$Member)*};
119
120our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
121our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
122our $Operators = qr{
123 <=|>=|==|!=|
124 =>|->|<<|>>|<|>|!|~|
c2fdda0d 125 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
6c72ffaa
AW
126 }x;
127
8905a67c
AW
128our $NonptrType;
129our $Type;
130our $Declare;
131
132our @typeList = (
133 qr{void},
134 qr{char},
135 qr{short},
136 qr{int},
137 qr{long},
138 qr{unsigned},
139 qr{float},
140 qr{double},
141 qr{bool},
142 qr{long\s+int},
143 qr{long\s+long},
144 qr{long\s+long\s+int},
145 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
146 qr{struct\s+$Ident},
147 qr{union\s+$Ident},
148 qr{enum\s+$Ident},
149 qr{${Ident}_t},
150 qr{${Ident}_handler},
151 qr{${Ident}_handler_fn},
152);
153
154sub build_types {
155 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
156 $NonptrType = qr{
157 \b
158 (?:const\s+)?
159 (?:unsigned\s+)?
cf655043
AW
160 (?:
161 $all|
162 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
163 )
8905a67c
AW
164 (?:\s+$Sparse|\s+const)*
165 \b
166 }x;
167 $Type = qr{
168 \b$NonptrType\b
169 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
c2fdda0d 170 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
8905a67c
AW
171 }x;
172 $Declare = qr{(?:$Storage\s+)?$Type};
173}
174build_types();
6c72ffaa
AW
175
176$chk_signoff = 0 if ($file);
177
4a0df2ef
AW
178my @dep_includes = ();
179my @dep_functions = ();
6c72ffaa
AW
180my $removal = "Documentation/feature-removal-schedule.txt";
181if ($tree && -f "$root/$removal") {
182 open(REMOVE, "<$root/$removal") ||
183 die "$P: $removal: open failed - $!\n";
0a920b5b 184 while (<REMOVE>) {
f0a594c1
AW
185 if (/^Check:\s+(.*\S)/) {
186 for my $entry (split(/[, ]+/, $1)) {
187 if ($entry =~ m@include/(.*)@) {
4a0df2ef 188 push(@dep_includes, $1);
4a0df2ef 189
f0a594c1
AW
190 } elsif ($entry !~ m@/@) {
191 push(@dep_functions, $entry);
192 }
4a0df2ef 193 }
0a920b5b
AW
194 }
195 }
196}
197
00df344f 198my @rawlines = ();
c2fdda0d
AW
199my @lines = ();
200my $vname;
6c72ffaa
AW
201for my $filename (@ARGV) {
202 if ($file) {
203 open(FILE, "diff -u /dev/null $filename|") ||
204 die "$P: $filename: diff failed - $!\n";
205 } else {
206 open(FILE, "<$filename") ||
207 die "$P: $filename: open failed - $!\n";
0a920b5b 208 }
c2fdda0d
AW
209 if ($filename eq '-') {
210 $vname = 'Your patch';
211 } else {
212 $vname = $filename;
213 }
6c72ffaa
AW
214 while (<FILE>) {
215 chomp;
216 push(@rawlines, $_);
217 }
218 close(FILE);
c2fdda0d 219 if (!process($filename)) {
6c72ffaa
AW
220 $exit = 1;
221 }
222 @rawlines = ();
13214adf 223 @lines = ();
0a920b5b
AW
224}
225
226exit($exit);
227
228sub top_of_kernel_tree {
6c72ffaa
AW
229 my ($root) = @_;
230
231 my @tree_check = (
232 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
233 "README", "Documentation", "arch", "include", "drivers",
234 "fs", "init", "ipc", "kernel", "lib", "scripts",
235 );
236
237 foreach my $check (@tree_check) {
238 if (! -e $root . '/' . $check) {
239 return 0;
240 }
0a920b5b 241 }
6c72ffaa 242 return 1;
0a920b5b
AW
243}
244
245sub expand_tabs {
246 my ($str) = @_;
247
248 my $res = '';
249 my $n = 0;
250 for my $c (split(//, $str)) {
251 if ($c eq "\t") {
252 $res .= ' ';
253 $n++;
254 for (; ($n % 8) != 0; $n++) {
255 $res .= ' ';
256 }
257 next;
258 }
259 $res .= $c;
260 $n++;
261 }
262
263 return $res;
264}
6c72ffaa
AW
265sub copy_spacing {
266 my ($str) = @_;
267
268 my $res = '';
269 for my $c (split(//, $str)) {
270 if ($c eq "\t") {
271 $res .= $c;
272 } else {
273 $res .= ' ';
274 }
275 }
276
277 return $res;
278}
0a920b5b 279
4a0df2ef
AW
280sub line_stats {
281 my ($line) = @_;
282
283 # Drop the diff line leader and expand tabs
284 $line =~ s/^.//;
285 $line = expand_tabs($line);
286
287 # Pick the indent from the front of the line.
288 my ($white) = ($line =~ /^(\s*)/);
289
290 return (length($line), length($white));
291}
292
00df344f
AW
293sub sanitise_line {
294 my ($line) = @_;
295
296 my $res = '';
297 my $l = '';
298
299 my $quote = '';
c2fdda0d 300 my $qlen = 0;
00df344f
AW
301
302 foreach my $c (split(//, $line)) {
c2fdda0d
AW
303 # The second backslash of a pair is not a "quote".
304 if ($l eq "\\" && $c eq "\\") {
305 $c = 'X';
306 }
00df344f
AW
307 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
308 if ($quote eq '') {
309 $quote = $c;
310 $res .= $c;
311 $l = $c;
c2fdda0d 312 $qlen = 0;
00df344f
AW
313 next;
314 } elsif ($quote eq $c) {
315 $quote = '';
316 }
317 }
c2fdda0d
AW
318 if ($quote eq "'" && $qlen > 1) {
319 $quote = '';
320 }
00df344f
AW
321 if ($quote && $c ne "\t") {
322 $res .= "X";
c2fdda0d 323 $qlen++;
00df344f
AW
324 } else {
325 $res .= $c;
326 }
327
328 $l = $c;
329 }
330
c2fdda0d 331 # Clear out the comments.
13214adf
AW
332 while ($res =~ m@(/\*.*?\*/)@g) {
333 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
c2fdda0d
AW
334 }
335 if ($res =~ m@(/\*.*)@) {
13214adf 336 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
c2fdda0d
AW
337 }
338 if ($res =~ m@^.(.*\*/)@) {
13214adf 339 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
c2fdda0d
AW
340 }
341
342 # The pathname on a #include may be surrounded by '<' and '>'.
343 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
344 my $clean = 'X' x length($1);
345 $res =~ s@\<.*\>@<$clean>@;
346
347 # The whole of a #error is a string.
348 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
349 my $clean = 'X' x length($1);
350 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
351 }
352
00df344f
AW
353 return $res;
354}
355
8905a67c
AW
356sub ctx_statement_block {
357 my ($linenr, $remain, $off) = @_;
358 my $line = $linenr - 1;
359 my $blk = '';
360 my $soff = $off;
361 my $coff = $off - 1;
362
13214adf
AW
363 my $loff = 0;
364
8905a67c
AW
365 my $type = '';
366 my $level = 0;
cf655043 367 my $p;
8905a67c
AW
368 my $c;
369 my $len = 0;
13214adf
AW
370
371 my $remainder;
8905a67c
AW
372 while (1) {
373 #warn "CSB: blk<$blk>\n";
374 # If we are about to drop off the end, pull in more
375 # context.
376 if ($off >= $len) {
377 for (; $remain > 0; $line++) {
c2fdda0d 378 next if ($lines[$line] =~ /^-/);
8905a67c 379 $remain--;
13214adf 380 $loff = $len;
c2fdda0d 381 $blk .= $lines[$line] . "\n";
8905a67c
AW
382 $len = length($blk);
383 $line++;
384 last;
385 }
386 # Bail if there is no further context.
387 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
13214adf 388 if ($off >= $len) {
8905a67c
AW
389 last;
390 }
391 }
cf655043 392 $p = $c;
8905a67c 393 $c = substr($blk, $off, 1);
13214adf 394 $remainder = substr($blk, $off);
8905a67c
AW
395
396 #warn "CSB: c<$c> type<$type> level<$level>\n";
397 # Statement ends at the ';' or a close '}' at the
398 # outermost level.
399 if ($level == 0 && $c eq ';') {
400 last;
401 }
402
13214adf 403 # An else is really a conditional as long as its not else if
cf655043
AW
404 if ($level == 0 && (!defined($p) || $p =~ /(?:\s|\})/) &&
405 $remainder =~ /(else)(?:\s|{)/ &&
406 $remainder !~ /else\s+if\b/) {
13214adf
AW
407 $coff = $off + length($1);
408 }
409
8905a67c
AW
410 if (($type eq '' || $type eq '(') && $c eq '(') {
411 $level++;
412 $type = '(';
413 }
414 if ($type eq '(' && $c eq ')') {
415 $level--;
416 $type = ($level != 0)? '(' : '';
417
418 if ($level == 0 && $coff < $soff) {
419 $coff = $off;
420 }
421 }
422 if (($type eq '' || $type eq '{') && $c eq '{') {
423 $level++;
424 $type = '{';
425 }
426 if ($type eq '{' && $c eq '}') {
427 $level--;
428 $type = ($level != 0)? '{' : '';
429
430 if ($level == 0) {
431 last;
432 }
433 }
434 $off++;
435 }
13214adf
AW
436 if ($off == $len) {
437 $line++;
438 $remain--;
439 }
8905a67c
AW
440
441 my $statement = substr($blk, $soff, $off - $soff + 1);
442 my $condition = substr($blk, $soff, $coff - $soff + 1);
443
444 #warn "STATEMENT<$statement>\n";
445 #warn "CONDITION<$condition>\n";
446
13214adf
AW
447 #print "off<$off> loff<$loff>\n";
448
449 return ($statement, $condition,
450 $line, $remain + 1, $off - $loff + 1, $level);
451}
452
cf655043
AW
453sub statement_lines {
454 my ($stmt) = @_;
455
456 # Strip the diff line prefixes and rip blank lines at start and end.
457 $stmt =~ s/(^|\n)./$1/g;
458 $stmt =~ s/^\s*//;
459 $stmt =~ s/\s*$//;
460
461 my @stmt_lines = ($stmt =~ /\n/g);
462
463 return $#stmt_lines + 2;
464}
465
466sub statement_rawlines {
467 my ($stmt) = @_;
468
469 my @stmt_lines = ($stmt =~ /\n/g);
470
471 return $#stmt_lines + 2;
472}
473
474sub statement_block_size {
475 my ($stmt) = @_;
476
477 $stmt =~ s/(^|\n)./$1/g;
478 $stmt =~ s/^\s*{//;
479 $stmt =~ s/}\s*$//;
480 $stmt =~ s/^\s*//;
481 $stmt =~ s/\s*$//;
482
483 my @stmt_lines = ($stmt =~ /\n/g);
484 my @stmt_statements = ($stmt =~ /;/g);
485
486 my $stmt_lines = $#stmt_lines + 2;
487 my $stmt_statements = $#stmt_statements + 1;
488
489 if ($stmt_lines > $stmt_statements) {
490 return $stmt_lines;
491 } else {
492 return $stmt_statements;
493 }
494}
495
13214adf
AW
496sub ctx_statement_full {
497 my ($linenr, $remain, $off) = @_;
498 my ($statement, $condition, $level);
499
500 my (@chunks);
501
cf655043 502 # Grab the first conditional/block pair.
13214adf
AW
503 ($statement, $condition, $linenr, $remain, $off, $level) =
504 ctx_statement_block($linenr, $remain, $off);
505 #print "F: c<$condition> s<$statement>\n";
cf655043
AW
506 push(@chunks, [ $condition, $statement ]);
507 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
508 return ($level, $linenr, @chunks);
509 }
510
511 # Pull in the following conditional/block pairs and see if they
512 # could continue the statement.
13214adf 513 for (;;) {
13214adf
AW
514 ($statement, $condition, $linenr, $remain, $off, $level) =
515 ctx_statement_block($linenr, $remain, $off);
cf655043
AW
516 #print "C: c<$condition> s<$statement> remain<$remain>\n";
517 last if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:else|do)\b/s));
518 #print "C: push\n";
519 push(@chunks, [ $condition, $statement ]);
13214adf
AW
520 }
521
522 return ($level, $linenr, @chunks);
8905a67c
AW
523}
524
4a0df2ef 525sub ctx_block_get {
f0a594c1 526 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
4a0df2ef
AW
527 my $line;
528 my $start = $linenr - 1;
4a0df2ef
AW
529 my $blk = '';
530 my @o;
531 my @c;
532 my @res = ();
533
f0a594c1 534 my $level = 0;
00df344f
AW
535 for ($line = $start; $remain > 0; $line++) {
536 next if ($rawlines[$line] =~ /^-/);
537 $remain--;
538
539 $blk .= $rawlines[$line];
f0a594c1
AW
540 foreach my $c (split(//, $rawlines[$line])) {
541 ##print "C<$c>L<$level><$open$close>O<$off>\n";
542 if ($off > 0) {
543 $off--;
544 next;
545 }
4a0df2ef 546
f0a594c1
AW
547 if ($c eq $close && $level > 0) {
548 $level--;
549 last if ($level == 0);
550 } elsif ($c eq $open) {
551 $level++;
552 }
553 }
4a0df2ef 554
f0a594c1 555 if (!$outer || $level <= 1) {
00df344f 556 push(@res, $rawlines[$line]);
4a0df2ef
AW
557 }
558
f0a594c1 559 last if ($level == 0);
4a0df2ef
AW
560 }
561
f0a594c1 562 return ($level, @res);
4a0df2ef
AW
563}
564sub ctx_block_outer {
565 my ($linenr, $remain) = @_;
566
f0a594c1
AW
567 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
568 return @r;
4a0df2ef
AW
569}
570sub ctx_block {
571 my ($linenr, $remain) = @_;
572
f0a594c1
AW
573 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
574 return @r;
653d4876
AW
575}
576sub ctx_statement {
f0a594c1
AW
577 my ($linenr, $remain, $off) = @_;
578
579 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
580 return @r;
581}
582sub ctx_block_level {
653d4876
AW
583 my ($linenr, $remain) = @_;
584
f0a594c1 585 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
4a0df2ef 586}
9c0ca6f9
AW
587sub ctx_statement_level {
588 my ($linenr, $remain, $off) = @_;
589
590 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
591}
4a0df2ef
AW
592
593sub ctx_locate_comment {
594 my ($first_line, $end_line) = @_;
595
596 # Catch a comment on the end of the line itself.
00df344f 597 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
4a0df2ef
AW
598 return $current_comment if (defined $current_comment);
599
600 # Look through the context and try and figure out if there is a
601 # comment.
602 my $in_comment = 0;
603 $current_comment = '';
604 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
00df344f
AW
605 my $line = $rawlines[$linenr - 1];
606 #warn " $line\n";
4a0df2ef
AW
607 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
608 $in_comment = 1;
609 }
610 if ($line =~ m@/\*@) {
611 $in_comment = 1;
612 }
613 if (!$in_comment && $current_comment ne '') {
614 $current_comment = '';
615 }
616 $current_comment .= $line . "\n" if ($in_comment);
617 if ($line =~ m@\*/@) {
618 $in_comment = 0;
619 }
620 }
621
622 chomp($current_comment);
623 return($current_comment);
624}
625sub ctx_has_comment {
626 my ($first_line, $end_line) = @_;
627 my $cmt = ctx_locate_comment($first_line, $end_line);
628
00df344f 629 ##print "LINE: $rawlines[$end_line - 1 ]\n";
4a0df2ef
AW
630 ##print "CMMT: $cmt\n";
631
632 return ($cmt ne '');
633}
634
6c72ffaa
AW
635sub cat_vet {
636 my ($vet) = @_;
637 my ($res, $coded);
9c0ca6f9 638
6c72ffaa
AW
639 $res = '';
640 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
641 $res .= $1;
642 if ($2 ne '') {
643 $coded = sprintf("^%c", unpack('C', $2) + 64);
644 $res .= $coded;
9c0ca6f9
AW
645 }
646 }
6c72ffaa 647 $res =~ s/$/\$/;
9c0ca6f9 648
6c72ffaa 649 return $res;
9c0ca6f9
AW
650}
651
c2fdda0d 652my $av_preprocessor = 0;
cf655043 653my $av_pending;
c2fdda0d
AW
654my @av_paren_type;
655
656sub annotate_reset {
657 $av_preprocessor = 0;
cf655043
AW
658 $av_pending = '_';
659 @av_paren_type = ('E');
c2fdda0d
AW
660}
661
6c72ffaa
AW
662sub annotate_values {
663 my ($stream, $type) = @_;
0a920b5b 664
6c72ffaa
AW
665 my $res;
666 my $cur = $stream;
667
c2fdda0d 668 print "$stream\n" if ($dbg_values > 1);
6c72ffaa 669
6c72ffaa 670 while (length($cur)) {
cf655043
AW
671 print " <" . join('', @av_paren_type) .
672 "> <$type> " if ($dbg_values > 1);
6c72ffaa 673 if ($cur =~ /^(\s+)/o) {
c2fdda0d
AW
674 print "WS($1)\n" if ($dbg_values > 1);
675 if ($1 =~ /\n/ && $av_preprocessor) {
cf655043 676 $type = pop(@av_paren_type);
c2fdda0d 677 $av_preprocessor = 0;
6c72ffaa
AW
678 }
679
8905a67c 680 } elsif ($cur =~ /^($Type)/) {
c2fdda0d 681 print "DECLARE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
682 $type = 'T';
683
684 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
c2fdda0d
AW
685 print "DEFINE($1)\n" if ($dbg_values > 1);
686 $av_preprocessor = 1;
cf655043 687 $av_pending = 'N';
6c72ffaa 688
cf655043
AW
689 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
690 print "PRE_START($1)\n" if ($dbg_values > 1);
c2fdda0d 691 $av_preprocessor = 1;
cf655043
AW
692
693 push(@av_paren_type, $type);
694 push(@av_paren_type, $type);
695 $type = 'N';
696
697 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
698 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
699 $av_preprocessor = 1;
700
701 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
702
703 $type = 'N';
704
705 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
706 print "PRE_END($1)\n" if ($dbg_values > 1);
707
708 $av_preprocessor = 1;
709
710 # Assume all arms of the conditional end as this
711 # one does, and continue as if the #endif was not here.
712 pop(@av_paren_type);
713 push(@av_paren_type, $type);
6c72ffaa
AW
714 $type = 'N';
715
716 } elsif ($cur =~ /^(\\\n)/o) {
c2fdda0d 717 print "PRECONT($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
718
719 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
c2fdda0d 720 print "SIZEOF($1)\n" if ($dbg_values > 1);
6c72ffaa 721 if (defined $2) {
cf655043 722 $av_pending = 'V';
6c72ffaa
AW
723 }
724 $type = 'N';
725
13214adf 726 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
c2fdda0d 727 print "COND($1)\n" if ($dbg_values > 1);
cf655043 728 $av_pending = 'N';
6c72ffaa
AW
729 $type = 'N';
730
731 } elsif ($cur =~/^(return|case|else)/o) {
c2fdda0d 732 print "KEYWORD($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
733 $type = 'N';
734
735 } elsif ($cur =~ /^(\()/o) {
c2fdda0d 736 print "PAREN('$1')\n" if ($dbg_values > 1);
cf655043
AW
737 push(@av_paren_type, $av_pending);
738 $av_pending = '_';
6c72ffaa
AW
739 $type = 'N';
740
741 } elsif ($cur =~ /^(\))/o) {
cf655043
AW
742 my $new_type = pop(@av_paren_type);
743 if ($new_type ne '_') {
744 $type = $new_type;
c2fdda0d
AW
745 print "PAREN('$1') -> $type\n"
746 if ($dbg_values > 1);
6c72ffaa 747 } else {
c2fdda0d 748 print "PAREN('$1')\n" if ($dbg_values > 1);
6c72ffaa
AW
749 }
750
751 } elsif ($cur =~ /^($Ident)\(/o) {
c2fdda0d 752 print "FUNC($1)\n" if ($dbg_values > 1);
cf655043 753 $av_pending = 'V';
6c72ffaa
AW
754
755 } elsif ($cur =~ /^($Ident|$Constant)/o) {
c2fdda0d 756 print "IDENT($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
757 $type = 'V';
758
759 } elsif ($cur =~ /^($Assignment)/o) {
c2fdda0d 760 print "ASSIGN($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
761 $type = 'N';
762
cf655043 763 } elsif ($cur =~/^(;|{|})/) {
c2fdda0d 764 print "END($1)\n" if ($dbg_values > 1);
13214adf
AW
765 $type = 'E';
766
cf655043 767 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
13214adf 768 print "CLOSE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
769 $type = 'N';
770
771 } elsif ($cur =~ /^($Operators)/o) {
c2fdda0d 772 print "OP($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
773 if ($1 ne '++' && $1 ne '--') {
774 $type = 'N';
775 }
776
777 } elsif ($cur =~ /(^.)/o) {
c2fdda0d 778 print "C($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
779 }
780 if (defined $1) {
781 $cur = substr($cur, length($1));
782 $res .= $type x length($1);
783 }
9c0ca6f9 784 }
0a920b5b 785
9c0ca6f9 786 return $res;
0a920b5b
AW
787}
788
8905a67c 789sub possible {
13214adf 790 my ($possible, $line) = @_;
8905a67c
AW
791
792 #print "CHECK<$possible>\n";
793 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
794 $possible ne 'goto' && $possible ne 'return' &&
795 $possible ne 'struct' && $possible ne 'enum' &&
796 $possible ne 'case' && $possible ne 'else' &&
797 $possible ne 'typedef') {
13214adf 798 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
8905a67c
AW
799 push(@typeList, $possible);
800 build_types();
801 }
802}
803
6c72ffaa
AW
804my $prefix = '';
805
f0a594c1 806sub report {
8905a67c
AW
807 my $line = $prefix . $_[0];
808
809 $line = (split('\n', $line))[0] . "\n" if ($terse);
810
13214adf 811 push(our @report, $line);
f0a594c1
AW
812}
813sub report_dump {
13214adf 814 our @report;
f0a594c1 815}
de7d4f0e 816sub ERROR {
f0a594c1 817 report("ERROR: $_[0]\n");
de7d4f0e 818 our $clean = 0;
6c72ffaa 819 our $cnt_error++;
de7d4f0e
AW
820}
821sub WARN {
f0a594c1 822 report("WARNING: $_[0]\n");
de7d4f0e 823 our $clean = 0;
6c72ffaa 824 our $cnt_warn++;
de7d4f0e
AW
825}
826sub CHK {
6c72ffaa
AW
827 if ($check) {
828 report("CHECK: $_[0]\n");
829 our $clean = 0;
830 our $cnt_chk++;
831 }
de7d4f0e
AW
832}
833
0a920b5b
AW
834sub process {
835 my $filename = shift;
0a920b5b
AW
836
837 my $linenr=0;
838 my $prevline="";
c2fdda0d 839 my $prevrawline="";
0a920b5b 840 my $stashline="";
c2fdda0d 841 my $stashrawline="";
0a920b5b 842
4a0df2ef 843 my $length;
0a920b5b
AW
844 my $indent;
845 my $previndent=0;
846 my $stashindent=0;
847
de7d4f0e 848 our $clean = 1;
0a920b5b
AW
849 my $signoff = 0;
850 my $is_patch = 0;
851
13214adf 852 our @report = ();
6c72ffaa
AW
853 our $cnt_lines = 0;
854 our $cnt_error = 0;
855 our $cnt_warn = 0;
856 our $cnt_chk = 0;
857
0a920b5b
AW
858 # Trace the real file/line as we go.
859 my $realfile = '';
860 my $realline = 0;
861 my $realcnt = 0;
862 my $here = '';
863 my $in_comment = 0;
c2fdda0d 864 my $comment_edge = 0;
0a920b5b
AW
865 my $first_line = 0;
866
13214adf
AW
867 my $prev_values = 'E';
868
869 # suppression flags
870 my $suppress_ifbraces = 0;
653d4876 871
c2fdda0d 872 # Pre-scan the patch sanitizing the lines.
de7d4f0e 873 # Pre-scan the patch looking for any __setup documentation.
c2fdda0d 874 #
de7d4f0e
AW
875 my @setup_docs = ();
876 my $setup_docs = 0;
c2fdda0d
AW
877 my $line;
878 foreach my $rawline (@rawlines) {
879 # Standardise the strings and chars within the input to
880 # simplify matching.
881 $line = sanitise_line($rawline);
882 push(@lines, $line);
883
884 ##print "==>$rawline\n";
885 ##print "-->$line\n";
886
de7d4f0e
AW
887 if ($line=~/^\+\+\+\s+(\S+)/) {
888 $setup_docs = 0;
889 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
890 $setup_docs = 1;
891 }
892 next;
893 }
894
895 if ($setup_docs && $line =~ /^\+/) {
896 push(@setup_docs, $line);
897 }
898 }
899
6c72ffaa
AW
900 $prefix = '';
901
0a920b5b
AW
902 foreach my $line (@lines) {
903 $linenr++;
904
c2fdda0d 905 my $rawline = $rawlines[$linenr - 1];
6c72ffaa 906
0a920b5b
AW
907#extract the filename as it passes
908 if ($line=~/^\+\+\+\s+(\S+)/) {
909 $realfile=$1;
00df344f 910 $realfile =~ s@^[^/]*/@@;
0a920b5b
AW
911 $in_comment = 0;
912 next;
913 }
914#extract the line range in the file after the patch is applied
6c72ffaa 915 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
0a920b5b 916 $is_patch = 1;
4a0df2ef 917 $first_line = $linenr + 1;
0a920b5b
AW
918 $in_comment = 0;
919 $realline=$1-1;
920 if (defined $2) {
921 $realcnt=$3+1;
922 } else {
923 $realcnt=1+1;
924 }
c2fdda0d 925 annotate_reset();
13214adf
AW
926 $prev_values = 'E';
927
928 $suppress_ifbraces = $linenr - 1;
0a920b5b
AW
929 next;
930 }
931
4a0df2ef
AW
932# track the line number as we move through the hunk, note that
933# new versions of GNU diff omit the leading space on completely
934# blank context lines so we need to count that too.
935 if ($line =~ /^( |\+|$)/) {
0a920b5b 936 $realline++;
d8aaf121 937 $realcnt-- if ($realcnt != 0);
0a920b5b 938
8905a67c
AW
939 # Guestimate if this is a continuing comment. Run
940 # the context looking for a comment "edge". If this
941 # edge is a close comment then we must be in a comment
942 # at context start.
943 if ($linenr == $first_line) {
944 my $edge;
945 for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
c2fdda0d 946 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
8905a67c
AW
947 last if (defined $edge);
948 }
949 if (defined $edge && $edge eq '*/') {
950 $in_comment = 1;
951 }
952 }
953
0a920b5b
AW
954 # Guestimate if this is a continuing comment. If this
955 # is the start of a diff block and this line starts
956 # ' *' then it is very likely a comment.
c2fdda0d 957 if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) {
0a920b5b
AW
958 $in_comment = 1;
959 }
8905a67c
AW
960
961 # Find the last comment edge on _this_ line.
c2fdda0d
AW
962 $comment_edge = 0;
963 while (($rawline =~ m@(/\*|\*/)@g)) {
8905a67c
AW
964 if ($1 eq '/*') {
965 $in_comment = 1;
966 } else {
967 $in_comment = 0;
968 }
c2fdda0d 969 $comment_edge = 1;
0a920b5b
AW
970 }
971
4a0df2ef 972 # Measure the line length and indent.
c2fdda0d 973 ($length, $indent) = line_stats($rawline);
0a920b5b
AW
974
975 # Track the previous line.
976 ($prevline, $stashline) = ($stashline, $line);
977 ($previndent, $stashindent) = ($stashindent, $indent);
c2fdda0d
AW
978 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
979
980 #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n";
6c72ffaa 981
d8aaf121
AW
982 } elsif ($realcnt == 1) {
983 $realcnt--;
0a920b5b
AW
984 }
985
986#make up the handle for any error we report on this line
6c72ffaa
AW
987 $here = "#$linenr: " if (!$file);
988 $here = "#$realline: " if ($file);
389834b6 989 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
0a920b5b 990
c2fdda0d
AW
991 my $hereline = "$here\n$rawline\n";
992 my $herecurr = "$here\n$rawline\n";
993 my $hereprev = "$here\n$prevrawline\n$rawline\n";
0a920b5b 994
6c72ffaa
AW
995 $prefix = "$filename:$realline: " if ($emacs && $file);
996 $prefix = "$filename:$linenr: " if ($emacs && !$file);
997 $cnt_lines++ if ($realcnt != 0);
998
0a920b5b 999#check the patch for a signoff:
d8aaf121 1000 if ($line =~ /^\s*signed-off-by:/i) {
4a0df2ef
AW
1001 # This is a signoff, if ugly, so do not double report.
1002 $signoff++;
0a920b5b 1003 if (!($line =~ /^\s*Signed-off-by:/)) {
de7d4f0e
AW
1004 WARN("Signed-off-by: is the preferred form\n" .
1005 $herecurr);
0a920b5b
AW
1006 }
1007 if ($line =~ /^\s*signed-off-by:\S/i) {
de7d4f0e
AW
1008 WARN("need space after Signed-off-by:\n" .
1009 $herecurr);
0a920b5b
AW
1010 }
1011 }
1012
00df344f 1013# Check for wrappage within a valid hunk of the file
8905a67c 1014 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
de7d4f0e 1015 ERROR("patch seems to be corrupt (line wrapped?)\n" .
6c72ffaa 1016 $herecurr) if (!$emitted_corrupt++);
de7d4f0e
AW
1017 }
1018
1019# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1020 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
c2fdda0d 1021 !($rawline =~ m/^(
de7d4f0e
AW
1022 [\x09\x0A\x0D\x20-\x7E] # ASCII
1023 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1024 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1025 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1026 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1027 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1028 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1029 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1030 )*$/x )) {
c2fdda0d 1031 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
00df344f
AW
1032 }
1033
1034#ignore lines being removed
1035 if ($line=~/^-/) {next;}
0a920b5b 1036
00df344f
AW
1037# check we are in a valid source file if not then ignore this hunk
1038 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
0a920b5b
AW
1039
1040#trailing whitespace
9c0ca6f9 1041 if ($line =~ /^\+.*\015/) {
c2fdda0d 1042 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
9c0ca6f9
AW
1043 ERROR("DOS line endings\n" . $herevet);
1044
c2fdda0d
AW
1045 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1046 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
de7d4f0e 1047 ERROR("trailing whitespace\n" . $herevet);
0a920b5b
AW
1048 }
1049#80 column limit
c2fdda0d 1050 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
de7d4f0e 1051 WARN("line over 80 characters\n" . $herecurr);
0a920b5b
AW
1052 }
1053
8905a67c
AW
1054# check for adding lines without a newline.
1055 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1056 WARN("adding a line without newline at end of file\n" . $herecurr);
1057 }
1058
0a920b5b
AW
1059# check we are in a valid source file *.[hc] if not then ignore this hunk
1060 next if ($realfile !~ /\.[hc]$/);
1061
1062# at the beginning of a line any tabs must come first and anything
1063# more than 8 must use tabs.
c2fdda0d
AW
1064 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1065 $rawline =~ /^\+\s* \s*/) {
1066 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
de7d4f0e 1067 ERROR("use tabs not spaces\n" . $herevet);
0a920b5b
AW
1068 }
1069
c2fdda0d 1070# check for RCS/CVS revision markers
cf655043 1071 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
c2fdda0d
AW
1072 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1073 }
22f2a2ef
AW
1074
1075# The rest of our checks refer specifically to C style
1076# only apply those _outside_ comments. Only skip
1077# lines in the middle of comments.
1078 next if (!$comment_edge && $in_comment);
00df344f 1079
9c0ca6f9 1080# Check for potential 'bare' types
c2fdda0d 1081 if ($realcnt) {
cf655043
AW
1082 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1083 $s =~ s/\n./ /g;
1084 $s =~ s/{.*$//;
1085
c2fdda0d 1086 # Ignore goto labels.
cf655043 1087 if ($s =~ /$Ident:\*$/) {
c2fdda0d
AW
1088
1089 # Ignore functions being called
cf655043 1090 } elsif ($s =~ /^.\s*$Ident\s*\(/) {
c2fdda0d 1091
8905a67c 1092 # definitions in global scope can only start with types
cf655043
AW
1093 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1094 possible($1, $s);
8905a67c
AW
1095
1096 # declarations always start with types
cf655043
AW
1097 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1098 possible($1, $s);
c2fdda0d 1099 }
8905a67c
AW
1100
1101 # any (foo ... *) is a pointer cast, and foo is a type
cf655043
AW
1102 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1103 possible($1, $s);
8905a67c
AW
1104 }
1105
1106 # Check for any sort of function declaration.
1107 # int foo(something bar, other baz);
1108 # void (*store_gdt)(x86_descr_ptr *);
cf655043 1109 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
8905a67c 1110 my ($name_len) = length($1);
8905a67c 1111
cf655043 1112 my $ctx = $s;
8905a67c
AW
1113 substr($ctx, 0, $name_len + 1) = '';
1114 $ctx =~ s/\)[^\)]*$//;
cf655043 1115
8905a67c
AW
1116 for my $arg (split(/\s*,\s*/, $ctx)) {
1117 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1118
cf655043 1119 possible($1, $s);
8905a67c
AW
1120 }
1121 }
9c0ca6f9 1122 }
8905a67c 1123
9c0ca6f9
AW
1124 }
1125
653d4876
AW
1126#
1127# Checks which may be anchored in the context.
1128#
00df344f 1129
653d4876
AW
1130# Check for switch () and associated case and default
1131# statements should be at the same indent.
00df344f
AW
1132 if ($line=~/\bswitch\s*\(.*\)/) {
1133 my $err = '';
1134 my $sep = '';
1135 my @ctx = ctx_block_outer($linenr, $realcnt);
1136 shift(@ctx);
1137 for my $ctx (@ctx) {
1138 my ($clen, $cindent) = line_stats($ctx);
1139 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1140 $indent != $cindent) {
1141 $err .= "$sep$ctx\n";
1142 $sep = '';
1143 } else {
1144 $sep = "[...]\n";
1145 }
1146 }
1147 if ($err ne '') {
9c0ca6f9 1148 ERROR("switch and case should be at the same indent\n$hereline$err");
de7d4f0e
AW
1149 }
1150 }
1151
1152# if/while/etc brace do not go on next line, unless defining a do while loop,
1153# or if that brace on the next line is for something else
1154 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
9c0ca6f9 1155 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
de7d4f0e
AW
1156 my $ctx_ln = $linenr + $#ctx + 1;
1157 my $ctx_cnt = $realcnt - $#ctx - 1;
1158 my $ctx = join("\n", @ctx);
1159
9c0ca6f9 1160 # Skip over any removed lines in the context following statement.
de7d4f0e
AW
1161 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
1162 $ctx_ln++;
1163 $ctx_cnt--;
1164 }
1165 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
1166
1167 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
f0a594c1 1168 ERROR("That open brace { should be on the previous line\n" .
de7d4f0e 1169 "$here\n$ctx\n$lines[$ctx_ln - 1]");
00df344f 1170 }
9c0ca6f9
AW
1171 if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
1172 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1173 if ($nindent > $indent) {
1174 WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
1175 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1176 }
1177 }
00df344f
AW
1178 }
1179
6c72ffaa
AW
1180 # Track the 'values' across context and added lines.
1181 my $opline = $line; $opline =~ s/^./ /;
1182 my $curr_values = annotate_values($opline . "\n", $prev_values);
1183 $curr_values = $prev_values . $curr_values;
c2fdda0d
AW
1184 if ($dbg_values) {
1185 my $outline = $opline; $outline =~ s/\t/ /g;
cf655043
AW
1186 print "$linenr > .$outline\n";
1187 print "$linenr > $curr_values\n";
c2fdda0d 1188 }
6c72ffaa
AW
1189 $prev_values = substr($curr_values, -1);
1190
00df344f
AW
1191#ignore lines not being added
1192 if ($line=~/^[^\+]/) {next;}
1193
653d4876
AW
1194# TEST: allow direct testing of the type matcher.
1195 if ($tst_type && $line =~ /^.$Declare$/) {
de7d4f0e 1196 ERROR("TEST: is type $Declare\n" . $herecurr);
653d4876
AW
1197 next;
1198 }
1199
f0a594c1
AW
1200# check for initialisation to aggregates open brace on the next line
1201 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1202 $line =~ /^.\s*{/) {
1203 ERROR("That open brace { should be on the previous line\n" . $hereprev);
1204 }
1205
653d4876
AW
1206#
1207# Checks which are anchored on the added line.
1208#
1209
1210# check for malformed paths in #include statements (uses RAW line)
1211 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1212 my $path = $1;
1213 if ($path =~ m{//}) {
de7d4f0e
AW
1214 ERROR("malformed #include filename\n" .
1215 $herecurr);
653d4876 1216 }
653d4876 1217 }
00df344f 1218
0a920b5b 1219# no C99 // comments
00df344f 1220 if ($line =~ m{//}) {
de7d4f0e 1221 ERROR("do not use C99 // comments\n" . $herecurr);
0a920b5b 1222 }
00df344f 1223 # Remove C99 comments.
0a920b5b 1224 $line =~ s@//.*@@;
6c72ffaa 1225 $opline =~ s@//.*@@;
0a920b5b
AW
1226
1227#EXPORT_SYMBOL should immediately follow its function closing }.
653d4876
AW
1228 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1229 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1230 my $name = $1;
0a920b5b
AW
1231 if (($prevline !~ /^}/) &&
1232 ($prevline !~ /^\+}/) &&
653d4876 1233 ($prevline !~ /^ }/) &&
cf655043
AW
1234 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1235 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1236 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
de7d4f0e 1237 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
0a920b5b
AW
1238 }
1239 }
1240
f0a594c1
AW
1241# check for external initialisers.
1242 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1243 ERROR("do not initialise externals to 0 or NULL\n" .
1244 $herecurr);
1245 }
653d4876 1246# check for static initialisers.
f0a594c1 1247 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
de7d4f0e
AW
1248 ERROR("do not initialise statics to 0 or NULL\n" .
1249 $herecurr);
0a920b5b
AW
1250 }
1251
653d4876
AW
1252# check for new typedefs, only function parameters and sparse annotations
1253# make sense.
1254 if ($line =~ /\btypedef\s/ &&
9c0ca6f9 1255 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
653d4876 1256 $line !~ /\b__bitwise(?:__|)\b/) {
de7d4f0e 1257 WARN("do not add new typedefs\n" . $herecurr);
0a920b5b
AW
1258 }
1259
1260# * goes on variable not on type
d8aaf121 1261 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
de7d4f0e
AW
1262 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1263 $herecurr);
d8aaf121
AW
1264
1265 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
de7d4f0e
AW
1266 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1267 $herecurr);
d8aaf121 1268
9c0ca6f9 1269 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
de7d4f0e
AW
1270 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1271 $herecurr);
d8aaf121 1272
9c0ca6f9 1273 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
de7d4f0e
AW
1274 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1275 $herecurr);
0a920b5b
AW
1276 }
1277
1278# # no BUG() or BUG_ON()
1279# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1280# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1281# print "$herecurr";
1282# $clean = 0;
1283# }
1284
8905a67c 1285 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
c2fdda0d 1286 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
8905a67c
AW
1287 }
1288
00df344f
AW
1289# printk should use KERN_* levels. Note that follow on printk's on the
1290# same line do not need a level, so we use the current block context
1291# to try and find and validate the current printk. In summary the current
1292# printk includes all preceeding printk's which have no newline on the end.
1293# we assume the first bad printk is the one to report.
f0a594c1 1294 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
00df344f
AW
1295 my $ok = 0;
1296 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1297 #print "CHECK<$lines[$ln - 1]\n";
1298 # we have a preceeding printk if it ends
1299 # with "\n" ignore it, else it is to blame
1300 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1301 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1302 $ok = 1;
1303 }
1304 last;
1305 }
1306 }
1307 if ($ok == 0) {
de7d4f0e 1308 WARN("printk() should include KERN_ facility level\n" . $herecurr);
00df344f 1309 }
0a920b5b
AW
1310 }
1311
653d4876
AW
1312# function brace can't be on same line, except for #defines of do while,
1313# or if closed on same line
c2fdda0d 1314 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
0a920b5b 1315 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
de7d4f0e 1316 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
0a920b5b 1317 }
653d4876 1318
8905a67c
AW
1319# open braces for enum, union and struct go on the same line.
1320 if ($line =~ /^.\s*{/ &&
1321 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1322 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1323 }
1324
f0a594c1 1325# check for spaces between functions and their parentheses.
6c72ffaa 1326 while ($line =~ /($Ident)\s+\(/g) {
c2fdda0d
AW
1327 my $name = $1;
1328 my $ctx = substr($line, 0, $-[1]);
1329
1330 # Ignore those directives where spaces _are_ permitted.
13214adf 1331 if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) {
c2fdda0d
AW
1332
1333 # cpp #define statements have non-optional spaces, ie
1334 # if there is a space between the name and the open
1335 # parenthesis it is simply not a parameter group.
1336 } elsif ($ctx =~ /^.\#\s*define\s*$/) {
1337
1338 # If this whole things ends with a type its most
1339 # likely a typedef for a function.
1340 } elsif ("$ctx$name" =~ /$Type$/) {
1341
1342 } else {
6c72ffaa
AW
1343 WARN("no space between function name and open parenthesis '('\n" . $herecurr);
1344 }
f0a594c1 1345 }
653d4876 1346# Check operator spacing.
0a920b5b 1347 if (!($line=~/\#\s*include/)) {
9c0ca6f9
AW
1348 my $ops = qr{
1349 <<=|>>=|<=|>=|==|!=|
1350 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1351 =>|->|<<|>>|<|>|=|!|~|
c2fdda0d 1352 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
9c0ca6f9 1353 }x;
cf655043 1354 my @elements = split(/($ops|;)/, $opline);
00df344f 1355 my $off = 0;
6c72ffaa
AW
1356
1357 my $blank = copy_spacing($opline);
1358
0a920b5b 1359 for (my $n = 0; $n < $#elements; $n += 2) {
4a0df2ef
AW
1360 $off += length($elements[$n]);
1361
1362 my $a = '';
1363 $a = 'V' if ($elements[$n] ne '');
1364 $a = 'W' if ($elements[$n] =~ /\s$/);
cf655043 1365 $a = 'C' if ($elements[$n] =~ /$;$/);
4a0df2ef
AW
1366 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1367 $a = 'O' if ($elements[$n] eq '');
1368 $a = 'E' if ($elements[$n] eq '' && $n == 0);
1369
0a920b5b 1370 my $op = $elements[$n + 1];
4a0df2ef
AW
1371
1372 my $c = '';
0a920b5b 1373 if (defined $elements[$n + 2]) {
4a0df2ef
AW
1374 $c = 'V' if ($elements[$n + 2] ne '');
1375 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
cf655043 1376 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4a0df2ef
AW
1377 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1378 $c = 'O' if ($elements[$n + 2] eq '');
22f2a2ef 1379 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
4a0df2ef
AW
1380 } else {
1381 $c = 'E';
0a920b5b
AW
1382 }
1383
00df344f 1384 # Pick up the preceeding and succeeding characters.
de7d4f0e 1385 my $ca = substr($opline, 0, $off);
00df344f 1386 my $cc = '';
653d4876 1387 if (length($opline) >= ($off + length($elements[$n + 1]))) {
d8aaf121 1388 $cc = substr($opline, $off + length($elements[$n + 1]));
00df344f 1389 }
de7d4f0e 1390 my $cb = "$ca$;$cc";
00df344f 1391
4a0df2ef
AW
1392 my $ctx = "${a}x${c}";
1393
1394 my $at = "(ctx:$ctx)";
1395
6c72ffaa 1396 my $ptr = substr($blank, 0, $off) . "^";
de7d4f0e 1397 my $hereptr = "$hereline$ptr\n";
0a920b5b 1398
9c0ca6f9
AW
1399 # Classify operators into binary, unary, or
1400 # definitions (* only) where they have more
1401 # than one mode.
6c72ffaa
AW
1402 my $op_type = substr($curr_values, $off + 1, 1);
1403 my $op_left = substr($curr_values, $off, 1);
1404 my $is_unary;
1405 if ($op_type eq 'T') {
1406 $is_unary = 2;
1407 } elsif ($op_left eq 'V') {
1408 $is_unary = 0;
1409 } else {
1410 $is_unary = 1;
9c0ca6f9 1411 }
9c0ca6f9 1412 #if ($op eq '-' || $op eq '&' || $op eq '*') {
6c72ffaa 1413 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
9c0ca6f9 1414 #}
0a920b5b 1415
13214adf
AW
1416 # Ignore operators passed as parameters.
1417 if ($op_type ne 'V' &&
1418 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1419
cf655043
AW
1420# # Ignore comments
1421# } elsif ($op =~ /^$;+$/) {
13214adf 1422
d8aaf121 1423 # ; should have either the end of line or a space or \ after it
13214adf 1424 } elsif ($op eq ';') {
cf655043
AW
1425 if ($ctx !~ /.x[WEBC]/ &&
1426 $cc !~ /^\\/ && $cc !~ /^;/) {
de7d4f0e 1427 ERROR("need space after that '$op' $at\n" . $hereptr);
d8aaf121
AW
1428 }
1429
1430 # // is a comment
1431 } elsif ($op eq '//') {
0a920b5b
AW
1432
1433 # -> should have no spaces
1434 } elsif ($op eq '->') {
4a0df2ef 1435 if ($ctx =~ /Wx.|.xW/) {
de7d4f0e 1436 ERROR("no spaces around that '$op' $at\n" . $hereptr);
0a920b5b
AW
1437 }
1438
1439 # , must have a space on the right.
1440 } elsif ($op eq ',') {
cf655043 1441 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
de7d4f0e 1442 ERROR("need space after that '$op' $at\n" . $hereptr);
0a920b5b
AW
1443 }
1444
9c0ca6f9
AW
1445 # '*' as part of a type definition -- reported already.
1446 } elsif ($op eq '*' && $is_unary == 2) {
1447 #warn "'*' is part of type\n";
1448
1449 # unary operators should have a space before and
1450 # none after. May be left adjacent to another
1451 # unary operator, or a cast
1452 } elsif ($op eq '!' || $op eq '~' ||
1453 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
cf655043 1454 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
de7d4f0e 1455 ERROR("need space before that '$op' $at\n" . $hereptr);
0a920b5b 1456 }
4a0df2ef 1457 if ($ctx =~ /.xW/) {
de7d4f0e 1458 ERROR("no space after that '$op' $at\n" . $hereptr);
0a920b5b
AW
1459 }
1460
1461 # unary ++ and unary -- are allowed no space on one side.
1462 } elsif ($op eq '++' or $op eq '--') {
cf655043 1463 if ($ctx !~ /[WOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
de7d4f0e 1464 ERROR("need space one side of that '$op' $at\n" . $hereptr);
0a920b5b 1465 }
13214adf 1466 if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) {
de7d4f0e 1467 ERROR("no space before that '$op' $at\n" . $hereptr);
653d4876 1468 }
0a920b5b 1469
0a920b5b 1470 # << and >> may either have or not have spaces both sides
9c0ca6f9
AW
1471 } elsif ($op eq '<<' or $op eq '>>' or
1472 $op eq '&' or $op eq '^' or $op eq '|' or
1473 $op eq '+' or $op eq '-' or
c2fdda0d
AW
1474 $op eq '*' or $op eq '/' or
1475 $op eq '%')
0a920b5b 1476 {
cf655043 1477 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO|Cx.|.xC/) {
de7d4f0e
AW
1478 ERROR("need consistent spacing around '$op' $at\n" .
1479 $hereptr);
0a920b5b
AW
1480 }
1481
1482 # All the others need spaces both sides.
cf655043 1483 } elsif ($ctx !~ /[EWC]x[CWE]/) {
22f2a2ef
AW
1484 # Ignore email addresses <foo@bar>
1485 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1486 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1487 ERROR("need spaces around that '$op' $at\n" . $hereptr);
1488 }
0a920b5b 1489 }
4a0df2ef 1490 $off += length($elements[$n + 1]);
0a920b5b
AW
1491 }
1492 }
1493
f0a594c1
AW
1494# check for multiple assignments
1495 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
6c72ffaa 1496 CHK("multiple assignments should be avoided\n" . $herecurr);
f0a594c1
AW
1497 }
1498
22f2a2ef
AW
1499## # check for multiple declarations, allowing for a function declaration
1500## # continuation.
1501## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1502## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1503##
1504## # Remove any bracketed sections to ensure we do not
1505## # falsly report the parameters of functions.
1506## my $ln = $line;
1507## while ($ln =~ s/\([^\(\)]*\)//g) {
1508## }
1509## if ($ln =~ /,/) {
1510## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1511## }
1512## }
f0a594c1 1513
0a920b5b 1514#need space before brace following if, while, etc
22f2a2ef
AW
1515 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1516 $line =~ /do{/) {
de7d4f0e
AW
1517 ERROR("need a space before the open brace '{'\n" . $herecurr);
1518 }
1519
1520# closing brace should have a space following it when it has anything
1521# on the line
1522 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1523 ERROR("need a space after that close brace '}'\n" . $herecurr);
0a920b5b
AW
1524 }
1525
22f2a2ef
AW
1526# check spacing on square brackets
1527 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1528 ERROR("no space after that open square bracket '['\n" . $herecurr);
1529 }
1530 if ($line =~ /\s\]/) {
1531 ERROR("no space before that close square bracket ']'\n" . $herecurr);
1532 }
1533
1534# check spacing on paretheses
9c0ca6f9
AW
1535 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1536 $line !~ /for\s*\(\s+;/) {
22f2a2ef
AW
1537 ERROR("no space after that open parenthesis '('\n" . $herecurr);
1538 }
13214adf 1539 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
9c0ca6f9 1540 $line !~ /for\s*\(.*;\s+\)/) {
22f2a2ef
AW
1541 ERROR("no space before that close parenthesis ')'\n" . $herecurr);
1542 }
1543
0a920b5b 1544#goto labels aren't indented, allow a single space however
4a0df2ef 1545 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
0a920b5b 1546 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
de7d4f0e 1547 WARN("labels should not be indented\n" . $herecurr);
0a920b5b
AW
1548 }
1549
1550# Need a space before open parenthesis after if, while etc
4a0df2ef 1551 if ($line=~/\b(if|while|for|switch)\(/) {
de7d4f0e 1552 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
0a920b5b
AW
1553 }
1554
1555# Check for illegal assignment in if conditional.
8905a67c
AW
1556 if ($line =~ /\bif\s*\(/) {
1557 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1558
1559 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
c2fdda0d 1560 ERROR("do not use assignment in if condition\n" . $herecurr);
8905a67c
AW
1561 }
1562
1563 # Find out what is on the end of the line after the
1564 # conditional.
1565 substr($s, 0, length($c)) = '';
1566 $s =~ s/\n.*//g;
13214adf
AW
1567 $s =~ s/$;//g; # Remove any comments
1568 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) {
8905a67c
AW
1569 ERROR("trailing statements should be on next line\n" . $herecurr);
1570 }
1571 }
1572
13214adf
AW
1573# Check for bitwise tests written as boolean
1574 if ($line =~ /
1575 (?:
1576 (?:\[|\(|\&\&|\|\|)
1577 \s*0[xX][0-9]+\s*
1578 (?:\&\&|\|\|)
1579 |
1580 (?:\&\&|\|\|)
1581 \s*0[xX][0-9]+\s*
1582 (?:\&\&|\|\||\)|\])
1583 )/x)
1584 {
1585 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1586 }
1587
8905a67c 1588# if and else should not have general statements after it
13214adf
AW
1589 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1590 my $s = $1;
1591 $s =~ s/$;//g; # Remove any comments
1592 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1593 ERROR("trailing statements should be on next line\n" . $herecurr);
1594 }
0a920b5b
AW
1595 }
1596
1597 # Check for }<nl>else {, these must be at the same
1598 # indent level to be relevant to each other.
1599 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1600 $previndent == $indent) {
de7d4f0e 1601 ERROR("else should follow close brace '}'\n" . $hereprev);
0a920b5b
AW
1602 }
1603
c2fdda0d
AW
1604 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1605 $previndent == $indent) {
1606 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1607
1608 # Find out what is on the end of the line after the
1609 # conditional.
1610 substr($s, 0, length($c)) = '';
1611 $s =~ s/\n.*//g;
1612
1613 if ($s =~ /^\s*;/) {
1614 ERROR("while should follow close brace '}'\n" . $hereprev);
1615 }
1616 }
1617
0a920b5b
AW
1618#studly caps, commented out until figure out how to distinguish between use of existing and adding new
1619# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1620# print "No studly caps, use _\n";
1621# print "$herecurr";
1622# $clean = 0;
1623# }
1624
1625#no spaces allowed after \ in define
1626 if ($line=~/\#define.*\\\s$/) {
de7d4f0e 1627 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
0a920b5b
AW
1628 }
1629
653d4876
AW
1630#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1631 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
6c72ffaa
AW
1632 my $checkfile = "$root/include/linux/$1.h";
1633 if (-f $checkfile && $1 ne 'irq.h') {
de7d4f0e
AW
1634 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1635 $herecurr);
0a920b5b
AW
1636 }
1637 }
1638
653d4876
AW
1639# multi-statement macros should be enclosed in a do while loop, grab the
1640# first statement and ensure its the whole macro if its not enclosed
cf655043 1641# in a known good container
9c0ca6f9
AW
1642 if ($prevline =~ /\#define.*\\/ &&
1643 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1644 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1645 $line !~ /^.\s*$Declare\s/) {
653d4876
AW
1646 # Grab the first statement, if that is the entire macro
1647 # its ok. This may start either on the #define line
1648 # or the one below.
d8aaf121
AW
1649 my $ln = $linenr;
1650 my $cnt = $realcnt;
f0a594c1 1651 my $off = 0;
653d4876 1652
f0a594c1
AW
1653 # If the macro starts on the define line start
1654 # grabbing the statement after the identifier
1655 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1656 ##print "1<$1> 2<$2>\n";
22f2a2ef 1657 if (defined $2 && $2 ne '') {
f0a594c1 1658 $off = length($1);
d8aaf121
AW
1659 $ln--;
1660 $cnt++;
8905a67c
AW
1661 while ($lines[$ln - 1] =~ /^-/) {
1662 $ln--;
1663 $cnt++;
1664 }
d8aaf121 1665 }
f0a594c1 1666 my @ctx = ctx_statement($ln, $cnt, $off);
de7d4f0e
AW
1667 my $ctx_ln = $ln + $#ctx + 1;
1668 my $ctx = join("\n", @ctx);
1669
1670 # Pull in any empty extension lines.
1671 while ($ctx =~ /\\$/ &&
1672 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1673 $ctx .= $lines[$ctx_ln - 1];
1674 $ctx_ln++;
1675 }
d8aaf121
AW
1676
1677 if ($ctx =~ /\\$/) {
1678 if ($ctx =~ /;/) {
de7d4f0e 1679 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
d8aaf121 1680 } else {
de7d4f0e 1681 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
d8aaf121 1682 }
653d4876 1683 }
0a920b5b
AW
1684 }
1685
f0a594c1 1686# check for redundant bracing round if etc
13214adf
AW
1687 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1688 my ($level, $endln, @chunks) =
cf655043 1689 ctx_statement_full($linenr, $realcnt, 1);
13214adf 1690 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
cf655043
AW
1691 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1692 if ($#chunks > 0 && $level == 0) {
13214adf
AW
1693 my $allowed = 0;
1694 my $seen = 0;
cf655043
AW
1695 my $herectx = $here . "\n";;
1696 my $ln = $linenr - 1;
13214adf
AW
1697 for my $chunk (@chunks) {
1698 my ($cond, $block) = @{$chunk};
1699
cf655043
AW
1700 $herectx .= "$rawlines[$ln]\n[...]\n";
1701 $ln += statement_rawlines($block) - 1;
1702
13214adf
AW
1703 substr($block, 0, length($cond)) = '';
1704
1705 $seen++ if ($block =~ /^\s*{/);
1706
cf655043
AW
1707 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1708 if (statement_lines($cond) > 1) {
1709 #print "APW: ALLOWED: cond<$cond>\n";
13214adf
AW
1710 $allowed = 1;
1711 }
1712 if ($block =~/\b(?:if|for|while)\b/) {
cf655043 1713 #print "APW: ALLOWED: block<$block>\n";
13214adf
AW
1714 $allowed = 1;
1715 }
cf655043
AW
1716 if (statement_block_size($block) > 1) {
1717 #print "APW: ALLOWED: lines block<$block>\n";
13214adf
AW
1718 $allowed = 1;
1719 }
1720 }
1721 if ($seen && !$allowed) {
cf655043 1722 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
13214adf 1723 }
cf655043
AW
1724 # Either way we have looked over this whole
1725 # statement and said what needs to be said.
1726 $suppress_ifbraces = $endln;
13214adf
AW
1727 }
1728 }
1729 if ($linenr > $suppress_ifbraces &&
1730 $line =~ /\b(if|while|for|else)\b/) {
cf655043
AW
1731 my ($level, $endln, @chunks) =
1732 ctx_statement_full($linenr, $realcnt, $-[0]);
1733
1734 my $allowed = 0;
1735
1736 # Check the pre-context.
1737 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1738 #print "APW: ALLOWED: pre<$1>\n";
1739 $allowed = 1;
1740 }
1741 # Check the condition.
1742 my ($cond, $block) = @{$chunks[0]};
1743 if (defined $cond) {
1744 substr($block, 0, length($cond)) = '';
1745 }
1746 if (statement_lines($cond) > 1) {
1747 #print "APW: ALLOWED: cond<$cond>\n";
1748 $allowed = 1;
1749 }
1750 if ($block =~/\b(?:if|for|while)\b/) {
1751 #print "APW: ALLOWED: block<$block>\n";
1752 $allowed = 1;
1753 }
1754 if (statement_block_size($block) > 1) {
1755 #print "APW: ALLOWED: lines block<$block>\n";
1756 $allowed = 1;
1757 }
1758 # Check the post-context.
1759 if (defined $chunks[1]) {
1760 my ($cond, $block) = @{$chunks[1]};
1761 if (defined $cond) {
1762 substr($block, 0, length($cond)) = '';
1763 }
1764 if ($block =~ /^\s*\{/) {
1765 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1766 $allowed = 1;
1767 }
1768 }
1769 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1770 my $herectx = $here . "\n";;
1771 my $end = $linenr + statement_rawlines($block) - 1;
1772
1773 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1774 $herectx .= $rawlines[$ln] . "\n";;
f0a594c1 1775 }
cf655043
AW
1776
1777 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
f0a594c1
AW
1778 }
1779 }
1780
653d4876 1781# don't include deprecated include files (uses RAW line)
4a0df2ef 1782 for my $inc (@dep_includes) {
653d4876 1783 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
de7d4f0e 1784 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
0a920b5b
AW
1785 }
1786 }
1787
4a0df2ef
AW
1788# don't use deprecated functions
1789 for my $func (@dep_functions) {
00df344f 1790 if ($line =~ /\b$func\b/) {
de7d4f0e 1791 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
4a0df2ef
AW
1792 }
1793 }
1794
1795# no volatiles please
6c72ffaa
AW
1796 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1797 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
de7d4f0e 1798 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4a0df2ef
AW
1799 }
1800
9c0ca6f9
AW
1801# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1802 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1803 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1804 }
1805
00df344f
AW
1806# warn about #if 0
1807 if ($line =~ /^.#\s*if\s+0\b/) {
de7d4f0e
AW
1808 CHK("if this code is redundant consider removing it\n" .
1809 $herecurr);
4a0df2ef
AW
1810 }
1811
f0a594c1
AW
1812# check for needless kfree() checks
1813 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1814 my $expr = $1;
1815 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1816 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1817 }
1818 }
1819
00df344f
AW
1820# warn about #ifdefs in C files
1821# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1822# print "#ifdef in C files should be avoided\n";
1823# print "$herecurr";
1824# $clean = 0;
1825# }
1826
22f2a2ef
AW
1827# warn about spacing in #ifdefs
1828 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1829 ERROR("exactly one space required after that #$1\n" . $herecurr);
1830 }
1831
4a0df2ef
AW
1832# check for spinlock_t definitions without a comment.
1833 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1834 my $which = $1;
1835 if (!ctx_has_comment($first_line, $linenr)) {
de7d4f0e 1836 CHK("$1 definition without comment\n" . $herecurr);
4a0df2ef
AW
1837 }
1838 }
1839# check for memory barriers without a comment.
1840 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1841 if (!ctx_has_comment($first_line, $linenr)) {
de7d4f0e 1842 CHK("memory barrier without comment\n" . $herecurr);
4a0df2ef
AW
1843 }
1844 }
1845# check of hardware specific defines
22f2a2ef 1846 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
de7d4f0e 1847 CHK("architecture specific defines should be avoided\n" . $herecurr);
0a920b5b 1848 }
653d4876 1849
de7d4f0e
AW
1850# check the location of the inline attribute, that it is between
1851# storage class and type.
9c0ca6f9
AW
1852 if ($line =~ /\b$Type\s+$Inline\b/ ||
1853 $line =~ /\b$Inline\s+$Storage\b/) {
de7d4f0e
AW
1854 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1855 }
1856
8905a67c
AW
1857# Check for __inline__ and __inline, prefer inline
1858 if ($line =~ /\b(__inline__|__inline)\b/) {
1859 WARN("plain inline is preferred over $1\n" . $herecurr);
1860 }
1861
de7d4f0e
AW
1862# check for new externs in .c files.
1863 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1864 WARN("externs should be avoided in .c files\n" . $herecurr);
1865 }
1866
1867# checks for new __setup's
1868 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1869 my $name = $1;
1870
1871 if (!grep(/$name/, @setup_docs)) {
1872 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1873 }
653d4876 1874 }
9c0ca6f9
AW
1875
1876# check for pointless casting of kmalloc return
1877 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
1878 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
1879 }
13214adf
AW
1880
1881# check for gcc specific __FUNCTION__
1882 if ($line =~ /__FUNCTION__/) {
1883 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
1884 }
1885 }
1886
1887 # If we have no input at all, then there is nothing to report on
1888 # so just keep quiet.
1889 if ($#rawlines == -1) {
1890 exit(0);
0a920b5b
AW
1891 }
1892
8905a67c
AW
1893 # In mailback mode only produce a report in the negative, for
1894 # things that appear to be patches.
1895 if ($mailback && ($clean == 1 || !$is_patch)) {
1896 exit(0);
1897 }
1898
1899 # This is not a patch, and we are are in 'no-patch' mode so
1900 # just keep quiet.
1901 if (!$chk_patch && !$is_patch) {
1902 exit(0);
1903 }
1904
1905 if (!$is_patch) {
de7d4f0e 1906 ERROR("Does not appear to be a unified-diff format patch\n");
0a920b5b
AW
1907 }
1908 if ($is_patch && $chk_signoff && $signoff == 0) {
de7d4f0e 1909 ERROR("Missing Signed-off-by: line(s)\n");
0a920b5b
AW
1910 }
1911
8905a67c 1912 print report_dump();
13214adf
AW
1913 if ($summary && !($clean == 1 && $quiet == 1)) {
1914 print "$filename " if ($summary_file);
8905a67c
AW
1915 print "total: $cnt_error errors, $cnt_warn warnings, " .
1916 (($check)? "$cnt_chk checks, " : "") .
1917 "$cnt_lines lines checked\n";
1918 print "\n" if ($quiet == 0);
f0a594c1 1919 }
8905a67c 1920
0a920b5b 1921 if ($clean == 1 && $quiet == 0) {
c2fdda0d 1922 print "$vname has no obvious style problems and is ready for submission.\n"
0a920b5b
AW
1923 }
1924 if ($clean == 0 && $quiet == 0) {
c2fdda0d 1925 print "$vname has style problems, please review. If any of these errors\n";
0a920b5b
AW
1926 print "are false positives report them to the maintainer, see\n";
1927 print "CHECKPATCH in MAINTAINERS.\n";
1928 }
13214adf 1929
0a920b5b
AW
1930 return $clean;
1931}