qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PULL v2 06/44] checkpatch: Brace handling on multi-line co


From: Stefan Hajnoczi
Subject: [Qemu-devel] [PULL v2 06/44] checkpatch: Brace handling on multi-line condition
Date: Mon, 12 Jan 2015 16:39:56 +0000

From: Max Reitz <address@hidden>

CODING_STYLE states the following about braces around blocks:

> The opening brace is on the line that contains the control flow
> statement that introduces the new block; [...]

This is obviously impossible with multi-line conditions. Therefore,
CODING_STYLE does not make any clear statement about where to put the
opening brace after a multi-line condition.

There is a reason to prefer to place the opening brace on an own line
after such a condition while still placing it on the same line as the
"control flow statement" if possible; that reason is that the last line
of a multi-line condition is indented, in the case of "if", it is often
indented by four spaces, just as much as the first statement in the
block will be indented. This is hard to read as there is no clearly
visible distinction between condition and block. Placing the opening
brace on a separate line solves this issue.

Also, there are cases where placing the opening brace on a separate line
is the only viable option; if the previous line had nearly 80 characters
and splitting it is not desirable, the opening brace is naturally placed
on an own line.

This patch fixes checkpatch.pl to not complain about braces on own lines
if the condition introducing the block spanned more than one line, or if
the previous line had 79 or 80 characters.

Furthermore, the warning about not having braces around a block is fixed
to mind braces not being on the last line of the condition.

Signed-off-by: Max Reitz <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
 scripts/checkpatch.pl | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 053e432..5df61f9 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1639,7 +1639,13 @@ sub process {
                        #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
                        #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*{/) {
+                       # The length of the "previous line" is checked against 
80 because it
+                       # includes the + at the beginning of the line (if the 
actual line has
+                       # 79 or 80 characters, it is no longer possible to add 
a space and an
+                       # opening brace there)
+                       if ($#ctx == 0 && $ctx !~ /{\s*/ &&
+                           defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] 
=~ /^\+\s*{/ &&
+                           defined($lines[$ctx_ln - 2]) && 
length($lines[$ctx_ln - 2]) < 80) {
                                ERROR("that open brace { should be on the 
previous line\n" .
                                        "$here\n$ctx\n$rawlines[$ctx_ln - 
1]\n");
                        }
@@ -2542,7 +2548,10 @@ sub process {
 
                                        substr($block, 0, length($cond), '');
 
-                                       $seen++ if ($block =~ /^\s*{/);
+                                       my $spaced_block = $block;
+                                       $spaced_block =~ s/\n\+/ /g;
+
+                                       $seen++ if ($spaced_block =~ /^\s*{/);
 
                                         print "APW: cond<$cond> block<$block> 
allowed<$allowed>\n"
                                             if $dbg_adv_apw;
-- 
2.1.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]