1*9880d681SAndroid Build Coastguard Worker //===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Error.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Twine.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ManagedStatic.h"
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker using namespace llvm;
17*9880d681SAndroid Build Coastguard Worker
18*9880d681SAndroid Build Coastguard Worker namespace {
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker enum class ErrorErrorCode : int {
21*9880d681SAndroid Build Coastguard Worker MultipleErrors = 1,
22*9880d681SAndroid Build Coastguard Worker InconvertibleError
23*9880d681SAndroid Build Coastguard Worker };
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker // FIXME: This class is only here to support the transition to llvm::Error. It
26*9880d681SAndroid Build Coastguard Worker // will be removed once this transition is complete. Clients should prefer to
27*9880d681SAndroid Build Coastguard Worker // deal with the Error value directly, rather than converting to error_code.
28*9880d681SAndroid Build Coastguard Worker class ErrorErrorCategory : public std::error_category {
29*9880d681SAndroid Build Coastguard Worker public:
name() const30*9880d681SAndroid Build Coastguard Worker const char *name() const LLVM_NOEXCEPT override { return "Error"; }
31*9880d681SAndroid Build Coastguard Worker
message(int condition) const32*9880d681SAndroid Build Coastguard Worker std::string message(int condition) const override {
33*9880d681SAndroid Build Coastguard Worker switch (static_cast<ErrorErrorCode>(condition)) {
34*9880d681SAndroid Build Coastguard Worker case ErrorErrorCode::MultipleErrors:
35*9880d681SAndroid Build Coastguard Worker return "Multiple errors";
36*9880d681SAndroid Build Coastguard Worker case ErrorErrorCode::InconvertibleError:
37*9880d681SAndroid Build Coastguard Worker return "Inconvertible error value. An error has occurred that could "
38*9880d681SAndroid Build Coastguard Worker "not be converted to a known std::error_code. Please file a "
39*9880d681SAndroid Build Coastguard Worker "bug.";
40*9880d681SAndroid Build Coastguard Worker }
41*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unhandled error code");
42*9880d681SAndroid Build Coastguard Worker }
43*9880d681SAndroid Build Coastguard Worker };
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker }
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker namespace llvm {
50*9880d681SAndroid Build Coastguard Worker
anchor()51*9880d681SAndroid Build Coastguard Worker void ErrorInfoBase::anchor() {}
52*9880d681SAndroid Build Coastguard Worker char ErrorInfoBase::ID = 0;
53*9880d681SAndroid Build Coastguard Worker char ErrorList::ID = 0;
54*9880d681SAndroid Build Coastguard Worker char ECError::ID = 0;
55*9880d681SAndroid Build Coastguard Worker char StringError::ID = 0;
56*9880d681SAndroid Build Coastguard Worker
logAllUnhandledErrors(Error E,raw_ostream & OS,Twine ErrorBanner)57*9880d681SAndroid Build Coastguard Worker void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
58*9880d681SAndroid Build Coastguard Worker if (!E)
59*9880d681SAndroid Build Coastguard Worker return;
60*9880d681SAndroid Build Coastguard Worker OS << ErrorBanner;
61*9880d681SAndroid Build Coastguard Worker handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
62*9880d681SAndroid Build Coastguard Worker EI.log(OS);
63*9880d681SAndroid Build Coastguard Worker OS << "\n";
64*9880d681SAndroid Build Coastguard Worker });
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker
convertToErrorCode() const68*9880d681SAndroid Build Coastguard Worker std::error_code ErrorList::convertToErrorCode() const {
69*9880d681SAndroid Build Coastguard Worker return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
70*9880d681SAndroid Build Coastguard Worker *ErrorErrorCat);
71*9880d681SAndroid Build Coastguard Worker }
72*9880d681SAndroid Build Coastguard Worker
inconvertibleErrorCode()73*9880d681SAndroid Build Coastguard Worker std::error_code inconvertibleErrorCode() {
74*9880d681SAndroid Build Coastguard Worker return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
75*9880d681SAndroid Build Coastguard Worker *ErrorErrorCat);
76*9880d681SAndroid Build Coastguard Worker }
77*9880d681SAndroid Build Coastguard Worker
errorCodeToError(std::error_code EC)78*9880d681SAndroid Build Coastguard Worker Error errorCodeToError(std::error_code EC) {
79*9880d681SAndroid Build Coastguard Worker if (!EC)
80*9880d681SAndroid Build Coastguard Worker return Error::success();
81*9880d681SAndroid Build Coastguard Worker return Error(llvm::make_unique<ECError>(ECError(EC)));
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker
errorToErrorCode(Error Err)84*9880d681SAndroid Build Coastguard Worker std::error_code errorToErrorCode(Error Err) {
85*9880d681SAndroid Build Coastguard Worker std::error_code EC;
86*9880d681SAndroid Build Coastguard Worker handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
87*9880d681SAndroid Build Coastguard Worker EC = EI.convertToErrorCode();
88*9880d681SAndroid Build Coastguard Worker });
89*9880d681SAndroid Build Coastguard Worker if (EC == inconvertibleErrorCode())
90*9880d681SAndroid Build Coastguard Worker report_fatal_error(EC.message());
91*9880d681SAndroid Build Coastguard Worker return EC;
92*9880d681SAndroid Build Coastguard Worker }
93*9880d681SAndroid Build Coastguard Worker
StringError(const Twine & S,std::error_code EC)94*9880d681SAndroid Build Coastguard Worker StringError::StringError(const Twine &S, std::error_code EC)
95*9880d681SAndroid Build Coastguard Worker : Msg(S.str()), EC(EC) {}
96*9880d681SAndroid Build Coastguard Worker
log(raw_ostream & OS) const97*9880d681SAndroid Build Coastguard Worker void StringError::log(raw_ostream &OS) const { OS << Msg; }
98*9880d681SAndroid Build Coastguard Worker
convertToErrorCode() const99*9880d681SAndroid Build Coastguard Worker std::error_code StringError::convertToErrorCode() const {
100*9880d681SAndroid Build Coastguard Worker return EC;
101*9880d681SAndroid Build Coastguard Worker }
102*9880d681SAndroid Build Coastguard Worker
report_fatal_error(Error Err,bool GenCrashDiag)103*9880d681SAndroid Build Coastguard Worker void report_fatal_error(Error Err, bool GenCrashDiag) {
104*9880d681SAndroid Build Coastguard Worker assert(Err && "report_fatal_error called with success value");
105*9880d681SAndroid Build Coastguard Worker std::string ErrMsg;
106*9880d681SAndroid Build Coastguard Worker {
107*9880d681SAndroid Build Coastguard Worker raw_string_ostream ErrStream(ErrMsg);
108*9880d681SAndroid Build Coastguard Worker logAllUnhandledErrors(std::move(Err), ErrStream, "");
109*9880d681SAndroid Build Coastguard Worker }
110*9880d681SAndroid Build Coastguard Worker report_fatal_error(ErrMsg);
111*9880d681SAndroid Build Coastguard Worker }
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker }
114