Skip to content

Commit 2fc2ee1

Browse files
authored
[clang-format] Fix poor spacing in AlignArrayOfStructures: Left (#77868)
Fixes #62904 `AlignArrayOfStructures: Left` combined with `SpacesInParentheses: true` causes the first cell of every row to have 1 additional space. We were only setting the first cell of the first row to be against the left brace, now every row will be against the left brace.
1 parent 85a8e5c commit 2fc2ee1

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,11 +1366,12 @@ void WhitespaceManager::alignArrayInitializersLeftJustified(
13661366
auto &Cells = CellDescs.Cells;
13671367
// Now go through and fixup the spaces.
13681368
auto *CellIter = Cells.begin();
1369-
// The first cell needs to be against the left brace.
1370-
if (Changes[CellIter->Index].NewlinesBefore == 0)
1371-
Changes[CellIter->Index].Spaces = BracePadding;
1372-
else
1373-
Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces;
1369+
// The first cell of every row needs to be against the left brace.
1370+
for (const auto *Next = CellIter; Next; Next = Next->NextColumnElement) {
1371+
auto &Change = Changes[Next->Index];
1372+
Change.Spaces =
1373+
Change.NewlinesBefore == 0 ? BracePadding : CellDescs.InitialSpaces;
1374+
}
13741375
++CellIter;
13751376
for (auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
13761377
auto MaxNetWidth = getMaximumNetWidth(

clang/unittests/Format/FormatTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21331,6 +21331,14 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2133121331
"00000000000000000000000000000000000000000000000000000000\" },\n"
2133221332
"};",
2133321333
Style);
21334+
21335+
Style.SpacesInParens = FormatStyle::SIPO_Custom;
21336+
Style.SpacesInParensOptions.Other = true;
21337+
verifyFormat("Foo foo[] = {\n"
21338+
" {1, 1},\n"
21339+
" {1, 1},\n"
21340+
"};",
21341+
Style);
2133421342
}
2133521343

2133621344
TEST_F(FormatTest, UnderstandsPragmas) {

0 commit comments

Comments
 (0)