[PATCH] D125451: [clang-format] Handle comments below r_brace in RemoveBracesLLVM
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu May 12 00:56:59 PDT 2022
owenpan created this revision.
owenpan added reviewers: curdeius, HazardyKnusperkeks, MyDeveloperDay.
Herald added a project: All.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
If a closing brace is followed by a non-trailing comment, the newline before the closing brace must also be removed.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D125451
Files:
clang/lib/Format/Format.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25348,6 +25348,20 @@
"}",
Style);
+ verifyFormat("if (a)\n"
+ " foo();\n"
+ "// comment\n"
+ "else\n"
+ " bar();",
+ "if (a) {\n"
+ " foo();\n"
+ "}\n"
+ "// comment\n"
+ "else {\n"
+ " bar();\n"
+ "}",
+ Style);
+
verifyFormat("if (a) {\n"
"Label:\n"
"}",
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1898,8 +1898,9 @@
assert(Token->isOneOf(tok::l_brace, tok::r_brace));
assert(Token->Next || Token == Line->Last);
const auto Start =
- Token == Line->Last || (Token->Next->is(tok::kw_else) &&
- Token->Next->NewlinesBefore > 0)
+ Token == Line->Last ||
+ (Token->Next->isOneOf(tok::kw_else, tok::comment) &&
+ Token->Next->NewlinesBefore > 0)
? Token->WhitespaceRange.getBegin()
: Token->Tok.getLocation();
const auto Range =
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125451.428868.patch
Type: text/x-patch
Size: 1462 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220512/d02f4c67/attachment.bin>
More information about the cfe-commits
mailing list