176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher//===- Error.cpp - system_error extensions for llvm-readobj -----*- C++ -*-===//
276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher//
376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher//                     The LLVM Compiler Infrastructure
476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher//
576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher// This file is distributed under the University of Illinois Open Source
676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher// License. See LICENSE.TXT for details.
776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher//
876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher//===----------------------------------------------------------------------===//
976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher//
1076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher// This defines a new error_category for the llvm-readobj tool.
1176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher//
1276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher//===----------------------------------------------------------------------===//
1376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
1476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "Error.h"
1576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher#include "llvm/Support/ErrorHandling.h"
1676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
1776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherusing namespace llvm;
1876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
1976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christophernamespace {
20cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesclass _readobj_error_category : public std::error_category {
2176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherpublic:
22cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  const char* name() const LLVM_NOEXCEPT override;
2336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::string message(int ev) const override;
2476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher};
2576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher} // namespace
2676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
2776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopherconst char *_readobj_error_category::name() const {
2876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  return "llvm.readobj";
2976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher}
3076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
31cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesstd::string _readobj_error_category::message(int EV) const {
32cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  switch (static_cast<readobj_error>(EV)) {
3376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  case readobj_error::success: return "Success";
3476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  case readobj_error::file_not_found:
3576e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return "No such file.";
3676e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  case readobj_error::unsupported_file_format:
3776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return "The file was not recognized as a valid object file.";
3876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  case readobj_error::unrecognized_file_format:
3976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return "Unrecognized file type.";
4076e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  case readobj_error::unsupported_obj_file_format:
4176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return "Unsupported object file format.";
4276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  case readobj_error::unknown_symbol:
4376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher    return "Unknown symbol.";
4476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  }
45cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  llvm_unreachable("An enumerator of readobj_error does not have a message "
46cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                   "defined.");
4776e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher}
4876e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher
4976e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christophernamespace llvm {
50cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesconst std::error_category &readobj_category() {
5176e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  static _readobj_error_category o;
5276e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher  return o;
5376e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher}
5476e70f340c09ba759ad96d8dfe416b64f24bc287Eric Christopher} // namespace llvm
55