[Support] Introduce createStringError helper function
authorVictor Leschuk <vleschuk@accesssoftek.com>
Thu, 26 Jul 2018 02:21:40 +0000 (02:21 +0000)
committerVictor Leschuk <vleschuk@accesssoftek.com>
Thu, 26 Jul 2018 02:21:40 +0000 (02:21 +0000)
The function in question is copy-pasted lots of times in DWARF-related classes.
Thus it will make sense to place its implementation into the Support library.

Reviewed by: lhames

Differential Revision: https://reviews.llvm.org/D49824

llvm-svn: 337995

llvm/include/llvm/Support/Error.h
llvm/lib/Support/Error.cpp
llvm/unittests/Support/ErrorTest.cpp

index 3ea567b4bcf7a28d32166bd29e0bcc183c4628a3..8015cab45a06844c52a3982b3003815bb1fcaaf0 100644 (file)
@@ -24,6 +24,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/ErrorOr.h"
+#include "llvm/Support/Format.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <cassert>
@@ -1121,6 +1122,18 @@ private:
   std::error_code EC;
 };
 
+/// Create formatted StringError object.
+template <typename... Ts>
+Error createStringError(std::error_code EC, char const *Fmt,
+                        const Ts &... Vals) {
+  std::string Buffer;
+  raw_string_ostream Stream(Buffer);
+  Stream << format(Fmt, Vals...);
+  return make_error<StringError>(Stream.str(), EC);
+}
+
+Error createStringError(std::error_code EC, char const *Msg);
+
 /// Helper for check-and-exit error handling.
 ///
 /// For tool use only. NOT FOR USE IN LIBRARY CODE.
index c43a1fa813e242e333af10822c749555225e6b08..83345bf6edb9e073de764dedb14cbbb8f0daaa93 100644 (file)
@@ -112,6 +112,10 @@ std::error_code StringError::convertToErrorCode() const {
   return EC;
 }
 
+Error createStringError(std::error_code EC, char const *Msg) {
+  return make_error<StringError>(Msg, EC);
+}
+
 void report_fatal_error(Error Err, bool GenCrashDiag) {
   assert(Err && "report_fatal_error called with success value");
   std::string ErrMsg;
index 2f9ce2d5646dd3d4b29fb35b17d4ba6c8c07438d..66ffd23f817466396f4f9463b76e2a0780877c7d 100644 (file)
@@ -443,6 +443,29 @@ TEST(Error, StringError) {
     << "Failed to convert StringError to error_code.";
 }
 
+TEST(Error, createStringError) {
+  static const char *Bar = "bar";
+  static const std::error_code EC = errc::invalid_argument;
+  std::string Msg;
+  raw_string_ostream S(Msg);
+  logAllUnhandledErrors(createStringError(EC, "foo%s%d0x%" PRIx8, Bar, 1, 0xff),
+                        S, "");
+  EXPECT_EQ(S.str(), "foobar10xff\n")
+    << "Unexpected createStringError() log result";
+
+  S.flush();
+  Msg.clear();
+  logAllUnhandledErrors(createStringError(EC, Bar), S, "");
+  EXPECT_EQ(S.str(), "bar\n")
+    << "Unexpected createStringError() (overloaded) log result";
+
+  S.flush();
+  Msg.clear();
+  auto Res = errorToErrorCode(createStringError(EC, "foo%s", Bar));
+  EXPECT_EQ(Res, EC)
+    << "Failed to convert createStringError() result to error_code.";
+}
+
 // Test that the ExitOnError utility works as expected.
 TEST(Error, ExitOnError) {
   ExitOnError ExitOnErr;