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

This fixes bug #25329, as well as misalignments like the following:

    int a, b = 2;
    int c    = 3;

http://reviews.llvm.org/D14325

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

Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -8703,6 +8703,16 @@
       "                          loooooooooooooooooooooongParameterB);\n"
       "int j = 2;",
       Alignment);
+
+  verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
+               "          typename B   = very_long_type_name_1,\n"
+               "          typename T_2 = very_long_type_name_2>\n"
+               "auto foo() {}\n",
+               Alignment);
+  verifyFormat("int a, b = 1;\n"
+               "int c  = 2;\n"
+               "int dd = 3;\n",
+               Alignment);
 }
 
 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
@@ -8903,6 +8913,23 @@
                "int              myvar = 1;",
                Alignment);
   Alignment.ColumnLimit = 80;
+  Alignment.AlignConsecutiveAssignments = false;
+
+  verifyFormat(
+      "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
+      "          typename LongType, typename B>\n"
+      "auto foo() {}\n",
+      Alignment);
+  verifyFormat("float a, b = 1;\n"
+               "int   c = 2;\n"
+               "int   dd = 3;\n",
+               Alignment);
+  Alignment.AlignConsecutiveAssignments = true;
+  verifyFormat("float a, b = 1;\n"
+               "int   c  = 2;\n"
+               "int   dd = 3;\n",
+               Alignment);
+  Alignment.AlignConsecutiveAssignments = false;
 }
 
 TEST_F(FormatTest, LinuxBraceBreaking) {
Index: lib/Format/WhitespaceManager.cpp
===================================================================
--- lib/Format/WhitespaceManager.cpp
+++ lib/Format/WhitespaceManager.cpp
@@ -167,6 +167,8 @@
   bool FoundAssignmentOnLine = false;
   bool FoundLeftBraceOnLine = false;
   bool FoundLeftParenOnLine = false;
+  unsigned CommasOnPrevLine = 0;
+  unsigned CommasOnLine = 0;
 
   // Aligns a sequence of assignment tokens, on the MinColumn column.
   //
@@ -186,6 +188,8 @@
 
   for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
     if (Changes[i].NewlinesBefore != 0) {
+      CommasOnPrevLine = CommasOnLine;
+      CommasOnLine = 0;
       EndOfSequence = i;
       // If there is a blank line, if the last line didn't contain any
       // assignment, or if we found an open brace or paren, the sequence ends
@@ -225,6 +229,9 @@
       FoundLeftParenOnLine = true;
       if (!FoundAssignmentOnLine)
         AlignSequence();
+    } else if (Changes[i].Kind == tok::comma) {
+      if (!FoundAssignmentOnLine)
+        CommasOnLine++;
     } else if (!FoundAssignmentOnLine && !FoundLeftBraceOnLine &&
                !FoundLeftParenOnLine && Changes[i].Kind == tok::equal) {
       FoundAssignmentOnLine = true;
@@ -237,7 +244,8 @@
         LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
       unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
 
-      if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) {
+      if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
+          CommasOnPrevLine != CommasOnLine) {
         AlignSequence();
         StartOfSequence = i;
       }
@@ -298,6 +306,8 @@
   bool FoundDeclarationOnLine = false;
   bool FoundLeftBraceOnLine = false;
   bool FoundLeftParenOnLine = false;
+  unsigned CommasOnPrevLine = 0;
+  unsigned CommasOnLine = 0;
 
   auto AlignSequence = [&] {
     if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
@@ -310,6 +320,8 @@
 
   for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
     if (Changes[i].NewlinesBefore != 0) {
+      CommasOnPrevLine = CommasOnLine;
+      CommasOnLine = 0;
       EndOfSequence = i;
       if (Changes[i].NewlinesBefore > 1 || !FoundDeclarationOnLine ||
           FoundLeftBraceOnLine || FoundLeftParenOnLine)
@@ -335,6 +347,9 @@
       FoundLeftParenOnLine = true;
       if (!FoundDeclarationOnLine)
         AlignSequence();
+    } else if (Changes[i].Kind == tok::comma) {
+      if (!FoundDeclarationOnLine)
+        CommasOnLine++;
     } else if (!FoundDeclarationOnLine && !FoundLeftBraceOnLine &&
                !FoundLeftParenOnLine && Changes[i].IsStartOfDeclName) {
       FoundDeclarationOnLine = true;
@@ -347,7 +362,8 @@
         LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
       unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
 
-      if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) {
+      if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
+          CommasOnPrevLine != CommasOnLine) {
         AlignSequence();
         StartOfSequence = i;
       }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to