Skip to content

Commit 5a9aa41

Browse files
committed
Also handle LBI_OuterScope
1 parent f041f9c commit 5a9aa41

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,10 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
13361336

13371337
if (Style.BraceWrapping.BeforeLambdaBody &&
13381338
Style.BraceWrapping.IndentBraces && Current.is(TT_LambdaLBrace)) {
1339-
return CurrentState.Indent + Style.IndentWidth;
1339+
const auto From = Style.LambdaBodyIndentation == FormatStyle::LBI_Signature
1340+
? CurrentState.Indent
1341+
: State.FirstIndent;
1342+
return From + Style.IndentWidth;
13401343
}
13411344

13421345
if ((NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block)) ||
@@ -2118,7 +2121,8 @@ void ContinuationIndenter::moveStateToNewBlock(LineState &State, bool NewLine) {
21182121
if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
21192122
State.NextToken->is(TT_LambdaLBrace) &&
21202123
!State.Line->MightBeFunctionDecl) {
2121-
State.Stack.back().NestedBlockIndent = State.FirstIndent;
2124+
const auto Indent = Style.IndentWidth * Style.BraceWrapping.IndentBraces;
2125+
State.Stack.back().NestedBlockIndent = State.FirstIndent + Indent;
21222126
}
21232127
unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
21242128
// ObjC block sometimes follow special indentation rules.

clang/unittests/Format/FormatTest.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24269,12 +24269,34 @@ TEST_F(FormatTest, EmptyLinesInLambdas) {
2426924269
}
2427024270

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

2428024302
TEST_F(FormatTest, FormatsBlocks) {

0 commit comments

Comments
 (0)