Files
revng-revng/lib/Pipeline/Context.cpp
Alessandro Di Federico 5820908675 Remove and ban \file
2025-12-16 17:41:55 +01:00

63 lines
1.8 KiB
C++

/// The pipeline context the place where all objects used by more that one
/// pipeline or container are stored.
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include <cstdlib>
#include "llvm/ADT/StringRef.h"
#include "revng/Pipeline/AllRegistries.h"
#include "revng/Pipeline/Context.h"
using namespace pipeline;
Logger pipeline::ExplanationLogger("pipeline");
Logger pipeline::CommandLogger("commands");
Context::Context() : TheKindRegistry(Registry::registerAllKinds()) {
}
llvm::Error Context::store(const revng::DirectoryPath &Path) const {
if (auto Error = Globals.store(Path))
return Error;
revng::FilePath IndexPath = Path.getFile("index");
auto MaybeWritableFile = IndexPath.getWritableFile();
if (not MaybeWritableFile)
return MaybeWritableFile.takeError();
MaybeWritableFile->get()->os() << CommitIndex << "\n";
return MaybeWritableFile->get()->commit();
}
llvm::Error Context::load(const revng::DirectoryPath &Path) {
if (auto Error = Globals.load(Path))
return Error;
revng::FilePath IndexPath = Path.getFile("index");
auto MaybeExists = IndexPath.exists();
if (not MaybeExists)
return MaybeExists.takeError();
// In the context of `lib/Pipeline`, both context and containers are default
// constructible. As such, we just skip loading files that do not exist,
// ending up with the default state.
if (not MaybeExists.get())
return llvm::Error::success();
auto MaybeReadableFile = IndexPath.getReadableFile();
if (not MaybeReadableFile)
return MaybeReadableFile.takeError();
llvm::StringRef Buffer = MaybeReadableFile->get()->buffer().getBuffer();
if (Buffer.trim().getAsInteger(10, CommitIndex)) {
return revng::createError("Malformed index file");
}
return llvm::Error::success();
}