Skip to content

Commit

Permalink
[clang-format] Preserve AmpAmpTokenType in nested parentheses
Browse files Browse the repository at this point in the history
When parsing a requires clause, the UnwrappedLineParser would delegate to
parseParens with an AmpAmpTokenType set to BinaryOperator. However,
parseParens would not carry this over into any nested parens, meaning it
could assign a different token type to an && in a requires clause.

This patch makes sure parseParens inherits its parameter when performing
a recursive call.

Fixes #63251

Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D153641
  • Loading branch information
rymiel committed Jun 26, 2023
1 parent 6f065bf commit 15e14f1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2431,14 +2431,14 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,

/// \brief Parses a pair of parentheses (and everything between them).
/// \param AmpAmpTokenType If different than TT_Unknown sets this type for all
/// double ampersands. This only counts for the current parens scope.
/// double ampersands. This applies for all nested scopes as well.
void UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
assert(FormatTok->is(tok::l_paren) && "'(' expected.");
nextToken();
do {
switch (FormatTok->Tok.getKind()) {
case tok::l_paren:
parseParens();
parseParens(AmpAmpTokenType);
if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
parseChildBlock();
break;
Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,26 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
annotate("auto bar() -> Template<type> requires(is_integral_v<T>) {}");
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
EXPECT_TOKEN(Tokens[9], tok::kw_requires, TT_RequiresClause);

Tokens = annotate("void foo() requires((A<T>) && C) {}");
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
EXPECT_TOKEN(Tokens[4], tok::kw_requires, TT_RequiresClause);
EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_BinaryOperator);

Tokens = annotate("void foo() requires(((A<T>) && C)) {}");
ASSERT_EQ(Tokens.size(), 20u) << Tokens;
EXPECT_TOKEN(Tokens[4], tok::kw_requires, TT_RequiresClause);
EXPECT_TOKEN(Tokens[13], tok::ampamp, TT_BinaryOperator);

Tokens = annotate("void foo() requires([](T&&){}(t)) {}");
ASSERT_EQ(Tokens.size(), 21u) << Tokens;
EXPECT_TOKEN(Tokens[4], tok::kw_requires, TT_RequiresClause);
EXPECT_TOKEN(Tokens[10], tok::ampamp, TT_PointerOrReference);

Tokens = annotate("void foo() requires([](T&& u){}(t)) {}");
ASSERT_EQ(Tokens.size(), 22u) << Tokens;
EXPECT_TOKEN(Tokens[4], tok::kw_requires, TT_RequiresClause);
EXPECT_TOKEN(Tokens[10], tok::ampamp, TT_PointerOrReference);
}

TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {
Expand Down

0 comments on commit 15e14f1

Please sign in to comment.