Remove WrapperMatcherInterface

Summary:
WrapperMatcherInterface is an abstraction over a member variable -- in
other words, not much of an abstraction at all. I think it makes code
harder to read more than in helps with deduplication. Not to even
mention the questionable usage of the ~Interface suffix for a type with
state.

Reviewers: ymandel

Reviewed By: ymandel

Subscribers: arichardson, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D80704
This commit is contained in:
Dmitri Gribenko 2020-05-28 17:36:48 +02:00
parent 73c120a989
commit 35492270ed
1 changed files with 62 additions and 54 deletions

View File

@ -489,19 +489,6 @@ private:
IntrusiveRefCntPtr<DynMatcherInterface> Implementation; IntrusiveRefCntPtr<DynMatcherInterface> Implementation;
}; };
/// Wrapper base class for a wrapping matcher.
///
/// This is just a container for a DynTypedMatcher that can be used as a base
/// class for another matcher.
template <typename T>
class WrapperMatcherInterface : public MatcherInterface<T> {
protected:
explicit WrapperMatcherInterface(DynTypedMatcher &&InnerMatcher)
: InnerMatcher(std::move(InnerMatcher)) {}
const DynTypedMatcher InnerMatcher;
};
/// Wrapper of a MatcherInterface<T> *that allows copying. /// Wrapper of a MatcherInterface<T> *that allows copying.
/// ///
/// A Matcher<Base> can be used anywhere a Matcher<Derived> is /// A Matcher<Base> can be used anywhere a Matcher<Derived> is
@ -572,10 +559,12 @@ public:
/// does only matches in the absence of qualifiers, or not, i.e. simply /// does only matches in the absence of qualifiers, or not, i.e. simply
/// ignores any qualifiers. /// ignores any qualifiers.
template <typename TypeT> template <typename TypeT>
class TypeToQualType : public WrapperMatcherInterface<QualType> { class TypeToQualType : public MatcherInterface<QualType> {
const DynTypedMatcher InnerMatcher;
public: public:
TypeToQualType(const Matcher<TypeT> &InnerMatcher) TypeToQualType(const Matcher<TypeT> &InnerMatcher)
: TypeToQualType::WrapperMatcherInterface(InnerMatcher) {} : InnerMatcher(InnerMatcher) {}
bool matches(const QualType &Node, ASTMatchFinder *Finder, bool matches(const QualType &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
@ -764,13 +753,15 @@ Matcher<ObjCMessageExpr> hasAnySelectorFunc(
/// Type argument DeclMatcherT is required by PolymorphicMatcherWithParam1 but /// Type argument DeclMatcherT is required by PolymorphicMatcherWithParam1 but
/// not actually used. /// not actually used.
template <typename T, typename DeclMatcherT> template <typename T, typename DeclMatcherT>
class HasDeclarationMatcher : public WrapperMatcherInterface<T> { class HasDeclarationMatcher : public MatcherInterface<T> {
static_assert(std::is_same<DeclMatcherT, Matcher<Decl>>::value, static_assert(std::is_same<DeclMatcherT, Matcher<Decl>>::value,
"instantiated with wrong types"); "instantiated with wrong types");
const DynTypedMatcher InnerMatcher;
public: public:
explicit HasDeclarationMatcher(const Matcher<Decl> &InnerMatcher) explicit HasDeclarationMatcher(const Matcher<Decl> &InnerMatcher)
: HasDeclarationMatcher::WrapperMatcherInterface(InnerMatcher) {} : InnerMatcher(InnerMatcher) {}
bool matches(const T &Node, ASTMatchFinder *Finder, bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
@ -1181,14 +1172,14 @@ struct ArgumentAdaptingMatcherFunc {
} }
}; };
template <typename T> template <typename T> class TraversalMatcher : public MatcherInterface<T> {
class TraversalMatcher : public WrapperMatcherInterface<T> { const DynTypedMatcher InnerMatcher;
clang::TraversalKind Traversal; clang::TraversalKind Traversal;
public: public:
explicit TraversalMatcher(clang::TraversalKind TK, const Matcher<T> &ChildMatcher) explicit TraversalMatcher(clang::TraversalKind TK,
: TraversalMatcher::WrapperMatcherInterface(ChildMatcher), Traversal(TK) { const Matcher<T> &InnerMatcher)
} : InnerMatcher(InnerMatcher), Traversal(TK) {}
bool matches(const T &Node, ASTMatchFinder *Finder, bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
@ -1337,10 +1328,12 @@ public:
/// ///
/// ChildT must be an AST base type. /// ChildT must be an AST base type.
template <typename T, typename ChildT> template <typename T, typename ChildT>
class HasMatcher : public WrapperMatcherInterface<T> { class HasMatcher : public MatcherInterface<T> {
const DynTypedMatcher InnerMatcher;
public: public:
explicit HasMatcher(const Matcher<ChildT> &ChildMatcher) explicit HasMatcher(const Matcher<ChildT> &InnerMatcher)
: HasMatcher::WrapperMatcherInterface(ChildMatcher) {} : InnerMatcher(InnerMatcher) {}
bool matches(const T &Node, ASTMatchFinder *Finder, bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
@ -1356,16 +1349,18 @@ public:
/// As opposed to the HasMatcher, the ForEachMatcher will produce a match /// As opposed to the HasMatcher, the ForEachMatcher will produce a match
/// for each child that matches. /// for each child that matches.
template <typename T, typename ChildT> template <typename T, typename ChildT>
class ForEachMatcher : public WrapperMatcherInterface<T> { class ForEachMatcher : public MatcherInterface<T> {
static_assert(IsBaseType<ChildT>::value, static_assert(IsBaseType<ChildT>::value,
"for each only accepts base type matcher"); "for each only accepts base type matcher");
public: const DynTypedMatcher InnerMatcher;
explicit ForEachMatcher(const Matcher<ChildT> &ChildMatcher)
: ForEachMatcher::WrapperMatcherInterface(ChildMatcher) {}
bool matches(const T& Node, ASTMatchFinder* Finder, public:
BoundNodesTreeBuilder* Builder) const override { explicit ForEachMatcher(const Matcher<ChildT> &InnerMatcher)
: InnerMatcher(InnerMatcher) {}
bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override {
return Finder->matchesChildOf( return Finder->matchesChildOf(
Node, this->InnerMatcher, Builder, Node, this->InnerMatcher, Builder,
TraversalKind::TK_IgnoreImplicitCastsAndParentheses, TraversalKind::TK_IgnoreImplicitCastsAndParentheses,
@ -1469,17 +1464,19 @@ BindableMatcher<T> makeDynCastAllOfComposite(
/// ///
/// DescendantT must be an AST base type. /// DescendantT must be an AST base type.
template <typename T, typename DescendantT> template <typename T, typename DescendantT>
class HasDescendantMatcher : public WrapperMatcherInterface<T> { class HasDescendantMatcher : public MatcherInterface<T> {
static_assert(IsBaseType<DescendantT>::value, static_assert(IsBaseType<DescendantT>::value,
"has descendant only accepts base type matcher"); "has descendant only accepts base type matcher");
const DynTypedMatcher DescendantMatcher;
public: public:
explicit HasDescendantMatcher(const Matcher<DescendantT> &DescendantMatcher) explicit HasDescendantMatcher(const Matcher<DescendantT> &DescendantMatcher)
: HasDescendantMatcher::WrapperMatcherInterface(DescendantMatcher) {} : DescendantMatcher(DescendantMatcher) {}
bool matches(const T &Node, ASTMatchFinder *Finder, bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
return Finder->matchesDescendantOf(Node, this->InnerMatcher, Builder, return Finder->matchesDescendantOf(Node, this->DescendantMatcher, Builder,
ASTMatchFinder::BK_First); ASTMatchFinder::BK_First);
} }
}; };
@ -1489,17 +1486,19 @@ public:
/// ///
/// \c ParentT must be an AST base type. /// \c ParentT must be an AST base type.
template <typename T, typename ParentT> template <typename T, typename ParentT>
class HasParentMatcher : public WrapperMatcherInterface<T> { class HasParentMatcher : public MatcherInterface<T> {
static_assert(IsBaseType<ParentT>::value, static_assert(IsBaseType<ParentT>::value,
"has parent only accepts base type matcher"); "has parent only accepts base type matcher");
const DynTypedMatcher ParentMatcher;
public: public:
explicit HasParentMatcher(const Matcher<ParentT> &ParentMatcher) explicit HasParentMatcher(const Matcher<ParentT> &ParentMatcher)
: HasParentMatcher::WrapperMatcherInterface(ParentMatcher) {} : ParentMatcher(ParentMatcher) {}
bool matches(const T &Node, ASTMatchFinder *Finder, bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
return Finder->matchesAncestorOf(Node, this->InnerMatcher, Builder, return Finder->matchesAncestorOf(Node, this->ParentMatcher, Builder,
ASTMatchFinder::AMM_ParentOnly); ASTMatchFinder::AMM_ParentOnly);
} }
}; };
@ -1509,17 +1508,19 @@ public:
/// ///
/// \c AncestorT must be an AST base type. /// \c AncestorT must be an AST base type.
template <typename T, typename AncestorT> template <typename T, typename AncestorT>
class HasAncestorMatcher : public WrapperMatcherInterface<T> { class HasAncestorMatcher : public MatcherInterface<T> {
static_assert(IsBaseType<AncestorT>::value, static_assert(IsBaseType<AncestorT>::value,
"has ancestor only accepts base type matcher"); "has ancestor only accepts base type matcher");
const DynTypedMatcher AncestorMatcher;
public: public:
explicit HasAncestorMatcher(const Matcher<AncestorT> &AncestorMatcher) explicit HasAncestorMatcher(const Matcher<AncestorT> &AncestorMatcher)
: HasAncestorMatcher::WrapperMatcherInterface(AncestorMatcher) {} : AncestorMatcher(AncestorMatcher) {}
bool matches(const T &Node, ASTMatchFinder *Finder, bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
return Finder->matchesAncestorOf(Node, this->InnerMatcher, Builder, return Finder->matchesAncestorOf(Node, this->AncestorMatcher, Builder,
ASTMatchFinder::AMM_All); ASTMatchFinder::AMM_All);
} }
}; };
@ -1531,18 +1532,20 @@ public:
/// As opposed to HasDescendantMatcher, ForEachDescendantMatcher will match /// As opposed to HasDescendantMatcher, ForEachDescendantMatcher will match
/// for each descendant node that matches instead of only for the first. /// for each descendant node that matches instead of only for the first.
template <typename T, typename DescendantT> template <typename T, typename DescendantT>
class ForEachDescendantMatcher : public WrapperMatcherInterface<T> { class ForEachDescendantMatcher : public MatcherInterface<T> {
static_assert(IsBaseType<DescendantT>::value, static_assert(IsBaseType<DescendantT>::value,
"for each descendant only accepts base type matcher"); "for each descendant only accepts base type matcher");
const DynTypedMatcher DescendantMatcher;
public: public:
explicit ForEachDescendantMatcher( explicit ForEachDescendantMatcher(
const Matcher<DescendantT> &DescendantMatcher) const Matcher<DescendantT> &DescendantMatcher)
: ForEachDescendantMatcher::WrapperMatcherInterface(DescendantMatcher) {} : DescendantMatcher(DescendantMatcher) {}
bool matches(const T &Node, ASTMatchFinder *Finder, bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
return Finder->matchesDescendantOf(Node, this->InnerMatcher, Builder, return Finder->matchesDescendantOf(Node, this->DescendantMatcher, Builder,
ASTMatchFinder::BK_All); ASTMatchFinder::BK_All);
} }
}; };
@ -1635,10 +1638,12 @@ public:
/// Matches nodes of type \c TLoc for which the inner /// Matches nodes of type \c TLoc for which the inner
/// \c Matcher<T> matches. /// \c Matcher<T> matches.
template <typename TLoc, typename T> template <typename TLoc, typename T>
class LocMatcher : public WrapperMatcherInterface<TLoc> { class LocMatcher : public MatcherInterface<TLoc> {
const DynTypedMatcher InnerMatcher;
public: public:
explicit LocMatcher(const Matcher<T> &InnerMatcher) explicit LocMatcher(const Matcher<T> &InnerMatcher)
: LocMatcher::WrapperMatcherInterface(InnerMatcher) {} : InnerMatcher(InnerMatcher) {}
bool matches(const TLoc &Node, ASTMatchFinder *Finder, bool matches(const TLoc &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
@ -1657,10 +1662,12 @@ private:
/// \c QualType. /// \c QualType.
/// ///
/// Used to implement the \c loc() matcher. /// Used to implement the \c loc() matcher.
class TypeLocTypeMatcher : public WrapperMatcherInterface<TypeLoc> { class TypeLocTypeMatcher : public MatcherInterface<TypeLoc> {
const DynTypedMatcher InnerMatcher;
public: public:
explicit TypeLocTypeMatcher(const Matcher<QualType> &InnerMatcher) explicit TypeLocTypeMatcher(const Matcher<QualType> &InnerMatcher)
: TypeLocTypeMatcher::WrapperMatcherInterface(InnerMatcher) {} : InnerMatcher(InnerMatcher) {}
bool matches(const TypeLoc &Node, ASTMatchFinder *Finder, bool matches(const TypeLoc &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
@ -1674,13 +1681,13 @@ public:
/// Matches nodes of type \c T for which the inner matcher matches on a /// Matches nodes of type \c T for which the inner matcher matches on a
/// another node of type \c T that can be reached using a given traverse /// another node of type \c T that can be reached using a given traverse
/// function. /// function.
template <typename T> template <typename T> class TypeTraverseMatcher : public MatcherInterface<T> {
class TypeTraverseMatcher : public WrapperMatcherInterface<T> { const DynTypedMatcher InnerMatcher;
public: public:
explicit TypeTraverseMatcher(const Matcher<QualType> &InnerMatcher, explicit TypeTraverseMatcher(const Matcher<QualType> &InnerMatcher,
QualType (T::*TraverseFunction)() const) QualType (T::*TraverseFunction)() const)
: TypeTraverseMatcher::WrapperMatcherInterface(InnerMatcher), : InnerMatcher(InnerMatcher), TraverseFunction(TraverseFunction) {}
TraverseFunction(TraverseFunction) {}
bool matches(const T &Node, ASTMatchFinder *Finder, bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {
@ -1699,12 +1706,13 @@ private:
/// matcher matches on a another node of type \c T that can be reached using a /// matcher matches on a another node of type \c T that can be reached using a
/// given traverse function. /// given traverse function.
template <typename T> template <typename T>
class TypeLocTraverseMatcher : public WrapperMatcherInterface<T> { class TypeLocTraverseMatcher : public MatcherInterface<T> {
const DynTypedMatcher InnerMatcher;
public: public:
explicit TypeLocTraverseMatcher(const Matcher<TypeLoc> &InnerMatcher, explicit TypeLocTraverseMatcher(const Matcher<TypeLoc> &InnerMatcher,
TypeLoc (T::*TraverseFunction)() const) TypeLoc (T::*TraverseFunction)() const)
: TypeLocTraverseMatcher::WrapperMatcherInterface(InnerMatcher), : InnerMatcher(InnerMatcher), TraverseFunction(TraverseFunction) {}
TraverseFunction(TraverseFunction) {}
bool matches(const T &Node, ASTMatchFinder *Finder, bool matches(const T &Node, ASTMatchFinder *Finder,
BoundNodesTreeBuilder *Builder) const override { BoundNodesTreeBuilder *Builder) const override {