Commit b34c648b authored by Joe Perches's avatar Joe Perches Committed by Linus Torvalds

checkpatch: better --fix of SPACING errors.

Previous attempt at fixing SPACING errors could make a hash of several
defects.

This patch should make --fix be a lot better at correcting these defects.

Trim left and right sides of these defects appropriately instead of a
somewhat random attempt at it.

Trim left spaces from any following bit of the modified line when only a
single space is required around an operator.
Signed-off-by: default avatarJoe Perches <joe@perches.com>
Cc: Phil Carmody <phil.carmody@partner.samsung.com>
Cc: Andy Whitcroft <apw@canonical.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent f95a7e6a
...@@ -1472,7 +1472,23 @@ sub check_absolute_file { ...@@ -1472,7 +1472,23 @@ sub check_absolute_file {
sub trim { sub trim {
my ($string) = @_; my ($string) = @_;
$string =~ s/(^\s+|\s+$)//g; $string =~ s/^\s+|\s+$//g;
return $string;
}
sub ltrim {
my ($string) = @_;
$string =~ s/^\s+//;
return $string;
}
sub rtrim {
my ($string) = @_;
$string =~ s/\s+$//;
return $string; return $string;
} }
...@@ -2821,6 +2837,7 @@ sub process { ...@@ -2821,6 +2837,7 @@ sub process {
$off = 0; $off = 0;
my $blank = copy_spacing($opline); my $blank = copy_spacing($opline);
my $last_after = -1;
for (my $n = 0; $n < $#elements; $n += 2) { for (my $n = 0; $n < $#elements; $n += 2) {
...@@ -2886,7 +2903,7 @@ sub process { ...@@ -2886,7 +2903,7 @@ sub process {
$cc !~ /^\\/ && $cc !~ /^;/) { $cc !~ /^\\/ && $cc !~ /^;/) {
if (ERROR("SPACING", if (ERROR("SPACING",
"space required after that '$op' $at\n" . $hereptr)) { "space required after that '$op' $at\n" . $hereptr)) {
$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
$line_fixed = 1; $line_fixed = 1;
} }
} }
...@@ -2901,11 +2918,11 @@ sub process { ...@@ -2901,11 +2918,11 @@ sub process {
if ($ctx =~ /Wx.|.xW/) { if ($ctx =~ /Wx.|.xW/) {
if (ERROR("SPACING", if (ERROR("SPACING",
"spaces prohibited around that '$op' $at\n" . $hereptr)) { "spaces prohibited around that '$op' $at\n" . $hereptr)) {
$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
$line_fixed = 1;
if (defined $fix_elements[$n + 2]) { if (defined $fix_elements[$n + 2]) {
$fix_elements[$n + 2] =~ s/^\s+//; $fix_elements[$n + 2] =~ s/^\s+//;
} }
$line_fixed = 1;
} }
} }
...@@ -2914,8 +2931,9 @@ sub process { ...@@ -2914,8 +2931,9 @@ sub process {
if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
if (ERROR("SPACING", if (ERROR("SPACING",
"space required after that '$op' $at\n" . $hereptr)) { "space required after that '$op' $at\n" . $hereptr)) {
$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " "; $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
$line_fixed = 1; $line_fixed = 1;
$last_after = $n;
} }
} }
...@@ -2932,8 +2950,10 @@ sub process { ...@@ -2932,8 +2950,10 @@ sub process {
if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
if (ERROR("SPACING", if (ERROR("SPACING",
"space required before that '$op' $at\n" . $hereptr)) { "space required before that '$op' $at\n" . $hereptr)) {
$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); if ($n != $last_after + 2) {
$line_fixed = 1; $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
$line_fixed = 1;
}
} }
} }
if ($op eq '*' && $cc =~/\s*$Modifier\b/) { if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
...@@ -2942,12 +2962,11 @@ sub process { ...@@ -2942,12 +2962,11 @@ sub process {
} elsif ($ctx =~ /.xW/) { } elsif ($ctx =~ /.xW/) {
if (ERROR("SPACING", if (ERROR("SPACING",
"space prohibited after that '$op' $at\n" . $hereptr)) { "space prohibited after that '$op' $at\n" . $hereptr)) {
$fixed_line =~ s/\s+$//; $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
$line_fixed = 1;
if (defined $fix_elements[$n + 2]) { if (defined $fix_elements[$n + 2]) {
$fix_elements[$n + 2] =~ s/^\s+//; $fix_elements[$n + 2] =~ s/^\s+//;
} }
$line_fixed = 1;
} }
} }
...@@ -2956,8 +2975,7 @@ sub process { ...@@ -2956,8 +2975,7 @@ sub process {
if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
if (ERROR("SPACING", if (ERROR("SPACING",
"space required one side of that '$op' $at\n" . $hereptr)) { "space required one side of that '$op' $at\n" . $hereptr)) {
$fixed_line =~ s/\s+$//; $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
$line_fixed = 1; $line_fixed = 1;
} }
} }
...@@ -2965,20 +2983,18 @@ sub process { ...@@ -2965,20 +2983,18 @@ sub process {
($ctx =~ /Wx./ && $cc =~ /^;/)) { ($ctx =~ /Wx./ && $cc =~ /^;/)) {
if (ERROR("SPACING", if (ERROR("SPACING",
"space prohibited before that '$op' $at\n" . $hereptr)) { "space prohibited before that '$op' $at\n" . $hereptr)) {
$fixed_line =~ s/\s+$//; $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
$line_fixed = 1; $line_fixed = 1;
} }
} }
if ($ctx =~ /ExW/) { if ($ctx =~ /ExW/) {
if (ERROR("SPACING", if (ERROR("SPACING",
"space prohibited after that '$op' $at\n" . $hereptr)) { "space prohibited after that '$op' $at\n" . $hereptr)) {
$fixed_line =~ s/\s+$//; $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
$line_fixed = 1;
if (defined $fix_elements[$n + 2]) { if (defined $fix_elements[$n + 2]) {
$fix_elements[$n + 2] =~ s/^\s+//; $fix_elements[$n + 2] =~ s/^\s+//;
} }
$line_fixed = 1;
} }
} }
...@@ -2992,8 +3008,10 @@ sub process { ...@@ -2992,8 +3008,10 @@ sub process {
if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
if (ERROR("SPACING", if (ERROR("SPACING",
"need consistent spacing around '$op' $at\n" . $hereptr)) { "need consistent spacing around '$op' $at\n" . $hereptr)) {
$fixed_line =~ s/\s+$//; $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; if (defined $fix_elements[$n + 2]) {
$fix_elements[$n + 2] =~ s/^\s+//;
}
$line_fixed = 1; $line_fixed = 1;
} }
} }
...@@ -3004,7 +3022,7 @@ sub process { ...@@ -3004,7 +3022,7 @@ sub process {
if ($ctx =~ /Wx./) { if ($ctx =~ /Wx./) {
if (ERROR("SPACING", if (ERROR("SPACING",
"space prohibited before that '$op' $at\n" . $hereptr)) { "space prohibited before that '$op' $at\n" . $hereptr)) {
$good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
$line_fixed = 1; $line_fixed = 1;
} }
} }
...@@ -3031,8 +3049,10 @@ sub process { ...@@ -3031,8 +3049,10 @@ sub process {
if ($ok == 0) { if ($ok == 0) {
if (ERROR("SPACING", if (ERROR("SPACING",
"spaces required around that '$op' $at\n" . $hereptr)) { "spaces required around that '$op' $at\n" . $hereptr)) {
$good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
$good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " "; if (defined $fix_elements[$n + 2]) {
$fix_elements[$n + 2] =~ s/^\s+//;
}
$line_fixed = 1; $line_fixed = 1;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment