[clang] [clang-format] Fix poor spacing in `AlignArrayOfStructures: Left` (PR #77868)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 11 19:26:02 PST 2024


https://github.com/XDeme created https://github.com/llvm/llvm-project/pull/77868

Fixes llvm/llvm-project#62904

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.

>From 9f705be37183ca030e7720707f75ffbf9feb670a Mon Sep 17 00:00:00 2001
From: XDeme <fernando.tagawa.gamail.com at gmail.com>
Date: Fri, 12 Jan 2024 00:18:56 -0300
Subject: [PATCH] [clang-format] Fix poor spacing in `AlignArrayOfStructures:
 Left` Fixes llvm/llvm-project#62904

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.
---
 clang/lib/Format/WhitespaceManager.cpp | 11 ++++++-----
 clang/unittests/Format/FormatTest.cpp  |  8 ++++++++
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index f1d176f182ffa4..d91a212d357689 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1366,11 +1366,12 @@ void WhitespaceManager::alignArrayInitializersLeftJustified(
   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 = BracePadding;
-  else
-    Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces;
+  // The first cell of every row needs to be against the left brace.
+  for (const auto *Next = CellIter; Next; Next = Next->NextColumnElement)
+    if (Changes[Next->Index].NewlinesBefore == 0)
+      Changes[Next->Index].Spaces = BracePadding;
+    else
+      Changes[Next->Index].Spaces = CellDescs.InitialSpaces;
   ++CellIter;
   for (auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
     auto MaxNetWidth = getMaximumNetWidth(
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 8f115fb8cbf0fb..4d44a1f0a9ff97 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21325,6 +21325,14 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
       "00000000000000000000000000000000000000000000000000000000\" },\n"
       "};",
       Style);
+
+  Style.SpacesInParens = FormatStyle::SIPO_Custom;
+  Style.SpacesInParensOptions.Other = true;
+  verifyFormat("Foo foo[] = {\n"
+               "    {1, 1},\n"
+               "    {1, 1},\n"
+               "};",
+               Style);
 }
 
 TEST_F(FormatTest, UnderstandsPragmas) {



More information about the cfe-commits mailing list