Skip to content

[clang-format] Add options to control wrapped lambda brace indent. #143249

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2541,6 +2541,40 @@ the configuration (without a prefix: ``Auto``).

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

* ``bool IndentLambdaBracesNested`` Indent nested wrapped lambda braces.

.. code-block:: c++

false:
function(
[]()
{
return true;
});

true:
function(
[]()
{
return true;
});

* ``bool IndentLambdaBracesUnnested`` Indent unnested wrapped lambda braces.

.. code-block:: c++

false:
auto foo = []()
{
return true;
};

true:
auto foo = []()
{
return true;
};

* ``bool SplitEmptyFunction`` If ``false``, empty function body can be put on a single line.
This option is used only if the opening brace of the function has
already been wrapped, i.e. the ``AfterFunction`` brace wrapping mode is
Expand Down
4 changes: 3 additions & 1 deletion clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ Bug Fixes in This Version
- Fixed an assertion failure in serialization of constexpr structs containing unions. (#GH140130)
- Fixed duplicate entries in TableGen that caused the wrong attribute to be selected. (GH#140701)
- Fixed type mismatch error when 'builtin-elementwise-math' arguments have different qualifiers, this should be well-formed. (#GH141397)
- Constant evaluation now correctly runs the destructor of a variable declared in
- Constant evaluation now correctly runs the destructor of a variable declared in
the second clause of a C-style ``for`` loop. (#GH139818)

Bug Fixes to Compiler Builtins
Expand Down Expand Up @@ -988,6 +988,8 @@ clang-format
``enum`` enumerator lists.
- Add ``OneLineFormatOffRegex`` option for turning formatting off for one line.
- Add ``SpaceAfterOperatorKeyword`` option.
- Add ``IndentLambdaBracesNested`` and ``IndentLambdaBracesUnnested`` to
``BraceWrapping`` options for controlling wrapped lambda brace indentation.

clang-refactor
--------------
Expand Down
32 changes: 32 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,38 @@ struct FormatStyle {
bool BeforeWhile;
/// Indent the wrapped braces themselves.
bool IndentBraces;
/// Indent nested wrapped lambda braces.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better name is in order, when reading nested lambda, I thought of a lambda within a lambda.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yeah, I based the name off of the description in the code. I.e. that it uses Current.NestingLevel.
I hadn't thought about the lambda in lambda.
Do you have any ideas of something clearer?
Maybe IndentBracesLambdaTopLevel for the unnested and then just IndentBracesLambda for the others? (I cannot think of an immediately obvious name for the nested case).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe IndentBracesLambdaScoped could work? Or IndentBracesLambdaInner? Could be paired with IndentBracesLambdaOuter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really have something, but you should swap Lambda and Braces => LambdaBraces.

When is NestingLevel > 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When it is inside some form of brackets.
From the documentation in FormatToken.h.

/// The nesting level of this token, i.e. the number of surrounding (),
/// [], {} or <>.
unsigned NestingLevel = 0;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I can't think of a nice name here... @owenca do you have an idea?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we allow this new option, I want a separate NFC patch that upgrades the IndentBraces sub-option to an enum or a struct first. See e.g. #140497.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a patch to upgrade IndentBraces. #143663

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had an idea about the name.
When looking again at the projects at work that needed the nested/unnested distinction, I saw that only indenting the unnested version was required.
Confusion could be eliminated by switching to an enum for IndentLambdaBraces with None, All and TopLevel.

/// \code
/// false:
/// function(
/// []()
/// {
/// return true;
/// });
///
/// true:
/// function(
/// []()
/// {
/// return true;
/// });
/// \endcode
bool IndentLambdaBracesNested;
/// Indent unnested wrapped lambda braces.
/// \code
/// false:
/// auto foo = []()
/// {
/// return true;
/// };
///
/// true:
/// auto foo = []()
/// {
/// return true;
/// };
/// \endcode
bool IndentLambdaBracesUnnested;
/// If ``false``, empty function body can be put on a single line.
/// This option is used only if the opening brace of the function has
/// already been wrapped, i.e. the ``AfterFunction`` brace wrapping mode is
Expand Down
15 changes: 10 additions & 5 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,12 +1334,14 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
Style.IndentWidth;
}

if (Style.BraceWrapping.BeforeLambdaBody &&
Style.BraceWrapping.IndentBraces && Current.is(TT_LambdaLBrace)) {
if (Style.BraceWrapping.BeforeLambdaBody && Current.is(TT_LambdaLBrace)) {
const auto Nested = Current.NestingLevel > 0;
const auto From = Style.LambdaBodyIndentation == FormatStyle::LBI_Signature
? CurrentState.Indent
: State.FirstIndent;
return From + Style.IndentWidth;
const auto Indent = Nested ? Style.BraceWrapping.IndentLambdaBracesNested
: Style.BraceWrapping.IndentLambdaBracesUnnested;
return From + (Indent * Style.IndentWidth);
}

if ((NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block)) ||
Expand Down Expand Up @@ -2123,8 +2125,11 @@ void ContinuationIndenter::moveStateToNewBlock(LineState &State, bool NewLine) {
if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
State.NextToken->is(TT_LambdaLBrace) &&
!State.Line->MightBeFunctionDecl) {
const auto Indent = Style.IndentWidth * Style.BraceWrapping.IndentBraces;
State.Stack.back().NestedBlockIndent = State.FirstIndent + Indent;
const auto Nested = State.NextToken->NestingLevel > 0;
const auto Indent = Nested ? Style.BraceWrapping.IndentLambdaBracesNested
: Style.BraceWrapping.IndentLambdaBracesUnnested;
State.Stack.back().NestedBlockIndent =
State.FirstIndent + (Indent * Style.IndentWidth);
}
unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
// ObjC block sometimes follow special indentation rules.
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
IO.mapOptional("BeforeLambdaBody", Wrapping.BeforeLambdaBody);
IO.mapOptional("BeforeWhile", Wrapping.BeforeWhile);
IO.mapOptional("IndentBraces", Wrapping.IndentBraces);
IO.mapOptional("IndentLambdaBracesNested",
Wrapping.IndentLambdaBracesNested);
IO.mapOptional("IndentLambdaBracesUnnested",
Wrapping.IndentLambdaBracesUnnested);
IO.mapOptional("SplitEmptyFunction", Wrapping.SplitEmptyFunction);
IO.mapOptional("SplitEmptyRecord", Wrapping.SplitEmptyRecord);
IO.mapOptional("SplitEmptyNamespace", Wrapping.SplitEmptyNamespace);
Expand Down Expand Up @@ -1382,6 +1386,8 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
/*BeforeLambdaBody=*/false,
/*BeforeWhile=*/false,
/*IndentBraces=*/false,
/*IndentLambdaBracesNested=*/false,
/*IndentLambdaBracesUnnested=*/false,
/*SplitEmptyFunction=*/true,
/*SplitEmptyRecord=*/true,
/*SplitEmptyNamespace=*/true};
Expand Down Expand Up @@ -1452,6 +1458,8 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
/*BeforeLambdaBody=*/true,
/*BeforeWhile=*/true,
/*IndentBraces=*/true,
/*IndentLambdaBracesNested=*/true,
/*IndentLambdaBracesUnnested=*/true,
/*SplitEmptyFunction=*/true,
/*SplitEmptyRecord=*/true,
/*SplitEmptyNamespace=*/true};
Expand Down Expand Up @@ -1552,6 +1560,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
/*BeforeLambdaBody=*/false,
/*BeforeWhile=*/false,
/*IndentBraces=*/false,
/*IndentLambdaBracesNested=*/false,
/*IndentLambdaBracesUnnested=*/false,
/*SplitEmptyFunction=*/true,
/*SplitEmptyRecord=*/true,
/*SplitEmptyNamespace=*/true};
Expand Down
2 changes: 2 additions & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentLambdaBracesNested);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentLambdaBracesUnnested);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
Expand Down
78 changes: 78 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24358,6 +24358,84 @@ TEST_F(FormatTest, LambdaBracesInGNU) {
Style);
}

