blob: 144853d4d78b95c6919c1e1d838474cfbef60c4b [file] [log] [blame]
Chandler Carruthe5172732012-01-02 09:19:48 +00001//===- llvm/unittest/Bitcode/BitReaderTest.cpp - Tests for BitReader ------===//
Rafael Espindolab7993462012-01-02 07:49:53 +00002//
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
Daniel Dunbar5fa5ecf2012-02-29 20:30:56 +000010#include "llvm/ADT/SmallString.h"
Rafael Espindolab7993462012-01-02 07:49:53 +000011#include "llvm/Bitcode/BitstreamWriter.h"
12#include "llvm/Bitcode/ReaderWriter.h"
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000013#include "llvm/AsmParser/Parser.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/Constants.h"
15#include "llvm/IR/Instructions.h"
16#include "llvm/IR/LLVMContext.h"
17#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000018#include "llvm/IR/Verifier.h"
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000019#include "llvm/Support/Debug.h"
Rafael Espindolab7993462012-01-02 07:49:53 +000020#include "llvm/Support/MemoryBuffer.h"
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000021#include "llvm/Support/SourceMgr.h"
Rafael Espindolab7993462012-01-02 07:49:53 +000022#include "gtest/gtest.h"
23
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000024using namespace llvm;
25
Rafael Espindolab7993462012-01-02 07:49:53 +000026namespace {
27
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000028std::unique_ptr<Module> parseAssembly(const char *Assembly) {
29 auto M = make_unique<Module>("Module", getGlobalContext());
Rafael Espindolab7993462012-01-02 07:49:53 +000030
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000031 SMDiagnostic Error;
32 bool Parsed =
33 ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get();
Rafael Espindolab7993462012-01-02 07:49:53 +000034
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000035 std::string ErrMsg;
36 raw_string_ostream OS(ErrMsg);
37 Error.print("", OS);
Rafael Espindolab7993462012-01-02 07:49:53 +000038
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000039 // A failure here means that the test itself is buggy.
40 if (!Parsed)
41 report_fatal_error(OS.str().c_str());
Rafael Espindolab7993462012-01-02 07:49:53 +000042
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000043 return M;
Rafael Espindolab7993462012-01-02 07:49:53 +000044}
45
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000046static void writeModuleToBuffer(std::unique_ptr<Module> Mod,
47 SmallVectorImpl<char> &Buffer) {
Daniel Dunbar5fa5ecf2012-02-29 20:30:56 +000048 raw_svector_ostream OS(Buffer);
NAKAMURA Takumi508baf72013-01-23 08:33:13 +000049 WriteBitcodeToFile(Mod.get(), OS);
Rafael Espindolab7993462012-01-02 07:49:53 +000050}
51
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000052static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
53 SmallString<1024> &Mem,
54 const char *Assembly) {
55 writeModuleToBuffer(parseAssembly(Assembly), Mem);
Daniel Dunbar5fa5ecf2012-02-29 20:30:56 +000056 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000057 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
58 return std::unique_ptr<Module>(ModuleOrErr.get());
Rafael Espindolab7993462012-01-02 07:49:53 +000059}
Chandler Carruthe5172732012-01-02 09:19:48 +000060
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000061TEST(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()));
Rafael Espindolab7993462012-01-02 07:49:53 +000073}
Duncan P. N. Exon Smith7a2990c2014-08-01 21:01:04 +000074
75} // end namespace