aboutsummaryrefslogtreecommitdiff
path: root/clang/include
diff options
context:
space:
mode:
authorZiqing Luo <ziqing@udel.edu>2022-07-21 13:20:19 -0700
committerZiqing Luo <ziqingluo@Ziqings-iMac-Pro.local>2022-07-21 13:35:31 -0700
commitb17baa1db613a2ce777aa122feb87488750a64d0 (patch)
treefeef68260d507bcd635f380f8f1d5350a205f6ab /clang/include
parent7c666c14f82ee13ddd222aba9543a5579e608e03 (diff)
downloadllvm-b17baa1db613a2ce777aa122feb87488750a64d0.zip
llvm-b17baa1db613a2ce777aa122feb87488750a64d0.tar.gz
llvm-b17baa1db613a2ce777aa122feb87488750a64d0.tar.bz2
[ASTMatchers] Adding a new matcher for callee declarations of Obj-C
message expressions For an Obj-C message expression `[o m]`, the adding matcher will match the declaration of the method `m`. This commit overloads the existing `callee` ASTMatcher, which originally was only for C/C++ nodes but also applies to Obj-C messages now. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D129398
Diffstat (limited to 'clang/include')
-rw-r--r--clang/include/clang/ASTMatchers/ASTMatchers.h33
1 files changed, 28 insertions, 5 deletions
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index ae5502d..9f4d807 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -3838,8 +3838,9 @@ AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>,
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 @@ AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>,
/// class Y { public: void x(); };
/// void z() { Y y; y.x(); }
/// \endcode
-AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, 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<Decl>, InnerMatcher, 1) {
+ if (const auto *CallNode = dyn_cast<CallExpr>(&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 = cast<ObjCMessageExpr>(&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