[PATCH] D6920: [clang-format] Add SpaceBeforeBrackets

2019-11-16 Thread Matthäus G. Chajdas via Phabricator via cfe-commits
Anteru added a comment.

Thanks a lot!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D6920/new/

https://reviews.llvm.org/D6920



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D6920: [clang-format] Add SpaceBeforeBrackets

2019-11-16 Thread Matthäus G. Chajdas via Phabricator via cfe-commits
Anteru updated this revision to Diff 229612.
Anteru edited the summary of this revision.
Anteru added a comment.

Updated as requested.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D6920/new/

https://reviews.llvm.org/D6920

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

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -10643,6 +10643,41 @@
   verifyFormat("int foo = [ , = ]() {};", Spaces);
 }
 
+TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
+  FormatStyle NoSpaceStyle = getLLVMStyle();
+  verifyFormat("int a[5];", NoSpaceStyle);
+  verifyFormat("a[3] += 42;", NoSpaceStyle);
+
+  verifyFormat("int a[1];", NoSpaceStyle);
+  verifyFormat("int 1 [a];", NoSpaceStyle);
+  verifyFormat("int a[1][2];", NoSpaceStyle);
+  verifyFormat("a[7] = 5;", NoSpaceStyle);
+  verifyFormat("int a = (f())[23];", NoSpaceStyle);
+  verifyFormat("f([] {})", NoSpaceStyle);
+
+  FormatStyle Space = getLLVMStyle();
+  Space.SpaceBeforeSquareBrackets = true;
+  verifyFormat("int c = []() -> int { return 2; }();\n", Space);
+  verifyFormat("return [i, args...] {};", Space);
+
+  verifyFormat("int a [5];", Space);
+  verifyFormat("a [3] += 42;", Space);
+  verifyFormat("constexpr char hello []{\"hello\"};", Space);
+  verifyFormat("double [](int i) { return 0; }\n"
+   "int i;",
+   Space);
+  verifyFormat("std::unique_ptr foo() {}", Space);
+  verifyFormat("int i = a [a][a]->f();", Space);
+  verifyFormat("int i = (*b) [a]->f();", Space);
+
+  verifyFormat("int a [1];", Space);
+  verifyFormat("int 1 [a];", Space);
+  verifyFormat("int a [1][2];", Space);
+  verifyFormat("a [7] = 5;", Space);
+  verifyFormat("int a = (f()) [23];", Space);
+  verifyFormat("f([] {})", Space);
+}
+
 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
   verifyFormat("int a = 5;");
   verifyFormat("a += 42;");
@@ -12529,6 +12564,7 @@
   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
+  CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
   CHECK_PARSE_BOOL(UseCRLF);
 
   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2724,7 +2724,9 @@
   !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
  TT_DesignatedInitializerLSquare,
  TT_StructuredBindingLSquare, TT_AttributeSquare) &&
-  !Left.isOneOf(tok::numeric_constant, TT_DictLiteral))
+  !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
+  !(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
+Right.is(TT_ArraySubscriptLSquare)))
 return false;
   if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
 return !Left.Children.empty(); // No spaces in "{}".
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -543,6 +543,8 @@
Style.SpacesInCStyleCastParentheses);
 IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
 IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
+IO.mapOptional("SpaceBeforeSquareBrackets",
+   Style.SpaceBeforeSquareBrackets);
 IO.mapOptional("Standard", Style.Standard);
 IO.mapOptional("StatementMacros", Style.StatementMacros);
 IO.mapOptional("TabWidth", Style.TabWidth);
@@ -813,6 +815,7 @@
   LLVMStyle.SpaceBeforeRangeBasedForLoopColon = true;
   LLVMStyle.SpaceBeforeAssignmentOperators = true;
   LLVMStyle.SpaceBeforeCpp11BracedList = false;
+  LLVMStyle.SpaceBeforeSquareBrackets = false;
   LLVMStyle.SpacesInAngles = false;
 
   LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -1986,6 +1986,15 @@
   /// \endcode
   bool SpacesInSquareBrackets;
 
+  /// If ``true``, spaces will be before  ``[``.
+  /// Lambdas will not be affected. Only the first ``[`` will get a space added.
+  /// \code
+  ///true:  false:
+  ///int a [5];vs.  int a[5];
+  ///int a [5][5]; vs.  int a[5][5];
+  /// \endcode
+  bool SpaceBeforeSquareBrackets;
+
   /// Supported language standards for parsing and formatting C++ constructs.
   /// \code
   ///Latest:vector>
@@ -2150,10 +2159,10 @@