[cfe-commits] [PATCH] Implement hasParent()-matcher

Daniel Jasper djasper at google.com
Mon Oct 22 07:56:47 PDT 2012


Hi klimek,

http://llvm-reviews.chandlerc.com/D71

Files:
  include/clang/ASTMatchers/ASTMatchers.h
  include/clang/ASTMatchers/ASTMatchersInternal.h
  lib/ASTMatchers/ASTMatchFinder.cpp
  unittests/ASTMatchers/ASTMatchersTest.cpp

Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -1402,6 +1402,24 @@
     DescendantT>(DescendantMatcher);
 }
 
+/// \brief Matches AST nodes that have a parent that matches the provided
+/// matcher.
+///
+/// Given
+/// \code
+/// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } }
+/// \endcode
+/// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }".
+///
+/// Usable as: Any Matcher
+template <typename ParentT>
+internal::ArgumentAdaptingMatcher<internal::HasParentMatcher, ParentT>
+hasParent(const internal::Matcher<ParentT> &AncestorMatcher) {
+  return internal::ArgumentAdaptingMatcher<
+    internal::HasParentMatcher,
+    ParentT>(AncestorMatcher);
+}
+
 /// \brief Matches AST nodes that have an ancestor that matches the provided
 /// matcher.
 ///
Index: include/clang/ASTMatchers/ASTMatchersInternal.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchersInternal.h
+++ include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -499,12 +499,13 @@
   template <typename T>
   bool matchesAncestorOf(const T &Node,
                          const DynTypedMatcher &Matcher,
-                         BoundNodesTreeBuilder *Builder) {
+                         BoundNodesTreeBuilder *Builder,
+                         bool ParentOnly) {
     TOOLING_COMPILE_ASSERT((llvm::is_base_of<Decl, T>::value ||
                             llvm::is_base_of<Stmt, T>::value),
                            only_Decl_or_Stmt_allowed_for_recursive_matching);
     return matchesAncestorOf(ast_type_traits::DynTypedNode::create(Node),
-                             Matcher, Builder);
+                             Matcher, Builder, ParentOnly);
   }
 
 protected:
@@ -521,7 +522,8 @@
 
   virtual bool matchesAncestorOf(const ast_type_traits::DynTypedNode &Node,
                                  const DynTypedMatcher &Matcher,
-                                 BoundNodesTreeBuilder *Builder) = 0;
+                                 BoundNodesTreeBuilder *Builder,
+                                 bool ParentOnly) = 0;
 };
 
 /// \brief Converts a \c Matcher<T> to a matcher of desired type \c To by
@@ -864,6 +866,29 @@
   const Matcher<DescendantT> DescendantMatcher;
 };
 
+/// \brief Matches nodes of type \c T that have a parent node of type \c ParentT
+/// for which the given inner matcher matches.
+///
+/// \c ParentT must be an AST base type.
+template <typename T, typename ParentT>
+class HasParentMatcher : public MatcherInterface<T> {
+  TOOLING_COMPILE_ASSERT(IsBaseType<ParentT>::value,
+                         has_parent_only_accepts_base_type_matcher);
+public:
+  explicit HasParentMatcher(const Matcher<ParentT> &ParentMatcher)
+      : ParentMatcher(ParentMatcher) {}
+
+  virtual bool matches(const T &Node,
+                       ASTMatchFinder *Finder,
+                       BoundNodesTreeBuilder *Builder) const {
+    return Finder->matchesAncestorOf(
+        Node, ParentMatcher, Builder, /*ParentOnly=*/true);
+  }
+
+ private:
+  const Matcher<ParentT> ParentMatcher;
+};
+
 /// \brief Matches nodes of type \c T that have at least one ancestor node of
 /// type \c AncestorT for which the given inner matcher matches.
 ///
@@ -880,7 +905,7 @@
                        ASTMatchFinder *Finder,
                        BoundNodesTreeBuilder *Builder) const {
     return Finder->matchesAncestorOf(
-        Node, AncestorMatcher, Builder);
+        Node, AncestorMatcher, Builder, /*ParentOnly=*/false);
   }
 
  private:
Index: lib/ASTMatchers/ASTMatchFinder.cpp
===================================================================
--- lib/ASTMatchers/ASTMatchFinder.cpp
+++ lib/ASTMatchers/ASTMatchFinder.cpp
@@ -352,16 +352,16 @@
                                   const Matcher<NamedDecl> &Base,
                                   BoundNodesTreeBuilder *Builder);
 
-  // Implements ASTMatchFinder::MatchesChildOf.
+  // Implements ASTMatchFinder::matchesChildOf.
   virtual bool matchesChildOf(const ast_type_traits::DynTypedNode &Node,
                               const DynTypedMatcher &Matcher,
                               BoundNodesTreeBuilder *Builder,
                               TraversalKind Traversal,
                               BindKind Bind) {
     return matchesRecursively(Node, Matcher, Builder, 1, Traversal,
                               Bind);
   }
-  // Implements ASTMatchFinder::MatchesDescendantOf.
+  // Implements ASTMatchFinder::matchesDescendantOf.
   virtual bool matchesDescendantOf(const ast_type_traits::DynTypedNode &Node,
                                    const DynTypedMatcher &Matcher,
                                    BoundNodesTreeBuilder *Builder,
@@ -372,7 +372,8 @@
   // Implements ASTMatchFinder::matchesAncestorOf.
   virtual bool matchesAncestorOf(const ast_type_traits::DynTypedNode &Node,
                                  const DynTypedMatcher &Matcher,
-                                 BoundNodesTreeBuilder *Builder) {
+                                 BoundNodesTreeBuilder *Builder,
+                                 bool ParentOnly) {
     if (!Parents) {
       // We always need to run over the whole translation unit, as
       // \c hasAncestor can escape any subtree.
@@ -395,6 +396,8 @@
       Ancestor = I->second;
       if (Matcher.matches(Ancestor, this, Builder))
         return true;
+      if (ParentOnly)
+        return false;
     }
     return false;
   }
Index: unittests/ASTMatchers/ASTMatchersTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -2884,6 +2884,15 @@
               hasAncestor(recordDecl(hasName("A")))))))));
 }
 
+TEST(HasParent, MatchesOnlyParent) {
+  EXPECT_TRUE(matches(
+      "void f() { if (true) { int x = 42; } }",
+      compoundStmt(hasParent(ifStmt()))));
+  EXPECT_TRUE(notMatches(
+      "void f() { for (;;) { int x = 42; } }",
+      compoundStmt(hasParent(ifStmt()))));
+}
+
 TEST(TypeMatching, MatchesTypes) {
   EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71.1.patch
Type: text/x-patch
Size: 6293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121022/15c17bf1/attachment.bin>


More information about the cfe-commits mailing list