diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -720,8 +720,9 @@ (Previous.is(tok::l_brace) && Previous.isNot(BK_Block) && Style.Cpp11BracedListStyle)) && State.Column > getNewLineColumn(State) && - (!Previous.Previous || !Previous.Previous->isOneOf( - tok::kw_for, tok::kw_while, tok::kw_switch)) && + (!Previous.Previous || + !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while, + tok::kw_switch)) && // Don't do this for simple (no expressions) one-argument function calls // as that feels like needlessly wasting whitespace, e.g.: // diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -5006,7 +5006,21 @@ return false; } const FormatToken *Previous = Right.MatchingParen->Previous; - return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf())); + if (Previous && (Previous->is(tok::kw_for) || Previous->isIf())) + return false; + if (Right.Next) { + // Avoid breaking when there is a left parens immediately following + // a right parens, such as in cast operators and indirect function calls. + if (Right.Next->is(tok::l_paren)) + return false; + // Avoid breaking between two consecutive right parens at outermost + // nesting or when dereferencing a function pointer. + if (Right.Next->is(tok::r_paren) && + (Right.NestingLevel == 0 || Previous->is(tok::star))) { + return false; + } + } + return true; } // Allow breaking after a trailing annotation, e.g. after a method diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -7210,53 +7210,59 @@ // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign // and BAS_Align. FormatStyle Style = getLLVMStyleWithColumns(35); - StringRef Input = "functionCall(paramA, paramB, paramC);\n" - "void functionDecl(int A, int B, int C);"; Style.AllowAllArgumentsOnNextLine = false; Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; - EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" - " paramC);\n" - "void functionDecl(int A, int B,\n" - " int C);"), - format(Input, Style)); + verifyFormat("functionCall(paramA, paramB,\n" + " paramC);\n" + "void functionDecl(int A, int B,\n" + " int C);", + Style); Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; - EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" - " paramC);\n" - "void functionDecl(int A, int B,\n" - " int C);"), - format(Input, Style)); - // However, BAS_AlwaysBreak should take precedence over + verifyFormat("functionCall(paramA, paramB,\n" + " paramC);\n" + "void functionDecl(int A, int B,\n" + " int C);", + Style); + // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over // AllowAllArgumentsOnNextLine. Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; - EXPECT_EQ(StringRef("functionCall(\n" - " paramA, paramB, paramC);\n" - "void functionDecl(\n" - " int A, int B, int C);"), - format(Input, Style)); + verifyFormat("functionCall(\n" + " paramA, paramB, paramC);\n" + "void functionDecl(\n" + " int A, int B, int C);", + Style); + Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; + verifyFormat("functionCall(\n" + " paramA, paramB, paramC\n" + ");\n" + "void functionDecl(\n" + " int A, int B, int C\n" + ");", + Style); // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the // first argument. Style.AllowAllArgumentsOnNextLine = true; Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; - EXPECT_EQ(StringRef("functionCall(\n" - " paramA, paramB, paramC);\n" - "void functionDecl(\n" - " int A, int B, int C);"), - format(Input, Style)); + verifyFormat("functionCall(\n" + " paramA, paramB, paramC);\n" + "void functionDecl(\n" + " int A, int B, int C);", + Style); // It wouldn't fit on one line with aligned parameters so this setting // doesn't change anything for BAS_Align. Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; - EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" - " paramC);\n" - "void functionDecl(int A, int B,\n" - " int C);"), - format(Input, Style)); + verifyFormat("functionCall(paramA, paramB,\n" + " paramC);\n" + "void functionDecl(int A, int B,\n" + " int C);", + Style); Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; - EXPECT_EQ(StringRef("functionCall(\n" - " paramA, paramB, paramC);\n" - "void functionDecl(\n" - " int A, int B, int C);"), - format(Input, Style)); + verifyFormat("functionCall(\n" + " paramA, paramB, paramC);\n" + "void functionDecl(\n" + " int A, int B, int C);", + Style); } TEST_F(FormatTest, BreakBeforeInlineASMColon) { @@ -8435,6 +8441,52 @@ " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" " aaaaaaaaaaaaaaaa);", Style); + + Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; + Style.BinPackArguments = false; + Style.BinPackParameters = false; + verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaa aaaaaaaa,\n" + " aaaaaaaaa aaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + ") {}", + Style); + verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" + " aaaaaaaaaaa aaaaaaaaa,\n" + " aaaaaaaaaaa aaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + ");", + Style); + verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" + " aaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "));", + Style); + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" + "));", + Style); + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" + "));", + Style); + verifyFormat( + "aaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" + " ),\n" + " aaaaaaaaaaaaaaaa\n" + ");", + Style); + verifyFormat( + "aaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" + " ) &&\n" + " aaaaaaaaaaaaaaaa\n" + ");", + Style); } TEST_F(FormatTest, ParenthesesAndOperandAlignment) { @@ -14274,86 +14326,86 @@ } TEST_F(FormatTest, BreaksStringLiteralOperands) { + FormatStyle Style = getLLVMStyleWithColumns(24); // In a function call with two operands, the second can be broken with no line // break before it. - EXPECT_EQ( - "func(a, \"long long \"\n" - " \"long long\");", - format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); + verifyFormat("func(a, \"long long \"\n" + " \"long long\");", + Style); // In a function call with three operands, the second must be broken with a // line break before it. - EXPECT_EQ("func(a,\n" - " \"long long long \"\n" - " \"long\",\n" - " c);", - format("func(a, \"long long long long\", c);", - getLLVMStyleWithColumns(24))); + verifyFormat("func(a,\n" + " \"long long long \"\n" + " \"long\",\n" + " c);", + Style); // In a function call with three operands, the third must be broken with a // line break before it. - EXPECT_EQ("func(a, b,\n" - " \"long long long \"\n" - " \"long\");", - format("func(a, b, \"long long long long\");", - getLLVMStyleWithColumns(24))); + verifyFormat("func(a, b,\n" + " \"long long long \"\n" + " \"long\");", + Style); // In a function call with three operands, both the second and the third must // be broken with a line break before them. - EXPECT_EQ("func(a,\n" - " \"long long long \"\n" - " \"long\",\n" - " \"long long long \"\n" - " \"long\");", - format("func(a, \"long long long long\", \"long long long long\");", - getLLVMStyleWithColumns(24))); + verifyFormat("func(a,\n" + " \"long long long \"\n" + " \"long\",\n" + " \"long long long \"\n" + " \"long\");", + Style); + Style = getStyleWithColumns(Style, 20); // In a chain of << with two operands, the second can be broken with no line // break before it. - EXPECT_EQ("a << \"line line \"\n" - " \"line\";", - format("a << \"line line line\";", getLLVMStyleWithColumns(20))); + verifyFormat("a << \"line line \"\n" + " \"line\";", + Style); // In a chain of << with three operands, the second can be broken with no line // break before it. - EXPECT_EQ( - "abcde << \"line \"\n" - " \"line line\"\n" - " << c;", - format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); + verifyFormat("abcde << \"line \"\n" + " \"line line\"\n" + " << c;", + Style); // In a chain of << with three operands, the third must be broken with a line // break before it. - EXPECT_EQ( - "a << b\n" - " << \"line line \"\n" - " \"line\";", - format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); + verifyFormat("a << b\n" + " << \"line line \"\n" + " \"line\";", + Style); // In a chain of << with three operands, the second can be broken with no line // break before it and the third must be broken with a line break before it. - EXPECT_EQ("abcd << \"line line \"\n" - " \"line\"\n" - " << \"line line \"\n" - " \"line\";", - format("abcd << \"line line line\" << \"line line line\";", - getLLVMStyleWithColumns(20))); + verifyFormat("abcd << \"line line \"\n" + " \"line\"\n" + " << \"line line \"\n" + " \"line\";", + Style); // In a chain of binary operators with two operands, the second can be broken // with no line break before it. - EXPECT_EQ( - "abcd + \"line line \"\n" - " \"line line\";", - format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); + verifyFormat("abcd + \"line line \"\n" + " \"line line\";", + Style); // In a chain of binary operators with three operands, the second must be // broken with a line break before it. - EXPECT_EQ("abcd +\n" - " \"line line \"\n" - " \"line line\" +\n" - " e;", - format("abcd + \"line line line line\" + e;", - getLLVMStyleWithColumns(20))); + verifyFormat("abcd +\n" + " \"line line \"\n" + " \"line line\" +\n" + " e;", + Style); + Style = getStyleWithColumns(Style, 25); // In a function call with two operands, with AlignAfterOpenBracket enabled, // the first must be broken with a line break before it. - FormatStyle Style = getLLVMStyleWithColumns(25); Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; - EXPECT_EQ("someFunction(\n" - " \"long long long \"\n" - " \"long\",\n" - " a);", - format("someFunction(\"long long long long\", a);", Style)); + verifyFormat("someFunction(\n" + " \"long long long \"\n" + " \"long\",\n" + " a);", + Style); + Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; + verifyFormat("someFunction(\n" + " \"long long long \"\n" + " \"long\",\n" + " a\n" + ");", + Style); } TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { @@ -16075,6 +16127,23 @@ " FoooooooooLooooong);\n" "}", Spaces); + + Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; + verifyFormat("void foo( ) {\n" + " size_t foo = (*(function))(\n" + " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " + "BarrrrrrrrrrrrLong,\n" + " FoooooooooLooooong\n" + " );\n" + "}", + Spaces); + verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); + verifyFormat("size_t idx = (size_t)a;", Spaces); + verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); + verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); + verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); + verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); + verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); } TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {