aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGalen Elias <galenelias@gmail.com>2023-08-31 14:13:06 -0700
committerOwen Pan <owenpiano@gmail.com>2023-08-31 14:21:19 -0700
commit58c67e724f718be06ea43c766871135a338c2d55 (patch)
treeb0ef74d333362c965f5247a816a6edc5522d8d92
parentc9ecaf32f6aa92f79a054478df55b4b8c3e53697 (diff)
downloadllvm-58c67e724f718be06ea43c766871135a338c2d55.zip
llvm-58c67e724f718be06ea43c766871135a338c2d55.tar.gz
llvm-58c67e724f718be06ea43c766871135a338c2d55.tar.bz2
[clang-format] Fix AlignArrayOfStructures + Cpp11BracedListStyle=false
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. Fixes #57611. Differential Revision: https://reviews.llvm.org/D158795
-rw-r--r--clang/lib/Format/WhitespaceManager.cpp15
-rw-r--r--clang/unittests/Format/FormatTest.cpp18
2 files changed, 27 insertions, 6 deletions
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 6951c2a..a27c152 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1230,6 +1230,7 @@ void WhitespaceManager::alignArrayInitializersRightJustified(
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 @@ void WhitespaceManager::alignArrayInitializersRightJustified(
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 @@ void WhitespaceManager::alignArrayInitializersRightJustified(
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 @@ void WhitespaceManager::alignArrayInitializersRightJustified(
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 @@ void WhitespaceManager::alignArrayInitializersLeftJustified(
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 @@ void WhitespaceManager::alignArrayInitializersLeftJustified(
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 @@ void WhitespaceManager::alignArrayInitializersLeftJustified(
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;
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6b4a5ce..21b681e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -20651,6 +20651,15 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
"});",
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 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
"});",
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"