Skip to content

Commit e6d7f46

Browse files
authored
[clang-format] Correctly annotate user-defined conversion function call (#137914)
Fix #137770
1 parent 8effc8d commit e6d7f46

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,11 +1656,15 @@ class AnnotatingParser {
16561656
// Skip to l_paren.
16571657
for (LParen = CurrentToken->Next;
16581658
LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1659+
if (LParen->isPointerOrReference())
1660+
LParen->setFinalizedType(TT_PointerOrReference);
16591661
}
16601662
}
16611663
if (LParen && LParen->is(tok::l_paren)) {
1662-
Tok->setFinalizedType(TT_FunctionDeclarationName);
1663-
LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1664+
if (!Contexts.back().IsExpression) {
1665+
Tok->setFinalizedType(TT_FunctionDeclarationName);
1666+
LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1667+
}
16641668
break;
16651669
}
16661670
}
@@ -1683,7 +1687,8 @@ class AnnotatingParser {
16831687
if (CurrentToken->is(tok::comma) && Previous->isNot(tok::kw_operator))
16841688
break;
16851689
if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1686-
tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1690+
tok::arrow) ||
1691+
Previous->isPointerOrReference() ||
16871692
// User defined literal.
16881693
Previous->TokenText.starts_with("\"\"")) {
16891694
Previous->setType(TT_OverloadedOperator);
@@ -3026,7 +3031,7 @@ class AnnotatingParser {
30263031

30273032
if (!NextToken ||
30283033
NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
3029-
TT_RequiresClause, TT_FunctionDeclarationLParen) ||
3034+
TT_RequiresClause) ||
30303035
(NextToken->is(tok::kw_noexcept) && !IsExpression) ||
30313036
NextToken->canBePointerOrReferenceQualifier() ||
30323037
(NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
392392

393393
Tokens = annotate("return s.operator int *();");
394394
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
395-
EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_FunctionDeclarationName);
395+
// Not TT_FunctionDeclarationName.
396+
EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_Unknown);
396397
EXPECT_TOKEN(Tokens[5], tok::star, TT_PointerOrReference);
397-
EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionDeclarationLParen);
398+
// Not TT_FunctionDeclarationLParen.
399+
EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_Unknown);
400+
401+
Tokens = annotate("B &b = x.operator B &();");
402+
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
403+
EXPECT_TOKEN(Tokens[8], tok::amp, TT_PointerOrReference);
398404

399405
Tokens = annotate("int8_t *a = MacroCall(int8_t, width * height * length);");
400406
ASSERT_EQ(Tokens.size(), 16u) << Tokens;

0 commit comments

Comments
 (0)