blob: 71e3168b686b4c1ae7a96f947511dea1c83b5acb [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 Carruth9fb823b2013-01-02 11:36:10 +000011#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"
Nick Lewycky1d9a8152010-02-15 22:09:09 +000019#include "gtest/gtest.h"
20
21namespace llvm {
22namespace {
23
24TEST(VerifierTest, Branch_i1) {
25 LLVMContext &C = getGlobalContext();
Rafael Espindola55fdcff2013-10-30 22:37:51 +000026 Module M("M", C);
Nick Lewycky1d9a8152010-02-15 22:09:09 +000027 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
Rafael Espindola55fdcff2013-10-30 22:37:51 +000028 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
29 BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
30 BasicBlock *Exit = BasicBlock::Create(C, "exit", F);
Nick Lewycky1d9a8152010-02-15 22:09:09 +000031 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
Chandler Carruth043949d2014-01-19 02:22:18 +000044 EXPECT_TRUE(verifyFunction(*F));
Nick Lewycky1d9a8152010-02-15 22:09:09 +000045}
46
Bill Wendlinge0d97df2013-06-19 19:26:44 +000047TEST(VerifierTest, InvalidRetAttribute) {
48 LLVMContext &C = getGlobalContext();
49 Module M("M", C);
50 FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
51 Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
52 AttributeSet AS = F->getAttributes();
53 F->setAttributes(AS.addAttribute(C, AttributeSet::ReturnIndex,
54 Attribute::UWTable));
55
56 std::string Error;
Chandler Carruth043949d2014-01-19 02:22:18 +000057 raw_string_ostream ErrorOS(Error);
58 EXPECT_TRUE(verifyModule(M, &ErrorOS));
59 EXPECT_TRUE(StringRef(ErrorOS.str()).startswith(
60 "Attribute 'uwtable' only applies to functions!"));
Bill Wendlinge0d97df2013-06-19 19:26:44 +000061}
62
Nick Lewycky1d9a8152010-02-15 22:09:09 +000063}
64}