[PATCH] D116767: [clang-format] Fix `BraceWrapping: AfterFunction` affecting synchronized blocks in Java.
Marek Kurdej via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 6 14:13:49 PST 2022
curdeius created this revision.
curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan.
curdeius requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Fixes https://github.com/llvm/llvm-project/issues/32031.
Before this change, BraceWrapping: AfterFunction would affect synchronized blocks in Java, but they should be formatted w.r.t. BraceWrapping: AfterControlStatement.
Using the config:
BreakBeforeBraces: Custom
BraceWrapping:
AfterControlStatement: false
AfterFunction: true
would result in misformatted code like:
class Foo {
void bar()
{
synchronized (this)
{
a();
a();
}
}
}
instead of:
class Foo {
void bar()
{
synchronized (this) {
a();
a();
}
}
}
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D116767
Files:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTestJava.cpp
Index: clang/unittests/Format/FormatTestJava.cpp
===================================================================
--- clang/unittests/Format/FormatTestJava.cpp
+++ clang/unittests/Format/FormatTestJava.cpp
@@ -431,6 +431,24 @@
verifyFormat("synchronized (mData) {\n"
" // ...\n"
"}");
+
+ FormatStyle Style = getLLVMStyle(FormatStyle::LK_Java);
+ Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
+ Style.BraceWrapping.AfterFunction = false;
+ verifyFormat("synchronized (mData)\n"
+ "{\n"
+ " // ...\n"
+ "}",
+ Style);
+
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
+ Style.BraceWrapping.AfterFunction = true;
+ verifyFormat("synchronized (mData) {\n"
+ " // ...\n"
+ "}",
+ Style);
}
TEST_F(FormatTestJava, AssertKeyword) {
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -1525,7 +1525,14 @@
// structural element.
// FIXME: Figure out cases where this is not true, and add projections
// for them (the one we know is missing are lambdas).
- if (Style.BraceWrapping.AfterFunction)
+ if ((Style.Language == FormatStyle::LK_Java) &&
+ Line->Tokens.front().Tok->is(Keywords.kw_synchronized)) {
+ // If necessary, we could set the type to something different than
+ // TT_FunctionLBrace.
+ if (Style.BraceWrapping.AfterControlStatement ==
+ FormatStyle::BWACS_Always)
+ addUnwrappedLine();
+ } else if (Style.BraceWrapping.AfterFunction)
addUnwrappedLine();
FormatTok->setType(TT_FunctionLBrace);
parseBlock();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116767.397987.patch
Type: text/x-patch
Size: 1963 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220106/73ae247e/attachment.bin>
More information about the cfe-commits
mailing list