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