Skip to content

[clang-format] Fix a bug in BWACS_MultiLine #135906

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 1 commit into from
Apr 17, 2025
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
15 changes: 13 additions & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4153,8 +4153,18 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
ChildSize + Current->SpacesRequiredBefore;
}

if (Current->is(TT_CtorInitializerColon))
if (Current->is(TT_ControlStatementLBrace)) {
if (Style.ColumnLimit > 0 &&
Style.BraceWrapping.AfterControlStatement ==
FormatStyle::BWACS_MultiLine &&
Line.Level * Style.IndentWidth + Line.Last->TotalLength >
Style.ColumnLimit) {
Current->CanBreakBefore = true;
Current->MustBreakBefore = true;
}
} else if (Current->is(TT_CtorInitializerColon)) {
InFunctionDecl = false;
}

// FIXME: Only calculate this if CanBreakBefore is true once static
// initializers etc. are sorted out.
Expand Down Expand Up @@ -5586,12 +5596,13 @@ static bool isAllmanLambdaBrace(const FormatToken &Tok) {

bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
const FormatToken &Right) const {
const FormatToken &Left = *Right.Previous;
if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
(!Style.RemoveEmptyLinesInUnwrappedLines || &Right == Line.First)) {
return true;
}

const FormatToken &Left = *Right.Previous;

if (Style.BreakFunctionDefinitionParameters && Line.MightBeFunctionDecl &&
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
Left.ParameterCount > 0) {
Expand Down
45 changes: 8 additions & 37 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,43 +424,14 @@ class LineJoiner {
: 0;
}
// Try to merge a control statement block with left brace wrapped.
if (NextLine.First->is(tok::l_brace)) {
if ((TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
tok::kw_for, tok::kw_switch, tok::kw_try,
tok::kw_do, TT_ForEachMacro) ||
(TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
Style.BraceWrapping.AfterControlStatement ==
FormatStyle::BWACS_MultiLine) {
// If possible, merge the next line's wrapped left brace with the
// current line. Otherwise, leave it on the next line, as this is a
// multi-line control statement.
return (Style.ColumnLimit == 0 || TheLine->Level * Style.IndentWidth +
TheLine->Last->TotalLength <=
Style.ColumnLimit)
? 1
: 0;
}
if (TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
tok::kw_for, TT_ForEachMacro)) {
return (Style.BraceWrapping.AfterControlStatement ==
FormatStyle::BWACS_Always)
? tryMergeSimpleBlock(I, E, Limit)
: 0;
}
if (TheLine->First->isOneOf(tok::kw_else, tok::kw_catch) &&
Style.BraceWrapping.AfterControlStatement ==
FormatStyle::BWACS_MultiLine) {
// This case if different from the upper BWACS_MultiLine processing
// in that a preceding r_brace is not on the same line as else/catch
// most likely because of BeforeElse/BeforeCatch set to true.
// If the line length doesn't fit ColumnLimit, leave l_brace on the
// next line to respect the BWACS_MultiLine.
return (Style.ColumnLimit == 0 ||
TheLine->Last->TotalLength <= Style.ColumnLimit)
? 1
: 0;
}
if (NextLine.First->is(TT_ControlStatementLBrace)) {
// If possible, merge the next line's wrapped left brace with the
// current line. Otherwise, leave it on the next line, as this is a
// multi-line control statement.
return Style.BraceWrapping.AfterControlStatement ==
FormatStyle::BWACS_Always
? tryMergeSimpleBlock(I, E, Limit)
: 0;
}
if (PreviousLine && TheLine->First->is(tok::l_brace)) {
switch (PreviousLine->First->Tok.getKind()) {
Expand Down
11 changes: 9 additions & 2 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ class CompoundStatementIndenter {
CompoundStatementIndenter(UnwrappedLineParser *Parser,
const FormatStyle &Style, unsigned &LineLevel)
: CompoundStatementIndenter(Parser, LineLevel,
Style.BraceWrapping.AfterControlStatement,
Style.BraceWrapping.AfterControlStatement ==
FormatStyle::BWACS_Always,
Style.BraceWrapping.IndentBraces) {}
CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel,
bool WrapBrace, bool IndentBrace)
Expand Down Expand Up @@ -3067,7 +3068,7 @@ void UnwrappedLineParser::parseTryCatch() {
parseStructuralElement();
--Line->Level;
}
while (true) {
for (bool SeenCatch = false;;) {
if (FormatTok->is(tok::at))
nextToken();
if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
Expand All @@ -3077,6 +3078,8 @@ void UnwrappedLineParser::parseTryCatch() {
FormatTok->is(Keywords.kw_finally)))) {
break;
}
if (FormatTok->is(tok::kw_catch))
SeenCatch = true;
nextToken();
while (FormatTok->isNot(tok::l_brace)) {
if (FormatTok->is(tok::l_paren)) {
Expand All @@ -3090,6 +3093,10 @@ void UnwrappedLineParser::parseTryCatch() {
}
nextToken();
}
if (SeenCatch) {
FormatTok->setFinalizedType(TT_ControlStatementLBrace);
SeenCatch = false;
}
NeedsUnwrappedLine = false;
Line->MustBeDeclaration = false;
CompoundStatementIndenter Indenter(this, Style, Line->Level);
Expand Down
11 changes: 11 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3419,6 +3419,17 @@ TEST_F(FormatTest, MultiLineControlStatements) {
"{\n"
"};",
Style);

Style = getLLVMStyle();
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
Style.AllowShortLoopsOnASingleLine = true;
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
verifyFormat("if (true) { return; }", Style);
verifyFormat("while (true) { return; }", Style);
// Failing test in https://reviews.llvm.org/D114521#3151727
verifyFormat("for (;;) { bar(); }", Style);
}

TEST_F(FormatTest, BeforeWhile) {
Expand Down
Loading