diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp --- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp +++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -150,15 +150,9 @@ }; /// A matcher that always returns true. -/// -/// We only ever need one instance of this matcher, so we create a global one -/// and reuse it to reduce the overhead of the matcher and increase the chance -/// of cache hits. class TrueMatcherImpl : public DynMatcherInterface { public: - TrueMatcherImpl() { - Retain(); // Reference count will never become zero. - } + TrueMatcherImpl() = default; bool dynMatches(const DynTypedNode &, ASTMatchFinder *, BoundNodesTreeBuilder *) const override { @@ -193,8 +187,6 @@ } // namespace -static llvm::ManagedStatic TrueMatcherInstance; - bool ASTMatchFinder::isTraversalIgnoringImplicitNodes() const { return getASTContext().getParentMapContext().getTraversalKind() == TK_IgnoreUnlessSpelledInSource; @@ -273,7 +265,12 @@ } DynTypedMatcher DynTypedMatcher::trueMatcher(ASTNodeKind NodeKind) { - return DynTypedMatcher(NodeKind, NodeKind, &*TrueMatcherInstance); + // We only ever need one instance of TrueMatcherImpl, so we create a static + // instance and reuse it to reduce the overhead of the matcher and increase + // the chance of cache hits. + static const llvm::IntrusiveRefCntPtr Instance = + new TrueMatcherImpl(); + return DynTypedMatcher(NodeKind, NodeKind, Instance); } bool DynTypedMatcher::canMatchNodesOfKind(ASTNodeKind Kind) const { diff --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h --- a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h +++ b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h @@ -75,6 +75,18 @@ RefCountedBase(const RefCountedBase &) {} RefCountedBase &operator=(const RefCountedBase &) = delete; +#ifndef NDEBUG + ~RefCountedBase() { + assert(RefCount == 0 && + "Destruction occured when there are still references to this."); + } +#else + // Default the destructor in release builds, A trivial destructor may enable + // better codegen. + ~RefCountedBase() = default; +#endif + +public: void Retain() const { ++RefCount; } void Release() const { @@ -94,6 +106,17 @@ ThreadSafeRefCountedBase & operator=(const ThreadSafeRefCountedBase &) = delete; +#ifndef NDEBUG + ~ThreadSafeRefCountedBase() { + assert(RefCount == 0 && + "Destruction occured when there are still references to this."); + } +#else + // Default the destructor in release builds, A trivial destructor may enable + // better codegen. + ~ThreadSafeRefCountedBase() = default; +#endif + public: void Retain() const { RefCount.fetch_add(1, std::memory_order_relaxed); }