checkpatch: add --fix option for OPEN_BRACE issues
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
@@ -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 · 6 lines
2.27.0 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 20 lines · Dwaipayan Ray
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++; }
Quoted 8 lines
if (bar &&
baz){ bar++; baz++; }Signed-off-by Dwaipayan Ray <[email protected]>
Looks good to me. How did you test this? Did you run on a couple of files in the kernel repository?
Quoted 69 lines
--- scripts/checkpatch.pl | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-)scripts/checkpatch.pl +33
-6@@ -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)); + } + } } } --
Quoted 2 lines
2.27.0
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 29 lines · Lukas Bulwahn
Quoted 27 lines · Dwaipayan Ray
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++; }
Quoted 5 lines
Quoted 1 line
Signed-off-by Dwaipayan Ray <[email protected]>Looks good to me. How did you test this? Did you run on a couple of files in the kernel repository?
I have only run on some test cases I made, which also included the style misuses above. Finding files in the kernel for this exact check might take a bit work. Should I do that? I think these custom examples pretty much handles all the cases.
Thanks,
Dwaipayan.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 33 lines · Dwaipayan Ray
Quoted 31 lines · Lukas Bulwahn
Quoted 28 lines · Dwaipayan Ray
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++; }Signed-off-by Dwaipayan Ray <[email protected]>
Quoted 9 lines
Quoted 3 lines
Looks good to me. How did you test this? Did you run on a couple of files in the kernel repository?
I have only run on some test cases I made, which also included the style misuses above. Finding files in the kernel for this exact check might take a bit work. Should I do that? I think these custom examples pretty much handles all the cases.
Well, it might be helpful to also submit some patches improving the code to see if the cases where it would trigger actually fits to the expectation of the maintainers, or if they intentionally have a different style in mind.
I think it is worth running checkpatch to find such cases.
Lukas
Quoted 2 lines
Thanks,
Dwaipayan.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 6 lines
Quoted 5 lines
I have only run on some test cases I made, which also included the style misuses above. Finding files in the kernel for this exact check might take a bit work. Should I do that? I think these custom examples pretty much handles all the cases.
Quoted 7 lines
Well, it might be helpful to also submit some patches improving the code to see if the cases where it would trigger actually fits to the expectation of the maintainers, or if they intentionally have a different style in mind.
I think it is worth running checkpatch to find such cases.
Okay thanks! I will try to find out some of those.
Regards,
Dwaipayan.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 7 lines · Dwaipayan Ray
Quoted 7 lines
Quoted 6 lines
I have only run on some test cases I made, which also included the style misuses above. Finding files in the kernel for this exact check might take a bit work. Should I do that? I think these custom examples pretty much handles all the cases.
Quoted 8 lines
Quoted 7 lines
Well, it might be helpful to also submit some patches improving the code to see if the cases where it would trigger actually fits to the expectation of the maintainers, or if they intentionally have a different style in mind.
I think it is worth running checkpatch to find such cases.
Okay thanks! I will try to find out some of those.
I checked out some conversions over the kernel files and it's working pretty much as intended.
For example infs/f2fs/segment.c:
static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx,
void *data) {
memcpy(data, zone, sizeof(struct blk_zone));was converted to:
static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx,
void *data)
{
memcpy(data, zone, sizeof(struct blk_zone));The brace positioning is correct after the fix.
Another instance in net/netfilter/xt_IDLETIMER.c:
if(ret < 0)
{
pr_debug("checkentry helper return invalid\n");
return -EINVAL;
}was changed to:
if(ret < 0) {
pr_debug("checkentry helper return invalid\n");
return -EINVAL;
}which is again I think the correct fix according to the conventions.
What do you think Lukas?
Thanks,
Dwaipayan.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees