Give unit test its own LLVMContext so MDNodes aren't leaked even if we never call llvm_shutdown.

Found by valgrind.

llvm-svn: 194797
This commit is contained in:
Benjamin Kramer 2013-11-15 09:34:33 +00:00
parent 352d0e52f5
commit 63d39da992
1 changed files with 10 additions and 9 deletions

View File

@ -25,12 +25,12 @@ namespace {
class IRBuilderTest : public testing::Test { class IRBuilderTest : public testing::Test {
protected: protected:
virtual void SetUp() { virtual void SetUp() {
M.reset(new Module("MyModule", getGlobalContext())); M.reset(new Module("MyModule", Ctx));
FunctionType *FTy = FunctionType::get(Type::getVoidTy(getGlobalContext()), FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
/*isVarArg=*/false); /*isVarArg=*/false);
F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
BB = BasicBlock::Create(getGlobalContext(), "", F); BB = BasicBlock::Create(Ctx, "", F);
GV = new GlobalVariable(*M, Type::getFloatTy(getGlobalContext()), true, GV = new GlobalVariable(*M, Type::getFloatTy(Ctx), true,
GlobalValue::ExternalLinkage, 0); GlobalValue::ExternalLinkage, 0);
} }
@ -39,6 +39,7 @@ protected:
M.reset(); M.reset();
} }
LLVMContext Ctx;
OwningPtr<Module> M; OwningPtr<Module> M;
Function *F; Function *F;
BasicBlock *BB; BasicBlock *BB;
@ -78,8 +79,8 @@ TEST_F(IRBuilderTest, Lifetime) {
TEST_F(IRBuilderTest, CreateCondBr) { TEST_F(IRBuilderTest, CreateCondBr) {
IRBuilder<> Builder(BB); IRBuilder<> Builder(BB);
BasicBlock *TBB = BasicBlock::Create(getGlobalContext(), "", F); BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
BasicBlock *FBB = BasicBlock::Create(getGlobalContext(), "", F); BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB); BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
TerminatorInst *TI = BB->getTerminator(); TerminatorInst *TI = BB->getTerminator();
@ -89,7 +90,7 @@ TEST_F(IRBuilderTest, CreateCondBr) {
EXPECT_EQ(FBB, TI->getSuccessor(1)); EXPECT_EQ(FBB, TI->getSuccessor(1));
BI->eraseFromParent(); BI->eraseFromParent();
MDNode *Weights = MDBuilder(getGlobalContext()).createBranchWeights(42, 13); MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights); BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
TI = BB->getTerminator(); TI = BB->getTerminator();
EXPECT_EQ(BI, TI); EXPECT_EQ(BI, TI);
@ -109,12 +110,12 @@ TEST_F(IRBuilderTest, LandingPadName) {
TEST_F(IRBuilderTest, GetIntTy) { TEST_F(IRBuilderTest, GetIntTy) {
IRBuilder<> Builder(BB); IRBuilder<> Builder(BB);
IntegerType *Ty1 = Builder.getInt1Ty(); IntegerType *Ty1 = Builder.getInt1Ty();
EXPECT_EQ(Ty1, IntegerType::get(getGlobalContext(), 1)); EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
DataLayout* DL = new DataLayout(M.get()); DataLayout* DL = new DataLayout(M.get());
IntegerType *IntPtrTy = Builder.getIntPtrTy(DL); IntegerType *IntPtrTy = Builder.getIntPtrTy(DL);
unsigned IntPtrBitSize = DL->getPointerSizeInBits(0); unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
EXPECT_EQ(IntPtrTy, IntegerType::get(getGlobalContext(), IntPtrBitSize)); EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
delete DL; delete DL;
} }