aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/lib/Format
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-10-21 13:31:11 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-12-08 17:35:41 +0000
commitbdb86d1a853a919764f65fdedcea76d76e4d619b (patch)
tree9192288f53762443b0d7453fd2d49bbbe0e344eb /contrib/llvm-project/clang/lib/Format
parent3bd749dbd90cc3b95719b65393df5ca8a0fe919d (diff)
parentcd255c5cf2441442b46200d298c0cbccf83caba5 (diff)
Diffstat (limited to 'contrib/llvm-project/clang/lib/Format')
-rw-r--r--contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp24
-rw-r--r--contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.h11
2 files changed, 35 insertions, 0 deletions
diff --git a/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp b/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp
index 852437b9390f..07ff86bc50a2 100644
--- a/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp
@@ -173,10 +173,12 @@ void UnwrappedLineParser::reset() {
CommentsBeforeNextToken.clear();
FormatTok = nullptr;
MustBreakBeforeNextToken = false;
+ IsDecltypeAutoFunction = false;
PreprocessorDirectives.clear();
CurrentLines = &Lines;
DeclarationScopeStack.clear();
NestedTooDeep.clear();
+ NestedLambdas.clear();
PPStack.clear();
Line->FirstStartColumn = FirstStartColumn;
@@ -1757,6 +1759,17 @@ void UnwrappedLineParser::parseStructuralElement(
if (parseStructLike())
return;
break;
+ case tok::kw_decltype:
+ nextToken();
+ if (FormatTok->is(tok::l_paren)) {
+ parseParens();
+ assert(FormatTok->Previous);
+ if (FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
+ tok::l_paren)) {
+ Line->SeenDecltypeAuto = true;
+ }
+ }
+ break;
case tok::period:
nextToken();
// In Java, classes have an implicit static member "class".
@@ -1818,6 +1831,7 @@ void UnwrappedLineParser::parseStructuralElement(
if (NextLBracesType != TT_Unknown)
FormatTok->setFinalizedType(NextLBracesType);
if (!tryToParsePropertyAccessor() && !tryToParseBracedList()) {
+ IsDecltypeAutoFunction = Line->SeenDecltypeAuto;
// A block outside of parentheses must be the last part of a
// structural element.
// FIXME: Figure out cases where this is not true, and add projections
@@ -1835,6 +1849,7 @@ void UnwrappedLineParser::parseStructuralElement(
}
FormatTok->setFinalizedType(TT_FunctionLBrace);
parseBlock();
+ IsDecltypeAutoFunction = false;
addUnwrappedLine();
return;
}
@@ -2249,9 +2264,15 @@ bool UnwrappedLineParser::tryToParseLambda() {
return true;
}
}
+
FormatTok->setFinalizedType(TT_LambdaLBrace);
LSquare.setFinalizedType(TT_LambdaLSquare);
+
+ NestedLambdas.push_back(Line->SeenDecltypeAuto);
parseChildBlock();
+ assert(!NestedLambdas.empty());
+ NestedLambdas.pop_back();
+
return true;
}
@@ -2471,6 +2492,8 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
PrevPrev->endsSequence(tok::kw_constexpr, tok::kw_if))));
const bool ReturnParens =
Style.RemoveParentheses == FormatStyle::RPS_ReturnStatement &&
+ ((NestedLambdas.empty() && !IsDecltypeAutoFunction) ||
+ (!NestedLambdas.empty() && !NestedLambdas.back())) &&
Prev && Prev->isOneOf(tok::kw_return, tok::kw_co_return) && Next &&
Next->is(tok::semi);
if ((DoubleParens && !Blacklisted) || ReturnParens) {
@@ -4386,6 +4409,7 @@ void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) {
Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
Line->FirstStartColumn = 0;
Line->IsContinuation = false;
+ Line->SeenDecltypeAuto = false;
if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove)
--Line->Level;
diff --git a/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.h b/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.h
index 57515af64a3e..96248d130ddb 100644
--- a/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.h
+++ b/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.h
@@ -61,6 +61,9 @@ struct UnwrappedLine {
bool MustBeDeclaration;
+ /// Whether the parser has seen \c decltype(auto) in this line.
+ bool SeenDecltypeAuto = false;
+
/// \c True if this line should be indented by ContinuationIndent in
/// addition to the normal indention level.
bool IsContinuation = false;
@@ -341,6 +344,14 @@ private:
// statement contains more than some predefined number of nested statements).
SmallVector<bool, 8> NestedTooDeep;
+ // Keeps a stack of the states of nested lambdas (true if the return type of
+ // the lambda is `decltype(auto)`).
+ SmallVector<bool, 4> NestedLambdas;
+
+ // Whether the parser is parsing the body of a function whose return type is
+ // `decltype(auto)`.
+ bool IsDecltypeAutoFunction = false;
+
// Represents preprocessor branch type, so we can find matching
// #if/#else/#endif directives.
enum PPBranchKind {