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.
Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all
Unified · Split
| @@ -583,6 +583,8 @@ $chk_signoff = 0 if ($file); · 582 unmodified lines | ||
| 583 | 583 | my @rawlines = (); |
| 584 | 584 | my @lines = (); |
| 585 | 585 | my @fixed = (); |
| 586 | +my @fixed_inserted = (); | |
| 587 | +my @fixed_deleted = (); | |
| 586 | 588 | my $fixlinenr = -1; |
| 587 | 589 | |
| 588 | 590 | my $vname; |
| @@ -613,6 +615,8 @@ for my $filename (@ARGV) { · 24 unmodified lines | ||
| 613 | 615 | @rawlines = (); |
| 614 | 616 | @lines = (); |
| 615 | 617 | @fixed = (); |
| 618 | + @fixed_inserted = (); | |
| 619 | + @fixed_deleted = (); | |
| 616 | 620 | $fixlinenr = -1; |
| 617 | 621 | } |
| 618 | 622 | |
| @@ -1526,6 +1530,69 @@ sub report_dump { · 907 unmodified lines | ||
| 1526 | 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 | 1596 | sub ERROR { |
| 1530 | 1597 | my ($type, $msg) = @_; |
| 1531 | 1598 | |
| @@ -2377,16 +2444,31 @@ sub process { · 845 unmodified lines | ||
| 2377 | 2444 | $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || |
| 2378 | 2445 | $line =~ /^\+\s*DECLARE/ || |
| 2379 | 2446 | $line =~ /^\+\s*__setup/)) { |
| 2380 | − CHK("LINE_SPACING", | |
| 2381 | − "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev); | |
| 2447 | + if (CHK("LINE_SPACING", | |
| 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 | 2458 | # check for multiple consecutive blank lines |
| 2385 | 2459 | if ($prevline =~ /^[\+ ]\s*$/ && |
| 2386 | 2460 | $line =~ /^\+\s*$/ && |
| 2387 | 2461 | $last_blank_line != ($linenr - 1)) { |
| 2388 | − CHK("LINE_SPACING", | |
| 2389 | − "Please don't use multiple blank lines\n" . $hereprev); | |
| 2462 | + if (CHK("LINE_SPACING", | |
| 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 | 2472 | $last_blank_line = $linenr; |
| 2391 | 2473 | } |
| 2392 | 2474 | |
| @@ -2424,8 +2506,15 @@ sub process { · 31 unmodified lines | ||
| 2424 | 2506 | $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && |
| 2425 | 2507 | # indentation of previous and current line are the same |
| 2426 | 2508 | (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { |
| 2427 | − WARN("LINE_SPACING", | |
| 2428 | − "Missing a blank line after declarations\n" . $hereprev); | |
| 2509 | + if (WARN("LINE_SPACING", | |
| 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 | 2520 | # check for spaces at the beginning of a line. |
| @@ -2627,7 +2716,7 @@ sub process { · 195 unmodified lines | ||
| 2627 | 2716 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 2628 | 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 | 2720 | ERROR("OPEN_BRACE", |
| 2632 | 2721 | "that open brace { should be on the previous line\n" . |
| 2633 | 2722 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
| @@ -2777,8 +2866,34 @@ sub process { · 143 unmodified lines | ||
| 2777 | 2866 | # check for initialisation to aggregates open brace on the next line |
| 2778 | 2867 | if ($line =~ /^.\s*{/ && |
| 2779 | 2868 | $prevline =~ /(?:^|[^=])=\s*$/) { |
| 2780 | − ERROR("OPEN_BRACE", | |
| 2781 | − "that open brace { should be on the previous line\n" . $hereprev); | |
| 2869 | + if (ERROR("OPEN_BRACE", | |
| 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 { · 2115 unmodified lines | ||
| 4900 | 5015 | hash_show_words(\%use_type, "Used"); |
| 4901 | 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 | 5021 | my $newfile = $filename; |
| 4905 | 5022 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); |
| 4906 | 5023 | my $linecount = 0; |
| 4907 | 5024 | my $f; |
| 4908 | 5025 | |
| 5026 | + @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); | |
| 5027 | + | |
| 4909 | 5028 | open($f, '>', $newfile) |
| 4910 | 5029 | or die "$P: Can't open $newfile for write\n"; |
| 4911 | 5030 | foreach my $fixed_line (@fixed) { |
| @@ -4913,7 +5032,7 @@ sub process { · 1 unmodified line | ||
| 4913 | 5032 | if ($file) { |
| 4914 | 5033 | if ($linecount > 3) { |
| 4915 | 5034 | $fixed_line =~ s/^\+//; |
| 4916 | − print $f $fixed_line. "\n"; | |
| 5035 | + print $f $fixed_line . "\n"; | |
| 4917 | 5036 | } |
| 4918 | 5037 | } else { |
| 4919 | 5038 | print $f $fixed_line . "\n"; |