[clang] Add configuration option PenaltyBreakBeforeMemberAccess (PR #118409)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 22 10:40:09 PST 2025
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/118409
>From 978e0415002be5ee51d226d15121392a8a0c2c85 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/3] [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 bf5ee281c43119..f5aa956f4033cc 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4316,7 +4316,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 61aa140dfdc9c0..afdc74be38ba5b 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 110219c95210fe4840199d84f0b7f4a2cb782799 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/3] 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 cad17c1b3957b6..ad89c279145db4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1242,6 +1242,7 @@ clang-format
- Adds support for bash globstar in ``.clang-format-ignore``.
- Adds ``WrapNamespaceBodyWithEmptyLines`` option.
- Adds the ``IndentExportBlock`` option.
+- Adds ``PenaltyBreakBeforeMemberAccess`` option.
libclang
--------
>From d5661c80beb5e4448e1d576993b332ced2b412e5 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/3] 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>`
More information about the cfe-commits
mailing list