Error.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This defines a new error_category for the Object library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Object/Error.h"
  14. #include "llvm/Support/ErrorHandling.h"
  15. #include "llvm/Support/ManagedStatic.h"
  16. using namespace llvm;
  17. using namespace object;
  18. namespace {
  19. // FIXME: This class is only here to support the transition to llvm::Error. It
  20. // will be removed once this transition is complete. Clients should prefer to
  21. // deal with the Error value directly, rather than converting to error_code.
  22. class _object_error_category : public std::error_category {
  23. public:
  24. const char* name() const noexcept override;
  25. std::string message(int ev) const override;
  26. };
  27. }
  28. const char *_object_error_category::name() const noexcept {
  29. return "llvm.object";
  30. }
  31. std::string _object_error_category::message(int EV) const {
  32. object_error E = static_cast<object_error>(EV);
  33. switch (E) {
  34. case object_error::arch_not_found:
  35. return "No object file for requested architecture";
  36. case object_error::invalid_file_type:
  37. return "The file was not recognized as a valid object file";
  38. case object_error::parse_failed:
  39. return "Invalid data was encountered while parsing the file";
  40. case object_error::unexpected_eof:
  41. return "The end of the file was unexpectedly encountered";
  42. case object_error::string_table_non_null_end:
  43. return "String table must end with a null terminator";
  44. case object_error::invalid_section_index:
  45. return "Invalid section index";
  46. case object_error::bitcode_section_not_found:
  47. return "Bitcode section not found in object file";
  48. case object_error::invalid_symbol_index:
  49. return "Invalid symbol index";
  50. }
  51. llvm_unreachable("An enumerator of object_error does not have a message "
  52. "defined.");
  53. }
  54. char BinaryError::ID = 0;
  55. char GenericBinaryError::ID = 0;
  56. GenericBinaryError::GenericBinaryError(Twine Msg) : Msg(Msg.str()) {}
  57. GenericBinaryError::GenericBinaryError(Twine Msg, object_error ECOverride)
  58. : Msg(Msg.str()) {
  59. setErrorCode(make_error_code(ECOverride));
  60. }
  61. void GenericBinaryError::log(raw_ostream &OS) const {
  62. OS << Msg;
  63. }
  64. static ManagedStatic<_object_error_category> error_category;
  65. const std::error_category &object::object_category() {
  66. return *error_category;
  67. }
  68. llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) {
  69. if (auto Err2 =
  70. handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error {
  71. // Try to handle 'M'. If successful, return a success value from
  72. // the handler.
  73. if (M->convertToErrorCode() == object_error::invalid_file_type)
  74. return Error::success();
  75. // We failed to handle 'M' - return it from the handler.
  76. // This value will be passed back from catchErrors and
  77. // wind up in Err2, where it will be returned from this function.
  78. return Error(std::move(M));
  79. }))
  80. return Err2;
  81. return Err;
  82. }