[llvm] r239867 - Use std::unique_ptr to manage the DataStreamer in bitcode parsing.
Rafael Espindola
rafael.espindola at gmail.com
Tue Jun 16 16:29:49 PDT 2015
Author: rafael
Date: Tue Jun 16 18:29:49 2015
New Revision: 239867
URL: http://llvm.org/viewvc/llvm-project?rev=239867&view=rev
Log:
Use std::unique_ptr to manage the DataStreamer in bitcode parsing.
We were already deleting it, this just makes it explicit.
Modified:
llvm/trunk/include/llvm/Bitcode/ReaderWriter.h
llvm/trunk/include/llvm/Support/DataStream.h
llvm/trunk/include/llvm/Support/StreamingMemoryObject.h
llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/trunk/lib/Support/DataStream.cpp
llvm/trunk/lib/Support/StreamingMemoryObject.cpp
llvm/trunk/tools/llvm-dis/llvm-dis.cpp
llvm/trunk/unittests/Support/StreamingMemoryObject.cpp
Modified: llvm/trunk/include/llvm/Bitcode/ReaderWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/ReaderWriter.h?rev=239867&r1=239866&r2=239867&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Bitcode/ReaderWriter.h (original)
+++ llvm/trunk/include/llvm/Bitcode/ReaderWriter.h Tue Jun 16 18:29:49 2015
@@ -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
Modified: llvm/trunk/include/llvm/Support/DataStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/DataStream.h?rev=239867&r1=239866&r2=239867&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/DataStream.h (original)
+++ llvm/trunk/include/llvm/Support/DataStream.h Tue Jun 16 18:29:49 2015
@@ -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_
Modified: llvm/trunk/include/llvm/Support/StreamingMemoryObject.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/StreamingMemoryObject.h?rev=239867&r1=239866&r2=239867&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/StreamingMemoryObject.h (original)
+++ llvm/trunk/include/llvm/Support/StreamingMemoryObject.h Tue Jun 16 18:29:49 2015
@@ -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;
Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=239867&r1=239866&r2=239867&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Tue Jun 16 18:29:49 2015
@@ -136,7 +136,7 @@ class BitcodeReader : public GVMateriali
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(MemoryBuffe
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::parseModu
// 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::parseModu
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::parseModu
}
}
-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::pars
}
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::materiali
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:
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::initStrea
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
};
// 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::g
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);
}
Modified: llvm/trunk/lib/Support/DataStream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/DataStream.cpp?rev=239867&r1=239866&r2=239867&view=diff
==============================================================================
--- llvm/trunk/lib/Support/DataStream.cpp (original)
+++ llvm/trunk/lib/Support/DataStream.cpp Tue Jun 16 18:29:49 2015
@@ -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);
}
Modified: llvm/trunk/lib/Support/StreamingMemoryObject.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StreamingMemoryObject.cpp?rev=239867&r1=239866&r2=239867&view=diff
==============================================================================
--- llvm/trunk/lib/Support/StreamingMemoryObject.cpp (original)
+++ llvm/trunk/lib/Support/StreamingMemoryObject.cpp Tue Jun 16 18:29:49 2015
@@ -123,9 +123,10 @@ MemoryObject *getNonStreamedMemoryObject
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);
}
}
Modified: llvm/trunk/tools/llvm-dis/llvm-dis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-dis/llvm-dis.cpp?rev=239867&r1=239866&r2=239867&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-dis/llvm-dis.cpp (original)
+++ llvm/trunk/tools/llvm-dis/llvm-dis.cpp Tue Jun 16 18:29:49 2015
@@ -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 {
Modified: llvm/trunk/unittests/Support/StreamingMemoryObject.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/StreamingMemoryObject.cpp?rev=239867&r1=239866&r2=239867&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/StreamingMemoryObject.cpp (original)
+++ llvm/trunk/unittests/Support/StreamingMemoryObject.cpp Tue Jun 16 18:29:49 2015
@@ -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 DataStre
}
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);
More information about the llvm-commits
mailing list