[PATCH] Replace overloaded ASTMatchFinder::addMatcher() function with one that takes a DynTypedMatcher.

Samuel Benzaquen sbenza at google.com
Wed May 8 12:12:21 PDT 2013


Hi klimek,

Replace overloaded ASTMatchFinder::addMatcher() function with one that takes a DynTypedMatcher.
Add DynTypedMatcher::clone() to support the change.
Make the visitor also try to match the inner Type value when matching QualTypes. This fixes an issue caused by the change where Matcher<Type> objects are not being implicitly converted to Matcher<QualType> when calling addMatcher().
This change is required by D714.

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

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

Index: include/clang/ASTMatchers/ASTMatchFinder.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchFinder.h
+++ include/clang/ASTMatchers/ASTMatchFinder.h
@@ -117,17 +117,7 @@
   ///
   /// Does not take ownership of 'Action'.
   /// @{
-  void addMatcher(const DeclarationMatcher &NodeMatch,
-                  MatchCallback *Action);
-  void addMatcher(const TypeMatcher &NodeMatch,
-                  MatchCallback *Action);
-  void addMatcher(const StatementMatcher &NodeMatch,
-                  MatchCallback *Action);
-  void addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
-                  MatchCallback *Action);
-  void addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
-                  MatchCallback *Action);
-  void addMatcher(const TypeLocMatcher &NodeMatch,
+  void addMatcher(const internal::DynTypedMatcher &NodeMatch,
                   MatchCallback *Action);
   /// @}
 
Index: include/clang/ASTMatchers/ASTMatchersInternal.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchersInternal.h
+++ include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -239,6 +239,9 @@
                        ASTMatchFinder *Finder,
                        BoundNodesTreeBuilder *Builder) const = 0;
 
+  /// \brief Makes a copy of this matcher object.
+  virtual DynTypedMatcher *clone() const = 0;
+
   /// \brief Returns a unique ID for the matcher.
   virtual uint64_t getID() const = 0;
 };
@@ -285,6 +288,9 @@
     return Implementation->matches(Node, Finder, Builder);
   }
 
+  /// \brief Makes a copy of this matcher object.
+  virtual Matcher<T> *clone() const { return new Matcher<T>(*this); }
+
   /// \brief Returns an ID that uniquely identifies the matcher.
   uint64_t getID() const {
     /// FIXME: Document the requirements this imposes on matcher
Index: lib/ASTMatchers/ASTMatchFinder.cpp
===================================================================
--- lib/ASTMatchers/ASTMatchFinder.cpp
+++ lib/ASTMatchers/ASTMatchFinder.cpp
@@ -409,6 +409,13 @@
     }
   }
 
+  // We assume that the QualType and the contained type are on the same
+  // hierarchy level. Thus, we try to match either of them.
+  void match(QualType Node) {
+    match(ast_type_traits::DynTypedNode::create(Node));
+    if (!Node.isNull()) match(*Node);
+  }
+
   template <typename T> void match(const T &Node) {
     match(ast_type_traits::DynTypedNode::create(Node));
   }
@@ -708,40 +715,9 @@
   }
 }
 
-void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch,
-                             MatchCallback *Action) {
-  MatcherCallbackPairs.push_back(std::make_pair(
-    new internal::Matcher<Decl>(NodeMatch), Action));
-}
-
-void MatchFinder::addMatcher(const TypeMatcher &NodeMatch,
-                             MatchCallback *Action) {
-  MatcherCallbackPairs.push_back(std::make_pair(
-    new internal::Matcher<QualType>(NodeMatch), Action));
-}
-
-void MatchFinder::addMatcher(const StatementMatcher &NodeMatch,
-                             MatchCallback *Action) {
-  MatcherCallbackPairs.push_back(std::make_pair(
-    new internal::Matcher<Stmt>(NodeMatch), Action));
-}
-
-void MatchFinder::addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
-                             MatchCallback *Action) {
-  MatcherCallbackPairs.push_back(std::make_pair(
-    new NestedNameSpecifierMatcher(NodeMatch), Action));
-}
-
-void MatchFinder::addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
-                             MatchCallback *Action) {
-  MatcherCallbackPairs.push_back(std::make_pair(
-    new NestedNameSpecifierLocMatcher(NodeMatch), Action));
-}
-
-void MatchFinder::addMatcher(const TypeLocMatcher &NodeMatch,
+void MatchFinder::addMatcher(const internal::DynTypedMatcher &NodeMatch,
                              MatchCallback *Action) {
-  MatcherCallbackPairs.push_back(std::make_pair(
-    new TypeLocMatcher(NodeMatch), Action));
+  MatcherCallbackPairs.push_back(std::make_pair(NodeMatch.clone(), Action));
 }
 
 ASTConsumer *MatchFinder::newASTConsumer() {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D770.1.patch
Type: text/x-patch
Size: 4135 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130508/561898d6/attachment.bin>


More information about the cfe-commits mailing list