diff --git a/unittests/IR/InstructionsTest.cpp b/unittests/IR/InstructionsTest.cpp index 39a7d77a107..e355d16d7e0 100644 --- a/unittests/IR/InstructionsTest.cpp +++ b/unittests/IR/InstructionsTest.cpp @@ -50,29 +50,58 @@ TEST(InstructionsTest, ReturnInst) { delete r1; } -TEST(InstructionsTest, CallInst) { - LLVMContext &C(getGlobalContext()); - std::unique_ptr M(new Module("MyModule", C)); +// Test fixture that provides a module and a single function within it. Useful +// for tests that need to refer to the function in some way. +class ModuleWithFunctionTest : public testing::Test { +protected: + ModuleWithFunctionTest() + : M(new Module("MyModule", Ctx)), + FArgTypes{Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx), + Type::getInt64Ty(Ctx)} { + FunctionType *FTy = + FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false); + F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); + } - Type *ArgTypes[] = {Type::getInt8Ty(C), Type::getInt32Ty(C), - Type::getInt64Ty(C)}; - FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), ArgTypes, false); - Function *F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); + LLVMContext Ctx; + std::unique_ptr M; + std::vector FArgTypes; + Function *F; +}; - Value *Args[] = {ConstantInt::get(Type::getInt8Ty(C), 20), - ConstantInt::get(Type::getInt32Ty(C), 9999), - ConstantInt::get(Type::getInt64Ty(C), 42)}; +TEST_F(ModuleWithFunctionTest, CallInst) { + Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20), + ConstantInt::get(Type::getInt32Ty(Ctx), 9999), + ConstantInt::get(Type::getInt64Ty(Ctx), 42)}; std::unique_ptr Call(CallInst::Create(F, Args)); // Make sure iteration over a call's arguments works as expected. unsigned Idx = 0; for (Value *Arg : Call->arg_operands()) { - EXPECT_EQ(ArgTypes[Idx], Arg->getType()); + EXPECT_EQ(FArgTypes[Idx], Arg->getType()); EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType()); Idx++; } } +TEST_F(ModuleWithFunctionTest, InvokeInst) { + BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F); + BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F); + + Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20), + ConstantInt::get(Type::getInt32Ty(Ctx), 9999), + ConstantInt::get(Type::getInt64Ty(Ctx), 42)}; + std::unique_ptr Invoke(InvokeInst::Create(F, BB1, BB2, Args)); + + // Make sure iteration over invoke's arguments works as expected. + unsigned Idx = 0; + for (Value *Arg : Invoke->arg_operands()) { + EXPECT_EQ(FArgTypes[Idx], Arg->getType()); + EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType()); + Idx++; + } +} + TEST(InstructionsTest, BranchInst) { LLVMContext &C(getGlobalContext());