Use defined for some preprocessor variables that might be undefined (#2476)

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.
This commit is contained in:
Ciro Santilli 2020-09-10 18:58:26 +01:00 committed by GitHub
parent 621906b3e7
commit b47efd35fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 7 deletions

View File

@ -289,7 +289,7 @@ template<typename T> struct optional_caster {
PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name + _("]")); PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name + _("]"));
}; };
#if PYBIND11_HAS_OPTIONAL #if defined(PYBIND11_HAS_OPTIONAL)
template<typename T> struct type_caster<std::optional<T>> template<typename T> struct type_caster<std::optional<T>>
: public optional_caster<std::optional<T>> {}; : public optional_caster<std::optional<T>> {};
@ -297,7 +297,7 @@ template<> struct type_caster<std::nullopt_t>
: public void_caster<std::nullopt_t> {}; : public void_caster<std::nullopt_t> {};
#endif #endif
#if PYBIND11_HAS_EXP_OPTIONAL #if defined(PYBIND11_HAS_EXP_OPTIONAL)
template<typename T> struct type_caster<std::experimental::optional<T>> template<typename T> struct type_caster<std::experimental::optional<T>>
: public optional_caster<std::experimental::optional<T>> {}; : public optional_caster<std::experimental::optional<T>> {};
@ -369,7 +369,7 @@ struct variant_caster<V<Ts...>> {
PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster<Ts>::name...) + _("]")); PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster<Ts>::name...) + _("]"));
}; };
#if PYBIND11_HAS_VARIANT #if defined(PYBIND11_HAS_VARIANT)
template <typename... Ts> template <typename... Ts>
struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> { }; struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> { };
#endif #endif

View File

@ -216,7 +216,7 @@ function(pybind11_enable_warnings target_name)
target_compile_options(${target_name} PRIVATE /W4) target_compile_options(${target_name} PRIVATE /W4)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)" AND NOT PYBIND11_CUDA_TESTS) 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 target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual
-Wdeprecated) -Wdeprecated -Wundef)
endif() endif()
if(PYBIND11_WERROR) if(PYBIND11_WERROR)

View File

@ -88,11 +88,11 @@ std::string abs(const Vector2&) {
// Here, we suppress the warning using `#pragma diagnostic`. // Here, we suppress the warning using `#pragma diagnostic`.
// Taken from: https://github.com/RobotLocomotion/drake/commit/aaf84b46 // Taken from: https://github.com/RobotLocomotion/drake/commit/aaf84b46
// TODO(eric): This could be resolved using a function / functor (e.g. `py::self()`). // 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) #if (__clang_major__ >= 10) && (__clang_minor__ >= 0) && (__clang_patchlevel__ >= 1)
#pragma GCC diagnostic ignored "-Wself-assign-overloaded" #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
#endif #endif
#elif (__clang__) #elif defined(__clang__)
#if (__clang_major__ >= 7) #if (__clang_major__ >= 7)
#pragma GCC diagnostic ignored "-Wself-assign-overloaded" #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
#endif #endif

View File

@ -15,7 +15,7 @@
#include <string> #include <string>
// Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14 // 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; using std::variant;
#elif defined(PYBIND11_TEST_BOOST) && (!defined(_MSC_VER) || _MSC_VER >= 1910) #elif defined(PYBIND11_TEST_BOOST) && (!defined(_MSC_VER) || _MSC_VER >= 1910)
# include <boost/variant.hpp> # include <boost/variant.hpp>