[cfe-commits] r173055 - in /cfe/trunk: lib/Format/Format.cpp lib/Format/UnwrappedLineParser.cpp test/Index/comment-c-decls.c test/Index/comment-to-html-xml-conversion.cpp test/Index/format-comment-cdecls.c unittests/Format/FormatTest.cpp

Manuel Klimek klimek at google.com
Mon Jan 21 08:42:44 PST 2013


Author: klimek
Date: Mon Jan 21 10:42:44 2013
New Revision: 173055

URL: http://llvm.org/viewvc/llvm-project?rev=173055&view=rev
Log:
Fixes formatting of empty blocks.

We now only put empty blocks into a single line, if all of:
- all tokens of the structural element fit into a single line
- we're not in a control flow statement

Note that we usually don't put record definitions into a single line, as
there's usually at least one more token (the semicolon) after the
closing brace. This doesn't hold when we are in a context where there is
no semicolon, like "enum E {}".

There were some missing tests around joining lines around the corner
cases of the allowed number of columns, so this patch adds some.

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/test/Index/comment-c-decls.c
    cfe/trunk/test/Index/comment-to-html-xml-conversion.cpp
    cfe/trunk/test/Index/format-comment-cdecls.c
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=173055&r1=173054&r2=173055&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jan 21 10:42:44 2013
@@ -1711,9 +1711,9 @@
     // Check whether the UnwrappedLine can be put onto a single line. If
     // so, this is bound to be the optimal solution (by definition) and we
     // don't need to analyze the entire solution space.
-    if (I->Last->TotalLength >= Limit)
+    if (I->Last->TotalLength > Limit)
       return;
-    Limit -= I->Last->TotalLength + 1; // One space.
+    Limit -= I->Last->TotalLength;
 
     if (I + 1 == E || (I + 1)->Type == LT_Invalid)
       return;
@@ -1738,7 +1738,7 @@
     if (I + 2 != E && (I + 2)->InPPDirective &&
         !(I + 2)->First.FormatTok.HasUnescapedNewline)
       return;
-    if ((I + 1)->Last->TotalLength > Limit)
+    if (1 + (I + 1)->Last->TotalLength > Limit)
       return;
     join(Line, *(++I));
   }
@@ -1755,7 +1755,7 @@
     AnnotatedLine &Line = *I;
     if (Line.Last->isNot(tok::r_paren))
       return;
-    if ((I + 1)->Last->TotalLength > Limit)
+    if (1 + (I + 1)->Last->TotalLength > Limit)
       return;
     if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment)
       return;
@@ -1768,11 +1768,6 @@
   void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
                         std::vector<AnnotatedLine>::iterator E,
                         unsigned Limit){
-    // Check that we still have three lines and they fit into the limit.
-    if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
-        !nextTwoLinesFitInto(I, Limit))
-      return;
-
     // First, check that the current line allows merging. This is the case if
     // we're not in a control flow statement and the last token is an opening
     // brace.
@@ -1788,38 +1783,44 @@
     if (!AllowedTokens)
       return;
 
-    // Second, check that the next line does not contain any braces - if it
-    // does, readability declines when putting it into a single line.
-    const AnnotatedToken *Tok = &(I + 1)->First;
-    if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
-      return;
-    do {
-      if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace))
+    AnnotatedToken *Tok = &(I + 1)->First;
+    if (Tok->Children.empty() && Tok->is(tok::r_brace) &&
+        !Tok->MustBreakBefore && Tok->TotalLength <= Limit) {
+      Tok->SpaceRequiredBefore = false;
+      join(Line, *(I + 1));
+      I += 1;
+    } else {
+      // Check that we still have three lines and they fit into the limit.
+      if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
+          !nextTwoLinesFitInto(I, Limit))
         return;
-      Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
-    } while (Tok != NULL);
 
-    // Last, check that the third line contains a single closing brace.
-    Tok = &(I + 2)->First;
-    if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
-        Tok->MustBreakBefore)
-      return;
+      // Second, check that the next line does not contain any braces - if it
+      // does, readability declines when putting it into a single line.
+      if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
+        return;
+      do {
+        if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace))
+          return;
+        Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
+      } while (Tok != NULL);
+
+      // Last, check that the third line contains a single closing brace.
+      Tok = &(I + 2)->First;
+      if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
+          Tok->MustBreakBefore)
+        return;
 
