jgehrig created this revision.
jgehrig added a project: clang.
Herald added a subscriber: cfe-commits.

First patch attempt :)

I've noticed that clang-format does not honor the BAS_AlwaysBreak when 
Cpp11BracedListStyle is set. This seems like a bug, as BAS_DontAlign will 
change the line continuation behavior.

This patch makes BAS_AlwaysBreak affect the line continuation behavior in the 
same way it does with functions or constructors.

Unit Tests should demonstrate the proposed modifications. I also have two 
related patches which I intend to propose: spaces inside Cpp11BracedList {}s, 
and parameterization of the 'magic number' which overrides BinPackArguments for 
Cpp11BracedLists.


Repository:
  rC Clang

https://reviews.llvm.org/D55470

Files:
  lib/Format/ContinuationIndenter.cpp
  unittests/Format/FormatTest.cpp

Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -7277,6 +7277,60 @@
   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
 }
 
+TEST_F(FormatTest, Cpp11ListAlwaysBreakOrDontAlign) {
+  FormatStyle Style = getLLVMStyle();
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+  Style.BinPackArguments = false;
+  Style.ColumnLimit = 40;
+  Style.Cpp11BracedListStyle = true;
+
+  verifyFormat("type foobar_a{\n"
+               "    aaaaaa,\n"
+               "    bbbbbb,\n"
+               "    cccccc,\n"
+               "    dddddddddddddddddddddddddd,\n"
+               "    eeeeeeeeeeeeeeeeeeeeeeeeee};",
+               Style);
+
+  verifyFormat("type some_namespace::foobar_b{\n"
+               "    a, abc, adbdef};",
+               Style);
+
+  verifyFormat("type foobar_c{\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int};",
+               Style);
+
+  Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+
+  verifyFormat("type foobar_a{aaaaaa,\n"
+               "    bbbbbb,\n"
+               "    cccccc,\n"
+               "    dddddddddddddddddddddddddd,\n"
+               "    eeeeeeeeeeeeeeeeeeeeeeeeee};",
+               Style);
+
+  verifyFormat("type some_namespace::foobar_b{\n"
+               "    a, abc, adbdef};",
+               Style);
+
+  verifyFormat("type foobar_c{an_int,\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int,\n"
+               "    an_int};",
+               Style);
+}
+
 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
Index: lib/Format/ContinuationIndenter.cpp
===================================================================
--- lib/Format/ContinuationIndenter.cpp
+++ lib/Format/ContinuationIndenter.cpp
@@ -607,7 +607,7 @@
   // disallowing any further line breaks if there is no line break after the
   // opening parenthesis. Don't break if it doesn't conserve columns.
   if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak &&
-      Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
+      Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square, tok::l_brace) &&
       State.Column > getNewLineColumn(State) &&
       (!Previous.Previous || !Previous.Previous->isOneOf(
                                  tok::kw_for, tok::kw_while, tok::kw_switch)) &&
@@ -625,6 +625,7 @@
     State.Stack.back().NoLineBreak = true;
 
   if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
+      (!Style.isCpp() || Style.AlignAfterOpenBracket != FormatStyle::BAS_AlwaysBreak) &&
       Previous.opensScope() && Previous.isNot(TT_ObjCMethodExpr) &&
       (Current.isNot(TT_LineComment) || Previous.BlockKind == BK_BracedInit))
     State.Stack.back().Indent = State.Column + Spaces;
@@ -1217,12 +1218,14 @@
     // Indent from 'LastSpace' unless these are fake parentheses encapsulating
     // a builder type call after 'return' or, if the alignment after opening
     // brackets is disabled.
+    const bool isAlignParametersMode =
+      Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
+      Style.AlignAfterOpenBracket != FormatStyle::BAS_AlwaysBreak;
     if (!Current.isTrailingComment() &&
         (Style.AlignOperands || *I < prec::Assignment) &&
         (!Previous || Previous->isNot(tok::kw_return) ||
          (Style.Language != FormatStyle::LK_Java && *I > 0)) &&
-        (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
-         *I != prec::Comma || Current.NestingLevel == 0))
+        (isAlignParametersMode || *I != prec::Comma || Current.NestingLevel == 0))
       NewParenState.Indent =
           std::max(std::max(State.Column, NewParenState.Indent),
                    State.Stack.back().LastSpace);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to