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"
20 enum class ErrorErrorCode : int {
25 // 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.
28 class ErrorErrorCategory : public std::error_category {
30 const char *name() const LLVM_NOEXCEPT override { return "Error"; }
32 std::string message(int condition) const override {
33 switch (static_cast<ErrorErrorCode>(condition)) {
34 case ErrorErrorCode::MultipleErrors:
35 return "Multiple errors";
36 case ErrorErrorCode::InconvertibleError:
37 return "Inconvertible error value. An error has occurred that could "
38 "not be converted to a known std::error_code. Please file a "
41 llvm_unreachable("Unhandled error code");
47 static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
51 void ErrorInfoBase::anchor() {}
52 char ErrorInfoBase::ID = 0;
53 char ErrorList::ID = 0;
55 char StringError::ID = 0;
57 void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
61 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
68 std::error_code ErrorList::convertToErrorCode() const {
69 return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
73 std::error_code inconvertibleErrorCode() {
74 return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
78 Error errorCodeToError(std::error_code EC) {
80 return Error::success();
81 return Error(llvm::make_unique<ECError>(ECError(EC)));
84 std::error_code errorToErrorCode(Error Err) {
86 handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
87 EC = EI.convertToErrorCode();
89 if (EC == inconvertibleErrorCode())
90 report_fatal_error(EC.message());
94 StringError::StringError(const Twine &S, std::error_code EC)
95 : Msg(S.str()), EC(EC) {}
97 void StringError::log(raw_ostream &OS) const { OS << Msg; }
99 std::error_code StringError::convertToErrorCode() const {
103 void report_fatal_error(Error Err, bool GenCrashDiag) {
104 assert(Err && "report_fatal_error called with success value");
107 raw_string_ostream ErrStream(ErrMsg);
108 logAllUnhandledErrors(std::move(Err), ErrStream, "");
110 report_fatal_error(ErrMsg);