From 9b4886ee55c4d13cf0f663fe0ee5e0cfacb39667 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Thu, 28 Feb 2013 01:44:26 +0000 Subject: [PATCH] [Support][ErrorOr] Add support for implicit conversion from error code/condition enums. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@176228 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/ErrorOr.h | 26 ++++++++++++++++++++++++++ unittests/Support/ErrorOrTest.cpp | 6 +++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/include/llvm/Support/ErrorOr.h b/include/llvm/Support/ErrorOr.h index ceec33d1852..f3ac305fe77 100644 --- a/include/llvm/Support/ErrorOr.h +++ b/include/llvm/Support/ErrorOr.h @@ -180,6 +180,16 @@ private: public: ErrorOr() : IsValid(false) {} + template + ErrorOr(E ErrorCode, typename enable_if_c::value || + is_error_condition_enum::value, + void *>::type = 0) + : HasError(true), IsValid(true) { + Error = new ErrorHolderBase; + Error->Error = make_error_code(ErrorCode); + Error->HasUserData = false; + } + ErrorOr(llvm::error_code EC) : HasError(true), IsValid(true) { Error = new ErrorHolderBase; Error->Error = EC; @@ -387,6 +397,22 @@ class ErrorOr { public: ErrorOr() : Error(0, 0) {} + template + ErrorOr(E ErrorCode, typename enable_if_c::value || + is_error_condition_enum::value, + void *> ::type = 0) + : Error(0, 0) { + error_code EC = make_error_code(ErrorCode); + if (EC == errc::success) { + Error.setInt(1); + return; + } + ErrorHolderBase *EHB = new ErrorHolderBase; + EHB->Error = EC; + EHB->HasUserData = false; + Error.setPointer(EHB); + } + ErrorOr(llvm::error_code EC) : Error(0, 0) { if (EC == errc::success) { Error.setInt(1); diff --git a/unittests/Support/ErrorOrTest.cpp b/unittests/Support/ErrorOrTest.cpp index aa0ddd5e79c..4853426c947 100644 --- a/unittests/Support/ErrorOrTest.cpp +++ b/unittests/Support/ErrorOrTest.cpp @@ -18,7 +18,7 @@ using namespace llvm; namespace { ErrorOr t1() {return 1;} -ErrorOr t2() {return make_error_code(errc::invalid_argument);} +ErrorOr t2() { return errc::invalid_argument; } TEST(ErrorOr, SimpleValue) { ErrorOr a = t1(); @@ -45,8 +45,8 @@ TEST(ErrorOr, Types) { *a = 42; EXPECT_EQ(42, x); - EXPECT_FALSE(ErrorOr(make_error_code(errc::broken_pipe))); - EXPECT_TRUE(ErrorOr(make_error_code(errc::success))); + EXPECT_FALSE(ErrorOr(errc::broken_pipe)); + EXPECT_TRUE(ErrorOr(errc::success)); #if LLVM_HAS_CXX11_STDLIB // Move only types. -- 2.34.1