From 2f1ac41a6d8d202dcc39ab8eb56ccea823dc062e Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Mon, 21 Jan 2013 16:42:44 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173055 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 67 ++++---- lib/Format/UnwrappedLineParser.cpp | 14 +- test/Index/comment-c-decls.c | 2 +- test/Index/comment-to-html-xml-conversion.cpp | 4 +- test/Index/format-comment-cdecls.c | 2 +- unittests/Format/FormatTest.cpp | 151 ++++++++++-------- 6 files changed, 128 insertions(+), 112 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 12fd070cfe..7c25cbb059 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1711,9 +1711,9 @@ private: // 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 @@ private: 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 @@ private: 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 @@ private: void tryMergeSimpleBlock(std::vector::iterator &I, std::vector::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 @@ private: 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::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) { diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index f1d08a6bdf..c054ef6c82 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -170,17 +170,15 @@ bool UnwrappedLineParser::parseBlock(unsigned AddLevels) { 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; } diff --git a/test/Index/comment-c-decls.c b/test/Index/comment-c-decls.c index 879f9a265a..371e453bef 100644 --- a/test/Index/comment-c-decls.c +++ b/test/Index/comment-c-decls.c @@ -88,7 +88,7 @@ enum e { Two, Three }; -// CHECK: enum e {\n} +// CHECK: enum e {} // CHECK: Two /** diff --git a/test/Index/comment-to-html-xml-conversion.cpp b/test/Index/comment-to-html-xml-conversion.cpp index c1ec08e282..0577eb0854 100644 --- a/test/Index/comment-to-html-xml-conversion.cpp +++ b/test/Index/comment-to-html-xml-conversion.cpp @@ -713,7 +713,7 @@ namespace comment_to_xml_conversion_13 { /// Aaa. enum comment_to_xml_conversion_15 { -// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:6: EnumDecl=comment_to_xml_conversion_15:{{.*}} FullCommentAsXML=[comment_to_xml_conversion_15c:@E@comment_to_xml_conversion_15enum comment_to_xml_conversion_15{{( : int)?}} {\n} Aaa.] +// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:6: EnumDecl=comment_to_xml_conversion_15:{{.*}} FullCommentAsXML=[comment_to_xml_conversion_15c:@E@comment_to_xml_conversion_15enum comment_to_xml_conversion_15{{( : int)?}} {} Aaa.] /// Aaa. comment_to_xml_conversion_16 @@ -722,7 +722,7 @@ enum comment_to_xml_conversion_15 { /// 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=[comment_to_xml_conversion_17c:@E@comment_to_xml_conversion_17enum class comment_to_xml_conversion_17 : int {\n} Aaa.] +// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:12: EnumDecl=comment_to_xml_conversion_17:{{.*}} FullCommentAsXML=[comment_to_xml_conversion_17c:@E@comment_to_xml_conversion_17enum class comment_to_xml_conversion_17 : int {} Aaa.] /// Aaa. comment_to_xml_conversion_18 diff --git a/test/Index/format-comment-cdecls.c b/test/Index/format-comment-cdecls.c index 861bba2f20..471be2baf2 100644 --- a/test/Index/format-comment-cdecls.c +++ b/test/Index/format-comment-cdecls.c @@ -83,7 +83,7 @@ enum e { Two, Three }; -// CHECK: enum e {\n} +// CHECK: enum e {} // CHECK: Two /** diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 149ca0c24a..bda3196b02 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -205,7 +205,7 @@ TEST_F(FormatTest, ParseIfElse) { } 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 @@ TEST_F(FormatTest, FormatsForLoop) { " ;"); verifyFormat("for (;;)\n" " f();"); - verifyFormat("for (;;) {}"); + verifyFormat("for (;;) {\n}"); verifyFormat("for (;;) {\n" " f();\n" "}"); @@ -229,18 +229,18 @@ TEST_F(FormatTest, FormatsForLoop) { verifyFormat( "for (std::vector::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, CommentsInStaticInitializers) { //===----------------------------------------------------------------------===// TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { - verifyFormat("class A {};"); + verifyFormat("class A {\n};"); } TEST_F(FormatTest, UnderstandsAccessSpecifiers) { @@ -476,14 +476,14 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) { } 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, FormatsBitfields) { 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, LayoutNestedBlocks) { 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, PutEmptyBlocksIntoOneLine) { 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 @@ TEST_F(FormatTest, ConstructorInitializers) { 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 @@ TEST_F(FormatTest, ConstructorInitializers) { 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, BreaksAsHighAsPossible) { 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 @@ TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { 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 @@ TEST_F(FormatTest, UnderstandsEquals) { " 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 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { // 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 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) { 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, FormatsFunctionTypes) { 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, IncorrectCodeUnbalancedBraces) { } 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 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { " 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 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { verifyFormat("template void f() {}\nint n;"); // Actual definitions... - verifyFormat("struct {} n;"); - verifyFormat("template