[PATCH] D158795: Fix AlignArrayOfStructures + Cpp11BracedListStyle=false

2023-08-31 Thread Owen Pan via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG58c67e724f71: [clang-format] Fix AlignArrayOfStructures + 
Cpp11BracedListStyle=false (authored by galenelias, committed by owenpan).

Changed prior to commit:
  https://reviews.llvm.org/D158795?vs=553809&id=555164#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D158795

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
@@ -20651,6 +20651,15 @@
"});",
Style);
 
+  Style.Cpp11BracedListStyle = false;
+  verifyFormat("struct test demo[] = {\n"
+   "  { 56,23, \"hello\" },\n"
+   "  { -1, 93463, \"world\" },\n"
+   "  {  7, 5,\"!!\" }\n"
+   "};",
+   Style);
+  Style.Cpp11BracedListStyle = true;
+
   Style.ColumnLimit = 0;
   verifyFormat(
   "test demo[] = {\n"
@@ -20882,6 +20891,15 @@
"});",
Style);
 
+  Style.Cpp11BracedListStyle = false;
+  verifyFormat("struct test demo[] = {\n"
+   "  { 56, 23,\"hello\" },\n"
+   "  { -1, 93463, \"world\" },\n"
+   "  { 7,  5, \"!!\"}\n"
+   "};",
+   Style);
+  Style.Cpp11BracedListStyle = true;
+
   Style.ColumnLimit = 0;
   verifyFormat(
   "test demo[] = {\n"
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -1230,6 +1230,7 @@
   if (!CellDescs.isRectangular())
 return;
 
+  const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
   auto &Cells = CellDescs.Cells;
   // Now go through and fixup the spaces.
   auto *CellIter = Cells.begin();
@@ -1247,7 +1248,7 @@
   do {
 const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
 if (Previous && Previous->isNot(TT_LineComment)) {
-  Changes[Next->Index].Spaces = 0;
+  Changes[Next->Index].Spaces = BracePadding;
   Changes[Next->Index].NewlinesBefore = 0;
 }
 Next = Next->NextColumnElement;
@@ -1280,7 +1281,7 @@
   NetWidth;
   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 += (i > 0) ? 1 : BracePadding;
   }
   alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
   for (const auto *Next = CellIter->NextColumnElement; Next;
@@ -1289,7 +1290,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 += (i > 0) ? 1 : BracePadding;
 }
 alignToStartOfCell(Next->Index, Next->EndIndex);
   }
@@ -1303,12 +1304,13 @@
   if (!CellDescs.isRectangular())
 return;
 
+  const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
   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.
   if (Changes[CellIter->Index].NewlinesBefore == 0)
-Changes[CellIter->Index].Spaces = 0;
+Changes[CellIter->Index].Spaces = BracePadding;
   else
 Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces;
   ++CellIter;
@@ -1321,7 +1323,8 @@
 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
+ : BracePadding);
 }
 auto RowCount = 1U;
 auto Offset = std::distance(Cells.begin(), CellIter);
@@ -1335,7 +1338,7 @@
   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 : BracePadding);
   }
   ++RowCount;
 }
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158795: Fix AlignArrayOfStructures + Cpp11BracedListStyle=false

2023-08-27 Thread Owen Pan via Phabricator via cfe-commits
owenpan accepted this revision.
owenpan added inline comments.



Comment at: clang/unittests/Format/FormatTest.cpp:20888
+   "  {  7, 5,\"!!\" }\n"
+   "};\n",
+   Style);

