From d1fbf2eee6eb8414623a4aa1bc01d04345fe2da4 Mon Sep 17 00:00:00 2001 From: Ivan Krysak Date: Thu, 11 Sep 2025 10:45:38 +0300 Subject: [PATCH] Model: Drop `ToolHelpers.h` --- include/revng/Model/ToolHelpers.h | 150 ------------------ .../abi/tools/ensure-rft-equivalence/Main.cpp | 12 +- tools/model/apply/Main.cpp | 13 +- tools/model/diff/Main.cpp | 2 +- tools/model/export/type-graph/Main.cpp | 1 - tools/model/import/debug-info/Main.cpp | 2 +- tools/model/opt/Main.cpp | 10 +- 7 files changed, 19 insertions(+), 171 deletions(-) delete mode 100644 include/revng/Model/ToolHelpers.h diff --git a/include/revng/Model/ToolHelpers.h b/include/revng/Model/ToolHelpers.h deleted file mode 100644 index 6d394c0ce..000000000 --- a/include/revng/Model/ToolHelpers.h +++ /dev/null @@ -1,150 +0,0 @@ -#pragma once - -// -// This file is distributed under the MIT License. See LICENSE.md for details. -// - -#include -#include -#include -#include - -#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 - NoOutputYAMLOption(Args...) {} -}; - -struct OutputYAMLOption { -protected: - llvm::cl::opt OutputYAML; - -public: - OutputYAMLOption(llvm::cl::OptionCategory &Category) : - OutputYAML("Y", - llvm::cl::desc("Write output as YAML"), - llvm::cl::cat(Category)) {} -}; - -template -class ModelOutputOptions - : public std::conditional_t { -private: - using Base = std::conditional_t; - -private: - llvm::cl::opt OutputAssembly; - - llvm::cl::opt 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); -} diff --git a/tests/abi/tools/ensure-rft-equivalence/Main.cpp b/tests/abi/tools/ensure-rft-equivalence/Main.cpp index 00d10a991..07fc29644 100644 --- a/tests/abi/tools/ensure-rft-equivalence/Main.cpp +++ b/tests/abi/tools/ensure-rft-equivalence/Main.cpp @@ -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; - auto LeftModule = Model::fromFile(LeftModelPath); - if (not LeftModule) - ExitOnError(LeftModule.takeError()); - TupleTree &LeftModel = *LeftModule; - - auto RightModule = Model::fromFile(RightModelPath); - if (not RightModule) - ExitOnError(RightModule.takeError()); - TupleTree &RightModel = *RightModule; + auto LeftModel = ExitOnError(Model::fromFile(LeftModelPath)); + auto RightModel = ExitOnError(Model::fromFile(RightModelPath)); std::error_code EC; llvm::ToolOutputFile OutputFile(OutputFilename, diff --git a/tools/model/apply/Main.cpp b/tools/model/apply/Main.cpp index 3285fb2b6..f60a5eee9 100644 --- a/tools/model/apply/Main.cpp +++ b/tools/model/apply/Main.cpp @@ -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 DiffPath(cl::Positional, cl::init("-"), cl::value_desc("model")); -static ModelOutputOptions Options(ThisToolCategory); +static cl::opt 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(DiffPath)); ExitOnError(Diff.apply(*Model)); - - auto DesiredOutput = Options.getDesiredOutput(); - ExitOnError(Model->toFile(Options.getPath())); + ExitOnError(Model->toFile(OutputFilename)); return EXIT_SUCCESS; } diff --git a/tools/model/diff/Main.cpp b/tools/model/diff/Main.cpp index e00d02098..499fa169a 100644 --- a/tools/model/diff/Main.cpp +++ b/tools/model/diff/Main.cpp @@ -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" diff --git a/tools/model/export/type-graph/Main.cpp b/tools/model/export/type-graph/Main.cpp index 8d8123ee2..4309a90ce 100644 --- a/tools/model/export/type-graph/Main.cpp +++ b/tools/model/export/type-graph/Main.cpp @@ -4,7 +4,6 @@ #include "llvm/Support/CommandLine.h" -#include "revng/Model/ToolHelpers.h" #include "revng/Model/TypeSystemPrinter.h" #include "revng/Support/InitRevng.h" diff --git a/tools/model/import/debug-info/Main.cpp b/tools/model/import/debug-info/Main.cpp index c84834a7d..f491ce6ce 100644 --- a/tools/model/import/debug-info/Main.cpp +++ b/tools/model/import/debug-info/Main.cpp @@ -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" diff --git a/tools/model/opt/Main.cpp b/tools/model/opt/Main.cpp index 66e5dc9bd..71414657b 100644 --- a/tools/model/opt/Main.cpp +++ b/tools/model/opt/Main.cpp @@ -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 Options(ThisToolCategory); +static cl::opt OutputFilename("o", + llvm::cl::init("-"), + llvm::cl::desc("Override output " + "filename"), + llvm::cl::value_desc("filename"), + llvm::cl::cat(ThisToolCategory)); static cl::opt InputFilename(cl::Positional, cl::desc(""), @@ -80,5 +84,5 @@ int main(int Argc, char *Argv[]) { } // Serialize - ExitOnError(MaybeModel.toFile(Options.getPath())); + ExitOnError(MaybeModel.toFile(OutputFilename)); }