From 2ca23c23d13d95ba66404c4ca3144e65a5ef4cb4 Mon Sep 17 00:00:00 2001 From: Yitzhak Mandelbaum Date: Fri, 15 Feb 2019 14:43:03 +0000 Subject: [PATCH] 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@354133 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../ASTMatchers/ASTMatchersTraversalTest.cpp | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp index 806603dff4..52950b37b0 100644 --- a/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -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"); -- 2.50.1