diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -1168,6 +1168,10 @@ auto MaxNetWidth = getMaximumNetWidth( Cells.begin(), CellIter, CellDescs.InitialSpaces, CellDescs.CellCounts[0], CellDescs.CellCounts.size()); + // If Cpp11BraceListStyle is false, the braces need to be space-separated from + // the elements. + if (!Style.Cpp11BracedListStyle) + MaxNetWidth += 1; if (ThisNetWidth < MaxNetWidth) Changes[CellIter->Index].Spaces = (MaxNetWidth - ThisNetWidth); auto RowCount = 1U; @@ -1186,9 +1190,10 @@ auto ThisWidth = calculateCellWidth(CellIter->Index, CellIter->EndIndex, true) + NetWidth; + auto BeginSpace = (i > 0) ? 1 : (Style.Cpp11BracedListStyle ? 0 : 1); if (Changes[CellIter->Index].NewlinesBefore == 0) { Changes[CellIter->Index].Spaces = (CellWidth - (ThisWidth + NetWidth)); - Changes[CellIter->Index].Spaces += (i > 0) ? 1 : 0; + Changes[CellIter->Index].Spaces += BeginSpace; } alignToStartOfCell(CellIter->Index, CellIter->EndIndex); for (const auto *Next = CellIter->NextColumnElement; Next; @@ -1197,7 +1202,7 @@ calculateCellWidth(Next->Index, Next->EndIndex, true) + NetWidth; if (Changes[Next->Index].NewlinesBefore == 0) { Changes[Next->Index].Spaces = (CellWidth - ThisWidth); - Changes[Next->Index].Spaces += (i > 0) ? 1 : 0; + Changes[Next->Index].Spaces += BeginSpace; } alignToStartOfCell(Next->Index, Next->EndIndex); } @@ -1214,9 +1219,10 @@ auto &Cells = CellDescs.Cells; // Now go through and fixup the spaces. auto *CellIter = Cells.begin(); - // The first cell needs to be against the left brace. + // The first cell needs to be against the left brace, unless + // Cpp11BracedListStyle is enabled. if (Changes[CellIter->Index].NewlinesBefore == 0) - Changes[CellIter->Index].Spaces = 0; + Changes[CellIter->Index].Spaces = Style.Cpp11BracedListStyle ? 0 : 1; else Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces; ++CellIter; @@ -1229,7 +1235,9 @@ if (Changes[CellIter->Index].NewlinesBefore == 0) { Changes[CellIter->Index].Spaces = MaxNetWidth - ThisNetWidth + - (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1 : 0); + (Changes[CellIter->Index].Tok->isNot(tok::r_brace) + ? 1 + : (Style.Cpp11BracedListStyle ? 0 : 1)); } auto RowCount = 1U; auto Offset = std::distance(Cells.begin(), CellIter); @@ -1243,7 +1251,9 @@ if (Changes[Next->Index].NewlinesBefore == 0) { Changes[Next->Index].Spaces = MaxNetWidth - ThisNetWidth + - (Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : 0); + (Changes[Next->Index].Tok->isNot(tok::r_brace) + ? 1 + : (Style.Cpp11BracedListStyle ? 0 : 1)); } ++RowCount; } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -20267,6 +20267,62 @@ "that really, in any just world, ought to be split over multiple " "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", Style)); + + Style.Cpp11BracedListStyle = false; + Style.ColumnLimit = 20; + EXPECT_EQ( + "demo = std::array<\n" + " struct test, 3>{\n" + " test{\n" + " 56, 23,\n" + " \"hello world \"\n" + " \"i am a very \"\n" + " \"long line \"\n" + " \"that \"\n" + " \"really, in \"\n" + " \"any just \"\n" + " \"world, \"\n" + " \"ought to be \"\n" + " \"split over \"\n" + " \"multiple \"\n" + " \"lines\" },\n" + " test{ -1, 93463,\n" + " \"world\" },\n" + " test{ 7, 5,\n" + " \"!!\" },\n" + "};", + format("demo = std::array{test{56, 23, \"hello world " + "i am a very long line that really, in any just world, ought " + "to be split over multiple lines\"},test{-1, 93463, \"world\"}," + "test{7, 5, \"!!\"},};", + Style)); + + Style.ColumnLimit = 100; + verifyFormat("struct test demo[] = {\n" + " { 56, 23, \"hello\" },\n" + " { -1, 93463, \"world\" },\n" + " { 7, 5, \"!!\" }\n" + "};\n" + "struct test demo[4] = {\n" + " { 56, 23, 21, \"oh\" }, // first line\n" + " { -1, 93463, 22, \"my\" }, // second line\n" + " { 7, 5, 1, \"goodness\" } // third line\n" + " { 234, 5, 1, \"gracious\" } // fourth line\n" + "};\n", + Style); + EXPECT_EQ( + "test demo[] = {\n" + " { 56,\n" + " \"hello world i am a very long line that really, in any just world" + ", ought to be split over \"\n" + " \"multiple lines\", 23 },\n" + " { -1, \"world\", 93463 },\n" + " { 7, \"!!\", 5 },\n" + "};", + format("test demo[] = {{56, \"hello world i am a very long line " + "that really, in any just world, ought to be split over multiple " + "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", + Style)); } TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { @@ -20451,6 +20507,56 @@ "that really, in any just world, ought to be split over multiple " "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", Style)); + + Style.Cpp11BracedListStyle = false; + Style.ColumnLimit = 20; + EXPECT_EQ( + "demo = std::array<\n" + " struct test, 3>{\n" + " test{\n" + " 56, 23,\n" + " \"hello world \"\n" + " \"i am a very \"\n" + " \"long line \"\n" + " \"that \"\n" + " \"really, in \"\n" + " \"any just \"\n" + " \"world, \"\n" + " \"ought to be \"\n" + " \"split over \"\n" + " \"multiple \"\n" + " \"lines\" },\n" + " test{ -1, 93463,\n" + " \"world\" },\n" + " test{ 7, 5,\n" + " \"!!\" },\n" + "};", + format("demo = std::array{test{56, 23, \"hello world " + "i am a very long line that really, in any just world, ought " + "to be split over multiple lines\"},test{-1, 93463, \"world\"}," + "test{7, 5, \"!!\"},};", + Style)); + Style.ColumnLimit = 100; + verifyFormat("test demo[] = {\n" + " { 56, /* a comment */ 23, \"hello\" },\n" + " { -1, 93463, \"world\" },\n" + " { 7, 5, \"!!\" }\n" + "};\n", + Style); + + EXPECT_EQ( + "test demo[] = {\n" + " { 56, 23,\n" + " \"hello world i am a very long line that really, in any just world" + ", ought to be split over \"\n" + " \"multiple lines\" },\n" + " { -1, 93463, \"world\" },\n" + " { 7, 5, \"!!\" },\n" + "};", + format("test demo[] = {{56, 23, \"hello world i am a very long line " + "that really, in any just world, ought to be split over multiple " + "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", + Style)); } TEST_F(FormatTest, UnderstandsPragmas) {