[PATCH] D145813: [clang-format] Respect Cpp11BraceListStyle when aligning array of structures

Hoe Hao Cheng via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 10 10:11:50 PST 2023


hch12907 created this revision.
hch12907 added a reviewer: owenpan.
Herald added a project: All.
hch12907 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When Cpp11BraceListStyle is set to false and AlignArrayOfStructures is
set, the first and last elements of a braced list should not be joined
to the brace.

Fixes #57611.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145813

Files:
  clang/lib/Format/WhitespaceManager.cpp


Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -1168,6 +1168,10 @@
         auto MaxNetWidth = getMaximumNetWidth(
             Cells.begin(), CellIter, CellDescs.InitialSpaces,
             CellDescs.CellCounts[0], CellDescs.CellCounts.size());
+        // If Cpp11BraceListStyle is false, the braces need to be space-separated from
+        // the elements.
+        if (!Style.Cpp11BracedListStyle)
+          MaxNetWidth += 1;
         if (ThisNetWidth < MaxNetWidth)
           Changes[CellIter->Index].Spaces = (MaxNetWidth - ThisNetWidth);
         auto RowCount = 1U;
@@ -1186,9 +1190,10 @@
       auto ThisWidth =
           calculateCellWidth(CellIter->Index, CellIter->EndIndex, true) +
           NetWidth;
+      auto BeginSpace = (i > 0) ? 1 : (Style.Cpp11BracedListStyle ? 0 : 1);
       if (Changes[CellIter->Index].NewlinesBefore == 0) {
         Changes[CellIter->Index].Spaces = (CellWidth - (ThisWidth + NetWidth));
-        Changes[CellIter->Index].Spaces += (i > 0) ? 1 : 0;
+        Changes[CellIter->Index].Spaces += BeginSpace;
       }
       alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
       for (const auto *Next = CellIter->NextColumnElement; Next;
@@ -1197,7 +1202,7 @@
             calculateCellWidth(Next->Index, Next->EndIndex, true) + NetWidth;
         if (Changes[Next->Index].NewlinesBefore == 0) {
           Changes[Next->Index].Spaces = (CellWidth - ThisWidth);
-          Changes[Next->Index].Spaces += (i > 0) ? 1 : 0;
+          Changes[Next->Index].Spaces += BeginSpace;
         }
         alignToStartOfCell(Next->Index, Next->EndIndex);
       }
@@ -1214,9 +1219,10 @@
   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.
+  // The first cell needs to be against the left brace, unless
+  // Cpp11BracedListStyle is enabled.
   if (Changes[CellIter->Index].NewlinesBefore == 0)
-    Changes[CellIter->Index].Spaces = 0;
+    Changes[CellIter->Index].Spaces = Style.Cpp11BracedListStyle ? 0 : 1;
   else
     Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces;
   ++CellIter;
@@ -1229,7 +1235,9 @@
     if (Changes[CellIter->Index].NewlinesBefore == 0) {
       Changes[CellIter->Index].Spaces =
           MaxNetWidth - ThisNetWidth +
-          (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1 : 0);
+          (Changes[CellIter->Index].Tok->isNot(tok::r_brace)
+               ? 1
+               : (Style.Cpp11BracedListStyle ? 0 : 1));
     }
     auto RowCount = 1U;
     auto Offset = std::distance(Cells.begin(), CellIter);
@@ -1243,7 +1251,9 @@
       if (Changes[Next->Index].NewlinesBefore == 0) {
         Changes[Next->Index].Spaces =
             MaxNetWidth - ThisNetWidth +
-            (Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : 0);
+            (Changes[Next->Index].Tok->isNot(tok::r_brace)
+                 ? 1
+                 : (Style.Cpp11BracedListStyle ? 0 : 1));
       }
       ++RowCount;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145813.504202.patch
Type: text/x-patch
Size: 3181 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230310/432e5bb3/attachment.bin>


More information about the cfe-commits mailing list