llvm.org GIT mirror llvm / c985046
raw_ostream: add operator<< overload for std::error_code Summary: The main motivation for this is unit tests, which contain a large macro for pretty-printing std::error_code, and this macro is duplicated in every file that needs to do this. However, the functionality may be useful elsewhere too. In this patch I have reimplemented the existing ASSERT_NO_ERROR macros to reuse the new functionality, but I have kept the macro (as a one-liner) as it is slightly more readable than ASSERT_EQ(..., std::error_code()). Reviewers: sammccall, ilya-biryukov Subscribers: zturner, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D65643 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368849 91177308-0d34-0410-b5e6-96231b3b80d8 Pavel Labath a month ago
10 changed file(s) with 25 addition(s) and 78 deletion(s). Raw diff Collapse all Expand all
221221 }
222222
223223 raw_ostream &operator<<(double N);
224
225 raw_ostream &operator<<(std::error_code EC);
224226
225227 /// Output \p N in hexadecimal, without any prefix or padding.
226228 raw_ostream &write_hex(unsigned long long N);
138138 return *this;
139139 }
140140
141 raw_ostream &raw_ostream::operator<<(std::error_code EC) {
142 return *this << EC.message() << " (" << EC.category().name() << ':'
143 << EC.value() << ')';
144 }
145
141146 raw_ostream &raw_ostream::write_hex(unsigned long long N) {
142147 llvm::write_hex(*this, N, HexPrintStyle::Lower);
143148 return *this;
1616 using namespace llvm;
1717 namespace fs = llvm::sys::fs;
1818
19 #define ASSERT_NO_ERROR(x) \
20 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
21 SmallString<128> MessageStorage; \
22 raw_svector_ostream Message(MessageStorage); \
23 Message << #x ": did not return errc::success.\n" \
24 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
25 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
26 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
27 } else { \
28 }
19 #define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code())
2920
3021 class MagicTest : public testing::Test {
3122 protected:
932932
933933 class TestErrorCategory : public std::error_category {
934934 public:
935 const char *name() const noexcept override { return "error"; }
935 const char *name() const noexcept override { return "test_error"; }
936936 std::string message(int Condition) const override {
937937 switch (static_cast(Condition)) {
938938 case test_error_code::unspecified:
974974 0);
975975 }
976976
977 TEST(Error, error_codeErrorMessageTest) {
978 EXPECT_NONFATAL_FAILURE(
979 EXPECT_EQ(make_error_code(test_error_code::unspecified),
980 make_error_code(test_error_code::error_2)),
981 "Which is: An unknown error has occurred. (test_error:1)");
982 }
983
977984 } // namespace
1717 using namespace llvm;
1818 using namespace llvm::sys;
1919
20 #define ASSERT_NO_ERROR(x) \
21 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
22 SmallString<128> MessageStorage; \
23 raw_svector_ostream Message(MessageStorage); \
24 Message << #x ": did not return errc::success.\n" \
25 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
26 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
27 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
28 } else { \
29 }
20 #define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code())
3021
3122 namespace {
3223 TEST(FileOutputBuffer, Test) {
1515
1616 #include "gtest/gtest.h"
1717
18 #define ASSERT_NO_ERROR(x) \
19 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
20 SmallString<128> MessageStorage; \
21 raw_svector_ostream Message(MessageStorage); \
22 Message << #x ": did not return errc::success.\n" \
23 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
24 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
25 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
26 } else { \
27 }
18 #define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code())
2819
2920 using namespace llvm;
3021
3737 using namespace llvm;
3838 using namespace llvm::sys;
3939
40 #define ASSERT_NO_ERROR(x) \
41 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
42 SmallString<128> MessageStorage; \
43 raw_svector_ostream Message(MessageStorage); \
44 Message << #x ": did not return errc::success.\n" \
45 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
46 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
47 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
48 } else { \
49 }
50
51 #define ASSERT_ERROR(x) \
52 if (!x) { \
53 SmallString<128> MessageStorage; \
54 raw_svector_ostream Message(MessageStorage); \
55 Message << #x ": did not return a failure error code.\n"; \
56 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
57 }
40 #define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code())
41 #define ASSERT_ERROR(x) ASSERT_NE(x, std::error_code())
5842
5943 namespace {
6044
12641248 int FileDescriptor2;
12651249 SmallString<64> ResultPath;
12661250 ASSERT_NO_ERROR(fs::openFileForRead(Twine(TempPath), FileDescriptor2,
1267 fs::OF_None, &ResultPath))
1251 fs::OF_None, &ResultPath));
12681252
12691253 // If we succeeded, check that the paths are the same (modulo case):
12701254 if (!ResultPath.empty()) {
3434 #error sleep_for is not implemented on your platform.
3535 #endif
3636
37 #define ASSERT_NO_ERROR(x) \
38 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
39 SmallString<128> MessageStorage; \
40 raw_svector_ostream Message(MessageStorage); \
41 Message << #x ": did not return errc::success.\n" \
42 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
43 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
44 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
45 } else { \
46 }
37 #define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code())
38
4739 // From TestMain.cpp.
4840 extern const char *TestMainArgv0;
4941
1616 using namespace llvm;
1717 using namespace llvm::sys;
1818
19 #define ASSERT_NO_ERROR(x) \
20 do { \
21 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
22 errs() << #x ": did not return errc::success.\n" \
23 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
24 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
25 } \
26 } while (false)
19 #define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code())
2720
2821 namespace {
2922 std::error_code CreateFileWithContent(const SmallString<128> &FilePath,
1414
1515 using namespace llvm;
1616
17 #define ASSERT_NO_ERROR(x) \
18 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
19 SmallString<128> MessageStorage; \
20 raw_svector_ostream Message(MessageStorage); \
21 Message << #x ": did not return errc::success.\n" \
22 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
23 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
24 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
25 } else { \
26 }
17 #define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code())
2718
2819 namespace {
2920