2012-02-01 03:58:34 +08:00
|
|
|
//===- unittest/ADT/IntrusiveRefCntPtrTest.cpp ----------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-02-01 03:58:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2013-11-19 11:08:35 +08:00
|
|
|
namespace llvm {
|
2013-11-19 08:57:56 +08:00
|
|
|
|
[ADT] Delete RefCountedBaseVPTR.
Summary:
This class is unnecessary.
Its comment indicated that it was a compile error to allocate an
instance of a class that inherits from RefCountedBaseVPTR on the stack.
This may have been true at one point, but it's not today.
Moreover you really do not want to allocate *any* refcounted object on
the stack, vptrs or not, so if we did have a way to prevent these
objects from being stack-allocated, we'd want to apply it to regular
RefCountedBase too, obviating the need for a separate RefCountedBaseVPTR
class.
It seems that the main way RefCountedBaseVPTR provides safety is by
making its subclass's destructor virtual. This may have been helpful at
one point, but these days clang will emit an error if you define a class
with virtual functions that inherits from RefCountedBase but doesn't
have a virtual destructor.
Reviewers: compnerd, dblaikie
Subscribers: cfe-commits, klimek, llvm-commits, mgorny
Differential Revision: https://reviews.llvm.org/D28162
llvm-svn: 290717
2016-12-30 03:59:26 +08:00
|
|
|
namespace {
|
2020-12-04 07:28:40 +08:00
|
|
|
int NumInstances = 0;
|
|
|
|
template <template <typename> class Base>
|
|
|
|
struct SimpleRefCounted : Base<SimpleRefCounted<Base>> {
|
[ADT] Delete RefCountedBaseVPTR.
Summary:
This class is unnecessary.
Its comment indicated that it was a compile error to allocate an
instance of a class that inherits from RefCountedBaseVPTR on the stack.
This may have been true at one point, but it's not today.
Moreover you really do not want to allocate *any* refcounted object on
the stack, vptrs or not, so if we did have a way to prevent these
objects from being stack-allocated, we'd want to apply it to regular
RefCountedBase too, obviating the need for a separate RefCountedBaseVPTR
class.
It seems that the main way RefCountedBaseVPTR provides safety is by
making its subclass's destructor virtual. This may have been helpful at
one point, but these days clang will emit an error if you define a class
with virtual functions that inherits from RefCountedBase but doesn't
have a virtual destructor.
Reviewers: compnerd, dblaikie
Subscribers: cfe-commits, klimek, llvm-commits, mgorny
Differential Revision: https://reviews.llvm.org/D28162
llvm-svn: 290717
2016-12-30 03:59:26 +08:00
|
|
|
SimpleRefCounted() { ++NumInstances; }
|
2020-12-04 07:28:40 +08:00
|
|
|
SimpleRefCounted(const SimpleRefCounted &RHS) : Base<SimpleRefCounted>(RHS) {
|
2017-01-05 06:49:55 +08:00
|
|
|
++NumInstances;
|
|
|
|
}
|
[ADT] Delete RefCountedBaseVPTR.
Summary:
This class is unnecessary.
Its comment indicated that it was a compile error to allocate an
instance of a class that inherits from RefCountedBaseVPTR on the stack.
This may have been true at one point, but it's not today.
Moreover you really do not want to allocate *any* refcounted object on
the stack, vptrs or not, so if we did have a way to prevent these
objects from being stack-allocated, we'd want to apply it to regular
RefCountedBase too, obviating the need for a separate RefCountedBaseVPTR
class.
It seems that the main way RefCountedBaseVPTR provides safety is by
making its subclass's destructor virtual. This may have been helpful at
one point, but these days clang will emit an error if you define a class
with virtual functions that inherits from RefCountedBase but doesn't
have a virtual destructor.
Reviewers: compnerd, dblaikie
Subscribers: cfe-commits, klimek, llvm-commits, mgorny
Differential Revision: https://reviews.llvm.org/D28162
llvm-svn: 290717
2016-12-30 03:59:26 +08:00
|
|
|
~SimpleRefCounted() { --NumInstances; }
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
2012-02-01 03:58:34 +08:00
|
|
|
|
2020-12-04 07:28:40 +08:00
|
|
|
template <typename T> struct IntrusiveRefCntPtrTest : testing::Test {};
|
|
|
|
|
|
|
|
typedef ::testing::Types<SimpleRefCounted<RefCountedBase>,
|
|
|
|
SimpleRefCounted<ThreadSafeRefCountedBase>>
|
|
|
|
IntrusiveRefCntTypes;
|
|
|
|
TYPED_TEST_CASE(IntrusiveRefCntPtrTest, IntrusiveRefCntTypes);
|
|
|
|
|
|
|
|
TYPED_TEST(IntrusiveRefCntPtrTest, RefCountedBaseCopyDoesNotLeak) {
|
|
|
|
EXPECT_EQ(0, NumInstances);
|
[ADT] Delete RefCountedBaseVPTR.
Summary:
This class is unnecessary.
Its comment indicated that it was a compile error to allocate an
instance of a class that inherits from RefCountedBaseVPTR on the stack.
This may have been true at one point, but it's not today.
Moreover you really do not want to allocate *any* refcounted object on
the stack, vptrs or not, so if we did have a way to prevent these
objects from being stack-allocated, we'd want to apply it to regular
RefCountedBase too, obviating the need for a separate RefCountedBaseVPTR
class.
It seems that the main way RefCountedBaseVPTR provides safety is by
making its subclass's destructor virtual. This may have been helpful at
one point, but these days clang will emit an error if you define a class
with virtual functions that inherits from RefCountedBase but doesn't
have a virtual destructor.
Reviewers: compnerd, dblaikie
Subscribers: cfe-commits, klimek, llvm-commits, mgorny
Differential Revision: https://reviews.llvm.org/D28162
llvm-svn: 290717
2016-12-30 03:59:26 +08:00
|
|
|
{
|
2020-12-04 07:28:40 +08:00
|
|
|
TypeParam *S1 = new TypeParam;
|
|
|
|
IntrusiveRefCntPtr<TypeParam> R1 = S1;
|
|
|
|
TypeParam *S2 = new TypeParam(*S1);
|
|
|
|
IntrusiveRefCntPtr<TypeParam> R2 = S2;
|
|
|
|
EXPECT_EQ(2, NumInstances);
|
[ADT] Delete RefCountedBaseVPTR.
Summary:
This class is unnecessary.
Its comment indicated that it was a compile error to allocate an
instance of a class that inherits from RefCountedBaseVPTR on the stack.
This may have been true at one point, but it's not today.
Moreover you really do not want to allocate *any* refcounted object on
the stack, vptrs or not, so if we did have a way to prevent these
objects from being stack-allocated, we'd want to apply it to regular
RefCountedBase too, obviating the need for a separate RefCountedBaseVPTR
class.
It seems that the main way RefCountedBaseVPTR provides safety is by
making its subclass's destructor virtual. This may have been helpful at
one point, but these days clang will emit an error if you define a class
with virtual functions that inherits from RefCountedBase but doesn't
have a virtual destructor.
Reviewers: compnerd, dblaikie
Subscribers: cfe-commits, klimek, llvm-commits, mgorny
Differential Revision: https://reviews.llvm.org/D28162
llvm-svn: 290717
2016-12-30 03:59:26 +08:00
|
|
|
}
|
2020-12-04 07:28:40 +08:00
|
|
|
EXPECT_EQ(0, NumInstances);
|
2012-02-01 03:58:34 +08:00
|
|
|
}
|
|
|
|
|
2020-12-09 06:54:55 +08:00
|
|
|
TYPED_TEST(IntrusiveRefCntPtrTest, InteropsWithUniquePtr) {
|
|
|
|
EXPECT_EQ(0, NumInstances);
|
|
|
|
{
|
|
|
|
auto S1 = std::make_unique<TypeParam>();
|
|
|
|
IntrusiveRefCntPtr<TypeParam> R1 = std::move(S1);
|
|
|
|
EXPECT_EQ(1, NumInstances);
|
|
|
|
EXPECT_EQ(S1, nullptr);
|
|
|
|
}
|
|
|
|
EXPECT_EQ(0, NumInstances);
|
|
|
|
}
|
|
|
|
|
2021-01-12 04:12:53 +08:00
|
|
|
TYPED_TEST(IntrusiveRefCntPtrTest, MakeIntrusiveRefCnt) {
|
|
|
|
EXPECT_EQ(0, NumInstances);
|
|
|
|
{
|
|
|
|
auto S1 = makeIntrusiveRefCnt<TypeParam>();
|
|
|
|
auto S2 = makeIntrusiveRefCnt<const TypeParam>();
|
|
|
|
EXPECT_EQ(2, NumInstances);
|
|
|
|
static_assert(
|
|
|
|
std::is_same<decltype(S1), IntrusiveRefCntPtr<TypeParam>>::value,
|
|
|
|
"Non-const type mismatch");
|
|
|
|
static_assert(
|
|
|
|
std::is_same<decltype(S2), IntrusiveRefCntPtr<const TypeParam>>::value,
|
|
|
|
"Const type mismatch");
|
|
|
|
}
|
|
|
|
EXPECT_EQ(0, NumInstances);
|
|
|
|
}
|
|
|
|
|
2012-02-01 03:58:34 +08:00
|
|
|
struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> {
|
|
|
|
InterceptRefCounted(bool *Released, bool *Retained)
|
|
|
|
: Released(Released), Retained(Retained) {}
|
|
|
|
bool * const Released;
|
|
|
|
bool * const Retained;
|
|
|
|
};
|
|
|
|
template <> struct IntrusiveRefCntPtrInfo<InterceptRefCounted> {
|
|
|
|
static void retain(InterceptRefCounted *I) {
|
|
|
|
*I->Retained = true;
|
|
|
|
I->Retain();
|
|
|
|
}
|
|
|
|
static void release(InterceptRefCounted *I) {
|
|
|
|
*I->Released = true;
|
|
|
|
I->Release();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
TEST(IntrusiveRefCntPtr, UsesTraitsToRetainAndRelease) {
|
|
|
|
bool Released = false;
|
|
|
|
bool Retained = false;
|
|
|
|
{
|
|
|
|
InterceptRefCounted *I = new InterceptRefCounted(&Released, &Retained);
|
|
|
|
IntrusiveRefCntPtr<InterceptRefCounted> R = I;
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(Released);
|
|
|
|
EXPECT_TRUE(Retained);
|
|
|
|
}
|
|
|
|
|
2021-01-27 11:51:22 +08:00
|
|
|
// Test that the generic constructors use SFINAE to disable invalid
|
|
|
|
// conversions.
|
|
|
|
struct X : RefCountedBase<X> {};
|
|
|
|
struct Y : X {};
|
|
|
|
struct Z : RefCountedBase<Z> {};
|
|
|
|
static_assert(!std::is_convertible<IntrusiveRefCntPtr<X> &&,
|
|
|
|
IntrusiveRefCntPtr<Y>>::value,
|
|
|
|
"X&& -> Y should be rejected with SFINAE");
|
|
|
|
static_assert(!std::is_convertible<const IntrusiveRefCntPtr<X> &,
|
|
|
|
IntrusiveRefCntPtr<Y>>::value,
|
|
|
|
"const X& -> Y should be rejected with SFINAE");
|
|
|
|
static_assert(
|
|
|
|
!std::is_convertible<std::unique_ptr<X>, IntrusiveRefCntPtr<Y>>::value,
|
|
|
|
"X -> Y should be rejected with SFINAE");
|
|
|
|
static_assert(!std::is_convertible<IntrusiveRefCntPtr<X> &&,
|
|
|
|
IntrusiveRefCntPtr<Z>>::value,
|
|
|
|
"X&& -> Z should be rejected with SFINAE");
|
|
|
|
static_assert(!std::is_convertible<const IntrusiveRefCntPtr<X> &,
|
|
|
|
IntrusiveRefCntPtr<Z>>::value,
|
2021-01-29 07:14:46 +08:00
|
|
|
"const X& -> Z should be rejected with SFINAE");
|
2021-01-27 11:51:22 +08:00
|
|
|
static_assert(
|
|
|
|
!std::is_convertible<std::unique_ptr<X>, IntrusiveRefCntPtr<Z>>::value,
|
|
|
|
"X -> Z should be rejected with SFINAE");
|
|
|
|
|
|
|
|
TEST(IntrusiveRefCntPtr, InteropsWithConvertible) {
|
|
|
|
// Check converting constructors and operator=.
|
|
|
|
auto Y1 = makeIntrusiveRefCnt<Y>();
|
|
|
|
auto Y2 = makeIntrusiveRefCnt<Y>();
|
|
|
|
auto Y3 = makeIntrusiveRefCnt<Y>();
|
|
|
|
auto Y4 = makeIntrusiveRefCnt<Y>();
|
|
|
|
const void *P1 = Y1.get();
|
|
|
|
const void *P2 = Y2.get();
|
|
|
|
const void *P3 = Y3.get();
|
|
|
|
const void *P4 = Y4.get();
|
|
|
|
IntrusiveRefCntPtr<X> X1 = std::move(Y1);
|
|
|
|
IntrusiveRefCntPtr<X> X2 = Y2;
|
|
|
|
IntrusiveRefCntPtr<X> X3;
|
|
|
|
IntrusiveRefCntPtr<X> X4;
|
|
|
|
X3 = std::move(Y3);
|
|
|
|
X4 = Y4;
|
|
|
|
EXPECT_EQ(P1, X1.get());
|
|
|
|
EXPECT_EQ(P2, X2.get());
|
|
|
|
EXPECT_EQ(P3, X3.get());
|
|
|
|
EXPECT_EQ(P4, X4.get());
|
|
|
|
}
|
|
|
|
|
2012-02-01 03:58:34 +08:00
|
|
|
} // end namespace llvm
|