[PATCH] D126438: [clang-format] Fix an invalid code generation in RemoveBracesLLVM
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed May 25 19:35:32 PDT 2022
owenpan created this revision.
owenpan added reviewers: curdeius, MyDeveloperDay, HazardyKnusperkeks.
owenpan added a project: clang-format.
Herald added a project: All.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Fixes https://github.com/llvm/llvm-project/issues/55706.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D126438
Files:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25349,6 +25349,30 @@
"}",
Style);
+ verifyFormat("if (a)\n"
+ " if (b)\n"
+ " c;\n"
+ " else {\n"
+ " if (d)\n"
+ " e;\n"
+ " }\n"
+ "else\n"
+ " f;",
+ Style);
+
+ verifyFormat("if (a)\n"
+ " if (b)\n"
+ " c;\n"
+ " else {\n"
+ " if (d)\n"
+ " e;\n"
+ " else if (f)\n"
+ " g;\n"
+ " }\n"
+ "else\n"
+ " h;",
+ Style);
+
verifyFormat("if (a)\n"
" b;\n"
"else if (c)\n"
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2587,10 +2587,13 @@
FormatTok->setFinalizedType(TT_ElseLBrace);
ElseLeftBrace = FormatTok;
CompoundStatementIndenter Indenter(this, Style, Line->Level);
- if (parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
- /*MunchSemi=*/true,
- KeepElseBraces) == IfStmtKind::IfOnly) {
- Kind = IfStmtKind::IfElseIf;
+ const IfStmtKind ElseBlockKind =
+ parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
+ /*MunchSemi=*/true, KeepElseBraces);
+ if ((ElseBlockKind == IfStmtKind::IfOnly ||
+ ElseBlockKind == IfStmtKind::IfElseIf) &&
+ FormatTok->is(tok::kw_else)) {
+ KeepElseBraces = true;
}
addUnwrappedLine();
} else if (FormatTok->is(tok::kw_if)) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126438.432168.patch
Type: text/x-patch
Size: 1984 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220526/4b3485cb/attachment.bin>
More information about the cfe-commits
mailing list