r354133 - Add tests for assorted `CXXMemberCallExpr` matchers.
Yitzhak Mandelbaum via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 15 06:43:03 PST 2019
Author: ymandel
Date: Fri Feb 15 06:43:03 2019
New Revision: 354133
URL: http://llvm.org/viewvc/llvm-project?rev=354133&view=rev
Log:
Add tests for assorted `CXXMemberCallExpr` matchers.
Summary: Add tests for matchers `on`, `onImplicitObjectArgument` and `hasObjectExpression`.
Reviewers: alexfh, steveire, aaron.ballman
Differential Revision: https://reviews.llvm.org/D56850
Modified:
cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp?rev=354133&r1=354132&r2=354133&view=diff
==============================================================================
--- cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp (original)
+++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp Fri Feb 15 06:43:03 2019
@@ -469,6 +469,95 @@ TEST(Matcher, isInstanceMessage) {
}
+TEST(MatcherCXXMemberCallExpr, On) {
+ auto MatchesType = cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("X")))));
+ EXPECT_TRUE(matches(
+ R"cc(
+ struct Y {
+ void m();
+ };
+ struct X : public Y {};
+ void z(X x) { x.m(); }
+ )cc",
+ MatchesType));
+ EXPECT_TRUE(notMatches(
+ R"cc(
+ struct Y {
+ void m();
+ };
+ void z(Y y) { y.m(); }
+ )cc",
+ MatchesType));
+
+ // Parens are ignored.
+ auto MatchesCall = cxxMemberCallExpr(on(callExpr()));
+ EXPECT_TRUE(matches(
+ R"cc(
+ struct Y {
+ void m();
+ };
+ Y g();
+ void z(Y y) { (g()).m(); }
+ )cc",
+ MatchesCall));
+}
+
+TEST(MatcherCXXMemberCallExpr, OnImplicitObjectArgument) {
+ auto Snippet1 = R"cc(
+ struct Y {
+ void m();
+ };
+ void z(Y y) { y.m(); }
+ )cc";
+ auto Snippet2 = R"cc(
+ struct Y {
+ void m();
+ };
+ struct X : public Y {};
+ void z(X x) { x.m(); }
+ )cc";
+ auto MatchesY = cxxMemberCallExpr(
+ onImplicitObjectArgument(hasType(cxxRecordDecl(hasName("Y")))));
+ EXPECT_TRUE(matches(Snippet1, MatchesY));
+ EXPECT_TRUE(matches(Snippet2, MatchesY));
+
+ auto MatchesX = cxxMemberCallExpr(
+ onImplicitObjectArgument(hasType(cxxRecordDecl(hasName("X")))));
+ EXPECT_TRUE(notMatches(Snippet2, MatchesX));
+
+ // Parens are not ignored.
+ auto MatchesCall = cxxMemberCallExpr(onImplicitObjectArgument(callExpr()));
+ EXPECT_TRUE(notMatches(
+ R"cc(
+ struct Y {
+ void m();
+ };
+ Y g();
+ void z(Y y) { (g()).m(); }
+ )cc",
+ MatchesCall));
+}
+
+TEST(Matcher, HasObjectExpr) {
+ auto M = memberExpr(hasObjectExpression(hasType(cxxRecordDecl(hasName("X")))));
+ EXPECT_TRUE(matches(
+ R"cc(
+ struct X {
+ int m;
+ int f(X x) { return x.m; }
+ };
+ )cc",
+ M));
+ EXPECT_TRUE(notMatches(
+ R"cc(
+ struct X {
+ int m;
+ int f(X x) { return m; }
+ };
+ )cc",
+ M));
+}
+
TEST(ForEachArgumentWithParam, ReportsNoFalsePositives) {
StatementMatcher ArgumentY =
declRefExpr(to(varDecl(hasName("y")))).bind("arg");
More information about the cfe-commits
mailing list