Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Fix poor spacing in AlignArrayOfStructures: Left #77868

Merged
merged 3 commits into from
Jan 20, 2024

Conversation

XDeme1
Copy link
Contributor

@XDeme1 XDeme1 commented Jan 12, 2024

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.

Fixes llvm#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.
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 12, 2024

@llvm/pr-subscribers-clang-format

Author: None (XDeme)

Changes

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

@XDeme1
Copy link
Contributor Author

XDeme1 commented Jan 20, 2024

@HazardyKnusperkeks ping

Copy link
Contributor

@HazardyKnusperkeks HazardyKnusperkeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, seems I've missed this change.

Co-authored-by: Owen Pan <owenpiano@gmail.com>
Copy link

github-actions bot commented Jan 20, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@owenca owenca merged commit 2fc2ee1 into llvm:main Jan 20, 2024
3 of 4 checks passed
@XDeme1 XDeme1 deleted the 62904 branch January 20, 2024 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-format poor spacing with braces initializer
4 participants