Index: docs/ClangFormatStyleOptions.rst =================================================================== --- docs/ClangFormatStyleOptions.rst +++ docs/ClangFormatStyleOptions.rst @@ -160,6 +160,17 @@ argument2); \endcode +**AlignConsecutiveAssignments** (``bool``) + If ``true``, aligns consecutive assignments. + + This will align the assignment operators of consecutive lines. This + will result in formattings like + \code + int aaaa = 12; + int b = 23; + int ccc = 23; + \endcode + **AlignEscapedNewlinesLeft** (``bool``) If ``true``, aligns escaped newlines as far left as possible. Otherwise puts them into the right-most column. Index: include/clang/Format/Format.h =================================================================== --- include/clang/Format/Format.h +++ include/clang/Format/Format.h @@ -247,6 +247,17 @@ /// \brief If \c true, aligns trailing comments. bool AlignTrailingComments; + /// \brief If \c true, aligns consecutive assignments. + /// + /// This will align the assignment operators of consecutive lines. This + /// will result in formattings like + /// \code + /// int aaaa = 12; + /// int b = 23; + /// int ccc = 23; + /// \endcode + bool AlignConsecutiveAssignments; + /// \brief If \c true, aligns escaped newlines as far left as possible. /// Otherwise puts them into the right-most column. bool AlignEscapedNewlinesLeft; Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -174,6 +174,7 @@ IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); IO.mapOptional("AlignOperands", Style.AlignOperands); IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); + IO.mapOptional("AlignConsecutiveAssignments", Style.AlignConsecutiveAssignments); IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", Style.AllowAllParametersOfDeclarationOnNextLine); IO.mapOptional("AllowShortBlocksOnASingleLine", @@ -329,6 +330,7 @@ LLVMStyle.AlignAfterOpenBracket = true; LLVMStyle.AlignOperands = true; LLVMStyle.AlignTrailingComments = true; + LLVMStyle.AlignConsecutiveAssignments = false; LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; LLVMStyle.AllowShortBlocksOnASingleLine = false; Index: lib/Format/WhitespaceManager.h =================================================================== --- lib/Format/WhitespaceManager.h +++ lib/Format/WhitespaceManager.h @@ -164,6 +164,13 @@ /// \c EscapedNewlineColumn for the first tokens or token parts in a line. void calculateLineBreakInformation(); + /// \brief Align consecutive assignments over all \c Changes. + void alignConsecutiveAssignments(); + + /// \brief Align consecutive assignments from change \p Start to change \p End at + /// the specified \p Column. + void alignConsecutiveAssignments(unsigned Start, unsigned End, unsigned Column); + /// \brief Align trailing comments over all \c Changes. void alignTrailingComments(); Index: lib/Format/WhitespaceManager.cpp =================================================================== --- lib/Format/WhitespaceManager.cpp +++ lib/Format/WhitespaceManager.cpp @@ -93,6 +93,7 @@ std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr)); calculateLineBreakInformation(); + alignConsecutiveAssignments(); alignTrailingComments(); alignEscapedNewlines(); generateChanges(); @@ -141,6 +142,97 @@ } } +// Walk through all of the changes and find sequences of “=“ to align. +// To do so, keep track of the lines and whether or not an “=“ was found on +// align. If a “=“ is found on a line, extend the current sequence. If the +// current line cannot be part of a sequence, e.g. because there is an empty +// line before it or it contains non-assignments, finalize the previous +// sequence. +void WhitespaceManager::alignConsecutiveAssignments() { + if (!Style.AlignConsecutiveAssignments) + return; + + unsigned MinColumn = 0; + unsigned StartOfSequence = 0; + unsigned EndOfSequence = 0; + bool FoundAssignmentOnLine = false; + bool FoundLeftParenOnLine = false; + unsigned CurrentLine = 0; + + auto AlignSequence = [&] { + alignConsecutiveAssignments(StartOfSequence, EndOfSequence, MinColumn); + MinColumn = 0; + StartOfSequence = 0; + EndOfSequence = 0; + }; + + for (unsigned i = 0, e = Changes.size(); i != e; ++i) { + if (Changes[i].NewlinesBefore != 0) { + CurrentLine += Changes[i].NewlinesBefore; + if (StartOfSequence > 0 && + (Changes[i].NewlinesBefore > 1 || !FoundAssignmentOnLine)) { + EndOfSequence = i; + AlignSequence(); + } + FoundAssignmentOnLine = false; + FoundLeftParenOnLine = false; + } + + if ((Changes[i].Kind == tok::equal && + (FoundAssignmentOnLine || ((Changes[i].NewlinesBefore > 0 || + Changes[i + 1].NewlinesBefore > 0)))) || + (!FoundLeftParenOnLine && Changes[i].Kind == tok::r_paren)) { + if (StartOfSequence > 0) + AlignSequence(); + } else if (Changes[i].Kind == tok::l_paren) { + FoundLeftParenOnLine = true; + if (!FoundAssignmentOnLine && StartOfSequence > 0) + AlignSequence(); + } else if (!FoundAssignmentOnLine && !FoundLeftParenOnLine && + Changes[i].Kind == tok::equal) { + FoundAssignmentOnLine = true; + EndOfSequence = i; + if (StartOfSequence == 0) + StartOfSequence = i; + + unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; + MinColumn = std::max(MinColumn, ChangeMinColumn); + } + } + + if (StartOfSequence > 0) { + EndOfSequence = Changes.size(); + AlignSequence(); + } +} + +void WhitespaceManager::alignConsecutiveAssignments(unsigned Start, + unsigned End, + unsigned Column) { + bool AlignedAssignment = false; + int PreviousShift = 0; + for (unsigned i = Start; i != End; ++i) { + int Shift = 0; + if (Changes[i].NewlinesBefore > 0) + AlignedAssignment = false; + if (!AlignedAssignment && Changes[i].Kind == tok::equal) { + Shift = Column - Changes[i].StartOfTokenColumn; + AlignedAssignment = true; + PreviousShift = Shift; + } + assert(Shift >= 0); + Changes[i].Spaces += Shift; + if (i + 1 != Changes.size()) + Changes[i + 1].PreviousEndOfTokenColumn += Shift; + Changes[i].StartOfTokenColumn += Shift; + if (AlignedAssignment) { + Changes[i].StartOfTokenColumn += PreviousShift; + if (i + 1 != Changes.size()) + Changes[i + 1].PreviousEndOfTokenColumn += PreviousShift; + } + } +} + void WhitespaceManager::alignTrailingComments() { unsigned MinColumn = 0; unsigned MaxColumn = UINT_MAX; Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -17,9 +17,7 @@ namespace clang { namespace format { -FormatStyle getGoogleStyle() { - return getGoogleStyle(FormatStyle::LK_Cpp); -} +FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } class FormatTest : public ::testing::Test { protected: @@ -36,8 +34,8 @@ return Result; } - std::string - format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { + std::string format(llvm::StringRef Code, + const FormatStyle &Style = getLLVMStyle()) { return format(Code, 0, Code.size(), Style); } @@ -173,8 +171,7 @@ // This might not strictly be correct, but is likely good in all practical // cases. - EXPECT_EQ("int b;\nint a;", - format("int b;int a;", 7, 0, getLLVMStyle())); + EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0, getLLVMStyle())); } TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { @@ -313,14 +310,14 @@ } TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { - verifyFormat("x = (a) and (b);"); - verifyFormat("x = (a) or (b);"); - verifyFormat("x = (a) bitand (b);"); - verifyFormat("x = (a) bitor (b);"); - verifyFormat("x = (a) not_eq (b);"); - verifyFormat("x = (a) and_eq (b);"); - verifyFormat("x = (a) or_eq (b);"); - verifyFormat("x = (a) xor (b);"); + verifyFormat("x = (a) and (b);"); + verifyFormat("x = (a) or (b);"); + verifyFormat("x = (a) bitand (b);"); + verifyFormat("x = (a) bitor (b);"); + verifyFormat("x = (a) not_eq (b);"); + verifyFormat("x = (a) and_eq (b);"); + verifyFormat("x = (a) or_eq (b);"); + verifyFormat("x = (a) xor (b);"); } //===----------------------------------------------------------------------===// @@ -677,7 +674,8 @@ " switch (x) { \\\n" " case a: \\\n" " foo = b; \\\n" - " }", getLLVMStyleWithColumns(20)); + " }", + getLLVMStyleWithColumns(20)); verifyFormat("#define OPERATION_CASE(name) \\\n" " case OP_name: \\\n" " return operations::Operation##name\n", @@ -1020,11 +1018,10 @@ " // at start\n" "}")); - verifyFormat( - "#define A \\\n" - " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" - " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", - getLLVMStyleWithColumns(60)); + verifyFormat("#define A \\\n" + " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" + " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", + getLLVMStyleWithColumns(60)); verifyFormat( "#define A \\\n" " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" @@ -1059,7 +1056,7 @@ " c);", format("SomeFunction(a,\n" " b,\n" - " // comment\n" + " // comment\n" " c);")); EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" " c);", @@ -1292,13 +1289,12 @@ EXPECT_EQ("// a b c d\n" "// e f g\n" "// h i j k", - format("// a b c d e f g h i j k", - getLLVMStyleWithColumns(10))); - EXPECT_EQ("// a b c d\n" - "// e f g\n" - "// h i j k", - format("\\\n// a b c d e f g h i j k", - getLLVMStyleWithColumns(10))); + format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); + EXPECT_EQ( + "// a b c d\n" + "// e f g\n" + "// h i j k", + format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); EXPECT_EQ("if (true) // A comment that\n" " // doesn't fit on\n" " // one line", @@ -1477,18 +1473,18 @@ "doesn't " "fit on one line. */", getLLVMStyleWithColumns(20))); - EXPECT_EQ("/* a b c d\n" - " * e f g\n" - " * h i j k\n" - " */", - format("/* a b c d e f g h i j k */", - getLLVMStyleWithColumns(10))); - EXPECT_EQ("/* a b c d\n" - " * e f g\n" - " * h i j k\n" - " */", - format("\\\n/* a b c d e f g h i j k */", - getLLVMStyleWithColumns(10))); + EXPECT_EQ( + "/* a b c d\n" + " * e f g\n" + " * h i j k\n" + " */", + format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); + EXPECT_EQ( + "/* a b c d\n" + " * e f g\n" + " * h i j k\n" + " */", + format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); EXPECT_EQ("/*\n" "This is a long\n" "comment that doesn't\n" @@ -1498,7 +1494,8 @@ "This is a long " "comment that doesn't " "fit on one line. \n" - "*/", getLLVMStyleWithColumns(20))); + "*/", + getLLVMStyleWithColumns(20))); EXPECT_EQ("/*\n" " * This is a long\n" " * comment that\n" @@ -1510,7 +1507,8 @@ " comment that " " doesn't fit on " " one line. \n" - " */", getLLVMStyleWithColumns(20))); + " */", + getLLVMStyleWithColumns(20))); EXPECT_EQ("/*\n" " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" " * so_it_should_be_broken\n" @@ -1544,7 +1542,8 @@ " doesn't fit on one" " line 1234567890\n" "*/\n" - "}", getLLVMStyleWithColumns(20))); + "}", + getLLVMStyleWithColumns(20))); EXPECT_EQ("{\n" " /*\n" " * This i s\n" @@ -1562,7 +1561,8 @@ " fit on one l i" " n e\n" " */\n" - "}", getLLVMStyleWithColumns(20))); + "}", + getLLVMStyleWithColumns(20))); EXPECT_EQ("/*\n" " * This is a long\n" " * comment that\n" @@ -1571,7 +1571,8 @@ " */", format(" /*\n" " * This is a long comment that doesn't fit on one line\n" - " */", getLLVMStyleWithColumns(20))); + " */", + getLLVMStyleWithColumns(20))); EXPECT_EQ("{\n" " if (something) /* This is a\n" " long\n" @@ -1865,7 +1866,6 @@ "#endif\n" "Five\n" "};")); - } //===----------------------------------------------------------------------===// @@ -2381,9 +2381,9 @@ TEST_F(FormatTest, StaticInitializers) { verifyFormat("static SomeClass SC = {1, 'a'};"); - verifyFormat( - "static SomeClass WithALoooooooooooooooooooongName = {\n" - " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); + verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" + " 100000000, " + "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); // Here, everything other than the "}" would fit on a line. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" @@ -2437,9 +2437,9 @@ " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); - verifyFormat( - "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" - " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};"); + verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" + " {rect.fRight - rect.fLeft, rect.fBottom - " + "rect.fTop}};"); verifyFormat( "SomeArrayOfSomeType a = {\n" @@ -2458,24 +2458,22 @@ " {{1, 2, 3}},\n" " {{1, 2, 3}}};"); - verifyFormat( - "struct {\n" - " unsigned bit;\n" - " const char *const name;\n" - "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" - " {kOsWin, \"Windows\"},\n" - " {kOsLinux, \"Linux\"},\n" - " {kOsCrOS, \"Chrome OS\"}};"); - verifyFormat( - "struct {\n" - " unsigned bit;\n" - " const char *const name;\n" - "} kBitsToOs[] = {\n" - " {kOsMac, \"Mac\"},\n" - " {kOsWin, \"Windows\"},\n" - " {kOsLinux, \"Linux\"},\n" - " {kOsCrOS, \"Chrome OS\"},\n" - "};"); + verifyFormat("struct {\n" + " unsigned bit;\n" + " const char *const name;\n" + "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" + " {kOsWin, \"Windows\"},\n" + " {kOsLinux, \"Linux\"},\n" + " {kOsCrOS, \"Chrome OS\"}};"); + verifyFormat("struct {\n" + " unsigned bit;\n" + " const char *const name;\n" + "} kBitsToOs[] = {\n" + " {kOsMac, \"Mac\"},\n" + " {kOsWin, \"Windows\"},\n" + " {kOsLinux, \"Linux\"},\n" + " {kOsCrOS, \"Chrome OS\"},\n" + "};"); } TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { @@ -2721,10 +2719,8 @@ "void f( );\n" "}")); // Only if the identifier contains at least 5 characters. - EXPECT_EQ("HTTP f();", - format("HTTP\nf();")); - EXPECT_EQ("MACRO\nf();", - format("MACRO\nf();")); + EXPECT_EQ("HTTP f();", format("HTTP\nf();")); + EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); // Only if everything is upper case. EXPECT_EQ("class A : public QObject {\n" " Q_Object A() {}\n" @@ -2740,7 +2736,8 @@ "<< SomeThing;")); verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " - "(n, buffers))\n", getChromiumStyle(FormatStyle::LK_Cpp)); + "(n, buffers))\n", + getChromiumStyle(FormatStyle::LK_Cpp)); } TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { @@ -2862,34 +2859,34 @@ " A(X x)\n" " try : t(0) {} catch (...) {}\n" "};")); - EXPECT_EQ( - "class SomeClass {\n" - "public:\n" - " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" - "};", - format("class SomeClass {\n" - "public:\n" - " SomeClass()\n" - " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" - "};")); - EXPECT_EQ( - "class SomeClass {\n" - "public:\n" - " SomeClass()\n" - " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" - "};", - format("class SomeClass {\n" - "public:\n" - " SomeClass()\n" - " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" - "};", getLLVMStyleWithColumns(40))); + EXPECT_EQ("class SomeClass {\n" + "public:\n" + " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" + "};", + format("class SomeClass {\n" + "public:\n" + " SomeClass()\n" + " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" + "};")); + EXPECT_EQ("class SomeClass {\n" + "public:\n" + " SomeClass()\n" + " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" + "};", + format("class SomeClass {\n" + "public:\n" + " SomeClass()\n" + " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" + "};", + getLLVMStyleWithColumns(40))); } TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { verifyFormat("#define A \\\n" " f({ \\\n" " g(); \\\n" - " });", getLLVMStyleWithColumns(11)); + " });", + getLLVMStyleWithColumns(11)); } TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { @@ -3008,12 +3005,11 @@ getLLVMStyleWithColumns(28)); verifyFormat("#if 1\n" "int i;"); - verifyFormat( - "#if 1\n" - "#endif\n" - "#if 1\n" - "#else\n" - "#endif\n"); + verifyFormat("#if 1\n" + "#endif\n" + "#if 1\n" + "#else\n" + "#endif\n"); verifyFormat("DEBUG({\n" " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" @@ -3037,14 +3033,13 @@ TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { FormatStyle SingleLine = getLLVMStyle(); SingleLine.AllowShortIfStatementsOnASingleLine = true; - verifyFormat( - "#if 0\n" - "#elif 1\n" - "#endif\n" - "void foo() {\n" - " if (test) foo2();\n" - "}", - SingleLine); + verifyFormat("#if 0\n" + "#elif 1\n" + "#endif\n" + "void foo() {\n" + " if (test) foo2();\n" + "}", + SingleLine); } TEST_F(FormatTest, LayoutBlockInsideParens) { @@ -3078,7 +3073,7 @@ EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });", format(" functionCall (aaaa, bbbb, {int i;});")); verifyFormat( - "Aaa(\n" // FIXME: There shouldn't be a linebreak here. + "Aaa(\n" // FIXME: There shouldn't be a linebreak here. " {\n" " int i; // break\n" " },\n" @@ -3331,10 +3326,9 @@ "}"); // Even explicit parentheses stress the precedence enough to make the // additional break unnecessary. - verifyFormat( - "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" - "}"); + verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" + "}"); // This cases is borderline, but with the indentation it is still readable. verifyFormat( "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" @@ -3345,11 +3339,10 @@ // If the LHS is a binary expression, we should still use the additional break // as otherwise the formatting hides the operator precedence. - verifyFormat( - "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" - " 5) {\n" - "}"); + verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" + " 5) {\n" + "}"); FormatStyle OnePerLine = getLLVMStyle(); OnePerLine.BinPackParameters = false; @@ -3440,10 +3433,11 @@ " // comment\n" " + b;", Style); - verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" - " + cc;", - Style); + verifyFormat( + "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" + " + cc;", + Style); // Forced by comments. verifyFormat( @@ -3468,17 +3462,16 @@ FormatStyle Style = getLLVMStyle(); Style.AlignOperands = false; Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; - verifyFormat( - "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" - " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" - " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " > ccccccccccccccccccccccccccccccccccccccccc;", - Style); + verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" + " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" + " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " > ccccccccccccccccccccccccccccccccccccccccc;", + Style); verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" @@ -3690,10 +3683,9 @@ // 1) break amongst arguments. verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" " Cccccccccccccc cccccccccccccc);"); - verifyFormat( - "template \n" - "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" - " TemplateIt *stop) {}"); + verifyFormat("template \n" + "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" + " TemplateIt *stop) {}"); // 2) break after return type. verifyFormat( @@ -3780,10 +3772,9 @@ verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", Style); - verifyFormat( - "void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", - Style); + verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", + Style); } TEST_F(FormatTest, TrailingReturnType) { @@ -3928,10 +3919,9 @@ verifyFormat( "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); - verifyFormat( - "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); // Indent consistently independent of call expression. verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" @@ -4072,14 +4062,13 @@ } TEST_F(FormatTest, FormatsBuilderPattern) { - verifyFormat( - "return llvm::StringSwitch(name)\n" - " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" - " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" - " .StartsWith(\".init\", ORDER_INIT)\n" - " .StartsWith(\".fini\", ORDER_FINI)\n" - " .StartsWith(\".hash\", ORDER_HASH)\n" - " .Default(ORDER_TEXT);\n"); + verifyFormat("return llvm::StringSwitch(name)\n" + " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" + " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" + " .StartsWith(\".init\", ORDER_INIT)\n" + " .StartsWith(\".fini\", ORDER_FINI)\n" + " .StartsWith(\".hash\", ORDER_HASH)\n" + " .Default(ORDER_TEXT);\n"); verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); @@ -4255,18 +4244,15 @@ " aaaaaaaaaaaaaaaaaaaaa));"); FormatStyle Style = getLLVMStyle(); Style.AlignAfterOpenBracket = false; - verifyFormat( - "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", - Style); - verifyFormat( - "SomeLongVariableName->someVeryLongFunctionName(\n" - " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", - Style); - verifyFormat( - "SomeLongVariableName->someFunction(\n" - " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", - Style); + verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", + Style); + verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" + " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", + Style); + verifyFormat("SomeLongVariableName->someFunction(\n" + " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", + Style); verifyFormat( "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", @@ -4359,13 +4345,12 @@ " // comment\n" " ? aaaa\n" " : bbbb;"); - verifyFormat( - "unsigned Indent =\n" - " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" - " ? IndentForLevel[TheLine.Level]\n" - " : TheLine * 2,\n" - " TheLine.InPPDirective, PreviousEndOfLineColumn);", - getLLVMStyleWithColumns(70)); + verifyFormat("unsigned Indent =\n" + " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" + " ? IndentForLevel[TheLine.Level]\n" + " : TheLine * 2,\n" + " TheLine.InPPDirective, PreviousEndOfLineColumn);", + getLLVMStyleWithColumns(70)); verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" " ? aaaaaaaaaaaaaaa\n" " : bbbbbbbbbbbbbbb //\n" @@ -4550,10 +4535,9 @@ " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" " *b = bbbbbbbbbbbbbbbbbbb;", Style); - verifyFormat( - "aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" - " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", - Style); + verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" + " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", + Style); } TEST_F(FormatTest, ConditionalExpressionsInBrackets) { @@ -4617,33 +4601,33 @@ FormatStyle AfterType = getLLVMStyle(); AfterType.AlwaysBreakAfterDefinitionReturnType = true; verifyFormat("const char *\n" - "f(void) {\n" // Break here. + "f(void) {\n" // Break here. " return \"\";\n" "}\n" - "const char *bar(void);\n", // No break here. + "const char *bar(void);\n", // No break here. AfterType); verifyFormat("template \n" "T *\n" - "f(T &c) {\n" // Break here. + "f(T &c) {\n" // Break here. " return NULL;\n" "}\n" - "template T *f(T &c);\n", // No break here. + "template T *f(T &c);\n", // No break here. AfterType); AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup; verifyFormat("const char *\n" - "f(void)\n" // Break here. + "f(void)\n" // Break here. "{\n" " return \"\";\n" "}\n" - "const char *bar(void);\n", // No break here. + "const char *bar(void);\n", // No break here. AfterType); verifyFormat("template \n" - "T *\n" // Problem here: no line break - "f(T &c)\n" // Break here. + "T *\n" // Problem here: no line break + "f(T &c)\n" // Break here. "{\n" " return NULL;\n" "}\n" - "template T *f(T &c);\n", // No break here. + "template T *f(T &c);\n", // No break here. AfterType); } @@ -4740,11 +4724,10 @@ "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); - verifyFormat( - "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" - " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); + verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); verifyFormat( "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); @@ -4878,12 +4861,11 @@ " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" "}"); - verifyFormat( - "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" - " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); + verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" @@ -6251,7 +6233,8 @@ EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); EXPECT_EQ("class C {\n" " A() : b(0) {}\n" - "};", format("class C{A():b(0){}};", NoColumnLimit)); + "};", + format("class C{A():b(0){}};", NoColumnLimit)); EXPECT_EQ("A()\n" " : b(0) {\n" "}", @@ -6639,10 +6622,10 @@ format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); - EXPECT_EQ( - "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", - format( - "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); + EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " + "forAllCells:(BOOL)flag;", + format("- (void)sendAction:(SEL)aSelector to:(id)anObject " + "forAllCells:(BOOL)flag;")); // Very long objectiveC method declaration. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" @@ -6664,7 +6647,7 @@ verifyGoogleFormat("- (void)setDelegate:(id)delegate;"); // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC // protocol lists (but not for template classes): - //verifyFormat("- (void)setDelegate:(id )delegate;"); + // verifyFormat("- (void)setDelegate:(id )delegate;"); verifyFormat("- (int (*)())foo:(int (*)())f;"); verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); @@ -6944,9 +6927,9 @@ verifyFormat("int a = &[foo bar:baz];"); verifyFormat("int a = *[foo bar:baz];"); // FIXME: Make casts work, without breaking f()[4]. - //verifyFormat("int a = (int)[foo bar:baz];"); - //verifyFormat("return (int)[foo bar:baz];"); - //verifyFormat("(void)[foo bar:baz];"); + // verifyFormat("int a = (int)[foo bar:baz];"); + // verifyFormat("return (int)[foo bar:baz];"); + // verifyFormat("(void)[foo bar:baz];"); verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); // Binary operators. @@ -7208,12 +7191,11 @@ verifyFormat( "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); - verifyFormat( - "NSDictionary *d = @{\n" - " @\"nam\" : NSUserNam(),\n" - " @\"dte\" : [NSDate date],\n" - " @\"processInfo\" : [NSProcessInfo processInfo]\n" - "};"); + verifyFormat("NSDictionary *d = @{\n" + " @\"nam\" : NSUserNam(),\n" + " @\"dte\" : [NSDate date],\n" + " @\"processInfo\" : [NSProcessInfo processInfo]\n" + "};"); verifyFormat( "@{\n" " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " @@ -7231,12 +7213,11 @@ "};"); // We should try to be robust in case someone forgets the "@". - verifyFormat( - "NSDictionary *d = {\n" - " @\"nam\" : NSUserNam(),\n" - " @\"dte\" : [NSDate date],\n" - " @\"processInfo\" : [NSProcessInfo processInfo]\n" - "};"); + verifyFormat("NSDictionary *d = {\n" + " @\"nam\" : NSUserNam(),\n" + " @\"dte\" : [NSDate date],\n" + " @\"processInfo\" : [NSProcessInfo processInfo]\n" + "};"); verifyFormat("NSMutableDictionary *dictionary =\n" " [NSMutableDictionary dictionaryWithDictionary:@{\n" " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" @@ -7460,11 +7441,11 @@ "loooooooooooooooooooong);", getLLVMStyleWithColumns(20))); - EXPECT_EQ("f(g(\"long string \"\n" - " \"literal\"),\n" - " b);", - format("f(g(\"long string literal\"), b);", - getLLVMStyleWithColumns(20))); + EXPECT_EQ( + "f(g(\"long string \"\n" + " \"literal\"),\n" + " b);", + format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); EXPECT_EQ("f(g(\"long string \"\n" " \"literal\",\n" " a),\n" @@ -7493,23 +7474,20 @@ " aaaaaaaaaaaaaaaaaaaa,\n" " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); - EXPECT_EQ( - "\"splitmea\"\n" - "\"trandomp\"\n" - "\"oint\"", - format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); - - EXPECT_EQ( - "\"split/\"\n" - "\"pathat/\"\n" - "\"slashes\"", - format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); - - EXPECT_EQ( - "\"split/\"\n" - "\"pathat/\"\n" - "\"slashes\"", - format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); + EXPECT_EQ("\"splitmea\"\n" + "\"trandomp\"\n" + "\"oint\"", + format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); + + EXPECT_EQ("\"split/\"\n" + "\"pathat/\"\n" + "\"slashes\"", + format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); + + EXPECT_EQ("\"split/\"\n" + "\"pathat/\"\n" + "\"slashes\"", + format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); EXPECT_EQ("\"split at \"\n" "\"spaces/at/\"\n" "\"slashes.at.any$\"\n" @@ -7554,12 +7532,11 @@ FormatStyle AlignLeft = getLLVMStyleWithColumns(12); AlignLeft.AlignEscapedNewlinesLeft = true; - EXPECT_EQ( - "#define A \\\n" - " \"some \" \\\n" - " \"text \" \\\n" - " \"other\";", - format("#define A \"some text other\";", AlignLeft)); + EXPECT_EQ("#define A \\\n" + " \"some \" \\\n" + " \"text \" \\\n" + " \"other\";", + format("#define A \"some text other\";", AlignLeft)); } TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { @@ -7723,9 +7700,7 @@ format("#define x(_a) printf(\"foo\"_a);", Style)); } -TEST_F(FormatTest, UnderstandsCpp1y) { - verifyFormat("int bi{1'000'000};"); -} +TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" @@ -7776,10 +7751,8 @@ } TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { - EXPECT_EQ("\"\\a\"", - format("\"\\a\"", getLLVMStyleWithColumns(3))); - EXPECT_EQ("\"\\\"", - format("\"\\\"", getLLVMStyleWithColumns(2))); + EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); + EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); EXPECT_EQ("\"test\"\n" "\"\\n\"", format("\"test\\n\"", getLLVMStyleWithColumns(7))); @@ -7789,8 +7762,7 @@ EXPECT_EQ("\"\\\\\\\\\"\n" "\"\\n\"", format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); - EXPECT_EQ("\"\\uff01\"", - format("\"\\uff01\"", getLLVMStyleWithColumns(7))); + EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); EXPECT_EQ("\"\\uff01\"\n" "\"test\"", format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); @@ -8080,25 +8052,25 @@ Tab)); EXPECT_EQ("/* some\n" " comment */", - format(" \t \t /* some\n" - " \t \t comment */", - Tab)); + format(" \t \t /* some\n" + " \t \t comment */", + Tab)); EXPECT_EQ("int a; /* some\n" " comment */", - format(" \t \t int a; /* some\n" - " \t \t comment */", - Tab)); + format(" \t \t int a; /* some\n" + " \t \t comment */", + Tab)); EXPECT_EQ("int a; /* some\n" "comment */", - format(" \t \t int\ta; /* some\n" - " \t \t comment */", - Tab)); + format(" \t \t int\ta; /* some\n" + " \t \t comment */", + Tab)); EXPECT_EQ("f(\"\t\t\"); /* some\n" " comment */", - format(" \t \t f(\"\t\t\"); /* some\n" - " \t \t comment */", - Tab)); + format(" \t \t f(\"\t\t\"); /* some\n" + " \t \t comment */", + Tab)); EXPECT_EQ("{\n" " /*\n" " * Comment\n" @@ -8151,20 +8123,25 @@ NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; verifyFormat("while(true)\n" - " continue;", NoSpace); + " continue;", + NoSpace); verifyFormat("for(;;)\n" - " continue;", NoSpace); + " continue;", + NoSpace); verifyFormat("if(true)\n" " f();\n" "else if(true)\n" - " f();", NoSpace); + " f();", + NoSpace); verifyFormat("do {\n" " do_something();\n" - "} while(something());", NoSpace); + "} while(something());", + NoSpace); verifyFormat("switch(x) {\n" "default:\n" " break;\n" - "}", NoSpace); + "}", + NoSpace); verifyFormat("auto i = std::make_unique(5);", NoSpace); verifyFormat("size_t x = sizeof(x);", NoSpace); verifyFormat("auto f(int x) -> decltype(x);", NoSpace); @@ -8227,20 +8204,25 @@ verifyFormat("call();", Spaces); verifyFormat("std::function callback;", Spaces); verifyFormat("while ( (bool)1 )\n" - " continue;", Spaces); + " continue;", + Spaces); verifyFormat("for ( ;; )\n" - " continue;", Spaces); + " continue;", + Spaces); verifyFormat("if ( true )\n" " f();\n" "else if ( true )\n" - " f();", Spaces); + " f();", + Spaces); verifyFormat("do {\n" " do_something( (int)i );\n" - "} while ( something() );", Spaces); + "} while ( something() );", + Spaces); verifyFormat("switch ( x ) {\n" "default:\n" " break;\n" - "}", Spaces); + "}", + Spaces); Spaces.SpacesInParentheses = false; Spaces.SpacesInCStyleCastParentheses = true; @@ -8253,27 +8235,31 @@ verifyFormat("#define x (( int )-1)", Spaces); // Run the first set of tests again with: - Spaces.SpacesInParentheses = false, - Spaces.SpaceInEmptyParentheses = true; + Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true; Spaces.SpacesInCStyleCastParentheses = true; verifyFormat("call(x, y, z);", Spaces); verifyFormat("call( );", Spaces); verifyFormat("std::function callback;", Spaces); verifyFormat("while (( bool )1)\n" - " continue;", Spaces); + " continue;", + Spaces); verifyFormat("for (;;)\n" - " continue;", Spaces); + " continue;", + Spaces); verifyFormat("if (true)\n" " f( );\n" "else if (true)\n" - " f( );", Spaces); + " f( );", + Spaces); verifyFormat("do {\n" " do_something(( int )i);\n" - "} while (something( ));", Spaces); + "} while (something( ));", + Spaces); verifyFormat("switch (x) {\n" "default:\n" " break;\n" - "}", Spaces); + "}", + Spaces); // Run the first set of tests again with: Spaces.SpaceAfterCStyleCast = true; @@ -8347,6 +8333,152 @@ verifyFormat("a or_eq 8;", Spaces); } +TEST_F(FormatTest, AlignConsecutiveAssignments) { + FormatStyle Alignment = getLLVMStyle(); + Alignment.AlignConsecutiveAssignments = false; + verifyFormat("int a = 5;\n" + "int oneTwoThree = 123;", + Alignment); + verifyFormat("int a = 5;\n" + "int oneTwoThree = 123;", + Alignment); + + Alignment.AlignConsecutiveAssignments = true; + verifyFormat("int a = 5;\n" + "int oneTwoThree = 123;", + Alignment); + verifyFormat("int a = method();\n" + "int oneTwoThree = 133;", + Alignment); + verifyFormat("a &= 5;\n" + "bcd *= 5;\n" + "ghtyf += 5;\n" + "dvfvdb -= 5;\n" + "a /= 5;\n" + "vdsvsv %= 5;\n" + "sfdbddfbdfbb ^= 5;\n" + "dvsdsv |= 5;\n" + "int dsvvdvsdvvv = 123;", + Alignment); + verifyFormat("int i = 1, j = 10;\n" + "something = 2000;", + Alignment); + verifyFormat("something = 2000;\n" + "int i = 1, j = 10;\n", + Alignment); + verifyFormat("something = 2000;\n" + "another = 911;\n" + "int i = 1, j = 10;\n" + "oneMore = 1;\n" + "i = 2;", + Alignment); + verifyFormat("int a = 5;\n" + "int one = 1;\n" + "method();\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + Alignment); + verifyFormat("int oneTwoThree = 123; // comment\n" + "int oneTwo = 12; // comment", + Alignment); + EXPECT_EQ("int a = 5;\n" + "\n" + "int oneTwoThree = 123;", + format("int a = 5;\n" + "\n" + "int oneTwoThree= 123;", + Alignment)); + EXPECT_EQ("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + format("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + Alignment)); + EXPECT_EQ("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + format("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + Alignment)); + Alignment.AlignEscapedNewlinesLeft = true; + verifyFormat("#define A \\\n" + " int aaaa = 12; \\\n" + " int b = 23; \\\n" + " int ccc = 234; \\\n" + " int dddddddddd = 2345;", + Alignment); + Alignment.AlignEscapedNewlinesLeft = false; + verifyFormat("#define A " + " \\\n" + " int aaaa = 12; " + " \\\n" + " int b = 23; " + " \\\n" + " int ccc = 234; " + " \\\n" + " int dddddddddd = 2345;", + Alignment); + verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " + "k = 4, int l = 5,\n" + " int m = 6) {\n" + " int j = 10;\n" + " otherThing = 1;\n" + "}", + Alignment); + verifyFormat("void SomeFunction(int parameter = 0) {\n" + " int i = 1;\n" + " int j = 2;\n" + " int big = 10000;\n" + "}", + Alignment); + verifyFormat("class C {\n" + "public:\n" + " int i = 1;\n" + " virtual void f() = 0;\n" + "};", + Alignment); + verifyFormat("int i = 1;\n" + "if (SomeType t = getSomething()) {\n" + "}\n" + "int j = 2;\n" + "int big = 10000;", + Alignment); + verifyFormat("int j = 7;\n" + "for (int k = 0; k < N; ++k) {\n" + "}\n" + "int j = 2;\n" + "int big = 10000;\n" + "}", + Alignment); + Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; + verifyFormat("int i = 1;\n" + "LooooooooooongType loooooooooooooooooooooongVariable\n" + " = someLooooooooooooooooongFunction();\n" + "int j = 2;", + Alignment); + Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; + verifyFormat("int i = 1;\n" + "LooooooooooongType loooooooooooooooooooooongVariable =\n" + " someLooooooooooooooooongFunction();\n" + "int j = 2;", + Alignment); + // FIXME: Should align all three assignments + verifyFormat( + "int i = 1;\n" + "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" + " loooooooooooooooooooooongParameterB);\n" + "int j = 2;", + Alignment); +} + TEST_F(FormatTest, LinuxBraceBreaking) { FormatStyle LinuxBraceStyle = getLLVMStyle(); LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; @@ -8778,9 +8910,8 @@ #define EXPECT_ALL_STYLES_EQUAL(Styles) \ for (size_t i = 1; i < Styles.size(); ++i) \ - EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " \ - << Styles.size() \ - << " differs from Style #0" + EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ + << " differs from Style #0" TEST_F(FormatTest, GetsPredefinedStyleByName) { SmallVector Styles; @@ -8886,6 +9017,7 @@ CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); CHECK_PARSE_BOOL(AlignOperands); CHECK_PARSE_BOOL(AlignTrailingComments); + CHECK_PARSE_BOOL(AlignConsecutiveAssignments); CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); @@ -9154,11 +9286,12 @@ Style.BreakBeforeTernaryOperators = true; EXPECT_EQ(0, parseConfiguration("---\n" - "BasedOnStyle: Google\n" - "---\n" - "Language: JavaScript\n" - "IndentWidth: 76\n" - "...\n", &Style).value()); + "BasedOnStyle: Google\n" + "---\n" + "Language: JavaScript\n" + "IndentWidth: 76\n" + "...\n", + &Style).value()); EXPECT_FALSE(Style.BreakBeforeTernaryOperators); EXPECT_EQ(76u, Style.IndentWidth); EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); @@ -9202,8 +9335,7 @@ getLLVMStyleWithColumns(31)); verifyFormat("// Однажды в студёную зимнюю пору...", getLLVMStyleWithColumns(36)); - verifyFormat("// 一 二 三 四 五 六 七 八 九 十", - getLLVMStyleWithColumns(32)); + verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); verifyFormat("/* Однажды в студёную зимнюю пору... */", getLLVMStyleWithColumns(39)); verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", @@ -9221,19 +9353,18 @@ EXPECT_EQ("\"aaaaaaaÄ\"\n" "\"\xc2\x8d\";", format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); + EXPECT_EQ("\"Однажды, в \"\n" + "\"студёную \"\n" + "\"зимнюю \"\n" + "\"пору,\"", + format("\"Однажды, в студёную зимнюю пору,\"", + getLLVMStyleWithColumns(13))); EXPECT_EQ( - "\"Однажды, в \"\n" - "\"студёную \"\n" - "\"зимнюю \"\n" - "\"пору,\"", - format("\"Однажды, в студёную зимнюю пору,\"", - getLLVMStyleWithColumns(13))); - EXPECT_EQ("\"一 二 三 \"\n" - "\"四 五六 \"\n" - "\"七 八 九 \"\n" - "\"十\"", - format("\"一 二 三 四 五六 七 八 九 十\"", - getLLVMStyleWithColumns(11))); + "\"一 二 三 \"\n" + "\"四 五六 \"\n" + "\"七 八 九 \"\n" + "\"十\"", + format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); EXPECT_EQ("\"一\t二 \"\n" "\"\t三 \"\n" "\"四 五\t六 \"\n" @@ -9243,7 +9374,6 @@ getLLVMStyleWithColumns(11))); } - TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { EXPECT_EQ("const char *sssss =\n" " \"一二三四五六七八\\\n" @@ -9426,22 +9556,20 @@ Style); // Wrap before binary operators. - EXPECT_EQ( - "void f()\n" - "{\n" - " if (aaaaaaaaaaaaaaaa\n" - " && bbbbbbbbbbbbbbbbbbbbbbbb\n" - " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" - " return;\n" - "}", - format( - "void f() {\n" - "if (aaaaaaaaaaaaaaaa\n" - "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" - "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" - "return;\n" - "}", - Style)); + EXPECT_EQ("void f()\n" + "{\n" + " if (aaaaaaaaaaaaaaaa\n" + " && bbbbbbbbbbbbbbbbbbbbbbbb\n" + " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" + " return;\n" + "}", + format("void f() {\n" + "if (aaaaaaaaaaaaaaaa\n" + "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" + "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" + "return;\n" + "}", + Style)); // Allow functions on a single line. verifyFormat("void f() { return; }", Style); @@ -9471,7 +9599,8 @@ " , b(b)\n" " , c(c)\n" "{\n" - "}", Style); + "}", + Style); verifyFormat("SomeClass::Constructor()\n" " : a(a)\n" "{\n" @@ -9864,12 +9993,10 @@ verifyFormat("f<<<1, 1>>>();"); verifyFormat("f<<<1, 1, 1, s>>>();"); verifyFormat("f<<>>();"); - EXPECT_EQ("f<<<1, 1>>>();", - format("f <<< 1, 1 >>> ();")); + EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); verifyFormat("f<<<1, 1>>>();"); verifyFormat("f<1><<<1, 1>>>();"); - EXPECT_EQ("f<<<1, 1>>>();", - format("f< param > <<< 1, 1 >>> ();")); + EXPECT_EQ("f<<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaa<<<\n 1, 1>>>();"); } @@ -10012,9 +10139,7 @@ " int k;")); } -TEST_F(FormatTest, DoNotCrashOnInvalidInput) { - format("? ) ="); -} +TEST_F(FormatTest, DoNotCrashOnInvalidInput) { format("? ) ="); } } // end namespace tooling } // end namespace clang