Skip to content

Commit 212f914

Browse files
committed
[clang-format] Add options to control wrapped lambda brace indent.
Adds options to customise how wrapped lambda braces are indented when using custom brace wrapping. IndentBraces was recently changed to also indent wrapped lambda braces. This has been changed to be controlled separately to allow the old behaviour to be maintained. Especially before a release is made with the new behaviour. In order to further increase flexibility, the indentation can be controlled separately for both nested and unnested lambdas.
1 parent eec9431 commit 212f914

File tree

7 files changed

+170
-5
lines changed

7 files changed

+170
-5
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,6 +2541,40 @@ the configuration (without a prefix: ``Auto``).
25412541

25422542
* ``bool IndentBraces`` Indent the wrapped braces themselves.
25432543

2544+
* ``bool IndentBracesLambdaNested`` Indent nested wrapped lambda braces.
2545+
2546+
.. code-block:: c++
2547+
2548+
false:
2549+
function(
2550+
[]()
2551+
{
2552+
return true;
2553+
});
2554+
2555+
true:
2556+
function(
2557+
[]()
2558+
{
2559+
return true;
2560+
});
2561+
2562+
* ``bool IndentBracesLambdaUnnested`` Indent unnested wrapped lambda braces.
2563+
2564+
.. code-block:: c++
2565+
2566+
false:
2567+
auto foo = []()
2568+
{
2569+
return true;
2570+
};
2571+
2572+
true:
2573+
auto foo = []()
2574+
{
2575+
return true;
2576+
};
2577+
25442578
* ``bool SplitEmptyFunction`` If ``false``, empty function body can be put on a single line.
25452579
This option is used only if the opening brace of the function has
25462580
already been wrapped, i.e. the ``AfterFunction`` brace wrapping mode is

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,8 @@ clang-format
988988
``enum`` enumerator lists.
989989
- Add ``OneLineFormatOffRegex`` option for turning formatting off for one line.
990990
- Add ``SpaceAfterOperatorKeyword`` option.
991+
- Add ``IndentBracesLambdaNested`` and ``IndentBracesLambdaUnnested`` to
992+
``BraceWrapping`` options for controlling wrapped lambda brace indentation.
991993

992994
clang-refactor
993995
--------------

clang/include/clang/Format/Format.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,38 @@ struct FormatStyle {
15471547
bool BeforeWhile;
15481548
/// Indent the wrapped braces themselves.
15491549
bool IndentBraces;
1550+
/// Indent nested wrapped lambda braces.
1551+
/// \code
1552+
/// false:
1553+
/// function(
1554+
/// []()
1555+
/// {
1556+
/// return true;
1557+
/// });
1558+
///
1559+
/// true:
1560+
/// function(
1561+
/// []()
1562+
/// {
1563+
/// return true;
1564+
/// });
1565+
/// \endcode
1566+
bool IndentBracesLambdaNested;
1567+
/// Indent unnested wrapped lambda braces.
1568+
/// \code
1569+
/// false:
1570+
/// auto foo = []()
1571+
/// {
1572+
/// return true;
1573+
/// };
1574+
///
1575+
/// true:
1576+
/// auto foo = []()
1577+
/// {
1578+
/// return true;
1579+
/// };
1580+
/// \endcode
1581+
bool IndentBracesLambdaUnnested;
15501582
/// If ``false``, empty function body can be put on a single line.
15511583
/// This option is used only if the opening brace of the function has
15521584
/// already been wrapped, i.e. the ``AfterFunction`` brace wrapping mode is

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,12 +1334,15 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
13341334
Style.IndentWidth;
13351335
}
13361336

