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