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 @@ -156,9 +156,7 @@ /// 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 { 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 @@ -70,11 +70,23 @@ template class RefCountedBase { mutable unsigned RefCount = 0; -public: +protected: RefCountedBase() = default; 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); } diff --git a/llvm/include/llvm/Support/ManagedStatic.h b/llvm/include/llvm/Support/ManagedStatic.h --- a/llvm/include/llvm/Support/ManagedStatic.h +++ b/llvm/include/llvm/Support/ManagedStatic.h @@ -15,21 +15,79 @@ #include #include +#include namespace llvm { +namespace detail { +// Helper struct to detect classes that has Retain and Release member functions +// that are const with no params. +template struct HasRetainRelease { +private: + template + static constexpr decltype(std::declval().Retain(), + std::declval().Release(), bool()) + test(int) { + return true; + } + + template static constexpr bool test(...) { return false; } + +public: + static constexpr bool Value = test(int()); +}; + +template +std::enable_if_t::Value, void *> createObject() { + return new T(); +} + +template +std::enable_if_t::Value, void *> createObject() { + T *Ptr = new T(); + Ptr->Retain(); + return Ptr; +} + +template +std::enable_if_t::Value> deleteObject(T *Ptr) { + delete Ptr; +} + +template +std::enable_if_t::Value> deleteObject(T *Ptr) { + Ptr->Release(); +} + +template +std::enable_if_t::Value> deleteObject(T *Ptr) { + delete[] Ptr; +} + +template +std::enable_if_t::Value> deleteObject(T *Ptr) { + for (auto I = Ptr, E = I + N; I != E; ++I) + I->Release(); +} +} // namespace detail + /// object_creator - Helper method for ManagedStatic. -template struct object_creator { - static void *call() { return new C(); } + +template struct object_creator { + static void *call() { return detail::createObject(); } }; /// object_deleter - Helper method for ManagedStatic. -/// template struct object_deleter { - static void call(void *Ptr) { delete (T *)Ptr; } + static void call(void *Ptr) { + return detail::deleteObject(static_cast(Ptr)); + } }; + template struct object_deleter { - static void call(void *Ptr) { delete[](T *)Ptr; } + static void call(void *Ptr) { + return detail::deleteObject(static_cast(Ptr)); + } }; // ManagedStatic must be initialized to zero, and it must *not* have a dynamic