https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/135479
>From f041f9c3eac94b8f1322ebaa9dfd60d4287840d7 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Fri, 11 Apr 2025 23:36:37 -0700 Subject: [PATCH 1/2] [clang-format] Wrap and indent lambda braces in GNU style Fix #133135 --- clang/lib/Format/ContinuationIndenter.cpp | 5 +++++ clang/lib/Format/Format.cpp | 2 +- clang/unittests/Format/FormatTest.cpp | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 1969f4297b211..9bebac1bed5c6 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -1334,6 +1334,11 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { Style.IndentWidth; } + if (Style.BraceWrapping.BeforeLambdaBody && + Style.BraceWrapping.IndentBraces && Current.is(TT_LambdaLBrace)) { + return CurrentState.Indent + Style.IndentWidth; + } + if ((NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block)) || (Style.isVerilog() && Keywords.isVerilogBegin(*NextNonComment))) { if (Current.NestingLevel == 0 || diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index b90bd8276e1e2..57175a0be1a6b 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 f0e67c604cc4b..108d5b90e31f8 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -24268,6 +24268,15 @@ TEST_F(FormatTest, EmptyLinesInLambdas) { "};"); } +TEST_F(FormatTest, LambdaBracesInGNU) { + verifyFormat("auto x = [&] ()\n" + " {\n" + " for (int i = 0; i < y; ++i)\n" + " return 97;\n" + " };", + getGNUStyle()); +} + TEST_F(FormatTest, FormatsBlocks) { FormatStyle ShortBlocks = getLLVMStyle(); ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; >From 5a9aa414c32d2b57e12e2c639e6385275ee91acd Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Sat, 12 Apr 2025 03:51:23 -0700 Subject: [PATCH 2/2] Also handle LBI_OuterScope --- clang/lib/Format/ContinuationIndenter.cpp | 8 ++++-- clang/unittests/Format/FormatTest.cpp | 32 +++++++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 9bebac1bed5c6..1cf15ea65681d 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -1336,7 +1336,10 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { if (Style.BraceWrapping.BeforeLambdaBody && Style.BraceWrapping.IndentBraces && Current.is(TT_LambdaLBrace)) { - return CurrentState.Indent + Style.IndentWidth; + 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)) || @@ -2118,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/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 108d5b90e31f8..7163a49820ae2 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -24269,12 +24269,34 @@ TEST_F(FormatTest, EmptyLinesInLambdas) { } TEST_F(FormatTest, LambdaBracesInGNU) { - verifyFormat("auto x = [&] ()\n" + 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" - " for (int i = 0; i < y; ++i)\n" - " return 97;\n" - " };", - getGNUStyle()); + " /* 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) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits