diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 50edca43ebb92..19c83d3910902 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2363,12 +2363,9 @@ bool UnwrappedLineParser::tryToParseLambda() { Arrow = FormatTok; nextToken(); break; - case tok::kw_requires: { - auto *RequiresToken = FormatTok; - nextToken(); - parseRequiresClause(RequiresToken); + case tok::kw_requires: + parseRequiresClause(); break; - } case tok::equal: if (!InTemplateParameterList) return true; @@ -2580,12 +2577,9 @@ bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) { if (IsEnum && !Style.AllowShortEnumsOnASingleLine) addUnwrappedLine(); break; - case tok::kw_requires: { - auto *RequiresToken = FormatTok; - nextToken(); - parseRequiresExpression(RequiresToken); + case tok::kw_requires: + parseRequiresExpression(); break; - } default: nextToken(); break; @@ -2727,12 +2721,9 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType, else nextToken(); break; - case tok::kw_requires: { - auto RequiresToken = FormatTok; - nextToken(); - parseRequiresExpression(RequiresToken); + case tok::kw_requires: + parseRequiresExpression(); break; - } case tok::ampamp: if (AmpAmpTokenType != TT_Unknown) FormatTok->setFinalizedType(AmpAmpTokenType); @@ -3467,23 +3458,20 @@ void UnwrappedLineParser::parseAccessSpecifier() { /// \returns true if it parsed a clause. bool UnwrappedLineParser::parseRequires(bool SeenEqual) { assert(FormatTok->is(tok::kw_requires) && "'requires' expected"); - auto RequiresToken = FormatTok; // We try to guess if it is a requires clause, or a requires expression. For - // that we first consume the keyword and check the next token. - nextToken(); - - switch (FormatTok->Tok.getKind()) { + // that we first check the next token. + switch (Tokens->peekNextToken(/*SkipComment=*/true)->Tok.getKind()) { case tok::l_brace: // This can only be an expression, never a clause. - parseRequiresExpression(RequiresToken); + parseRequiresExpression(); return false; case tok::l_paren: // Clauses and expression can start with a paren, it's unclear what we have. break; default: // All other tokens can only be a clause. - parseRequiresClause(RequiresToken); + parseRequiresClause(); return true; } @@ -3494,13 +3482,13 @@ bool UnwrappedLineParser::parseRequires(bool SeenEqual) { // requires (C && ... // But first let's look behind. - auto *PreviousNonComment = RequiresToken->getPreviousNonComment(); + auto *PreviousNonComment = FormatTok->getPreviousNonComment(); if (!PreviousNonComment || PreviousNonComment->is(TT_RequiresExpressionLBrace)) { // If there is no token, or an expression left brace, we are a requires // clause within a requires expression. - parseRequiresClause(RequiresToken); + parseRequiresClause(); return true; } @@ -3512,7 +3500,7 @@ bool UnwrappedLineParser::parseRequires(bool SeenEqual) { case tok::star: case tok::amp: // This is a requires clause. - parseRequiresClause(RequiresToken); + parseRequiresClause(); return true; case tok::ampamp: { // This can be either: @@ -3523,7 +3511,7 @@ bool UnwrappedLineParser::parseRequires(bool SeenEqual) { // void member(...) const && requires (C ... auto PrevPrev = PreviousNonComment->getPreviousNonComment(); if ((PrevPrev && PrevPrev->is(tok::kw_const)) || !SeenEqual) { - parseRequiresClause(RequiresToken); + parseRequiresClause(); return true; } break; @@ -3531,11 +3519,11 @@ bool UnwrappedLineParser::parseRequires(bool SeenEqual) { default: if (PreviousNonComment->isTypeOrIdentifier(LangOpts)) { // This is a requires clause. - parseRequiresClause(RequiresToken); + parseRequiresClause(); return true; } // It's an expression. - parseRequiresExpression(RequiresToken); + parseRequiresExpression(); return false; } @@ -3564,7 +3552,7 @@ bool UnwrappedLineParser::parseRequires(bool SeenEqual) { case tok::comma: if (OpenAngles == 0) { FormatTok = Tokens->setPosition(StoredPosition); - parseRequiresExpression(RequiresToken); + parseRequiresExpression(); return false; } break; @@ -3579,7 +3567,7 @@ bool UnwrappedLineParser::parseRequires(bool SeenEqual) { case tok::identifier: if (FoundType && !LastWasColonColon && OpenAngles == 0) { FormatTok = Tokens->setPosition(StoredPosition); - parseRequiresExpression(RequiresToken); + parseRequiresExpression(); return false; } FoundType = true; @@ -3594,7 +3582,7 @@ bool UnwrappedLineParser::parseRequires(bool SeenEqual) { default: if (NextToken->isTypeName(LangOpts)) { FormatTok = Tokens->setPosition(StoredPosition); - parseRequiresExpression(RequiresToken); + parseRequiresExpression(); return false; } break; @@ -3602,31 +3590,29 @@ bool UnwrappedLineParser::parseRequires(bool SeenEqual) { } // This seems to be a complicated expression, just assume it's a clause. FormatTok = Tokens->setPosition(StoredPosition); - parseRequiresClause(RequiresToken); + parseRequiresClause(); return true; } /// Parses a requires clause. -/// \param RequiresToken The requires keyword token, which starts this clause. -/// \pre We need to be on the next token after the requires keyword. /// \sa parseRequiresExpression /// /// Returns if it either has finished parsing the clause, or it detects, that /// the clause is incorrect. -void UnwrappedLineParser::parseRequiresClause(FormatToken *RequiresToken) { - assert(FormatTok->getPreviousNonComment() == RequiresToken); - assert(RequiresToken->is(tok::kw_requires) && "'requires' expected"); +void UnwrappedLineParser::parseRequiresClause() { + assert(FormatTok->is(tok::kw_requires) && "'requires' expected"); // If there is no previous token, we are within a requires expression, // otherwise we will always have the template or function declaration in front // of it. bool InRequiresExpression = - !RequiresToken->Previous || - RequiresToken->Previous->is(TT_RequiresExpressionLBrace); + !FormatTok->Previous || + FormatTok->Previous->is(TT_RequiresExpressionLBrace); - RequiresToken->setFinalizedType(InRequiresExpression - ? TT_RequiresClauseInARequiresExpression - : TT_RequiresClause); + FormatTok->setFinalizedType(InRequiresExpression + ? TT_RequiresClauseInARequiresExpression + : TT_RequiresClause); + nextToken(); // NOTE: parseConstraintExpression is only ever called from this function. // It could be inlined into here. @@ -3637,17 +3623,15 @@ void UnwrappedLineParser::parseRequiresClause(FormatToken *RequiresToken) { } /// Parses a requires expression. -/// \param RequiresToken The requires keyword token, which starts this clause. -/// \pre We need to be on the next token after the requires keyword. /// \sa parseRequiresClause /// /// Returns if it either has finished parsing the expression, or it detects, /// that the expression is incorrect. -void UnwrappedLineParser::parseRequiresExpression(FormatToken *RequiresToken) { - assert(FormatTok->getPreviousNonComment() == RequiresToken); - assert(RequiresToken->is(tok::kw_requires) && "'requires' expected"); +void UnwrappedLineParser::parseRequiresExpression() { + assert(FormatTok->is(tok::kw_requires) && "'requires' expected"); - RequiresToken->setFinalizedType(TT_RequiresExpression); + FormatTok->setFinalizedType(TT_RequiresExpression); + nextToken(); if (FormatTok->is(tok::l_paren)) { FormatTok->setFinalizedType(TT_RequiresExpressionLParen); @@ -3687,12 +3671,9 @@ void UnwrappedLineParser::parseConstraintExpression() { bool LambdaThisTimeAllowed = std::exchange(LambdaNextTimeAllowed, false); switch (FormatTok->Tok.getKind()) { - case tok::kw_requires: { - auto RequiresToken = FormatTok; - nextToken(); - parseRequiresExpression(RequiresToken); + case tok::kw_requires: + parseRequiresExpression(); break; - } case tok::l_paren: if (!TopLevelParensAllowed) diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h index 0161a5063ad40..86022d9b316c6 100644 --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -169,8 +169,8 @@ class UnwrappedLineParser { bool parseEnum(); bool parseStructLike(); bool parseRequires(bool SeenEqual); - void parseRequiresClause(FormatToken *RequiresToken); - void parseRequiresExpression(FormatToken *RequiresToken); + void parseRequiresClause(); + void parseRequiresExpression(); void parseConstraintExpression(); void parseCppExportBlock(); void parseNamespaceOrExportBlock(unsigned AddLevels);