From d2ffaaeb6c82456aeaa060861947b64c42a88f2b Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Mon, 26 Aug 2019 18:29:51 +0000 Subject: [PATCH] FileManager: Use llvm::Expected in new getFileRef API `FileManager::getFileRef` is a modern API which we expect to convert to over time. We should modernize the error handling as well, using `llvm::Expected` instead of `llvm::ErrorOr`, to help clients that care about errors to ensure nothing is missed. However, not all clients care. I've also added another path for those that don't: - `FileEntryRef` is now copy- and move-assignable (using a pointer instead of a reference). - `FileManager::getOptionalFileRef` returns an `llvm::Optional` instead of `llvm::Expected`. - Added an `llvm::expectedToOptional` utility in case this is useful elsewhere. https://reviews.llvm.org/D66705 llvm-svn: 369943 --- include/llvm/Support/Error.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/llvm/Support/Error.h b/include/llvm/Support/Error.h index c0e4d362364..f961a29b33a 100644 --- a/include/llvm/Support/Error.h +++ b/include/llvm/Support/Error.h @@ -982,6 +982,20 @@ inline void consumeError(Error Err) { handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {}); } +/// Convert an Expected to an Optional without doing anything. This method +/// should be used only where an error can be considered a reasonable and +/// expected return value. +/// +/// Uses of this method are potentially indicative of problems: perhaps the +/// error should be propagated further, or the error-producer should just +/// return an Optional in the first place. +template Optional expectedToOptional(Expected &&E) { + if (E) + return std::move(*E); + consumeError(E.takeError()); + return None; +} + /// Helper for converting an Error to a bool. /// /// This method returns true if Err is in an error state, or false if it is