diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -7255,14 +7255,24 @@ -
Matches if the call expression's callee's declaration matches the -given matcher. ++ Matcher<CallExpr> callee Matcher<Decl> InnerMatcher @@ -8814,6 +8824,27 @@ Matches 1) if the call expression's callee's declaration matches the +given matcher; or 2) if the Obj-C message expression's callee's method +declaration matches the given matcher. Example matches y.x() (matcher = callExpr(callee( cxxMethodDecl(hasName("x"))))) class Y { public: void x(); }; void z() { Y y; y.x(); } + +Example 2. Matches [I foo] with +objcMessageExpr(callee(objcMethodDecl(hasName("foo")))) + + @interface I: NSObject + +(void)foo; + @end + ... + [I foo]
Matches 1) if the call expression's callee's declaration matches the
+given matcher; or 2) if the Obj-C message expression's callee's method
+declaration matches the given matcher.
+
+Example matches y.x() (matcher = callExpr(callee(
+ cxxMethodDecl(hasName("x")))))
+ class Y { public: void x(); };
+ void z() { Y y; y.x(); }
+
+Example 2. Matches [I foo] with
+objcMessageExpr(callee(objcMethodDecl(hasName("foo"))))
+
+ @interface I: NSObject
+ +(void)foo;
+ @end
+ ...
+ [I foo]
+Matches any argument of a call expression or a constructor call
expression, or an ObjC-message-send expression.
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -3838,8 +3838,9 @@
InnerMatcher.matches(*ExprNode, Finder, Builder));
}
-/// Matches if the call expression's callee's declaration matches the
-/// given matcher.
+/// Matches 1) if the call expression's callee's declaration matches the
+/// given matcher; or 2) if the Obj-C message expression's callee's method
+/// declaration matches the given matcher.
///
/// Example matches y.x() (matcher = callExpr(callee(
/// cxxMethodDecl(hasName("x")))))
@@ -3847,9 +3848,31 @@
/// class Y { public: void x(); };
/// void z() { Y y; y.x(); }
/// \endcode
-AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher, InnerMatcher,
- 1) {
- return callExpr(hasDeclaration(InnerMatcher)).matches(Node, Finder, Builder);
+///
+/// Example 2. Matches [I foo] with
+/// objcMessageExpr(callee(objcMethodDecl(hasName("foo"))))
+///
+/// \code
+/// @interface I: NSObject
+/// +(void)foo;
+/// @end
+/// ...
+/// [I foo]
+/// \endcode
+AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
+ callee, AST_POLYMORPHIC_SUPPORTED_TYPES(ObjCMessageExpr, CallExpr),
+ internal::Matcher, InnerMatcher, 1) {
+ if (const auto *CallNode = dyn_cast(&Node))
+ return callExpr(hasDeclaration(InnerMatcher))
+ .matches(Node, Finder, Builder);
+ else {
+ // The dynamic cast below is guaranteed to succeed as there are only 2
+ // supported return types.
+ const auto *MsgNode = dyn_cast(&Node);
+ const Decl *DeclNode = MsgNode->getMethodDecl();
+ return (DeclNode != nullptr &&
+ InnerMatcher.matches(*DeclNode, Finder, Builder));
+ }
}
/// Matches if the expression's or declaration's type matches a type
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
--- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2307,6 +2307,45 @@
hasName("cc"), hasInitializer(integerLiteral(equals(1))))))))));
}
+TEST(ASTMatchersTestObjC, ObjCMessageCalees) {
+ StatementMatcher MessagingFoo =
+ objcMessageExpr(callee(objcMethodDecl(hasName("foo"))));
+
+ EXPECT_TRUE(matchesObjC("@interface I"
+ "+ (void)foo;"
+ "@end\n"
+ "int main() {"
+ " [I foo];"
+ "}",
+ MessagingFoo));
+ EXPECT_TRUE(notMatchesObjC("@interface I"
+ "+ (void)foo;"
+ "+ (void)bar;"
+ "@end\n"
+ "int main() {"
+ " [I bar];"
+ "}",
+ MessagingFoo));
+ EXPECT_TRUE(matchesObjC("@interface I"
+ "- (void)foo;"
+ "- (void)bar;"
+ "@end\n"
+ "int main() {"
+ " I *i;"
+ " [i foo];"
+ "}",
+ MessagingFoo));
+ EXPECT_TRUE(notMatchesObjC("@interface I"
+ "- (void)foo;"
+ "- (void)bar;"
+ "@end\n"
+ "int main() {"
+ " I *i;"
+ " [i bar];"
+ "}",
+ MessagingFoo));
+}
+
TEST(ASTMatchersTestObjC, ObjCMessageExpr) {
// Don't find ObjCMessageExpr where none are present.
EXPECT_TRUE(notMatchesObjC("", objcMessageExpr(anything())));