Skip to content

Commit ff055d9

Browse files
committed
Fix formatting of if statements with BlockIndent
A bug with BlockIndent prevents line breaks within if (and else if) clauses. While fixing this bug, it appears that AlignAfterOpenBracket is not designed to work with loop and if statements, but AlwaysBreak works on if clauses. The documentation and tests are not clear on whether or not this is intended. This patch preserves the AlwaysBreak behavior and supports BlockIndent on if clauses while fixing the bug. It may be reasonable to go the other way and create an explicit option for alignment of if (and loop) clauses intentionally. Fixes llvm#54663. Differential Revision: https://reviews.llvm.org/D154755
1 parent fb7fe49 commit ff055d9

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
775775
Style.Cpp11BracedListStyle)) &&
776776
State.Column > getNewLineColumn(State) &&
777777
(!Previous.Previous ||
778-
!Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
779-
tok::kw_switch)) &&
778+
!(Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
779+
tok::kw_switch) ||
780+
(Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
781+
Previous.Previous->isIf()))) &&
780782
// Don't do this for simple (no expressions) one-argument function calls
781783
// as that feels like needlessly wasting whitespace, e.g.:
782784
//

clang/unittests/Format/FormatTest.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25889,8 +25889,8 @@ TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
2588925889
"}",
2589025890
Style);
2589125891

25892-
verifyFormat("if (quitelongarg !=\n"
25893-
" (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
25892+
verifyFormat("if (quiteLongArg !=\n"
25893+
" (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
2589425894
"comment\n"
2589525895
" return;\n"
2589625896
"}",
@@ -25903,12 +25903,44 @@ TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
2590325903
"}",
2590425904
Style);
2590525905

25906-
verifyFormat("if (quitelongarg !=\n"
25907-
" (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
25906+
verifyFormat("if (quiteLongArg !=\n"
25907+
" (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
2590825908
"comment\n"
2590925909
" return;\n"
2591025910
"}",
2591125911
Style);
25912+
25913+
verifyFormat("void foo() {\n"
25914+
" if (camelCaseName < alsoLongName ||\n"
25915+
" anotherEvenLongerName <=\n"
25916+
" thisReallyReallyReallyReallyReallyReallyLongerName ||"
25917+
"\n"
25918+
" otherName < thisLastName) {\n"
25919+
" return;\n"
25920+
" } else if (quiteLongName < alsoLongName ||\n"
25921+
" anotherEvenLongerName <=\n"
25922+
" thisReallyReallyReallyReallyReallyReallyLonger"
25923+
"Name ||\n"
25924+
" otherName < thisLastName) {\n"
25925+
" return;\n"
25926+
" }\n"
25927+
"}",
25928+
Style);
25929+
25930+
Style.ContinuationIndentWidth = 2;
25931+
verifyFormat("void foo() {\n"
25932+
" if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
25933+
" ontoMultipleLines && whenFormattedCorrectly) {\n"
25934+
" if (false) {\n"
25935+
" return;\n"
25936+
" } else if (thisIsRatherALongIfClause && "
25937+
"thatIExpectToBeBroken ||\n"
25938+
" ontoMultipleLines && whenFormattedCorrectly) {\n"
25939+
" return;\n"
25940+
" }\n"
25941+
" }\n"
25942+
"}",
25943+
Style);
2591225944
}
2591325945

2591425946
TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {

0 commit comments

Comments
 (0)