Remove VariadicOperatorMatcherInterface as it is redundant with logic from DynTypedMa...
authorSamuel Benzaquen <sbenza@google.com>
Mon, 17 Nov 2014 14:55:49 +0000 (14:55 +0000)
committerSamuel Benzaquen <sbenza@google.com>
Mon, 17 Nov 2014 14:55:49 +0000 (14:55 +0000)
Summary:
The generic variadic matcher is faster (one less virtual function call
per match) and doesn't require template instantiations which reduces
compile time and binary size.
Registry.cpp.o generates ~14% less symbols and compiles ~7.5% faster.
The change also speeds up our clang-tidy benchmark by ~2%.

Reviewers: klimek

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D6278

llvm-svn: 222131

clang/include/clang/ASTMatchers/ASTMatchersInternal.h
clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
clang/lib/ASTMatchers/ASTMatchersInternal.cpp
clang/lib/ASTMatchers/Dynamic/Marshallers.h
clang/lib/ASTMatchers/Dynamic/VariantValue.cpp

index c2615c6..ba571f0 100644 (file)
@@ -1121,36 +1121,11 @@ private:
 /// \brief VariadicOperatorMatcher related types.
 /// @{
 
-/// \brief Function signature for any variadic operator. It takes the inner
-///   matchers as an array of DynTypedMatcher.
-typedef bool (*VariadicOperatorFunction)(
-    const ast_type_traits::DynTypedNode DynNode, ASTMatchFinder *Finder,
-    BoundNodesTreeBuilder *Builder, ArrayRef<DynTypedMatcher> InnerMatchers);
-
-/// \brief \c MatcherInterface<T> implementation for an variadic operator.
-template <typename T>
-class VariadicOperatorMatcherInterface : public MatcherInterface<T> {
-public:
-  VariadicOperatorMatcherInterface(VariadicOperatorFunction Func,
-                                   std::vector<DynTypedMatcher> InnerMatchers)
-      : Func(Func), InnerMatchers(std::move(InnerMatchers)) {}
-
-  bool matches(const T &Node, ASTMatchFinder *Finder,
-               BoundNodesTreeBuilder *Builder) const override {
-    return Func(ast_type_traits::DynTypedNode::create(Node), Finder, Builder,
-                InnerMatchers);
-  }
-
-private:
-  const VariadicOperatorFunction Func;
-  const std::vector<DynTypedMatcher> InnerMatchers;
-};
-
 /// \brief "No argument" placeholder to use as template paratemers.
 struct VariadicOperatorNoArg {};
 
-/// \brief Polymorphic matcher object that uses a \c VariadicOperatorFunction
-///   operator.
+/// \brief Polymorphic matcher object that uses a \c
+/// DynTypedMatcher::VariadicOperatorFunction operator.
 ///
 /// Input matchers can have any type (including other polymorphic matcher
 /// types), and the actual Matcher<T> is generated on demand with an implicit
@@ -1165,7 +1140,8 @@ template <typename P1, typename P2 = VariadicOperatorNoArg,
           typename P9 = VariadicOperatorNoArg>
 class VariadicOperatorMatcher {
 public:
-  VariadicOperatorMatcher(VariadicOperatorFunction Func, const P1 &Param1,
+  VariadicOperatorMatcher(DynTypedMatcher::VariadicOperatorFunction Func,
+                          const P1 &Param1,
                           const P2 &Param2 = VariadicOperatorNoArg(),
                           const P3 &Param3 = VariadicOperatorNoArg(),
                           const P4 &Param4 = VariadicOperatorNoArg(),
@@ -1189,9 +1165,8 @@ public:
     addMatcher<T>(Param7, Matchers);
     addMatcher<T>(Param8, Matchers);
     addMatcher<T>(Param9, Matchers);
-    // FIXME: Use DynTypedMatcher::constructVariadic() instead.
-    return Matcher<T>(
-        new VariadicOperatorMatcherInterface<T>(Func, std::move(Matchers)));
+    return DynTypedMatcher::constructVariadic(Func, std::move(Matchers))
+        .template unconditionalConvertTo<T>();
   }
 
 private:
@@ -1206,7 +1181,7 @@ private:
   static void addMatcher(VariadicOperatorNoArg,
                          std::vector<DynTypedMatcher> &Matchers) {}
 
-  const VariadicOperatorFunction Func;
+  const DynTypedMatcher::VariadicOperatorFunction Func;
   const P1 Param1;
   const P2 Param2;
   const P3 Param3;
@@ -1224,7 +1199,7 @@ private:
 /// It supports 1-9 argument overloaded operator(). More can be added if needed.
 template <unsigned MinCount, unsigned MaxCount>
 struct VariadicOperatorMatcherFunc {
-  VariadicOperatorFunction Func;
+  DynTypedMatcher::VariadicOperatorFunction Func;
 
   template <unsigned Count, typename T>
   struct EnableIfValidArity
@@ -1350,9 +1325,9 @@ BindableMatcher<T> makeAllOfComposite(
   for (const auto *InnerMatcher : InnerMatchers) {
     DynMatchers.push_back(*InnerMatcher);
   }
-  // FIXME: Use DynTypedMatcher::constructVariadic() instead.
-  return BindableMatcher<T>(new VariadicOperatorMatcherInterface<T>(
-      AllOfVariadicOperator, std::move(DynMatchers)));
+  return BindableMatcher<T>(DynTypedMatcher::constructVariadic(
+                                AllOfVariadicOperator, std::move(DynMatchers))
+                                .template unconditionalConvertTo<T>());
 }
 
 /// \brief Creates a Matcher<T> that matches if
index d06b77c..e4b5519 100644 (file)
@@ -106,9 +106,9 @@ class VariantMatcher {
     /// \brief Constructs a variadic typed matcher from \p InnerMatchers.
     /// Will try to convert each inner matcher to the destination type and
     /// return llvm::None if it fails to do so.
-    llvm::Optional<DynTypedMatcher> constructVariadicOperator(
-        ast_matchers::internal::VariadicOperatorFunction Func,
-        ArrayRef<VariantMatcher> InnerMatchers) const;
+    llvm::Optional<DynTypedMatcher>
+    constructVariadicOperator(DynTypedMatcher::VariadicOperatorFunction Func,
+                              ArrayRef<VariantMatcher> InnerMatchers) const;
 
   protected:
     ~MatcherOps() {}
@@ -147,9 +147,9 @@ public:
   /// \brief Creates a 'variadic' operator matcher.
   ///
   /// It will bind to the appropriate type on getTypedMatcher<T>().
-  static VariantMatcher VariadicOperatorMatcher(
-      ast_matchers::internal::VariadicOperatorFunction Func,
-      std::vector<VariantMatcher> Args);
+  static VariantMatcher
+  VariadicOperatorMatcher(DynTypedMatcher::VariadicOperatorFunction Func,
+                          std::vector<VariantMatcher> Args);
 
   /// \brief Makes the matcher the "null" matcher.
   void reset();
index 06ec767..af6f0ab 100644 (file)
@@ -32,7 +32,7 @@ namespace {
 
 class VariadicMatcher : public DynMatcherInterface {
  public:
-  VariadicMatcher(VariadicOperatorFunction Func,
+  VariadicMatcher(DynTypedMatcher::VariadicOperatorFunction Func,
                   std::vector<DynTypedMatcher> InnerMatchers)
       : Func(Func), InnerMatchers(std::move(InnerMatchers)) {}
 
@@ -43,7 +43,7 @@ class VariadicMatcher : public DynMatcherInterface {
   }
 
  private:
-  VariadicOperatorFunction Func;
+  DynTypedMatcher::VariadicOperatorFunction Func;
   std::vector<DynTypedMatcher> InnerMatchers;
 };
 
@@ -86,7 +86,8 @@ static llvm::ManagedStatic<TrueMatcherImpl> TrueMatcherInstance;
 }  // namespace
 
 DynTypedMatcher DynTypedMatcher::constructVariadic(
-    VariadicOperatorFunction Func, std::vector<DynTypedMatcher> InnerMatchers) {
+    DynTypedMatcher::VariadicOperatorFunction Func,
+    std::vector<DynTypedMatcher> InnerMatchers) {
   assert(InnerMatchers.size() > 0 && "Array must not be empty.");
   assert(std::all_of(InnerMatchers.begin(), InnerMatchers.end(),
                      [&InnerMatchers](const DynTypedMatcher &M) {
index 30beb69..42c880e 100644 (file)
@@ -556,7 +556,7 @@ private:
 /// \brief Variadic operator marshaller function.
 class VariadicOperatorMatcherDescriptor : public MatcherDescriptor {
 public:
-  typedef ast_matchers::internal::VariadicOperatorFunction VarFunc;
+  typedef DynTypedMatcher::VariadicOperatorFunction VarFunc;
   VariadicOperatorMatcherDescriptor(unsigned MinCount, unsigned MaxCount,
                                     VarFunc Func, StringRef MatcherName)
       : MinCount(MinCount), MaxCount(MaxCount), Func(Func),
index 3d14157..08b3b1d 100644 (file)
@@ -58,7 +58,7 @@ VariantMatcher::MatcherOps::canConstructFrom(const DynTypedMatcher &Matcher,
 
 llvm::Optional<DynTypedMatcher>
 VariantMatcher::MatcherOps::constructVariadicOperator(
-    ast_matchers::internal::VariadicOperatorFunction Func,
+    DynTypedMatcher::VariadicOperatorFunction Func,
     ArrayRef<VariantMatcher> InnerMatchers) const {
   std::vector<DynTypedMatcher> DynMatchers;
   for (const auto &InnerMatcher : InnerMatchers) {
@@ -176,7 +176,7 @@ public:
 
 class VariantMatcher::VariadicOpPayload : public VariantMatcher::Payload {
 public:
-  VariadicOpPayload(ast_matchers::internal::VariadicOperatorFunction Func,
+  VariadicOpPayload(DynTypedMatcher::VariadicOperatorFunction Func,
                     std::vector<VariantMatcher> Args)
       : Func(Func), Args(std::move(Args)) {}
 
@@ -209,7 +209,7 @@ public:
   }
 
 private:
-  const ast_matchers::internal::VariadicOperatorFunction Func;
+  const DynTypedMatcher::VariadicOperatorFunction Func;
   const std::vector<VariantMatcher> Args;
 };
 
@@ -225,7 +225,7 @@ VariantMatcher::PolymorphicMatcher(std::vector<DynTypedMatcher> Matchers) {
 }
 
 VariantMatcher VariantMatcher::VariadicOperatorMatcher(
-    ast_matchers::internal::VariadicOperatorFunction Func,
+    DynTypedMatcher::VariadicOperatorFunction Func,
     std::vector<VariantMatcher> Args) {
   return VariantMatcher(new VariadicOpPayload(Func, std::move(Args)));
 }