diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 4010f7fbd25be..1fbde066842bc 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -399,8 +399,14 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { const auto &CurrentState = State.Stack.back(); if (Style.BraceWrapping.BeforeLambdaBody && Current.CanBreakBefore && Current.is(TT_LambdaLBrace) && Previous.isNot(TT_LineComment)) { - auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack); - return LambdaBodyLength > getColumnLimit(State); + if (Current.MatchingParen->MustBreakBefore) + return true; + + auto LambdaEnd = getLengthToMatchingParen(Current, State.Stack) + + Current.MatchingParen->UnbreakableTailLength + + State.Column - 1; + + return Style.ColumnLimit > 0 && LambdaEnd > getColumnLimit(State); } if (Current.MustBreakBefore || (Current.is(TT_InlineASMColon) && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 944e7c3fb152a..cb7a4196269e6 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -24358,6 +24358,85 @@ TEST_F(FormatTest, LambdaBracesInGNU) { Style); } +TEST_F(FormatTest, BreakBeforeLambdaBodyWrapping) { + verifyFormat("connect([]() {\n" + " foo();\n" + " bar();\n" + "});"); + + auto Style = getLLVMStyle(); + Style.BreakBeforeBraces = FormatStyle::BS_Custom; + Style.BraceWrapping.BeforeLambdaBody = true; + + verifyFormat("connect(\n" + " []()\n" + " {\n" + " foo();\n" + " bar();\n" + " });", + Style); + + Style.ColumnLimit = 0; + verifyFormat("auto noLimit = []() { return 0; };", Style); + + Style.ColumnLimit = 41; + verifyFormat("auto lambda = []() { return foo + bar; };", Style); + Style.ColumnLimit = 40; + verifyFormat("auto lambda = []()\n" + "{ return foo + bar; };", + Style); + Style.ColumnLimit = 22; + verifyFormat("auto lambda = []()\n" + "{ return foo + bar; };", + Style); + Style.ColumnLimit = 21; + verifyFormat("auto lambda = []()\n" + "{\n" + " return foo + bar;\n" + "};", + Style); + + Style.ColumnLimit = 67; + verifyFormat( + "auto result = [](int foo, int bar) { return foo + bar; }(foo, bar);", + Style); + Style.ColumnLimit = 66; + verifyFormat("auto result = [](int foo, int bar)\n" + "{ return foo + bar; }(foo, bar);", + Style); + + Style.ColumnLimit = 36; + verifyFormat("myFunc([&]() { return foo + bar; });", Style); + Style.ColumnLimit = 35; + verifyFormat("myFunc([&]()\n" + " { return foo + bar; });", + Style); + + Style = getGoogleStyleWithColumns(100); + Style.BreakBeforeBraces = FormatStyle::BS_Allman; + Style.IndentWidth = 4; + verifyFormat( + "void Func()\n" + "{\n" + " []()\n" + " {\n" + " return TestVeryLongThingName::TestVeryLongFunctionName()\n" + " .TestAnotherVeryVeryLongFunctionName();\n" + " }\n" + "}\n", + Style); + verifyFormat( + "void Func()\n" + "{\n" + " []()\n" + " {\n" + " return TestVeryLongThingName::TestVeryLongFunctionName()\n" + " .TestAnotherVeryVeryVeryLongFunctionName();\n" + " }\n" + "}\n", + Style); +} + TEST_F(FormatTest, FormatsBlocks) { FormatStyle ShortBlocks = getLLVMStyle(); ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;