Skip to content

Commit

Permalink
[clang-format] Fix mismatched break in BlockIndent (llvm#124998)
Browse files Browse the repository at this point in the history
Near the ColumnLimit a break could be inserted before a right parens
with BlockIndent without a break after the matching left parens. Avoid
these hanging right parens by disallowing breaks before right parens
unless there was a break after the left parens.

Fixes llvm#103306
  • Loading branch information
gedare authored Jan 31, 2025
1 parent b89617d commit b873479
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,13 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
}
}

// Allow breaking before the right parens with block indentation if there was
// a break after the left parens, which is tracked by BreakBeforeClosingParen.
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
Current.is(tok::r_paren)) {
return CurrentState.BreakBeforeClosingParen;
}

// Don't allow breaking before a closing brace of a block-indented braced list
// initializer if there isn't already a break.
if (Current.is(tok::r_brace) && Current.MatchingParen &&
Expand Down
4 changes: 4 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9614,6 +9614,10 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
" auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" ) {};",
Style);
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(\n"
" &bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
");",
Style);
}

TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
Expand Down

0 comments on commit b873479

Please sign in to comment.