blob: a5ce5fc9d301026c139cab694239a8a88ac23b85 [file] [log] [blame]
Chandler Carruth74b6a772013-01-07 15:35:46 +00001//===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests ------------===//
Nick Lewycky1d9a8152010-02-15 22:09:09 +00002//
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 Carruth5ad5f152014-01-13 09:26:24 +000010#include "llvm/IR/Verifier.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000011#include "llvm/ADT/OwningPtr.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000012#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 Lewycky1d9a8152010-02-15 22:09:09 +000020#include "gtest/gtest.h"
21
22namespace llvm {
23namespace {
24
25TEST(VerifierTest, Branch_i1) {
26 LLVMContext &C = getGlobalContext();
Rafael Espindola55fdcff2013-10-30 22:37:51 +000027 Module M("M", C);
Nick Lewycky1d9a8152010-02-15 22:09:09 +000028 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
Rafael Espindola55fdcff2013-10-30 22:37:51 +000029 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
30 BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
31 BasicBlock *Exit = BasicBlock::Create(C, "exit", F);
Nick Lewycky1d9a8152010-02-15 22:09:09 +000032 ReturnInst::Create(C, Exit);
33
34 // To avoid triggering an assertion in BranchInst::Create, we first create
35 // a branch with an 'i1' condition ...
36
37 Constant *False = ConstantInt::getFalse(C);
38 BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry);
39
40 // ... then use setOperand to redirect it to a value of different type.
41
42 Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0);
43 BI->setOperand(0, Zero32);
44
Chandler Carruth043949d2014-01-19 02:22:18 +000045 EXPECT_TRUE(verifyFunction(*F));
Nick Lewycky1d9a8152010-02-15 22:09:09 +000046}
47
Rafael Espindola45e6c192011-01-08 16:42:36 +000048TEST(VerifierTest, AliasUnnamedAddr) {
49 LLVMContext &C = getGlobalContext();
50 Module M("M", C);
Chris Lattner229907c2011-07-18 04:54:35 +000051 Type *Ty = Type::getInt8Ty(C);
Rafael Espindola45e6c192011-01-08 16:42:36 +000052 Constant *Init = Constant::getNullValue(Ty);
53 GlobalVariable *Aliasee = new GlobalVariable(M, Ty, true,
54 GlobalValue::ExternalLinkage,
55 Init, "foo");
56 GlobalAlias *GA = new GlobalAlias(Type::getInt8PtrTy(C),
57 GlobalValue::ExternalLinkage,
58 "bar", Aliasee, &M);
59 GA->setUnnamedAddr(true);
60 std::string Error;
Chandler Carruth043949d2014-01-19 02:22:18 +000061 raw_string_ostream ErrorOS(Error);
62 EXPECT_TRUE(verifyModule(M, &ErrorOS));
63 EXPECT_TRUE(
64 StringRef(ErrorOS.str()).startswith("Alias cannot have unnamed_addr"));
Rafael Espindola45e6c192011-01-08 16:42:36 +000065}
Bill Wendlinge0d97df2013-06-19 19:26:44 +000066
67TEST(VerifierTest, InvalidRetAttribute) {
68 LLVMContext &C = getGlobalContext();
69 Module M("M", C);
70 FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
71 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
72 AttributeSet AS = F->getAttributes();
73 F->setAttributes(AS.addAttribute(C, AttributeSet::ReturnIndex,
74 Attribute::UWTable));
75
76 std::string Error;
Chandler Carruth043949d2014-01-19 02:22:18 +000077 raw_string_ostream ErrorOS(Error);
78 EXPECT_TRUE(verifyModule(M, &ErrorOS));
79 EXPECT_TRUE(StringRef(ErrorOS.str()).startswith(
80 "Attribute 'uwtable' only applies to functions!"));
Bill Wendlinge0d97df2013-06-19 19:26:44 +000081}
82
Nick Lewycky1d9a8152010-02-15 22:09:09 +000083}
84}