diff options
author | Andy Whitcroft <apw@shadowen.org> | 2008-02-08 07:22:03 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2008-02-08 12:22:42 -0500 |
commit | 13214adf738abc92b0a00c0763fd3be79eebaa7c (patch) | |
tree | 89e6e4ac322c9bea2b4b6cf988357853b5d1ae40 | |
parent | 24649c00ca334955ac7d8a79f5a7834fc7ea441d (diff) |
update checkpatch.pl to version 0.14
This version brings the remainder of the queued fixes. A number of fixes
for items missed reported by Andrew Morton and others. Also a handful
of new checks and fixes for false positives. Of note:
- new warning associated with --file to try and avoid cleanup only patches,
- corrected handling of completly empty files,
- corrected report handling with multiple files,
- handling of possible types in the face of multiple declarations,
- detection of unnessary braces on complex if statements (where present), and
- all new comment spacing handling.
Andi Kleen (1):
Introduce a warning when --file mode is used
Andy Whitcroft (14):
Version: 0.14
clean up some space violations in checkpatch.pl
a completly empty file should not provoke a whinge
reset report lines buffers between files
unary ++/-- may abutt close braces
__typeof__ is also unary
comments: revamp comment handling
add --summary-file option adding filename to summary line
trailing backslashes are not trailing statements
handle operators passed as parameters such as to ASSERTCMP
possible types -- enhance debugging
check for boolean operations with constants
possible types: handle multiple declarations
detect and report if statements where all branches are single statements
Arjan van de Ven (1):
quiet option should not print the summary on no errors
Bartlomiej Zolnierkiewicz (1):
warn about using __FUNCTION__
Timur Tabi (1):
loosen spacing checks for __asm__
Signed-off-by: Andy Whitcroft <apw@shadowen.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rwxr-xr-x | scripts/checkpatch.pl | 226 |
1 files changed, 184 insertions, 42 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 545471a99eea..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.13'; | 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,6 +24,7 @@ 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; |
28 | my %debug; | 29 | my %debug; |
29 | GetOptions( | 30 | GetOptions( |
@@ -31,7 +32,6 @@ GetOptions( | |||
31 | 'tree!' => \$tree, | 32 | 'tree!' => \$tree, |
32 | 'signoff!' => \$chk_signoff, | 33 | 'signoff!' => \$chk_signoff, |
33 | 'patch!' => \$chk_patch, | 34 | 'patch!' => \$chk_patch, |
34 | 'test-type!' => \$tst_type, | ||
35 | 'emacs!' => \$emacs, | 35 | 'emacs!' => \$emacs, |
36 | 'terse!' => \$terse, | 36 | 'terse!' => \$terse, |
37 | 'file!' => \$file, | 37 | 'file!' => \$file, |
@@ -40,7 +40,10 @@ GetOptions( | |||
40 | 'root=s' => \$root, | 40 | 'root=s' => \$root, |
41 | 'summary!' => \$summary, | 41 | 'summary!' => \$summary, |
42 | 'mailback!' => \$mailback, | 42 | 'mailback!' => \$mailback, |
43 | 'summary-file!' => \$summary_file, | ||
44 | |||
43 | 'debug=s' => \%debug, | 45 | 'debug=s' => \%debug, |
46 | 'test-type!' => \$tst_type, | ||
44 | ) or exit; | 47 | ) or exit; |
45 | 48 | ||
46 | my $exit = 0; | 49 | my $exit = 0; |
@@ -48,13 +51,15 @@ my $exit = 0; | |||
48 | if ($#ARGV < 0) { | 51 | if ($#ARGV < 0) { |
49 | print "usage: $P [options] patchfile\n"; | 52 | print "usage: $P [options] patchfile\n"; |
50 | print "version: $V\n"; | 53 | print "version: $V\n"; |
51 | print "options: -q => quiet\n"; | 54 | print "options: -q => quiet\n"; |
52 | print " --no-tree => run without a kernel tree\n"; | 55 | print " --no-tree => run without a kernel tree\n"; |
53 | print " --terse => one line per report\n"; | 56 | print " --terse => one line per report\n"; |
54 | print " --emacs => emacs compile window format\n"; | 57 | print " --emacs => emacs compile window format\n"; |
55 | print " --file => check a source file\n"; | 58 | print " --file => check a source file\n"; |
56 | print " --strict => enable more subjective tests\n"; | 59 | print " --strict => enable more subjective tests\n"; |
57 | 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"; | ||
58 | exit(1); | 63 | exit(1); |
59 | } | 64 | } |
60 | 65 | ||
@@ -213,6 +218,7 @@ for my $filename (@ARGV) { | |||
213 | $exit = 1; | 218 | $exit = 1; |
214 | } | 219 | } |
215 | @rawlines = (); | 220 | @rawlines = (); |
221 | @lines = (); | ||
216 | } | 222 | } |
217 | 223 | ||
218 | exit($exit); | 224 | exit($exit); |
@@ -321,14 +327,14 @@ sub sanitise_line { | |||
321 | } | 327 | } |
322 | 328 | ||
323 | # Clear out the comments. | 329 | # Clear out the comments. |
324 | while ($res =~ m@(/\*.*?\*/)@) { | 330 | while ($res =~ m@(/\*.*?\*/)@g) { |
325 | substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); | 331 | substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]); |
326 | } | 332 | } |
327 | if ($res =~ m@(/\*.*)@) { | 333 | if ($res =~ m@(/\*.*)@) { |
328 | substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); | 334 | substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]); |
329 | } | 335 | } |
330 | if ($res =~ m@^.(.*\*/)@) { | 336 | if ($res =~ m@^.(.*\*/)@) { |
331 | substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); | 337 | substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]); |
332 | } | 338 | } |
333 | 339 | ||
334 | # The pathname on a #include may be surrounded by '<' and '>'. | 340 | # The pathname on a #include may be surrounded by '<' and '>'. |
@@ -352,10 +358,14 @@ sub ctx_statement_block { | |||
352 | my $soff = $off; | 358 | my $soff = $off; |
353 | my $coff = $off - 1; | 359 | my $coff = $off - 1; |
354 | 360 | ||
361 | my $loff = 0; | ||
362 | |||
355 | my $type = ''; | 363 | my $type = ''; |
356 | my $level = 0; | 364 | my $level = 0; |
357 | my $c; | 365 | my $c; |
358 | my $len = 0; | 366 | my $len = 0; |
367 | |||
368 | my $remainder; | ||
359 | while (1) { | 369 | while (1) { |
360 | #warn "CSB: blk<$blk>\n"; | 370 | #warn "CSB: blk<$blk>\n"; |
361 | # 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 |
@@ -364,6 +374,7 @@ sub ctx_statement_block { | |||
364 | for (; $remain > 0; $line++) { | 374 | for (; $remain > 0; $line++) { |
365 | next if ($lines[$line] =~ /^-/); | 375 | next if ($lines[$line] =~ /^-/); |
366 | $remain--; | 376 | $remain--; |
377 | $loff = $len; | ||
367 | $blk .= $lines[$line] . "\n"; | 378 | $blk .= $lines[$line] . "\n"; |
368 | $len = length($blk); | 379 | $len = length($blk); |
369 | $line++; | 380 | $line++; |
@@ -371,11 +382,12 @@ sub ctx_statement_block { | |||
371 | } | 382 | } |
372 | # Bail if there is no further context. | 383 | # Bail if there is no further context. |
373 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; | 384 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
374 | if ($off == $len) { | 385 | if ($off >= $len) { |
375 | last; | 386 | last; |
376 | } | 387 | } |
377 | } | 388 | } |
378 | $c = substr($blk, $off, 1); | 389 | $c = substr($blk, $off, 1); |
390 | $remainder = substr($blk, $off); | ||
379 | 391 | ||
380 | #warn "CSB: c<$c> type<$type> level<$level>\n"; | 392 | #warn "CSB: c<$c> type<$type> level<$level>\n"; |
381 | # Statement ends at the ';' or a close '}' at the | 393 | # Statement ends at the ';' or a close '}' at the |
@@ -384,6 +396,12 @@ sub ctx_statement_block { | |||
384 | last; | 396 | last; |
385 | } | 397 | } |
386 | 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 | |||
387 | if (($type eq '' || $type eq '(') && $c eq '(') { | 405 | if (($type eq '' || $type eq '(') && $c eq '(') { |
388 | $level++; | 406 | $level++; |
389 | $type = '('; | 407 | $type = '('; |
@@ -410,6 +428,10 @@ sub ctx_statement_block { | |||
410 | } | 428 | } |
411 | $off++; | 429 | $off++; |
412 | } | 430 | } |
431 | if ($off == $len) { | ||
432 | $line++; | ||
433 | $remain--; | ||
434 | } | ||
413 | 435 | ||
414 | my $statement = substr($blk, $soff, $off - $soff + 1); | 436 | my $statement = substr($blk, $soff, $off - $soff + 1); |
415 | my $condition = substr($blk, $soff, $coff - $soff + 1); | 437 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
@@ -417,7 +439,30 @@ sub ctx_statement_block { | |||
417 | #warn "STATEMENT<$statement>\n"; | 439 | #warn "STATEMENT<$statement>\n"; |
418 | #warn "CONDITION<$condition>\n"; | 440 | #warn "CONDITION<$condition>\n"; |
419 | 441 | ||
420 | 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); | ||
421 | } | 466 | } |
422 | 467 | ||
423 | sub ctx_block_get { | 468 | sub ctx_block_get { |
@@ -598,7 +643,7 @@ sub annotate_values { | |||
598 | } | 643 | } |
599 | $type = 'N'; | 644 | $type = 'N'; |
600 | 645 | ||
601 | } elsif ($cur =~ /^(if|while|typeof|for)\b/o) { | 646 | } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) { |
602 | print "COND($1)\n" if ($dbg_values > 1); | 647 | print "COND($1)\n" if ($dbg_values > 1); |
603 | $av_paren_type[$av_paren] = 'N'; | 648 | $av_paren_type[$av_paren] = 'N'; |
604 | $type = 'N'; | 649 | $type = 'N'; |
@@ -635,8 +680,12 @@ sub annotate_values { | |||
635 | print "ASSIGN($1)\n" if ($dbg_values > 1); | 680 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
636 | $type = 'N'; | 681 | $type = 'N'; |
637 | 682 | ||
638 | } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { | 683 | } elsif ($cur =~/^(;)/) { |
639 | print "END($1)\n" if ($dbg_values > 1); | 684 | print "END($1)\n" if ($dbg_values > 1); |
685 | $type = 'E'; | ||
686 | |||
687 | } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { | ||
688 | print "CLOSE($1)\n" if ($dbg_values > 1); | ||
640 | $type = 'N'; | 689 | $type = 'N'; |
641 | 690 | ||
642 | } elsif ($cur =~ /^($Operators)/o) { | 691 | } elsif ($cur =~ /^($Operators)/o) { |
@@ -658,7 +707,7 @@ sub annotate_values { | |||
658 | } | 707 | } |
659 | 708 | ||
660 | sub possible { | 709 | sub possible { |
661 | my ($possible) = @_; | 710 | my ($possible, $line) = @_; |
662 | 711 | ||
663 | #print "CHECK<$possible>\n"; | 712 | #print "CHECK<$possible>\n"; |
664 | if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && | 713 | if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && |
@@ -666,7 +715,7 @@ sub possible { | |||
666 | $possible ne 'struct' && $possible ne 'enum' && | 715 | $possible ne 'struct' && $possible ne 'enum' && |
667 | $possible ne 'case' && $possible ne 'else' && | 716 | $possible ne 'case' && $possible ne 'else' && |
668 | $possible ne 'typedef') { | 717 | $possible ne 'typedef') { |
669 | warn "POSSIBLE: $possible\n" if ($dbg_possible); | 718 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
670 | push(@typeList, $possible); | 719 | push(@typeList, $possible); |
671 | build_types(); | 720 | build_types(); |
672 | } | 721 | } |
@@ -674,16 +723,15 @@ sub possible { | |||
674 | 723 | ||
675 | my $prefix = ''; | 724 | my $prefix = ''; |
676 | 725 | ||
677 | my @report = (); | ||
678 | sub report { | 726 | sub report { |
679 | my $line = $prefix . $_[0]; | 727 | my $line = $prefix . $_[0]; |
680 | 728 | ||
681 | $line = (split('\n', $line))[0] . "\n" if ($terse); | 729 | $line = (split('\n', $line))[0] . "\n" if ($terse); |
682 | 730 | ||
683 | push(@report, $line); | 731 | push(our @report, $line); |
684 | } | 732 | } |
685 | sub report_dump { | 733 | sub report_dump { |
686 | @report; | 734 | our @report; |
687 | } | 735 | } |
688 | sub ERROR { | 736 | sub ERROR { |
689 | report("ERROR: $_[0]\n"); | 737 | report("ERROR: $_[0]\n"); |
@@ -721,6 +769,7 @@ sub process { | |||
721 | my $signoff = 0; | 769 | my $signoff = 0; |
722 | my $is_patch = 0; | 770 | my $is_patch = 0; |
723 | 771 | ||
772 | our @report = (); | ||
724 | our $cnt_lines = 0; | 773 | our $cnt_lines = 0; |
725 | our $cnt_error = 0; | 774 | our $cnt_error = 0; |
726 | our $cnt_warn = 0; | 775 | our $cnt_warn = 0; |
@@ -735,7 +784,10 @@ sub process { | |||
735 | my $comment_edge = 0; | 784 | my $comment_edge = 0; |
736 | my $first_line = 0; | 785 | my $first_line = 0; |
737 | 786 | ||
738 | my $prev_values = 'N'; | 787 | my $prev_values = 'E'; |
788 | |||
789 | # suppression flags | ||
790 | my $suppress_ifbraces = 0; | ||
739 | 791 | ||
740 | # Pre-scan the patch sanitizing the lines. | 792 | # Pre-scan the patch sanitizing the lines. |
741 | # Pre-scan the patch looking for any __setup documentation. | 793 | # Pre-scan the patch looking for any __setup documentation. |
@@ -791,7 +843,9 @@ sub process { | |||
791 | $realcnt=1+1; | 843 | $realcnt=1+1; |
792 | } | 844 | } |
793 | annotate_reset(); | 845 | annotate_reset(); |
794 | $prev_values = 'N'; | 846 | $prev_values = 'E'; |
847 | |||
848 | $suppress_ifbraces = $linenr - 1; | ||
795 | next; | 849 | next; |
796 | } | 850 | } |
797 | 851 | ||
@@ -953,22 +1007,22 @@ sub process { | |||
953 | 1007 | ||
954 | # definitions in global scope can only start with types | 1008 | # definitions in global scope can only start with types |
955 | } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) { | 1009 | } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) { |
956 | possible($1); | 1010 | possible($1, $line); |
957 | 1011 | ||
958 | # declarations always start with types | 1012 | # declarations always start with types |
959 | } elsif ($prev_values eq 'N' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=)/) { | 1013 | } elsif ($prev_values eq 'E' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) { |
960 | possible($1); | 1014 | possible($1); |
961 | } | 1015 | } |
962 | 1016 | ||
963 | # any (foo ... *) is a pointer cast, and foo is a type | 1017 | # any (foo ... *) is a pointer cast, and foo is a type |
964 | while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) { | 1018 | while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) { |
965 | possible($1); | 1019 | possible($1, $line); |
966 | } | 1020 | } |
967 | 1021 | ||
968 | # Check for any sort of function declaration. | 1022 | # Check for any sort of function declaration. |
969 | # int foo(something bar, other baz); | 1023 | # int foo(something bar, other baz); |
970 | # void (*store_gdt)(x86_descr_ptr *); | 1024 | # void (*store_gdt)(x86_descr_ptr *); |
971 | if ($prev_values eq 'N' && $line =~ /^(.(?:typedef\s*)?(?:(?:$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*)\(/) { |
972 | my ($name_len) = length($1); | 1026 | my ($name_len) = length($1); |
973 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len); | 1027 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len); |
974 | my $ctx = join("\n", @ctx); | 1028 | my $ctx = join("\n", @ctx); |
@@ -979,7 +1033,7 @@ sub process { | |||
979 | for my $arg (split(/\s*,\s*/, $ctx)) { | 1033 | for my $arg (split(/\s*,\s*/, $ctx)) { |
980 | 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)$/) { |
981 | 1035 | ||
982 | possible($1); | 1036 | possible($1, $line); |
983 | } | 1037 | } |
984 | } | 1038 | } |
985 | } | 1039 | } |
@@ -1189,7 +1243,7 @@ sub process { | |||
1189 | my $ctx = substr($line, 0, $-[1]); | 1243 | my $ctx = substr($line, 0, $-[1]); |
1190 | 1244 | ||
1191 | # Ignore those directives where spaces _are_ permitted. | 1245 | # Ignore those directives where spaces _are_ permitted. |
1192 | if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/) { | 1246 | if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) { |
1193 | 1247 | ||
1194 | # cpp #define statements have non-optional spaces, ie | 1248 | # cpp #define statements have non-optional spaces, ie |
1195 | # if there is a space between the name and the open | 1249 | # if there is a space between the name and the open |
@@ -1212,7 +1266,7 @@ sub process { | |||
1212 | =>|->|<<|>>|<|>|=|!|~| | 1266 | =>|->|<<|>>|<|>|=|!|~| |
1213 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% | 1267 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
1214 | }x; | 1268 | }x; |
1215 | my @elements = split(/($ops|;)/, $opline); | 1269 | my @elements = split(/($;+|$ops|;)/, $opline); |
1216 | my $off = 0; | 1270 | my $off = 0; |
1217 | 1271 | ||
1218 | my $blank = copy_spacing($opline); | 1272 | my $blank = copy_spacing($opline); |
@@ -1272,8 +1326,15 @@ sub process { | |||
1272 | # 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"; |
1273 | #} | 1327 | #} |
1274 | 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 | |||
1275 | # ; 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 |
1276 | if ($op eq ';') { | 1337 | } elsif ($op eq ';') { |
1277 | if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && | 1338 | if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && |
1278 | $cc !~ /^;/) { | 1339 | $cc !~ /^;/) { |
1279 | ERROR("need space after that '$op' $at\n" . $hereptr); | 1340 | ERROR("need space after that '$op' $at\n" . $hereptr); |
@@ -1315,7 +1376,7 @@ sub process { | |||
1315 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { | 1376 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { |
1316 | ERROR("need space one side of that '$op' $at\n" . $hereptr); | 1377 | ERROR("need space one side of that '$op' $at\n" . $hereptr); |
1317 | } | 1378 | } |
1318 | if ($ctx =~ /Wx./ && $cc =~ /^;/) { | 1379 | if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
1319 | ERROR("no space before that '$op' $at\n" . $hereptr); | 1380 | ERROR("no space before that '$op' $at\n" . $hereptr); |
1320 | } | 1381 | } |
1321 | 1382 | ||
@@ -1388,7 +1449,7 @@ sub process { | |||
1388 | $line !~ /for\s*\(\s+;/) { | 1449 | $line !~ /for\s*\(\s+;/) { |
1389 | ERROR("no space after that open parenthesis '('\n" . $herecurr); | 1450 | ERROR("no space after that open parenthesis '('\n" . $herecurr); |
1390 | } | 1451 | } |
1391 | if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ && | 1452 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
1392 | $line !~ /for\s*\(.*;\s+\)/) { | 1453 | $line !~ /for\s*\(.*;\s+\)/) { |
1393 | ERROR("no space before that close parenthesis ')'\n" . $herecurr); | 1454 | ERROR("no space before that close parenthesis ')'\n" . $herecurr); |
1394 | } | 1455 | } |
@@ -1416,16 +1477,34 @@ sub process { | |||
1416 | # conditional. | 1477 | # conditional. |
1417 | substr($s, 0, length($c)) = ''; | 1478 | substr($s, 0, length($c)) = ''; |
1418 | $s =~ s/\n.*//g; | 1479 | $s =~ s/\n.*//g; |
1419 | 1480 | $s =~ s/$;//g; # Remove any comments | |
1420 | if (length($c) && $s !~ /^\s*({|;|\/\*.*\*\/)?\s*\\*\s*$/) { | 1481 | if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) { |
1421 | ERROR("trailing statements should be on next line\n" . $herecurr); | 1482 | ERROR("trailing statements should be on next line\n" . $herecurr); |
1422 | } | 1483 | } |
1423 | } | 1484 | } |
1424 | 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 | |||
1425 | # if and else should not have general statements after it | 1501 | # if and else should not have general statements after it |
1426 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ && | 1502 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
1427 | $1 !~ /^\s*(?:\sif|{|\\|$)/) { | 1503 | my $s = $1; |
1428 | 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 | } | ||
1429 | } | 1508 | } |
1430 | 1509 | ||
1431 | # Check for }<nl>else {, these must be at the same | 1510 | # Check for }<nl>else {, these must be at the same |
@@ -1518,7 +1597,48 @@ sub process { | |||
1518 | } | 1597 | } |
1519 | 1598 | ||
1520 | # check for redundant bracing round if etc | 1599 | # check for redundant bracing round if etc |
1521 | 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/) { | ||
1522 | # Locate the end of the opening statement. | 1642 | # Locate the end of the opening statement. |
1523 | my @control = ctx_statement($linenr, $realcnt, 0); | 1643 | my @control = ctx_statement($linenr, $realcnt, 0); |
1524 | my $nr = $linenr + (scalar(@control) - 1); | 1644 | my $nr = $linenr + (scalar(@control) - 1); |
@@ -1541,7 +1661,7 @@ sub process { | |||
1541 | my $after = $1; | 1661 | my $after = $1; |
1542 | 1662 | ||
1543 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; | 1663 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; |
1544 | #print "stmt<$stmt>\n\n"; | 1664 | #print "before<$before> stmt<$stmt> after<$after>\n\n"; |
1545 | 1665 | ||
1546 | # Count the newlines, if there is only one | 1666 | # Count the newlines, if there is only one |
1547 | # then the block should not have {}'s. | 1667 | # then the block should not have {}'s. |
@@ -1659,6 +1779,17 @@ sub process { | |||
1659 | if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { | 1779 | if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { |
1660 | 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); |
1661 | } | 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); | ||
1662 | } | 1793 | } |
1663 | 1794 | ||
1664 | # In mailback mode only produce a report in the negative, for | 1795 | # In mailback mode only produce a report in the negative, for |
@@ -1681,7 +1812,8 @@ sub process { | |||
1681 | } | 1812 | } |
1682 | 1813 | ||
1683 | print report_dump(); | 1814 | print report_dump(); |
1684 | if ($summary) { | 1815 | if ($summary && !($clean == 1 && $quiet == 1)) { |
1816 | print "$filename " if ($summary_file); | ||
1685 | print "total: $cnt_error errors, $cnt_warn warnings, " . | 1817 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
1686 | (($check)? "$cnt_chk checks, " : "") . | 1818 | (($check)? "$cnt_chk checks, " : "") . |
1687 | "$cnt_lines lines checked\n"; | 1819 | "$cnt_lines lines checked\n"; |
@@ -1696,5 +1828,15 @@ sub process { | |||
1696 | print "are false positives report them to the maintainer, see\n"; | 1828 | print "are false positives report them to the maintainer, see\n"; |
1697 | print "CHECKPATCH in MAINTAINERS.\n"; | 1829 | print "CHECKPATCH in MAINTAINERS.\n"; |
1698 | } | 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 | |||
1699 | return $clean; | 1841 | return $clean; |
1700 | } | 1842 | } |