diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1523,8 +1523,16 @@ // 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(); addUnwrappedLine(); diff --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp --- a/clang/unittests/Format/FormatTestJava.cpp +++ b/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) {