-    // If the merged line fits, we use that instead and skip the next two lines.
-    Line.Last->Children.push_back((I + 1)->First);
-    while (!Line.Last->Children.empty()) {
-      Line.Last->Children[0].Parent = Line.Last;
-      Line.Last = &Line.Last->Children[0];
+      join(Line, *(I + 1));
+      join(Line, *(I + 2));
+      I += 2;
     }
-
-    join(Line, *(I + 1));
-    join(Line, *(I + 2));
-    I += 2;
   }
 
   bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
                            unsigned Limit) {
-    return (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <= Limit;
+    return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
+           Limit;
   }
 
   void join(AnnotatedLine &A, const AnnotatedLine &B) {

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=173055&r1=173054&r2=173055&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Jan 21 10:42:44 2013
@@ -170,17 +170,15 @@
   assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected");
   nextToken();
 
-  if (!FormatTok.Tok.is(tok::r_brace)) {
-    addUnwrappedLine();
+  addUnwrappedLine();
 
-    Line->Level += AddLevels;
-    parseLevel(/*HasOpeningBrace=*/true);
-    Line->Level -= AddLevels;
+  Line->Level += AddLevels;
+  parseLevel(/*HasOpeningBrace=*/true);
+  Line->Level -= AddLevels;
 
-    if (!FormatTok.Tok.is(tok::r_brace))
-      return true;
+  if (!FormatTok.Tok.is(tok::r_brace))
+    return true;
 
-  }
   nextToken();  // Munch the closing brace.
   return false;
 }

Modified: cfe/trunk/test/Index/comment-c-decls.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/comment-c-decls.c?rev=173055&r1=173054&r2=173055&view=diff
==============================================================================
--- cfe/trunk/test/Index/comment-c-decls.c (original)
+++ cfe/trunk/test/Index/comment-c-decls.c Mon Jan 21 10:42:44 2013
@@ -88,7 +88,7 @@
   Two,
   Three
 };
