diff options
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-x | scripts/checkpatch.pl | 500 |
1 files changed, 372 insertions, 128 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 579f50fa838c..2086a856400a 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -9,7 +9,7 @@ use strict; | |||
9 | my $P = $0; | 9 | my $P = $0; |
10 | $P =~ s@.*/@@g; | 10 | $P =~ s@.*/@@g; |
11 | 11 | ||
12 | my $V = '0.12'; | 12 | my $V = '0.14'; |
13 | 13 | ||
14 | use Getopt::Long qw(:config no_auto_abbrev); | 14 | use Getopt::Long qw(:config no_auto_abbrev); |
15 | 15 | ||
@@ -24,13 +24,14 @@ my $file = 0; | |||
24 | my $check = 0; | 24 | my $check = 0; |
25 | my $summary = 1; | 25 | my $summary = 1; |
26 | my $mailback = 0; | 26 | my $mailback = 0; |
27 | my $summary_file = 0; | ||
27 | my $root; | 28 | my $root; |
29 | my %debug; | ||
28 | GetOptions( | 30 | GetOptions( |
29 | 'q|quiet+' => \$quiet, | 31 | 'q|quiet+' => \$quiet, |
30 | 'tree!' => \$tree, | 32 | 'tree!' => \$tree, |
31 | 'signoff!' => \$chk_signoff, | 33 | 'signoff!' => \$chk_signoff, |
32 | 'patch!' => \$chk_patch, | 34 | 'patch!' => \$chk_patch, |
33 | 'test-type!' => \$tst_type, | ||
34 | 'emacs!' => \$emacs, | 35 | 'emacs!' => \$emacs, |
35 | 'terse!' => \$terse, | 36 | 'terse!' => \$terse, |
36 | 'file!' => \$file, | 37 | 'file!' => \$file, |
@@ -39,6 +40,10 @@ GetOptions( | |||
39 | 'root=s' => \$root, | 40 | 'root=s' => \$root, |
40 | 'summary!' => \$summary, | 41 | 'summary!' => \$summary, |
41 | 'mailback!' => \$mailback, | 42 | 'mailback!' => \$mailback, |
43 | 'summary-file!' => \$summary_file, | ||
44 | |||
45 | 'debug=s' => \%debug, | ||
46 | 'test-type!' => \$tst_type, | ||
42 | ) or exit; | 47 | ) or exit; |
43 | 48 | ||
44 | my $exit = 0; | 49 | my $exit = 0; |
@@ -46,16 +51,24 @@ my $exit = 0; | |||
46 | if ($#ARGV < 0) { | 51 | if ($#ARGV < 0) { |
47 | print "usage: $P [options] patchfile\n"; | 52 | print "usage: $P [options] patchfile\n"; |
48 | print "version: $V\n"; | 53 | print "version: $V\n"; |
49 | print "options: -q => quiet\n"; | 54 | print "options: -q => quiet\n"; |
50 | print " --no-tree => run without a kernel tree\n"; | 55 | print " --no-tree => run without a kernel tree\n"; |
51 | print " --terse => one line per report\n"; | 56 | print " --terse => one line per report\n"; |
52 | print " --emacs => emacs compile window format\n"; | 57 | print " --emacs => emacs compile window format\n"; |
53 | print " --file => check a source file\n"; | 58 | print " --file => check a source file\n"; |
54 | print " --strict => enable more subjective tests\n"; | 59 | print " --strict => enable more subjective tests\n"; |
55 | print " --root => path to the kernel tree root\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"; | ||
56 | exit(1); | 63 | exit(1); |
57 | } | 64 | } |
58 | 65 | ||
66 | my $dbg_values = 0; | ||
67 | my $dbg_possible = 0; | ||
68 | for my $key (keys %debug) { | ||
69 | eval "\${dbg_$key} = '$debug{$key}';" | ||
70 | } | ||
71 | |||
59 | if ($terse) { | 72 | if ($terse) { |
60 | $emacs = 1; | 73 | $emacs = 1; |
61 | $quiet++; | 74 | $quiet++; |
@@ -110,7 +123,7 @@ our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; | |||
110 | our $Operators = qr{ | 123 | our $Operators = qr{ |
111 | <=|>=|==|!=| | 124 | <=|>=|==|!=| |
112 | =>|->|<<|>>|<|>|!|~| | 125 | =>|->|<<|>>|<|>|!|~| |
113 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/ | 126 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
114 | }x; | 127 | }x; |
115 | 128 | ||
116 | our $NonptrType; | 129 | our $NonptrType; |
@@ -152,7 +165,7 @@ sub build_types { | |||
152 | $Type = qr{ | 165 | $Type = qr{ |
153 | \b$NonptrType\b | 166 | \b$NonptrType\b |
154 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? | 167 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? |
155 | (?:\s+$Sparse|\s+$Attribute)* | 168 | (?:\s+$Inline|\s+$Sparse|\s+$Attribute)* |
156 | }x; | 169 | }x; |
157 | $Declare = qr{(?:$Storage\s+)?$Type}; | 170 | $Declare = qr{(?:$Storage\s+)?$Type}; |
158 | } | 171 | } |
@@ -181,6 +194,8 @@ if ($tree && -f "$root/$removal") { | |||
181 | } | 194 | } |
182 | 195 | ||
183 | my @rawlines = (); | 196 | my @rawlines = (); |
197 | my @lines = (); | ||
198 | my $vname; | ||
184 | for my $filename (@ARGV) { | 199 | for my $filename (@ARGV) { |
185 | if ($file) { | 200 | if ($file) { |
186 | open(FILE, "diff -u /dev/null $filename|") || | 201 | open(FILE, "diff -u /dev/null $filename|") || |
@@ -189,15 +204,21 @@ for my $filename (@ARGV) { | |||
189 | open(FILE, "<$filename") || | 204 | open(FILE, "<$filename") || |
190 | die "$P: $filename: open failed - $!\n"; | 205 | die "$P: $filename: open failed - $!\n"; |
191 | } | 206 | } |
207 | if ($filename eq '-') { | ||
208 | $vname = 'Your patch'; | ||
209 | } else { | ||
210 | $vname = $filename; | ||
211 | } | ||
192 | while (<FILE>) { | 212 | while (<FILE>) { |
193 | chomp; | 213 | chomp; |
194 | push(@rawlines, $_); | 214 | push(@rawlines, $_); |
195 | } | 215 | } |
196 | close(FILE); | 216 | close(FILE); |
197 | if (!process($filename, @rawlines)) { | 217 | if (!process($filename)) { |
198 | $exit = 1; | 218 | $exit = 1; |
199 | } | 219 | } |
200 | @rawlines = (); | 220 | @rawlines = (); |
221 | @lines = (); | ||
201 | } | 222 | } |
202 | 223 | ||
203 | exit($exit); | 224 | exit($exit); |
@@ -274,20 +295,30 @@ sub sanitise_line { | |||
274 | my $l = ''; | 295 | my $l = ''; |
275 | 296 | ||
276 | my $quote = ''; | 297 | my $quote = ''; |
298 | my $qlen = 0; | ||
277 | 299 | ||
278 | foreach my $c (split(//, $line)) { | 300 | foreach my $c (split(//, $line)) { |
301 | # The second backslash of a pair is not a "quote". | ||
302 | if ($l eq "\\" && $c eq "\\") { | ||
303 | $c = 'X'; | ||
304 | } | ||
279 | if ($l ne "\\" && ($c eq "'" || $c eq '"')) { | 305 | if ($l ne "\\" && ($c eq "'" || $c eq '"')) { |
280 | if ($quote eq '') { | 306 | if ($quote eq '') { |
281 | $quote = $c; | 307 | $quote = $c; |
282 | $res .= $c; | 308 | $res .= $c; |
283 | $l = $c; | 309 | $l = $c; |
310 | $qlen = 0; | ||
284 | next; | 311 | next; |
285 | } elsif ($quote eq $c) { | 312 | } elsif ($quote eq $c) { |
286 | $quote = ''; | 313 | $quote = ''; |
287 | } | 314 | } |
288 | } | 315 | } |
316 | if ($quote eq "'" && $qlen > 1) { | ||
317 | $quote = ''; | ||
318 | } | ||
289 | if ($quote && $c ne "\t") { | 319 | if ($quote && $c ne "\t") { |
290 | $res .= "X"; | 320 | $res .= "X"; |
321 | $qlen++; | ||
291 | } else { | 322 | } else { |
292 | $res .= $c; | 323 | $res .= $c; |
293 | } | 324 | } |
@@ -295,6 +326,28 @@ sub sanitise_line { | |||
295 | $l = $c; | 326 | $l = $c; |
296 | } | 327 | } |
297 | 328 | ||
329 | # Clear out the comments. | ||
330 | while ($res =~ m@(/\*.*?\*/)@g) { | ||
331 | substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]); | ||
332 | } | ||
333 | if ($res =~ m@(/\*.*)@) { | ||
334 | substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]); | ||
335 | } | ||
336 | if ($res =~ m@^.(.*\*/)@) { | ||
337 | substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]); | ||
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 | |||
298 | return $res; | 351 | return $res; |
299 | } | 352 | } |
300 | 353 | ||
@@ -305,30 +358,36 @@ sub ctx_statement_block { | |||
305 | my $soff = $off; | 358 | my $soff = $off; |
306 | my $coff = $off - 1; | 359 | my $coff = $off - 1; |
307 | 360 | ||
361 | my $loff = 0; | ||
362 | |||
308 | my $type = ''; | 363 | my $type = ''; |
309 | my $level = 0; | 364 | my $level = 0; |
310 | my $c; | 365 | my $c; |
311 | my $len = 0; | 366 | my $len = 0; |
367 | |||
368 | my $remainder; | ||
312 | while (1) { | 369 | while (1) { |
313 | #warn "CSB: blk<$blk>\n"; | 370 | #warn "CSB: blk<$blk>\n"; |
314 | # If we are about to drop off the end, pull in more | 371 | # If we are about to drop off the end, pull in more |
315 | # context. | 372 | # context. |
316 | if ($off >= $len) { | 373 | if ($off >= $len) { |
317 | for (; $remain > 0; $line++) { | 374 | for (; $remain > 0; $line++) { |
318 | next if ($rawlines[$line] =~ /^-/); | 375 | next if ($lines[$line] =~ /^-/); |
319 | $remain--; | 376 | $remain--; |
320 | $blk .= sanitise_line($rawlines[$line]) . "\n"; | 377 | $loff = $len; |
378 | $blk .= $lines[$line] . "\n"; | ||
321 | $len = length($blk); | 379 | $len = length($blk); |
322 | $line++; | 380 | $line++; |
323 | last; | 381 | last; |
324 | } | 382 | } |
325 | # Bail if there is no further context. | 383 | # Bail if there is no further context. |
326 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; | 384 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
327 | if ($off == $len) { | 385 | if ($off >= $len) { |
328 | last; | 386 | last; |
329 | } | 387 | } |
330 | } | 388 | } |
331 | $c = substr($blk, $off, 1); | 389 | $c = substr($blk, $off, 1); |
390 | $remainder = substr($blk, $off); | ||
332 | 391 | ||
333 | #warn "CSB: c<$c> type<$type> level<$level>\n"; | 392 | #warn "CSB: c<$c> type<$type> level<$level>\n"; |
334 | # Statement ends at the ';' or a close '}' at the | 393 | # Statement ends at the ';' or a close '}' at the |
@@ -337,6 +396,12 @@ sub ctx_statement_block { | |||
337 | last; | 396 | last; |
338 | } | 397 | } |
339 | 398 | ||
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 | |||
340 | if (($type eq '' || $type eq '(') && $c eq '(') { | 405 | if (($type eq '' || $type eq '(') && $c eq '(') { |
341 | $level++; | 406 | $level++; |
342 | $type = '('; | 407 | $type = '('; |
@@ -363,6 +428,10 @@ sub ctx_statement_block { | |||
363 | } | 428 | } |
364 | $off++; | 429 | $off++; |
365 | } | 430 | } |
431 | if ($off == $len) { | ||
432 | $line++; | ||
433 | $remain--; | ||
434 | } | ||
366 | 435 | ||
367 | my $statement = substr($blk, $soff, $off - $soff + 1); | 436 | my $statement = substr($blk, $soff, $off - $soff + 1); |
368 | my $condition = substr($blk, $soff, $coff - $soff + 1); | 437 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
@@ -370,7 +439,30 @@ sub ctx_statement_block { | |||
370 | #warn "STATEMENT<$statement>\n"; | 439 | #warn "STATEMENT<$statement>\n"; |
371 | #warn "CONDITION<$condition>\n"; | 440 | #warn "CONDITION<$condition>\n"; |
372 | 441 | ||
373 | return ($statement, $condition); | 442 | #print "off<$off> loff<$loff>\n"; |
443 | |||
444 | return ($statement, $condition, | ||
445 | $line, $remain + 1, $off - $loff + 1, $level); | ||
446 | } | ||
447 | |||
448 | sub 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); | ||
374 | } | 466 | } |
375 | 467 | ||
376 | sub ctx_block_get { | 468 | sub ctx_block_get { |
@@ -500,103 +592,110 @@ sub cat_vet { | |||
500 | return $res; | 592 | return $res; |
501 | } | 593 | } |
502 | 594 | ||
595 | my $av_preprocessor = 0; | ||
596 | my $av_paren = 0; | ||
597 | my @av_paren_type; | ||
598 | |||
599 | sub annotate_reset { | ||
600 | $av_preprocessor = 0; | ||
601 | $av_paren = 0; | ||
602 | @av_paren_type = (); | ||
603 | } | ||
604 | |||
503 | sub annotate_values { | 605 | sub annotate_values { |
504 | my ($stream, $type) = @_; | 606 | my ($stream, $type) = @_; |
505 | 607 | ||
506 | my $res; | 608 | my $res; |
507 | my $cur = $stream; | 609 | my $cur = $stream; |
508 | 610 | ||
509 | my $debug = 0; | 611 | print "$stream\n" if ($dbg_values > 1); |
510 | |||
511 | print "$stream\n" if ($debug); | ||
512 | |||
513 | ##my $type = 'N'; | ||
514 | my $pos = 0; | ||
515 | my $preprocessor = 0; | ||
516 | my $paren = 0; | ||
517 | my @paren_type; | ||
518 | 612 | ||
519 | while (length($cur)) { | 613 | while (length($cur)) { |
520 | print " <$type> " if ($debug); | 614 | print " <$type> " if ($dbg_values > 1); |
521 | if ($cur =~ /^(\s+)/o) { | 615 | if ($cur =~ /^(\s+)/o) { |
522 | print "WS($1)\n" if ($debug); | 616 | print "WS($1)\n" if ($dbg_values > 1); |
523 | if ($1 =~ /\n/ && $preprocessor) { | 617 | if ($1 =~ /\n/ && $av_preprocessor) { |
524 | $preprocessor = 0; | 618 | $av_preprocessor = 0; |
525 | $type = 'N'; | 619 | $type = 'N'; |
526 | } | 620 | } |
527 | 621 | ||
528 | } elsif ($cur =~ /^($Type)/) { | 622 | } elsif ($cur =~ /^($Type)/) { |
529 | print "DECLARE($1)\n" if ($debug); | 623 | print "DECLARE($1)\n" if ($dbg_values > 1); |
530 | $type = 'T'; | 624 | $type = 'T'; |
531 | 625 | ||
532 | } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) { | 626 | } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) { |
533 | print "DEFINE($1)\n" if ($debug); | 627 | print "DEFINE($1)\n" if ($dbg_values > 1); |
534 | $preprocessor = 1; | 628 | $av_preprocessor = 1; |
535 | $paren_type[$paren] = 'N'; | 629 | $av_paren_type[$av_paren] = 'N'; |
536 | 630 | ||
537 | } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|endif))/o) { | 631 | } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|elif|endif))/o) { |
538 | print "PRE($1)\n" if ($debug); | 632 | print "PRE($1)\n" if ($dbg_values > 1); |
539 | $preprocessor = 1; | 633 | $av_preprocessor = 1; |
540 | $type = 'N'; | 634 | $type = 'N'; |
541 | 635 | ||
542 | } elsif ($cur =~ /^(\\\n)/o) { | 636 | } elsif ($cur =~ /^(\\\n)/o) { |
543 | print "PRECONT($1)\n" if ($debug); | 637 | print "PRECONT($1)\n" if ($dbg_values > 1); |
544 | 638 | ||
545 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { | 639 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
546 | print "SIZEOF($1)\n" if ($debug); | 640 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
547 | if (defined $2) { | 641 | if (defined $2) { |
548 | $paren_type[$paren] = 'V'; | 642 | $av_paren_type[$av_paren] = 'V'; |
549 | } | 643 | } |
550 | $type = 'N'; | 644 | $type = 'N'; |
551 | 645 | ||
552 | } elsif ($cur =~ /^(if|while|typeof|for)\b/o) { | 646 | } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) { |
553 | print "COND($1)\n" if ($debug); | 647 | print "COND($1)\n" if ($dbg_values > 1); |
554 | $paren_type[$paren] = 'N'; | 648 | $av_paren_type[$av_paren] = 'N'; |
555 | $type = 'N'; | 649 | $type = 'N'; |
556 | 650 | ||
557 | } elsif ($cur =~/^(return|case|else)/o) { | 651 | } elsif ($cur =~/^(return|case|else)/o) { |
558 | print "KEYWORD($1)\n" if ($debug); | 652 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
559 | $type = 'N'; | 653 | $type = 'N'; |
560 | 654 | ||
561 | } elsif ($cur =~ /^(\()/o) { | 655 | } elsif ($cur =~ /^(\()/o) { |
562 | print "PAREN('$1')\n" if ($debug); | 656 | print "PAREN('$1')\n" if ($dbg_values > 1); |
563 | $paren++; | 657 | $av_paren++; |
564 | $type = 'N'; | 658 | $type = 'N'; |
565 | 659 | ||
566 | } elsif ($cur =~ /^(\))/o) { | 660 | } elsif ($cur =~ /^(\))/o) { |
567 | $paren-- if ($paren > 0); | 661 | $av_paren-- if ($av_paren > 0); |
568 | if (defined $paren_type[$paren]) { | 662 | if (defined $av_paren_type[$av_paren]) { |
569 | $type = $paren_type[$paren]; | 663 | $type = $av_paren_type[$av_paren]; |
570 | undef $paren_type[$paren]; | 664 | undef $av_paren_type[$av_paren]; |
571 | print "PAREN('$1') -> $type\n" if ($debug); | 665 | print "PAREN('$1') -> $type\n" |
666 | if ($dbg_values > 1); | ||
572 | } else { | 667 | } else { |
573 | print "PAREN('$1')\n" if ($debug); | 668 | print "PAREN('$1')\n" if ($dbg_values > 1); |
574 | } | 669 | } |
575 | 670 | ||
576 | } elsif ($cur =~ /^($Ident)\(/o) { | 671 | } elsif ($cur =~ /^($Ident)\(/o) { |
577 | print "FUNC($1)\n" if ($debug); | 672 | print "FUNC($1)\n" if ($dbg_values > 1); |
578 | $paren_type[$paren] = 'V'; | 673 | $av_paren_type[$av_paren] = 'V'; |
579 | 674 | ||
580 | } elsif ($cur =~ /^($Ident|$Constant)/o) { | 675 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
581 | print "IDENT($1)\n" if ($debug); | 676 | print "IDENT($1)\n" if ($dbg_values > 1); |
582 | $type = 'V'; | 677 | $type = 'V'; |
583 | 678 | ||
584 | } elsif ($cur =~ /^($Assignment)/o) { | 679 | } elsif ($cur =~ /^($Assignment)/o) { |
585 | print "ASSIGN($1)\n" if ($debug); | 680 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
586 | $type = 'N'; | 681 | $type = 'N'; |
587 | 682 | ||
683 | } elsif ($cur =~/^(;)/) { | ||
684 | print "END($1)\n" if ($dbg_values > 1); | ||
685 | $type = 'E'; | ||
686 | |||
588 | } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { | 687 | } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { |
589 | print "END($1)\n" if ($debug); | 688 | print "CLOSE($1)\n" if ($dbg_values > 1); |
590 | $type = 'N'; | 689 | $type = 'N'; |
591 | 690 | ||
592 | } elsif ($cur =~ /^($Operators)/o) { | 691 | } elsif ($cur =~ /^($Operators)/o) { |
593 | print "OP($1)\n" if ($debug); | 692 | print "OP($1)\n" if ($dbg_values > 1); |
594 | if ($1 ne '++' && $1 ne '--') { | 693 | if ($1 ne '++' && $1 ne '--') { |
595 | $type = 'N'; | 694 | $type = 'N'; |
596 | } | 695 | } |
597 | 696 | ||
598 | } elsif ($cur =~ /(^.)/o) { | 697 | } elsif ($cur =~ /(^.)/o) { |
599 | print "C($1)\n" if ($debug); | 698 | print "C($1)\n" if ($dbg_values > 1); |
600 | } | 699 | } |
601 | if (defined $1) { | 700 | if (defined $1) { |
602 | $cur = substr($cur, length($1)); | 701 | $cur = substr($cur, length($1)); |
@@ -608,7 +707,7 @@ sub annotate_values { | |||
608 | } | 707 | } |
609 | 708 | ||
610 | sub possible { | 709 | sub possible { |
611 | my ($possible) = @_; | 710 | my ($possible, $line) = @_; |
612 | 711 | ||
613 | #print "CHECK<$possible>\n"; | 712 | #print "CHECK<$possible>\n"; |
614 | if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && | 713 | if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && |
@@ -616,7 +715,7 @@ sub possible { | |||
616 | $possible ne 'struct' && $possible ne 'enum' && | 715 | $possible ne 'struct' && $possible ne 'enum' && |
617 | $possible ne 'case' && $possible ne 'else' && | 716 | $possible ne 'case' && $possible ne 'else' && |
618 | $possible ne 'typedef') { | 717 | $possible ne 'typedef') { |
619 | #print "POSSIBLE<$possible>\n"; | 718 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
620 | push(@typeList, $possible); | 719 | push(@typeList, $possible); |
621 | build_types(); | 720 | build_types(); |
622 | } | 721 | } |
@@ -624,16 +723,15 @@ sub possible { | |||
624 | 723 | ||
625 | my $prefix = ''; | 724 | my $prefix = ''; |
626 | 725 | ||
627 | my @report = (); | ||
628 | sub report { | 726 | sub report { |
629 | my $line = $prefix . $_[0]; | 727 | my $line = $prefix . $_[0]; |
630 | 728 | ||
631 | $line = (split('\n', $line))[0] . "\n" if ($terse); | 729 | $line = (split('\n', $line))[0] . "\n" if ($terse); |
632 | 730 | ||
633 | push(@report, $line); | 731 | push(our @report, $line); |
634 | } | 732 | } |
635 | sub report_dump { | 733 | sub report_dump { |
636 | @report; | 734 | our @report; |
637 | } | 735 | } |
638 | sub ERROR { | 736 | sub ERROR { |
639 | report("ERROR: $_[0]\n"); | 737 | report("ERROR: $_[0]\n"); |
@@ -655,11 +753,12 @@ sub CHK { | |||
655 | 753 | ||
656 | sub process { | 754 | sub process { |
657 | my $filename = shift; | 755 | my $filename = shift; |
658 | my @lines = @_; | ||
659 | 756 | ||
660 | my $linenr=0; | 757 | my $linenr=0; |
661 | my $prevline=""; | 758 | my $prevline=""; |
759 | my $prevrawline=""; | ||
662 | my $stashline=""; | 760 | my $stashline=""; |
761 | my $stashrawline=""; | ||
663 | 762 | ||
664 | my $length; | 763 | my $length; |
665 | my $indent; | 764 | my $indent; |
@@ -670,6 +769,7 @@ sub process { | |||
670 | my $signoff = 0; | 769 | my $signoff = 0; |
671 | my $is_patch = 0; | 770 | my $is_patch = 0; |
672 | 771 | ||
772 | our @report = (); | ||
673 | our $cnt_lines = 0; | 773 | our $cnt_lines = 0; |
674 | our $cnt_error = 0; | 774 | our $cnt_error = 0; |
675 | our $cnt_warn = 0; | 775 | our $cnt_warn = 0; |
@@ -681,14 +781,29 @@ sub process { | |||
681 | my $realcnt = 0; | 781 | my $realcnt = 0; |
682 | my $here = ''; | 782 | my $here = ''; |
683 | my $in_comment = 0; | 783 | my $in_comment = 0; |
784 | my $comment_edge = 0; | ||
684 | my $first_line = 0; | 785 | my $first_line = 0; |
685 | 786 | ||
686 | my $prev_values = 'N'; | 787 | my $prev_values = 'E'; |
788 | |||
789 | # suppression flags | ||
790 | my $suppress_ifbraces = 0; | ||
687 | 791 | ||
792 | # Pre-scan the patch sanitizing the lines. | ||
688 | # Pre-scan the patch looking for any __setup documentation. | 793 | # Pre-scan the patch looking for any __setup documentation. |
794 | # | ||
689 | my @setup_docs = (); | 795 | my @setup_docs = (); |
690 | my $setup_docs = 0; | 796 | my $setup_docs = 0; |
691 | foreach my $line (@lines) { | 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 | |||
692 | if ($line=~/^\+\+\+\s+(\S+)/) { | 807 | if ($line=~/^\+\+\+\s+(\S+)/) { |
693 | $setup_docs = 0; | 808 | $setup_docs = 0; |
694 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { | 809 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
@@ -707,8 +822,7 @@ sub process { | |||
707 | foreach my $line (@lines) { | 822 | foreach my $line (@lines) { |
708 | $linenr++; | 823 | $linenr++; |
709 | 824 | ||
710 | my $rawline = $line; | 825 | my $rawline = $rawlines[$linenr - 1]; |
711 | |||
712 | 826 | ||
713 | #extract the filename as it passes | 827 | #extract the filename as it passes |
714 | if ($line=~/^\+\+\+\s+(\S+)/) { | 828 | if ($line=~/^\+\+\+\s+(\S+)/) { |
@@ -728,7 +842,10 @@ sub process { | |||
728 | } else { | 842 | } else { |
729 | $realcnt=1+1; | 843 | $realcnt=1+1; |
730 | } | 844 | } |
731 | $prev_values = 'N'; | 845 | annotate_reset(); |
846 | $prev_values = 'E'; | ||
847 | |||
848 | $suppress_ifbraces = $linenr - 1; | ||
732 | next; | 849 | next; |
733 | } | 850 | } |
734 | 851 | ||
@@ -746,7 +863,7 @@ sub process { | |||
746 | if ($linenr == $first_line) { | 863 | if ($linenr == $first_line) { |
747 | my $edge; | 864 | my $edge; |
748 | for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) { | 865 | for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) { |
749 | ($edge) = ($lines[$ln - 1] =~ m@(/\*|\*/)@); | 866 | ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@); |
750 | last if (defined $edge); | 867 | last if (defined $edge); |
751 | } | 868 | } |
752 | if (defined $edge && $edge eq '*/') { | 869 | if (defined $edge && $edge eq '*/') { |
@@ -757,25 +874,30 @@ sub process { | |||
757 | # Guestimate if this is a continuing comment. If this | 874 | # Guestimate if this is a continuing comment. If this |
758 | # is the start of a diff block and this line starts | 875 | # is the start of a diff block and this line starts |
759 | # ' *' then it is very likely a comment. | 876 | # ' *' then it is very likely a comment. |
760 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { | 877 | if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) { |
761 | $in_comment = 1; | 878 | $in_comment = 1; |
762 | } | 879 | } |
763 | 880 | ||
764 | # Find the last comment edge on _this_ line. | 881 | # Find the last comment edge on _this_ line. |
765 | while (($line =~ m@(/\*|\*/)@g)) { | 882 | $comment_edge = 0; |
883 | while (($rawline =~ m@(/\*|\*/)@g)) { | ||
766 | if ($1 eq '/*') { | 884 | if ($1 eq '/*') { |
767 | $in_comment = 1; | 885 | $in_comment = 1; |
768 | } else { | 886 | } else { |
769 | $in_comment = 0; | 887 | $in_comment = 0; |
770 | } | 888 | } |
889 | $comment_edge = 1; | ||
771 | } | 890 | } |
772 | 891 | ||
773 | # Measure the line length and indent. | 892 | # Measure the line length and indent. |
774 | ($length, $indent) = line_stats($line); | 893 | ($length, $indent) = line_stats($rawline); |
775 | 894 | ||
776 | # Track the previous line. | 895 | # Track the previous line. |
777 | ($prevline, $stashline) = ($stashline, $line); | 896 | ($prevline, $stashline) = ($stashline, $line); |
778 | ($previndent, $stashindent) = ($stashindent, $indent); | 897 | ($previndent, $stashindent) = ($stashindent, $indent); |
898 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); | ||
899 | |||
900 | #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n"; | ||
779 | 901 | ||
780 | } elsif ($realcnt == 1) { | 902 | } elsif ($realcnt == 1) { |
781 | $realcnt--; | 903 | $realcnt--; |
@@ -786,9 +908,9 @@ sub process { | |||
786 | $here = "#$realline: " if ($file); | 908 | $here = "#$realline: " if ($file); |
787 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); | 909 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
788 | 910 | ||
789 | my $hereline = "$here\n$line\n"; | 911 | my $hereline = "$here\n$rawline\n"; |
790 | my $herecurr = "$here\n$line\n"; | 912 | my $herecurr = "$here\n$rawline\n"; |
791 | my $hereprev = "$here\n$prevline\n$line\n"; | 913 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
792 | 914 | ||
793 | $prefix = "$filename:$realline: " if ($emacs && $file); | 915 | $prefix = "$filename:$realline: " if ($emacs && $file); |
794 | $prefix = "$filename:$linenr: " if ($emacs && !$file); | 916 | $prefix = "$filename:$linenr: " if ($emacs && !$file); |
@@ -816,7 +938,7 @@ sub process { | |||
816 | 938 | ||
817 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php | 939 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
818 | if (($realfile =~ /^$/ || $line =~ /^\+/) && | 940 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
819 | !($line =~ m/^( | 941 | !($rawline =~ m/^( |
820 | [\x09\x0A\x0D\x20-\x7E] # ASCII | 942 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
821 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | 943 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
822 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | 944 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
@@ -826,7 +948,7 @@ sub process { | |||
826 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | 948 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
827 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 | 949 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
828 | )*$/x )) { | 950 | )*$/x )) { |
829 | ERROR("Invalid UTF-8\n" . $herecurr); | 951 | ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr); |
830 | } | 952 | } |
831 | 953 | ||
832 | #ignore lines being removed | 954 | #ignore lines being removed |
@@ -837,15 +959,15 @@ sub process { | |||
837 | 959 | ||
838 | #trailing whitespace | 960 | #trailing whitespace |
839 | if ($line =~ /^\+.*\015/) { | 961 | if ($line =~ /^\+.*\015/) { |
840 | my $herevet = "$here\n" . cat_vet($line) . "\n"; | 962 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
841 | ERROR("DOS line endings\n" . $herevet); | 963 | ERROR("DOS line endings\n" . $herevet); |
842 | 964 | ||
843 | } elsif ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) { | 965 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
844 | my $herevet = "$here\n" . cat_vet($line) . "\n"; | 966 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
845 | ERROR("trailing whitespace\n" . $herevet); | 967 | ERROR("trailing whitespace\n" . $herevet); |
846 | } | 968 | } |
847 | #80 column limit | 969 | #80 column limit |
848 | if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) { | 970 | if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) { |
849 | WARN("line over 80 characters\n" . $herecurr); | 971 | WARN("line over 80 characters\n" . $herecurr); |
850 | } | 972 | } |
851 | 973 | ||
@@ -859,46 +981,48 @@ sub process { | |||
859 | 981 | ||
860 | # at the beginning of a line any tabs must come first and anything | 982 | # at the beginning of a line any tabs must come first and anything |
861 | # more than 8 must use tabs. | 983 | # more than 8 must use tabs. |
862 | if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { | 984 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
863 | my $herevet = "$here\n" . cat_vet($line) . "\n"; | 985 | $rawline =~ /^\+\s* \s*/) { |
986 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | ||
864 | ERROR("use tabs not spaces\n" . $herevet); | 987 | ERROR("use tabs not spaces\n" . $herevet); |
865 | } | 988 | } |
866 | 989 | ||
867 | # Remove comments from the line before processing. | 990 | # check for RCS/CVS revision markers |
868 | my $comment_edge = ($line =~ s@/\*.*\*/@@g) + | 991 | if ($rawline =~ /\$(Revision|Log|Id)(?:\$|)/) { |
869 | ($line =~ s@/\*.*@@) + | 992 | WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
870 | ($line =~ s@^(.).*\*/@$1@); | 993 | } |
871 | 994 | ||
872 | # The rest of our checks refer specifically to C style | 995 | # The rest of our checks refer specifically to C style |
873 | # only apply those _outside_ comments. Only skip | 996 | # only apply those _outside_ comments. Only skip |
874 | # lines in the middle of comments. | 997 | # lines in the middle of comments. |
875 | next if (!$comment_edge && $in_comment); | 998 | next if (!$comment_edge && $in_comment); |
876 | 999 | ||
877 | # Standardise the strings and chars within the input to simplify matching. | ||
878 | $line = sanitise_line($line); | ||
879 | |||
880 | # Check for potential 'bare' types | 1000 | # Check for potential 'bare' types |
881 | if ($realcnt && | 1001 | if ($realcnt) { |
882 | $line !~ /$Ident:\s*$/ && | 1002 | # Ignore goto labels. |
883 | ($line =~ /^.\s*$Ident\s*\(\*+\s*$Ident\)\s*\(/ || | 1003 | if ($line =~ /$Ident:\*$/) { |
884 | $line !~ /^.\s*$Ident\s*\(/)) { | 1004 | |
1005 | # Ignore functions being called | ||
1006 | } elsif ($line =~ /^.\s*$Ident\s*\(/) { | ||
1007 | |||
885 | # definitions in global scope can only start with types | 1008 | # definitions in global scope can only start with types |
886 | if ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) { | 1009 | } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) { |
887 | possible($1); | 1010 | possible($1, $line); |
888 | 1011 | ||
889 | # declarations always start with types | 1012 | # declarations always start with types |
890 | } elsif ($prev_values eq 'N' && $line =~ /^.\s*(?:$Storage\s+)?($Ident)\b\s*\**\s*$Ident\s*(?:;|=)/) { | 1013 | } elsif ($prev_values eq 'E' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) { |
891 | possible($1); | 1014 | possible($1); |
1015 | } | ||
892 | 1016 | ||
893 | # any (foo ... *) is a pointer cast, and foo is a type | 1017 | # any (foo ... *) is a pointer cast, and foo is a type |
894 | } elsif ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/) { | 1018 | while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) { |
895 | possible($1); | 1019 | possible($1, $line); |
896 | } | 1020 | } |
897 | 1021 | ||
898 | # Check for any sort of function declaration. | 1022 | # Check for any sort of function declaration. |
899 | # int foo(something bar, other baz); | 1023 | # int foo(something bar, other baz); |
900 | # void (*store_gdt)(x86_descr_ptr *); | 1024 | # void (*store_gdt)(x86_descr_ptr *); |
901 | if ($prev_values eq 'N' && $line =~ /^(.(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) { | 1025 | if ($prev_values eq 'E' && $line =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) { |
902 | my ($name_len) = length($1); | 1026 | my ($name_len) = length($1); |
903 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len); | 1027 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len); |
904 | my $ctx = join("\n", @ctx); | 1028 | my $ctx = join("\n", @ctx); |
@@ -909,7 +1033,7 @@ sub process { | |||
909 | for my $arg (split(/\s*,\s*/, $ctx)) { | 1033 | for my $arg (split(/\s*,\s*/, $ctx)) { |
910 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) { | 1034 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) { |
911 | 1035 | ||
912 | possible($1); | 1036 | possible($1, $line); |
913 | } | 1037 | } |
914 | } | 1038 | } |
915 | } | 1039 | } |
@@ -974,8 +1098,11 @@ sub process { | |||
974 | my $opline = $line; $opline =~ s/^./ /; | 1098 | my $opline = $line; $opline =~ s/^./ /; |
975 | my $curr_values = annotate_values($opline . "\n", $prev_values); | 1099 | my $curr_values = annotate_values($opline . "\n", $prev_values); |
976 | $curr_values = $prev_values . $curr_values; | 1100 | $curr_values = $prev_values . $curr_values; |
977 | #warn "--> $opline\n"; | 1101 | if ($dbg_values) { |
978 | #warn "--> $curr_values ($prev_values)\n"; | 1102 | my $outline = $opline; $outline =~ s/\t/ /g; |
1103 | warn "--> .$outline\n"; | ||
1104 | warn "--> $curr_values\n"; | ||
1105 | } | ||
979 | $prev_values = substr($curr_values, -1); | 1106 | $prev_values = substr($curr_values, -1); |
980 | 1107 | ||
981 | #ignore lines not being added | 1108 | #ignore lines not being added |
@@ -1004,9 +1131,6 @@ sub process { | |||
1004 | ERROR("malformed #include filename\n" . | 1131 | ERROR("malformed #include filename\n" . |
1005 | $herecurr); | 1132 | $herecurr); |
1006 | } | 1133 | } |
1007 | # Sanitise this special form of string. | ||
1008 | $path = 'X' x length($path); | ||
1009 | $line =~ s{\<.*\>}{<$path>}; | ||
1010 | } | 1134 | } |
1011 | 1135 | ||
1012 | # no C99 // comments | 1136 | # no C99 // comments |
@@ -1074,7 +1198,7 @@ sub process { | |||
1074 | # } | 1198 | # } |
1075 | 1199 | ||
1076 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { | 1200 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
1077 | WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged" . $herecurr); | 1201 | WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); |
1078 | } | 1202 | } |
1079 | 1203 | ||
1080 | # printk should use KERN_* levels. Note that follow on printk's on the | 1204 | # printk should use KERN_* levels. Note that follow on printk's on the |
@@ -1102,7 +1226,7 @@ sub process { | |||
1102 | 1226 | ||
1103 | # function brace can't be on same line, except for #defines of do while, | 1227 | # function brace can't be on same line, except for #defines of do while, |
1104 | # or if closed on same line | 1228 | # or if closed on same line |
1105 | if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and | 1229 | if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and |
1106 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { | 1230 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { |
1107 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); | 1231 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); |
1108 | } | 1232 | } |
@@ -1115,8 +1239,22 @@ sub process { | |||
1115 | 1239 | ||
1116 | # check for spaces between functions and their parentheses. | 1240 | # check for spaces between functions and their parentheses. |
1117 | while ($line =~ /($Ident)\s+\(/g) { | 1241 | while ($line =~ /($Ident)\s+\(/g) { |
1118 | if ($1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/ && | 1242 | my $name = $1; |
1119 | $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) { | 1243 | my $ctx = substr($line, 0, $-[1]); |
1244 | |||
1245 | # Ignore those directives where spaces _are_ permitted. | ||
1246 | if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) { | ||
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 { | ||
1120 | WARN("no space between function name and open parenthesis '('\n" . $herecurr); | 1258 | WARN("no space between function name and open parenthesis '('\n" . $herecurr); |
1121 | } | 1259 | } |
1122 | } | 1260 | } |
@@ -1126,9 +1264,9 @@ sub process { | |||
1126 | <<=|>>=|<=|>=|==|!=| | 1264 | <<=|>>=|<=|>=|==|!=| |
1127 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| | 1265 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
1128 | =>|->|<<|>>|<|>|=|!|~| | 1266 | =>|->|<<|>>|<|>|=|!|~| |
1129 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/ | 1267 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
1130 | }x; | 1268 | }x; |
1131 | my @elements = split(/($ops|;)/, $opline); | 1269 | my @elements = split(/($;+|$ops|;)/, $opline); |
1132 | my $off = 0; | 1270 | my $off = 0; |
1133 | 1271 | ||
1134 | my $blank = copy_spacing($opline); | 1272 | my $blank = copy_spacing($opline); |
@@ -1188,8 +1326,15 @@ sub process { | |||
1188 | # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n"; | 1326 | # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n"; |
1189 | #} | 1327 | #} |
1190 | 1328 | ||
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 | |||
1191 | # ; should have either the end of line or a space or \ after it | 1336 | # ; should have either the end of line or a space or \ after it |
1192 | if ($op eq ';') { | 1337 | } elsif ($op eq ';') { |
1193 | if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && | 1338 | if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && |
1194 | $cc !~ /^;/) { | 1339 | $cc !~ /^;/) { |
1195 | ERROR("need space after that '$op' $at\n" . $hereptr); | 1340 | ERROR("need space after that '$op' $at\n" . $hereptr); |
@@ -1231,7 +1376,7 @@ sub process { | |||
1231 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { | 1376 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { |
1232 | ERROR("need space one side of that '$op' $at\n" . $hereptr); | 1377 | ERROR("need space one side of that '$op' $at\n" . $hereptr); |
1233 | } | 1378 | } |
1234 | if ($ctx =~ /Wx./ && $cc =~ /^;/) { | 1379 | if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
1235 | ERROR("no space before that '$op' $at\n" . $hereptr); | 1380 | ERROR("no space before that '$op' $at\n" . $hereptr); |
1236 | } | 1381 | } |
1237 | 1382 | ||
@@ -1239,7 +1384,8 @@ sub process { | |||
1239 | } elsif ($op eq '<<' or $op eq '>>' or | 1384 | } elsif ($op eq '<<' or $op eq '>>' or |
1240 | $op eq '&' or $op eq '^' or $op eq '|' or | 1385 | $op eq '&' or $op eq '^' or $op eq '|' or |
1241 | $op eq '+' or $op eq '-' or | 1386 | $op eq '+' or $op eq '-' or |
1242 | $op eq '*' or $op eq '/') | 1387 | $op eq '*' or $op eq '/' or |
1388 | $op eq '%') | ||
1243 | { | 1389 | { |
1244 | if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) { | 1390 | if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) { |
1245 | ERROR("need consistent spacing around '$op' $at\n" . | 1391 | ERROR("need consistent spacing around '$op' $at\n" . |
@@ -1303,7 +1449,7 @@ sub process { | |||
1303 | $line !~ /for\s*\(\s+;/) { | 1449 | $line !~ /for\s*\(\s+;/) { |
1304 | ERROR("no space after that open parenthesis '('\n" . $herecurr); | 1450 | ERROR("no space after that open parenthesis '('\n" . $herecurr); |
1305 | } | 1451 | } |
1306 | if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ && | 1452 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
1307 | $line !~ /for\s*\(.*;\s+\)/) { | 1453 | $line !~ /for\s*\(.*;\s+\)/) { |
1308 | ERROR("no space before that close parenthesis ')'\n" . $herecurr); | 1454 | ERROR("no space before that close parenthesis ')'\n" . $herecurr); |
1309 | } | 1455 | } |
@@ -1324,23 +1470,41 @@ sub process { | |||
1324 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); | 1470 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
1325 | 1471 | ||
1326 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) { | 1472 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) { |
1327 | ERROR("do not use assignment in if condition ($c)\n" . $herecurr); | 1473 | ERROR("do not use assignment in if condition\n" . $herecurr); |
1328 | } | 1474 | } |
1329 | 1475 | ||
1330 | # Find out what is on the end of the line after the | 1476 | # Find out what is on the end of the line after the |
1331 | # conditional. | 1477 | # conditional. |
1332 | substr($s, 0, length($c)) = ''; | 1478 | substr($s, 0, length($c)) = ''; |
1333 | $s =~ s/\n.*//g; | 1479 | $s =~ s/\n.*//g; |
1334 | 1480 | $s =~ s/$;//g; # Remove any comments | |
1335 | if (length($c) && $s !~ /^\s*({|;|\/\*.*\*\/)?\s*\\*\s*$/) { | 1481 | if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) { |
1336 | ERROR("trailing statements should be on next line\n" . $herecurr); | 1482 | ERROR("trailing statements should be on next line\n" . $herecurr); |
1337 | } | 1483 | } |
1338 | } | 1484 | } |
1339 | 1485 | ||
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 | |||
1340 | # if and else should not have general statements after it | 1501 | # if and else should not have general statements after it |
1341 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ && | 1502 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
1342 | $1 !~ /^\s*(?:\sif|{|\\|$)/) { | 1503 | my $s = $1; |
1343 | ERROR("trailing statements should be on next line\n" . $herecurr); | 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 | } | ||
1344 | } | 1508 | } |
1345 | 1509 | ||
1346 | # Check for }<nl>else {, these must be at the same | 1510 | # Check for }<nl>else {, these must be at the same |
@@ -1350,6 +1514,20 @@ sub process { | |||
1350 | ERROR("else should follow close brace '}'\n" . $hereprev); | 1514 | ERROR("else should follow close brace '}'\n" . $hereprev); |
1351 | } | 1515 | } |
1352 | 1516 | ||
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 | |||
1353 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new | 1531 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
1354 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { | 1532 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { |
1355 | # print "No studly caps, use _\n"; | 1533 | # print "No studly caps, use _\n"; |
@@ -1419,7 +1597,48 @@ sub process { | |||
1419 | } | 1597 | } |
1420 | 1598 | ||
1421 | # check for redundant bracing round if etc | 1599 | # check for redundant bracing round if etc |
1422 | if ($line =~ /\b(if|while|for|else)\b/) { | 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/) { | ||
1423 | # Locate the end of the opening statement. | 1642 | # Locate the end of the opening statement. |
1424 | my @control = ctx_statement($linenr, $realcnt, 0); | 1643 | my @control = ctx_statement($linenr, $realcnt, 0); |
1425 | my $nr = $linenr + (scalar(@control) - 1); | 1644 | my $nr = $linenr + (scalar(@control) - 1); |
@@ -1442,13 +1661,16 @@ sub process { | |||
1442 | my $after = $1; | 1661 | my $after = $1; |
1443 | 1662 | ||
1444 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; | 1663 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; |
1445 | #print "stmt<$stmt>\n\n"; | 1664 | #print "before<$before> stmt<$stmt> after<$after>\n\n"; |
1446 | 1665 | ||
1447 | # Count the newlines, if there is only one | 1666 | # Count the newlines, if there is only one |
1448 | # then the block should not have {}'s. | 1667 | # then the block should not have {}'s. |
1449 | my @lines = ($stmt =~ /\n/g); | 1668 | my @lines = ($stmt =~ /\n/g); |
1669 | my @statements = ($stmt =~ /;/g); | ||
1450 | #print "lines<" . scalar(@lines) . ">\n"; | 1670 | #print "lines<" . scalar(@lines) . ">\n"; |
1671 | #print "statements<" . scalar(@statements) . ">\n"; | ||
1451 | if ($lvl == 0 && scalar(@lines) == 0 && | 1672 | if ($lvl == 0 && scalar(@lines) == 0 && |
1673 | scalar(@statements) < 2 && | ||
1452 | $stmt !~ /{/ && $stmt !~ /\bif\b/ && | 1674 | $stmt !~ /{/ && $stmt !~ /\bif\b/ && |
1453 | $before !~ /}/ && $after !~ /{/) { | 1675 | $before !~ /}/ && $after !~ /{/) { |
1454 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; | 1676 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; |
@@ -1557,6 +1779,17 @@ sub process { | |||
1557 | if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { | 1779 | if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { |
1558 | WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); | 1780 | WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
1559 | } | 1781 | } |
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); | ||
1560 | } | 1793 | } |
1561 | 1794 | ||
1562 | # In mailback mode only produce a report in the negative, for | 1795 | # In mailback mode only produce a report in the negative, for |
@@ -1579,7 +1812,8 @@ sub process { | |||
1579 | } | 1812 | } |
1580 | 1813 | ||
1581 | print report_dump(); | 1814 | print report_dump(); |
1582 | if ($summary) { | 1815 | if ($summary && !($clean == 1 && $quiet == 1)) { |
1816 | print "$filename " if ($summary_file); | ||
1583 | print "total: $cnt_error errors, $cnt_warn warnings, " . | 1817 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
1584 | (($check)? "$cnt_chk checks, " : "") . | 1818 | (($check)? "$cnt_chk checks, " : "") . |
1585 | "$cnt_lines lines checked\n"; | 1819 | "$cnt_lines lines checked\n"; |
@@ -1587,12 +1821,22 @@ sub process { | |||
1587 | } | 1821 | } |
1588 | 1822 | ||
1589 | if ($clean == 1 && $quiet == 0) { | 1823 | if ($clean == 1 && $quiet == 0) { |
1590 | print "Your patch has no obvious style problems and is ready for submission.\n" | 1824 | print "$vname has no obvious style problems and is ready for submission.\n" |
1591 | } | 1825 | } |
1592 | if ($clean == 0 && $quiet == 0) { | 1826 | if ($clean == 0 && $quiet == 0) { |
1593 | print "Your patch has style problems, please review. If any of these errors\n"; | 1827 | print "$vname has style problems, please review. If any of these errors\n"; |
1594 | print "are false positives report them to the maintainer, see\n"; | 1828 | print "are false positives report them to the maintainer, see\n"; |
1595 | print "CHECKPATCH in MAINTAINERS.\n"; | 1829 | print "CHECKPATCH in MAINTAINERS.\n"; |
1596 | } | 1830 | } |
1831 | print <<EOL if ($file == 1 && $quiet == 0); | ||
1832 | |||
1833 | WARNING: Using --file mode. Please do not send patches to linux-kernel | ||
1834 | that change whole existing files if you did not significantly change most | ||
1835 | of the the file for other reasons anyways or just wrote the file newly | ||
1836 | from scratch. Pure code style patches have a significant cost in a | ||
1837 | quickly changing code base like Linux because they cause rejects | ||
1838 | with other changes. | ||
1839 | EOL | ||
1840 | |||
1597 | return $clean; | 1841 | return $clean; |
1598 | } | 1842 | } |