-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-format][NFC] Clean up DisallowLineBreaks lambda #144255
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
Merged
Merged
Conversation
This file contains hidden or 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
Member
|
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesSee also https://github.com/llvm/llvm-project/pull/141576/files#r2141808121 Full diff: https://github.com/llvm/llvm-project/pull/144255.diff 1 Files Affected:
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 424b6dbc0da79..710d592327259 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -329,9 +329,9 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
// statement and we are aligning lambda blocks to their signatures.
if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
- State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks &&
- Style.LambdaBodyIndentation == FormatStyle::LBI_Signature) {
- return false;
+ State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks) {
+ return Style.isCpp() &&
+ Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope;
}
// Don't break after very short return types (e.g. "void") as that is often
@@ -706,42 +706,51 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
const FormatToken &Previous = *State.NextToken->Previous;
auto &CurrentState = State.Stack.back();
- bool DisallowLineBreaksOnThisLine =
- Style.LambdaBodyIndentation == FormatStyle::LBI_Signature &&
- // Deal with lambda arguments in C++. The aim here is to ensure that we
- // don't over-indent lambda function bodies when lambdas are passed as
- // arguments to function calls. We do this by ensuring that either all
- // arguments (including any lambdas) go on the same line as the function
- // call, or we break before the first argument.
- Style.isCpp() && [&] {
- // For example, `/*Newline=*/false`.
- if (Previous.is(TT_BlockComment) && Current.SpacesRequiredBefore == 0)
- return false;
- const auto *PrevNonComment = Current.getPreviousNonComment();
- if (!PrevNonComment || PrevNonComment->isNot(tok::l_paren))
- return false;
- if (Current.isOneOf(tok::comment, tok::l_paren, TT_LambdaLSquare))
- return false;
- auto BlockParameterCount = PrevNonComment->BlockParameterCount;
- if (BlockParameterCount == 0)
- return false;
+ // Deal with lambda arguments in C++. The aim here is to ensure that we don't
+ // over-indent lambda function bodies when lambdas are passed as arguments to
+ // function calls. We do this by ensuring that either all arguments (including
+ // any lambdas) go on the same line as the function call, or we break before
+ // the first argument.
+ auto DisallowLineBreaks = [&] {
+ if (!Style.isCpp() ||
+ Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope) {
+ return false;
+ }
- // Multiple lambdas in the same function call.
- if (BlockParameterCount > 1)
- return true;
+ // For example, `/*Newline=*/false`.
+ if (Previous.is(TT_BlockComment) && Current.SpacesRequiredBefore == 0)
+ return false;
- // A lambda followed by another arg.
- if (!PrevNonComment->Role)
- return false;
- auto Comma = PrevNonComment->Role->lastComma();
- if (!Comma)
- return false;
- auto Next = Comma->getNextNonComment();
- return Next &&
- !Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret);
- }();
+ if (Current.isOneOf(tok::comment, tok::l_paren, TT_LambdaLSquare))
+ return false;
+
+ const auto *Prev = Current.getPreviousNonComment();
+ if (!Prev)
+ return false;
+
+ if (Prev->isNot(tok::l_paren))
+ return false;
+
+ if (Prev->BlockParameterCount == 0)
+ return false;
+
+ // Multiple lambdas in the same function call.
+ if (Prev->BlockParameterCount > 1)
+ return true;
+
+ // A lambda followed by another arg.
+ if (!Prev->Role)
+ return false;
+
+ const auto *LastComma = Prev->Role->lastComma();
+ if (!LastComma)
+ return Current.endsSequence(TT_LambdaLSquare, tok::l_paren);
+
+ const auto *Next = LastComma->getNextNonComment();
+ return Next && !Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret);
+ };
- if (DisallowLineBreaksOnThisLine)
+ if (DisallowLineBreaks())
State.NoLineBreak = true;
if (Current.is(tok::equal) &&
|
HazardyKnusperkeks
approved these changes
Jun 15, 2025
akuhlens
pushed a commit
to akuhlens/llvm-project
that referenced
this pull request
Jun 24, 2025
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.
See also https://github.com/llvm/llvm-project/pull/141576/files#r2141808121