OSDN Git Service

Introduce errorToBool() helper and use it.
authorNico Weber <nicolasweber@gmx.de>
Tue, 23 Jan 2018 19:03:13 +0000 (19:03 +0000)
committerNico Weber <nicolasweber@gmx.de>
Tue, 23 Jan 2018 19:03:13 +0000 (19:03 +0000)
errorToBool() converts an Error to a bool and puts the Error in a checked
state.  No behavior change.

https://reviews.llvm.org/D42422

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@323238 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 8567af3..797bbaa 100644 (file)
@@ -963,6 +963,18 @@ inline void consumeError(Error Err) {
   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
index 626d2f5..f7fe85e 100644 (file)
@@ -57,11 +57,7 @@ LTOModule::~LTOModule() {}
 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 @@ bool LTOModule::isBitcodeFile(StringRef Path) {
 
   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 @@ bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer,
                                    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 @@ bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer,
 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));