1 //===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Error.h"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include <system_error>
20 enum class ErrorErrorCode
: int {
26 // 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.
29 class ErrorErrorCategory
: public std::error_category
{
31 const char *name() const noexcept override
{ return "Error"; }
33 std::string
message(int condition
) const override
{
34 switch (static_cast<ErrorErrorCode
>(condition
)) {
35 case ErrorErrorCode::MultipleErrors
:
36 return "Multiple errors";
37 case ErrorErrorCode::InconvertibleError
:
38 return "Inconvertible error value. An error has occurred that could "
39 "not be converted to a known std::error_code. Please file a "
41 case ErrorErrorCode::FileError
:
42 return "A file error occurred.";
44 llvm_unreachable("Unhandled error code");
50 ErrorErrorCategory
&getErrorErrorCat() {
51 static ErrorErrorCategory ErrorErrorCat
;
57 void ErrorInfoBase::anchor() {}
58 char ErrorInfoBase::ID
= 0;
59 char ErrorList::ID
= 0;
60 void ECError::anchor() {}
62 char StringError::ID
= 0;
63 char FileError::ID
= 0;
65 void logAllUnhandledErrors(Error E
, raw_ostream
&OS
, Twine ErrorBanner
) {
69 handleAllErrors(std::move(E
), [&](const ErrorInfoBase
&EI
) {
75 /// Write all error messages (if any) in E to a string. The newline character
76 /// is used to separate error messages.
77 std::string
toString(Error E
) {
78 SmallVector
<std::string
, 2> Errors
;
79 handleAllErrors(std::move(E
), [&Errors
](const ErrorInfoBase
&EI
) {
80 Errors
.push_back(EI
.message());
82 return join(Errors
.begin(), Errors
.end(), "\n");
85 std::string
toStringWithoutConsuming(const Error
&E
) {
86 SmallVector
<std::string
, 2> Errors
;
87 visitErrors(E
, [&Errors
](const ErrorInfoBase
&EI
) {
88 Errors
.push_back(EI
.message());
90 return join(Errors
.begin(), Errors
.end(), "\n");
93 std::error_code
ErrorList::convertToErrorCode() const {
94 return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors
),
98 std::error_code
inconvertibleErrorCode() {
99 return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError
),
103 std::error_code
FileError::convertToErrorCode() const {
104 std::error_code NestedEC
= Err
->convertToErrorCode();
105 if (NestedEC
== inconvertibleErrorCode())
106 return std::error_code(static_cast<int>(ErrorErrorCode::FileError
),
111 Error
errorCodeToError(std::error_code EC
) {
113 return Error::success();
114 return Error(std::make_unique
<ECError
>(ECError(EC
)));
117 std::error_code
errorToErrorCode(Error Err
) {
119 handleAllErrors(std::move(Err
), [&](const ErrorInfoBase
&EI
) {
120 EC
= EI
.convertToErrorCode();
122 if (EC
== inconvertibleErrorCode())
123 report_fatal_error(Twine(EC
.message()));
127 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
128 void Error::fatalUncheckedError() const {
129 dbgs() << "Program aborted due to an unhandled Error:\n";
131 getPtr()->log(dbgs());
134 dbgs() << "Error value was Success. (Note: Success values must still be "
135 "checked prior to being destroyed).\n";
140 StringError::StringError(std::error_code EC
, const Twine
&S
)
141 : Msg(S
.str()), EC(EC
) {}
143 StringError::StringError(const Twine
&S
, std::error_code EC
)
144 : Msg(S
.str()), EC(EC
), PrintMsgOnly(true) {}
146 StringError::StringError(std::string
&&S
, std::error_code EC
, bool PrintMsgOnly
)
147 : Msg(S
), EC(EC
), PrintMsgOnly(PrintMsgOnly
) {}
149 void StringError::log(raw_ostream
&OS
) const {
159 std::error_code
StringError::convertToErrorCode() const {
163 Error
createStringError(std::string
&&Msg
, std::error_code EC
) {
164 return make_error
<StringError
>(Msg
, EC
);
167 void report_fatal_error(Error Err
, bool GenCrashDiag
) {
168 assert(Err
&& "report_fatal_error called with success value");
171 raw_string_ostream
ErrStream(ErrMsg
);
172 logAllUnhandledErrors(std::move(Err
), ErrStream
);
174 report_fatal_error(Twine(ErrMsg
), GenCrashDiag
);
177 } // end namespace llvm
179 LLVMErrorTypeId
LLVMGetErrorTypeId(LLVMErrorRef Err
) {
180 return reinterpret_cast<ErrorInfoBase
*>(Err
)->dynamicClassID();
183 void LLVMConsumeError(LLVMErrorRef Err
) { consumeError(unwrap(Err
)); }
187 void LLVMCantFail(LLVMErrorRef Err
) {
188 cantFail(unwrap(Err
));
191 char *LLVMGetErrorMessage(LLVMErrorRef Err
) {
192 std::string Tmp
= toString(unwrap(Err
));
193 char *ErrMsg
= new char[Tmp
.size() + 1];
194 memcpy(ErrMsg
, Tmp
.data(), Tmp
.size());
195 ErrMsg
[Tmp
.size()] = '\0';
199 void LLVMDisposeErrorMessage(char *ErrMsg
) { delete[] ErrMsg
; }
201 LLVMErrorTypeId
LLVMGetStringErrorTypeId() {
202 return reinterpret_cast<void *>(&StringError::ID
);
205 LLVMErrorRef
LLVMCreateStringError(const char *ErrMsg
) {
206 return wrap(make_error
<StringError
>(ErrMsg
, inconvertibleErrorCode()));