]> xenbits.xensource.com Git - libvirt.git/commitdiff
bracket-spacing: Add syntax-check for unnecessary curly brackets
authorMartin Kletzander <mkletzan@redhat.com>
Wed, 12 Nov 2014 16:22:27 +0000 (17:22 +0100)
committerMartin Kletzander <mkletzan@redhat.com>
Fri, 14 Nov 2014 16:13:36 +0000 (17:13 +0100)
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.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
build-aux/bracket-spacing.pl

index 3d3d7d8d481e54e2d7d18b64e8fa837f3b0055e3..5bc96d27500f3d0f19d0be40751e7befa608e219 100755 (executable)
@@ -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>)) {
@@ -161,6 +166,37 @@ foreach my $file (@ARGV) {
             print "$file:$.: $line";
             $ret = 1;
         }
+
+        # One line conditional statements with one line bodies should
+        # not use curly brackets.
+        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) {
+
+            print "Curly brackets around single-line body:\n";
+            print "$file:$cb_linenum-$.:\n$cb_code$line";
+            $ret = 1;
+
+            # There _should_ be no need to reset the values; but to
+            # keep my inner peace...
+            $cb_linenum = 0;
+            $cb_scolon = 0;
+            $cb_code = "";
+        }
     }
     close FILE;
 }