2016-03-24 07:57:28 +08:00
|
|
|
//===----- lib/Support/Error.cpp - Error and associated utilities ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/Error.h"
|
2016-05-27 09:37:32 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2016-03-24 07:57:28 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2016-09-14 16:55:18 +08:00
|
|
|
#include <system_error>
|
2016-03-24 07:57:28 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-05-27 09:37:32 +08:00
|
|
|
enum class ErrorErrorCode : int {
|
|
|
|
MultipleErrors = 1,
|
2016-05-27 09:54:25 +08:00
|
|
|
InconvertibleError
|
2016-03-24 07:57:28 +08:00
|
|
|
};
|
|
|
|
|
2016-05-25 04:13:46 +08:00
|
|
|
// FIXME: This class is only here to support the transition to llvm::Error. It
|
|
|
|
// will be removed once this transition is complete. Clients should prefer to
|
|
|
|
// deal with the Error value directly, rather than converting to error_code.
|
2016-03-24 07:57:28 +08:00
|
|
|
class ErrorErrorCategory : public std::error_category {
|
|
|
|
public:
|
2016-10-20 07:52:38 +08:00
|
|
|
const char *name() const noexcept override { return "Error"; }
|
2016-03-24 07:57:28 +08:00
|
|
|
|
|
|
|
std::string message(int condition) const override {
|
|
|
|
switch (static_cast<ErrorErrorCode>(condition)) {
|
|
|
|
case ErrorErrorCode::MultipleErrors:
|
|
|
|
return "Multiple errors";
|
2016-05-27 09:54:25 +08:00
|
|
|
case ErrorErrorCode::InconvertibleError:
|
|
|
|
return "Inconvertible error value. An error has occurred that could "
|
2016-05-27 09:37:32 +08:00
|
|
|
"not be converted to a known std::error_code. Please file a "
|
|
|
|
"bug.";
|
|
|
|
}
|
2016-03-24 07:57:28 +08:00
|
|
|
llvm_unreachable("Unhandled error code");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-24 23:19:39 +08:00
|
|
|
}
|
2016-03-24 07:57:28 +08:00
|
|
|
|
2016-05-27 09:37:32 +08:00
|
|
|
static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2016-03-24 07:57:28 +08:00
|
|
|
void ErrorInfoBase::anchor() {}
|
|
|
|
char ErrorInfoBase::ID = 0;
|
2016-03-25 07:49:34 +08:00
|
|
|
char ErrorList::ID = 0;
|
|
|
|
char ECError::ID = 0;
|
2016-05-27 09:37:32 +08:00
|
|
|
char StringError::ID = 0;
|
2016-03-24 23:26:43 +08:00
|
|
|
|
2017-10-12 07:54:34 +08:00
|
|
|
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner) {
|
2016-07-05 06:47:53 +08:00
|
|
|
if (!E)
|
|
|
|
return;
|
|
|
|
OS << ErrorBanner;
|
|
|
|
handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
|
|
|
|
EI.log(OS);
|
|
|
|
OS << "\n";
|
|
|
|
});
|
|
|
|
}
|
2016-03-24 07:57:28 +08:00
|
|
|
|
2016-07-14 10:24:01 +08:00
|
|
|
|
2016-03-24 07:57:28 +08:00
|
|
|
std::error_code ErrorList::convertToErrorCode() const {
|
|
|
|
return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
|
|
|
|
*ErrorErrorCat);
|
|
|
|
}
|
2016-05-27 09:37:32 +08:00
|
|
|
|
2016-05-27 09:54:25 +08:00
|
|
|
std::error_code inconvertibleErrorCode() {
|
|
|
|
return std::error_code(static_cast<int>(ErrorErrorCode::InconvertibleError),
|
2016-05-27 09:37:32 +08:00
|
|
|
*ErrorErrorCat);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error errorCodeToError(std::error_code EC) {
|
|
|
|
if (!EC)
|
|
|
|
return Error::success();
|
|
|
|
return Error(llvm::make_unique<ECError>(ECError(EC)));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code errorToErrorCode(Error Err) {
|
|
|
|
std::error_code EC;
|
|
|
|
handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
|
|
|
|
EC = EI.convertToErrorCode();
|
|
|
|
});
|
2016-05-27 09:54:25 +08:00
|
|
|
if (EC == inconvertibleErrorCode())
|
2016-05-27 09:37:32 +08:00
|
|
|
report_fatal_error(EC.message());
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:31:52 +08:00
|
|
|
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
|
|
|
|
void Error::fatalUncheckedError() const {
|
|
|
|
dbgs() << "Program aborted due to an unhandled Error:\n";
|
|
|
|
if (getPtr())
|
|
|
|
getPtr()->log(dbgs());
|
|
|
|
else
|
|
|
|
dbgs() << "Error value was Success. (Note: Success values must still be "
|
|
|
|
"checked prior to being destroyed).\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-27 09:37:32 +08:00
|
|
|
StringError::StringError(const Twine &S, std::error_code EC)
|
|
|
|
: Msg(S.str()), EC(EC) {}
|
|
|
|
|
|
|
|
void StringError::log(raw_ostream &OS) const { OS << Msg; }
|
|
|
|
|
|
|
|
std::error_code StringError::convertToErrorCode() const {
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
2018-07-26 10:21:40 +08:00
|
|
|
Error createStringError(std::error_code EC, char const *Msg) {
|
|
|
|
return make_error<StringError>(Msg, EC);
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:24:01 +08:00
|
|
|
void report_fatal_error(Error Err, bool GenCrashDiag) {
|
|
|
|
assert(Err && "report_fatal_error called with success value");
|
|
|
|
std::string ErrMsg;
|
|
|
|
{
|
|
|
|
raw_string_ostream ErrStream(ErrMsg);
|
|
|
|
logAllUnhandledErrors(std::move(Err), ErrStream, "");
|
|
|
|
}
|
|
|
|
report_fatal_error(ErrMsg);
|
|
|
|
}
|
|
|
|
|
2016-05-27 09:37:32 +08:00
|
|
|
}
|
2016-11-29 06:23:53 +08:00
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
// One of these two variables will be referenced by a symbol defined in
|
|
|
|
// llvm-config.h. We provide a link-time (or load time for DSO) failure when
|
|
|
|
// there is a mismatch in the build configuration of the API client and LLVM.
|
|
|
|
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
|
|
|
|
int EnableABIBreakingChecks;
|
|
|
|
#else
|
|
|
|
int DisableABIBreakingChecks;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // end namespace llvm
|
2016-11-30 01:32:43 +08:00
|
|
|
#endif
|