forked from OSchip/llvm-project
Add tests for WeakVH and AssertingVH. These pointed out that the overloads for
the comparison operators were not only unnecessary in the presence of the implicit conversion; they caused ambiguous overload errors. So I deleted them. llvm-svn: 70243
This commit is contained in:
parent
4de47b445b
commit
41f2477a50
|
@ -39,7 +39,7 @@ public:
|
||||||
class ValueHandleBase {
|
class ValueHandleBase {
|
||||||
friend class Value;
|
friend class Value;
|
||||||
protected:
|
protected:
|
||||||
/// HandleBaseKind - This indicates what base class the handle actually is.
|
/// HandleBaseKind - This indicates what sub class the handle actually is.
|
||||||
/// This is to avoid having a vtable for the light-weight handle pointers. The
|
/// This is to avoid having a vtable for the light-weight handle pointers. The
|
||||||
/// fully generally Callback version does have a vtable.
|
/// fully generally Callback version does have a vtable.
|
||||||
enum HandleBaseKind {
|
enum HandleBaseKind {
|
||||||
|
@ -87,20 +87,7 @@ public:
|
||||||
|
|
||||||
Value *operator->() const { return getValPtr(); }
|
Value *operator->() const { return getValPtr(); }
|
||||||
Value &operator*() const { return *getValPtr(); }
|
Value &operator*() const { return *getValPtr(); }
|
||||||
|
|
||||||
bool operator==(const Value *RHS) const { return VP == RHS; }
|
|
||||||
bool operator==(const ValueHandleBase &RHS) const { return VP == RHS.VP; }
|
|
||||||
bool operator!=(const Value *RHS) const { return VP != RHS; }
|
|
||||||
bool operator!=(const ValueHandleBase &RHS) const { return VP != RHS.VP; }
|
|
||||||
bool operator<(const Value *RHS) const { return VP < RHS; }
|
|
||||||
bool operator<(const ValueHandleBase &RHS) const { return VP < RHS.VP; }
|
|
||||||
bool operator>(const Value *RHS) const { return VP > RHS; }
|
|
||||||
bool operator>(const ValueHandleBase &RHS) const { return VP > RHS.VP; }
|
|
||||||
bool operator<=(const Value *RHS) const { return VP <= RHS; }
|
|
||||||
bool operator<=(const ValueHandleBase &RHS) const { return VP <= RHS.VP; }
|
|
||||||
bool operator>=(const Value *RHS) const { return VP >= RHS; }
|
|
||||||
bool operator>=(const ValueHandleBase &RHS) const { return VP >= RHS.VP; }
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Value *getValPtr() const { return VP; }
|
Value *getValPtr() const { return VP; }
|
||||||
private:
|
private:
|
||||||
|
@ -198,33 +185,6 @@ public:
|
||||||
|
|
||||||
ValueTy *operator->() const { return getValPtr(); }
|
ValueTy *operator->() const { return getValPtr(); }
|
||||||
ValueTy &operator*() const { return *getValPtr(); }
|
ValueTy &operator*() const { return *getValPtr(); }
|
||||||
|
|
||||||
// Duplicate these from the base class so that they work when assertions are
|
|
||||||
// off.
|
|
||||||
bool operator==(const Value *RHS) const { return getValPtr() == RHS; }
|
|
||||||
bool operator!=(const Value *RHS) const { return getValPtr() != RHS; }
|
|
||||||
bool operator<(const Value *RHS) const { return getValPtr() < RHS; }
|
|
||||||
bool operator>(const Value *RHS) const { return getValPtr() > RHS; }
|
|
||||||
bool operator<=(const Value *RHS) const { return getValPtr() <= RHS; }
|
|
||||||
bool operator>=(const Value *RHS) const { return getValPtr() >= RHS; }
|
|
||||||
bool operator==(const AssertingVH &RHS) const {
|
|
||||||
return getValPtr() == RHS.getValPtr();
|
|
||||||
}
|
|
||||||
bool operator!=(const AssertingVH &RHS) const {
|
|
||||||
return getValPtr() != RHS.getValPtr();
|
|
||||||
}
|
|
||||||
bool operator<(const AssertingVH &RHS) const {
|
|
||||||
return getValPtr() < RHS.getValPtr();
|
|
||||||
}
|
|
||||||
bool operator>(const AssertingVH &RHS) const {
|
|
||||||
return getValPtr() > RHS.getValPtr();
|
|
||||||
}
|
|
||||||
bool operator<=(const AssertingVH &RHS) const {
|
|
||||||
return getValPtr() <= RHS.getValPtr();
|
|
||||||
}
|
|
||||||
bool operator>=(const AssertingVH &RHS) const {
|
|
||||||
return getValPtr() >= RHS.getValPtr();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // End llvm namespace
|
} // End llvm namespace
|
||||||
|
|
|
@ -0,0 +1,181 @@
|
||||||
|
//===- llvm/unittest/Support/ValueHandleTest.cpp - ValueHandle tests --------===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#include "llvm/Support/ValueHandle.h"
|
||||||
|
|
||||||
|
#include "llvm/Constants.h"
|
||||||
|
#include "llvm/Instructions.h"
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
using namespace llvm;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
class ValueHandle : public testing::Test {
|
||||||
|
protected:
|
||||||
|
Constant *ConstantV;
|
||||||
|
std::auto_ptr<BitCastInst> BitcastV;
|
||||||
|
|
||||||
|
ValueHandle() : ConstantV(ConstantInt::get(Type::Int32Ty, 0)),
|
||||||
|
BitcastV(new BitCastInst(ConstantV, Type::Int32Ty)) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, WeakVH_BasicOperation) {
|
||||||
|
WeakVH WVH(BitcastV.get());
|
||||||
|
EXPECT_EQ(BitcastV.get(), WVH);
|
||||||
|
WVH = ConstantV;
|
||||||
|
EXPECT_EQ(ConstantV, WVH);
|
||||||
|
|
||||||
|
// Make sure I can call a method on the underlying Value. It
|
||||||
|
// doesn't matter which method.
|
||||||
|
EXPECT_EQ(Type::Int32Ty, WVH->getType());
|
||||||
|
EXPECT_EQ(Type::Int32Ty, (*WVH).getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, WeakVH_Comparisons) {
|
||||||
|
WeakVH BitcastWVH(BitcastV.get());
|
||||||
|
WeakVH ConstantWVH(ConstantV);
|
||||||
|
|
||||||
|
EXPECT_TRUE(BitcastWVH == BitcastWVH);
|
||||||
|
EXPECT_TRUE(BitcastV.get() == BitcastWVH);
|
||||||
|
EXPECT_TRUE(BitcastWVH == BitcastV.get());
|
||||||
|
EXPECT_FALSE(BitcastWVH == ConstantWVH);
|
||||||
|
|
||||||
|
EXPECT_TRUE(BitcastWVH != ConstantWVH);
|
||||||
|
EXPECT_TRUE(BitcastV.get() != ConstantWVH);
|
||||||
|
EXPECT_TRUE(BitcastWVH != ConstantV);
|
||||||
|
EXPECT_FALSE(BitcastWVH != BitcastWVH);
|
||||||
|
|
||||||
|
// Cast to Value* so comparisons work.
|
||||||
|
Value *BV = BitcastV.get();
|
||||||
|
Value *CV = ConstantV;
|
||||||
|
EXPECT_EQ(BV < CV, BitcastWVH < ConstantWVH);
|
||||||
|
EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantWVH);
|
||||||
|
EXPECT_EQ(BV > CV, BitcastWVH > ConstantWVH);
|
||||||
|
EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantWVH);
|
||||||
|
|
||||||
|
EXPECT_EQ(BV < CV, BitcastV.get() < ConstantWVH);
|
||||||
|
EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantWVH);
|
||||||
|
EXPECT_EQ(BV > CV, BitcastV.get() > ConstantWVH);
|
||||||
|
EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantWVH);
|
||||||
|
|
||||||
|
EXPECT_EQ(BV < CV, BitcastWVH < ConstantV);
|
||||||
|
EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantV);
|
||||||
|
EXPECT_EQ(BV > CV, BitcastWVH > ConstantV);
|
||||||
|
EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantV);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, WeakVH_FollowsRAUW) {
|
||||||
|
WeakVH WVH(BitcastV.get());
|
||||||
|
WeakVH WVH_Copy(WVH);
|
||||||
|
WeakVH WVH_Recreated(BitcastV.get());
|
||||||
|
BitcastV->replaceAllUsesWith(ConstantV);
|
||||||
|
EXPECT_EQ(ConstantV, WVH);
|
||||||
|
EXPECT_EQ(ConstantV, WVH_Copy);
|
||||||
|
EXPECT_EQ(ConstantV, WVH_Recreated);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, WeakVH_NullOnDeletion) {
|
||||||
|
WeakVH WVH(BitcastV.get());
|
||||||
|
WeakVH WVH_Copy(WVH);
|
||||||
|
WeakVH WVH_Recreated(BitcastV.get());
|
||||||
|
BitcastV.reset();
|
||||||
|
Value *null_value = NULL;
|
||||||
|
EXPECT_EQ(null_value, WVH);
|
||||||
|
EXPECT_EQ(null_value, WVH_Copy);
|
||||||
|
EXPECT_EQ(null_value, WVH_Recreated);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, AssertingVH_BasicOperation) {
|
||||||
|
AssertingVH<CastInst> AVH(BitcastV.get());
|
||||||
|
CastInst *implicit_to_exact_type = AVH;
|
||||||
|
implicit_to_exact_type = implicit_to_exact_type; // Avoid warning.
|
||||||
|
|
||||||
|
AssertingVH<Value> GenericAVH(BitcastV.get());
|
||||||
|
EXPECT_EQ(BitcastV.get(), GenericAVH);
|
||||||
|
GenericAVH = ConstantV;
|
||||||
|
EXPECT_EQ(ConstantV, GenericAVH);
|
||||||
|
|
||||||
|
// Make sure I can call a method on the underlying CastInst. It
|
||||||
|
// doesn't matter which method.
|
||||||
|
EXPECT_FALSE(AVH->mayWriteToMemory());
|
||||||
|
EXPECT_FALSE((*AVH).mayWriteToMemory());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, AssertingVH_Comparisons) {
|
||||||
|
AssertingVH<Value> BitcastAVH(BitcastV.get());
|
||||||
|
AssertingVH<Value> ConstantAVH(ConstantV);
|
||||||
|
|
||||||
|
EXPECT_TRUE(BitcastAVH == BitcastAVH);
|
||||||
|
EXPECT_TRUE(BitcastV.get() == BitcastAVH);
|
||||||
|
EXPECT_TRUE(BitcastAVH == BitcastV.get());
|
||||||
|
EXPECT_FALSE(BitcastAVH == ConstantAVH);
|
||||||
|
|
||||||
|
EXPECT_TRUE(BitcastAVH != ConstantAVH);
|
||||||
|
EXPECT_TRUE(BitcastV.get() != ConstantAVH);
|
||||||
|
EXPECT_TRUE(BitcastAVH != ConstantV);
|
||||||
|
EXPECT_FALSE(BitcastAVH != BitcastAVH);
|
||||||
|
|
||||||
|
// Cast to Value* so comparisons work.
|
||||||
|
Value *BV = BitcastV.get();
|
||||||
|
Value *CV = ConstantV;
|
||||||
|
EXPECT_EQ(BV < CV, BitcastAVH < ConstantAVH);
|
||||||
|
EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantAVH);
|
||||||
|
EXPECT_EQ(BV > CV, BitcastAVH > ConstantAVH);
|
||||||
|
EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantAVH);
|
||||||
|
|
||||||
|
EXPECT_EQ(BV < CV, BitcastV.get() < ConstantAVH);
|
||||||
|
EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantAVH);
|
||||||
|
EXPECT_EQ(BV > CV, BitcastV.get() > ConstantAVH);
|
||||||
|
EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantAVH);
|
||||||
|
|
||||||
|
EXPECT_EQ(BV < CV, BitcastAVH < ConstantV);
|
||||||
|
EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantV);
|
||||||
|
EXPECT_EQ(BV > CV, BitcastAVH > ConstantV);
|
||||||
|
EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantV);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) {
|
||||||
|
AssertingVH<Value> AVH(BitcastV.get());
|
||||||
|
BitcastV->replaceAllUsesWith(ConstantV);
|
||||||
|
EXPECT_EQ(BitcastV.get(), AVH);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, AssertingVH_ReducesToPointer) {
|
||||||
|
EXPECT_EQ(sizeof(CastInst *), sizeof(AssertingVH<CastInst>));
|
||||||
|
}
|
||||||
|
|
||||||
|
#else // !NDEBUG
|
||||||
|
|
||||||
|
#ifdef GTEST_HAS_DEATH_TEST
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, AssertingVH_Asserts) {
|
||||||
|
AssertingVH<Value> AVH(BitcastV.get());
|
||||||
|
EXPECT_DEATH({BitcastV.reset();},
|
||||||
|
"An asserting value handle still pointed to this value!");
|
||||||
|
AssertingVH<Value> Copy(AVH);
|
||||||
|
AVH = NULL;
|
||||||
|
EXPECT_DEATH({BitcastV.reset();},
|
||||||
|
"An asserting value handle still pointed to this value!");
|
||||||
|
Copy = NULL;
|
||||||
|
BitcastV.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // GTEST_HAS_DEATH_TEST
|
||||||
|
|
||||||
|
#endif // NDEBUG
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue