llvm.org GIT mirror llvm / 2e9b60a
Rewrite BitReaderTest, NFC Rewrite the single unit test in `BitReaderTest` so that it's easier to add more tests. - Parse from an assembly string rather than using API. - Use more helper functions. - Use a separate context for the module on the other side. Aside from relying on the assembly parser, there's no functionality change intended. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214556 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 5 years ago
2 changed file(s) with 40 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
99 #include "llvm/ADT/SmallString.h"
1010 #include "llvm/Bitcode/BitstreamWriter.h"
1111 #include "llvm/Bitcode/ReaderWriter.h"
12 #include "llvm/AsmParser/Parser.h"
1213 #include "llvm/IR/Constants.h"
1314 #include "llvm/IR/Instructions.h"
1415 #include "llvm/IR/LLVMContext.h"
1516 #include "llvm/IR/Module.h"
1617 #include "llvm/IR/Verifier.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/Support/Debug.h"
1819 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/SourceMgr.h"
1921 #include "gtest/gtest.h"
2022
21 namespace llvm {
23 using namespace llvm;
24
2225 namespace {
2326
24 static Module *makeLLVMModule() {
25 Module* Mod = new Module("test-mem", getGlobalContext());
27 std::unique_ptr parseAssembly(const char *Assembly) {
28 auto M = make_unique("Module", getGlobalContext());
2629
27 FunctionType* FuncTy =
28 FunctionType::get(Type::getVoidTy(Mod->getContext()), false);
29 Function* Func = Function::Create(FuncTy,GlobalValue::ExternalLinkage,
30 "func", Mod);
30 SMDiagnostic Error;
31 bool Parsed =
32 ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get();
3133
32 BasicBlock* Entry = BasicBlock::Create(Mod->getContext(), "entry", Func);
33 new UnreachableInst(Mod->getContext(), Entry);
34 std::string ErrMsg;
35 raw_string_ostream OS(ErrMsg);
36 Error.print("", OS);
3437
35 BasicBlock* BB = BasicBlock::Create(Mod->getContext(), "bb", Func);
36 new UnreachableInst(Mod->getContext(), BB);
38 // A failure here means that the test itself is buggy.
39 if (!Parsed)
40 report_fatal_error(OS.str().c_str());
3741
38 PointerType* Int8Ptr = Type::getInt8PtrTy(Mod->getContext());
39 new GlobalVariable(*Mod, Int8Ptr, /*isConstant=*/true,
40 GlobalValue::ExternalLinkage,
41 BlockAddress::get(BB), "table");
42
43 return Mod;
42 return M;
4443 }
4544
46 static void writeModuleToBuffer(SmallVectorImpl &Buffer) {
47 std::unique_ptr Mod(makeLLVMModule());
45 static void writeModuleToBuffer(std::unique_ptr Mod,
46 SmallVectorImpl &Buffer) {
4847 raw_svector_ostream OS(Buffer);
4948 WriteBitcodeToFile(Mod.get(), OS);
5049 }
5150
51 static std::unique_ptr getLazyModuleFromAssembly(LLVMContext &Context,
52 SmallString<1024> &Mem,
53 const char *Assembly) {
54 writeModuleToBuffer(parseAssembly(Assembly), Mem);
55 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
56 ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
57 return std::unique_ptr(ModuleOrErr.get());
58 }
59
5260 TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
5361 SmallString<1024> Mem;
54 writeModuleToBuffer(Mem);
55 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
56 ErrorOr ModuleOrErr =
57 getLazyBitcodeModule(Buffer, getGlobalContext());
58 std::unique_ptr m(ModuleOrErr.get());
59 PassManager passes;
60 passes.add(createVerifierPass());
61 passes.add(createDebugInfoVerifierPass());
62 passes.run(*m);
62
63 LLVMContext Context;
64 std::unique_ptr M = getLazyModuleFromAssembly(
65 Context, Mem, "@table = constant i8* blockaddress(@func, %bb)\n"
66 "define void @func() {\n"
67 " unreachable\n"
68 "bb:\n"
69 " unreachable\n"
70 "}\n");
71 EXPECT_FALSE(verifyModule(*M, &dbgs()));
6372 }
6473
65 }
66 }
74 } // end namespace
0 set(LLVM_LINK_COMPONENTS
1 BitReader
1 IRReader
22 BitWriter
33 Core
44 Support