1e517273eSChandler Carruth //===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===// 2e517273eSChandler Carruth // 3e517273eSChandler Carruth // The LLVM Compiler Infrastructure 4e517273eSChandler Carruth // 5e517273eSChandler Carruth // This file is distributed under the University of Illinois Open Source 6e517273eSChandler Carruth // License. See LICENSE.TXT for details. 7e517273eSChandler Carruth // 8e517273eSChandler Carruth //===----------------------------------------------------------------------===// 9e517273eSChandler Carruth 105fa5ecf8SDaniel Dunbar #include "llvm/ADT/SmallString.h" 11e517273eSChandler Carruth #include "llvm/Bitcode/BitstreamWriter.h" 12e517273eSChandler Carruth #include "llvm/Bitcode/ReaderWriter.h" 13*7a2990cfSDuncan P. N. Exon Smith #include "llvm/AsmParser/Parser.h" 149fb823bbSChandler Carruth #include "llvm/IR/Constants.h" 159fb823bbSChandler Carruth #include "llvm/IR/Instructions.h" 169fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h" 179fb823bbSChandler Carruth #include "llvm/IR/Module.h" 185ad5f15cSChandler Carruth #include "llvm/IR/Verifier.h" 19*7a2990cfSDuncan P. N. Exon Smith #include "llvm/Support/Debug.h" 20e517273eSChandler Carruth #include "llvm/Support/MemoryBuffer.h" 21*7a2990cfSDuncan P. N. Exon Smith #include "llvm/Support/SourceMgr.h" 22e517273eSChandler Carruth #include "gtest/gtest.h" 23e517273eSChandler Carruth 24*7a2990cfSDuncan P. N. Exon Smith using namespace llvm; 25*7a2990cfSDuncan P. N. Exon Smith 26e517273eSChandler Carruth namespace { 27e517273eSChandler Carruth 28*7a2990cfSDuncan P. N. Exon Smith std::unique_ptr<Module> parseAssembly(const char *Assembly) { 29*7a2990cfSDuncan P. N. Exon Smith auto M = make_unique<Module>("Module", getGlobalContext()); 30e517273eSChandler Carruth 31*7a2990cfSDuncan P. N. Exon Smith SMDiagnostic Error; 32*7a2990cfSDuncan P. N. Exon Smith bool Parsed = 33*7a2990cfSDuncan P. N. Exon Smith ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get(); 34e517273eSChandler Carruth 35*7a2990cfSDuncan P. N. Exon Smith std::string ErrMsg; 36*7a2990cfSDuncan P. N. Exon Smith raw_string_ostream OS(ErrMsg); 37*7a2990cfSDuncan P. N. Exon Smith Error.print("", OS); 38e517273eSChandler Carruth 39*7a2990cfSDuncan P. N. Exon Smith // A failure here means that the test itself is buggy. 40*7a2990cfSDuncan P. N. Exon Smith if (!Parsed) 41*7a2990cfSDuncan P. N. Exon Smith report_fatal_error(OS.str().c_str()); 42e517273eSChandler Carruth 43*7a2990cfSDuncan P. N. Exon Smith return M; 44e517273eSChandler Carruth } 45e517273eSChandler Carruth 46*7a2990cfSDuncan P. N. Exon Smith static void writeModuleToBuffer(std::unique_ptr<Module> Mod, 47*7a2990cfSDuncan P. N. Exon Smith SmallVectorImpl<char> &Buffer) { 485fa5ecf8SDaniel Dunbar raw_svector_ostream OS(Buffer); 49508baf79SNAKAMURA Takumi WriteBitcodeToFile(Mod.get(), OS); 50e517273eSChandler Carruth } 51e517273eSChandler Carruth 52*7a2990cfSDuncan P. N. Exon Smith static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context, 53*7a2990cfSDuncan P. N. Exon Smith SmallString<1024> &Mem, 54*7a2990cfSDuncan P. N. Exon Smith const char *Assembly) { 55*7a2990cfSDuncan P. N. Exon Smith writeModuleToBuffer(parseAssembly(Assembly), Mem); 565fa5ecf8SDaniel Dunbar MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false); 57*7a2990cfSDuncan P. N. Exon Smith ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context); 58*7a2990cfSDuncan P. N. Exon Smith return std::unique_ptr<Module>(ModuleOrErr.get()); 59e517273eSChandler Carruth } 60e517273eSChandler Carruth 61*7a2990cfSDuncan P. N. Exon Smith TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677 62*7a2990cfSDuncan P. N. Exon Smith SmallString<1024> Mem; 63*7a2990cfSDuncan P. N. Exon Smith 64*7a2990cfSDuncan P. N. Exon Smith LLVMContext Context; 65*7a2990cfSDuncan P. N. Exon Smith std::unique_ptr<Module> M = getLazyModuleFromAssembly( 66*7a2990cfSDuncan P. N. Exon Smith Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n" 67*7a2990cfSDuncan P. N. Exon Smith "define void @func() {\n" 68*7a2990cfSDuncan P. N. Exon Smith " unreachable\n" 69*7a2990cfSDuncan P. N. Exon Smith "bb:\n" 70*7a2990cfSDuncan P. N. Exon Smith " unreachable\n" 71*7a2990cfSDuncan P. N. Exon Smith "}\n"); 72*7a2990cfSDuncan P. N. Exon Smith EXPECT_FALSE(verifyModule(*M, &dbgs())); 73e517273eSChandler Carruth } 74*7a2990cfSDuncan P. N. Exon Smith 75*7a2990cfSDuncan P. N. Exon Smith } // end namespace 76