Use std::unique_ptr to manage the DataStreamer in bitcode parsing.

We were already deleting it, this just makes it explicit.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239867 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2015-06-16 23:29:49 +00:00
parent 3fea1651f6
commit 4a4558e436
8 changed files with 55 additions and 48 deletions

View File

@ -41,7 +41,8 @@ namespace llvm {
/// Read the header of the specified stream and prepare for lazy
/// deserialization and streaming of function bodies.
ErrorOr<std::unique_ptr<Module>> getStreamedBitcodeModule(
StringRef Name, DataStreamer *Streamer, LLVMContext &Context,
StringRef Name, std::unique_ptr<DataStreamer> Streamer,
LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler = nullptr);
/// Read the header of the specified bitcode buffer and extract just the

View File

@ -17,6 +17,7 @@
#ifndef LLVM_SUPPORT_DATASTREAM_H
#define LLVM_SUPPORT_DATASTREAM_H
#include <memory>
#include <string>
namespace llvm {
@ -30,9 +31,8 @@ public:
virtual ~DataStreamer();
};
DataStreamer *getDataFileStreamer(const std::string &Filename,
std::string *Err);
std::unique_ptr<DataStreamer> getDataFileStreamer(const std::string &Filename,
std::string *Err);
}
#endif // LLVM_SUPPORT_DATASTREAM_H_

View File

