Update getLazyBitcodeModule to use ErrorOr for error handling.
[oota-llvm.git] / unittests / Bitcode / BitReaderTest.cpp
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/Bitcode/BitstreamWriter.h"
12 #include "llvm/Bitcode/ReaderWriter.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/Verifier.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "gtest/gtest.h"
21
22 namespace llvm {
23 namespace {
24
25 static 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
47 static void writeModuleToBuffer(SmallVectorImpl<char> &Buffer) {
48   OwningPtr<Module> Mod(makeLLVMModule());
49   raw_svector_ostream OS(Buffer);
50   WriteBitcodeToFile(Mod.get(), OS);
51 }
52
53 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
54   SmallString<1024> Mem;
55   writeModuleToBuffer(Mem);
56   MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
57   ErrorOr<Module *> ModuleOrErr =
58       getLazyBitcodeModule(Buffer, getGlobalContext());
59   OwningPtr<Module> m(ModuleOrErr.get());
60   PassManager passes;
61   passes.add(createVerifierPass());
62   passes.run(*m);
63 }
64
65 }
66 }