LLVM  4.0.0
Support/Error.cpp
Go to the documentation of this file.
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"
14 #include <system_error>
15 
16 
17 using namespace llvm;
18 
19 namespace {
20 
21  enum class ErrorErrorCode : int {
22  MultipleErrors = 1,
23  InconvertibleError
24  };
25 
26  // FIXME: This class is only here to support the transition to llvm::Error. It
27  // will be removed once this transition is complete. Clients should prefer to
28  // deal with the Error value directly, rather than converting to error_code.
29  class ErrorErrorCategory : public std::error_category {
30  public:
31  const char *name() const noexcept override { return "Error"; }
32 
33  std::string message(int condition) const override {
34  switch (static_cast<ErrorErrorCode>(condition)) {
35  case ErrorErrorCode::MultipleErrors:
36  return "Multiple errors";
37  case ErrorErrorCode::InconvertibleError:
38  return "Inconvertible error value. An error has occurred that could "
39  "not be converted to a known std::error_code. Please file a "
40  "bug.";
41  }
42  llvm_unreachable("Unhandled error code");
43  }
44  };
45 
46 }
47 
49 
50 namespace llvm {
51 
52 void ErrorInfoBase::anchor() {}
53 char ErrorInfoBase::ID = 0;
54 char ErrorList::ID = 0;
55 char ECError::ID = 0;
56 char StringError::ID = 0;
57 
58 void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
59  if (!E)
60  return;
61  OS << ErrorBanner;
62  handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
63  EI.log(OS);
64  OS << "\n";
65  });
66 }
67 
68 
69 std::error_code ErrorList::convertToErrorCode() const {
70  return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
71  *ErrorErrorCat);
72 }
73 
74 std::error_code inconvertibleErrorCode() {
75  return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
76  *ErrorErrorCat);
77 }
78 
79 Error errorCodeToError(std::error_code EC) {
80  if (!EC)
81  return Error::success();
82  return Error(llvm::make_unique<ECError>(ECError(EC)));
83 }
84 
85 std::error_code errorToErrorCode(Error Err) {
86  std::error_code EC;
87  handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
88  EC = EI.convertToErrorCode();
89  });
90  if (EC == inconvertibleErrorCode())
91  report_fatal_error(EC.message());
92  return EC;
93 }
94 
95 StringError::StringError(const Twine &S, std::error_code EC)
96  : Msg(S.str()), EC(EC) {}
97 
98 void StringError::log(raw_ostream &OS) const { OS << Msg; }
99 
100 std::error_code StringError::convertToErrorCode() const {
101  return EC;
102 }
103 
104 void report_fatal_error(Error Err, bool GenCrashDiag) {
105  assert(Err && "report_fatal_error called with success value");
106  std::string ErrMsg;
107  {
108  raw_string_ostream ErrStream(ErrMsg);
109  logAllUnhandledErrors(std::move(Err), ErrStream, "");
110  }
111  report_fatal_error(ErrMsg);
112 }
113 
114 }
115 
116 #ifndef _MSC_VER
117 namespace llvm {
118 
119 // One of these two variables will be referenced by a symbol defined in
120 // llvm-config.h. We provide a link-time (or load time for DSO) failure when
121 // there is a mismatch in the build configuration of the API client and LLVM.
122 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
123 int EnableABIBreakingChecks;
124 #else
126 #endif
127 
128 } // end namespace llvm
129 #endif
int DisableABIBreakingChecks
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Base class for error info classes.
Definition: Support/Error.h:46
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
virtual void log(raw_ostream &OS) const =0
Print an error message to an output stream.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
static char ID
static ManagedStatic< ErrorErrorCategory > ErrorErrorCat
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
static ManagedStatic< _object_error_category > error_category
void handleAllErrors(Error E, HandlerTs &&...Handlers)
Behaves the same as handleErrors, except that it requires that all errors be handled by the given han...
StringError(const Twine &S, std::error_code EC)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static ErrorSuccess success()
Create a success value.
static char ID
virtual std::error_code convertToErrorCode() const =0
Convert this error to a std::error_code.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
ErrorErrorCode
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:463
aarch64 promote const
static const char * name
Lightweight error class with error context and mandatory checking.
This class wraps a std::error_code in a Error.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:63
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...