1 //===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/Bitcode/BitstreamWriter.h"
13 #include "llvm/Bitcode/ReaderWriter.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"
28 std::unique_ptr
<Module
> parseAssembly(const char *Assembly
) {
30 std::unique_ptr
<Module
> M
=
31 parseAssemblyString(Assembly
, Error
, getGlobalContext());
34 raw_string_ostream
OS(ErrMsg
);
37 // A failure here means that the test itself is buggy.
39 report_fatal_error(OS
.str().c_str());
44 static void writeModuleToBuffer(std::unique_ptr
<Module
> Mod
,
45 SmallVectorImpl
<char> &Buffer
) {
46 raw_svector_ostream
OS(Buffer
);
47 WriteBitcodeToFile(Mod
.get(), OS
);
50 static std::unique_ptr
<Module
> getLazyModuleFromAssembly(LLVMContext
&Context
,
51 SmallString
<1024> &Mem
,
52 const char *Assembly
) {
53 writeModuleToBuffer(parseAssembly(Assembly
), Mem
);
54 std::unique_ptr
<MemoryBuffer
> Buffer
=
55 MemoryBuffer::getMemBuffer(Mem
.str(), "test", false);
56 ErrorOr
<Module
*> ModuleOrErr
=
57 getLazyBitcodeModule(std::move(Buffer
), Context
);
58 return std::unique_ptr
<Module
>(ModuleOrErr
.get());
61 TEST(BitReaderTest
, DematerializeFunctionPreservesLinkageType
) {
62 SmallString
<1024> Mem
;
65 std::unique_ptr
<Module
> M
= getLazyModuleFromAssembly(
66 Context
, Mem
, "define internal i32 @func() {\n"
70 EXPECT_FALSE(verifyModule(*M
, &dbgs()));
72 M
->getFunction("func")->materialize();
73 EXPECT_FALSE(M
->getFunction("func")->empty());
74 EXPECT_TRUE(M
->getFunction("func")->getLinkage() ==
75 GlobalValue::InternalLinkage
);
77 // Check that the linkage type is preserved after dematerialization.
78 M
->getFunction("func")->Dematerialize();
79 EXPECT_TRUE(M
->getFunction("func")->empty());
80 EXPECT_TRUE(M
->getFunction("func")->getLinkage() ==
81 GlobalValue::InternalLinkage
);
82 EXPECT_FALSE(verifyModule(*M
, &dbgs()));
85 TEST(BitReaderTest
, MaterializeFunctionsForBlockAddr
) { // PR11677
86 SmallString
<1024> Mem
;
89 std::unique_ptr
<Module
> M
= getLazyModuleFromAssembly(
90 Context
, Mem
, "@table = constant i8* blockaddress(@func, %bb)\n"
91 "define void @func() {\n"
96 EXPECT_FALSE(verifyModule(*M
, &dbgs()));
98 // Try (and fail) to dematerialize @func.
99 M
->getFunction("func")->Dematerialize();
100 EXPECT_FALSE(M
->getFunction("func")->empty());
103 TEST(BitReaderTest
, MaterializeFunctionsForBlockAddrInFunctionBefore
) {
104 SmallString
<1024> Mem
;
107 std::unique_ptr
<Module
> M
= getLazyModuleFromAssembly(
108 Context
, Mem
, "define i8* @before() {\n"
109 " ret i8* blockaddress(@func, %bb)\n"
111 "define void @other() {\n"
114 "define void @func() {\n"
119 EXPECT_TRUE(M
->getFunction("before")->empty());
120 EXPECT_TRUE(M
->getFunction("func")->empty());
121 EXPECT_FALSE(verifyModule(*M
, &dbgs()));
123 // Materialize @before, pulling in @func.
124 EXPECT_FALSE(M
->getFunction("before")->materialize());
125 EXPECT_FALSE(M
->getFunction("func")->empty());
126 EXPECT_TRUE(M
->getFunction("other")->empty());
127 EXPECT_FALSE(verifyModule(*M
, &dbgs()));
129 // Try (and fail) to dematerialize @func.
130 M
->getFunction("func")->Dematerialize();
131 EXPECT_FALSE(M
->getFunction("func")->empty());
132 EXPECT_FALSE(verifyModule(*M
, &dbgs()));
135 TEST(BitReaderTest
, MaterializeFunctionsForBlockAddrInFunctionAfter
) {
136 SmallString
<1024> Mem
;
139 std::unique_ptr
<Module
> M
= getLazyModuleFromAssembly(
140 Context
, Mem
, "define void @func() {\n"
145 "define void @other() {\n"
148 "define i8* @after() {\n"
149 " ret i8* blockaddress(@func, %bb)\n"
151 EXPECT_TRUE(M
->getFunction("after")->empty());
152 EXPECT_TRUE(M
->getFunction("func")->empty());
153 EXPECT_FALSE(verifyModule(*M
, &dbgs()));
155 // Materialize @after, pulling in @func.
156 EXPECT_FALSE(M
->getFunction("after")->materialize());
157 EXPECT_FALSE(M
->getFunction("func")->empty());
158 EXPECT_TRUE(M
->getFunction("other")->empty());
159 EXPECT_FALSE(verifyModule(*M
, &dbgs()));
161 // Try (and fail) to dematerialize @func.
162 M
->getFunction("func")->Dematerialize();
163 EXPECT_FALSE(M
->getFunction("func")->empty());
164 EXPECT_FALSE(verifyModule(*M
, &dbgs()));