Author: Owen Pan Date: 2025-04-30T20:08:00-07:00 New Revision: e6d7f46ce9a6ffdfc206802131e0d79afb624b3f
URL: https://github.com/llvm/llvm-project/commit/e6d7f46ce9a6ffdfc206802131e0d79afb624b3f DIFF: https://github.com/llvm/llvm-project/commit/e6d7f46ce9a6ffdfc206802131e0d79afb624b3f.diff LOG: [clang-format] Correctly annotate user-defined conversion function call (#137914) Fix #137770 Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index e56cc92987af7..ea0086fd49a33 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1656,11 +1656,15 @@ class AnnotatingParser { // Skip to l_paren. for (LParen = CurrentToken->Next; LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) { + if (LParen->isPointerOrReference()) + LParen->setFinalizedType(TT_PointerOrReference); } } if (LParen && LParen->is(tok::l_paren)) { - Tok->setFinalizedType(TT_FunctionDeclarationName); - LParen->setFinalizedType(TT_FunctionDeclarationLParen); + if (!Contexts.back().IsExpression) { + Tok->setFinalizedType(TT_FunctionDeclarationName); + LParen->setFinalizedType(TT_FunctionDeclarationLParen); + } break; } } @@ -1683,7 +1687,8 @@ class AnnotatingParser { if (CurrentToken->is(tok::comma) && Previous->isNot(tok::kw_operator)) break; if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma, - tok::star, tok::arrow, tok::amp, tok::ampamp) || + tok::arrow) || + Previous->isPointerOrReference() || // User defined literal. Previous->TokenText.starts_with("\"\"")) { Previous->setType(TT_OverloadedOperator); @@ -3026,7 +3031,7 @@ class AnnotatingParser { if (!NextToken || NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren, - TT_RequiresClause, TT_FunctionDeclarationLParen) || + TT_RequiresClause) || (NextToken->is(tok::kw_noexcept) && !IsExpression) || NextToken->canBePointerOrReferenceQualifier() || (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) { diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 844f51f9e7c2e..124fb43dcf5ac 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -392,9 +392,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { Tokens = annotate("return s.operator int *();"); ASSERT_EQ(Tokens.size(), 10u) << Tokens; - EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_FunctionDeclarationName); + // Not TT_FunctionDeclarationName. + EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_Unknown); EXPECT_TOKEN(Tokens[5], tok::star, TT_PointerOrReference); - EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionDeclarationLParen); + // Not TT_FunctionDeclarationLParen. + EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_Unknown); + + Tokens = annotate("B &b = x.operator B &();"); + ASSERT_EQ(Tokens.size(), 13u) << Tokens; + EXPECT_TOKEN(Tokens[8], tok::amp, TT_PointerOrReference); Tokens = annotate("int8_t *a = MacroCall(int8_t, width * height * length);"); ASSERT_EQ(Tokens.size(), 16u) << Tokens; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits