LLVM  8.0.1
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  FileError,
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  case ErrorErrorCode::FileError:
42  return "A file error occurred.";
43  }
44  llvm_unreachable("Unhandled error code");
45  }
46  };
47 
48 }
49 
51 
52 namespace llvm {
53 
54 void ErrorInfoBase::anchor() {}
55 char ErrorInfoBase::ID = 0;
56 char ErrorList::ID = 0;
57 void ECError::anchor() {}
58 char ECError::ID = 0;
59 char StringError::ID = 0;
60 char FileError::ID = 0;
61 
62 void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
63  if (!E)
64  return;
65  OS << ErrorBanner;
66  handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
67  EI.log(OS);
68  OS << "\n";
69  });
70 }
71 
72 
73 std::error_code ErrorList::convertToErrorCode() const {
74  return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
75  *ErrorErrorCat);
76 }
77 
78 std::error_code inconvertibleErrorCode() {
79  return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
80  *ErrorErrorCat);
81 }
82 
83 std::error_code FileError::convertToErrorCode() const {
84  return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
85  *ErrorErrorCat);
86 }
87 
88 Error errorCodeToError(std::error_code EC) {
89  if (!EC)
90  return Error::success();
91  return Error(llvm::make_unique<ECError>(ECError(EC)));
92 }
93 
94 std::error_code errorToErrorCode(Error Err) {
95  std::error_code EC;
96  handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
97  EC = EI.convertToErrorCode();
98  });
99  if (EC == inconvertibleErrorCode())
100  report_fatal_error(EC.message());
101  return EC;
102 }
103 
104 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
105 void Error::fatalUncheckedError() const {
106  dbgs() << "Program aborted due to an unhandled Error:\n";
107  if (getPtr())
108  getPtr()->log(dbgs());
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 
153  return reinterpret_cast<ErrorInfoBase *>(Err)->dynamicClassID();
154 }
155 
157 
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 
169  return reinterpret_cast<void *>(&StringError::ID);
170 }
171 
172 #ifndef _MSC_VER
173 namespace llvm {
174 
175 // One of these two variables will be referenced by a symbol defined in
176 // llvm-config.h. We provide a link-time (or load time for DSO) failure when
177 // there is a mismatch in the build configuration of the API client and LLVM.
178 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
179 int EnableABIBreakingChecks;
180 #else
182 #endif
183 
184 } // end namespace llvm
185 #endif
int DisableABIBreakingChecks
Definition: Error.cpp:181
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:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
static const char * getPtr(const MachOObjectFile &O, size_t Offset)
StringError(std::error_code EC, const Twine &S=Twine())
Definition: Error.cpp:116
block Block Frequency true
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Error.cpp:73
ErrorErrorCode
Definition: Error.cpp:20
Base class for error info classes.
Definition: Error.h:49
This class wraps a filename and another Error.
Definition: Error.h:1178
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:967
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:195
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:122
static char ID
Definition: Error.h:381
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Error.cpp:83
struct LLVMOpaqueError * LLVMErrorRef
Opaque reference to an error instance.
Definition: Error.h:26
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:88
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:75
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:982
const void * LLVMErrorTypeId
Error type identifier.
Definition: Error.h:31
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:62
static ManagedStatic< ErrorErrorCategory > ErrorErrorCat
Definition: Error.cpp:50
#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 by contract all errors must be handled by the given han...
Definition: Error.h:905
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err)
Returns the type id for the given error instance, which must be a failure value (i.e.
Definition: Error.cpp:152
static char ID
Definition: Error.h:1077
virtual std::error_code convertToErrorCode() const =0
Convert this error to a std::error_code.
void LLVMConsumeError(LLVMErrorRef Err)
Dispose of the given error without handling it.
Definition: Error.cpp:156
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
LLVMErrorTypeId LLVMGetStringErrorTypeId()
Returns the type id for llvm StringError.
Definition: Error.cpp:168
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Error.cpp:132
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:483
aarch64 promote const
static const char * name
char * LLVMGetErrorMessage(LLVMErrorRef Err)
Returns the given string&#39;s error message.
Definition: Error.cpp:158
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
This class wraps a std::error_code in a Error.
Definition: Error.h:1066
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61
void LLVMDisposeErrorMessage(char *ErrMsg)
Dispose of the given error message.
Definition: Error.cpp:166
static char ID
Definition: Error.h:1143
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:94
static char ID
Definition: Error.h:1194
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1164
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:78