diff options
author | Joe Perches <joe@perches.com> | 2014-08-06 19:11:05 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-08-06 21:01:28 -0400 |
commit | d752fcc88b7dddc0bbe6d43b658b306a9d7d1dae (patch) | |
tree | 7499c60afad317d6fce1bdb9e0799a40b9b7c11a /scripts | |
parent | 194f66fc9567367b3fa2dcbc1b87b091330ef186 (diff) |
checkpatch: add ability to insert and delete lines to patch/file
This can be valuable to insert or delete blank lines as well as fix
misplaced brace or else uses.
Store indexes of lines to be added/deleted and the new lines.
When creating the --fix file, insert or delete the appropriate lines and
update the patch range information.
Signed-off-by: Joe Perches <joe@perches.com>
Cc: Andy Whitcroft <apw@canonical.com>
Cc: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/checkpatch.pl | 141 |
1 files changed, 130 insertions, 11 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index f905d7b50530..f3d9a883f8be 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -583,6 +583,8 @@ $chk_signoff = 0 if ($file); | |||
583 | my @rawlines = (); | 583 | my @rawlines = (); |
584 | my @lines = (); | 584 | my @lines = (); |
585 | my @fixed = (); | 585 | my @fixed = (); |
586 | my @fixed_inserted = (); | ||
587 | my @fixed_deleted = (); | ||
586 | my $fixlinenr = -1; | 588 | my $fixlinenr = -1; |
587 | 589 | ||
588 | my $vname; | 590 | my $vname; |
@@ -613,6 +615,8 @@ for my $filename (@ARGV) { | |||
613 | @rawlines = (); | 615 | @rawlines = (); |
614 | @lines = (); | 616 | @lines = (); |
615 | @fixed = (); | 617 | @fixed = (); |
618 | @fixed_inserted = (); | ||
619 | @fixed_deleted = (); | ||
616 | $fixlinenr = -1; | 620 | $fixlinenr = -1; |
617 | } | 621 | } |
618 | 622 | ||
@@ -1526,6 +1530,69 @@ sub report_dump { | |||
1526 | our @report; | 1530 | our @report; |
1527 | } | 1531 | } |
1528 | 1532 | ||
1533 | sub fixup_current_range { | ||
1534 | my ($lineRef, $offset, $length) = @_; | ||
1535 | |||
1536 | if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { | ||
1537 | my $o = $1; | ||
1538 | my $l = $2; | ||
1539 | my $no = $o + $offset; | ||
1540 | my $nl = $l + $length; | ||
1541 | $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; | ||
1542 | } | ||
1543 | } | ||
1544 | |||
1545 | sub fix_inserted_deleted_lines { | ||
1546 | my ($linesRef, $insertedRef, $deletedRef) = @_; | ||
1547 | |||
1548 | my $range_last_linenr = 0; | ||
1549 | my $delta_offset = 0; | ||
1550 | |||
1551 | my $old_linenr = 0; | ||
1552 | my $new_linenr = 0; | ||
1553 | |||
1554 | my $next_insert = 0; | ||
1555 | my $next_delete = 0; | ||
1556 | |||
1557 | my @lines = (); | ||
1558 | |||
1559 | my $inserted = @{$insertedRef}[$next_insert++]; | ||
1560 | my $deleted = @{$deletedRef}[$next_delete++]; | ||
1561 | |||
1562 | foreach my $old_line (@{$linesRef}) { | ||
1563 | my $save_line = 1; | ||
1564 | my $line = $old_line; #don't modify the array | ||
1565 | if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename | ||
1566 | $delta_offset = 0; | ||
1567 | } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk | ||
1568 | $range_last_linenr = $new_linenr; | ||
1569 | fixup_current_range(\$line, $delta_offset, 0); | ||
1570 | } | ||
1571 | |||
1572 | while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { | ||
1573 | $deleted = @{$deletedRef}[$next_delete++]; | ||
1574 | $save_line = 0; | ||
1575 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); | ||
1576 | } | ||
1577 | |||
1578 | while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { | ||
1579 | push(@lines, ${$inserted}{'LINE'}); | ||
1580 | $inserted = @{$insertedRef}[$next_insert++]; | ||
1581 | $new_linenr++; | ||
1582 | fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); | ||
1583 | } | ||
1584 | |||
1585 | if ($save_line) { | ||
1586 | push(@lines, $line); | ||
1587 | $new_linenr++; | ||
1588 | } | ||
1589 | |||
1590 | $old_linenr++; | ||
1591 | } | ||
1592 | |||
1593 | return @lines; | ||
1594 | } | ||
1595 | |||
1529 | sub ERROR { | 1596 | sub ERROR { |
1530 | my ($type, $msg) = @_; | 1597 | my ($type, $msg) = @_; |
1531 | 1598 | ||
@@ -2377,16 +2444,31 @@ sub process { | |||
2377 | $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || | 2444 | $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || |
2378 | $line =~ /^\+\s*DECLARE/ || | 2445 | $line =~ /^\+\s*DECLARE/ || |
2379 | $line =~ /^\+\s*__setup/)) { | 2446 | $line =~ /^\+\s*__setup/)) { |
2380 | CHK("LINE_SPACING", | 2447 | if (CHK("LINE_SPACING", |
2381 | "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev); | 2448 | "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && |
2449 | $fix) { | ||
2450 | my $inserted = { | ||
2451 | LINENR => $fixlinenr, | ||
2452 | LINE => "\+", | ||
2453 | }; | ||
2454 | push(@fixed_inserted, $inserted); | ||
2455 | } | ||
2382 | } | 2456 | } |
2383 | 2457 | ||
2384 | # check for multiple consecutive blank lines | 2458 | # check for multiple consecutive blank lines |
2385 | if ($prevline =~ /^[\+ ]\s*$/ && | 2459 | if ($prevline =~ /^[\+ ]\s*$/ && |
2386 | $line =~ /^\+\s*$/ && | 2460 | $line =~ /^\+\s*$/ && |
2387 | $last_blank_line != ($linenr - 1)) { | 2461 | $last_blank_line != ($linenr - 1)) { |
2388 | CHK("LINE_SPACING", | 2462 | if (CHK("LINE_SPACING", |
2389 | "Please don't use multiple blank lines\n" . $hereprev); | 2463 | "Please don't use multiple blank lines\n" . $hereprev) && |
2464 | $fix) { | ||
2465 | my $deleted = { | ||
2466 | LINENR => $fixlinenr, | ||
2467 | LINE => $rawline, | ||
2468 | }; | ||
2469 | push(@fixed_deleted, $deleted); | ||
2470 | } | ||
2471 | |||
2390 | $last_blank_line = $linenr; | 2472 | $last_blank_line = $linenr; |
2391 | } | 2473 | } |
2392 | 2474 | ||
@@ -2424,8 +2506,15 @@ sub process { | |||
2424 | $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && | 2506 | $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && |
2425 | # indentation of previous and current line are the same | 2507 | # indentation of previous and current line are the same |
2426 | (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { | 2508 | (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { |
2427 | WARN("LINE_SPACING", | 2509 | if (WARN("LINE_SPACING", |
2428 | "Missing a blank line after declarations\n" . $hereprev); | 2510 | "Missing a blank line after declarations\n" . $hereprev) && |
2511 | $fix) { | ||
2512 | my $inserted = { | ||
2513 | LINENR => $fixlinenr, | ||
2514 | LINE => "\+", | ||
2515 | }; | ||
2516 | push(@fixed_inserted, $inserted); | ||
2517 | } | ||
2429 | } | 2518 | } |
2430 | 2519 | ||
2431 | # check for spaces at the beginning of a line. | 2520 | # check for spaces at the beginning of a line. |
@@ -2627,7 +2716,7 @@ sub process { | |||
2627 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; | 2716 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
2628 | #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; | 2717 | #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; |
2629 | 2718 | ||
2630 | if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { | 2719 | if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
2631 | ERROR("OPEN_BRACE", | 2720 | ERROR("OPEN_BRACE", |
2632 | "that open brace { should be on the previous line\n" . | 2721 | "that open brace { should be on the previous line\n" . |
2633 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); | 2722 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
@@ -2777,8 +2866,34 @@ sub process { | |||
2777 | # check for initialisation to aggregates open brace on the next line | 2866 | # check for initialisation to aggregates open brace on the next line |
2778 | if ($line =~ /^.\s*{/ && | 2867 | if ($line =~ /^.\s*{/ && |
2779 | $prevline =~ /(?:^|[^=])=\s*$/) { | 2868 | $prevline =~ /(?:^|[^=])=\s*$/) { |
2780 | ERROR("OPEN_BRACE", | 2869 | if (ERROR("OPEN_BRACE", |
2781 | "that open brace { should be on the previous line\n" . $hereprev); | 2870 | "that open brace { should be on the previous line\n" . $hereprev) && |
2871 | $fix && $prevline =~ /^\+/) { | ||
2872 | my $deleted = { | ||
2873 | LINENR => $fixlinenr - 1, | ||
2874 | LINE => $prevrawline, | ||
2875 | }; | ||
2876 | push(@fixed_deleted, $deleted); | ||
2877 | $deleted = { | ||
2878 | LINENR => $fixlinenr, | ||
2879 | LINE => $rawline, | ||
2880 | }; | ||
2881 | push(@fixed_deleted, $deleted); | ||
2882 | my $fixedline = $prevrawline; | ||
2883 | $fixedline =~ s/\s*=\s*$/ = {/; | ||
2884 | my $inserted = { | ||
2885 | LINENR => $fixlinenr, | ||
2886 | LINE => $fixedline, | ||
2887 | }; | ||
2888 | push(@fixed_inserted, $inserted); | ||
2889 | $fixedline = $line; | ||
2890 | $fixedline =~ s/^(.\s*){\s*/$1/; | ||
2891 | $inserted = { | ||
2892 | LINENR => $fixlinenr, | ||
2893 | LINE => $fixedline, | ||
2894 | }; | ||
2895 | push(@fixed_inserted, $inserted); | ||
2896 | } | ||
2782 | } | 2897 | } |
2783 | 2898 | ||
2784 | # | 2899 | # |
@@ -4900,12 +5015,16 @@ sub process { | |||
4900 | hash_show_words(\%use_type, "Used"); | 5015 | hash_show_words(\%use_type, "Used"); |
4901 | hash_show_words(\%ignore_type, "Ignored"); | 5016 | hash_show_words(\%ignore_type, "Ignored"); |
4902 | 5017 | ||
4903 | if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { | 5018 | if ($clean == 0 && $fix && |
5019 | ("@rawlines" ne "@fixed" || | ||
5020 | $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { | ||
4904 | my $newfile = $filename; | 5021 | my $newfile = $filename; |
4905 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); | 5022 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); |
4906 | my $linecount = 0; | 5023 | my $linecount = 0; |
4907 | my $f; | 5024 | my $f; |
4908 | 5025 | ||
5026 | @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); | ||
5027 | |||
4909 | open($f, '>', $newfile) | 5028 | open($f, '>', $newfile) |
4910 | or die "$P: Can't open $newfile for write\n"; | 5029 | or die "$P: Can't open $newfile for write\n"; |
4911 | foreach my $fixed_line (@fixed) { | 5030 | foreach my $fixed_line (@fixed) { |
@@ -4913,7 +5032,7 @@ sub process { | |||
4913 | if ($file) { | 5032 | if ($file) { |
4914 | if ($linecount > 3) { | 5033 | if ($linecount > 3) { |
4915 | $fixed_line =~ s/^\+//; | 5034 | $fixed_line =~ s/^\+//; |
4916 | print $f $fixed_line. "\n"; | 5035 | print $f $fixed_line . "\n"; |
4917 | } | 5036 | } |
4918 | } else { | 5037 | } else { |
4919 | print $f $fixed_line . "\n"; | 5038 | print $f $fixed_line . "\n"; |