diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/checkpatch.pl | 145 |
1 files changed, 92 insertions, 53 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 56c364c1df81..277c32647f36 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.05'; | 12 | my $V = '0.06'; |
13 | 13 | ||
14 | use Getopt::Long qw(:config no_auto_abbrev); | 14 | use Getopt::Long qw(:config no_auto_abbrev); |
15 | 15 | ||
@@ -271,24 +271,38 @@ sub process { | |||
271 | my $in_comment = 0; | 271 | my $in_comment = 0; |
272 | my $first_line = 0; | 272 | my $first_line = 0; |
273 | 273 | ||
274 | my $ident = '[A-Za-z\d_]+'; | 274 | my $Ident = qr{[A-Za-z\d_]+}; |
275 | my $storage = '(?:extern|static)'; | 275 | my $Storage = qr{extern|static}; |
276 | my $sparse = '(?:__user|__kernel|__force|__iomem)'; | 276 | my $Sparse = qr{__user|__kernel|__force|__iomem}; |
277 | my $type = '(?:unsigned\s+)?' . | 277 | my $NonptrType = qr{ |
278 | '(?:void|char|short|int|long|unsigned|float|double|' . | 278 | \b |
279 | 'long\s+long|' . | 279 | (?:const\s+)? |
280 | "struct\\s+${ident}|" . | 280 | (?:unsigned\s+)? |
281 | "union\\s+${ident}|" . | 281 | (?: |
282 | "${ident}_t)" . | 282 | void| |
283 | "(?:\\s+$sparse)*" . | 283 | char| |
284 | '(?:\s*\*+)?'; | 284 | short| |
285 | my $attribute = '(?:__read_mostly|__init|__initdata)'; | 285 | int| |
286 | 286 | long| | |
287 | my $Ident = $ident; | 287 | unsigned| |
288 | my $Type = $type; | 288 | float| |
289 | my $Storage = $storage; | 289 | double| |
290 | my $Declare = "(?:$storage\\s+)?$type"; | 290 | long\s+int| |
291 | my $Attribute = $attribute; | 291 | long\s+long| |
292 | long\s+long\s+int| | ||
293 | struct\s+$Ident| | ||
294 | union\s+$Ident| | ||
295 | ${Ident}_t | ||
296 | ) | ||
297 | (?:\s+$Sparse)* | ||
298 | \b | ||
299 | }x; | ||
300 | my $Type = qr{ | ||
301 | \b$NonptrType\b | ||
302 | (?:\s*\*+\s*const|\s*\*+)? | ||
303 | }x; | ||
304 | my $Declare = qr{(?:$Storage\s+)?$Type}; | ||
305 | my $Attribute = qr{__read_mostly|__init|__initdata}; | ||
292 | 306 | ||
293 | foreach my $line (@lines) { | 307 | foreach my $line (@lines) { |
294 | $linenr++; | 308 | $linenr++; |
@@ -321,6 +335,7 @@ sub process { | |||
321 | # blank context lines so we need to count that too. | 335 | # blank context lines so we need to count that too. |
322 | if ($line =~ /^( |\+|$)/) { | 336 | if ($line =~ /^( |\+|$)/) { |
323 | $realline++; | 337 | $realline++; |
338 | $realcnt-- if ($realcnt != 0); | ||
324 | 339 | ||
325 | # track any sort of multi-line comment. Obviously if | 340 | # track any sort of multi-line comment. Obviously if |
326 | # the added text or context do not include the whole | 341 | # the added text or context do not include the whole |
@@ -345,8 +360,9 @@ sub process { | |||
345 | # Track the previous line. | 360 | # Track the previous line. |
346 | ($prevline, $stashline) = ($stashline, $line); | 361 | ($prevline, $stashline) = ($stashline, $line); |
347 | ($previndent, $stashindent) = ($stashindent, $indent); | 362 | ($previndent, $stashindent) = ($stashindent, $indent); |
363 | } elsif ($realcnt == 1) { | ||
364 | $realcnt--; | ||
348 | } | 365 | } |
349 | $realcnt-- if ($realcnt != 0); | ||
350 | 366 | ||
351 | #make up the handle for any error we report on this line | 367 | #make up the handle for any error we report on this line |
352 | $here = "#$linenr: "; | 368 | $here = "#$linenr: "; |
@@ -357,14 +373,11 @@ sub process { | |||
357 | my $hereprev = "$here\n$prevline\n$line\n\n"; | 373 | my $hereprev = "$here\n$prevline\n$line\n\n"; |
358 | 374 | ||
359 | #check the patch for a signoff: | 375 | #check the patch for a signoff: |
360 | if ($line =~ /^\s*Signed-off-by:\s/) { | 376 | if ($line =~ /^\s*signed-off-by:/i) { |
361 | $signoff++; | ||
362 | |||
363 | } elsif ($line =~ /^\s*signed-off-by:/i) { | ||
364 | # This is a signoff, if ugly, so do not double report. | 377 | # This is a signoff, if ugly, so do not double report. |
365 | $signoff++; | 378 | $signoff++; |
366 | if (!($line =~ /^\s*Signed-off-by:/)) { | 379 | if (!($line =~ /^\s*Signed-off-by:/)) { |
367 | print "use Signed-off-by:\n"; | 380 | print "Signed-off-by: is the preferred form\n"; |
368 | print "$herecurr"; | 381 | print "$herecurr"; |
369 | $clean = 0; | 382 | $clean = 0; |
370 | } | 383 | } |
@@ -389,7 +402,7 @@ sub process { | |||
389 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); | 402 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
390 | 403 | ||
391 | #trailing whitespace | 404 | #trailing whitespace |
392 | if ($line=~/^\+.*\S\s+$/) { | 405 | if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) { |
393 | my $herevet = "$here\n" . cat_vet($line) . "\n\n"; | 406 | my $herevet = "$here\n" . cat_vet($line) . "\n\n"; |
394 | print "trailing whitespace\n"; | 407 | print "trailing whitespace\n"; |
395 | print "$herevet"; | 408 | print "$herevet"; |
@@ -525,24 +538,23 @@ sub process { | |||
525 | } | 538 | } |
526 | 539 | ||
527 | # * goes on variable not on type | 540 | # * goes on variable not on type |
528 | if ($line =~ m{[A-Za-z\d_]+(\*+) [A-Za-z\d_]+}) { | 541 | if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) { |
529 | print "\"foo$1 bar\" should be \"foo $1bar\"\n"; | 542 | print "\"(foo$1)\" should be \"(foo $1)\"\n"; |
530 | print "$herecurr"; | 543 | print "$herecurr"; |
531 | $clean = 0; | 544 | $clean = 0; |
532 | } | 545 | |
533 | if ($line =~ m{$Type (\*) [A-Za-z\d_]+} || | 546 | } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) { |
534 | $line =~ m{[A-Za-z\d_]+ (\*\*+) [A-Za-z\d_]+}) { | 547 | print "\"(foo $1 )\" should be \"(foo $1)\"\n"; |
535 | print "\"foo $1 bar\" should be \"foo $1bar\"\n"; | ||
536 | print "$herecurr"; | 548 | print "$herecurr"; |
537 | $clean = 0; | 549 | $clean = 0; |
538 | } | 550 | |
539 | if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_](\*+)\)}) { | 551 | } elsif ($line =~ m{$NonptrType(\*+)(?:\s+const)?\s+[A-Za-z\d_]+}) { |
540 | print "\"(foo$1)\" should be \"(foo $1)\"\n"; | 552 | print "\"foo$1 bar\" should be \"foo $1bar\"\n"; |
541 | print "$herecurr"; | 553 | print "$herecurr"; |
542 | $clean = 0; | 554 | $clean = 0; |
543 | } | 555 | |
544 | if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_]\s+(\*+)\s+\)}) { | 556 | } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+const)\s+[A-Za-z\d_]+}) { |
545 | print "\"(foo $1 )\" should be \"(foo $1)\"\n"; | 557 | print "\"foo $1 bar\" should be \"foo $1bar\"\n"; |
546 | print "$herecurr"; | 558 | print "$herecurr"; |
547 | $clean = 0; | 559 | $clean = 0; |
548 | } | 560 | } |
@@ -581,7 +593,7 @@ sub process { | |||
581 | 593 | ||
582 | # function brace can't be on same line, except for #defines of do while, | 594 | # function brace can't be on same line, except for #defines of do while, |
583 | # or if closed on same line | 595 | # or if closed on same line |
584 | if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and | 596 | if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and |
585 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { | 597 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { |
586 | print "braces following function declarations go on the next line\n"; | 598 | print "braces following function declarations go on the next line\n"; |
587 | print "$herecurr"; | 599 | print "$herecurr"; |
@@ -624,7 +636,7 @@ sub process { | |||
624 | my $ca = substr($opline, $off - 1, 1); | 636 | my $ca = substr($opline, $off - 1, 1); |
625 | my $cc = ''; | 637 | my $cc = ''; |
626 | if (length($opline) >= ($off + length($elements[$n + 1]))) { | 638 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
627 | $cc = substr($opline, $off + length($elements[$n + 1]), 1); | 639 | $cc = substr($opline, $off + length($elements[$n + 1])); |
628 | } | 640 | } |
629 | 641 | ||
630 | my $ctx = "${a}x${c}"; | 642 | my $ctx = "${a}x${c}"; |
@@ -636,8 +648,16 @@ sub process { | |||
636 | 648 | ||
637 | ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n"; | 649 | ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n"; |
638 | 650 | ||
639 | # We need ; as an operator. // is a comment. | 651 | # ; should have either the end of line or a space or \ after it |
640 | if ($op eq ';' or $op eq '//') { | 652 | if ($op eq ';') { |
653 | if ($ctx !~ /.x[WE]/ && $cc !~ /^\\/) { | ||
654 | print "need space after that '$op' $at\n"; | ||
655 | print "$hereptr"; | ||
656 | $clean = 0; | ||
657 | } | ||
658 | |||
659 | # // is a comment | ||
660 | } elsif ($op eq '//') { | ||
641 | 661 | ||
642 | # -> should have no spaces | 662 | # -> should have no spaces |
643 | } elsif ($op eq '->') { | 663 | } elsif ($op eq '->') { |
@@ -649,7 +669,7 @@ sub process { | |||
649 | 669 | ||
650 | # , must have a space on the right. | 670 | # , must have a space on the right. |
651 | } elsif ($op eq ',') { | 671 | } elsif ($op eq ',') { |
652 | if ($ctx !~ /.xW|.xE/ && $cc ne '}') { | 672 | if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) { |
653 | print "need space after that '$op' $at\n"; | 673 | print "need space after that '$op' $at\n"; |
654 | print "$hereptr"; | 674 | print "$hereptr"; |
655 | $clean = 0; | 675 | $clean = 0; |
@@ -670,12 +690,12 @@ sub process { | |||
670 | 690 | ||
671 | # unary ++ and unary -- are allowed no space on one side. | 691 | # unary ++ and unary -- are allowed no space on one side. |
672 | } elsif ($op eq '++' or $op eq '--') { | 692 | } elsif ($op eq '++' or $op eq '--') { |
673 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOB]/) { | 693 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { |
674 | print "need space one side of that '$op' $at\n"; | 694 | print "need space one side of that '$op' $at\n"; |
675 | print "$hereptr"; | 695 | print "$hereptr"; |
676 | $clean = 0; | 696 | $clean = 0; |
677 | } | 697 | } |
678 | if ($ctx =~ /Wx./ && $cc eq ';') { | 698 | if ($ctx =~ /Wx./ && $cc =~ /^;/) { |
679 | print "no space before that '$op' $at\n"; | 699 | print "no space before that '$op' $at\n"; |
680 | print "$hereptr"; | 700 | print "$hereptr"; |
681 | $clean = 0; | 701 | $clean = 0; |
@@ -707,7 +727,7 @@ sub process { | |||
707 | # | 727 | # |
708 | } elsif ($op eq '*') { | 728 | } elsif ($op eq '*') { |
709 | if ($ca eq '*') { | 729 | if ($ca eq '*') { |
710 | if ($cc =~ /\s/) { | 730 | if ($cc =~ /^\s(?!\s*const)/) { |
711 | print "no space after that '$op' $at\n"; | 731 | print "no space after that '$op' $at\n"; |
712 | print "$hereptr"; | 732 | print "$hereptr"; |
713 | $clean = 0; | 733 | $clean = 0; |
@@ -803,7 +823,7 @@ sub process { | |||
803 | 823 | ||
804 | # if/while/etc brace do not go on next line, unless defining a do while loop, | 824 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
805 | # or if that brace on the next line is for something else | 825 | # or if that brace on the next line is for something else |
806 | if ($prevline=~/\b(if|while|for|switch)\s*\(/) { | 826 | if ($prevline=~/\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/) { |
807 | my @opened = $prevline=~/\(/g; | 827 | my @opened = $prevline=~/\(/g; |
808 | my @closed = $prevline=~/\)/g; | 828 | my @closed = $prevline=~/\)/g; |
809 | my $nr_line = $linenr; | 829 | my $nr_line = $linenr; |
@@ -823,14 +843,22 @@ sub process { | |||
823 | @closed = $prevline=~/\)/g; | 843 | @closed = $prevline=~/\)/g; |
824 | } | 844 | } |
825 | 845 | ||
826 | if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and | 846 | if (($prevline=~/\b(?:(if|while|for|switch)\s*\(.*\)|do|else)\s*$/) and ($next_line=~/{/) and |
827 | !($next_line=~/\b(if|while|for|switch)/) and !($next_line=~/\#define.*do.*while/)) { | 847 | !($next_line=~/\b(?:if|while|for|switch|do|else)\b/) and !($next_line=~/\#define.*do.*while/)) { |
828 | print "That { should be on the previous line\n"; | 848 | print "That { should be on the previous line\n"; |
829 | print "$here\n$display_segment\n$next_line\n\n"; | 849 | print "$here\n$display_segment\n$next_line\n\n"; |
830 | $clean = 0; | 850 | $clean = 0; |
831 | } | 851 | } |
832 | } | 852 | } |
833 | 853 | ||
854 | # if and else should not have general statements after it | ||
855 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ && | ||
856 | $1 !~ /^\s*(?:\sif|{|$)/) { | ||
857 | print "trailing statements should be on next line\n"; | ||
858 | print "$herecurr"; | ||
859 | $clean = 0; | ||
860 | } | ||
861 | |||
834 | # multi-statement macros should be enclosed in a do while loop, grab the | 862 | # multi-statement macros should be enclosed in a do while loop, grab the |
835 | # first statement and ensure its the whole macro if its not enclosed | 863 | # first statement and ensure its the whole macro if its not enclosed |
836 | # in a known goot container | 864 | # in a known goot container |
@@ -841,11 +869,22 @@ sub process { | |||
841 | # Grab the first statement, if that is the entire macro | 869 | # Grab the first statement, if that is the entire macro |
842 | # its ok. This may start either on the #define line | 870 | # its ok. This may start either on the #define line |
843 | # or the one below. | 871 | # or the one below. |
844 | my $ctx1 = join('', ctx_statement($linenr - 1, $realcnt + 1)); | 872 | my $ln = $linenr; |
845 | my $ctx2 = join('', ctx_statement($linenr, $realcnt)); | 873 | my $cnt = $realcnt; |
846 | 874 | ||
847 | if ($ctx1 =~ /\\$/ && $ctx2 =~ /\\$/) { | 875 | # If the macro starts on the define line start there. |
848 | print "Macros with multiple statements should be enclosed in a do - while loop\n"; | 876 | if ($prevline !~ m{^.#\s*define\s*$Ident(?:\([^\)]*\))?\s*\\\s*$}) { |
877 | $ln--; | ||
878 | $cnt++; | ||
879 | } | ||
880 | my $ctx = join('', ctx_statement($ln, $cnt)); | ||
881 | |||
882 | if ($ctx =~ /\\$/) { | ||
883 | if ($ctx =~ /;/) { | ||
884 | print "Macros with multiple statements should be enclosed in a do - while loop\n"; | ||
885 | } else { | ||
886 | print "Macros with complex values should be enclosed in parenthesis\n"; | ||
887 | } | ||
849 | print "$hereprev"; | 888 | print "$hereprev"; |
850 | $clean = 0; | 889 | $clean = 0; |
851 | } | 890 | } |