diff --git a/llvm/include/llvm/Support/ErrorOr.h b/llvm/include/llvm/Support/ErrorOr.h index ceec33d18526..f3ac305fe775 100644 --- a/llvm/include/llvm/Support/ErrorOr.h +++ b/llvm/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/llvm/unittests/Support/ErrorOrTest.cpp b/llvm/unittests/Support/ErrorOrTest.cpp index aa0ddd5e79c9..4853426c9470 100644 --- a/llvm/unittests/Support/ErrorOrTest.cpp +++ b/llvm/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.