[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
This commit is contained in:
Chris Bieneman 2016-05-12 01:52:33 +00:00
parent dd7a9752c2
commit eb3d76315f
3 changed files with 45 additions and 11 deletions

View File

@ -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<int>(Code), obj2yaml_category());
}
} // namespace llvm

View File

@ -10,6 +10,8 @@
#ifndef LLVM_TOOLS_OBJ2YAML_ERROR_H
#define LLVM_TOOLS_OBJ2YAML_ERROR_H
#include "llvm/Support/Error.h"
#include <system_error>
namespace llvm {
@ -27,6 +29,22 @@ inline std::error_code make_error_code(obj2yaml_error e) {
return std::error_code(static_cast<int>(e), obj2yaml_category());
}
class Obj2YamlError : public ErrorInfo<Obj2YamlError> {
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 {

View File

@ -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<Obj2YamlError>(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<Obj2YamlError>(obj2yaml_error::not_implemented);
}
std::error_code macho2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
if (const auto *MachOObj = dyn_cast<object::MachOUniversalBinary>(&Obj))
return macho2yaml(Out, *MachOObj);
if (const auto *MachOObj = dyn_cast<object::MachOUniversalBinary>(&Obj)) {
if (auto Err = macho2yaml(Out, *MachOObj)) {
return errorToErrorCode(std::move(Err));
}
return obj2yaml_error::success;
}
if (const auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
return macho2yaml(Out, *MachOObj);
if (const auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj)) {
if (auto Err = macho2yaml(Out, *MachOObj)) {
return errorToErrorCode(std::move(Err));
}
return obj2yaml_error::success;
}
return obj2yaml_error::unsupported_obj_file_format;
}