InstrProf: Add unit tests for the profile reader and writer

This required some minor API to be added to these types to avoid
needing temp files.

Also, I've used initializer lists in the tests, as MSVC 2013 claims to
support them. I'll redo this without them if the bots complain.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@229455 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Justin Bogner
2015-02-16 23:27:48 +00:00
parent ca64d76c1b
commit 77ae39453c
6 changed files with 146 additions and 16 deletions

View File

@ -25,12 +25,7 @@ setupMemoryBuffer(std::string Path) {
MemoryBuffer::getFileOrSTDIN(Path);
if (std::error_code EC = BufferOrErr.getError())
return EC;
auto Buffer = std::move(BufferOrErr.get());
// Sanity check the file.
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
return instrprof_error::too_large;
return std::move(Buffer);
return std::move(BufferOrErr.get());
}
static std::error_code initializeReader(InstrProfReader &Reader) {
@ -43,10 +38,16 @@ InstrProfReader::create(std::string Path) {
auto BufferOrError = setupMemoryBuffer(Path);
if (std::error_code EC = BufferOrError.getError())
return EC;
return InstrProfReader::create(std::move(BufferOrError.get()));
}
ErrorOr<std::unique_ptr<InstrProfReader>>
InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
// Sanity check the buffer.
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
return instrprof_error::too_large;
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)));
@ -70,14 +71,20 @@ IndexedInstrProfReader::create(std::string Path) {
auto BufferOrError = setupMemoryBuffer(Path);
if (std::error_code EC = BufferOrError.getError())
return EC;
return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
}
auto Buffer = std::move(BufferOrError.get());
std::unique_ptr<IndexedInstrProfReader> Result;
ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
// Sanity check the buffer.
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
return instrprof_error::too_large;
// Create the reader.
if (!IndexedInstrProfReader::hasFormat(*Buffer))
return instrprof_error::bad_magic;
Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer));
// Initialize the reader and return the result.
if (std::error_code EC = initializeReader(*Result))