1e7aad357SLang Hames //===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
2e7aad357SLang Hames //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e7aad357SLang Hames //
7e7aad357SLang Hames //===----------------------------------------------------------------------===//
8e7aad357SLang Hames 
9e7aad357SLang Hames #include "llvm/Support/Error.h"
10c5e0bbd7SLang Hames #include "llvm/ADT/Twine.h"
11e7aad357SLang Hames #include "llvm/Support/ErrorHandling.h"
12e7aad357SLang Hames #include "llvm/Support/ManagedStatic.h"
132ec8b150SVassil Vassilev #include <system_error>
14e7aad357SLang Hames 
15e7aad357SLang Hames using namespace llvm;
16e7aad357SLang Hames 
17e7aad357SLang Hames namespace {
18e7aad357SLang Hames 
19c5e0bbd7SLang Hames   enum class ErrorErrorCode : int {
20c5e0bbd7SLang Hames     MultipleErrors = 1,
21e11f2217SAlexandre Ganea     FileError,
22bd8e9542SLang Hames     InconvertibleError
23e7aad357SLang Hames   };
24e7aad357SLang Hames 
254718f8b5SPeter Collingbourne   // FIXME: This class is only here to support the transition to llvm::Error. It
264718f8b5SPeter Collingbourne   // will be removed once this transition is complete. Clients should prefer to
274718f8b5SPeter Collingbourne   // deal with the Error value directly, rather than converting to error_code.
28e7aad357SLang Hames   class ErrorErrorCategory : public std::error_category {
29e7aad357SLang Hames   public:
30990504e6SReid Kleckner     const char *name() const noexcept override { return "Error"; }
31e7aad357SLang Hames 
32e7aad357SLang Hames     std::string message(int condition) const override {
33e7aad357SLang Hames       switch (static_cast<ErrorErrorCode>(condition)) {
34e7aad357SLang Hames       case ErrorErrorCode::MultipleErrors:
35e7aad357SLang Hames         return "Multiple errors";
36bd8e9542SLang Hames       case ErrorErrorCode::InconvertibleError:
37bd8e9542SLang Hames         return "Inconvertible error value. An error has occurred that could "
38c5e0bbd7SLang Hames                "not be converted to a known std::error_code. Please file a "
39c5e0bbd7SLang Hames                "bug.";
40e11f2217SAlexandre Ganea       case ErrorErrorCode::FileError:
41e11f2217SAlexandre Ganea           return "A file error occurred.";
42c5e0bbd7SLang Hames       }
43e7aad357SLang Hames       llvm_unreachable("Unhandled error code");
44e7aad357SLang Hames     }
45e7aad357SLang Hames   };
46e7aad357SLang Hames 
47d8c1be66SNAKAMURA Takumi }
48e7aad357SLang Hames 
49c5e0bbd7SLang Hames static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
50c5e0bbd7SLang Hames 
51c5e0bbd7SLang Hames namespace llvm {
52c5e0bbd7SLang Hames 
53e7aad357SLang Hames void ErrorInfoBase::anchor() {}
54e7aad357SLang Hames char ErrorInfoBase::ID = 0;
55a15b76b3SReid Kleckner char ErrorList::ID = 0;
5690004149SDavid Bolvansky void ECError::anchor() {}
57a15b76b3SReid Kleckner char ECError::ID = 0;
58c5e0bbd7SLang Hames char StringError::ID = 0;
59e11f2217SAlexandre Ganea char FileError::ID = 0;
60e6d29c99SNAKAMURA Takumi 
6141a9ee98SZachary Turner void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
622b1c093cSLang Hames   if (!E)
632b1c093cSLang Hames     return;
642b1c093cSLang Hames   OS << ErrorBanner;
652b1c093cSLang Hames   handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
662b1c093cSLang Hames     EI.log(OS);
672b1c093cSLang Hames     OS << "\n";
682b1c093cSLang Hames   });
692b1c093cSLang Hames }
70e7aad357SLang Hames 
71fc209623SLang Hames 
72e7aad357SLang Hames std::error_code ErrorList::convertToErrorCode() const {
73e7aad357SLang Hames   return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
74e7aad357SLang Hames                          *ErrorErrorCat);
75e7aad357SLang Hames }
76c5e0bbd7SLang Hames 
77bd8e9542SLang Hames std::error_code inconvertibleErrorCode() {
78bd8e9542SLang Hames   return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
79c5e0bbd7SLang Hames                          *ErrorErrorCat);
80c5e0bbd7SLang Hames }
81c5e0bbd7SLang Hames 
82e11f2217SAlexandre Ganea std::error_code FileError::convertToErrorCode() const {
83e11f2217SAlexandre Ganea   return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
84e11f2217SAlexandre Ganea                          *ErrorErrorCat);
85e11f2217SAlexandre Ganea }
86e11f2217SAlexandre Ganea 
87c5e0bbd7SLang Hames Error errorCodeToError(std::error_code EC) {
88c5e0bbd7SLang Hames   if (!EC)
89c5e0bbd7SLang Hames     return Error::success();
90*0eaee545SJonas Devlieghere   return Error(std::make_unique<ECError>(ECError(EC)));
91c5e0bbd7SLang Hames }
92c5e0bbd7SLang Hames 
93c5e0bbd7SLang Hames std::error_code errorToErrorCode(Error Err) {
94c5e0bbd7SLang Hames   std::error_code EC;
95c5e0bbd7SLang Hames   handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
96c5e0bbd7SLang Hames     EC = EI.convertToErrorCode();
97c5e0bbd7SLang Hames   });
98bd8e9542SLang Hames   if (EC == inconvertibleErrorCode())
99c5e0bbd7SLang Hames     report_fatal_error(EC.message());
100c5e0bbd7SLang Hames   return EC;
101c5e0bbd7SLang Hames }
102c5e0bbd7SLang Hames 
10318f21a48SZachary Turner #if LLVM_ENABLE_ABI_BREAKING_CHECKS
10418f21a48SZachary Turner void Error::fatalUncheckedError() const {
10518f21a48SZachary Turner   dbgs() << "Program aborted due to an unhandled Error:\n";
10618f21a48SZachary Turner   if (getPtr())
10718f21a48SZachary Turner     getPtr()->log(dbgs());
10818f21a48SZachary Turner   else
10918f21a48SZachary Turner     dbgs() << "Error value was Success. (Note: Success values must still be "
11018f21a48SZachary Turner               "checked prior to being destroyed).\n";
11118f21a48SZachary Turner   abort();
11218f21a48SZachary Turner }
11318f21a48SZachary Turner #endif
11418f21a48SZachary Turner 
115e11f2217SAlexandre Ganea StringError::StringError(std::error_code EC, const Twine &S)
116c5e0bbd7SLang Hames     : Msg(S.str()), EC(EC) {}
117c5e0bbd7SLang Hames 
118e11f2217SAlexandre Ganea StringError::StringError(const Twine &S, std::error_code EC)
119e11f2217SAlexandre Ganea     : Msg(S.str()), EC(EC), PrintMsgOnly(true) {}
120e11f2217SAlexandre Ganea 
121e11f2217SAlexandre Ganea void StringError::log(raw_ostream &OS) const {
122e11f2217SAlexandre Ganea   if (PrintMsgOnly) {
123e11f2217SAlexandre Ganea     OS << Msg;
124e11f2217SAlexandre Ganea   } else {
125e11f2217SAlexandre Ganea     OS << EC.message();
126e11f2217SAlexandre Ganea     if (!Msg.empty())
127e11f2217SAlexandre Ganea       OS << (" " + Msg);
128e11f2217SAlexandre Ganea   }
129e11f2217SAlexandre Ganea }
130c5e0bbd7SLang Hames 
131c5e0bbd7SLang Hames std::error_code StringError::convertToErrorCode() const {
132c5e0bbd7SLang Hames   return EC;
133c5e0bbd7SLang Hames }
134c5e0bbd7SLang Hames 
135e58e9907SVictor Leschuk Error createStringError(std::error_code EC, char const *Msg) {
136e58e9907SVictor Leschuk   return make_error<StringError>(Msg, EC);
137e58e9907SVictor Leschuk }
138e58e9907SVictor Leschuk 
139fc209623SLang Hames void report_fatal_error(Error Err, bool GenCrashDiag) {
140fc209623SLang Hames   assert(Err && "report_fatal_error called with success value");
141fc209623SLang Hames   std::string ErrMsg;
142fc209623SLang Hames   {
143fc209623SLang Hames     raw_string_ostream ErrStream(ErrMsg);
14445eb84f3SJonas Devlieghere     logAllUnhandledErrors(std::move(Err), ErrStream);
145fc209623SLang Hames   }
146fc209623SLang Hames   report_fatal_error(ErrMsg);
147fc209623SLang Hames }
148fc209623SLang Hames 
14900fb14daSLang Hames } // end namespace llvm
15000fb14daSLang Hames 
15100fb14daSLang Hames LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err) {
15200fb14daSLang Hames   return reinterpret_cast<ErrorInfoBase *>(Err)->dynamicClassID();
15300fb14daSLang Hames }
15400fb14daSLang Hames 
15500fb14daSLang Hames void LLVMConsumeError(LLVMErrorRef Err) { consumeError(unwrap(Err)); }
15600fb14daSLang Hames 
15700fb14daSLang Hames char *LLVMGetErrorMessage(LLVMErrorRef Err) {
15800fb14daSLang Hames   std::string Tmp = toString(unwrap(Err));
15900fb14daSLang Hames   char *ErrMsg = new char[Tmp.size() + 1];
16000fb14daSLang Hames   memcpy(ErrMsg, Tmp.data(), Tmp.size());
16100fb14daSLang Hames   ErrMsg[Tmp.size()] = '\0';
16200fb14daSLang Hames   return ErrMsg;
16300fb14daSLang Hames }
16400fb14daSLang Hames 
16500fb14daSLang Hames void LLVMDisposeErrorMessage(char *ErrMsg) { delete[] ErrMsg; }
16600fb14daSLang Hames 
16700fb14daSLang Hames LLVMErrorTypeId LLVMGetStringErrorTypeId() {
16800fb14daSLang Hames   return reinterpret_cast<void *>(&StringError::ID);
169c5e0bbd7SLang Hames }
17028dd54c3SMehdi Amini 
17128dd54c3SMehdi Amini #ifndef _MSC_VER
17228dd54c3SMehdi Amini namespace llvm {
17328dd54c3SMehdi Amini 
17428dd54c3SMehdi Amini // One of these two variables will be referenced by a symbol defined in
17528dd54c3SMehdi Amini // llvm-config.h. We provide a link-time (or load time for DSO) failure when
17628dd54c3SMehdi Amini // there is a mismatch in the build configuration of the API client and LLVM.
17728dd54c3SMehdi Amini #if LLVM_ENABLE_ABI_BREAKING_CHECKS
17828dd54c3SMehdi Amini int EnableABIBreakingChecks;
17928dd54c3SMehdi Amini #else
18028dd54c3SMehdi Amini int DisableABIBreakingChecks;
18128dd54c3SMehdi Amini #endif
18228dd54c3SMehdi Amini 
18328dd54c3SMehdi Amini } // end namespace llvm
18428dd54c3SMehdi Amini #endif
185