blob: 91e6c15155807ae3208fc92923ea0e6ecf392f80 [file] [log] [blame]
Chandler Carruth972cc0d2012-01-02 09:19:48 +00001//===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===//
Rafael Espindola47f79bb2012-01-02 07:49:53 +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
10#include "llvm/Analysis/Verifier.h"
11#include "llvm/Bitcode/BitstreamWriter.h"
12#include "llvm/Bitcode/ReaderWriter.h"
13#include "llvm/Constants.h"
14#include "llvm/Instructions.h"
15#include "llvm/LLVMContext.h"
16#include "llvm/Module.h"
17#include "llvm/PassManager.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "gtest/gtest.h"
20
21namespace llvm {
22namespace {
23
24static Module *makeLLVMModule() {
25 Module* Mod = new Module("test-mem", getGlobalContext());
26
27 FunctionType* FuncTy =
28 FunctionType::get(Type::getVoidTy(Mod->getContext()), false);
29 Function* Func = Function::Create(FuncTy,GlobalValue::ExternalLinkage,
30 "func", Mod);
31
32 BasicBlock* Entry = BasicBlock::Create(Mod->getContext(), "entry", Func);
33 new UnreachableInst(Mod->getContext(), Entry);
34
35 BasicBlock* BB = BasicBlock::Create(Mod->getContext(), "bb", Func);
36 new UnreachableInst(Mod->getContext(), BB);
37
38 PointerType* Int8Ptr = Type::getInt8PtrTy(Mod->getContext());
39 new GlobalVariable(*Mod, Int8Ptr, /*isConstant=*/true,
40 GlobalValue::ExternalLinkage,
41 BlockAddress::get(BB), "table");
42
43 return Mod;
44}
45
46static void writeModuleToBuffer(std::vector<unsigned char> &Buffer) {
47 Module *Mod = makeLLVMModule();
48 BitstreamWriter Stream(Buffer);
49 WriteBitcodeToStream(Mod, Stream);
50}
51
Chandler Carruth972cc0d2012-01-02 09:19:48 +000052TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
Rafael Espindola47f79bb2012-01-02 07:49:53 +000053 std::vector<unsigned char> Mem;
54 writeModuleToBuffer(Mem);
55 StringRef Data((const char*)&Mem[0], Mem.size());
56 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Data, "test", false);
57 std::string errMsg;
58 Module *m = getLazyBitcodeModule(Buffer, getGlobalContext(), &errMsg);
59 PassManager passes;
60 passes.add(createVerifierPass());
61 passes.run(*m);
62}
Chandler Carruth972cc0d2012-01-02 09:19:48 +000063
Rafael Espindola47f79bb2012-01-02 07:49:53 +000064}
65}