galenelias wrote:
> owenpan wrote:
> > galenelias wrote:
> > > owenpan wrote:
> > > > 
> > > This is consistent with basically every single adjacent test in this 
> > > function.  While I agree that this is unnecessary, in general I error on 
> > > the side of consistency with the surrounding tests.  I'll defer to the 
> > > maintainers, just wanted to make sure this is actually the preferred 
> > > change given the numerous adjacent tests with this form.
> > If you rebase your patch, you'll see that the trailing newlines in the 
> > surrounding tests have been removed. (Even if they had not been removed, we 
> > still wouldn't want new tests to have superfluous trailing newlines.)
> Thanks for the information, looks like I missed your cleanup change by a day. 
>  In general some code-bases favor consistency over some extra gunk, so I tend 
> to default to consistency, but good to know going forward.  I've rebased now.
Np!


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

https://reviews.llvm.org/D158795

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


[PATCH] D158795: Fix AlignArrayOfStructures + Cpp11BracedListStyle=false

2023-08-27 Thread Galen Elias via Phabricator via cfe-commits
galenelias marked 4 inline comments as done.
galenelias added inline comments.



Comment at: clang/unittests/Format/FormatTest.cpp:20888
+   "  {  7, 5,\"!!\" }\n"
+   "};\n",
+   Style);

owenpan wrote:
> galenelias wrote:
> > owenpan wrote:
> > > 
> > This is consistent with basically every single adjacent test in this 
> > function.  While I agree that this is unnecessary, in general I error on 
> > the side of consistency with the surrounding tests.  I'll defer to the 
> > maintainers, just wanted to make sure this is actually the preferred change 
> > given the numerous adjacent tests with this form.
> If you rebase your patch, you'll see that the trailing newlines in the 
> surrounding tests have been removed. (Even if they had not been removed, we 
> still wouldn't want new tests to have superfluous trailing newlines.)
Thanks for the information, looks like I missed your cleanup change by a day.  
In general some code-bases favor consistency over some extra gunk, so I tend to 
default to consistency, but good to know going forward.  I've rebased now.


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

https://reviews.llvm.org/D158795

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


[PATCH] D158795: Fix AlignArrayOfStructures + Cpp11BracedListStyle=false

2023-08-27 Thread Galen Elias via Phabricator via cfe-commits
galenelias updated this revision to Diff 553809.

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

https://reviews.llvm.org/D158795

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
@@ -20879,6 +20879,16 @@
"});",
Style);
 
+  Style.Cpp11BracedListStyle = false;
+  verifyFormat("struct test demo[] = {\n"
+   "  { 56,23, \"hello\" },\n"
+   "  { -1, 93463, \"world\" },\n"
+   "  {  7, 5,\"!!\" }\n"
+   "};",
+   Style);
+
+  Style.Cpp11BracedListStyle = true;
+
   Style.ColumnLimit = 0;
   EXPECT_EQ(
   "test demo[] = {\n"
@@ -2,6 +21121,15 @@
"});",
Style);
 
+  Style.Cpp11BracedListStyle = false;
+  verifyFormat("struct test demo[] = {\n"
+   "  { 56, 23,\"hello\" },\n"
+   "  { -1, 93463, \"world\" },\n"
+   "  { 7,  5, \"!!\"}\n"
+   "};",
+   Style);
+  Style.Cpp11BracedListStyle = true;
+
   Style.ColumnLimit = 0;
   EXPECT_EQ(
   "test demo[] = {\n"
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -1226,6 +1226,7 @@
   if (!CellDescs.isRectangular())
 return;
 
+  const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
   auto &Cells = CellDescs.Cells;
   // Now go through and fixup the spaces.
   auto *CellIter = Cells.begin();
@@ -1243,7 +1244,7 @@
   do {
 const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
 if (Previous && Previous->isNot(TT_LineComment)) {
-  Changes[Next->Index].Spaces = 0;
+  Changes[Next->Index].Spaces = BracePadding;
   Changes[Next->Index].NewlinesBefore = 0;
 }
 Next = Next->NextColumnElement;
@@ -1276,7 +1277,7 @@
   NetWidth;
   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 += (i > 0) ? 1 : BracePadding;
   }
   alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
   for (const auto *Next = CellIter->NextColumnElement; Next;
@@ -1285,7 +1286,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 += (i > 0) ? 1 : BracePadding;
 }
 alignToStartOfCell(Next->Index, Next->EndIndex);
   }
@@ -1299,12 +1300,13 @@
   if (!CellDescs.isRectangular())
 return;
 
