[llvm] r368849 - raw_ostream: add operator<< overload for std::error_code
Pavel Labath via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 14 06:33:28 PDT 2019
Author: labath
Date: Wed Aug 14 06:33:28 2019
New Revision: 368849
URL: http://llvm.org/viewvc/llvm-project?rev=368849&view=rev
Log:
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
Modified:
llvm/trunk/include/llvm/Support/raw_ostream.h
llvm/trunk/lib/Support/raw_ostream.cpp
llvm/trunk/unittests/BinaryFormat/TestFileMagic.cpp
llvm/trunk/unittests/Support/ErrorTest.cpp
llvm/trunk/unittests/Support/FileOutputBufferTest.cpp
llvm/trunk/unittests/Support/Host.cpp
llvm/trunk/unittests/Support/Path.cpp
llvm/trunk/unittests/Support/ProgramTest.cpp
llvm/trunk/unittests/Support/ReplaceFileTest.cpp
llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp
Modified: llvm/trunk/include/llvm/Support/raw_ostream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/raw_ostream.h?rev=368849&r1=368848&r2=368849&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
+++ llvm/trunk/include/llvm/Support/raw_ostream.h Wed Aug 14 06:33:28 2019
@@ -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);
Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=368849&r1=368848&r2=368849&view=diff
==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Wed Aug 14 06:33:28 2019
@@ -139,6 +139,11 @@ raw_ostream &raw_ostream::operator<<(lon
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;
Modified: llvm/trunk/unittests/BinaryFormat/TestFileMagic.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/BinaryFormat/TestFileMagic.cpp?rev=368849&r1=368848&r2=368849&view=diff
==============================================================================
--- llvm/trunk/unittests/BinaryFormat/TestFileMagic.cpp (original)
+++ llvm/trunk/unittests/BinaryFormat/TestFileMagic.cpp Wed Aug 14 06:33:28 2019
@@ -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:
Modified: llvm/trunk/unittests/Support/ErrorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorTest.cpp?rev=368849&r1=368848&r2=368849&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ErrorTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrorTest.cpp Wed Aug 14 06:33:28 2019
@@ -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<test_error_code>(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
Modified: llvm/trunk/unittests/Support/FileOutputBufferTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/FileOutputBufferTest.cpp?rev=368849&r1=368848&r2=368849&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/FileOutputBufferTest.cpp (original)
+++ llvm/trunk/unittests/Support/FileOutputBufferTest.cpp Wed Aug 14 06:33:28 2019
@@ -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) {
Modified: llvm/trunk/unittests/Support/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Host.cpp?rev=368849&r1=368848&r2=368849&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Host.cpp (original)
+++ llvm/trunk/unittests/Support/Host.cpp Wed Aug 14 06:33:28 2019
@@ -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;
Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=368849&r1=368848&r2=368849&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Wed Aug 14 06:33:28 2019
@@ -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()) {
Modified: llvm/trunk/unittests/Support/ProgramTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ProgramTest.cpp?rev=368849&r1=368848&r2=368849&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ProgramTest.cpp (original)
+++ llvm/trunk/unittests/Support/ProgramTest.cpp Wed Aug 14 06:33:28 2019
@@ -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;
Modified: llvm/trunk/unittests/Support/ReplaceFileTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ReplaceFileTest.cpp?rev=368849&r1=368848&r2=368849&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ReplaceFileTest.cpp (original)
+++ llvm/trunk/unittests/Support/ReplaceFileTest.cpp Wed Aug 14 06:33:28 2019
@@ -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,
Modified: llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp?rev=368849&r1=368848&r2=368849&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp (original)
+++ llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp Wed Aug 14 06:33:28 2019
@@ -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 {
More information about the llvm-commits
mailing list