From: Dwaipayan Ray <dwaipayanray1@gmail.com>
To: joe@perches.com
Cc: linux-kernel-mentees@lists.linuxfoundation.org,
dwaipayanray1@gmail.com, linux-kernel@vger.kernel.org,
lukas.bulwahn@gmail.com
Subject: [PATCH] checkpatch: add --fix option for OPEN_BRACE issues
Date: Wed, 18 Nov 2020 18:10:35 +0530 [thread overview]
Message-ID: <20201118124035.96976-1-dwaipayanray1@gmail.com> (raw)
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 <dwaipayanray1@gmail.com>
---
scripts/checkpatch.pl | 38 +++++++++++++++++++++++++++++++++-----
1 file changed, 33 insertions(+), 5 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 0da6422cd0fd..8da6cde20c68 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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));
+ }
+ }
}
}
--
2.27.0
next reply other threads:[~2020-11-18 12:41 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-18 12:40 Dwaipayan Ray [this message]
2020-11-18 18:14 ` Joe Perches
2020-11-18 18:33 ` Dwaipayan Ray
2020-11-18 18:39 ` Joe Perches
2020-11-18 18:45 ` Dwaipayan Ray
2020-11-18 19:58 ` Joe Perches
2020-11-18 20:22 ` Dwaipayan Ray
2020-11-18 20:39 ` Joe Perches
2020-11-18 20:52 ` Dwaipayan Ray
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201118124035.96976-1-dwaipayanray1@gmail.com \
--to=dwaipayanray1@gmail.com \
--cc=joe@perches.com \
--cc=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lukas.bulwahn@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®