LCOV - code coverage report
Current view: top level - lib/Support - Error.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 22 37 59.5 %
Date: 2018-06-17 00:07:59 Functions: 8 15 53.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #include "llvm/Support/Error.h"
      11             : #include "llvm/ADT/Twine.h"
      12             : #include "llvm/Support/ErrorHandling.h"
      13             : #include "llvm/Support/ManagedStatic.h"
      14             : #include <system_error>
      15             : 
      16             : using namespace llvm;
      17             : 
      18             : namespace {
      19             : 
      20             :   enum class ErrorErrorCode : int {
      21             :     MultipleErrors = 1,
      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        8721 :   class ErrorErrorCategory : public std::error_category {
      29             :   public:
      30           0 :     const char *name() const noexcept override { return "Error"; }
      31             : 
      32           0 :     std::string message(int condition) const override {
      33           0 :       switch (static_cast<ErrorErrorCode>(condition)) {
      34             :       case ErrorErrorCode::MultipleErrors:
      35           0 :         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           0 :                "bug.";
      40             :       }
      41           0 :       llvm_unreachable("Unhandled error code");
      42             :     }
      43             :   };
      44             : 
      45             : }
      46             : 
      47             : static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
      48             : 
      49             : namespace llvm {
      50             : 
      51           0 : void ErrorInfoBase::anchor() {}
      52             : char ErrorInfoBase::ID = 0;
      53             : char ErrorList::ID = 0;
      54             : char ECError::ID = 0;
      55             : char StringError::ID = 0;
      56             : 
      57        1131 : void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
      58        1131 :   if (!E)
      59             :     return;
      60             :   OS << ErrorBanner;
      61        1854 :   handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
      62         495 :     EI.log(OS);
      63         495 :     OS << "\n";
      64         495 :   });
      65             : }
      66             : 
      67             : 
      68           0 : std::error_code ErrorList::convertToErrorCode() const {
      69           0 :   return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
      70           0 :                          *ErrorErrorCat);
      71             : }
      72             : 
      73       39954 : std::error_code inconvertibleErrorCode() {
      74       39954 :   return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
      75       79908 :                          *ErrorErrorCat);
      76             : }
      77             : 
      78       10434 : Error errorCodeToError(std::error_code EC) {
      79       10434 :   if (!EC)
      80             :     return Error::success();
      81             :   return Error(llvm::make_unique<ECError>(ECError(EC)));
      82             : }
      83             : 
      84        4605 : std::error_code errorToErrorCode(Error Err) {
      85             :   std::error_code EC;
      86       13815 :   handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
      87          55 :     EC = EI.convertToErrorCode();
      88             :   });
      89        9210 :   if (EC == inconvertibleErrorCode())
      90           0 :     report_fatal_error(EC.message());
      91        4605 :   return EC;
      92             : }
      93             : 
      94             : #if LLVM_ENABLE_ABI_BREAKING_CHECKS
      95             : void Error::fatalUncheckedError() const {
      96             :   dbgs() << "Program aborted due to an unhandled Error:\n";
      97             :   if (getPtr())
      98             :     getPtr()->log(dbgs());
      99             :   else
     100             :     dbgs() << "Error value was Success. (Note: Success values must still be "
     101             :               "checked prior to being destroyed).\n";
     102             :   abort();
     103             : }
     104             : #endif
     105             : 
     106       35630 : StringError::StringError(const Twine &S, std::error_code EC)
     107       35630 :     : Msg(S.str()), EC(EC) {}
     108             : 
     109        4854 : void StringError::log(raw_ostream &OS) const { OS << Msg; }
     110             : 
     111          45 : std::error_code StringError::convertToErrorCode() const {
     112          45 :   return EC;
     113             : }
     114             : 
     115           0 : void report_fatal_error(Error Err, bool GenCrashDiag) {
     116             :   assert(Err && "report_fatal_error called with success value");
     117             :   std::string ErrMsg;
     118             :   {
     119           0 :     raw_string_ostream ErrStream(ErrMsg);
     120           0 :     logAllUnhandledErrors(std::move(Err), ErrStream, "");
     121             :   }
     122           0 :   report_fatal_error(ErrMsg);
     123             : }
     124             : 
     125             : }
     126             : 
     127             : #ifndef _MSC_VER
     128             : namespace llvm {
     129             : 
     130             : // One of these two variables will be referenced by a symbol defined in
     131             : // llvm-config.h. We provide a link-time (or load time for DSO) failure when
     132             : // there is a mismatch in the build configuration of the API client and LLVM.
     133             : #if LLVM_ENABLE_ABI_BREAKING_CHECKS
     134             : int EnableABIBreakingChecks;
     135             : #else
     136             : int DisableABIBreakingChecks;
     137             : #endif
     138             : 
     139             : } // end namespace llvm
     140             : #endif

Generated by: LCOV version 1.13