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