[PATCH] D127532: [clang-format] Fix a bug in RemoveBracesLLVM
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 10 13:12:34 PDT 2022
owenpan created this revision.
owenpan added reviewers: curdeius, HazardyKnusperkeks, MyDeveloperDay.
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.
Remove the braces of an `else` block only if the `r_brace` of the block is followed by an `if`.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D127532
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
@@ -25576,6 +25576,17 @@
" g;",
Style);
+ verifyFormat("if (a) {\n"
+ " b;\n"
+ " c;\n"
+ "} else { // comment\n"
+ " if (d) {\n"
+ " e;\n"
+ " f;\n"
+ " }\n"
+ "}",
+ 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
@@ -2610,6 +2610,7 @@
nextToken();
handleAttributes();
if (FormatTok->is(tok::l_brace)) {
+ const bool FollowedByIf = Tokens->peekNextToken()->is(tok::kw_if);
FormatTok->setFinalizedType(TT_ElseLBrace);
ElseLeftBrace = FormatTok;
CompoundStatementIndenter Indenter(this, Style, Line->Level);
@@ -2621,7 +2622,7 @@
KeepElseBraces = KeepElseBraces ||
ElseBlockKind == IfStmtKind::IfOnly ||
ElseBlockKind == IfStmtKind::IfElseIf;
- } else if (IfLBrace && !IfLBrace->Optional) {
+ } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
KeepElseBraces = true;
assert(ElseLeftBrace->MatchingParen);
markOptionalBraces(ElseLeftBrace);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127532.436022.patch
Type: text/x-patch
Size: 1611 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220610/538bd493/attachment-0001.bin>
More information about the cfe-commits
mailing list