[PATCH 2/2] maint: tighten curly brace syntax checking

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

 



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@xxxxxxxxxx>
---
 HACKING              | 27 +++++++++++++++++++++++++++
 cfg.mk               | 18 ++++++++++++------
 docs/hacking.html.in | 31 +++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/HACKING b/HACKING
index 88a4286..add0841 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 122cf58..77f1868 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 bc76542..8f2b9d6 100644
--- a/docs/hacking.html.in
+++ b/docs/hacking.html.in
@@ -593,6 +593,37 @@
   }
 </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
-- 
1.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]