[PATCH] D125162: [clang-format] fix alignment w/o binpacked args

cha5on via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat May 7 04:07:19 PDT 2022


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

The combination of

- AlignConsecutiveAssignments.Enabled = true
- BinPackArguments = false

would result in the first continuation line of a braced-init-list being
improperly indented (missing a shift) when in a continued function call.
Indentation was also wrong for braced-init-lists continuing a
direct-list-initialization.  Check for opening braced lists in
continuation and ensure that the correct shift occurs.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125162

Files:
  clang/lib/Format/WhitespaceManager.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -17280,6 +17280,46 @@
   //              "    ccc ? aaaaa : bbbbb,\n"
   //              "    dddddddddddddddddddddddddd);",
   //              Alignment);
+
+  // Confirm proper handling of AlignConsecutiveAssignments with
+  // BinPackArguments.
+  Alignment = getLLVMStyleWithColumns(50);
+  Alignment.AlignConsecutiveAssignments.Enabled = true;
+  Alignment.BinPackArguments = false;
+  {
+    const char *Expected = "struct A {\n"
+                           "  int a;\n"
+                           "  int b;\n"
+                           "};\n"
+                           "struct B {\n"
+                           "  A a1;\n"
+                           "  A a2;\n"
+                           "};\n"
+                           "int a_longer_name_for_wrap = 1;\n"
+                           "// comment to prevent alignment\n"
+                           "int a_long_name = 1;\n"
+                           "auto b          = B({a_long_name, a_long_name},\n"
+                           "                    {a_longer_name_for_wrap,\n"
+                           "                     a_longer_name_for_wrap});";
+
+    const char *ToFormat = "struct A {\n"
+                           "int a;\n"
+                           "int      b;\n"
+                           "};\n"
+                           "struct B{\n"
+                           " A a1;\n"
+                           "  A a2;\n"
+                           "};\n"
+                           "int a_longer_name_for_wrap = 1;\n"
+                           "// comment to prevent alignment\n"
+                           "int a_long_name = 1;\n"
+                           "auto b          = B({a_long_name,\n"
+                           "                     a_long_name},\n"
+                           "         {a_longer_name_for_wrap,\n"
+                           "a_longer_name_for_wrap});";
+
+    verifyFormat(Expected, ToFormat, Alignment);
+  }
 }
 
 TEST_F(FormatTest, AlignConsecutiveBitFields) {
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -371,6 +371,9 @@
             return false;
           if (Changes[ScopeStart].NewlinesBefore > 0)
             return false;
+          if (Changes[i].Tok->is(tok::l_brace) &&
+              Changes[i].Tok->is(BK_BracedInit))
+            return true;
           return Style.BinPackArguments;
         }
 
@@ -387,6 +390,15 @@
             Changes[i].Tok->Previous->is(TT_ConditionalExpr))
           return true;
 
+        // Continued direct-list-initialization using braced list.
+        if (ScopeStart > Start + 1 &&
+            Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
+            Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
+            Changes[i].Tok->is(tok::l_brace) &&
+            Changes[i].Tok->is(BK_BracedInit)) {
+          return true;
+        }
+
         // Continued braced list.
         if (ScopeStart > Start + 1 &&
             Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125162.427841.patch
Type: text/x-patch
Size: 3327 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220507/0337d873/attachment.bin>


More information about the cfe-commits mailing list