[PATCH] D49824: [Support] Introduce createStringError helper function

Victor Leschuk via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 25 15:51:48 PDT 2018


vleschuk created this revision.
vleschuk added reviewers: jhenderson, probinson, dblaikie.
Herald added subscribers: JDevlieghere, aprantl.

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.


Repository:
  rL LLVM

https://reviews.llvm.org/D49824

Files:
  include/llvm/Support/Error.h
  unittests/Support/ErrorTest.cpp


Index: unittests/Support/ErrorTest.cpp
===================================================================
--- unittests/Support/ErrorTest.cpp
+++ unittests/Support/ErrorTest.cpp
@@ -443,6 +443,17 @@
     << "Failed to convert StringError to error_code.";
 }
 
+TEST(Error, createStringError) {
+  static const char *bar = "bar";
+  std::string Msg;
+  raw_string_ostream S(Msg);
+  logAllUnhandledErrors(createStringError("foo%s%d0x%" PRIx8, bar, 42, 0xff),
+                        S, "");
+  EXPECT_EQ(S.str(), "foobar420xff\n")
+    << "Unexpected createStringError log result";
+
+}
+
 // Test that the ExitOnError utility works as expected.
 TEST(Error, ExitOnError) {
   ExitOnError ExitOnErr;
Index: include/llvm/Support/Error.h
===================================================================
--- include/llvm/Support/Error.h
+++ include/llvm/Support/Error.h
@@ -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,15 @@
   std::error_code EC;
 };
 
+/// Create formatted StringError object.
+template <typename... Ts>
+Error createStringError(char const *Fmt, const Ts &... Vals) {
+  std::string Buffer;
+  raw_string_ostream Stream(Buffer);
+  Stream << format(Fmt, Vals...);
+  return make_error<StringError>(Stream.str(), inconvertibleErrorCode());
+}
+
 /// Helper for check-and-exit error handling.
 ///
 /// For tool use only. NOT FOR USE IN LIBRARY CODE.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49824.157382.patch
Type: text/x-patch
Size: 1579 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180725/a94cd29c/attachment.bin>


More information about the llvm-commits mailing list