[llvm] r264467 - [Support] Add Error::errorForOutParameter helper.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 25 14:56:35 PDT 2016


Author: lhames
Date: Fri Mar 25 16:56:35 2016
New Revision: 264467

URL: http://llvm.org/viewvc/llvm-project?rev=264467&view=rev
Log:
[Support] Add Error::errorForOutParameter helper.

This helper method creates a pre-checked Error suitable for use as an out
parameter in a constructor. This avoids the need to have the constructor
check a known-good error before assigning to it.


Modified:
    llvm/trunk/include/llvm/Support/Error.h
    llvm/trunk/unittests/Support/ErrorTest.cpp

Modified: llvm/trunk/include/llvm/Support/Error.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Error.h?rev=264467&r1=264466&r2=264467&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Error.h (original)
+++ llvm/trunk/include/llvm/Support/Error.h Fri Mar 25 16:56:35 2016
@@ -143,6 +143,13 @@ public:
   /// constructor, but should be preferred for readability where possible.
   static Error success() { return Error(); }
 
+  /// Create a 'pre-checked' success value suitable for use as an out-parameter.
+  static Error errorForOutParameter() {
+    Error Err;
+    (void)!!Err;
+    return Err;
+  }
+
   // Errors are not copy-constructable.
   Error(const Error &Other) = delete;
 

Modified: llvm/trunk/unittests/Support/ErrorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorTest.cpp?rev=264467&r1=264466&r2=264467&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ErrorTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrorTest.cpp Fri Mar 25 16:56:35 2016
@@ -105,6 +105,12 @@ TEST(Error, UncheckedSuccess) {
 }
 #endif
 
+// Test that errors to be used as out parameters are implicitly checked (
+// and thus destruct quietly).
+TEST(Error, ErrorAsOutParameter) {
+  Error E = Error::errorForOutParameter();
+}
+
 // Check that we abort on unhandled failure cases. (Force conversion to bool
 // to make sure that we don't accidentally treat checked errors as handled).
 // Test runs in debug mode only.




More information about the llvm-commits mailing list