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