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

via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 20 13:23:19 PST 2024


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

>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 1/2] [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) {

>From a0eec9816b91057bb15a7e3fb403e655b563d3e2 Mon Sep 17 00:00:00 2001
From: XDeme <66138117+XDeme at users.noreply.github.com>
Date: Sat, 20 Jan 2024 18:23:13 -0300
Subject: [PATCH 2/2] Apply suggestions from code review

Co-authored-by: Owen Pan <owenpiano at gmail.com>
---
 clang/lib/Format/WhitespaceManager.cpp | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index d91a212d357689..429c26ea261f2a 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1367,11 +1367,10 @@ void WhitespaceManager::alignArrayInitializersLeftJustified(
   // Now go through and fixup the spaces.
   auto *CellIter = Cells.begin();
   // 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;
+  for (const auto *Next = CellIter; Next; Next = Next->NextColumnElement) {
+    auto &Change = Changes[Next->Index];
+    Change.Spaces = Change.NewlinesBefore == 0 ? BracePadding : CellDescs.InitialSpaces;
+  }
   ++CellIter;
   for (auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
     auto MaxNetWidth = getMaximumNetWidth(



More information about the cfe-commits mailing list