@ -24,7 +24,7 @@ namespace llvm {
/// setKnownObjectSize methods which are not applicable to non-streamed objects.
class StreamingMemoryObject : public MemoryObject {
public:
StreamingMemoryObject(DataStreamer *streamer);
StreamingMemoryObject(std::unique_ptr<DataStreamer> Streamer);
uint64_t getExtent() const override;
uint64_t readBytes(uint8_t *Buf, uint64_t Size,
uint64_t Address) const override;

View File

@ -136,7 +136,7 @@ class BitcodeReader : public GVMaterializer {
std::unique_ptr<MemoryBuffer> Buffer;
std::unique_ptr<BitstreamReader> StreamFile;
BitstreamCursor Stream;
DataStreamer *Streamer;
bool IsStreamed;
uint64_t NextUnreadBit = 0;
bool SeenValueSymbolTable = false;
@ -223,7 +223,7 @@ public:
BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler);
BitcodeReader(DataStreamer *Streamer, LLVMContext &Context,
BitcodeReader(LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler);
~BitcodeReader() override { freeState(); }
@ -241,7 +241,8 @@ public:
/// \brief Main interface to parsing a bitcode buffer.
/// \returns true if an error occurred.
std::error_code parseBitcodeInto(Module *M,
std::error_code parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
Module *M,
bool ShouldLazyLoadMetadata = false);
/// \brief Cheap mechanism to just extract module triple
@ -368,9 +369,9 @@ private:
std::error_code parseMetadataAttachment(Function &F);
ErrorOr<std::string> parseModuleTriple();
std::error_code parseUseLists();
std::error_code initStream();
std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
std::error_code initStreamFromBuffer();
std::error_code initLazyStream();
std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
std::error_code findFunctionInStream(
Function *F,
DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
@ -426,14 +427,14 @@ BitcodeReader::BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler)
: Context(Context),
DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
Buffer(Buffer), Streamer(nullptr), ValueList(Context),
Buffer(Buffer), IsStreamed(false), ValueList(Context),
MDValueList(Context) {}
BitcodeReader::BitcodeReader(DataStreamer *Streamer, LLVMContext &Context,
BitcodeReader::BitcodeReader(LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler)
: Context(Context),
DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
Buffer(nullptr), Streamer(Streamer), ValueList(Context),
Buffer(nullptr), IsStreamed(true), ValueList(Context),
MDValueList(Context) {}
std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
@ -2778,7 +2779,7 @@ std::error_code BitcodeReader::parseModule(bool Resume,
// the bitcode. If the bitcode file is old, the symbol table will be
// at the end instead and will not have been seen yet. In this case,
// just finish the parse now.
if (Streamer && SeenValueSymbolTable) {
if (IsStreamed && SeenValueSymbolTable) {
NextUnreadBit = Stream.GetCurrentBitNo();
return std::error_code();
}
@ -3029,7 +3030,7 @@ std::error_code BitcodeReader::parseModule(bool Resume,
if (!isProto) {
Func->setIsMaterializable(true);
FunctionsWithBodies.push_back(Func);
if (Streamer)
if (IsStreamed)
DeferredFunctionInfo[Func] = 0;
}
break;
@ -3077,11 +3078,12 @@ std::error_code BitcodeReader::parseModule(bool Resume,
}
}
std::error_code BitcodeReader::parseBitcodeInto(Module *M,
bool ShouldLazyLoadMetadata) {
std::error_code
BitcodeReader::parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
Module *M, bool ShouldLazyLoadMetadata) {
TheModule = M;
if (std::error_code EC = initStream())
if (std::error_code EC = initStream(std::move(Streamer)))
return EC;
// Sniff for the signature.
@ -3154,7 +3156,7 @@ ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
}
ErrorOr<std::string> BitcodeReader::parseTriple() {
if (std::error_code EC = initStream())
if (std::error_code EC = initStream(nullptr))
return EC;
// Sniff for the signature.
@ -4399,7 +4401,7 @@ std::error_code BitcodeReader::materialize(GlobalValue *GV) {
assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
// If its position is recorded as 0, its body is somewhere in the stream
// but we haven't seen it yet.
if (DFII->second == 0 && Streamer)
if (DFII->second == 0 && IsStreamed)
if (std::error_code EC = findFunctionInStream(F, DFII))
return EC;
@ -4514,9 +4516,10 @@ std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
return IdentifiedStructTypes;
}
std::error_code BitcodeReader::initStream() {
std::error_code
BitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) {
if (Streamer)
return initLazyStream();
return initLazyStream(std::move(Streamer));
return initStreamFromBuffer();
}
@ -4539,10 +4542,12 @@ std::error_code BitcodeReader::initStreamFromBuffer() {
return std::error_code();
}
std::error_code BitcodeReader::initLazyStream() {
std::error_code
BitcodeReader::initLazyStream(std::unique_ptr<DataStreamer> Streamer) {
// Check and strip off the bitcode wrapper; BitstreamReader expects never to
// see it.
auto OwnedBytes = llvm::make_unique<StreamingMemoryObject>(Streamer);
auto OwnedBytes =
llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
StreamingMemoryObject &Bytes = *OwnedBytes;
StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
Stream.init(&*StreamFile);
@ -4617,7 +4622,8 @@ getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
};
// Delay parsing Metadata if ShouldLazyLoadMetadata is true.
if (std::error_code EC = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata))
if (std::error_code EC =
R->parseBitcodeInto(nullptr, M.get(), ShouldLazyLoadMetadata))
return cleanupOnError(EC);
if (!WillMaterializeAll)
@ -4636,14 +4642,13 @@ ErrorOr<std::unique_ptr<Module>> llvm::getLazyBitcodeModule(
DiagnosticHandler, ShouldLazyLoadMetadata);
}
ErrorOr<std::unique_ptr<Module>>
llvm::getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer,
LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler) {
ErrorOr<std::unique_ptr<Module>> llvm::getStreamedBitcodeModule(
StringRef Name, std::unique_ptr<DataStreamer> Streamer,
LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler) {
std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
BitcodeReader *R = new BitcodeReader(Streamer, Context, DiagnosticHandler);
BitcodeReader *R = new BitcodeReader(Context, DiagnosticHandler);
M->setMaterializer(R);
if (std::error_code EC = R->parseBitcodeInto(M.get()))
if (std::error_code EC = R->parseBitcodeInto(std::move(Streamer), M.get()))
return EC;
return std::move(M);
}

View File

@ -16,6 +16,7 @@
#include "llvm/Support/DataStream.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Program.h"
#include <string>
@ -73,16 +74,13 @@ public:
}
namespace llvm {
DataStreamer *getDataFileStreamer(const std::string &Filename,
std::string *StrError) {
DataFileStreamer *s = new DataFileStreamer();
std::unique_ptr<DataStreamer>
llvm::getDataFileStreamer(const std::string &Filename, std::string *StrError) {
std::unique_ptr<DataFileStreamer> s = make_unique<DataFileStreamer>();
if (std::error_code e = s->OpenFile(Filename)) {
*StrError = std::string("Could not open ") + Filename + ": " +
e.message() + "\n";
return nullptr;
}
return s;
}
return std::move(s);
}

View File

@ -123,9 +123,10 @@ MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
return new RawMemoryObject(Start, End);
}
StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
ObjectSize(0), EOFReached(false) {
BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
StreamingMemoryObject::StreamingMemoryObject(
std::unique_ptr<DataStreamer> Streamer)
: Bytes(kChunkSize), Streamer(std::move(Streamer)), BytesRead(0),
BytesSkipped(0), ObjectSize(0), EOFReached(false) {
BytesRead = this->Streamer->GetBytes(&Bytes[0], kChunkSize);
}
}

View File

@ -148,7 +148,8 @@ int main(int argc, char **argv) {
std::unique_ptr<Module> M;
// Use the bitcode streaming interface
DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
std::unique_ptr<DataStreamer> Streamer =
getDataFileStreamer(InputFilename, &ErrorMessage);
if (Streamer) {
std::string DisplayFilename;
if (InputFilename == "-")
@ -156,7 +157,7 @@ int main(int argc, char **argv) {
else
DisplayFilename = InputFilename;
ErrorOr<std::unique_ptr<Module>> MOrErr =
getStreamedBitcodeModule(DisplayFilename, Streamer, Context);
getStreamedBitcodeModule(DisplayFilename, std::move(Streamer), Context);
M = std::move(*MOrErr);
M->materializeAllPermanently();
} else {

View File

@ -7,6 +7,7 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/StreamingMemoryObject.h"
#include "gtest/gtest.h"
#include <string.h>
@ -23,14 +24,14 @@ class NullDataStreamer : public DataStreamer {
}
TEST(StreamingMemoryObject, Test) {
auto *DS = new NullDataStreamer();
StreamingMemoryObject O(DS);
auto DS = make_unique<NullDataStreamer>();
StreamingMemoryObject O(std::move(DS));
EXPECT_TRUE(O.isValidAddress(32 * 1024));
}
TEST(StreamingMemoryObject, TestSetKnownObjectSize) {
auto *DS = new NullDataStreamer();
StreamingMemoryObject O(DS);
auto DS = make_unique<NullDataStreamer>();
StreamingMemoryObject O(std::move(DS));
uint8_t Buf[32];
EXPECT_EQ((uint64_t) 16, O.readBytes(Buf, 16, 0));
O.setKnownObjectSize(24);