Modernize the getStreamedBitcodeModule interface a bit. NFC.

llvm-svn: 224499
This commit is contained in:
Rafael Espindola 2014-12-18 05:08:43 +00:00
parent f7df7221d1
commit 7d727b5f11
4 changed files with 29 additions and 33 deletions

View File

@ -61,9 +61,8 @@ public:
init(Start, End); init(Start, End);
} }
BitstreamReader(MemoryObject *bytes) : IgnoreBlockInfoNames(true) { BitstreamReader(std::unique_ptr<MemoryObject> BitcodeBytes)
BitcodeBytes.reset(bytes); : BitcodeBytes(std::move(BitcodeBytes)), IgnoreBlockInfoNames(true) {}
}
BitstreamReader(BitstreamReader &&Other) { BitstreamReader(BitstreamReader &&Other) {
*this = std::move(Other); *this = std::move(Other);

View File

@ -33,14 +33,11 @@ namespace llvm {
ErrorOr<Module *> getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, ErrorOr<Module *> getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
LLVMContext &Context); LLVMContext &Context);
/// getStreamedBitcodeModule - Read the header of the specified stream /// Read the header of the specified stream and prepare for lazy
/// and prepare for lazy deserialization and streaming of function bodies. /// deserialization and streaming of function bodies.
/// On error, this returns null, and fills in *ErrMsg with an error ErrorOr<std::unique_ptr<Module>>
/// description if ErrMsg is non-null. getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer,
Module *getStreamedBitcodeModule(const std::string &name, LLVMContext &Context);
DataStreamer *streamer,
LLVMContext &Context,
std::string *ErrMsg = nullptr);
/// Read the header of the specified bitcode buffer and extract just the /// Read the header of the specified bitcode buffer and extract just the
/// triple information. If successful, this returns a string. On error, this /// triple information. If successful, this returns a string. On error, this

View File

@ -3529,12 +3529,13 @@ std::error_code BitcodeReader::InitStreamFromBuffer() {
std::error_code BitcodeReader::InitLazyStream() { std::error_code BitcodeReader::InitLazyStream() {
// Check and strip off the bitcode wrapper; BitstreamReader expects never to // Check and strip off the bitcode wrapper; BitstreamReader expects never to
// see it. // see it.
StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer); auto OwnedBytes = make_unique<StreamingMemoryObject>(LazyStreamer);
StreamFile.reset(new BitstreamReader(Bytes)); StreamingMemoryObject &Bytes = *OwnedBytes;
StreamFile = make_unique<BitstreamReader>(std::move(OwnedBytes));
Stream.init(&*StreamFile); Stream.init(&*StreamFile);
unsigned char buf[16]; unsigned char buf[16];
if (Bytes->readBytes(buf, 16, 0) != 16) if (Bytes.readBytes(buf, 16, 0) != 16)
return Error(BitcodeError::InvalidBitcodeSignature); return Error(BitcodeError::InvalidBitcodeSignature);
if (!isBitcode(buf, buf + 16)) if (!isBitcode(buf, buf + 16))
@ -3544,8 +3545,8 @@ std::error_code BitcodeReader::InitLazyStream() {
const unsigned char *bitcodeStart = buf; const unsigned char *bitcodeStart = buf;
const unsigned char *bitcodeEnd = buf + 16; const unsigned char *bitcodeEnd = buf + 16;
SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false); SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
Bytes->dropLeadingBytes(bitcodeStart - buf); Bytes.dropLeadingBytes(bitcodeStart - buf);
Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart); Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
} }
return std::error_code(); return std::error_code();
} }
@ -3651,20 +3652,15 @@ llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false); return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false);
} }
Module *llvm::getStreamedBitcodeModule(const std::string &name, ErrorOr<std::unique_ptr<Module>>
DataStreamer *streamer, llvm::getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer,
LLVMContext &Context, LLVMContext &Context) {
std::string *ErrMsg) { std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
Module *M = new Module(name, Context); BitcodeReader *R = new BitcodeReader(Streamer, Context);
BitcodeReader *R = new BitcodeReader(streamer, Context);
M->setMaterializer(R); M->setMaterializer(R);
if (std::error_code EC = R->ParseBitcodeInto(M)) { if (std::error_code EC = R->ParseBitcodeInto(M.get()))
if (ErrMsg) return EC;
*ErrMsg = EC.message(); return std::move(M);
delete M; // Also deletes R.
return nullptr;
}
return M;
} }
ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBufferRef Buffer, ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBufferRef Buffer,

View File

@ -127,15 +127,19 @@ int main(int argc, char **argv) {
std::unique_ptr<Module> M; std::unique_ptr<Module> M;
// Use the bitcode streaming interface // Use the bitcode streaming interface
DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage); DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
if (streamer) { if (Streamer) {
std::string DisplayFilename; std::string DisplayFilename;
if (InputFilename == "-") if (InputFilename == "-")
DisplayFilename = "<stdin>"; DisplayFilename = "<stdin>";
else else
DisplayFilename = InputFilename; DisplayFilename = InputFilename;
M.reset(getStreamedBitcodeModule(DisplayFilename, streamer, Context, ErrorOr<std::unique_ptr<Module>> MOrErr =
&ErrorMessage)); getStreamedBitcodeModule(DisplayFilename, Streamer, Context);
if (std::error_code EC = MOrErr.getError())
ErrorMessage = EC.message();
else
M = std::move(*MOrErr);
if(M.get()) { if(M.get()) {
if (std::error_code EC = M->materializeAllPermanently()) { if (std::error_code EC = M->materializeAllPermanently()) {
ErrorMessage = EC.message(); ErrorMessage = EC.message();