Skip to content

Commit 2e9b60a

Browse files
committed
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
1 parent ab41806 commit 2e9b60a

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

unittests/Bitcode/BitReaderTest.cpp

+39-31
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,66 @@
1010
#include "llvm/ADT/SmallString.h"
1111
#include "llvm/Bitcode/BitstreamWriter.h"
1212
#include "llvm/Bitcode/ReaderWriter.h"
13+
#include "llvm/AsmParser/Parser.h"
1314
#include "llvm/IR/Constants.h"
1415
#include "llvm/IR/Instructions.h"
1516
#include "llvm/IR/LLVMContext.h"
1617
#include "llvm/IR/Module.h"
1718
#include "llvm/IR/Verifier.h"
18-
#include "llvm/PassManager.h"
19+
#include "llvm/Support/Debug.h"
1920
#include "llvm/Support/MemoryBuffer.h"
21+
#include "llvm/Support/SourceMgr.h"
2022
#include "gtest/gtest.h"
2123

22-
namespace llvm {
23-
namespace {
24+
using namespace llvm;
2425

25-
static Module *makeLLVMModule() {
26-
Module* Mod = new Module("test-mem", getGlobalContext());
26+
namespace {
2727

28-
FunctionType* FuncTy =
29-
FunctionType::get(Type::getVoidTy(Mod->getContext()), false);
30-
Function* Func = Function::Create(FuncTy,GlobalValue::ExternalLinkage,
31-
"func", Mod);
28+
std::unique_ptr<Module> parseAssembly(const char *Assembly) {
29+
auto M = make_unique<Module>("Module", getGlobalContext());
3230

33-
BasicBlock* Entry = BasicBlock::Create(Mod->getContext(), "entry", Func);
34-
new UnreachableInst(Mod->getContext(), Entry);
31+
SMDiagnostic Error;
32+
bool Parsed =
33+
ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get();
3534

36-
BasicBlock* BB = BasicBlock::Create(Mod->getContext(), "bb", Func);
37-
new UnreachableInst(Mod->getContext(), BB);
35+
std::string ErrMsg;
36+
raw_string_ostream OS(ErrMsg);
37+
Error.print("", OS);
3838

39-
PointerType* Int8Ptr = Type::getInt8PtrTy(Mod->getContext());
40-
new GlobalVariable(*Mod, Int8Ptr, /*isConstant=*/true,
41-
GlobalValue::ExternalLinkage,
42-
BlockAddress::get(BB), "table");
39+
// A failure here means that the test itself is buggy.
40+
if (!Parsed)
41+
report_fatal_error(OS.str().c_str());
4342

44-
return Mod;
43+
return M;
4544
}
4645

47-
static void writeModuleToBuffer(SmallVectorImpl<char> &Buffer) {
48-
std::unique_ptr<Module> Mod(makeLLVMModule());
46+
static void writeModuleToBuffer(std::unique_ptr<Module> Mod,
47+
SmallVectorImpl<char> &Buffer) {
4948
raw_svector_ostream OS(Buffer);
5049
WriteBitcodeToFile(Mod.get(), OS);
5150
}
5251

53-
TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
54-
SmallString<1024> Mem;
55-
writeModuleToBuffer(Mem);
52+
static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
53+
SmallString<1024> &Mem,
54+
const char *Assembly) {
55+
writeModuleToBuffer(parseAssembly(Assembly), Mem);
5656
MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
57-
ErrorOr<Module *> ModuleOrErr =
58-
getLazyBitcodeModule(Buffer, getGlobalContext());
59-
std::unique_ptr<Module> m(ModuleOrErr.get());
60-
PassManager passes;
61-
passes.add(createVerifierPass());
62-
passes.add(createDebugInfoVerifierPass());
63-
passes.run(*m);
57+
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
58+
return std::unique_ptr<Module>(ModuleOrErr.get());
6459
}
6560

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()));
6673
}
67-
}
74+
75+
} // end namespace

unittests/Bitcode/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
set(LLVM_LINK_COMPONENTS
2-
BitReader
2+
IRReader
33
BitWriter
44
Core
55
Support

0 commit comments

Comments
 (0)