From: Eric Blake Date: Wed, 3 Sep 2014 22:18:19 +0000 (-0600) Subject: maint: tighten curly brace syntax checking X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=bc7e63d39ca0e96073ba04e5a0c2b5471ca05b85;p=libvirt.git maint: tighten curly brace syntax checking 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 --- diff --git a/HACKING b/HACKING index 88a42864b8..add0841cb8 100644 --- 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 122cf58c00..c7119e6945 100644 --- 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 '[:*?"<>|]'); \ diff --git a/docs/hacking.html.in b/docs/hacking.html.in index bc76542071..8f2b9d6ef4 100644 --- a/docs/hacking.html.in +++ b/docs/hacking.html.in @@ -593,6 +593,37 @@ } +

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

Macros defined with an ALL_CAPS name should generally be