[llvm] r268465 - [Support] Add a free toString function for Error

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Tue May 3 16:32:31 PDT 2016


Author: vedantk
Date: Tue May  3 18:32:31 2016
New Revision: 268465

URL: http://llvm.org/viewvc/llvm-project?rev=268465&view=rev
Log:
[Support] Add a free toString function for Error

toString() consumes an Error and returns a string representation of its
contents. This commit also adds a message() method to ErrorInfoBase for
convenience.

Differential Revision: http://reviews.llvm.org/D19883

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=268465&r1=268464&r2=268465&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Error.h (original)
+++ llvm/trunk/include/llvm/Support/Error.h Tue May  3 18:32:31 2016
@@ -16,6 +16,7 @@
 
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/raw_ostream.h"
@@ -35,6 +36,14 @@ public:
   /// Print an error message to an output stream.
   virtual void log(raw_ostream &OS) const = 0;
 
+  /// Return the error message as a string.
+  virtual std::string message() const {
+    std::string Msg;
+    raw_string_ostream OS(Msg);
+    log(OS);
+    return Msg;
+  }
+
   /// Convert this error to a std::error_code.
   ///
   /// This is a temporary crutch to enable interaction with code still
@@ -537,6 +546,16 @@ inline void logAllUnhandledErrors(Error
   });
 }
 
+/// Write all error messages (if any) in E to a string. The newline character
+/// is used to separate error messages.
+inline std::string toString(Error E) {
+  SmallVector<std::string, 2> Errors;
+  handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) {
+    Errors.push_back(EI.message());
+  });
+  return join(Errors.begin(), Errors.end(), "\n");
+}
+
 /// Consume a Error without doing anything. This method should be used
 /// only where an error can be considered a reasonable and expected return
 /// value.

Modified: llvm/trunk/unittests/Support/ErrorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorTest.cpp?rev=268465&r1=268464&r2=268465&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ErrorTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrorTest.cpp Tue May  3 18:32:31 2016
@@ -544,4 +544,23 @@ TEST(Error, ErrorCodeConversions) {
   }
 }
 
+// Test that error messages work.
+TEST(Error, ErrorMessage) {
+  EXPECT_EQ(toString(Error::success()).compare(""), 0);
+
+  Error E1 = make_error<CustomError>(0);
+  EXPECT_EQ(toString(std::move(E1)).compare("CustomError { 0}"), 0);
+
+  Error E2 = make_error<CustomError>(0);
+  handleAllErrors(std::move(E2), [](const CustomError &CE) {
+    EXPECT_EQ(CE.message().compare("CustomError { 0}"), 0);
+  });
+
+  Error E3 = joinErrors(make_error<CustomError>(0), make_error<CustomError>(1));
+  EXPECT_EQ(toString(std::move(E3))
+                .compare("CustomError { 0}\n"
+                         "CustomError { 1}"),
+            0);
+}
+
 } // end anon namespace




More information about the llvm-commits mailing list