[PATCH] D145813: [clang-format] Respect Cpp11BraceListStyle when aligning array of structures
hch12907 created this revision. hch12907 added a reviewer: owenpan. Herald added a project: All. hch12907 requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits. When Cpp11BraceListStyle is set to false and AlignArrayOfStructures is set, the first and last elements of a braced list should not be joined to the brace. Fixes #57611. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D145813 Files: clang/lib/Format/WhitespaceManager.cpp Index: clang/lib/Format/WhitespaceManager.cpp === --- clang/lib/Format/WhitespaceManager.cpp +++ 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; } Index: clang/lib/Format/WhitespaceManager.cpp === --- clang/lib/Format/WhitespaceManager.cpp +++ 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 *
[PATCH] D145813: [clang-format] Respect Cpp11BraceListStyle when aligning array of structures
hch12907 updated this revision to Diff 504238. hch12907 added a comment. Added unit tests Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D145813/new/ https://reviews.llvm.org/D145813 Files: clang/lib/Format/WhitespaceManager.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp === --- clang/unittests/Format/FormatTest.cpp +++ 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) { Index: clang/lib/Format/WhitespaceManager.cpp ==
[PATCH] D145813: [clang-format] Respect Cpp11BraceListStyle when aligning array of structures
hch12907 added inline comments. Comment at: clang/unittests/Format/FormatTest.cpp:20273 + Style.ColumnLimit = 20; + EXPECT_EQ( + "demo = std::array<\n" MyDeveloperDay wrote: > any reason you didn't use verifyFormat here? No particular reason - I simply copied other tests and modified them to suit the Cpp11BracedListStyle == false case, and one of those tests used EXPECT_EQ. Should I change it to verifyFormat? Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D145813/new/ https://reviews.llvm.org/D145813 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits