Skip to contentSkip to conversation

checkpatch: add --fix option for OPEN_BRACE issues

PATCH In review Dwaipayan Ray 2 participants 9 messages Nov 18, 2020 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: add --fix option for OPEN_BRACE issues

Brace style misuses of the following types are now corrected:

int foo(int bar,

        int baz) { bar++;
    return bar + baz;
}

int foo(int bar,

        int baz) {
    return bar + baz;
}

if (bar &&
baz)

{   bar++;
    baz++;
}

if (bar &&
baz)

{
    bar++;
    baz++;
}
---
 scripts/checkpatch.pl | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
scripts/checkpatch.pl +33 -5
index 0da6422cd0fd..8da6cde20c68 100755
@@ -3937,9 +3937,23 @@ sub process {
 			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
 
 			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
-				ERROR("OPEN_BRACE",
-				      "that open brace { should be on the previous line\n" .
-					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
+				if (ERROR("OPEN_BRACE",
+					  "that open brace { should be on the previous line\n" .
+						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n") &&
+				    $fix) {
+					my $line1 = $rawlines[$ctx_ln - 2];
+					my $line2 = $rawlines[$ctx_ln - 1];
+					fix_delete_line($ctx_ln - 2, $line1);
+					fix_delete_line($ctx_ln - 1, $line2);
+
+					my $fixedline = rtrim($line1) . " {";
+					fix_insert_line($ctx_ln - 1, $fixedline);
+					$fixedline = $line2;
+					$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
+					if ($fixedline !~ /^\+\s*$/) {
+						fix_insert_line($ctx_ln - 1, $fixedline);
+					}
+				}
 			}
 			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
 			    $ctx =~ /\)\s*\;\s*$/ &&
@@ -6659,16 +6673,30 @@ sub process {
 			my $ok = 0;
 			my $cnt = statement_rawlines($stat);
 			my $herectx = $here . "\n";
+			my $cur_ln = $linenr - 1;
 			for (my $n = 0; $n < $cnt; $n++) {
 				my $rl = raw_line($linenr, $n);
 				$herectx .=  $rl . "\n";
 				$ok = 1 if ($rl =~ /^[ \+]\{/);
 				$ok = 1 if ($rl =~ /\{/ && $n == 0);
 				last if $rl =~ /^[ \+].*\{/;
+				$cur_ln++;
 			}
 			if (!$ok) {
-				ERROR("OPEN_BRACE",
-				      "open brace '{' following function definitions go on the next line\n" . $herectx);
+				if (ERROR("OPEN_BRACE",
+					  "open brace '{' following function definitions go on the next line\n" . $herectx) &&
+				    $fix && $rawlines[$cur_ln] =~ /^\+/) {
+					fix_delete_line($cur_ln, $rawlines[$cur_ln]);
+					my $fixed_line = $rawlines[$cur_ln];
+					$fixed_line =~ /(^.*\)\s*)\{(.*)$/;
+					my $line1 = rtrim($1);
+					my $line2 = $2;
+					fix_insert_line($cur_ln, $line1);
+					fix_insert_line($cur_ln, "\+{");
+					if ($line2 !~ /^\s*$/) {
+						fix_insert_line($cur_ln, "\+\t" . trim($line2));
+					}
+				}
 			}
 		}
 
Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 2 lines · Dwaipayan Ray

Brace style misuses of the following types are now corrected:

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 3944

Quoted 13 lines
scripts/checkpatch.pl +0 -0
@@ -3937,9 +3937,23 @@ sub process {
  			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";

  if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {

scripts/checkpatch.pl +5 -3
-				ERROR("OPEN_BRACE",
-				      "that open brace { should be on the previous line\n" .
-					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
+				if (ERROR("OPEN_BRACE",
+					  "that open brace { should be on the previous line\n" .
+						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n") &&
+				    $fix) {
+					my $line1 = $rawlines[$ctx_ln - 2];

How are you sure that in a patch context this line always starts with /^\+/ ?

Quoted 12 lines
scripts/checkpatch.pl +12 -0
+					my $line2 = $rawlines[$ctx_ln - 1];
+					fix_delete_line($ctx_ln - 2, $line1);
+					fix_delete_line($ctx_ln - 1, $line2);
+
+					my $fixedline = rtrim($line1) . " {";
+					fix_insert_line($ctx_ln - 1, $fixedline);
+					$fixedline = $line2;
+					$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
+					if ($fixedline !~ /^\+\s*$/) {
+						fix_insert_line($ctx_ln - 1, $fixedline);
+					}
+				}
#2 lore raw
Quoted 11 lines · Joe Perches
Quoted 2 lines · Dwaipayan Ray

Brace style misuses of the following types are now corrected:

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 4 lines
scripts/checkpatch.pl +0 -0
@@ -3937,9 +3937,23 @@ sub process {
                      #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
 
 
Quoted 11 lines

scripts/checkpatch.pl sub process { · line 3944

Quoted 9 lines
scripts/checkpatch.pl +5 -3
                      if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
-                             ERROR("OPEN_BRACE",
-                                   "that open brace { should be on the previous line\n" .
-                                     "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
+                             if (ERROR("OPEN_BRACE",
+                                       "that open brace { should be on the previous line\n" .
+                                             "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n") &&
+                                 $fix) {
+                                     my $line1 = $rawlines[$ctx_ln - 2];

How are you sure that in a patch context this line always starts with /^\+/ ?

Hi,
I followed it from the other fixes for OPEN_BRACE which were already there. In the patch context if the lines are added then only I think the fix should be triggered. Other instances should not be modified.

Thanks,
Dwaipayan.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

maintainer
#3 lore raw
Quoted 24 lines · Dwaipayan Ray
Quoted 11 lines · Joe Perches
Quoted 2 lines · Dwaipayan Ray

Brace style misuses of the following types are now corrected:

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 4 lines
scripts/checkpatch.pl +0 -0
@@ -3937,9 +3937,23 @@ sub process {
                       #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
 
 
Quoted 11 lines

scripts/checkpatch.pl sub process { · line 3944

Quoted 9 lines
scripts/checkpatch.pl +5 -3
                       if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
-                             ERROR("OPEN_BRACE",
-                                   "that open brace { should be on the previous line\n" .
-                                     "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
+                             if (ERROR("OPEN_BRACE",
+                                       "that open brace { should be on the previous line\n" .
+                                             "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n") &&
+                                 $fix) {
+                                     my $line1 = $rawlines[$ctx_ln - 2];

How are you sure that in a patch context this line always starts with /^\+/ ?

Quoted 4 lines

Hi,
I followed it from the other fixes for OPEN_BRACE which were already there. In the patch context if the lines are added then only I think the fix should be triggered. Other instances should not be modified.

As far as I know there are no existing uses of --fix with OPEN_BRACE.

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#4 lore raw
Quoted 26 lines · Joe Perches
Quoted 24 lines · Dwaipayan Ray
Quoted 22 lines · Joe Perches
Quoted 2 lines · Dwaipayan Ray

Brace style misuses of the following types are now corrected:

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 3945

Quoted 13 lines
scripts/checkpatch.pl +5 -3
@@ -3937,9 +3937,23 @@ sub process {
                      #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
 
 
                      if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
-                             ERROR("OPEN_BRACE",
-                                   "that open brace { should be on the previous line\n" .
-                                     "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
+                             if (ERROR("OPEN_BRACE",
+                                       "that open brace { should be on the previous line\n" .
+                                             "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n") &&
+                                 $fix) {
+                                     my $line1 = $rawlines[$ctx_ln - 2];

How are you sure that in a patch context this line always starts with /^\+/ ?

Quoted 7 lines
Quoted 4 lines

Hi,
I followed it from the other fixes for OPEN_BRACE which were already there. In the patch context if the lines are added then only I think the fix should be triggered. Other instances should not be modified.

As far as I know there are no existing uses of --fix with OPEN_BRACE.

I think you added it via 8d1824780f2f1 ("checkpatch: add --fix option for a couple OPEN_BRACE misuses")

Thanks,
Dwaipayan.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

maintainer
#5 lore raw
Quoted 32 lines · Dwaipayan Ray
Quoted 31 lines · Joe Perches
Quoted 28 lines · Dwaipayan Ray
Quoted 11 lines · Joe Perches
Quoted 2 lines · Dwaipayan Ray

Brace style misuses of the following types are now corrected:

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 4 lines
scripts/checkpatch.pl +0 -0
@@ -3937,9 +3937,23 @@ sub process {
                       #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
 
 
Quoted 11 lines

scripts/checkpatch.pl sub process { · line 3944

Quoted 9 lines
scripts/checkpatch.pl +5 -3
                       if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
-                             ERROR("OPEN_BRACE",
-                                   "that open brace { should be on the previous line\n" .
-                                     "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
+                             if (ERROR("OPEN_BRACE",
+                                       "that open brace { should be on the previous line\n" .
+                                             "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n") &&
+                                 $fix) {
+                                     my $line1 = $rawlines[$ctx_ln - 2];

How are you sure that in a patch context this line always starts with /^\+/ ?

Hi,
I followed it from the other fixes for OPEN_BRACE which were already there. In the patch context if the lines are added then only I think the fix should be triggered. Other instances should not be modified.

Quoted 5 lines
Quoted 2 lines

As far as I know there are no existing uses of --fix with OPEN_BRACE.

I think you added it via 8d1824780f2f1 ("checkpatch: add --fix option for a couple OPEN_BRACE misuses")

The difference here is that you are dealing with a $stat context and the existing --fix entries are just for single line fixes.

#6 lore raw
Quoted 34 lines · Joe Perches
Quoted 32 lines · Dwaipayan Ray
Quoted 31 lines · Joe Perches
Quoted 28 lines · Dwaipayan Ray
Quoted 11 lines · Joe Perches
Quoted 2 lines · Dwaipayan Ray

Brace style misuses of the following types are now corrected:

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 4 lines
scripts/checkpatch.pl +0 -0
@@ -3937,9 +3937,23 @@ sub process {
                      #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
 
 
Quoted 11 lines

scripts/checkpatch.pl sub process { · line 3944

Quoted 9 lines
scripts/checkpatch.pl +5 -3
                      if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
-                             ERROR("OPEN_BRACE",
-                                   "that open brace { should be on the previous line\n" .
-                                     "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
+                             if (ERROR("OPEN_BRACE",
+                                       "that open brace { should be on the previous line\n" .
+                                             "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n") &&
+                                 $fix) {
+                                     my $line1 = $rawlines[$ctx_ln - 2];

How are you sure that in a patch context this line always starts with /^\+/ ?

Hi,
I followed it from the other fixes for OPEN_BRACE which were already there. In the patch context if the lines are added then only I think the fix should be triggered. Other instances should not be modified.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

As far as I know there are no existing uses of --fix with OPEN_BRACE.

I think you added it via 8d1824780f2f1 ("checkpatch: add --fix option for a couple OPEN_BRACE misuses")

The difference here is that you are dealing with a $stat context and the existing --fix entries are just for single line fixes.

Hi,
Ya I understand that. Though I am dealing with $stat content, I am also directly accessing $rawlines here.
So I think that should have the proper patch line format, starting with + or - or so.

So in this case if the error is triggered, checking for /^+/ should be done becase it would be wrong to fix the others with /^[- ]/

Is there something else that I am not getting here?

Thanks,
Dwaipayan.

maintainer
#7 lore raw
Quoted 47 lines · Dwaipayan Ray
Quoted 30 lines · Joe Perches
Quoted 29 lines · Dwaipayan Ray
Quoted 28 lines · Joe Perches
Quoted 26 lines · Dwaipayan Ray
Quoted 9 lines · Joe Perches
Quoted 2 lines · Dwaipayan Ray

Brace style misuses of the following types are now corrected:

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 3 lines
scripts/checkpatch.pl +0 -0
@@ -3937,9 +3937,23 @@ sub process {
                       #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
 
Quoted 11 lines

scripts/checkpatch.pl sub process { · line 3944

Quoted 9 lines
scripts/checkpatch.pl +5 -3
                       if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
-                             ERROR("OPEN_BRACE",
-                                   "that open brace { should be on the previous line\n" .
-                                     "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
+                             if (ERROR("OPEN_BRACE",
+                                       "that open brace { should be on the previous line\n" .
+                                             "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n") &&
+                                 $fix) {
+                                     my $line1 = $rawlines[$ctx_ln - 2];

How are you sure that in a patch context this line always starts with /^\+/ ?

Hi,
I followed it from the other fixes for OPEN_BRACE which were already there. In the patch context if the lines are added then only I think the fix should be triggered. Other instances should not be modified.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

As far as I know there are no existing uses of --fix with OPEN_BRACE.

I think you added it via 8d1824780f2f1 ("checkpatch: add --fix option for a couple OPEN_BRACE misuses")

The difference here is that you are dealing with a $stat context and the existing --fix entries are just for single line fixes.

Hi,
Ya I understand that. Though I am dealing with $stat content, I am also directly accessing $rawlines here.
So I think that should have the proper patch line format, starting with + or - or so.

Quoted 4 lines

So in this case if the error is triggered, checking for /^+/ should be done becase it would be wrong to fix the others with /^[- ]/

Is there something else that I am not getting here?

$stat does not include lines that are skipped if the lines start with -

Patch context may be:

line content

1 func(...
2 - original arguments);
3 + changed);

where $stat does not include the 'original arguments' changed line

	func(...,
	     changed);

but the $rawlines[] entries are consecutive.

Anyway, this needs to be handled very carefully if handled at all.

I think it's easier to avoid handling these cases and let the patch submitter fix it manually if appropriate.

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#8 lore raw
Quoted 30 lines
Quoted 10 lines
Quoted 3 lines

The difference here is that you are dealing with a $stat context and the existing --fix entries are just for single line fixes.

Hi,
Ya I understand that. Though I am dealing with $stat content, I am also directly accessing $rawlines here.
So I think that should have the proper patch line format, starting with + or - or so.

Quoted 4 lines

So in this case if the error is triggered, checking for /^+/ should be done becase it would be wrong to fix the others with /^[- ]/

Is there something else that I am not getting here?

$stat does not include lines that are skipped if the lines start with -

Patch context may be:

line content

1 func(...

2       -            original arguments);
3       +            changed);

where $stat does not include the 'original arguments' changed line

        func(...,
             changed);
Quoted 7 lines

but the $rawlines[] entries are consecutive.

Anyway, this needs to be handled very carefully if handled at all.

I think it's easier to avoid handling these cases and let the patch submitter fix it manually if appropriate.

Thanks. I get your point now. It seems it's much more complex than I thought it to be. I will avoid working on this one then.

Thanks & Regards,
Dwaipayan.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees