From 06e59e738c45840b039b5caa8a46d7d0066b89a6 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Sat, 13 Mar 2010 19:58:26 +0000 Subject: Teach this test not to leak. Also, clean up all the cast cruft. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@98446 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Transforms/Utils/Cloning.cpp | 125 +++++++++++++++++++++++---------- 1 file changed, 89 insertions(+), 36 deletions(-) (limited to 'unittests/Transforms') diff --git a/unittests/Transforms/Utils/Cloning.cpp b/unittests/Transforms/Utils/Cloning.cpp index 17047e7ca1..b65ac34dac 100644 --- a/unittests/Transforms/Utils/Cloning.cpp +++ b/unittests/Transforms/Utils/Cloning.cpp @@ -11,78 +11,131 @@ #include "llvm/Argument.h" #include "llvm/Instructions.h" #include "llvm/LLVMContext.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/STLExtras.h" using namespace llvm; -TEST(CloneInstruction, OverflowBits) { +class CloneInstruction : public ::testing::Test { +protected: + virtual void SetUp() { + V = NULL; + } + + template + T *clone(T *V1) { + Value *V2 = V1->clone(); + Orig.insert(V1); + Clones.insert(V2); + return cast(V2); + } + + void eraseClones() { + DeleteContainerPointers(Clones); + } + + virtual void TearDown() { + eraseClones(); + DeleteContainerPointers(Orig); + delete V; + } + + SmallPtrSet Orig; // Erase on exit + SmallPtrSet Clones; // Erase in eraseClones + LLVMContext context; - Value *V = new Argument(Type::getInt32Ty(context)); + Value *V; +}; + +TEST_F(CloneInstruction, OverflowBits) { + V = new Argument(Type::getInt32Ty(context)); BinaryOperator *Add = BinaryOperator::Create(Instruction::Add, V, V); BinaryOperator *Sub = BinaryOperator::Create(Instruction::Sub, V, V); BinaryOperator *Mul = BinaryOperator::Create(Instruction::Mul, V, V); - EXPECT_FALSE(cast(Add->clone())->hasNoUnsignedWrap()); - EXPECT_FALSE(cast(Add->clone())->hasNoSignedWrap()); - EXPECT_FALSE(cast(Sub->clone())->hasNoUnsignedWrap()); - EXPECT_FALSE(cast(Sub->clone())->hasNoSignedWrap()); - EXPECT_FALSE(cast(Mul->clone())->hasNoUnsignedWrap()); - EXPECT_FALSE(cast(Mul->clone())->hasNoSignedWrap()); + BinaryOperator *AddClone = this->clone(Add); + BinaryOperator *SubClone = this->clone(Sub); + BinaryOperator *MulClone = this->clone(Mul); + + EXPECT_FALSE(AddClone->hasNoUnsignedWrap()); + EXPECT_FALSE(AddClone->hasNoSignedWrap()); + EXPECT_FALSE(SubClone->hasNoUnsignedWrap()); + EXPECT_FALSE(SubClone->hasNoSignedWrap()); + EXPECT_FALSE(MulClone->hasNoUnsignedWrap()); + EXPECT_FALSE(MulClone->hasNoSignedWrap()); + + eraseClones(); Add->setHasNoUnsignedWrap(); Sub->setHasNoUnsignedWrap(); Mul->setHasNoUnsignedWrap(); - EXPECT_TRUE(cast(Add->clone())->hasNoUnsignedWrap()); - EXPECT_FALSE(cast(Add->clone())->hasNoSignedWrap()); - EXPECT_TRUE(cast(Sub->clone())->hasNoUnsignedWrap()); - EXPECT_FALSE(cast(Sub->clone())->hasNoSignedWrap()); - EXPECT_TRUE(cast(Mul->clone())->hasNoUnsignedWrap()); - EXPECT_FALSE(cast(Mul->clone())->hasNoSignedWrap()); + AddClone = this->clone(Add); + SubClone = this->clone(Sub); + MulClone = this->clone(Mul); + + EXPECT_TRUE(AddClone->hasNoUnsignedWrap()); + EXPECT_FALSE(AddClone->hasNoSignedWrap()); + EXPECT_TRUE(SubClone->hasNoUnsignedWrap()); + EXPECT_FALSE(SubClone->hasNoSignedWrap()); + EXPECT_TRUE(MulClone->hasNoUnsignedWrap()); + EXPECT_FALSE(MulClone->hasNoSignedWrap()); + + eraseClones(); Add->setHasNoSignedWrap(); Sub->setHasNoSignedWrap(); Mul->setHasNoSignedWrap(); - EXPECT_TRUE(cast(Add->clone())->hasNoUnsignedWrap()); - EXPECT_TRUE(cast(Add->clone())->hasNoSignedWrap()); - EXPECT_TRUE(cast(Sub->clone())->hasNoUnsignedWrap()); - EXPECT_TRUE(cast(Sub->clone())->hasNoSignedWrap()); - EXPECT_TRUE(cast(Mul->clone())->hasNoUnsignedWrap()); - EXPECT_TRUE(cast(Mul->clone())->hasNoSignedWrap()); + AddClone = this->clone(Add); + SubClone = this->clone(Sub); + MulClone = this->clone(Mul); + + EXPECT_TRUE(AddClone->hasNoUnsignedWrap()); + EXPECT_TRUE(AddClone->hasNoSignedWrap()); + EXPECT_TRUE(SubClone->hasNoUnsignedWrap()); + EXPECT_TRUE(SubClone->hasNoSignedWrap()); + EXPECT_TRUE(MulClone->hasNoUnsignedWrap()); + EXPECT_TRUE(MulClone->hasNoSignedWrap()); + + eraseClones(); Add->setHasNoUnsignedWrap(false); Sub->setHasNoUnsignedWrap(false); Mul->setHasNoUnsignedWrap(false); - EXPECT_FALSE(cast(Add->clone())->hasNoUnsignedWrap()); - EXPECT_TRUE(cast(Add->clone())->hasNoSignedWrap()); - EXPECT_FALSE(cast(Sub->clone())->hasNoUnsignedWrap()); - EXPECT_TRUE(cast(Sub->clone())->hasNoSignedWrap()); - EXPECT_FALSE(cast(Mul->clone())->hasNoUnsignedWrap()); - EXPECT_TRUE(cast(Mul->clone())->hasNoSignedWrap()); + AddClone = this->clone(Add); + SubClone = this->clone(Sub); + MulClone = this->clone(Mul); + + EXPECT_FALSE(AddClone->hasNoUnsignedWrap()); + EXPECT_TRUE(AddClone->hasNoSignedWrap()); + EXPECT_FALSE(SubClone->hasNoUnsignedWrap()); + EXPECT_TRUE(SubClone->hasNoSignedWrap()); + EXPECT_FALSE(MulClone->hasNoUnsignedWrap()); + EXPECT_TRUE(MulClone->hasNoSignedWrap()); } -TEST(CloneInstruction, Inbounds) { - LLVMContext context; - Value *V = new Argument(Type::getInt32PtrTy(context)); +TEST_F(CloneInstruction, Inbounds) { + V = new Argument(Type::getInt32PtrTy(context)); + Constant *Z = Constant::getNullValue(Type::getInt32Ty(context)); std::vector ops; ops.push_back(Z); GetElementPtrInst *GEP = GetElementPtrInst::Create(V, ops.begin(), ops.end()); - EXPECT_FALSE(cast(GEP->clone())->isInBounds()); + EXPECT_FALSE(this->clone(GEP)->isInBounds()); GEP->setIsInBounds(); - EXPECT_TRUE(cast(GEP->clone())->isInBounds()); + EXPECT_TRUE(this->clone(GEP)->isInBounds()); } -TEST(CloneInstruction, Exact) { - LLVMContext context; - Value *V = new Argument(Type::getInt32Ty(context)); +TEST_F(CloneInstruction, Exact) { + V = new Argument(Type::getInt32Ty(context)); BinaryOperator *SDiv = BinaryOperator::Create(Instruction::SDiv, V, V); - EXPECT_FALSE(cast(SDiv->clone())->isExact()); + EXPECT_FALSE(this->clone(SDiv)->isExact()); SDiv->setIsExact(true); - EXPECT_TRUE(cast(SDiv->clone())->isExact()); + EXPECT_TRUE(this->clone(SDiv)->isExact()); } -- cgit v1.2.3