blob: 72bc08af2ddb51de3bc530a92982142487524d4d [file] [log] [blame]
Lang Hamese7aad352016-03-23 23:57:28 +00001//===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Lang Hamese7aad352016-03-23 23:57:28 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/Error.h"
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000010#include "llvm/ADT/Twine.h"
Lang Hamese7aad352016-03-23 23:57:28 +000011#include "llvm/Support/ErrorHandling.h"
12#include "llvm/Support/ManagedStatic.h"
Vassil Vassilev2ec8b152016-09-14 08:55:18 +000013#include <system_error>
Lang Hamese7aad352016-03-23 23:57:28 +000014
15using namespace llvm;
16
17namespace {
18
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000019 enum class ErrorErrorCode : int {
20 MultipleErrors = 1,
Alexandre Ganeae11f2212018-08-30 13:10:42 +000021 FileError,
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:
Reid Kleckner990504e2016-10-19 23:52:38 +000030 const char *name() const noexcept override { return "Error"; }
Lang Hamese7aad352016-03-23 23:57:28 +000031
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.";
Alexandre Ganeae11f2212018-08-30 13:10:42 +000040 case ErrorErrorCode::FileError:
41 return "A file error occurred.";
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000042 }
Lang Hamese7aad352016-03-23 23:57:28 +000043 llvm_unreachable("Unhandled error code");
44 }
45 };
46
NAKAMURA Takumid8c1be62016-03-24 15:19:39 +000047}
Lang Hamese7aad352016-03-23 23:57:28 +000048
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000049static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
50
51namespace llvm {
52
Lang Hamese7aad352016-03-23 23:57:28 +000053void ErrorInfoBase::anchor() {}
54char ErrorInfoBase::ID = 0;
Reid Klecknera15b76b2016-03-24 23:49:34 +000055char ErrorList::ID = 0;
David Bolvansky900041492018-12-30 13:18:17 +000056void ECError::anchor() {}
Reid Klecknera15b76b2016-03-24 23:49:34 +000057char ECError::ID = 0;
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000058char StringError::ID = 0;
Alexandre Ganeae11f2212018-08-30 13:10:42 +000059char FileError::ID = 0;
NAKAMURA Takumie6d29c92016-03-24 15:26:43 +000060
Zachary Turner41a9ee92017-10-11 23:54:34 +000061void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
Lang Hames2b1c0932016-07-04 22:47:53 +000062 if (!E)
63 return;
64 OS << ErrorBanner;
65 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
66 EI.log(OS);
67 OS << "\n";
68 });
69}
Lang Hamese7aad352016-03-23 23:57:28 +000070
Lang Hamesfc209622016-07-14 02:24:01 +000071
Lang Hamese7aad352016-03-23 23:57:28 +000072std::error_code ErrorList::convertToErrorCode() const {
73 return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
74 *ErrorErrorCat);
75}
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000076
Lang Hamesbd8e9542016-05-27 01:54:25 +000077std::error_code inconvertibleErrorCode() {
78 return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000079 *ErrorErrorCat);
80}
81
Alexandre Ganeae11f2212018-08-30 13:10:42 +000082std::error_code FileError::convertToErrorCode() const {
83 return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
84 *ErrorErrorCat);
85}
86
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000087Error errorCodeToError(std::error_code EC) {
88 if (!EC)
89 return Error::success();
90 return Error(llvm::make_unique<ECError>(ECError(EC)));
91}
92
93std::error_code errorToErrorCode(Error Err) {
94 std::error_code EC;
95 handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
96 EC = EI.convertToErrorCode();
97 });
Lang Hamesbd8e9542016-05-27 01:54:25 +000098 if (EC == inconvertibleErrorCode())
Lang Hamesc5e0bbd2016-05-27 01:37:32 +000099 report_fatal_error(EC.message());
100 return EC;
101}
102
Zachary Turner18f21a42017-11-09 19:31:52 +0000103#if LLVM_ENABLE_ABI_BREAKING_CHECKS
104void Error::fatalUncheckedError() const {
105 dbgs() << "Program aborted due to an unhandled Error:\n";
106 if (getPtr())
107 getPtr()->log(dbgs());
108 else
109 dbgs() << "Error value was Success. (Note: Success values must still be "
110 "checked prior to being destroyed).\n";
111 abort();
112}
113#endif
114
Alexandre Ganeae11f2212018-08-30 13:10:42 +0000115StringError::StringError(std::error_code EC, const Twine &S)
Lang Hamesc5e0bbd2016-05-27 01:37:32 +0000116 : Msg(S.str()), EC(EC) {}
117
Alexandre Ganeae11f2212018-08-30 13:10:42 +0000118StringError::StringError(const Twine &S, std::error_code EC)
119 : Msg(S.str()), EC(EC), PrintMsgOnly(true) {}
120
121void StringError::log(raw_ostream &OS) const {
122 if (PrintMsgOnly) {
123 OS << Msg;
124 } else {
125 OS << EC.message();
126 if (!Msg.empty())
127 OS << (" " + Msg);
128 }
129}
Lang Hamesc5e0bbd2016-05-27 01:37:32 +0000130
131std::error_code StringError::convertToErrorCode() const {
132 return EC;
133}
134
Victor Leschuke58e9902018-07-26 02:21:40 +0000135Error createStringError(std::error_code EC, char const *Msg) {
136 return make_error<StringError>(Msg, EC);
137}
138
Lang Hamesfc209622016-07-14 02:24:01 +0000139void report_fatal_error(Error Err, bool GenCrashDiag) {
140 assert(Err && "report_fatal_error called with success value");
141 std::string ErrMsg;
142 {
143 raw_string_ostream ErrStream(ErrMsg);
Jonas Devlieghere45eb84f2018-11-11 01:46:03 +0000144 logAllUnhandledErrors(std::move(Err), ErrStream);
Lang Hamesfc209622016-07-14 02:24:01 +0000145 }
146 report_fatal_error(ErrMsg);
147}
148
Lang Hames00fb14d2018-08-15 18:42:11 +0000149} // end namespace llvm
150
151LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err) {
152 return reinterpret_cast<ErrorInfoBase *>(Err)->dynamicClassID();
153}
154
155void LLVMConsumeError(LLVMErrorRef Err) { consumeError(unwrap(Err)); }
156
157char *LLVMGetErrorMessage(LLVMErrorRef Err) {
158 std::string Tmp = toString(unwrap(Err));
159 char *ErrMsg = new char[Tmp.size() + 1];
160 memcpy(ErrMsg, Tmp.data(), Tmp.size());
161 ErrMsg[Tmp.size()] = '\0';
162 return ErrMsg;
163}
164
165void LLVMDisposeErrorMessage(char *ErrMsg) { delete[] ErrMsg; }
166
167LLVMErrorTypeId LLVMGetStringErrorTypeId() {
168 return reinterpret_cast<void *>(&StringError::ID);
Lang Hamesc5e0bbd2016-05-27 01:37:32 +0000169}
Mehdi Amini28dd54c2016-11-28 22:23:53 +0000170
171#ifndef _MSC_VER
172namespace llvm {
173
174// One of these two variables will be referenced by a symbol defined in
175// llvm-config.h. We provide a link-time (or load time for DSO) failure when
176// there is a mismatch in the build configuration of the API client and LLVM.
177#if LLVM_ENABLE_ABI_BREAKING_CHECKS
178int EnableABIBreakingChecks;
179#else
180int DisableABIBreakingChecks;
181#endif
182
183} // end namespace llvm
NAKAMURA Takumib5cb3e52016-11-29 17:32:43 +0000184#endif