Chandler Carruth | 972cc0d | 2012-01-02 09:19:48 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===// |
Rafael Espindola | 47f79bb | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | fdc8f78 | 2012-02-29 20:30:56 +0000 | [diff] [blame^] | 10 | #include "llvm/ADT/SmallString.h" |
Rafael Espindola | 47f79bb | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 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 | |
| 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 | |
Daniel Dunbar | fdc8f78 | 2012-02-29 20:30:56 +0000 | [diff] [blame^] | 47 | static void writeModuleToBuffer(SmallVectorImpl<char> &Buffer) { |
Rafael Espindola | 47f79bb | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 48 | Module *Mod = makeLLVMModule(); |
Daniel Dunbar | fdc8f78 | 2012-02-29 20:30:56 +0000 | [diff] [blame^] | 49 | raw_svector_ostream OS(Buffer); |
| 50 | WriteBitcodeToFile(Mod, OS); |
Rafael Espindola | 47f79bb | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Chandler Carruth | 972cc0d | 2012-01-02 09:19:48 +0000 | [diff] [blame] | 53 | TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677 |
Daniel Dunbar | fdc8f78 | 2012-02-29 20:30:56 +0000 | [diff] [blame^] | 54 | SmallString<1024> Mem; |
Rafael Espindola | 47f79bb | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 55 | writeModuleToBuffer(Mem); |
Daniel Dunbar | fdc8f78 | 2012-02-29 20:30:56 +0000 | [diff] [blame^] | 56 | MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false); |
Rafael Espindola | 47f79bb | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 57 | std::string errMsg; |
| 58 | Module *m = getLazyBitcodeModule(Buffer, getGlobalContext(), &errMsg); |
| 59 | PassManager passes; |
| 60 | passes.add(createVerifierPass()); |
| 61 | passes.run(*m); |
| 62 | } |
Chandler Carruth | 972cc0d | 2012-01-02 09:19:48 +0000 | [diff] [blame] | 63 | |
Rafael Espindola | 47f79bb | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 64 | } |
| 65 | } |