Skip to content

[clang-format] Wrap and indent lambda braces in GNU style #135479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24268,6 +24268,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;
Expand Down