Skip to content

Commit 86bc2d2

Browse files
committed
[clang-format] Add PenaltyBreakBeforeMemberAccess
Add a configuration option to set the penalty for breaking before a member access operator.
1 parent 5aafc6d commit 86bc2d2

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

clang/include/clang/Format/Format.h

Lines changed: 5 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4314,7 +4314,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
43144314
// aaaaaaa
43154315
// .aaaaaaaaa.bbbbbbbb(cccccccc);
43164316
return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
4317-
? 150
4317+
? Style.PenaltyBreakBeforeMemberAccess
43184318
: 35;
43194319
}
43204320

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 2 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22365,6 +22365,19 @@ TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
2236522365
Style);
2236622366
}
2236722367

22368+
TEST_F(FormatTest, BreakPenaltyBeforeMemberAccess) {
22369+
FormatStyle Style = getLLVMStyle();
22370+
Style.ColumnLimit = 8;
22371+
Style.PenaltyExcessCharacter = 15;
22372+
verifyFormat("foo->bar\n"
22373+
" .b(a);",
22374+
Style);
22375+
Style.PenaltyBreakBeforeMemberAccess = 200;
22376+
verifyFormat("foo->bar.b(\n"
22377+
" a);",
22378+
Style);
22379+
}
22380+
2236822381
TEST_F(FormatTest, BreakPenaltyScopeResolution) {
2236922382
FormatStyle Style = getLLVMStyle();
2237022383
Style.ColumnLimit = 20;

0 commit comments

Comments
 (0)