diff options
Diffstat (limited to 'unittests/Format/FormatTest.cpp')
-rw-r--r-- | unittests/Format/FormatTest.cpp | 1644 |
1 files changed, 1204 insertions, 440 deletions
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 8e770c2..1c100c3 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -17,18 +17,29 @@ namespace clang { namespace format { -FormatStyle getGoogleStyle() { - return getGoogleStyle(FormatStyle::LK_Cpp); -} +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, unsigned Offset, unsigned Length, - const FormatStyle &Style) { + const FormatStyle &Style, + IncompleteCheck CheckIncomplete = IC_ExpectComplete) { DEBUG(llvm::errs() << "---\n"); DEBUG(llvm::errs() << Code << "\n\n"); std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); - tooling::Replacements Replaces = reformat(Style, Code, Ranges); + bool IncompleteFormat = false; + tooling::Replacements Replaces = + reformat(Style, Code, Ranges, "<stdin>", &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); @@ -36,9 +47,10 @@ protected: return Result; } - std::string - format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { - return format(Code, 0, Code.size(), Style); + std::string format(llvm::StringRef Code, + const FormatStyle &Style = getLLVMStyle(), + IncompleteCheck CheckIncomplete = IC_ExpectComplete) { + return format(Code, 0, Code.size(), Style, CheckIncomplete); } FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { @@ -58,6 +70,12 @@ protected: 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()); } @@ -67,6 +85,12 @@ protected: 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; }; @@ -167,8 +191,7 @@ TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) { // This might not strictly be correct, but is likely good in all practical // cases. - EXPECT_EQ("int b;\nint a;", - format("int b;int a;", 7, 0, getLLVMStyle())); + EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0, getLLVMStyle())); } TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { @@ -307,14 +330,14 @@ TEST_F(FormatTest, ReformatsMovedLines) { } 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);"); + 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);"); } //===----------------------------------------------------------------------===// @@ -413,6 +436,12 @@ TEST_F(FormatTest, FormatShortBracedStatements) { " f();\n" "}", AllowSimpleBracedStatements); + verifyFormat("if (true) {\n" + " f();\n" + "} else {\n" + " f();\n" + "}", + AllowSimpleBracedStatements); verifyFormat("template <int> struct A2 {\n" " struct B {};\n" @@ -424,6 +453,12 @@ TEST_F(FormatTest, FormatShortBracedStatements) { " f();\n" "}", AllowSimpleBracedStatements); + verifyFormat("if (true) {\n" + " f();\n" + "} else {\n" + " f();\n" + "}", + AllowSimpleBracedStatements); AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; verifyFormat("while (true) {\n" @@ -487,6 +522,11 @@ TEST_F(FormatTest, ElseIf) { "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" "}"); + verifyFormat("if (a) {\n" + "} else if (\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" + "}", + getLLVMStyleWithColumns(62)); } TEST_F(FormatTest, FormatsForLoop) { @@ -517,14 +557,21 @@ TEST_F(FormatTest, FormatsForLoop) { " I = FD->getDeclsInPrototypeScope().begin(),\n" " E = FD->getDeclsInPrototypeScope().end();\n" " I != E; ++I) {\n}"); + verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" + " I = Container.begin(),\n" + " E = Container.end();\n" + " I != E; ++I) {\n}", + getLLVMStyleWithColumns(76)); - // FIXME: Not sure whether we want extra identation in line 3 here: verifyFormat( "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\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" "}"); @@ -664,11 +711,17 @@ TEST_F(FormatTest, FormatsSwitchStatement) { " switch (x) { \\\n" " case a: \\\n" " foo = b; \\\n" - " }", getLLVMStyleWithColumns(20)); + " }", + 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" @@ -799,9 +852,16 @@ TEST_F(FormatTest, FormatsLabels) { " some_more_code();\n" " }\n" "}"); - verifyFormat("some_code();\n" + verifyFormat("{\n" + " some_code();\n" "test_label:\n" - "some_other_code();"); + " some_other_code();\n" + "}"); + verifyFormat("{\n" + " some_code();\n" + "test_label:;\n" + " int i = 0;\n" + "}"); } //===----------------------------------------------------------------------===// @@ -1007,11 +1067,10 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { " // 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(60)); verifyFormat( "#define A \\\n" " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" @@ -1029,6 +1088,11 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { " // 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) { @@ -1043,7 +1107,7 @@ TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { " c);", format("SomeFunction(a,\n" " b,\n" - " // comment\n" + " // comment\n" " c);")); EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" " c);", @@ -1276,13 +1340,12 @@ TEST_F(FormatTest, SplitsLongCxxComments) { 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))); + 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", @@ -1461,18 +1524,18 @@ TEST_F(FormatTest, SplitsLongLinesInComments) { "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( + "/* 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" @@ -1482,7 +1545,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) { "This is a long " "comment that doesn't " "fit on one line. \n" - "*/", getLLVMStyleWithColumns(20))); + "*/", + getLLVMStyleWithColumns(20))); EXPECT_EQ("/*\n" " * This is a long\n" " * comment that\n" @@ -1494,7 +1558,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) { " comment that " " doesn't fit on " " one line. \n" - " */", getLLVMStyleWithColumns(20))); + " */", + getLLVMStyleWithColumns(20))); EXPECT_EQ("/*\n" " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" " * so_it_should_be_broken\n" @@ -1528,7 +1593,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) { " doesn't fit on one" " line 1234567890\n" "*/\n" - "}", getLLVMStyleWithColumns(20))); + "}", + getLLVMStyleWithColumns(20))); EXPECT_EQ("{\n" " /*\n" " * This i s\n" @@ -1546,7 +1612,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) { " fit on one l i" " n e\n" " */\n" - "}", getLLVMStyleWithColumns(20))); + "}", + getLLVMStyleWithColumns(20))); EXPECT_EQ("/*\n" " * This is a long\n" " * comment that\n" @@ -1555,7 +1622,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) { " */", format(" /*\n" " * This is a long comment that doesn't fit on one line\n" - " */", getLLVMStyleWithColumns(20))); + " */", + getLLVMStyleWithColumns(20))); EXPECT_EQ("{\n" " if (something) /* This is a\n" " long\n" @@ -1590,6 +1658,17 @@ TEST_F(FormatTest, SplitsLongLinesInComments) { " */", 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" @@ -1849,7 +1928,6 @@ TEST_F(FormatTest, IgnoresIf0Contents) { "#endif\n" "Five\n" "};")); - } //===----------------------------------------------------------------------===// @@ -1879,7 +1957,16 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) { " void f() {}\n" "public Q_SLOTS:\n" " void f() {}\n" + "signals:\n" + " void g();\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) { @@ -1914,6 +2001,30 @@ TEST_F(FormatTest, SeparatesLogicalBlocks) { "\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) { @@ -2103,6 +2214,12 @@ TEST_F(FormatTest, FormatsBitfields) { " 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) { @@ -2204,10 +2321,24 @@ TEST_F(FormatTest, FormatsInlineASM) { " 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) { @@ -2230,6 +2361,29 @@ TEST_F(FormatTest, FormatTryCatch) { " 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) { @@ -2264,6 +2418,20 @@ TEST_F(FormatTest, FormatTryCatchBraceStyles) { " // 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" @@ -2289,21 +2457,24 @@ TEST_F(FormatTest, FormatTryCatchBraceStyles) { TEST_F(FormatTest, FormatObjCTryCatch) { verifyFormat("@try {\n" " f();\n" - "}\n" - "@catch (NSException e) {\n" + "} @catch (NSException e) {\n" " @throw;\n" - "}\n" - "@finally {\n" + "} @finally {\n" " exit(42);\n" "}"); + verifyFormat("DEBUG({\n" + " @try {\n" + " } @finally {\n" + " }\n" + "});\n"); } TEST_F(FormatTest, StaticInitializers) { verifyFormat("static SomeClass SC = {1, 'a'};"); - verifyFormat( - "static SomeClass WithALoooooooooooooooooooongName = {\n" - " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); + verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" + " 100000000, " + "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); // Here, everything other than the "}" would fit on a line. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" @@ -2357,9 +2528,9 @@ TEST_F(FormatTest, NestedStaticInitializers) { " {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("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" + " {rect.fRight - rect.fLeft, rect.fBottom - " + "rect.fTop}};"); verifyFormat( "SomeArrayOfSomeType a = {\n" @@ -2378,24 +2549,22 @@ TEST_F(FormatTest, NestedStaticInitializers) { " {{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" - "};"); + 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) { @@ -2407,6 +2576,14 @@ TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { verifyFormat("virtual void write(ELFWriter *writerrr,\n" " OwningPtr<FileOutputBuffer> &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) { @@ -2456,6 +2633,10 @@ TEST_F(FormatTest, DoesntRemoveUnknownTokens) { "\\na : b);", format("const char * c = STRINGIFY(\n" "\\na : b);")); + + verifyFormat("a\r\\"); + verifyFormat("a\v\\"); + verifyFormat("a\f\\"); } TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { @@ -2585,17 +2766,35 @@ TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { } TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { - verifyFormat("#define A :"); + verifyIncompleteFormat("#define A :"); verifyFormat("#define SOMECASES \\\n" " case 1: \\\n" " case 2\n", getLLVMStyleWithColumns(20)); verifyFormat("#define A template <typename T>"); - verifyFormat("#define STR(x) #x\n" - "f(STR(this_is_a_string_literal{));"); + 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) { @@ -2610,6 +2809,10 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { "\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" @@ -2619,10 +2822,8 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { "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();")); + 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" @@ -2638,7 +2839,8 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { "<< SomeThing;")); verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " - "(n, buffers))\n", getChromiumStyle(FormatStyle::LK_Cpp)); + "(n, buffers))\n", + getChromiumStyle(FormatStyle::LK_Cpp)); } TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { @@ -2760,34 +2962,34 @@ TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { " 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))); + 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))); } TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { verifyFormat("#define A \\\n" " f({ \\\n" " g(); \\\n" - " });", getLLVMStyleWithColumns(11)); + " });", + getLLVMStyleWithColumns(11)); } TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { @@ -2805,15 +3007,15 @@ TEST_F(FormatTest, FormatUnbalancedStructuralElements) { format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); } -TEST_F(FormatTest, EscapedNewlineAtStartOfToken) { +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 <class T> f();", format("\\\ntemplate <class T> f();")); -} - -TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) { EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); + EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); } TEST_F(FormatTest, DontCrashOnBlockComments) { @@ -2906,12 +3108,11 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { getLLVMStyleWithColumns(28)); verifyFormat("#if 1\n" "int i;"); - verifyFormat( - "#if 1\n" - "#endif\n" - "#if 1\n" - "#else\n" - "#endif\n"); + verifyFormat("#if 1\n" + "#endif\n" + "#if 1\n" + "#else\n" + "#endif\n"); verifyFormat("DEBUG({\n" " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" @@ -2919,6 +3120,12 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { "#if a\n" "#else\n" "#endif"); + + verifyIncompleteFormat("void f(\n" + "#if A\n" + " );\n" + "#else\n" + "#endif"); } TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { @@ -2929,48 +3136,50 @@ TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 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); + verifyFormat("#if 0\n" + "#elif 1\n" + "#endif\n" + "void foo() {\n" + " if (test) foo2();\n" + "}", + SingleLine); } TEST_F(FormatTest, LayoutBlockInsideParens) { - EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );")); - EXPECT_EQ("functionCall({\n" - " int i;\n" - " int j;\n" - "});", - format(" functionCall ( {int i;int j;} );")); - EXPECT_EQ("functionCall({\n" - " int i;\n" - " int j;\n" - "}, aaaa, bbbb, cccc);", - format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);")); - EXPECT_EQ("functionCall(\n" - " {\n" - " int i;\n" - " int j;\n" - " },\n" - " aaaa, bbbb, // comment\n" - " cccc);", - format(" functionCall ( {int i;int j;}, aaaa, bbbb, // comment\n" - "cccc);")); - EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });", - format(" functionCall (aaaa, bbbb, {int i;});")); - EXPECT_EQ("functionCall(aaaa, bbbb, {\n" - " int i;\n" - " int j;\n" - "});", - format(" functionCall (aaaa, bbbb, {int i;int j;});")); - EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });", - format(" functionCall (aaaa, bbbb, {int i;});")); + verifyFormat("functionCall({ int i; });"); + verifyFormat("functionCall({\n" + " int i;\n" + " int j;\n" + "});"); + verifyFormat("functionCall({\n" + " int i;\n" + " int j;\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. + "Aaa(\n" // FIXME: There shouldn't be a linebreak here. " {\n" " int i; // break\n" " },\n" @@ -2980,6 +3189,30 @@ TEST_F(FormatTest, LayoutBlockInsideParens) { " if (a)\n" " f();\n" "});"); + EXPECT_EQ("int longlongname; // comment\n" + "int x = f({\n" + " int x; // comment\n" + " int y; // comment\n" + "});", + format("int longlongname; // comment\n" + "int x = f({\n" + " int x; // comment\n" + " int y; // comment\n" + "});", + 65, 0, getLLVMStyle())); + EXPECT_EQ("int s = f({\n" + " class X {\n" + " public:\n" + " void f();\n" + " };\n" + "});", + format("int s = f({\n" + " class X {\n" + " public:\n" + " void f();\n" + " };\n" + "});", + 0, 0, getLLVMStyle())); } TEST_F(FormatTest, LayoutBlockInsideStatement) { @@ -3056,6 +3289,29 @@ TEST_F(FormatTest, LayoutNestedBlocks) { " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" "}, a);", Style); + + 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, IndividualStatementsOfNestedBlocks) { @@ -3127,6 +3383,19 @@ TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) { " int a; //\n" "});", 0, 0, getLLVMStyle())); + EXPECT_EQ("someFunction(\n" + " [] {\n" + " // Only with this comment.\n" + " int i; // invoke formatting here.\n" + " }, // force line break\n" + " aaa);", + format("someFunction(\n" + " [] {\n" + " // Only with this comment.\n" + " int i; // invoke formatting here.\n" + " }, // force line break\n" + " aaa);", + 63, 1, getLLVMStyle())); } TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { @@ -3221,10 +3490,9 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) { "}"); // Even explicit parentheses stress the precedence enough to make the // additional break unnecessary. - verifyFormat( - "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" - "}"); + verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" + "}"); // This cases is borderline, but with the indentation it is still readable. verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" @@ -3235,11 +3503,10 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) { // 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" - "}"); + verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" + " 5) {\n" + "}"); FormatStyle OnePerLine = getLLVMStyle(); OnePerLine.BinPackParameters = false; @@ -3330,9 +3597,14 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { " // comment\n" " + b;", Style); - verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" - " + cc;", + verifyFormat( + "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" + " + cc;", + Style); + + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", Style); // Forced by comments. @@ -3358,17 +3630,16 @@ 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("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" @@ -3580,10 +3851,9 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { // 1) break amongst arguments. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" " Cccccccccccccc cccccccccccccc);"); - verifyFormat( - "template <class TemplateIt>\n" - "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" - " TemplateIt *stop) {}"); + verifyFormat("template <class TemplateIt>\n" + "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" + " TemplateIt *stop) {}"); // 2) break after return type. verifyFormat( @@ -3664,6 +3934,15 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" "aaaaaaaaaa<aaaaaa>::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) { @@ -3770,6 +4049,23 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 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" + "string OldFunction(const string ¶meter) {}"); + verifyFormat("template <typename T>\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) { @@ -3808,10 +4104,9 @@ TEST_F(FormatTest, BreaksDesireably) { verifyFormat( "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); - verifyFormat( - "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); // Indent consistently independent of call expression. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" @@ -3952,20 +4247,19 @@ TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { } TEST_F(FormatTest, FormatsBuilderPattern) { - verifyFormat( - "return llvm::StringSwitch<Reference::Kind>(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 llvm::StringSwitch<Reference::Kind>(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->aaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); verifyFormat( "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" @@ -3974,7 +4268,7 @@ TEST_F(FormatTest, FormatsBuilderPattern) { "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" " aaaaaa->aaaaaaaaaaaa()\n" " ->aaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " ->aaaaaaaaaaaaaaaaa();"); verifyGoogleFormat( "void f() {\n" @@ -4009,6 +4303,18 @@ TEST_F(FormatTest, FormatsBuilderPattern) { " ->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<bbbbbbbbbbbbbbbbbbbbb>();"); @@ -4135,18 +4441,15 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) { " aaaaaaaaaaaaaaaaaaaaa));"); FormatStyle Style = getLLVMStyle(); Style.AlignAfterOpenBracket = false; - verifyFormat( - "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", - Style); - verifyFormat( - "SomeLongVariableName->someVeryLongFunctionName(\n" - " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", - Style); - verifyFormat( - "SomeLongVariableName->someFunction(\n" - " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", - Style); + verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\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) {}", @@ -4239,13 +4542,12 @@ TEST_F(FormatTest, BreaksConditionalExpressions) { " // 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("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" @@ -4422,17 +4724,17 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) { " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); - // FIXME: If multiple variables are defined, the "*" needs to move to the new - // line. Also fix indent for breaking after the type, this looks bad. - verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" - " * b = bbbbbbbbbbbbbbbbbbb;", - getGoogleStyle()); - // Not ideal, but pointer-with-type does not allow much here. - verifyGoogleFormat( - "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, * b = bbbbbbbbbbbbbbbbbbb,\n" - " * b = bbbbbbbbbbbbbbbbbbb, * d = ddddddddddddddddddd;"); + 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) { @@ -4485,44 +4787,53 @@ TEST_F(FormatTest, AlignsStringLiterals) { " \"jkl\");"); verifyFormat("f(L\"a\"\n" - " L\"b\")"); + " 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, AlwaysBreakAfterDefinitionReturnType) { FormatStyle AfterType = getLLVMStyle(); AfterType.AlwaysBreakAfterDefinitionReturnType = true; verifyFormat("const char *\n" - "f(void) {\n" // Break here. + "f(void) {\n" // Break here. " return \"\";\n" "}\n" - "const char *bar(void);\n", // No break here. + "const char *bar(void);\n", // No break here. AfterType); verifyFormat("template <class T>\n" "T *\n" - "f(T &c) {\n" // Break here. + "f(T &c) {\n" // Break here. " return NULL;\n" "}\n" - "template <class T> T *f(T &c);\n", // No break here. + "template <class T> T *f(T &c);\n", // No break here. AfterType); AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup; verifyFormat("const char *\n" - "f(void)\n" // Break here. + "f(void)\n" // Break here. "{\n" " return \"\";\n" "}\n" - "const char *bar(void);\n", // No break here. + "const char *bar(void);\n", // No break here. AfterType); verifyFormat("template <class T>\n" - "T *\n" // Problem here: no line break - "f(T &c)\n" // Break here. + "T *\n" // Problem here: no line break + "f(T &c)\n" // Break here. "{\n" " return NULL;\n" "}\n" - "template <class T> T *f(T &c);\n", // No break here. + "template <class T> T *f(T &c);\n", // No break here. AfterType); } @@ -4587,10 +4898,13 @@ TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { // Exempt ObjC strings for now. EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" - " \"bbbb\";", + " @\"bbbb\";", format("NSString *const kString = @\"aaaa\"\n" - "\"bbbb\";", + "@\"bbbb\";", Break)); + + Break.ColumnLimit = 0; + verifyFormat("const char *hello = \"hello llvm\";", Break); } TEST_F(FormatTest, AlignsPipes) { @@ -4612,15 +4926,12 @@ TEST_F(FormatTest, AlignsPipes) { "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); - verifyFormat( - "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); - verifyFormat( - "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" - " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); + 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);"); @@ -4650,6 +4961,10 @@ TEST_F(FormatTest, AlignsPipes) { "}"); verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); + verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaa)\n" + " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); // Breaking before the first "<<" is generally not desirable. verifyFormat( @@ -4691,8 +5006,9 @@ TEST_F(FormatTest, AlignsPipes) { "}"); // Handle 'endl'. - verifyFormat("llvm::errs() << aaaa << endl\n" - " << bbbb << endl;"); + verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" + " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); + verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); } TEST_F(FormatTest, UnderstandsEquals) { @@ -4746,18 +5062,18 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" " ccccccccccccccccccccccc);"); verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\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("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" @@ -4780,9 +5096,9 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" " aaaaaaaaa()->aaaaaa()->aaaaa());"); - // FIXME: Should we break before .a()? verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();"); + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " .a();"); FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackParameters = false; @@ -4959,6 +5275,9 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) { verifyFormat("f<int>();"); verifyFormat("template <typename T> void f() {}"); verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); + verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " + "sizeof(char)>::type>;"); + verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); // Not template parameters. verifyFormat("return a < b && c > d;"); @@ -4974,6 +5293,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) { getLLVMStyleWithColumns(60)); verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); + verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); } TEST_F(FormatTest, UnderstandsBinaryOperators) { @@ -5092,17 +5412,42 @@ TEST_F(FormatTest, UnderstandsOverloadedOperators) { verifyFormat("using A::operator+;"); - verifyFormat("Deleted &operator=(const Deleted &)& = default;"); - verifyFormat("Deleted &operator=(const Deleted &)&& = delete;"); - verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;"); - verifyGoogleFormat("Deleted& operator=(const Deleted&)&& = delete;"); - verifyFormat("string // break\n" "operator()() & {}"); verifyFormat("string // break\n" "operator()() && {}"); } +TEST_F(FormatTest, UnderstandsFunctionRefQualification) { + verifyFormat("Deleted &operator=(const Deleted &)& = default;"); + verifyFormat("Deleted &operator=(const Deleted &)&& = delete;"); + verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;"); + verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;"); + verifyFormat("Deleted &operator=(const Deleted &)&;"); + verifyFormat("Deleted &operator=(const Deleted &)&&;"); + verifyFormat("SomeType MemberFunction(const Deleted &)&;"); + verifyFormat("SomeType MemberFunction(const Deleted &)&&;"); + + verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;"); + verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;"); + verifyGoogleFormat("Deleted& operator=(const Deleted&)&;"); + verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;"); + + FormatStyle Spaces = getLLVMStyle(); + Spaces.SpacesInCStyleCastParentheses = true; + verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces); + verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces); + verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces); + verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces); + + Spaces.SpacesInCStyleCastParentheses = false; + Spaces.SpacesInParentheses = true; + verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces); + verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces); + verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces); + verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces); +} + TEST_F(FormatTest, UnderstandsNewAndDelete) { verifyFormat("void f() {\n" " A *a = new A;\n" @@ -5134,6 +5479,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyIndependentOfContext("int a = *b;"); verifyIndependentOfContext("int a = *b * c;"); verifyIndependentOfContext("int a = b * *c;"); + verifyIndependentOfContext("int a = b * (10);"); + verifyIndependentOfContext("S << b * (10);"); verifyIndependentOfContext("return 10 * b;"); verifyIndependentOfContext("return *b * *c;"); verifyIndependentOfContext("return a & ~b;"); @@ -5166,13 +5513,16 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyFormat("auto PointerBinding = [](const char *S) {};"); verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); verifyFormat("[](const decltype(*a) &value) {}"); + verifyFormat("#define MACRO() [](A *a) { return 1; }"); verifyIndependentOfContext("typedef void (*f)(int *a);"); verifyIndependentOfContext("int i{a * b};"); verifyIndependentOfContext("aaa && aaa->f();"); verifyIndependentOfContext("int x = ~*p;"); verifyFormat("Constructor() : a(a), area(width * height) {}"); verifyFormat("Constructor() : a(a), area(a, width * height) {}"); + verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); verifyFormat("void f() { f(a, c * d); }"); + verifyFormat("void f() { f(new a(), c * d); }"); verifyIndependentOfContext("InvalidRegions[*R] = 0;"); @@ -5193,6 +5543,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyGoogleFormat("**outparam = 1;"); + verifyGoogleFormat("*outparam = a * b;"); verifyGoogleFormat("int main(int argc, char** argv) {}"); verifyGoogleFormat("A<int*> a;"); verifyGoogleFormat("A<int**> a;"); @@ -5237,6 +5588,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyIndependentOfContext("if (int *a = (&b))"); verifyIndependentOfContext("while (int *a = &b)"); verifyIndependentOfContext("size = sizeof *a;"); + verifyIndependentOfContext("if (a && (b = c))"); verifyFormat("void f() {\n" " for (const int &v : Values) {\n" " }\n" @@ -5255,8 +5607,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyIndependentOfContext("A = new SomeType *[Length]();"); verifyIndependentOfContext("T **t = new T *;"); verifyIndependentOfContext("T **t = new T *();"); - verifyGoogleFormat("A = new SomeType* [Length]();"); - verifyGoogleFormat("A = new SomeType* [Length];"); + verifyGoogleFormat("A = new SomeType*[Length]();"); + verifyGoogleFormat("A = new SomeType*[Length];"); verifyGoogleFormat("T** t = new T*;"); verifyGoogleFormat("T** t = new T*();"); @@ -5324,8 +5676,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyFormat("A<int **> a;", PointerMiddle); verifyFormat("A<int *, int *> a;", PointerMiddle); verifyFormat("A<int * []> a;", PointerMiddle); - verifyFormat("A = new SomeType * [Length]();", PointerMiddle); - verifyFormat("A = new SomeType * [Length];", PointerMiddle); + verifyFormat("A = new SomeType *[Length]();", PointerMiddle); + verifyFormat("A = new SomeType *[Length];", PointerMiddle); verifyFormat("T ** t = new T *;", PointerMiddle); } @@ -5431,6 +5783,7 @@ TEST_F(FormatTest, FormatsCasts) { verifyFormat("my_int a = (const my_int)-1;"); verifyFormat("my_int a = (const my_int *)-1;"); verifyFormat("my_int a = (my_int)(my_int)-1;"); + verifyFormat("my_int a = (ns::my_int)-2;"); // FIXME: single value wrapped with paren will be treated as cast. verifyFormat("void f(int i = (kValue)*kMask) {}"); @@ -5503,11 +5856,18 @@ TEST_F(FormatTest, FormatsFunctionTypes) { verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); } +TEST_F(FormatTest, FormatsPointersToArrayTypes) { + verifyFormat("A (*foo_)[6];"); + verifyFormat("vector<int> (*foo_)[6];"); +} + TEST_F(FormatTest, BreaksLongVariableDeclarations) { verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" " LoooooooooooooooooooooooooooooooooooooooongVariable;"); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" " LoooooooooooooooooooooooooooooooooooooooongVariable;"); + verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" + " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); // Different ways of ()-initializiation. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" @@ -5516,6 +5876,8 @@ TEST_F(FormatTest, BreaksLongVariableDeclarations) { " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); + verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" + " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); } TEST_F(FormatTest, BreaksLongDeclarations) { @@ -5525,6 +5887,8 @@ TEST_F(FormatTest, BreaksLongDeclarations) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); + verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" + "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" @@ -5563,12 +5927,12 @@ TEST_F(FormatTest, BreaksLongDeclarations) { " SourceLocation L, IdentifierIn *II,\n" " Type *T) {}"); verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" - "ReallyReallyLongFunctionName(\n" + "ReallyReaaallyLongFunctionName(\n" " const std::string &SomeParameter,\n" - " const SomeType<string, SomeOtherTemplateParameter> &\n" - " ReallyReallyLongParameterName,\n" - " const SomeType<string, SomeOtherTemplateParameter> &\n" - " AnotherLongParameterName) {}"); + " const SomeType<string, SomeOtherTemplateParameter>\n" + " &ReallyReallyLongParameterName,\n" + " const SomeType<string, SomeOtherTemplateParameter>\n" + " &AnotherLongParameterName) {}"); verifyFormat("template <typename A>\n" "SomeLoooooooooooooooooooooongType<\n" " typename some_namespace::SomeOtherType<A>::Type>\n" @@ -5592,8 +5956,15 @@ TEST_F(FormatTest, BreaksLongDeclarations) { " int aaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" - " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" + " aaaaaaaaaaaaaaaaaaaaaaaa);"); + verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); } TEST_F(FormatTest, FormatsArrays) { @@ -5617,6 +5988,8 @@ TEST_F(FormatTest, FormatsArrays) { "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" " .aaaaaaa[0]\n" " .aaaaaaaaaaaaaaaaaaaaaa();"); + + verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); } TEST_F(FormatTest, LineStartsWithSpecialCharacter) { @@ -5632,6 +6005,7 @@ TEST_F(FormatTest, HandlesIncludeDirectives) { "#include \"string.h\"\n" "#include <a-a>\n" "#include < path with space >\n" + "#include_next <test.h>" "#include \"abc.h\" // this is included for ABC\n" "#include \"some long include\" // with a comment\n" "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", @@ -5744,6 +6118,7 @@ TEST_F(FormatTest, IncorrectAccessSpecifier) { TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { verifyFormat("{"); verifyFormat("#})"); + verifyNoCrash("(/**/[:!] ?[)."); } TEST_F(FormatTest, IncorrectCodeDoNoWhile) { @@ -5760,16 +6135,16 @@ TEST_F(FormatTest, IncorrectCodeDoNoWhile) { TEST_F(FormatTest, IncorrectCodeMissingParens) { verifyFormat("if {\n foo;\n foo();\n}"); verifyFormat("switch {\n foo;\n foo();\n}"); - verifyFormat("for {\n foo;\n foo();\n}"); + verifyIncompleteFormat("for {\n foo;\n foo();\n}"); verifyFormat("while {\n foo;\n foo();\n}"); verifyFormat("do {\n foo;\n foo();\n} while;"); } TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { - verifyFormat("namespace {\n" - "class Foo { Foo (\n" - "};\n" - "} // comment"); + verifyIncompleteFormat("namespace {\n" + "class Foo { Foo (\n" + "};\n" + "} // comment"); } TEST_F(FormatTest, IncorrectCodeErrorDetection) { @@ -5832,10 +6207,23 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); verifyFormat("Node n{1, Node{1000}, //\n" " 2};"); + verifyFormat("Aaaa aaaaaaa{\n" + " {\n" + " aaaa,\n" + " },\n" + "};"); + verifyFormat("class C : public D {\n" + " SomeClass SC{2};\n" + "};"); + verifyFormat("class C : public A {\n" + " class D : public B {\n" + " void f() { int i{2}; }\n" + " };\n" + "};"); - // In combination with BinPackParameters = false. + // In combination with BinPackArguments = false. FormatStyle NoBinPacking = getLLVMStyle(); - NoBinPacking.BinPackParameters = false; + NoBinPacking.BinPackArguments = false; verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" " bbbbb,\n" " ccccc,\n" @@ -5949,9 +6337,18 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { ExtraSpaces); verifyFormat( "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" - " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n" - " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};"); + " aaaaaaa,\n" + " aaaaaaaaaa,\n" + " aaaaa,\n" + " aaaaaaaaaaaaaaa,\n" + " aaa,\n" + " aaaaaaaaaa,\n" + " a,\n" + " aaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaa,\n" + " a};"); verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); } @@ -5962,11 +6359,9 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" " 1, 22, 333, 4444, 55555, 666666, 7777777};"); - verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " // line comment\n" + verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555,\n" - " // line comment\n" + " 1, 22, 333, 4444, 55555, //\n" " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" " 1, 22, 333, 4444, 55555, 666666, 7777777};"); verifyFormat( @@ -5980,6 +6375,14 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); + verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" + " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" + " // Separating comment.\n" + " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); + verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" + " // Leading comment\n" + " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" + " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); verifyFormat("vector<int> x = {1, 1, 1, 1,\n" " 1, 1, 1, 1};", getLLVMStyleWithColumns(39)); @@ -5989,6 +6392,24 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", getLLVMStyleWithColumns(43)); + verifyFormat( + "static unsigned SomeValues[10][3] = {\n" + " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" + " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); + verifyFormat("static auto fields = new vector<string>{\n" + " \"aaaaaaaaaaaaa\",\n" + " \"aaaaaaaaaaaaa\",\n" + " \"aaaaaaaaaaaa\",\n" + " \"aaaaaaaaaaaaaa\",\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" + " \"aaaaaaaaaaaa\",\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" + "};"); + verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); + verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" + " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" + " 3, cccccccccccccccccccccc};", + getLLVMStyleWithColumns(60)); // Trailing commas. verifyFormat("vector<int> x = {\n" @@ -6003,15 +6424,21 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { " 1, 1, 1, 1,\n" " /**/ /**/};", getLLVMStyleWithColumns(39)); + + // Trailing comment in the first line. + verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" + " 1111111111, 2222222222, 33333333333, 4444444444, //\n" + " 111111111, 222222222, 3333333333, 444444444, //\n" + " 11111111, 22222222, 333333333, 44444444};"); + + // With nested lists, we should either format one item per line or all nested + // lists one on line. + // FIXME: For some nested lists, we can do better. verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" " {aaaaaaaaaaaaaaaaaaa},\n" " {aaaaaaaaaaaaaaaaaaaaa},\n" " {aaaaaaaaaaaaaaaaa}};", getLLVMStyleWithColumns(60)); - - // With nested lists, we should either format one item per line or all nested - // lists one one line. - // FIXME: For some nested lists, we can do better. verifyFormat( "SomeStruct my_struct_array = {\n" " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" @@ -6025,6 +6452,8 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { // No column layout should be used here. verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); + + verifyNoCrash("a<,"); } TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { @@ -6079,7 +6508,8 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); EXPECT_EQ("class C {\n" " A() : b(0) {}\n" - "};", format("class C{A():b(0){}};", NoColumnLimit)); + "};", + format("class C{A():b(0){}};", NoColumnLimit)); EXPECT_EQ("A()\n" " : b(0) {\n" "}", @@ -6157,6 +6587,8 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { verifyFormat("class __declspec(X) Z {\n} n;"); verifyFormat("class A##B##C {\n} n;"); verifyFormat("class alignas(16) Z {\n} n;"); + verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); + verifyFormat("class MACROA MACRO(X) Z {\n} n;"); // Redefinition from nested context: verifyFormat("class A::B::C {\n} n;"); @@ -6298,7 +6730,7 @@ TEST_F(FormatTest, BlockComments) { EXPECT_EQ("/*\n" "*\n" " * aaaaaa\n" - "*aaaaaa\n" + " * aaaaaa\n" "*/", format("/*\n" "*\n" @@ -6467,12 +6899,14 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); - EXPECT_EQ( - "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", - format( - "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); + EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " + "forAllCells:(BOOL)flag;", + format("- (void)sendAction:(SEL)aSelector to:(id)anObject " + "forAllCells:(BOOL)flag;")); // Very long objectiveC method declaration. + verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" + " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" " inRange:(NSRange)range\n" " outRange:(NSRange)out_range\n" @@ -6486,11 +6920,26 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { " outRange8:(NSRange)out_range8\n" " outRange9:(NSRange)out_range9;"); + // When the function name has to be wrapped. + FormatStyle Style = getLLVMStyle(); + Style.IndentWrappedFunctionNames = false; + verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" + "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" + " anotherName:(NSString)bbbbbbbbbbbbbb {\n" + "}", + Style); + Style.IndentWrappedFunctionNames = true; + verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" + " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" + " anotherName:(NSString)bbbbbbbbbbbbbb {\n" + "}", + Style); + verifyFormat("- (int)sum:(vector<int>)numbers;"); verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC // protocol lists (but not for template classes): - //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); + // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); verifyFormat("- (int (*)())foo:(int (*)())f;"); verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); @@ -6564,7 +7013,7 @@ TEST_F(FormatTest, FormatObjCInterface) { "+ (id)init;\n" "@end"); - verifyGoogleFormat("@interface Foo (HackStuff) <MyProtocol>\n" + verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" "+ (id)init;\n" "@end"); @@ -6606,7 +7055,7 @@ TEST_F(FormatTest, FormatObjCInterface) { FormatStyle OnePerLine = getGoogleStyle(); OnePerLine.BinPackParameters = false; - verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n" + verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" @@ -6770,9 +7219,9 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { verifyFormat("int a = &[foo bar:baz];"); verifyFormat("int a = *[foo bar:baz];"); // FIXME: Make casts work, without breaking f()[4]. - //verifyFormat("int a = (int)[foo bar:baz];"); - //verifyFormat("return (int)[foo bar:baz];"); - //verifyFormat("(void)[foo bar:baz];"); + // verifyFormat("int a = (int)[foo bar:baz];"); + // verifyFormat("return (int)[foo bar:baz];"); + // verifyFormat("(void)[foo bar:baz];"); verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); // Binary operators. @@ -6811,9 +7260,16 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { verifyFormat("return in[42];"); verifyFormat("for (auto v : in[1]) {\n}"); + verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); + verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); + verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); + verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); + verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" "}"); verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); + verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); + verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); @@ -6898,6 +7354,15 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { " fraction:1.0\n" " respectFlipped:NO\n" " hints:nil];"); + verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); + verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); + verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" + " aaaaaaaaaaaaaaaaaaaaaa];"); + verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" + " .aaaaaaaa];", // FIXME: Indentation seems off. + getLLVMStyleWithColumns(60)); verifyFormat( "scoped_nsobject<NSTextField> message(\n" @@ -6914,6 +7379,13 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); + // FIXME: This violates the column limit. + verifyFormat( + "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" + " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" + " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", + getLLVMStyleWithColumns(60)); + // Variadic parameters. verifyFormat( "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); @@ -7026,20 +7498,19 @@ TEST_F(FormatTest, ObjCDictLiterals) { "}"); verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); - verifyFormat("[self setDict:@{}"); - verifyFormat("[self setDict:@{@1 : @2}"); + verifyIncompleteFormat("[self setDict:@{}"); + verifyIncompleteFormat("[self setDict:@{@1 : @2}"); verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); verifyFormat( "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); verifyFormat( "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); - verifyFormat( - "NSDictionary *d = @{\n" - " @\"nam\" : NSUserNam(),\n" - " @\"dte\" : [NSDate date],\n" - " @\"processInfo\" : [NSProcessInfo processInfo]\n" - "};"); + verifyFormat("NSDictionary *d = @{\n" + " @\"nam\" : NSUserNam(),\n" + " @\"dte\" : [NSDate date],\n" + " @\"processInfo\" : [NSProcessInfo processInfo]\n" + "};"); verifyFormat( "@{\n" " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " @@ -7057,12 +7528,11 @@ TEST_F(FormatTest, ObjCDictLiterals) { "};"); // We should try to be robust in case someone forgets the "@". - verifyFormat( - "NSDictionary *d = {\n" - " @\"nam\" : NSUserNam(),\n" - " @\"dte\" : [NSDate date],\n" - " @\"processInfo\" : [NSProcessInfo processInfo]\n" - "};"); + verifyFormat("NSDictionary *d = {\n" + " @\"nam\" : NSUserNam(),\n" + " @\"dte\" : [NSDate date],\n" + " @\"processInfo\" : [NSProcessInfo processInfo]\n" + "};"); verifyFormat("NSMutableDictionary *dictionary =\n" " [NSMutableDictionary dictionaryWithDictionary:@{\n" " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" @@ -7072,7 +7542,7 @@ TEST_F(FormatTest, ObjCDictLiterals) { } TEST_F(FormatTest, ObjCArrayLiterals) { - verifyFormat("@["); + verifyIncompleteFormat("@["); verifyFormat("@[]"); verifyFormat( "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); @@ -7098,6 +7568,10 @@ TEST_F(FormatTest, ObjCArrayLiterals) { " @\"aaaaaaaaaaaaaaaaa\",\n" " @\"aaaaaaaaaaaaaaaaa\"\n" "];"); + verifyFormat("NSArray *array = @[\n" + " @\"a\",\n" + " @\"a\",\n" // Trailing comma -> one per line. + "];"); // We should try to be robust in case someone forgets the "@". verifyFormat("NSArray *some_variable = [\n" @@ -7282,11 +7756,11 @@ TEST_F(FormatTest, BreaksStringLiterals) { "loooooooooooooooooooong);", getLLVMStyleWithColumns(20))); - EXPECT_EQ("f(g(\"long string \"\n" - " \"literal\"),\n" - " b);", - format("f(g(\"long string literal\"), b);", - getLLVMStyleWithColumns(20))); + EXPECT_EQ( + "f(g(\"long string \"\n" + " \"literal\"),\n" + " b);", + format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); EXPECT_EQ("f(g(\"long string \"\n" " \"literal\",\n" " a),\n" @@ -7315,23 +7789,20 @@ TEST_F(FormatTest, BreaksStringLiterals) { " aaaaaaaaaaaaaaaaaaaa,\n" " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); - EXPECT_EQ( - "\"splitmea\"\n" - "\"trandomp\"\n" - "\"oint\"", - format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); + EXPECT_EQ("\"splitmea\"\n" + "\"trandomp\"\n" + "\"oint\"", + format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); - EXPECT_EQ( - "\"split/\"\n" - "\"pathat/\"\n" - "\"slashes\"", - format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); + EXPECT_EQ("\"split/\"\n" + "\"pathat/\"\n" + "\"slashes\"", + format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); - EXPECT_EQ( - "\"split/\"\n" - "\"pathat/\"\n" - "\"slashes\"", - format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); + EXPECT_EQ("\"split/\"\n" + "\"pathat/\"\n" + "\"slashes\"", + format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); EXPECT_EQ("\"split at \"\n" "\"spaces/at/\"\n" "\"slashes.at.any$\"\n" @@ -7376,12 +7847,11 @@ TEST_F(FormatTest, BreaksStringLiterals) { FormatStyle AlignLeft = getLLVMStyleWithColumns(12); AlignLeft.AlignEscapedNewlinesLeft = true; - EXPECT_EQ( - "#define A \\\n" - " \"some \" \\\n" - " \"text \" \\\n" - " \"other\";", - format("#define A \"some text other\";", AlignLeft)); + EXPECT_EQ("#define A \\\n" + " \"some \" \\\n" + " \"text \" \\\n" + " \"other\";", + format("#define A \"some text other\";", AlignLeft)); } TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { @@ -7413,6 +7883,12 @@ TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { EXPECT_EQ("@\"NSString \"\n" "@\"literal\";", format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); + + // This input makes clang-format try to split the incomplete unicode escape + // sequence, which used to lead to a crasher. + verifyNoCrash( + "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + getLLVMStyleWithColumns(60)); } TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { @@ -7447,6 +7923,22 @@ TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { EXPECT_EQ( "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); + EXPECT_EQ("f(\n" + "#if !TEST\n" + " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" + "#endif\n" + " );", + format("f(\n" + "#if !TEST\n" + "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" + "#endif\n" + ");")); + EXPECT_EQ("f(\n" + "\n" + " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", + format("f(\n" + "\n" + "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); } TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { @@ -7523,9 +8015,7 @@ TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { format("#define x(_a) printf(\"foo\"_a);", Style)); } -TEST_F(FormatTest, UnderstandsCpp1y) { - verifyFormat("int bi{1'000'000};"); -} +TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" @@ -7576,10 +8066,8 @@ TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { } TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { - EXPECT_EQ("\"\\a\"", - format("\"\\a\"", getLLVMStyleWithColumns(3))); - EXPECT_EQ("\"\\\"", - format("\"\\\"", getLLVMStyleWithColumns(2))); + EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); + EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); EXPECT_EQ("\"test\"\n" "\"\\n\"", format("\"test\\n\"", getLLVMStyleWithColumns(7))); @@ -7589,8 +8077,7 @@ TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { EXPECT_EQ("\"\\\\\\\\\"\n" "\"\\n\"", format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); - EXPECT_EQ("\"\\uff01\"", - format("\"\\uff01\"", getLLVMStyleWithColumns(7))); + EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); EXPECT_EQ("\"\\uff01\"\n" "\"test\"", format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); @@ -7880,25 +8367,25 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { Tab)); EXPECT_EQ("/* some\n" " comment */", - format(" \t \t /* some\n" - " \t \t comment */", - Tab)); + format(" \t \t /* some\n" + " \t \t comment */", + Tab)); EXPECT_EQ("int a; /* some\n" " comment */", - format(" \t \t int a; /* some\n" - " \t \t comment */", - Tab)); + format(" \t \t int a; /* some\n" + " \t \t comment */", + Tab)); EXPECT_EQ("int a; /* some\n" "comment */", - format(" \t \t int\ta; /* some\n" - " \t \t comment */", - Tab)); + format(" \t \t int\ta; /* some\n" + " \t \t comment */", + Tab)); EXPECT_EQ("f(\"\t\t\"); /* some\n" " comment */", - format(" \t \t f(\"\t\t\"); /* some\n" - " \t \t comment */", - Tab)); + format(" \t \t f(\"\t\t\"); /* some\n" + " \t \t comment */", + Tab)); EXPECT_EQ("{\n" " /*\n" " * Comment\n" @@ -7951,20 +8438,25 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; verifyFormat("while(true)\n" - " continue;", NoSpace); + " continue;", + NoSpace); verifyFormat("for(;;)\n" - " continue;", NoSpace); + " continue;", + NoSpace); verifyFormat("if(true)\n" " f();\n" "else if(true)\n" - " f();", NoSpace); + " f();", + NoSpace); verifyFormat("do {\n" " do_something();\n" - "} while(something());", NoSpace); + "} while(something());", + NoSpace); verifyFormat("switch(x) {\n" "default:\n" " break;\n" - "}", NoSpace); + "}", + NoSpace); verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); verifyFormat("size_t x = sizeof(x);", NoSpace); verifyFormat("auto f(int x) -> decltype(x);", NoSpace); @@ -7973,6 +8465,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { verifyFormat("size_t x = alignof(MyType);", NoSpace); verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); verifyFormat("int f() throw(Deprecated);", NoSpace); + verifyFormat("typedef void (*cb)(int);", NoSpace); FormatStyle Space = getLLVMStyle(); Space.SpaceBeforeParens = FormatStyle::SBPO_Always; @@ -8017,6 +8510,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { verifyFormat("size_t x = alignof (MyType);", Space); verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); verifyFormat("int f () throw (Deprecated);", Space); + verifyFormat("typedef void (*cb) (int);", Space); } TEST_F(FormatTest, ConfigurableSpacesInParentheses) { @@ -8024,21 +8518,28 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) { Spaces.SpacesInParentheses = true; verifyFormat("call( x, y, z );", Spaces); + verifyFormat("call();", Spaces); + verifyFormat("std::function<void( int, int )> callback;", Spaces); verifyFormat("while ( (bool)1 )\n" - " continue;", Spaces); + " continue;", + Spaces); verifyFormat("for ( ;; )\n" - " continue;", Spaces); + " continue;", + Spaces); verifyFormat("if ( true )\n" " f();\n" "else if ( true )\n" - " f();", Spaces); + " f();", + Spaces); verifyFormat("do {\n" " do_something( (int)i );\n" - "} while ( something() );", Spaces); + "} while ( something() );", + Spaces); verifyFormat("switch ( x ) {\n" "default:\n" " break;\n" - "}", Spaces); + "}", + Spaces); Spaces.SpacesInParentheses = false; Spaces.SpacesInCStyleCastParentheses = true; @@ -8050,37 +8551,38 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) { verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); verifyFormat("#define x (( int )-1)", Spaces); - Spaces.SpacesInParentheses = false; - Spaces.SpaceInEmptyParentheses = true; - verifyFormat("call(x, y, z);", Spaces); - verifyFormat("call( )", Spaces); - - // Run the first set of tests again with - // Spaces.SpacesInParentheses = false, - // Spaces.SpaceInEmptyParentheses = true and - // Spaces.SpacesInCStyleCastParentheses = true - Spaces.SpacesInParentheses = false, - Spaces.SpaceInEmptyParentheses = true; + // Run the first set of tests again with: + Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true; Spaces.SpacesInCStyleCastParentheses = true; verifyFormat("call(x, y, z);", Spaces); + verifyFormat("call( );", Spaces); + verifyFormat("std::function<void(int, int)> callback;", Spaces); verifyFormat("while (( bool )1)\n" - " continue;", Spaces); + " continue;", + Spaces); verifyFormat("for (;;)\n" - " continue;", Spaces); + " continue;", + Spaces); verifyFormat("if (true)\n" " f( );\n" "else if (true)\n" - " f( );", Spaces); + " f( );", + Spaces); verifyFormat("do {\n" " do_something(( int )i);\n" - "} while (something( ));", Spaces); + "} while (something( ));", + Spaces); verifyFormat("switch (x) {\n" "default:\n" " break;\n" - "}", Spaces); + "}", + Spaces); + // Run the first set of tests again with: Spaces.SpaceAfterCStyleCast = true; verifyFormat("call(x, y, z);", Spaces); + verifyFormat("call( );", Spaces); + verifyFormat("std::function<void(int, int)> callback;", Spaces); verifyFormat("while (( bool ) 1)\n" " continue;", Spaces); @@ -8101,6 +8603,8 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) { " break;\n" "}", Spaces); + + // Run subset of tests again with: Spaces.SpacesInCStyleCastParentheses = false; Spaces.SpaceAfterCStyleCast = true; verifyFormat("while ((bool) 1)\n" @@ -8146,6 +8650,152 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { verifyFormat("a or_eq 8;", Spaces); } +TEST_F(FormatTest, AlignConsecutiveAssignments) { + FormatStyle Alignment = getLLVMStyle(); + Alignment.AlignConsecutiveAssignments = false; + verifyFormat("int a = 5;\n" + "int oneTwoThree = 123;", + Alignment); + verifyFormat("int a = 5;\n" + "int oneTwoThree = 123;", + Alignment); + + Alignment.AlignConsecutiveAssignments = true; + verifyFormat("int a = 5;\n" + "int oneTwoThree = 123;", + Alignment); + verifyFormat("int a = method();\n" + "int oneTwoThree = 133;", + Alignment); + verifyFormat("a &= 5;\n" + "bcd *= 5;\n" + "ghtyf += 5;\n" + "dvfvdb -= 5;\n" + "a /= 5;\n" + "vdsvsv %= 5;\n" + "sfdbddfbdfbb ^= 5;\n" + "dvsdsv |= 5;\n" + "int dsvvdvsdvvv = 123;", + Alignment); + verifyFormat("int i = 1, j = 10;\n" + "something = 2000;", + Alignment); + verifyFormat("something = 2000;\n" + "int i = 1, j = 10;\n", + Alignment); + verifyFormat("something = 2000;\n" + "another = 911;\n" + "int i = 1, j = 10;\n" + "oneMore = 1;\n" + "i = 2;", + Alignment); + verifyFormat("int a = 5;\n" + "int one = 1;\n" + "method();\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + Alignment); + verifyFormat("int oneTwoThree = 123; // comment\n" + "int oneTwo = 12; // comment", + Alignment); + EXPECT_EQ("int a = 5;\n" + "\n" + "int oneTwoThree = 123;", + format("int a = 5;\n" + "\n" + "int oneTwoThree= 123;", + Alignment)); + EXPECT_EQ("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + format("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + Alignment)); + EXPECT_EQ("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + format("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + Alignment)); + Alignment.AlignEscapedNewlinesLeft = true; + verifyFormat("#define A \\\n" + " int aaaa = 12; \\\n" + " int b = 23; \\\n" + " int ccc = 234; \\\n" + " int dddddddddd = 2345;", + Alignment); + Alignment.AlignEscapedNewlinesLeft = false; + verifyFormat("#define A " + " \\\n" + " int aaaa = 12; " + " \\\n" + " int b = 23; " + " \\\n" + " int ccc = 234; " + " \\\n" + " int dddddddddd = 2345;", + Alignment); + verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " + "k = 4, int l = 5,\n" + " int m = 6) {\n" + " int j = 10;\n" + " otherThing = 1;\n" + "}", + Alignment); + verifyFormat("void SomeFunction(int parameter = 0) {\n" + " int i = 1;\n" + " int j = 2;\n" + " int big = 10000;\n" + "}", + Alignment); + verifyFormat("class C {\n" + "public:\n" + " int i = 1;\n" + " virtual void f() = 0;\n" + "};", + Alignment); + verifyFormat("int i = 1;\n" + "if (SomeType t = getSomething()) {\n" + "}\n" + "int j = 2;\n" + "int big = 10000;", + Alignment); + verifyFormat("int j = 7;\n" + "for (int k = 0; k < N; ++k) {\n" + "}\n" + "int j = 2;\n" + "int big = 10000;\n" + "}", + Alignment); + Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; + verifyFormat("int i = 1;\n" + "LooooooooooongType loooooooooooooooooooooongVariable\n" + " = someLooooooooooooooooongFunction();\n" + "int j = 2;", + Alignment); + Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; + verifyFormat("int i = 1;\n" + "LooooooooooongType loooooooooooooooooooooongVariable =\n" + " someLooooooooooooooooongFunction();\n" + "int j = 2;", + Alignment); + // FIXME: Should align all three assignments + verifyFormat( + "int i = 1;\n" + "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" + " loooooooooooooooooooooongParameterB);\n" + "int j = 2;", + Alignment); +} + TEST_F(FormatTest, LinuxBraceBreaking) { FormatStyle LinuxBraceStyle = getLLVMStyle(); LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; @@ -8575,11 +9225,16 @@ TEST_F(FormatTest, UnderstandsPragmas) { "(including parentheses).")); } +TEST_F(FormatTest, UnderstandPragmaOption) { + verifyFormat("#pragma option -C -A"); + + EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); +} + #define EXPECT_ALL_STYLES_EQUAL(Styles) \ for (size_t i = 1; i < Styles.size(); ++i) \ - EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \ - << Styles.size() \ - << " differs from Style #0" + EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ + << " differs from Style #0" TEST_F(FormatTest, GetsPredefinedStyleByName) { SmallVector<FormatStyle, 3> Styles; @@ -8685,6 +9340,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); CHECK_PARSE_BOOL(AlignOperands); CHECK_PARSE_BOOL(AlignTrailingComments); + CHECK_PARSE_BOOL(AlignConsecutiveAssignments); CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); @@ -8953,11 +9609,12 @@ TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { Style.BreakBeforeTernaryOperators = true; EXPECT_EQ(0, parseConfiguration("---\n" - "BasedOnStyle: Google\n" - "---\n" - "Language: JavaScript\n" - "IndentWidth: 76\n" - "...\n", &Style).value()); + "BasedOnStyle: Google\n" + "---\n" + "Language: JavaScript\n" + "IndentWidth: 76\n" + "...\n", + &Style).value()); EXPECT_FALSE(Style.BreakBeforeTernaryOperators); EXPECT_EQ(76u, Style.IndentWidth); EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); @@ -9001,8 +9658,7 @@ TEST_F(FormatTest, CountsUTF8CharactersProperly) { getLLVMStyleWithColumns(31)); verifyFormat("// Однажды в студёную зимнюю пору...", getLLVMStyleWithColumns(36)); - verifyFormat("// 一 二 三 四 五 六 七 八 九 十", - getLLVMStyleWithColumns(32)); + verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); verifyFormat("/* Однажды в студёную зимнюю пору... */", getLLVMStyleWithColumns(39)); verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", @@ -9020,19 +9676,18 @@ TEST_F(FormatTest, SplitsUTF8Strings) { EXPECT_EQ("\"aaaaaaaÄ\"\n" "\"\xc2\x8d\";", format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); + EXPECT_EQ("\"Однажды, в \"\n" + "\"студёную \"\n" + "\"зимнюю \"\n" + "\"пору,\"", + format("\"Однажды, в студёную зимнюю пору,\"", + getLLVMStyleWithColumns(13))); EXPECT_EQ( - "\"Однажды, в \"\n" - "\"студёную \"\n" - "\"зимнюю \"\n" - "\"пору,\"", - format("\"Однажды, в студёную зимнюю пору,\"", - getLLVMStyleWithColumns(13))); - EXPECT_EQ("\"一 二 三 \"\n" - "\"四 五六 \"\n" - "\"七 八 九 \"\n" - "\"十\"", - format("\"一 二 三 四 五六 七 八 九 十\"", - getLLVMStyleWithColumns(11))); + "\"一 二 三 \"\n" + "\"四 五六 \"\n" + "\"七 八 九 \"\n" + "\"十\"", + format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); EXPECT_EQ("\"一\t二 \"\n" "\"\t三 \"\n" "\"四 五\t六 \"\n" @@ -9042,7 +9697,6 @@ TEST_F(FormatTest, SplitsUTF8Strings) { getLLVMStyleWithColumns(11))); } - TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { EXPECT_EQ("const char *sssss =\n" " \"一二三四五六七八\\\n" @@ -9225,22 +9879,20 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) { Style); // Wrap before binary operators. - EXPECT_EQ( - "void f()\n" - "{\n" - " if (aaaaaaaaaaaaaaaa\n" - " && bbbbbbbbbbbbbbbbbbbbbbbb\n" - " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" - " return;\n" - "}", - format( - "void f() {\n" - "if (aaaaaaaaaaaaaaaa\n" - "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" - "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" - "return;\n" - "}", - Style)); + EXPECT_EQ("void f()\n" + "{\n" + " if (aaaaaaaaaaaaaaaa\n" + " && bbbbbbbbbbbbbbbbbbbbbbbb\n" + " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" + " return;\n" + "}", + format("void f() {\n" + "if (aaaaaaaaaaaaaaaa\n" + "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" + "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" + "return;\n" + "}", + Style)); // Allow functions on a single line. verifyFormat("void f() { return; }", Style); @@ -9270,7 +9922,8 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) { " , b(b)\n" " , c(c)\n" "{\n" - "}", Style); + "}", + Style); verifyFormat("SomeClass::Constructor()\n" " : a(a)\n" "{\n" @@ -9395,6 +10048,17 @@ TEST_F(FormatTest, FormatsLambdas) { " : Field([] { // comment\n" " int i;\n" " }) {}"); + verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" + " return some_parameter.size();\n" + "};"); + verifyFormat("int i = aaaaaa ? 1 //\n" + " : [] {\n" + " return 2; //\n" + " }();"); + verifyFormat("llvm::errs() << \"number of twos is \"\n" + " << std::count_if(v.begin(), v.end(), [](int x) {\n" + " return x == 2; // force break\n" + " });"); // Lambdas with return types. verifyFormat("int c = []() -> int { return 2; }();\n"); @@ -9408,6 +10072,12 @@ TEST_F(FormatTest, FormatsLambdas) { " int j) -> int {\n" " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" "};"); + verifyFormat( + "aaaaaaaaaaaaaaaaaaaaaa(\n" + " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" + " return aaaaaaaaaaaaaaaaa;\n" + " });", + getLLVMStyleWithColumns(70)); // Multiple lambdas in the same parentheses change indentation rules. verifyFormat("SomeFunction(\n" @@ -9447,6 +10117,13 @@ TEST_F(FormatTest, FormatsLambdas) { " doo_dah();\n" " })) {\n" "}"); + verifyFormat("auto lambda = []() {\n" + " int a = 2\n" + "#if A\n" + " + 2\n" + "#endif\n" + " ;\n" + "};"); } TEST_F(FormatTest, FormatsBlocks) { @@ -9550,6 +10227,74 @@ TEST_F(FormatTest, FormatsBlocks) { FourIndent); } +TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { + FormatStyle ZeroColumn = getLLVMStyle(); + ZeroColumn.ColumnLimit = 0; + + verifyFormat("[[SessionService sharedService] " + "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" + " if (window) {\n" + " [self windowDidLoad:window];\n" + " } else {\n" + " [self errorLoadingWindow];\n" + " }\n" + "}];", + ZeroColumn); + EXPECT_EQ("[[SessionService sharedService]\n" + " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" + " if (window) {\n" + " [self windowDidLoad:window];\n" + " } else {\n" + " [self errorLoadingWindow];\n" + " }\n" + " }];", + format("[[SessionService sharedService]\n" + "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" + " if (window) {\n" + " [self windowDidLoad:window];\n" + " } else {\n" + " [self errorLoadingWindow];\n" + " }\n" + "}];", + ZeroColumn)); + verifyFormat("[myObject doSomethingWith:arg1\n" + " firstBlock:^(Foo *a) {\n" + " // ...\n" + " int i;\n" + " }\n" + " secondBlock:^(Bar *b) {\n" + " // ...\n" + " int i;\n" + " }\n" + " thirdBlock:^Foo(Bar *b) {\n" + " // ...\n" + " int i;\n" + " }];", + ZeroColumn); + verifyFormat("f(^{\n" + " @autoreleasepool {\n" + " if (a) {\n" + " g();\n" + " }\n" + " }\n" + "});", + ZeroColumn); + verifyFormat("void (^largeBlock)(void) = ^{\n" + " // ...\n" + "};", + ZeroColumn); + + ZeroColumn.AllowShortBlocksOnASingleLine = true; + EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", + format("void (^largeBlock)(void) = ^{ int i; };", + ZeroColumn)); + ZeroColumn.AllowShortBlocksOnASingleLine = false; + EXPECT_EQ("void (^largeBlock)(void) = ^{\n" + " int i;\n" + "};", + format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); +} + TEST_F(FormatTest, SupportsCRLF) { EXPECT_EQ("int a;\r\n" "int b;\r\n" @@ -9646,6 +10391,27 @@ TEST_F(FormatTest, SpacesInAngles) { verifyFormat("A<A<int>>();", Spaces); } +TEST_F(FormatTest, TripleAngleBrackets) { + verifyFormat("f<<<1, 1>>>();"); + verifyFormat("f<<<1, 1, 1, s>>>();"); + verifyFormat("f<<<a, b, c, d>>>();"); + EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); + verifyFormat("f<param><<<1, 1>>>();"); + verifyFormat("f<1><<<1, 1>>>();"); + EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaa<<<\n 1, 1>>>();"); +} + +TEST_F(FormatTest, MergeLessLessAtEnd) { + verifyFormat("<<"); + EXPECT_EQ("< < <", format("\\\n<<<")); + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaallvm::outs() <<"); + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaallvm::outs()\n <<"); +} + TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { std::string code = "#if A\n" "#if B\n" @@ -9775,9 +10541,7 @@ TEST_F(FormatTest, DisableRegions) { " int k;")); } -TEST_F(FormatTest, DoNotCrashOnInvalidInput) { - format("? ) ="); -} +TEST_F(FormatTest, DoNotCrashOnInvalidInput) { format("? ) ="); } } // end namespace tooling } // end namespace clang |