Replace OwningPtr<T> with std::unique_ptr<T>.
[oota-llvm.git] / unittests / IR / VerifierTest.cpp
1 //===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests ------------===//
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
10 #include "llvm/IR/Verifier.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/DerivedTypes.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/GlobalAlias.h"
15 #include "llvm/IR/GlobalVariable.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "gtest/gtest.h"
20
21 namespace llvm {
22 namespace {
23
24 TEST(VerifierTest, Branch_i1) {
25   LLVMContext &C = getGlobalContext();
26   Module M("M", C);
27   FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
28   Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
29   BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
30   BasicBlock *Exit = BasicBlock::Create(C, "exit", F);
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));
45 }
46
47 TEST(VerifierTest, AliasUnnamedAddr) {
48   LLVMContext &C = getGlobalContext();
49   Module M("M", C);
50   Type *Ty = Type::getInt8Ty(C);
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   raw_string_ostream ErrorOS(Error);
61   EXPECT_TRUE(verifyModule(M, &ErrorOS));
62   EXPECT_TRUE(
63       StringRef(ErrorOS.str()).startswith("Alias cannot have unnamed_addr"));
64 }
65
66 TEST(VerifierTest, InvalidRetAttribute) {
67   LLVMContext &C = getGlobalContext();
68   Module M("M", C);
69   FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
70   Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
71   AttributeSet AS = F->getAttributes();
72   F->setAttributes(AS.addAttribute(C, AttributeSet::ReturnIndex,
73                                    Attribute::UWTable));
74
75   std::string Error;
76   raw_string_ostream ErrorOS(Error);
77   EXPECT_TRUE(verifyModule(M, &ErrorOS));
78   EXPECT_TRUE(StringRef(ErrorOS.str()).startswith(
79       "Attribute 'uwtable' only applies to functions!"));
80 }
81
82 }
83 }