From 8290a5a0dafd6cb24ba327d25c4a4321c80bba0f Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 29 Oct 2020 09:12:14 -0400 Subject: [PATCH] clang -Wnon-virtual-dtor compatibility (#2626) * Adding missing virtual destructors, to silence clang -Wnon-virtual-dtor warnings. Tested with clang version 9.0.1-12 under an Ubuntu-like OS. Originally discovered in the Google-internal environment. * adding -Wnon-virtual-dtor for GNU|Intel|Clang --- tests/CMakeLists.txt | 11 +++++++++-- tests/test_callbacks.cpp | 6 +++++- tests/test_exceptions.cpp | 7 +++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5ae0e44ac..dae8b5ad4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -243,8 +243,15 @@ function(pybind11_enable_warnings target_name) if(MSVC) target_compile_options(${target_name} PRIVATE /W4) elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)" AND NOT PYBIND11_CUDA_TESTS) - target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual - -Wdeprecated -Wundef) + target_compile_options( + ${target_name} + PRIVATE -Wall + -Wextra + -Wconversion + -Wcast-qual + -Wdeprecated + -Wundef + -Wnon-virtual-dtor) endif() if(PYBIND11_WERROR) diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index 71b88c44c..683dfb3ef 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -117,7 +117,11 @@ TEST_SUBMODULE(callbacks, m) { } }); - class AbstractBase { public: virtual unsigned int func() = 0; }; + class AbstractBase { + public: + virtual ~AbstractBase() = default; + virtual unsigned int func() = 0; + }; m.def("func_accepting_func_accepting_base", [](std::function) { }); struct MovableObject { diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp index b95ff4780..e27c16dfe 100644 --- a/tests/test_exceptions.cpp +++ b/tests/test_exceptions.cpp @@ -32,6 +32,13 @@ class MyException3 { public: explicit MyException3(const char * m) : message{m} {} virtual const char * what() const noexcept {return message.c_str();} + // Rule of 5 BEGIN: to preempt compiler warnings. + MyException3(const MyException3&) = default; + MyException3(MyException3&&) = default; + MyException3& operator=(const MyException3&) = default; + MyException3& operator=(MyException3&&) = default; + virtual ~MyException3() = default; + // Rule of 5 END. private: std::string message = ""; };