Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 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 | |
| 14 | #include "llvm/Object/Error.h" |
| 15 | #include "llvm/Support/ErrorHandling.h" |
Chris Bieneman | 684981e | 2014-09-19 22:09:18 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace object; |
| 20 | |
| 21 | namespace { |
Peter Collingbourne | 4718f8b | 2016-05-24 20:13:46 +0000 | [diff] [blame] | 22 | // FIXME: This class is only here to support the transition to llvm::Error. It |
| 23 | // will be removed once this transition is complete. Clients should prefer to |
| 24 | // deal with the Error value directly, rather than converting to error_code. |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 25 | class _object_error_category : public std::error_category { |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 26 | public: |
Reid Kleckner | 990504e | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 27 | const char* name() const noexcept override; |
Justin Bogner | e3bfdc4 | 2014-03-15 04:05:59 +0000 | [diff] [blame] | 28 | std::string message(int ev) const override; |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 29 | }; |
| 30 | } |
| 31 | |
Reid Kleckner | 990504e | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 32 | const char *_object_error_category::name() const noexcept { |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 33 | return "llvm.object"; |
| 34 | } |
| 35 | |
Rafael Espindola | e00fec8 | 2014-06-03 05:26:12 +0000 | [diff] [blame] | 36 | std::string _object_error_category::message(int EV) const { |
| 37 | object_error E = static_cast<object_error>(EV); |
Rafael Espindola | e107ade | 2013-06-18 13:30:31 +0000 | [diff] [blame] | 38 | switch (E) { |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 39 | case object_error::arch_not_found: |
| 40 | return "No object file for requested architecture"; |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 41 | case object_error::invalid_file_type: |
| 42 | return "The file was not recognized as a valid object file"; |
| 43 | case object_error::parse_failed: |
| 44 | return "Invalid data was encountered while parsing the file"; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 45 | case object_error::unexpected_eof: |
| 46 | return "The end of the file was unexpectedly encountered"; |
Rafael Espindola | 6a1bfb2 | 2015-06-29 14:39:25 +0000 | [diff] [blame] | 47 | case object_error::string_table_non_null_end: |
| 48 | return "String table must end with a null terminator"; |
Rafael Espindola | 6def304 | 2015-07-01 12:56:27 +0000 | [diff] [blame] | 49 | case object_error::invalid_section_index: |
| 50 | return "Invalid section index"; |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 51 | case object_error::bitcode_section_not_found: |
| 52 | return "Bitcode section not found in object file"; |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 53 | } |
Rafael Espindola | e107ade | 2013-06-18 13:30:31 +0000 | [diff] [blame] | 54 | llvm_unreachable("An enumerator of object_error does not have a message " |
| 55 | "defined."); |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 58 | char BinaryError::ID = 0; |
| 59 | char GenericBinaryError::ID = 0; |
| 60 | |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 61 | GenericBinaryError::GenericBinaryError(Twine Msg) : Msg(Msg.str()) {} |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 62 | |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 63 | GenericBinaryError::GenericBinaryError(Twine Msg, object_error ECOverride) |
| 64 | : Msg(Msg.str()) { |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 65 | setErrorCode(make_error_code(ECOverride)); |
| 66 | } |
| 67 | |
| 68 | void GenericBinaryError::log(raw_ostream &OS) const { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 69 | OS << Msg; |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Bieneman | 684981e | 2014-09-19 22:09:18 +0000 | [diff] [blame] | 72 | static ManagedStatic<_object_error_category> error_category; |
| 73 | |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 74 | const std::error_category &object::object_category() { |
Chris Bieneman | 684981e | 2014-09-19 22:09:18 +0000 | [diff] [blame] | 75 | return *error_category; |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 76 | } |
Lang Hames | 8a63b2a | 2016-05-17 21:38:53 +0000 | [diff] [blame] | 77 | |
| 78 | llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) { |
| 79 | if (auto Err2 = |
| 80 | handleErrors(std::move(Err), |
| 81 | [](std::unique_ptr<ECError> M) { |
| 82 | // Try to handle 'M'. If successful, return a success value from |
| 83 | // the handler. |
| 84 | if (M->convertToErrorCode() == object_error::invalid_file_type) |
| 85 | return Error::success(); |
| 86 | |
| 87 | // We failed to handle 'M' - return it from the handler. |
| 88 | // This value will be passed back from catchErrors and |
| 89 | // wind up in Err2, where it will be returned from this function. |
| 90 | return Error(std::move(M)); |
| 91 | })) |
| 92 | return Err2; |
| 93 | return Err; |
| 94 | } |