Skip to content

Commit 0093450

Browse files
authored
[clang-format] Add TT_CompoundRequirementLBrace for better annotation (#121539)
Also, add `ST_CompoundRequirement` to help annotating */&/&& in compound requirement expressions as `TT_BinaryOperator`. Fixes #121471.
1 parent 743aee4 commit 0093450

File tree

6 files changed

+25
-4
lines changed

6 files changed

+25
-4
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace format {
4545
TYPE(CastRParen) \
4646
TYPE(ClassLBrace) \
4747
TYPE(ClassRBrace) \
48+
TYPE(CompoundRequirementLBrace) \
4849
/* ternary ?: expression */ \
4950
TYPE(ConditionalExpr) \
5051
/* the condition in an if statement */ \

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ class AnnotatingParser {
143143
case TT_StructLBrace:
144144
case TT_UnionLBrace:
145145
return ST_Class;
146+
case TT_CompoundRequirementLBrace:
147+
return ST_CompoundRequirement;
146148
default:
147149
return ST_Other;
148150
}
@@ -2076,7 +2078,7 @@ class AnnotatingParser {
20762078
TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
20772079
TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
20782080
TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2079-
TT_BracedListLBrace)) {
2081+
TT_CompoundRequirementLBrace, TT_BracedListLBrace)) {
20802082
CurrentToken->setType(TT_Unknown);
20812083
}
20822084
CurrentToken->Role.reset();
@@ -3100,6 +3102,9 @@ class AnnotatingParser {
31003102
}
31013103
}
31023104

3105+
if (!Scopes.empty() && Scopes.back() == ST_CompoundRequirement)
3106+
return TT_BinaryOperator;
3107+
31033108
return TT_PointerOrReference;
31043109
}
31053110

clang/lib/Format/TokenAnnotator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ enum ScopeType {
4040
ST_ChildBlock,
4141
// Contained in class declaration/definition.
4242
ST_Class,
43+
// Contained in compound requirement.
44+
ST_CompoundRequirement,
4345
// Contained within other scope block (function, loop, if/else, etc).
4446
ST_Other,
4547
};

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ class LineJoiner {
535535
// Try to merge records.
536536
if (TheLine->Last->is(TT_EnumLBrace)) {
537537
ShouldMerge = Style.AllowShortEnumsOnASingleLine;
538-
} else if (TheLine->Last->is(TT_RequiresExpressionLBrace)) {
538+
} else if (TheLine->Last->is(TT_CompoundRequirementLBrace)) {
539539
ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
540540
} else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
541541
// NOTE: We use AfterClass (whereas AfterStruct exists) for both classes

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
392392
break;
393393
case tok::l_brace:
394394
if (InRequiresExpression) {
395-
FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
395+
FormatTok->setFinalizedType(TT_CompoundRequirementLBrace);
396396
} else if (FormatTok->Previous &&
397397
FormatTok->Previous->ClosesRequiresClause) {
398398
// We need the 'default' case here to correctly parse a function
@@ -1705,7 +1705,8 @@ void UnwrappedLineParser::parseStructuralElement(
17051705
}
17061706

17071707
for (const bool InRequiresExpression =
1708-
OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
1708+
OpeningBrace && OpeningBrace->isOneOf(TT_RequiresExpressionLBrace,
1709+
TT_CompoundRequirementLBrace);
17091710
!eof();) {
17101711
if (IsCpp && FormatTok->isCppAlternativeOperatorKeyword()) {
17111712
if (auto *Next = Tokens->peekNextToken(/*SkipComment=*/true);

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,18 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {
14561456
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_RequiresExpressionLBrace);
14571457
}
14581458

1459+
TEST_F(TokenAnnotatorTest, CompoundRequirement) {
1460+
auto Tokens = annotate("template <typename T, typename V>\n"
1461+
"concept CheckMultiplicableBy = requires(T a, V b) {\n"
1462+
" { a * b } -> std::same_as<T>;\n"
1463+
"};");
1464+
ASSERT_EQ(Tokens.size(), 36u) << Tokens;
1465+
1466+
EXPECT_TOKEN(Tokens[19], tok::l_brace, TT_RequiresExpressionLBrace);
1467+
EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_CompoundRequirementLBrace);
1468+
EXPECT_TOKEN(Tokens[22], tok::star, TT_BinaryOperator);
1469+
}
1470+
14591471
TEST_F(TokenAnnotatorTest, UnderstandsPragmaRegion) {
14601472
// Everything after #pragma region should be ImplicitStringLiteral
14611473
auto Tokens = annotate("#pragma region Foo(Bar: Hello)");

0 commit comments

Comments
 (0)