[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:29 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: None (XDeme)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/77868.diff


2 Files Affected:

- (modified) clang/lib/Format/WhitespaceManager.cpp (+6-5) 
- (modified) clang/unittests/Format/FormatTest.cpp (+8) 


``````````diff
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) {

``````````

</details>


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


More information about the cfe-commits mailing list