aboutsummaryrefslogtreecommitdiff
path: root/unittests/Format/FormatTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'unittests/Format/FormatTest.cpp')
-rw-r--r--unittests/Format/FormatTest.cpp1116
1 files changed, 899 insertions, 217 deletions
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 21bc86275461..8e770c2e9cd5 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -111,6 +111,7 @@ TEST_F(FormatTest, NestedNameSpecifiers) {
verifyFormat("vector<::Type> v;");
verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
+ verifyFormat("bool a = 2 < ::SomeFunction();");
}
TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
@@ -158,9 +159,21 @@ TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
25, 0, getLLVMStyleWithColumns(12)));
}
+TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) {
+ EXPECT_EQ("int b;\n\nint a;",
+ format("int b;\n\nint a;", 8, 0, getLLVMStyle()));
+ EXPECT_EQ("int b;\n\nint a;",
+ format("int b;\n\nint a;", 7, 0, getLLVMStyle()));
+
+ // This might not strictly be correct, but is likely good in all practical
+ // cases.
+ EXPECT_EQ("int b;\nint a;",
+ format("int b;int a;", 7, 0, getLLVMStyle()));
+}
+
TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
EXPECT_EQ("int a;\n\n int b;",
- format("int a;\n \n\n int b;", 7, 0, getLLVMStyle()));
+ format("int a;\n \n\n int b;", 8, 0, getLLVMStyle()));
EXPECT_EQ("int a;\n\n int b;",
format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
}
@@ -174,7 +187,7 @@ TEST_F(FormatTest, RemovesEmptyLines) {
"\n"
"};"));
- // Don't remove empty lines at the start of namespaces.
+ // Don't remove empty lines at the start of namespaces or extern "C" blocks.
EXPECT_EQ("namespace N {\n"
"\n"
"int i;\n"
@@ -184,6 +197,29 @@ TEST_F(FormatTest, RemovesEmptyLines) {
"int i;\n"
"}",
getGoogleStyle()));
+ EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
+ "\n"
+ "int i;\n"
+ "}",
+ format("extern /**/ \"C\" /**/ {\n"
+ "\n"
+ "int i;\n"
+ "}",
+ getGoogleStyle()));
+
+ // ...but do keep inlining and removing empty lines for non-block extern "C"
+ // functions.
+ verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
+ EXPECT_EQ("extern \"C\" int f() {\n"
+ " int i = 42;\n"
+ " return i;\n"
+ "}",
+ format("extern \"C\" int f() {\n"
+ "\n"
+ " int i = 42;\n"
+ " return i;\n"
+ "}",
+ getGoogleStyle()));
// Remove empty lines at the beginning and end of blocks.
EXPECT_EQ("void f() {\n"
@@ -700,6 +736,58 @@ TEST_F(FormatTest, CaseRanges) {
"}");
}
+TEST_F(FormatTest, ShortCaseLabels) {
+ FormatStyle Style = getLLVMStyle();
+ Style.AllowShortCaseLabelsOnASingleLine = true;
+ verifyFormat("switch (a) {\n"
+ "case 1: x = 1; break;\n"
+ "case 2: return;\n"
+ "case 3:\n"
+ "case 4:\n"
+ "case 5: return;\n"
+ "case 6: // comment\n"
+ " return;\n"
+ "case 7:\n"
+ " // comment\n"
+ " return;\n"
+ "default: y = 1; break;\n"
+ "}",
+ Style);
+ verifyFormat("switch (a) {\n"
+ "#if FOO\n"
+ "case 0: return 0;\n"
+ "#endif\n"
+ "}",
+ Style);
+ verifyFormat("switch (a) {\n"
+ "case 1: {\n"
+ "}\n"
+ "case 2: {\n"
+ " return;\n"
+ "}\n"
+ "case 3: {\n"
+ " x = 1;\n"
+ " return;\n"
+ "}\n"
+ "case 4:\n"
+ " if (x)\n"
+ " return;\n"
+ "}",
+ Style);
+ Style.ColumnLimit = 21;
+ verifyFormat("switch (a) {\n"
+ "case 1: x = 1; break;\n"
+ "case 2: return;\n"
+ "case 3:\n"
+ "case 4:\n"
+ "case 5: return;\n"
+ "default:\n"
+ " y = 1;\n"
+ " break;\n"
+ "}",
+ Style);
+}
+
TEST_F(FormatTest, FormatsLabels) {
verifyFormat("void f() {\n"
" some_code();\n"
@@ -791,6 +879,12 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
" // Comment inside a statement.\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
+ verifyFormat("SomeFunction(a,\n"
+ " // comment\n"
+ " b + x);");
+ verifyFormat("SomeFunction(a, a,\n"
+ " // comment\n"
+ " b + x);");
verifyFormat(
"bool aaaaaaaaaaaaa = // comment\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
@@ -904,6 +998,14 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
" // first\n"
"// at start\n"
"otherLine();"));
+ EXPECT_EQ("void f() {\n"
+ " lineWith(); // comment\n"
+ " // at start\n"
+ "}",
+ format("void f() {\n"
+ " lineWith(); // comment\n"
+ " // at start\n"
+ "}"));
verifyFormat(
"#define A \\\n"
@@ -1159,8 +1261,8 @@ TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
}
TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
- EXPECT_EQ("void\n"
- "ffffffffff(int aaaaa /* test */);",
+ EXPECT_EQ("void ffffffffff(\n"
+ " int aaaaa /* test */);",
format("void ffffffffff(int aaaaa /* test */);",
getLLVMStyleWithColumns(35)));
}
@@ -1210,11 +1312,11 @@ TEST_F(FormatTest, SplitsLongCxxComments) {
format("// A comment before a macro definition\n"
"#define a b",
getLLVMStyleWithColumns(20)));
- EXPECT_EQ("void\n"
- "ffffff(int aaaaaaaaa, // wwww\n"
- " int bbbbbbbbbb, // xxxxxxx\n"
- " // yyyyyyyyyy\n"
- " int c, int d, int e) {}",
+ EXPECT_EQ("void ffffff(\n"
+ " int aaaaaaaaa, // wwww\n"
+ " int bbbbbbbbbb, // xxxxxxx\n"
+ " // yyyyyyyyyy\n"
+ " int c, int d, int e) {}",
format("void ffffff(\n"
" int aaaaaaaaa, // wwww\n"
" int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
@@ -1846,6 +1948,7 @@ TEST_F(FormatTest, FormatsClasses) {
verifyFormat("template <class R, class C>\n"
"struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
" : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
+ verifyFormat("class ::A::B {};");
}
TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
@@ -1903,7 +2006,8 @@ TEST_F(FormatTest, FormatsEnum) {
verifyFormat("enum E { // comment\n"
" ONE,\n"
" TWO\n"
- "};");
+ "};\n"
+ "int i;");
}
TEST_F(FormatTest, FormatsEnumsWithErrors) {
@@ -1973,6 +2077,21 @@ TEST_F(FormatTest, FormatsNSEnums) {
" // Information about aThirdDecentlyLongValue.\n"
" aThirdDecentlyLongValue\n"
"};");
+ verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
+ " a = 1,\n"
+ " b = 2,\n"
+ " c = 3,\n"
+ "};");
+ verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
+ " a = 1,\n"
+ " b = 2,\n"
+ " c = 3,\n"
+ "};");
+ verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
+ " a = 1,\n"
+ " b = 2,\n"
+ " c = 3,\n"
+ "};");
}
TEST_F(FormatTest, FormatsBitfields) {
@@ -2063,12 +2182,32 @@ TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
TEST_F(FormatTest, FormatsInlineASM) {
verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
+ verifyFormat("asm(\"nop\" ::: \"memory\");");
verifyFormat(
"asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
" \"cpuid\\n\\t\"\n"
" \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
" : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
" : \"a\"(value));");
+ EXPECT_EQ(
+ "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
+ " __asm {\n"
+ " mov edx,[that] // vtable in edx\n"
+ " mov eax,methodIndex\n"
+ " call [edx][eax*4] // stdcall\n"
+ " }\n"
+ "}",
+ format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
+ " __asm {\n"
+ " mov edx,[that] // vtable in edx\n"
+ " mov eax,methodIndex\n"
+ " call [edx][eax*4] // stdcall\n"
+ " }\n"
+ "}"));
+ verifyFormat("void function() {\n"
+ " // comment\n"
+ " asm(\"\");\n"
+ "}");
}
TEST_F(FormatTest, FormatTryCatch) {
@@ -2247,6 +2386,16 @@ TEST_F(FormatTest, NestedStaticInitializers) {
" {kOsWin, \"Windows\"},\n"
" {kOsLinux, \"Linux\"},\n"
" {kOsCrOS, \"Chrome OS\"}};");
+ verifyFormat(
+ "struct {\n"
+ " unsigned bit;\n"
+ " const char *const name;\n"
+ "} kBitsToOs[] = {\n"
+ " {kOsMac, \"Mac\"},\n"
+ " {kOsWin, \"Windows\"},\n"
+ " {kOsLinux, \"Linux\"},\n"
+ " {kOsCrOS, \"Chrome OS\"},\n"
+ "};");
}
TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
@@ -2482,6 +2631,14 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
" Q_Object\n"
" A() {\n}\n"
"} ;"));
+
+ // Only if the next line can actually start an unwrapped line.
+ EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
+ format("SOME_WEIRD_LOG_MACRO\n"
+ "<< SomeThing;"));
+
+ verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
+ "(n, buffers))\n", getChromiumStyle(FormatStyle::LK_Cpp));
}
TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
@@ -2659,6 +2816,17 @@ TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
}
+TEST_F(FormatTest, DontCrashOnBlockComments) {
+ EXPECT_EQ(
+ "int xxxxxxxxx; /* "
+ "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
+ "zzzzzz\n"
+ "0*/",
+ format("int xxxxxxxxx; /* "
+ "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
+ "0*/"));
+}
+
TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
verifyFormat("#define A \\\n"
" int v( \\\n"
@@ -2779,11 +2947,19 @@ TEST_F(FormatTest, LayoutBlockInsideParens) {
"});",
format(" functionCall ( {int i;int j;} );"));
EXPECT_EQ("functionCall({\n"
- " int i;\n"
- " int j;\n"
- " },\n"
- " aaaa, bbbb, cccc);",
+ " int i;\n"
+ " int j;\n"
+ "}, aaaa, bbbb, cccc);",
format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);"));
+ EXPECT_EQ("functionCall(\n"
+ " {\n"
+ " int i;\n"
+ " int j;\n"
+ " },\n"
+ " aaaa, bbbb, // comment\n"
+ " cccc);",
+ format(" functionCall ( {int i;int j;}, aaaa, bbbb, // comment\n"
+ "cccc);"));
EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
format(" functionCall (aaaa, bbbb, {int i;});"));
EXPECT_EQ("functionCall(aaaa, bbbb, {\n"
@@ -2794,7 +2970,8 @@ TEST_F(FormatTest, LayoutBlockInsideParens) {
EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });",
format(" functionCall (aaaa, bbbb, {int i;});"));
verifyFormat(
- "Aaa({\n"
+ "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
+ " {\n"
" int i; // break\n"
" },\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
@@ -2876,10 +3053,9 @@ TEST_F(FormatTest, LayoutNestedBlocks) {
FormatStyle Style = getGoogleStyle();
Style.ColumnLimit = 45;
verifyFormat("Debug(aaaaa, {\n"
- " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
- " return;\n"
- " },\n"
- " a);", Style);
+ " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
+ "}, a);",
+ Style);
}
TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
@@ -3025,6 +3201,9 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
" bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
" cccccc) {\n}");
+ verifyFormat("b = a &&\n"
+ " // Comment\n"
+ " b.c && d;");
// If the LHS of a comparison is not a binary expression itself, the
// additional linebreak confuses many people.
@@ -3114,39 +3293,47 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
// everything until something with the same indent as the operator is found.
// FIXME: Is this a good system?
FormatStyle Style = getLLVMStyle();
- Style.BreakBeforeBinaryOperators = true;
+ Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
verifyFormat(
"bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
- " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
" && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " > ccccccccccccccccccccccccccccccccccccccccc;",
+ " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " > ccccccccccccccccccccccccccccccccccccccccc;",
Style);
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
Style);
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
Style);
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
+ " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
Style);
verifyFormat("if () {\n"
"} else if (aaaaa\n"
" && bbbbb // break\n"
- " > ccccc) {\n"
+ " > ccccc) {\n"
"}",
Style);
+ verifyFormat("return (a)\n"
+ " // comment\n"
+ " + b;",
+ Style);
+ verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " + cc;",
+ Style);
// Forced by comments.
verifyFormat(
@@ -3167,6 +3354,48 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
Style);
}
+TEST_F(FormatTest, NoOperandAlignment) {
+ FormatStyle Style = getLLVMStyle();
+ Style.AlignOperands = false;
+ Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
+ verifyFormat(
+ "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " > ccccccccccccccccccccccccccccccccccccccccc;",
+ Style);
+
+ verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " + cc;",
+ Style);
+ verifyFormat("int a = aa\n"
+ " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " * cccccccccccccccccccccccccccccccccccc;",
+ Style);
+
+ Style.AlignAfterOpenBracket = false;
+ verifyFormat("return (a > b\n"
+ " // comment1\n"
+ " // comment2\n"
+ " || c);",
+ Style);
+}
+
+TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
+ FormatStyle Style = getLLVMStyle();
+ Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
+ verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
+ Style);
+}
+
TEST_F(FormatTest, ConstructorInitializers) {
verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
@@ -3409,6 +3638,10 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" bbbb bbbb);");
+ verifyFormat("void SomeLoooooooooooongFunction(\n"
+ " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " int bbbbbbbbbbbbb);");
// Treat overloaded operators like other functions.
verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
@@ -3427,6 +3660,10 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
" int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
"aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
+ verifyGoogleFormat(
+ "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
+ "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
}
TEST_F(FormatTest, TrailingReturnType) {
@@ -3437,6 +3674,9 @@ TEST_F(FormatTest, TrailingReturnType) {
verifyFormat("template <size_t Order, typename T>\n"
"auto load_img(const std::string &filename)\n"
" -> alias::tensor<Order, T, mem::tag::cpu> {}");
+ verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
+ " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
+ verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
// Not trailing return types.
verifyFormat("void f() { auto a = b->c(); }");
@@ -3447,8 +3687,8 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
// they are not function-like.
FormatStyle Style = getGoogleStyle();
Style.ColumnLimit = 47;
- verifyFormat("void\n"
- "someLongFunction(int someLongParameter) const {\n}",
+ verifyFormat("void someLongFunction(\n"
+ " int someLoooooooooooooongParameter) const {\n}",
getLLVMStyleWithColumns(47));
verifyFormat("LoooooongReturnType\n"
"someLoooooooongFunction() const {}",
@@ -3513,6 +3753,7 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
" LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
" AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
+ verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
verifyFormat(
"void aaaaaaaaaaaaaaaaaa()\n"
@@ -3607,9 +3848,25 @@ TEST_F(FormatTest, BreaksDesireably) {
" NSTrackingActiveAlways;");
}
+TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
+ FormatStyle NoBinPacking = getGoogleStyle();
+ NoBinPacking.BinPackParameters = false;
+ NoBinPacking.BinPackArguments = true;
+ verifyFormat("void f() {\n"
+ " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
+ "}",
+ NoBinPacking);
+ verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
+ " int aaaaaaaaaaaaaaaaaaaa,\n"
+ " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
+ NoBinPacking);
+}
+
TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
FormatStyle NoBinPacking = getGoogleStyle();
NoBinPacking.BinPackParameters = false;
+ NoBinPacking.BinPackArguments = false;
verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
@@ -3866,6 +4123,66 @@ TEST_F(FormatTest, AlignsAfterReturn) {
" code == a || code == b;");
}
+TEST_F(FormatTest, AlignsAfterOpenBracket) {
+ verifyFormat(
+ "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
+ " aaaaaaaaa aaaaaaa) {}");
+ verifyFormat(
+ "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
+ " aaaaaaaaaaa aaaaaaaaa);");
+ verifyFormat(
+ "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaa));");
+ FormatStyle Style = getLLVMStyle();
+ Style.AlignAfterOpenBracket = false;
+ verifyFormat(
+ "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
+ Style);
+ verifyFormat(
+ "SomeLongVariableName->someVeryLongFunctionName(\n"
+ " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
+ Style);
+ verifyFormat(
+ "SomeLongVariableName->someFunction(\n"
+ " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
+ Style);
+ verifyFormat(
+ "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
+ " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
+ Style);
+ verifyFormat(
+ "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
+ " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
+ Style);
+ verifyFormat(
+ "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
+ Style);
+}
+
+TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
+ FormatStyle Style = getLLVMStyleWithColumns(40);
+ verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
+ " bbbbbbbbbbbbbbbbbbbbbb);",
+ Style);
+ Style.AlignAfterOpenBracket = true;
+ Style.AlignOperands = false;
+ verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
+ " bbbbbbbbbbbbbbbbbbbbbb);",
+ Style);
+ Style.AlignAfterOpenBracket = false;
+ Style.AlignOperands = true;
+ verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
+ " bbbbbbbbbbbbbbbbbbbbbb);",
+ Style);
+ Style.AlignAfterOpenBracket = false;
+ Style.AlignOperands = false;
+ verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
+ " bbbbbbbbbbbbbbbbbbbbbb);",
+ Style);
+}
+
TEST_F(FormatTest, BreaksConditionalExpressions) {
verifyFormat(
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
@@ -3918,6 +4235,10 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
" aaaaaaaaa\n"
" ? b\n"
" : c);");
+ verifyFormat("return aaaa == bbbb\n"
+ " // comment\n"
+ " ? aaaa\n"
+ " : bbbb;");
verifyFormat(
"unsigned Indent =\n"
" format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
@@ -3948,7 +4269,7 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
FormatStyle NoBinPacking = getLLVMStyle();
- NoBinPacking.BinPackParameters = false;
+ NoBinPacking.BinPackArguments = false;
verifyFormat(
"void f() {\n"
" g(aaa,\n"
@@ -3966,6 +4287,32 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
" ?: aaaaaaaaaaaaaaa);\n"
"}",
NoBinPacking);
+
+ verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
+ " // comment.\n"
+ " ccccccccccccccccccccccccccccccccccccccc\n"
+ " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
+
+ // Assignments in conditional expressions. Apparently not uncommon :-(.
+ verifyFormat("return a != b\n"
+ " // comment\n"
+ " ? a = b\n"
+ " : a = b;");
+ verifyFormat("return a != b\n"
+ " // comment\n"
+ " ? a = a != b\n"
+ " // comment\n"
+ " ? a = b\n"
+ " : a\n"
+ " : a;\n");
+ verifyFormat("return a != b\n"
+ " // comment\n"
+ " ? a\n"
+ " : a = a != b\n"
+ " // comment\n"
+ " ? a = b\n"
+ " : a;");
}
TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
@@ -4079,13 +4426,13 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
// line. Also fix indent for breaking after the type, this looks bad.
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
- " *b = bbbbbbbbbbbbbbbbbbb;",
+ " * b = bbbbbbbbbbbbbbbbbbb;",
getGoogleStyle());
// Not ideal, but pointer-with-type does not allow much here.
verifyGoogleFormat(
- "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
- " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
+ "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, * b = bbbbbbbbbbbbbbbbbbb,\n"
+ " * b = bbbbbbbbbbbbbbbbbbb, * d = ddddddddddddddddddd;");
}
TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
@@ -4145,6 +4492,40 @@ TEST_F(FormatTest, AlignsStringLiterals) {
getLLVMStyleWithColumns(25));
}
+TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
+ FormatStyle AfterType = getLLVMStyle();
+ AfterType.AlwaysBreakAfterDefinitionReturnType = true;
+ verifyFormat("const char *\n"
+ "f(void) {\n" // Break here.
+ " return \"\";\n"
+ "}\n"
+ "const char *bar(void);\n", // No break here.
+ AfterType);
+ verifyFormat("template <class T>\n"
+ "T *\n"
+ "f(T &c) {\n" // Break here.
+ " return NULL;\n"
+ "}\n"
+ "template <class T> T *f(T &c);\n", // No break here.
+ AfterType);
+ AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
+ verifyFormat("const char *\n"
+ "f(void)\n" // Break here.
+ "{\n"
+ " return \"\";\n"
+ "}\n"
+ "const char *bar(void);\n", // No break here.
+ AfterType);
+ verifyFormat("template <class T>\n"
+ "T *\n" // Problem here: no line break
+ "f(T &c)\n" // Break here.
+ "{\n"
+ " return NULL;\n"
+ "}\n"
+ "template <class T> T *f(T &c);\n", // No break here.
+ AfterType);
+}
+
TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
FormatStyle NoBreak = getLLVMStyle();
NoBreak.AlwaysBreakBeforeMultilineStrings = false;
@@ -4308,6 +4689,10 @@ TEST_F(FormatTest, AlignsPipes) {
" CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
" << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
"}");
+
+ // Handle 'endl'.
+ verifyFormat("llvm::errs() << aaaa << endl\n"
+ " << bbbb << endl;");
}
TEST_F(FormatTest, UnderstandsEquals) {
@@ -4353,6 +4738,11 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
" .WillRepeatedly(Return(SomeValue));");
+ verifyFormat("void f() {\n"
+ " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
+ " .Times(2)\n"
+ " .WillRepeatedly(Return(SomeValue));\n"
+ "}");
verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
" ccccccccccccccccccccccc);");
verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
@@ -4480,6 +4870,8 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) {
" B>*>(\n"
"\n"
" );"));
+ verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
FormatStyle AlwaysBreak = getLLVMStyle();
AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
@@ -4559,11 +4951,14 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
+ verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
+
verifyFormat("test >> a >> b;");
verifyFormat("test << a >> b;");
verifyFormat("f<int>();");
verifyFormat("template <typename T> void f() {}");
+ verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
// Not template parameters.
verifyFormat("return a < b && c > d;");
@@ -4577,6 +4972,8 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
getLLVMStyleWithColumns(60));
+ verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
+ verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
}
TEST_F(FormatTest, UnderstandsBinaryOperators) {
@@ -4687,6 +5084,7 @@ TEST_F(FormatTest, UnderstandsOverloadedOperators) {
" return left.group < right.group;\n"
"}");
verifyFormat("SomeType &operator=(const SomeType &S);");
+ verifyFormat("f.template operator()<int>();");
verifyGoogleFormat("operator void*();");
verifyGoogleFormat("operator SomeType<SomeType<int>>();");
@@ -4712,6 +5110,12 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
" delete a;\n"
" delete (A *)a;\n"
"}");
+ verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
+ " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
+ verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
+ " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
+ " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
+ verifyFormat("delete[] h->p;");
}
TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
@@ -4765,6 +5169,10 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyIndependentOfContext("typedef void (*f)(int *a);");
verifyIndependentOfContext("int i{a * b};");
verifyIndependentOfContext("aaa && aaa->f();");
+ verifyIndependentOfContext("int x = ~*p;");
+ verifyFormat("Constructor() : a(a), area(width * height) {}");
+ verifyFormat("Constructor() : a(a), area(a, width * height) {}");
+ verifyFormat("void f() { f(a, c * d); }");
verifyIndependentOfContext("InvalidRegions[*R] = 0;");
@@ -4784,6 +5192,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+ verifyGoogleFormat("**outparam = 1;");
verifyGoogleFormat("int main(int argc, char** argv) {}");
verifyGoogleFormat("A<int*> a;");
verifyGoogleFormat("A<int**> a;");
@@ -4798,6 +5207,14 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyGoogleFormat("Type* t = x++ * y;");
verifyGoogleFormat(
"const char* const p = reinterpret_cast<const char* const>(q);");
+ verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
+ verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
+ verifyGoogleFormat("template <typename T>\n"
+ "void f(int i = 0, SomeType** temps = NULL);");
+
+ FormatStyle Left = getLLVMStyle();
+ Left.PointerAlignment = FormatStyle::PAS_Left;
+ verifyFormat("x = *a(x) = *a(y);", Left);
verifyIndependentOfContext("a = *(x + y);");
verifyIndependentOfContext("a = &(x + y);");
@@ -4858,6 +5275,19 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyFormat("vector<a * b> v;");
verifyFormat("foo<b && false>();");
verifyFormat("foo<b & 1>();");
+ verifyFormat("decltype(*::std::declval<const T &>()) void F();");
+ verifyFormat(
+ "template <class T, class = typename std::enable_if<\n"
+ " std::is_integral<T>::value &&\n"
+ " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
+ "void F();",
+ getLLVMStyleWithColumns(76));
+ verifyFormat(
+ "template <class T,\n"
+ " class = typename ::std::enable_if<\n"
+ " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
+ "void F();",
+ getGoogleStyleWithColumns(68));
verifyIndependentOfContext("MACRO(int *i);");
verifyIndependentOfContext("MACRO(auto *a);");
@@ -4880,7 +5310,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
// FIXME: We cannot handle this case yet; we might be able to figure out that
// foo<x> d > v; doesn't make sense.
- verifyFormat("foo<a < b && c> d > v;");
+ verifyFormat("foo<a<b && c> d> v;");
FormatStyle PointerMiddle = getLLVMStyle();
PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
@@ -4987,6 +5417,7 @@ TEST_F(FormatTest, FormatsCasts) {
verifyFormat("my_int a = (my_int)sizeof(int);");
verifyFormat("return (my_int)aaa;");
verifyFormat("#define x ((int)-1)");
+ verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
verifyFormat("#define p(q) ((int *)&q)");
verifyFormat("fn(a)(b) + 1;");
@@ -4994,11 +5425,12 @@ TEST_F(FormatTest, FormatsCasts) {
verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
verifyFormat("my_int a = (my_int)~0;");
verifyFormat("my_int a = (my_int)++a;");
- verifyFormat("my_int a = (my_int)+2;");
+ verifyFormat("my_int a = (my_int)-2;");
verifyFormat("my_int a = (my_int)1;");
verifyFormat("my_int a = (my_int *)1;");
verifyFormat("my_int a = (const my_int)-1;");
verifyFormat("my_int a = (const my_int *)-1;");
+ verifyFormat("my_int a = (my_int)(my_int)-1;");
// FIXME: single value wrapped with paren will be treated as cast.
verifyFormat("void f(int i = (kValue)*kMask) {}");
@@ -5095,6 +5527,8 @@ TEST_F(FormatTest, BreaksLongDeclarations) {
"LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
"LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
+ "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
"LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
@@ -5156,6 +5590,10 @@ TEST_F(FormatTest, BreaksLongDeclarations) {
"aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" int aaaaaaaaaaaaaaaaaaaaaaa);");
+
+ verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
+ " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
}
TEST_F(FormatTest, FormatsArrays) {
@@ -5210,6 +5648,8 @@ TEST_F(FormatTest, HandlesIncludeDirectives) {
"#include <strstream>\n"
"#endif");
+ verifyFormat("#define MY_IMPORT <a/b>");
+
// Protocol buffer definition or missing "#".
verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
getLLVMStyleWithColumns(30));
@@ -5218,6 +5658,10 @@ TEST_F(FormatTest, HandlesIncludeDirectives) {
Style.AlwaysBreakBeforeMultilineStrings = true;
Style.ColumnLimit = 0;
verifyFormat("#import \"abc.h\"", Style);
+
+ // But 'import' might also be a regular C++ namespace.
+ verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
}
//===----------------------------------------------------------------------===//
@@ -5385,6 +5829,9 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
verifyFormat("int foo(int i) { return fo1{}(i); }");
verifyFormat("int foo(int i) { return fo1{}(i); }");
verifyFormat("auto i = decltype(x){};");
+ verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
+ verifyFormat("Node n{1, Node{1000}, //\n"
+ " 2};");
// In combination with BinPackParameters = false.
FormatStyle NoBinPacking = getLLVMStyle();
@@ -5415,13 +5862,20 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
" kkkkkk,\n"
"};",
NoBinPacking);
+ verifyFormat(
+ "const Aaaaaa aaaaa = {\n"
+ " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
+ " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
+ " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
+ "};",
+ NoBinPacking);
// FIXME: The alignment of these trailing comments might be bad. Then again,
// this might be utterly useless in real code.
verifyFormat("Constructor::Constructor()\n"
- " : some_value{ //\n"
- " aaaaaaa //\n"
- " } {}");
+ " : some_value{ //\n"
+ " aaaaaaa, //\n"
+ " bbbbbbb} {}");
// In braced lists, the first comment is always assumed to belong to the
// first element. Thus, it can be moved to the next or previous line as
@@ -5445,6 +5899,13 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
" // Second element:\n"
" 2};",
getLLVMStyleWithColumns(30)));
+ // A trailing comma should still lead to an enforced line break.
+ EXPECT_EQ("vector<int> SomeVector = {\n"
+ " // aaa\n"
+ " 1, 2,\n"
+ "};",
+ format("vector<int> SomeVector = { // aaa\n"
+ " 1, 2, };"));
FormatStyle ExtraSpaces = getLLVMStyle();
ExtraSpaces.Cpp11BracedListStyle = false;
@@ -5473,32 +5934,24 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
" bbbbbbbbbbbbbbbbbbbb, bbbbb };",
ExtraSpaces);
verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
- verifyFormat("DoSomethingWithVector({\n"
- " {} /* No data */\n"
- " },\n"
- " { { 1, 2 } });",
+ verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
ExtraSpaces);
verifyFormat(
"someFunction(OtherParam,\n"
" BracedList{ // comment 1 (Forcing interesting break)\n"
" param1, param2,\n"
" // comment 2\n"
- " param3, param4\n"
- " });",
+ " param3, param4 });",
ExtraSpaces);
verifyFormat(
"std::this_thread::sleep_for(\n"
" std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
ExtraSpaces);
- verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
- " aaaaaaa, aaaaaaaaaa,\n"
- " aaaaa, aaaaaaaaaaaaaaa,\n"
- " aaa, aaaaaaaaaa,\n"
- " a, aaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaa, a\n"
- "};",
- ExtraSpaces);
+ verifyFormat(
+ "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
+ " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
+ " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
}
@@ -5546,10 +5999,9 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
" 1, 1, 1, 1, 1, 1, 1, 1, //\n"
"};",
getLLVMStyleWithColumns(39));
- verifyFormat("vector<int> x = {\n"
- " 1, 1, 1, 1, 1, 1, 1, 1,\n"
- " /**/ /**/\n"
- "};",
+ verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
+ " 1, 1, 1, 1,\n"
+ " /**/ /**/};",
getLLVMStyleWithColumns(39));
verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
" {aaaaaaaaaaaaaaaaaaa},\n"
@@ -6112,7 +6564,7 @@ TEST_F(FormatTest, FormatObjCInterface) {
"+ (id)init;\n"
"@end");
- verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
+ verifyGoogleFormat("@interface Foo (HackStuff) <MyProtocol>\n"
"+ (id)init;\n"
"@end");
@@ -6154,7 +6606,7 @@ TEST_F(FormatTest, FormatObjCInterface) {
FormatStyle OnePerLine = getGoogleStyle();
OnePerLine.BinPackParameters = false;
- verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
+ verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
@@ -6291,11 +6743,16 @@ TEST_F(FormatTest, FormatObjCMethodDeclarations) {
" evenLongerKeyword:(float)theInterval\n"
" error:(NSError **)theError {\n"
"}");
+ verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
+ " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
+ " NS_DESIGNATED_INITIALIZER;",
+ getLLVMStyleWithColumns(60));
}
TEST_F(FormatTest, FormatObjCMethodExpr) {
verifyFormat("[foo bar:baz];");
verifyFormat("return [foo bar:baz];");
+ verifyFormat("return (a)[foo bar:baz];");
verifyFormat("f([foo bar:baz]);");
verifyFormat("f(2, [foo bar:baz]);");
verifyFormat("f(2, a ? b : c);");
@@ -6353,8 +6810,10 @@ TEST_F(FormatTest, FormatObjCMethodExpr) {
// Whew!
verifyFormat("return in[42];");
+ verifyFormat("for (auto v : in[1]) {\n}");
verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
"}");
+ verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
@@ -6408,17 +6867,16 @@ TEST_F(FormatTest, FormatObjCMethodExpr) {
" der:NO]);\n"
"}",
getLLVMStyleWithColumns(70));
- verifyFormat("{\n"
- " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
- " initWithContentRect:NSMakeRect(origin_global.x,\n"
- " origin_global.y,\n"
- " pos.width(),\n"
- " pos.height())\n"
- " styleMask:NSBorderlessWindowMask\n"
- " backing:NSBackingStoreBuffered\n"
- " defer:NO]);\n"
- "}",
- getChromiumStyle(FormatStyle::LK_Cpp));
+ verifyFormat(
+ "void f() {\n"
+ " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
+ " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
+ " pos.width(), pos.height())\n"
+ " styleMask:NSBorderlessWindowMask\n"
+ " backing:NSBackingStoreBuffered\n"
+ " defer:NO]);\n"
+ "}",
+ getChromiumStyle(FormatStyle::LK_Cpp));
verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
" with:contentsNativeView];");
@@ -6957,22 +7415,14 @@ TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
}
-TEST_F(FormatTest, BreaksRawStringLiterals) {
- EXPECT_EQ("R\"x(raw )x\"\n"
- "R\"x(literal)x\";",
- format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
- EXPECT_EQ("uR\"x(raw )x\"\n"
- "uR\"x(literal)x\";",
- format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
- EXPECT_EQ("u8R\"x(raw )x\"\n"
- "u8R\"x(literal)x\";",
- format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17)));
- EXPECT_EQ("LR\"x(raw )x\"\n"
- "LR\"x(literal)x\";",
- format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
- EXPECT_EQ("UR\"x(raw )x\"\n"
- "UR\"x(literal)x\";",
- format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
+TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
+ FormatStyle Style = getGoogleStyleWithColumns(15);
+ EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
+ EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
+ EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
+ EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
+ EXPECT_EQ("u8R\"x(raw literal)x\";",
+ format("u8R\"x(raw literal)x\";", Style));
}
TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
@@ -7161,11 +7611,6 @@ TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
"\"00000000\"\n"
"\"1\"",
format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
- // FIXME: We probably don't need to care about escape sequences in raw
- // literals.
- EXPECT_EQ("R\"(\\x)\"\n"
- "R\"(\\x00)\"\n",
- format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
}
TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
@@ -7213,7 +7658,7 @@ TEST_F(FormatTest, ConfigurableIndentWidth) {
}
TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
- verifyFormat("void\n"
+ verifyFormat("double\n"
"f();",
getLLVMStyleWithColumns(8));
}
@@ -7353,11 +7798,10 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
Tab);
verifyFormat("{\n"
"\tQ({\n"
- "\t\t int a;\n"
- "\t\t someFunction(aaaaaaaaaa,\n"
- "\t\t bbbbbbbbb);\n"
- "\t },\n"
- "\t p);\n"
+ "\t\tint a;\n"
+ "\t\tsomeFunction(aaaaaaaa,\n"
+ "\t\t bbbbbbb);\n"
+ "\t}, p);\n"
"}",
Tab);
EXPECT_EQ("{\n"
@@ -7521,6 +7965,14 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
"default:\n"
" break;\n"
"}", NoSpace);
+ verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
+ verifyFormat("size_t x = sizeof(x);", NoSpace);
+ verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
+ verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
+ verifyFormat("alignas(128) char a[128];", NoSpace);
+ verifyFormat("size_t x = alignof(MyType);", NoSpace);
+ verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
+ verifyFormat("int f() throw(Deprecated);", NoSpace);
FormatStyle Space = getLLVMStyle();
Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
@@ -7557,6 +8009,14 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
verifyFormat("#if defined(x)\n"
"#endif",
Space);
+ verifyFormat("auto i = std::make_unique<int> (5);", Space);
+ verifyFormat("size_t x = sizeof (x);", Space);
+ verifyFormat("auto f (int x) -> decltype (x);", Space);
+ verifyFormat("int f (T x) noexcept (x.create ());", Space);
+ verifyFormat("alignas (128) char a[128];", Space);
+ verifyFormat("size_t x = alignof (MyType);", Space);
+ verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
+ verifyFormat("int f () throw (Deprecated);", Space);
}
TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
@@ -7618,6 +8078,60 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
"default:\n"
" break;\n"
"}", Spaces);
+
+ Spaces.SpaceAfterCStyleCast = true;
+ verifyFormat("call(x, y, z);", Spaces);
+ verifyFormat("while (( bool ) 1)\n"
+ " continue;",
+ Spaces);
+ verifyFormat("for (;;)\n"
+ " continue;",
+ Spaces);
+ verifyFormat("if (true)\n"
+ " f( );\n"
+ "else if (true)\n"
+ " f( );",
+ Spaces);
+ verifyFormat("do {\n"
+ " do_something(( int ) i);\n"
+ "} while (something( ));",
+ Spaces);
+ verifyFormat("switch (x) {\n"
+ "default:\n"
+ " break;\n"
+ "}",
+ Spaces);
+ Spaces.SpacesInCStyleCastParentheses = false;
+ Spaces.SpaceAfterCStyleCast = true;
+ verifyFormat("while ((bool) 1)\n"
+ " continue;",
+ Spaces);
+ verifyFormat("do {\n"
+ " do_something((int) i);\n"
+ "} while (something( ));",
+ Spaces);
+}
+
+TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
+ verifyFormat("int a[5];");
+ verifyFormat("a[3] += 42;");
+
+ FormatStyle Spaces = getLLVMStyle();
+ Spaces.SpacesInSquareBrackets = true;
+ // Lambdas unchanged.
+ verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
+ verifyFormat("return [i, args...] {};", Spaces);
+
+ // Not lambdas.
+ verifyFormat("int a[ 5 ];", Spaces);
+ verifyFormat("a[ 3 ] += 42;", Spaces);
+ verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
+ verifyFormat("double &operator[](int i) { return 0; }\n"
+ "int i;",
+ Spaces);
+ verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
+ verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
+ verifyFormat("int i = (*b)[ a ]->f();", Spaces);
}
TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
@@ -7633,8 +8147,8 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
}
TEST_F(FormatTest, LinuxBraceBreaking) {
- FormatStyle BreakBeforeBrace = getLLVMStyle();
- BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
+ FormatStyle LinuxBraceStyle = getLLVMStyle();
+ LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
verifyFormat("namespace a\n"
"{\n"
"class A\n"
@@ -7647,14 +8161,33 @@ TEST_F(FormatTest, LinuxBraceBreaking) {
" }\n"
" }\n"
" void g() { return; }\n"
- "}\n"
- "}",
- BreakBeforeBrace);
+ "};\n"
+ "struct B {\n"
+ " int x;\n"
+ "};\n"
+ "}\n",
+ LinuxBraceStyle);
+ verifyFormat("enum X {\n"
+ " Y = 0,\n"
+ "}\n",
+ LinuxBraceStyle);
+ verifyFormat("struct S {\n"
+ " int Type;\n"
+ " union {\n"
+ " int x;\n"
+ " double y;\n"
+ " } Value;\n"
+ " class C\n"
+ " {\n"
+ " MyFavoriteType Value;\n"
+ " } Class;\n"
+ "}\n",
+ LinuxBraceStyle);
}
TEST_F(FormatTest, StroustrupBraceBreaking) {
- FormatStyle BreakBeforeBrace = getLLVMStyle();
- BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
+ FormatStyle StroustrupBraceStyle = getLLVMStyle();
+ StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
verifyFormat("namespace a {\n"
"class A {\n"
" void f()\n"
@@ -7665,9 +8198,23 @@ TEST_F(FormatTest, StroustrupBraceBreaking) {
" }\n"
" }\n"
" void g() { return; }\n"
- "}\n"
- "}",
- BreakBeforeBrace);
+ "};\n"
+ "struct B {\n"
+ " int x;\n"
+ "};\n"
+ "}\n",
+ StroustrupBraceStyle);
+
+ verifyFormat("void foo()\n"
+ "{\n"
+ " if (a) {\n"
+ " a();\n"
+ " }\n"
+ " else {\n"
+ " b();\n"
+ " }\n"
+ "}\n",
+ StroustrupBraceStyle);
verifyFormat("#ifdef _DEBUG\n"
"int foo(int i = 0)\n"
@@ -7677,7 +8224,7 @@ TEST_F(FormatTest, StroustrupBraceBreaking) {
"{\n"
" return i;\n"
"}",
- BreakBeforeBrace);
+ StroustrupBraceStyle);
verifyFormat("void foo() {}\n"
"void bar()\n"
@@ -7689,7 +8236,7 @@ TEST_F(FormatTest, StroustrupBraceBreaking) {
"{\n"
"}\n"
"#endif",
- BreakBeforeBrace);
+ StroustrupBraceStyle);
verifyFormat("void foobar() { int i = 5; }\n"
"#ifdef _DEBUG\n"
@@ -7697,12 +8244,12 @@ TEST_F(FormatTest, StroustrupBraceBreaking) {
"#else\n"
"void bar() { foobar(); }\n"
"#endif",
- BreakBeforeBrace);
+ StroustrupBraceStyle);
}
TEST_F(FormatTest, AllmanBraceBreaking) {
- FormatStyle BreakBeforeBrace = getLLVMStyle();
- BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman;
+ FormatStyle AllmanBraceStyle = getLLVMStyle();
+ AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
verifyFormat("namespace a\n"
"{\n"
"class A\n"
@@ -7716,9 +8263,13 @@ TEST_F(FormatTest, AllmanBraceBreaking) {
" }\n"
" }\n"
" void g() { return; }\n"
- "}\n"
+ "};\n"
+ "struct B\n"
+ "{\n"
+ " int x;\n"
+ "};\n"
"}",
- BreakBeforeBrace);
+ AllmanBraceStyle);
verifyFormat("void f()\n"
"{\n"
@@ -7735,7 +8286,7 @@ TEST_F(FormatTest, AllmanBraceBreaking) {
" c();\n"
" }\n"
"}\n",
- BreakBeforeBrace);
+ AllmanBraceStyle);
verifyFormat("void f()\n"
"{\n"
@@ -7752,7 +8303,7 @@ TEST_F(FormatTest, AllmanBraceBreaking) {
" c();\n"
" } while (false)\n"
"}\n",
- BreakBeforeBrace);
+ AllmanBraceStyle);
verifyFormat("void f(int a)\n"
"{\n"
@@ -7772,18 +8323,18 @@ TEST_F(FormatTest, AllmanBraceBreaking) {
" break;\n"
" }\n"
"}\n",
- BreakBeforeBrace);
+ AllmanBraceStyle);
verifyFormat("enum X\n"
"{\n"
" Y = 0,\n"
"}\n",
- BreakBeforeBrace);
+ AllmanBraceStyle);
verifyFormat("enum X\n"
"{\n"
" Y = 0\n"
"}\n",
- BreakBeforeBrace);
+ AllmanBraceStyle);
verifyFormat("@interface BSApplicationController ()\n"
"{\n"
@@ -7791,7 +8342,7 @@ TEST_F(FormatTest, AllmanBraceBreaking) {
" id _extraIvar;\n"
"}\n"
"@end\n",
- BreakBeforeBrace);
+ AllmanBraceStyle);
verifyFormat("#ifdef _DEBUG\n"
"int foo(int i = 0)\n"
@@ -7801,7 +8352,7 @@ TEST_F(FormatTest, AllmanBraceBreaking) {
"{\n"
" return i;\n"
"}",
- BreakBeforeBrace);
+ AllmanBraceStyle);
verifyFormat("void foo() {}\n"
"void bar()\n"
@@ -7813,7 +8364,7 @@ TEST_F(FormatTest, AllmanBraceBreaking) {
"{\n"
"}\n"
"#endif",
- BreakBeforeBrace);
+ AllmanBraceStyle);
verifyFormat("void foobar() { int i = 5; }\n"
"#ifdef _DEBUG\n"
@@ -7821,37 +8372,42 @@ TEST_F(FormatTest, AllmanBraceBreaking) {
"#else\n"
"void bar() { foobar(); }\n"
"#endif",
- BreakBeforeBrace);
+ AllmanBraceStyle);
// This shouldn't affect ObjC blocks..
verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
- " // ...\n"
- " int i;\n"
+ " // ...\n"
+ " int i;\n"
"}];",
- BreakBeforeBrace);
+ AllmanBraceStyle);
verifyFormat("void (^block)(void) = ^{\n"
- " // ...\n"
- " int i;\n"
+ " // ...\n"
+ " int i;\n"
"};",
- BreakBeforeBrace);
+ AllmanBraceStyle);
// .. or dict literals.
verifyFormat("void f()\n"
"{\n"
" [object someMethod:@{ @\"a\" : @\"b\" }];\n"
"}",
- BreakBeforeBrace);
+ AllmanBraceStyle);
+ verifyFormat("int f()\n"
+ "{ // comment\n"
+ " return 42;\n"
+ "}",
+ AllmanBraceStyle);
- BreakBeforeBrace.ColumnLimit = 19;
- verifyFormat("void f() { int i; }", BreakBeforeBrace);
- BreakBeforeBrace.ColumnLimit = 18;
+ AllmanBraceStyle.ColumnLimit = 19;
+ verifyFormat("void f() { int i; }", AllmanBraceStyle);
+ AllmanBraceStyle.ColumnLimit = 18;
verifyFormat("void f()\n"
"{\n"
" int i;\n"
"}",
- BreakBeforeBrace);
- BreakBeforeBrace.ColumnLimit = 80;
+ AllmanBraceStyle);
+ AllmanBraceStyle.ColumnLimit = 80;
- FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace;
+ FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
verifyFormat("void f(bool b)\n"
@@ -8108,34 +8664,41 @@ TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
EXPECT_ALL_STYLES_EQUAL(Styles);
}
+#define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
+ Style.FIELD = false; \
+ EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
+ EXPECT_TRUE(Style.FIELD); \
+ EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
+ EXPECT_FALSE(Style.FIELD);
+
+#define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
+
#define CHECK_PARSE(TEXT, FIELD, VALUE) \
EXPECT_NE(VALUE, Style.FIELD); \
EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
EXPECT_EQ(VALUE, Style.FIELD)
-#define CHECK_PARSE_BOOL(FIELD) \
- Style.FIELD = false; \
- EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \
- EXPECT_TRUE(Style.FIELD); \
- EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \
- EXPECT_FALSE(Style.FIELD);
-
-TEST_F(FormatTest, ParsesConfiguration) {
+TEST_F(FormatTest, ParsesConfigurationBools) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
+ CHECK_PARSE_BOOL(AlignAfterOpenBracket);
CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
+ CHECK_PARSE_BOOL(AlignOperands);
CHECK_PARSE_BOOL(AlignTrailingComments);
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
+ CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
+ CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
CHECK_PARSE_BOOL(BinPackParameters);
- CHECK_PARSE_BOOL(BreakBeforeBinaryOperators);
+ CHECK_PARSE_BOOL(BinPackArguments);
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
CHECK_PARSE_BOOL(DerivePointerAlignment);
+ CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
CHECK_PARSE_BOOL(IndentCaseLabels);
CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
@@ -8143,15 +8706,24 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
CHECK_PARSE_BOOL(Cpp11BracedListStyle);
CHECK_PARSE_BOOL(SpacesInParentheses);
+ CHECK_PARSE_BOOL(SpacesInSquareBrackets);
CHECK_PARSE_BOOL(SpacesInAngles);
CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
CHECK_PARSE_BOOL(SpacesInContainerLiterals);
CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
+ CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
+}
+
+#undef CHECK_PARSE_BOOL
+TEST_F(FormatTest, ParsesConfiguration) {
+ FormatStyle Style = {};
+ Style.Language = FormatStyle::LK_Cpp;
CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
ConstructorInitializerIndentWidth, 1234u);
+ CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
@@ -8165,9 +8737,19 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
Style.PointerAlignment = FormatStyle::PAS_Middle;
- CHECK_PARSE("PointerAlignment: Left", PointerAlignment, FormatStyle::PAS_Left);
- CHECK_PARSE("PointerAlignment: Right", PointerAlignment, FormatStyle::PAS_Right);
- CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, FormatStyle::PAS_Middle);
+ CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
+ FormatStyle::PAS_Left);
+ CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
+ FormatStyle::PAS_Right);
+ CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
+ FormatStyle::PAS_Middle);
+ // For backward compatibility:
+ CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
+ FormatStyle::PAS_Left);
+ CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
+ FormatStyle::PAS_Right);
+ CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
+ FormatStyle::PAS_Middle);
Style.Standard = FormatStyle::LS_Auto;
CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
@@ -8176,24 +8758,41 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
+ Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
+ CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
+ BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
+ CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
+ FormatStyle::BOS_None);
+ CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
+ FormatStyle::BOS_All);
+ // For backward compatibility:
+ CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
+ FormatStyle::BOS_None);
+ CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
+ FormatStyle::BOS_All);
+
Style.UseTab = FormatStyle::UT_ForIndentation;
- CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
- CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
+ // For backward compatibility:
+ CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
+ CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
- CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
- AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
- CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
- AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
+ CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
+ AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
+ // For backward compatibility:
+ CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
+ AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
+ CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
+ AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
@@ -8344,7 +8943,6 @@ TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
}
#undef CHECK_PARSE
-#undef CHECK_PARSE_BOOL
TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
FormatStyle Style = {};
@@ -8519,7 +9117,10 @@ TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
Style);
+}
+TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
+ FormatStyle Style = getLLVMStyle();
Style.BreakConstructorInitializersBeforeComma = true;
Style.ConstructorInitializerIndentWidth = 4;
verifyFormat("SomeClass::Constructor()\n"
@@ -8592,6 +9193,11 @@ TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
Style);
}
+TEST_F(FormatTest, Destructors) {
+ verifyFormat("void F(int &i) { i.~int(); }");
+ verifyFormat("void F(int &i) { i->~int(); }");
+}
+
TEST_F(FormatTest, FormatsWithWebKitStyle) {
FormatStyle Style = getWebKitStyle();
@@ -8644,7 +9250,7 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) {
verifyFormat("Constructor()\n"
" : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
" , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
- " aaaaaaaaaaaaaa)\n"
+ " aaaaaaaaaaaaaa)\n"
" , aaaaaaaaaaaaaaaaaaaaaaa()\n"
"{\n"
"}",
@@ -8685,6 +9291,11 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) {
"double b; // align comments.",
Style);
+ // Do not align operands.
+ EXPECT_EQ("ASSERT(aaaa\n"
+ " || bbbb);",
+ format("ASSERT ( aaaa\n||bbbb);", Style));
+
// Accept input's line breaks.
EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
" || bbbbbbbbbbbbbbb) {\n"
@@ -8764,30 +9375,50 @@ TEST_F(FormatTest, FormatsLambdas) {
verifyFormat("SomeFunction([]() { // A cool function...\n"
" return 43;\n"
"});");
+ EXPECT_EQ("SomeFunction([]() {\n"
+ "#define A a\n"
+ " return 43;\n"
+ "});",
+ format("SomeFunction([](){\n"
+ "#define A a\n"
+ "return 43;\n"
+ "});"));
verifyFormat("void f() {\n"
" SomeFunction([](decltype(x), A *a) {});\n"
"}");
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" [](const aaaaaaaaaa &a) { return a; });");
+ verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
+ " SomeOtherFunctioooooooooooooooooooooooooon();\n"
+ "});");
+ verifyFormat("Constructor()\n"
+ " : Field([] { // comment\n"
+ " int i;\n"
+ " }) {}");
// Lambdas with return types.
verifyFormat("int c = []() -> int { return 2; }();\n");
verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
+ verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
+ verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
+ verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
+ verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
" int j) -> int {\n"
" return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
"};");
// Multiple lambdas in the same parentheses change indentation rules.
- verifyFormat("SomeFunction([]() {\n"
- " int i = 42;\n"
- " return i;\n"
- " },\n"
- " []() {\n"
- " int j = 43;\n"
- " return j;\n"
- " });");
+ verifyFormat("SomeFunction(\n"
+ " []() {\n"
+ " int i = 42;\n"
+ " return i;\n"
+ " },\n"
+ " []() {\n"
+ " int j = 43;\n"
+ " return j;\n"
+ " });");
// More complex introducers.
verifyFormat("return [i, args...] {};");
@@ -8810,89 +9441,113 @@ TEST_F(FormatTest, FormatsLambdas) {
verifyFormat("void f() {\n"
" MACRO((const AA &a) { return 1; });\n"
"}");
+
+ verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
+ " doo_dah();\n"
+ " doo_dah();\n"
+ " })) {\n"
+ "}");
}
TEST_F(FormatTest, FormatsBlocks) {
- verifyFormat("int (^Block)(int, int);");
- verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
- verifyFormat("void (^block)(int) = ^(id test) { int i; };");
- verifyFormat("void (^block)(int) = ^(int test) { int i; };");
- verifyFormat("void (^block)(int) = ^id(int test) { int i; };");
- verifyFormat("void (^block)(int) = ^int(int test) { int i; };");
-
- verifyFormat("foo(^{ bar(); });");
- verifyFormat("foo(a, ^{ bar(); });");
-
- // FIXME: Make whitespace formatting consistent. Ask a ObjC dev how
- // it would ideally look.
- verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];");
- verifyFormat("int i = {[operation setCompletionBlock : ^{ [self "
- "onOperationDone]; }]};");
- verifyFormat("[operation setCompletionBlock:^(int *i) { f(); }];");
- verifyFormat("int a = [operation block:^int(int *i) { return 1; }];");
+ FormatStyle ShortBlocks = getLLVMStyle();
+ ShortBlocks.AllowShortBlocksOnASingleLine = true;
+ verifyFormat("int (^Block)(int, int);", ShortBlocks);
+ verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
+ verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
+ verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
+ verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
+ verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
+
+ verifyFormat("foo(^{ bar(); });", ShortBlocks);
+ verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
+ verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
+
+ verifyFormat("[operation setCompletionBlock:^{\n"
+ " [self onOperationDone];\n"
+ "}];");
+ verifyFormat("int i = {[operation setCompletionBlock:^{\n"
+ " [self onOperationDone];\n"
+ "}]};");
+ verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
+ " f();\n"
+ "}];");
+ verifyFormat("int a = [operation block:^int(int *i) {\n"
+ " return 1;\n"
+ "}];");
verifyFormat("[myObject doSomethingWith:arg1\n"
- " aaa:^int(int *a) { return 1; }\n"
+ " aaa:^int(int *a) {\n"
+ " return 1;\n"
+ " }\n"
" bbb:f(a * bbbbbbbb)];");
verifyFormat("[operation setCompletionBlock:^{\n"
- " [self.delegate newDataAvailable];\n"
+ " [self.delegate newDataAvailable];\n"
"}];",
getLLVMStyleWithColumns(60));
verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
- " NSString *path = [self sessionFilePath];\n"
- " if (path) {\n"
- " // ...\n"
- " }\n"
+ " NSString *path = [self sessionFilePath];\n"
+ " if (path) {\n"
+ " // ...\n"
+ " }\n"
"});");
verifyFormat("[[SessionService sharedService]\n"
" loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
- " if (window) {\n"
- " [self windowDidLoad:window];\n"
- " } else {\n"
- " [self errorLoadingWindow];\n"
- " }\n"
+ " if (window) {\n"
+ " [self windowDidLoad:window];\n"
+ " } else {\n"
+ " [self errorLoadingWindow];\n"
+ " }\n"
" }];");
verifyFormat("void (^largeBlock)(void) = ^{\n"
- " // ...\n"
+ " // ...\n"
"};\n",
getLLVMStyleWithColumns(40));
verifyFormat("[[SessionService sharedService]\n"
" loadWindowWithCompletionBlock: //\n"
" ^(SessionWindow *window) {\n"
- " if (window) {\n"
- " [self windowDidLoad:window];\n"
- " } else {\n"
- " [self errorLoadingWindow];\n"
- " }\n"
+ " if (window) {\n"
+ " [self windowDidLoad:window];\n"
+ " } else {\n"
+ " [self errorLoadingWindow];\n"
+ " }\n"
" }];",
getLLVMStyleWithColumns(60));
verifyFormat("[myObject doSomethingWith:arg1\n"
" firstBlock:^(Foo *a) {\n"
- " // ...\n"
- " int i;\n"
+ " // ...\n"
+ " int i;\n"
" }\n"
" secondBlock:^(Bar *b) {\n"
- " // ...\n"
- " int i;\n"
+ " // ...\n"
+ " int i;\n"
" }\n"
" thirdBlock:^Foo(Bar *b) {\n"
- " // ...\n"
- " int i;\n"
+ " // ...\n"
+ " int i;\n"
" }];");
verifyFormat("[myObject doSomethingWith:arg1\n"
" firstBlock:-1\n"
" secondBlock:^(Bar *b) {\n"
- " // ...\n"
- " int i;\n"
+ " // ...\n"
+ " int i;\n"
" }];");
verifyFormat("f(^{\n"
- " @autoreleasepool {\n"
- " if (a) {\n"
- " g();\n"
- " }\n"
+ " @autoreleasepool {\n"
+ " if (a) {\n"
+ " g();\n"
" }\n"
+ " }\n"
"});");
+ verifyFormat("Block b = ^int *(A *a, B *b) {}");
+
+ FormatStyle FourIndent = getLLVMStyle();
+ FourIndent.ObjCBlockIndentWidth = 4;
+ verifyFormat("[operation setCompletionBlock:^{\n"
+ " [self onOperationDone];\n"
+ "}];",
+ FourIndent);
}
TEST_F(FormatTest, SupportsCRLF) {
@@ -9097,5 +9752,32 @@ TEST_F(FormatTest, HandleConflictMarkers) {
"int i;\n"));
}
+TEST_F(FormatTest, DisableRegions) {
+ EXPECT_EQ("int i;\n"
+ "// clang-format off\n"
+ " int j;\n"
+ "// clang-format on\n"
+ "int k;",
+ format(" int i;\n"
+ " // clang-format off\n"
+ " int j;\n"
+ " // clang-format on\n"
+ " int k;"));
+ EXPECT_EQ("int i;\n"
+ "/* clang-format off */\n"
+ " int j;\n"
+ "/* clang-format on */\n"
+ "int k;",
+ format(" int i;\n"
+ " /* clang-format off */\n"
+ " int j;\n"
+ " /* clang-format on */\n"
+ " int k;"));
+}
+
+TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
+ format("? ) =");
+}
+
} // end namespace tooling
} // end namespace clang