Skip to content

[clang-format] Add TT_CompoundRequirementLBrace for better annotation #121539

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 1 commit into from
Jan 5, 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
1 change: 1 addition & 0 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace format {
TYPE(CastRParen) \
TYPE(ClassLBrace) \
TYPE(ClassRBrace) \
TYPE(CompoundRequirementLBrace) \
/* ternary ?: expression */ \
TYPE(ConditionalExpr) \
/* the condition in an if statement */ \
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class AnnotatingParser {
case TT_StructLBrace:
case TT_UnionLBrace:
return ST_Class;
case TT_CompoundRequirementLBrace:
return ST_CompoundRequirement;
default:
return ST_Other;
}
Expand Down Expand Up @@ -2076,7 +2078,7 @@ class AnnotatingParser {
TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
TT_BracedListLBrace)) {
TT_CompoundRequirementLBrace, TT_BracedListLBrace)) {
CurrentToken->setType(TT_Unknown);
}
CurrentToken->Role.reset();
Expand Down Expand Up @@ -3100,6 +3102,9 @@ class AnnotatingParser {
}
}

if (!Scopes.empty() && Scopes.back() == ST_CompoundRequirement)
return TT_BinaryOperator;

return TT_PointerOrReference;
}

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 @@ -40,6 +40,8 @@ enum ScopeType {
ST_ChildBlock,
// Contained in class declaration/definition.
ST_Class,
// Contained in compound requirement.
ST_CompoundRequirement,
// Contained within other scope block (function, loop, if/else, etc).
ST_Other,
};
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ class LineJoiner {
// Try to merge records.
if (TheLine->Last->is(TT_EnumLBrace)) {
ShouldMerge = Style.AllowShortEnumsOnASingleLine;
} else if (TheLine->Last->is(TT_RequiresExpressionLBrace)) {
} else if (TheLine->Last->is(TT_CompoundRequirementLBrace)) {
ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
} else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
// NOTE: We use AfterClass (whereas AfterStruct exists) for both classes
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
break;
case tok::l_brace:
if (InRequiresExpression) {
FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
FormatTok->setFinalizedType(TT_CompoundRequirementLBrace);
} else if (FormatTok->Previous &&
FormatTok->Previous->ClosesRequiresClause) {
// We need the 'default' case here to correctly parse a function
Expand Down Expand Up @@ -1702,7 +1702,8 @@ void UnwrappedLineParser::parseStructuralElement(
}

for (const bool InRequiresExpression =
OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
OpeningBrace && OpeningBrace->isOneOf(TT_RequiresExpressionLBrace,
TT_CompoundRequirementLBrace);
!eof();) {
if (IsCpp && FormatTok->isCppAlternativeOperatorKeyword()) {
if (auto *Next = Tokens->peekNextToken(/*SkipComment=*/true);
Expand Down
12 changes: 12 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,18 @@ 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