1 //===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Error.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/ErrorHandling.h"
13 #include "llvm/Support/ManagedStatic.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 static ManagedStatic
<ErrorErrorCategory
> ErrorErrorCat
;
54 void ErrorInfoBase::anchor() {}
55 char ErrorInfoBase::ID
= 0;
56 char ErrorList::ID
= 0;
57 void ECError::anchor() {}
59 char StringError::ID
= 0;
60 char FileError::ID
= 0;
62 void logAllUnhandledErrors(Error E
, raw_ostream
&OS
, Twine ErrorBanner
) {
66 handleAllErrors(std::move(E
), [&](const ErrorInfoBase
&EI
) {
73 std::error_code
ErrorList::convertToErrorCode() const {
74 return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors
),
78 std::error_code
inconvertibleErrorCode() {
79 return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError
),
83 std::error_code
FileError::convertToErrorCode() const {
84 return std::error_code(static_cast<int>(ErrorErrorCode::FileError
),
88 Error
errorCodeToError(std::error_code EC
) {
90 return Error::success();
91 return Error(llvm::make_unique
<ECError
>(ECError(EC
)));
94 std::error_code
errorToErrorCode(Error Err
) {
96 handleAllErrors(std::move(Err
), [&](const ErrorInfoBase
&EI
) {
97 EC
= EI
.convertToErrorCode();
99 if (EC
== inconvertibleErrorCode())
100 report_fatal_error(EC
.message());
104 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
105 void Error::fatalUncheckedError() const {
106 dbgs() << "Program aborted due to an unhandled Error:\n";
108 getPtr()->log(dbgs());
110 dbgs() << "Error value was Success. (Note: Success values must still be "
111 "checked prior to being destroyed).\n";
116 StringError::StringError(std::error_code EC
, const Twine
&S
)
117 : Msg(S
.str()), EC(EC
) {}
119 StringError::StringError(const Twine
&S
, std::error_code EC
)
120 : Msg(S
.str()), EC(EC
), PrintMsgOnly(true) {}
122 void StringError::log(raw_ostream
&OS
) const {
132 std::error_code
StringError::convertToErrorCode() const {
136 Error
createStringError(std::error_code EC
, char const *Msg
) {
137 return make_error
<StringError
>(Msg
, EC
);
140 void report_fatal_error(Error Err
, bool GenCrashDiag
) {
141 assert(Err
&& "report_fatal_error called with success value");
144 raw_string_ostream
ErrStream(ErrMsg
);
145 logAllUnhandledErrors(std::move(Err
), ErrStream
);
147 report_fatal_error(ErrMsg
);
150 } // end namespace llvm
152 LLVMErrorTypeId
LLVMGetErrorTypeId(LLVMErrorRef Err
) {
153 return reinterpret_cast<ErrorInfoBase
*>(Err
)->dynamicClassID();
156 void LLVMConsumeError(LLVMErrorRef Err
) { consumeError(unwrap(Err
)); }
158 char *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';
166 void LLVMDisposeErrorMessage(char *ErrMsg
) { delete[] ErrMsg
; }
168 LLVMErrorTypeId
LLVMGetStringErrorTypeId() {
169 return reinterpret_cast<void *>(&StringError::ID
);
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
179 int EnableABIBreakingChecks
;
181 int DisableABIBreakingChecks
;
184 } // end namespace llvm