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