1337-
if (Style.BraceWrapping.BeforeLambdaBody &&
1338-
Style.BraceWrapping.IndentBraces && Current.is(TT_LambdaLBrace)) {
1337+
if (Style.BraceWrapping.BeforeLambdaBody && Current.is(TT_LambdaLBrace)) {
1338+
const auto Nested = Current.NestingLevel != 0;
13391339
const auto From = Style.LambdaBodyIndentation == FormatStyle::LBI_Signature
13401340
? CurrentState.Indent
13411341
: State.FirstIndent;
1342-
return From + Style.IndentWidth;
1342+
const auto Indent =
1343+
(Style.BraceWrapping.IndentBracesLambdaNested && Nested) ||
1344+
(Style.BraceWrapping.IndentBracesLambdaUnnested && !Nested);
1345+
return From + (Indent * Style.IndentWidth);
13431346
}
13441347

13451348
if ((NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block)) ||
@@ -2123,8 +2126,12 @@ void ContinuationIndenter::moveStateToNewBlock(LineState &State, bool NewLine) {
21232126
if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
21242127
State.NextToken->is(TT_LambdaLBrace) &&
21252128
!State.Line->MightBeFunctionDecl) {
2126-
const auto Indent = Style.IndentWidth * Style.BraceWrapping.IndentBraces;
2127-
State.Stack.back().NestedBlockIndent = State.FirstIndent + Indent;
2129+
const auto Nested = State.NextToken->NestingLevel != 0;
2130+
const auto Indent =
2131+
(Style.BraceWrapping.IndentBracesLambdaNested && Nested) ||
2132+
(Style.BraceWrapping.IndentBracesLambdaUnnested && !Nested);
2133+
State.Stack.back().NestedBlockIndent =
2134+
State.FirstIndent + (Indent * Style.IndentWidth);
21282135
}
21292136
unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
21302137
// ObjC block sometimes follow special indentation rules.

clang/lib/Format/Format.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
196196
IO.mapOptional("BeforeLambdaBody", Wrapping.BeforeLambdaBody);
197197
IO.mapOptional("BeforeWhile", Wrapping.BeforeWhile);
198198
IO.mapOptional("IndentBraces", Wrapping.IndentBraces);
199+
IO.mapOptional("IndentBracesLambdaNested",
200+
Wrapping.IndentBracesLambdaNested);
201+
IO.mapOptional("IndentBracesLambdaUnnested",
202+
Wrapping.IndentBracesLambdaUnnested);
199203
IO.mapOptional("SplitEmptyFunction", Wrapping.SplitEmptyFunction);
200204
IO.mapOptional("SplitEmptyRecord", Wrapping.SplitEmptyRecord);
201205
IO.mapOptional("SplitEmptyNamespace", Wrapping.SplitEmptyNamespace);
@@ -1382,6 +1386,8 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
13821386
/*BeforeLambdaBody=*/false,
13831387
/*BeforeWhile=*/false,
13841388
/*IndentBraces=*/false,
1389+
/*IndentBracesLambdaNested=*/false,
1390+
/*IndentBracesLambdaUnnested=*/false,
13851391
/*SplitEmptyFunction=*/true,
13861392
/*SplitEmptyRecord=*/true,
13871393
/*SplitEmptyNamespace=*/true};
@@ -1452,6 +1458,8 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
14521458
/*BeforeLambdaBody=*/true,
14531459
/*BeforeWhile=*/true,
14541460
/*IndentBraces=*/true,
1461+
/*IndentBracesLambdaNested=*/true,
1462+
/*IndentBracesLambdaUnnested=*/true,
14551463
/*SplitEmptyFunction=*/true,
14561464
/*SplitEmptyRecord=*/true,
14571465
/*SplitEmptyNamespace=*/true};
@@ -1552,6 +1560,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
15521560
/*BeforeLambdaBody=*/false,
15531561
/*BeforeWhile=*/false,
15541562
/*IndentBraces=*/false,
1563+
/*IndentBracesLambdaNested=*/false,
1564+
/*IndentBracesLambdaUnnested=*/false,
15551565
/*SplitEmptyFunction=*/true,
15561566
/*SplitEmptyRecord=*/true,
15571567
/*SplitEmptyNamespace=*/true};

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
236236
CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
237237
CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
238238
CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
239+
CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBracesLambdaNested);
240+
CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBracesLambdaUnnested);
239241
CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
240242
CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
241243
CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);

clang/unittests/Format/FormatTest.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24358,6 +24358,84 @@ TEST_F(FormatTest, LambdaBracesInGNU) {
2435824358
Style);
2435924359
}
2436024360

24361+
TEST_F(FormatTest, LambdaBracesIndentationNested) {
24362+
auto Style = getLLVMStyle();
24363+
EXPECT_EQ(Style.LambdaBodyIndentation, FormatStyle::LBI_Signature);
24364+
24365+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
24366+
Style.BraceWrapping.BeforeLambdaBody = true;
24367+
verifyFormat("function(\n"
24368+
" [&]()\n"
24369+
" {\n"
24370+
" for (int i = 0; i < y; ++i)\n"
24371+
" return 97;\n"
24372+
" });",
24373+
Style);
24374+
24375+
Style.BraceWrapping.IndentBracesLambdaNested = true;
24376+
verifyFormat("function(\n"
24377+
" [&]()\n"
24378+
" {\n"
24379+
" for (int i = 0; i < y; ++i)\n"
24380+
" return 97;\n"
24381+
" });",
24382+
Style);
24383+
24384+
Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
24385+
verifyFormat("function([&]()\n"
24386+
" {\n"
24387+
" for (int i = 0; i < y; ++i)\n"
24388+
" return 97;\n"
24389+
" });",
24390+
Style);
24391+
24392+
Style.BraceWrapping.IndentBracesLambdaNested = false;
24393+
verifyFormat("function([&]()\n"
24394+
"{\n"
24395+
" for (int i = 0; i < y; ++i)\n"
24396+
" return 97;\n"
24397+
"});",
24398+
Style);
24399+
}
24400+
24401+
TEST_F(FormatTest, LambdaBracesIndentationUnnested) {
24402+
auto Style = getLLVMStyle();
24403+
EXPECT_EQ(Style.LambdaBodyIndentation, FormatStyle::LBI_Signature);
24404+
24405+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
24406+
Style.BraceWrapping.BeforeLambdaBody = true;
24407+
verifyFormat("auto x = [&]()\n"
24408+
"{\n"
24409+
" for (int i = 0; i < y; ++i)\n"
24410+
" return 97;\n"
24411+
"};",
24412+
Style);
24413+
24414+
Style.BraceWrapping.IndentBracesLambdaUnnested = true;
24415+
verifyFormat("auto x = [&]()\n"
24416+
" {\n"
24417+
" for (int i = 0; i < y; ++i)\n"
24418+
" return 97;\n"
24419+
" };",
24420+
Style);
24421+
24422+
Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
24423+
verifyFormat("auto x = [&]()\n"
24424+
" {\n"
24425+
" for (int i = 0; i < y; ++i)\n"
24426+
" return 97;\n"
24427+
" };",
24428+
Style);
24429+
24430+
Style.BraceWrapping.IndentBracesLambdaUnnested = false;
24431+
verifyFormat("auto x = [&]()\n"
24432+
"{\n"
24433+
" for (int i = 0; i < y; ++i)\n"
24434+
" return 97;\n"
24435+
"};",
24436+
Style);
24437+
}
24438+
2436124439
TEST_F(FormatTest, FormatsBlocks) {
2436224440
FormatStyle ShortBlocks = getLLVMStyle();
2436324441
ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;

0 commit comments

Comments
 (0)