#include "llvm/Support/Error.h"
#include "llvm-c/Error.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest-spi.h"
#include "gtest/gtest.h"
#include <memory>
usingnamespacellvm;
namespace {
class CustomError : public ErrorInfo<CustomError> { … };
char CustomError::ID = …;
class CustomSubError : public ErrorInfo<CustomSubError, CustomError> { … };
char CustomSubError::ID = …;
static Error handleCustomError(const CustomError &CE) { … }
static void handleCustomErrorVoid(const CustomError &CE) { … }
static Error handleCustomErrorUP(std::unique_ptr<CustomError> CE) { … }
static void handleCustomErrorUPVoid(std::unique_ptr<CustomError> CE) { … }
TEST(Error, CheckedSuccess) { … }
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
TEST(Error, UncheckedSuccess) {
EXPECT_DEATH({ Error E = Error::success(); },
"Program aborted due to an unhandled Error:")
<< "Unchecked Error Succes value did not cause abort()";
}
#endif
void errAsOutParamHelper(Error &Err) { … }
TEST(Error, ErrorAsOutParameterChecked) { … }
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
TEST(Error, ErrorAsOutParameterUnchecked) {
EXPECT_DEATH({ Error E = Error::success(); errAsOutParamHelper(E); },
"Program aborted due to an unhandled Error:")
<< "ErrorAsOutParameter did not clear the checked flag on destruction.";
}
#endif
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
TEST(Error, UncheckedError) {
auto DropUnhandledError = []() {
Error E = make_error<CustomError>(42);
(void)!E;
};
EXPECT_DEATH(DropUnhandledError(),
"Program aborted due to an unhandled Error:")
<< "Unhandled Error failure value did not cause abort()";
}
#endif
TEST(Error, IsAHandling) { … }
TEST(Error, HandleCustomError) { … }
TEST(Error, HandlerTypeDeduction) { … }
TEST(Error, HandleCustomErrorWithCustomBaseClass) { … }
TEST(Error, FirstHandlerOnly) { … }
TEST(Error, HandlerShadowing) { … }
TEST(Error, CheckJoinErrors) { … }
TEST(Error, ConsumeSuccess) { … }
TEST(Error, ConsumeError) { … }
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
TEST(Error, FailureToHandle) {
auto FailToHandle = []() {
handleAllErrors(make_error<CustomError>(7), [&](const CustomSubError &SE) {
errs() << "This should never be called";
exit(1);
});
};
EXPECT_DEATH(FailToHandle(),
"Failure value returned from cantFail wrapped call\n"
"CustomError \\{7\\}")
<< "Unhandled Error in handleAllErrors call did not cause an "
"abort()";
}
#endif
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
TEST(Error, FailureFromHandler) {
auto ReturnErrorFromHandler = []() {
handleAllErrors(make_error<CustomError>(7),
[&](std::unique_ptr<CustomSubError> SE) {
return Error(std::move(SE));
});
};
EXPECT_DEATH(ReturnErrorFromHandler(),
"Failure value returned from cantFail wrapped call\n"
"CustomError \\{7\\}")
<< " Error returned from handler in handleAllErrors call did not "
"cause abort()";
}
#endif
TEST(Error, CatchErrorFromHandler) { … }
TEST(Error, StringError) { … }
TEST(Error, createStringError) { … }
TEST(ErrorDeathTest, ExitOnError) { … }
TEST(Error, CantFailSuccess) { … }
#if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG)
TEST(Error, CantFailDeath) {
EXPECT_DEATH(cantFail(make_error<StringError>("Original error message",
inconvertibleErrorCode()),
"Cantfail call failed"),
"Cantfail call failed\n"
"Original error message")
<< "cantFail(Error) did not cause an abort for failure value";
EXPECT_DEATH(
{
auto IEC = inconvertibleErrorCode();
int X = cantFail(Expected<int>(make_error<StringError>("foo", IEC)));
(void)X;
},
"Failure value returned from cantFail wrapped call")
<< "cantFail(Expected<int>) did not cause an abort for failure value";
}
#endif
TEST(Error, CheckedExpectedInSuccessMode) { … }
TEST(Error, ExpectedWithReferenceType) { … }
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
TEST(Error, UncheckedExpectedInSuccessModeDestruction) {
EXPECT_DEATH({ Expected<int> A = 7; },
"Expected<T> must be checked before access or destruction.")
<< "Unchecked Expected<T> success value did not cause an abort().";
}
#endif
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
TEST(Error, UncheckedExpectedInSuccessModeAccess) {
EXPECT_DEATH(
{
const Expected<int> A = 7;
*A;
},
"Expected<T> must be checked before access or destruction.")
<< "Unchecked Expected<T> success value did not cause an abort().";
}
#endif
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
TEST(Error, UncheckedExpectedInSuccessModeAssignment) {
EXPECT_DEATH(
{
Expected<int> A = 7;
A = 7;
},
"Expected<T> must be checked before access or destruction.")
<< "Unchecked Expected<T> success value did not cause an abort().";
}
#endif
TEST(Error, ExpectedInFailureMode) { … }
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
TEST(Error, AccessExpectedInFailureMode) {
Expected<int> A = make_error<CustomError>(42);
EXPECT_DEATH(*A, "Expected<T> must be checked before access or destruction.")
<< "Incorrect Expected error value";
consumeError(A.takeError());
}
#endif
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
TEST(Error, UnhandledExpectedInFailureMode) {
EXPECT_DEATH({ Expected<int> A = make_error<CustomError>(42); },
"Expected<T> must be checked before access or destruction.")
<< "Unchecked Expected<T> failure value did not cause an abort()";
}
#endif
TEST(Error, ExpectedCovariance) { … }
TEST(Error, HandleExpectedSuccess) { … }
enum FooStrategy { … };
static Expected<int> foo(FooStrategy S) { … }
TEST(Error, HandleExpectedUnhandledError) { … }
TEST(Error, HandleExpectedHandledError) { … }
TEST(Error, ErrorCodeConversions) { … }
TEST(Error, ErrorMessage) { … }
TEST(Error, Stream) { … }
TEST(Error, SucceededMatcher) { … }
TEST(Error, FailedMatcher) { … }
TEST(Error, HasValueMatcher) { … }
TEST(Error, FailedWithMessageMatcher) { … }
TEST(Error, C_API) { … }
TEST(Error, FileErrorTest) { … }
TEST(Error, FileErrorErrorCode) { … }
enum class test_error_code { … };
}
namespace std {
template <>
struct is_error_code_enum<test_error_code> : std::true_type { … };
}
namespace {
const std::error_category &TErrorCategory();
inline std::error_code make_error_code(test_error_code E) { … }
class TestDebugError : public ErrorInfo<TestDebugError, StringError> { … };
class TestErrorCategory : public std::error_category { … };
const std::error_category &TErrorCategory() { … }
char TestDebugError::ID;
TEST(Error, SubtypeStringErrorTest) { … }
static Error createAnyError() { … }
struct MoveOnlyBox { … };
TEST(Error, moveInto) { … }
TEST(Error, FatalBadAllocErrorHandlersInteraction) { … }
TEST(Error, BadAllocFatalErrorHandlersInteraction) { … }
TEST(Error, ForwardToExpected) { … }
}