[PATCH] D54405: Record whether a AST Matcher constructs a Node

Stephen Kelly via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 13 01:39:55 PST 2018


steveire updated this revision to Diff 173819.
steveire added a comment.

Update


Repository:
  rC Clang

https://reviews.llvm.org/D54405

Files:
  include/clang/ASTMatchers/ASTMatchersInternal.h
  lib/ASTMatchers/Dynamic/Registry.cpp


Index: lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- lib/ASTMatchers/Dynamic/Registry.cpp
+++ lib/ASTMatchers/Dynamic/Registry.cpp
@@ -44,18 +44,54 @@
 
 using ConstructorMap = llvm::StringMap<std::unique_ptr<const MatcherDescriptor>>;
 
+using NodeConstructorMap =
+    std::map<ast_type_traits::ASTNodeKind,
+             std::pair<std::string, const internal::MatcherDescriptor *>>;
+
 class RegistryMaps {
 public:
   RegistryMaps();
   ~RegistryMaps();
 
   const ConstructorMap &constructors() const { return Constructors; }
+  const NodeConstructorMap &nodeConstructors() const {
+    return NodeConstructors;
+  }
+
+  void registerNodeMatcher(ast_type_traits::ASTNodeKind kind,
+                           internal::MatcherDescriptor *Callback);
 
 private:
   void registerMatcher(StringRef MatcherName,
                        std::unique_ptr<MatcherDescriptor> Callback);
 
+  template <typename MatcherType>
+  void registerIfNodeMatcher(MatcherType,
+                             internal::MatcherDescriptor *Descriptor,
+                             StringRef) {}
+
+  template <typename ResultT>
+  void registerIfNodeMatcher(
+      ast_matchers::internal::VariadicAllOfMatcher<ResultT> VarFunc,
+      internal::MatcherDescriptor *Descriptor, StringRef MatcherName) {
+    auto NodeKind = ast_type_traits::ASTNodeKind::getFromNodeKind<
+        typename ast_matchers::internal::VariadicAllOfMatcher<ResultT>::Type>();
+    if (!NodeKind.isNone())
+      NodeConstructors[NodeKind] = std::make_pair(MatcherName, Descriptor);
+  }
+
+  template <typename BaseT, typename DerivedT>
+  void registerIfNodeMatcher(
+      ast_matchers::internal::VariadicDynCastAllOfMatcher<BaseT, DerivedT>
+          VarFunc,
+      internal::MatcherDescriptor *Descriptor, StringRef MatcherName) {
+    auto NodeKind = ast_type_traits::ASTNodeKind::getFromNodeKind<DerivedT>();
+    if (!NodeKind.isNone())
+      NodeConstructors[NodeKind] = std::make_pair(MatcherName, Descriptor);
+  }
+
   ConstructorMap Constructors;
+  NodeConstructorMap NodeConstructors;
 };
 
 } // namespace
@@ -67,8 +103,13 @@
 }
 
 #define REGISTER_MATCHER(name)                                                 \
-  registerMatcher(#name, internal::makeMatcherAutoMarshall(                    \
-                             ::clang::ast_matchers::name, #name));
+  {                                                                            \
+    auto Descriptor =                                                          \
+        internal::makeMatcherAutoMarshall(::clang::ast_matchers::name, #name); \
+    registerIfNodeMatcher(::clang::ast_matchers::name, Descriptor.get(),       \
+                          #name);                                              \
+    registerMatcher(#name, std::move(Descriptor));                             \
+  }
 
 #define REGISTER_MATCHER_OVERLOAD(name)                                        \
   registerMatcher(#name,                                                       \
Index: include/clang/ASTMatchers/ASTMatchersInternal.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchersInternal.h
+++ include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -1577,6 +1577,7 @@
     : public VariadicFunction<BindableMatcher<T>, Matcher<T>,
                               makeAllOfComposite<T>> {
 public:
+  using Type = T;
   VariadicAllOfMatcher() {}
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54405.173819.patch
Type: text/x-patch
Size: 3498 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181113/fabe0f9f/attachment-0001.bin>


More information about the cfe-commits mailing list