Skip to content

Commit 2497945

Browse files
authored
[clang-format] Correctly annotate requires clause in && requires( (#132882)
Fix #132334
1 parent 629ff2d commit 2497945

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,7 @@ void UnwrappedLineParser::parseStructuralElement(
17081708
break;
17091709
}
17101710

1711+
bool SeenEqual = false;
17111712
for (const bool InRequiresExpression =
17121713
OpeningBrace && OpeningBrace->isOneOf(TT_RequiresExpressionLBrace,
17131714
TT_CompoundRequirementLBrace);
@@ -1782,7 +1783,7 @@ void UnwrappedLineParser::parseStructuralElement(
17821783
break;
17831784
case tok::kw_requires: {
17841785
if (IsCpp) {
1785-
bool ParsedClause = parseRequires();
1786+
bool ParsedClause = parseRequires(SeenEqual);
17861787
if (ParsedClause)
17871788
return;
17881789
} else {
@@ -2062,6 +2063,7 @@ void UnwrappedLineParser::parseStructuralElement(
20622063
break;
20632064
}
20642065

2066+
SeenEqual = true;
20652067
nextToken();
20662068
if (FormatTok->is(tok::l_brace)) {
20672069
// Block kind should probably be set to BK_BracedInit for any language.
@@ -3416,7 +3418,7 @@ void UnwrappedLineParser::parseAccessSpecifier() {
34163418
/// \brief Parses a requires, decides if it is a clause or an expression.
34173419
/// \pre The current token has to be the requires keyword.
34183420
/// \returns true if it parsed a clause.
3419-
bool UnwrappedLineParser::parseRequires() {
3421+
bool UnwrappedLineParser::parseRequires(bool SeenEqual) {
34203422
assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
34213423
auto RequiresToken = FormatTok;
34223424

@@ -3472,7 +3474,7 @@ bool UnwrappedLineParser::parseRequires() {
34723474
// We check the one token before that for a const:
34733475
// void member(...) const && requires (C<T> ...
34743476
auto PrevPrev = PreviousNonComment->getPreviousNonComment();
3475-
if (PrevPrev && PrevPrev->is(tok::kw_const)) {
3477+
if ((PrevPrev && PrevPrev->is(tok::kw_const)) || !SeenEqual) {
34763478
parseRequiresClause(RequiresToken);
34773479
return true;
34783480
}

clang/lib/Format/UnwrappedLineParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class UnwrappedLineParser {
167167
void parseAccessSpecifier();
168168
bool parseEnum();
169169
bool parseStructLike();
170-
bool parseRequires();
170+
bool parseRequires(bool SeenEqual);
171171
void parseRequiresClause(FormatToken *RequiresToken);
172172
void parseRequiresExpression(FormatToken *RequiresToken);
173173
void parseConstraintExpression();

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
13991399
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
14001400
EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
14011401
EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1402+
1403+
Tokens = annotate("void foo() &&\n"
1404+
" requires(!bar)\n"
1405+
"{\n"
1406+
" baz();\n"
1407+
"}");
1408+
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1409+
EXPECT_TOKEN(Tokens[4], tok::ampamp, TT_PointerOrReference);
1410+
EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
14021411
}
14031412

14041413
TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {

0 commit comments

Comments
 (0)