Do not add newline in empty blocks.
void f() {}
now gets formatted in one line.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172067 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index c0ed6c5..39efeab 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -1033,6 +1033,8 @@
if (Left.is(tok::at) &&
Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword)
return false;
+ if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
+ return false;
return true;
}
diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp
index 736178e..e9c6211 100644
--- a/lib/Format/UnwrappedLineParser.cpp
+++ b/lib/Format/UnwrappedLineParser.cpp
@@ -167,15 +167,17 @@
assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected");
nextToken();
- addUnwrappedLine();
+ if (!FormatTok.Tok.is(tok::r_brace)) {
+ 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 261481a..879f9a2 100644
--- a/test/Index/comment-c-decls.c
+++ b/test/Index/comment-c-decls.c
@@ -73,7 +73,7 @@
*/
double dS1;
};
-// CHECK: <Declaration>struct S {\n}</Declaration>
+// CHECK: <Declaration>struct S {}</Declaration>
// CHECK: <Declaration>int iS1</Declaration>
// CHECK: <Declaration>double dS1</Declaration>
@@ -101,4 +101,4 @@
*\brief block declaration
*/
int (^Block1) (int i, int j) = ^(int i, int j) { return i + j; };
-// CHECK: <Declaration>int (^Block1)(int, int) = ^(int i, int j) {\n}</Declaration>
+// CHECK: <Declaration>int (^Block1)(int, int) = ^(int i, int j) {}</Declaration>
diff --git a/test/Index/comment-cplus-decls.cpp b/test/Index/comment-cplus-decls.cpp
index 2fa688c..de1c2c5 100644
--- a/test/Index/comment-cplus-decls.cpp
+++ b/test/Index/comment-cplus-decls.cpp
@@ -39,7 +39,7 @@
*/
data* reserved;
};
-// CHECK: <Declaration>class Test {\n}</Declaration>
+// CHECK: <Declaration>class Test {}</Declaration>
// CHECK: <Declaration>Test() : reserved(new Test::data())</Declaration>
// CHECK: <Declaration>unsigned int getID() const</Declaration>
// CHECK: <Declaration>void ~Test()</Declaration>
diff --git a/test/Index/comment-cplus-template-decls.cpp b/test/Index/comment-cplus-template-decls.cpp
index f0900bb..039f092 100644
--- a/test/Index/comment-cplus-template-decls.cpp
+++ b/test/Index/comment-cplus-template-decls.cpp
@@ -25,7 +25,7 @@
*/
void f() { }
};
-// CHECK: <Declaration>template <typename T> struct A {\n}</Declaration>
+// CHECK: <Declaration>template <typename T> struct A {}</Declaration>
// CHECK: <Declaration>A<T>()</Declaration>
// CHECK: <Declaration>void ~A<T>()</Declaration>
@@ -40,7 +40,7 @@
void f();
};
-// CHECK: <Declaration>template <typename T> struct D : A<T> {\n}</Declaration>
+// CHECK: <Declaration>template <typename T> struct D : A<T> {}</Declaration>
// CHECK: <Declaration>using A<T>::f</Declaration>
struct Base {
@@ -55,7 +55,7 @@
*/
using Base::foo;
};
-// CHECK: <Declaration>template <typename T> struct E : Base {\n}</Declaration>
+// CHECK: <Declaration>template <typename T> struct E : Base {}</Declaration>
// CHECK: <Declaration>using Base::foo</Declaration>
/// \tparam
@@ -66,4 +66,4 @@
template<template<template<typename CCC> class DDD, class BBB> class AAA>
void func_template_2();
-<Declaration>template <template <template <typename CCC> class DDD, class BBB> class AAA> void func_template_2()</Declaration>
+// FIXME: There is not Declaration field in the generated output.
diff --git a/test/Index/comment-to-html-xml-conversion.cpp b/test/Index/comment-to-html-xml-conversion.cpp
index 45f8df3..c90470a 100644
--- a/test/Index/comment-to-html-xml-conversion.cpp
+++ b/test/Index/comment-to-html-xml-conversion.cpp
@@ -588,7 +588,7 @@
/// Aaa.
class comment_to_xml_conversion_01 {
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:7: ClassDecl=comment_to_xml_conversion_01:{{.*}} FullCommentAsXML=[<Class file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="7"><Name>comment_to_xml_conversion_01</Name><USR>c:@C@comment_to_xml_conversion_01</USR><Declaration>class comment_to_xml_conversion_01 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:7: ClassDecl=comment_to_xml_conversion_01:{{.*}} FullCommentAsXML=[<Class file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="7"><Name>comment_to_xml_conversion_01</Name><USR>c:@C@comment_to_xml_conversion_01</USR><Declaration>class comment_to_xml_conversion_01 {}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
/// \param aaa Blah blah.
comment_to_xml_conversion_01(int aaa);
@@ -645,7 +645,7 @@
template<typename T, typename U>
class comment_to_xml_conversion_08 { };
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:9: ClassTemplate=comment_to_xml_conversion_08:{{.*}} FullCommentAsXML=[<Class templateKind="template" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="9"><Name>comment_to_xml_conversion_08</Name><USR>c:@C@comment_to_xml_conversion_01@CT>2#T#T@comment_to_xml_conversion_08</USR><Declaration>template <typename T, typename U> class comment_to_xml_conversion_08 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:9: ClassTemplate=comment_to_xml_conversion_08:{{.*}} FullCommentAsXML=[<Class templateKind="template" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="9"><Name>comment_to_xml_conversion_08</Name><USR>c:@C@comment_to_xml_conversion_01@CT>2#T#T@comment_to_xml_conversion_08</USR><Declaration>template <typename T, typename U> class comment_to_xml_conversion_08 {}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
/// Aaa.
template<typename T>
@@ -670,19 +670,19 @@
template<typename T, typename U>
class comment_to_xml_conversion_11 { };
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:7: ClassTemplate=comment_to_xml_conversion_11:{{.*}} FullCommentAsXML=[<Class templateKind="template" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="7"><Name>comment_to_xml_conversion_11</Name><USR>c:@CT>2#T#T@comment_to_xml_conversion_11</USR><Declaration>template <typename T, typename U> class comment_to_xml_conversion_11 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:7: ClassTemplate=comment_to_xml_conversion_11:{{.*}} FullCommentAsXML=[<Class templateKind="template" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="7"><Name>comment_to_xml_conversion_11</Name><USR>c:@CT>2#T#T@comment_to_xml_conversion_11</USR><Declaration>template <typename T, typename U> class comment_to_xml_conversion_11 {}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
/// Aaa.
template<typename T>
class comment_to_xml_conversion_11<T, int> { };
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:7: ClassTemplatePartialSpecialization=comment_to_xml_conversion_11:{{.*}} FullCommentAsXML=[<Class templateKind="partialSpecialization" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="7"><Name>comment_to_xml_conversion_11</Name><USR>c:@CP>1#T@comment_to_xml_conversion_11>#t0.0#I</USR><Declaration>class comment_to_xml_conversion_11 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:7: ClassTemplatePartialSpecialization=comment_to_xml_conversion_11:{{.*}} FullCommentAsXML=[<Class templateKind="partialSpecialization" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="7"><Name>comment_to_xml_conversion_11</Name><USR>c:@CP>1#T@comment_to_xml_conversion_11>#t0.0#I</USR><Declaration>class comment_to_xml_conversion_11 {}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
/// Aaa.
template<>
class comment_to_xml_conversion_11<int, int> { };
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:7: ClassDecl=comment_to_xml_conversion_11:{{.*}} FullCommentAsXML=[<Class templateKind="specialization" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="7"><Name>comment_to_xml_conversion_11</Name><USR>c:@C@comment_to_xml_conversion_11>#I#I</USR><Declaration>class comment_to_xml_conversion_11 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:7: ClassDecl=comment_to_xml_conversion_11:{{.*}} FullCommentAsXML=[<Class templateKind="specialization" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="7"><Name>comment_to_xml_conversion_11</Name><USR>c:@C@comment_to_xml_conversion_11>#I#I</USR><Declaration>class comment_to_xml_conversion_11 {}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
/// Aaa.
int comment_to_xml_conversion_12;
@@ -691,11 +691,11 @@
/// Aaa.
namespace comment_to_xml_conversion_13 {
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:11: Namespace=comment_to_xml_conversion_13:{{.*}} FullCommentAsXML=[<Namespace file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="11"><Name>comment_to_xml_conversion_13</Name><USR>c:@N@comment_to_xml_conversion_13</USR><Declaration>namespace comment_to_xml_conversion_13 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Namespace>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:11: Namespace=comment_to_xml_conversion_13:{{.*}} FullCommentAsXML=[<Namespace file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="11"><Name>comment_to_xml_conversion_13</Name><USR>c:@N@comment_to_xml_conversion_13</USR><Declaration>namespace comment_to_xml_conversion_13 {}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Namespace>]
/// Aaa.
namespace comment_to_xml_conversion_14 {
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:13: Namespace=comment_to_xml_conversion_14:{{.*}} FullCommentAsXML=[<Namespace file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="13"><Name>comment_to_xml_conversion_14</Name><USR>c:@N@comment_to_xml_conversion_13@N@comment_to_xml_conversion_14</USR><Declaration>namespace comment_to_xml_conversion_14 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Namespace>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:13: Namespace=comment_to_xml_conversion_14:{{.*}} FullCommentAsXML=[<Namespace file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="13"><Name>comment_to_xml_conversion_14</Name><USR>c:@N@comment_to_xml_conversion_13@N@comment_to_xml_conversion_14</USR><Declaration>namespace comment_to_xml_conversion_14 {}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Namespace>]
}
}
diff --git a/test/Index/format-comment-cdecls.c b/test/Index/format-comment-cdecls.c
index ff36dd7..861bba2 100644
--- a/test/Index/format-comment-cdecls.c
+++ b/test/Index/format-comment-cdecls.c
@@ -68,7 +68,7 @@
*/
double dS1;
};
-// CHECK: <Declaration>struct S {\n}</Declaration>
+// CHECK: <Declaration>struct S {}</Declaration>
// CHECK: <Declaration>int iS1</Declaration>
// CHECK: <Declaration>double dS1</Declaration>
@@ -96,4 +96,4 @@
*\brief block declaration
*/
int (^Block1) (int i, int j) = ^(int i, int j) { return i + j; };
-// CHECK: <Declaration>int (^Block1)(int, int) = ^(int i, int j) {\n}</Declaration>
+// CHECK: <Declaration>int (^Block1)(int, int) = ^(int i, int j) {}</Declaration>
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 39d8bb8..7a16da5 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -115,7 +115,7 @@
}
TEST_F(FormatTest, FormatsNestedBlockStatements) {
- EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}"));
+ EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
}
TEST_F(FormatTest, FormatsNestedCall) {
@@ -163,9 +163,7 @@
}
TEST_F(FormatTest, ElseIf) {
- verifyFormat("if (a) {\n"
- "} else if (b) {\n"
- "}");
+ verifyFormat("if (a) {} else if (b) {}");
verifyFormat("if (a)\n"
" f();\n"
"else if (b)\n"
@@ -181,8 +179,7 @@
" ;");
verifyFormat("for (;;)\n"
" f();");
- verifyFormat("for (;;) {\n"
- "}");
+ verifyFormat("for (;;) {}");
verifyFormat("for (;;) {\n"
" f();\n"
"}");
@@ -190,19 +187,18 @@
verifyFormat(
"for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
" E = UnwrappedLines.end();\n"
- " I != E; ++I) {\n}");
+ " I != E; ++I) {}");
verifyFormat(
"for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
- " ++IIIII) {\n}");
+ " ++IIIII) {}");
}
TEST_F(FormatTest, FormatsWhileLoop) {
- verifyFormat("while (true) {\n}");
+ verifyFormat("while (true) {}");
verifyFormat("while (true)\n"
" f();");
- verifyFormat("while () {\n"
- "}");
+ verifyFormat("while () {}");
verifyFormat("while () {\n"
" f();\n"
"}");
@@ -283,7 +279,7 @@
TEST_F(FormatTest, UnderstandsSingleLineComments) {
verifyFormat("// line 1\n"
"// line 2\n"
- "void f() {\n}\n");
+ "void f() {}\n");
verifyFormat("void f() {\n"
" // Doesn't do anything\n"
@@ -320,7 +316,7 @@
//===----------------------------------------------------------------------===//
TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
- verifyFormat("class A {\n};");
+ verifyFormat("class A {};");
}
TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
@@ -328,30 +324,24 @@
"public:\n"
"protected:\n"
"private:\n"
- " void f() {\n"
- " }\n"
+ " void f() {}\n"
"};");
verifyGoogleFormat("class A {\n"
" public:\n"
" protected:\n"
" private:\n"
- " void f() {\n"
- " }\n"
+ " void f() {}\n"
"};");
}
TEST_F(FormatTest, FormatsDerivedClass) {
- verifyFormat("class A : public B {\n"
- "};");
- verifyFormat("class A : public ::B {\n"
- "};");
+ verifyFormat("class A : public B {};");
+ verifyFormat("class A : public ::B {};");
}
TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
- verifyFormat("class A {\n"
- "} a, b;");
- verifyFormat("struct A {\n"
- "} a, b;");
+ verifyFormat("class A {} a, b;");
+ verifyFormat("struct A {} a, b;");
}
TEST_F(FormatTest, FormatsEnum) {
@@ -371,29 +361,25 @@
TEST_F(FormatTest, FormatsNamespaces) {
verifyFormat("namespace some_namespace {\n"
- "class A {\n"
- "};\n"
+ "class A {};\n"
"void f() {\n"
" f();\n"
"}\n"
"}");
verifyFormat("namespace {\n"
- "class A {\n"
- "};\n"
+ "class A {};\n"
"void f() {\n"
" f();\n"
"}\n"
"}");
verifyFormat("inline namespace X {\n"
- "class A {\n"
- "};\n"
+ "class A {};\n"
"void f() {\n"
" f();\n"
"}\n"
"}");
verifyFormat("using namespace some_namespace;\n"
- "class A {\n"
- "};\n"
+ "class A {};\n"
"void f() {\n"
" f();\n"
"}");
@@ -419,8 +405,7 @@
"}");
verifyFormat("class A {\n"
" int a;\n"
- " A() try : a(0) {\n"
- " }\n"
+ " A() try : a(0) {}\n"
" catch (...) {\n"
" throw;\n"
" }\n"
@@ -519,7 +504,7 @@
}
TEST_F(FormatTest, LayoutRemainingTokens) {
- EXPECT_EQ("{\n}", format("{}"));
+ EXPECT_EQ("{}", format("{}"));
}
TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
@@ -635,6 +620,10 @@
"}");
}
+TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
+ EXPECT_EQ("{}", format("{}"));
+}
+
//===----------------------------------------------------------------------===//
// Line break tests.
//===----------------------------------------------------------------------===//
@@ -642,8 +631,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) {\n"
- "}");
+ " int c, int ddddddddddddd) {}");
}
TEST_F(FormatTest, FormatsAwesomeMethodCall) {
@@ -654,33 +642,29 @@
}
TEST_F(FormatTest, ConstructorInitializers) {
- verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}");
+ verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
verifyFormat(
"SomeClass::Constructor()\n"
- " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
- "}");
+ " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
verifyFormat(
"SomeClass::Constructor()\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
- " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
- "}");
+ " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
verifyFormat("Constructor()\n"
" : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
- " aaaaaaaaaaaaaaaaaaaaaaa() {\n"
- "}");
+ " aaaaaaaaaaaaaaaaaaaaaaa() {}");
// 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"
- "}");
+ " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
verifyGoogleFormat("MyClass::MyClass(int var)\n"
" : some_var_(var), // 4 space indent\n"
@@ -698,11 +682,11 @@
TEST_F(FormatTest, BreaksDesireably) {
verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
" aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
- " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
+ " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}");
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -739,14 +723,14 @@
TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
verifyFormat(
"if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
+ " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}");
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
- " ccccccccccccccccccccccccc) {\n}");
+ " ccccccccccccccccccccccccc) {}");
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
- " ccccccccccccccccccccccccc) {\n}");
+ " ccccccccccccccccccccccccc) {}");
verifyFormat(
"if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
- " ccccccccccccccccccccccccc) {\n}");
+ " ccccccccccccccccccccccccc) {}");
}
TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
@@ -833,19 +817,17 @@
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
verifyFormat(
"if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
- "}");
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
verifyFormat(
"if (a) {\n"
+ " f();\n"
"} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
- "}");
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
verifyFormat(
// FIXME: Does an expression like this ever make sense? If yes, fix.
"if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
- " 10000000) {\n"
- "}");
+ " 10000000) {}");
}
TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
@@ -873,8 +855,7 @@
// Here, it is not necessary to wrap at "." or "->".
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
- " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
- "}");
+ " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
verifyFormat(
"aaaaaaaaaaa->aaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
@@ -924,7 +905,7 @@
verifyFormat("test << a >> b;");
verifyFormat("f<int>();");
- verifyFormat("template <typename T> void f() {\n}");
+ verifyFormat("template <typename T> void f() {}");
}
TEST_F(FormatTest, UnderstandsUnaryOperators) {
@@ -932,13 +913,13 @@
verifyFormat("f(-1, -2, -3);");
verifyFormat("a[-1] = 5;");
verifyFormat("int a = 5 + -2;");
- verifyFormat("if (i == -1) {\n}");
- verifyFormat("if (i != -1) {\n}");
- verifyFormat("if (i > -1) {\n}");
- verifyFormat("if (i < -1) {\n}");
+ verifyFormat("if (i == -1) {}");
+ verifyFormat("if (i != -1) {}");
+ verifyFormat("if (i > -1) {}");
+ verifyFormat("if (i < -1) {}");
verifyFormat("++(a->f());");
verifyFormat("--(a->f());");
- verifyFormat("if (!(a->f())) {\n}");
+ verifyFormat("if (!(a->f())) {}");
verifyFormat("a-- > b;");
verifyFormat("b ? -a : c;");
@@ -974,7 +955,7 @@
}
TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
- verifyFormat("int *f(int *a) {\n}");
+ verifyFormat("int *f(int *a) {}");
verifyFormat("f(a, *a);");
verifyFormat("f(*a);");
verifyFormat("int a = b * 10;");
@@ -987,7 +968,7 @@
verifyFormat("int a = *b;");
verifyFormat("int a = *b * c;");
verifyFormat("int a = b * *c;");
- verifyFormat("int main(int argc, char **argv) {\n}");
+ verifyFormat("int main(int argc, char **argv) {}");
verifyFormat("return 10 * b;");
verifyFormat("return *b * *c;");
verifyFormat("return a & ~b;");
@@ -1016,7 +997,7 @@
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
- verifyGoogleFormat("int main(int argc, char** argv) {\n}");
+ verifyGoogleFormat("int main(int argc, char** argv) {}");
verifyGoogleFormat("A<int*> a;");
verifyGoogleFormat("A<int**> a;");
verifyGoogleFormat("A<int*, int*> a;");
@@ -1036,11 +1017,11 @@
TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
- " int LoooooooooooooooongParam2) {\n}");
+ " int LoooooooooooooooongParam2) {}");
verifyFormat(
"TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
" SourceLocation L, IdentifierIn *II,\n"
- " Type *T) {\n}");
+ " Type *T) {}");
}
TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
@@ -1083,17 +1064,14 @@
verifyFormat("public:");
verifyFormat("class A {\n"
"public\n"
- " void f() {\n"
- " }\n"
+ " void f() {}\n"
"};");
verifyFormat("public\n"
"int qwerty;");
verifyFormat("public\n"
- "B {\n"
- "}");
+ "B {}");
verifyFormat("public\n"
- "{\n"
- "}");
+ "{}");
verifyFormat("public\n"
"B {\n"
" int x;\n"
@@ -1105,13 +1083,10 @@
}
TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
- verifyFormat("do {\n"
- "}");
- verifyFormat("do {\n"
- "}\n"
+ verifyFormat("do {}");
+ verifyFormat("do {}\n"
"f();");
- verifyFormat("do {\n"
- "}\n"
+ verifyFormat("do {}\n"
"wheeee(fun);");
verifyFormat("do {\n"
" f();\n"
@@ -1124,10 +1099,10 @@
}
TEST_F(FormatTest, IncorrectCodeErrorDetection) {
- EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n"));
- EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n"));
- EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n"));
- EXPECT_EQ("{\n {\n }\n }\n}\n", format("{\n {\n }\n }\n}\n"));
+ EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
+ EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
+ EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
+ EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = 10;
@@ -1296,8 +1271,7 @@
"@package\n"
" int field4;\n"
"}\n"
- "+ (id)init {\n"
- "}\n"
+ "+ (id)init {}\n"
"@end");
verifyGoogleFormat("@implementation Foo : NSObject {\n"
@@ -1310,8 +1284,7 @@
" @package\n"
" int field4;\n"
"}\n"
- "+ (id)init {\n"
- "}\n"
+ "+ (id)init {}\n"
"@end");
verifyFormat("@implementation Foo\n"
@@ -1331,28 +1304,24 @@
"@end");
verifyFormat("@implementation Foo : Bar\n"
- "+ (id)init {\n"
- "}\n"
+ "+ (id)init {}\n"
"@end");
verifyFormat("@implementation Foo {\n"
" int _i;\n"
"}\n"
- "+ (id)init {\n"
- "}\n"
+ "+ (id)init {}\n"
"@end");
verifyFormat("@implementation Foo : Bar {\n"
" int _i;\n"
"}\n"
- "+ (id)init {\n"
- "}\n"
+ "+ (id)init {}\n"
"@end");
// FIXME: there should be a space before '(' for categories.
verifyFormat("@implementation Foo(HackStuff)\n"
- "+ (id)init {\n"
- "}\n"
+ "+ (id)init {}\n"
"@end");
}