summaryrefslogtreecommitdiff
path: root/unittests/Format/FormatTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'unittests/Format/FormatTest.cpp')
-rw-r--r--unittests/Format/FormatTest.cpp248
1 files changed, 233 insertions, 15 deletions
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index f694c5b214..58dbffd863 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -32,8 +32,8 @@ protected:
CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
getFormattingLangOpts());
- tooling::Replacements Replace = reformat(
- Style, Lex, Context.Sources, Ranges, new IgnoringDiagConsumer());
+ tooling::Replacements Replace =
+ reformat(Style, Lex, Context.Sources, Ranges);
ReplacementCount = Replace.size();
EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n");
@@ -218,7 +218,7 @@ TEST_F(FormatTest, ReformatsMovedLines) {
// Tests for control statements.
//===----------------------------------------------------------------------===//
-TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
+TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
verifyFormat("if (true)\n f();\ng();");
verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
@@ -245,6 +245,10 @@ TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
"}",
AllowsMergedIf);
+ EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
+ EXPECT_EQ("if (a) return; // comment",
+ format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
+
AllowsMergedIf.ColumnLimit = 14;
verifyFormat("if (a) return;", AllowsMergedIf);
verifyFormat("if (aaaaaaaaa)\n"
@@ -741,11 +745,11 @@ TEST_F(FormatTest, SplitsLongCxxComments) {
}
TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
- EXPECT_EQ("namespace {}\n// Test\n#define A",
+ EXPECT_EQ("namespace {\n}\n// Test\n#define A",
format("namespace {}\n // Test\n#define A"));
- EXPECT_EQ("namespace {}\n/* Test */\n#define A",
+ EXPECT_EQ("namespace {\n}\n/* Test */\n#define A",
format("namespace {}\n /* Test */\n#define A"));
- EXPECT_EQ("namespace {}\n/* Test */ #define A",
+ EXPECT_EQ("namespace {\n}\n/* Test */ #define A",
format("namespace {}\n /* Test */ #define A"));
}
@@ -1106,7 +1110,7 @@ TEST_F(FormatTest, FormatsInlineASM) {
"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\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
" : \"a\"(value));");
}
@@ -1642,6 +1646,14 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
" SourceMgr.getSpellingColumnNumber(\n"
" TheLine.Last->FormatTok.Tok.getLocation()) -\n"
" 1);");
+
+ FormatStyle OnePerLine = getLLVMStyle();
+ OnePerLine.BinPackParameters = false;
+ verifyFormat(
+ "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
+ OnePerLine);
}
TEST_F(FormatTest, ExpressionIndentation) {
@@ -1761,6 +1773,33 @@ TEST_F(FormatTest, MemoizationTests) {
" aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
" aaaaa())))))))))))))))))))))))))))))))))))))));",
getLLVMStyleWithColumns(65));
+ verifyFormat(
+ "aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa(\n"
+ " aaaaa,\n"
+ " aaaaa))))))))))));",
+ getLLVMStyleWithColumns(65));
// This test takes VERY long when memoization is broken.
FormatStyle OnePerLine = getLLVMStyle();
@@ -1963,7 +2002,7 @@ TEST_F(FormatTest, FormatsBuilderPattern) {
" .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
" .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
" .Default(ORDER_TEXT);\n");
-
+
verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
" aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
verifyFormat(
@@ -2204,6 +2243,14 @@ TEST_F(FormatTest, AlignsStringLiterals) {
"#define LL_FORMAT \"ll\"\n"
"printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
" \"d, ddddddddd: %\" LL_FORMAT \"d\");");
+
+ verifyFormat("#define A(X) \\\n"
+ " \"aaaaa\" #X \"bbbbbb\" \\\n"
+ " \"ccccc\"",
+ getLLVMStyleWithColumns(23));
+ verifyFormat("#define A \"def\"\n"
+ "f(\"abc\" A \"ghi\"\n"
+ " \"jkl\");");
}
TEST_F(FormatTest, AlignsPipes) {
@@ -2638,10 +2685,16 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
+ verifyFormat("#define MACRO \\\n"
+ " int *i = a * b; \\\n"
+ " void f(a *b);",
+ getLLVMStyleWithColumns(19));
+
verifyIndependentOfContext("A = new SomeType *[Length];");
verifyIndependentOfContext("A = new SomeType *[Length]();");
verifyGoogleFormat("A = new SomeType* [Length]();");
verifyGoogleFormat("A = new SomeType* [Length];");
+
FormatStyle PointerLeft = getLLVMStyle();
PointerLeft.PointerBindsToType = true;
verifyFormat("delete *x;", PointerLeft);
@@ -2749,6 +2802,10 @@ TEST_F(FormatTest, FormatsFunctionTypes) {
verifyGoogleFormat("A<void*(int*, SomeType*)>;");
verifyGoogleFormat("void* (*a)(int);");
+
+ // Other constructs can look somewhat like function types:
+ verifyFormat("A<sizeof(*x)> a;");
+ verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
}
TEST_F(FormatTest, BreaksLongDeclarations) {
@@ -2778,6 +2835,10 @@ TEST_F(FormatTest, BreaksLongDeclarations) {
" ReallyReallyLongParameterName,\n"
" const SomeType<string, SomeOtherTemplateParameter> &\n"
" AnotherLongParameterName) {}");
+ verifyFormat("template <typename A>\n"
+ "SomeLoooooooooooooooooooooongType<\n"
+ " typename some_namespace::SomeOtherType<A>::Type>\n"
+ "Function() {}");
verifyFormat(
"aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
" aaaaaaaaaaaaaaaaaaaaaaa;");
@@ -2860,7 +2921,10 @@ TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
" return\n"
"}",
format("void f ( ) { if ( a ) return }"));
- EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }"));
+ EXPECT_EQ("namespace N {\n"
+ "void f()\n"
+ "}",
+ format("namespace N { void f() }"));
EXPECT_EQ("namespace N {\n"
"void f() {}\n"
"void g()\n"
@@ -3016,6 +3080,13 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
verifyFormat("class A::B::C {\n} n;");
// Template definitions.
+ verifyFormat(
+ "template <typename F>\n"
+ "Matcher(const Matcher<F> &Other,\n"
+ " typename enable_if_c<is_base_of<F, T>::value &&\n"
+ " !is_same<F, T>::value>::type * = 0)\n"
+ " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
+
// FIXME: This is still incorrectly handled at the formatter side.
verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
@@ -3765,7 +3836,7 @@ TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
"a;\n"
"}\n"
"{\n"
- " b;\n"
+ " b; //\n"
"}\n"
"}",
format("{\n"
@@ -3773,15 +3844,15 @@ TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
"a;\n"
"}\n"
"{\n"
- " b;\n"
+ " b; //\n"
"}\n"
"}",
22, 2, getLLVMStyle()));
EXPECT_EQ(" {\n"
- " a;\n"
+ " a; //\n"
" }",
format(" {\n"
- "a;\n"
+ "a; //\n"
" }",
4, 2, getLLVMStyle()));
EXPECT_EQ("void f() {}\n"
@@ -3899,6 +3970,48 @@ TEST_F(FormatTest, BreakStringLiterals) {
format("#define A \"some text other\";", AlignLeft));
}
+TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
+ EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
+ " \"ddeeefff\");",
+ format("someFunction(\"aaabbbcccdddeeefff\");",
+ getLLVMStyleWithColumns(25)));
+ EXPECT_EQ("someFunction1234567890(\n"
+ " \"aaabbbcccdddeeefff\");",
+ format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
+ getLLVMStyleWithColumns(26)));
+ EXPECT_EQ("someFunction1234567890(\n"
+ " \"aaabbbcccdddeeeff\"\n"
+ " \"f\");",
+ format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
+ getLLVMStyleWithColumns(25)));
+ EXPECT_EQ("someFunction1234567890(\n"
+ " \"aaabbbcccdddeeeff\"\n"
+ " \"f\");",
+ format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
+ getLLVMStyleWithColumns(24)));
+ EXPECT_EQ("someFunction(\n"
+ " \"aaabbbcc \"\n"
+ " \"dddeeefff\");",
+ format("someFunction(\"aaabbbcc dddeeefff\");",
+ getLLVMStyleWithColumns(25)));
+ EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
+ " \"ddeeefff\");",
+ format("someFunction(\"aaabbbccc ddeeefff\");",
+ getLLVMStyleWithColumns(25)));
+ EXPECT_EQ("someFunction1234567890(\n"
+ " \"aaabb \"\n"
+ " \"cccdddeeefff\");",
+ format("someFunction1234567890(\"aaabb cccdddeeefff\");",
+ getLLVMStyleWithColumns(25)));
+ EXPECT_EQ("#define A \\\n"
+ " string s = \\\n"
+ " \"123456789\" \\\n"
+ " \"0\"; \\\n"
+ " int i;",
+ format("#define A string s = \"1234567890\"; int i;",
+ getLLVMStyleWithColumns(20)));
+}
+
TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
EXPECT_EQ("\"\\a\"",
format("\"\\a\"", getLLVMStyleWithColumns(3)));
@@ -3953,6 +4066,101 @@ TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
"}");
}
+TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
+ verifyFormat("class X {\n"
+ " void f() {\n"
+ " }\n"
+ "};",
+ getLLVMStyleWithColumns(12));
+}
+
+TEST_F(FormatTest, ConfigurableIndentWidth) {
+ FormatStyle EightIndent = getLLVMStyleWithColumns(18);
+ EightIndent.IndentWidth = 8;
+ verifyFormat("void f() {\n"
+ " someFunction();\n"
+ " if (true) {\n"
+ " f();\n"
+ " }\n"
+ "}",
+ EightIndent);
+ verifyFormat("class X {\n"
+ " void f() {\n"
+ " }\n"
+ "};",
+ EightIndent);
+ verifyFormat("int x[] = {\n"
+ " call(),\n"
+ " call(),\n"
+ "};",
+ EightIndent);
+}
+
+TEST_F(FormatTest, ConfigurableUseOfTab) {
+ FormatStyle Tab = getLLVMStyleWithColumns(42);
+ Tab.IndentWidth = 8;
+ Tab.UseTab = true;
+ Tab.AlignEscapedNewlinesLeft = true;
+ verifyFormat("class X {\n"
+ "\tvoid f() {\n"
+ "\t\tsomeFunction(parameter1,\n"
+ "\t\t\t parameter2);\n"
+ "\t}\n"
+ "};",
+ Tab);
+ verifyFormat("#define A \\\n"
+ "\tvoid f() { \\\n"
+ "\t\tsomeFunction( \\\n"
+ "\t\t parameter1, \\\n"
+ "\t\t parameter2); \\\n"
+ "\t}",
+ Tab);
+}
+
+TEST_F(FormatTest, LinuxBraceBreaking) {
+ FormatStyle BreakBeforeBrace = getLLVMStyle();
+ BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
+ verifyFormat("namespace a\n"
+ "{\n"
+ "class A\n"
+ "{\n"
+ " void f()\n"
+ " {\n"
+ " if (true) {\n"
+ " a();\n"
+ " b();\n"
+ " }\n"
+ " }\n"
+ " void g()\n"
+ " {\n"
+ " return;\n"
+ " }\n"
+ "}\n"
+ "}",
+ BreakBeforeBrace);
+}
+
+TEST_F(FormatTest, StroustrupBraceBreaking) {
+ FormatStyle BreakBeforeBrace = getLLVMStyle();
+ BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
+ verifyFormat("namespace a {\n"
+ "class A {\n"
+ " void f()\n"
+ " {\n"
+ " if (true) {\n"
+ " a();\n"
+ " b();\n"
+ " }\n"
+ " }\n"
+ " void g()\n"
+ " {\n"
+ " return;\n"
+ " }\n"
+ "}\n"
+ "}",
+ BreakBeforeBrace);
+}
+
bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
for (size_t i = 1; i < Styles.size(); ++i)
if (!(Styles[0] == Styles[i]))
@@ -3994,9 +4202,9 @@ TEST_F(FormatTest, ParsesConfiguration) {
#define CHECK_PARSE_BOOL(FIELD) \
Style.FIELD = false; \
EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \
- EXPECT_EQ(true, Style.FIELD); \
+ EXPECT_TRUE(Style.FIELD); \
EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \
- EXPECT_EQ(false, Style.FIELD);
+ EXPECT_FALSE(Style.FIELD);
CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
@@ -4007,6 +4215,7 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE_BOOL(IndentCaseLabels);
CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
CHECK_PARSE_BOOL(PointerBindsToType);
+ CHECK_PARSE_BOOL(UseTab);
CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
@@ -4016,6 +4225,7 @@ TEST_F(FormatTest, ParsesConfiguration) {
PenaltyReturnTypeOnItsOwnLine, 1234u);
CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
SpacesBeforeTrailingComments, 1234u);
+ CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
Style.Standard = FormatStyle::LS_Auto;
CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
@@ -4027,6 +4237,14 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
+ Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
+ CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
+ FormatStyle::BS_Attach);
+ CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
+ FormatStyle::BS_Linux);
+ CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
+ FormatStyle::BS_Stroustrup);
+
#undef CHECK_PARSE
#undef CHECK_PARSE_BOOL
}