diff options
Diffstat (limited to 'scripts/checkpatch.pl')
| -rwxr-xr-x | scripts/checkpatch.pl | 249 |
1 files changed, 200 insertions, 49 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 25e20a27fc59..dae7d30dca0f 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.07'; | 12 | my $V = '0.09'; |
| 13 | 13 | ||
| 14 | use Getopt::Long qw(:config no_auto_abbrev); | 14 | use Getopt::Long qw(:config no_auto_abbrev); |
| 15 | 15 | ||
| @@ -47,16 +47,14 @@ my $removal = 'Documentation/feature-removal-schedule.txt'; | |||
| 47 | if ($tree && -f $removal) { | 47 | if ($tree && -f $removal) { |
| 48 | open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n"; | 48 | open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n"; |
| 49 | while (<REMOVE>) { | 49 | while (<REMOVE>) { |
| 50 | if (/^Files:\s+(.*\S)/) { | 50 | if (/^Check:\s+(.*\S)/) { |
| 51 | for my $file (split(/[, ]+/, $1)) { | 51 | for my $entry (split(/[, ]+/, $1)) { |
| 52 | if ($file =~ m@include/(.*)@) { | 52 | if ($entry =~ m@include/(.*)@) { |
| 53 | push(@dep_includes, $1); | 53 | push(@dep_includes, $1); |
| 54 | } | ||
| 55 | } | ||
| 56 | 54 | ||
| 57 | } elsif (/^Funcs:\s+(.*\S)/) { | 55 | } elsif ($entry !~ m@/@) { |
| 58 | for my $func (split(/[, ]+/, $1)) { | 56 | push(@dep_functions, $entry); |
| 59 | push(@dep_functions, $func); | 57 | } |
| 60 | } | 58 | } |
| 61 | } | 59 | } |
| 62 | } | 60 | } |
| @@ -153,7 +151,7 @@ sub sanitise_line { | |||
| 153 | } | 151 | } |
| 154 | 152 | ||
| 155 | sub ctx_block_get { | 153 | sub ctx_block_get { |
| 156 | my ($linenr, $remain, $outer, $open, $close) = @_; | 154 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
| 157 | my $line; | 155 | my $line; |
| 158 | my $start = $linenr - 1; | 156 | my $start = $linenr - 1; |
| 159 | my $blk = ''; | 157 | my $blk = ''; |
| @@ -161,38 +159,58 @@ sub ctx_block_get { | |||
| 161 | my @c; | 159 | my @c; |
| 162 | my @res = (); | 160 | my @res = (); |
| 163 | 161 | ||
| 162 | my $level = 0; | ||
| 164 | for ($line = $start; $remain > 0; $line++) { | 163 | for ($line = $start; $remain > 0; $line++) { |
| 165 | next if ($rawlines[$line] =~ /^-/); | 164 | next if ($rawlines[$line] =~ /^-/); |
| 166 | $remain--; | 165 | $remain--; |
| 167 | 166 | ||
| 168 | $blk .= $rawlines[$line]; | 167 | $blk .= $rawlines[$line]; |
| 168 | foreach my $c (split(//, $rawlines[$line])) { | ||
| 169 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; | ||
| 170 | if ($off > 0) { | ||
| 171 | $off--; | ||
| 172 | next; | ||
| 173 | } | ||
| 169 | 174 | ||
| 170 | @o = ($blk =~ /$open/g); | 175 | if ($c eq $close && $level > 0) { |
| 171 | @c = ($blk =~ /$close/g); | 176 | $level--; |
| 177 | last if ($level == 0); | ||
| 178 | } elsif ($c eq $open) { | ||
| 179 | $level++; | ||
| 180 | } | ||
| 181 | } | ||
| 172 | 182 | ||
| 173 | if (!$outer || (scalar(@o) - scalar(@c)) == 1) { | 183 | if (!$outer || $level <= 1) { |
| 174 | push(@res, $rawlines[$line]); | 184 | push(@res, $rawlines[$line]); |
| 175 | } | 185 | } |
| 176 | 186 | ||
| 177 | last if (scalar(@o) == scalar(@c)); | 187 | last if ($level == 0); |
| 178 | } | 188 | } |
| 179 | 189 | ||
| 180 | return @res; | 190 | return ($level, @res); |
| 181 | } | 191 | } |
| 182 | sub ctx_block_outer { | 192 | sub ctx_block_outer { |
| 183 | my ($linenr, $remain) = @_; | 193 | my ($linenr, $remain) = @_; |
| 184 | 194 | ||
| 185 | return ctx_block_get($linenr, $remain, 1, '\{', '\}'); | 195 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 196 | return @r; | ||
| 186 | } | 197 | } |
| 187 | sub ctx_block { | 198 | sub ctx_block { |
| 188 | my ($linenr, $remain) = @_; | 199 | my ($linenr, $remain) = @_; |
| 189 | 200 | ||
| 190 | return ctx_block_get($linenr, $remain, 0, '\{', '\}'); | 201 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 202 | return @r; | ||
| 191 | } | 203 | } |
| 192 | sub ctx_statement { | 204 | sub ctx_statement { |
| 205 | my ($linenr, $remain, $off) = @_; | ||
| 206 | |||
| 207 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); | ||
| 208 | return @r; | ||
| 209 | } | ||
| 210 | sub ctx_block_level { | ||
| 193 | my ($linenr, $remain) = @_; | 211 | my ($linenr, $remain) = @_; |
| 194 | 212 | ||
| 195 | return ctx_block_get($linenr, $remain, 0, '\(', '\)'); | 213 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 196 | } | 214 | } |
| 197 | 215 | ||
| 198 | sub ctx_locate_comment { | 216 | sub ctx_locate_comment { |
| @@ -246,16 +264,23 @@ sub cat_vet { | |||
| 246 | return $vet; | 264 | return $vet; |
| 247 | } | 265 | } |
| 248 | 266 | ||
| 267 | my @report = (); | ||
| 268 | sub report { | ||
| 269 | push(@report, $_[0]); | ||
| 270 | } | ||
| 271 | sub report_dump { | ||
| 272 | @report; | ||
| 273 | } | ||
| 249 | sub ERROR { | 274 | sub ERROR { |
| 250 | print "ERROR: $_[0]\n"; | 275 | report("ERROR: $_[0]\n"); |
| 251 | our $clean = 0; | 276 | our $clean = 0; |
| 252 | } | 277 | } |
| 253 | sub WARN { | 278 | sub WARN { |
| 254 | print "WARNING: $_[0]\n"; | 279 | report("WARNING: $_[0]\n"); |
| 255 | our $clean = 0; | 280 | our $clean = 0; |
| 256 | } | 281 | } |
| 257 | sub CHK { | 282 | sub CHK { |
| 258 | print "CHECK: $_[0]\n"; | 283 | report("CHECK: $_[0]\n"); |
| 259 | our $clean = 0; | 284 | our $clean = 0; |
| 260 | } | 285 | } |
| 261 | 286 | ||
| @@ -286,7 +311,7 @@ sub process { | |||
| 286 | 311 | ||
| 287 | my $Ident = qr{[A-Za-z\d_]+}; | 312 | my $Ident = qr{[A-Za-z\d_]+}; |
| 288 | my $Storage = qr{extern|static}; | 313 | my $Storage = qr{extern|static}; |
| 289 | my $Sparse = qr{__user|__kernel|__force|__iomem}; | 314 | my $Sparse = qr{__user|__kernel|__force|__iomem|__must_check|__init_refok}; |
| 290 | my $NonptrType = qr{ | 315 | my $NonptrType = qr{ |
| 291 | \b | 316 | \b |
| 292 | (?:const\s+)? | 317 | (?:const\s+)? |
| @@ -300,6 +325,7 @@ sub process { | |||
| 300 | unsigned| | 325 | unsigned| |
| 301 | float| | 326 | float| |
| 302 | double| | 327 | double| |
| 328 | bool| | ||
| 303 | long\s+int| | 329 | long\s+int| |
| 304 | long\s+long| | 330 | long\s+long| |
| 305 | long\s+long\s+int| | 331 | long\s+long\s+int| |
| @@ -315,10 +341,14 @@ sub process { | |||
| 315 | }x; | 341 | }x; |
| 316 | my $Type = qr{ | 342 | my $Type = qr{ |
| 317 | \b$NonptrType\b | 343 | \b$NonptrType\b |
| 318 | (?:\s*\*+\s*const|\s*\*+)? | 344 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? |
| 345 | (?:\s+$Sparse)* | ||
| 319 | }x; | 346 | }x; |
| 320 | my $Declare = qr{(?:$Storage\s+)?$Type}; | 347 | my $Declare = qr{(?:$Storage\s+)?$Type}; |
| 321 | my $Attribute = qr{__read_mostly|__init|__initdata}; | 348 | my $Attribute = qr{const|__read_mostly|__init|__initdata|__meminit}; |
| 349 | |||
| 350 | my $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; | ||
| 351 | my $Lval = qr{$Ident(?:$Member)*}; | ||
| 322 | 352 | ||
| 323 | # Pre-scan the patch looking for any __setup documentation. | 353 | # Pre-scan the patch looking for any __setup documentation. |
| 324 | my @setup_docs = (); | 354 | my @setup_docs = (); |
| @@ -466,16 +496,15 @@ sub process { | |||
| 466 | ERROR("use tabs not spaces\n" . $herevet); | 496 | ERROR("use tabs not spaces\n" . $herevet); |
| 467 | } | 497 | } |
| 468 | 498 | ||
| 469 | # | ||
| 470 | # The rest of our checks refer specifically to C style | ||
| 471 | # only apply those _outside_ comments. | ||
| 472 | # | ||
| 473 | next if ($in_comment); | ||
| 474 | |||
| 475 | # Remove comments from the line before processing. | 499 | # Remove comments from the line before processing. |
| 476 | $line =~ s@/\*.*\*/@@g; | 500 | my $comment_edge = ($line =~ s@/\*.*\*/@@g) + |
| 477 | $line =~ s@/\*.*@@; | 501 | ($line =~ s@/\*.*@@) + |
| 478 | $line =~ s@.*\*/@@; | 502 | ($line =~ s@^(.).*\*/@$1@); |
| 503 | |||
| 504 | # The rest of our checks refer specifically to C style | ||
| 505 | # only apply those _outside_ comments. Only skip | ||
| 506 | # lines in the middle of comments. | ||
| 507 | next if (!$comment_edge && $in_comment); | ||
| 479 | 508 | ||
| 480 | # Standardise the strings and chars within the input to simplify matching. | 509 | # Standardise the strings and chars within the input to simplify matching. |
| 481 | $line = sanitise_line($line); | 510 | $line = sanitise_line($line); |
| @@ -509,7 +538,7 @@ sub process { | |||
| 509 | # if/while/etc brace do not go on next line, unless defining a do while loop, | 538 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 510 | # or if that brace on the next line is for something else | 539 | # or if that brace on the next line is for something else |
| 511 | if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) { | 540 | if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) { |
| 512 | my @ctx = ctx_statement($linenr, $realcnt); | 541 | my @ctx = ctx_statement($linenr, $realcnt, 0); |
| 513 | my $ctx_ln = $linenr + $#ctx + 1; | 542 | my $ctx_ln = $linenr + $#ctx + 1; |
| 514 | my $ctx_cnt = $realcnt - $#ctx - 1; | 543 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 515 | my $ctx = join("\n", @ctx); | 544 | my $ctx = join("\n", @ctx); |
| @@ -521,7 +550,7 @@ sub process { | |||
| 521 | ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>"; | 550 | ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>"; |
| 522 | 551 | ||
| 523 | if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { | 552 | if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
| 524 | ERROR("That { should be on the previous line\n" . | 553 | ERROR("That open brace { should be on the previous line\n" . |
| 525 | "$here\n$ctx\n$lines[$ctx_ln - 1]"); | 554 | "$here\n$ctx\n$lines[$ctx_ln - 1]"); |
| 526 | } | 555 | } |
| 527 | } | 556 | } |
| @@ -535,6 +564,12 @@ sub process { | |||
| 535 | next; | 564 | next; |
| 536 | } | 565 | } |
| 537 | 566 | ||
| 567 | # check for initialisation to aggregates open brace on the next line | ||
| 568 | if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && | ||
| 569 | $line =~ /^.\s*{/) { | ||
| 570 | ERROR("That open brace { should be on the previous line\n" . $hereprev); | ||
| 571 | } | ||
| 572 | |||
| 538 | # | 573 | # |
| 539 | # Checks which are anchored on the added line. | 574 | # Checks which are anchored on the added line. |
| 540 | # | 575 | # |
| @@ -565,13 +600,18 @@ sub process { | |||
| 565 | if (($prevline !~ /^}/) && | 600 | if (($prevline !~ /^}/) && |
| 566 | ($prevline !~ /^\+}/) && | 601 | ($prevline !~ /^\+}/) && |
| 567 | ($prevline !~ /^ }/) && | 602 | ($prevline !~ /^ }/) && |
| 568 | ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) { | 603 | ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) { |
| 569 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); | 604 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
| 570 | } | 605 | } |
| 571 | } | 606 | } |
| 572 | 607 | ||
| 608 | # check for external initialisers. | ||
| 609 | if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) { | ||
| 610 | ERROR("do not initialise externals to 0 or NULL\n" . | ||
| 611 | $herecurr); | ||
| 612 | } | ||
| 573 | # check for static initialisers. | 613 | # check for static initialisers. |
| 574 | if ($line=~/\s*static\s.*=\s+(0|NULL);/) { | 614 | if ($line =~ /\s*static\s.*=\s*(0|NULL);/) { |
| 575 | ERROR("do not initialise statics to 0 or NULL\n" . | 615 | ERROR("do not initialise statics to 0 or NULL\n" . |
| 576 | $herecurr); | 616 | $herecurr); |
| 577 | } | 617 | } |
| @@ -593,11 +633,11 @@ sub process { | |||
| 593 | ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" . | 633 | ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" . |
| 594 | $herecurr); | 634 | $herecurr); |
| 595 | 635 | ||
| 596 | } elsif ($line =~ m{$NonptrType(\*+)(?:\s+const)?\s+[A-Za-z\d_]+}) { | 636 | } elsif ($line =~ m{$NonptrType(\*+)(?:\s+$Attribute)?\s+[A-Za-z\d_]+}) { |
| 597 | ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" . | 637 | ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" . |
| 598 | $herecurr); | 638 | $herecurr); |
| 599 | 639 | ||
| 600 | } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+const)\s+[A-Za-z\d_]+}) { | 640 | } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+$Attribute)\s+[A-Za-z\d_]+}) { |
| 601 | ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" . | 641 | ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" . |
| 602 | $herecurr); | 642 | $herecurr); |
| 603 | } | 643 | } |
| @@ -614,7 +654,7 @@ sub process { | |||
| 614 | # to try and find and validate the current printk. In summary the current | 654 | # to try and find and validate the current printk. In summary the current |
| 615 | # printk includes all preceeding printk's which have no newline on the end. | 655 | # printk includes all preceeding printk's which have no newline on the end. |
| 616 | # we assume the first bad printk is the one to report. | 656 | # we assume the first bad printk is the one to report. |
| 617 | if ($line =~ /\bprintk\((?!KERN_)/) { | 657 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
| 618 | my $ok = 0; | 658 | my $ok = 0; |
| 619 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { | 659 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 620 | #print "CHECK<$lines[$ln - 1]\n"; | 660 | #print "CHECK<$lines[$ln - 1]\n"; |
| @@ -639,6 +679,12 @@ sub process { | |||
| 639 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); | 679 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); |
| 640 | } | 680 | } |
| 641 | 681 | ||
| 682 | # check for spaces between functions and their parentheses. | ||
| 683 | if ($line =~ /($Ident)\s+\(/ && | ||
| 684 | $1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright)$/ && | ||
| 685 | $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) { | ||
| 686 | WARN("no space between function name and open parenthesis '('\n" . $herecurr); | ||
| 687 | } | ||
| 642 | # Check operator spacing. | 688 | # Check operator spacing. |
| 643 | # Note we expand the line with the leading + as the real | 689 | # Note we expand the line with the leading + as the real |
| 644 | # line will be displayed with the leading + and the tabs | 690 | # line will be displayed with the leading + and the tabs |
| @@ -647,7 +693,7 @@ sub process { | |||
| 647 | $opline = expand_tabs($opline); | 693 | $opline = expand_tabs($opline); |
| 648 | $opline =~ s/^./ /; | 694 | $opline =~ s/^./ /; |
| 649 | if (!($line=~/\#\s*include/)) { | 695 | if (!($line=~/\#\s*include/)) { |
| 650 | my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); | 696 | my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|=>|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); |
| 651 | my $off = 0; | 697 | my $off = 0; |
| 652 | for (my $n = 0; $n < $#elements; $n += 2) { | 698 | for (my $n = 0; $n < $#elements; $n += 2) { |
| 653 | $off += length($elements[$n]); | 699 | $off += length($elements[$n]); |
| @@ -667,6 +713,7 @@ sub process { | |||
| 667 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); | 713 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
| 668 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); | 714 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 669 | $c = 'O' if ($elements[$n + 2] eq ''); | 715 | $c = 'O' if ($elements[$n + 2] eq ''); |
| 716 | $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/); | ||
| 670 | } else { | 717 | } else { |
| 671 | $c = 'E'; | 718 | $c = 'E'; |
| 672 | } | 719 | } |
| @@ -767,14 +814,39 @@ sub process { | |||
| 767 | 814 | ||
| 768 | # All the others need spaces both sides. | 815 | # All the others need spaces both sides. |
| 769 | } elsif ($ctx !~ /[EW]x[WE]/) { | 816 | } elsif ($ctx !~ /[EW]x[WE]/) { |
| 770 | ERROR("need spaces around that '$op' $at\n" . $hereptr); | 817 | # Ignore email addresses <foo@bar> |
| 818 | if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) && | ||
| 819 | !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) { | ||
| 820 | ERROR("need spaces around that '$op' $at\n" . $hereptr); | ||
| 821 | } | ||
| 771 | } | 822 | } |
| 772 | $off += length($elements[$n + 1]); | 823 | $off += length($elements[$n + 1]); |
| 773 | } | 824 | } |
| 774 | } | 825 | } |
| 775 | 826 | ||
| 827 | # check for multiple assignments | ||
| 828 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { | ||
| 829 | WARN("multiple assignments should be avoided\n" . $herecurr); | ||
| 830 | } | ||
| 831 | |||
| 832 | ## # check for multiple declarations, allowing for a function declaration | ||
| 833 | ## # continuation. | ||
| 834 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && | ||
| 835 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { | ||
| 836 | ## | ||
| 837 | ## # Remove any bracketed sections to ensure we do not | ||
| 838 | ## # falsly report the parameters of functions. | ||
| 839 | ## my $ln = $line; | ||
| 840 | ## while ($ln =~ s/\([^\(\)]*\)//g) { | ||
| 841 | ## } | ||
| 842 | ## if ($ln =~ /,/) { | ||
| 843 | ## WARN("declaring multiple variables together should be avoided\n" . $herecurr); | ||
| 844 | ## } | ||
| 845 | ## } | ||
| 846 | |||
| 776 | #need space before brace following if, while, etc | 847 | #need space before brace following if, while, etc |
| 777 | if ($line =~ /\(.*\){/ || $line =~ /do{/) { | 848 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 849 | $line =~ /do{/) { | ||
| 778 | ERROR("need a space before the open brace '{'\n" . $herecurr); | 850 | ERROR("need a space before the open brace '{'\n" . $herecurr); |
| 779 | } | 851 | } |
| 780 | 852 | ||
| @@ -784,6 +856,22 @@ sub process { | |||
| 784 | ERROR("need a space after that close brace '}'\n" . $herecurr); | 856 | ERROR("need a space after that close brace '}'\n" . $herecurr); |
| 785 | } | 857 | } |
| 786 | 858 | ||
| 859 | # check spacing on square brackets | ||
| 860 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { | ||
| 861 | ERROR("no space after that open square bracket '['\n" . $herecurr); | ||
| 862 | } | ||
| 863 | if ($line =~ /\s\]/) { | ||
| 864 | ERROR("no space before that close square bracket ']'\n" . $herecurr); | ||
| 865 | } | ||
| 866 | |||
| 867 | # check spacing on paretheses | ||
| 868 | if ($line =~ /\(\s/ && $line !~ /\(\s*$/) { | ||
| 869 | ERROR("no space after that open parenthesis '('\n" . $herecurr); | ||
| 870 | } | ||
| 871 | if ($line =~ /\s\)/) { | ||
| 872 | ERROR("no space before that close parenthesis ')'\n" . $herecurr); | ||
| 873 | } | ||
| 874 | |||
| 787 | #goto labels aren't indented, allow a single space however | 875 | #goto labels aren't indented, allow a single space however |
| 788 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and | 876 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
| 789 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { | 877 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
| @@ -847,13 +935,18 @@ sub process { | |||
| 847 | # or the one below. | 935 | # or the one below. |
| 848 | my $ln = $linenr; | 936 | my $ln = $linenr; |
| 849 | my $cnt = $realcnt; | 937 | my $cnt = $realcnt; |
| 938 | my $off = 0; | ||
| 850 | 939 | ||
| 851 | # If the macro starts on the define line start there. | 940 | # If the macro starts on the define line start |
| 852 | if ($prevline !~ m{^.#\s*define\s*$Ident(?:\([^\)]*\))?\s*\\\s*$}) { | 941 | # grabbing the statement after the identifier |
| 942 | $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$}; | ||
| 943 | ##print "1<$1> 2<$2>\n"; | ||
| 944 | if (defined $2 && $2 ne '') { | ||
| 945 | $off = length($1); | ||
| 853 | $ln--; | 946 | $ln--; |
| 854 | $cnt++; | 947 | $cnt++; |
| 855 | } | 948 | } |
| 856 | my @ctx = ctx_statement($ln, $cnt); | 949 | my @ctx = ctx_statement($ln, $cnt, $off); |
| 857 | my $ctx_ln = $ln + $#ctx + 1; | 950 | my $ctx_ln = $ln + $#ctx + 1; |
| 858 | my $ctx = join("\n", @ctx); | 951 | my $ctx = join("\n", @ctx); |
| 859 | 952 | ||
| @@ -873,6 +966,48 @@ sub process { | |||
| 873 | } | 966 | } |
| 874 | } | 967 | } |
| 875 | 968 | ||
| 969 | # check for redundant bracing round if etc | ||
| 970 | if ($line =~ /\b(if|while|for|else)\b/) { | ||
| 971 | # Locate the end of the opening statement. | ||
| 972 | my @control = ctx_statement($linenr, $realcnt, 0); | ||
| 973 | my $nr = $linenr + (scalar(@control) - 1); | ||
| 974 | my $cnt = $realcnt - (scalar(@control) - 1); | ||
| 975 | |||
| 976 | my $off = $realcnt - $cnt; | ||
| 977 | #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n"; | ||
| 978 | |||
| 979 | # If this is is a braced statement group check it | ||
| 980 | if ($lines[$nr - 1] =~ /{\s*$/) { | ||
| 981 | my ($lvl, @block) = ctx_block_level($nr, $cnt); | ||
| 982 | |||
| 983 | my $stmt = join(' ', @block); | ||
| 984 | $stmt =~ s/(^[^{]*){//; | ||
| 985 | my $before = $1; | ||
| 986 | $stmt =~ s/}([^}]*$)//; | ||
| 987 | my $after = $1; | ||
| 988 | |||
| 989 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; | ||
| 990 | #print "stmt<$stmt>\n\n"; | ||
| 991 | |||
| 992 | # Count the ;'s if there is fewer than two | ||
| 993 | # then there can only be one statement, | ||
| 994 | # if there is a brace inside we cannot | ||
| 995 | # trivially detect if its one statement. | ||
| 996 | # Also nested if's often require braces to | ||
| 997 | # disambiguate the else binding so shhh there. | ||
| 998 | my @semi = ($stmt =~ /;/g); | ||
| 999 | push(@semi, "/**/") if ($stmt =~ m@/\*@); | ||
| 1000 | ##print "semi<" . scalar(@semi) . ">\n"; | ||
| 1001 | if ($lvl == 0 && scalar(@semi) < 2 && | ||
| 1002 | $stmt !~ /{/ && $stmt !~ /\bif\b/ && | ||
| 1003 | $before !~ /}/ && $after !~ /{/) { | ||
| 1004 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; | ||
| 1005 | shift(@block); | ||
| 1006 | WARN("braces {} are not necessary for single statement blocks\n" . $herectx); | ||
| 1007 | } | ||
| 1008 | } | ||
| 1009 | } | ||
| 1010 | |||
| 876 | # don't include deprecated include files (uses RAW line) | 1011 | # don't include deprecated include files (uses RAW line) |
| 877 | for my $inc (@dep_includes) { | 1012 | for my $inc (@dep_includes) { |
| 878 | if ($rawline =~ m@\#\s*include\s*\<$inc>@) { | 1013 | if ($rawline =~ m@\#\s*include\s*\<$inc>@) { |
| @@ -898,6 +1033,14 @@ sub process { | |||
| 898 | $herecurr); | 1033 | $herecurr); |
| 899 | } | 1034 | } |
| 900 | 1035 | ||
| 1036 | # check for needless kfree() checks | ||
| 1037 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { | ||
| 1038 | my $expr = $1; | ||
| 1039 | if ($line =~ /\bkfree\(\Q$expr\E\);/) { | ||
| 1040 | WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev); | ||
| 1041 | } | ||
| 1042 | } | ||
| 1043 | |||
| 901 | # warn about #ifdefs in C files | 1044 | # warn about #ifdefs in C files |
| 902 | # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { | 1045 | # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
| 903 | # print "#ifdef in C files should be avoided\n"; | 1046 | # print "#ifdef in C files should be avoided\n"; |
| @@ -905,6 +1048,11 @@ sub process { | |||
| 905 | # $clean = 0; | 1048 | # $clean = 0; |
| 906 | # } | 1049 | # } |
| 907 | 1050 | ||
| 1051 | # warn about spacing in #ifdefs | ||
| 1052 | if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) { | ||
| 1053 | ERROR("exactly one space required after that #$1\n" . $herecurr); | ||
| 1054 | } | ||
| 1055 | |||
| 908 | # check for spinlock_t definitions without a comment. | 1056 | # check for spinlock_t definitions without a comment. |
| 909 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { | 1057 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { |
| 910 | my $which = $1; | 1058 | my $which = $1; |
| @@ -919,14 +1067,14 @@ sub process { | |||
| 919 | } | 1067 | } |
| 920 | } | 1068 | } |
| 921 | # check of hardware specific defines | 1069 | # check of hardware specific defines |
| 922 | if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) { | 1070 | if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
| 923 | CHK("architecture specific defines should be avoided\n" . $herecurr); | 1071 | CHK("architecture specific defines should be avoided\n" . $herecurr); |
| 924 | } | 1072 | } |
| 925 | 1073 | ||
| 926 | # check the location of the inline attribute, that it is between | 1074 | # check the location of the inline attribute, that it is between |
| 927 | # storage class and type. | 1075 | # storage class and type. |
| 928 | if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ || | 1076 | if ($line =~ /$Type\s+(?:inline|__always_inline|noinline)\b/ || |
| 929 | $line =~ /\b(?:inline|always_inline)\s+$Storage/) { | 1077 | $line =~ /\b(?:inline|__always_inline|noinline)\s+$Storage/) { |
| 930 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); | 1078 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); |
| 931 | } | 1079 | } |
| 932 | 1080 | ||
| @@ -952,6 +1100,9 @@ sub process { | |||
| 952 | ERROR("Missing Signed-off-by: line(s)\n"); | 1100 | ERROR("Missing Signed-off-by: line(s)\n"); |
| 953 | } | 1101 | } |
| 954 | 1102 | ||
| 1103 | if ($clean == 0 && ($chk_patch || $is_patch)) { | ||
| 1104 | print report_dump(); | ||
| 1105 | } | ||
| 955 | if ($clean == 1 && $quiet == 0) { | 1106 | if ($clean == 1 && $quiet == 0) { |
| 956 | print "Your patch has no obvious style problems and is ready for submission.\n" | 1107 | print "Your patch has no obvious style problems and is ready for submission.\n" |
| 957 | } | 1108 | } |
