Skip to content

[clang-format] Reorder TokenAnnotator::canBreakBefore #119044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6105,6 +6105,35 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
return false;
}

// We can break before an r_brace if there was a break after the matching
// l_brace, which is tracked by BreakBeforeClosingBrace, or if we are in a
// block-indented initialization list.
if (Right.is(tok::r_brace)) {
return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) ||
(Right.isBlockIndentedInitRBrace(Style)));
}

// We only break before r_paren if we're in a block indented context.
if (Right.is(tok::r_paren)) {
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
!Right.MatchingParen) {
return false;
}
auto Next = Right.Next;
if (Next && Next->is(tok::r_paren))
Next = Next->Next;
if (Next && Next->is(tok::l_paren))
return false;
const FormatToken *Previous = Right.MatchingParen->Previous;
return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
}

if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
Right.is(TT_TrailingAnnotation) &&
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
return false;
}

if (Left.is(tok::at))
return false;
if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
Expand Down Expand Up @@ -6260,34 +6289,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
return false;
}

// We only break before r_brace if there was a corresponding break before
// the l_brace, which is tracked by BreakBeforeClosingBrace.
if (Right.is(tok::r_brace)) {
return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) ||
(Right.isBlockIndentedInitRBrace(Style)));
}

// We only break before r_paren if we're in a block indented context.
if (Right.is(tok::r_paren)) {
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
!Right.MatchingParen) {
return false;
}
auto Next = Right.Next;
if (Next && Next->is(tok::r_paren))
Next = Next->Next;
if (Next && Next->is(tok::l_paren))
return false;
const FormatToken *Previous = Right.MatchingParen->Previous;
return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
}

if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
Right.is(TT_TrailingAnnotation) &&
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
return false;
}

// Allow breaking after a trailing annotation, e.g. after a method
// declaration.
if (Left.is(TT_TrailingAnnotation)) {
Expand Down
9 changes: 9 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9441,6 +9441,15 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
" aaaaaaaaaaaaaaaa\n"
");",
Style);
verifyFormat("void foo(\n"
" void (*foobarpntr)(\n"
" aaaaaaaaaaaaaaaaaa *,\n"
" bbbbbbbbbbbbbb *,\n"
" cccccccccccccccccccc *,\n"
" dddddddddddddddddd *\n"
" )\n"
");",
Style);
verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
" aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
"};",
Expand Down
Loading