Index: lib/Format/WhitespaceManager.cpp =================================================================== --- lib/Format/WhitespaceManager.cpp +++ lib/Format/WhitespaceManager.cpp @@ -165,8 +165,10 @@ unsigned StartOfSequence = 0; unsigned EndOfSequence = 0; bool FoundAssignmentOnLine = false; - bool FoundLeftBraceOnLine = false; - bool FoundLeftParenOnLine = false; + unsigned LeftBracesOnLine = 0; + unsigned LeftParensOnLine = 0; + unsigned CommasOnPrevLine = 0; + unsigned CommasOnLine = 0; // Aligns a sequence of assignment tokens, on the MinColumn column. // @@ -186,12 +188,14 @@ 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 // here. if (Changes[i].NewlinesBefore > 1 || !FoundAssignmentOnLine || - FoundLeftBraceOnLine || FoundLeftParenOnLine) { + LeftBracesOnLine > 0 || LeftParensOnLine > 0) { // NB: In the latter case, the sequence should end at the beggining of // the previous line, but it doesn't really matter as there is no // assignment on it @@ -199,8 +203,8 @@ } FoundAssignmentOnLine = false; - FoundLeftBraceOnLine = false; - FoundLeftParenOnLine = false; + LeftBracesOnLine = 0; + LeftParensOnLine = 0; } // If there is more than one "=" per line, or if the "=" appears first on @@ -210,23 +214,28 @@ Changes[i + 1].NewlinesBefore > 0)) { AlignSequence(); } else if (Changes[i].Kind == tok::r_brace) { - if (!FoundLeftBraceOnLine) + if (LeftBracesOnLine == 0) AlignSequence(); - FoundLeftBraceOnLine = false; + else + LeftBracesOnLine--; } else if (Changes[i].Kind == tok::l_brace) { - FoundLeftBraceOnLine = true; + LeftBracesOnLine++; if (!FoundAssignmentOnLine) AlignSequence(); } else if (Changes[i].Kind == tok::r_paren) { - if (!FoundLeftParenOnLine) + if (LeftParensOnLine == 0) AlignSequence(); - FoundLeftParenOnLine = false; + else + LeftParensOnLine--; } else if (Changes[i].Kind == tok::l_paren) { - FoundLeftParenOnLine = true; + LeftParensOnLine++; if (!FoundAssignmentOnLine) AlignSequence(); - } else if (!FoundAssignmentOnLine && !FoundLeftBraceOnLine && - !FoundLeftParenOnLine && Changes[i].Kind == tok::equal) { + } else if (Changes[i].Kind == tok::comma) { + if (!FoundAssignmentOnLine) + CommasOnLine++; + } else if (!FoundAssignmentOnLine && LeftBracesOnLine == 0 && + LeftParensOnLine == 0 && Changes[i].Kind == tok::equal) { FoundAssignmentOnLine = true; if (StartOfSequence == 0) StartOfSequence = i; @@ -237,7 +246,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; } @@ -296,8 +306,10 @@ unsigned StartOfSequence = 0; unsigned EndOfSequence = 0; bool FoundDeclarationOnLine = false; - bool FoundLeftBraceOnLine = false; - bool FoundLeftParenOnLine = false; + unsigned LeftBracesOnLine = 0; + unsigned LeftParensOnLine = 0; + unsigned CommasOnPrevLine = 0; + unsigned CommasOnLine = 0; auto AlignSequence = [&] { if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) @@ -310,33 +322,40 @@ 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) + LeftBracesOnLine > 0 || LeftParensOnLine > 0) AlignSequence(); FoundDeclarationOnLine = false; - FoundLeftBraceOnLine = false; - FoundLeftParenOnLine = false; + LeftBracesOnLine = 0; + LeftParensOnLine = 0; } if (Changes[i].Kind == tok::r_brace) { - if (!FoundLeftBraceOnLine) + if (!LeftBracesOnLine) AlignSequence(); - FoundLeftBraceOnLine = false; + else + LeftBracesOnLine--; } else if (Changes[i].Kind == tok::l_brace) { - FoundLeftBraceOnLine = true; + LeftBracesOnLine++; if (!FoundDeclarationOnLine) AlignSequence(); } else if (Changes[i].Kind == tok::r_paren) { - if (!FoundLeftParenOnLine) + if (!LeftParensOnLine) AlignSequence(); - FoundLeftParenOnLine = false; + else + LeftParensOnLine--; } else if (Changes[i].Kind == tok::l_paren) { - FoundLeftParenOnLine = true; + LeftParensOnLine++; if (!FoundDeclarationOnLine) AlignSequence(); - } else if (!FoundDeclarationOnLine && !FoundLeftBraceOnLine && - !FoundLeftParenOnLine && Changes[i].IsStartOfDeclName) { + } else if (Changes[i].Kind == tok::comma) { + if (!FoundDeclarationOnLine) + CommasOnLine++; + } else if (!FoundDeclarationOnLine && LeftBracesOnLine == 0 && + LeftParensOnLine == 0 && Changes[i].IsStartOfDeclName) { FoundDeclarationOnLine = true; if (StartOfSequence == 0) StartOfSequence = i; @@ -347,7 +366,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; } Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -8703,6 +8703,19 @@ " loooooooooooooooooooooongParameterB);\n" "int j = 2;", Alignment); + + verifyFormat("template \n" + "auto foo() {}\n", + Alignment); + verifyFormat("int a, b = 1;\n" + "int c = 2;\n" + "int dd = 3;\n", + Alignment); + verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" + "float b[1][] = {{3.f}};\n", + Alignment); } TEST_F(FormatTest, AlignConsecutiveDeclarations) { @@ -8903,6 +8916,29 @@ "int myvar = 1;", Alignment); Alignment.ColumnLimit = 80; + Alignment.AlignConsecutiveAssignments = false; + + verifyFormat( + "template \n" + "auto foo() {}\n", + Alignment); + verifyFormat("float a, b = 1;\n" + "int c = 2;\n" + "int dd = 3;\n", + Alignment); + verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" + "float b[1][] = {{3.f}};\n", + Alignment); + Alignment.AlignConsecutiveAssignments = true; + verifyFormat("float a, b = 1;\n" + "int c = 2;\n" + "int dd = 3;\n", + Alignment); + verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" + "float b[1][] = {{3.f}};\n", + Alignment); + Alignment.AlignConsecutiveAssignments = false; } TEST_F(FormatTest, LinuxBraceBreaking) {