//===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "FormatTestUtils.h" #include "clang/Format/Format.h" #include "llvm/Support/Debug.h" #include "gtest/gtest.h" #define DEBUG_TYPE "format-test" namespace clang { namespace format { namespace { FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } class FormatTest : public ::testing::Test { protected: enum IncompleteCheck { IC_ExpectComplete, IC_ExpectIncomplete, IC_DoNotCheck }; std::string format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle(), IncompleteCheck CheckIncomplete = IC_ExpectComplete) { DEBUG(llvm::errs() << "---\n"); DEBUG(llvm::errs() << Code << "\n\n"); std::vector Ranges(1, tooling::Range(0, Code.size())); bool IncompleteFormat = false; tooling::Replacements Replaces = reformat(Style, Code, Ranges, "", &IncompleteFormat); if (CheckIncomplete != IC_DoNotCheck) { bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete; EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n"; } ReplacementCount = Replaces.size(); std::string Result = applyAllReplacements(Code, Replaces); EXPECT_NE("", Result); DEBUG(llvm::errs() << "\n" << Result << "\n\n"); return Result; } FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { FormatStyle Style = getLLVMStyle(); Style.ColumnLimit = ColumnLimit; return Style; } FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { FormatStyle Style = getGoogleStyle(); Style.ColumnLimit = ColumnLimit; return Style; } void verifyFormat(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); } void verifyIncompleteFormat(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { EXPECT_EQ(Code.str(), format(test::messUp(Code), Style, IC_ExpectIncomplete)); } void verifyGoogleFormat(llvm::StringRef Code) { verifyFormat(Code, getGoogleStyle()); } void verifyIndependentOfContext(llvm::StringRef text) { verifyFormat(text); verifyFormat(llvm::Twine("void f() { " + text + " }").str()); } /// \brief Verify that clang-format does not crash on the given input. void verifyNoCrash(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { format(Code, Style, IC_DoNotCheck); } int ReplacementCount; }; TEST_F(FormatTest, MessUp) { EXPECT_EQ("1 2 3", test::messUp("1 2 3")); EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); } //===----------------------------------------------------------------------===// // Basic function tests. //===----------------------------------------------------------------------===// TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { EXPECT_EQ(";", format(";")); } TEST_F(FormatTest, FormatsGlobalStatementsAt0) { EXPECT_EQ("int i;", format(" int i;")); EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); } TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { EXPECT_EQ("int i;", format("int\ni;")); } TEST_F(FormatTest, FormatsNestedBlockStatements) { EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); } TEST_F(FormatTest, FormatsNestedCall) { verifyFormat("Method(f1, f2(f3));"); verifyFormat("Method(f1(f2, f3()));"); verifyFormat("Method(f1(f2, (f3())));"); } 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) { EXPECT_EQ("if (a) {\n" " f();\n" "}", format("if(a){f();}")); EXPECT_EQ(4, ReplacementCount); EXPECT_EQ("if (a) {\n" " f();\n" "}", format("if (a) {\n" " f();\n" "}")); EXPECT_EQ(0, ReplacementCount); EXPECT_EQ("/*\r\n" "\r\n" "*/\r\n", format("/*\r\n" "\r\n" "*/\r\n")); EXPECT_EQ(0, ReplacementCount); } TEST_F(FormatTest, RemovesEmptyLines) { EXPECT_EQ("class C {\n" " int i;\n" "};", format("class C {\n" " int i;\n" "\n" "};")); // Don't remove empty lines at the start of namespaces or extern "C" blocks. EXPECT_EQ("namespace N {\n" "\n" "int i;\n" "}", format("namespace N {\n" "\n" "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" "\n" " if (a) {\n" "\n" " f();\n" " }\n" "}", format("void f() {\n" "\n" " if (a) {\n" "\n" " f();\n" "\n" " }\n" "\n" "}", getLLVMStyle())); EXPECT_EQ("void f() {\n" " if (a) {\n" " f();\n" " }\n" "}", format("void f() {\n" "\n" " if (a) {\n" "\n" " f();\n" "\n" " }\n" "\n" "}", getGoogleStyle())); // Don't remove empty lines in more complex control statements. EXPECT_EQ("void f() {\n" " if (a) {\n" " f();\n" "\n" " } else if (b) {\n" " f();\n" " }\n" "}", format("void f() {\n" " if (a) {\n" " f();\n" "\n" " } else if (b) {\n" " f();\n" "\n" " }\n" "\n" "}")); // FIXME: This is slightly inconsistent. EXPECT_EQ("namespace {\n" "int i;\n" "}", format("namespace {\n" "int i;\n" "\n" "}")); EXPECT_EQ("namespace {\n" "int i;\n" "\n" "} // namespace", format("namespace {\n" "int i;\n" "\n" "} // namespace")); } TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { verifyFormat("x = (a) and (b);"); verifyFormat("x = (a) or (b);"); verifyFormat("x = (a) bitand (b);"); verifyFormat("x = (a) bitor (b);"); verifyFormat("x = (a) not_eq (b);"); verifyFormat("x = (a) and_eq (b);"); verifyFormat("x = (a) or_eq (b);"); verifyFormat("x = (a) xor (b);"); } //===----------------------------------------------------------------------===// // Tests for control statements. //===----------------------------------------------------------------------===// 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();"); FormatStyle AllowsMergedIf = getLLVMStyle(); AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; verifyFormat("if (a)\n" " // comment\n" " f();", AllowsMergedIf); verifyFormat("if (a)\n" " ;", AllowsMergedIf); verifyFormat("if (a)\n" " if (b) return;", AllowsMergedIf); verifyFormat("if (a) // Can't merge this\n" " f();\n", AllowsMergedIf); verifyFormat("if (a) /* still don't merge */\n" " f();", AllowsMergedIf); verifyFormat("if (a) { // Never merge this\n" " f();\n" "}", AllowsMergedIf); verifyFormat("if (a) { /* Never merge this */\n" " f();\n" "}", AllowsMergedIf); AllowsMergedIf.ColumnLimit = 14; verifyFormat("if (a) return;", AllowsMergedIf); verifyFormat("if (aaaaaaaaa)\n" " return;", AllowsMergedIf); AllowsMergedIf.ColumnLimit = 13; verifyFormat("if (a)\n return;", AllowsMergedIf); } TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { FormatStyle AllowsMergedLoops = getLLVMStyle(); AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; verifyFormat("while (true) continue;", AllowsMergedLoops); verifyFormat("for (;;) continue;", AllowsMergedLoops); verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); verifyFormat("while (true)\n" " ;", AllowsMergedLoops); verifyFormat("for (;;)\n" " ;", AllowsMergedLoops); verifyFormat("for (;;)\n" " for (;;) continue;", AllowsMergedLoops); verifyFormat("for (;;) // Can't merge this\n" " continue;", AllowsMergedLoops); verifyFormat("for (;;) /* still don't merge */\n" " continue;", AllowsMergedLoops); } TEST_F(FormatTest, FormatShortBracedStatements) { FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; verifyFormat("if (true) {}", AllowSimpleBracedStatements); verifyFormat("while (true) {}", AllowSimpleBracedStatements); verifyFormat("for (;;) {}", AllowSimpleBracedStatements); verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); verifyFormat("if (true) { //\n" " f();\n" "}", AllowSimpleBracedStatements); verifyFormat("if (true) {\n" " f();\n" " f();\n" "}", AllowSimpleBracedStatements); verifyFormat("if (true) {\n" " f();\n" "} else {\n" " f();\n" "}", AllowSimpleBracedStatements); verifyFormat("template struct A2 {\n" " struct B {};\n" "};", AllowSimpleBracedStatements); AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; verifyFormat("if (true) {\n" " f();\n" "}", AllowSimpleBracedStatements); verifyFormat("if (true) {\n" " f();\n" "} else {\n" " f();\n" "}", AllowSimpleBracedStatements); AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; verifyFormat("while (true) {\n" " f();\n" "}", AllowSimpleBracedStatements); verifyFormat("for (;;) {\n" " f();\n" "}", AllowSimpleBracedStatements); } TEST_F(FormatTest, ParseIfElse) { verifyFormat("if (true)\n" " if (true)\n" " if (true)\n" " f();\n" " else\n" " g();\n" " else\n" " h();\n" "else\n" " i();"); verifyFormat("if (true)\n" " if (true)\n" " if (true) {\n" " if (true)\n" " f();\n" " } else {\n" " g();\n" " }\n" " else\n" " h();\n" "else {\n" " i();\n" "}"); verifyFormat("void f() {\n" " if (a) {\n" " } else {\n" " }\n" "}"); } TEST_F(FormatTest, ElseIf) { verifyFormat("if (a) {\n} else if (b) {\n}"); verifyFormat("if (a)\n" " f();\n" "else if (b)\n" " g();\n" "else\n" " h();"); verifyFormat("if (a) {\n" " f();\n" "}\n" "// or else ..\n" "else {\n" " g()\n" "}"); verifyFormat("if (a) {\n" "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" "}"); verifyFormat("if (a) {\n" "} else if (\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" "}", getLLVMStyleWithColumns(62)); } TEST_F(FormatTest, FormatsForLoop) { verifyFormat( "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" " ++VeryVeryLongLoopVariable)\n" " ;"); verifyFormat("for (;;)\n" " f();"); verifyFormat("for (;;) {\n}"); verifyFormat("for (;;) {\n" " f();\n" "}"); verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); verifyFormat( "for (std::vector::iterator I = UnwrappedLines.begin(),\n" " E = UnwrappedLines.end();\n" " I != E; ++I) {\n}"); verifyFormat( "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" " ++IIIII) {\n}"); verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); verifyFormat("for (llvm::ArrayRef::iterator\n" " I = FD->getDeclsInPrototypeScope().begin(),\n" " E = FD->getDeclsInPrototypeScope().end();\n" " I != E; ++I) {\n}"); verifyFormat("for (SmallVectorImpl::iterator\n" " I = Container.begin(),\n" " E = Container.end();\n" " I != E; ++I) {\n}", getLLVMStyleWithColumns(76)); verifyFormat( "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" " ++aaaaaaaaaaa) {\n}"); verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" " ++i) {\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" "}"); verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackParameters = false; verifyFormat("for (int aaaaaaaaaaa = 1;\n" " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaa);\n" " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" "}", NoBinPacking); verifyFormat( "for (std::vector::iterator I = UnwrappedLines.begin(),\n" " E = UnwrappedLines.end();\n" " I != E;\n" " ++I) {\n}", NoBinPacking); } TEST_F(FormatTest, RangeBasedForLoops) { verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); } TEST_F(FormatTest, ForEachLoops) { verifyFormat("void f() {\n" " foreach (Item *item, itemlist) {}\n" " Q_FOREACH (Item *item, itemlist) {}\n" " BOOST_FOREACH (Item *item, itemlist) {}\n" " UNKNOWN_FORACH(Item * item, itemlist) {}\n" "}"); // As function-like macros. verifyFormat("#define foreach(x, y)\n" "#define Q_FOREACH(x, y)\n" "#define BOOST_FOREACH(x, y)\n" "#define UNKNOWN_FOREACH(x, y)\n"); // Not as function-like macros. verifyFormat("#define foreach (x, y)\n" "#define Q_FOREACH (x, y)\n" "#define BOOST_FOREACH (x, y)\n" "#define UNKNOWN_FOREACH (x, y)\n"); } TEST_F(FormatTest, FormatsWhileLoop) { verifyFormat("while (true) {\n}"); verifyFormat("while (true)\n" " f();"); verifyFormat("while () {\n}"); verifyFormat("while () {\n" " f();\n" "}"); } TEST_F(FormatTest, FormatsDoWhile) { verifyFormat("do {\n" " do_something();\n" "} while (something());"); verifyFormat("do\n" " do_something();\n" "while (something());"); } TEST_F(FormatTest, FormatsSwitchStatement) { verifyFormat("switch (x) {\n" "case 1:\n" " f();\n" " break;\n" "case kFoo:\n" "case ns::kBar:\n" "case kBaz:\n" " break;\n" "default:\n" " g();\n" " break;\n" "}"); verifyFormat("switch (x) {\n" "case 1: {\n" " f();\n" " break;\n" "}\n" "case 2: {\n" " break;\n" "}\n" "}"); verifyFormat("switch (x) {\n" "case 1: {\n" " f();\n" " {\n" " g();\n" " h();\n" " }\n" " break;\n" "}\n" "}"); verifyFormat("switch (x) {\n" "case 1: {\n" " f();\n" " if (foo) {\n" " g();\n" " h();\n" " }\n" " break;\n" "}\n" "}"); verifyFormat("switch (x) {\n" "case 1: {\n" " f();\n" " g();\n" "} break;\n" "}"); verifyFormat("switch (test)\n" " ;"); 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)); verifyFormat("#define OPERATION_CASE(name) \\\n" " case OP_name: \\\n" " return operations::Operation##name\n", getLLVMStyleWithColumns(40)); verifyFormat("switch (x) {\n" "case 1:;\n" "default:;\n" " int i;\n" "}"); verifyGoogleFormat("switch (x) {\n" " case 1:\n" " f();\n" " break;\n" " case kFoo:\n" " case ns::kBar:\n" " case kBaz:\n" " break;\n" " default:\n" " g();\n" " break;\n" "}"); verifyGoogleFormat("switch (x) {\n" " case 1: {\n" " f();\n" " break;\n" " }\n" "}"); verifyGoogleFormat("switch (test)\n" " ;"); verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" " case OP_name: \\\n" " return operations::Operation##name\n"); verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" " // Get the correction operation class.\n" " switch (OpCode) {\n" " CASE(Add);\n" " CASE(Subtract);\n" " default:\n" " return operations::Unknown;\n" " }\n" "#undef OPERATION_CASE\n" "}"); verifyFormat("DEBUG({\n" " switch (x) {\n" " case A:\n" " f();\n" " break;\n" " // On B:\n" " case B:\n" " g();\n" " break;\n" " }\n" "});"); verifyFormat("switch (a) {\n" "case (b):\n" " return;\n" "}"); verifyFormat("switch (a) {\n" "case some_namespace::\n" " some_constant:\n" " return;\n" "}", getLLVMStyleWithColumns(34)); } TEST_F(FormatTest, CaseRanges) { verifyFormat("switch (x) {\n" "case 'A' ... 'Z':\n" "case 1 ... 5:\n" " break;\n" "}"); } 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" "case 8:\n" " x = 8; // comment\n" " break;\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" "test_label:\n" " some_other_code();\n" " {\n" " some_more_code();\n" " another_label:\n" " some_more_code();\n" " }\n" "}"); verifyFormat("{\n" " some_code();\n" "test_label:\n" " some_other_code();\n" "}"); verifyFormat("{\n" " some_code();\n" "test_label:;\n" " int i = 0;\n" "}"); } //===----------------------------------------------------------------------===// // Tests for comments. //===----------------------------------------------------------------------===// TEST_F(FormatTest, UnderstandsSingleLineComments) { verifyFormat("//* */"); verifyFormat("// line 1\n" "// line 2\n" "void f() {}\n"); verifyFormat("void f() {\n" " // Doesn't do anything\n" "}"); verifyFormat("SomeObject\n" " // Calling someFunction on SomeObject\n" " .someFunction();"); verifyFormat("auto result = SomeObject\n" " // Calling someFunction on SomeObject\n" " .someFunction();"); verifyFormat("void f(int i, // some comment (probably for i)\n" " int j, // some comment (probably for j)\n" " int k); // some comment (probably for k)"); verifyFormat("void f(int i,\n" " // some comment (probably for j)\n" " int j,\n" " // some comment (probably for k)\n" " int k);"); verifyFormat("int i // This is a fancy variable\n" " = 5; // with nicely aligned comment."); verifyFormat("// Leading comment.\n" "int a; // Trailing comment."); verifyFormat("int a; // Trailing comment\n" " // on 2\n" " // or 3 lines.\n" "int b;"); verifyFormat("int a; // Trailing comment\n" "\n" "// Leading comment.\n" "int b;"); verifyFormat("int a; // Comment.\n" " // More details.\n" "int bbbb; // Another comment."); verifyFormat( "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" "int cccccccccccccccccccccccccccccc; // comment\n" "int ddd; // looooooooooooooooooooooooong comment\n" "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" "int ccccccccccccccccccc; // comment"); verifyFormat("#include \"a\" // comment\n" "#include \"a/b/c\" // comment"); verifyFormat("#include // comment\n" "#include // comment"); EXPECT_EQ("#include \"a\" // comment\n" "#include \"a/b/c\" // comment", format("#include \\\n" " \"a\" // comment\n" "#include \"a/b/c\" // comment")); verifyFormat("enum E {\n" " // comment\n" " VAL_A, // comment\n" " VAL_B\n" "};"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 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" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat("int aaaa; // aaaaa\n" "int aa; // aaaaaaa", getLLVMStyleWithColumns(20)); EXPECT_EQ("void f() { // This does something ..\n" "}\n" "int a; // This is unrelated", format("void f() { // This does something ..\n" " }\n" "int a; // This is unrelated")); EXPECT_EQ("class C {\n" " void f() { // This does something ..\n" " } // awesome..\n" "\n" " int a; // This is unrelated\n" "};", format("class C{void f() { // This does something ..\n" " } // awesome..\n" " \n" "int a; // This is unrelated\n" "};")); EXPECT_EQ("int i; // single line trailing comment", format("int i;\\\n// single line trailing comment")); verifyGoogleFormat("int a; // Trailing comment."); verifyFormat("someFunction(anotherFunction( // Force break.\n" " parameter));"); verifyGoogleFormat("#endif // HEADER_GUARD"); verifyFormat("const char *test[] = {\n" " // A\n" " \"aaaa\",\n" " // B\n" " \"aaaaa\"};"); 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" "});")); EXPECT_EQ("lineWith(); // comment\n" "// at start\n" "otherLine();", format("lineWith(); // comment\n" "// at start\n" "otherLine();")); EXPECT_EQ("lineWith(); // comment\n" " // at start\n" "otherLine();", format("lineWith(); // comment\n" " // at start\n" "otherLine();")); EXPECT_EQ("lineWith(); // comment\n" "// at start\n" "otherLine(); // comment", format("lineWith(); // comment\n" "// at start\n" "otherLine(); // comment")); EXPECT_EQ("lineWith();\n" "// at start\n" "otherLine(); // comment", format("lineWith();\n" " // at start\n" "otherLine(); // comment")); EXPECT_EQ("// first\n" "// at start\n" "otherLine(); // comment", format("// first\n" " // at start\n" "otherLine(); // comment")); EXPECT_EQ("f();\n" "// first\n" "// at start\n" "otherLine(); // comment", format("f();\n" "// first\n" " // at start\n" "otherLine(); // comment")); verifyFormat("f(); // comment\n" "// first\n" "// at start\n" "otherLine();"); EXPECT_EQ("f(); // comment\n" "// first\n" "// at start\n" "otherLine();", format("f(); // comment\n" "// first\n" " // at start\n" "otherLine();")); EXPECT_EQ("f(); // comment\n" " // first\n" "// at start\n" "otherLine();", format("f(); // comment\n" " // 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" " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", getLLVMStyleWithColumns(60)); verifyFormat( "#define A \\\n" " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", getLLVMStyleWithColumns(61)); verifyFormat("if ( // This is some comment\n" " x + 3) {\n" "}"); EXPECT_EQ("if ( // This is some comment\n" " // spanning two lines\n" " x + 3) {\n" "}", format("if( // This is some comment\n" " // spanning two lines\n" " x + 3) {\n" "}")); verifyNoCrash("/\\\n/"); verifyNoCrash("/\\\n* */"); // The 0-character somehow makes the lexer return a proper comment. verifyNoCrash(StringRef("/*\\\0\n/", 6)); } TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { EXPECT_EQ("SomeFunction(a,\n" " b, // comment\n" " c);", format("SomeFunction(a,\n" " b, // comment\n" " c);")); EXPECT_EQ("SomeFunction(a, b,\n" " // comment\n" " c);", format("SomeFunction(a,\n" " b,\n" " // comment\n" " c);")); EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" " c);", format("SomeFunction(a, b, // comment (unclear relation)\n" " c);")); EXPECT_EQ("SomeFunction(a, // comment\n" " b,\n" " c); // comment", format("SomeFunction(a, // comment\n" " b,\n" " c); // comment")); } TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { EXPECT_EQ("// comment", format("// comment ")); EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", format("int aaaaaaa, bbbbbbb; // comment ", getLLVMStyleWithColumns(33))); EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); } TEST_F(FormatTest, UnderstandsBlockComments) { verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }"); EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" " bbbbbbbbbbbbbbbbbbbbbbbbb);", format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" "/* Trailing comment for aa... */\n" " bbbbbbbbbbbbbbbbbbbbbbbbb);")); EXPECT_EQ( "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); EXPECT_EQ( "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" "}", format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaa ,\n" " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" "}")); FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackParameters = false; verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" " /* parameter 2 */ aaaaaa,\n" " /* parameter 3 */ aaaaaa,\n" " /* parameter 4 */ aaaaaa);", NoBinPacking); // Aligning block comments in macros. verifyGoogleFormat("#define A \\\n" " int i; /*a*/ \\\n" " int jjj; /*b*/"); } TEST_F(FormatTest, AlignsBlockComments) { 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 indentation.\n" "*/", format(" /*\n" " Don't try to outdent if there's not enough indentation.\n" " */")); EXPECT_EQ("int i; /* Comment with empty...\n" " *\n" " * line. */", format("int i; /* Comment with empty...\n" " *\n" " * line. */")); EXPECT_EQ("int foobar = 0; /* comment */\n" "int bar = 0; /* multiline\n" " comment 1 */\n" "int baz = 0; /* multiline\n" " comment 2 */\n" "int bzz = 0; /* multiline\n" " comment 3 */", format("int foobar = 0; /* comment */\n" "int bar = 0; /* multiline\n" " comment 1 */\n" "int baz = 0; /* multiline\n" " comment 2 */\n" "int bzz = 0; /* multiline\n" " comment 3 */")); EXPECT_EQ("int foobar = 0; /* comment */\n" "int bar = 0; /* multiline\n" " comment */\n" "int baz = 0; /* multiline\n" "comment */", format("int foobar = 0; /* comment */\n" "int bar = 0; /* multiline\n" "comment */\n" "int baz = 0; /* multiline\n" "comment */")); } TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) { FormatStyle Style = getLLVMStyleWithColumns(20); Style.ReflowComments = false; verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); } TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); EXPECT_EQ( "void ffffffffffff(\n" " int aaaaaaaa, int bbbbbbbb,\n" " int cccccccccccc) { /*\n" " aaaaaaaaaa\n" " aaaaaaaaaaaaa\n" " bbbbbbbbbbbbbb\n" " bbbbbbbbbb\n" " */\n" "}", format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" "{ /*\n" " aaaaaaaaaa aaaaaaaaaaaaa\n" " bbbbbbbbbbbbbb bbbbbbbbbb\n" " */\n" "}", getLLVMStyleWithColumns(40))); } TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { EXPECT_EQ("void ffffffffff(\n" " int aaaaa /* test */);", format("void ffffffffff(int aaaaa /* test */);", getLLVMStyleWithColumns(35))); } 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 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 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("// Add leading\n" "// whitespace", format("//Add leading whitespace", getLLVMStyleWithColumns(20))); EXPECT_EQ("/// Add leading\n" "/// whitespace", format("///Add leading whitespace", getLLVMStyleWithColumns(20))); EXPECT_EQ("//! Add leading\n" "//! whitespace", format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); EXPECT_EQ("// Even if it makes the line exceed the column\n" "// limit", format("//Even if it makes the line exceed the column limit", getLLVMStyleWithColumns(51))); EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); EXPECT_EQ("// aa bb cc dd", format("// aa bb cc dd ", getLLVMStyleWithColumns(15))); 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))); 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" " int c, int d, int e) {}", getLLVMStyleWithColumns(40))); EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", getLLVMStyleWithColumns(20))); EXPECT_EQ( "#define XXX // a b c d\n" " // e f g h", format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); EXPECT_EQ( "#define XXX // q w e r\n" " // t y u i", format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); } TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { EXPECT_EQ("// A comment\n" "// that doesn't\n" "// fit on one\n" "// line", format("// A comment that doesn't fit on one line", getLLVMStyleWithColumns(20))); EXPECT_EQ("/// A comment\n" "/// that doesn't\n" "/// fit on one\n" "/// line", format("/// A comment that doesn't fit on one line", getLLVMStyleWithColumns(20))); } TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", getLLVMStyleWithColumns(50))); // FIXME: One day we might want to implement adjustment of leading whitespace // of the consecutive lines in this kind of comment: EXPECT_EQ("double\n" " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", getLLVMStyleWithColumns(49))); } TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { FormatStyle Pragmas = getLLVMStyleWithColumns(30); Pragmas.CommentPragmas = "^ IWYU pragma:"; EXPECT_EQ( "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); EXPECT_EQ( "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); } TEST_F(FormatTest, PriorityOfCommentBreaking) { EXPECT_EQ("if (xxx ==\n" " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" " zzz)\n" " q();", format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" " zzz) q();", getLLVMStyleWithColumns(40))); EXPECT_EQ("if (xxxxxxxxxx ==\n" " yyy && // aaaaaa bbbbbbbb cccc\n" " zzz)\n" " q();", format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" " zzz) q();", getLLVMStyleWithColumns(40))); EXPECT_EQ("if (xxxxxxxxxx &&\n" " yyy || // aaaaaa bbbbbbbb cccc\n" " zzz)\n" " q();", format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" " zzz) q();", getLLVMStyleWithColumns(40))); EXPECT_EQ("fffffffff(\n" " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" " zzz);", format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" " zzz);", getLLVMStyleWithColumns(40))); } TEST_F(FormatTest, MultiLineCommentsInDefines) { EXPECT_EQ("#define A(x) /* \\\n" " a comment \\\n" " inside */ \\\n" " f();", format("#define A(x) /* \\\n" " a comment \\\n" " inside */ \\\n" " f();", getLLVMStyleWithColumns(17))); EXPECT_EQ("#define A( \\\n" " x) /* \\\n" " a comment \\\n" " inside */ \\\n" " f();", format("#define A( \\\n" " x) /* \\\n" " a comment \\\n" " inside */ \\\n" " f();", getLLVMStyleWithColumns(17))); } 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\n" " comment */\n" " ;\n" "}", format("{\n" " if (something) /* This is a long comment */\n" " ;\n" "}", getLLVMStyleWithColumns(30))); 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))); EXPECT_EQ("/* some comment\n" " * a comment\n" "* that we break\n" " * another comment\n" "* we have to break\n" "* a left comment\n" " */", format(" /* some comment\n" " * a comment that we break\n" " * another comment we have to break\n" "* a left comment\n" " */", getLLVMStyleWithColumns(20))); EXPECT_EQ("/**\n" " * multiline block\n" " * comment\n" " *\n" " */", format("/**\n" " * multiline block comment\n" " *\n" " */", getLLVMStyleWithColumns(20))); EXPECT_EQ("/*\n" "\n" "\n" " */\n", format(" /* \n" " \n" " \n" " */\n")); EXPECT_EQ("/* a a */", format("/* a a */", getLLVMStyleWithColumns(15))); EXPECT_EQ("/* a a bc */", format("/* a a bc */", getLLVMStyleWithColumns(15))); EXPECT_EQ("/* aaa aaa\n" " * aaaaa */", format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); EXPECT_EQ("/* aaa aaa\n" " * aaaaa */", format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); } 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" " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaa, // comment\n" " aaaaaaaaaaaaaaaaaaaa};", format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" " aaaaaaaaaaaaaaaaaaaa , // comment\n" " aaaaaaaaaaaaaaaaaaaa };")); verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" " bbbbbbbbbbb, ccccccccccc};"); verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" " // comment for bb....\n" " bbbbbbbbbbb, ccccccccccc};"); verifyGoogleFormat( "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" " bbbbbbbbbbb, ccccccccccc};"); verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" " // comment for bb....\n" " bbbbbbbbbbb, ccccccccccc};"); verifyFormat("S s = {{a, b, c}, // Group #1\n" " {d, e, f}, // Group #2\n" " {g, h, i}}; // Group #3"); verifyFormat("S s = {{// Group #1\n" " a, b, c},\n" " {// Group #2\n" " d, e, f},\n" " {// Group #3\n" " g, h, i}};"); EXPECT_EQ("S s = {\n" " // Some comment\n" " a,\n" "\n" " // Comment after empty line\n" " b}", format("S s = {\n" " // Some comment\n" " a,\n" " \n" " // Comment after empty line\n" " b\n" "}")); EXPECT_EQ("S s = {\n" " /* Some comment */\n" " a,\n" "\n" " /* Comment after empty line */\n" " b}", format("S s = {\n" " /* Some comment */\n" " a,\n" " \n" " /* Comment after empty line */\n" " b\n" "}")); verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" " 0x00, 0x00, 0x00, 0x00}; // comment\n"); } TEST_F(FormatTest, IgnoresIf0Contents) { EXPECT_EQ("#if 0\n" "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" "#endif\n" "void f() {}", format("#if 0\n" "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" "#endif\n" "void f( ) { }")); EXPECT_EQ("#if false\n" "void f( ) { }\n" "#endif\n" "void g() {}\n", format("#if false\n" "void f( ) { }\n" "#endif\n" "void g( ) { }\n")); EXPECT_EQ("enum E {\n" " One,\n" " Two,\n" "#if 0\n" "Three,\n" " Four,\n" "#endif\n" " Five\n" "};", format("enum E {\n" " One,Two,\n" "#if 0\n" "Three,\n" " Four,\n" "#endif\n" " Five};")); EXPECT_EQ("enum F {\n" " One,\n" "#if 1\n" " Two,\n" "#if 0\n" "Three,\n" " Four,\n" "#endif\n" " Five\n" "#endif\n" "};", format("enum F {\n" "One,\n" "#if 1\n" "Two,\n" "#if 0\n" "Three,\n" " Four,\n" "#endif\n" "Five\n" "#endif\n" "};")); EXPECT_EQ("enum G {\n" " One,\n" "#if 0\n" "Two,\n" "#else\n" " Three,\n" "#endif\n" " Four\n" "};", format("enum G {\n" "One,\n" "#if 0\n" "Two,\n" "#else\n" "Three,\n" "#endif\n" "Four\n" "};")); EXPECT_EQ("enum H {\n" " One,\n" "#if 0\n" "#ifdef Q\n" "Two,\n" "#else\n" "Three,\n" "#endif\n" "#endif\n" " Four\n" "};", format("enum H {\n" "One,\n" "#if 0\n" "#ifdef Q\n" "Two,\n" "#else\n" "Three,\n" "#endif\n" "#endif\n" "Four\n" "};")); EXPECT_EQ("enum I {\n" " One,\n" "#if /* test */ 0 || 1\n" "Two,\n" "Three,\n" "#endif\n" " Four\n" "};", format("enum I {\n" "One,\n" "#if /* test */ 0 || 1\n" "Two,\n" "Three,\n" "#endif\n" "Four\n" "};")); EXPECT_EQ("enum J {\n" " One,\n" "#if 0\n" "#if 0\n" "Two,\n" "#else\n" "Three,\n" "#endif\n" "Four,\n" "#endif\n" " Five\n" "};", format("enum J {\n" "One,\n" "#if 0\n" "#if 0\n" "Two,\n" "#else\n" "Three,\n" "#endif\n" "Four,\n" "#endif\n" "Five\n" "};")); } //===----------------------------------------------------------------------===// // Tests for classes, namespaces, etc. //===----------------------------------------------------------------------===// TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { verifyFormat("class A {};"); } TEST_F(FormatTest, UnderstandsAccessSpecifiers) { verifyFormat("class A {\n" "public:\n" "public: // comment\n" "protected:\n" "private:\n" " void f() {}\n" "};"); verifyGoogleFormat("class A {\n" " public:\n" " protected:\n" " private:\n" " void f() {}\n" "};"); verifyFormat("class A {\n" "public slots:\n" " void f1() {}\n" "public Q_SLOTS:\n" " void f2() {}\n" "protected slots:\n" " void f3() {}\n" "protected Q_SLOTS:\n" " void f4() {}\n" "private slots:\n" " void f5() {}\n" "private Q_SLOTS:\n" " void f6() {}\n" "signals:\n" " void g1();\n" "Q_SIGNALS:\n" " void g2();\n" "};"); // Don't interpret 'signals' the wrong way. verifyFormat("signals.set();"); verifyFormat("for (Signals signals : f()) {\n}"); verifyFormat("{\n" " signals.set(); // This needs indentation.\n" "}"); } 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" "};")); EXPECT_EQ("class A {\n" "protected:\n" "public:\n" " void f();\n" "};", format("class A {\n" "protected:\n" "\n" "public:\n" "\n" " void f();\n" "};")); // Even ensure proper spacing inside macros. EXPECT_EQ("#define B \\\n" " class A { \\\n" " protected: \\\n" " public: \\\n" " void f(); \\\n" " };", format("#define B \\\n" " class A { \\\n" " protected: \\\n" " \\\n" " public: \\\n" " \\\n" " void f(); \\\n" " };", getGoogleStyle())); // But don't remove empty lines after macros ending in access specifiers. EXPECT_EQ("#define A private:\n" "\n" "int i;", format("#define A private:\n" "\n" "int i;")); } TEST_F(FormatTest, FormatsClasses) { verifyFormat("class A : public B {};"); verifyFormat("class A : public ::B {};"); verifyFormat( "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); verifyFormat( "class A : public B, public C, public D, public E, public F {};"); verifyFormat("class AAAAAAAAAAAA : public B,\n" " public C,\n" " public D,\n" " public E,\n" " public F,\n" " public G {};"); verifyFormat("class\n" " ReallyReallyLongClassName {\n" " int i;\n" "};", getLLVMStyleWithColumns(32)); verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" " aaaaaaaaaaaaaaaa> {};"); verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" " : public aaaaaaaaaaaaaaaaaaa {};"); verifyFormat("template \n" "struct Aaaaaaaaaaaaaaaaa\n" " : Aaaaaaaaaaaaaaaaa {};"); verifyFormat("class ::A::B {};"); } TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { verifyFormat("class A {\n} a, b;"); verifyFormat("struct A {\n} a, b;"); verifyFormat("union A {\n} a;"); } TEST_F(FormatTest, FormatsEnum) { verifyFormat("enum {\n" " Zero,\n" " One = 1,\n" " Two = One + 1,\n" " Three = (One + Two),\n" " Four = (Zero && (One ^ Two)) | (One << Two),\n" " Five = (One, Two, Three, Four, 5)\n" "};"); verifyGoogleFormat("enum {\n" " Zero,\n" " One = 1,\n" " Two = One + 1,\n" " Three = (One + Two),\n" " Four = (Zero && (One ^ Two)) | (One << Two),\n" " Five = (One, Two, Three, Four, 5)\n" "};"); verifyFormat("enum Enum {};"); verifyFormat("enum {};"); verifyFormat("enum X E {} d;"); verifyFormat("enum __attribute__((...)) E {} d;"); verifyFormat("enum __declspec__((...)) E {} d;"); verifyFormat("enum {\n" " Bar = Foo::value\n" "};", getLLVMStyleWithColumns(30)); verifyFormat("enum ShortEnum { A, B, C };"); verifyGoogleFormat("enum ShortEnum { A, B, C };"); EXPECT_EQ("enum KeepEmptyLines {\n" " ONE,\n" "\n" " TWO,\n" "\n" " THREE\n" "}", format("enum KeepEmptyLines {\n" " ONE,\n" "\n" " TWO,\n" "\n" "\n" " THREE\n" "}")); verifyFormat("enum E { // comment\n" " ONE,\n" " TWO\n" "};\n" "int i;"); // Not enums. verifyFormat("enum X f() {\n" " a();\n" " return 42;\n" "}"); verifyFormat("enum X Type::f() {\n" " a();\n" " return 42;\n" "}"); verifyFormat("enum ::X f() {\n" " a();\n" " return 42;\n" "}"); verifyFormat("enum ns::X f() {\n" " a();\n" " return 42;\n" "}"); } TEST_F(FormatTest, FormatsEnumsWithErrors) { verifyFormat("enum Type {\n" " One = 0; // These semicolons should be commas.\n" " Two = 1;\n" "};"); verifyFormat("namespace n {\n" "enum Type {\n" " One,\n" " Two, // missing };\n" " int i;\n" "}\n" "void g() {}"); } TEST_F(FormatTest, FormatsEnumStruct) { verifyFormat("enum struct {\n" " Zero,\n" " One = 1,\n" " Two = One + 1,\n" " Three = (One + Two),\n" " Four = (Zero && (One ^ Two)) | (One << Two),\n" " Five = (One, Two, Three, Four, 5)\n" "};"); verifyFormat("enum struct Enum {};"); verifyFormat("enum struct {};"); verifyFormat("enum struct X E {} d;"); verifyFormat("enum struct __attribute__((...)) E {} d;"); verifyFormat("enum struct __declspec__((...)) E {} d;"); verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); } TEST_F(FormatTest, FormatsEnumClass) { verifyFormat("enum class {\n" " Zero,\n" " One = 1,\n" " Two = One + 1,\n" " Three = (One + Two),\n" " Four = (Zero && (One ^ Two)) | (One << Two),\n" " Five = (One, Two, Three, Four, 5)\n" "};"); verifyFormat("enum class Enum {};"); verifyFormat("enum class {};"); verifyFormat("enum class X E {} d;"); verifyFormat("enum class __attribute__((...)) E {} d;"); verifyFormat("enum class __declspec__((...)) E {} d;"); verifyFormat("enum class X f() {\n a();\n return 42;\n}"); } TEST_F(FormatTest, FormatsEnumTypes) { verifyFormat("enum X : int {\n" " A, // Force multiple lines.\n" " B\n" "};"); verifyFormat("enum X : int { A, B };"); verifyFormat("enum X : std::uint32_t { A, B };"); } TEST_F(FormatTest, FormatsNSEnums) { verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" " // Information about someDecentlyLongValue.\n" " someDecentlyLongValue,\n" " // Information about anotherDecentlyLongValue.\n" " anotherDecentlyLongValue,\n" " // 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) { verifyFormat("struct Bitfields {\n" " unsigned sClass : 8;\n" " unsigned ValueKind : 2;\n" "};"); verifyFormat("struct A {\n" " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" "};"); verifyFormat("struct MyStruct {\n" " uchar data;\n" " uchar : 8;\n" " uchar : 8;\n" " uchar other;\n" "};"); } TEST_F(FormatTest, FormatsNamespaces) { verifyFormat("namespace some_namespace {\n" "class A {};\n" "void f() { f(); }\n" "}"); verifyFormat("namespace {\n" "class A {};\n" "void f() { f(); }\n" "}"); verifyFormat("inline namespace X {\n" "class A {};\n" "void f() { f(); }\n" "}"); verifyFormat("using namespace some_namespace;\n" "class A {};\n" "void f() { f(); }"); // This code is more common than we thought; if we // layout this correctly the semicolon will go into // its own line, which is undesirable. verifyFormat("namespace {};"); verifyFormat("namespace {\n" "class A {};\n" "};"); verifyFormat("namespace {\n" "int SomeVariable = 0; // comment\n" "} // namespace"); EXPECT_EQ("#ifndef HEADER_GUARD\n" "#define HEADER_GUARD\n" "namespace my_namespace {\n" "int i;\n" "} // my_namespace\n" "#endif // HEADER_GUARD", format("#ifndef HEADER_GUARD\n" " #define HEADER_GUARD\n" " namespace my_namespace {\n" "int i;\n" "} // my_namespace\n" "#endif // HEADER_GUARD")); EXPECT_EQ("namespace A::B {\n" "class C {};\n" "}", format("namespace A::B {\n" "class C {};\n" "}")); FormatStyle Style = getLLVMStyle(); Style.NamespaceIndentation = FormatStyle::NI_All; EXPECT_EQ("namespace out {\n" " int i;\n" " namespace in {\n" " int i;\n" " } // namespace\n" "} // namespace", format("namespace out {\n" "int i;\n" "namespace in {\n" "int i;\n" "} // namespace\n" "} // namespace", Style)); Style.NamespaceIndentation = FormatStyle::NI_Inner; EXPECT_EQ("namespace out {\n" "int i;\n" "namespace in {\n" " int i;\n" "} // namespace\n" "} // namespace", format("namespace out {\n" "int i;\n" "namespace in {\n" "int i;\n" "} // namespace\n" "} // namespace", Style)); } 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" "}")); EXPECT_EQ("_asm {\n" " xor eax, eax;\n" " cpuid;\n" "}", format("_asm {\n" " xor eax, eax;\n" " cpuid;\n" "}")); verifyFormat("void function() {\n" " // comment\n" " asm(\"\");\n" "}"); EXPECT_EQ("__asm {\n" "}\n" "int i;", format("__asm {\n" "}\n" "int i;")); } TEST_F(FormatTest, FormatTryCatch) { verifyFormat("try {\n" " throw a * b;\n" "} catch (int a) {\n" " // Do nothing.\n" "} catch (...) {\n" " exit(42);\n" "}"); // Function-level try statements. verifyFormat("int f() try { return 4; } catch (...) {\n" " return 5;\n" "}"); verifyFormat("class A {\n" " int a;\n" " A() try : a(0) {\n" " } catch (...) {\n" " throw;\n" " }\n" "};\n"); // Incomplete try-catch blocks. verifyIncompleteFormat("try {} catch ("); } TEST_F(FormatTest, FormatSEHTryCatch) { verifyFormat("__try {\n" " int a = b * c;\n" "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" " // Do nothing.\n" "}"); verifyFormat("__try {\n" " int a = b * c;\n" "} __finally {\n" " // Do nothing.\n" "}"); verifyFormat("DEBUG({\n" " __try {\n" " } __finally {\n" " }\n" "});\n"); } TEST_F(FormatTest, IncompleteTryCatchBlocks) { verifyFormat("try {\n" " f();\n" "} catch {\n" " g();\n" "}"); verifyFormat("try {\n" " f();\n" "} catch (A a) MACRO(x) {\n" " g();\n" "} catch (B b) MACRO(x) {\n" " g();\n" "}"); } TEST_F(FormatTest, FormatTryCatchBraceStyles) { FormatStyle Style = getLLVMStyle(); for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, FormatStyle::BS_WebKit}) { Style.BreakBeforeBraces = BraceStyle; verifyFormat("try {\n" " // something\n" "} catch (...) {\n" " // something\n" "}", Style); } Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; verifyFormat("try {\n" " // something\n" "}\n" "catch (...) {\n" " // something\n" "}", Style); verifyFormat("__try {\n" " // something\n" "}\n" "__finally {\n" " // something\n" "}", Style); verifyFormat("@try {\n" " // something\n" "}\n" "@finally {\n" " // something\n" "}", Style); Style.BreakBeforeBraces = FormatStyle::BS_Allman; verifyFormat("try\n" "{\n" " // something\n" "}\n" "catch (...)\n" "{\n" " // something\n" "}", Style); Style.BreakBeforeBraces = FormatStyle::BS_GNU; verifyFormat("try\n" " {\n" " // something\n" " }\n" "catch (...)\n" " {\n" " // something\n" " }", Style); Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BraceWrapping.BeforeCatch = true; verifyFormat("try {\n" " // something\n" "}\n" "catch (...) {\n" " // something\n" "}", Style); } TEST_F(FormatTest, FormatObjCTryCatch) { verifyFormat("@try {\n" " f();\n" "} @catch (NSException e) {\n" " @throw;\n" "} @finally {\n" " exit(42);\n" "}"); verifyFormat("DEBUG({\n" " @try {\n" " } @finally {\n" " }\n" "});\n"); } TEST_F(FormatTest, FormatObjCAutoreleasepool) { FormatStyle Style = getLLVMStyle(); verifyFormat("@autoreleasepool {\n" " f();\n" "}\n" "@autoreleasepool {\n" " f();\n" "}\n", Style); Style.BreakBeforeBraces = FormatStyle::BS_Allman; verifyFormat("@autoreleasepool\n" "{\n" " f();\n" "}\n" "@autoreleasepool\n" "{\n" " f();\n" "}\n", Style); } TEST_F(FormatTest, StaticInitializers) { verifyFormat("static SomeClass SC = {1, 'a'};"); verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" " 100000000, " "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); // Here, everything other than the "}" would fit on a line. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" " 10000000000000000000000000};"); EXPECT_EQ("S s = {a,\n" "\n" " b};", format("S s = {\n" " a,\n" "\n" " b\n" "};")); // FIXME: This would fit into the column limit if we'd fit "{ {" on the first // line. However, the formatting looks a bit off and this probably doesn't // happen often in practice. verifyFormat("static int Variable[1] = {\n" " {1000000000000000000000000000000000000}};", getLLVMStyleWithColumns(40)); } TEST_F(FormatTest, DesignatedInitializers) { verifyFormat("const struct A a = {.a = 1, .b = 2};"); verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" " .bbbbbbbbbb = 2,\n" " .cccccccccc = 3,\n" " .dddddddddd = 4,\n" " .eeeeeeeeee = 5};"); verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" " .ccccccccccccccccccccccccccc = 3,\n" " .ddddddddddddddddddddddddddd = 4,\n" " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); } TEST_F(FormatTest, NestedStaticInitializers) { verifyFormat("static A x = {{{}}};\n"); verifyFormat("static A x = {{{init1, init2, init3, init4},\n" " {init1, init2, init3, init4}}};", getLLVMStyleWithColumns(50)); verifyFormat("somes Status::global_reps[3] = {\n" " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", getLLVMStyleWithColumns(60)); verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" " {rect.fRight - rect.fLeft, rect.fBottom - " "rect.fTop}};"); verifyFormat( "SomeArrayOfSomeType a = {\n" " {{1, 2, 3},\n" " {1, 2, 3},\n" " {111111111111111111111111111111, 222222222222222222222222222222,\n" " 333333333333333333333333333333},\n" " {1, 2, 3},\n" " {1, 2, 3}}};"); verifyFormat( "SomeArrayOfSomeType a = {\n" " {{1, 2, 3}},\n" " {{1, 2, 3}},\n" " {{111111111111111111111111111111, 222222222222222222222222222222,\n" " 333333333333333333333333333333}},\n" " {{1, 2, 3}},\n" " {{1, 2, 3}}};"); verifyFormat("struct {\n" " unsigned bit;\n" " const char *const name;\n" "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" " {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) { verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" " \\\n" " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); } TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { verifyFormat("virtual void write(ELFWriter *writerrr,\n" " OwningPtr &buffer) = 0;"); // Do break defaulted and deleted functions. verifyFormat("virtual void ~Deeeeeeeestructor() =\n" " default;", getLLVMStyleWithColumns(40)); verifyFormat("virtual void ~Deeeeeeeestructor() =\n" " delete;", getLLVMStyleWithColumns(40)); } TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", getLLVMStyleWithColumns(40)); verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", getLLVMStyleWithColumns(40)); EXPECT_EQ("#define Q \\\n" " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" " \"aaaaaaaa.cpp\"", format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", getLLVMStyleWithColumns(40))); } TEST_F(FormatTest, UnderstandsLinePPDirective) { EXPECT_EQ("# 123 \"A string literal\"", format(" # 123 \"A string literal\"")); } TEST_F(FormatTest, LayoutUnknownPPDirective) { EXPECT_EQ("#;", format("#;")); verifyFormat("#\n;\n;\n;"); } TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { EXPECT_EQ("#line 42 \"test\"\n", format("# \\\n line \\\n 42 \\\n \"test\"\n")); EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", getLLVMStyleWithColumns(12))); } TEST_F(FormatTest, EndOfFileEndsPPDirective) { EXPECT_EQ("#line 42 \"test\"", format("# \\\n line \\\n 42 \\\n \"test\"")); EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); } TEST_F(FormatTest, DoesntRemoveUnknownTokens) { verifyFormat("#define A \\x20"); verifyFormat("#define A \\ x20"); EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); verifyFormat("#define A ''"); verifyFormat("#define A ''qqq"); verifyFormat("#define A `qqq"); verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); EXPECT_EQ("const char *c = STRINGIFY(\n" "\\na : b);", format("const char * c = STRINGIFY(\n" "\\na : b);")); verifyFormat("a\r\\"); verifyFormat("a\v\\"); verifyFormat("a\f\\"); } TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); // FIXME: We never break before the macro name. verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); verifyFormat("#define A A\n#define A A"); verifyFormat("#define A(X) A\n#define A A"); verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); } TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { EXPECT_EQ("// somecomment\n" "#include \"a.h\"\n" "#define A( \\\n" " A, B)\n" "#include \"b.h\"\n" "// somecomment\n", format(" // somecomment\n" " #include \"a.h\"\n" "#define A(A,\\\n" " B)\n" " #include \"b.h\"\n" " // somecomment\n", getLLVMStyleWithColumns(13))); } TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { EXPECT_EQ("#define A \\\n" " c; \\\n" " e;\n" "f;", format("#define A c; e;\n" "f;", getLLVMStyleWithColumns(14))); } TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } TEST_F(FormatTest, MacroDefinitionInsideStatement) { EXPECT_EQ("int x,\n" "#define A\n" " y;", format("int x,\n#define A\ny;")); } TEST_F(FormatTest, HashInMacroDefinition) { EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); verifyFormat("#define A \\\n" " { \\\n" " f(#c); \\\n" " }", getLLVMStyleWithColumns(11)); verifyFormat("#define A(X) \\\n" " void function##X()", getLLVMStyleWithColumns(22)); verifyFormat("#define A(a, b, c) \\\n" " void a##b##c()", getLLVMStyleWithColumns(22)); verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); } TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { EXPECT_EQ("#define A (x)", format("#define A (x)")); EXPECT_EQ("#define A(x)", format("#define A(x)")); } TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { EXPECT_EQ("#define A b;", format("#define A \\\n" " \\\n" " b;", getLLVMStyleWithColumns(25))); EXPECT_EQ("#define A \\\n" " \\\n" " a; \\\n" " b;", format("#define A \\\n" " \\\n" " a; \\\n" " b;", getLLVMStyleWithColumns(11))); EXPECT_EQ("#define A \\\n" " a; \\\n" " \\\n" " b;", format("#define A \\\n" " a; \\\n" " \\\n" " b;", getLLVMStyleWithColumns(11))); } TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { verifyIncompleteFormat("#define A :"); verifyFormat("#define SOMECASES \\\n" " case 1: \\\n" " case 2\n", getLLVMStyleWithColumns(20)); verifyFormat("#define A template "); verifyIncompleteFormat("#define STR(x) #x\n" "f(STR(this_is_a_string_literal{));"); verifyFormat("#pragma omp threadprivate( \\\n" " y)), // expected-warning", getLLVMStyleWithColumns(28)); verifyFormat("#d, = };"); verifyFormat("#if \"a"); verifyIncompleteFormat("({\n" "#define b \\\n" " } \\\n" " a\n" "a", getLLVMStyleWithColumns(15)); verifyFormat("#define A \\\n" " { \\\n" " {\n" "#define B \\\n" " } \\\n" " }", getLLVMStyleWithColumns(15)); verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); } TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. EXPECT_EQ("class A : public QObject {\n" " Q_OBJECT\n" "\n" " A() {}\n" "};", format("class A : public QObject {\n" " Q_OBJECT\n" "\n" " A() {\n}\n" "} ;")); EXPECT_EQ("MACRO\n" "/*static*/ int i;", format("MACRO\n" " /*static*/ int i;")); EXPECT_EQ("SOME_MACRO\n" "namespace {\n" "void f();\n" "}", format("SOME_MACRO\n" " namespace {\n" "void f( );\n" "}")); // Only if the identifier contains at least 5 characters. EXPECT_EQ("HTTP f();", format("HTTP\nf();")); EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); // Only if everything is upper case. EXPECT_EQ("class A : public QObject {\n" " Q_Object A() {}\n" "};", format("class A : public QObject {\n" " 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) { EXPECT_EQ("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; }", 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" "}")); // Same inside macros. EXPECT_EQ("#define LIST(L) \\\n" " L(A) \\\n" " L(B) \\\n" " L(C)", format("#define LIST(L) \\\n" " L(A) \\\n" " L(B) \\\n" " L(C)", getGoogleStyle())); // These must not be recognized as macros. 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" " } 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(int i) noexcept() : {}\n" " A(X x)\n" // FIXME: function-level try blocks are broken. " try : t(0) {\n" " } catch (...) {\n" " }\n" "};", format("class A {\n" " A()\n : t(0) {}\n" " A(int i)\n noexcept() : {}\n" " A(X x)\n" " try : t(0) {} catch (...) {}\n" "};")); EXPECT_EQ("class SomeClass {\n" "public:\n" " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" "};", format("class SomeClass {\n" "public:\n" " SomeClass()\n" " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" "};")); EXPECT_EQ("class SomeClass {\n" "public:\n" " SomeClass()\n" " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" "};", format("class SomeClass {\n" "public:\n" " SomeClass()\n" " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" "};", getLLVMStyleWithColumns(40))); verifyFormat("MACRO(>)"); } TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { verifyFormat("#define A \\\n" " f({ \\\n" " g(); \\\n" " });", getLLVMStyleWithColumns(11)); } TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); } TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { verifyFormat("{\n { a #c; }\n}"); } TEST_F(FormatTest, FormatUnbalancedStructuralElements) { EXPECT_EQ("#define A \\\n { \\\n {\nint i;", format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); EXPECT_EQ("#define A \\\n } \\\n }\nint i;", format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); } TEST_F(FormatTest, EscapedNewlines) { EXPECT_EQ( "#define A \\\n int i; \\\n int j;", format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); EXPECT_EQ("template f();", format("\\\ntemplate f();")); EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); EXPECT_EQ("", format("")); } 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" " a); \\\n" " int i;", getLLVMStyleWithColumns(11)); } TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { EXPECT_EQ( "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" " \\\n" " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" "\n" "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" "\\\n" "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" " \n" " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); } TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { EXPECT_EQ("int\n" "#define A\n" " a;", format("int\n#define A\na;")); verifyFormat("functionCallTo(\n" " someOtherFunction(\n" " withSomeParameters, whichInSequence,\n" " areLongerThanALine(andAnotherCall,\n" "#define A B\n" " withMoreParamters,\n" " whichStronglyInfluenceTheLayout),\n" " andMoreParameters),\n" " trailing);", getLLVMStyleWithColumns(69)); verifyFormat("Foo::Foo()\n" "#ifdef BAR\n" " : baz(0)\n" "#endif\n" "{\n" "}"); verifyFormat("void f() {\n" " if (true)\n" "#ifdef A\n" " f(42);\n" " x();\n" "#else\n" " g();\n" " x();\n" "#endif\n" "}"); verifyFormat("void f(param1, param2,\n" " param3,\n" "#ifdef A\n" " param4(param5,\n" "#ifdef A1\n" " param6,\n" "#ifdef A2\n" " param7),\n" "#else\n" " param8),\n" " param9,\n" "#endif\n" " param10,\n" "#endif\n" " param11)\n" "#else\n" " param12)\n" "#endif\n" "{\n" " x();\n" "}", getLLVMStyleWithColumns(28)); verifyFormat("#if 1\n" "int i;"); verifyFormat("#if 1\n" "#endif\n" "#if 1\n" "#else\n" "#endif\n"); verifyFormat("DEBUG({\n" " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" "});\n" "#if a\n" "#else\n" "#endif"); verifyIncompleteFormat("void f(\n" "#if A\n" " );\n" "#else\n" "#endif"); } TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { verifyFormat("#endif\n" "#if B"); } TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { FormatStyle SingleLine = getLLVMStyle(); SingleLine.AllowShortIfStatementsOnASingleLine = true; verifyFormat("#if 0\n" "#elif 1\n" "#endif\n" "void foo() {\n" " if (test) foo2();\n" "}", SingleLine); } TEST_F(FormatTest, LayoutBlockInsideParens) { verifyFormat("functionCall({ int i; });"); verifyFormat("functionCall({\n" " int i;\n" " int j;\n" "});"); verifyFormat("functionCall(\n" " {\n" " int i;\n" " int j;\n" " },\n" " aaaa, bbbb, cccc);"); verifyFormat("functionA(functionB({\n" " int i;\n" " int j;\n" " }),\n" " aaaa, bbbb, cccc);"); verifyFormat("functionCall(\n" " {\n" " int i;\n" " int j;\n" " },\n" " aaaa, bbbb, // comment\n" " cccc);"); verifyFormat("functionA(functionB({\n" " int i;\n" " int j;\n" " }),\n" " aaaa, bbbb, // comment\n" " cccc);"); verifyFormat("functionCall(aaaa, bbbb, { int i; });"); verifyFormat("functionCall(aaaa, bbbb, {\n" " int i;\n" " int j;\n" "});"); verifyFormat( "Aaa(\n" // FIXME: There shouldn't be a linebreak here. " {\n" " int i; // break\n" " },\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" " ccccccccccccccccc));"); verifyFormat("DEBUG({\n" " if (a)\n" " f();\n" "});"); } TEST_F(FormatTest, LayoutBlockInsideStatement) { EXPECT_EQ("SOME_MACRO { int i; }\n" "int i;", format(" SOME_MACRO {int i;} int i;")); } TEST_F(FormatTest, LayoutNestedBlocks) { verifyFormat("void AddOsStrings(unsigned bitmask) {\n" " struct s {\n" " int i;\n" " };\n" " s kBitsToOs[] = {{10}};\n" " for (int i = 0; i < 10; ++i)\n" " return;\n" "}"); verifyFormat("call(parameter, {\n" " something();\n" " // Comment using all columns.\n" " somethingelse();\n" "});", getLLVMStyleWithColumns(40)); verifyFormat("DEBUG( //\n" " { f(); }, a);"); verifyFormat("DEBUG( //\n" " {\n" " f(); //\n" " },\n" " a);"); EXPECT_EQ("call(parameter, {\n" " something();\n" " // Comment too\n" " // looooooooooong.\n" " somethingElse();\n" "});", format("call(parameter, {\n" " something();\n" " // Comment too looooooooooong.\n" " somethingElse();\n" "});", getLLVMStyleWithColumns(29))); EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); EXPECT_EQ("DEBUG({ // comment\n" " int i;\n" "});", format("DEBUG({ // comment\n" "int i;\n" "});")); EXPECT_EQ("DEBUG({\n" " int i;\n" "\n" " // comment\n" " int j;\n" "});", format("DEBUG({\n" " int i;\n" "\n" " // comment\n" " int j;\n" "});")); verifyFormat("DEBUG({\n" " if (a)\n" " return;\n" "});"); verifyGoogleFormat("DEBUG({\n" " if (a) return;\n" "});"); FormatStyle Style = getGoogleStyle(); Style.ColumnLimit = 45; verifyFormat("Debug(aaaaa,\n" " {\n" " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" " },\n" " a);", Style); verifyFormat("SomeFunction({MACRO({ return output; }), b});"); verifyNoCrash("^{v^{a}}"); } TEST_F(FormatTest, FormatNestedBlocksInMacros) { EXPECT_EQ("#define MACRO() \\\n" " Debug(aaa, /* force line break */ \\\n" " { \\\n" " int i; \\\n" " int j; \\\n" " })", format("#define MACRO() Debug(aaa, /* force line break */ \\\n" " { int i; int j; })", getGoogleStyle())); EXPECT_EQ("#define A \\\n" " [] { \\\n" " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" " }", format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", getGoogleStyle())); } TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { EXPECT_EQ("{}", format("{}")); verifyFormat("enum E {};"); verifyFormat("enum E {}"); } TEST_F(FormatTest, FormatBeginBlockEndMacros) { FormatStyle Style = getLLVMStyle(); Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; Style.MacroBlockEnd = "^[A-Z_]+_END$"; verifyFormat("FOO_BEGIN\n" " FOO_ENTRY\n" "FOO_END", Style); verifyFormat("FOO_BEGIN\n" " NESTED_FOO_BEGIN\n" " NESTED_FOO_ENTRY\n" " NESTED_FOO_END\n" "FOO_END", Style); verifyFormat("FOO_BEGIN(Foo, Bar)\n" " int x;\n" " x = 1;\n" "FOO_END(Baz)", Style); } //===----------------------------------------------------------------------===// // Line break tests. //===----------------------------------------------------------------------===// TEST_F(FormatTest, PreventConfusingIndents) { verifyFormat( "void f() {\n" " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" " parameter, parameter, parameter)),\n" " SecondLongCall(parameter));\n" "}"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " [aaaaaaaaaaaaaaaaaaaaaaaa\n" " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" " aaaaaaaaaaaaaaaaaaaaaaaa<\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" " aaaaaaaaaaaaaaaaaaaaaaaa>;"); verifyFormat("int a = bbbb && ccc && fffff(\n" "#define A Just forcing a new line\n" " ddd);"); } TEST_F(FormatTest, LineBreakingInBinaryExpressions) { verifyFormat( "bool aaaaaaa =\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" " bbbbbbbb();"); verifyFormat( "bool aaaaaaa =\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" " bbbbbbbb();"); verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" " ccccccccc == ddddddddddd;"); verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" " ccccccccc == ddddddddddd;"); verifyFormat( "bool aaaaaaaaaaaaaaaaaaaaa =\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" " ccccccccc == ddddddddddd;"); verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" " aaaaaa) &&\n" " bbbbbb && cccccc;"); verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" " aaaaaa) >>\n" " bbbbbb;"); verifyFormat("aa = Whitespaces.addUntouchableComment(\n" " SourceMgr.getSpellingColumnNumber(\n" " TheLine.Last->FormatTok.Tok.getLocation()) -\n" " 1);"); 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. verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" "}"); verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" "}"); verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" "}"); // Even explicit parentheses stress the precedence enough to make the // additional break unnecessary. verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" "}"); // This cases is borderline, but with the indentation it is still readable. verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" "}", getLLVMStyleWithColumns(75)); // If the LHS is a binary expression, we should still use the additional break // as otherwise the formatting hides the operator precedence. verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" " 5) {\n" "}"); FormatStyle OnePerLine = getLLVMStyle(); OnePerLine.BinPackParameters = false; verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", OnePerLine); } 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}"); verifyFormat("if () {\n" "} else if (aaaaa &&\n" " bbbbb > // break\n" " ccccc) {\n" "}"); // Presence of a trailing comment used to change indentation of b. verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" " b;\n" "return aaaaaaaaaaaaaaaaaaa +\n" " b; //", getLLVMStyleWithColumns(30)); } TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { // Not sure what the best system is here. Like this, the LHS can be found // immediately above an operator (everything with the same or a higher // indent). The RHS is aligned right of the operator and so compasses // everything until something with the same indent as the operator is found. // FIXME: Is this a good system? FormatStyle Style = getLLVMStyle(); Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; verifyFormat( "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " > ccccccccccccccccccccccccccccccccccccccccc;", Style); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", Style); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", Style); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", Style); verifyFormat("if () {\n" "} else if (aaaaa\n" " && bbbbb // break\n" " > ccccc) {\n" "}", Style); verifyFormat("return (a)\n" " // comment\n" " + b;", Style); verifyFormat( "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" " + cc;", Style); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", Style); // Forced by comments. verifyFormat( "unsigned ContentSize =\n" " sizeof(int16_t) // DWARF ARange version number\n" " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" " + sizeof(int8_t) // Pointer Size (in bytes)\n" " + sizeof(int8_t); // Segment Size (in bytes)"); verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" " == boost::fusion::at_c<1>(iiii).second;", Style); Style.ColumnLimit = 60; verifyFormat("zzzzzzzzzz\n" " = bbbbbbbbbbbbbbbbb\n" " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 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 = FormatStyle::BAS_DontAlign; 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) {}", getLLVMStyleWithColumns(45)); verifyFormat("Constructor()\n" " : Inttializer(FitsOnTheLine) {}", getLLVMStyleWithColumns(44)); verifyFormat("Constructor()\n" " : Inttializer(FitsOnTheLine) {}", getLLVMStyleWithColumns(43)); verifyFormat("template \n" "Constructor() : Initializer(FitsOnTheLine) {}", getLLVMStyleWithColumns(45)); verifyFormat( "SomeClass::Constructor()\n" " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); verifyFormat( "SomeClass::Constructor()\n" " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); verifyFormat( "SomeClass::Constructor()\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " : aaaaaaaaaa(aaaaaa) {}"); verifyFormat("Constructor()\n" " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaaaa() {}"); verifyFormat("Constructor()\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); verifyFormat("Constructor(int Parameter = 0)\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); verifyFormat("Constructor()\n" " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" "}", getLLVMStyleWithColumns(60)); verifyFormat("Constructor()\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); // Here a line could be saved by splitting the second initializer onto two // lines, but that is not desirable. verifyFormat("Constructor()\n" " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaa(aaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); FormatStyle OnePerLine = getLLVMStyle(); OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; verifyFormat("SomeClass::Constructor()\n" " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", OnePerLine); verifyFormat("SomeClass::Constructor()\n" " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", OnePerLine); verifyFormat("MyClass::MyClass(int var)\n" " : some_var_(var), // 4 space indent\n" " some_other_var_(var + 1) { // lined up\n" "}", OnePerLine); verifyFormat("Constructor()\n" " : aaaaa(aaaaaa),\n" " aaaaa(aaaaaa),\n" " aaaaa(aaaaaa),\n" " aaaaa(aaaaaa),\n" " aaaaa(aaaaaa) {}", OnePerLine); verifyFormat("Constructor()\n" " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaa) {}", OnePerLine); OnePerLine.ColumnLimit = 60; verifyFormat("Constructor()\n" " : aaaaaaaaaaaaaaaaaaaa(a),\n" " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", OnePerLine); EXPECT_EQ("Constructor()\n" " : // Comment forcing unwanted break.\n" " aaaa(aaaa) {}", format("Constructor() :\n" " // Comment forcing unwanted break.\n" " aaaa(aaaa) {}")); } 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)); 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)); verifyFormat( "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a),\n" " a)", 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"; for (unsigned i = 0, e = 80; i != e; ++i) { input += " a,\n"; } input += " a) {}"; verifyFormat(input, OnePerLine); } TEST_F(FormatTest, BreaksAsHighAsPossible) { verifyFormat( "void f() {\n" " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" " f();\n" "}"); verifyFormat("if (Intervals[i].getRange().getFirst() <\n" " 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);"); verifyFormat("template \n" "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" " TemplateIt *stop) {}"); // 2) break after return type. verifyFormat( "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", getGoogleStyle()); // 3) break after (. verifyFormat( "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", getGoogleStyle()); // 4) break before after nested name specifiers. verifyFormat( "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" "SomeClasssssssssssssssssssssssssssssssssssssss::\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", getGoogleStyle()); // 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( "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", getGoogleStyle()); 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);"); verifyFormat("void SomeLoooooooooooongFunction(\n" " std::unique_ptr\n" " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " int bbbbbbbbbbbbb);"); // Treat overloaded operators like other functions. verifyFormat("SomeLoooooooooooooooooooooooooogType\n" "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); verifyFormat("SomeLoooooooooooooooooooooooooogType\n" "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); verifyFormat("SomeLoooooooooooooooooooooooooogType\n" "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); verifyGoogleFormat( "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); verifyGoogleFormat( "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); verifyGoogleFormat( "typename aaaaaaaaaa::aaaaaaaaaaa\n" "aaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); FormatStyle Style = getLLVMStyle(); Style.PointerAlignment = FormatStyle::PAS_Left; verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", Style); verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", Style); } TEST_F(FormatTest, TrailingReturnType) { verifyFormat("auto foo() -> int;\n"); verifyFormat("struct S {\n" " auto bar() const -> int;\n" "};"); verifyFormat("template \n" "auto load_img(const std::string &filename)\n" " -> alias::tensor {}"); verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); verifyFormat("template \n" "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" " -> decltype(eaaaaaaaaaaaaaaa(t.a).aaaaaaaa());"); // Not trailing return types. verifyFormat("void f() { auto a = b->c(); }"); } TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { // Avoid breaking before trailing 'const' or other trailing annotations, if // they are not function-like. FormatStyle Style = getGoogleStyle(); Style.ColumnLimit = 47; verifyFormat("void someLongFunction(\n" " int someLoooooooooooooongParameter) const {\n}", getLLVMStyleWithColumns(47)); verifyFormat("LoooooongReturnType\n" "someLoooooooongFunction() const {}", getLLVMStyleWithColumns(47)); verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" " const {}", Style); verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" " aaaaaaaaaaa aaaaa) const override;"); verifyGoogleFormat( "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" " const override;"); // Even if the first parameter has to be wrapped. verifyFormat("void someLongFunction(\n" " int someLongParameter) const {}", getLLVMStyleWithColumns(46)); verifyFormat("void someLongFunction(\n" " int someLongParameter) const {}", Style); verifyFormat("void someLongFunction(\n" " int someLongParameter) override {}", Style); verifyFormat("void someLongFunction(\n" " int someLongParameter) OVERRIDE {}", Style); verifyFormat("void someLongFunction(\n" " int someLongParameter) final {}", Style); verifyFormat("void someLongFunction(\n" " int someLongParameter) FINAL {}", Style); verifyFormat("void someLongFunction(\n" " int parameter) const override {}", Style); Style.BreakBeforeBraces = FormatStyle::BS_Allman; verifyFormat("void someLongFunction(\n" " int someLongParameter) const\n" "{\n" "}", Style); // Unless these are unknown annotations. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " LONG_AND_UGLY_ANNOTATION;"); // Breaking before function-like trailing annotations is fine to keep them // close to their arguments. verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); verifyFormat( "void aaaaaaaaaaaaaaaaaa()\n" " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaa));"); verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " __attribute__((unused));"); verifyGoogleFormat( "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " GUARDED_BY(aaaaaaaaaaaa);"); verifyGoogleFormat( "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " GUARDED_BY(aaaaaaaaaaaa);"); verifyGoogleFormat( "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyGoogleFormat( "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" " aaaaaaaaaaaaaaaaaaaaaaaaa;"); } TEST_F(FormatTest, FunctionAnnotations) { verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" "int OldFunction(const string ¶meter) {}"); verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" "string OldFunction(const string ¶meter) {}"); verifyFormat("template \n" "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" "string OldFunction(const string ¶meter) {}"); // Not function annotations. verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); } TEST_F(FormatTest, BreaksDesireably) { verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" "}"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); verifyFormat( "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat( "void f() {\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" "}"); verifyFormat( "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); verifyFormat( "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); // Indent consistently independent of call expression and unary operator. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" " dddddddddddddddddddddddddddddd));"); verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" " dddddddddddddddddddddddddddddd));"); verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" " dddddddddddddddddddddddddddddd));"); // This test case breaks on an incorrect memoization, i.e. an optimization not // taking into account the StopAt value. verifyFormat( "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("{\n {\n {\n" " Annotation.SpaceRequiredBefore =\n" " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" " }\n }\n}"); // Break on an outer level if there was a break on an inner level. EXPECT_EQ("f(g(h(a, // comment\n" " b, c),\n" " d, e),\n" " x, y);", format("f(g(h(a, // comment\n" " b, c), d, e), x, y);")); // Prefer breaking similar line breaks. verifyFormat( "const int kTrackingOptions = NSTrackingMouseMoved |\n" " NSTrackingMouseEnteredAndExited |\n" " 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);", NoBinPacking); verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" " aaaaaaaaaaaaa,\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", NoBinPacking); verifyFormat( "aaaaaaaa(aaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", NoBinPacking); verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaaaaa();", NoBinPacking); verifyFormat("void f() {\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" "}", NoBinPacking); verifyFormat( "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaa,\n" " aaaaaaaaaaaa);", NoBinPacking); verifyFormat( "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" " ddddddddddddddddddddddddddddd),\n" " test);", NoBinPacking); verifyFormat("std::vector\n" " aaaaaaaaaaaaaaaaaa;", NoBinPacking); verifyFormat("a(\"a\"\n" " \"a\",\n" " a);"); NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" " aaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", NoBinPacking); verifyFormat( "void f() {\n" " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaa();\n" "}", NoBinPacking); verifyFormat( "template \n" "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", NoBinPacking); } TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { FormatStyle Style = getLLVMStyleWithColumns(15); Style.ExperimentalAutoDetectBinPacking = true; EXPECT_EQ("aaa(aaaa,\n" " aaaa,\n" " aaaa);\n" "aaa(aaaa,\n" " aaaa,\n" " aaaa);", format("aaa(aaaa,\n" // one-per-line " aaaa,\n" " aaaa );\n" "aaa(aaaa, aaaa, aaaa);", // inconclusive Style)); EXPECT_EQ("aaa(aaaa, aaaa,\n" " aaaa);\n" "aaa(aaaa, aaaa,\n" " aaaa);", format("aaa(aaaa, aaaa,\n" // bin-packed " aaaa );\n" "aaa(aaaa, aaaa, aaaa);", // inconclusive Style)); } TEST_F(FormatTest, FormatsBuilderPattern) { verifyFormat("return llvm::StringSwitch(name)\n" " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" " .StartsWith(\".init\", ORDER_INIT)\n" " .StartsWith(\".fini\", ORDER_FINI)\n" " .StartsWith(\".hash\", ORDER_HASH)\n" " .Default(ORDER_TEXT);\n"); verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); verifyFormat( "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); verifyFormat( "aaaaaaa->aaaaaaa\n" " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); verifyFormat( "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" " aaaaaaaaaaaaaa);"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" " aaaaaa->aaaaaaaaaaaa()\n" " ->aaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " ->aaaaaaaaaaaaaaaaa();"); verifyGoogleFormat( "void f() {\n" " someo->Add((new util::filetools::Handler(dir))\n" " ->OnEvent1(NewPermanentCallback(\n" " this, &HandlerHolderClass::EventHandlerCBA))\n" " ->OnEvent2(NewPermanentCallback(\n" " this, &HandlerHolderClass::EventHandlerCBB))\n" " ->OnEvent3(NewPermanentCallback(\n" " this, &HandlerHolderClass::EventHandlerCBC))\n" " ->OnEvent5(NewPermanentCallback(\n" " this, &HandlerHolderClass::EventHandlerCBD))\n" " ->OnEvent6(NewPermanentCallback(\n" " this, &HandlerHolderClass::EventHandlerCBE)));\n" "}"); verifyFormat( "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); verifyFormat("aaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaa();"); verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaa();"); verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaa();"); verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" " ->aaaaaaaaaaaaaae(0)\n" " ->aaaaaaaaaaaaaaa();"); // Don't linewrap after very short segments. verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); verifyFormat("aaa()\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" " .has();"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); // Prefer not to break after empty parentheses. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" " First->LastNewlineOffset);"); // Prefer not to create "hanging" indents. verifyFormat( "return !soooooooooooooome_map\n" " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " .second;"); } TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" " ccccccccccccccccccccccccc) {\n}"); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" " ccccccccccccccccccccccccc) {\n}"); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" " ccccccccccccccccccccccccc) {\n}"); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" " ccccccccccccccccccccccccc) {\n}"); verifyFormat( "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" " ccccccccccccccccccccccccc) {\n}"); verifyFormat( "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" " ccccccccccccccccccccccccc) {\n}"); verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" " aaaaaaaaaaaaaaa != aa) {\n}"); verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" " aaaaaaaaaaaaaaa != aa) {\n}"); } TEST_F(FormatTest, BreaksAfterAssignments) { verifyFormat( "unsigned Cost =\n" " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" " SI->getPointerAddressSpaceee());\n"); verifyFormat( "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("unsigned OriginalStartColumn =\n" " SourceMgr.getSpellingColumnNumber(\n" " Current.FormatTok.getStartOfNonWhitespace()) -\n" " 1;"); } TEST_F(FormatTest, AlignsAfterAssignments) { verifyFormat( "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat( "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat( "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat( "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat( "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaa;"); } TEST_F(FormatTest, AlignsAfterReturn) { verifyFormat( "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 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;"); verifyFormat("return\n" " // true if code is one of a or b.\n" " 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 = FormatStyle::BAS_DontAlign; 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); Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; Style.BinPackArguments = false; Style.BinPackParameters = false; verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaa aaaaaaaa,\n" " aaaaaaaaa aaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", Style); verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" " aaaaaaaaaaa aaaaaaaaa,\n" " aaaaaaaaaaa aaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", Style); verifyFormat("SomeLongVariableName->someFunction(\n" " foooooooo(\n" " aaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", Style); } TEST_F(FormatTest, ParenthesesAndOperandAlignment) { FormatStyle Style = getLLVMStyleWithColumns(40); verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" " bbbbbbbbbbbbbbbbbbbbbb);", Style); Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; Style.AlignOperands = false; verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" " bbbbbbbbbbbbbbbbbbbbbb);", Style); Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; Style.AlignOperands = true; verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" " bbbbbbbbbbbbbbbbbbbbbb);", Style); Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; Style.AlignOperands = false; verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" " bbbbbbbbbbbbbbbbbbbbbb);", Style); } TEST_F(FormatTest, BreaksConditionalExpressions) { verifyFormat( "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat( "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" " : aaaaaaaaaaaaa);"); verifyFormat( "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaa);"); verifyFormat( "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaa);"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 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" " ? 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" " ? IndentForLevel[TheLine.Level]\n" " : TheLine * 2,\n" " TheLine.InPPDirective, PreviousEndOfLineColumn);", getLLVMStyleWithColumns(70)); verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" " ? aaaaaaaaaaaaaaa\n" " : bbbbbbbbbbbbbbb //\n" " ? ccccccccccccccc\n" " : ddddddddddddddd;"); verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" " ? aaaaaaaaaaaaaaa\n" " : (bbbbbbbbbbbbbbb //\n" " ? ccccccccccccccc\n" " : ddddddddddddddd);"); verifyFormat( "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaa\n" " : aaaaaaaaaa;"); verifyFormat( "aaaaaa = aaaaaaaaaaaa\n" " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " : aaaaaaaaaaaaaaaaaaaaaa\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackArguments = false; verifyFormat( "void f() {\n" " g(aaa,\n" " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " ? aaaaaaaaaaaaaaa\n" " : aaaaaaaaaaaaaaa);\n" "}", NoBinPacking); verifyFormat( "void f() {\n" " g(aaa,\n" " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " ?: 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) { FormatStyle Style = getLLVMStyle(); Style.BreakBeforeTernaryOperators = false; Style.ColumnLimit = 70; verifyFormat( "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", Style); verifyFormat( "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", Style); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" " aaaaaaaaaaaaa);", Style); verifyFormat( "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaa);", Style); verifyFormat( "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaa);", Style); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", Style); verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", Style); verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", Style); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", Style); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", Style); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" " aaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaaaaa;", Style); verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" " aaaaaaaaa ?\n" " b :\n" " c);", Style); verifyFormat( "unsigned Indent =\n" " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" " IndentForLevel[TheLine.Level] :\n" " TheLine * 2,\n" " TheLine.InPPDirective, PreviousEndOfLineColumn);", Style); verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" " aaaaaaaaaaaaaaa :\n" " bbbbbbbbbbbbbbb ? //\n" " ccccccccccccccc :\n" " ddddddddddddddd;", Style); verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" " aaaaaaaaaaaaaaa :\n" " (bbbbbbbbbbbbbbb ? //\n" " ccccccccccccccc :\n" " ddddddddddddddd);", Style); } TEST_F(FormatTest, DeclarationsOfMultipleVariables) { verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); verifyFormat("bool a = true, b = false;"); verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); verifyFormat( "bool aaaaaaaaaaaaaaaaaaaaa =\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;"); FormatStyle Style = getGoogleStyle(); Style.PointerAlignment = FormatStyle::PAS_Left; Style.DerivePointerAlignment = false; verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" " *b = bbbbbbbbbbbbbbbbbbb;", Style); verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", Style); } TEST_F(FormatTest, ConditionalExpressionsInBrackets) { verifyFormat("arr[foo ? bar : baz];"); verifyFormat("f()[foo ? bar : baz];"); verifyFormat("(a + b)[foo ? bar : baz];"); verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); } TEST_F(FormatTest, AlignsStringLiterals) { verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" " \"short literal\");"); verifyFormat( "looooooooooooooooooooooooongFunction(\n" " \"short literal\"\n" " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); verifyFormat("someFunction(\"Always break between multi-line\"\n" " \" string literals\",\n" " and, other, parameters);"); EXPECT_EQ("fun + \"1243\" /* comment */\n" " \"5678\";", format("fun + \"1243\" /* comment */\n" " \"5678\";", getLLVMStyleWithColumns(28))); EXPECT_EQ( "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" " \"aaaaaaaaaaaaaaaaaaaaa\"\n" " \"aaaaaaaaaaaaaaaa\";", format("aaaaaa =" "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " "aaaaaaaaaaaaaaaaaaaaa\" " "\"aaaaaaaaaaaaaaaa\";")); verifyFormat("a = a + \"a\"\n" " \"a\"\n" " \"a\";"); verifyFormat("f(\"a\", \"b\"\n" " \"c\");"); verifyFormat( "#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\");"); verifyFormat("f(L\"a\"\n" " L\"b\");"); verifyFormat("#define A(X) \\\n" " L\"aaaaa\" #X L\"bbbbbb\" \\\n" " L\"ccccc\"", getLLVMStyleWithColumns(25)); verifyFormat("f(@\"a\"\n" " @\"b\");"); verifyFormat("NSString s = @\"a\"\n" " @\"b\"\n" " @\"c\";"); verifyFormat("NSString s = @\"a\"\n" " \"b\"\n" " \"c\";"); } TEST_F(FormatTest, ReturnTypeBreakingStyle) { FormatStyle Style = getLLVMStyle(); // No declarations or definitions should be moved to own line. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; verifyFormat("class A {\n" " int f() { return 1; }\n" " int g();\n" "};\n" "int f() { return 1; }\n" "int g();\n", Style); // All declarations and definitions should have the return type moved to its // own // line. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; verifyFormat("class E {\n" " int\n" " f() {\n" " return 1;\n" " }\n" " int\n" " g();\n" "};\n" "int\n" "f() {\n" " return 1;\n" "}\n" "int\n" "g();\n", Style); // Top-level definitions, and no kinds of declarations should have the // return type moved to its own line. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; verifyFormat("class B {\n" " int f() { return 1; }\n" " int g();\n" "};\n" "int\n" "f() {\n" " return 1;\n" "}\n" "int g();\n", Style); // Top-level definitions and declarations should have the return type moved // to its own line. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; verifyFormat("class C {\n" " int f() { return 1; }\n" " int g();\n" "};\n" "int\n" "f() {\n" " return 1;\n" "}\n" "int\n" "g();\n", Style); // All definitions should have the return type moved to its own line, but no // kinds of declarations. Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; verifyFormat("class D {\n" " int\n" " f() {\n" " return 1;\n" " }\n" " int g();\n" "};\n" "int\n" "f() {\n" " return 1;\n" "}\n" "int g();\n", Style); verifyFormat("const char *\n" "f(void) {\n" // Break here. " return \"\";\n" "}\n" "const char *bar(void);\n", // No break here. Style); verifyFormat("template \n" "T *\n" "f(T &c) {\n" // Break here. " return NULL;\n" "}\n" "template T *f(T &c);\n", // No break here. Style); verifyFormat("class C {\n" " int\n" " operator+() {\n" " return 1;\n" " }\n" " int\n" " operator()() {\n" " return 1;\n" " }\n" "};\n", Style); verifyFormat("void\n" "A::operator()() {}\n" "void\n" "A::operator>>() {}\n" "void\n" "A::operator+() {}\n", Style); verifyFormat("void *operator new(std::size_t s);", // No break here. Style); verifyFormat("void *\n" "operator new(std::size_t s) {}", Style); verifyFormat("void *\n" "operator delete[](void *ptr) {}", Style); Style.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. Style); verifyFormat("template \n" "T *\n" // Problem here: no line break "f(T &c)\n" // Break here. "{\n" " return NULL;\n" "}\n" "template T *f(T &c);\n", // No break here. Style); } TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { FormatStyle NoBreak = getLLVMStyle(); NoBreak.AlwaysBreakBeforeMultilineStrings = false; FormatStyle Break = getLLVMStyle(); Break.AlwaysBreakBeforeMultilineStrings = true; verifyFormat("aaaa = \"bbbb\"\n" " \"cccc\";", NoBreak); verifyFormat("aaaa =\n" " \"bbbb\"\n" " \"cccc\";", Break); verifyFormat("aaaa(\"bbbb\"\n" " \"cccc\");", NoBreak); verifyFormat("aaaa(\n" " \"bbbb\"\n" " \"cccc\");", Break); verifyFormat("aaaa(qqq, \"bbbb\"\n" " \"cccc\");", NoBreak); verifyFormat("aaaa(qqq,\n" " \"bbbb\"\n" " \"cccc\");", Break); verifyFormat("aaaa(qqq,\n" " L\"bbbb\"\n" " L\"cccc\");", Break); verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" " \"bbbb\"));", Break); verifyFormat("string s = someFunction(\n" " \"abc\"\n" " \"abc\");", Break); // As we break before unary operators, breaking right after them is bad. verifyFormat("string foo = abc ? \"x\"\n" " \"blah blah blah blah blah blah\"\n" " : \"y\";", Break); // Don't break if there is no column gain. verifyFormat("f(\"aaaa\"\n" " \"bbbb\");", Break); // Treat literals with escaped newlines like multi-line string literals. EXPECT_EQ("x = \"a\\\n" "b\\\n" "c\";", format("x = \"a\\\n" "b\\\n" "c\";", NoBreak)); EXPECT_EQ("xxxx =\n" " \"a\\\n" "b\\\n" "c\";", format("xxxx = \"a\\\n" "b\\\n" "c\";", Break)); // Exempt ObjC strings for now. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" " @\"bbbb\";", format("NSString *const kString = @\"aaaa\"\n" "@\"bbbb\";", Break)); Break.ColumnLimit = 0; verifyFormat("const char *hello = \"hello llvm\";", Break); } TEST_F(FormatTest, AlignsPipes) { verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat( "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" " << aaaaaaaaaaaaaaaaaaaa;"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat( "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); verifyFormat( "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); verifyFormat( "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("return out << \"somepacket = {\\n\"\n" " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" " << \"}\";"); verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); verifyFormat( "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); verifyFormat( "void f() {\n" " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" "}"); verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaa)\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat("LOG_IF(aaa == //\n" " bbb)\n" " << a << b;"); // Breaking before the first "<<" is generally not desirable. verifyFormat( "llvm::errs()\n" " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", getLLVMStyleWithColumns(70)); verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << \"aaaaaaaaaaaaaaaaaaa: \"\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << \"aaaaaaaaaaaaaaaaaaa: \"\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", getLLVMStyleWithColumns(70)); // But sometimes, breaking before the first "<<" is desirable. verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" " << BEF << IsTemplate << Description << E->getType();"); verifyFormat( "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); // Incomplete string literal. EXPECT_EQ("llvm::errs() << \"\n" " << a;", format("llvm::errs() << \"\n<cccccc)\n" " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" "}"); // Handle 'endl'. verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); } TEST_F(FormatTest, UnderstandsEquals) { verifyFormat( "aaaaaaaaaaaaaaaaa =\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); verifyFormat( "if (a) {\n" " f();\n" "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" "}"); verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" " 100000000 + 10000000) {\n}"); } TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" " .looooooooooooooooooooooooooooooooooooooongFunction();"); verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" " ->looooooooooooooooooooooooooooooooooooooongFunction();"); verifyFormat( "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" " Parameter2);"); verifyFormat( "ShortObject->shortFunction(\n" " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); verifyFormat("loooooooooooooongFunction(\n" " LoooooooooooooongObject->looooooooooooooooongFunction());"); verifyFormat( "function(LoooooooooooooooooooooooooooooooooooongObject\n" " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 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" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaa(aaaaa),\n" " aaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("void f() {\n" " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" "}"); verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " 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" " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); verifyFormat( "aaaaaaaaaaa->aaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 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());"); verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " .a();"); FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackParameters = false; verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", NoBinPacking); // If there is a subsequent call, change to hanging indentation. verifyFormat( "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); verifyFormat( "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); } TEST_F(FormatTest, WrapsTemplateDeclarations) { verifyFormat("template \n" "virtual void loooooooooooongFunction(int Param1, int Param2);"); verifyFormat("template \n" "// T should be one of {A, B}.\n" "virtual void loooooooooooongFunction(int Param1, int Param2);"); verifyFormat( "template \n" "using comment_to_xml_conversion = comment_to_xml_conversion;"); verifyFormat("template \n" "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); verifyFormat( "template \n" "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" " int Paaaaaaaaaaaaaaaaaaaaram2);"); verifyFormat( "template \n" "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("template \n" "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " int aaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat( "template \n" "void f();"); verifyFormat("template class cccccccccccccccccccccc,\n" " typename ddddddddddddd>\n" "class C {};"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("void f() {\n" " a(\n" " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" "}"); verifyFormat("template class C {};"); verifyFormat("template void f();"); verifyFormat("template void f() {}"); verifyFormat( "aaaaaaaaaaaaa *aaaa =\n" " new aaaaaaaaaaaaa(\n" " bbbbbbbbbbbbbbbbbbbbbbbb);", getLLVMStyleWithColumns(72)); EXPECT_EQ("static_cast *>(\n" "\n" " );", format("static_cast*>(\n" "\n" " );")); verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); FormatStyle AlwaysBreak = getLLVMStyle(); AlwaysBreak.AlwaysBreakTemplateDeclarations = true; verifyFormat("template \nclass C {};", AlwaysBreak); verifyFormat("template \nvoid f();", AlwaysBreak); verifyFormat("template \nvoid f() {}", AlwaysBreak); verifyFormat("void aaaaaaaaaaaaaaaaaaa(\n" " ccccccccccccccccccccccccccccccccccccccccccccccc);"); verifyFormat("template