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

2022-01-03 Thread G Pery via Phabricator via cfe-commits
GPery added a comment.

In D116170#3217198 , @curdeius wrote:

> @GPery, do you need help landing this?
> If so, please indicate the name and email you'd like to be used for the 
> commit.

Oh, sure, I didn't realise this part wasn't automatic.
It's "gpery" and gp...@pm.me, thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116170/new/

https://reviews.llvm.org/D116170

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2021-12-22 Thread G Pery via Phabricator via cfe-commits
GPery created this revision.
GPery requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116170

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -18717,6 +18717,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);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ 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();
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ 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);
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ 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 &&
Index: clang/docs/ClangFormatStyleOptions.rst
===
--- clang/docs/ClangFormatStyleOptions.rst
+++ 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.
 


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -18717,6 +18717,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);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ 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))
   

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

2021-12-27 Thread G Pery via Phabricator via cfe-commits
GPery updated this revision to Diff 396317.
GPery edited the summary of this revision.
GPery added a comment.

Add a unit test and initialisation in LLVMStyle


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116170/new/

https://reviews.llvm.org/D116170

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ 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 );",
+  format("int foo("
+ "int );",
+ 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);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ 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();
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ 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;
Index: clang/include/clang/Format/Format.h
===
--- clang/include/clang/Format/Format.h
+++ 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 &&
Index: clang/docs/ClangFormatStyleOptions.rst
===
--- clang/docs/ClangFormatStyleOptions.rst
+++ 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 outsi

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

2021-12-29 Thread G Pery via Phabricator via cfe-commits
GPery created this revision.
GPery requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Resorted and added tests, thanks for the comments <3


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116374

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -18512,6 +18512,75 @@
 format("int a = /* long block comment */ 42;", Style));
 }
 
+TEST_F(FormatTest, BreakPenaltyAfterLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 8;
+  Style.PenaltyExcessCharacter = 15;
+  EXPECT_EQ("int foo(\n"
+"int );",
+format("int foo(\n"
+   "int );",
+   Style));
+  Style.PenaltyBreakOpenParenthesis = 200;
+  EXPECT_EQ("int foo(int );",
+format("int foo(\n"
+   "int );",
+   Style));
+}
+
+TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 5;
+  Style.PenaltyExcessCharacter = 150;
+  EXPECT_EQ("foo((\n"
+"int));",
+format("foo((\n"
+   "int));",
+   Style));
+  Style.PenaltyBreakOpenParenthesis = 10;
+  EXPECT_EQ("foo((int)\n"
+");",
+format("foo((\n"
+   "int));",
+   Style));
+}
+
+TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 4;
+  Style.PenaltyExcessCharacter = 100;
+  EXPECT_EQ("for (\n"
+"int i =\n"
+"0;\n"
+"i <\n"
+"2;\n"
+"i++) {\n"
+"}",
+format("for (\n"
+   "int i =\n"
+   "0;\n"
+   "i <\n"
+   "2;\n"
+   "i++) {\n"
+   "}",
+   Style));
+  Style.PenaltyBreakOpenParenthesis = 1250;
+  EXPECT_EQ("for (int i =\n"
+" 0;\n"
+" i <\n"
+" 2;\n"
+" i++) {\n"
+"}",
+format("for (\n"
+   "int i =\n"
+   "0;\n"
+   "i <\n"
+   "2;\n"
+   "i++) {\n"
+   "}",
+   Style));
+}
+
 #define EXPECT_ALL_STYLES_EQUAL(Styles)\
   for (size_t i = 1; i < Styles.size(); ++i)   \
   EXPECT_EQ(Styles[0], Styles[i])  \
