Skip to content

Commit 46bf67d

Browse files
authored
[clang-format] Reorder TokenAnnotator::canBreakBefore (#119044)
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 at a minimum.
1 parent aac000a commit 46bf67d

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
@@ -6110,6 +6110,35 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
61106110
return false;
61116111
}
61126112

6113+
// We can break before an r_brace if there was a break after the matching
6114+
// l_brace, which is tracked by BreakBeforeClosingBrace, or if we are in a
6115+
// block-indented initialization list.
6116+
if (Right.is(tok::r_brace)) {
6117+
return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) ||
6118+
(Right.isBlockIndentedInitRBrace(Style)));
6119+
}
6120+
6121+
// We only break before r_paren if we're in a block indented context.
6122+
if (Right.is(tok::r_paren)) {
6123+
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
6124+
!Right.MatchingParen) {
6125+
return false;
6126+
}
6127+
auto Next = Right.Next;
6128+
if (Next && Next->is(tok::r_paren))
6129+
Next = Next->Next;
6130+
if (Next && Next->is(tok::l_paren))
6131+
return false;
6132+
const FormatToken *Previous = Right.MatchingParen->Previous;
6133+
return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
6134+
}
6135+
6136+
if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
6137+
Right.is(TT_TrailingAnnotation) &&
6138+
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
6139+
return false;
6140+
}
6141+
61136142
if (Left.is(tok::at))
61146143
return false;
61156144
if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
@@ -6265,34 +6294,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
62656294
return false;
62666295
}
62676296

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