-// CHECK: <Declaration>enum e {\n}</Declaration>
+// CHECK: <Declaration>enum e {}</Declaration>
 // CHECK: <Declaration>Two</Declaration>
 
 /**

Modified: cfe/trunk/test/Index/comment-to-html-xml-conversion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/comment-to-html-xml-conversion.cpp?rev=173055&r1=173054&r2=173055&view=diff
==============================================================================
--- cfe/trunk/test/Index/comment-to-html-xml-conversion.cpp (original)
+++ cfe/trunk/test/Index/comment-to-html-xml-conversion.cpp Mon Jan 21 10:42:44 2013
@@ -713,7 +713,7 @@
 
 /// Aaa.
 enum comment_to_xml_conversion_15 {
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:6: EnumDecl=comment_to_xml_conversion_15:{{.*}} FullCommentAsXML=[<Enum file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="6"><Name>comment_to_xml_conversion_15</Name><USR>c:@E at comment_to_xml_conversion_15</USR><Declaration>enum comment_to_xml_conversion_15{{( : int)?}} {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Enum>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:6: EnumDecl=comment_to_xml_conversion_15:{{.*}} FullCommentAsXML=[<Enum file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="6"><Name>comment_to_xml_conversion_15</Name><USR>c:@E at comment_to_xml_conversion_15</USR><Declaration>enum comment_to_xml_conversion_15{{( : int)?}} {}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Enum>]
 
   /// Aaa.
   comment_to_xml_conversion_16
@@ -722,7 +722,7 @@
 
 /// Aaa.
 enum class comment_to_xml_conversion_17 {
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:12: EnumDecl=comment_to_xml_conversion_17:{{.*}} FullCommentAsXML=[<Enum file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="12"><Name>comment_to_xml_conversion_17</Name><USR>c:@E at comment_to_xml_conversion_17</USR><Declaration>enum class comment_to_xml_conversion_17 : int {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Enum>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:12: EnumDecl=comment_to_xml_conversion_17:{{.*}} FullCommentAsXML=[<Enum file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="12"><Name>comment_to_xml_conversion_17</Name><USR>c:@E at comment_to_xml_conversion_17</USR><Declaration>enum class comment_to_xml_conversion_17 : int {}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Enum>]
 
   /// Aaa.
   comment_to_xml_conversion_18

Modified: cfe/trunk/test/Index/format-comment-cdecls.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/format-comment-cdecls.c?rev=173055&r1=173054&r2=173055&view=diff
==============================================================================
--- cfe/trunk/test/Index/format-comment-cdecls.c (original)
+++ cfe/trunk/test/Index/format-comment-cdecls.c Mon Jan 21 10:42:44 2013
@@ -83,7 +83,7 @@
   Two,
   Three
 };
-// CHECK: <Declaration>enum e {\n}</Declaration>
+// CHECK: <Declaration>enum e {}</Declaration>
 // CHECK: <Declaration>Two</Declaration>
 
 /**

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=173055&r1=173054&r2=173055&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan 21 10:42:44 2013
@@ -205,7 +205,7 @@
 }
 
 TEST_F(FormatTest, ElseIf) {
-  verifyFormat("if (a) {} else if (b) {}");
+  verifyFormat("if (a) {\n} else if (b) {\n}");
   verifyFormat("if (a)\n"
                "  f();\n"
                "else if (b)\n"
@@ -221,7 +221,7 @@
       "  ;");
   verifyFormat("for (;;)\n"
                "  f();");
-  verifyFormat("for (;;) {}");
+  verifyFormat("for (;;) {\n}");
   verifyFormat("for (;;) {\n"
                "  f();\n"
                "}");
@@ -229,18 +229,18 @@
   verifyFormat(
       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
       "                                          E = UnwrappedLines.end();\n"
-      "     I != E; ++I) {}");
+      "     I != E; ++I) {\n}");
 
   verifyFormat(
       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
-      "     ++IIIII) {}");
+      "     ++IIIII) {\n}");
 }
 
 TEST_F(FormatTest, FormatsWhileLoop) {
-  verifyFormat("while (true) {}");
+  verifyFormat("while (true) {\n}");
   verifyFormat("while (true)\n"
                "  f();");
-  verifyFormat("while () {}");
+  verifyFormat("while () {\n}");
   verifyFormat("while () {\n"
                "  f();\n"
                "}");
@@ -457,7 +457,7 @@
 //===----------------------------------------------------------------------===//
 
 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
-  verifyFormat("class A {};");
+  verifyFormat("class A {\n};");
 }
 
 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
@@ -476,14 +476,14 @@
 }
 
 TEST_F(FormatTest, FormatsDerivedClass) {
-  verifyFormat("class A : public B {};");
-  verifyFormat("class A : public ::B {};");
+  verifyFormat("class A : public B {\n};");
+  verifyFormat("class A : public ::B {\n};");
 }
 
 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
-  verifyFormat("class A {} a, b;");
-  verifyFormat("struct A {} a, b;");
-  verifyFormat("union A {} a;");
+  verifyFormat("class A {\n} a, b;");
+  verifyFormat("struct A {\n} a, b;");
+  verifyFormat("union A {\n} a;");
 }
 
 TEST_F(FormatTest, FormatsEnum) {
@@ -510,19 +510,19 @@
 
 TEST_F(FormatTest, FormatsNamespaces) {
   verifyFormat("namespace some_namespace {\n"
-               "class A {};\n"
+               "class A {\n};\n"
                "void f() { f(); }\n"
                "}");
   verifyFormat("namespace {\n"
-               "class A {};\n"
+               "class A {\n};\n"
                "void f() { f(); }\n"
                "}");
   verifyFormat("inline namespace X {\n"
-               "class A {};\n"
+               "class A {\n};\n"
                "void f() { f(); }\n"
                "}");
   verifyFormat("using namespace some_namespace;\n"
-               "class A {};\n"
+               "class A {\n};\n"
                "void f() { f(); }");
 }
 
@@ -801,6 +801,12 @@
 
 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
   EXPECT_EQ("{}", format("{}"));
+
+  // Negative test for enum.
+  verifyFormat("enum E {\n};");
+
+  // Note that when there's a missing ';', we still join...
+  verifyFormat("enum E {}");
 }
 
 //===----------------------------------------------------------------------===//
@@ -810,7 +816,7 @@
 TEST_F(FormatTest, FormatsFunctionDefinition) {
   verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
                " int h, int j, int f,\n"
-               "       int c, int ddddddddddddd) {}");
+               "       int c, int ddddddddddddd) {\n}");
 }
 
 TEST_F(FormatTest, FormatsAwesomeMethodCall) {
@@ -824,46 +830,49 @@
   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
                getLLVMStyleWithColumns(45));
-  verifyFormat("Constructor()\n"
-               "    : Inttializer(FitsOnTheLine) {}",
+  verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}",
                getLLVMStyleWithColumns(44));
+  verifyFormat("Constructor()\n"
+               "    : Inttializer(FitsOnTheLine) {\n}",
+               getLLVMStyleWithColumns(43));
 
   verifyFormat(
       "SomeClass::Constructor()\n"
-      "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
+      "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
 
   verifyFormat(
       "SomeClass::Constructor()\n"
       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
-      "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
+      "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
   verifyGoogleFormat(
       "SomeClass::Constructor()\n"
       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
-      "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
+      "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
   verifyGoogleFormat(
       "SomeClass::Constructor()\n"
       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),  // Some comment\n"
       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
-      "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
+      "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
 
   verifyFormat(
       "SomeClass::Constructor()\n"
       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
-      "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
+      "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
 
   verifyFormat("Constructor()\n"
                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
-               "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
+               "      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) {}");
+  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"
@@ -876,7 +885,7 @@
   for (unsigned i = 0, e = 80; i != e; ++i) {
     input += "           a,\n";
   }
-  input += "           a) {}";
+  input += "           a) {\n}";
   verifyGoogleFormat(input);
 }
 
@@ -890,11 +899,11 @@
 TEST_F(FormatTest, BreaksDesireably) {
   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
-               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
+               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
 
   verifyFormat(
       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
-      "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
+      "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
 
   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -967,20 +976,20 @@
   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
                "    GUARDED_BY(aaaaaaaaaaaaa);");
   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
-               "    GUARDED_BY(aaaaaaaaaaaaa) {}");
+               "    GUARDED_BY(aaaaaaaaaaaaa) {\n}");
 }
 
 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
   verifyFormat(
       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
-      "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
+      "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
-               "    ccccccccccccccccccccccccc) {}");
+               "    ccccccccccccccccccccccccc) {\n}");
   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
-               "    ccccccccccccccccccccccccc) {}");
+               "    ccccccccccccccccccccccccc) {\n}");
   verifyFormat(
       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
-      "    ccccccccccccccccccccccccc) {}");
+      "    ccccccccccccccccccccccccc) {\n}");
 }
 
 TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
@@ -1077,17 +1086,18 @@
       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   verifyFormat(
       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
-      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
+      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
   verifyFormat(
       "if (a) {\n"
       "  f();\n"
       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
-      "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
+      "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
+      "}");
 
   verifyFormat(
       // FIXME: Does an expression like this ever make sense? If yes, fix.
       "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
-      "    10000000) {}");
+      "    10000000) {\n}");
 }
 
 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
@@ -1115,7 +1125,7 @@
 
   // Here, it is not necessary to wrap at "." or "->".
   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
-               "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
+               "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
   verifyFormat(
       "aaaaaaaaaaa->aaaaaaaaa(\n"
       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
@@ -1174,15 +1184,15 @@
   verifyFormat("f(-1, -2, -3);");
   verifyFormat("a[-1] = 5;");
   verifyFormat("int a = 5 + -2;");
-  verifyFormat("if (i == -1) {}");
-  verifyFormat("if (i != -1) {}");
-  verifyFormat("if (i > -1) {}");
-  verifyFormat("if (i < -1) {}");
+  verifyFormat("if (i == -1) {\n}");
+  verifyFormat("if (i != -1) {\n}");
+  verifyFormat("if (i > -1) {\n}");
+  verifyFormat("if (i < -1) {\n}");
   verifyFormat("++(a->f());");
   verifyFormat("--(a->f());");
   verifyFormat("(a->f())++;");
   verifyFormat("a[42]++;");
-  verifyFormat("if (!(a->f())) {}");
+  verifyFormat("if (!(a->f())) {\n}");
 
   verifyFormat("a-- > b;");
   verifyFormat("b ? -a : c;");
@@ -1341,11 +1351,11 @@
 
 TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
   verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
-               "                  int LoooooooooooooooongParam2) {}");
+               "                  int LoooooooooooooooongParam2) {\n}");
   verifyFormat(
       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
       "                                   SourceLocation L, IdentifierIn *II,\n"
-      "                                   Type *T) {}");
+      "                                   Type *T) {\n}");
 }
 
 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
@@ -1431,10 +1441,10 @@
 }
 
 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
-  verifyFormat("do {}");
-  verifyFormat("do {}\n"
+  verifyFormat("do {\n}");
+  verifyFormat("do {\n}\n"
                "f();");
-  verifyFormat("do {}\n"
+  verifyFormat("do {\n}\n"
                "wheeee(fun);");
   verifyFormat("do {\n"
                "  f();\n"
@@ -1503,6 +1513,12 @@
                "  int a;\n"
                "#error {\n"
                "}");
+
+  verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
+  verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
+
+  verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
+  verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
 }
 
 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
@@ -1522,24 +1538,25 @@
   verifyFormat("template <union X> void f() {}\nint n;");
 
   // Actual definitions...
-  verifyFormat("struct {} n;");
-  verifyFormat("template <template <class T, class Y>, class Z> class X {} n;");
+  verifyFormat("struct {\n} n;");
+  verifyFormat(
+      "template <template <class T, class Y>, class Z> class X {\n} n;");
   verifyFormat("union Z {\n  int n;\n} x;");
-  verifyFormat("class MACRO Z {} n;");
-  verifyFormat("class MACRO(X) Z {} n;");
-  verifyFormat("class __attribute__(X) Z {} n;");
-  verifyFormat("class __declspec(X) Z {} n;");
+  verifyFormat("class MACRO Z {\n} n;");
+  verifyFormat("class MACRO(X) Z {\n} n;");
+  verifyFormat("class __attribute__(X) Z {\n} n;");
+  verifyFormat("class __declspec(X) Z {\n} n;");
 
   // Redefinition from nested context:
-  verifyFormat("class A::B::C {} n;");
+  verifyFormat("class A::B::C {\n} n;");
 
   // Template definitions.
   // FIXME: This is still incorrectly handled at the formatter side.
-  verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {};");
+  verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
 
   // FIXME:
   // This now gets parsed incorrectly as class definition.
-  // verifyFormat("class A<int> f() {}\nint n;");
+  // verifyFormat("class A<int> f() {\n}\nint n;");
 
   // Elaborate types where incorrectly parsing the structural element would
   // break the indent.
@@ -1758,7 +1775,7 @@
                "@package\n"
                "  int field4;\n"
                "}\n"
-               "+ (id)init {}\n"
+               "+ (id)init {\n}\n"
                "@end");
 
   verifyGoogleFormat("@implementation Foo : NSObject {\n"
@@ -1771,7 +1788,7 @@
                      " @package\n"
                      "  int field4;\n"
                      "}\n"
-                     "+ (id)init {}\n"
+                     "+ (id)init {\n}\n"
                      "@end");
 
   verifyFormat("@implementation Foo\n"
@@ -1794,24 +1811,24 @@
                "@end");
 
   verifyFormat("@implementation Foo : Bar\n"
-               "+ (id)init {}\n"
-               "- (void)foo {}\n"
+               "+ (id)init {\n}\n"
+               "- (void)foo {\n}\n"
                "@end");
 
   verifyFormat("@implementation Foo {\n"
                "  int _i;\n"
                "}\n"
-               "+ (id)init {}\n"
+               "+ (id)init {\n}\n"
                "@end");
 
   verifyFormat("@implementation Foo : Bar {\n"
                "  int _i;\n"
                "}\n"
-               "+ (id)init {}\n"
+               "+ (id)init {\n}\n"
                "@end");
 
   verifyFormat("@implementation Foo (HackStuff)\n"
-               "+ (id)init {}\n"
+               "+ (id)init {\n}\n"
                "@end");
 }
 





More information about the cfe-commits mailing list