From: Shuai Wang Date: Mon, 17 Sep 2018 18:48:43 +0000 (+0000) Subject: [ASTMatchers] Let isArrow also support UnresolvedMemberExpr, CXXDependentScopeMemberExpr X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=926e91b0e6be63d21bd89ea9a1cc5ca17d0f3528;p=clang [ASTMatchers] Let isArrow also support UnresolvedMemberExpr, CXXDependentScopeMemberExpr Reviewers: aaron.ballman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D52157 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@342407 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index 68a1ab5915..f4557edbcf 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -2235,6 +2235,32 @@ cxxConstructorDecl(hasAnyConstructorInitializer(isWritten())) +Matcher<CXXDependentScopeMemberExpr>isArrow +
Matches member expressions that are called with '->' as opposed
+to '.'.
+
+Member calls on the implicit this pointer match as called with '->'.
+
+Given
+  class Y {
+    void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
+    template <class T> void f() { this->f<T>(); f<T>(); }
+    int a;
+    static int b;
+  };
+  template <class T>
+  class Z {
+    void x() { this->m; }
+  };
+memberExpr(isArrow())
+  matches this->x, x, y.x, a, this->b
+cxxDependentScopeMemberExpr(isArrow())
+  matches this->m
+unresolvedMemberExpr(isArrow())
+  matches this->f<T>, f<T>
+
+ + Matcher<CXXMethodDecl>isConst
Matches if the given method declaration is const.
 
@@ -3228,11 +3254,20 @@ Member calls on the implicit this pointer match as called with '->'.
 Given
   class Y {
     void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
+    template <class T> void f() { this->f<T>(); f<T>(); }
     int a;
     static int b;
   };
+  template <class T>
+  class Z {
+    void x() { this->m; }
+  };
 memberExpr(isArrow())
   matches this->x, x, y.x, a, this->b
+cxxDependentScopeMemberExpr(isArrow())
+  matches this->m
+unresolvedMemberExpr(isArrow())
+  matches this->f<T>, f<T>
 
@@ -3886,6 +3921,32 @@ Example matches a || b (matcher = binaryOperator(hasOperatorName("||"))) +Matcher<UnresolvedMemberExpr>isArrow +
Matches member expressions that are called with '->' as opposed
+to '.'.
+
+Member calls on the implicit this pointer match as called with '->'.
+
+Given
+  class Y {
+    void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
+    template <class T> void f() { this->f<T>(); f<T>(); }
+    int a;
+    static int b;
+  };
+  template <class T>
+  class Z {
+    void x() { this->m; }
+  };
+memberExpr(isArrow())
+  matches this->x, x, y.x, a, this->b
+cxxDependentScopeMemberExpr(isArrow())
+  matches this->m
+unresolvedMemberExpr(isArrow())
+  matches this->f<T>, f<T>
+
+ + Matcher<VarDecl>hasAutomaticStorageDuration
Matches a variable declaration that has automatic storage duration.
 
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index 500d0a6670..50aa63cd95 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -4697,13 +4697,24 @@ AST_MATCHER(CXXMethodDecl, isUserProvided) {
 /// \code
 ///   class Y {
 ///     void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
+///     template  void f() { this->f(); f(); }
 ///     int a;
 ///     static int b;
 ///   };
+///   template 
+///   class Z {
+///     void x() { this->m; }
+///   };
 /// \endcode
 /// memberExpr(isArrow())
 ///   matches this->x, x, y.x, a, this->b
-AST_MATCHER(MemberExpr, isArrow) {
+/// cxxDependentScopeMemberExpr(isArrow())
+///   matches this->m
+/// unresolvedMemberExpr(isArrow())
+///   matches this->f, f
+AST_POLYMORPHIC_MATCHER(
+    isArrow, AST_POLYMORPHIC_SUPPORTED_TYPES(MemberExpr, UnresolvedMemberExpr,
+                                             CXXDependentScopeMemberExpr)) {
   return Node.isArrow();
 }
 
diff --git a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 537ffbb896..39a5d57729 100644
--- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -765,6 +765,11 @@ TEST(IsArrow, MatchesMemberVariablesViaArrow) {
                       memberExpr(isArrow())));
   EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };",
                          memberExpr(isArrow())));
+  EXPECT_TRUE(matches("template  class Y { void x() { this->m; } };",
+                      cxxDependentScopeMemberExpr(isArrow())));
+  EXPECT_TRUE(
+      notMatches("template  class Y { void x() { (*this).m; } };",
+                 cxxDependentScopeMemberExpr(isArrow())));
 }
 
 TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) {
@@ -783,6 +788,14 @@ TEST(IsArrow, MatchesMemberCallsViaArrow) {
                       memberExpr(isArrow())));
   EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };",
                          memberExpr(isArrow())));
+  EXPECT_TRUE(
+      matches("class Y { template  void x() { this->x(); } };",
+              unresolvedMemberExpr(isArrow())));
+  EXPECT_TRUE(matches("class Y { template  void x() { x(); } };",
+                      unresolvedMemberExpr(isArrow())));
+  EXPECT_TRUE(
+      notMatches("class Y { template  void x() { (*this).x(); } };",
+                 unresolvedMemberExpr(isArrow())));
 }
 
 TEST(ConversionDeclaration, IsExplicit) {
diff --git a/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
index 8860170bae..b40289fcd5 100644
--- a/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -27,7 +27,7 @@ TEST(Finder, DynamicOnlyAcceptsSomeMatchers) {
                                        nullptr));
 
   // Do not accept non-toplevel matchers.
-  EXPECT_FALSE(Finder.addDynamicMatcher(isArrow(), nullptr));
+  EXPECT_FALSE(Finder.addDynamicMatcher(isMain(), nullptr));
   EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x"), nullptr));
 }
 
diff --git a/unittests/ASTMatchers/Dynamic/RegistryTest.cpp b/unittests/ASTMatchers/Dynamic/RegistryTest.cpp
index aae229bd04..1ca394d8d8 100644
--- a/unittests/ASTMatchers/Dynamic/RegistryTest.cpp
+++ b/unittests/ASTMatchers/Dynamic/RegistryTest.cpp
@@ -440,7 +440,8 @@ TEST_F(RegistryTest, Errors) {
       Error.get()).isNull());
   EXPECT_EQ("Incorrect type for arg 1. "
             "(Expected = Matcher) != "
-            "(Actual = Matcher&Matcher)",
+            "(Actual = Matcher&Matcher"
+            ")",
             Error->toString());
 }