aboutsummaryrefslogtreecommitdiff
path: root/unittests/Format/FormatTest.cpp
diff options
context:
space:
mode:
authorEli Bendersky <eliben@chromium.org>2013-07-15 16:08:08 -0700
committerEli Bendersky <eliben@chromium.org>2013-07-15 16:08:08 -0700
commite789858899a7b36caf11b371a97411a1582a482b (patch)
treee8c28b178b32010f73b477b3c65b5ff74437530c /unittests/Format/FormatTest.cpp
parent99a5501f5ae5b75017dfc386d4abf648234e85df (diff)
parent20c7d45a4da9f58ad805ad1d37f92fe7dc232ec8 (diff)
Merge commit '20c7d45a4da9f58ad805ad1d37f92fe7dc232ec8'
Conflicts: lib/CodeGen/ItaniumCXXABI.cpp
Diffstat (limited to 'unittests/Format/FormatTest.cpp')
-rw-r--r--unittests/Format/FormatTest.cpp836
1 files changed, 786 insertions, 50 deletions
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 5cab6f96f3..4c948e89e9 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -302,6 +302,7 @@ TEST_F(FormatTest, FormatsForLoop) {
verifyFormat("for (;;) {\n"
" f();\n"
"}");
+ verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
verifyFormat(
"for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
@@ -311,8 +312,8 @@ TEST_F(FormatTest, FormatsForLoop) {
verifyFormat(
"for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
" ++IIIII) {\n}");
- verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " aaaaaaaaaaa = aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
+ verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
+ " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
" aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
" I = FD->getDeclsInPrototypeScope().begin(),\n"
@@ -323,12 +324,16 @@ TEST_F(FormatTest, FormatsForLoop) {
verifyFormat(
"for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
" ++aaaaaaaaaaa) {\n}");
verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
" aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
"}");
+ verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
+ " aaaaaaaaaa);\n"
+ " iter; ++iter) {\n"
+ "}");
FormatStyle NoBinPacking = getLLVMStyle();
NoBinPacking.BinPackParameters = false;
@@ -426,7 +431,28 @@ TEST_F(FormatTest, FormatsSwitchStatement) {
verifyFormat("switch (x) {\n"
"default: {\n"
" // Do nothing.\n"
+ "}\n"
+ "}");
+ verifyFormat("switch (x) {\n"
+ "// comment\n"
+ "// if 1, do f()\n"
+ "case 1:\n"
+ " f();\n"
+ "}");
+ verifyFormat("switch (x) {\n"
+ "case 1:\n"
+ " // Do amazing stuff\n"
+ " {\n"
+ " f();\n"
+ " g();\n"
+ " }\n"
+ " break;\n"
"}");
+ verifyFormat("#define A \\\n"
+ " switch (x) { \\\n"
+ " case a: \\\n"
+ " foo = b; \\\n"
+ " }", getLLVMStyleWithColumns(20));
verifyGoogleFormat("switch (x) {\n"
" case 1:\n"
@@ -471,6 +497,7 @@ TEST_F(FormatTest, FormatsLabels) {
//===----------------------------------------------------------------------===//
TEST_F(FormatTest, UnderstandsSingleLineComments) {
+ verifyFormat("//* */");
verifyFormat("// line 1\n"
"// line 2\n"
"void f() {}\n");
@@ -572,6 +599,36 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
verifyGoogleFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaa); // 81 cols with this comment");
+ EXPECT_EQ("D(a, {\n"
+ " // test\n"
+ " int a;\n"
+ "});",
+ format("D(a, {\n"
+ "// test\n"
+ "int a;\n"
+ "});"));
+}
+
+TEST_F(FormatTest, CanFormatCommentsLocally) {
+ EXPECT_EQ("int a; // comment\n"
+ "int b; // comment",
+ format("int a; // comment\n"
+ "int b; // comment",
+ 0, 0, getLLVMStyle()));
+ EXPECT_EQ("int a; // comment\n"
+ " // line 2\n"
+ "int b;",
+ format("int a; // comment\n"
+ " // line 2\n"
+ "int b;",
+ 28, 0, getLLVMStyle()));
+ EXPECT_EQ("int aaaaaa; // comment\n"
+ "int b;\n"
+ "int c; // unrelated comment",
+ format("int aaaaaa; // comment\n"
+ "int b;\n"
+ "int c; // unrelated comment",
+ 31, 0, getLLVMStyle()));
}
TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
@@ -586,7 +643,7 @@ TEST_F(FormatTest, UnderstandsMultiLineComments) {
EXPECT_EQ(
"f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
" bbbbbbbbbbbbbbbbbbbbbbbbb);",
- format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n"
+ format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n/* Trailing comment for aa... */\n"
" bbbbbbbbbbbbbbbbbbbbbbbbb);"));
EXPECT_EQ(
"f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
@@ -603,6 +660,251 @@ TEST_F(FormatTest, UnderstandsMultiLineComments) {
NoBinPacking);
}
+TEST_F(FormatTest, AlignsMultiLineComments) {
+ EXPECT_EQ("/*\n"
+ " * Really multi-line\n"
+ " * comment.\n"
+ " */\n"
+ "void f() {}",
+ format(" /*\n"
+ " * Really multi-line\n"
+ " * comment.\n"
+ " */\n"
+ " void f() {}"));
+ EXPECT_EQ("class C {\n"
+ " /*\n"
+ " * Another multi-line\n"
+ " * comment.\n"
+ " */\n"
+ " void f() {}\n"
+ "};",
+ format("class C {\n"
+ "/*\n"
+ " * Another multi-line\n"
+ " * comment.\n"
+ " */\n"
+ "void f() {}\n"
+ "};"));
+ EXPECT_EQ("/*\n"
+ " 1. This is a comment with non-trivial formatting.\n"
+ " 1.1. We have to indent/outdent all lines equally\n"
+ " 1.1.1. to keep the formatting.\n"
+ " */",
+ format(" /*\n"
+ " 1. This is a comment with non-trivial formatting.\n"
+ " 1.1. We have to indent/outdent all lines equally\n"
+ " 1.1.1. to keep the formatting.\n"
+ " */"));
+ EXPECT_EQ("/*\n"
+ " Don't try to outdent if there's not enough inentation.\n"
+ " */",
+ format(" /*\n"
+ " Don't try to outdent if there's not enough inentation.\n"
+ " */"));
+}
+
+TEST_F(FormatTest, SplitsLongCxxComments) {
+ EXPECT_EQ("// A comment that\n"
+ "// doesn't fit on\n"
+ "// one line",
+ format("// A comment that doesn't fit on one line",
+ getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("// a b c d\n"
+ "// e f g\n"
+ "// h i j k",
+ format("// a b c d e f g h i j k",
+ getLLVMStyleWithColumns(10)));
+ EXPECT_EQ("// a b c d\n"
+ "// e f g\n"
+ "// h i j k",
+ format("\\\n// a b c d e f g h i j k",
+ getLLVMStyleWithColumns(10)));
+ EXPECT_EQ("if (true) // A comment that\n"
+ " // doesn't fit on\n"
+ " // one line",
+ format("if (true) // A comment that doesn't fit on one line ",
+ getLLVMStyleWithColumns(30)));
+ EXPECT_EQ("// Don't_touch_leading_whitespace",
+ format("// Don't_touch_leading_whitespace",
+ getLLVMStyleWithColumns(20)));
+ EXPECT_EQ(
+ "//Don't add leading\n"
+ "//whitespace",
+ format("//Don't add leading whitespace", getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("// A comment before\n"
+ "// a macro\n"
+ "// definition\n"
+ "#define a b",
+ format("// A comment before a macro definition\n"
+ "#define a b",
+ getLLVMStyleWithColumns(20)));
+}
+
+TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
+ EXPECT_EQ("namespace {}\n// Test\n#define A",
+ format("namespace {}\n // Test\n#define A"));
+ EXPECT_EQ("namespace {}\n/* Test */\n#define A",
+ format("namespace {}\n /* Test */\n#define A"));
+ EXPECT_EQ("namespace {}\n/* Test */ #define A",
+ format("namespace {}\n /* Test */ #define A"));
+}
+
+TEST_F(FormatTest, SplitsLongLinesInComments) {
+ EXPECT_EQ("/* This is a long\n"
+ " * comment that\n"
+ " * doesn't\n"
+ " * fit on one line.\n"
+ " */",
+ format("/* "
+ "This is a long "
+ "comment that "
+ "doesn't "
+ "fit on one line. */",
+ getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("/* a b c d\n"
+ " * e f g\n"
+ " * h i j k\n"
+ " */",
+ format("/* a b c d e f g h i j k */",
+ getLLVMStyleWithColumns(10)));
+ EXPECT_EQ("/* a b c d\n"
+ " * e f g\n"
+ " * h i j k\n"
+ " */",
+ format("\\\n/* a b c d e f g h i j k */",
+ getLLVMStyleWithColumns(10)));
+ EXPECT_EQ("/*\n"
+ "This is a long\n"
+ "comment that doesn't\n"
+ "fit on one line.\n"
+ "*/",
+ format("/*\n"
+ "This is a long "
+ "comment that doesn't "
+ "fit on one line. \n"
+ "*/", getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("/*\n"
+ " * This is a long\n"
+ " * comment that\n"
+ " * doesn't fit on\n"
+ " * one line.\n"
+ " */",
+ format("/* \n"
+ " * This is a long "
+ " comment that "
+ " doesn't fit on "
+ " one line. \n"
+ " */", getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("/*\n"
+ " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
+ " * so_it_should_be_broken\n"
+ " * wherever_a_space_occurs\n"
+ " */",
+ format("/*\n"
+ " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
+ " so_it_should_be_broken "
+ " wherever_a_space_occurs \n"
+ " */",
+ getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("/*\n"
+ " * This_comment_can_not_be_broken_into_lines\n"
+ " */",
+ format("/*\n"
+ " * This_comment_can_not_be_broken_into_lines\n"
+ " */",
+ getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("{\n"
+ " /*\n"
+ " This is another\n"
+ " long comment that\n"
+ " doesn't fit on one\n"
+ " line 1234567890\n"
+ " */\n"
+ "}",
+ format("{\n"
+ "/*\n"
+ "This is another "
+ " long comment that "
+ " doesn't fit on one"
+ " line 1234567890\n"
+ "*/\n"
+ "}", getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("{\n"
+ " /*\n"
+ " * This i s\n"
+ " * another comment\n"
+ " * t hat doesn' t\n"
+ " * fit on one l i\n"
+ " * n e\n"
+ " */\n"
+ "}",
+ format("{\n"
+ "/*\n"
+ " * This i s"
+ " another comment"
+ " t hat doesn' t"
+ " fit on one l i"
+ " n e\n"
+ " */\n"
+ "}", getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("/*\n"
+ " * This is a long\n"
+ " * comment that\n"
+ " * doesn't fit on\n"
+ " * one line\n"
+ " */",
+ format(" /*\n"
+ " * This is a long comment that doesn't fit on one line\n"
+ " */", getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("{\n"
+ " if (something) /* This is a\n"
+ "long comment */\n"
+ " ;\n"
+ "}",
+ format("{\n"
+ " if (something) /* This is a long comment */\n"
+ " ;\n"
+ "}",
+ getLLVMStyleWithColumns(30)));
+}
+
+TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
+ EXPECT_EQ("#define X \\\n"
+ " /* \\\n"
+ " Test \\\n"
+ " Macro comment \\\n"
+ " with a long \\\n"
+ " line \\\n"
+ " */ \\\n"
+ " A + B",
+ format("#define X \\\n"
+ " /*\n"
+ " Test\n"
+ " Macro comment with a long line\n"
+ " */ \\\n"
+ " A + B",
+ getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("#define X \\\n"
+ " /* Macro comment \\\n"
+ " with a long \\\n"
+ " line */ \\\n"
+ " A + B",
+ format("#define X \\\n"
+ " /* Macro comment with a long\n"
+ " line */ \\\n"
+ " A + B",
+ getLLVMStyleWithColumns(20)));
+ EXPECT_EQ("#define X \\\n"
+ " /* Macro comment \\\n"
+ " * with a long \\\n"
+ " * line */ \\\n"
+ " A + B",
+ format("#define X \\\n"
+ " /* Macro comment with a long line */ \\\n"
+ " A + B",
+ getLLVMStyleWithColumns(20)));
+}
+
TEST_F(FormatTest, CommentsInStaticInitializers) {
EXPECT_EQ(
"static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
@@ -674,6 +976,7 @@ TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
verifyFormat("class A {\n"
"public:\n"
+ "public: // comment\n"
"protected:\n"
"private:\n"
" void f() {}\n"
@@ -686,7 +989,29 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
"};");
}
-TEST_F(FormatTest, FormatsDerivedClass) {
+TEST_F(FormatTest, SeparatesLogicalBlocks) {
+ EXPECT_EQ("class A {\n"
+ "public:\n"
+ " void f();\n"
+ "\n"
+ "private:\n"
+ " void g() {}\n"
+ " // test\n"
+ "protected:\n"
+ " int h;\n"
+ "};",
+ format("class A {\n"
+ "public:\n"
+ "void f();\n"
+ "private:\n"
+ "void g() {}\n"
+ "// test\n"
+ "protected:\n"
+ "int h;\n"
+ "};"));
+}
+
+TEST_F(FormatTest, FormatsClasses) {
verifyFormat("class A : public B {\n};");
verifyFormat("class A : public ::B {\n};");
@@ -694,9 +1019,9 @@ TEST_F(FormatTest, FormatsDerivedClass) {
"class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
" public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n"
"};\n");
- verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA :\n"
- " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
- " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n"
+ verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
+ " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
+ " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n"
"};\n");
verifyFormat(
"class A : public B, public C, public D, public E, public F, public G {\n"
@@ -708,6 +1033,10 @@ TEST_F(FormatTest, FormatsDerivedClass) {
" public F,\n"
" public G {\n"
"};");
+
+ verifyFormat("class\n"
+ " ReallyReallyLongClassName {\n};",
+ getLLVMStyleWithColumns(32));
}
TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
@@ -771,6 +1100,16 @@ TEST_F(FormatTest, FormatsNamespaces) {
TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
+TEST_F(FormatTest, FormatsInlineASM) {
+ verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
+ 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));");
+}
+
TEST_F(FormatTest, FormatTryCatch) {
// FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
// also not create single-line-blocks.
@@ -881,8 +1220,9 @@ TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
}
TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
- verifyFormat("virtual void write(ELFWriter *writerrr,\n"
- " OwningPtr<FileOutputBuffer> &buffer) = 0;");
+ verifyFormat(
+ "virtual void write(ELFWriter *writerrr,\n"
+ " OwningPtr<FileOutputBuffer> &buffer) = 0;");
}
TEST_F(FormatTest, LayoutUnknownPPDirective) {
@@ -920,18 +1260,18 @@ TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
}
TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
- EXPECT_EQ("// some comment\n"
+ EXPECT_EQ("// somecomment\n"
"#include \"a.h\"\n"
"#define A( \\\n"
" A, B)\n"
"#include \"b.h\"\n"
- "// some comment\n",
- format(" // some comment\n"
+ "// somecomment\n",
+ format(" // somecomment\n"
" #include \"a.h\"\n"
"#define A(A,\\\n"
" B)\n"
" #include \"b.h\"\n"
- " // some comment\n",
+ " // somecomment\n",
getLLVMStyleWithColumns(13)));
}
@@ -1010,9 +1350,11 @@ TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
}
TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
+ verifyFormat("#define A :");
+
// FIXME: Improve formatting of case labels in macros.
verifyFormat("#define SOMECASES \\\n"
- "case 1: \\\n"
+ " case 1: \\\n"
" case 2\n",
getLLVMStyleWithColumns(20));
@@ -1021,6 +1363,115 @@ TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
"f(STR(this_is_a_string_literal{));");
}
+TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
+ EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
+ "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
+ "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
+ "class X {\n"
+ "};\n"
+ "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
+ "int *createScopDetectionPass() { return 0; }",
+ format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
+ " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
+ " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
+ " class X {};\n"
+ " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
+ " int *createScopDetectionPass() { return 0; }"));
+ // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
+ // braces, so that inner block is indented one level more.
+ EXPECT_EQ("int q() {\n"
+ " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
+ " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
+ " IPC_END_MESSAGE_MAP()\n"
+ "}",
+ format("int q() {\n"
+ " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
+ " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
+ " IPC_END_MESSAGE_MAP()\n"
+ "}"));
+ EXPECT_EQ("int q() {\n"
+ " f(x);\n"
+ " f(x) {}\n"
+ " f(x)->g();\n"
+ " f(x)->*g();\n"
+ " f(x).g();\n"
+ " f(x) = x;\n"
+ " f(x) += x;\n"
+ " f(x) -= x;\n"
+ " f(x) *= x;\n"
+ " f(x) /= x;\n"
+ " f(x) %= x;\n"
+ " f(x) &= x;\n"
+ " f(x) |= x;\n"
+ " f(x) ^= x;\n"
+ " f(x) >>= x;\n"
+ " f(x) <<= x;\n"
+ " f(x)[y].z();\n"
+ " LOG(INFO) << x;\n"
+ " ifstream(x) >> x;\n"
+ "}\n",
+ format("int q() {\n"
+ " f(x)\n;\n"
+ " f(x)\n {}\n"
+ " f(x)\n->g();\n"
+ " f(x)\n->*g();\n"
+ " f(x)\n.g();\n"
+ " f(x)\n = x;\n"
+ " f(x)\n += x;\n"
+ " f(x)\n -= x;\n"
+ " f(x)\n *= x;\n"
+ " f(x)\n /= x;\n"
+ " f(x)\n %= x;\n"
+ " f(x)\n &= x;\n"
+ " f(x)\n |= x;\n"
+ " f(x)\n ^= x;\n"
+ " f(x)\n >>= x;\n"
+ " f(x)\n <<= x;\n"
+ " f(x)\n[y].z();\n"
+ " LOG(INFO)\n << x;\n"
+ " ifstream(x)\n >> x;\n"
+ "}\n"));
+ EXPECT_EQ("int q() {\n"
+ " f(x)\n"
+ " if (1) {\n"
+ " }\n"
+ " f(x)\n"
+ " while (1) {\n"
+ " }\n"
+ " f(x)\n"
+ " g(x);\n"
+ " f(x)\n"
+ " try {\n"
+ " q();\n"
+ " }\n"
+ " catch (...) {\n"
+ " }\n"
+ "}\n",
+ format("int q() {\n"
+ "f(x)\n"
+ "if (1) {}\n"
+ "f(x)\n"
+ "while (1) {}\n"
+ "f(x)\n"
+ "g(x);\n"
+ "f(x)\n"
+ "try { q(); } catch (...) {}\n"
+ "}\n"));
+ EXPECT_EQ("class A {\n"
+ " A() : t(0) {}\n"
+ " A(X x)\n" // FIXME: function-level try blocks are broken.
+ " try : t(0) {\n"
+ " }\n"
+ " catch (...) {\n"
+ " }\n"
+ "};",
+ format("class A {\n"
+ " A()\n : t(0) {}\n"
+ " A(X x)\n"
+ " try : t(0) {} catch (...) {}\n"
+ "};"));
+}
+
TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
}
@@ -1155,6 +1606,30 @@ TEST_F(FormatTest, PreventConfusingIndents) {
" ddd);");
}
+TEST_F(FormatTest, ExpressionIndentation) {
+ verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
+ " ccccccccccccccccccccccccccccccccccccccccc;");
+ verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
+ verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
+ verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
+}
+
TEST_F(FormatTest, ConstructorInitializers) {
verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
@@ -1224,8 +1699,34 @@ TEST_F(FormatTest, ConstructorInitializers) {
" aaaaa(aaaaaa),\n"
" aaaaa(aaaaaa) {}",
OnePerLine);
+ verifyFormat("Constructor()\n"
+ " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaa) {}",
+ OnePerLine);
+}
+
+TEST_F(FormatTest, MemoizationTests) {
+ // This breaks if the memoization lookup does not take \c Indent and
+ // \c LastSpace into account.
+ verifyFormat(
+ "extern CFRunLoopTimerRef\n"
+ "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
+ " CFTimeInterval interval, CFOptionFlags flags,\n"
+ " CFIndex order, CFRunLoopTimerCallBack callout,\n"
+ " CFRunLoopTimerContext *context) {}");
+
+ // Deep nesting somewhat works around our memoization.
+ verifyFormat(
+ "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
+ " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
+ " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
+ " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
+ " aaaaa())))))))))))))))))))))))))))))))))))))));",
+ getLLVMStyleWithColumns(65));
// This test takes VERY long when memoization is broken.
+ FormatStyle OnePerLine = getLLVMStyle();
+ OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
OnePerLine.BinPackParameters = false;
std::string input = "Constructor()\n"
" : aaaa(a,\n";
@@ -1247,6 +1748,63 @@ TEST_F(FormatTest, BreaksAsHighAsPossible) {
" Intervals[i - 1].getRange().getLast()) {\n}");
}
+TEST_F(FormatTest, BreaksFunctionDeclarations) {
+ // Principially, we break function declarations in a certain order:
+ // 1) break amongst arguments.
+ verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
+ " Cccccccccccccc cccccccccccccc);");
+
+ // 2) break after return type.
+ verifyFormat(
+ "Aaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
+
+ // 3) break after (.
+ verifyFormat(
+ "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
+ " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
+
+ // 4) break before after nested name specifiers.
+ verifyFormat(
+ "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " SomeClasssssssssssssssssssssssssssssssssssssss::\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
+
+ // However, there are exceptions, if a sufficient amount of lines can be
+ // saved.
+ // FIXME: The precise cut-offs wrt. the number of saved lines might need some
+ // more adjusting.
+ verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc);");
+ verifyFormat(
+ "Aaaaaaaaaaaaaaaaaa\n"
+ " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
+ verifyFormat(
+ "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc);");
+ verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
+ " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
+ " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
+
+ // Break after multi-line parameters.
+ verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " bbbb bbbb);");
+}
+
TEST_F(FormatTest, BreaksDesireably) {
verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
" aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
@@ -1283,6 +1841,10 @@ TEST_F(FormatTest, BreaksDesireably) {
verifyFormat(
"aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
+ verifyFormat(
+ "aaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
// This test case breaks on an incorrect memoization, i.e. an optimization not
// taking into account the StopAt value.
@@ -1300,7 +1862,7 @@ TEST_F(FormatTest, BreaksDesireably) {
}
TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
- FormatStyle NoBinPacking = getLLVMStyle();
+ FormatStyle NoBinPacking = getGoogleStyle();
NoBinPacking.BinPackParameters = false;
verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaa,\n"
@@ -1363,9 +1925,9 @@ 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"
- " aaaaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
+ " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
verifyFormat(
"aaaaaaa->aaaaaaa\n"
" ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
@@ -1382,15 +1944,20 @@ TEST_F(FormatTest, FormatsBuilderPattern) {
TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
- " GUARDED_BY(aaaaaaaaaaaaa);");
+ " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
- " GUARDED_BY(aaaaaaaaaaaaa);");
+ " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
- " GUARDED_BY(aaaaaaaaaaaaa) {}");
+ " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
verifyFormat(
"void aaaaaaaaaaaaaaaaaa()\n"
" __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaa));");
+ verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " __attribute__((unused));");
+ verifyFormat(
+ "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " GUARDED_BY(aaaaaaaaaaaa);");
}
TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
@@ -1452,6 +2019,17 @@ TEST_F(FormatTest, AlignsAfterReturn) {
verifyFormat(
"return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
" aaaaaaaaaaaaaaaaaaaaaaaaa);");
+ verifyFormat(
+ "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
+ " aaaaaaaaaaaaaaaaaaaaaa();");
+ verifyFormat(
+ "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
+ " aaaaaaaaaaaaaaaaaaaaaa());");
+ verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+ verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
}
TEST_F(FormatTest, BreaksConditionalExpressions) {
@@ -1467,7 +2045,7 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
" : aaaaaaaaaaaaa);");
verifyFormat(
"aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaa\n"
+ " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaa);");
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
@@ -1486,12 +2064,16 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
+ verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " : aaaaaaaaaaaaaaaa;");
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" ? aaaaaaaaaaaaaaa\n"
" : aaaaaaaaaaaaaaa;");
verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
- " aaaaaaaaa\n"
+ " aaaaaaaaa\n"
" ? b\n"
" : c);");
verifyFormat(
@@ -1520,16 +2102,30 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
" aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
verifyFormat("bool a = true, b = false;");
- // FIXME: Indentation looks weird.
verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
" bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
verifyFormat(
"bool aaaaaaaaaaaaaaaaaaaaa =\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
" d = e && f;");
-
+ verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
+ " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
+ verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
+ " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
+ verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
+ " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
+ // FIXME: If multiple variables are defined, the "*" needs to move to the new
+ // line. Also fix indent for breaking after the type, this looks bad.
+ verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
+ " *b = bbbbbbbbbbbbbbbbbbb;");
+
+ // Not ideal, but pointer-with-type does not allow much here.
+ verifyGoogleFormat(
+ "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
+ " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
}
TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
@@ -1605,6 +2201,12 @@ TEST_F(FormatTest, AlignsPipes) {
" << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
" << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
" << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
+ verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
+ " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
+
+ verifyFormat(
+ "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
}
TEST_F(FormatTest, UnderstandsEquals) {
@@ -1652,6 +2254,17 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
" .WillRepeatedly(Return(SomeValue));");
verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
" .insert(ccccccccccccccccccccccc);");
+ verifyFormat(
+ "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
+ " .aaaaaaaaaaaaaaa(\n"
+ " aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
+ verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
+ "}");
// Here, it is not necessary to wrap at "." or "->".
verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
@@ -1664,6 +2277,14 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
+ verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
+ " aaaaaaaaa()->aaaaaa()->aaaaa());");
+ verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
+ " aaaaaaaaa()->aaaaaa()->aaaaa());");
+
+ // FIXME: Should we break before .a()?
+ verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
FormatStyle NoBinPacking = getLLVMStyle();
NoBinPacking.BinPackParameters = false;
@@ -1695,7 +2316,7 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) {
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat("template <typename T>\n"
"void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " int aaaaaaaaaaaaaaaaa);");
+ " int aaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat(
"template <typename T1, typename T2 = char, typename T3 = char,\n"
" typename T4 = char>\n"
@@ -1739,6 +2360,10 @@ TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaa);",
getLLVMStyleWithColumns(74));
+
+ verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
}
TEST_F(FormatTest, UnderstandsTemplateParameters) {
@@ -1770,7 +2395,18 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
TEST_F(FormatTest, UnderstandsBinaryOperators) {
verifyFormat("COMPARE(a, ==, b);");
- verifyFormat("(a->*f)()");
+}
+
+TEST_F(FormatTest, UnderstandsPointersToMembers) {
+ verifyFormat("int A::*x;");
+ // FIXME: Recognize pointers to member functions.
+ //verifyFormat("int (S::*func)(void *);");
+ verifyFormat("int(S::*func)(void *);");
+ verifyFormat("(a->*f)();");
+ verifyFormat("a->*x;");
+ verifyFormat("(a.*f)();");
+ verifyFormat("((*a).*f)();");
+ verifyFormat("a.*x;");
}
TEST_F(FormatTest, UnderstandsUnaryOperators) {
@@ -1800,6 +2436,8 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) {
"case -1:\n"
" break;\n"
"}");
+ verifyFormat("#define X -1");
+ verifyFormat("#define X -kConstant");
verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
@@ -1834,6 +2472,11 @@ TEST_F(FormatTest, UndestandsOverloadedOperators) {
verifyFormat(
"ostream &operator<<(ostream &OutputStream,\n"
" SomeReallyLongType WithSomeReallyLongValue);");
+ verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
+ " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
+ " return left.group < right.group;\n"
+ "}");
+ verifyFormat("SomeType &operator=(const SomeType &S);");
verifyGoogleFormat("operator void*();");
verifyGoogleFormat("operator SomeType<SomeType<int>>();");
@@ -1880,7 +2523,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyIndependentOfContext("a[a * a] = 1;");
verifyIndependentOfContext("f() * b;");
verifyIndependentOfContext("a * [self dostuff];");
- verifyIndependentOfContext("a * (a + b);");
+ verifyIndependentOfContext("int x = a * (a + b);");
verifyIndependentOfContext("(a *)(a + b);");
verifyIndependentOfContext("int *pa = (int *)&a;");
verifyIndependentOfContext("return sizeof(int **);");
@@ -1902,6 +2545,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
"const char *const p = reinterpret_cast<const char *const>(q);");
verifyIndependentOfContext("A<int **, int **> a;");
verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
+ verifyFormat("for (char **a = b; *a; ++a) {\n}");
+ verifyFormat("for (; a && b;) {\n}");
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -1949,8 +2594,15 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
+ verifyIndependentOfContext("A = new SomeType *[Length];");
verifyIndependentOfContext("A = new SomeType *[Length]();");
verifyGoogleFormat("A = new SomeType* [Length]();");
+ verifyGoogleFormat("A = new SomeType* [Length];");
+}
+
+TEST_F(FormatTest, UnderstandsEllipsis) {
+ verifyFormat("int printf(const char *fmt, ...);");
+ verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
}
TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
@@ -1995,7 +2647,7 @@ TEST_F(FormatTest, UnderstandsRvalueReferences) {
TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
verifyFormat("void f() {\n"
" x[aaaaaaaaa -\n"
- " b] = 23;\n"
+ " b] = 23;\n"
"}",
getLLVMStyleWithColumns(15));
}
@@ -2033,25 +2685,45 @@ TEST_F(FormatTest, FormatsCasts) {
verifyFormat("virtual void foo(int *) override;");
verifyFormat("virtual void foo(char &) const;");
verifyFormat("virtual void foo(int *a, char *) const;");
+ verifyFormat("int a = sizeof(int *) + b;");
+ verifyFormat("int a = alignof(int *) + b;");
}
TEST_F(FormatTest, FormatsFunctionTypes) {
verifyFormat("A<bool()> a;");
verifyFormat("A<SomeType()> a;");
verifyFormat("A<void(*)(int, std::string)> a;");
+ verifyFormat("A<void *(int)>;");
+ verifyFormat("void *(*a)(int *, SomeType *);");
// FIXME: Inconsistent.
verifyFormat("int (*func)(void *);");
verifyFormat("void f() { int(*func)(void *); }");
+
+ verifyGoogleFormat("A<void*(int*, SomeType*)>;");
+ verifyGoogleFormat("void* (*a)(int);");
}
TEST_F(FormatTest, BreaksLongDeclarations) {
+ verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
+ " AnotherNameForTheLongType;");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
+ " LoooooooooooooooooooooooooooooooooooooooongVariable;");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
+ " LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
+ "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
+
+ // FIXME: Without the comment, this breaks after "(".
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
+ " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
+
verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
" int LoooooooooooooooooooongParam2) {}");
verifyFormat(
- "TypeSpecDecl *\n"
- "TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n"
- " IdentifierIn *II, Type *T) {}");
+ "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
+ " SourceLocation L, IdentifierIn *II,\n"
+ " Type *T) {}");
verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
"ReallyReallyLongFunctionName(\n"
" const std::string &SomeParameter,\n"
@@ -2061,7 +2733,7 @@ TEST_F(FormatTest, BreaksLongDeclarations) {
" AnotherLongParameterName) {}");
verifyFormat(
"aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
- "aaaaaaaaaaaaaaaaaaaaaaa;");
+ " aaaaaaaaaaaaaaaaaaaaaaa;");
verifyGoogleFormat(
"TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
@@ -2091,6 +2763,7 @@ TEST_F(FormatTest, HandlesIncludeDirectives) {
"#include \"string.h\"\n"
"#include <a-a>\n"
"#include < path with space >\n"
+ "#include \"abc.h\" // this is included for ABC\n"
"#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
getLLVMStyleWithColumns(35));
@@ -2150,7 +2823,7 @@ TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
verifyFormat("int aaaaaaaa =\n"
- " // Overly long comment\n"
+ " // Overlylongcomment\n"
" b;",
getLLVMStyleWithColumns(20));
verifyFormat("function(\n"
@@ -2175,7 +2848,10 @@ TEST_F(FormatTest, IncorrectAccessSpecifier) {
"B { int x; }");
}
-TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { verifyFormat("{"); }
+TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
+ verifyFormat("{");
+ verifyFormat("#})");
+}
TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
verifyFormat("do {\n}");
@@ -2306,6 +2982,9 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
" class X x;\n"
"else\n"
" f();\n");
+
+ // This is simply incomplete. Formatting is not important, but must not crash.
+ verifyFormat("class A:");
}
TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
@@ -2315,6 +2994,17 @@ TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
EXPECT_EQ("#warning 1", format(" # warning 1"));
}
+TEST_F(FormatTest, FormatHashIfExpressions) {
+ // FIXME: Come up with a better indentation for #elif.
+ verifyFormat(
+ "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
+ " defined(BBBBBBBB)\n"
+ "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
+ " defined(BBBBBBBB)\n"
+ "#endif",
+ getLLVMStyleWithColumns(65));
+}
+
TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
FormatStyle AllowsMergedIf = getGoogleStyle();
AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
@@ -2325,16 +3015,16 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
format("if (true)\nreturn 42;", AllowsMergedIf));
FormatStyle ShortMergedIf = AllowsMergedIf;
ShortMergedIf.ColumnLimit = 25;
- verifyFormat("#define A \\\n"
+ verifyFormat("#define A \\\n"
" if (true) return 42;",
ShortMergedIf);
- verifyFormat("#define A \\\n"
- " f(); \\\n"
+ verifyFormat("#define A \\\n"
+ " f(); \\\n"
" if (true)\n"
"#define B",
ShortMergedIf);
- verifyFormat("#define A \\\n"
- " f(); \\\n"
+ verifyFormat("#define A \\\n"
+ " f(); \\\n"
" if (true)\n"
"g();",
ShortMergedIf);
@@ -2372,12 +3062,12 @@ TEST_F(FormatTest, BlockComments) {
EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
format("/* *//* */ /* */\n/* *//* */ /* */"));
EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
- EXPECT_EQ("#define A /* */\\\n"
+ EXPECT_EQ("#define A /*123*/\\\n"
" b\n"
"/* */\n"
"someCall(\n"
" parameter);",
- format("#define A /* */ b\n"
+ format("#define A /*123*/ b\n"
"/* */\n"
"someCall(parameter);",
getLLVMStyleWithColumns(15)));
@@ -2517,6 +3207,7 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
// If there's no return type (very rare in practice!), LLVM and Google style
// agree.
+ verifyFormat("- foo;");
verifyFormat("- foo:(int)f;");
verifyGoogleFormat("- foo:(int)foo;");
}
@@ -2955,8 +3646,18 @@ TEST_F(FormatTest, ObjCLiterals) {
verifyFormat("@{ @\"one\" : @1 }");
verifyFormat("return @{ @\"one\" : @1 };");
verifyFormat("@{ @\"one\" : @1, }");
- verifyFormat("@{ @\"one\" : @{ @2 : @1 } }");
- verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }");
+
+ // FIXME: Breaking in cases where we think there's a structural error
+ // showed that we're incorrectly parsing this code. We need to fix the
+ // parsing here.
+ verifyFormat("@{ @\"one\" : @\n"
+ "{ @2 : @1 }\n"
+ "}");
+ verifyFormat("@{ @\"one\" : @\n"
+ "{ @2 : @1 }\n"
+ ",\n"
+ "}");
+
verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
verifyFormat("[self setDict:@{}");
verifyFormat("[self setDict:@{ @1 : @2 }");
@@ -3041,12 +3742,22 @@ TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
format("void f() {}\n"
"void g() {}",
13, 0, getLLVMStyle()));
+ EXPECT_EQ("int a; // comment\n"
+ " // line 2\n"
+ "int b;",
+ format("int a; // comment\n"
+ " // line 2\n"
+ " int b;",
+ 35, 0, getLLVMStyle()));
}
TEST_F(FormatTest, BreakStringLiterals) {
EXPECT_EQ("\"some text \"\n"
"\"other\";",
format("\"some text other\";", getLLVMStyleWithColumns(12)));
+ EXPECT_EQ("\"some text \"\n"
+ "\"other\";",
+ format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
EXPECT_EQ(
"#define A \\\n"
" \"some \" \\\n"
@@ -3071,7 +3782,8 @@ TEST_F(FormatTest, BreakStringLiterals) {
"\"text\"",
format("\"some text\"", getLLVMStyleWithColumns(7)));
EXPECT_EQ("\"some\"\n"
- "\" text\"",
+ "\" tex\"\n"
+ "\"t\"",
format("\"some text\"", getLLVMStyleWithColumns(6)));
EXPECT_EQ("\"some\"\n"
"\" tex\"\n"
@@ -3129,6 +3841,15 @@ TEST_F(FormatTest, BreakStringLiterals) {
"\"pathat/\"\n"
"\"slashes\"",
format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
+
+ FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
+ AlignLeft.AlignEscapedNewlinesLeft = true;
+ EXPECT_EQ(
+ "#define A \\\n"
+ " \"some \" \\\n"
+ " \"text \" \\\n"
+ " \"other\";",
+ format("#define A \"some text other\";", AlignLeft));
}
TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
@@ -3164,11 +3885,26 @@ TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
"\"000001\"",
format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
EXPECT_EQ("\"test\\000\"\n"
- "\"000000001\"",
+ "\"00000000\"\n"
+ "\"1\"",
format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
EXPECT_EQ("R\"(\\x\\x00)\"\n",
format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7)));
}
+TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
+ verifyFormat("void f() {\n"
+ " return g() {}\n"
+ " void h() {}");
+ verifyFormat("if (foo)\n"
+ " return { forgot_closing_brace();\n"
+ "test();");
+ verifyFormat("int a[] = { void forgot_closing_brace()\n"
+ "{\n"
+ " f();\n"
+ " g();\n"
+ "}");
+}
+
} // end namespace tooling
} // end namespace clang