[clang] Add configuration option PenaltyBreakBeforeMemberAccess (PR #118409)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 2 15:09:36 PST 2024
https://github.com/gedare created https://github.com/llvm/llvm-project/pull/118409
The penalty for breaking before a member access is hard-coded to 150. Add a configuration option to allow setting it.
>From dfa4d88b072e99c1c425b85003090a46ca4ec252 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 6383934afa2c40..e1d28ffb20259a 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -3620,6 +3620,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;
@@ -5247,6 +5251,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 ee52972ce66f4a..77930a03228f87 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1076,6 +1076,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);
@@ -1638,6 +1640,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 bc5239209f3aab..135c71dbc11ccb 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4280,7 +4280,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 7fc7492271668b..121cc6f8b11e8a 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -261,6 +261,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 250e51b5421664..aa10a0c213a03a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22215,6 +22215,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 c2a6586e7512433751fb01680ad2d6a93f5846a5 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 2 Dec 2024 15:37:09 -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 0bb2eb820cd726..b6e5dbc97ae602 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -978,6 +978,7 @@ clang-format
``Never``, and ``true`` to ``Always``.
- Adds ``RemoveEmptyLinesInUnwrappedLines`` option.
- Adds ``KeepFormFeed`` option and set it to ``true`` for ``GNU`` style.
+- Adds ``PenaltyBreakBeforeMemberAccess`` option.
libclang
--------
>From 4d355658357eeaacd49cdecd0418afd9b00ae6b0 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 2 Dec 2024 16:06:09 -0700
Subject: [PATCH 3/3] dump format style
---
clang/docs/ClangFormatStyleOptions.rst | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 4be448171699ca..3d38b5a9e3c639 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -5176,6 +5176,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