1 //===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/IR/Constants.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "gtest/gtest.h"
25 static Module *makeLLVMModule() {
26 Module* Mod = new Module("test-mem", getGlobalContext());
28 FunctionType* FuncTy =
29 FunctionType::get(Type::getVoidTy(Mod->getContext()), false);
30 Function* Func = Function::Create(FuncTy,GlobalValue::ExternalLinkage,
33 BasicBlock* Entry = BasicBlock::Create(Mod->getContext(), "entry", Func);
34 new UnreachableInst(Mod->getContext(), Entry);
36 BasicBlock* BB = BasicBlock::Create(Mod->getContext(), "bb", Func);
37 new UnreachableInst(Mod->getContext(), BB);
39 PointerType* Int8Ptr = Type::getInt8PtrTy(Mod->getContext());
40 new GlobalVariable(*Mod, Int8Ptr, /*isConstant=*/true,
41 GlobalValue::ExternalLinkage,
42 BlockAddress::get(BB), "table");
47 static void writeModuleToBuffer(SmallVectorImpl<char> &Buffer) {
48 OwningPtr<Module> Mod(makeLLVMModule());
49 raw_svector_ostream OS(Buffer);
50 WriteBitcodeToFile(Mod.get(), OS);
53 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
54 SmallString<1024> Mem;
55 writeModuleToBuffer(Mem);
56 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
58 OwningPtr<Module> m(getLazyBitcodeModule(Buffer, getGlobalContext(), &errMsg));
60 passes.add(createVerifierPass());