+  const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
   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.
   if (Changes[CellIter->Index].NewlinesBefore == 0)
-Changes[CellIter->Index].Spaces = 0;
+Changes[CellIter->Index].Spaces = BracePadding;
   else
 Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces;
   ++CellIter;
@@ -1317,7 +1319,8 @@
 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
+ : BracePadding);
 }
 auto RowCount = 1U;
 auto Offset = std::distance(Cells.begin(), CellIter);
@@ -1331,7 +1334,7 @@
   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 : BracePadding);
   }
   ++RowCount;
 }
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158795: Fix AlignArrayOfStructures + Cpp11BracedListStyle=false

2023-08-26 Thread Owen Pan via Phabricator via cfe-commits
owenpan added inline comments.



Comment at: clang/unittests/Format/FormatTest.cpp:20888
+   "  {  7, 5,\"!!\" }\n"
+   "};\n",
+   Style);

galenelias wrote:
> owenpan wrote:
> > 
> This is consistent with basically every single adjacent test in this 
> function.  While I agree that this is unnecessary, in general I error on the 
> side of consistency with the surrounding tests.  I'll defer to the 
> maintainers, just wanted to make sure this is actually the preferred change 
> given the numerous adjacent tests with this form.
If you rebase your patch, you'll see that the trailing newlines in the 
surrounding tests have been removed. (Even if they had not been removed, we 
still wouldn't want new tests to have superfluous trailing newlines.)


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

https://reviews.llvm.org/D158795

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


[PATCH] D158795: Fix AlignArrayOfStructures + Cpp11BracedListStyle=false

2023-08-26 Thread Galen Elias via Phabricator via cfe-commits
galenelias updated this revision to Diff 553783.

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

https://reviews.llvm.org/D158795

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
@@ -20880,6 +20880,16 @@
"});\n",
Style);
 
+  Style.Cpp11BracedListStyle = false;
+  verifyFormat("struct test demo[] = {\n"
+   "  { 56,23, \"hello\" },\n"
+   "  { -1, 93463, \"world\" },\n"
+   "  {  7, 5,\"!!\" }\n"
+   "};\n",
+   Style);
+
+  Style.Cpp11BracedListStyle = true;
+
   Style.ColumnLimit = 0;
   EXPECT_EQ(
   "test demo[] = {\n"
@@ -21112,6 +21122,15 @@
"});\n",
Style);
 
+  Style.Cpp11BracedListStyle = false;
+  verifyFormat("struct test demo[] = {\n"
+   "  { 56, 23,\"hello\" },\n"
+   "  { -1, 93463, \"world\" },\n"
+   "  { 7,  5, \"!!\"}\n"
+   "};\n",
+   Style);
+  Style.Cpp11BracedListStyle = true;
+
   Style.ColumnLimit = 0;
   EXPECT_EQ(
   "test demo[] = {\n"
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -1226,6 +1226,7 @@
   if (!CellDescs.isRectangular())
 return;
 
+  const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
   auto &Cells = CellDescs.Cells;
   // Now go through and fixup the spaces.
   auto *CellIter = Cells.begin();
@@ -1243,7 +1244,7 @@
   do {
 const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
 if (Previous && Previous->isNot(TT_LineComment)) {
-  Changes[Next->Index].Spaces = 0;
+  Changes[Next->Index].Spaces = BracePadding;
   Changes[Next->Index].NewlinesBefore = 0;
 }
 Next = Next->NextColumnElement;
@@ -1276,7 +1277,7 @@
   NetWidth;
   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 += (i > 0) ? 1 : BracePadding;
   }
   alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
   for (const auto *Next = CellIter->NextColumnElement; Next;
@@ -1285,7 +1286,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 += (i > 0) ? 1 : BracePadding;
 }
 alignToStartOfCell(Next->Index, Next->EndIndex);
   }
@@ -1299,12 +1300,13 @@
   if (!CellDescs.isRectangular())
 return;
 
+  const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
   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.
   if (Changes[CellIter->Index].NewlinesBefore == 0)
