]> xenbits.xensource.com Git - libvirt.git/commitdiff
maint: tighten curly brace syntax checking
authorEric Blake <eblake@redhat.com>
Wed, 3 Sep 2014 22:18:19 +0000 (16:18 -0600)
committerEric Blake <eblake@redhat.com>
Thu, 4 Sep 2014 21:38:00 +0000 (15:38 -0600)
Now that hanging brace offenders have been fixed, we can automate
the check, and document our style.  Done as a separate commit from
code changes, to make it easier to just backport code changes, if
that is ever needed.

* cfg.mk (sc_curly_braces_style): Catch hanging braces.
* docs/hacking.html.in: Document it.
* HACKING: Regenerate.

Signed-off-by: Eric Blake <eblake@redhat.com>
HACKING
cfg.mk
docs/hacking.html.in

diff --git a/HACKING b/HACKING
index 88a42864b86b52c823fdf091baad8e1bf5527237..add0841cb8d16a728fc4f03b246580bb7eee741d 100644 (file)
--- a/HACKING
+++ b/HACKING
@@ -461,6 +461,33 @@ But if negating a complex condition is too ugly, then at least add braces:
       x = y;
   }
 
+Use hanging braces for compound statements: the opening brace of a compound
+statement should be on the same line as the condition being tested. Only
+top-level function bodies, nested scopes, and compound structure declarations
+should ever have { on a line by itself.
+
+  void
+  foo(int a, int b)
+  {                          // correct - function body
+      int 2d[][] = {
+        {                    // correct - complex initialization
+          1, 2,
+        },
+      };
+      if (a)
+      {                      // BAD: compound brace on its own line
+          do_stuff();
+      }
+      {                      // correct - nested scope
+          int tmp;
+          if (a < b) {       // correct - hanging brace
+              tmp = b;
+              b = a;
+              a = tmp;
+          }
+      }
+  }
+
 
 Preprocessor
 ============
diff --git a/cfg.mk b/cfg.mk
index 122cf58c00a542bc4e0875532d0f6e432e251cd2..c7119e6945e0b16337e1e1b48f198e15736c7084 100644 (file)
--- a/cfg.mk
+++ b/cfg.mk
@@ -918,12 +918,18 @@ sc_require_if_else_matching_braces:
          $(_sc_search_regexp)
 
 sc_curly_braces_style:
-       @files=$$($(VC_LIST_EXCEPT) | grep '\.[ch]$$');                \
-       $(GREP) -nHP                                                   \
-'^\s*(?!([a-zA-Z_]*for_?each[a-zA-Z_]*) ?\()([_a-zA-Z0-9]+( [_a-zA-Z0-9]+)* ?\()?(\*?[_a-zA-Z0-9]+(,? \*?[_a-zA-Z0-9\[\]]+)+|void)\) ?\{' \
-       $$files && { echo '$(ME): Non-K&R style used for curly'        \
-                         'braces around function body, see'           \
-                         'HACKING' 1>&2; exit 1; } || :
+       @files=$$($(VC_LIST_EXCEPT) | grep '\.[ch]$$');                 \
+       if $(GREP) -nHP                                                 \
+'^\s*(?!([a-zA-Z_]*for_?each[a-zA-Z_]*) ?\()([_a-zA-Z0-9]+( [_a-zA-Z0-9]+)* ?\()?(\*?[_a-zA-Z0-9]+(,? \*?[_a-zA-Z0-9\[\]]+)+|void)\) ?\{'              \
+       $$files; then                                                   \
+         echo '$(ME): Non-K&R style used for curly braces around'      \
+               'function body, see HACKING' 1>&2; exit 1;              \
+       fi;                                                             \
+       if $(GREP) -A1 -En ' ((if|for|while|switch) \(|(else|do)\b)[^{]*$$'\
+         $$files | $(GREP) '^[^ ]*- *{'; then                          \
+         echo '$(ME): Use hanging braces for compound statements,'     \
+               'see HACKING' 1>&2; exit 1;                             \
+       fi
 
 sc_prohibit_windows_special_chars_in_filename:
        @files=$$($(VC_LIST_EXCEPT) | grep '[:*?"<>|]');               \
index bc76542071c423da8aa67d1d5739a52fdcd47ced..8f2b9d6ef4ebe9f0ad0cf9d43ae0645f4454f053 100644 (file)
   }
 </pre>
 
+    <p>Use hanging braces for compound statements: the opening brace
+      of a compound statement should be on the same line as the
+      condition being tested.  Only top-level function bodies, nested
+      scopes, and compound structure declarations should ever have {
+      on a line by itself.
+    </p>
+
+<pre>
+  void
+  foo(int a, int b)
+  {                          // correct - function body
+      int 2d[][] = {
+        {                    // correct - complex initialization
+          1, 2,
+        },
+      };
+      if (a)
+      {                      // BAD: compound brace on its own line
+          do_stuff();
+      }
+      {                      // correct - nested scope
+          int tmp;
+          if (a &lt; b) {       // correct - hanging brace
+              tmp = b;
+              b = a;
+              a = tmp;
+          }
+      }
+  }
+</pre>
+
     <h2><a name="preprocessor">Preprocessor</a></h2>
 
     <p>Macros defined with an ALL_CAPS name should generally be