Skip to content

[clang-format] Add LT_RequiresExpression and LT_SimpleRequirement #121681

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 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,10 @@ class AnnotatingParser {
return false;
break;
case tok::l_brace:
if (Style.Language == FormatStyle::LK_TextProto) {
if (IsCpp) {
if (Tok->is(TT_RequiresExpressionLBrace))
Line.Type = LT_RequiresExpression;
} else if (Style.Language == FormatStyle::LK_TextProto) {
FormatToken *Previous = Tok->getPreviousNonComment();
if (Previous && Previous->isNot(TT_DictLiteral))
Previous->setType(TT_SelectorName);
Expand Down Expand Up @@ -2024,8 +2027,11 @@ class AnnotatingParser {
if (!consumeToken())
return LT_Invalid;
}
if (Line.Type == LT_AccessModifier)
return LT_AccessModifier;
if (const auto Type = Line.Type; Type == LT_AccessModifier ||
Type == LT_RequiresExpression ||
Type == LT_SimpleRequirement) {
return Type;
}
if (KeywordVirtualFound)
return LT_VirtualFunctionDecl;
if (ImportStatement)
Expand Down Expand Up @@ -3102,8 +3108,10 @@ class AnnotatingParser {
}
}

if (!Scopes.empty() && Scopes.back() == ST_CompoundRequirement)
if (Line.Type == LT_SimpleRequirement ||
(!Scopes.empty() && Scopes.back() == ST_CompoundRequirement)) {
return TT_BinaryOperator;
}

return TT_PointerOrReference;
}
Expand Down Expand Up @@ -3693,8 +3701,15 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {

if (!Line.Children.empty()) {
ScopeStack.push_back(ST_ChildBlock);
for (auto &Child : Line.Children)
const bool InRequiresExpression = Line.Type == LT_RequiresExpression;
for (auto &Child : Line.Children) {
if (InRequiresExpression &&
!Child->First->isOneOf(tok::kw_typename, tok::kw_requires,
TT_CompoundRequirementLBrace)) {
Child->Type = LT_SimpleRequirement;
}
annotate(*Child);
}
// ScopeStack can become empty if Child has an unmatched `}`.
if (!ScopeStack.empty())
ScopeStack.pop_back();
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Format/TokenAnnotator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ enum LineType {
LT_VirtualFunctionDecl,
LT_ArrayOfStructInitializer,
LT_CommentAbovePPDirective,
LT_RequiresExpression,
LT_SimpleRequirement,
};

enum ScopeType {
Expand Down
27 changes: 15 additions & 12 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,21 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_CastRParen);
EXPECT_TOKEN(Tokens[11], tok::star, TT_BinaryOperator);

Tokens = annotate("template <typename T>\n"
"concept C = requires(T a, T b) { a && b; };");
ASSERT_EQ(Tokens.size(), 24u) << Tokens;
EXPECT_TOKEN(Tokens[16], tok::l_brace, TT_RequiresExpressionLBrace);
EXPECT_TOKEN(Tokens[18], tok::ampamp, TT_BinaryOperator);

Tokens = annotate("template <typename T, typename V>\n"
"concept CheckMultiplicableBy = requires(T a, V b) {\n"
" { a * b } -> std::same_as<T>;\n"
"};");
ASSERT_EQ(Tokens.size(), 36u) << Tokens;
EXPECT_TOKEN(Tokens[19], tok::l_brace, TT_RequiresExpressionLBrace);
EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_CompoundRequirementLBrace);
EXPECT_TOKEN(Tokens[22], tok::star, TT_BinaryOperator);
}

TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Expand Down Expand Up @@ -1456,18 +1471,6 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_RequiresExpressionLBrace);
}

TEST_F(TokenAnnotatorTest, CompoundRequirement) {
auto Tokens = annotate("template <typename T, typename V>\n"
"concept CheckMultiplicableBy = requires(T a, V b) {\n"
" { a * b } -> std::same_as<T>;\n"
"};");
ASSERT_EQ(Tokens.size(), 36u) << Tokens;

EXPECT_TOKEN(Tokens[19], tok::l_brace, TT_RequiresExpressionLBrace);
EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_CompoundRequirementLBrace);
EXPECT_TOKEN(Tokens[22], tok::star, TT_BinaryOperator);
}

TEST_F(TokenAnnotatorTest, UnderstandsPragmaRegion) {
// Everything after #pragma region should be ImplicitStringLiteral
auto Tokens = annotate("#pragma region Foo(Bar: Hello)");
Expand Down
Loading