blob: 6a8189596dac679aeade5e5b53fa9d54d0b3b3d3 [file] [log] [blame]
Lang Hamese7aad352016-03-23 23:57:28 +00001//===----- 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 Hamesc5e0bbd2016-05-27 01:37:32 +000011
12#include "llvm/ADT/Twine.h"
Lang Hamese7aad352016-03-23 23:57:28 +000013#include "llvm/Support/ErrorHandling.h"
14#include "llvm/Support/ManagedStatic.h"
15
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000016
Lang Hamese7aad352016-03-23 23:57:28 +000017using namespace llvm;
18
19namespace {
20
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000021 enum class ErrorErrorCode : int {
22 MultipleErrors = 1,
23 UnconvertibleError
Lang Hamese7aad352016-03-23 23:57:28 +000024 };
25
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000026 // 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 Hamese7aad352016-03-23 23:57:28 +000029 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 Hamesc5e0bbd2016-05-27 01:37:32 +000037 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 Hamese7aad352016-03-23 23:57:28 +000042 llvm_unreachable("Unhandled error code");
43 }
44 };
45
NAKAMURA Takumid8c1be62016-03-24 15:19:39 +000046}
Lang Hamese7aad352016-03-23 23:57:28 +000047
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000048static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
49
50namespace llvm {
51
Lang Hamese7aad352016-03-23 23:57:28 +000052void ErrorInfoBase::anchor() {}
53char ErrorInfoBase::ID = 0;
Reid Klecknera15b76b2016-03-24 23:49:34 +000054char ErrorList::ID = 0;
55char ECError::ID = 0;
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000056char StringError::ID = 0;
NAKAMURA Takumie6d29c92016-03-24 15:26:43 +000057
Lang Hamese7aad352016-03-23 23:57:28 +000058
59std::error_code ErrorList::convertToErrorCode() const {
60 return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
61 *ErrorErrorCat);
62}
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000063
64std::error_code unconvertibleErrorCode() {
65 return std::error_code(static_cast<int>(ErrorErrorCode::UnconvertibleError),
66 *ErrorErrorCat);
67}
68
69Error errorCodeToError(std::error_code EC) {
70 if (!EC)
71 return Error::success();
72 return Error(llvm::make_unique<ECError>(ECError(EC)));
73}
74
75std::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
85StringError::StringError(const Twine &S, std::error_code EC)
86 : Msg(S.str()), EC(EC) {}
87
88void StringError::log(raw_ostream &OS) const { OS << Msg; }
89
90std::error_code StringError::convertToErrorCode() const {
91 return EC;
92}
93
94}