Remove PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE macro completely. Some small extra cleanup.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-07-01 18:48:23 -07:00
parent ae38889322
commit 13076e4fb7
10 changed files with 9 additions and 121 deletions

View File

@ -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<type> &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<type>() { return cast_op<type &>(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 <typename T_, \
::pybind11::detail::enable_if_t< \
std::is_same<type, ::pybind11::detail::remove_cv_t<T_>>::value, \
@ -138,22 +136,6 @@ public:
template <typename T_> \
using cast_op_type = ::pybind11::detail::movable_cast_op_type<T_>
#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 <typename CharT>
using is_std_char_type = any_of<std::is_same<CharT, char>, /* 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 <typename _T>
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<Ts>::name...) + const_name("]");
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
template <typename T>
using cast_op_type = type;
@ -885,7 +864,6 @@ struct move_only_holder_caster {
return type_caster_base<type>::cast_holder(ptr, std::addressof(src));
}
static constexpr auto name = type_caster_base<type>::name;
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
};
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT

View File

@ -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<value_and_holder>();
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
private:
value_and_holder *value = nullptr;

View File

@ -616,7 +616,6 @@ template <typename T>
struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
smart_holder_type_caster_class_hooks {
static constexpr auto name = const_name<T>();
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
// static handle cast(T, ...)
// is redundant (leads to ambiguous overloads).
@ -778,7 +777,6 @@ template <typename T>
struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_load<T>,
smart_holder_type_caster_class_hooks {
static constexpr auto name = const_name<T>();
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
static handle cast(const std::shared_ptr<T> &src, return_value_policy policy, handle parent) {
switch (policy) {
@ -843,7 +841,6 @@ template <typename T>
struct smart_holder_type_caster<std::shared_ptr<T const>> : smart_holder_type_caster_load<T>,
smart_holder_type_caster_class_hooks {
static constexpr auto name = const_name<T>();
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
static handle
cast(const std::shared_ptr<T const> &src, return_value_policy policy, handle parent) {
@ -864,7 +861,6 @@ template <typename T, typename D>
struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caster_load<T>,
smart_holder_type_caster_class_hooks {
static constexpr auto name = const_name<T>();
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
static handle cast(std::unique_ptr<T, D> &&src, return_value_policy policy, handle parent) {
if (policy != return_value_policy::automatic
@ -948,7 +944,6 @@ template <typename T, typename D>
struct smart_holder_type_caster<std::unique_ptr<T const, D>>
: smart_holder_type_caster_load<T>, smart_holder_type_caster_class_hooks {
static constexpr auto name = const_name<T>();
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
static handle
cast(std::unique_ptr<T const, D> &&src, return_value_policy policy, handle parent) {

View File

@ -13,7 +13,6 @@
#include "common.h"
#include "descr.h"
#include "internals.h"
#include "type_caster_odr_guard.h"
#include "typeid.h"
#include <cstdint>
@ -912,7 +911,6 @@ class type_caster_base : public type_caster_generic {
public:
static constexpr auto name = const_name<type>();
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) {}

View File

@ -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 <size_t N, typename... Ts>
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<N>(), sloc) {}
template <size_t... Is>
constexpr tu_local_descr(char const (&s)[N + 1],
index_sequence<Is...>,
src_loc sloc = src_loc::here())
: text{s[Is]..., '\0'}, sloc(sloc) {}
template <typename... Chars>
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr tu_local_descr(char c, Chars... cs, src_loc sloc = src_loc::here())
: text{c, static_cast<char>(cs)..., '\0'}, sloc(sloc) {}
};
template <size_t N>
constexpr tu_local_descr<N - 1> tu_local_const_name(char const (&text)[N],
src_loc sloc = src_loc::here()) {
return tu_local_descr<N - 1>(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 <typename TypeCasterType, typename SFINAE = void>
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 <typename TypeCasterType>
struct get_type_caster_source_file_line<
TypeCasterType,
enable_if_t<std::is_class<decltype(TypeCasterType::source_file_line)>::value>> {
static constexpr auto source_file_line = TypeCasterType::source_file_line;
};
template <typename IntrinsicType, typename TypeCasterType>
struct type_caster_odr_guard : TypeCasterType {
static tu_local_no_data_always_false translation_unit_local;
@ -194,19 +127,13 @@ template <typename IntrinsicType, typename TypeCasterType>
tu_local_no_data_always_false
type_caster_odr_guard<IntrinsicType, TypeCasterType>::translation_unit_local
= []() {
type_caster_odr_guard_impl(
typeid(IntrinsicType),
get_type_caster_source_file_line<TypeCasterType>::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

View File

@ -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

View File

@ -28,7 +28,6 @@ template <>
class type_caster<ConstRefCasted> {
public:
static constexpr auto name = const_name<ConstRefCasted>();
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
// Input is unimportant, a new value will always be constructed based on the
// cast operator.

View File

@ -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<int>());
return true;

View File

@ -15,7 +15,6 @@ struct type_mrc {
struct minimal_real_caster {
static constexpr auto name = py::detail::const_name<type_mrc>();
PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE
static py::handle
cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) {

View File

@ -15,9 +15,6 @@ struct type_mrc {
struct minimal_real_caster {
static constexpr auto name = py::detail::const_name<type_mrc>();
#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*/) {