1e7aad357SLang Hames //===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
2e7aad357SLang Hames //
3e7aad357SLang Hames //                     The LLVM Compiler Infrastructure
4e7aad357SLang Hames //
5e7aad357SLang Hames // This file is distributed under the University of Illinois Open Source
6e7aad357SLang Hames // License. See LICENSE.TXT for details.
7e7aad357SLang Hames //
8e7aad357SLang Hames //===----------------------------------------------------------------------===//
9e7aad357SLang Hames 
10e7aad357SLang Hames #include "llvm/Support/Error.h"
11c5e0bbd7SLang Hames #include "llvm/ADT/Twine.h"
12e7aad357SLang Hames #include "llvm/Support/ErrorHandling.h"
13e7aad357SLang Hames #include "llvm/Support/ManagedStatic.h"
142ec8b150SVassil Vassilev #include <system_error>
15e7aad357SLang Hames 
16c5e0bbd7SLang Hames 
17e7aad357SLang Hames using namespace llvm;
18e7aad357SLang Hames 
19e7aad357SLang Hames namespace {
20e7aad357SLang Hames 
21c5e0bbd7SLang Hames   enum class ErrorErrorCode : int {
22c5e0bbd7SLang Hames     MultipleErrors = 1,
23bd8e9542SLang Hames     InconvertibleError
24e7aad357SLang Hames   };
25e7aad357SLang Hames 
264718f8b5SPeter Collingbourne   // FIXME: This class is only here to support the transition to llvm::Error. It
274718f8b5SPeter Collingbourne   // will be removed once this transition is complete. Clients should prefer to
284718f8b5SPeter Collingbourne   // deal with the Error value directly, rather than converting to error_code.
29e7aad357SLang Hames   class ErrorErrorCategory : public std::error_category {
30e7aad357SLang Hames   public:
31990504e6SReid Kleckner     const char *name() const noexcept override { return "Error"; }
32e7aad357SLang Hames 
33e7aad357SLang Hames     std::string message(int condition) const override {
34e7aad357SLang Hames       switch (static_cast<ErrorErrorCode>(condition)) {
35e7aad357SLang Hames       case ErrorErrorCode::MultipleErrors:
36e7aad357SLang Hames         return "Multiple errors";
37bd8e9542SLang Hames       case ErrorErrorCode::InconvertibleError:
38bd8e9542SLang Hames         return "Inconvertible error value. An error has occurred that could "
39c5e0bbd7SLang Hames                "not be converted to a known std::error_code. Please file a "
40c5e0bbd7SLang Hames                "bug.";
41c5e0bbd7SLang Hames       }
42e7aad357SLang Hames       llvm_unreachable("Unhandled error code");
43e7aad357SLang Hames     }
44e7aad357SLang Hames   };
45e7aad357SLang Hames 
46d8c1be66SNAKAMURA Takumi }
47e7aad357SLang Hames 
48c5e0bbd7SLang Hames static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
49c5e0bbd7SLang Hames 
50c5e0bbd7SLang Hames namespace llvm {
51c5e0bbd7SLang Hames 
52e7aad357SLang Hames void ErrorInfoBase::anchor() {}
53e7aad357SLang Hames char ErrorInfoBase::ID = 0;
54a15b76b3SReid Kleckner char ErrorList::ID = 0;
55a15b76b3SReid Kleckner char ECError::ID = 0;
56c5e0bbd7SLang Hames char StringError::ID = 0;
57e6d29c99SNAKAMURA Takumi 
582b1c093cSLang Hames void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
592b1c093cSLang Hames   if (!E)
602b1c093cSLang Hames     return;
612b1c093cSLang Hames   OS << ErrorBanner;
622b1c093cSLang Hames   handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
632b1c093cSLang Hames     EI.log(OS);
642b1c093cSLang Hames     OS << "\n";
652b1c093cSLang Hames   });
662b1c093cSLang Hames }
67e7aad357SLang Hames 
68fc209623SLang Hames 
69e7aad357SLang Hames std::error_code ErrorList::convertToErrorCode() const {
70e7aad357SLang Hames   return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
71e7aad357SLang Hames                          *ErrorErrorCat);
72e7aad357SLang Hames }
73c5e0bbd7SLang Hames 
74bd8e9542SLang Hames std::error_code inconvertibleErrorCode() {
75bd8e9542SLang Hames   return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
76c5e0bbd7SLang Hames                          *ErrorErrorCat);
77c5e0bbd7SLang Hames }
78c5e0bbd7SLang Hames 
79c5e0bbd7SLang Hames Error errorCodeToError(std::error_code EC) {
80c5e0bbd7SLang Hames   if (!EC)
81c5e0bbd7SLang Hames     return Error::success();
82c5e0bbd7SLang Hames   return Error(llvm::make_unique<ECError>(ECError(EC)));
83c5e0bbd7SLang Hames }
84c5e0bbd7SLang Hames 
85c5e0bbd7SLang Hames std::error_code errorToErrorCode(Error Err) {
86c5e0bbd7SLang Hames   std::error_code EC;
87c5e0bbd7SLang Hames   handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
88c5e0bbd7SLang Hames     EC = EI.convertToErrorCode();
89c5e0bbd7SLang Hames   });
90bd8e9542SLang Hames   if (EC == inconvertibleErrorCode())
91c5e0bbd7SLang Hames     report_fatal_error(EC.message());
92c5e0bbd7SLang Hames   return EC;
93c5e0bbd7SLang Hames }
94c5e0bbd7SLang Hames 
95c5e0bbd7SLang Hames StringError::StringError(const Twine &S, std::error_code EC)
96c5e0bbd7SLang Hames     : Msg(S.str()), EC(EC) {}
97c5e0bbd7SLang Hames 
98c5e0bbd7SLang Hames void StringError::log(raw_ostream &OS) const { OS << Msg; }
99c5e0bbd7SLang Hames 
100c5e0bbd7SLang Hames std::error_code StringError::convertToErrorCode() const {
101c5e0bbd7SLang Hames   return EC;
102c5e0bbd7SLang Hames }
103c5e0bbd7SLang Hames 
104fc209623SLang Hames void report_fatal_error(Error Err, bool GenCrashDiag) {
105fc209623SLang Hames   assert(Err && "report_fatal_error called with success value");
106fc209623SLang Hames   std::string ErrMsg;
107fc209623SLang Hames   {
108fc209623SLang Hames     raw_string_ostream ErrStream(ErrMsg);
109fc209623SLang Hames     logAllUnhandledErrors(std::move(Err), ErrStream, "");
110fc209623SLang Hames   }
111fc209623SLang Hames   report_fatal_error(ErrMsg);
112fc209623SLang Hames }
113fc209623SLang Hames 
114c5e0bbd7SLang Hames }
115*28dd54c3SMehdi Amini 
116*28dd54c3SMehdi Amini #ifndef _MSC_VER
117*28dd54c3SMehdi Amini namespace llvm {
118*28dd54c3SMehdi Amini 
119*28dd54c3SMehdi Amini // One of these two variables will be referenced by a symbol defined in
120*28dd54c3SMehdi Amini // llvm-config.h. We provide a link-time (or load time for DSO) failure when
121*28dd54c3SMehdi Amini // there is a mismatch in the build configuration of the API client and LLVM.
122*28dd54c3SMehdi Amini #if LLVM_ENABLE_ABI_BREAKING_CHECKS
123*28dd54c3SMehdi Amini int EnableABIBreakingChecks;
124*28dd54c3SMehdi Amini #else
125*28dd54c3SMehdi Amini int DisableABIBreakingChecks;
126*28dd54c3SMehdi Amini #endif
127*28dd54c3SMehdi Amini 
128*28dd54c3SMehdi Amini } // end namespace llvm
129*28dd54c3SMehdi Amini #endif