Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 1 | //===----- lib/Support/Error.cpp - Error and associated utilities ---------===// |
| 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 | #include "llvm/Support/Error.h" |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Twine.h" |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ErrorHandling.h" |
| 13 | #include "llvm/Support/ManagedStatic.h" |
Vassil Vassilev | 2ec8b15 | 2016-09-14 08:55:18 +0000 | [diff] [blame] | 14 | #include <system_error> |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | namespace { |
| 19 | |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame] | 20 | enum class ErrorErrorCode : int { |
| 21 | MultipleErrors = 1, |
Lang Hames | bd8e954 | 2016-05-27 01:54:25 +0000 | [diff] [blame] | 22 | InconvertibleError |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 23 | }; |
| 24 | |
Peter Collingbourne | 4718f8b | 2016-05-24 20:13:46 +0000 | [diff] [blame] | 25 | // FIXME: This class is only here to support the transition to llvm::Error. It |
| 26 | // will be removed once this transition is complete. Clients should prefer to |
| 27 | // deal with the Error value directly, rather than converting to error_code. |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 28 | class ErrorErrorCategory : public std::error_category { |
| 29 | public: |
Reid Kleckner | 990504e | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 30 | const char *name() const noexcept override { return "Error"; } |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 31 | |
| 32 | std::string message(int condition) const override { |
| 33 | switch (static_cast<ErrorErrorCode>(condition)) { |
| 34 | case ErrorErrorCode::MultipleErrors: |
| 35 | return "Multiple errors"; |
Lang Hames | bd8e954 | 2016-05-27 01:54:25 +0000 | [diff] [blame] | 36 | case ErrorErrorCode::InconvertibleError: |
| 37 | return "Inconvertible error value. An error has occurred that could " |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame] | 38 | "not be converted to a known std::error_code. Please file a " |
| 39 | "bug."; |
| 40 | } |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 41 | llvm_unreachable("Unhandled error code"); |
| 42 | } |
| 43 | }; |
| 44 | |
NAKAMURA Takumi | d8c1be6 | 2016-03-24 15:19:39 +0000 | [diff] [blame] | 45 | } |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 46 | |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame] | 47 | static ManagedStatic<ErrorErrorCategory> ErrorErrorCat; |
| 48 | |
| 49 | namespace llvm { |
| 50 | |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 51 | void ErrorInfoBase::anchor() {} |
| 52 | char ErrorInfoBase::ID = 0; |
Reid Kleckner | a15b76b | 2016-03-24 23:49:34 +0000 | [diff] [blame] | 53 | char ErrorList::ID = 0; |
| 54 | char ECError::ID = 0; |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame] | 55 | char StringError::ID = 0; |
NAKAMURA Takumi | e6d29c9 | 2016-03-24 15:26:43 +0000 | [diff] [blame] | 56 | |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 57 | void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) { |
Lang Hames | 2b1c093 | 2016-07-04 22:47:53 +0000 | [diff] [blame] | 58 | if (!E) |
| 59 | return; |
| 60 | OS << ErrorBanner; |
| 61 | handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) { |
| 62 | EI.log(OS); |
| 63 | OS << "\n"; |
| 64 | }); |
| 65 | } |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 66 | |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 67 | |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 68 | std::error_code ErrorList::convertToErrorCode() const { |
| 69 | return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors), |
| 70 | *ErrorErrorCat); |
| 71 | } |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame] | 72 | |
Lang Hames | bd8e954 | 2016-05-27 01:54:25 +0000 | [diff] [blame] | 73 | std::error_code inconvertibleErrorCode() { |
| 74 | return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError), |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame] | 75 | *ErrorErrorCat); |
| 76 | } |
| 77 | |
| 78 | Error errorCodeToError(std::error_code EC) { |
| 79 | if (!EC) |
| 80 | return Error::success(); |
| 81 | return Error(llvm::make_unique<ECError>(ECError(EC))); |
| 82 | } |
| 83 | |
| 84 | std::error_code errorToErrorCode(Error Err) { |
| 85 | std::error_code EC; |
| 86 | handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) { |
| 87 | EC = EI.convertToErrorCode(); |
| 88 | }); |
Lang Hames | bd8e954 | 2016-05-27 01:54:25 +0000 | [diff] [blame] | 89 | if (EC == inconvertibleErrorCode()) |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame] | 90 | report_fatal_error(EC.message()); |
| 91 | return EC; |
| 92 | } |
| 93 | |
Zachary Turner | 18f21a4 | 2017-11-09 19:31:52 +0000 | [diff] [blame] | 94 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
| 95 | void Error::fatalUncheckedError() const { |
| 96 | dbgs() << "Program aborted due to an unhandled Error:\n"; |
| 97 | if (getPtr()) |
| 98 | getPtr()->log(dbgs()); |
| 99 | else |
| 100 | dbgs() << "Error value was Success. (Note: Success values must still be " |
| 101 | "checked prior to being destroyed).\n"; |
| 102 | abort(); |
| 103 | } |
| 104 | #endif |
| 105 | |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame] | 106 | StringError::StringError(const Twine &S, std::error_code EC) |
| 107 | : Msg(S.str()), EC(EC) {} |
| 108 | |
| 109 | void StringError::log(raw_ostream &OS) const { OS << Msg; } |
| 110 | |
| 111 | std::error_code StringError::convertToErrorCode() const { |
| 112 | return EC; |
| 113 | } |
| 114 | |
Victor Leschuk | e58e990 | 2018-07-26 02:21:40 +0000 | [diff] [blame^] | 115 | Error createStringError(std::error_code EC, char const *Msg) { |
| 116 | return make_error<StringError>(Msg, EC); |
| 117 | } |
| 118 | |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 119 | void report_fatal_error(Error Err, bool GenCrashDiag) { |
| 120 | assert(Err && "report_fatal_error called with success value"); |
| 121 | std::string ErrMsg; |
| 122 | { |
| 123 | raw_string_ostream ErrStream(ErrMsg); |
| 124 | logAllUnhandledErrors(std::move(Err), ErrStream, ""); |
| 125 | } |
| 126 | report_fatal_error(ErrMsg); |
| 127 | } |
| 128 | |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame] | 129 | } |
Mehdi Amini | 28dd54c | 2016-11-28 22:23:53 +0000 | [diff] [blame] | 130 | |
| 131 | #ifndef _MSC_VER |
| 132 | namespace llvm { |
| 133 | |
| 134 | // One of these two variables will be referenced by a symbol defined in |
| 135 | // llvm-config.h. We provide a link-time (or load time for DSO) failure when |
| 136 | // there is a mismatch in the build configuration of the API client and LLVM. |
| 137 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
| 138 | int EnableABIBreakingChecks; |
| 139 | #else |
| 140 | int DisableABIBreakingChecks; |
| 141 | #endif |
| 142 | |
| 143 | } // end namespace llvm |
NAKAMURA Takumi | b5cb3e5 | 2016-11-29 17:32:43 +0000 | [diff] [blame] | 144 | #endif |