TEST_F(FormatTest, LambdaBracesIndentationNested) {
auto Style = getLLVMStyle();
EXPECT_EQ(Style.LambdaBodyIndentation, FormatStyle::LBI_Signature);

Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.BeforeLambdaBody = true;
verifyFormat("function(\n"
" [&]()\n"
" {\n"
" for (int i = 0; i < y; ++i)\n"
" return 97;\n"
" });",
Style);

Style.BraceWrapping.IndentLambdaBracesNested = true;
verifyFormat("function(\n"
" [&]()\n"
" {\n"
" for (int i = 0; i < y; ++i)\n"
" return 97;\n"
" });",
Style);

Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
verifyFormat("function([&]()\n"
" {\n"
" for (int i = 0; i < y; ++i)\n"
" return 97;\n"
" });",
Style);

Style.BraceWrapping.IndentLambdaBracesNested = false;
verifyFormat("function([&]()\n"
"{\n"
" for (int i = 0; i < y; ++i)\n"
" return 97;\n"
"});",
Style);
}

TEST_F(FormatTest, LambdaBracesIndentationUnnested) {
auto Style = getLLVMStyle();
EXPECT_EQ(Style.LambdaBodyIndentation, FormatStyle::LBI_Signature);

Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.BeforeLambdaBody = true;
verifyFormat("auto x = [&]()\n"
"{\n"
" for (int i = 0; i < y; ++i)\n"
" return 97;\n"
"};",
Style);

Style.BraceWrapping.IndentLambdaBracesUnnested = true;
verifyFormat("auto x = [&]()\n"
" {\n"
" for (int i = 0; i < y; ++i)\n"
" return 97;\n"
" };",
Style);

Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
verifyFormat("auto x = [&]()\n"
" {\n"
" for (int i = 0; i < y; ++i)\n"
" return 97;\n"
" };",
Style);

Style.BraceWrapping.IndentLambdaBracesUnnested = false;
verifyFormat("auto x = [&]()\n"
"{\n"
" for (int i = 0; i < y; ++i)\n"
" return 97;\n"
"};",
Style);
}

TEST_F(FormatTest, FormatsBlocks) {
FormatStyle ShortBlocks = getLLVMStyle();
ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
Expand Down