[libvirt] [PATCH v2 22/22] bracket-spacing: Add syntax-check for unnecessary curly brackets

John Ferlan jferlan at redhat.com
Thu Nov 13 23:18:40 UTC 2014



On 11/13/2014 09:37 AM, Martin Kletzander wrote:
> We're looking for three consecutive lines, first one is a if/for/while
> with a condition and start of body, second one is a body with one and
> only semicolon and third is end of the body by itself.
> 

And because of a later ";;" check - one cannot entirely work around this
single colon count...


> Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
> ---
>  build-aux/bracket-spacing.pl | 35 +++++++++++++++++++++++++++++++++++
>  1 file changed, 35 insertions(+)
> 
> diff --git a/build-aux/bracket-spacing.pl b/build-aux/bracket-spacing.pl
> index d178703..eb2d372 100755
> --- a/build-aux/bracket-spacing.pl
> +++ b/build-aux/bracket-spacing.pl
> @@ -27,6 +27,11 @@ my $ret = 0;
>  my $incomment = 0;
> 
>  foreach my $file (@ARGV) {
> +    # Per-file variables for multiline Curly Bracket (cb_) check
> +    my $cb_linenum = 0;
> +    my $cb_code = "";
> +    my $cb_scolon = 0;
> +
>      open FILE, $file;
> 
>      while (defined (my $line = <FILE>)) {
> @@ -153,6 +158,36 @@ foreach my $file (@ARGV) {
>              print "$file:$.: $line";
>              $ret = 1;
>          }
> +
> +        # One line conditional statements with one line bodies should
> +        # not use curly brackets.  We also need to use $line instead $data as

as what?

> +        if ($data =~ /^\s*(if|while|for)\b.*\{$/) {
> +            $cb_linenum = $.;
> +            $cb_code = $line;
> +            $cb_scolon = 0;
> +        }
> +
> +        # We need to check for exactly one semicolon inside the body,
> +        # because empty statements (e.g. with comment only) are
> +        # allowed
> +        if ($cb_linenum == $. - 1 && $data =~ /^[^;]*;[^;]*$/) {
> +            $cb_code .= $line;
> +            $cb_scolon = 1;
> +        }
> +
> +        if ($data =~ /^\s*}\s*$/ &&
> +            $cb_linenum == $. - 2 &&
> +            $cb_scolon) {
> +

[1] Perhaps maybe an additional print here indicating failure is because
of single line condition no need for brackets

> +            print "$file:$cb_linenum-$.:\n$cb_code$line";
> +            $ret = 1;
> +
> +            # There _should_ be no need to reset the values; but to
> +            # keep my inner piece...
> +            $cb_linenum = 0;
> +            $cb_scolon = 0;
> +            $cb_code = "";
> +        }

[1]I have to say the error one gets if they violate this rule:

maint.mk: incorrect whitespace, see HACKING for rules
make: *** [bracket-spacing-check] Error 1

Is rather ambiguous - I was trying to find a way around the
EDIT_NOT_CHANGED issue from 17/22 without changing the macro and when I
saw the message it really didn't seem to convey the message that other
checks seem to convey.  This is not an incorrect whitespace - it's
extraneous/unnecessary brackets (although I do understand the error
comes from the file name - it's just odd to see).

BTW: I *did* find a way around the check, although I know it's not a
proper comment...

-    if (STREQ(doc, doc_edited))
+    if (STREQ(doc, doc_edited)) {
+        // comment
         EDIT_NOT_CHANGED;
+    }

John
>      }
>      close FILE;
>  }
> 




More information about the libvir-list mailing list