-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[clang-format] Fix AllowShortLambdasOnASingleLine interfering with lambda brace wrapping. #81848
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…mbda brace wrapping. Fix an issue where the lambda body left brace could sometimes fail to be wrapped when AllowShortLambdasOnASingleLine is enabled. Now, when BraceWrapping.BeforeLambdaBody is enabled, if the brace is not wrapped we prevent breaks in the lambda body. Resolves llvm#81845
@llvm/pr-subscribers-clang-format Author: None (rmarker) ChangesFix an issue where the lambda body left brace could sometimes fail to be wrapped when AllowShortLambdasOnASingleLine is enabled. Now, when BraceWrapping.BeforeLambdaBody is enabled, if the brace is not wrapped, we prevent breaks in the lambda body. Resolves #81845 Full diff: https://github.com/llvm/llvm-project/pull/81848.diff 3 Files Affected:
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 159d130cb67332..96e9684409c744 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1794,7 +1794,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
}
if (Current.MatchingParen && Current.is(BK_Block)) {
- moveStateToNewBlock(State);
+ moveStateToNewBlock(State, Newline);
return;
}
@@ -1981,7 +1981,7 @@ void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
}
}
-void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
+void ContinuationIndenter::moveStateToNewBlock(LineState &State, bool NewLine) {
if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
State.NextToken->is(TT_LambdaLBrace) &&
!State.Line->MightBeFunctionDecl) {
@@ -1993,10 +1993,18 @@ void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace)
? Style.ObjCBlockIndentWidth
: Style.IndentWidth);
+
+ // Even when wrapping before lambda body, the left brace can still be added to
+ // the same line. This occurs when checking whether the whole lambda body can
+ // go on a single line. In this case we have to make sure there are no line
+ // breaks in the body, otherwise we could just end up with a regular lambda
+ // body without the brace wrapped.
+ bool NoLineBreak = Style.BraceWrapping.BeforeLambdaBody && !NewLine &&
+ State.NextToken->is(TT_LambdaLBrace);
+
State.Stack.push_back(ParenState(State.NextToken, NewIndent,
State.Stack.back().LastSpace,
- /*AvoidBinPacking=*/true,
- /*NoLineBreak=*/false));
+ /*AvoidBinPacking=*/true, NoLineBreak));
State.Stack.back().NestedBlockIndent = NestedBlockIndent;
State.Stack.back().BreakBeforeParameter = true;
}
diff --git a/clang/lib/Format/ContinuationIndenter.h b/clang/lib/Format/ContinuationIndenter.h
index 057b85bd32d505..2598947bb624c5 100644
--- a/clang/lib/Format/ContinuationIndenter.h
+++ b/clang/lib/Format/ContinuationIndenter.h
@@ -104,7 +104,7 @@ class ContinuationIndenter {
/// Update 'State' according to the next token being one of ")>}]".
void moveStatePastScopeCloser(LineState &State);
/// Update 'State' with the next token opening a nested block.
- void moveStateToNewBlock(LineState &State);
+ void moveStateToNewBlock(LineState &State, bool NewLine);
/// Reformats a raw string literal.
///
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index b0687eaecb10fe..b33613bf143170 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22893,6 +22893,19 @@ TEST_F(FormatTest, FormatsLambdas) {
LLVMWithBeforeLambdaBody);
verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
LLVMWithBeforeLambdaBody);
+ verifyFormat(
+ "FctWithTwoParams_SLS_All(\n"
+ " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });\n",
+ LLVMWithBeforeLambdaBody);
+ verifyFormat(
+ "FctWithTwoParams_SLS_All(\n"
+ " 87,\n"
+ " []()\n"
+ " {\n"
+ " return "
+ "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
+ " });",
+ LLVMWithBeforeLambdaBody);
verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
LLVMWithBeforeLambdaBody);
verifyFormat(
|
owenca
approved these changes
Feb 16, 2024
Co-authored-by: Owen Pan <owenpiano@gmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix an issue where the lambda body left brace could sometimes fail to be wrapped when AllowShortLambdasOnASingleLine is enabled.
Now, when BraceWrapping.BeforeLambdaBody is enabled, if the brace is not wrapped, we prevent breaks in the lambda body.
Resolves #81845