Error.cpp 5.0 KB

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