[PATCH] D42422: Introduce errorToBool() which converts an Error to a bool and puts the Error in a checked state.

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 23 07:33:25 PST 2018


thakis created this revision.
thakis added reviewers: lhames, espindola.
Herald added a subscriber: mehdi_amini.

Addresses review comments from https://reviews.llvm.org/D39437

(Sorry for the delay; I was gone for a bit.)


https://reviews.llvm.org/D42422

Files:
  include/llvm/Support/Error.h
  lib/LTO/LTOModule.cpp


Index: lib/LTO/LTOModule.cpp
===================================================================
--- lib/LTO/LTOModule.cpp
+++ lib/LTO/LTOModule.cpp
@@ -57,11 +57,7 @@
 bool LTOModule::isBitcodeFile(const void *Mem, size_t Length) {
   Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
       MemoryBufferRef(StringRef((const char *)Mem, Length), "<mem>"));
-  if (!BCData) {
-    consumeError(BCData.takeError());
-    return false;
-  }
-  return true;
+  return !errorToBool(BCData.takeError());
 }
 
 bool LTOModule::isBitcodeFile(StringRef Path) {
@@ -72,11 +68,7 @@
 
   Expected<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
       BufferOrErr.get()->getMemBufferRef());
-  if (!BCData) {
-    consumeError(BCData.takeError());
-    return false;
-  }
-  return true;
+  return !errorToBool(BCData.takeError());
 }
 
 bool LTOModule::isThinLTO() {
@@ -92,10 +84,8 @@
                                    StringRef TriplePrefix) {
   Expected<MemoryBufferRef> BCOrErr =
       IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
-  if (!BCOrErr) {
-    consumeError(BCOrErr.takeError());
+  if (errorToBool(BCOrErr.takeError()))
     return false;
-  }
   LLVMContext Context;
   ErrorOr<std::string> TripleOrErr =
       expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(*BCOrErr));
@@ -107,10 +97,8 @@
 std::string LTOModule::getProducerString(MemoryBuffer *Buffer) {
   Expected<MemoryBufferRef> BCOrErr =
       IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
-  if (!BCOrErr) {
-    consumeError(BCOrErr.takeError());
+  if (errorToBool(BCOrErr.takeError()))
     return "";
-  }
   LLVMContext Context;
   ErrorOr<std::string> ProducerOrErr = expectedToErrorOrAndEmitErrors(
       Context, getBitcodeProducerString(*BCOrErr));
Index: include/llvm/Support/Error.h
===================================================================
--- include/llvm/Support/Error.h
+++ include/llvm/Support/Error.h
@@ -963,6 +963,18 @@
   handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {});
 }
 
+/// Helper for converting an Error to a bool.
+///
+/// This method returns true if Err is in an error state, or false if it is
+/// in a success state.  Puts Err in a checked state in both cases (unlike
+/// Error::operator bool(), which only does this for success states).
+inline bool errorToBool(Error Err) {
+  bool IsError = static_cast<bool>(Err);
+  if (IsError)
+    consumeError(std::move(Err));
+  return IsError;
+}
+
 /// Helper for Errors used as out-parameters.
 ///
 /// This helper is for use with the Error-as-out-parameter idiom, where an error


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42422.131071.patch
Type: text/x-patch
Size: 2650 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180123/cbd02e47/attachment.bin>


More information about the llvm-commits mailing list