From 0ee107b3775e11fcee4d09f50961152983dedb9e Mon Sep 17 00:00:00 2001
From: Daniel Jasper <djasper@google.com>
Date: Tue, 24 Dec 2013 13:31:25 +0000
Subject: [PATCH] clang-format: (WebKit) Disallow 1-line constructors with
 initializers.

Before:
  Constructor() : a(a) {}

After:
  Constructor()
      : a(a)
  {
  }

This style guide is pretty precise about this.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@197980 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Format/Format.cpp           | 21 ++++++++++++++++-----
 unittests/Format/FormatTest.cpp | 28 ++++++++++++++++++++++++----
 2 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index cff6fc5f6d9..b7b41c9741a 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -480,7 +480,7 @@ public:
                            SmallVectorImpl<AnnotatedLine *>::const_iterator I,
                            SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
     // We can never merge stuff if there are trailing line comments.
-    AnnotatedLine *TheLine = *I;
+    const AnnotatedLine *TheLine = *I;
     if (TheLine->Last->Type == TT_LineComment)
       return 0;
 
@@ -498,7 +498,8 @@ public:
     if (I + 1 == E || I[1]->Type == LT_Invalid)
       return 0;
 
-    if (TheLine->Last->Type == TT_FunctionLBrace) {
+    if (TheLine->Last->Type == TT_FunctionLBrace &&
+        TheLine->First != TheLine->Last) {
       return Style.AllowShortFunctionsOnASingleLine
                  ? tryMergeSimpleBlock(I, E, Limit)
                  : 0;
@@ -510,9 +511,11 @@ public:
     }
     if (I[1]->First->Type == TT_FunctionLBrace &&
         Style.BreakBeforeBraces != FormatStyle::BS_Attach) {
-      // Reduce the column limit by the number of spaces we need to insert
-      // around braces.
-      Limit = Limit > 3 ? Limit - 3 : 0;
+      // Check for Limit <= 2 to accomodate for the " {".
+      if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
+        return 0;
+      Limit -= 2;
+
       unsigned MergedLines = 0;
       if (Style.AllowShortFunctionsOnASingleLine) {
         MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
@@ -641,6 +644,14 @@ private:
     return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
   }
 
+  bool containsMustBreak(const AnnotatedLine *Line) {
+    for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
+      if (Tok->MustBreakBefore)
+        return true;
+    }
+    return false;
+  }
+
   const FormatStyle &Style;
 };
 
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 65124a80575..1ef701a6eb2 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -6997,6 +6997,16 @@ TEST_F(FormatTest, AllmanBraceBreaking) {
                "}\n",
                BreakBeforeBrace);
 
+  BreakBeforeBrace.ColumnLimit = 19;
+  verifyFormat("void f() { int i; }", BreakBeforeBrace);
+  BreakBeforeBrace.ColumnLimit = 18;
+  verifyFormat("void f()\n"
+               "{\n"
+               "  int i;\n"
+               "}",
+               BreakBeforeBrace);
+  BreakBeforeBrace.ColumnLimit = 80;
+
   FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace;
   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
@@ -7716,16 +7726,26 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) {
                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
                "                               aaaaaaaaaaaaaa)\n"
-               "    , aaaaaaaaaaaaaaaaaaaaaaa() {}",
+               "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
+               "{\n"
+               "}",
                Style);
   verifyFormat("SomeClass::Constructor()\n"
-               "    : a(a) {}",
+               "    : a(a)\n"
+               "{\n"
+               "}",
                Style);
+  EXPECT_EQ("SomeClass::Constructor()\n"
+            "    : a(a)\n"
+            "{\n"
+            "}",
+            format("SomeClass::Constructor():a(a){}", Style));
   verifyFormat("SomeClass::Constructor()\n"
                "    : a(a)\n"
                "    , b(b)\n"
-               "    , c(c) {}",
-               Style);
+               "    , c(c)\n"
+               "{\n"
+               "}", Style);
   verifyFormat("SomeClass::Constructor()\n"
                "    : a(a)\n"
                "{\n"
-- 
GitLab