1 //===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/Verifier.h"
11 #include "llvm/ADT/OwningPtr.h"
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"
20 #include "gtest/gtest.h"
25 TEST(VerifierTest, Branch_i1) {
26 LLVMContext &C = getGlobalContext();
28 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
29 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
30 BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
31 BasicBlock *Exit = BasicBlock::Create(C, "exit", F);
32 ReturnInst::Create(C, Exit);
34 // To avoid triggering an assertion in BranchInst::Create, we first create
35 // a branch with an 'i1' condition ...
37 Constant *False = ConstantInt::getFalse(C);
38 BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry);
40 // ... then use setOperand to redirect it to a value of different type.
42 Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0);
43 BI->setOperand(0, Zero32);
45 EXPECT_TRUE(verifyFunction(*F, ReturnStatusAction));
48 TEST(VerifierTest, AliasUnnamedAddr) {
49 LLVMContext &C = getGlobalContext();
51 Type *Ty = Type::getInt8Ty(C);
52 Constant *Init = Constant::getNullValue(Ty);
53 GlobalVariable *Aliasee = new GlobalVariable(M, Ty, true,
54 GlobalValue::ExternalLinkage,
56 GlobalAlias *GA = new GlobalAlias(Type::getInt8PtrTy(C),
57 GlobalValue::ExternalLinkage,
59 GA->setUnnamedAddr(true);
61 EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error));
62 EXPECT_TRUE(StringRef(Error).startswith("Alias cannot have unnamed_addr"));
65 TEST(VerifierTest, InvalidRetAttribute) {
66 LLVMContext &C = getGlobalContext();
68 FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
69 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
70 AttributeSet AS = F->getAttributes();
71 F->setAttributes(AS.addAttribute(C, AttributeSet::ReturnIndex,
75 EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error));
76 EXPECT_TRUE(StringRef(Error).
77 startswith("Attribute 'uwtable' only applies to functions!"));