History

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.

Joe Perches committed 12 years ago

Commit d752fcc1 parent 194f66f1 file · +130 −11Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+130−11
@@ -583,6 +583,8 @@ $chk_signoff = 0 if ($file); · 582 unmodified lines
583583 my @rawlines = ();
584584 my @lines = ();
585585 my @fixed = ();
586+my @fixed_inserted = ();
587+my @fixed_deleted = ();
586588 my $fixlinenr = -1;
587589
588590 my $vname;
@@ -613,6 +615,8 @@ for my $filename (@ARGV) { · 24 unmodified lines
613615 @rawlines = ();
614616 @lines = ();
615617 @fixed = ();
618+ @fixed_inserted = ();
619+ @fixed_deleted = ();
616620 $fixlinenr = -1;
617621 }
618622
@@ -1526,6 +1530,69 @@ sub report_dump { · 907 unmodified lines
15261530 our @report;
15271531 }
15281532
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+
15291596 sub ERROR {
15301597 my ($type, $msg) = @_;
15311598
@@ -2377,16 +2444,31 @@ sub process { · 845 unmodified lines
23772444 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
23782445 $line =~ /^\+\s*DECLARE/ ||
23792446 $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+ }
23822456 }
23832457
23842458 # check for multiple consecutive blank lines
23852459 if ($prevline =~ /^[\+ ]\s*$/ &&
23862460 $line =~ /^\+\s*$/ &&
23872461 $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+
23902472 $last_blank_line = $linenr;
23912473 }
23922474
@@ -2424,8 +2506,15 @@ sub process { · 31 unmodified lines
24242506 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
24252507 # indentation of previous and current line are the same
24262508 (($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+ }
24292518 }
24302519
24312520 # check for spaces at the beginning of a line.
@@ -2627,7 +2716,7 @@ sub process { · 195 unmodified lines
26272716 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
26282717 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
26292718
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*{/) {
26312720 ERROR("OPEN_BRACE",
26322721 "that open brace { should be on the previous line\n" .
26332722 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
@@ -2777,8 +2866,34 @@ sub process { · 143 unmodified lines
27772866 # check for initialisation to aggregates open brace on the next line
27782867 if ($line =~ /^.\s*{/ &&
27792868 $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+ }
27822897 }
27832898
27842899 #
@@ -4900,12 +5015,16 @@ sub process { · 2115 unmodified lines
49005015 hash_show_words(\%use_type, "Used");
49015016 hash_show_words(\%ignore_type, "Ignored");
49025017
4903− if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
5018+ if ($clean == 0 && $fix &&
5019+ ("@rawlines" ne "@fixed" ||
5020+ $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
49045021 my $newfile = $filename;
49055022 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
49065023 my $linecount = 0;
49075024 my $f;
49085025
5026+ @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5027+
49095028 open($f, '>', $newfile)
49105029 or die "$P: Can't open $newfile for write\n";
49115030 foreach my $fixed_line (@fixed) {
@@ -4913,7 +5032,7 @@ sub process { · 1 unmodified line
49135032 if ($file) {
49145033 if ($linecount > 3) {
49155034 $fixed_line =~ s/^\+//;
4916− print $f $fixed_line. "\n";
5035+ print $f $fixed_line . "\n";
49175036 }
49185037 } else {
49195038 print $f $fixed_line . "\n";