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();
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();
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits