Skip to content

Commit d50ebd4

Browse files
gedareowenca
andauthored
[clang-format] Add style option PenaltyBreakBeforeMemberAccess (#118409)
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 <[email protected]>
1 parent 3a439e2 commit d50ebd4

File tree

7 files changed

+39
-3
lines changed

7 files changed

+39
-3
lines changed

clang/docs/ClangFormatStyleOptions.rst

+5
Original file line numberDiff line numberDiff line change
@@ -5198,6 +5198,11 @@ the configuration (without a prefix: ``Auto``).
51985198
**PenaltyBreakBeforeFirstCallParameter** (``Unsigned``) :versionbadge:`clang-format 3.7` :ref:`<PenaltyBreakBeforeFirstCallParameter>`
51995199
The penalty for breaking a function call after ``call(``.
52005200

5201+
.. _PenaltyBreakBeforeMemberAccess:
5202+
5203+
**PenaltyBreakBeforeMemberAccess** (``Unsigned``) :versionbadge:`clang-format 20` :ref:`<PenaltyBreakBeforeMemberAccess>`
5204+
The penalty for breaking before a member access operator (``.``, ``->``).
5205+
52015206
.. _PenaltyBreakComment:
52025207

52035208
**PenaltyBreakComment** (``Unsigned``) :versionbadge:`clang-format 3.7` :ref:`<PenaltyBreakComment>`

clang/docs/ReleaseNotes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,7 @@ clang-format
12941294
- Adds support for bash globstar in ``.clang-format-ignore``.
12951295
- Adds ``WrapNamespaceBodyWithEmptyLines`` option.
12961296
- Adds the ``IndentExportBlock`` option.
1297+
- Adds ``PenaltyBreakBeforeMemberAccess`` option.
12971298

12981299
libclang
12991300
--------

clang/include/clang/Format/Format.h

+5
Original file line numberDiff line numberDiff line change
@@ -3639,6 +3639,10 @@ struct FormatStyle {
36393639
/// \version 3.7
36403640
unsigned PenaltyBreakBeforeFirstCallParameter;
36413641

3642+
/// The penalty for breaking before a member access operator (``.``, ``->``).
3643+
/// \version 20
3644+
unsigned PenaltyBreakBeforeMemberAccess;
3645+
36423646
/// The penalty for each line break introduced inside a comment.
36433647
/// \version 3.7
36443648
unsigned PenaltyBreakComment;
@@ -5311,6 +5315,7 @@ struct FormatStyle {
53115315
PenaltyBreakAssignment == R.PenaltyBreakAssignment &&
53125316
PenaltyBreakBeforeFirstCallParameter ==
53135317
R.PenaltyBreakBeforeFirstCallParameter &&
5318+
PenaltyBreakBeforeMemberAccess == R.PenaltyBreakBeforeMemberAccess &&
53145319
PenaltyBreakComment == R.PenaltyBreakComment &&
53155320
PenaltyBreakFirstLessLess == R.PenaltyBreakFirstLessLess &&
53165321
PenaltyBreakOpenParenthesis == R.PenaltyBreakOpenParenthesis &&

clang/lib/Format/Format.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,8 @@ template <> struct MappingTraits<FormatStyle> {
10911091
IO.mapOptional("PenaltyBreakAssignment", Style.PenaltyBreakAssignment);
10921092
IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
10931093
Style.PenaltyBreakBeforeFirstCallParameter);
1094+
IO.mapOptional("PenaltyBreakBeforeMemberAccess",
1095+
Style.PenaltyBreakBeforeMemberAccess);
10941096
IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
10951097
IO.mapOptional("PenaltyBreakFirstLessLess",
10961098
Style.PenaltyBreakFirstLessLess);
@@ -1659,6 +1661,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
16591661

16601662
LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
16611663
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
1664+
LLVMStyle.PenaltyBreakBeforeMemberAccess = 150;
16621665
LLVMStyle.PenaltyBreakComment = 300;
16631666
LLVMStyle.PenaltyBreakFirstLessLess = 120;
16641667
LLVMStyle.PenaltyBreakOpenParenthesis = 0;

clang/lib/Format/TokenAnnotator.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -4313,9 +4313,11 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
43134313
//
43144314
// aaaaaaa
43154315
// .aaaaaaaaa.bbbbbbbb(cccccccc);
4316-
return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
4317-
? 150
4318-
: 35;
4316+
const auto *NextOperator = Right.NextOperator;
4317+
const auto Penalty = Style.PenaltyBreakBeforeMemberAccess;
4318+
return NextOperator && NextOperator->Previous->closesScope()
4319+
? std::min(Penalty, 35u)
4320+
: Penalty;
43194321
}
43204322

43214323
if (Right.is(TT_TrailingAnnotation) &&

clang/unittests/Format/ConfigParseTest.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ TEST(ConfigParseTest, ParsesConfiguration) {
266266
CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
267267
CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
268268
PenaltyBreakBeforeFirstCallParameter, 1234u);
269+
CHECK_PARSE("PenaltyBreakBeforeMemberAccess: 1234",
270+
PenaltyBreakBeforeMemberAccess, 1234u);
269271
CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
270272
PenaltyBreakTemplateDeclaration, 1234u);
271273
CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis,

clang/unittests/Format/FormatTest.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -22365,6 +22365,24 @@ TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
2236522365
Style);
2236622366
}
2236722367

22368+
TEST_F(FormatTest, BreakPenaltyBeforeMemberAccess) {
22369+
auto Style = getLLVMStyle();
22370+
EXPECT_EQ(Style.PenaltyBreakBeforeMemberAccess, 150u);
22371+
22372+
Style.ColumnLimit = 60;
22373+
Style.PenaltyBreakBeforeMemberAccess = 110;
22374+
verifyFormat("aaaaaaaa.aaaaaaaa.bbbbbbbb()\n"
22375+
" .ccccccccccccccccccccc(dddddddd);\n"
22376+
"aaaaaaaa.aaaaaaaa\n"
22377+
" .bbbbbbbb(cccccccccccccccccccccccccccccccc);",
22378+
Style);
22379+
22380+
Style.ColumnLimit = 13;
22381+
verifyFormat("foo->bar\n"
22382+
" .b(a);",
22383+
Style);
22384+
}
22385+
2236822386
TEST_F(FormatTest, BreakPenaltyScopeResolution) {
2236922387
FormatStyle Style = getLLVMStyle();
2237022388
Style.ColumnLimit = 20;

0 commit comments

Comments
 (0)