summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorG. Pery <gpery@pm.me>2022-01-03 20:55:13 +0100
committerMarek Kurdej <marek.kurdej+llvm.org@gmail.com>2022-01-03 21:06:34 +0100
commitcfe3180742adfc72ad6f5de55cbfc84befb90c97 (patch)
tree12bfbec084b7f8e0e8119c7e83494bf208dfc269
parent7203140748cec7185479cc413ea68f34d2eced99 (diff)
downloadllvm-cfe3180742adfc72ad6f5de55cbfc84befb90c97.tar.gz
[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
-rw-r--r--clang/docs/ClangFormatStyleOptions.rst3
-rw-r--r--clang/include/clang/Format/Format.h5
-rw-r--r--clang/lib/Format/Format.cpp3
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp2
-rw-r--r--clang/unittests/Format/FormatTest.cpp62
5 files changed, 75 insertions, 0 deletions
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 07c77acb8481..777398f460e0 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -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.
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index dbc406417ba1..5044158a2015 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -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 &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 0ae9fa60d337..fdccb8b15e82 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -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;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 914997a54989..a161ee87e6b5 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -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;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 470b0c7a19e5..49635f3f15ea 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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);