blob: 68cfe2836a29c4b19444ad396ec0b684d6b2cd46 [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
Daniel Dunbarfdc8f782012-02-29 20:30:56 +000010#include "llvm/ADT/SmallString.h"
Rafael Espindola47f79bb2012-01-02 07:49:53 +000011#include "llvm/Analysis/Verifier.h"
12#include "llvm/Bitcode/BitstreamWriter.h"
13#include "llvm/Bitcode/ReaderWriter.h"
14#include "llvm/Constants.h"
15#include "llvm/Instructions.h"
16#include "llvm/LLVMContext.h"
17#include "llvm/Module.h"
18#include "llvm/PassManager.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "gtest/gtest.h"
21
22namespace llvm {
23namespace {
24
25static Module *makeLLVMModule() {
26 Module* Mod = new Module("test-mem", getGlobalContext());
27
28 FunctionType* FuncTy =
29 FunctionType::get(Type::getVoidTy(Mod->getContext()), false);
30 Function* Func = Function::Create(FuncTy,GlobalValue::ExternalLinkage,
31 "func", Mod);
32
33 BasicBlock* Entry = BasicBlock::Create(Mod->getContext(), "entry", Func);
34 new UnreachableInst(Mod->getContext(), Entry);
35
36 BasicBlock* BB = BasicBlock::Create(Mod->getContext(), "bb", Func);
37 new UnreachableInst(Mod->getContext(), BB);
38
39 PointerType* Int8Ptr = Type::getInt8PtrTy(Mod->getContext());
40 new GlobalVariable(*Mod, Int8Ptr, /*isConstant=*/true,
41 GlobalValue::ExternalLinkage,
42 BlockAddress::get(BB), "table");
43
44 return Mod;
45}
46
Daniel Dunbarfdc8f782012-02-29 20:30:56 +000047static void writeModuleToBuffer(SmallVectorImpl<char> &Buffer) {
Rafael Espindola47f79bb2012-01-02 07:49:53 +000048 Module *Mod = makeLLVMModule();
Daniel Dunbarfdc8f782012-02-29 20:30:56 +000049 raw_svector_ostream OS(Buffer);
50 WriteBitcodeToFile(Mod, OS);
Rafael Espindola47f79bb2012-01-02 07:49:53 +000051}
52
Chandler Carruth972cc0d2012-01-02 09:19:48 +000053TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
Daniel Dunbarfdc8f782012-02-29 20:30:56 +000054 SmallString<1024> Mem;
Rafael Espindola47f79bb2012-01-02 07:49:53 +000055 writeModuleToBuffer(Mem);
Daniel Dunbarfdc8f782012-02-29 20:30:56 +000056 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
Rafael Espindola47f79bb2012-01-02 07:49:53 +000057 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}