llvm.org GIT mirror llvm / f4c9d7c
[Support] Introduce createStringError helper function 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337995 91177308-0d34-0410-b5e6-96231b3b80d8 Victor Leschuk 1 year, 2 months ago
3 changed file(s) with 40 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
2323 #include "llvm/Support/Debug.h"
2424 #include "llvm/Support/ErrorHandling.h"
2525 #include "llvm/Support/ErrorOr.h"
26 #include "llvm/Support/Format.h"
2627 #include "llvm/Support/raw_ostream.h"
2728 #include
2829 #include
11201121 std::error_code EC;
11211122 };
11221123
1124 /// Create formatted StringError object.
1125 template
1126 Error createStringError(std::error_code EC, char const *Fmt,
1127 const Ts &... Vals) {
1128 std::string Buffer;
1129 raw_string_ostream Stream(Buffer);
1130 Stream << format(Fmt, Vals...);
1131 return make_error(Stream.str(), EC);
1132 }
1133
1134 Error createStringError(std::error_code EC, char const *Msg);
1135
11231136 /// Helper for check-and-exit error handling.
11241137 ///
11251138 /// For tool use only. NOT FOR USE IN LIBRARY CODE.
111111 return EC;
112112 }
113113
114 Error createStringError(std::error_code EC, char const *Msg) {
115 return make_error(Msg, EC);
116 }
117
114118 void report_fatal_error(Error Err, bool GenCrashDiag) {
115119 assert(Err && "report_fatal_error called with success value");
116120 std::string ErrMsg;
442442 << "Failed to convert StringError to error_code.";
443443 }
444444
445 TEST(Error, createStringError) {
446 static const char *Bar = "bar";
447 static const std::error_code EC = errc::invalid_argument;
448 std::string Msg;
449 raw_string_ostream S(Msg);
450 logAllUnhandledErrors(createStringError(EC, "foo%s%d0x%" PRIx8, Bar, 1, 0xff),
451 S, "");
452 EXPECT_EQ(S.str(), "foobar10xff\n")
453 << "Unexpected createStringError() log result";
454
455 S.flush();
456 Msg.clear();
457 logAllUnhandledErrors(createStringError(EC, Bar), S, "");
458 EXPECT_EQ(S.str(), "bar\n")
459 << "Unexpected createStringError() (overloaded) log result";
460
461 S.flush();
462 Msg.clear();
463 auto Res = errorToErrorCode(createStringError(EC, "foo%s", Bar));
464 EXPECT_EQ(Res, EC)
465 << "Failed to convert createStringError() result to error_code.";
466 }
467
445468 // Test that the ExitOnError utility works as expected.
446469 TEST(Error, ExitOnError) {
447470 ExitOnError ExitOnErr;