[Reproducer] Add LoadBuffer<> helper (NFC)

Introduce a helper method named LoadBuffer in the Loader to abstract
reading a reproducer file from disk.

llvm-svn: 375060
This commit is contained in:
Jonas Devlieghere 2019-10-17 00:01:57 +00:00
parent 27ef81cd48
commit b2575da9aa
2 changed files with 13 additions and 11 deletions

View File

@ -302,6 +302,15 @@ public:
return GetRoot().CopyByAppendingPathComponent(T::file);
}
template <typename T> llvm::Expected<std::string> LoadBuffer() {
FileSpec file = GetFile<typename T::Info>();
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer =
llvm::vfs::getRealFileSystem()->getBufferForFile(file.GetPath());
if (!buffer)
return llvm::errorCodeToError(buffer.getError());
return (*buffer)->getBuffer().str();
}
llvm::Error LoadIndex();
const FileSpec &GetRoot() const { return m_root; }

View File

@ -265,19 +265,12 @@ protected:
return true;
}
case eReproducerProviderVersion: {
FileSpec version_file = loader->GetFile<VersionProvider::Info>();
// Load the version info into a buffer.
ErrorOr<std::unique_ptr<MemoryBuffer>> buffer =
vfs::getRealFileSystem()->getBufferForFile(version_file.GetPath());
if (!buffer) {
SetError(result, errorCodeToError(buffer.getError()));
Expected<std::string> version = loader->LoadBuffer<VersionProvider>();
if (!version) {
SetError(result, version.takeError());
return false;
}
// Return the version string.
StringRef version = (*buffer)->getBuffer();
result.AppendMessage(version.str());
result.AppendMessage(*version);
result.SetStatus(eReturnStatusSuccessFinishResult);
return true;
}