Chandler Carruth | c779e96 | 2013-01-07 15:35:46 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests ------------===// |
Nick Lewycky | 6a7cb63 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chandler Carruth | 5a88dda | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 10 | #include "llvm/Analysis/Verifier.h" |
| 11 | #include "llvm/ADT/OwningPtr.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 12 | #include "llvm/IR/Constants.h" |
| 13 | #include "llvm/IR/DerivedTypes.h" |
| 14 | #include "llvm/IR/Function.h" |
| 15 | #include "llvm/IR/GlobalAlias.h" |
| 16 | #include "llvm/IR/GlobalVariable.h" |
| 17 | #include "llvm/IR/Instructions.h" |
| 18 | #include "llvm/IR/LLVMContext.h" |
| 19 | #include "llvm/IR/Module.h" |
Nick Lewycky | 6a7cb63 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | namespace { |
| 24 | |
| 25 | TEST(VerifierTest, Branch_i1) { |
| 26 | LLVMContext &C = getGlobalContext(); |
| 27 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); |
Jeffrey Yasskin | 645a86f | 2010-03-13 01:34:56 +0000 | [diff] [blame] | 28 | OwningPtr<Function> F(Function::Create(FTy, GlobalValue::ExternalLinkage)); |
| 29 | BasicBlock *Entry = BasicBlock::Create(C, "entry", F.get()); |
| 30 | BasicBlock *Exit = BasicBlock::Create(C, "exit", F.get()); |
Nick Lewycky | 6a7cb63 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 31 | ReturnInst::Create(C, Exit); |
| 32 | |
| 33 | // To avoid triggering an assertion in BranchInst::Create, we first create |
| 34 | // a branch with an 'i1' condition ... |
| 35 | |
| 36 | Constant *False = ConstantInt::getFalse(C); |
| 37 | BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry); |
| 38 | |
| 39 | // ... then use setOperand to redirect it to a value of different type. |
| 40 | |
| 41 | Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0); |
| 42 | BI->setOperand(0, Zero32); |
| 43 | |
| 44 | EXPECT_TRUE(verifyFunction(*F, ReturnStatusAction)); |
| 45 | } |
| 46 | |
Rafael Espindola | bea4626 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 47 | TEST(VerifierTest, AliasUnnamedAddr) { |
| 48 | LLVMContext &C = getGlobalContext(); |
| 49 | Module M("M", C); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 50 | Type *Ty = Type::getInt8Ty(C); |
Rafael Espindola | bea4626 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 51 | Constant *Init = Constant::getNullValue(Ty); |
| 52 | GlobalVariable *Aliasee = new GlobalVariable(M, Ty, true, |
| 53 | GlobalValue::ExternalLinkage, |
| 54 | Init, "foo"); |
| 55 | GlobalAlias *GA = new GlobalAlias(Type::getInt8PtrTy(C), |
| 56 | GlobalValue::ExternalLinkage, |
| 57 | "bar", Aliasee, &M); |
| 58 | GA->setUnnamedAddr(true); |
| 59 | std::string Error; |
| 60 | EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error)); |
| 61 | EXPECT_TRUE(StringRef(Error).startswith("Alias cannot have unnamed_addr")); |
| 62 | } |
Nick Lewycky | 6a7cb63 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 63 | } |
| 64 | } |