Skip to content

Commit ec4e47c

Browse files
committed
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 | ^~~~~~~~~~~~~~~~~~~~ ```
1 parent fbc7563 commit ec4e47c

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

include/pybind11/stl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,15 @@ template<typename T> struct optional_caster {
289289
PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name + _("]"));
290290
};
291291

292-
#if PYBIND11_HAS_OPTIONAL
292+
#if defined(PYBIND11_HAS_OPTIONAL)
293293
template<typename T> struct type_caster<std::optional<T>>
294294
: public optional_caster<std::optional<T>> {};
295295

296296
template<> struct type_caster<std::nullopt_t>
297297
: public void_caster<std::nullopt_t> {};
298298
#endif
299299

300-
#if PYBIND11_HAS_EXP_OPTIONAL
300+
#if defined(PYBIND11_HAS_EXP_OPTIONAL)
301301
template<typename T> struct type_caster<std::experimental::optional<T>>
302302
: public optional_caster<std::experimental::optional<T>> {};
303303

@@ -369,7 +369,7 @@ struct variant_caster<V<Ts...>> {
369369
PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster<Ts>::name...) + _("]"));
370370
};
371371

372-
#if PYBIND11_HAS_VARIANT
372+
#if defined(PYBIND11_HAS_VARIANT)
373373
template <typename... Ts>
374374
struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> { };
375375
#endif

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function(pybind11_enable_warnings target_name)
197197
target_compile_options(${target_name} PRIVATE /W4)
198198
elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)")
199199
target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual
200-
-Wdeprecated)
200+
-Wdeprecated -Wundef)
201201
endif()
202202

203203
if(PYBIND11_WERROR)

tests/test_operator_overloading.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ std::string abs(const Vector2&) {
8888
// Here, we suppress the warning using `#pragma diagnostic`.
8989
// Taken from: https://github.com/RobotLocomotion/drake/commit/aaf84b46
9090
// TODO(eric): This could be resolved using a function / functor (e.g. `py::self()`).
91-
#if (__APPLE__) && (__clang__)
91+
#if defined(__APPLE__) && defined(__clang__)
9292
#if (__clang_major__ >= 10) && (__clang_minor__ >= 0) && (__clang_patchlevel__ >= 1)
9393
#pragma GCC diagnostic ignored "-Wself-assign-overloaded"
9494
#endif
95-
#elif (__clang__)
95+
#elif defined(__clang__)
9696
#if (__clang_major__ >= 7)
9797
#pragma GCC diagnostic ignored "-Wself-assign-overloaded"
9898
#endif

0 commit comments

Comments
 (0)