Some more work on source code comments.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-07-11 19:39:47 -07:00
parent 2a14ee6e48
commit 38688ec32c
2 changed files with 19 additions and 8 deletions

View File

@ -1,3 +1,6 @@
// Copyright (c) 2022 The Pybind Development Team.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/* /*
pybind11/detail/descr.h: Helper type for concatenating type signatures at compile time pybind11/detail/descr.h: Helper type for concatenating type signatures at compile time
@ -31,11 +34,13 @@ PYBIND11_NAMESPACE_BEGIN(detail)
#ifdef PYBIND11_DETAIL_DESCR_SRC_LOC_ON #ifdef PYBIND11_DETAIL_DESCR_SRC_LOC_ON
// Not using std::source_location because // struct src_loc supports type_caster_odr_guard.h
// (https://en.cppreference.com/w/cpp/utility/source_location):
// 1. It is unspecified whether the copy/move constructors and the copy/move // Not using std::source_location because:
// assignment operators of source_location are trivial and/or constexpr. // 1. "It is unspecified whether the copy/move constructors and the copy/move
// 2. A no-op stub is needed (below). // assignment operators of source_location are trivial and/or constexpr."
// (https://en.cppreference.com/w/cpp/utility/source_location).
// 2. A matching no-op stub is needed (below) to avoid code duplication.
struct src_loc { struct src_loc {
const char *file; const char *file;
unsigned line; unsigned line;
@ -72,7 +77,7 @@ struct src_loc {
#endif #endif
#ifdef PYBIND11_DETAIL_DESCR_SRC_LOC_ON #ifdef PYBIND11_DETAIL_DESCR_SRC_LOC_ON
namespace { namespace { // Activates TU-local features (see type_caster_odr_guard.h).
#endif #endif
/* Concatenate type signatures at compile time */ /* Concatenate type signatures at compile time */

View File

@ -9,8 +9,8 @@
// The type_caster ODR guard feature requires Translation-Unit-local entities // 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 // (https://en.cppreference.com/w/cpp/language/tu_local), a C++20 feature, but
// almost all tested C++17 compilers support this feature already. // almost all tested C++17 compilers support this feature already.
// The preconditions for PYBIND11_DETAIL_DESCR_SRC_LOC_ON happen to be a subset of // The preconditions for PYBIND11_DETAIL_DESCR_SRC_LOC_ON (descr.h) happen to be
// the preconditions for PYBIND11_TYPE_CASTER_ODR_GUARD_ON. // 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) \ #if !defined(PYBIND11_TYPE_CASTER_ODR_GUARD_ON) && !defined(PYBIND11_TYPE_CASTER_ODR_GUARD_OFF) \
&& defined(PYBIND11_DETAIL_DESCR_SRC_LOC_ON) && defined(PYBIND11_DETAIL_DESCR_SRC_LOC_ON)
# define PYBIND11_TYPE_CASTER_ODR_GUARD_ON # define PYBIND11_TYPE_CASTER_ODR_GUARD_ON
@ -58,6 +58,7 @@ inline std::string source_file_line_basename(const char *sfl) {
return std::string(sfl + i_base); return std::string(sfl + i_base);
} }
// This macro is for cooperation with test_type_caster_odr_guard_?.cpp
# ifndef PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_THROW_DISABLED # ifndef PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_THROW_DISABLED
# define PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_THROW_DISABLED false # define PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_THROW_DISABLED false
# endif # endif
@ -67,6 +68,7 @@ inline void type_caster_odr_guard_impl(const std::type_info &intrinsic_type_info
bool throw_disabled) { bool throw_disabled) {
std::string source_file_line_from_sloc std::string source_file_line_from_sloc
= std::string(sloc.file) + ':' + std::to_string(sloc.line); = std::string(sloc.file) + ':' + std::to_string(sloc.line);
// This macro is purely for debugging.
# define PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_PRINTF_OFF # define PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_PRINTF_OFF
# ifdef PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_PRINTF_ON # ifdef PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_PRINTF_ON
// std::cout cannot be used here: static initialization could be incomplete. // std::cout cannot be used here: static initialization could be incomplete.
@ -110,10 +112,13 @@ struct type_caster_odr_guard : TypeCasterType {
static tu_local_no_data_always_false translation_unit_local; static tu_local_no_data_always_false translation_unit_local;
type_caster_odr_guard() { type_caster_odr_guard() {
// Possibly, good optimizers will elide this `if` (and below) completely.
// It is needed only to trigger the TU-local mechanisms.
if (translation_unit_local) { if (translation_unit_local) {
} }
} }
// The original author of this function is @amauryfa
template <typename CType, typename... Arg> template <typename CType, typename... Arg>
static handle cast(CType &&src, return_value_policy policy, handle parent, Arg &&...arg) { static handle cast(CType &&src, return_value_policy policy, handle parent, Arg &&...arg) {
if (translation_unit_local) { if (translation_unit_local) {
@ -127,6 +132,7 @@ template <typename IntrinsicType, typename TypeCasterType>
tu_local_no_data_always_false tu_local_no_data_always_false
type_caster_odr_guard<IntrinsicType, TypeCasterType>::translation_unit_local type_caster_odr_guard<IntrinsicType, TypeCasterType>::translation_unit_local
= []() { = []() {
// Executed only once per process (e.g. when a PYBIND11_MODULE is initialized).
type_caster_odr_guard_impl(typeid(IntrinsicType), type_caster_odr_guard_impl(typeid(IntrinsicType),
TypeCasterType::name.sloc, TypeCasterType::name.sloc,
PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_THROW_DISABLED); PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_THROW_DISABLED);