Skip to content

Commit 0951449

Browse files
committedJul 14, 2014
[ASTMatchers] Make hasOverloadedOperatorName also match freestanding overloads.
Freestanding overloads are represented as FunctionDecls in the AST, make the matcher also match them. Differential Revision: http://reviews.llvm.org/D4493 llvm-svn: 212940
1 parent 8e25416 commit 0951449

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed
 

‎clang/include/clang/ASTMatchers/ASTMatchers.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1542,14 +1542,14 @@ AST_MATCHER_P(NamedDecl, matchesName, std::string, RegExp) {
15421542
/// line and \c recordDecl(hasMethod(hasOverloadedOperatorName("*"))) matches
15431543
/// the declaration of \c A.
15441544
///
1545-
/// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<CXXMethodDecl>
1545+
/// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<FunctionDecl>
15461546
inline internal::PolymorphicMatcherWithParam1<
15471547
internal::HasOverloadedOperatorNameMatcher, StringRef,
1548-
AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, CXXMethodDecl)>
1548+
AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, FunctionDecl)>
15491549
hasOverloadedOperatorName(const StringRef Name) {
15501550
return internal::PolymorphicMatcherWithParam1<
15511551
internal::HasOverloadedOperatorNameMatcher, StringRef,
1552-
AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, CXXMethodDecl)>(
1552+
AST_POLYMORPHIC_SUPPORTED_TYPES_2(CXXOperatorCallExpr, FunctionDecl)>(
15531553
Name);
15541554
}
15551555

‎clang/include/clang/ASTMatchers/ASTMatchersInternal.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ template <typename T> struct has_getDecl {
517517
template <typename T, typename ArgT>
518518
class HasOverloadedOperatorNameMatcher : public SingleNodeMatcherInterface<T> {
519519
static_assert(std::is_same<T, CXXOperatorCallExpr>::value ||
520-
std::is_same<T, CXXMethodDecl>::value,
520+
std::is_base_of<FunctionDecl, T>::value,
521521
"unsupported class for matcher");
522522
static_assert(std::is_same<ArgT, StringRef>::value,
523523
"argument type must be StringRef");
@@ -541,7 +541,7 @@ class HasOverloadedOperatorNameMatcher : public SingleNodeMatcherInterface<T> {
541541

542542
/// \brief Returns true only if CXXMethodDecl represents an overloaded
543543
/// operator and has the given operator name.
544-
bool matchesSpecialized(const CXXMethodDecl &Node) const {
544+
bool matchesSpecialized(const FunctionDecl &Node) const {
545545
return Node.isOverloadedOperator() &&
546546
getOperatorSpelling(Node.getOverloadedOperator()) == Name;
547547
}

‎clang/unittests/ASTMatchers/ASTMatchersTest.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1095,12 +1095,19 @@ TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) {
10951095
"bool operator&&(Y x, Y y) { return true; }; "
10961096
"Y a; Y b; bool c = a && b;",
10971097
OpCallLessLess));
1098+
StatementMatcher OpStarCall =
1099+
operatorCallExpr(hasOverloadedOperatorName("*"));
1100+
EXPECT_TRUE(matches("class Y; int operator*(Y &); void f(Y &y) { *y; }",
1101+
OpStarCall));
10981102
DeclarationMatcher ClassWithOpStar =
10991103
recordDecl(hasMethod(hasOverloadedOperatorName("*")));
11001104
EXPECT_TRUE(matches("class Y { int operator*(); };",
11011105
ClassWithOpStar));
11021106
EXPECT_TRUE(notMatches("class Y { void myOperator(); };",
11031107
ClassWithOpStar)) ;
1108+
DeclarationMatcher AnyOpStar = functionDecl(hasOverloadedOperatorName("*"));
1109+
EXPECT_TRUE(matches("class Y; int operator*(Y &);", AnyOpStar));
1110+
EXPECT_TRUE(matches("class Y { int operator*(); };", AnyOpStar));
11041111
}
11051112

11061113
TEST(Matcher, NestedOverloadedOperatorCalls) {

0 commit comments

Comments
 (0)