From 96f91428c6ad2d19f1ce87ae160b78f52ed989fb Mon Sep 17 00:00:00 2001 From: Deniz Evrenci Date: Sat, 12 Oct 2019 02:44:20 +0900 Subject: [PATCH] Add defaulted copy and move operations to format_error and system_error (#1347) * Avoid weak vtables by providing a private virtual member function * Add warning Wweak-vtables to clang when FMT_PEDANTIC is on * Add defaulted copy and move operations to format_error and system_error Compiler generated copy operations are deprecated and move operations are not generated altogether. * Add warning Wdeprecated to clang when FMT_PEDANTIC is on --- CMakeLists.txt | 2 +- include/fmt/format.h | 8 ++++++++ test/core-test.cc | 5 +++++ test/test-assert.h | 5 +++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2af12e0b..71529e52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,7 +98,7 @@ endif () if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -pedantic -Wconversion - -Wno-sign-conversion) + -Wno-sign-conversion -Wdeprecated -Wweak-vtables) check_cxx_compiler_flag(-Wzero-as-null-pointer-constant HAS_NULLPTR_WARNING) if (HAS_NULLPTR_WARNING) set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} diff --git a/include/fmt/format.h b/include/fmt/format.h index e198233b..908e27e6 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -630,6 +630,10 @@ class FMT_API format_error : public std::runtime_error { explicit format_error(const char* message) : std::runtime_error(message) {} explicit format_error(const std::string& message) : std::runtime_error(message) {} + format_error(const format_error&) = default; + format_error& operator=(const format_error&) = default; + format_error(format_error&&) = default; + format_error& operator=(format_error&&) = default; ~format_error() FMT_NOEXCEPT; }; @@ -2689,6 +2693,10 @@ class FMT_API system_error : public std::runtime_error { : std::runtime_error("") { init(error_code, message, make_format_args(args...)); } + system_error(const system_error&) = default; + system_error& operator=(const system_error&) = default; + system_error(system_error&&) = default; + system_error& operator=(system_error&&) = default; ~system_error() FMT_NOEXCEPT; int error_code() const { return error_code_; } diff --git a/test/core-test.cc b/test/core-test.cc index 76612b99..931f44b7 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -127,8 +127,13 @@ TEST(BufferTest, Ctor) { struct dying_buffer : test_buffer { MOCK_METHOD0(die, void()); ~dying_buffer() { die(); } + + private: + virtual void avoid_weak_vtable(); }; +void dying_buffer::avoid_weak_vtable() {} + TEST(BufferTest, VirtualDtor) { typedef StrictMock stict_mock_buffer; stict_mock_buffer* mock_buffer = new stict_mock_buffer(); diff --git a/test/test-assert.h b/test/test-assert.h index 034a4ce6..c02dc0f2 100644 --- a/test/test-assert.h +++ b/test/test-assert.h @@ -14,8 +14,13 @@ class assertion_failure : public std::logic_error { public: explicit assertion_failure(const char* message) : std::logic_error(message) {} + + private: + virtual void avoid_weak_vtable(); }; +void assertion_failure::avoid_weak_vtable() {} + #define FMT_ASSERT(condition, message) \ if (!(condition)) throw assertion_failure(message);