From eb3d76315f43ccbf02a89eed57be992fa1ec5e6c Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Thu, 12 May 2016 01:52:33 +0000 Subject: [PATCH] [obj2yaml] Adding Error/Expected to macho2yaml I figure if I'm adding Mach support I may as well use the new fancy Error model. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269264 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/obj2yaml/Error.cpp | 12 +++++++++++- tools/obj2yaml/Error.h | 18 ++++++++++++++++++ tools/obj2yaml/macho2yaml.cpp | 26 ++++++++++++++++---------- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/tools/obj2yaml/Error.cpp b/tools/obj2yaml/Error.cpp index 9eb70c6a47b..9c2aeefef2f 100644 --- a/tools/obj2yaml/Error.cpp +++ b/tools/obj2yaml/Error.cpp @@ -42,8 +42,18 @@ std::string _obj2yaml_error_category::message(int ev) const { } namespace llvm { - const std::error_category &obj2yaml_category() { + +const std::error_category &obj2yaml_category() { static _obj2yaml_error_category o; return o; } + +char Obj2YamlError::ID = 0; + +void Obj2YamlError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; } + +std::error_code Obj2YamlError::convertToErrorCode() const { + return std::error_code(static_cast(Code), obj2yaml_category()); +} + } // namespace llvm diff --git a/tools/obj2yaml/Error.h b/tools/obj2yaml/Error.h index 7be92e9aace..f4e191c872c 100644 --- a/tools/obj2yaml/Error.h +++ b/tools/obj2yaml/Error.h @@ -10,6 +10,8 @@ #ifndef LLVM_TOOLS_OBJ2YAML_ERROR_H #define LLVM_TOOLS_OBJ2YAML_ERROR_H +#include "llvm/Support/Error.h" + #include namespace llvm { @@ -27,6 +29,22 @@ inline std::error_code make_error_code(obj2yaml_error e) { return std::error_code(static_cast(e), obj2yaml_category()); } +class Obj2YamlError : public ErrorInfo { +public: + static char ID; + Obj2YamlError(obj2yaml_error C) : Code(C) {} + Obj2YamlError(const std::string &ErrMsg) : ErrMsg(std::move(ErrMsg)) {} + Obj2YamlError(obj2yaml_error C, std::string ErrMsg) + : ErrMsg(std::move(ErrMsg)), Code(C) {} + void log(raw_ostream &OS) const override; + const std::string &getErrorMessage() const { return ErrMsg; } + std::error_code convertToErrorCode() const override; + +private: + std::string ErrMsg; + obj2yaml_error Code; +}; + } // namespace llvm namespace std { diff --git a/tools/obj2yaml/macho2yaml.cpp b/tools/obj2yaml/macho2yaml.cpp index c3b972dcbe3..76f99c8dc36 100644 --- a/tools/obj2yaml/macho2yaml.cpp +++ b/tools/obj2yaml/macho2yaml.cpp @@ -14,22 +14,28 @@ using namespace llvm; -std::error_code macho2yaml(raw_ostream &Out, - const object::MachOObjectFile &Obj) { - return obj2yaml_error::not_implemented; +Error macho2yaml(raw_ostream &Out, const object::MachOObjectFile &Obj) { + return make_error(obj2yaml_error::not_implemented); } -std::error_code macho2yaml(raw_ostream &Out, - const object::MachOUniversalBinary &Obj) { - return obj2yaml_error::not_implemented; +Error macho2yaml(raw_ostream &Out, const object::MachOUniversalBinary &Obj) { + return make_error(obj2yaml_error::not_implemented); } std::error_code macho2yaml(raw_ostream &Out, const object::ObjectFile &Obj) { - if (const auto *MachOObj = dyn_cast(&Obj)) - return macho2yaml(Out, *MachOObj); + if (const auto *MachOObj = dyn_cast(&Obj)) { + if (auto Err = macho2yaml(Out, *MachOObj)) { + return errorToErrorCode(std::move(Err)); + } + return obj2yaml_error::success; + } - if (const auto *MachOObj = dyn_cast(&Obj)) - return macho2yaml(Out, *MachOObj); + if (const auto *MachOObj = dyn_cast(&Obj)) { + if (auto Err = macho2yaml(Out, *MachOObj)) { + return errorToErrorCode(std::move(Err)); + } + return obj2yaml_error::success; + } return obj2yaml_error::unsupported_obj_file_format; }