Skip to content

Commit 1d9d396

Browse files
committed
[clang-format] Allow line breaking with PointerAlignment configured
Fixes #139514. Within a long declaration involving the pointer, the program has allowed breaking between the type and the star when the configuration specified that the star should stick to the variable. Now it also allows breaking between the star and the variable when the configuration specified that the star should stick to the type.
1 parent f607b2a commit 1d9d396

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,8 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
15661566
}
15671567

15681568
if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) ||
1569-
Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon)) {
1569+
Previous.isOneOf(TT_PointerOrReference, tok::coloncolon, tok::equal,
1570+
TT_JsTypeColon)) {
15701571
return ContinuationIndent;
15711572
}
15721573
if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4343,7 +4343,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
43434343
return Style.PenaltyReturnTypeOnItsOwnLine;
43444344
return 200;
43454345
}
4346-
if (Right.is(TT_PointerOrReference))
4346+
if (Left.is(TT_PointerOrReference) || Right.is(TT_PointerOrReference))
43474347
return 190;
43484348
if (Right.is(TT_LambdaArrow))
43494349
return 110;
@@ -6262,8 +6262,10 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
62626262
TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) {
62636263
return true;
62646264
}
6265+
// It is fine to break the line when the * or & should be separate from the
6266+
// name according to the PointerAlignment config.
62656267
if (Left.is(TT_PointerOrReference))
6266-
return false;
6268+
return Right.SpacesRequiredBefore;
62676269
if (Right.isTrailingComment()) {
62686270
// We rely on MustBreakBefore being set correctly here as we should not
62696271
// change the "binding" behavior of a comment.

clang/unittests/Format/FormatTest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8644,6 +8644,38 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
86448644
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
86458645
Style);
86468646

8647+
Style.ColumnLimit = 70;
8648+
verifyFormat(
8649+
"void foo( //\n"
8650+
" const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName*\n"
8651+
" const my_super_super_super_super_long_variable_name) {}",
8652+
Style);
8653+
verifyFormat(
8654+
"void foo(const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName*\n"
8655+
" my_super_super_super_super_long_variable_name) {}",
8656+
Style);
8657+
verifyFormat(
8658+
"void foo(const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName*\n"
8659+
" const my_super_super_super_super_long_variable_name) {}",
8660+
Style);
8661+
8662+
Style.PointerAlignment = FormatStyle::PAS_Middle;
8663+
verifyFormat(
8664+
"void foo( //\n"
8665+
" const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName *\n"
8666+
" const my_super_super_super_super_long_variable_name) {}",
8667+
Style);
8668+
verifyFormat(
8669+
"void foo(\n"
8670+
" const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName *\n"
8671+
" my_super_super_super_super_long_variable_name) {}",
8672+
Style);
8673+
verifyFormat(
8674+
"void foo(\n"
8675+
" const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName *\n"
8676+
" const my_super_super_super_super_long_variable_name) {}",
8677+
Style);
8678+
86478679
Style = getLLVMStyleWithColumns(45);
86488680
Style.PenaltyReturnTypeOnItsOwnLine = 400;
86498681
verifyFormat("template <bool abool, // a comment\n"

0 commit comments

Comments
 (0)