Please use GitHub pull requests for new patches. Avoid migrating existing patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
llvm/unittests/Support/ErrorTest.cpp
Show First 20 Lines • Show All 384 Lines • ▼ Show 20 Lines | TEST(Error, FailureToHandle) { | ||||
auto FailToHandle = []() { | auto FailToHandle = []() { | ||||
handleAllErrors(make_error<CustomError>(7), [&](const CustomSubError &SE) { | handleAllErrors(make_error<CustomError>(7), [&](const CustomSubError &SE) { | ||||
errs() << "This should never be called"; | errs() << "This should never be called"; | ||||
exit(1); | exit(1); | ||||
}); | }); | ||||
}; | }; | ||||
EXPECT_DEATH(FailToHandle(), | EXPECT_DEATH(FailToHandle(), | ||||
"Failure value returned from cantFail wrapped call\n" | "CustomError \\{7\\}\n" | ||||
"CustomError \\{7\\}") | "Failure value returned from cantFail wrapped call") | ||||
<< "Unhandled Error in handleAllErrors call did not cause an " | << "Unhandled Error in handleAllErrors call did not cause an " | ||||
"abort()"; | "abort()"; | ||||
} | } | ||||
#endif | #endif | ||||
// Test that handleAllUnhandledErrors crashes if an error is returned from a | // Test that handleAllUnhandledErrors crashes if an error is returned from a | ||||
// handler. | // handler. | ||||
// Test runs in debug mode only. | // Test runs in debug mode only. | ||||
#if LLVM_ENABLE_ABI_BREAKING_CHECKS | #if LLVM_ENABLE_ABI_BREAKING_CHECKS | ||||
TEST(Error, FailureFromHandler) { | TEST(Error, FailureFromHandler) { | ||||
auto ReturnErrorFromHandler = []() { | auto ReturnErrorFromHandler = []() { | ||||
handleAllErrors(make_error<CustomError>(7), | handleAllErrors(make_error<CustomError>(7), | ||||
[&](std::unique_ptr<CustomSubError> SE) { | [&](std::unique_ptr<CustomSubError> SE) { | ||||
return Error(std::move(SE)); | return Error(std::move(SE)); | ||||
}); | }); | ||||
}; | }; | ||||
EXPECT_DEATH(ReturnErrorFromHandler(), | EXPECT_DEATH(ReturnErrorFromHandler(), | ||||
"Failure value returned from cantFail wrapped call\n" | "CustomError \\{7\\}\n" | ||||
"CustomError \\{7\\}") | "Failure value returned from cantFail wrapped call") | ||||
<< " Error returned from handler in handleAllErrors call did not " | << " Error returned from handler in handleAllErrors call did not " | ||||
"cause abort()"; | "cause abort()"; | ||||
} | } | ||||
#endif | #endif | ||||
// Test that we can return values from handleErrors. | // Test that we can return values from handleErrors. | ||||
TEST(Error, CatchErrorFromHandler) { | TEST(Error, CatchErrorFromHandler) { | ||||
int ErrorInfo = 0; | int ErrorInfo = 0; | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | TEST(Error, ExitOnError) { | ||||
EXPECT_EXIT(ExitOnErr(Expected<int>(make_error<CustomSubError>(0, 0))), | EXPECT_EXIT(ExitOnErr(Expected<int>(make_error<CustomSubError>(0, 0))), | ||||
::testing::ExitedWithCode(2), "Error in tool:") | ::testing::ExitedWithCode(2), "Error in tool:") | ||||
<< "exitOnError returned an unexpected error result"; | << "exitOnError returned an unexpected error result"; | ||||
} | } | ||||
// Test that the ExitOnError utility works as expected. | // Test that the ExitOnError utility works as expected. | ||||
TEST(Error, CantFailSuccess) { | TEST(Error, CantFailSuccess) { | ||||
cantFail(Error::success()); | llvm_cantFail(Error::success()); | ||||
int X = cantFail(Expected<int>(42)); | int X = llvm_cantFail(Expected<int>(42)); | ||||
EXPECT_EQ(X, 42) << "Expected value modified by cantFail"; | EXPECT_EQ(X, 42) << "Expected value modified by llvm_cantFail"; | ||||
int Dummy = 42; | int Dummy = 42; | ||||
int &Y = cantFail(Expected<int&>(Dummy)); | int &Y = llvm_cantFail(Expected<int&>(Dummy)); | ||||
EXPECT_EQ(&Dummy, &Y) << "Reference mangled by cantFail"; | EXPECT_EQ(&Dummy, &Y) << "Reference mangled by llvm_cantFail"; | ||||
} | } | ||||
// Test that cantFail results in a crash if you pass it a failure value. | // Test that llvm_cantFail results in a crash if you pass it a failure value. | ||||
#if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG) | #if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG) | ||||
TEST(Error, CantFailDeath) { | TEST(Error, CantFailDeath) { | ||||
EXPECT_DEATH(cantFail(make_error<StringError>("Original error message", | EXPECT_DEATH(llvm_cantFail(make_error<StringError>("Original error message", | ||||
inconvertibleErrorCode()), | inconvertibleErrorCode()), | ||||
"Cantfail call failed"), | "Cantfail call failed"), | ||||
"Cantfail call failed\n" | "Original error message\n" | ||||
"Original error message") | "Cantfail call failed") | ||||
<< "cantFail(Error) did not cause an abort for failure value"; | << "llvm_cantFail(Error) did not cause an abort for failure value"; | ||||
EXPECT_DEATH( | EXPECT_DEATH( | ||||
{ | { | ||||
auto IEC = inconvertibleErrorCode(); | auto IEC = inconvertibleErrorCode(); | ||||
int X = cantFail(Expected<int>(make_error<StringError>("foo", IEC))); | int X = llvm_cantFail(Expected<int>(make_error<StringError>("foo", IEC))); | ||||
(void)X; | (void)X; | ||||
}, | }, | ||||
"Failure value returned from cantFail wrapped call") | "Failure value returned from cantFail wrapped call") | ||||
<< "cantFail(Expected<int>) did not cause an abort for failure value"; | << "llvm_cantFail(Expected<int>) did not cause an abort for failure value"; | ||||
} | } | ||||
#endif | #endif | ||||
TEST(Error, CantFailSourceLocation) { | |||||
std::string ErrStr; | |||||
raw_string_ostream OS(ErrStr); | |||||
OS << "\nFailure value returned from cantFail wrapped call"; | |||||
#if defined(NDEBUG) | |||||
// __FILE__ and __LINE_ not added | |||||
OS << "\n"; | |||||
EXPECT_DEATH(llvm_cantFail(make_error<CustomError>(1)), OS.str()) | |||||
<< "llvm_cantFail(Error) did not cause an abort for failure value"; | |||||
#else | |||||
// __FILE__ and __LINE__ added | |||||
int Line = __LINE__; | |||||
OS << " at " << __FILE__ << ":" << (Line + 2) << "\n"; | |||||
EXPECT_DEATH(llvm_cantFail(make_error<CustomError>(1)), OS.str()) | |||||
<< "llvm_cantFail(Error) did not cause an abort for failure value"; | |||||
#endif | |||||
} | |||||
// Test Checked Expected<T> in success mode. | // Test Checked Expected<T> in success mode. | ||||
TEST(Error, CheckedExpectedInSuccessMode) { | TEST(Error, CheckedExpectedInSuccessMode) { | ||||
Expected<int> A = 7; | Expected<int> A = 7; | ||||
EXPECT_TRUE(!!A) << "Expected with non-error value doesn't convert to 'true'"; | EXPECT_TRUE(!!A) << "Expected with non-error value doesn't convert to 'true'"; | ||||
// Access is safe in second test, since we checked the error in the first. | // Access is safe in second test, since we checked the error in the first. | ||||
EXPECT_EQ(*A, 7) << "Incorrect Expected non-error value"; | EXPECT_EQ(*A, 7) << "Incorrect Expected non-error value"; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 441 Lines • Show Last 20 Lines |