[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent
Gedare Bloom via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 7 16:29:02 PDT 2023
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.
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 #54663.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D154755
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25483,7 +25483,6 @@
verifyFormat("if (quitelongarg !=\n"
" (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
"comment\n"
- " return;\n"
"}",
Style);
@@ -25494,12 +25493,56 @@
"}",
Style);
+ // Treating if clauses as block indents causes a known bug (#54808, #63383)
+ // breaking the following test. It gets formatted instead as:
+ // "if (quitelongarg != (alsolongarg - 1)\n"
+ // ") { // ABC is a very longgggggggggggg comment"
+ // "return;"
+ // "}"
+
+#if 0
verifyFormat("if (quitelongarg !=\n"
" (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
"comment\n"
" return;\n"
"}",
Style);
+#endif
+
+ verifyFormat("void foo() {\n"
+ " if (quitelongname < alsolongname ||\n"
+ " anotherevenlongername <=\n"
+ " thisreallyreallyreallyreallyreallyreallylongername ||"
+ "\n"
+ " othername < thislastname) {\n"
+ " return;\n"
+ " } else if (\n"
+ " quitelongname < alsolongname ||\n"
+ " anotherevenlongername <=\n"
+ " thisreallyreallyreallyreallyreallyreallylongername ||"
+ "\n"
+ " othername < thislastname\n"
+ " ) {\n"
+ " return;\n"
+ " }\n"
+ "}",
+ Style);
+
+ Style.ContinuationIndentWidth = 2;
+ verifyFormat("void foo() {\n"
+ " if (\n"
+ " ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+ " ontoMultipleLines && whenFormattedCorrectly\n"
+ " ) {\n"
+ " if (false) {\n"
+ " } else if (\n"
+ " thisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+ " ontoMultipleLines && whenFormattedCorrectly\n"
+ " ) {\n"
+ " }\n"
+ " }\n"
+ "}",
+ Style);
}
TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -5499,7 +5499,7 @@
if (Next && Next->is(tok::l_paren))
return false;
const FormatToken *Previous = Right.MatchingParen->Previous;
- return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
+ return !(Previous && Previous->is(tok::kw_for));
}
// Allow breaking after a trailing annotation, e.g. after a method
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154755.538295.patch
Type: text/x-patch
Size: 3023 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230707/999e5cc7/attachment-0001.bin>
More information about the cfe-commits
mailing list