@@ -18717,6 +18786,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);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2857,6 +2857,8 @@
   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;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -756,6 +756,8 @@
 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
 IO.mapOptional("PenaltyBreakFirstLessLess",
Style.PenaltyBreakFirstLessLess);
+IO.mapOptional("PenaltyBreakOpenParenthesis",
+   Style.PenaltyBreakOpenParenthesis);
 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
 IO.mapOptional("Penalt

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

2021-12-29 Thread G Pery via Phabricator via cfe-commits
GPery updated this revision to Diff 396526.
GPery added a comment.

Resorted everything and added more tests, thanks for the comments <3


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116374/new/

https://reviews.llvm.org/D116374

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -18512,6 +18512,75 @@
 format("int a = /* long block comment */ 42;", Style));
 }
 
+TEST_F(FormatTest, BreakPenaltyAfterLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 8;
+  Style.PenaltyExcessCharacter = 15;
+  EXPECT_EQ("int foo(\n"
+"int );",
+format("int foo(\n"
+   "int );",
+   Style));
+  Style.PenaltyBreakOpenParenthesis = 200;
+  EXPECT_EQ("int foo(int );",
+format("int foo(\n"
+   "int );",
+   Style));
+}
+
+TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 5;
+  Style.PenaltyExcessCharacter = 150;
+  EXPECT_EQ("foo((\n"
+"int));",
+format("foo((\n"
+   "int));",
+   Style));
+  Style.PenaltyBreakOpenParenthesis = 10;
+  EXPECT_EQ("foo((int)\n"
+");",
+format("foo((\n"
+   "int));",
+   Style));
+}
+
+TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 4;
+  Style.PenaltyExcessCharacter = 100;
+  EXPECT_EQ("for (\n"
+"int i =\n"
+"0;\n"
+"i <\n"
+"2;\n"
+"i++) {\n"
+"}",
+format("for (\n"
+   "int i =\n"
+   "0;\n"
+   "i <\n"
+   "2;\n"
+   "i++) {\n"
+   "}",
+   Style));
+  Style.PenaltyBreakOpenParenthesis = 1250;
+  EXPECT_EQ("for (int i =\n"
+" 0;\n"
+" i <\n"
+" 2;\n"
+" i++) {\n"
+"}",
+format("for (\n"
+   "int i =\n"
+   "0;\n"
+   "i <\n"
+   "2;\n"
+   "i++) {\n"
+   "}",
+   Style));
+}
+
 #define EXPECT_ALL_STYLES_EQUAL(Styles)\
   for (size_t i = 1; i < Styles.size(); ++i)   \
   EXPECT_EQ(Styles[0], Styles[i])  \
@@ -18717,6 +18786,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);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2857,6 +2857,8 @@
   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;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -756,6 +756,8 @@
 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
 IO.mapOptional("PenaltyBreakFirstLessLess",
Style.PenaltyBreakFirstLessLess);
+IO.mapOptional("PenaltyBreakOpenParenthesis",
+   Style.PenaltyBreakOpenParenthesis);
 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
 IO.mapOptiona

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

2021-12-29 Thread G Pery via Phabricator via cfe-commits
GPery updated this revision to Diff 396527.
GPery added a comment.

Resorted everything and added more tests, thanks for the comments <3


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116170/new/

https://reviews.llvm.org/D116170

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -18512,6 +18512,75 @@
 format("int a = /* long block comment */ 42;", Style));
 }
 
