1//===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===//
2//
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/ADT/SmallString.h"
11#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
47static void writeModuleToBuffer(SmallVectorImpl<char> &Buffer) {
48  Module *Mod = makeLLVMModule();
49  raw_svector_ostream OS(Buffer);
50  WriteBitcodeToFile(Mod, OS);
51}
52
53TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
54  SmallString<1024> Mem;
55  writeModuleToBuffer(Mem);
56  MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "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}
63
64}
65}
66