Skip to content

[clang-format] Add style option PenaltyBreakBeforeMemberAccess #118409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`
Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -5311,6 +5315,7 @@ struct FormatStyle {
PenaltyBreakAssignment == R.PenaltyBreakAssignment &&
PenaltyBreakBeforeFirstCallParameter ==
R.PenaltyBreakBeforeFirstCallParameter &&
PenaltyBreakBeforeMemberAccess == R.PenaltyBreakBeforeMemberAccess &&
PenaltyBreakComment == R.PenaltyBreakComment &&
PenaltyBreakFirstLessLess == R.PenaltyBreakFirstLessLess &&
PenaltyBreakOpenParenthesis == R.PenaltyBreakOpenParenthesis &&
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4313,9 +4313,10 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
//
// aaaaaaa
// .aaaaaaaaa.bbbbbbbb(cccccccc);
return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
? 150
: 35;
const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
return Right.NextOperator && Right.NextOperator->Previous->closesScope()
? std::min(Penalty, 35u)
: Penalty;
}

if (Right.is(TT_TrailingAnnotation) &&
Expand Down
2 changes: 2 additions & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22365,6 +22365,29 @@ TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
Style);
}

TEST_F(FormatTest, BreakPenaltyBeforeMemberAccess) {
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"
" .b(a);",
Style);
Style.PenaltyBreakBeforeMemberAccess = 200;
verifyFormat("foo->bar.b(\n"
" a);",
Style);
}

TEST_F(FormatTest, BreakPenaltyScopeResolution) {
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = 20;
Expand Down
Loading