diff options
author | Joe Perches <joe@perches.com> | 2013-09-11 17:24:01 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-09-11 18:58:48 -0400 |
commit | b34c648bb33ca143b98851fd7fe7250f1875c463 (patch) | |
tree | f3e531f96906eb951677147daf8b15cdbde3a309 /scripts/checkpatch.pl | |
parent | f95a7e6a462ed1338bd576ccb557ff86772a0776 (diff) |
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: Joe Perches <joe@perches.com>
Cc: Phil Carmody <phil.carmody@partner.samsung.com>
Cc: Andy Whitcroft <apw@canonical.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-x | scripts/checkpatch.pl | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index db7778a8f414..e53df2b086b2 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -1472,7 +1472,23 @@ sub check_absolute_file { | |||
1472 | sub trim { | 1472 | sub trim { |
1473 | my ($string) = @_; | 1473 | my ($string) = @_; |
1474 | 1474 | ||
1475 | $string =~ s/(^\s+|\s+$)//g; | 1475 | $string =~ s/^\s+|\s+$//g; |
1476 | |||
1477 | return $string; | ||
1478 | } | ||
1479 | |||
1480 | sub ltrim { | ||
1481 | my ($string) = @_; | ||
1482 | |||
1483 | $string =~ s/^\s+//; | ||
1484 | |||
1485 | return $string; | ||
1486 | } | ||
1487 | |||
1488 | sub rtrim { | ||
1489 | my ($string) = @_; | ||
1490 | |||
1491 | $string =~ s/\s+$//; | ||
1476 | 1492 | ||
1477 | return $string; | 1493 | return $string; |
1478 | } | 1494 | } |
@@ -2821,6 +2837,7 @@ sub process { | |||
2821 | $off = 0; | 2837 | $off = 0; |
2822 | 2838 | ||
2823 | my $blank = copy_spacing($opline); | 2839 | my $blank = copy_spacing($opline); |
2840 | my $last_after = -1; | ||
2824 | 2841 | ||
2825 | for (my $n = 0; $n < $#elements; $n += 2) { | 2842 | for (my $n = 0; $n < $#elements; $n += 2) { |
2826 | 2843 | ||
@@ -2886,7 +2903,7 @@ sub process { | |||
2886 | $cc !~ /^\\/ && $cc !~ /^;/) { | 2903 | $cc !~ /^\\/ && $cc !~ /^;/) { |
2887 | if (ERROR("SPACING", | 2904 | if (ERROR("SPACING", |
2888 | "space required after that '$op' $at\n" . $hereptr)) { | 2905 | "space required after that '$op' $at\n" . $hereptr)) { |
2889 | $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; | 2906 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
2890 | $line_fixed = 1; | 2907 | $line_fixed = 1; |
2891 | } | 2908 | } |
2892 | } | 2909 | } |
@@ -2901,11 +2918,11 @@ sub process { | |||
2901 | if ($ctx =~ /Wx.|.xW/) { | 2918 | if ($ctx =~ /Wx.|.xW/) { |
2902 | if (ERROR("SPACING", | 2919 | if (ERROR("SPACING", |
2903 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { | 2920 | "spaces prohibited around that '$op' $at\n" . $hereptr)) { |
2904 | $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); | 2921 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
2905 | $line_fixed = 1; | ||
2906 | if (defined $fix_elements[$n + 2]) { | 2922 | if (defined $fix_elements[$n + 2]) { |
2907 | $fix_elements[$n + 2] =~ s/^\s+//; | 2923 | $fix_elements[$n + 2] =~ s/^\s+//; |
2908 | } | 2924 | } |
2925 | $line_fixed = 1; | ||
2909 | } | 2926 | } |
2910 | } | 2927 | } |
2911 | 2928 | ||
@@ -2914,8 +2931,9 @@ sub process { | |||
2914 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { | 2931 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
2915 | if (ERROR("SPACING", | 2932 | if (ERROR("SPACING", |
2916 | "space required after that '$op' $at\n" . $hereptr)) { | 2933 | "space required after that '$op' $at\n" . $hereptr)) { |
2917 | $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " "; | 2934 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
2918 | $line_fixed = 1; | 2935 | $line_fixed = 1; |
2936 | $last_after = $n; | ||
2919 | } | 2937 | } |
2920 | } | 2938 | } |
2921 | 2939 | ||
@@ -2932,8 +2950,10 @@ sub process { | |||
2932 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { | 2950 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
2933 | if (ERROR("SPACING", | 2951 | if (ERROR("SPACING", |
2934 | "space required before that '$op' $at\n" . $hereptr)) { | 2952 | "space required before that '$op' $at\n" . $hereptr)) { |
2935 | $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); | 2953 | if ($n != $last_after + 2) { |
2936 | $line_fixed = 1; | 2954 | $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); |
2955 | $line_fixed = 1; | ||
2956 | } | ||
2937 | } | 2957 | } |
2938 | } | 2958 | } |
2939 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { | 2959 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
@@ -2942,12 +2962,11 @@ sub process { | |||
2942 | } elsif ($ctx =~ /.xW/) { | 2962 | } elsif ($ctx =~ /.xW/) { |
2943 | if (ERROR("SPACING", | 2963 | if (ERROR("SPACING", |
2944 | "space prohibited after that '$op' $at\n" . $hereptr)) { | 2964 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
2945 | $fixed_line =~ s/\s+$//; | 2965 | $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); |
2946 | $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); | ||
2947 | $line_fixed = 1; | ||
2948 | if (defined $fix_elements[$n + 2]) { | 2966 | if (defined $fix_elements[$n + 2]) { |
2949 | $fix_elements[$n + 2] =~ s/^\s+//; | 2967 | $fix_elements[$n + 2] =~ s/^\s+//; |
2950 | } | 2968 | } |
2969 | $line_fixed = 1; | ||
2951 | } | 2970 | } |
2952 | } | 2971 | } |
2953 | 2972 | ||
@@ -2956,8 +2975,7 @@ sub process { | |||
2956 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { | 2975 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
2957 | if (ERROR("SPACING", | 2976 | if (ERROR("SPACING", |
2958 | "space required one side of that '$op' $at\n" . $hereptr)) { | 2977 | "space required one side of that '$op' $at\n" . $hereptr)) { |
2959 | $fixed_line =~ s/\s+$//; | 2978 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; |
2960 | $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " "; | ||
2961 | $line_fixed = 1; | 2979 | $line_fixed = 1; |
2962 | } | 2980 | } |
2963 | } | 2981 | } |
@@ -2965,20 +2983,18 @@ sub process { | |||
2965 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { | 2983 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
2966 | if (ERROR("SPACING", | 2984 | if (ERROR("SPACING", |
2967 | "space prohibited before that '$op' $at\n" . $hereptr)) { | 2985 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
2968 | $fixed_line =~ s/\s+$//; | 2986 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
2969 | $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); | ||
2970 | $line_fixed = 1; | 2987 | $line_fixed = 1; |
2971 | } | 2988 | } |
2972 | } | 2989 | } |
2973 | if ($ctx =~ /ExW/) { | 2990 | if ($ctx =~ /ExW/) { |
2974 | if (ERROR("SPACING", | 2991 | if (ERROR("SPACING", |
2975 | "space prohibited after that '$op' $at\n" . $hereptr)) { | 2992 | "space prohibited after that '$op' $at\n" . $hereptr)) { |
2976 | $fixed_line =~ s/\s+$//; | 2993 | $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); |
2977 | $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); | ||
2978 | $line_fixed = 1; | ||
2979 | if (defined $fix_elements[$n + 2]) { | 2994 | if (defined $fix_elements[$n + 2]) { |
2980 | $fix_elements[$n + 2] =~ s/^\s+//; | 2995 | $fix_elements[$n + 2] =~ s/^\s+//; |
2981 | } | 2996 | } |
2997 | $line_fixed = 1; | ||
2982 | } | 2998 | } |
2983 | } | 2999 | } |
2984 | 3000 | ||
@@ -2992,8 +3008,10 @@ sub process { | |||
2992 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { | 3008 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
2993 | if (ERROR("SPACING", | 3009 | if (ERROR("SPACING", |
2994 | "need consistent spacing around '$op' $at\n" . $hereptr)) { | 3010 | "need consistent spacing around '$op' $at\n" . $hereptr)) { |
2995 | $fixed_line =~ s/\s+$//; | 3011 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
2996 | $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; | 3012 | if (defined $fix_elements[$n + 2]) { |
3013 | $fix_elements[$n + 2] =~ s/^\s+//; | ||
3014 | } | ||
2997 | $line_fixed = 1; | 3015 | $line_fixed = 1; |
2998 | } | 3016 | } |
2999 | } | 3017 | } |
@@ -3004,7 +3022,7 @@ sub process { | |||
3004 | if ($ctx =~ /Wx./) { | 3022 | if ($ctx =~ /Wx./) { |
3005 | if (ERROR("SPACING", | 3023 | if (ERROR("SPACING", |
3006 | "space prohibited before that '$op' $at\n" . $hereptr)) { | 3024 | "space prohibited before that '$op' $at\n" . $hereptr)) { |
3007 | $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); | 3025 | $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); |
3008 | $line_fixed = 1; | 3026 | $line_fixed = 1; |
3009 | } | 3027 | } |
3010 | } | 3028 | } |
@@ -3031,8 +3049,10 @@ sub process { | |||
3031 | if ($ok == 0) { | 3049 | if ($ok == 0) { |
3032 | if (ERROR("SPACING", | 3050 | if (ERROR("SPACING", |
3033 | "spaces required around that '$op' $at\n" . $hereptr)) { | 3051 | "spaces required around that '$op' $at\n" . $hereptr)) { |
3034 | $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; | 3052 | $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; |
3035 | $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " "; | 3053 | if (defined $fix_elements[$n + 2]) { |
3054 | $fix_elements[$n + 2] =~ s/^\s+//; | ||
3055 | } | ||
3036 | $line_fixed = 1; | 3056 | $line_fixed = 1; |
3037 | } | 3057 | } |
3038 | } | 3058 | } |