blob: bb5c31a2a35b174a6a0d67f272f7e6ac10c3b1ef [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
16using namespace llvm;
17
18namespace {
19
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000020 enum class ErrorErrorCode : int {
21 MultipleErrors = 1,
Alexandre Ganeae11f2212018-08-30 13:10:42 +000022 FileError,
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:
Reid Kleckner990504e2016-10-19 23:52:38 +000031 const char *name() const noexcept override { return "Error"; }
Lang Hamese7aad352016-03-23 23:57:28 +000032
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.";
Alexandre Ganeae11f2212018-08-30 13:10:42 +000041 case ErrorErrorCode::FileError:
42 return "A file error occurred.";
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000043 }
Lang Hamese7aad352016-03-23 23:57:28 +000044 llvm_unreachable("Unhandled error code");
45 }
46 };
47
NAKAMURA Takumid8c1be62016-03-24 15:19:39 +000048}
Lang Hamese7aad352016-03-23 23:57:28 +000049
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000050static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
51
52namespace llvm {
53
Lang Hamese7aad352016-03-23 23:57:28 +000054void ErrorInfoBase::anchor() {}
55char ErrorInfoBase::ID = 0;
Reid Klecknera15b76b2016-03-24 23:49:34 +000056char ErrorList::ID = 0;
Richard Trieua87b70d2018-12-29 02:02:13 +000057void ECError::anchor() {};
Reid Klecknera15b76b2016-03-24 23:49:34 +000058char ECError::ID = 0;
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000059char StringError::ID = 0;
Alexandre Ganeae11f2212018-08-30 13:10:42 +000060char FileError::ID = 0;
NAKAMURA Takumie6d29c92016-03-24 15:26:43 +000061
Zachary Turner41a9ee92017-10-11 23:54:34 +000062void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
Lang Hames2b1c0932016-07-04 22:47:53 +000063 if (!E)
64 return;
65 OS << ErrorBanner;
66 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
67 EI.log(OS);
68 OS << "\n";
69 });
70}
Lang Hamese7aad352016-03-23 23:57:28 +000071
Lang Hamesfc209622016-07-14 02:24:01 +000072
Lang Hamese7aad352016-03-23 23:57:28 +000073std::error_code ErrorList::convertToErrorCode() const {
74 return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
75 *ErrorErrorCat);
76}
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000077
Lang Hamesbd8e9542016-05-27 01:54:25 +000078std::error_code inconvertibleErrorCode() {
79 return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000080 *ErrorErrorCat);
81}
82
Alexandre Ganeae11f2212018-08-30 13:10:42 +000083std::error_code FileError::convertToErrorCode() const {
84 return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
85 *ErrorErrorCat);
86}
87
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000088Error errorCodeToError(std::error_code EC) {
89 if (!EC)
90 return Error::success();
91 return Error(llvm::make_unique<ECError>(ECError(EC)));
92}
93
94std::error_code errorToErrorCode(Error Err) {
95 std::error_code EC;
96 handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
97 EC = EI.convertToErrorCode();
98 });
Lang Hamesbd8e9542016-05-27 01:54:25 +000099 if (EC == inconvertibleErrorCode())
Lang Hamesc5e0bbd2016-05-27 01:37:32 +0000100 report_fatal_error(EC.message());
101 return EC;
102}
103
Zachary Turner18f21a42017-11-09 19:31:52 +0000104#if LLVM_ENABLE_ABI_BREAKING_CHECKS
105void Error::fatalUncheckedError() const {
106 dbgs() << "Program aborted due to an unhandled Error:\n";
107 if (getPtr())
108 getPtr()->log(dbgs());
109 else
110 dbgs() << "Error value was Success. (Note: Success values must still be "
111 "checked prior to being destroyed).\n";
112 abort();
113}
114#endif
115
Alexandre Ganeae11f2212018-08-30 13:10:42 +0000116StringError::StringError(std::error_code EC, const Twine &S)
Lang Hamesc5e0bbd2016-05-27 01:37:32 +0000117 : Msg(S.str()), EC(EC) {}
118
Alexandre Ganeae11f2212018-08-30 13:10:42 +0000119StringError::StringError(const Twine &S, std::error_code EC)
120 : Msg(S.str()), EC(EC), PrintMsgOnly(true) {}
121
122void StringError::log(raw_ostream &OS) const {
123 if (PrintMsgOnly) {
124 OS << Msg;
125 } else {
126 OS << EC.message();
127 if (!Msg.empty())
128 OS << (" " + Msg);
129 }
130}
Lang Hamesc5e0bbd2016-05-27 01:37:32 +0000131
132std::error_code StringError::convertToErrorCode() const {
133 return EC;
134}
135
Victor Leschuke58e9902018-07-26 02:21:40 +0000136Error createStringError(std::error_code EC, char const *Msg) {
137 return make_error<StringError>(Msg, EC);
138}
139
Lang Hamesfc209622016-07-14 02:24:01 +0000140void report_fatal_error(Error Err, bool GenCrashDiag) {
141 assert(Err && "report_fatal_error called with success value");
142 std::string ErrMsg;
143 {
144 raw_string_ostream ErrStream(ErrMsg);
Jonas Devlieghere45eb84f2018-11-11 01:46:03 +0000145 logAllUnhandledErrors(std::move(Err), ErrStream);
Lang Hamesfc209622016-07-14 02:24:01 +0000146 }
147 report_fatal_error(ErrMsg);
148}
149
Lang Hames00fb14d2018-08-15 18:42:11 +0000150} // end namespace llvm
151
152LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err) {
153 return reinterpret_cast<ErrorInfoBase *>(Err)->dynamicClassID();
154}
155
156void LLVMConsumeError(LLVMErrorRef Err) { consumeError(unwrap(Err)); }
157
158char *LLVMGetErrorMessage(LLVMErrorRef Err) {
159 std::string Tmp = toString(unwrap(Err));
160 char *ErrMsg = new char[Tmp.size() + 1];
161 memcpy(ErrMsg, Tmp.data(), Tmp.size());
162 ErrMsg[Tmp.size()] = '\0';
163 return ErrMsg;
164}
165
166void LLVMDisposeErrorMessage(char *ErrMsg) { delete[] ErrMsg; }
167
168LLVMErrorTypeId LLVMGetStringErrorTypeId() {
169 return reinterpret_cast<void *>(&StringError::ID);
Lang Hamesc5e0bbd2016-05-27 01:37:32 +0000170}
Mehdi Amini28dd54c2016-11-28 22:23:53 +0000171
172#ifndef _MSC_VER
173namespace llvm {
174
175// One of these two variables will be referenced by a symbol defined in
176// llvm-config.h. We provide a link-time (or load time for DSO) failure when
177// there is a mismatch in the build configuration of the API client and LLVM.
178#if LLVM_ENABLE_ABI_BREAKING_CHECKS
179int EnableABIBreakingChecks;
180#else
181int DisableABIBreakingChecks;
182#endif
183
184} // end namespace llvm
NAKAMURA Takumib5cb3e52016-11-29 17:32:43 +0000185#endif