Skip to content

[clang-format]: Fix formatting of if statements with BlockIndent #77699

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 5 commits into from
Jan 23, 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
24 changes: 17 additions & 7 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,15 +768,25 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// parenthesis by disallowing any further line breaks if there is no line
// break after the opening parenthesis. Don't break if it doesn't conserve
// columns.
auto IsOpeningBracket = [&](const FormatToken &Tok) {
auto IsStartOfBracedList = [&]() {
return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
Style.Cpp11BracedListStyle;
};
if (!Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
!IsStartOfBracedList()) {
return false;
}
if (!Tok.Previous)
return true;
if (Tok.Previous->isIf())
return Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak;
return !Tok.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
tok::kw_switch);
};
if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) &&
(Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) ||
(Previous.is(tok::l_brace) && Previous.isNot(BK_Block) &&
Style.Cpp11BracedListStyle)) &&
State.Column > getNewLineColumn(State) &&
(!Previous.Previous ||
!Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
tok::kw_switch)) &&
IsOpeningBracket(Previous) && State.Column > getNewLineColumn(State) &&
// Don't do this for simple (no expressions) one-argument function calls
// as that feels like needlessly wasting whitespace, e.g.:
//
Expand Down
40 changes: 36 additions & 4 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25889,8 +25889,8 @@ TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);

verifyFormat("if (quitelongarg !=\n"
" (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
verifyFormat("if (quiteLongArg !=\n"
" (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
"comment\n"
" return;\n"
"}",
Expand All @@ -25903,12 +25903,44 @@ TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);

verifyFormat("if (quitelongarg !=\n"
" (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
verifyFormat("if (quiteLongArg !=\n"
" (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
"comment\n"
" return;\n"
"}",
Style);

verifyFormat("void foo() {\n"
" if (camelCaseName < alsoLongName ||\n"
" anotherEvenLongerName <=\n"
" thisReallyReallyReallyReallyReallyReallyLongerName ||"
"\n"
" otherName < thisLastName) {\n"
" return;\n"
" } else if (quiteLongName < alsoLongName ||\n"
" anotherEvenLongerName <=\n"
" thisReallyReallyReallyReallyReallyReallyLonger"
"Name ||\n"
" otherName < thisLastName) {\n"
" return;\n"
" }\n"
"}",
Style);

Style.ContinuationIndentWidth = 2;
verifyFormat("void foo() {\n"
" if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
" ontoMultipleLines && whenFormattedCorrectly) {\n"
" if (false) {\n"
" return;\n"
" } else if (thisIsRatherALongIfClause && "
"thatIExpectToBeBroken ||\n"
" ontoMultipleLines && whenFormattedCorrectly) {\n"
" return;\n"
" }\n"
" }\n"
"}",
Style);
}

TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
Expand Down