[ASTMatchers ]Make MatcherDescriptors indicate the node type they match
authorStephen Kelly <steveire@gmail.com>
Sat, 6 Feb 2021 23:48:14 +0000 (23:48 +0000)
committerStephen Kelly <steveire@gmail.com>
Sun, 7 Feb 2021 15:13:28 +0000 (15:13 +0000)
clang/include/clang/ASTMatchers/Dynamic/Registry.h
clang/lib/ASTMatchers/Dynamic/Marshallers.h
clang/lib/ASTMatchers/Dynamic/Registry.cpp
clang/unittests/ASTMatchers/Dynamic/RegistryTest.cpp

index 215206b..9ce77c3 100644 (file)
@@ -66,6 +66,8 @@ class Registry {
 public:
   Registry() = delete;
 
+  static ASTNodeKind nodeMatcherType(MatcherCtor);
+
   /// Look up a matcher in the registry by name,
   ///
   /// \return An opaque value which may be used to refer to the matcher
index 4110623..71d7443 100644 (file)
@@ -309,6 +309,8 @@ public:
                                 ArrayRef<ParserValue> Args,
                                 Diagnostics *Error) const = 0;
 
+  virtual ASTNodeKind nodeMatcherType() const { return ASTNodeKind(); }
+
   /// Returns whether the matcher is variadic. Variadic matchers can take any
   /// number of arguments, but they must be of the same type.
   virtual bool isVariadic() const = 0;
@@ -576,6 +578,8 @@ public:
                                   LeastDerivedKind);
   }
 
+  ASTNodeKind nodeMatcherType() const override { return RetKinds[0]; }
+
 private:
   const RunFunc Func;
   const std::string MatcherName;
@@ -611,6 +615,8 @@ public:
     }
   }
 
+  ASTNodeKind nodeMatcherType() const override { return DerivedKind; }
+
 private:
   const ASTNodeKind DerivedKind;
 };
index 0887eb7..908a9ee 100644 (file)
@@ -562,6 +562,10 @@ RegistryMaps::~RegistryMaps() = default;
 
 static llvm::ManagedStatic<RegistryMaps> RegistryData;
 
+ASTNodeKind Registry::nodeMatcherType(MatcherCtor Ctor) {
+  return Ctor->nodeMatcherType();
+}
+
 // static
 llvm::Optional<MatcherCtor> Registry::lookupMatcherCtor(StringRef MatcherName) {
   auto it = RegistryData->constructors().find(MatcherName);
index 7d6c51f..386fd52 100644 (file)
@@ -497,6 +497,12 @@ TEST_F(RegistryTest, Completion) {
       "Matcher<CXXRecordDecl> isSameOrDerivedFrom(string|Matcher<NamedDecl>)"));
 }
 
+TEST_F(RegistryTest, NodeType) {
+  EXPECT_TRUE(Registry::nodeMatcherType(*lookupMatcherCtor("callExpr")).isSame(ASTNodeKind::getFromNodeKind<CallExpr>()));
+  EXPECT_TRUE(Registry::nodeMatcherType(*lookupMatcherCtor("has")).isNone());
+  EXPECT_TRUE(Registry::nodeMatcherType(*lookupMatcherCtor("allOf")).isNone());
+}
+
 TEST_F(RegistryTest, HasArgs) {
   Matcher<Decl> Value = constructMatcher(
       "decl", constructMatcher("hasAttr", StringRef("attr::WarnUnused")))