aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests
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/unittests
parent7c666c14f82ee13ddd222aba9543a5579e608e03 (diff)
[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/unittests')
-rw-r--r--clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp39
-rw-r--r--clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp19
2 files changed, 58 insertions, 0 deletions
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
index 195ac67c804d..a83927e6de24 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2307,6 +2307,45 @@ TEST_P(ASTMatchersTest,
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())));
diff --git a/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp b/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp
index 318fd3ad1857..7c7d45ac565f 100644
--- a/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp
+++ b/clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp
@@ -198,13 +198,32 @@ TEST_F(RegistryTest, OverloadedMatchers) {
constructMatcher("hasName", StringRef("x")))))
.getTypedMatcher<Stmt>();
+ Matcher<Stmt> ObjCMsgExpr =
+ constructMatcher(
+ "objcMessageExpr",
+ constructMatcher(
+ "callee",
+ constructMatcher("objcMethodDecl",
+ constructMatcher("hasName", StringRef("x")))))
+ .getTypedMatcher<Stmt>();
+
std::string Code = "class Y { public: void x(); }; void z() { Y y; y.x(); }";
EXPECT_FALSE(matches(Code, CallExpr0));
EXPECT_TRUE(matches(Code, CallExpr1));
+ EXPECT_FALSE(matches(Code, ObjCMsgExpr));
Code = "class Z { public: void z() { this->z(); } };";
EXPECT_TRUE(matches(Code, CallExpr0));
EXPECT_FALSE(matches(Code, CallExpr1));
+ EXPECT_FALSE(matches(Code, ObjCMsgExpr));
+
+ Code = "@interface I "
+ "+ (void)x; "
+ "@end\n"
+ "int main() { [I x]; }";
+ EXPECT_FALSE(matchesObjC(Code, CallExpr0));
+ EXPECT_FALSE(matchesObjC(Code, CallExpr1));
+ EXPECT_TRUE(matchesObjC(Code, ObjCMsgExpr));
Matcher<Decl> DeclDecl = declaratorDecl(hasTypeLoc(
constructMatcher(