LLVM  6.0.0svn
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 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  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  }
41  llvm_unreachable("Unhandled error code");
42  }
43  };
44 
45 }
46 
48 
49 namespace llvm {
50 
51 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 void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
58  if (!E)
59  return;
60  OS << ErrorBanner;
61  handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
62  EI.log(OS);
63  OS << "\n";
64  });
65 }
66 
67 
68 std::error_code ErrorList::convertToErrorCode() const {
69  return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
70  *ErrorErrorCat);
71 }
72 
73 std::error_code inconvertibleErrorCode() {
74  return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
75  *ErrorErrorCat);
76 }
77 
78 Error errorCodeToError(std::error_code EC) {
79  if (!EC)
80  return Error::success();
81  return Error(llvm::make_unique<ECError>(ECError(EC)));
82 }
83 
84 std::error_code errorToErrorCode(Error Err) {
85  std::error_code EC;
86  handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
87  EC = EI.convertToErrorCode();
88  });
89  if (EC == inconvertibleErrorCode())
90  report_fatal_error(EC.message());
91  return EC;
92 }
93 
94 StringError::StringError(const Twine &S, std::error_code EC)
95  : Msg(S.str()), EC(EC) {}
96 
97 void StringError::log(raw_ostream &OS) const { OS << Msg; }
98 
99 std::error_code StringError::convertToErrorCode() const {
100  return EC;
101 }
102 
103 void report_fatal_error(Error Err, bool GenCrashDiag) {
104  assert(Err && "report_fatal_error called with success value");
105  std::string ErrMsg;
106  {
107  raw_string_ostream ErrStream(ErrMsg);
108  logAllUnhandledErrors(std::move(Err), ErrStream, "");
109  }
110  report_fatal_error(ErrMsg);
111 }
112 
113 }
114 
115 #ifndef _MSC_VER
116 namespace llvm {
117 
118 // One of these two variables will be referenced by a symbol defined in
119 // llvm-config.h. We provide a link-time (or load time for DSO) failure when
120 // there is a mismatch in the build configuration of the API client and LLVM.
121 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
122 int EnableABIBreakingChecks;
123 #else
125 #endif
126 
127 } // end namespace llvm
128 #endif
int DisableABIBreakingChecks
Definition: Error.cpp:124
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:103
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
Definition: Error.cpp:57
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Error.cpp:68
ErrorErrorCode
Definition: Error.cpp:20
Base class for error info classes.
Definition: Error.h:47
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.
Definition: Error.cpp:97
static char ID
Definition: Error.h:363
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.
Definition: Error.cpp:78
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:74
static ManagedStatic< ErrorErrorCategory > ErrorErrorCat
Definition: Error.cpp:47
StringError(const Twine &S, std::error_code EC)
Definition: Error.cpp:94
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that it requires that all errors be handled by the given han...
Definition: Error.h:880
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
static char ID
Definition: Error.h:1034
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.
Definition: Error.cpp:99
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:466
aarch64 promote const
static const char * name
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
This class wraps a std::error_code in a Error.
Definition: Error.h:1025
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:61
static char ID
Definition: Error.h:1081
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:84
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:73