Model: Drop ToolHelpers.h

This commit is contained in:
Ivan Krysak
2025-09-11 10:45:38 +03:00
parent b6cb5c2ca3
commit d1fbf2eee6
7 changed files with 19 additions and 171 deletions
-150
View File
@@ -1,150 +0,0 @@
#pragma once
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include <fstream>
#include <memory>
#include <string>
#include <type_traits>
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/Module.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/raw_os_ostream.h"
#include "revng/Model/Binary.h"
#include "revng/Model/LoadModelPass.h"
#include "revng/Support/Assert.h"
namespace ModelOutputType {
enum Values {
Invalid,
YAML,
LLVMIR,
BitCode
};
inline bool requiresModule(Values V) {
switch (V) {
case YAML:
return false;
case LLVMIR:
case BitCode:
return true;
default:
revng_abort();
}
}
inline llvm::sys::fs::OpenFlags getFlags(Values V) {
using namespace llvm::sys::fs;
switch (V) {
case YAML:
case LLVMIR:
return OF_Text;
case BitCode:
return OF_None;
default:
revng_abort();
}
}
} // namespace ModelOutputType
struct NoOutputYAMLOption {
template<typename... Args>
NoOutputYAMLOption(Args...) {}
};
struct OutputYAMLOption {
protected:
llvm::cl::opt<bool> OutputYAML;
public:
OutputYAMLOption(llvm::cl::OptionCategory &Category) :
OutputYAML("Y",
llvm::cl::desc("Write output as YAML"),
llvm::cl::cat(Category)) {}
};
template<bool YAML = true>
class ModelOutputOptions
: public std::conditional_t<YAML, OutputYAMLOption, NoOutputYAMLOption> {
private:
using Base = std::conditional_t<YAML, OutputYAMLOption, NoOutputYAMLOption>;
private:
llvm::cl::opt<bool> OutputAssembly;
llvm::cl::opt<std::string> OutputFilename;
public:
ModelOutputOptions(llvm::cl::OptionCategory &Category) :
Base(Category),
OutputAssembly("S",
llvm::cl::desc("Write output as LLVM assembly"),
llvm::cl::cat(Category)),
OutputFilename("o",
llvm::cl::init("-"),
llvm::cl::desc("Override output filename"),
llvm::cl::value_desc("filename"),
llvm::cl::cat(Category)) {}
public:
ModelOutputType::Values getDesiredOutput() const {
if constexpr (YAML) {
if (this->OutputYAML && OutputAssembly)
return ModelOutputType::Invalid;
if (this->OutputYAML)
return ModelOutputType::YAML;
if (OutputAssembly)
return ModelOutputType::LLVMIR;
return ModelOutputType::YAML;
} else {
if (OutputAssembly)
return ModelOutputType::LLVMIR;
return ModelOutputType::YAML;
}
revng_abort();
}
std::string getPath() const { return OutputFilename; }
};
inline void writeModel(const model::Binary &Model, llvm::Module &M) {
Model.verify(true);
llvm::NamedMDNode *NamedMD = M.getNamedMetadata(ModelMetadataName);
revng_check(not NamedMD, "The model has already been serialized");
std::string Buffer;
{
llvm::raw_string_ostream Stream(Buffer);
serialize(Stream, Model);
}
llvm::LLVMContext &Context = M.getContext();
auto Tuple = llvm::MDTuple::get(Context,
{ llvm::MDString::get(Context, Buffer) });
NamedMD = M.getOrInsertNamedMetadata(ModelMetadataName);
NamedMD->addOperand(Tuple);
}
@@ -23,7 +23,6 @@
#include "revng/Model/NameBuilder.h"
#include "revng/Model/Pass/PurgeUnnamedAndUnreachableTypes.h"
#include "revng/Model/ToolHelpers.h"
#include "revng/Support/InitRevng.h"
#include "revng/Support/MetaAddress/YAMLTraits.h"
#include "revng/Support/YAMLTraits.h"
@@ -113,15 +112,8 @@ int main(int Argc, char *Argv[]) {
llvm::ExitOnError ExitOnError;
using Model = TupleTree<model::Binary>;
auto LeftModule = Model::fromFile(LeftModelPath);
if (not LeftModule)
ExitOnError(LeftModule.takeError());
TupleTree<model::Binary> &LeftModel = *LeftModule;
auto RightModule = Model::fromFile(RightModelPath);
if (not RightModule)
ExitOnError(RightModule.takeError());
TupleTree<model::Binary> &RightModel = *RightModule;
auto LeftModel = ExitOnError(Model::fromFile(LeftModelPath));
auto RightModel = ExitOnError(Model::fromFile(RightModelPath));
std::error_code EC;
llvm::ToolOutputFile OutputFile(OutputFilename,
+8 -5
View File
@@ -10,7 +10,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "revng/Model/ToolHelpers.h"
#include "revng/Model/Binary.h"
#include "revng/Support/InitRevng.h"
#include "revng/Support/MetaAddress/YAMLTraits.h"
#include "revng/Support/YAMLTraits.h"
@@ -31,7 +31,12 @@ static cl::opt<std::string> DiffPath(cl::Positional,
cl::init("-"),
cl::value_desc("model"));
static ModelOutputOptions<false> Options(ThisToolCategory);
static cl::opt<std::string> OutputFilename("o",
llvm::cl::init("-"),
llvm::cl::desc("Override output "
"filename"),
llvm::cl::value_desc("filename"),
llvm::cl::cat(ThisToolCategory));
int main(int Argc, char *Argv[]) {
revng::InitRevng X(Argc, Argv, "", { &ThisToolCategory });
@@ -47,9 +52,7 @@ int main(int Argc, char *Argv[]) {
auto Diff = ExitOnError(fromFileOrSTDIN<TypeDiff>(DiffPath));
ExitOnError(Diff.apply(*Model));
auto DesiredOutput = Options.getDesiredOutput();
ExitOnError(Model->toFile(Options.getPath()));
ExitOnError(Model->toFile(OutputFilename));
return EXIT_SUCCESS;
}
+1 -1
View File
@@ -11,7 +11,7 @@
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/raw_ostream.h"
#include "revng/Model/ToolHelpers.h"
#include "revng/Model/Binary.h"
#include "revng/Support/InitRevng.h"
#include "revng/Support/MetaAddress/YAMLTraits.h"
#include "revng/Support/YAMLTraits.h"
-1
View File
@@ -4,7 +4,6 @@
#include "llvm/Support/CommandLine.h"
#include "revng/Model/ToolHelpers.h"
#include "revng/Model/TypeSystemPrinter.h"
#include "revng/Support/InitRevng.h"
+1 -1
View File
@@ -10,12 +10,12 @@
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ToolOutputFile.h"
#include "revng/ABI/DefaultFunctionPrototype.h"
#include "revng/Model/Importer/Binary/Options.h"
#include "revng/Model/Importer/DebugInfo/DwarfImporter.h"
#include "revng/Model/Importer/DebugInfo/PDBImporter.h"
#include "revng/Model/ToolHelpers.h"
#include "revng/Support/InitRevng.h"
#include "revng/Support/MetaAddress.h"
+7 -3
View File
@@ -11,14 +11,18 @@
#include "revng/Model/Pass/RegisterModelPass.h"
#include "revng/Model/Processing.h"
#include "revng/Model/ToolHelpers.h"
#include "revng/Support/InitRevng.h"
namespace cl = llvm::cl;
static cl::OptionCategory ThisToolCategory("Tool options", "");
extern cl::OptionCategory ModelPassCategory;
static ModelOutputOptions<true> Options(ThisToolCategory);
static cl::opt<std::string> OutputFilename("o",
llvm::cl::init("-"),
llvm::cl::desc("Override output "
"filename"),
llvm::cl::value_desc("filename"),
llvm::cl::cat(ThisToolCategory));
static cl::opt<std::string> InputFilename(cl::Positional,
cl::desc("<input model file>"),
@@ -80,5 +84,5 @@ int main(int Argc, char *Argv[]) {
}
// Serialize
ExitOnError(MaybeModel.toFile(Options.getPath()));
ExitOnError(MaybeModel.toFile(OutputFilename));
}