diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 39eb706fc2fd..55e1d1ceb55b 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -1334,6 +1334,14 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { Style.IndentWidth; } + if (Style.BraceWrapping.BeforeLambdaBody && + Style.BraceWrapping.IndentBraces && Current.is(TT_LambdaLBrace)) { + const auto From = Style.LambdaBodyIndentation == FormatStyle::LBI_Signature + ? CurrentState.Indent + : State.FirstIndent; + return From + Style.IndentWidth; + } + if ((NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block)) || (Style.isVerilog() && Keywords.isVerilogBegin(*NextNonComment))) { if (Current.NestingLevel == 0 || @@ -2113,7 +2121,8 @@ void ContinuationIndenter::moveStateToNewBlock(LineState &State, bool NewLine) { if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope && State.NextToken->is(TT_LambdaLBrace) && !State.Line->MightBeFunctionDecl) { - State.Stack.back().NestedBlockIndent = State.FirstIndent; + const auto Indent = Style.IndentWidth * Style.BraceWrapping.IndentBraces; + State.Stack.back().NestedBlockIndent = State.FirstIndent + Indent; } unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent; // ObjC block sometimes follow special indentation rules. diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 3802766b652d..c601967a8715 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1435,7 +1435,7 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) { /*AfterExternBlock=*/true, /*BeforeCatch=*/true, /*BeforeElse=*/true, - /*BeforeLambdaBody=*/false, + /*BeforeLambdaBody=*/true, /*BeforeWhile=*/true, /*IndentBraces=*/true, /*SplitEmptyFunction=*/true, diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 9dac6d6c5f5b..bf3260c6216d 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -24267,6 +24267,37 @@ TEST_F(FormatTest, EmptyLinesInLambdas) { "};"); } +TEST_F(FormatTest, LambdaBracesInGNU) { + auto Style = getGNUStyle(); + EXPECT_EQ(Style.LambdaBodyIndentation, FormatStyle::LBI_Signature); + + constexpr StringRef Code("auto x = [&] ()\n" + " {\n" + " for (int i = 0; i < y; ++i)\n" + " return 97;\n" + " };"); + verifyFormat(Code, Style); + + Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; + verifyFormat(Code, Style); + verifyFormat("for_each_thread ([] (thread_info *thread)\n" + " {\n" + " /* Lambda body. */\n" + " });", + "for_each_thread([](thread_info *thread) {\n" + " /* Lambda body. */\n" + "});", + Style); + verifyFormat("iterate_over_lwps (scope_ptid, [=] (struct lwp_info *info)\n" + " {\n" + " /* Lambda body. */\n" + " });", + "iterate_over_lwps(scope_ptid, [=](struct lwp_info *info) {\n" + " /* Lambda body. */\n" + "});", + Style); +} + TEST_F(FormatTest, FormatsBlocks) { FormatStyle ShortBlocks = getLLVMStyle(); ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;