Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- Error.h - system_error extensions for Object -------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This declares a new error_category for the Object library. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_OBJECT_ERROR_H |
| 14 | #define LLVM_OBJECT_ERROR_H |
| 15 | |
| 16 | #include "llvm/ADT/Twine.h" |
| 17 | #include "llvm/Support/Error.h" |
| 18 | #include <system_error> |
| 19 | |
| 20 | namespace llvm { |
| 21 | namespace object { |
| 22 | |
| 23 | class Binary; |
| 24 | |
| 25 | const std::error_category &object_category(); |
| 26 | |
| 27 | enum class object_error { |
| 28 | // Error code 0 is absent. Use std::error_code() instead. |
| 29 | arch_not_found = 1, |
| 30 | invalid_file_type, |
| 31 | parse_failed, |
| 32 | unexpected_eof, |
| 33 | string_table_non_null_end, |
| 34 | invalid_section_index, |
| 35 | bitcode_section_not_found, |
| 36 | invalid_symbol_index, |
| 37 | }; |
| 38 | |
| 39 | inline std::error_code make_error_code(object_error e) { |
| 40 | return std::error_code(static_cast<int>(e), object_category()); |
| 41 | } |
| 42 | |
| 43 | /// Base class for all errors indicating malformed binary files. |
| 44 | /// |
| 45 | /// Having a subclass for all malformed binary files allows archive-walking |
| 46 | /// code to skip malformed files without having to understand every possible |
| 47 | /// way that a binary file might be malformed. |
| 48 | /// |
| 49 | /// Currently inherits from ECError for easy interoperability with |
| 50 | /// std::error_code, but this will be removed in the future. |
| 51 | class BinaryError : public ErrorInfo<BinaryError, ECError> { |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 52 | virtual void anchor(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 53 | public: |
| 54 | static char ID; |
| 55 | BinaryError() { |
| 56 | // Default to parse_failed, can be overridden with setErrorCode. |
| 57 | setErrorCode(make_error_code(object_error::parse_failed)); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | /// Generic binary error. |
| 62 | /// |
| 63 | /// For errors that don't require their own specific sub-error (most errors) |
| 64 | /// this class can be used to describe the error via a string message. |
| 65 | class GenericBinaryError : public ErrorInfo<GenericBinaryError, BinaryError> { |
| 66 | public: |
| 67 | static char ID; |
| 68 | GenericBinaryError(Twine Msg); |
| 69 | GenericBinaryError(Twine Msg, object_error ECOverride); |
| 70 | const std::string &getMessage() const { return Msg; } |
| 71 | void log(raw_ostream &OS) const override; |
| 72 | private: |
| 73 | std::string Msg; |
| 74 | }; |
| 75 | |
| 76 | /// isNotObjectErrorInvalidFileType() is used when looping through the children |
| 77 | /// of an archive after calling getAsBinary() on the child and it returns an |
| 78 | /// llvm::Error. In the cases we want to loop through the children and ignore the |
| 79 | /// non-objects in the archive this is used to test the error to see if an |
| 80 | /// error() function needs to called on the llvm::Error. |
| 81 | Error isNotObjectErrorInvalidFileType(llvm::Error Err); |
| 82 | |
| 83 | } // end namespace object. |
| 84 | |
| 85 | } // end namespace llvm. |
| 86 | |
| 87 | namespace std { |
| 88 | template <> |
| 89 | struct is_error_code_enum<llvm::object::object_error> : std::true_type {}; |
| 90 | } |
| 91 | |
| 92 | #endif |