diff mbox

[RFC] scripts/checkpatch.pl: Bug fix

Message ID 1521114316-19980-1-git-send-email-suhang16@mails.ucas.ac.cn (mailing list archive)
State New, archived
Headers show

Commit Message

Su Hang March 15, 2018, 11:45 a.m. UTC
Bug fix: checkpatch.pl stops complaining about following pattern:
"""
do {
    //do somethins;
} while (conditions);
"""

Two things need to be mentioned:
1) Before I casue this bug, checkpatch.pl will raise a wrong
complain:
"""
ERROR: braces {} are necessary even for single statement blocks
+    for (i == 0; i < 0; ++i)
+    {
+        ;
+    } else
"""
In this patch, this wrong complain get fixed.

2) Becasue all(`if`, `while`, `for`) check have been done in this
`if` block(Line: 2356), and this block contains following statement:
""" Line: 2379
$suppress_ifbraces{$ln + $offset} = 1;
"""
So the block after this block may never run:
""" Line: 2415
if (!defined $suppress_ifbraces{$linenr - 1} &&
	$line =~ /\b(if|while|for|else)\b/ &&
	$line !~ /\#\s*if/ &&
	$line !~ /\#\s*else/) {
"""
But I think, maybe it's ok, becasue all check has been done by
up block. I'm not sure, please give me some advice.

Signed-off-by: Su Hang <suhang16@mails.ucas.ac.cn>
---
 scripts/checkpatch.pl | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

--
2.7.4

Comments

Peter Maydell March 15, 2018, 11:50 a.m. UTC | #1
On 15 March 2018 at 11:45, Su Hang <suhang16@mails.ucas.ac.cn> wrote:
> Bug fix: checkpatch.pl stops complaining about following pattern:
> """
> do {
>     //do somethins;
> } while (conditions);
> """
>
> Two things need to be mentioned:
> 1) Before I casue this bug, checkpatch.pl will raise a wrong
> complain:
> """
> ERROR: braces {} are necessary even for single statement blocks
> +    for (i == 0; i < 0; ++i)
> +    {
> +        ;
> +    } else
> """

This is a bit of an odd example -- for() loops don't
take "else" clauses, so what is the 'else' doing here?

(Also, for QEMU style the "{" should be on the same line as the for(),
not on a line of its own.)

thanks
-- PMM
Su Hang March 15, 2018, 12:06 p.m. UTC | #2
Sorry about inappropriate example, what I want to express is,
when checkpatch.pl find pattern like this:
"""
for (i = 0; i < 0; ++i)
{
    ;
}
"""
It should raise
"""
ERROR: that open brace { should be on the previous line
#4: FILE: test.c:4:
+    for (i = 0; i < 0; ++i)
+    {
"""

Instead of:
""" 
ERROR: braces {} are necessary even for single statement blocks
#4: FILE: test.c:4:
+    for (i = 0; i < 0; ++i)
+    {
+        ;
+    }
"""

> -----Original Messages-----
> From: "Peter Maydell" <peter.maydell@linaro.org>
> Sent Time: 2018-03-15 19:50:55 (Thursday)
> To: "Su Hang" <suhang16@mails.ucas.ac.cn>
> Cc: "Eric Blake" <eblake@redhat.com>, "Vladimir Sementsov-Ogievskiy" <vsementsov@virtuozzo.com>, "QEMU Developers" <qemu-devel@nongnu.org>
> Subject: Re: [Qemu-devel] [PATCH RFC] scripts/checkpatch.pl: Bug fix
> 
> On 15 March 2018 at 11:45, Su Hang <suhang16@mails.ucas.ac.cn> wrote:
> > Bug fix: checkpatch.pl stops complaining about following pattern:
> > """
> > do {
> >     //do somethins;
> > } while (conditions);
> > """
> >
> > Two things need to be mentioned:
> > 1) Before I casue this bug, checkpatch.pl will raise a wrong
> > complain:
> > """
> > ERROR: braces {} are necessary even for single statement blocks
> > +    for (i == 0; i < 0; ++i)
> > +    {
> > +        ;
> > +    } else
> > """
> 
> This is a bit of an odd example -- for() loops don't
> take "else" clauses, so what is the 'else' doing here?
> 
> (Also, for QEMU style the "{" should be on the same line as the for(),
> not on a line of its own.)
> 
> thanks
> -- PMM
diff mbox

Patch

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index d1fe79bcc47c..2ca833f22e5a 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2355,6 +2355,18 @@  sub process {
 # check for missing bracing around if etc
 		if ($line =~ /(^.*)\b(?:if|while|for)\b/ &&
 			$line !~ /\#\s*if/) {
+			my $allowed = 0;
+
+			# Check the pre-context.
+			if ($line =~ /(\}.*?)$/) {
+				my $pre = $1;
+
+				if ($line !~ /else/) {
+					print "APW: ALLOWED: pre<$pre> line<$line>\n"
+						if $dbg_adv_apw;
+					$allowed = 1;
+				}
+			}
 			my ($level, $endln, @chunks) =
 				ctx_statement_full($linenr, $realcnt, 1);
                         if ($dbg_adv_apw) {
@@ -2363,7 +2375,6 @@  sub process {
                                 if $#chunks >= 1;
                         }
 			if ($#chunks >= 0 && $level == 0) {
-				my $allowed = 0;
 				my $seen = 0;
 				my $herectx = $here . "\n";
 				my $ln = $linenr - 1;
@@ -2407,7 +2418,7 @@  sub process {
                                             $allowed = 1;
 					}
 				}
-				if ($seen != ($#chunks + 1)) {
+				if ($seen != ($#chunks + 1) && !$allowed) {
 					ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
 				}
 			}