Skip to content

Commit c5bf1fc

Browse files
committed
[clang-format] Reorder TokenAnnotator::canBreakBefore
Move the checks related to breaking before right braces and right parens earlier to avoid conflicting checks that prevent breaking based on the left-hand token. This allows properly formatting declarations with pointers and references.
1 parent 516d6ed commit c5bf1fc

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6105,6 +6105,35 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
61056105
return false;
61066106
}
61076107

6108+
// We can break before an r_brace if there was a corresponding break after
6109+
// the l_brace, which is tracked by BreakBeforeClosingBrace, or if we are
6110+
// in a block indented initialization list.
6111+
if (Right.is(tok::r_brace)) {
6112+
return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) ||
6113+
(Right.isBlockIndentedInitRBrace(Style)));
6114+
}
6115+
6116+
// We only break before r_paren if we're in a block indented context.
6117+
if (Right.is(tok::r_paren)) {
6118+
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
6119+
!Right.MatchingParen) {
6120+
return false;
6121+
}
6122+
auto Next = Right.Next;
6123+
if (Next && Next->is(tok::r_paren))
6124+
Next = Next->Next;
6125+
if (Next && Next->is(tok::l_paren))
6126+
return false;
6127+
const FormatToken *Previous = Right.MatchingParen->Previous;
6128+
return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
6129+
}
6130+
6131+
if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6132+
Right.is(TT_TrailingAnnotation) &&
6133+
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
6134+
return false;
6135+
}
6136+
61086137
if (Left.is(tok::at))
61096138
return false;
61106139
if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
@@ -6260,34 +6289,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
62606289
return false;
62616290
}
62626291

6263-
// We only break before r_brace if there was a corresponding break before
6264-
// the l_brace, which is tracked by BreakBeforeClosingBrace.
6265-
if (Right.is(tok::r_brace)) {
6266-
return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) ||
6267-
(Right.isBlockIndentedInitRBrace(Style)));
6268-
}
6269-
6270-
// We only break before r_paren if we're in a block indented context.
6271-
if (Right.is(tok::r_paren)) {
6272-
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
6273-
!Right.MatchingParen) {
6274-
return false;
6275-
}
6276-
auto Next = Right.Next;
6277-
if (Next && Next->is(tok::r_paren))
6278-
Next = Next->Next;
6279-
if (Next && Next->is(tok::l_paren))
6280-
return false;
6281-
const FormatToken *Previous = Right.MatchingParen->Previous;
6282-
return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
6283-
}
6284-
6285-
if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6286-
Right.is(TT_TrailingAnnotation) &&
6287-
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
6288-
return false;
6289-
}
6290-
62916292
// Allow breaking after a trailing annotation, e.g. after a method
62926293
// declaration.
62936294
if (Left.is(TT_TrailingAnnotation)) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9441,6 +9441,15 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
94419441
" aaaaaaaaaaaaaaaa\n"
94429442
");",
94439443
Style);
9444+
verifyFormat("void foo(\n"
9445+
" void (*foobarpntr)(\n"
9446+
" aaaaaaaaaaaaaaaaaa *,\n"
9447+
" bbbbbbbbbbbbbb *,\n"
9448+
" cccccccccccccccccccc *,\n"
9449+
" dddddddddddddddddd *\n"
9450+
" )\n"
9451+
");",
9452+
Style);
94449453
verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
94459454
" aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
94469455
"};",

0 commit comments

Comments
 (0)