Skip to content

Commit

Permalink
[clang-format] Add style option PenaltyBreakBeforeMemberAccess (#11…
Browse files Browse the repository at this point in the history
…8409)

The penalty for breaking before a member access is hard-coded to 150.
Add a configuration option to allow setting it.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
  • Loading branch information
gedare and owenca authored Jan 28, 2025
1 parent 3a439e2 commit d50ebd4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
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 @@ -1294,6 +1294,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
8 changes: 5 additions & 3 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4313,9 +4313,11 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
//
// aaaaaaa
// .aaaaaaaaa.bbbbbbbb(cccccccc);
return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
? 150
: 35;
const auto *NextOperator = Right.NextOperator;
const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
return NextOperator && 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
18 changes: 18 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22365,6 +22365,24 @@ 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 = 13;
verifyFormat("foo->bar\n"
" .b(a);",
Style);
}

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

0 comments on commit d50ebd4

Please sign in to comment.