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>
21 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 "
42 llvm_unreachable("Unhandled error code");
48 static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
52 void ErrorInfoBase::anchor() {}
53 char ErrorInfoBase::ID = 0;
54 char ErrorList::ID = 0;
56 char StringError::ID = 0;
58 void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
62 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
69 std::error_code ErrorList::convertToErrorCode() const {
70 return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
74 std::error_code inconvertibleErrorCode() {
75 return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
79 Error errorCodeToError(std::error_code EC) {
81 return Error::success();
82 return Error(llvm::make_unique<ECError>(ECError(EC)));
85 std::error_code errorToErrorCode(Error Err) {
87 handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
88 EC = EI.convertToErrorCode();
90 if (EC == inconvertibleErrorCode())
91 report_fatal_error(EC.message());
95 StringError::StringError(const Twine &S, std::error_code EC)
96 : Msg(S.str()), EC(EC) {}
98 void StringError::log(raw_ostream &OS) const { OS << Msg; }
100 std::error_code StringError::convertToErrorCode() const {
104 void report_fatal_error(Error Err, bool GenCrashDiag) {
105 assert(Err && "report_fatal_error called with success value");
108 raw_string_ostream ErrStream(ErrMsg);
109 logAllUnhandledErrors(std::move(Err), ErrStream, "");
111 report_fatal_error(ErrMsg);
119 // One of these two variables will be referenced by a symbol defined in
120 // llvm-config.h. We provide a link-time (or load time for DSO) failure when
121 // there is a mismatch in the build configuration of the API client and LLVM.
122 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
123 int EnableABIBreakingChecks;
125 int DisableABIBreakingChecks;
128 } // end namespace llvm