[clang] Add configuration option PenaltyBreakBeforeMemberAccess (PR #118409)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 27 11:30:35 PST 2025
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/118409
>From 86bc2d2fe03d84879870eec1a8c10912076686c2 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 2 Dec 2024 15:16:39 -0700
Subject: [PATCH 1/6] [clang-format] Add PenaltyBreakBeforeMemberAccess
Add a configuration option to set the penalty for breaking
before a member access operator.
---
clang/include/clang/Format/Format.h | 5 +++++
clang/lib/Format/Format.cpp | 3 +++
clang/lib/Format/TokenAnnotator.cpp | 2 +-
clang/unittests/Format/ConfigParseTest.cpp | 2 ++
clang/unittests/Format/FormatTest.cpp | 13 +++++++++++++
5 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index fd526f189ec833..6f432d1d503154 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -3639,6 +3639,10 @@ struct FormatStyle {
/// \version 3.7
unsigned PenaltyBreakBeforeFirstCallParameter;
+ /// The penalty for breaking before a member access operator (``.``, ``->``).
+ /// \version 20
+ unsigned PenaltyBreakBeforeMemberAccess;
+
/// The penalty for each line break introduced inside a comment.
/// \version 3.7
unsigned PenaltyBreakComment;
@@ -5311,6 +5315,7 @@ struct FormatStyle {
PenaltyBreakAssignment == R.PenaltyBreakAssignment &&
PenaltyBreakBeforeFirstCallParameter ==
R.PenaltyBreakBeforeFirstCallParameter &&
+ PenaltyBreakBeforeMemberAccess == R.PenaltyBreakBeforeMemberAccess &&
PenaltyBreakComment == R.PenaltyBreakComment &&
PenaltyBreakFirstLessLess == R.PenaltyBreakFirstLessLess &&
PenaltyBreakOpenParenthesis == R.PenaltyBreakOpenParenthesis &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c25d9bf7c22519..f02bf95cfeed7e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1091,6 +1091,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("PenaltyBreakAssignment", Style.PenaltyBreakAssignment);
IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
Style.PenaltyBreakBeforeFirstCallParameter);
+ IO.mapOptional("PenaltyBreakBeforeMemberAccess",
+ Style.PenaltyBreakBeforeMemberAccess);
IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
IO.mapOptional("PenaltyBreakFirstLessLess",
Style.PenaltyBreakFirstLessLess);
@@ -1659,6 +1661,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
+ LLVMStyle.PenaltyBreakBeforeMemberAccess = 150;
LLVMStyle.PenaltyBreakComment = 300;
LLVMStyle.PenaltyBreakFirstLessLess = 120;
LLVMStyle.PenaltyBreakOpenParenthesis = 0;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 655766178fbb0e..493ca1fdfd208b 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4314,7 +4314,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
// aaaaaaa
// .aaaaaaaaa.bbbbbbbb(cccccccc);
return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
- ? 150
+ ? Style.PenaltyBreakBeforeMemberAccess
: 35;
}
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 9746aa35478465..10788449a1a1d3 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -266,6 +266,8 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
PenaltyBreakBeforeFirstCallParameter, 1234u);
+ CHECK_PARSE("PenaltyBreakBeforeMemberAccess: 1234",
+ PenaltyBreakBeforeMemberAccess, 1234u);
CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
PenaltyBreakTemplateDeclaration, 1234u);
CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis,
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 265461561d2012..2760333c15b49d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22365,6 +22365,19 @@ TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
Style);
}
+TEST_F(FormatTest, BreakPenaltyBeforeMemberAccess) {
+ FormatStyle Style = getLLVMStyle();
+ Style.ColumnLimit = 8;
+ Style.PenaltyExcessCharacter = 15;
+ verifyFormat("foo->bar\n"
+ " .b(a);",
+ Style);
+ Style.PenaltyBreakBeforeMemberAccess = 200;
+ verifyFormat("foo->bar.b(\n"
+ " a);",
+ Style);
+}
+
TEST_F(FormatTest, BreakPenaltyScopeResolution) {
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = 20;
>From dd96085e38e9329fc51ad77fd1ff11b9f4451475 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 22 Jan 2025 11:35:30 -0700
Subject: [PATCH 2/6] Update release notes
---
clang/docs/ReleaseNotes.rst | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 031c5d84e49f97..a646f899158dd8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1292,6 +1292,7 @@ clang-format
- Adds support for bash globstar in ``.clang-format-ignore``.
- Adds ``WrapNamespaceBodyWithEmptyLines`` option.
- Adds the ``IndentExportBlock`` option.
+- Adds ``PenaltyBreakBeforeMemberAccess`` option.
libclang
--------
>From c00f9d1f63ddebca499e263e1b9d423c658b96d7 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 22 Jan 2025 11:35:41 -0700
Subject: [PATCH 3/6] Regenerate ClangFormatStyleOptions
---
clang/docs/ClangFormatStyleOptions.rst | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 30a2325949f48a..bbb912eb10e94d 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -5198,6 +5198,11 @@ the configuration (without a prefix: ``Auto``).
**PenaltyBreakBeforeFirstCallParameter** (``Unsigned``) :versionbadge:`clang-format 3.7` :ref:`¶ <PenaltyBreakBeforeFirstCallParameter>`
The penalty for breaking a function call after ``call(``.
+.. _PenaltyBreakBeforeMemberAccess:
+
+**PenaltyBreakBeforeMemberAccess** (``Unsigned``) :versionbadge:`clang-format 20` :ref:`¶ <PenaltyBreakBeforeMemberAccess>`
+ The penalty for breaking before a member access operator (``.``, ``->``).
+
.. _PenaltyBreakComment:
**PenaltyBreakComment** (``Unsigned``) :versionbadge:`clang-format 3.7` :ref:`¶ <PenaltyBreakComment>`
>From 68c99bb2e0b14adb0d3d50212c4f3a16db9e2956 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 27 Jan 2025 08:18:28 -0700
Subject: [PATCH 4/6] Invert logic and use for all member accesses if less than
35
---
clang/lib/Format/TokenAnnotator.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 493ca1fdfd208b..a0bb542f4fbe3d 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4313,9 +4313,10 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
//
// aaaaaaa
// .aaaaaaaaa.bbbbbbbb(cccccccc);
- return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
- ? Style.PenaltyBreakBeforeMemberAccess
- : 35;
+ const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
+ return Right.NextOperator && Right.NextOperator->Previous->closesScope()
+ ? std::min(Penalty, 35u)
+ : Penalty;
}
if (Right.is(TT_TrailingAnnotation) &&
>From 5dbd54979ceb61d7da30f6ca20b56599e6af3a58 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 27 Jan 2025 08:45:58 -0700
Subject: [PATCH 5/6] Add suggested test case
---
clang/unittests/Format/FormatTest.cpp | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 2760333c15b49d..a2f522a5b640ed 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22366,7 +22366,17 @@ TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
}
TEST_F(FormatTest, BreakPenaltyBeforeMemberAccess) {
- FormatStyle Style = getLLVMStyle();
+ auto Style = getLLVMStyle();
+ EXPECT_EQ(Style.PenaltyBreakBeforeMemberAccess, 150u);
+
+ Style.ColumnLimit = 60;
+ Style.PenaltyBreakBeforeMemberAccess = 110;
+ verifyFormat("aaaaaaaa.aaaaaaaa.bbbbbbbb()\n"
+ " .ccccccccccccccccccccc(dddddddd);\n"
+ "aaaaaaaa.aaaaaaaa\n"
+ " .bbbbbbbb(cccccccccccccccccccccccccccccccc);",
+ Style);
+
Style.ColumnLimit = 8;
Style.PenaltyExcessCharacter = 15;
verifyFormat("foo->bar\n"
>From 36c29c0997791aeda9922f51452eadd6c90d7b75 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 27 Jan 2025 12:30:19 -0700
Subject: [PATCH 6/6] Simplify testcase as suggested
---
clang/unittests/Format/FormatTest.cpp | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index a2f522a5b640ed..e2725e231b6d83 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22377,12 +22377,7 @@ TEST_F(FormatTest, BreakPenaltyBeforeMemberAccess) {
" .bbbbbbbb(cccccccccccccccccccccccccccccccc);",
Style);
- Style.ColumnLimit = 8;
- Style.PenaltyExcessCharacter = 15;
- verifyFormat("foo->bar\n"
- " .b(a);",
- Style);
- Style.PenaltyBreakBeforeMemberAccess = 200;
+ Style.ColumnLimit = 13;
verifyFormat("foo->bar.b(\n"
" a);",
Style);
More information about the cfe-commits
mailing list