diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -3194,6 +3194,9 @@ **PenaltyBreakTemplateDeclaration** (``Unsigned``) :versionbadge:`clang-format 7` The penalty for breaking after template declaration. +**PenaltyBreakOpenParenthesis** (``Unsigned``) :versionbadge:`clang-format 14` + The penalty for breaking after ``(``. + **PenaltyExcessCharacter** (``Unsigned``) :versionbadge:`clang-format 3.7` The penalty for each character outside of the column limit. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2895,6 +2895,10 @@ /// \version 7 unsigned PenaltyBreakTemplateDeclaration; + /// The penalty for breaking after ``(``. + /// \version 14 + unsigned PenaltyBreakOpenParenthesis; + /// The penalty for each character outside of the column limit. /// \version 3.7 unsigned PenaltyExcessCharacter; @@ -3781,6 +3785,7 @@ R.PenaltyBreakBeforeFirstCallParameter && PenaltyBreakComment == R.PenaltyBreakComment && PenaltyBreakFirstLessLess == R.PenaltyBreakFirstLessLess && + PenaltyBreakOpenParenthesis == R.PenaltyBreakOpenParenthesis && PenaltyBreakString == R.PenaltyBreakString && PenaltyExcessCharacter == R.PenaltyExcessCharacter && PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -759,6 +759,8 @@ IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); IO.mapOptional("PenaltyBreakTemplateDeclaration", Style.PenaltyBreakTemplateDeclaration); + IO.mapOptional("PenaltyBreakOpenParenthesis", + Style.PenaltyBreakOpenParenthesis); IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", Style.PenaltyReturnTypeOnItsOwnLine); @@ -1233,6 +1235,7 @@ LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19; LLVMStyle.PenaltyBreakTemplateDeclaration = prec::Relational; + LLVMStyle.PenaltyBreakOpenParenthesis = 0; LLVMStyle.PenaltyIndentedWhitespace = 0; LLVMStyle.DisableFormat = false; 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 @@ -2907,6 +2907,8 @@ } if (Left.ClosesTemplateDeclaration) return Style.PenaltyBreakTemplateDeclaration; + if (Left.is(tok::l_paren)) + return Style.PenaltyBreakOpenParenthesis; if (Left.is(TT_ConditionalExpr)) return prec::Conditional; prec::Level Level = Left.getPrecedence(); 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 @@ -18512,6 +18512,17 @@ format("int a = /* long block comment */ 42;", Style)); } +TEST_F(FormatTest, BreakPenaltyAfterLParen) { + FormatStyle Style = getLLVMStyle(); + Style.PenaltyBreakOpenParenthesis = 100; + Style.ColumnLimit = 14; + Style.PenaltyExcessCharacter = 1; + EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", + format("int foo(" + "int aaaaaaaaaaaaaaaaaaaaaaaa);", + Style)); +} + #define EXPECT_ALL_STYLES_EQUAL(Styles) \ for (size_t i = 1; i < Styles.size(); ++i) \ EXPECT_EQ(Styles[0], Styles[i]) \ @@ -18717,6 +18728,8 @@ PenaltyBreakBeforeFirstCallParameter, 1234u); CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", PenaltyBreakTemplateDeclaration, 1234u); + CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis, + 1234u); CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", PenaltyReturnTypeOnItsOwnLine, 1234u);