blob: df540d10bc5b30e3b4fe71f837d59082ec018a77 [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#include "llvm/ADT/Twine.h"
Lang Hamese7aad352016-03-23 23:57:28 +000012#include "llvm/Support/ErrorHandling.h"
13#include "llvm/Support/ManagedStatic.h"
14
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000015
Lang Hamese7aad352016-03-23 23:57:28 +000016using namespace llvm;
17
18namespace {
19
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000020 enum class ErrorErrorCode : int {
21 MultipleErrors = 1,
Lang Hamesbd8e9542016-05-27 01:54:25 +000022 InconvertibleError
Lang Hamese7aad352016-03-23 23:57:28 +000023 };
24
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000025 // 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 Hamese7aad352016-03-23 23:57:28 +000028 class ErrorErrorCategory : public std::error_category {
29 public:
30 const char *name() const LLVM_NOEXCEPT override { return "Error"; }
31
32 std::string message(int condition) const override {
33 switch (static_cast<ErrorErrorCode>(condition)) {
34 case ErrorErrorCode::MultipleErrors:
35 return "Multiple errors";
Lang Hamesbd8e9542016-05-27 01:54:25 +000036 case ErrorErrorCode::InconvertibleError:
37 return "Inconvertible error value. An error has occurred that could "
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000038 "not be converted to a known std::error_code. Please file a "
39 "bug.";
40 }
Lang Hamese7aad352016-03-23 23:57:28 +000041 llvm_unreachable("Unhandled error code");
42 }
43 };
44
NAKAMURA Takumid8c1be62016-03-24 15:19:39 +000045}
Lang Hamese7aad352016-03-23 23:57:28 +000046
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000047static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
48
49namespace llvm {
50
Lang Hamese7aad352016-03-23 23:57:28 +000051void ErrorInfoBase::anchor() {}
52char ErrorInfoBase::ID = 0;
Reid Klecknera15b76b2016-03-24 23:49:34 +000053char ErrorList::ID = 0;
54char ECError::ID = 0;
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000055char StringError::ID = 0;
NAKAMURA Takumie6d29c92016-03-24 15:26:43 +000056
Lang Hames2b1c0932016-07-04 22:47:53 +000057void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
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 Hamese7aad352016-03-23 23:57:28 +000066
67std::error_code ErrorList::convertToErrorCode() const {
68 return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
69 *ErrorErrorCat);
70}
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000071
Lang Hamesbd8e9542016-05-27 01:54:25 +000072std::error_code inconvertibleErrorCode() {
73 return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000074 *ErrorErrorCat);
75}
76
77Error errorCodeToError(std::error_code EC) {
78 if (!EC)
79 return Error::success();
80 return Error(llvm::make_unique<ECError>(ECError(EC)));
81}
82
83std::error_code errorToErrorCode(Error Err) {
84 std::error_code EC;
85 handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
86 EC = EI.convertToErrorCode();
87 });
Lang Hamesbd8e9542016-05-27 01:54:25 +000088 if (EC == inconvertibleErrorCode())
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000089 report_fatal_error(EC.message());
90 return EC;
91}
92
93StringError::StringError(const Twine &S, std::error_code EC)
94 : Msg(S.str()), EC(EC) {}
95
96void StringError::log(raw_ostream &OS) const { OS << Msg; }
97
98std::error_code StringError::convertToErrorCode() const {
99 return EC;
100}
101
102}