From c9850460b726c1764a6bbd18eca5219f10a28133 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Wed, 14 Aug 2019 13:33:28 +0000 Subject: [PATCH] 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 --- include/llvm/Support/raw_ostream.h | 2 ++ lib/Support/raw_ostream.cpp | 5 +++++ unittests/BinaryFormat/TestFileMagic.cpp | 11 +--------- unittests/Support/ErrorTest.cpp | 9 +++++++- unittests/Support/FileOutputBufferTest.cpp | 11 +--------- unittests/Support/Host.cpp | 11 +--------- unittests/Support/Path.cpp | 22 +++----------------- unittests/Support/ProgramTest.cpp | 12 ++--------- unittests/Support/ReplaceFileTest.cpp | 9 +------- unittests/Support/raw_pwrite_stream_test.cpp | 11 +--------- 10 files changed, 25 insertions(+), 78 deletions(-) diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h index 4a6b51ac609..3ac70b8a705 100644 --- a/include/llvm/Support/raw_ostream.h +++ b/include/llvm/Support/raw_ostream.h @@ -223,6 +223,8 @@ public: raw_ostream &operator<<(double N); + raw_ostream &operator<<(std::error_code EC); + /// Output \p N in hexadecimal, without any prefix or padding. raw_ostream &write_hex(unsigned long long N); diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index b9989371f5e..4a2d6d20951 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -139,6 +139,11 @@ raw_ostream &raw_ostream::operator<<(long long N) { return *this; } +raw_ostream &raw_ostream::operator<<(std::error_code EC) { + return *this << EC.message() << " (" << EC.category().name() << ':' + << EC.value() << ')'; +} + raw_ostream &raw_ostream::write_hex(unsigned long long N) { llvm::write_hex(*this, N, HexPrintStyle::Lower); return *this; diff --git a/unittests/BinaryFormat/TestFileMagic.cpp b/unittests/BinaryFormat/TestFileMagic.cpp index 9ed707bb5b3..3c0fe277484 100644 --- a/unittests/BinaryFormat/TestFileMagic.cpp +++ b/unittests/BinaryFormat/TestFileMagic.cpp @@ -17,16 +17,7 @@ using namespace llvm; namespace fs = llvm::sys::fs; -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code()) class MagicTest : public testing::Test { protected: diff --git a/unittests/Support/ErrorTest.cpp b/unittests/Support/ErrorTest.cpp index c4a9f3e5168..ec5c30b5fba 100644 --- a/unittests/Support/ErrorTest.cpp +++ b/unittests/Support/ErrorTest.cpp @@ -933,7 +933,7 @@ public: class TestErrorCategory : public std::error_category { public: - const char *name() const noexcept override { return "error"; } + const char *name() const noexcept override { return "test_error"; } std::string message(int Condition) const override { switch (static_cast(Condition)) { case test_error_code::unspecified: @@ -975,4 +975,11 @@ TEST(Error, SubtypeStringErrorTest) { 0); } +TEST(Error, error_codeErrorMessageTest) { + EXPECT_NONFATAL_FAILURE( + EXPECT_EQ(make_error_code(test_error_code::unspecified), + make_error_code(test_error_code::error_2)), + "Which is: An unknown error has occurred. (test_error:1)"); +} + } // namespace diff --git a/unittests/Support/FileOutputBufferTest.cpp b/unittests/Support/FileOutputBufferTest.cpp index 8afc2125ef4..24b6b0d4b2a 100644 --- a/unittests/Support/FileOutputBufferTest.cpp +++ b/unittests/Support/FileOutputBufferTest.cpp @@ -18,16 +18,7 @@ using namespace llvm; using namespace llvm::sys; -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code()) namespace { TEST(FileOutputBuffer, Test) { diff --git a/unittests/Support/Host.cpp b/unittests/Support/Host.cpp index ec9dd951a9f..287daba1db6 100644 --- a/unittests/Support/Host.cpp +++ b/unittests/Support/Host.cpp @@ -16,16 +16,7 @@ #include "gtest/gtest.h" -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code()) using namespace llvm; diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index 1802b0031ad..101f7608004 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -38,24 +38,8 @@ using namespace llvm; using namespace llvm::sys; -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } - -#define ASSERT_ERROR(x) \ - if (!x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return a failure error code.\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } +#define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code()) +#define ASSERT_ERROR(x) ASSERT_NE(x, std::error_code()) namespace { @@ -1265,7 +1249,7 @@ TEST_F(FileSystemTest, OpenFileForRead) { int FileDescriptor2; SmallString<64> ResultPath; ASSERT_NO_ERROR(fs::openFileForRead(Twine(TempPath), FileDescriptor2, - fs::OF_None, &ResultPath)) + fs::OF_None, &ResultPath)); // If we succeeded, check that the paths are the same (modulo case): if (!ResultPath.empty()) { diff --git a/unittests/Support/ProgramTest.cpp b/unittests/Support/ProgramTest.cpp index 85a1d532c9a..6a1438bac9f 100644 --- a/unittests/Support/ProgramTest.cpp +++ b/unittests/Support/ProgramTest.cpp @@ -35,16 +35,8 @@ void sleep_for(unsigned int seconds) { #error sleep_for is not implemented on your platform. #endif -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code()) + // From TestMain.cpp. extern const char *TestMainArgv0; diff --git a/unittests/Support/ReplaceFileTest.cpp b/unittests/Support/ReplaceFileTest.cpp index d2273d77f5e..9a870a78360 100644 --- a/unittests/Support/ReplaceFileTest.cpp +++ b/unittests/Support/ReplaceFileTest.cpp @@ -17,14 +17,7 @@ using namespace llvm; using namespace llvm::sys; -#define ASSERT_NO_ERROR(x) \ - do { \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - errs() << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - } \ - } while (false) +#define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code()) namespace { std::error_code CreateFileWithContent(const SmallString<128> &FilePath, diff --git a/unittests/Support/raw_pwrite_stream_test.cpp b/unittests/Support/raw_pwrite_stream_test.cpp index 459eb940a43..b2a7434fb53 100644 --- a/unittests/Support/raw_pwrite_stream_test.cpp +++ b/unittests/Support/raw_pwrite_stream_test.cpp @@ -15,16 +15,7 @@ using namespace llvm; -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code()) namespace { -- 2.40.0