Skip to content

Commit

Permalink
Add readability-qualified-auto to clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
Skylion007 committed Feb 3, 2022
1 parent 53056b1 commit 287527f
Show file tree
Hide file tree
Showing 26 changed files with 94 additions and 93 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Expand Up @@ -39,6 +39,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,
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/attr.h
Expand Up @@ -326,7 +326,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);
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/cast.h
Expand Up @@ -268,7 +268,7 @@ class type_caster<void> : public type_caster<void_type> {
}

/* Check if this is a C++ type */
auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
const 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<instance *>(h.ptr())).begin()->value_ptr();
return true;
Expand Down Expand Up @@ -325,7 +325,7 @@ class type_caster<bool> {
#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());
}
Expand Down
40 changes: 20 additions & 20 deletions include/pybind11/detail/class.h
Expand Up @@ -66,7 +66,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!");

Expand All @@ -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;
Expand Down Expand Up @@ -131,7 +131,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);
Expand Down Expand Up @@ -180,7 +180,7 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P
}

// This must be a pybind11 instance
auto instance = reinterpret_cast<detail::instance *>(self);
auto *instance = reinterpret_cast<detail::instance *>(self);

// Ensure that the base __init__ function(s) were called
for (const auto &vh : values_and_holders(instance)) {
Expand Down Expand Up @@ -244,7 +244,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!");

Expand All @@ -253,7 +253,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;
Expand Down Expand Up @@ -285,7 +285,7 @@ inline void traverse_offset_bases(void *valueptr,
instance *self,
bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
for (handle h : reinterpret_borrow<tuple>(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);
Expand Down Expand Up @@ -341,7 +341,7 @@ inline PyObject *make_new_instance(PyTypeObject *type) {
}
#endif
PyObject *self = type->tp_alloc(type, 0);
auto inst = reinterpret_cast<instance *>(self);
auto *inst = reinterpret_cast<instance *>(self);
// Allocate the value/holder internals:
inst->allocate_layout();

Expand All @@ -366,14 +366,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<detail::instance *>(nurse);
auto *instance = reinterpret_cast<detail::instance *>(nurse);
instance->has_patients = true;
Py_INCREF(patient);
internals.patients[nurse].push_back(patient);
}

inline void clear_patients(PyObject *self) {
auto instance = reinterpret_cast<detail::instance *>(self);
auto *instance = reinterpret_cast<detail::instance *>(self);
auto &internals = get_internals();
auto pos = internals.patients.find(self);
assert(pos != internals.patients.end());
Expand All @@ -390,7 +390,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<detail::instance *>(self);
auto *instance = reinterpret_cast<detail::instance *>(self);

// Deallocate any values/holders, if present:
for (auto &v_h : values_and_holders(instance)) {
Expand Down Expand Up @@ -426,7 +426,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
Expand Down Expand Up @@ -455,7 +455,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!");

Expand All @@ -464,7 +464,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<ssize_t>(sizeof(instance));
Expand Down Expand Up @@ -527,7 +527,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
Expand Down Expand Up @@ -622,7 +622,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
module_ = rec.scope.attr("__name__");
}

auto full_name = c_str(
const auto *full_name = c_str(
#if !defined(PYPY_VERSION)
module_ ? str(module_).cast<std::string>() + "." + rec.name :
#endif
Expand All @@ -639,16 +639,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
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!");

Expand All @@ -657,7 +657,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);
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/detail/internals.h
Expand Up @@ -309,7 +309,7 @@ bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
template <class T,
enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
return handle_nested_exception(*nep, p);
}
return false;
Expand Down Expand Up @@ -346,7 +346,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<const std::nested_exception *>(std::addressof(e))) {
if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
handle_nested_exception(*nep, p);
}
e.set_error();
Expand Down
32 changes: 16 additions & 16 deletions include/pybind11/detail/type_caster_base.h
Expand Up @@ -109,7 +109,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_

auto const &type_dict = get_internals().registered_types_py;
for (size_t i = 0; i < check.size(); i++) {
auto type = check[i];
auto *type = check[i];
// Ignore Python2 old-style class super type:
if (!PyType_Check((PyObject *) type))
continue;
Expand Down Expand Up @@ -176,7 +176,7 @@ inline const std::vector<detail::type_info *> &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;
if (bases.size() > 1)
Expand Down Expand Up @@ -205,9 +205,9 @@ inline detail::type_info *get_global_type_info(const std::type_index &tp) {
/// 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;

if (throw_if_missing) {
Expand All @@ -229,7 +229,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();
}
Expand Down Expand Up @@ -387,7 +387,7 @@ instance::get_value_and_holder(const type_info *find_type /*= nullptr default in
}

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();

Expand All @@ -409,7 +409,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
}
Expand Down Expand Up @@ -562,7 +562,7 @@ class type_caster_generic {
return registered_inst;

auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
auto wrapper = reinterpret_cast<instance *>(inst.ptr());
auto *wrapper = reinterpret_cast<instance *>(inst.ptr());
wrapper->owned = false;
void *&valueptr = values_and_holders(wrapper).begin()->value_ptr();

Expand Down Expand Up @@ -636,7 +636,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 {
Expand All @@ -651,7 +651,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);
Expand Down Expand Up @@ -691,7 +691,7 @@ class type_caster_generic {
|| (cpptype && !same_type(*cpptype, *foreign_typeinfo->cpptype)))
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;
}
Expand Down Expand Up @@ -721,7 +721,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
Expand All @@ -738,7 +738,7 @@ class type_caster_generic {
// if 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(
Expand All @@ -757,7 +757,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<object>(converter(src.ptr(), typeinfo->type));
if (load_impl<ThisT>(temp, false)) {
loader_life_support::add_patient(temp);
Expand All @@ -770,7 +770,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);
}
Expand Down Expand Up @@ -947,7 +947,7 @@ 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<const void *, const type_info *> 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<itype>::get(src, instance_type);
if (instance_type && !same_type(cast_type, *instance_type)) {
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/embed.h
Expand Up @@ -150,7 +150,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<std::string> strings{safe_argv, safe_argv + argv_size};
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/functional.h
Expand Up @@ -44,10 +44,10 @@ struct type_caster<std::function<Return(Args...)>> {
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<capsule>(cfunc_self)) {
auto c = reinterpret_borrow<capsule>(cfunc_self);
auto rec = (function_record *) c;
auto *rec = (function_record *) c;

while (rec != nullptr) {
if (rec->is_stateless
Expand Down

0 comments on commit 287527f

Please sign in to comment.