From 52a281ceef3433fc2dc73052ef04cc63bbf8c1c0 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Wed, 9 Sep 2020 18:51:16 +0100 Subject: [PATCH] Use defined for some preprocessor variables that might be undefined The variables PYBIND11_HAS_OPTIONAL, PYBIND11_HAS_EXP_OPTIONAL, PYBIND11_HAS_VARIANT, __clang__, __APPLE__ were not checked for defined in a minortity of instances. If the project using pybind11 sets -Wundef, the warnings will show. The test build is also modified to catch the problem. The errors looked like: ``` ext/pybind11/include/pybind11/stl.h:292:5: error: "PYBIND11_HAS_OPTIONAL" is not defined, evaluates to 0 [-Werror=undef] 292 | #if PYBIND11_HAS_OPTIONAL | ^~~~~~~~~~~~~~~~~~~~~ ext/pybind11/include/pybind11/stl.h:300:5: error: "PYBIND11_HAS_EXP_OPTIONAL" is not defined, evaluates to 0 [-Werror=undef] 300 | #if PYBIND11_HAS_EXP_OPTIONAL | ^~~~~~~~~~~~~~~~~~~~~~~~~ ext/pybind11/include/pybind11/stl.h:372:5: error: "PYBIND11_HAS_VARIANT" is not defined, evaluates to 0 [-Werror=undef] 372 | #if PYBIND11_HAS_VARIANT | ^~~~~~~~~~~~~~~~~~~~ ``` --- include/pybind11/stl.h | 6 +++--- tests/CMakeLists.txt | 2 +- tests/test_operator_overloading.cpp | 4 ++-- tests/test_stl.cpp | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 6c2bebda87..721bb669f0 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -289,7 +289,7 @@ template struct optional_caster { PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name + _("]")); }; -#if PYBIND11_HAS_OPTIONAL +#if defined(PYBIND11_HAS_OPTIONAL) template struct type_caster> : public optional_caster> {}; @@ -297,7 +297,7 @@ template<> struct type_caster : public void_caster {}; #endif -#if PYBIND11_HAS_EXP_OPTIONAL +#if defined(PYBIND11_HAS_EXP_OPTIONAL) template struct type_caster> : public optional_caster> {}; @@ -369,7 +369,7 @@ struct variant_caster> { PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster::name...) + _("]")); }; -#if PYBIND11_HAS_VARIANT +#if defined(PYBIND11_HAS_VARIANT) template struct type_caster> : variant_caster> { }; #endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 72de21018a..a39cdf56e2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -197,7 +197,7 @@ function(pybind11_enable_warnings target_name) target_compile_options(${target_name} PRIVATE /W4) elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)") target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual - -Wdeprecated) + -Wdeprecated -Wundef) endif() if(PYBIND11_WERROR) diff --git a/tests/test_operator_overloading.cpp b/tests/test_operator_overloading.cpp index f3c2eaafa9..d176c4644b 100644 --- a/tests/test_operator_overloading.cpp +++ b/tests/test_operator_overloading.cpp @@ -88,11 +88,11 @@ std::string abs(const Vector2&) { // Here, we suppress the warning using `#pragma diagnostic`. // Taken from: https://github.com/RobotLocomotion/drake/commit/aaf84b46 // TODO(eric): This could be resolved using a function / functor (e.g. `py::self()`). - #if (__APPLE__) && (__clang__) + #if defined(__APPLE__) && defined(__clang__) #if (__clang_major__ >= 10) && (__clang_minor__ >= 0) && (__clang_patchlevel__ >= 1) #pragma GCC diagnostic ignored "-Wself-assign-overloaded" #endif - #elif (__clang__) + #elif defined(__clang__) #if (__clang_major__ >= 7) #pragma GCC diagnostic ignored "-Wself-assign-overloaded" #endif diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 928635788e..b230717d2f 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -15,7 +15,7 @@ #include // Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14 -#if PYBIND11_HAS_VARIANT +#if defined(PYBIND11_HAS_VARIANT) using std::variant; #elif defined(PYBIND11_TEST_BOOST) && (!defined(_MSC_VER) || _MSC_VER >= 1910) # include