diff --git a/include/pybind11/detail/descr.h b/include/pybind11/detail/descr.h index dcec1158c..15c6ee902 100644 --- a/include/pybind11/detail/descr.h +++ b/include/pybind11/detail/descr.h @@ -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 @@ -31,11 +34,13 @@ PYBIND11_NAMESPACE_BEGIN(detail) #ifdef PYBIND11_DETAIL_DESCR_SRC_LOC_ON -// Not using std::source_location because -// (https://en.cppreference.com/w/cpp/utility/source_location): -// 1. It is unspecified whether the copy/move constructors and the copy/move -// assignment operators of source_location are trivial and/or constexpr. -// 2. A no-op stub is needed (below). +// struct src_loc supports type_caster_odr_guard.h + +// Not using std::source_location because: +// 1. "It is unspecified whether the copy/move constructors and the copy/move +// 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 { const char *file; unsigned line; @@ -72,7 +77,7 @@ struct src_loc { #endif #ifdef PYBIND11_DETAIL_DESCR_SRC_LOC_ON -namespace { +namespace { // Activates TU-local features (see type_caster_odr_guard.h). #endif /* Concatenate type signatures at compile time */ diff --git a/include/pybind11/detail/type_caster_odr_guard.h b/include/pybind11/detail/type_caster_odr_guard.h index 327193bfc..d756336ec 100644 --- a/include/pybind11/detail/type_caster_odr_guard.h +++ b/include/pybind11/detail/type_caster_odr_guard.h @@ -9,8 +9,8 @@ // 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. -// The preconditions for PYBIND11_DETAIL_DESCR_SRC_LOC_ON happen to be a subset of -// the preconditions for PYBIND11_TYPE_CASTER_ODR_GUARD_ON. +// The preconditions for PYBIND11_DETAIL_DESCR_SRC_LOC_ON (descr.h) 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 @@ -58,6 +58,7 @@ inline std::string source_file_line_basename(const char *sfl) { 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 # define PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_THROW_DISABLED false # endif @@ -67,6 +68,7 @@ inline void type_caster_odr_guard_impl(const std::type_info &intrinsic_type_info bool throw_disabled) { std::string source_file_line_from_sloc = 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 # ifdef PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_PRINTF_ON // 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; 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) { } } + // The original author of this function is @amauryfa template static handle cast(CType &&src, return_value_policy policy, handle parent, Arg &&...arg) { if (translation_unit_local) { @@ -127,6 +132,7 @@ template tu_local_no_data_always_false type_caster_odr_guard::translation_unit_local = []() { + // Executed only once per process (e.g. when a PYBIND11_MODULE is initialized). type_caster_odr_guard_impl(typeid(IntrinsicType), TypeCasterType::name.sloc, PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_THROW_DISABLED);