[clang-format] Add penalty for breaking after '('

My team has a vendetta against lines ending with an open parenthesis, thought it might be useful for others too 😊

Reviewed By: HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D116170
This commit is contained in:
G. Pery 2022-01-03 20:55:13 +01:00 committed by Marek Kurdej
parent 7203140748
commit cfe3180742
5 changed files with 75 additions and 0 deletions

View File

@ -3198,6 +3198,9 @@ the configuration (without a prefix: ``Auto``).
**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.

View File

@ -2887,6 +2887,10 @@ struct FormatStyle {
/// \version 3.7
unsigned PenaltyBreakFirstLessLess;
/// The penalty for breaking after ``(``.
/// \version 14
unsigned PenaltyBreakOpenParenthesis;
/// The penalty for each line break introduced inside a string literal.
/// \version 3.7
unsigned PenaltyBreakString;
@ -3781,6 +3785,7 @@ struct FormatStyle {
R.PenaltyBreakBeforeFirstCallParameter &&
PenaltyBreakComment == R.PenaltyBreakComment &&
PenaltyBreakFirstLessLess == R.PenaltyBreakFirstLessLess &&
PenaltyBreakOpenParenthesis == R.PenaltyBreakOpenParenthesis &&
PenaltyBreakString == R.PenaltyBreakString &&
PenaltyExcessCharacter == R.PenaltyExcessCharacter &&
PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine &&

View File

@ -756,6 +756,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
IO.mapOptional("PenaltyBreakFirstLessLess",
Style.PenaltyBreakFirstLessLess);
IO.mapOptional("PenaltyBreakOpenParenthesis",
Style.PenaltyBreakOpenParenthesis);
IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
IO.mapOptional("PenaltyBreakTemplateDeclaration",
Style.PenaltyBreakTemplateDeclaration);
@ -1232,6 +1234,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.PenaltyExcessCharacter = 1000000;
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
LLVMStyle.PenaltyBreakOpenParenthesis = 0;
LLVMStyle.PenaltyBreakTemplateDeclaration = prec::Relational;
LLVMStyle.PenaltyIndentedWhitespace = 0;

View File

@ -2857,6 +2857,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Left.Previous->isOneOf(tok::identifier, tok::greater))
return 500;
if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
return Style.PenaltyBreakOpenParenthesis;
if (Left.is(tok::l_paren) && InFunctionDecl &&
Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
return 100;

View File

@ -18524,6 +18524,66 @@ TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
format("int a = /* long block comment */ 42;", Style));
}
TEST_F(FormatTest, BreakPenaltyAfterLParen) {
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = 8;
Style.PenaltyExcessCharacter = 15;
verifyFormat("int foo(\n"
" int aaaaaaaaaaaaaaaaaaaaaaaa);",
Style);
Style.PenaltyBreakOpenParenthesis = 200;
EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
format("int foo(\n"
" int aaaaaaaaaaaaaaaaaaaaaaaa);",
Style));
}
TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = 5;
Style.PenaltyExcessCharacter = 150;
verifyFormat("foo((\n"
" int)aaaaaaaaaaaaaaaaaaaaaaaa);",
Style);
Style.PenaltyBreakOpenParenthesis = 100000;
EXPECT_EQ("foo((int)\n"
" aaaaaaaaaaaaaaaaaaaaaaaa);",
format("foo((\n"
"int)aaaaaaaaaaaaaaaaaaaaaaaa);",
Style));
}
TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = 4;
Style.PenaltyExcessCharacter = 100;
verifyFormat("for (\n"
" int iiiiiiiiiiiiiiiii =\n"
" 0;\n"
" iiiiiiiiiiiiiiiii <\n"
" 2;\n"
" iiiiiiiiiiiiiiiii++) {\n"
"}",
Style);
Style.PenaltyBreakOpenParenthesis = 1250;
EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n"
" 0;\n"
" iiiiiiiiiiiiiiiii <\n"
" 2;\n"
" iiiiiiiiiiiiiiiii++) {\n"
"}",
format("for (\n"
" int iiiiiiiiiiiiiiiii =\n"
" 0;\n"
" iiiiiiiiiiiiiiiii <\n"
" 2;\n"
" iiiiiiiiiiiiiiiii++) {\n"
"}",
Style));
}
#define EXPECT_ALL_STYLES_EQUAL(Styles) \
for (size_t i = 1; i < Styles.size(); ++i) \
EXPECT_EQ(Styles[0], Styles[i]) \
@ -18729,6 +18789,8 @@ TEST_F(FormatTest, ParsesConfiguration) {
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);