Skip to content

Commit 5f744cc

Browse files
authored
[clang-format] Wrap and indent lambda braces in GNU style (#135479)
Fix #133135
1 parent 09c8cfe commit 5f744cc

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,14 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
13341334
Style.IndentWidth;
13351335
}
13361336

1337+
if (Style.BraceWrapping.BeforeLambdaBody &&
1338+
Style.BraceWrapping.IndentBraces && Current.is(TT_LambdaLBrace)) {
1339+
const auto From = Style.LambdaBodyIndentation == FormatStyle::LBI_Signature
1340+
? CurrentState.Indent
1341+
: State.FirstIndent;
1342+
return From + Style.IndentWidth;
1343+
}
1344+
13371345
if ((NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block)) ||
13381346
(Style.isVerilog() && Keywords.isVerilogBegin(*NextNonComment))) {
13391347
if (Current.NestingLevel == 0 ||
@@ -2113,7 +2121,8 @@ void ContinuationIndenter::moveStateToNewBlock(LineState &State, bool NewLine) {
21132121
if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
21142122
State.NextToken->is(TT_LambdaLBrace) &&
21152123
!State.Line->MightBeFunctionDecl) {
2116-
State.Stack.back().NestedBlockIndent = State.FirstIndent;
2124+
const auto Indent = Style.IndentWidth * Style.BraceWrapping.IndentBraces;
2125+
State.Stack.back().NestedBlockIndent = State.FirstIndent + Indent;
21172126
}
21182127
unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
21192128
// ObjC block sometimes follow special indentation rules.

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
14351435
/*AfterExternBlock=*/true,
14361436
/*BeforeCatch=*/true,
14371437
/*BeforeElse=*/true,
1438-
/*BeforeLambdaBody=*/false,
1438+
/*BeforeLambdaBody=*/true,
14391439
/*BeforeWhile=*/true,
14401440
/*IndentBraces=*/true,
14411441
/*SplitEmptyFunction=*/true,

clang/unittests/Format/FormatTest.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24267,6 +24267,37 @@ TEST_F(FormatTest, EmptyLinesInLambdas) {
2426724267
"};");
2426824268
}
2426924269

24270+
TEST_F(FormatTest, LambdaBracesInGNU) {
24271+
auto Style = getGNUStyle();
24272+
EXPECT_EQ(Style.LambdaBodyIndentation, FormatStyle::LBI_Signature);
24273+
24274+
constexpr StringRef Code("auto x = [&] ()\n"
24275+
" {\n"
24276+
" for (int i = 0; i < y; ++i)\n"
24277+
" return 97;\n"
24278+
" };");
24279+
verifyFormat(Code, Style);
24280+
24281+
Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
24282+
verifyFormat(Code, Style);
24283+
verifyFormat("for_each_thread ([] (thread_info *thread)\n"
24284+
" {\n"
24285+
" /* Lambda body. */\n"
24286+
" });",
24287+
"for_each_thread([](thread_info *thread) {\n"
24288+
" /* Lambda body. */\n"
24289+
"});",
24290+
Style);
24291+
verifyFormat("iterate_over_lwps (scope_ptid, [=] (struct lwp_info *info)\n"
24292+
" {\n"
24293+
" /* Lambda body. */\n"
24294+
" });",
24295+
"iterate_over_lwps(scope_ptid, [=](struct lwp_info *info) {\n"
24296+
" /* Lambda body. */\n"
24297+
"});",
24298+
Style);
24299+
}
24300+
2427024301
TEST_F(FormatTest, FormatsBlocks) {
2427124302
FormatStyle ShortBlocks = getLLVMStyle();
2427224303
ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;

0 commit comments

Comments
 (0)