[PATCH] [clang-format] Add SpaceBeforeBrackets

Matthäus G. Chajdas clang at anteru.net
Sun Jan 11 06:54:50 PST 2015


Adds a new option SpaceBeforeBrackets to add spaces before brackets (i.e. int a[23]; -> int a [23];)

http://reviews.llvm.org/D6920

Files:
  include/clang/Format/Format.h
  lib/Format/Format.cpp
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTest.cpp

Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -353,6 +353,9 @@
   /// \brief If \c true, spaces will be inserted after '[' and before ']'.
   bool SpacesInSquareBrackets;
 
+  /// \brief If \c true, a space will be inserted between identifer and '['.
+  bool SpaceBeforeBrackets;
+
   /// \brief If \c true, spaces may be inserted into '()'.
   bool SpaceInEmptyParentheses;
 
@@ -470,6 +473,7 @@
            SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
            SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
            SpaceBeforeParens == R.SpaceBeforeParens &&
+           SpaceBeforeBrackets == R.SpaceBeforeBrackets &&
            SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
            ContinuationIndentWidth == R.ContinuationIndentWidth &&
            CommentPragmas == R.CommentPragmas &&
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -252,6 +252,7 @@
                    Style.SpacesInContainerLiterals);
     IO.mapOptional("SpaceBeforeAssignmentOperators",
                    Style.SpaceBeforeAssignmentOperators);
+    IO.mapOptional("SpaceBeforeBrackets", Style.SpaceBeforeBrackets);
     IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
     IO.mapOptional("CommentPragmas", Style.CommentPragmas);
     IO.mapOptional("ForEachMacros", Style.ForEachMacros);
@@ -379,6 +380,7 @@
   LLVMStyle.SpaceAfterCStyleCast = false;
   LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
   LLVMStyle.SpaceBeforeAssignmentOperators = true;
+  LLVMStyle.SpaceBeforeBrackets = false;
   LLVMStyle.SpacesInAngles = false;
 
   LLVMStyle.PenaltyBreakComment = 300;
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1659,7 +1659,8 @@
              Right.MatchingParen->is(TT_ArraySubscriptLSquare)));
   if (Right.is(tok::l_square) &&
       !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare) &&
-      !Left.isOneOf(tok::numeric_constant, TT_DictLiteral))
+      !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
+      !(!Left.is(tok::r_square) && Style.SpaceBeforeBrackets))
     return false;
   if (Left.is(tok::colon))
     return !Left.is(TT_ObjCMethodExpr);
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -7989,6 +7989,24 @@
   verifyFormat("int f () throw (Deprecated);", Space);
 }
 
+TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
+    FormatStyle NoSpace = getLLVMStyle();
+    NoSpace.SpaceBeforeBrackets = false;
+
+    verifyFormat("int a[1];", NoSpace);
+    verifyFormat("int a[1][2];", NoSpace);
+    verifyFormat("a[7] = 5;", NoSpace);
+    verifyFormat("int a = (f())[23];", NoSpace);
+
+    FormatStyle Space = getLLVMStyle();
+    Space.SpaceBeforeBrackets = true;
+
+    verifyFormat("int a [1];", Space);
+    verifyFormat("int a [1][2];", Space);
+    verifyFormat("a [7] = 5;", Space);
+    verifyFormat("int a = (f()) [23];", Space);
+}
+
 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
   FormatStyle Spaces = getLLVMStyle();
 
@@ -8683,6 +8701,7 @@
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
+  CHECK_PARSE_BOOL(SpaceBeforeBrackets);
 }
 
 #undef CHECK_PARSE_BOOL

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D6920.17989.patch
Type: text/x-patch
Size: 3712 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150111/68912dc6/attachment.bin>


More information about the cfe-commits mailing list