Error.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This defines a new error_category for the Object library.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Object/Error.h"
  13. #include "llvm/ADT/Twine.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. void BinaryError::anchor() {}
  55. char BinaryError::ID = 0;
  56. char GenericBinaryError::ID = 0;
  57. GenericBinaryError::GenericBinaryError(const Twine &Msg) : Msg(Msg.str()) {}
  58. GenericBinaryError::GenericBinaryError(const Twine &Msg,
  59. object_error ECOverride)
  60. : Msg(Msg.str()) {
  61. setErrorCode(make_error_code(ECOverride));
  62. }
  63. void GenericBinaryError::log(raw_ostream &OS) const {
  64. OS << Msg;
  65. }
  66. static ManagedStatic<_object_error_category> error_category;
  67. const std::error_category &object::object_category() {
  68. return *error_category;
  69. }
  70. llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) {
  71. return handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error {
  72. // Try to handle 'M'. If successful, return a success value from
  73. // the handler.
  74. if (M->convertToErrorCode() == object_error::invalid_file_type)
  75. return Error::success();
  76. // We failed to handle 'M' - return it from the handler.
  77. // This value will be passed back from catchErrors and
  78. // wind up in Err2, where it will be returned from this function.
  79. return Error(std::move(M));
  80. });
  81. }