[PATCH] D24395: Align declarations that are preceded by different number of commas.

Nikola Smiljanić via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 9 07:07:29 PDT 2016


nikola created this revision.
nikola added a reviewer: djasper.
nikola added a subscriber: cfe-commits.
Herald added a subscriber: klimek.

Having a template with more than one template argument breaks alignment of consecutive declarations. Something like this won't be correctly aligned:

int x;
std::pair<int, bool> y;

https://reviews.llvm.org/D24395

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

Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -9310,6 +9310,10 @@
   verifyFormat("int      oneTwoThree{0}; // comment\n"
                "unsigned oneTwo;         // comment",
                Alignment);
+  verifyFormat("template <typename T, typename U> struct pair {};\n"
+               "int                a;\n"
+               "pair<double, char> b;",
+               Alignment);
   EXPECT_EQ("float const a = 5;\n"
             "\n"
             "int oneTwoThree = 123;",
Index: lib/Format/WhitespaceManager.cpp
===================================================================
--- lib/Format/WhitespaceManager.cpp
+++ lib/Format/WhitespaceManager.cpp
@@ -197,7 +197,8 @@
 // finalize the previous sequence.
 template <typename F>
 static void AlignTokens(const FormatStyle &Style, F &&Matches,
-                        SmallVector<WhitespaceManager::Change, 16> &Changes) {
+                        SmallVector<WhitespaceManager::Change, 16> &Changes,
+                        bool AligningAssignments) {
   unsigned MinColumn = 0;
   unsigned MaxColumn = UINT_MAX;
 
@@ -214,9 +215,9 @@
   unsigned NestingLevelOfLastMatch = 0;
   unsigned NestingLevel = 0;
 
-  // Keep track of the number of commas before the matching tokens, we will only
-  // align a sequence of matching tokens if they are preceded by the same number
-  // of commas.
+  // Keep track of the number of commas before the matching tokens, when
+  // aligning assignments we will only align a sequence of matching tokens
+  // if they are preceded by the same number of commas.
   unsigned CommasBeforeLastMatch = 0;
   unsigned CommasBeforeMatch = 0;
 
@@ -271,10 +272,10 @@
       continue;
 
     // If there is more than one matching token per line, or if the number of
-    // preceding commas, or the scope depth, do not match anymore, end the
-    // sequence.
-    if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch ||
-        NestingLevel != NestingLevelOfLastMatch)
+    // preceding commas when aligning assignments, or the scope depth, do not
+    // match anymore, end the sequence.
+    if (FoundMatchOnLine || NestingLevel != NestingLevelOfLastMatch ||
+        (AligningAssignments && CommasBeforeMatch != CommasBeforeLastMatch))
       AlignCurrentSequence();
 
     CommasBeforeLastMatch = CommasBeforeMatch;
@@ -321,7 +322,7 @@
 
                 return C.Kind == tok::equal;
               },
-              Changes);
+              Changes, true);
 }
 
 void WhitespaceManager::alignConsecutiveDeclarations() {
@@ -336,7 +337,7 @@
   //   SomeVeryLongType const& v3;
 
   AlignTokens(Style, [](Change const &C) { return C.IsStartOfDeclName; },
-              Changes);
+              Changes, false);
 }
 
 void WhitespaceManager::alignTrailingComments() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24395.70830.patch
Type: text/x-patch
Size: 2912 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160909/da2292d1/attachment-0001.bin>


More information about the cfe-commits mailing list