summaryrefslogtreecommitdiffstats
path: root/llvm/lib/ProfileData
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/ProfileData')
-rw-r--r--llvm/lib/ProfileData/InstrProfReader.cpp30
-rw-r--r--llvm/lib/ProfileData/SampleProfReader.cpp25
-rw-r--r--llvm/lib/ProfileData/SampleProfWriter.cpp12
3 files changed, 39 insertions, 28 deletions
diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp
index e333473f6f8..0160a640fdc 100644
--- a/llvm/lib/ProfileData/InstrProfReader.cpp
+++ b/llvm/lib/ProfileData/InstrProfReader.cpp
@@ -21,32 +21,34 @@
using namespace llvm;
-static std::error_code
-setupMemoryBuffer(std::string Path, std::unique_ptr<MemoryBuffer> &Buffer) {
+static ErrorOr<std::unique_ptr<MemoryBuffer>>
+setupMemoryBuffer(std::string Path) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getFileOrSTDIN(Path);
if (std::error_code EC = BufferOrErr.getError())
return EC;
- Buffer = std::move(BufferOrErr.get());
+ auto Buffer = std::move(BufferOrErr.get());
// Sanity check the file.
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
return instrprof_error::too_large;
- return instrprof_error::success;
+ return std::move(Buffer);
}
static std::error_code initializeReader(InstrProfReader &Reader) {
return Reader.readHeader();
}
-std::error_code
-InstrProfReader::create(std::string Path,
- std::unique_ptr<InstrProfReader> &Result) {
+ErrorOr<std::unique_ptr<InstrProfReader>>
+InstrProfReader::create(std::string Path) {
// Set up the buffer to read.
- std::unique_ptr<MemoryBuffer> Buffer;
- if (std::error_code EC = setupMemoryBuffer(Path, Buffer))
+ auto BufferOrError = setupMemoryBuffer(Path);
+ if (std::error_code EC = BufferOrError.getError())
return EC;
+ auto Buffer = std::move(BufferOrError.get());
+ std::unique_ptr<InstrProfReader> Result;
+
// Create the reader.
if (IndexedInstrProfReader::hasFormat(*Buffer))
Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
@@ -58,16 +60,20 @@ InstrProfReader::create(std::string Path,
Result.reset(new TextInstrProfReader(std::move(Buffer)));
// Initialize the reader and return the result.
- return initializeReader(*Result);
+ if (std::error_code EC = initializeReader(*Result))
+ return EC;
+
+ return std::move(Result);
}
std::error_code IndexedInstrProfReader::create(
std::string Path, std::unique_ptr<IndexedInstrProfReader> &Result) {
// Set up the buffer to read.
- std::unique_ptr<MemoryBuffer> Buffer;
- if (std::error_code EC = setupMemoryBuffer(Path, Buffer))
+ auto BufferOrError = setupMemoryBuffer(Path);
+ if (std::error_code EC = BufferOrError.getError())
return EC;
+ auto Buffer = std::move(BufferOrError.get());
// Create the reader.
if (!IndexedInstrProfReader::hasFormat(*Buffer))
return instrprof_error::bad_magic;
diff --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp
index a6e4e0ce502..b39bfd6e2ec 100644
--- a/llvm/lib/ProfileData/SampleProfReader.cpp
+++ b/llvm/lib/ProfileData/SampleProfReader.cpp
@@ -356,18 +356,18 @@ bool SampleProfileReaderBinary::hasFormat(const MemoryBuffer &Buffer) {
/// \brief Prepare a memory buffer for the contents of \p Filename.
///
/// \returns an error code indicating the status of the buffer.
-static std::error_code
-setupMemoryBuffer(std::string Filename, std::unique_ptr<MemoryBuffer> &Buffer) {
+static ErrorOr<std::unique_ptr<MemoryBuffer>>
+setupMemoryBuffer(std::string Filename) {
auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
if (std::error_code EC = BufferOrErr.getError())
return EC;
- Buffer = std::move(BufferOrErr.get());
+ auto Buffer = std::move(BufferOrErr.get());
// Sanity check the file.
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
return sampleprof_error::too_large;
- return sampleprof_error::success;
+ return std::move(Buffer);
}
/// \brief Create a sample profile reader based on the format of the input file.
@@ -379,18 +379,21 @@ setupMemoryBuffer(std::string Filename, std::unique_ptr<MemoryBuffer> &Buffer) {
/// \param C The LLVM context to use to emit diagnostics.
///
/// \returns an error code indicating the status of the created reader.
-std::error_code
-SampleProfileReader::create(StringRef Filename,
- std::unique_ptr<SampleProfileReader> &Reader,
- LLVMContext &C) {
- std::unique_ptr<MemoryBuffer> Buffer;
- if (std::error_code EC = setupMemoryBuffer(Filename, Buffer))
+ErrorOr<std::unique_ptr<SampleProfileReader>>
+SampleProfileReader::create(StringRef Filename, LLVMContext &C) {
+ auto BufferOrError = setupMemoryBuffer(Filename);
+ if (std::error_code EC = BufferOrError.getError())
return EC;
+ auto Buffer = std::move(BufferOrError.get());
+ std::unique_ptr<SampleProfileReader> Reader;
if (SampleProfileReaderBinary::hasFormat(*Buffer))
Reader.reset(new SampleProfileReaderBinary(std::move(Buffer), C));
else
Reader.reset(new SampleProfileReaderText(std::move(Buffer), C));
- return Reader->readHeader();
+ if (std::error_code EC = Reader->readHeader())
+ return EC;
+
+ return std::move(Reader);
}
diff --git a/llvm/lib/ProfileData/SampleProfWriter.cpp b/llvm/lib/ProfileData/SampleProfWriter.cpp
index 49d6fdbf4f7..85250452bd1 100644
--- a/llvm/lib/ProfileData/SampleProfWriter.cpp
+++ b/llvm/lib/ProfileData/SampleProfWriter.cpp
@@ -107,11 +107,10 @@ bool SampleProfileWriterBinary::write(StringRef FName,
/// \param Format Encoding format for the profile file.
///
/// \returns an error code indicating the status of the created writer.
-std::error_code
-SampleProfileWriter::create(StringRef Filename,
- std::unique_ptr<SampleProfileWriter> &Writer,
- SampleProfileFormat Format) {
+ErrorOr<std::unique_ptr<SampleProfileWriter>>
+SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
std::error_code EC;
+ std::unique_ptr<SampleProfileWriter> Writer;
if (Format == SPF_Binary)
Writer.reset(new SampleProfileWriterBinary(Filename, EC));
@@ -120,5 +119,8 @@ SampleProfileWriter::create(StringRef Filename,
else
EC = sampleprof_error::unrecognized_format;
- return EC;
+ if (EC)
+ return EC;
+
+ return std::move(Writer);
}
OpenPOWER on IntegriCloud