[PATCH] D132905: [clang-format] Fix a bug in inserting braces at trailing comments
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 30 22:51:07 PDT 2022
This revision was automatically updated to reflect the committed changes.
Closed by commit rGbc10ab8da1b5: [clang-format] Fix a bug in inserting braces at trailing comments (authored by owenpan).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D132905/new/
https://reviews.llvm.org/D132905
Files:
clang/lib/Format/Format.cpp
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
@@ -25201,6 +25201,13 @@
" f();",
Style);
+ verifyFormat("if (a) { //\n"
+ " b = 1;\n"
+ "}",
+ "if (a) //\n"
+ " b = 1;",
+ Style);
+
verifyFormat("if (a) { // comment\n"
" // comment\n"
" f();\n"
@@ -25266,6 +25273,19 @@
"if (a + b > c)\n"
" f();",
Style);
+
+ Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
+ // TODO: Delete the following line after #57421 is fixed.
+ Style.BraceWrapping.AfterFunction = true;
+
+ verifyFormat("if (a) //\n"
+ "{\n"
+ " b = 1;\n"
+ "}",
+ "if (a) //\n"
+ " b = 1;",
+ Style);
}
TEST_F(FormatTest, RemoveBraces) {
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2606,7 +2606,10 @@
if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
PreprocessorDirectives.empty()) {
- Tok = getLastNonComment(*Line);
+ assert(!Line->Tokens.empty());
+ Tok = Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Never
+ ? getLastNonComment(*Line)
+ : Line->Tokens.back().Tok;
assert(Tok);
if (Tok->BraceCount < 0) {
assert(Tok->BraceCount == -1);
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1869,7 +1869,7 @@
std::string Brace;
if (Token->BraceCount < 0) {
assert(Token->BraceCount == -1);
- Brace = '{';
+ Brace = "\n{";
} else {
Brace = '\n' + std::string(Token->BraceCount, '}');
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132905.456862.patch
Type: text/x-patch
Size: 2229 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220831/bb785602/attachment.bin>
More information about the cfe-commits
mailing list