diff options
author | dim <dim@FreeBSD.org> | 2013-12-22 00:07:40 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2013-12-22 00:07:40 +0000 |
commit | 952eddef9aff85b1e92626e89baaf7a360e2ac85 (patch) | |
tree | df8df0b0067b381eab470a3b8f28d14a552a6340 /unittests/Format/FormatTest.cpp | |
parent | ea266cad53e3d49771fa38103913d3ec7a166694 (diff) | |
download | FreeBSD-src-952eddef9aff85b1e92626e89baaf7a360e2ac85.zip FreeBSD-src-952eddef9aff85b1e92626e89baaf7a360e2ac85.tar.gz |
Vendor import of clang release_34 branch r197841 (effectively, 3.4 RC3):
https://llvm.org/svn/llvm-project/cfe/branches/release_34@197841
Diffstat (limited to 'unittests/Format/FormatTest.cpp')
-rw-r--r-- | unittests/Format/FormatTest.cpp | 3984 |
1 files changed, 3691 insertions, 293 deletions
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 4c948e8..b6574c7 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -10,7 +10,6 @@ #define DEBUG_TYPE "format-test" #include "clang/Format/Format.h" -#include "../Tooling/RewriterTestContext.h" #include "clang/Lex/Lexer.h" #include "llvm/Support/Debug.h" #include "gtest/gtest.h" @@ -23,21 +22,14 @@ protected: std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, const FormatStyle &Style) { DEBUG(llvm::errs() << "---\n"); - RewriterTestContext Context; - FileID ID = Context.createInMemoryFile("input.cc", Code); - SourceLocation Start = - Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); - std::vector<CharSourceRange> Ranges( - 1, - CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); - Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, - getFormattingLangOpts()); - tooling::Replacements Replace = reformat( - Style, Lex, Context.Sources, Ranges, new IgnoringDiagConsumer()); - ReplacementCount = Replace.size(); - EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); - DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n"); - return Context.getRewrittenText(ID); + DEBUG(llvm::errs() << Code << "\n\n"); + std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); + tooling::Replacements Replaces = reformat(Style, Code, Ranges); + ReplacementCount = Replaces.size(); + std::string Result = applyAllReplacements(Code, Replaces); + EXPECT_NE("", Result); + DEBUG(llvm::errs() << "\n" << Result << "\n\n"); + return Result; } std::string @@ -75,7 +67,14 @@ protected: JustReplacedNewline = false; } } - return MessedUp; + std::string WithoutWhitespace; + if (MessedUp[0] != ' ') + WithoutWhitespace.push_back(MessedUp[0]); + for (unsigned i = 1, e = MessedUp.size(); i != e; ++i) { + if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ') + WithoutWhitespace.push_back(MessedUp[i]); + } + return WithoutWhitespace; } FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { @@ -112,20 +111,20 @@ TEST_F(FormatTest, MessUp) { EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); - EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); + EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); } //===----------------------------------------------------------------------===// // Basic function tests. //===----------------------------------------------------------------------===// -TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { +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 \r 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;")); } @@ -147,6 +146,7 @@ TEST_F(FormatTest, FormatsNestedCall) { TEST_F(FormatTest, NestedNameSpecifiers) { verifyFormat("vector< ::Type> v;"); verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); + verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); } TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { @@ -201,6 +201,53 @@ TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { format("int a;\n \n\n int b;", 9, 0, getLLVMStyle())); } +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 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, ReformatsMovedLines) { EXPECT_EQ( "template <typename T> T *getFETokenInfo() const {\n" @@ -218,25 +265,31 @@ TEST_F(FormatTest, ReformatsMovedLines) { // Tests for control statements. //===----------------------------------------------------------------------===// -TEST_F(FormatTest, FormatIfWithoutCompountStatement) { +TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { verifyFormat("if (true)\n f();\ng();"); verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); - FormatStyle AllowsMergedIf = getGoogleStyle(); + 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" + 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" + verifyFormat("if (a) { // Never merge this\n" " f();\n" "}", AllowsMergedIf); @@ -245,6 +298,10 @@ TEST_F(FormatTest, FormatIfWithoutCompountStatement) { "}", AllowsMergedIf); + EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf)); + EXPECT_EQ("if (a) return; // comment", + format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf)); + AllowsMergedIf.ColumnLimit = 14; verifyFormat("if (a) return;", AllowsMergedIf); verifyFormat("if (aaaaaaaaa)\n" @@ -255,6 +312,29 @@ TEST_F(FormatTest, FormatIfWithoutCompountStatement) { 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, ParseIfElse) { verifyFormat("if (true)\n" " if (true)\n" @@ -279,6 +359,11 @@ TEST_F(FormatTest, ParseIfElse) { "else {\n" " i();\n" "}"); + verifyFormat("void f() {\n" + " if (a) {\n" + " } else {\n" + " }\n" + "}"); } TEST_F(FormatTest, ElseIf) { @@ -289,6 +374,13 @@ TEST_F(FormatTest, ElseIf) { " g();\n" "else\n" " h();"); + verifyFormat("if (a) {\n" + " f();\n" + "}\n" + "// or else ..\n" + "else {\n" + " g()\n" + "}"); } TEST_F(FormatTest, FormatsForLoop) { @@ -453,6 +545,10 @@ TEST_F(FormatTest, FormatsSwitchStatement) { " 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)); verifyGoogleFormat("switch (x) {\n" " case 1:\n" @@ -473,7 +569,40 @@ TEST_F(FormatTest, FormatsSwitchStatement) { " }\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" + "});"); +} + +TEST_F(FormatTest, CaseRanges) { + verifyFormat("switch (x) {\n" + "case 'A' ... 'Z':\n" + "case 1 ... 5:\n" + " break;\n" + "}"); } TEST_F(FormatTest, FormatsLabels) { @@ -505,6 +634,9 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { verifyFormat("void f() {\n" " // Doesn't do anything\n" "}"); + verifyFormat("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)"); @@ -543,6 +675,11 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { "#include \"a/b/c\" // comment"); verifyFormat("#include <a> // comment\n" "#include <a/b/c> // 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" @@ -571,14 +708,17 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { format("void f() { // This does something ..\n" " }\n" "int a; // This is unrelated")); - EXPECT_EQ("void f() { // This does something ..\n" - "} // awesome..\n" + EXPECT_EQ("class C {\n" + " void f() { // This does something ..\n" + " } // awesome..\n" "\n" - "int a; // This is unrelated", - format("void f() { // This does something ..\n" + " int a; // This is unrelated\n" + "};", + format("class C{void f() { // This does something ..\n" " } // awesome..\n" " \n" - "int a; // This is unrelated")); + "int a; // This is unrelated\n" + "};")); EXPECT_EQ("int i; // single line trailing comment", format("int i;\\\n// single line trailing comment")); @@ -598,7 +738,7 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { "};"); verifyGoogleFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaa); // 81 cols with this comment"); + " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); EXPECT_EQ("D(a, {\n" " // test\n" " int a;\n" @@ -607,6 +747,66 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { "// 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();")); } TEST_F(FormatTest, CanFormatCommentsLocally) { @@ -636,10 +836,12 @@ TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 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, UnderstandsMultiLineComments) { - verifyFormat("f(/*test=*/ true);"); +TEST_F(FormatTest, UnderstandsBlockComments) { + verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); EXPECT_EQ( "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" " bbbbbbbbbbbbbbbbbbbbbbbbb);", @@ -650,6 +852,15 @@ TEST_F(FormatTest, UnderstandsMultiLineComments) { " /* 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; @@ -660,7 +871,7 @@ TEST_F(FormatTest, UnderstandsMultiLineComments) { NoBinPacking); } -TEST_F(FormatTest, AlignsMultiLineComments) { +TEST_F(FormatTest, AlignsBlockComments) { EXPECT_EQ("/*\n" " * Really multi-line\n" " * comment.\n" @@ -696,11 +907,49 @@ TEST_F(FormatTest, AlignsMultiLineComments) { " 1.1.1. to keep the formatting.\n" " */")); EXPECT_EQ("/*\n" - " Don't try to outdent if there's not enough inentation.\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 inentation.\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. */")); +} + +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\n" + "ffffffffff(int aaaaa /* test */);", + format("void ffffffffff(int aaaaa /* test */);", + getLLVMStyleWithColumns(35))); } TEST_F(FormatTest, SplitsLongCxxComments) { @@ -727,10 +976,20 @@ TEST_F(FormatTest, SplitsLongCxxComments) { EXPECT_EQ("// Don't_touch_leading_whitespace", format("// Don't_touch_leading_whitespace", getLLVMStyleWithColumns(20))); - EXPECT_EQ( - "//Don't add leading\n" - "//whitespace", - format("//Don't add leading whitespace", getLLVMStyleWithColumns(20))); + EXPECT_EQ("// 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" @@ -738,6 +997,106 @@ TEST_F(FormatTest, SplitsLongCxxComments) { format("// A comment before a macro definition\n" "#define a b", getLLVMStyleWithColumns(20))); + EXPECT_EQ("void ffffff(int aaaaaaaaa, // wwww\n" + " int a, int bbb, // xxxxxxx\n" + " // yyyyyyyyy\n" + " int c, int d, int e) {}", + format("void ffffff(\n" + " int aaaaaaaaa, // wwww\n" + " int a,\n" + " int bbb, // xxxxxxx yyyyyyyyy\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, 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("int\n" + "a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" + " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" + " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" + " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" + " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + getLLVMStyleWithColumns(49))); +} + +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(&xxx, // aaaaaaaaaaaa\n" + " // 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) { @@ -858,7 +1217,8 @@ TEST_F(FormatTest, SplitsLongLinesInComments) { " */", getLLVMStyleWithColumns(20))); EXPECT_EQ("{\n" " if (something) /* This is a\n" - "long comment */\n" + " long\n" + " comment */\n" " ;\n" "}", format("{\n" @@ -866,6 +1226,48 @@ TEST_F(FormatTest, SplitsLongLinesInComments) { " ;\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" + "\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) { @@ -923,11 +1325,11 @@ TEST_F(FormatTest, CommentsInStaticInitializers) { " // 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 };"); + "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" @@ -953,11 +1355,20 @@ TEST_F(FormatTest, CommentsInStaticInitializers) { " // Comment after empty line\n" " b\n" "}")); - EXPECT_EQ("S s = { a, b };", format("S s = {\n" - " a,\n" - "\n" - " b\n" - "};")); + EXPECT_EQ("S s = {\n" + " /* Some comment */\n" + " a,\n" + "\n" + " /* Comment after empty line */\n" + " b\n" + "}", + 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" @@ -965,12 +1376,150 @@ TEST_F(FormatTest, CommentsInStaticInitializers) { "};"); } +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 {\n};"); + verifyFormat("class A {};"); } TEST_F(FormatTest, UnderstandsAccessSpecifiers) { @@ -1009,34 +1558,49 @@ TEST_F(FormatTest, SeparatesLogicalBlocks) { "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" + "};")); } TEST_F(FormatTest, FormatsClasses) { - verifyFormat("class A : public B {\n};"); - verifyFormat("class A : public ::B {\n};"); + verifyFormat("class A : public B {};"); + verifyFormat("class A : public ::B {};"); verifyFormat( "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" - " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" - "};\n"); + " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" - " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" - "};\n"); + " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); verifyFormat( - "class A : public B, public C, public D, public E, public F, public G {\n" - "};"); + "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 {\n" - "};"); + " public G {};"); verifyFormat("class\n" - " ReallyReallyLongClassName {\n};", + " ReallyReallyLongClassName {\n" + " int i;\n" + "};", getLLVMStyleWithColumns(32)); + verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" + " aaaaaaaaaaaaaaaa> {};"); + verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" + " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaa> {};"); } TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { @@ -1054,14 +1618,82 @@ TEST_F(FormatTest, FormatsEnum) { " Four = (Zero && (One ^ Two)) | (One << Two),\n" " Five = (One, Two, Three, Four, 5)\n" "};"); - verifyFormat("enum Enum {\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 X f() {\n a();\n return 42;\n}"); verifyFormat("enum {\n" + " Bar = Foo<int, int>::value\n" + "};"); +} + +TEST_F(FormatTest, FormatsEnumsWithErrors) { + verifyFormat("enum Type {\n" + " One = 0;\n" // These semicolons should be commas. + " Two = 1;\n" + "};"); + verifyFormat("namespace n {\n" + "enum Type {\n" + " One,\n" + " Two,\n" // missing }; + " 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,\n" + " B\n" + "};"); + verifyFormat("enum X : std::uint32_t {\n" + " A,\n" + " B\n" "};"); - verifyFormat("enum X E {\n} d;"); - verifyFormat("enum __attribute__((...)) E {\n} d;"); - verifyFormat("enum __declspec__((...)) E {\n} d;"); - verifyFormat("enum X f() {\n a();\n return 42;\n}"); } TEST_F(FormatTest, FormatsBitfields) { @@ -1069,33 +1701,83 @@ TEST_F(FormatTest, FormatsBitfields) { " unsigned sClass : 8;\n" " unsigned ValueKind : 2;\n" "};"); + verifyFormat("struct A {\n" + " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" + " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" + "};"); } TEST_F(FormatTest, FormatsNamespaces) { verifyFormat("namespace some_namespace {\n" - "class A {\n};\n" + "class A {};\n" "void f() { f(); }\n" "}"); verifyFormat("namespace {\n" - "class A {\n};\n" + "class A {};\n" "void f() { f(); }\n" "}"); verifyFormat("inline namespace X {\n" - "class A {\n};\n" + "class A {};\n" "void f() { f(); }\n" "}"); verifyFormat("using namespace some_namespace;\n" - "class A {\n};\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 undesireable. - verifyFormat("namespace {\n};"); + verifyFormat("namespace {};"); verifyFormat("namespace {\n" - "class A {\n" - "};\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")); + + 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;"); } @@ -1106,7 +1788,7 @@ TEST_F(FormatTest, FormatsInlineASM) { "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" " \"cpuid\\n\\t\"\n" " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" - " : \"=a\" (*rEAX), \"=S\" (*rEBX), \"=c\" (*rECX), \"=d\" (*rEDX)\n" + " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" " : \"a\"(value));"); } @@ -1152,22 +1834,46 @@ TEST_F(FormatTest, FormatObjCTryCatch) { TEST_F(FormatTest, StaticInitializers) { verifyFormat("static SomeClass SC = { 1, 'a' };"); - // FIXME: Format like enums if the static initializer does not fit on a line. verifyFormat( "static SomeClass WithALoooooooooooooooooooongName = {\n" " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" "};"); - verifyFormat( - "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" - " looooooooooooooooooooooooooooooooooongname,\n" - " looooooooooooooooooooooooooooooong };"); - // Allow bin-packing in static initializers as this would often lead to - // terrible results, e.g.: - verifyGoogleFormat( - "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" - " looooooooooooooooooooooooooooooooooongname,\n" - " looooooooooooooooooooooooooooooong };"); + // Here, everything other than the "}" would fit on a line. + verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" + " 100000000000000000000000\n" + "};"); + EXPECT_EQ("S s = { a, 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 }\n" + "};", + 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\n" + "};"); + + verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); } TEST_F(FormatTest, NestedStaticInitializers) { @@ -1180,11 +1886,10 @@ TEST_F(FormatTest, NestedStaticInitializers) { " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" "};"); - verifyGoogleFormat("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 }\n" - "};"); + 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" @@ -1202,15 +1907,21 @@ TEST_F(FormatTest, NestedStaticInitializers) { " 222222222222222222222222222222,\n" " 333333333333333333333333333333 } },\n" " { { 1, 2, 3 } }, { { 1, 2, 3 } } };"); + verifyGoogleFormat( + "SomeArrayOfSomeType a = {\n" + " {{1, 2, 3}}, {{1, 2, 3}},\n" + " {{111111111111111111111111111111, 222222222222222222222222222222,\n" + " 333333333333333333333333333333}},\n" + " {{1, 2, 3}}, {{1, 2, 3}}};"); - // FIXME: We might at some point want to handle this similar to parameter - // lists, where we have an option to put each on a single line. verifyFormat( "struct {\n" " unsigned bit;\n" " const char *const name;\n" - "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n" - " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };"); + "} kBitsToOs[] = { { kOsMac, \"Mac\" },\n" + " { kOsWin, \"Windows\" },\n" + " { kOsLinux, \"Linux\" },\n" + " { kOsCrOS, \"Chrome OS\" } };"); } TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { @@ -1220,14 +1931,28 @@ TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { } TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { - verifyFormat( - "virtual void write(ELFWriter *writerrr,\n" - " OwningPtr<FileOutputBuffer> &buffer) = 0;"); + verifyFormat("virtual void write(ELFWriter *writerrr,\n" + " OwningPtr<FileOutputBuffer> &buffer) = 0;"); } -TEST_F(FormatTest, LayoutUnknownPPDirective) { - EXPECT_EQ("#123 \"A string literal\"", +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;"); } @@ -1245,12 +1970,22 @@ TEST_F(FormatTest, EndOfFileEndsPPDirective) { 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\\\"\");"); +} + 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 AA( \\\n B)", getLLVMStyleWithColumns(12)); verifyFormat("#define A A\n#define A A"); verifyFormat("#define A(X) A\n#define A A"); @@ -1289,9 +2024,29 @@ TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } -TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { - EXPECT_EQ("# define A\\\n b;", - format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); +TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) { + EXPECT_EQ("int i;\n" + "#define A \\\n" + " int i; \\\n" + " int j\n" + "int k;", + format("int i;\n" + "#define A \\\n" + " int i ; \\\n" + " int j\n" + "int k;", + 8, 0, getGoogleStyle())); // 8: position of "#define". + EXPECT_EQ("int i;\n" + "#define A \\\n" + " int i; \\\n" + " int j\n" + "int k;", + format("int i;\n" + "#define A \\\n" + " int i ; \\\n" + " int j\n" + "int k;", + 45, 0, getGoogleStyle())); // 45: position of "j". } TEST_F(FormatTest, MacroDefinitionInsideStatement) { @@ -1302,10 +2057,11 @@ TEST_F(FormatTest, MacroDefinitionInsideStatement) { } 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" + verifyFormat("#define A \\\n" + " { \\\n" + " f(#c); \\\n" " }", getLLVMStyleWithColumns(11)); @@ -1351,24 +2107,59 @@ TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { verifyFormat("#define A :"); - - // FIXME: Improve formatting of case labels in macros. verifyFormat("#define SOMECASES \\\n" " case 1: \\\n" " case 2\n", getLLVMStyleWithColumns(20)); - verifyFormat("#define A template <typename T>"); verifyFormat("#define STR(x) #x\n" "f(STR(this_is_a_string_literal{));"); + verifyFormat("#pragma omp threadprivate( \\\n" + " y)), // expected-warning", + getLLVMStyleWithColumns(28)); +} + +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("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" + "\n" + " A() {\n}\n" + "} ;")); } TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" - "class X {\n" - "};\n" + "class X {};\n" "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" "int *createScopDetectionPass() { return 0; }", format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" @@ -1389,6 +2180,8 @@ TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { " IPC_MESSAGE_HANDLER(xxx, qqq)\n" " IPC_END_MESSAGE_MAP()\n" "}")); + + // These must not be recognized as macros. EXPECT_EQ("int q() {\n" " f(x);\n" " f(x) {}\n" @@ -1472,6 +2265,13 @@ TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { "};")); } +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}}")); } @@ -1487,10 +2287,15 @@ TEST_F(FormatTest, FormatUnbalancedStructuralElements) { format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); } -TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { +TEST_F(FormatTest, EscapedNewlineAtStartOfToken) { EXPECT_EQ( "#define A \\\n int i; \\\n int j;", format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); + EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); +} + +TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) { + EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); } TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { @@ -1521,7 +2326,7 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { EXPECT_EQ("int\n" "#define A\n" " a;", - format("int\n#define A\na;")); + format("int\n#define A\na;", getGoogleStyle())); verifyFormat("functionCallTo(\n" " someOtherFunction(\n" " withSomeParameters, whichInSequence,\n" @@ -1532,13 +2337,106 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { " 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"); +} + +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) { + 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" + " },\n" + " aaaa, bbbb, cccc);", + format(" functionCall ( {int i;int j;}, aaaa, bbbb, 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 ( {int i;} );")); + format(" functionCall (aaaa, bbbb, {int i;int j;});")); + EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });", + format(" functionCall (aaaa, bbbb, {int i;});")); + verifyFormat( + "Aaa({\n" + " int i; // break\n" + " },\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" + " ccccccccccccccccc));"); + verifyFormat("DEBUG({\n" + " if (a)\n" + " f();\n" + "});"); } TEST_F(FormatTest, LayoutBlockInsideStatement) { @@ -1556,15 +2454,87 @@ TEST_F(FormatTest, LayoutNestedBlocks) { " 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)); + 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" + " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" + " return;\n" + " },\n" + " a);", Style); + + EXPECT_EQ("Debug({\n" + " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" + " return;\n" + " },\n" + " a);", + format("Debug({\n" + " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" + " return;\n" + " },\n" + " a);", + 50, 1, getLLVMStyle())); +} + +TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) { + EXPECT_EQ("DEBUG({\n" + " int i;\n" + " int j;\n" + "});", + format("DEBUG( {\n" + " int i;\n" + " int j;\n" + "} ) ;", + 40, 1, getLLVMStyle())); } TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { EXPECT_EQ("{}", format("{}")); - - // Negative test for enum. - verifyFormat("enum E {\n};"); - - // Note that when there's a missing ';', we still join... + verifyFormat("enum E {};"); verifyFormat("enum E {}"); } @@ -1572,30 +2542,23 @@ TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { // Line break tests. //===----------------------------------------------------------------------===// -TEST_F(FormatTest, FormatsFunctionDefinition) { - verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," - " int h, int j, int f,\n" - " int c, int ddddddddddddd) {}"); -} - -TEST_F(FormatTest, FormatsAwesomeMethodCall) { - verifyFormat( - "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" - " parameter, parameter, parameter)),\n" - " SecondLongCall(parameter));"); -} - 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];"); + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " [aaaaaaaaaaaaaaaaaaaaaaaa\n" + " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" + " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" " aaaaaaaaaaaaaaaaaaaaaaaa<\n" @@ -1606,6 +2569,75 @@ TEST_F(FormatTest, PreventConfusingIndents) { " ddd);"); } +TEST_F(FormatTest, LineBreakingInBinaryExpressions) { + verifyFormat( + "bool aaaaaaa =\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" + " bbbbbbbb();"); + verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" + " ccccccccc == ddddddddddd;"); + + verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" + " aaaaaa) &&\n" + " bbbbbb && cccccc;"); + verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" + " aaaaaa) >>\n" + " bbbbbb;"); + verifyFormat("Whitespaces.addUntouchableComment(\n" + " SourceMgr.getSpellingColumnNumber(\n" + " TheLine.Last->FormatTok.Tok.getLocation()) -\n" + " 1);"); + + verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" + " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" + " cccccc) {\n}"); + + // 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" @@ -1628,6 +2660,66 @@ TEST_F(FormatTest, ExpressionIndentation) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); + verifyFormat("if () {\n" + "} else if (aaaaa && 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 = true; + 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 && bbbbb // break\n" + " > ccccc) {\n" + "}", + 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)"); } TEST_F(FormatTest, ConstructorInitializers) { @@ -1667,6 +2759,13 @@ TEST_F(FormatTest, ConstructorInitializers) { 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 desireable. @@ -1723,6 +2822,53 @@ TEST_F(FormatTest, MemoizationTests) { " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" " aaaaa())))))))))))))))))))))))))))))))))))))));", getLLVMStyleWithColumns(65)); + verifyFormat( + "aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa(\n" + " aaaaa,\n" + " aaaaa))))))))))));", + getLLVMStyleWithColumns(65)); + 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(); @@ -1753,22 +2899,29 @@ 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) {}"); // 2) break after return type. verifyFormat( - "Aaaaaaaaaaaaaaaaaaaaaaaa\n" - " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);"); + "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", + getGoogleStyle()); // 3) break after (. verifyFormat( "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" - " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);"); + " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", + getGoogleStyle()); // 4) break before after nested name specifiers. verifyFormat( "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " SomeClasssssssssssssssssssssssssssssssssssssss::\n" - " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);"); + " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", + getGoogleStyle()); // However, there are exceptions, if a sufficient amount of lines can be // saved. @@ -1780,10 +2933,11 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { " Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc);"); verifyFormat( - "Aaaaaaaaaaaaaaaaaa\n" + "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" - " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); + " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", + getGoogleStyle()); verifyFormat( "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc,\n" @@ -1803,6 +2957,85 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " bbbb bbbb);"); + + // Treat overloaded operators like other functions. + verifyFormat("SomeLoooooooooooooooooooooooooogType\n" + "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); + verifyFormat("SomeLoooooooooooooooooooooooooogType\n" + "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); + verifyGoogleFormat( + "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" + " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); +} + +TEST_F(FormatTest, TrailingReturnType) { + verifyFormat("auto foo() -> int;\n"); + verifyFormat("struct S {\n" + " auto bar() const -> int;\n" + "};"); + verifyFormat("template <size_t Order, typename T>\n" + "auto load_img(const std::string &filename)\n" + " -> alias::tensor<Order, T, mem::tag::cpu> {}"); + + // 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\n" + "someLongFunction(int someLongParameter) 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;"); + + // Unless this would lead to the first parameter being broken. + verifyFormat("void someLongFunction(int someLongParameter)\n" + " const {}", + getLLVMStyleWithColumns(46)); + verifyFormat("void someLongFunction(int someLongParameter)\n" + " const {}", + Style); + 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) {}"); + + verifyFormat( + "void aaaaaaaaaaaaaaaaaa()\n" + " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaa));"); + verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " __attribute__((unused));"); + verifyFormat( + "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " GUARDED_BY(aaaaaaaaaaaa);", + getGoogleStyle()); + verifyFormat( + "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " GUARDED_BY(aaaaaaaaaaaa);", + getGoogleStyle()); } TEST_F(FormatTest, BreaksDesireably) { @@ -1842,9 +3075,15 @@ TEST_F(FormatTest, BreaksDesireably) { "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); verifyFormat( - "aaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + + // Indent consistently indenpendent of call expression. + verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" + " dddddddddddddddddddddddddddddd));\n" + "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" + " dddddddddddddddddddddddddddddd));"); // This test case breaks on an incorrect memoization, i.e. an optimization not // taking into account the StopAt value. @@ -1859,6 +3098,20 @@ TEST_F(FormatTest, BreaksDesireably) { " 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, FormatsOneParameterPerLineIfNecessary) { @@ -1882,8 +3135,10 @@ TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" " .aaaaaaaaaaaaaaaaaa();", NoBinPacking); - verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);", + verifyFormat("void f() {\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" + "}", NoBinPacking); verifyFormat( @@ -1916,48 +3171,104 @@ TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { " .aaaaaaa();\n" "}", NoBinPacking); + verifyFormat( + "template <class SomeType, class SomeOtherType>\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<Reference::Kind>(name)\n" " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" - " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" - " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\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\n" - " ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); verifyFormat( "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" " aaaaaaaaaaaaaa);"); verifyFormat( - "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n" - " ->aaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" - " ->aaaaaaaaaaaaaaaaa();"); -} + "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" + "}"); -TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { - 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) {}"); verifyFormat( - "void aaaaaaaaaaaaaaaaaa()\n" - " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaa));"); - verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " __attribute__((unused));"); - verifyFormat( - "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " GUARDED_BY(aaaaaaaaaaaa);"); + "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();"); + + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" + " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" + " .has<bbbbbbbbbbbbbbbbbbbbb>();"); + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" + " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); + + // Prefer not to break after empty parentheses. + verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" + " First->LastNewlineOffset);"); } TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { @@ -1990,8 +3301,12 @@ TEST_F(FormatTest, BreaksAfterAssignments) { " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); verifyFormat( - "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" - " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); + "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) { @@ -2007,9 +3322,10 @@ TEST_F(FormatTest, AlignsAfterAssignments) { verifyFormat( "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaa);"); - verifyFormat("double LooooooooooooooooooooooooongResult =\n" - " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" - " aaaaaaaaaaaaaaaaaaaaaaaa;"); + verifyFormat( + "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaa;"); } TEST_F(FormatTest, AlignsAfterReturn) { @@ -2030,13 +3346,16 @@ TEST_F(FormatTest, AlignsAfterReturn) { 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, BreaksConditionalExpressions) { verifyFormat( - "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat( "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); @@ -2048,6 +3367,10 @@ TEST_F(FormatTest, BreaksConditionalExpressions) { " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaa);"); + verifyFormat( + "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaa);"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" @@ -2060,7 +3383,11 @@ TEST_F(FormatTest, BreaksConditionalExpressions) { " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); - + verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); @@ -2083,6 +3410,16 @@ TEST_F(FormatTest, BreaksConditionalExpressions) { " : 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);"); FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackParameters = false; @@ -2095,6 +3432,102 @@ TEST_F(FormatTest, BreaksConditionalExpressions) { " : aaaaaaaaaaaaaaa);\n" "}", NoBinPacking); + verifyFormat( + "void f() {\n" + " g(aaa,\n" + " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " ?: aaaaaaaaaaaaaaa);\n" + "}", + NoBinPacking); +} + +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) { @@ -2118,9 +3551,10 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) { " ***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" + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" - " *b = bbbbbbbbbbbbbbbbbbb;"); + " *b = bbbbbbbbbbbbbbbbbbb;", + getGoogleStyle()); // Not ideal, but pointer-with-type does not allow much here. verifyGoogleFormat( @@ -2161,11 +3595,78 @@ TEST_F(FormatTest, AlignsStringLiterals) { 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\");"); +} + +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); + + // 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("x =\n" + " \"a\\\n" + "b\\\n" + "c\";", + format("x = \"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)); } TEST_F(FormatTest, AlignsPipes) { @@ -2189,12 +3690,15 @@ TEST_F(FormatTest, AlignsPipes) { " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 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" + " << \" 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" @@ -2203,10 +3707,43 @@ TEST_F(FormatTest, AlignsPipes) { " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); + verifyFormat( + "void f() {\n" + " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" + " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" + "}"); + + // 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(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<<a;")); } TEST_F(FormatTest, UnderstandsEquals) { @@ -2254,12 +3791,19 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { " .WillRepeatedly(Return(SomeValue));"); verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" " .insert(ccccccccccccccccccccccc);"); + verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n" + " aaaaaaaaaaaaaaaaaaaaa);"); + verifyFormat("void f() {\n" + " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" + "}"); verifyFormat( "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" - " .aaaaaaaaaaaaaaa(\n" - " aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); + " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" @@ -2294,11 +3838,23 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { " 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));"); } TEST_F(FormatTest, WrapsTemplateDeclarations) { verifyFormat("template <typename T>\n" "virtual void loooooooooooongFunction(int Param1, int Param2);"); + verifyFormat("template <typename T>\n" + "// T should be one of {A, B}.\n" + "virtual void loooooooooooongFunction(int Param1, int Param2);"); verifyFormat( "template <typename T>\n" "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); @@ -2325,8 +3881,39 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) { "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); - verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" - " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); + verifyFormat("void f() {\n" + " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" + " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" + "}"); + + verifyFormat("template <typename T> class C {};"); + verifyFormat("template <typename T> void f();"); + verifyFormat("template <typename T> void f() {}"); + verifyFormat( + "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" + " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" + " bbbbbbbbbbbbbbbbbbbbbbbb);", + getLLVMStyleWithColumns(72)); + + FormatStyle AlwaysBreak = getLLVMStyle(); + AlwaysBreak.AlwaysBreakTemplateDeclarations = true; + verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); + verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); + verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); + verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" + " ccccccccccccccccccccccccccccccccccccccccccccccc);"); + verifyFormat("template <template <typename> class Fooooooo,\n" + " template <typename> class Baaaaaaar>\n" + "struct C {};", + AlwaysBreak); + verifyFormat("template <typename T> // T can be A, B or C.\n" + "struct C {};", + AlwaysBreak); } TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { @@ -2338,14 +3925,12 @@ TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); - // FIXME: Should we have an extra indent after the second break? + // FIXME: Should we have the extra indent after the second break? verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); - // FIXME: Look into whether we should indent 4 from the start or 4 from - // "bbbbb..." here instead of what we are doing now. verifyFormat( "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" " cccccccccccccccccccccccccccccccccccccccccccccc());"); @@ -2383,14 +3968,33 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) { verifyGoogleFormat("A<A<int> > a;"); verifyGoogleFormat("A<A<A<int> > > a;"); verifyGoogleFormat("A<A<A<A<int> > > > a;"); + verifyGoogleFormat("A<::A<int>> a;"); + verifyGoogleFormat("A<::A> a;"); + verifyGoogleFormat("A< ::A> a;"); + verifyGoogleFormat("A< ::A<int> > a;"); EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); + EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); + EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); verifyFormat("test >> a >> b;"); verifyFormat("test << a >> b;"); verifyFormat("f<int>();"); verifyFormat("template <typename T> void f() {}"); + + // Not template parameters. + verifyFormat("return a < b && c > d;"); + verifyFormat("void f() {\n" + " while (a < b && c > d) {\n" + " }\n" + "}"); + verifyFormat("template <typename... Types>\n" + "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); + + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", + getLLVMStyleWithColumns(60)); } TEST_F(FormatTest, UnderstandsBinaryOperators) { @@ -2399,14 +4003,23 @@ TEST_F(FormatTest, UnderstandsBinaryOperators) { TEST_F(FormatTest, UnderstandsPointersToMembers) { verifyFormat("int A::*x;"); - // FIXME: Recognize pointers to member functions. - //verifyFormat("int (S::*func)(void *);"); - verifyFormat("int(S::*func)(void *);"); - verifyFormat("(a->*f)();"); - verifyFormat("a->*x;"); - verifyFormat("(a.*f)();"); - verifyFormat("((*a).*f)();"); - verifyFormat("a.*x;"); + verifyFormat("int (S::*func)(void *);"); + verifyFormat("void f() { int (S::*func)(void *); }"); + verifyFormat("typedef bool *(Class::*Member)() const;"); + verifyFormat("void f() {\n" + " (a->*f)();\n" + " a->*x;\n" + " (a.*f)();\n" + " ((*a).*f)();\n" + " a.*x;\n" + "}"); + verifyFormat("void f() {\n" + " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" + " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" + "}"); + FormatStyle Style = getLLVMStyle(); + Style.PointerBindsToType = true; + verifyFormat("typedef bool* (Class::*Member)() const;", Style); } TEST_F(FormatTest, UnderstandsUnaryOperators) { @@ -2427,9 +4040,9 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) { verifyFormat("a-- > b;"); verifyFormat("b ? -a : c;"); verifyFormat("n * sizeof char16;"); - verifyFormat("n * alignof char16;"); + verifyFormat("n * alignof char16;", getGoogleStyle()); verifyFormat("sizeof(char);"); - verifyFormat("alignof(char);"); + verifyFormat("alignof(char);", getGoogleStyle()); verifyFormat("return -1;"); verifyFormat("switch (a) {\n" @@ -2447,7 +4060,19 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) { verifyFormat("int a = i /* confusing comment */++;"); } -TEST_F(FormatTest, UndestandsOverloadedOperators) { +TEST_F(FormatTest, IndentsRelativeToUnaryOperators) { + verifyFormat("if (!aaaaaaaaaa( // break\n" + " aaaaa)) {\n" + "}"); + verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" + " aaaaa));"); + + // Only indent relative to unary operators if the expression is nested. + verifyFormat("*aaa = aaaaaaa( // break\n" + " bbbbbb);"); +} + +TEST_F(FormatTest, UnderstandsOverloadedOperators) { verifyFormat("bool operator<();"); verifyFormat("bool operator>();"); verifyFormat("bool operator=();"); @@ -2468,6 +4093,8 @@ TEST_F(FormatTest, UndestandsOverloadedOperators) { verifyFormat("void *operator new[](std::size_t size);"); verifyFormat("void operator delete(void *ptr);"); verifyFormat("void operator delete[](void *ptr);"); + verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" + "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); verifyFormat( "ostream &operator<<(ostream &OutputStream,\n" @@ -2480,6 +4107,9 @@ TEST_F(FormatTest, UndestandsOverloadedOperators) { verifyGoogleFormat("operator void*();"); verifyGoogleFormat("operator SomeType<SomeType<int>>();"); + verifyGoogleFormat("operator ::A();"); + + verifyFormat("using A::operator+;"); } TEST_F(FormatTest, UnderstandsNewAndDelete) { @@ -2529,12 +4159,14 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyIndependentOfContext("return sizeof(int **);"); verifyIndependentOfContext("return sizeof(int ******);"); verifyIndependentOfContext("return (int **&)a;"); + verifyIndependentOfContext("f((*PointerToArray)[10]);"); verifyFormat("void f(Type (*parameter)[10]) {}"); verifyGoogleFormat("return sizeof(int**);"); verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); - // FIXME: The newline is wrong. - verifyFormat("auto a = [](int **&, int ***) {}\n;"); + verifyFormat("auto a = [](int **&, int ***) {};"); + verifyFormat("auto PointerBinding = [](const char *S) {};"); + verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); verifyIndependentOfContext("InvalidRegions[*R] = 0;"); @@ -2587,22 +4219,47 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyIndependentOfContext("if (*b[i])"); verifyIndependentOfContext("if (int *a = (&b))"); verifyIndependentOfContext("while (int *a = &b)"); + verifyIndependentOfContext("size = sizeof *a;"); verifyFormat("void f() {\n" " for (const int &v : Values) {\n" " }\n" "}"); verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); + verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); + + verifyFormat("#define A (!a * b)"); + verifyFormat("#define MACRO \\\n" + " int *i = a * b; \\\n" + " void f(a *b);", + getLLVMStyleWithColumns(19)); verifyIndependentOfContext("A = new SomeType *[Length];"); verifyIndependentOfContext("A = new SomeType *[Length]();"); + verifyIndependentOfContext("T **t = new T *;"); + verifyIndependentOfContext("T **t = new T *();"); verifyGoogleFormat("A = new SomeType* [Length]();"); verifyGoogleFormat("A = new SomeType* [Length];"); + verifyGoogleFormat("T** t = new T*;"); + verifyGoogleFormat("T** t = new T*();"); + + FormatStyle PointerLeft = getLLVMStyle(); + PointerLeft.PointerBindsToType = true; + verifyFormat("delete *x;", PointerLeft); +} + +TEST_F(FormatTest, UnderstandsAttributes) { + verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); } TEST_F(FormatTest, UnderstandsEllipsis) { verifyFormat("int printf(const char *fmt, ...);"); verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); + verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); + + FormatStyle PointersLeft = getLLVMStyle(); + PointersLeft.PointerBindsToType = true; + verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); } TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { @@ -2636,12 +4293,17 @@ TEST_F(FormatTest, UnderstandsRvalueReferences) { verifyGoogleFormat("int f(int a, char&& b) {}"); verifyGoogleFormat("void f() { int&& a = b; }"); - // FIXME: These require somewhat deeper changes in template arguments - // formatting. - // verifyIndependentOfContext("A<int &&> a;"); - // verifyIndependentOfContext("A<int &&, int &&> a;"); - // verifyGoogleFormat("A<int&&> a;"); - // verifyGoogleFormat("A<int&&, int&&> a;"); + verifyIndependentOfContext("A<int &&> a;"); + verifyIndependentOfContext("A<int &&, int &&> a;"); + verifyGoogleFormat("A<int&&> a;"); + verifyGoogleFormat("A<int&&, int&&> a;"); + + // Not rvalue references: + verifyFormat("template <bool B, bool C> class A {\n" + " static_assert(B && C, \"Something is wrong\");\n" + "};"); + verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); + verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); } TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { @@ -2657,10 +4319,33 @@ TEST_F(FormatTest, FormatsCasts) { verifyFormat("Type *A = (Type *)P;"); verifyFormat("Type *A = (vector<Type *, int *>)P;"); verifyFormat("int a = (int)(2.0f);"); - - // FIXME: These also need to be identified. - verifyFormat("int a = (int) 2.0f;"); - verifyFormat("int a = (int) * b;"); + verifyFormat("int a = (int)2.0f;"); + verifyFormat("x[(int32)y];"); + verifyFormat("x = (int32)y;"); + verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); + verifyFormat("int a = (int)*b;"); + verifyFormat("int a = (int)2.0f;"); + verifyFormat("int a = (int)~0;"); + verifyFormat("int a = (int)++a;"); + verifyFormat("int a = (int)sizeof(int);"); + verifyFormat("int a = (int)+2;"); + verifyFormat("my_int a = (my_int)2.0f;"); + verifyFormat("my_int a = (my_int)sizeof(int);"); + verifyFormat("return (my_int)aaa;"); + verifyFormat("#define x ((int)-1)"); + verifyFormat("#define p(q) ((int *)&q)"); + + // FIXME: Without type knowledge, this can still fall apart miserably. + verifyFormat("void f() { my_int a = (my_int) * b; }"); + verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }"); + verifyFormat("my_int a = (my_int) ~0;"); + verifyFormat("my_int a = (my_int)++ a;"); + verifyFormat("my_int a = (my_int) + 2;"); + + // Don't break after a cast's + verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" + " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" + " bbbbbbbbbbbbbbbbbbbbbb);"); // These are not casts. verifyFormat("void f(int *) {}"); @@ -2668,7 +4353,7 @@ TEST_F(FormatTest, FormatsCasts) { verifyFormat("f(foo).b;"); verifyFormat("f(foo)(b);"); verifyFormat("f(foo)[b];"); - verifyFormat("[](foo) { return 4; }(bar)];"); + verifyFormat("[](foo) { return 4; }(bar);"); verifyFormat("(*funptr)(foo)[4];"); verifyFormat("funptrs[4](foo)[4];"); verifyFormat("void f(int *);"); @@ -2679,44 +4364,76 @@ TEST_F(FormatTest, FormatsCasts) { verifyFormat("void f(int i = (kValue) * kMask) {}"); verifyFormat("void f(int i = (kA * kB) & kMask) {}"); verifyFormat("int a = sizeof(int) * b;"); - verifyFormat("int a = alignof(int) * b;"); + verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); + verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); + verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); + verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); // These are not casts, but at some point were confused with casts. verifyFormat("virtual void foo(int *) override;"); verifyFormat("virtual void foo(char &) const;"); verifyFormat("virtual void foo(int *a, char *) const;"); verifyFormat("int a = sizeof(int *) + b;"); - verifyFormat("int a = alignof(int *) + b;"); + verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); + + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" + " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); + // FIXME: The indentation here is not ideal. + verifyFormat( + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" + " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); } TEST_F(FormatTest, FormatsFunctionTypes) { verifyFormat("A<bool()> a;"); verifyFormat("A<SomeType()> a;"); - verifyFormat("A<void(*)(int, std::string)> a;"); + verifyFormat("A<void (*)(int, std::string)> a;"); verifyFormat("A<void *(int)>;"); verifyFormat("void *(*a)(int *, SomeType *);"); - - // FIXME: Inconsistent. verifyFormat("int (*func)(void *);"); - verifyFormat("void f() { int(*func)(void *); }"); + verifyFormat("void f() { int (*func)(void *); }"); + verifyFormat("template <class CallbackClass>\n" + "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); verifyGoogleFormat("A<void*(int*, SomeType*)>;"); verifyGoogleFormat("void* (*a)(int);"); + verifyGoogleFormat( + "template <class CallbackClass>\n" + "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); + + // Other constructs can look somewhat like function types: + verifyFormat("A<sizeof(*x)> a;"); + verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); + verifyFormat("some_var = function(*some_pointer_var)[0];"); + verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); } TEST_F(FormatTest, BreaksLongDeclarations) { verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" - " AnotherNameForTheLongType;"); + " AnotherNameForTheLongType;", + getGoogleStyle()); + verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", + getGoogleStyle()); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" - " LoooooooooooooooooooooooooooooooooooooooongVariable;"); + " LoooooooooooooooooooooooooooooooooooooooongVariable;", + getGoogleStyle()); + verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" + " LoooooooooooooooooooooooooooooooooooooooongVariable;", + getGoogleStyle()); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" - " LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); + " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", + getGoogleStyle()); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); + verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" + "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); // FIXME: Without the comment, this breaks after "(". - verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" - " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();"); + verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" + " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", + getGoogleStyle()); verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" " int LoooooooooooooooooooongParam2) {}"); @@ -2731,10 +4448,14 @@ TEST_F(FormatTest, BreaksLongDeclarations) { " ReallyReallyLongParameterName,\n" " const SomeType<string, SomeOtherTemplateParameter> &\n" " AnotherLongParameterName) {}"); - verifyFormat( + verifyFormat("template <typename A>\n" + "SomeLoooooooooooooooooooooongType<\n" + " typename some_namespace::SomeOtherType<A>::Type>\n" + "Function() {}"); + + verifyGoogleFormat( "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" " aaaaaaaaaaaaaaaaaaaaaaa;"); - verifyGoogleFormat( "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" " SourceLocation L) {}"); @@ -2750,6 +4471,22 @@ TEST_F(FormatTest, BreaksLongDeclarations) { " int aaaaaaaaaaaaaaaaaaaaaaa);"); } +TEST_F(FormatTest, FormatsArrays) { + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" + " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" + " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); + verifyFormat( + "llvm::outs() << \"aaaaaaaaaaaa: \"\n" + " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" + " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); +} + TEST_F(FormatTest, LineStartsWithSpecialCharacter) { verifyFormat("(a)->b();"); verifyFormat("--a;"); @@ -2764,14 +4501,20 @@ TEST_F(FormatTest, HandlesIncludeDirectives) { "#include <a-a>\n" "#include < path with space >\n" "#include \"abc.h\" // this is included for ABC\n" + "#include \"some long include\" // with a comment\n" "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", getLLVMStyleWithColumns(35)); + EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); + EXPECT_EQ("#include <a>", format("#include<a>")); verifyFormat("#import <string>"); verifyFormat("#import <a/b/c.h>"); verifyFormat("#import \"a/b/string\""); verifyFormat("#import \"string.h\""); verifyFormat("#import \"string.h\""); + verifyFormat("#if __has_include(<strstream>)\n" + "#include <strstream>\n" + "#endif"); } //===----------------------------------------------------------------------===// @@ -2813,7 +4556,10 @@ TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { " return\n" "}", format("void f ( ) { if ( a ) return }")); - EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); + EXPECT_EQ("namespace N {\n" + "void f()\n" + "}", + format("namespace N { void f() }")); EXPECT_EQ("namespace N {\n" "void f() {}\n" "void g()\n" @@ -2874,20 +4620,22 @@ TEST_F(FormatTest, IncorrectCodeMissingParens) { TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { verifyFormat("namespace {\n" - "class Foo { Foo ( }; } // comment"); + "class Foo { Foo (\n" + "};\n" + "} // comment"); } TEST_F(FormatTest, IncorrectCodeErrorDetection) { - EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); + EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); - EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); + EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); EXPECT_EQ("{\n" - " {\n" - " breakme(\n" - " qwe);\n" - "}\n", + " {\n" + " breakme(\n" + " qwe);\n" + " }\n", format("{\n" " {\n" " breakme(qwe);\n" @@ -2907,14 +4655,119 @@ TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { verifyFormat("return (a)(b) { 1, 2, 3 };"); } -TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { - // FIXME: This is bad, find a better and more generic solution. +TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { + verifyFormat("vector<int> x{ 1, 2, 3, 4 };"); + verifyFormat("vector<T> x{ {}, {}, {}, {} };"); + verifyFormat("f({ 1, 2 });"); + verifyFormat("auto v = Foo{ 1 };"); + verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });"); + verifyFormat("Class::Class : member{ 1, 2, 3 } {}"); + verifyFormat("new vector<int>{ 1, 2, 3 };"); + verifyFormat("new int[3]{ 1, 2, 3 };"); + verifyFormat("return { arg1, arg2 };"); + verifyFormat("return { arg1, SomeType{ parameter } };"); + verifyFormat("new T{ arg1, arg2 };"); + verifyFormat("f(MyMap[{ composite, key }]);"); + verifyFormat("class Class {\n" + " T member = { arg1, arg2 };\n" + "};"); + verifyFormat( + "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" + " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" + " bbbbbbbbbbbbbbbbbbbb, bbbbb };"); + verifyFormat("DoSomethingWithVector({} /* No data */);"); + verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });"); + verifyFormat( + "someFunction(OtherParam, BracedList{\n" + " // comment 1 (Forcing interesting break)\n" + " param1, param2,\n" + " // comment 2\n" + " param3, param4\n" + " });"); + verifyFormat( + "std::this_thread::sleep_for(\n" + " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);"); + + FormatStyle NoSpaces = getLLVMStyle(); + NoSpaces.Cpp11BracedListStyle = true; + verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces); + verifyFormat("vector<T> x{{}, {}, {}, {}};", NoSpaces); + verifyFormat("f({1, 2});", NoSpaces); + verifyFormat("auto v = Foo{-1};", NoSpaces); + verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", NoSpaces); + verifyFormat("Class::Class : member{1, 2, 3} {}", NoSpaces); + verifyFormat("new vector<int>{1, 2, 3};", NoSpaces); + verifyFormat("new int[3]{1, 2, 3};", NoSpaces); + verifyFormat("return {arg1, arg2};", NoSpaces); + verifyFormat("return {arg1, SomeType{parameter}};", NoSpaces); + verifyFormat("new T{arg1, arg2};", NoSpaces); + verifyFormat("f(MyMap[{composite, key}]);", NoSpaces); + verifyFormat("class Class {\n" + " T member = {arg1, arg2};\n" + "};", + NoSpaces); + verifyFormat("Constructor::Constructor()\n" + " : some_value{ //\n" + " aaaaaaa //\n" + " } {}", + NoSpaces); +} + +TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { + 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, 666666, 7777777,\n" + " 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" + " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555,\n" + " // line comment\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777 };"); verifyFormat( - "Aaa({\n" - " int i;\n" - "},\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" - " ccccccccccccccccc));"); + "vector<int> x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, // comment\n" + " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" + " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" + " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" + " 7777777 };"); + 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\n" + "};"); + verifyFormat("vector<int> x = { 1, 1, 1, 1,\n" + " 1, 1, 1, 1 };", + getLLVMStyleWithColumns(39)); + verifyFormat("vector<int> x = { 1, 1, 1, 1,\n" + " 1, 1, 1, 1 };", + getLLVMStyleWithColumns(38)); + verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" + " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1\n" + "};", + getLLVMStyleWithColumns(40)); + + // Trailing commas. + verifyFormat("vector<int> x = { 1, 1, 1, 1,\n" + " 1, 1, 1, 1, };", + getLLVMStyleWithColumns(39)); + verifyFormat("vector<int> x = { 1, 1, 1, 1,\n" + " 1, 1, 1, 1, //\n" + "};", + getLLVMStyleWithColumns(39)); + verifyFormat("vector<int> x = { 1, 1, 1, 1,\n" + " 1, 1, 1, 1,\n" + " /**/ /**/ };", + getLLVMStyleWithColumns(39)); + verifyFormat("return { { aaaaaaaaaaaaaaaaaaaaa },\n" + " { aaaaaaaaaaaaaaaaaaa },\n" + " { aaaaaaaaaaaaaaaaaaaaa },\n" + " { aaaaaaaaaaaaaaaaa } };", + getLLVMStyleWithColumns(60)); } TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { @@ -2930,6 +4783,11 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { " int a;\n" "#error {\n" "}"); + verifyFormat("void f() {} // comment"); + verifyFormat("void f() { int a; } // comment"); + verifyFormat("void f() {\n" + "} // comment", + getLLVMStyleWithColumns(15)); verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); @@ -2964,13 +4822,21 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { verifyFormat("class __attribute__(X) Z {\n} n;"); verifyFormat("class __declspec(X) Z {\n} n;"); verifyFormat("class A##B##C {\n} n;"); + verifyFormat("class alignas(16) Z {\n} n;"); // Redefinition from nested context: verifyFormat("class A::B::C {\n} n;"); // Template definitions. + verifyFormat( + "template <typename F>\n" + "Matcher(const Matcher<F> &Other,\n" + " typename enable_if_c<is_base_of<F, T>::value &&\n" + " !is_same<F, T>::value>::type * = 0)\n" + " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); + // FIXME: This is still incorrectly handled at the formatter side. - verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); + verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); // FIXME: // This now gets parsed incorrectly as class definition. @@ -2984,12 +4850,15 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { " f();\n"); // This is simply incomplete. Formatting is not important, but must not crash. - verifyFormat("class A:"); + verifyFormat("class A:"); } TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { - verifyFormat("#error Leave all white!!!!! space* alone!\n"); - verifyFormat("#warning Leave all white!!!!! space* alone!\n"); + EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", + format("#error Leave all white!!!!! space* alone!\n")); + EXPECT_EQ( + "#warning Leave all white!!!!! space* alone!\n", + format("#warning Leave all white!!!!! space* alone!\n")); EXPECT_EQ("#error 1", format(" # error 1")); EXPECT_EQ("#warning 1", format(" # warning 1")); } @@ -3034,7 +4903,8 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { " if (true) continue;\n" "#endif\n" " // Comment\n" - " if (true) continue;", + " if (true) continue;\n" + "}", ShortMergedIf); } @@ -3062,7 +4932,7 @@ TEST_F(FormatTest, BlockComments) { EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", format("/* *//* */ /* */\n/* *//* */ /* */")); EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); - EXPECT_EQ("#define A /*123*/\\\n" + EXPECT_EQ("#define A /*123*/ \\\n" " b\n" "/* */\n" "someCall(\n" @@ -3078,6 +4948,27 @@ TEST_F(FormatTest, BlockComments) { format("#define A\n" "/* */someCall(parameter);", getLLVMStyleWithColumns(15))); + EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); + EXPECT_EQ("/*\n" + "*\n" + " * aaaaaa\n" + "*aaaaaa\n" + "*/", + format("/*\n" + "*\n" + " * aaaaaa aaaaaa\n" + "*/", + getLLVMStyleWithColumns(10))); + EXPECT_EQ("/*\n" + "**\n" + "* aaaaaa\n" + "*aaaaaa\n" + "*/", + format("/*\n" + "**\n" + "* aaaaaa aaaaaa\n" + "*/", + getLLVMStyleWithColumns(10))); FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackParameters = false; @@ -3109,6 +5000,35 @@ TEST_F(FormatTest, BlockComments) { format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" "int cccccccccccccccccccccccccccccc; /* comment */\n")); + + verifyFormat("void f(int * /* unused */) {}"); + + EXPECT_EQ("/*\n" + " **\n" + " */", + format("/*\n" + " **\n" + " */")); + EXPECT_EQ("/*\n" + " *q\n" + " */", + format("/*\n" + " *q\n" + " */")); + EXPECT_EQ("/*\n" + " * q\n" + " */", + format("/*\n" + " * q\n" + " */")); + EXPECT_EQ("/*\n" + " **/", + format("/*\n" + " **/")); + EXPECT_EQ("/*\n" + " ***/", + format("/*\n" + " ***/")); } TEST_F(FormatTest, BlockCommentsInMacros) { @@ -3132,6 +5052,30 @@ TEST_F(FormatTest, BlockCommentsInMacros) { getLLVMStyleWithColumns(20))); } +TEST_F(FormatTest, BlockCommentsAtEndOfLine) { + EXPECT_EQ("a = {\n" + " 1111 /* */\n" + "};", + format("a = {1111 /* */\n" + "};", + getLLVMStyleWithColumns(15))); + EXPECT_EQ("a = {\n" + " 1111 /* */\n" + "};", + format("a = {1111 /* */\n" + "};", + getLLVMStyleWithColumns(15))); + + // FIXME: The formatting is still wrong here. + EXPECT_EQ("a = {\n" + " 1111 /* a\n" + " */\n" + "};", + format("a = {1111 /* a */\n" + "};", + getLLVMStyleWithColumns(15))); +} + TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { // FIXME: This is not what we want... verifyFormat("{\n" @@ -3202,8 +5146,8 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { // protocol lists (but not for template classes): //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); - verifyFormat("- (int(*)())foo:(int(*)())f;"); - verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); + verifyFormat("- (int (*)())foo:(int (*)())f;"); + verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); // If there's no return type (very rare in practice!), LLVM and Google style // agree. @@ -3386,6 +5330,10 @@ TEST_F(FormatTest, FormatObjCImplementation) { verifyFormat("@implementation Foo (HackStuff)\n" "+ (id)init {\n}\n" "@end"); + verifyFormat("@implementation ObjcClass\n" + "- (void)method;\n" + "{}\n" + "@end"); } TEST_F(FormatTest, FormatObjCProtocol) { @@ -3419,6 +5367,11 @@ TEST_F(FormatTest, FormatObjCProtocol) { "@optional\n" "@property(assign) int madProp;\n" "@end\n"); + + verifyFormat("@property(nonatomic, assign, readonly)\n" + " int *looooooooooooooooooooooooooooongNumber;\n" + "@property(nonatomic, assign, readonly)\n" + " NSString *looooooooooooooooooooooooooooongName;"); } TEST_F(FormatTest, FormatObjCMethodDeclarations) { @@ -3449,7 +5402,7 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { verifyFormat("int a = ++[foo bar:baz];"); verifyFormat("int a = --[foo bar:baz];"); verifyFormat("int a = sizeof [foo bar:baz];"); - verifyFormat("int a = alignof [foo bar:baz];"); + verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); verifyFormat("int a = &[foo bar:baz];"); verifyFormat("int a = *[foo bar:baz];"); // FIXME: Make casts work, without breaking f()[4]. @@ -3508,22 +5461,26 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { verifyFormat("throw [self errorFor:a];"); verifyFormat("@throw [self errorFor:a];"); + verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); + verifyFormat("[(id)foo bar:(id) ? baz : quux];"); + verifyFormat("4 > 4 ? (id)a : (id)baz;"); + // This tests that the formatter doesn't break after "backing" but before ":", // which would be at 80 columns. verifyFormat( "void f() {\n" " if ((self = [super initWithContentRect:contentRect\n" - " styleMask:styleMask\n" + " styleMask:styleMask ?: otherMask\n" " backing:NSBackingStoreBuffered\n" " defer:YES]))"); verifyFormat( "[foo checkThatBreakingAfterColonWorksOk:\n" - " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); + " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); verifyFormat("[myObj short:arg1 // Force line break\n" - " longKeyword:arg2\n" - " evenLongerKeyword:arg3\n" + " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" + " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" " error:arg4];"); verifyFormat( "void f() {\n" @@ -3534,6 +5491,16 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { " backing:NSBackingStoreBuffered\n" " defer:NO]);\n" "}"); + verifyFormat( + "void f() {\n" + " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" + " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" + " pos.width(), pos.height())\n" + " syeMask:NSBorderlessWindowMask\n" + " bking:NSBackingStoreBuffered\n" + " der:NO]);\n" + "}", + getLLVMStyleWithColumns(70)); verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" " with:contentsNativeView];"); @@ -3560,6 +5527,27 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { "scoped_nsobject<NSTextField> message(\n" " // The frame will be fixed up when |-setMessageText:| is called.\n" " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); + verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" + " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" + " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" + " aaaa:bbb];"); + verifyFormat("[self param:function( //\n" + " parameter)]"); + verifyFormat( + "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" + " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" + " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); + + // Variadic parameters. + verifyFormat( + "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); + verifyFormat( + "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); + verifyFormat("[self // break\n" + " a:a\n" + " aaa:aaa];"); } TEST_F(FormatTest, ObjCAt) { @@ -3618,6 +5606,9 @@ TEST_F(FormatTest, ObjCSnippets) { verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); + + verifyFormat("@import foo.bar;\n" + "@import baz;"); } TEST_F(FormatTest, ObjCLiterals) { @@ -3634,29 +5625,17 @@ TEST_F(FormatTest, ObjCLiterals) { verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); verifyFormat("NSNumber *favoriteColor = @(Green);"); verifyFormat("NSString *path = @(getenv(\"PATH\"));"); +} - verifyFormat("@["); - verifyFormat("@[]"); - verifyFormat( - "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); - verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); - +TEST_F(FormatTest, ObjCDictLiterals) { verifyFormat("@{"); verifyFormat("@{}"); verifyFormat("@{ @\"one\" : @1 }"); verifyFormat("return @{ @\"one\" : @1 };"); verifyFormat("@{ @\"one\" : @1, }"); - // FIXME: Breaking in cases where we think there's a structural error - // showed that we're incorrectly parsing this code. We need to fix the - // parsing here. - verifyFormat("@{ @\"one\" : @\n" - "{ @2 : @1 }\n" - "}"); - verifyFormat("@{ @\"one\" : @\n" - "{ @2 : @1 }\n" - ",\n" - "}"); + verifyFormat("@{ @\"one\" : @{ @2 : @1 } }"); + verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }"); verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); verifyFormat("[self setDict:@{}"); @@ -3667,10 +5646,67 @@ TEST_F(FormatTest, ObjCLiterals) { verifyFormat( "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); - // FIXME: Nested and multi-line array and dictionary literals need more work. verifyFormat( - "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n" - " @\"processInfo\" : [NSProcessInfo processInfo] };"); + "NSDictionary *d = @{\n" + " @\"nam\" : NSUserNam(),\n" + " @\"dte\" : [NSDate date],\n" + " @\"processInfo\" : [NSProcessInfo processInfo]\n" + "};"); + verifyFormat( + "@{\n" + " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " + "regularFont,\n" + "};"); + verifyGoogleFormat( + "@{\n" + " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " + "regularFont,\n" + "};"); + + // 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" + "};"); +} + +TEST_F(FormatTest, ObjCArrayLiterals) { + verifyFormat("@["); + verifyFormat("@[]"); + verifyFormat( + "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); + verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); + verifyFormat("NSArray *array = @[ [foo description] ];"); + + verifyFormat( + "NSArray *some_variable = @[\n" + " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\"\n" + "];"); + verifyFormat("NSArray *some_variable = @[\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + "];"); + verifyGoogleFormat("NSArray *some_variable = @[\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\"\n" + "];"); + + // We should try to be robust in case someone forgets the "@". + verifyFormat("NSArray *some_variable = [\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + " @\"aaaaaaaaaaaaaaaaa\",\n" + "];"); } TEST_F(FormatTest, ReformatRegionAdjustsIndent) { @@ -3718,7 +5754,7 @@ TEST_F(FormatTest, ReformatRegionAdjustsIndent) { "a;\n" "}\n" "{\n" - " b;\n" + " b; //\n" "}\n" "}", format("{\n" @@ -3726,15 +5762,15 @@ TEST_F(FormatTest, ReformatRegionAdjustsIndent) { "a;\n" "}\n" "{\n" - " b;\n" + " b; //\n" "}\n" "}", 22, 2, getLLVMStyle())); EXPECT_EQ(" {\n" - " a;\n" + " a; //\n" " }", format(" {\n" - "a;\n" + "a; //\n" " }", 4, 2, getLLVMStyle())); EXPECT_EQ("void f() {}\n" @@ -3749,6 +5785,13 @@ TEST_F(FormatTest, ReformatRegionAdjustsIndent) { " // line 2\n" " int b;", 35, 0, getLLVMStyle())); + EXPECT_EQ(" int a;\n" + " void\n" + " ffffff() {\n" + " }", + format(" int a;\n" + "void ffffff() {}", + 11, 0, getLLVMStyleWithColumns(11))); } TEST_F(FormatTest, BreakStringLiterals) { @@ -3808,6 +5851,18 @@ TEST_F(FormatTest, BreakStringLiterals) { format("variable = f(\"long string literal\", short, " "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" + " a),\n" + " b);", + format("f(g(\"long string literal\", a), b);", + getLLVMStyleWithColumns(20))); EXPECT_EQ( "f(\"one two\".split(\n" " variable));", @@ -3842,6 +5897,53 @@ TEST_F(FormatTest, BreakStringLiterals) { "\"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" + "\"non-alphanumeric%\"\n" + "\"1111111111characte\"\n" + "\"rs\"", + format("\"split at " + "spaces/at/" + "slashes.at." + "any$non-" + "alphanumeric%" + "1111111111characte" + "rs\"", + getLLVMStyleWithColumns(20))); + + // Verify that splitting the strings understands + // Style::AlwaysBreakBeforeMultilineStrings. + EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n" + " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" + " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", + format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaa\");", + getGoogleStyle())); + EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", + format("return \"aaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaa\";", + getGoogleStyle())); + EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", + format("llvm::outs() << " + "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaa\";")); + EXPECT_EQ("ffff(\n" + " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", + format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", + getGoogleStyle())); + FormatStyle AlignLeft = getLLVMStyleWithColumns(12); AlignLeft.AlignEscapedNewlinesLeft = true; EXPECT_EQ( @@ -3852,6 +5954,177 @@ TEST_F(FormatTest, BreakStringLiterals) { format("#define A \"some text other\";", AlignLeft)); } +TEST_F(FormatTest, BreaksWideStringLiterals) { + EXPECT_EQ( + "u8\"utf8 string \"\n" + "u8\"literal\";", + format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); + EXPECT_EQ( + "u\"utf16 string \"\n" + "u\"literal\";", + format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); + EXPECT_EQ( + "U\"utf32 string \"\n" + "U\"literal\";", + format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); + EXPECT_EQ("L\"wide string \"\n" + "L\"literal\";", + format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); +} + +TEST_F(FormatTest, BreaksRawStringLiterals) { + EXPECT_EQ("R\"x(raw )x\"\n" + "R\"x(literal)x\";", + format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15))); + EXPECT_EQ("uR\"x(raw )x\"\n" + "uR\"x(literal)x\";", + format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16))); + EXPECT_EQ("u8R\"x(raw )x\"\n" + "u8R\"x(literal)x\";", + format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17))); + EXPECT_EQ("LR\"x(raw )x\"\n" + "LR\"x(literal)x\";", + format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16))); + EXPECT_EQ("UR\"x(raw )x\"\n" + "UR\"x(literal)x\";", + format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16))); +} + +TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { + FormatStyle Style = getLLVMStyleWithColumns(20); + EXPECT_EQ( + "_T(\"aaaaaaaaaaaaaa\")\n" + "_T(\"aaaaaaaaaaaaaa\")\n" + "_T(\"aaaaaaaaaaaa\")", + format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); + EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" + " _T(\"aaaaaa\"),\n" + " z);", + format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); + + // FIXME: Handle embedded spaces in one iteration. + // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" + // "_T(\"aaaaaaaaaaaaa\")\n" + // "_T(\"aaaaaaaaaaaaa\")\n" + // "_T(\"a\")", + // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", + // getLLVMStyleWithColumns(20))); + EXPECT_EQ( + "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", + format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); +} + +TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { + EXPECT_EQ( + "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", + format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); +} + +TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { + EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", + format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); + EXPECT_EQ("fffffffffff(g(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\",\n" + " a),\n" + " b);", + format("fffffffffff(g(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\", a), b);", + getGoogleStyleWithColumns(20))); + EXPECT_EQ("fffffffffff(\n" + " g(R\"x(qqq\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\",\n" + " a),\n" + " b);", + format("fffffffffff(g(R\"x(qqq\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\", a), b);", + getGoogleStyleWithColumns(20))); + + EXPECT_EQ("fffffffffff(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\");", + format("fffffffffff(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\");", + getGoogleStyleWithColumns(20))); + EXPECT_EQ("fffffffffff(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\" +\n" + " bbbbbb);", + format("fffffffffff(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\" + bbbbbb);", + getGoogleStyleWithColumns(20))); +} + +TEST_F(FormatTest, SkipsUnknownStringLiterals) { + verifyFormat("string a = \"unterminated;"); + EXPECT_EQ("function(\"unterminated,\n" + " OtherParameter);", + format("function( \"unterminated,\n" + " OtherParameter);")); +} + +TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { + EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", + format("#define x(_a) printf(\"foo\"_a);", getLLVMStyle())); +} + +TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { + EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" + " \"ddeeefff\");", + format("someFunction(\"aaabbbcccdddeeefff\");", + getLLVMStyleWithColumns(25))); + EXPECT_EQ("someFunction1234567890(\n" + " \"aaabbbcccdddeeefff\");", + format("someFunction1234567890(\"aaabbbcccdddeeefff\");", + getLLVMStyleWithColumns(26))); + EXPECT_EQ("someFunction1234567890(\n" + " \"aaabbbcccdddeeeff\"\n" + " \"f\");", + format("someFunction1234567890(\"aaabbbcccdddeeefff\");", + getLLVMStyleWithColumns(25))); + EXPECT_EQ("someFunction1234567890(\n" + " \"aaabbbcccdddeeeff\"\n" + " \"f\");", + format("someFunction1234567890(\"aaabbbcccdddeeefff\");", + getLLVMStyleWithColumns(24))); + EXPECT_EQ("someFunction(\"aaabbbcc \"\n" + " \"ddde \"\n" + " \"efff\");", + format("someFunction(\"aaabbbcc ddde efff\");", + getLLVMStyleWithColumns(25))); + EXPECT_EQ("someFunction(\"aaabbbccc \"\n" + " \"ddeeefff\");", + format("someFunction(\"aaabbbccc ddeeefff\");", + getLLVMStyleWithColumns(25))); + EXPECT_EQ("someFunction1234567890(\n" + " \"aaabb \"\n" + " \"cccdddeeefff\");", + format("someFunction1234567890(\"aaabb cccdddeeefff\");", + getLLVMStyleWithColumns(25))); + EXPECT_EQ("#define A \\\n" + " string s = \\\n" + " \"123456789\" \\\n" + " \"0\"; \\\n" + " int i;", + format("#define A string s = \"1234567890\"; int i;", + getLLVMStyleWithColumns(20))); + // FIXME: Put additional penalties on breaking at non-whitespace locations. + EXPECT_EQ("someFunction(\"aaabbbcc \"\n" + " \"dddeeeff\"\n" + " \"f\");", + format("someFunction(\"aaabbbcc dddeeefff\");", + getLLVMStyleWithColumns(25))); +} + TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); @@ -3888,8 +6161,11 @@ TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { "\"00000000\"\n" "\"1\"", format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); - EXPECT_EQ("R\"(\\x\\x00)\"\n", - format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7))); + // FIXME: We probably don't need to care about escape sequences in raw + // literals. + EXPECT_EQ("R\"(\\x)\"\n" + "R\"(\\x00)\"\n", + format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7))); } TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { @@ -3899,12 +6175,1134 @@ TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { verifyFormat("if (foo)\n" " return { forgot_closing_brace();\n" "test();"); - verifyFormat("int a[] = { void forgot_closing_brace()\n" - "{\n" + verifyFormat("int a[] = { void forgot_closing_brace() { f();\n" + "g();\n" + "}"); +} + +TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { + verifyFormat("class X {\n" + " void f() {\n" + " }\n" + "};", + getLLVMStyleWithColumns(12)); +} + +TEST_F(FormatTest, ConfigurableIndentWidth) { + FormatStyle EightIndent = getLLVMStyleWithColumns(18); + EightIndent.IndentWidth = 8; + verifyFormat("void f() {\n" + " someFunction();\n" + " if (true) {\n" + " f();\n" + " }\n" + "}", + EightIndent); + verifyFormat("class X {\n" + " void f() {\n" + " }\n" + "};", + EightIndent); + verifyFormat("int x[] = {\n" + " call(),\n" + " call(),\n" + "};", + EightIndent); +} + +TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { + verifyFormat("void\n" + "f();", + getLLVMStyleWithColumns(8)); +} + +TEST_F(FormatTest, ConfigurableUseOfTab) { + FormatStyle Tab = getLLVMStyleWithColumns(42); + Tab.IndentWidth = 8; + Tab.UseTab = FormatStyle::UT_Always; + Tab.AlignEscapedNewlinesLeft = true; + + EXPECT_EQ("if (aaaaaaaa && // q\n" + " bb)\t\t// w\n" + "\t;", + format("if (aaaaaaaa &&// q\n" + "bb)// w\n" + ";", + Tab)); + EXPECT_EQ("if (aaa && bbb) // w\n" + "\t;", + format("if(aaa&&bbb)// w\n" + ";", + Tab)); + + verifyFormat("class X {\n" + "\tvoid f() {\n" + "\t\tsomeFunction(parameter1,\n" + "\t\t\t parameter2);\n" + "\t}\n" + "};", + Tab); + verifyFormat("#define A \\\n" + "\tvoid f() { \\\n" + "\t\tsomeFunction( \\\n" + "\t\t parameter1, \\\n" + "\t\t parameter2); \\\n" + "\t}", + Tab); + EXPECT_EQ("void f() {\n" + "\tf();\n" + "\tg();\n" + "}", + format("void f() {\n" + "\tf();\n" + "\tg();\n" + "}", + 0, 0, Tab)); + EXPECT_EQ("void f() {\n" + "\tf();\n" + "\tg();\n" + "}", + format("void f() {\n" + "\tf();\n" + "\tg();\n" + "}", + 16, 0, Tab)); + EXPECT_EQ("void f() {\n" + " \tf();\n" + "\tg();\n" + "}", + format("void f() {\n" + " \tf();\n" + " \tg();\n" + "}", + 21, 0, Tab)); + + Tab.TabWidth = 4; + Tab.IndentWidth = 8; + verifyFormat("class TabWidth4Indent8 {\n" + "\t\tvoid f() {\n" + "\t\t\t\tsomeFunction(parameter1,\n" + "\t\t\t\t\t\t\t parameter2);\n" + "\t\t}\n" + "};", + Tab); + + Tab.TabWidth = 4; + Tab.IndentWidth = 4; + verifyFormat("class TabWidth4Indent4 {\n" + "\tvoid f() {\n" + "\t\tsomeFunction(parameter1,\n" + "\t\t\t\t\t parameter2);\n" + "\t}\n" + "};", + Tab); + + Tab.TabWidth = 8; + Tab.IndentWidth = 4; + verifyFormat("class TabWidth8Indent4 {\n" + " void f() {\n" + "\tsomeFunction(parameter1,\n" + "\t\t parameter2);\n" + " }\n" + "};", + Tab); + + Tab.TabWidth = 8; + Tab.IndentWidth = 8; + EXPECT_EQ("/*\n" + "\t a\t\tcomment\n" + "\t in multiple lines\n" + " */", + format(" /*\t \t \n" + " \t \t a\t\tcomment\t \t\n" + " \t \t in multiple lines\t\n" + " \t */", + Tab)); + + Tab.UseTab = FormatStyle::UT_ForIndentation; + verifyFormat("T t[] = {\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "};", + Tab); + verifyFormat("enum A {\n" + "\ta1,\n" + "\ta2,\n" + "\ta3\n" + "};", + Tab); + EXPECT_EQ("if (aaaaaaaa && // q\n" + " bb) // w\n" + "\t;", + format("if (aaaaaaaa &&// q\n" + "bb)// w\n" + ";", + Tab)); + verifyFormat("class X {\n" + "\tvoid f() {\n" + "\t\tsomeFunction(parameter1,\n" + "\t\t parameter2);\n" + "\t}\n" + "};", + Tab); + verifyFormat("{\n" + "\tQ({\n" + "\t\t int a;\n" + "\t\t someFunction(aaaaaaaaaa,\n" + "\t\t bbbbbbbbb);\n" + "\t },\n" + "\t p);\n" + "}", + Tab); + EXPECT_EQ("{\n" + "\t/* aaaa\n" + "\t bbbb */\n" + "}", + format("{\n" + "/* aaaa\n" + " bbbb */\n" + "}", + Tab)); + EXPECT_EQ("{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + format("{\n" + "/*\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "*/\n" + "}", + Tab)); + EXPECT_EQ("{\n" + "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t// bbbbbbbbbbbbb\n" + "}", + format("{\n" + "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "}", + Tab)); + EXPECT_EQ("{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + format("{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + Tab)); + EXPECT_EQ("{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + format("{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + Tab)); + EXPECT_EQ("{\n" + "\t/*\n" + " asdf\n" + "\t*/\n" + "}", + format("{\n" + "\t/*\n" + " asdf\n" + "\t*/\n" + "}", + Tab)); + + Tab.UseTab = FormatStyle::UT_Never; + EXPECT_EQ("/*\n" + " a\t\tcomment\n" + " in multiple lines\n" + " */", + format(" /*\t \t \n" + " \t \t a\t\tcomment\t \t\n" + " \t \t in multiple lines\t\n" + " \t */", + Tab)); + EXPECT_EQ("/* some\n" + " comment */", + 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)); + + EXPECT_EQ("int a; /* some\n" + "comment */", + 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)); + EXPECT_EQ("{\n" + " /*\n" + " * Comment\n" + " */\n" + " int i;\n" + "}", + format("{\n" + "\t/*\n" + "\t * Comment\n" + "\t */\n" + "\t int i;\n" + "}")); +} + +TEST_F(FormatTest, CalculatesOriginalColumn) { + EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "q\"; /* some\n" + " comment */", + format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "q\"; /* some\n" + " comment */", + getLLVMStyle())); + EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" + "/* some\n" + " comment */", + format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" + " /* some\n" + " comment */", + getLLVMStyle())); + EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "qqq\n" + "/* some\n" + " comment */", + format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "qqq\n" + " /* some\n" + " comment */", + getLLVMStyle())); + EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "wwww; /* some\n" + " comment */", + format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "wwww; /* some\n" + " comment */", + getLLVMStyle())); +} + +TEST_F(FormatTest, ConfigurableSpaceAfterControlStatementKeyword) { + FormatStyle NoSpace = getLLVMStyle(); + NoSpace.SpaceAfterControlStatementKeyword = false; + + verifyFormat("while(true)\n" + " continue;", NoSpace); + verifyFormat("for(;;)\n" + " continue;", NoSpace); + verifyFormat("if(true)\n" " f();\n" - " g();\n" + "else if(true)\n" + " f();", NoSpace); + verifyFormat("do {\n" + " do_something();\n" + "} while(something());", NoSpace); + verifyFormat("switch(x) {\n" + "default:\n" + " break;\n" + "}", NoSpace); +} + +TEST_F(FormatTest, ConfigurableSpacesInParentheses) { + FormatStyle Spaces = getLLVMStyle(); + + Spaces.SpacesInParentheses = true; + verifyFormat("call( x, y, z );", Spaces); + verifyFormat("while ( (bool)1 )\n" + " continue;", Spaces); + verifyFormat("for ( ;; )\n" + " continue;", Spaces); + verifyFormat("if ( true )\n" + " f();\n" + "else if ( true )\n" + " f();", Spaces); + verifyFormat("do {\n" + " do_something( (int)i );\n" + "} while ( something() );", Spaces); + verifyFormat("switch ( x ) {\n" + "default:\n" + " break;\n" + "}", Spaces); + + Spaces.SpacesInParentheses = false; + Spaces.SpacesInCStyleCastParentheses = true; + verifyFormat("Type *A = ( Type * )P;", Spaces); + verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); + verifyFormat("x = ( int32 )y;", Spaces); + verifyFormat("int a = ( int )(2.0f);", Spaces); + verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); + 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; + Spaces.SpacesInCStyleCastParentheses = true; + verifyFormat("call(x, y, z);", Spaces); + verifyFormat("while (( bool )1)\n" + " continue;", Spaces); + verifyFormat("for (;;)\n" + " continue;", Spaces); + verifyFormat("if (true)\n" + " f( );\n" + "else if (true)\n" + " f( );", Spaces); + verifyFormat("do {\n" + " do_something(( int )i);\n" + "} while (something( ));", Spaces); + verifyFormat("switch (x) {\n" + "default:\n" + " break;\n" + "}", Spaces); +} + +TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { + verifyFormat("int a = 5;"); + verifyFormat("a += 42;"); + verifyFormat("a or_eq 8;"); + + FormatStyle Spaces = getLLVMStyle(); + Spaces.SpaceBeforeAssignmentOperators = false; + verifyFormat("int a= 5;", Spaces); + verifyFormat("a+= 42;", Spaces); + verifyFormat("a or_eq 8;", Spaces); +} + +TEST_F(FormatTest, LinuxBraceBreaking) { + FormatStyle BreakBeforeBrace = getLLVMStyle(); + BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux; + verifyFormat("namespace a\n" + "{\n" + "class A\n" + "{\n" + " void f()\n" + " {\n" + " if (true) {\n" + " a();\n" + " b();\n" + " }\n" + " }\n" + " void g()\n" + " {\n" + " return;\n" + " }\n" + "}\n" + "}", + BreakBeforeBrace); +} + +TEST_F(FormatTest, StroustrupBraceBreaking) { + FormatStyle BreakBeforeBrace = getLLVMStyle(); + BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup; + verifyFormat("namespace a {\n" + "class A {\n" + " void f()\n" + " {\n" + " if (true) {\n" + " a();\n" + " b();\n" + " }\n" + " }\n" + " void g()\n" + " {\n" + " return;\n" + " }\n" + "}\n" + "}", + BreakBeforeBrace); +} + +TEST_F(FormatTest, AllmanBraceBreaking) { + FormatStyle BreakBeforeBrace = getLLVMStyle(); + BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman; + verifyFormat("namespace a\n" + "{\n" + "class A\n" + "{\n" + " void f()\n" + " {\n" + " if (true)\n" + " {\n" + " a();\n" + " b();\n" + " }\n" + " }\n" + " void g()\n" + " {\n" + " return;\n" + " }\n" + "}\n" + "}", + BreakBeforeBrace); + + verifyFormat("void f()\n" + "{\n" + " if (true)\n" + " {\n" + " a();\n" + " }\n" + " else if (false)\n" + " {\n" + " b();\n" + " }\n" + " else\n" + " {\n" + " c();\n" + " }\n" + "}\n", + BreakBeforeBrace); + + verifyFormat("void f()\n" + "{\n" + " for (int i = 0; i < 10; ++i)\n" + " {\n" + " a();\n" + " }\n" + " while (false)\n" + " {\n" + " b();\n" + " }\n" + " do\n" + " {\n" + " c();\n" + " } while (false)\n" + "}\n", + BreakBeforeBrace); + + verifyFormat("void f(int a)\n" + "{\n" + " switch (a)\n" + " {\n" + " case 0:\n" + " break;\n" + " case 1:\n" + " {\n" + " break;\n" + " }\n" + " case 2:\n" + " {\n" + " }\n" + " break;\n" + " default:\n" + " break;\n" + " }\n" + "}\n", + BreakBeforeBrace); + + verifyFormat("enum X\n" + "{\n" + " Y = 0,\n" + "}\n", + BreakBeforeBrace); + + FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace; + BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; + BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; + verifyFormat("void f(bool b)\n" + "{\n" + " if (b)\n" + " {\n" + " return;\n" + " }\n" + "}\n", + BreakBeforeBraceShortIfs); + verifyFormat("void f(bool b)\n" + "{\n" + " if (b) return;\n" + "}\n", + BreakBeforeBraceShortIfs); + verifyFormat("void f(bool b)\n" + "{\n" + " while (b)\n" + " {\n" + " return;\n" + " }\n" + "}\n", + BreakBeforeBraceShortIfs); +} + +TEST_F(FormatTest, CatchExceptionReferenceBinding) { + verifyFormat("void f() {\n" + " try {\n" + " }\n" + " catch (const Exception &e) {\n" + " }\n" + "}\n", + getLLVMStyle()); +} + +TEST_F(FormatTest, UnderstandsPragmas) { + verifyFormat("#pragma omp reduction(| : var)"); + verifyFormat("#pragma omp reduction(+ : var)"); +} + +bool allStylesEqual(ArrayRef<FormatStyle> Styles) { + for (size_t i = 1; i < Styles.size(); ++i) + if (!(Styles[0] == Styles[i])) + return false; + return true; +} + +TEST_F(FormatTest, GetsPredefinedStyleByName) { + FormatStyle Styles[3]; + + Styles[0] = getLLVMStyle(); + EXPECT_TRUE(getPredefinedStyle("LLVM", &Styles[1])); + EXPECT_TRUE(getPredefinedStyle("lLvM", &Styles[2])); + EXPECT_TRUE(allStylesEqual(Styles)); + + Styles[0] = getGoogleStyle(); + EXPECT_TRUE(getPredefinedStyle("Google", &Styles[1])); + EXPECT_TRUE(getPredefinedStyle("gOOgle", &Styles[2])); + EXPECT_TRUE(allStylesEqual(Styles)); + + Styles[0] = getChromiumStyle(); + EXPECT_TRUE(getPredefinedStyle("Chromium", &Styles[1])); + EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", &Styles[2])); + EXPECT_TRUE(allStylesEqual(Styles)); + + Styles[0] = getMozillaStyle(); + EXPECT_TRUE(getPredefinedStyle("Mozilla", &Styles[1])); + EXPECT_TRUE(getPredefinedStyle("moZILla", &Styles[2])); + EXPECT_TRUE(allStylesEqual(Styles)); + + Styles[0] = getWebKitStyle(); + EXPECT_TRUE(getPredefinedStyle("WebKit", &Styles[1])); + EXPECT_TRUE(getPredefinedStyle("wEbKit", &Styles[2])); + EXPECT_TRUE(allStylesEqual(Styles)); + + EXPECT_FALSE(getPredefinedStyle("qwerty", &Styles[0])); +} + +TEST_F(FormatTest, ParsesConfiguration) { + FormatStyle Style = {}; +#define CHECK_PARSE(TEXT, FIELD, VALUE) \ + EXPECT_NE(VALUE, Style.FIELD); \ + EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ + EXPECT_EQ(VALUE, Style.FIELD) + +#define CHECK_PARSE_BOOL(FIELD) \ + Style.FIELD = false; \ + EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value()); \ + EXPECT_TRUE(Style.FIELD); \ + EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value()); \ + EXPECT_FALSE(Style.FIELD); + + CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); + CHECK_PARSE_BOOL(AlignTrailingComments); + CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); + CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); + CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); + CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); + CHECK_PARSE_BOOL(BinPackParameters); + CHECK_PARSE_BOOL(BreakBeforeBinaryOperators); + CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); + CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); + CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); + CHECK_PARSE_BOOL(DerivePointerBinding); + CHECK_PARSE_BOOL(IndentCaseLabels); + CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); + CHECK_PARSE_BOOL(PointerBindsToType); + CHECK_PARSE_BOOL(Cpp11BracedListStyle); + CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType); + CHECK_PARSE_BOOL(SpacesInParentheses); + CHECK_PARSE_BOOL(SpacesInAngles); + CHECK_PARSE_BOOL(SpaceInEmptyParentheses); + CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); + CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword); + CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); + + CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); + CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", + ConstructorInitializerIndentWidth, 1234u); + CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); + CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); + CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", + PenaltyBreakBeforeFirstCallParameter, 1234u); + CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); + CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", + PenaltyReturnTypeOnItsOwnLine, 1234u); + CHECK_PARSE("SpacesBeforeTrailingComments: 1234", + SpacesBeforeTrailingComments, 1234u); + CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); + CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); + + Style.Standard = FormatStyle::LS_Auto; + CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); + CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); + CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); + CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); + CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); + + Style.UseTab = FormatStyle::UT_ForIndentation; + CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); + CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); + CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); + CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); + CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); + + Style.ColumnLimit = 123; + FormatStyle BaseStyle = getLLVMStyle(); + CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); + CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); + + Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; + CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, + FormatStyle::BS_Attach); + CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, + FormatStyle::BS_Linux); + CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, + FormatStyle::BS_Stroustrup); + + Style.NamespaceIndentation = FormatStyle::NI_All; + CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, + FormatStyle::NI_None); + CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, + FormatStyle::NI_Inner); + CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, + FormatStyle::NI_All); + +#undef CHECK_PARSE +#undef CHECK_PARSE_BOOL +} + +TEST_F(FormatTest, ConfigurationRoundTripTest) { + FormatStyle Style = getLLVMStyle(); + std::string YAML = configurationAsText(Style); + FormatStyle ParsedStyle = {}; + EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); + EXPECT_EQ(Style, ParsedStyle); +} + +TEST_F(FormatTest, WorksFor8bitEncodings) { + EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" + "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" + "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" + "\"\xef\xee\xf0\xf3...\"", + format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " + "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " + "\xef\xee\xf0\xf3...\"", + getLLVMStyleWithColumns(12))); +} + +TEST_F(FormatTest, HandlesUTF8BOM) { + EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); + EXPECT_EQ("\xef\xbb\xbf#include <iostream>", + format("\xef\xbb\xbf#include <iostream>")); + EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", + format("\xef\xbb\xbf\n#include <iostream>")); +} + +// FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. +#if !defined(_MSC_VER) + +TEST_F(FormatTest, CountsUTF8CharactersProperly) { + verifyFormat("\"Однажды в студёную зимнюю пору...\"", + getLLVMStyleWithColumns(35)); + verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", + getLLVMStyleWithColumns(31)); + verifyFormat("// Однажды в студёную зимнюю пору...", + getLLVMStyleWithColumns(36)); + verifyFormat("// 一 二 三 四 五 六 七 八 九 十", + getLLVMStyleWithColumns(32)); + verifyFormat("/* Однажды в студёную зимнюю пору... */", + getLLVMStyleWithColumns(39)); + verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", + getLLVMStyleWithColumns(35)); +} + +TEST_F(FormatTest, SplitsUTF8Strings) { + EXPECT_EQ( + "\"Однажды, в \"\n" + "\"студёную \"\n" + "\"зимнюю \"\n" + "\"пору,\"", + format("\"Однажды, в студёную зимнюю пору,\"", + getLLVMStyleWithColumns(13))); + EXPECT_EQ("\"一 二 三 \"\n" + "\"四 五六 \"\n" + "\"七 八 九 \"\n" + "\"十\"", + format("\"一 二 三 四 五六 七 八 九 十\"", + getLLVMStyleWithColumns(11))); + EXPECT_EQ("\"一\t二 \"\n" + "\"\t三 \"\n" + "\"四 五\t六 \"\n" + "\"\t七 \"\n" + "\"八九十\tqq\"", + format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", + getLLVMStyleWithColumns(11))); +} + + +TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { + EXPECT_EQ("const char *sssss =\n" + " \"一二三四五六七八\\\n" + " 九 十\";", + format("const char *sssss = \"一二三四五六七八\\\n" + " 九 十\";", + getLLVMStyleWithColumns(30))); +} + +TEST_F(FormatTest, SplitsUTF8LineComments) { + EXPECT_EQ("// Я из лесу\n" + "// вышел; был\n" + "// сильный\n" + "// мороз.", + format("// Я из лесу вышел; был сильный мороз.", + getLLVMStyleWithColumns(13))); + EXPECT_EQ("// 一二三\n" + "// 四五六七\n" + "// 八 九\n" + "// 十", + format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); +} + +TEST_F(FormatTest, SplitsUTF8BlockComments) { + EXPECT_EQ("/* Гляжу,\n" + " * поднимается\n" + " * медленно в\n" + " * гору\n" + " * Лошадка,\n" + " * везущая\n" + " * хворосту\n" + " * воз. */", + format("/* Гляжу, поднимается медленно в гору\n" + " * Лошадка, везущая хворосту воз. */", + getLLVMStyleWithColumns(13))); + EXPECT_EQ( + "/* 一二三\n" + " * 四五六七\n" + " * 八 九\n" + " * 十 */", + format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); + EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n" + " * 𝕓𝕪𝕥𝕖\n" + " * 𝖀𝕿𝕱-𝟠 */", + format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12))); +} + +#endif // _MSC_VER + +TEST_F(FormatTest, ConstructorInitializerIndentWidth) { + FormatStyle Style = getLLVMStyle(); + + Style.ConstructorInitializerIndentWidth = 4; + verifyFormat( + "SomeClass::Constructor()\n" + " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" + " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", + Style); + + Style.ConstructorInitializerIndentWidth = 2; + verifyFormat( + "SomeClass::Constructor()\n" + " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" + " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", + Style); + + Style.ConstructorInitializerIndentWidth = 0; + verifyFormat( + "SomeClass::Constructor()\n" + ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" + " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", + Style); + + Style.BreakConstructorInitializersBeforeComma = true; + Style.ConstructorInitializerIndentWidth = 4; + verifyFormat("SomeClass::Constructor()\n" + " : a(a)\n" + " , b(b)\n" + " , c(c) {}", + Style); + + Style.ConstructorInitializerIndentWidth = 2; + verifyFormat("SomeClass::Constructor()\n" + " : a(a)\n" + " , b(b)\n" + " , c(c) {}", + Style); + + Style.ConstructorInitializerIndentWidth = 0; + verifyFormat("SomeClass::Constructor()\n" + ": a(a)\n" + ", b(b)\n" + ", c(c) {}", + Style); + + Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; + Style.ConstructorInitializerIndentWidth = 4; + verifyFormat( + "SomeClass::Constructor()\n" + " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", + Style); + Style.ConstructorInitializerIndentWidth = 4; + Style.ColumnLimit = 60; + verifyFormat("SomeClass::Constructor()\n" + " : aaaaaaaa(aaaaaaaa)\n" + " , aaaaaaaa(aaaaaaaa)\n" + " , aaaaaaaa(aaaaaaaa) {}", + Style); +} + +TEST_F(FormatTest, FormatsWithWebKitStyle) { + FormatStyle Style = getWebKitStyle(); + + // Don't indent in outer namespaces. + verifyFormat("namespace outer {\n" + "int i;\n" + "namespace inner {\n" + " int i;\n" + "} // namespace inner\n" + "} // namespace outer\n" + "namespace other_outer {\n" + "int i;\n" + "}", + Style); + + // Don't indent case labels. + verifyFormat("switch (variable) {\n" + "case 1:\n" + "case 2:\n" + " doSomething();\n" + " break;\n" + "default:\n" + " ++variable;\n" + "}", + 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)); + + // Constructor initializers are formatted one per line with the "," on the + // new line. + verifyFormat("Constructor()\n" + " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" + " aaaaaaaaaaaaaa)\n" + " , aaaaaaaaaaaaaaaaaaaaaaa()\n{\n}", + Style); + + // Access specifiers should be aligned left. + verifyFormat("class C {\n" + "public:\n" + " int i;\n" + "};", + Style); + + // Do not align comments. + verifyFormat("int a; // Do not\n" + "double b; // align comments.", + Style); + + // Accept input's line breaks. + EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" + " || bbbbbbbbbbbbbbb) {\n" + " i++;\n" + "}", + format("if (aaaaaaaaaaaaaaa\n" + "|| bbbbbbbbbbbbbbb) { i++; }", + Style)); + EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" + " i++;\n" + "}", + format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); + + // Don't automatically break all macro definitions (llvm.org/PR17842). + verifyFormat("#define aNumber 10", Style); + // However, generally keep the line breaks that the user authored. + EXPECT_EQ("#define aNumber \\\n" + " 10", + format("#define aNumber \\\n" + " 10", + Style)); +} + +TEST_F(FormatTest, FormatsProtocolBufferDefinitions) { + // It seems that clang-format can format protocol buffer definitions + // (see https://code.google.com/p/protobuf/). + verifyFormat("message SomeMessage {\n" + " required int32 field1 = 1;\n" + " optional string field2 = 2 [default = \"2\"]\n" "}"); } +TEST_F(FormatTest, FormatsLambdas) { + verifyFormat("int c = [b]() mutable {\n" + " return [&b] { return b++; }();\n" + "}();\n"); + verifyFormat("int c = [&] {\n" + " [=] { return b++; }();\n" + "}();\n"); + verifyFormat("int c = [&, &a, a] {\n" + " [=, c, &d] { return b++; }();\n" + "}();\n"); + verifyFormat("int c = [&a, &a, a] {\n" + " [=, a, b, &c] { return b++; }();\n" + "}();\n"); + verifyFormat("auto c = { [&a, &a, a] {\n" + " [=, a, b, &c] { return b++; }();\n" + "} }\n"); + verifyFormat("auto c = { [&a, &a, a] { [=, a, b, &c] {}(); } }\n"); + verifyFormat("void f() {\n" + " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" + "}\n"); + verifyFormat("void f() {\n" + " other(x.begin(), //\n" + " x.end(), //\n" + " [&](int, int) { return 1; });\n" + "}\n"); + + // Not lambdas. + verifyFormat("constexpr char hello[]{ \"hello\" };"); + verifyFormat("double &operator[](int i) { return 0; }\n" + "int i;"); +} + +TEST_F(FormatTest, FormatsBlocks) { + // FIXME: Make whitespace formatting consistent. Ask a ObjC dev how + // it would ideally look. + verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];"); + verifyFormat("int i = {[operation setCompletionBlock : ^{ [self " + "onOperationDone]; }] };"); +} + +TEST_F(FormatTest, SupportsCRLF) { + EXPECT_EQ("int a;\r\n" + "int b;\r\n" + "int c;\r\n", + format("int a;\r\n" + " int b;\r\n" + " int c;\r\n", + getLLVMStyle())); + EXPECT_EQ("int a;\r\n" + "int b;\r\n" + "int c;\r\n", + format("int a;\r\n" + " int b;\n" + " int c;\r\n", + getLLVMStyle())); + EXPECT_EQ("int a;\n" + "int b;\n" + "int c;\n", + format("int a;\r\n" + " int b;\n" + " int c;\n", + getLLVMStyle())); + EXPECT_EQ("\"aaaaaaa \"\r\n" + "\"bbbbbbb\";\r\n", + format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); + EXPECT_EQ("#define A \\\r\n" + " b; \\\r\n" + " c; \\\r\n" + " d;\r\n", + format("#define A \\\r\n" + " b; \\\r\n" + " c; d; \r\n", + getGoogleStyle())); + + EXPECT_EQ("/*\r\n" + "multi line block comments\r\n" + "should not introduce\r\n" + "an extra carriage return\r\n" + "*/\r\n", + format("/*\r\n" + "multi line block comments\r\n" + "should not introduce\r\n" + "an extra carriage return\r\n" + "*/\r\n")); +} + +TEST_F(FormatTest, MunchSemicolonAfterBlocks) { + verifyFormat("MY_CLASS(C) {\n" + " int i;\n" + " int j;\n" + "};"); +} + +TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { + FormatStyle TwoIndent = getLLVMStyleWithColumns(15); + TwoIndent.ContinuationIndentWidth = 2; + + EXPECT_EQ("int i =\n" + " longFunction(\n" + " arg);", + format("int i = longFunction(arg);", TwoIndent)); + + FormatStyle SixIndent = getLLVMStyleWithColumns(20); + SixIndent.ContinuationIndentWidth = 6; + + EXPECT_EQ("int i =\n" + " longFunction(\n" + " arg);", + format("int i = longFunction(arg);", SixIndent)); +} + +TEST_F(FormatTest, SpacesInAngles) { + FormatStyle Spaces = getLLVMStyle(); + Spaces.SpacesInAngles = true; + + verifyFormat("static_cast< int >(arg);", Spaces); + verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); + verifyFormat("f< int, float >();", Spaces); + verifyFormat("template <> g() {}", Spaces); + verifyFormat("template < std::vector< int > > f() {}", Spaces); + + Spaces.Standard = FormatStyle::LS_Cpp03; + Spaces.SpacesInAngles = true; + verifyFormat("A< A< int > >();", Spaces); + + Spaces.SpacesInAngles = false; + verifyFormat("A<A<int> >();", Spaces); + + Spaces.Standard = FormatStyle::LS_Cpp11; + Spaces.SpacesInAngles = true; + verifyFormat("A< A< int > >();", Spaces); + + Spaces.SpacesInAngles = false; + verifyFormat("A<A<int>>();", Spaces); +} + } // end namespace tooling } // end namespace clang |