xref: /freebsd/contrib/llvm-project/llvm/lib/Support/Error.cpp (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 //===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Support/Error.h"
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include <system_error>
13 
14 using namespace llvm;
15 
16 namespace {
17 
18   enum class ErrorErrorCode : int {
19     MultipleErrors = 1,
20     FileError,
21     InconvertibleError
22   };
23 
24   // FIXME: This class is only here to support the transition to llvm::Error. It
25   // will be removed once this transition is complete. Clients should prefer to
26   // deal with the Error value directly, rather than converting to error_code.
27   class ErrorErrorCategory : public std::error_category {
28   public:
29     const char *name() const noexcept override { return "Error"; }
30 
31     std::string message(int condition) const override {
32       switch (static_cast<ErrorErrorCode>(condition)) {
33       case ErrorErrorCode::MultipleErrors:
34         return "Multiple errors";
35       case ErrorErrorCode::InconvertibleError:
36         return "Inconvertible error value. An error has occurred that could "
37                "not be converted to a known std::error_code. Please file a "
38                "bug.";
39       case ErrorErrorCode::FileError:
40           return "A file error occurred.";
41       }
42       llvm_unreachable("Unhandled error code");
43     }
44   };
45 
46 }
47 
48 ErrorErrorCategory &getErrorErrorCat() {
49   static ErrorErrorCategory ErrorErrorCat;
50   return ErrorErrorCat;
51 }
52 
53 namespace llvm {
54 
55 void ErrorInfoBase::anchor() {}
56 char ErrorInfoBase::ID = 0;
57 char ErrorList::ID = 0;
58 void ECError::anchor() {}
59 char ECError::ID = 0;
60 char StringError::ID = 0;
61 char FileError::ID = 0;
62 
63 void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
64   if (!E)
65     return;
66   OS << ErrorBanner;
67   handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
68     EI.log(OS);
69     OS << "\n";
70   });
71 }
72 
73 
74 std::error_code ErrorList::convertToErrorCode() const {
75   return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
76                          getErrorErrorCat());
77 }
78 
79 std::error_code inconvertibleErrorCode() {
80   return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
81                          getErrorErrorCat());
82 }
83 
84 std::error_code FileError::convertToErrorCode() const {
85   std::error_code NestedEC = Err->convertToErrorCode();
86   if (NestedEC == inconvertibleErrorCode())
87     return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
88                            getErrorErrorCat());
89   return NestedEC;
90 }
91 
92 Error errorCodeToError(std::error_code EC) {
93   if (!EC)
94     return Error::success();
95   return Error(std::make_unique<ECError>(ECError(EC)));
96 }
97 
98 std::error_code errorToErrorCode(Error Err) {
99   std::error_code EC;
100   handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
101     EC = EI.convertToErrorCode();
102   });
103   if (EC == inconvertibleErrorCode())
104     report_fatal_error(Twine(EC.message()));
105   return EC;
106 }
107 
108 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
109 void Error::fatalUncheckedError() const {
110   dbgs() << "Program aborted due to an unhandled Error:\n";
111   if (getPtr()) {
112     getPtr()->log(dbgs());
113     dbgs() << "\n";
114   }else
115     dbgs() << "Error value was Success. (Note: Success values must still be "
116               "checked prior to being destroyed).\n";
117   abort();
118 }
119 #endif
120 
121 StringError::StringError(std::error_code EC, const Twine &S)
122     : Msg(S.str()), EC(EC) {}
123 
124 StringError::StringError(const Twine &S, std::error_code EC)
125     : Msg(S.str()), EC(EC), PrintMsgOnly(true) {}
126 
127 void StringError::log(raw_ostream &OS) const {
128   if (PrintMsgOnly) {
129     OS << Msg;
130   } else {
131     OS << EC.message();
132     if (!Msg.empty())
133       OS << (" " + Msg);
134   }
135 }
136 
137 std::error_code StringError::convertToErrorCode() const {
138   return EC;
139 }
140 
141 Error createStringError(std::error_code EC, char const *Msg) {
142   return make_error<StringError>(Msg, EC);
143 }
144 
145 void report_fatal_error(Error Err, bool GenCrashDiag) {
146   assert(Err && "report_fatal_error called with success value");
147   std::string ErrMsg;
148   {
149     raw_string_ostream ErrStream(ErrMsg);
150     logAllUnhandledErrors(std::move(Err), ErrStream);
151   }
152   report_fatal_error(Twine(ErrMsg));
153 }
154 
155 } // end namespace llvm
156 
157 LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err) {
158   return reinterpret_cast<ErrorInfoBase *>(Err)->dynamicClassID();
159 }
160 
161 void LLVMConsumeError(LLVMErrorRef Err) { consumeError(unwrap(Err)); }
162 
163 char *LLVMGetErrorMessage(LLVMErrorRef Err) {
164   std::string Tmp = toString(unwrap(Err));
165   char *ErrMsg = new char[Tmp.size() + 1];
166   memcpy(ErrMsg, Tmp.data(), Tmp.size());
167   ErrMsg[Tmp.size()] = '\0';
168   return ErrMsg;
169 }
170 
171 void LLVMDisposeErrorMessage(char *ErrMsg) { delete[] ErrMsg; }
172 
173 LLVMErrorTypeId LLVMGetStringErrorTypeId() {
174   return reinterpret_cast<void *>(&StringError::ID);
175 }
176 
177 LLVMErrorRef LLVMCreateStringError(const char *ErrMsg) {
178   return wrap(make_error<StringError>(ErrMsg, inconvertibleErrorCode()));
179 }
180