diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 9f94f2902..1d38f3a47 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -95,7 +95,6 @@ private: public: bool load(handle src, bool convert) { return subcaster.load(src, convert); } static constexpr auto name = caster_t::name; - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static handle cast(const std::reference_wrapper &src, return_value_policy policy, handle parent) { // It is definitely wrong to take ownership of this pointer, so mask that rvp @@ -110,13 +109,12 @@ public: explicit operator std::reference_wrapper() { return cast_op(subcaster); } }; -#define PYBIND11_DETAIL_TYPE_CASTER_HEAD(type, py_name) \ +#define PYBIND11_TYPE_CASTER(type, py_name) \ protected: \ type value; \ \ public: \ - static constexpr auto name = py_name; -#define PYBIND11_DETAIL_TYPE_CASTER_TAIL(type) \ + static constexpr auto name = py_name; \ template >::value, \ @@ -138,22 +136,6 @@ public: template \ using cast_op_type = ::pybind11::detail::movable_cast_op_type -#ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_ON - -# define PYBIND11_TYPE_CASTER(type, py_name) \ - PYBIND11_DETAIL_TYPE_CASTER_HEAD(type, py_name) \ - static constexpr auto source_file_line \ - = ::pybind11::detail::tu_local_const_name(__FILE__ ":" PYBIND11_TOSTRING(__LINE__)); \ - PYBIND11_DETAIL_TYPE_CASTER_TAIL(type) - -#else - -# define PYBIND11_TYPE_CASTER(type, py_name) \ - PYBIND11_DETAIL_TYPE_CASTER_HEAD(type, py_name) \ - PYBIND11_DETAIL_TYPE_CASTER_TAIL(type) - -#endif - template using is_std_char_type = any_of, /* std::string */ #if defined(PYBIND11_HAS_U8STRING) @@ -346,7 +328,6 @@ public: using cast_op_type = void *&; explicit operator void *&() { return value; } static constexpr auto name = const_name("capsule"); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE private: void *value = nullptr; @@ -667,7 +648,6 @@ public: } static constexpr auto name = const_name(PYBIND11_STRING_NAME); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE template using cast_op_type = pybind11::detail::cast_op_type<_T>; }; @@ -712,7 +692,6 @@ public: static constexpr auto name = const_name("Tuple[") + concat(make_caster::name...) + const_name("]"); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE template using cast_op_type = type; @@ -885,7 +864,6 @@ struct move_only_holder_caster { return type_caster_base::cast_holder(ptr, std::addressof(src)); } static constexpr auto name = type_caster_base::name; - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE }; #ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index c95f5abb1..18adc40b1 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -27,7 +27,6 @@ public: using cast_op_type = value_and_holder &; explicit operator value_and_holder &() { return *value; } static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE private: value_and_holder *value = nullptr; diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index 95346cd08..52b37d591 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -616,7 +616,6 @@ template struct smart_holder_type_caster : smart_holder_type_caster_load, smart_holder_type_caster_class_hooks { static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE // static handle cast(T, ...) // is redundant (leads to ambiguous overloads). @@ -778,7 +777,6 @@ template struct smart_holder_type_caster> : smart_holder_type_caster_load, smart_holder_type_caster_class_hooks { static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static handle cast(const std::shared_ptr &src, return_value_policy policy, handle parent) { switch (policy) { @@ -843,7 +841,6 @@ template struct smart_holder_type_caster> : smart_holder_type_caster_load, smart_holder_type_caster_class_hooks { static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static handle cast(const std::shared_ptr &src, return_value_policy policy, handle parent) { @@ -864,7 +861,6 @@ template struct smart_holder_type_caster> : smart_holder_type_caster_load, smart_holder_type_caster_class_hooks { static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static handle cast(std::unique_ptr &&src, return_value_policy policy, handle parent) { if (policy != return_value_policy::automatic @@ -948,7 +944,6 @@ template struct smart_holder_type_caster> : smart_holder_type_caster_load, smart_holder_type_caster_class_hooks { static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static handle cast(std::unique_ptr &&src, return_value_policy policy, handle parent) { diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 556d7afbf..777fbb716 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -13,7 +13,6 @@ #include "common.h" #include "descr.h" #include "internals.h" -#include "type_caster_odr_guard.h" #include "typeid.h" #include @@ -912,7 +911,6 @@ class type_caster_base : public type_caster_generic { public: static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE type_caster_base() : type_caster_base(typeid(type)) {} explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) {} diff --git a/include/pybind11/detail/type_caster_odr_guard.h b/include/pybind11/detail/type_caster_odr_guard.h index 3ec839c87..4d36b3f0d 100644 --- a/include/pybind11/detail/type_caster_odr_guard.h +++ b/include/pybind11/detail/type_caster_odr_guard.h @@ -9,17 +9,14 @@ // The type_caster ODR guard feature requires Translation-Unit-local entities // (https://en.cppreference.com/w/cpp/language/tu_local), a C++20 feature, but // almost all tested C++17 compilers support this feature already. -// This highly correlates with the preconditions for PYBIND11_DETAIL_DESCR_SRC_LOC_ON. +// The preconditions for PYBIND11_DETAIL_DESCR_SRC_LOC_ON happen to be a subset of +// the preconditions for PYBIND11_TYPE_CASTER_ODR_GUARD_ON. #if !defined(PYBIND11_TYPE_CASTER_ODR_GUARD_ON) && !defined(PYBIND11_TYPE_CASTER_ODR_GUARD_OFF) \ && defined(PYBIND11_DETAIL_DESCR_SRC_LOC_ON) # define PYBIND11_TYPE_CASTER_ODR_GUARD_ON #endif -#ifndef PYBIND11_TYPE_CASTER_ODR_GUARD_ON - -# define PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE - -#else +#ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_ON # if !defined(PYBIND11_CPP20) && defined(__GNUC__) && !defined(__clang__) # pragma GCC diagnostic ignored "-Wsubobject-linkage" @@ -66,28 +63,18 @@ inline std::string source_file_line_basename(const char *sfl) { # endif inline void type_caster_odr_guard_impl(const std::type_info &intrinsic_type_info, - const char *source_file_line_from_macros, const src_loc &sloc, bool throw_disabled) { std::string source_file_line_from_sloc = std::string(sloc.file) + ':' + std::to_string(sloc.line); - // std::cout cannot be used here: static initialization could be incomplete. # define PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_PRINTF_OFF # ifdef PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_PRINTF_ON + // std::cout cannot be used here: static initialization could be incomplete. std::fprintf(stdout, "\nTYPE_CASTER_ODR_GUARD_IMPL %s %s\n", clean_type_id(intrinsic_type_info.name()).c_str(), - source_file_line_from_macros); - std::fprintf(stdout, - "%s %s %s\n", - (source_file_line_from_sloc == source_file_line_from_macros - ? " SLOC_SAME" - : " SLOC_DIFF"), - clean_type_id(intrinsic_type_info.name()).c_str(), source_file_line_from_sloc.c_str()); std::fflush(stdout); -# else - silence_unused_warnings(source_file_line_from_macros); # endif auto ins = type_caster_odr_guard_registry().insert( {std::type_index(intrinsic_type_info), source_file_line_from_sloc}); @@ -112,66 +99,12 @@ inline void type_caster_odr_guard_impl(const std::type_info &intrinsic_type_info namespace { -template -struct tu_local_descr { - char text[N + 1]{'\0'}; - src_loc sloc; - - explicit constexpr tu_local_descr(src_loc sloc = src_loc::here()) : sloc(sloc) {} - // NOLINTNEXTLINE(google-explicit-constructor) - constexpr tu_local_descr(char const (&s)[N + 1], src_loc sloc = src_loc::here()) - : tu_local_descr(s, make_index_sequence(), sloc) {} - - template - constexpr tu_local_descr(char const (&s)[N + 1], - index_sequence, - src_loc sloc = src_loc::here()) - : text{s[Is]..., '\0'}, sloc(sloc) {} - - template - // NOLINTNEXTLINE(google-explicit-constructor) - constexpr tu_local_descr(char c, Chars... cs, src_loc sloc = src_loc::here()) - : text{c, static_cast(cs)..., '\0'}, sloc(sloc) {} -}; - -template -constexpr tu_local_descr tu_local_const_name(char const (&text)[N], - src_loc sloc = src_loc::here()) { - return tu_local_descr(text, sloc); -} -constexpr tu_local_descr<0> tu_local_const_name(char const (&)[1], - src_loc sloc = src_loc::here()) { - return tu_local_descr<0>(sloc); -} - struct tu_local_no_data_always_false { explicit operator bool() const noexcept { return false; } }; } // namespace -# ifndef PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT -# define PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT -# endif - -template -struct get_type_caster_source_file_line { -# ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT - static_assert(TypeCasterType::source_file_line, - "PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE is MISSING: Please add that macro to the " - "TypeCasterType, or undefine PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT"); -# else - static constexpr auto source_file_line = tu_local_const_name("UNAVAILABLE"); -# endif -}; - -template -struct get_type_caster_source_file_line< - TypeCasterType, - enable_if_t::value>> { - static constexpr auto source_file_line = TypeCasterType::source_file_line; -}; - template struct type_caster_odr_guard : TypeCasterType { static tu_local_no_data_always_false translation_unit_local; @@ -194,19 +127,13 @@ template tu_local_no_data_always_false type_caster_odr_guard::translation_unit_local = []() { - type_caster_odr_guard_impl( - typeid(IntrinsicType), - get_type_caster_source_file_line::source_file_line.text, - TypeCasterType::name.sloc, - PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_THROW_DISABLED); + type_caster_odr_guard_impl(typeid(IntrinsicType), + TypeCasterType::name.sloc, + PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_THROW_DISABLED); return tu_local_no_data_always_false(); }(); PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) -# define PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE \ - static constexpr auto source_file_line \ - = ::pybind11::detail::tu_local_const_name(__FILE__ ":" PYBIND11_TOSTRING(__LINE__)); - #endif diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index 93eb6ee0a..beb50266e 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -392,7 +392,6 @@ public: } static constexpr auto name = props::descriptor; - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE // NOLINTNEXTLINE(google-explicit-constructor) operator Type *() { return &value; } @@ -437,7 +436,6 @@ public: } static constexpr auto name = props::descriptor; - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE // Explicitly delete these: support python -> C++ conversion on these (i.e. these can be return // types but not bound arguments). We still provide them (with an explicitly delete) so that @@ -625,7 +623,6 @@ public: } static constexpr auto name = props::descriptor; - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE // Explicitly delete these: support python -> C++ conversion on these (i.e. these can be return // types but not bound arguments). We still provide them (with an explicitly delete) so that diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index 654e7f7f1..9c0ec73f6 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -28,7 +28,6 @@ template <> class type_caster { public: static constexpr auto name = const_name(); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE // Input is unimportant, a new value will always be constructed based on the // cast operator. diff --git a/tests/test_copy_move.cpp b/tests/test_copy_move.cpp index 82e9eec29..28c244564 100644 --- a/tests/test_copy_move.cpp +++ b/tests/test_copy_move.cpp @@ -134,7 +134,6 @@ protected: public: static constexpr auto name = const_name("CopyOnlyInt"); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE bool load(handle src, bool) { value = CopyOnlyInt(src.cast()); return true; diff --git a/tests/test_type_caster_odr_guard_1.cpp b/tests/test_type_caster_odr_guard_1.cpp index 0851b0ce2..2deaf1754 100644 --- a/tests/test_type_caster_odr_guard_1.cpp +++ b/tests/test_type_caster_odr_guard_1.cpp @@ -15,7 +15,6 @@ struct type_mrc { struct minimal_real_caster { static constexpr auto name = py::detail::const_name(); - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE static py::handle cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) { diff --git a/tests/test_type_caster_odr_guard_2.cpp b/tests/test_type_caster_odr_guard_2.cpp index ae4631a99..e255f2a23 100644 --- a/tests/test_type_caster_odr_guard_2.cpp +++ b/tests/test_type_caster_odr_guard_2.cpp @@ -15,9 +15,6 @@ struct type_mrc { struct minimal_real_caster { static constexpr auto name = py::detail::const_name(); -#ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT - PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE -#endif static py::handle cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) {