Better support for constructor initializers.

We used to format initializers like this (with a sort of hacky implementation):
Constructor()
    : Val1(A),
      Val2(B) {

and now format like this (with a somewhat better solution):
Constructor()
    : Val1(A), Val2(B) {

assuming this would not fit on a single line. Also added tests.

As a side effect we now first analyze whether an UnwrappedLine needs to be
split at all. If not, not splitting it is the best solution by definition. As
this should be a very common case in normal code, not exploring the entire
solution space can provide significant speedup.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@170457 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index fef56ca..c9cd825 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -374,6 +374,41 @@
       "    parameter, parameter, parameter)), SecondLongCall(parameter));");
 }
 
+TEST_F(FormatTest, ConstructorInitializers) {
+  verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}");
+
+  verifyFormat(
+      "SomeClass::Constructor()\n"
+      "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
+      "}");
+
+  verifyFormat(
+      "SomeClass::Constructor()\n"
+      "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
+      "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
+      "}");
+
+  verifyFormat("Constructor()\n"
+               "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
+               "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+               "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
+               "      aaaaaaaaaaaaaaaaaaaaaaa() {\n"
+               "}");
+
+  // Here a line could be saved by splitting the second initializer onto two
+  // lines, but that is not desireable.
+  verifyFormat("Constructor()\n"
+               "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
+               "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
+               "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
+               "}");
+
+  verifyGoogleFormat("MyClass::MyClass(int var)\n"
+                     "    : some_var_(var),  // 4 space indent\n"
+                     "      some_other_var_(var + 1) {  // lined up\n"
+                     "}");
+}
+
 TEST_F(FormatTest, BreaksAsHighAsPossible) {
   verifyFormat(
       "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
@@ -461,13 +496,11 @@
 }
 
 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
-  verifyFormat(
-      "LoooooooooooooooooooooooooooooooooooooongObject\n"
-      "    .looooooooooooooooooooooooooooooooooooooongFunction();");
+  verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
+               "    .looooooooooooooooooooooooooooooooooooooongFunction();");
 
-  verifyFormat(
-      "LoooooooooooooooooooooooooooooooooooooongObject\n"
-      "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
+  verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
+               "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
 
   verifyFormat(
       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
@@ -485,10 +518,9 @@
       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
 
-  verifyFormat(
-      "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
-      "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
-      "}");
+  verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
+               "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
+               "}");
 }
 
 TEST_F(FormatTest, UnderstandsTemplateParameters) {