Skip to content

Commit 6233d3d

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 6233d3d

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

clang/lib/Format/TokenAnnotator.cpp

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

6108+
6109+
// We only break before r_brace if there was a corresponding break before
6110+
// the l_brace, which is tracked by BreakBeforeClosingBrace.
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+
}
61086136
if (Left.is(tok::at))
61096137
return false;
61106138
if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
@@ -6260,33 +6288,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
62606288
return false;
62616289
}
62626290

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-
}
62906291

62916292
// Allow breaking after a trailing annotation, e.g. after a method
62926293
// declaration.

clang/unittests/Format/FormatTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9383,6 +9383,13 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
93839383
" aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
93849384
" aaaaaaaaaaaaaaaa);",
93859385
Style);
9386+
verifyFormat("void foo(\n"
9387+
" void (*foobarpntr)(\n"
9388+
" aaaaaaaaaaaaaaaaaa *,\n"
9389+
" bbbbbbbbbbbbbb *,\n"
9390+
" cccccccccccccccccccc *,\n"
9391+
" dddddddddddddddddd *));",
9392+
Style);
93869393
verifyFormat(
93879394
"fooooooooooo(new BARRRRRRRRR(\n"
93889395
" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
@@ -9441,6 +9448,15 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
94419448
" aaaaaaaaaaaaaaaa\n"
94429449
");",
94439450
Style);
9451+
verifyFormat("void foo(\n"
9452+
" void (*foobarpntr)(\n"
9453+
" aaaaaaaaaaaaaaaaaa *,\n"
9454+
" bbbbbbbbbbbbbb *,\n"
9455+
" cccccccccccccccccccc *,\n"
9456+
" dddddddddddddddddd *\n"
9457+
" )\n"
9458+
");",
9459+
Style);
94449460
verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
94459461
" aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
94469462
"};",

0 commit comments

Comments
 (0)