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/AsmParser/Parser.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/IR/Verifier.h" 19 #include "llvm/Support/Debug.h" 20 #include "llvm/Support/MemoryBuffer.h" 21 #include "llvm/Support/SourceMgr.h" 22 #include "gtest/gtest.h" 23 24 using namespace llvm; 25 26 namespace { 27 28 std::unique_ptr<Module> parseAssembly(const char *Assembly) { 29 auto M = make_unique<Module>("Module", getGlobalContext()); 30 31 SMDiagnostic Error; 32 bool Parsed = 33 ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get(); 34 35 std::string ErrMsg; 36 raw_string_ostream OS(ErrMsg); 37 Error.print("", OS); 38 39 // A failure here means that the test itself is buggy. 40 if (!Parsed) 41 report_fatal_error(OS.str().c_str()); 42 43 return M; 44 } 45 46 static void writeModuleToBuffer(std::unique_ptr<Module> Mod, 47 SmallVectorImpl<char> &Buffer) { 48 raw_svector_ostream OS(Buffer); 49 WriteBitcodeToFile(Mod.get(), OS); 50 } 51 52 static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context, 53 SmallString<1024> &Mem, 54 const char *Assembly) { 55 writeModuleToBuffer(parseAssembly(Assembly), Mem); 56 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false); 57 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context); 58 return std::unique_ptr<Module>(ModuleOrErr.get()); 59 } 60 61 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677 62 SmallString<1024> Mem; 63 64 LLVMContext Context; 65 std::unique_ptr<Module> M = getLazyModuleFromAssembly( 66 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n" 67 "define void @func() {\n" 68 " unreachable\n" 69 "bb:\n" 70 " unreachable\n" 71 "}\n"); 72 EXPECT_FALSE(verifyModule(*M, &dbgs())); 73 } 74 75 } // end namespace 76