-Changes[CellIter->Index].Spaces = 0;
+Changes[CellIter->Index].Spaces = BracePadding;
   else
 Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces;
   ++CellIter;
@@ -1317,7 +1319,7 @@
 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 : BracePadding);
 }
 auto RowCount = 1U;
 auto Offset = std::distance(Cells.begin(), CellIter);
@@ -1331,7 +1333,7 @@
   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 : BracePadding);
   }
   ++RowCount;
 }
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158795: Fix AlignArrayOfStructures + Cpp11BracedListStyle=false

2023-08-26 Thread Galen Elias via Phabricator via cfe-commits
galenelias marked an inline comment as done.
galenelias added inline comments.



Comment at: clang/lib/Format/WhitespaceManager.cpp:1247
 if (Previous && Previous->isNot(TT_LineComment)) {
-  Changes[Next->Index].Spaces = 0;
+  Changes[Next->Index].Spaces = BracePadding;
   Changes[Next->Index].NewlinesBefore = 0;

owenpan wrote:
> owenpan wrote:
> > Can we assert that `Spaces == 0`? If not, we should add a test case.
> We can't assert that, but setting `Spaces` here seems superfluous as it's set 
> correctly below anyways?
I admit I'm not super confident on my understanding of this code, but this 
setting of Spaces is not redundant with any below settings.  If we set it to 
'3' for instance, that won't get overwritten later (because the other sets are 
all conditional, and don't hit for the `}` token).

So, I think this is still required (minus the issue of the existing 'Spaces' 
calculation from previous formatting pass seemingly already setting Spaces to 
the correct value).



Comment at: clang/unittests/Format/FormatTest.cpp:20888
+   "  {  7, 5,\"!!\" }\n"
+   "};\n",
+   Style);

owenpan wrote:
> 
This is consistent with basically every single adjacent test in this function.  
While I agree that this is unnecessary, in general I error on the side of 
consistency with the surrounding tests.  I'll defer to the maintainers, just 
wanted to make sure this is actually the preferred change given the numerous 
adjacent tests with this form.


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

https://reviews.llvm.org/D158795

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


[PATCH] D158795: Fix AlignArrayOfStructures + Cpp11BracedListStyle=false

2023-08-25 Thread Owen Pan via Phabricator via cfe-commits
owenpan added inline comments.



Comment at: clang/lib/Format/WhitespaceManager.cpp:1247
 if (Previous && Previous->isNot(TT_LineComment)) {
-  Changes[Next->Index].Spaces = 0;
+  Changes[Next->Index].Spaces = BracePadding;
   Changes[Next->Index].NewlinesBefore = 0;

owenpan wrote:
> Can we assert that `Spaces == 0`? If not, we should add a test case.
We can't assert that, but setting `Spaces` here seems superfluous as it's set 
correctly below anyways?


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

https://reviews.llvm.org/D158795

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


[PATCH] D158795: Fix AlignArrayOfStructures + Cpp11BracedListStyle=false

2023-08-25 Thread Owen Pan via Phabricator via cfe-commits
owenpan added a comment.

> I'm not exactly sure why this function needs to override the Spaces as it 
> seems to generally already be set to either 0 or 1 according to the other 
> formatting settings

If so, can we address the issue without the "explicit fix"?




Comment at: clang/lib/Format/WhitespaceManager.cpp:1229
 
+  const auto BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
   auto &Cells = CellDescs.Cells;





Comment at: clang/lib/Format/WhitespaceManager.cpp:1247
 if (Previous && Previous->isNot(TT_LineComment)) {
-  Changes[Next->Index].Spaces = 0;
+  Changes[Next->Index].Spaces = BracePadding;
   Changes[Next->Index].NewlinesBefore = 0;

Can we assert that `Spaces == 0`? If not, we should add a test case.



Comment at: clang/lib/Format/WhitespaceManager.cpp:1303
 
+  const auto BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
   auto &Cells = CellDescs.Cells;

Ditto.



Comment at: clang/unittests/Format/FormatTest.cpp:20888
+   "  {  7, 5,\"!!\" }\n"
+   "};\n",
+   Style);





Comment at: clang/unittests/Format/FormatTest.cpp:21130
+   "  { 7,  5, \"!!\"}\n"
+   "};\n",
+   Style);

