blob: 28d9127777fb4095e66fd62ceca91abd47f3e50b [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"
Vassil Vassilev2ec8b152016-09-14 08:55:18 +000014#include <system_error>
Lang Hamese7aad352016-03-23 23:57:28 +000015
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,
Lang Hamesbd8e9542016-05-27 01:54:25 +000023 InconvertibleError
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 Hamesbd8e9542016-05-27 01:54:25 +000037 case ErrorErrorCode::InconvertibleError:
38 return "Inconvertible error value. An error has occurred that could "
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000039 "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 Hames2b1c0932016-07-04 22:47:53 +000058void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
59 if (!E)
60 return;
61 OS << ErrorBanner;
62 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
63 EI.log(OS);
64 OS << "\n";
65 });
66}
Lang Hamese7aad352016-03-23 23:57:28 +000067
Lang Hamesfc209622016-07-14 02:24:01 +000068
Lang Hamese7aad352016-03-23 23:57:28 +000069std::error_code ErrorList::convertToErrorCode() const {
70 return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
71 *ErrorErrorCat);
72}
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000073
Lang Hamesbd8e9542016-05-27 01:54:25 +000074std::error_code inconvertibleErrorCode() {
75 return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000076 *ErrorErrorCat);
77}
78
79Error errorCodeToError(std::error_code EC) {
80 if (!EC)
81 return Error::success();
82 return Error(llvm::make_unique<ECError>(ECError(EC)));
83}
84
85std::error_code errorToErrorCode(Error Err) {
86 std::error_code EC;
87 handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
88 EC = EI.convertToErrorCode();
89 });
Lang Hamesbd8e9542016-05-27 01:54:25 +000090 if (EC == inconvertibleErrorCode())
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000091 report_fatal_error(EC.message());
92 return EC;
93}
94
95StringError::StringError(const Twine &S, std::error_code EC)
96 : Msg(S.str()), EC(EC) {}
97
98void StringError::log(raw_ostream &OS) const { OS << Msg; }
99
100std::error_code StringError::convertToErrorCode() const {
101 return EC;
102}
103
Lang Hamesfc209622016-07-14 02:24:01 +0000104void report_fatal_error(Error Err, bool GenCrashDiag) {
105 assert(Err && "report_fatal_error called with success value");
106 std::string ErrMsg;
107 {
108 raw_string_ostream ErrStream(ErrMsg);
109 logAllUnhandledErrors(std::move(Err), ErrStream, "");
110 }
111 report_fatal_error(ErrMsg);
112}
113
Lang Hamesc5e0bbd2016-05-27 01:37:32 +0000114}