[clang] 91b9e67 - [clang-format] Fix `BraceWrapping: AfterFunction` affecting synchronized blocks in Java.
Marek Kurdej via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 7 01:06:53 PST 2022
Author: Marek Kurdej
Date: 2022-01-07T10:06:49+01:00
New Revision: 91b9e6729c11cce8cf5fea727c6cb81ab8ab5ba4
URL: https://github.com/llvm/llvm-project/commit/91b9e6729c11cce8cf5fea727c6cb81ab8ab5ba4
DIFF: https://github.com/llvm/llvm-project/commit/91b9e6729c11cce8cf5fea727c6cb81ab8ab5ba4.diff
LOG: [clang-format] Fix `BraceWrapping: AfterFunction` affecting synchronized blocks in Java.
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();
}
}
}
```
Reviewed By: MyDeveloperDay, owenpan
Differential Revision: https://reviews.llvm.org/D116767
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTestJava.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 410b0557c4cd..5895c2efdef4 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1523,8 +1523,16 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) {
// 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
diff erent than
+ // TT_FunctionLBrace.
+ if (Style.BraceWrapping.AfterControlStatement ==
+ FormatStyle::BWACS_Always)
+ addUnwrappedLine();
+ } else if (Style.BraceWrapping.AfterFunction) {
addUnwrappedLine();
+ }
FormatTok->setType(TT_FunctionLBrace);
parseBlock();
addUnwrappedLine();
diff --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp
index 9a18c2853abc..84f6420b9e1a 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -431,6 +431,24 @@ TEST_F(FormatTestJava, SynchronizedKeyword) {
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) {
More information about the cfe-commits
mailing list