Ditto.


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

https://reviews.llvm.org/D158795

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


[PATCH] D158795: Fix AlignArrayOfStructures + Cpp11BracedListStyle=false

2023-08-24 Thread Galen Elias via Phabricator via cfe-commits
galenelias created this revision.
galenelias added a project: clang-format.
Herald added projects: All, clang.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
galenelias requested review of this revision.

Fixes: https://github.com/llvm/llvm-project/issues/57611

Currently AlignArrayOfStructures=Left is hard coding setting `Spaces` to
`0` for the token following the initial opening brace, but not touching
`Spaces` for the subsequent lines, which leads to the array being
misaligned.  Additionally, it's not adding a space before the trailing
`}` which is generally done when Cpp11BracedListStyle=false.

I'm not exactly sure why this function needs to override the `Spaces` as
it seems to generally already be set to either `0` or `1` according to
the other formatting settings, but I'm going with an explicit fix where
I just force the padding to `1` when Cpp11BracedListStyle=false.

AlignArrayOfStructures=Right doesn't have any alignment problems, but
isn't adding the expected padding around the braces either, so I'm
giving that the same treatment.


https://reviews.llvm.org/D158795

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
@@ -20880,6 +20880,16 @@
"});\n",
Style);
 
+  Style.Cpp11BracedListStyle = false;
+  verifyFormat("struct test demo[] = {\n"
+   "  { 56,23, \"hello\" },\n"
+   "  { -1, 93463, \"world\" },\n"
+   "  {  7, 5,\"!!\" }\n"
+   "};\n",
+   Style);
+
+  Style.Cpp11BracedListStyle = true;
+
   Style.ColumnLimit = 0;
   EXPECT_EQ(
   "test demo[] = {\n"
@@ -21112,6 +21122,15 @@
"});\n",
Style);
 
+  Style.Cpp11BracedListStyle = false;
+  verifyFormat("struct test demo[] = {\n"
+   "  { 56, 23,\"hello\" },\n"
+   "  { -1, 93463, \"world\" },\n"
+   "  { 7,  5, \"!!\"}\n"
+   "};\n",
+   Style);
+  Style.Cpp11BracedListStyle = true;
+
   Style.ColumnLimit = 0;
   EXPECT_EQ(
   "test demo[] = {\n"
Index: clang/lib/Format/WhitespaceManager.cpp
===
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -1226,6 +1226,7 @@
   if (!CellDescs.isRectangular())
 return;
 
+  const auto BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
   auto &Cells = CellDescs.Cells;
   // Now go through and fixup the spaces.
   auto *CellIter = Cells.begin();
@@ -1243,7 +1244,7 @@
   do {
 const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
 if (Previous && Previous->isNot(TT_LineComment)) {
-  Changes[Next->Index].Spaces = 0;
+  Changes[Next->Index].Spaces = BracePadding;
   Changes[Next->Index].NewlinesBefore = 0;
 }
 Next = Next->NextColumnElement;
@@ -1276,7 +1277,7 @@
   NetWidth;
   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 += (i > 0) ? 1 : BracePadding;
   }
   alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
   for (const auto *Next = CellIter->NextColumnElement; Next;
@@ -1285,7 +1286,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 += (i > 0) ? 1 : BracePadding;
 }
 alignToStartOfCell(Next->Index, Next->EndIndex);
   }
@@ -1299,12 +1300,13 @@
   if (!CellDescs.isRectangular())
 return;
 
+  const auto BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
   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.
   if (Changes[CellIter->Index].NewlinesBefore == 0)
-Changes[CellIter->Index].Spaces = 0;
+Changes[CellIter->Index].Spaces = BracePadding;
   else
 Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces;
   ++CellIter;
@@ -1317,7 +1319,7 @@
 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 : BracePadding);
 }
 auto RowCount = 1U;
 auto Offset = std::distance(Cells