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 | |
| 12 | #include "llvm/ADT/Twine.h" |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 13 | #include "llvm/Support/ErrorHandling.h" |
| 14 | #include "llvm/Support/ManagedStatic.h" |
| 15 | |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame^] | 16 | |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame^] | 21 | enum class ErrorErrorCode : int { |
| 22 | MultipleErrors = 1, |
| 23 | UnconvertibleError |
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: |
| 31 | const char *name() const LLVM_NOEXCEPT override { return "Error"; } |
| 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 | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame^] | 37 | case ErrorErrorCode::UnconvertibleError: |
| 38 | return "Unconvertible error value. An error has occurred that could " |
| 39 | "not be converted to a known std::error_code. Please file a " |
| 40 | "bug."; |
| 41 | } |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 42 | llvm_unreachable("Unhandled error code"); |
| 43 | } |
| 44 | }; |
| 45 | |
NAKAMURA Takumi | d8c1be6 | 2016-03-24 15:19:39 +0000 | [diff] [blame] | 46 | } |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 47 | |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame^] | 48 | static ManagedStatic<ErrorErrorCategory> ErrorErrorCat; |
| 49 | |
| 50 | namespace llvm { |
| 51 | |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 52 | void ErrorInfoBase::anchor() {} |
| 53 | char ErrorInfoBase::ID = 0; |
Reid Kleckner | a15b76b | 2016-03-24 23:49:34 +0000 | [diff] [blame] | 54 | char ErrorList::ID = 0; |
| 55 | char ECError::ID = 0; |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame^] | 56 | char StringError::ID = 0; |
NAKAMURA Takumi | e6d29c9 | 2016-03-24 15:26:43 +0000 | [diff] [blame] | 57 | |
Lang Hames | e7aad35 | 2016-03-23 23:57:28 +0000 | [diff] [blame] | 58 | |
| 59 | std::error_code ErrorList::convertToErrorCode() const { |
| 60 | return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors), |
| 61 | *ErrorErrorCat); |
| 62 | } |
Lang Hames | c5e0bbd | 2016-05-27 01:37:32 +0000 | [diff] [blame^] | 63 | |
| 64 | std::error_code unconvertibleErrorCode() { |
| 65 | return std::error_code(static_cast<int>(ErrorErrorCode::UnconvertibleError), |
| 66 | *ErrorErrorCat); |
| 67 | } |
| 68 | |
| 69 | Error errorCodeToError(std::error_code EC) { |
| 70 | if (!EC) |
| 71 | return Error::success(); |
| 72 | return Error(llvm::make_unique<ECError>(ECError(EC))); |
| 73 | } |
| 74 | |
| 75 | std::error_code errorToErrorCode(Error Err) { |
| 76 | std::error_code EC; |
| 77 | handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) { |
| 78 | EC = EI.convertToErrorCode(); |
| 79 | }); |
| 80 | if (EC == unconvertibleErrorCode()) |
| 81 | report_fatal_error(EC.message()); |
| 82 | return EC; |
| 83 | } |
| 84 | |
| 85 | StringError::StringError(const Twine &S, std::error_code EC) |
| 86 | : Msg(S.str()), EC(EC) {} |
| 87 | |
| 88 | void StringError::log(raw_ostream &OS) const { OS << Msg; } |
| 89 | |
| 90 | std::error_code StringError::convertToErrorCode() const { |
| 91 | return EC; |
| 92 | } |
| 93 | |
| 94 | } |