Files
revng-revng/lib/Pipeline/ContainerSet.cpp
Giacomo Vercesi 05dc27715d Improve pipeline saving capabilities
This commit introduces some changes to how the revng pipeline handles
serializing to disk. Specificaly:

* Pipeline globals (specifically model.yml) are better handled if they
  are in a subdirectory. They are now saved in the "context"
  subdirectory.
* In python:revng.api the pipeline is serialized whenever there is a
  non-reproducible change to the state (e.g. binary upload or model
  change).
  In the case of analyses this is done conservatively by checking that
  the diff produced is not empty.
* The logic for computing a step's subdirectory has been moved to the
  pipeline runner, consequently if a step is asked to serialize it
  will not create any subdirectories.
* Functionality for saving a single step/context has been exposed in
  Pipeline C.
* Finally, all path concatenations are now handled by
  llvm::sys::path::append, for extra os-agnosticism.
2022-06-29 14:50:58 +02:00

149 lines
4.2 KiB
C++

/// \file ContainerSet.cpp
/// \brief ContainerSets are collections of containers able to spawn them on
/// request
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include "revng/Pipeline/ContainerSet.h"
#include "revng/Pipeline/Errors.h"
using namespace pipeline;
using namespace llvm;
using namespace std;
ContainerSet ContainerSet::cloneFiltered(const ContainerToTargetsMap &Targets) {
ContainerSet ToReturn;
for (const auto &Pair : Content) {
const auto &ContainerName = Pair.first();
const auto &Container = Pair.second;
auto ExtractedNames = Targets.contains(ContainerName) ?
Targets.at(ContainerName) :
TargetsList();
auto Cloned = Container != nullptr ?
Container->cloneFiltered(ExtractedNames) :
nullptr;
ToReturn.add(ContainerName, *Factories[Pair.first()], move(Cloned));
}
revng_assert(ToReturn.Content.size() == Content.size());
return ToReturn;
}
bool ContainerSet::contains(const Target &Target) const {
return llvm::any_of(Content, [&Target](const auto &Container) {
return Container.second->enumerate().contains(Target);
});
}
Error ContainerSet::remove(const ContainerToTargetsMap &ToRemove) {
for (const auto &Target : ToRemove) {
const auto &ContainerName = Target.first();
const auto &NamesToRemove = Target.second;
if (not contains(ContainerName))
continue;
if (NamesToRemove.size() == 0)
continue;
if (auto Ok = at(ContainerName).remove(NamesToRemove); not Ok)
return make_error<UnknownTargetError>(NamesToRemove, ContainerName);
}
return Error::success();
}
void ContainerSet::intersect(ContainerToTargetsMap &ToIntersect) const {
for (auto &ContainerStatus : ToIntersect) {
const auto &ContainerName = ContainerStatus.first();
auto &Names = ContainerStatus.second;
if (not contains(ContainerName)) {
Names = {};
return;
}
const auto &Container = at(ContainerName);
auto Enumerated = Container.enumerate();
erase_if(Names, [&Enumerated](const Target &Target) {
return not Enumerated.contains(Target);
});
}
}
llvm::Error ContainerSet::storeToDisk(StringRef Directory) const {
for (const auto &Pair : Content) {
llvm::SmallString<128> Filename;
llvm::sys::path::append(Filename, Directory, Pair.first());
const auto &Container = Pair.second;
if (Container == nullptr)
continue;
if (auto Error = Container->storeToDisk(Filename); !!Error)
return Error;
}
return Error::success();
}
llvm::Error ContainerSet::loadFromDisk(StringRef Directory) {
for (auto &Pair : Content) {
llvm::SmallString<128> Filename;
llvm::sys::path::append(Filename, Directory, Pair.first());
if (auto Error = (*this)[Pair.first()].loadFromDisk(Filename); !!Error)
return Error;
}
return Error::success();
}
llvm::Error ContainerSet::verify() const {
for (const auto &Pair : Content) {
if (Pair.second == nullptr)
continue;
if (auto Error = Pair.second->verify(); Error)
return Error;
}
return llvm::Error::success();
}
ContainerToTargetsMap ContainerSet::enumerate() const {
ContainerToTargetsMap Status;
for (const auto &Pair : *this) {
const auto &Name = Pair.first();
const auto &MaybeCont = Pair.second;
if (MaybeCont != nullptr)
Status[Name] = MaybeCont->enumerate();
}
return Status;
}
llvm::Error ContainerBase::storeToDisk(llvm::StringRef Path) const {
std::error_code EC;
llvm::raw_fd_ostream OS(Path, EC, llvm::sys::fs::F_None);
if (EC)
return llvm::createStringError(EC,
"could not write file at %s",
Path.str().c_str());
return serialize(OS);
}
llvm::Error ContainerBase::loadFromDisk(llvm::StringRef Path) {
if (not llvm::sys::fs::exists(Path)) {
clear();
return llvm::Error::success();
}
if (auto MaybeBuffer = MemoryBuffer::getFile(Path); !MaybeBuffer)
return llvm::createStringError(MaybeBuffer.getError(),
"could not read file");
else
return deserialize(**MaybeBuffer);
}