From 4a4558e436353fa1fc4cd4ddff7491e631d71357 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 16 Jun 2015 23:29:49 +0000 Subject: [PATCH] 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 --- include/llvm/Bitcode/ReaderWriter.h | 3 +- include/llvm/Support/DataStream.h | 6 +-- include/llvm/Support/StreamingMemoryObject.h | 2 +- lib/Bitcode/Reader/BitcodeReader.cpp | 57 +++++++++++--------- lib/Support/DataStream.cpp | 12 ++--- lib/Support/StreamingMemoryObject.cpp | 9 ++-- tools/llvm-dis/llvm-dis.cpp | 5 +- unittests/Support/StreamingMemoryObject.cpp | 9 ++-- 8 files changed, 55 insertions(+), 48 deletions(-) diff --git a/include/llvm/Bitcode/ReaderWriter.h b/include/llvm/Bitcode/ReaderWriter.h index b3b944515c7..81103b82582 100644 --- a/include/llvm/Bitcode/ReaderWriter.h +++ b/include/llvm/Bitcode/ReaderWriter.h @@ -41,7 +41,8 @@ namespace llvm { /// Read the header of the specified stream and prepare for lazy /// deserialization and streaming of function bodies. ErrorOr> getStreamedBitcodeModule( - StringRef Name, DataStreamer *Streamer, LLVMContext &Context, + StringRef Name, std::unique_ptr Streamer, + LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler = nullptr); /// Read the header of the specified bitcode buffer and extract just the diff --git a/include/llvm/Support/DataStream.h b/include/llvm/Support/DataStream.h index 8bc41336030..a544316f430 100644 --- a/include/llvm/Support/DataStream.h +++ b/include/llvm/Support/DataStream.h @@ -17,6 +17,7 @@ #ifndef LLVM_SUPPORT_DATASTREAM_H #define LLVM_SUPPORT_DATASTREAM_H +#include #include namespace llvm { @@ -30,9 +31,8 @@ public: virtual ~DataStreamer(); }; -DataStreamer *getDataFileStreamer(const std::string &Filename, - std::string *Err); - +std::unique_ptr getDataFileStreamer(const std::string &Filename, + std::string *Err); } #endif // LLVM_SUPPORT_DATASTREAM_H_ diff --git a/include/llvm/Support/StreamingMemoryObject.h b/include/llvm/Support/StreamingMemoryObject.h index 932e635cd07..7cb6438d134 100644 --- a/include/llvm/Support/StreamingMemoryObject.h +++ b/include/llvm/Support/StreamingMemoryObject.h @@ -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 Streamer); uint64_t getExtent() const override; uint64_t readBytes(uint8_t *Buf, uint64_t Size, uint64_t Address) const override; diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index d012da61c4d..8a88de4622f 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -136,7 +136,7 @@ class BitcodeReader : public GVMaterializer { std::unique_ptr Buffer; std::unique_ptr 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 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 parseModuleTriple(); std::error_code parseUseLists(); - std::error_code initStream(); + std::error_code initStream(std::unique_ptr Streamer); std::error_code initStreamFromBuffer(); - std::error_code initLazyStream(); + std::error_code initLazyStream(std::unique_ptr Streamer); std::error_code findFunctionInStream( Function *F, DenseMap::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 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 BitcodeReader::parseModuleTriple() { } ErrorOr 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 BitcodeReader::getIdentifiedStructTypes() const { return IdentifiedStructTypes; } -std::error_code BitcodeReader::initStream() { +std::error_code +BitcodeReader::initStream(std::unique_ptr 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 Streamer) { // Check and strip off the bitcode wrapper; BitstreamReader expects never to // see it. - auto OwnedBytes = llvm::make_unique(Streamer); + auto OwnedBytes = + llvm::make_unique(std::move(Streamer)); StreamingMemoryObject &Bytes = *OwnedBytes; StreamFile = llvm::make_unique(std::move(OwnedBytes)); Stream.init(&*StreamFile); @@ -4617,7 +4622,8 @@ getLazyBitcodeModuleImpl(std::unique_ptr &&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> llvm::getLazyBitcodeModule( DiagnosticHandler, ShouldLazyLoadMetadata); } -ErrorOr> -llvm::getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer, - LLVMContext &Context, - DiagnosticHandlerFunction DiagnosticHandler) { +ErrorOr> llvm::getStreamedBitcodeModule( + StringRef Name, std::unique_ptr Streamer, + LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler) { std::unique_ptr M = make_unique(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); } diff --git a/lib/Support/DataStream.cpp b/lib/Support/DataStream.cpp index c24315526cf..3b10fc5eeca 100644 --- a/lib/Support/DataStream.cpp +++ b/lib/Support/DataStream.cpp @@ -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 @@ -73,16 +74,13 @@ public: } -namespace llvm { -DataStreamer *getDataFileStreamer(const std::string &Filename, - std::string *StrError) { - DataFileStreamer *s = new DataFileStreamer(); +std::unique_ptr +llvm::getDataFileStreamer(const std::string &Filename, std::string *StrError) { + std::unique_ptr s = make_unique(); 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); } diff --git a/lib/Support/StreamingMemoryObject.cpp b/lib/Support/StreamingMemoryObject.cpp index 6c5652af04c..5a44e624eb8 100644 --- a/lib/Support/StreamingMemoryObject.cpp +++ b/lib/Support/StreamingMemoryObject.cpp @@ -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 Streamer) + : Bytes(kChunkSize), Streamer(std::move(Streamer)), BytesRead(0), + BytesSkipped(0), ObjectSize(0), EOFReached(false) { + BytesRead = this->Streamer->GetBytes(&Bytes[0], kChunkSize); } } diff --git a/tools/llvm-dis/llvm-dis.cpp b/tools/llvm-dis/llvm-dis.cpp index 26f14b9b1a2..4b7d94d5b26 100644 --- a/tools/llvm-dis/llvm-dis.cpp +++ b/tools/llvm-dis/llvm-dis.cpp @@ -148,7 +148,8 @@ int main(int argc, char **argv) { std::unique_ptr M; // Use the bitcode streaming interface - DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage); + std::unique_ptr 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> MOrErr = - getStreamedBitcodeModule(DisplayFilename, Streamer, Context); + getStreamedBitcodeModule(DisplayFilename, std::move(Streamer), Context); M = std::move(*MOrErr); M->materializeAllPermanently(); } else { diff --git a/unittests/Support/StreamingMemoryObject.cpp b/unittests/Support/StreamingMemoryObject.cpp index c043efbb5e4..e86aa9cae51 100644 --- a/unittests/Support/StreamingMemoryObject.cpp +++ b/unittests/Support/StreamingMemoryObject.cpp @@ -7,6 +7,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/STLExtras.h" #include "llvm/Support/StreamingMemoryObject.h" #include "gtest/gtest.h" #include @@ -23,14 +24,14 @@ class NullDataStreamer : public DataStreamer { } TEST(StreamingMemoryObject, Test) { - auto *DS = new NullDataStreamer(); - StreamingMemoryObject O(DS); + auto DS = make_unique(); + 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(); + StreamingMemoryObject O(std::move(DS)); uint8_t Buf[32]; EXPECT_EQ((uint64_t) 16, O.readBytes(Buf, 16, 0)); O.setKnownObjectSize(24);