Error.cpp 5.0 KB

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