+TEST_F(FormatTest, BreakPenaltyAfterLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 8;
+  Style.PenaltyExcessCharacter = 15;
+  EXPECT_EQ("int foo(\n"
+"int );",
+format("int foo(\n"
+   "int );",
+   Style));
+  Style.PenaltyBreakOpenParenthesis = 200;
+  EXPECT_EQ("int foo(int );",
+format("int foo(\n"
+   "int );",
+   Style));
+}
+
+TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 5;
+  Style.PenaltyExcessCharacter = 150;
+  EXPECT_EQ("foo((\n"
+"int));",
+format("foo((\n"
+   "int));",
+   Style));
+  Style.PenaltyBreakOpenParenthesis = 10;
+  EXPECT_EQ("foo((int)\n"
+");",
+format("foo((\n"
+   "int));",
+   Style));
+}
+
+TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 4;
+  Style.PenaltyExcessCharacter = 100;
+  EXPECT_EQ("for (\n"
+"int i =\n"
+"0;\n"
+"i <\n"
+"2;\n"
+"i++) {\n"
+"}",
+format("for (\n"
+   "int i =\n"
+   "0;\n"
+   "i <\n"
+   "2;\n"
+   "i++) {\n"
+   "}",
+   Style));
+  Style.PenaltyBreakOpenParenthesis = 1250;
+  EXPECT_EQ("for (int i =\n"
+" 0;\n"
+" i <\n"
+" 2;\n"
+" i++) {\n"
+"}",
+format("for (\n"
+   "int i =\n"
+   "0;\n"
+   "i <\n"
+   "2;\n"
+   "i++) {\n"
+   "}",
+   Style));
+}
+
 #define EXPECT_ALL_STYLES_EQUAL(Styles)\
   for (size_t i = 1; i < Styles.size(); ++i)   \
   EXPECT_EQ(Styles[0], Styles[i])  \
@@ -18717,6 +18786,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);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2857,6 +2857,8 @@
   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;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -756,6 +756,8 @@
 IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
 IO.mapOptional("PenaltyBreakFirstLessLess",
Style.PenaltyBreakFirstLessLess);
+IO.mapOptional("PenaltyBreakOpenParenthesis",
+   Style.PenaltyBreakOpenParenthesis);
 IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
 IO.mapOptiona

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

2021-12-29 Thread G Pery via Phabricator via cfe-commits
GPery updated this revision to Diff 396547.
GPery marked 4 inline comments as done.
GPery added a comment.

Now using verifyFormat!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116170/new/

https://reviews.llvm.org/D116170

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -18512,6 +18512,66 @@
 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 );",
+   Style);
+  Style.PenaltyBreakOpenParenthesis = 200;
+  EXPECT_EQ("int foo(int );",
+format("int foo(\n"
+   "int );",
+   Style));
+}
+
+TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 5;
+  Style.PenaltyExcessCharacter = 150;
+  verifyFormat("foo((\n"
+   "int));",
+
+   Style);
+  Style.PenaltyBreakOpenParenthesis = 10;
+  EXPECT_EQ("foo((int)\n"
+");",
+format("foo((\n"
+   "int));",
+   Style));
+}
+
+TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 4;
+  Style.PenaltyExcessCharacter = 100;
+  verifyFormat("for (\n"
+   "int i =\n"
+   "0;\n"
+   "i <\n"
+   "2;\n"
+   "i++) {\n"
+   "}",
+
+   Style);
+  Style.PenaltyBreakOpenParenthesis = 1250;
+  EXPECT_EQ("for (int i =\n"
+" 0;\n"
+" i <\n"
+" 2;\n"
+" i++) {\n"
+"}",
+format("for (\n"
+   "int i =\n"
+   "0;\n"
+   "i <\n"
+   "2;\n"
+   "i++) {\n"
+   "}",
+   Style));
+}
+
 #define EXPECT_ALL_STYLES_EQUAL(Styles)\
   for (size_t i = 1; i < Styles.size(); ++i)   \
   EXPECT_EQ(Styles[0], Styles[i])  \
@@ -18717,6 +18777,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);
Index: clang/lib/Format/TokenAnnotator.cpp
===
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2857,6 +2857,8 @@
   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;
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -756,6 +756,8 @@
 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 @@
   LLVMStyle.PenaltyExcessCharacter = 100;
   LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
   LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
+  LLVMStyle.PenaltyBreakOpenParenthesis = 0;
   LLVMStyle.PenaltyBreakTemplateDeclaration = prec::Relational;
   LLVMStyle.PenaltyIndentedWhitespace = 0;
 
In

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

2021-12-29 Thread G Pery via Phabricator via cfe-commits
GPery added a comment.

Fixed and marked :D


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116170/new/

https://reviews.llvm.org/D116170

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits