From 3c38b9d79ecae5be028ad921208f3e062668418d Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 8 Feb 2022 14:37:15 -0800 Subject: [PATCH 1/2] Adding readability-qualified-auto to .clang-tidy Ported from @henryiii's https://github.com/pybind/pybind11/pull/3683/commits/287527f705c8badee8ba9f6278d691fe0450ae43 --- .clang-tidy | 1 + include/pybind11/attr.h | 2 +- include/pybind11/cast.h | 4 +- include/pybind11/detail/class.h | 45 +++++++++++----------- include/pybind11/detail/internals.h | 4 +- include/pybind11/detail/type_caster_base.h | 32 +++++++-------- include/pybind11/embed.h | 2 +- include/pybind11/functional.h | 4 +- include/pybind11/gil.h | 4 +- include/pybind11/numpy.h | 6 +-- include/pybind11/pybind11.h | 32 +++++++-------- include/pybind11/pytypes.h | 2 +- include/pybind11/stl/filesystem.h | 4 +- include/pybind11/stl_bind.h | 4 +- tests/test_buffers.cpp | 2 +- tests/test_builtin_casters.cpp | 2 +- tests/test_call_policies.cpp | 2 +- tests/test_callbacks.cpp | 2 +- tests/test_class.cpp | 2 +- tests/test_embed/test_interpreter.cpp | 4 +- tests/test_numpy_array.cpp | 2 +- tests/test_numpy_dtypes.cpp | 24 ++++++------ tests/test_pytypes.cpp | 2 +- tests/test_smart_ptr.cpp | 4 +- tests/test_stl_binders.cpp | 23 ++++++----- 25 files changed, 107 insertions(+), 108 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 97bbd53968..1957b8690e 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -40,6 +40,7 @@ readability-implicit-bool-conversion, readability-make-member-function-const, readability-misplaced-array-index, readability-non-const-parameter, +readability-qualified-auto, readability-redundant-function-ptr-dereference, readability-redundant-smartptr-get, readability-redundant-string-cstr, diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index f23582b1bf..669c72614e 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -311,7 +311,7 @@ struct type_record { bool is_final : 1; PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) { - auto base_info = detail::get_type_info(base, false); + auto *base_info = detail::get_type_info(base, false); if (!base_info) { std::string tname(base.name()); detail::clean_type_id(tname); diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 71b8c2f0ff..cfec7e5f0e 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -251,7 +251,7 @@ template <> class type_caster : public type_caster { } /* Check if this is a C++ type */ - auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr()); + const auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr()); if (bases.size() == 1) { // Only allowing loading from a single-value type value = values_and_holders(reinterpret_cast(h.ptr())).begin()->value_ptr(); return true; @@ -306,7 +306,7 @@ template <> class type_caster { #else // Alternate approach for CPython: this does the same as the above, but optimized // using the CPython API so as to avoid an unneeded attribute lookup. - else if (auto tp_as_number = src.ptr()->ob_type->tp_as_number) { + else if (auto *tp_as_number = src.ptr()->ob_type->tp_as_number) { if (PYBIND11_NB_BOOL(tp_as_number)) { res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr()); } diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 28edc12903..e50c8060fb 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -65,7 +65,7 @@ inline PyTypeObject *make_static_property_type() { issue no Python C API calls which could potentially invoke the garbage collector (the GC will call type_traverse(), which will in turn find the newly constructed type in an invalid state) */ - auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); + auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); if (!heap_type) { pybind11_fail("make_static_property_type(): error allocating type!"); } @@ -75,7 +75,7 @@ inline PyTypeObject *make_static_property_type() { heap_type->ht_qualname = name_obj.inc_ref().ptr(); #endif - auto type = &heap_type->ht_type; + auto *type = &heap_type->ht_type; type->tp_name = name; type->tp_base = type_incref(&PyProperty_Type); type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; @@ -130,7 +130,7 @@ extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyOb // 1. `Type.static_prop = value` --> descr_set: `Type.static_prop.__set__(value)` // 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop` // 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment - const auto static_prop = (PyObject *) get_internals().static_property_type; + auto *const static_prop = (PyObject *) get_internals().static_property_type; const auto call_descr_set = (descr != nullptr) && (value != nullptr) && (PyObject_IsInstance(descr, static_prop) != 0) && (PyObject_IsInstance(value, static_prop) == 0); @@ -179,7 +179,7 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P } // This must be a pybind11 instance - auto instance = reinterpret_cast(self); + auto *instance = reinterpret_cast(self); // Ensure that the base __init__ function(s) were called for (const auto &vh : values_and_holders(instance)) { @@ -245,7 +245,7 @@ inline PyTypeObject* make_default_metaclass() { issue no Python C API calls which could potentially invoke the garbage collector (the GC will call type_traverse(), which will in turn find the newly constructed type in an invalid state) */ - auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); + auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); if (!heap_type) { pybind11_fail("make_default_metaclass(): error allocating metaclass!"); } @@ -255,7 +255,7 @@ inline PyTypeObject* make_default_metaclass() { heap_type->ht_qualname = name_obj.inc_ref().ptr(); #endif - auto type = &heap_type->ht_type; + auto *type = &heap_type->ht_type; type->tp_name = name; type->tp_base = type_incref(&PyType_Type); type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; @@ -285,7 +285,7 @@ inline PyTypeObject* make_default_metaclass() { inline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self, bool (*f)(void * /*parentptr*/, instance * /*self*/)) { for (handle h : reinterpret_borrow(tinfo->type->tp_bases)) { - if (auto parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) { + if (auto *parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) { for (auto &c : parent_tinfo->implicit_casts) { if (c.first == tinfo->cpptype) { auto *parentptr = c.second(valueptr); @@ -344,7 +344,7 @@ inline PyObject *make_new_instance(PyTypeObject *type) { } #endif PyObject *self = type->tp_alloc(type, 0); - auto inst = reinterpret_cast(self); + auto *inst = reinterpret_cast(self); // Allocate the value/holder internals: inst->allocate_layout(); @@ -369,14 +369,14 @@ extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject inline void add_patient(PyObject *nurse, PyObject *patient) { auto &internals = get_internals(); - auto instance = reinterpret_cast(nurse); + auto *instance = reinterpret_cast(nurse); instance->has_patients = true; Py_INCREF(patient); internals.patients[nurse].push_back(patient); } inline void clear_patients(PyObject *self) { - auto instance = reinterpret_cast(self); + auto *instance = reinterpret_cast(self); auto &internals = get_internals(); auto pos = internals.patients.find(self); assert(pos != internals.patients.end()); @@ -394,7 +394,7 @@ inline void clear_patients(PyObject *self) { /// Clears all internal data from the instance and removes it from registered instances in /// preparation for deallocation. inline void clear_instance(PyObject *self) { - auto instance = reinterpret_cast(self); + auto *instance = reinterpret_cast(self); // Deallocate any values/holders, if present: for (auto &v_h : values_and_holders(instance)) { @@ -435,7 +435,7 @@ inline void clear_instance(PyObject *self) { extern "C" inline void pybind11_object_dealloc(PyObject *self) { clear_instance(self); - auto type = Py_TYPE(self); + auto *type = Py_TYPE(self); type->tp_free(self); #if PY_VERSION_HEX < 0x03080000 @@ -464,7 +464,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) { issue no Python C API calls which could potentially invoke the garbage collector (the GC will call type_traverse(), which will in turn find the newly constructed type in an invalid state) */ - auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0); + auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0); if (!heap_type) { pybind11_fail("make_object_base_type(): error allocating type!"); } @@ -474,7 +474,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) { heap_type->ht_qualname = name_obj.inc_ref().ptr(); #endif - auto type = &heap_type->ht_type; + auto *type = &heap_type->ht_type; type->tp_name = name; type->tp_base = type_incref(&PyBaseObject_Type); type->tp_basicsize = static_cast(sizeof(instance)); @@ -538,7 +538,7 @@ extern "C" inline int pybind11_clear(PyObject *self) { /// Give instances of this type a `__dict__` and opt into garbage collection. inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) { - auto type = &heap_type->ht_type; + auto *type = &heap_type->ht_type; type->tp_flags |= Py_TPFLAGS_HAVE_GC; type->tp_dictoffset = type->tp_basicsize; // place dict at the end type->tp_basicsize += (ssize_t)sizeof(PyObject *); // and allocate enough space for it @@ -639,11 +639,11 @@ inline PyObject* make_new_python_type(const type_record &rec) { } } - auto full_name = c_str( + const auto *full_name = c_str( #if !defined(PYPY_VERSION) module_ ? str(module_).cast() + "." + rec.name : #endif - rec.name); + rec.name); char *tp_doc = nullptr; if (rec.doc && options::show_user_defined_docstrings()) { @@ -656,17 +656,16 @@ inline PyObject* make_new_python_type(const type_record &rec) { auto &internals = get_internals(); auto bases = tuple(rec.bases); - auto base = (bases.empty()) ? internals.instance_base - : bases[0].ptr(); + auto *base = (bases.empty()) ? internals.instance_base : bases[0].ptr(); /* Danger zone: from now (and until PyType_Ready), make sure to issue no Python C API calls which could potentially invoke the garbage collector (the GC will call type_traverse(), which will in turn find the newly constructed type in an invalid state) */ - auto metaclass = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() - : internals.default_metaclass; + auto *metaclass + = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() : internals.default_metaclass; - auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0); + auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0); if (!heap_type) { pybind11_fail(std::string(rec.name) + ": Unable to create type object!"); } @@ -676,7 +675,7 @@ inline PyObject* make_new_python_type(const type_record &rec) { heap_type->ht_qualname = qualname.inc_ref().ptr(); #endif - auto type = &heap_type->ht_type; + auto *type = &heap_type->ht_type; type->tp_name = full_name; type->tp_doc = tp_doc; type->tp_base = type_incref((PyTypeObject *)base); diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 661acf5dad..911ec3df41 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -301,7 +301,7 @@ bool handle_nested_exception(const T &exc, const std::exception_ptr &p) { template >::value, int> = 0> bool handle_nested_exception(const T &exc, const std::exception_ptr &p) { - if (auto *nep = dynamic_cast(std::addressof(exc))) { + if (const auto *nep = dynamic_cast(std::addressof(exc))) { return handle_nested_exception(*nep, p); } return false; @@ -338,7 +338,7 @@ inline void translate_exception(std::exception_ptr p) { return; } catch (const builtin_exception &e) { // Could not use template since it's an abstract class. - if (auto *nep = dynamic_cast(std::addressof(e))) { + if (const auto *nep = dynamic_cast(std::addressof(e))) { handle_nested_exception(*nep, p); } e.set_error(); diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 212a944afe..76758915bd 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -112,7 +112,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector &all_type_info(PyTypeObject *type) * `all_type_info` instead if you want to support multiple bases. */ PYBIND11_NOINLINE detail::type_info* get_type_info(PyTypeObject *type) { - auto &bases = all_type_info(type); + const auto &bases = all_type_info(type); if (bases.empty()) { return nullptr; } @@ -213,10 +213,10 @@ inline detail::type_info *get_global_type_info(const std::type_index &tp) { /// Return the type info for a given C++ type; on lookup failure can either throw or return nullptr. PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp, bool throw_if_missing = false) { - if (auto ltype = get_local_type_info(tp)) { + if (auto *ltype = get_local_type_info(tp)) { return ltype; } - if (auto gtype = get_global_type_info(tp)) { + if (auto *gtype = get_global_type_info(tp)) { return gtype; } @@ -238,7 +238,7 @@ PYBIND11_NOINLINE handle find_registered_python_instance(void *src, const detail::type_info *tinfo) { auto it_instances = get_internals().registered_instances.equal_range(src); for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) { - for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) { + for (auto *instance_type : detail::all_type_info(Py_TYPE(it_i->second))) { if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype)) { return handle((PyObject *) it_i->second).inc_ref(); } @@ -400,7 +400,7 @@ PYBIND11_NOINLINE value_and_holder instance::get_value_and_holder(const type_inf } PYBIND11_NOINLINE void instance::allocate_layout() { - auto &tinfo = all_type_info(Py_TYPE(this)); + const auto &tinfo = all_type_info(Py_TYPE(this)); const size_t n_types = tinfo.size(); @@ -424,7 +424,7 @@ PYBIND11_NOINLINE void instance::allocate_layout() { // values that tracks whether each associated holder has been initialized. Each [block] is // padded, if necessary, to an integer multiple of sizeof(void *). size_t space = 0; - for (auto t : tinfo) { + for (auto *t : tinfo) { space += 1; // value pointer space += t->holder_size_in_ptrs; // holder instance } @@ -585,7 +585,7 @@ class type_caster_generic { } auto inst = reinterpret_steal(make_new_instance(tinfo->type)); - auto wrapper = reinterpret_cast(inst.ptr()); + auto *wrapper = reinterpret_cast(inst.ptr()); wrapper->owned = false; void *&valueptr = values_and_holders(wrapper).begin()->value_ptr(); @@ -659,7 +659,7 @@ class type_caster_generic { auto *&vptr = v_h.value_ptr(); // Lazy allocation for unallocated values: if (vptr == nullptr) { - auto *type = v_h.type ? v_h.type : typeinfo; + const auto *type = v_h.type ? v_h.type : typeinfo; if (type->operator_new) { vptr = type->operator_new(type->type_size); } else { @@ -678,7 +678,7 @@ class type_caster_generic { value = vptr; } bool try_implicit_casts(handle src, bool convert) { - for (auto &cast : typeinfo->implicit_casts) { + for (const auto &cast : typeinfo->implicit_casts) { type_caster_generic sub_caster(*cast.first); if (sub_caster.load(src, convert)) { value = cast.second(sub_caster.value); @@ -721,7 +721,7 @@ class type_caster_generic { return false; } - if (auto result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) { + if (auto *result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) { value = result; return true; } @@ -753,7 +753,7 @@ class type_caster_generic { } // Case 2: We have a derived class if (PyType_IsSubtype(srctype, typeinfo->type)) { - auto &bases = all_type_info(srctype); + const auto &bases = all_type_info(srctype); bool no_cpp_mi = typeinfo->simple_type; // Case 2a: the python type is a Python-inherited derived class that inherits from just @@ -770,7 +770,7 @@ class type_caster_generic { // we can find an exact match (or, for a simple C++ type, an inherited match); if so, we // can safely reinterpret_cast to the relevant pointer. if (bases.size() > 1) { - for (auto base : bases) { + for (auto *base : bases) { if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type) : base->type == typeinfo->type) { this_.load_value(reinterpret_cast(src.ptr())->get_value_and_holder(base)); return true; @@ -788,7 +788,7 @@ class type_caster_generic { // Perform an implicit conversion if (convert) { - for (auto &converter : typeinfo->implicit_conversions) { + for (const auto &converter : typeinfo->implicit_conversions) { auto temp = reinterpret_steal(converter(src.ptr(), typeinfo->type)); if (load_impl(temp, false)) { loader_life_support::add_patient(temp); @@ -802,7 +802,7 @@ class type_caster_generic { // Failed to match local typeinfo. Try again with global. if (typeinfo->module_local) { - if (auto gtype = get_global_type_info(*typeinfo->cpptype)) { + if (auto *gtype = get_global_type_info(*typeinfo->cpptype)) { typeinfo = gtype; return load(src, false); } @@ -973,7 +973,7 @@ template class type_caster_base : public type_caster_generic { // polymorphic type (using RTTI by default, but can be overridden by specializing // polymorphic_type_hook). If the instance isn't derived, returns the base version. static std::pair src_and_type(const itype *src) { - auto &cast_type = typeid(itype); + const auto &cast_type = typeid(itype); const std::type_info *instance_type = nullptr; const void *vsrc = polymorphic_type_hook::get(src, instance_type); if (instance_type && !same_type(cast_type, *instance_type)) { diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 9da2862f38..0c3f778136 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -157,7 +157,7 @@ inline void set_interpreter_argv(int argc, const char *const *argv, bool add_pro widened_argv[ii] = widened_argv_entries.back().get(); } - auto pysys_argv = widened_argv.get(); + auto *pysys_argv = widened_argv.get(); #else // python 2.x std::vector strings{safe_argv, safe_argv + argv_size}; diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 9e2f0b5b6b..95a1a02af7 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -46,10 +46,10 @@ struct type_caster> { captured variables), in which case the roundtrip can be avoided. */ if (auto cfunc = func.cpp_function()) { - auto cfunc_self = PyCFunction_GET_SELF(cfunc.ptr()); + auto *cfunc_self = PyCFunction_GET_SELF(cfunc.ptr()); if (isinstance(cfunc_self)) { auto c = reinterpret_borrow(cfunc_self); - auto rec = (function_record *) c; + auto *rec = (function_record *) c; while (rec != nullptr) { if (rec->is_stateless diff --git a/include/pybind11/gil.h b/include/pybind11/gil.h index b493fd145b..18762832cb 100644 --- a/include/pybind11/gil.h +++ b/include/pybind11/gil.h @@ -137,7 +137,7 @@ class gil_scoped_release { auto &internals = detail::get_internals(); tstate = PyEval_SaveThread(); if (disassoc) { - auto key = internals.tstate; + auto *key = internals.tstate; PYBIND11_TLS_DELETE_VALUE(key); } } @@ -160,7 +160,7 @@ class gil_scoped_release { PyEval_RestoreThread(tstate); } if (disassoc) { - auto key = detail::get_internals().tstate; + auto *key = detail::get_internals().tstate; PYBIND11_TLS_REPLACE_VALUE(key, tstate); } } diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 40bbff8ea0..57f0c90948 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -1130,7 +1130,7 @@ struct npy_format_descriptor::index]; static pybind11::dtype dtype() { - if (auto ptr = npy_api::get().PyArray_DescrFromType_(value)) { + if (auto *ptr = npy_api::get().PyArray_DescrFromType_(value)) { return reinterpret_steal(ptr); } pybind11_fail("Unsupported buffer format!"); @@ -1200,7 +1200,7 @@ PYBIND11_NOINLINE void register_structured_dtype( formats.append(field.descr); offsets.append(pybind11::int_(field.offset)); } - auto dtype_ptr + auto *dtype_ptr = pybind11::dtype(std::move(names), std::move(formats), std::move(offsets), itemsize) .release() .ptr(); @@ -1699,7 +1699,7 @@ struct vectorize_helper { } /* Call the function */ - auto mutable_data = returned_array::mutable_data(result); + auto *mutable_data = returned_array::mutable_data(result); if (trivial == broadcast_trivial::non_trivial) { apply_broadcast(buffers, params, mutable_data, size, shape, i_seq, vi_seq, bi_seq); } else { diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 14e84513fd..560c3e0c1b 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -162,7 +162,7 @@ class cpp_function : public function { /* Store the function including any extra state it might have (e.g. a lambda capture object) */ // The unique_ptr makes sure nothing is leaked in case of an exception. auto unique_rec = make_function_record(); - auto rec = unique_rec.get(); + auto *rec = unique_rec.get(); /* Store the capture object directly in the function record if there is enough space */ if (PYBIND11_SILENCE_MSVC_C4127(sizeof(capture) <= sizeof(rec->data))) { @@ -220,8 +220,8 @@ class cpp_function : public function { process_attributes::precall(call); /* Get a pointer to the capture object */ - auto data = (sizeof(capture) <= sizeof(call.func.data) - ? &call.func.data : call.func.data[0]); + const auto *data = (sizeof(capture) <= sizeof(call.func.data) ? &call.func.data + : call.func.data[0]); auto *cap = const_cast(reinterpret_cast(data)); /* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */ @@ -287,12 +287,12 @@ class cpp_function : public function { class strdup_guard { public: ~strdup_guard() { - for (auto s : strings) { + for (auto *s : strings) { std::free(s); } } char *operator()(const char *s) { - auto t = PYBIND11_COMPAT_STRDUP(s); + auto *t = PYBIND11_COMPAT_STRDUP(s); strings.push_back(t); return t; } @@ -309,7 +309,7 @@ class cpp_function : public function { // Do NOT receive `unique_rec` by value. If this function fails to move out the unique_ptr, // we do not want this to destuct the pointer. `initialize` (the caller) still relies on the // pointee being alive after this call. Only move out if a `capsule` is going to keep it alive. - auto rec = unique_rec.get(); + auto *rec = unique_rec.get(); // Keep track of strdup'ed strings, and clean them up as long as the function's capsule // has not taken ownership yet (when `unique_rec.release()` is called). @@ -355,7 +355,7 @@ class cpp_function : public function { std::string signature; size_t type_index = 0, arg_index = 0; bool is_starred = false; - for (auto *pc = text; *pc != '\0'; ++pc) { + for (const auto *pc = text; *pc != '\0'; ++pc) { const auto c = *pc; if (c == '{') { @@ -396,7 +396,7 @@ class cpp_function : public function { if (!t) { pybind11_fail("Internal error while parsing type signature (1)"); } - if (auto tinfo = detail::get_type_info(*t)) { + if (auto *tinfo = detail::get_type_info(*t)) { handle th((PyObject *) tinfo->type); signature += th.attr("__module__").cast() + "." + @@ -535,7 +535,7 @@ class cpp_function : public function { } // Then specific overload signatures bool first_user_def = true; - for (auto it = chain_start; it != nullptr; it = it->next) { + for (auto *it = chain_start; it != nullptr; it = it->next) { if (options::show_function_signatures()) { if (index > 0) { signatures += "\n"; @@ -652,8 +652,8 @@ class cpp_function : public function { return nullptr; } - const auto tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr()); - const auto pi = reinterpret_cast(parent.ptr()); + auto *const tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr()); + auto *const pi = reinterpret_cast(parent.ptr()); self_value_and_holder = pi->get_value_and_holder(tinfo, true); // If this value is already registered it must mean __init__ is invoked multiple times; @@ -1207,7 +1207,7 @@ class module_ : public object { /* m_clear */ nullptr, /* m_free */ nullptr }; - auto m = PyModule_Create(def); + auto *m = PyModule_Create(def); #else // Ignore module_def *def; only necessary for Python 3 (void) def; @@ -1317,7 +1317,7 @@ class generic_type : public object { void mark_parents_nonsimple(PyTypeObject *value) { auto t = reinterpret_borrow(value->tp_bases); for (handle h : t) { - auto tinfo2 = get_type_info((PyTypeObject *) h.ptr()); + auto *tinfo2 = get_type_info((PyTypeObject *) h.ptr()); if (tinfo2) { tinfo2->simple_type = false; } @@ -1329,7 +1329,7 @@ class generic_type : public object { buffer_info *(*get_buffer)(PyObject *, void *), void *get_buffer_data) { auto *type = (PyHeapTypeObject*) m_ptr; - auto tinfo = detail::get_type_info(&type->ht_type); + auto *tinfo = detail::get_type_info(&type->ht_type); if (!type->ht_type.tp_as_buffer) { pybind11_fail("To be able to register buffer protocol support for the type '" @@ -2304,7 +2304,7 @@ template void implicitly_convertible() return result; }; - if (auto tinfo = detail::get_type_info(typeid(OutputType))) { + if (auto *tinfo = detail::get_type_info(typeid(OutputType))) { tinfo->implicit_conversions.push_back(implicit_caster); } else { pybind11_fail("implicitly_convertible: Unable to find type " + type_id()); @@ -2568,7 +2568,7 @@ PYBIND11_NAMESPACE_END(detail) :return: The Python method by this name from the object or an empty function wrapper. \endrst */ template function get_override(const T *this_ptr, const char *name) { - auto tinfo = detail::get_type_info(typeid(T)); + auto *tinfo = detail::get_type_info(typeid(T)); return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function(); } diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 74388349b0..339f0fff5f 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1524,7 +1524,7 @@ class capsule : public object { /// Get the pointer the capsule holds. template T* get_pointer() const { - auto name = this->name(); + const auto *name = this->name(); T *result = static_cast(PyCapsule_GetPointer(m_ptr, name)); if (!result) { PyErr_Clear(); diff --git a/include/pybind11/stl/filesystem.h b/include/pybind11/stl/filesystem.h index a9a6c8512c..59ac38b9ba 100644 --- a/include/pybind11/stl/filesystem.h +++ b/include/pybind11/stl/filesystem.h @@ -68,7 +68,7 @@ template struct path_caster { PyObject* native = nullptr; if constexpr (std::is_same_v) { if (PyUnicode_FSConverter(buf, &native) != 0) { - if (auto c_str = PyBytes_AsString(native)) { + if (auto *c_str = PyBytes_AsString(native)) { // AsString returns a pointer to the internal buffer, which // must not be free'd. value = c_str; @@ -76,7 +76,7 @@ template struct path_caster { } } else if constexpr (std::is_same_v) { if (PyUnicode_FSDecoder(buf, &native) != 0) { - if (auto c_str = PyUnicode_AsWideCharString(native, nullptr)) { + if (auto *c_str = PyUnicode_AsWideCharString(native, nullptr)) { // AsWideCharString returns a new string that must be free'd. value = c_str; // Copies the string. PyMem_Free(c_str); diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 8ae6829653..6db1faf404 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -464,7 +464,7 @@ class_ bind_vector(handle scope, std::string const &name, A // If the value_type is unregistered (e.g. a converting type) or is itself registered // module-local then make the vector binding module-local as well: using vtype = typename Vector::value_type; - auto vtype_info = detail::get_type_info(typeid(vtype)); + auto *vtype_info = detail::get_type_info(typeid(vtype)); bool local = !vtype_info || vtype_info->module_local; Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward(args)...); @@ -651,7 +651,7 @@ class_ bind_map(handle scope, const std::string &name, Args&&. // If either type is a non-module-local bound type then make the map binding non-local as well; // otherwise (e.g. both types are either module-local or converting) the map will be // module-local. - auto tinfo = detail::get_type_info(typeid(MappedType)); + auto *tinfo = detail::get_type_info(typeid(MappedType)); bool local = !tinfo || tinfo->module_local; if (local) { tinfo = detail::get_type_info(typeid(KeyType)); diff --git a/tests/test_buffers.cpp b/tests/test_buffers.cpp index 0920363966..cedbb73eac 100644 --- a/tests/test_buffers.cpp +++ b/tests/test_buffers.cpp @@ -89,7 +89,7 @@ TEST_SUBMODULE(buffers, m) { throw std::runtime_error("Incompatible buffer format!"); } - auto v = new Matrix(info.shape[0], info.shape[1]); + auto *v = new Matrix(info.shape[0], info.shape[1]); memcpy(v->data(), info.ptr, sizeof(float) * (size_t) (v->rows() * v->cols())); return v; })) diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index 5ff5a8fc4f..96519d44e9 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -277,7 +277,7 @@ TEST_SUBMODULE(builtin_casters, m) { m.def("refwrap_list", [](bool copy) { static IncType x1(1), x2(2); py::list l; - for (auto &f : {std::ref(x1), std::ref(x2)}) { + for (const auto &f : {std::ref(x1), std::ref(x2)}) { l.append(py::cast(f, copy ? py::return_value_policy::copy : py::return_value_policy::reference)); } diff --git a/tests/test_call_policies.cpp b/tests/test_call_policies.cpp index cdbd9d42fe..8136bf4f8e 100644 --- a/tests/test_call_policies.cpp +++ b/tests/test_call_policies.cpp @@ -95,7 +95,7 @@ TEST_SUBMODULE(call_policies, m) { // but it's unclear how to test it without `PyGILState_GetThisThreadState`. auto report_gil_status = []() { auto is_gil_held = false; - if (auto tstate = py::detail::get_thread_state_unchecked()) { + if (auto *tstate = py::detail::get_thread_state_unchecked()) { is_gil_held = (tstate == PyGILState_GetThisThreadState()); } diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index b73a623709..55f9c86148 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -148,7 +148,7 @@ TEST_SUBMODULE(callbacks, m) { py::arg("expect_none") = false); m.def("test_dummy_function", [](const std::function &f) -> std::string { using fn_type = int (*)(int); - auto result = f.target(); + const auto *result = f.target(); if (!result) { auto r = f(1); return "can't convert to function pointer: eval(1) = " + std::to_string(r); diff --git a/tests/test_class.cpp b/tests/test_class.cpp index 977297e572..7445b3c026 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -255,7 +255,7 @@ TEST_SUBMODULE(class_, m) { return py::str().release().ptr(); }; - auto def = new PyMethodDef{"f", f, METH_VARARGS, nullptr}; + auto *def = new PyMethodDef{"f", f, METH_VARARGS, nullptr}; py::capsule def_capsule(def, [](void *ptr) { delete reinterpret_cast(ptr); }); return py::reinterpret_steal(PyCFunction_NewEx(def, def_capsule.ptr(), m.ptr())); }()); diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp index 9ffc257ba0..601a19092f 100644 --- a/tests/test_embed/test_interpreter.cpp +++ b/tests/test_embed/test_interpreter.cpp @@ -240,8 +240,8 @@ TEST_CASE("Subinterpreter") { REQUIRE(has_pybind11_internals_static()); /// Create and switch to a subinterpreter. - auto main_tstate = PyThreadState_Get(); - auto sub_tstate = Py_NewInterpreter(); + auto *main_tstate = PyThreadState_Get(); + auto *sub_tstate = Py_NewInterpreter(); // Subinterpreters get their own copy of builtins. detail::get_internals() still // works by returning from the static variable, i.e. all interpreters share a single diff --git a/tests/test_numpy_array.cpp b/tests/test_numpy_array.cpp index 4712c80925..bc1797845b 100644 --- a/tests/test_numpy_array.cpp +++ b/tests/test_numpy_array.cpp @@ -89,7 +89,7 @@ template arr data_t(const arr_t& a, Ix... index) { } template arr& mutate_data(arr& a, Ix... index) { - auto ptr = (uint8_t *) a.mutable_data(index...); + auto *ptr = (uint8_t *) a.mutable_data(index...); for (py::ssize_t i = 0; i < a.nbytes() - a.offset_at(index...); i++) { ptr[i] = (uint8_t) (ptr[i] * 2); } diff --git a/tests/test_numpy_dtypes.cpp b/tests/test_numpy_dtypes.cpp index bd702e403c..468a89a3c9 100644 --- a/tests/test_numpy_dtypes.cpp +++ b/tests/test_numpy_dtypes.cpp @@ -165,7 +165,7 @@ template py::array_t create_recarray(size_t n) { auto arr = mkarray_via_buffer(n); auto req = arr.request(); - auto ptr = static_cast(req.ptr); + auto *ptr = static_cast(req.ptr); for (size_t i = 0; i < n; i++) { SET_TEST_VALS(ptr[i], i); } @@ -175,7 +175,7 @@ py::array_t create_recarray(size_t n) { template py::list print_recarray(py::array_t arr) { const auto req = arr.request(); - const auto ptr = static_cast(req.ptr); + auto *const ptr = static_cast(req.ptr); auto l = py::list(); for (py::ssize_t i = 0; i < req.size; i++) { std::stringstream ss; @@ -192,8 +192,8 @@ py::array_t test_array_ctors(int i) { std::vector shape { 3, 2 }; std::vector strides { 8, 4 }; - auto ptr = data.data(); - auto vptr = (void *) ptr; + auto *ptr = data.data(); + auto *vptr = (void *) ptr; auto dtype = py::dtype("int32"); py::buffer_info buf_ndim1(vptr, 4, "i", 6); @@ -328,7 +328,7 @@ TEST_SUBMODULE(numpy_dtypes, m) { m.def("create_rec_nested", [](size_t n) { // test_signature py::array_t arr = mkarray_via_buffer(n); auto req = arr.request(); - auto ptr = static_cast(req.ptr); + auto *ptr = static_cast(req.ptr); for (size_t i = 0; i < n; i++) { SET_TEST_VALS(ptr[i].a, i); SET_TEST_VALS(ptr[i].b, i + 1); @@ -339,7 +339,7 @@ TEST_SUBMODULE(numpy_dtypes, m) { m.def("create_rec_partial_nested", [](size_t n) { py::array_t arr = mkarray_via_buffer(n); auto req = arr.request(); - auto ptr = static_cast(req.ptr); + auto *ptr = static_cast(req.ptr); for (size_t i = 0; i < n; i++) { SET_TEST_VALS(ptr[i].a, i); } @@ -397,14 +397,14 @@ TEST_SUBMODULE(numpy_dtypes, m) { m.def("test_dtype_ctors", &test_dtype_ctors); m.def("test_dtype_kind", [dtype_names]() { py::list list; - for (auto &dt_name : dtype_names) { + for (const auto &dt_name : dtype_names) { list.append(py::dtype(dt_name).kind()); } return list; }); m.def("test_dtype_char_", [dtype_names]() { py::list list; - for (auto &dt_name : dtype_names) { + for (const auto &dt_name : dtype_names) { list.append(py::dtype(dt_name).char_()); } return list; @@ -430,7 +430,7 @@ TEST_SUBMODULE(numpy_dtypes, m) { py::array_t arr = mkarray_via_buffer(non_empty ? 4 : 0); if (non_empty) { auto req = arr.request(); - auto ptr = static_cast(req.ptr); + auto *ptr = static_cast(req.ptr); for (py::ssize_t i = 0; i < req.size * req.itemsize; i++) { static_cast(req.ptr)[i] = 0; } @@ -450,7 +450,7 @@ TEST_SUBMODULE(numpy_dtypes, m) { // test_array_array m.def("create_array_array", [](size_t n) { py::array_t arr = mkarray_via_buffer(n); - auto ptr = (ArrayStruct *) arr.mutable_data(); + auto *ptr = (ArrayStruct *) arr.mutable_data(); for (size_t i = 0; i < n; i++) { for (size_t j = 0; j < 3; j++) { for (size_t k = 0; k < 4; k++) { @@ -476,7 +476,7 @@ TEST_SUBMODULE(numpy_dtypes, m) { // test_enum_array m.def("create_enum_array", [](size_t n) { py::array_t arr = mkarray_via_buffer(n); - auto ptr = (EnumStruct *) arr.mutable_data(); + auto *ptr = (EnumStruct *) arr.mutable_data(); for (size_t i = 0; i < n; i++) { ptr[i].e1 = static_cast(-1 + ((int) i % 2) * 2); ptr[i].e2 = static_cast(1 + (i % 2)); @@ -488,7 +488,7 @@ TEST_SUBMODULE(numpy_dtypes, m) { // test_complex_array m.def("create_complex_array", [](size_t n) { py::array_t arr = mkarray_via_buffer(n); - auto ptr = (ComplexStruct *) arr.mutable_data(); + auto *ptr = (ComplexStruct *) arr.mutable_data(); for (size_t i = 0; i < n; i++) { ptr[i].cflt.real(float(i)); ptr[i].cflt.imag(float(i) + 0.25f); diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 3605ad8eac..196a54c41f 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -146,7 +146,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("return_capsule_with_name_and_destructor", []() { auto capsule = py::capsule((void *) 12345, "pointer type description", [](PyObject *ptr) { if (ptr) { - auto name = PyCapsule_GetName(ptr); + const auto *name = PyCapsule_GetName(ptr); py::print("destructing capsule ({}, '{}')"_s.format( (size_t) PyCapsule_GetPointer(ptr, name), name )); diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index 5bd77eae33..c86bed1ce0 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -113,7 +113,7 @@ class MyObject4 { static void cleanupAllInstances() { auto tmp = std::move(myobject4_instances); myobject4_instances.clear(); - for (auto o : tmp) { + for (auto *o : tmp) { delete o; } } @@ -141,7 +141,7 @@ class MyObject4a { static void cleanupAllInstances() { auto tmp = std::move(myobject4a_instances); myobject4a_instances.clear(); - for (auto o : tmp) { + for (auto *o : tmp) { delete o; } } diff --git a/tests/test_stl_binders.cpp b/tests/test_stl_binders.cpp index 7fb973ca43..bf534aec8d 100644 --- a/tests/test_stl_binders.cpp +++ b/tests/test_stl_binders.cpp @@ -41,7 +41,7 @@ class E_nc { }; template Container *one_to_n(int n) { - auto v = new Container(); + auto *v = new Container(); for (int i = 1; i <= n; i++) { v->emplace_back(i); } @@ -49,7 +49,7 @@ template Container *one_to_n(int n) { } template Map *times_ten(int n) { - auto m = new Map(); + auto *m = new Map(); for (int i = 1; i <= n; i++) { m->emplace(int(i), E_nc(10 * i)); } @@ -57,7 +57,7 @@ template Map *times_ten(int n) { } template NestMap *times_hundred(int n) { - auto m = new NestMap(); + auto *m = new NestMap(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { (*m)[i].emplace(int(j * 10), E_nc(100 * j)); @@ -99,16 +99,15 @@ TEST_SUBMODULE(stl_binders, m) { m.def("get_umnc", ×_ten>); // Issue #1885: binding nested std::map> with E non-copyable py::bind_map>>(m, "MapVecENC"); - m.def("get_nvnc", [](int n) - { - auto m = new std::map>(); - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - (*m)[i].emplace_back(j); - } + m.def("get_nvnc", [](int n) { + auto *m = new std::map>(); + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + (*m)[i].emplace_back(j); } - return m; - }); + } + return m; + }); py::bind_map>>(m, "MapMapENC"); m.def("get_nmnc", ×_hundred>>); py::bind_map>>(m, "UmapUmapENC"); From 6828dc3f6d7146596d25137c24693c6ce4687ef9 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 3 Feb 2022 16:19:39 -0500 Subject: [PATCH 2/2] fix: support Python < 3.6 --- include/pybind11/gil.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/pybind11/gil.h b/include/pybind11/gil.h index 18762832cb..22864adb20 100644 --- a/include/pybind11/gil.h +++ b/include/pybind11/gil.h @@ -137,7 +137,9 @@ class gil_scoped_release { auto &internals = detail::get_internals(); tstate = PyEval_SaveThread(); if (disassoc) { - auto *key = internals.tstate; + // Python >= 3.7 can remove this, it's an int before 3.7 + // NOLINTNEXTLINE(readability-qualified-auto) + auto key = internals.tstate; PYBIND11_TLS_DELETE_VALUE(key); } } @@ -160,7 +162,9 @@ class gil_scoped_release { PyEval_RestoreThread(tstate); } if (disassoc) { - auto *key = detail::get_internals().tstate; + // Python >= 3.7 can remove this, it's an int before 3.7 + // NOLINTNEXTLINE(readability-qualified-auto) + auto key = detail::get_internals().tstate; PYBIND11_TLS_REPLACE_VALUE(key, tstate); } }