diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index 406ec93114a87..ce32039c14afc 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -1542,14 +1542,14 @@ AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) { /// line and \c recordDecl(hasMethod(hasOverloadedOperatorName("*"))) matches /// the declaration of \c A. /// -/// Usable as: Matcher, Matcher +/// Usable as: Matcher, Matcher inline internal::PolymorphicMatcherWithParam1< internal::HasOverloadedOperatorNameMatcher, StringRef, - AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, CXXMethodDecl)> + AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, FunctionDecl)> hasOverloadedOperatorName(const StringRef Name) { return internal::PolymorphicMatcherWithParam1< internal::HasOverloadedOperatorNameMatcher, StringRef, - AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, CXXMethodDecl)>( + AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, FunctionDecl)>( Name); } diff --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h index 12df77c15679b..94435fd274eb7 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -517,7 +517,7 @@ template struct has_getDecl { template class HasOverloadedOperatorNameMatcher : public SingleNodeMatcherInterface { static_assert(std::is_same::value || - std::is_same::value, + std::is_base_of::value, "unsupported class for matcher"); static_assert(std::is_same::value, "argument type must be StringRef"); @@ -541,7 +541,7 @@ class HasOverloadedOperatorNameMatcher : public SingleNodeMatcherInterface { /// \brief Returns true only if CXXMethodDecl represents an overloaded /// operator and has the given operator name. - bool matchesSpecialized(const CXXMethodDecl &Node) const { + bool matchesSpecialized(const FunctionDecl &Node) const { return Node.isOverloadedOperator() && getOperatorSpelling(Node.getOverloadedOperator()) == Name; } diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index e424acdaacdc3..f5cb130b1d734 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -1095,12 +1095,19 @@ TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) { "bool operator&&(Y x, Y y) { return true; }; " "Y a; Y b; bool c = a && b;", OpCallLessLess)); + StatementMatcher OpStarCall = + operatorCallExpr(hasOverloadedOperatorName("*")); + EXPECT_TRUE(matches("class Y; int operator*(Y &); void f(Y &y) { *y; }", + OpStarCall)); DeclarationMatcher ClassWithOpStar = recordDecl(hasMethod(hasOverloadedOperatorName("*"))); EXPECT_TRUE(matches("class Y { int operator*(); };", ClassWithOpStar)); EXPECT_TRUE(notMatches("class Y { void myOperator(); };", ClassWithOpStar)) ; + DeclarationMatcher AnyOpStar = functionDecl(hasOverloadedOperatorName("*")); + EXPECT_TRUE(matches("class Y; int operator*(Y &);", AnyOpStar)); + EXPECT_TRUE(matches("class Y { int operator*(); };", AnyOpStar)); } TEST(Matcher, NestedOverloadedOperatorCalls) {