diff options
author | Tom Tromey <tom@tromey.com> | 2020-12-11 09:33:36 -0700 |
---|---|---|
committer | Tom Tromey <tom@tromey.com> | 2020-12-11 09:33:38 -0700 |
commit | 54746ce37a483e235ac128b6f545be9888bf3521 (patch) | |
tree | 00098f97c2514f9895f1e7bab52678ed4dba9dee | |
parent | d8f168ddd08f1d5fde1a193724cdc40d524039a7 (diff) | |
download | fsf-binutils-gdb-54746ce37a483e235ac128b6f545be9888bf3521.zip fsf-binutils-gdb-54746ce37a483e235ac128b6f545be9888bf3521.tar.gz fsf-binutils-gdb-54746ce37a483e235ac128b6f545be9888bf3521.tar.bz2 |
C++-ify varobj iteration
This changes the varobj iteration code to use a C++ class rather than
a C struct with a separate "ops" structure. The only implementation
is updated to use inheritance. This simplifies the code quite nicely.
gdb/ChangeLog
2020-12-11 Tom Tromey <tom@tromey.com>
* varobj.c (update_dynamic_varobj_children, install_visualizer)
(varobj::~varobj): Update.
* varobj-iter.h (struct varobj_iter): Change to interface class.
(struct varobj_iter_ops): Remove.
(varobj_iter_next, varobj_iter_delete): Remove.
* python/py-varobj.c (struct py_varobj_iter): Derive from
varobj_iter. Add constructor, destructor. Rename members.
(py_varobj_iter::~py_varobj_iter): Rename from
py_varobj_iter_dtor.
(py_varobj_iter::next): Rename from py_varobj_iter_next.
(py_varobj_iter_ops): Remove.
(py_varobj_iter): Rename from py_varobj_iter_ctor.
(py_varobj_iter_new): Remove.
(py_varobj_get_iterator): Update.
-rw-r--r-- | gdb/ChangeLog | 17 | ||||
-rw-r--r-- | gdb/python/py-varobj.c | 80 | ||||
-rw-r--r-- | gdb/varobj-iter.h | 41 | ||||
-rw-r--r-- | gdb/varobj.c | 10 |
4 files changed, 54 insertions, 94 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog index d724e74..53f87d9 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,22 @@ 2020-12-11 Tom Tromey <tom@tromey.com> + * varobj.c (update_dynamic_varobj_children, install_visualizer) + (varobj::~varobj): Update. + * varobj-iter.h (struct varobj_iter): Change to interface class. + (struct varobj_iter_ops): Remove. + (varobj_iter_next, varobj_iter_delete): Remove. + * python/py-varobj.c (struct py_varobj_iter): Derive from + varobj_iter. Add constructor, destructor. Rename members. + (py_varobj_iter::~py_varobj_iter): Rename from + py_varobj_iter_dtor. + (py_varobj_iter::next): Rename from py_varobj_iter_next. + (py_varobj_iter_ops): Remove. + (py_varobj_iter): Rename from py_varobj_iter_ctor. + (py_varobj_iter_new): Remove. + (py_varobj_get_iterator): Update. + +2020-12-11 Tom Tromey <tom@tromey.com> + * varobj.h (all_root_varobjs): Take a function_view. * varobj.c (all_root_varobjs): Take a function_view. (varobj_invalidate_iter): Remove unused parameter. diff --git a/gdb/python/py-varobj.c b/gdb/python/py-varobj.c index 9c791f64..df6f9c5 100644 --- a/gdb/python/py-varobj.c +++ b/gdb/python/py-varobj.c @@ -21,34 +21,43 @@ /* A dynamic varobj iterator "class" for python pretty-printed varobjs. This inherits struct varobj_iter. */ -struct py_varobj_iter +struct py_varobj_iter : public varobj_iter { - /* The 'base class'. */ - struct varobj_iter base; + py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter); + ~py_varobj_iter () override; + + varobj_item *next () override; + +private: + + /* The varobj this iterator is listing children for. */ + struct varobj *m_var; + + /* The next raw index we will try to check is available. If it is + equal to number_of_children, then we've already iterated the + whole set. */ + int m_next_raw_index = 0; /* The python iterator returned by the printer's 'children' method, or NULL if not available. */ - PyObject *iter; + PyObject *m_iter; }; /* Implementation of the 'dtor' method of pretty-printed varobj iterators. */ -static void -py_varobj_iter_dtor (struct varobj_iter *self) +py_varobj_iter::~py_varobj_iter () { - struct py_varobj_iter *dis = (struct py_varobj_iter *) self; - gdbpy_enter_varobj enter_py (self->var); - Py_XDECREF (dis->iter); + gdbpy_enter_varobj enter_py (m_var); + Py_XDECREF (m_iter); } /* Implementation of the 'next' method of pretty-printed varobj iterators. */ -static varobj_item * -py_varobj_iter_next (struct varobj_iter *self) +varobj_item * +py_varobj_iter::next () { - struct py_varobj_iter *t = (struct py_varobj_iter *) self; PyObject *py_v; varobj_item *vitem; const char *name = NULL; @@ -56,9 +65,9 @@ py_varobj_iter_next (struct varobj_iter *self) if (!gdb_python_initialized) return NULL; - gdbpy_enter_varobj enter_py (self->var); + gdbpy_enter_varobj enter_py (m_var); - gdbpy_ref<> item (PyIter_Next (t->iter)); + gdbpy_ref<> item (PyIter_Next (m_iter)); if (item == NULL) { @@ -78,7 +87,7 @@ py_varobj_iter_next (struct varobj_iter *self) } std::string name_str = string_printf ("<error at %d>", - self->next_raw_index++); + m_next_raw_index++); item.reset (Py_BuildValue ("(ss)", name_str.c_str (), value_str.get ())); if (item == NULL) @@ -107,45 +116,18 @@ py_varobj_iter_next (struct varobj_iter *self) gdbpy_print_stack (); vitem->name = name; - self->next_raw_index++; + m_next_raw_index++; return vitem; } -/* The 'vtable' of pretty-printed python varobj iterators. */ - -static const struct varobj_iter_ops py_varobj_iter_ops = -{ - py_varobj_iter_dtor, - py_varobj_iter_next -}; - /* Constructor of pretty-printed varobj iterators. VAR is the varobj whose children the iterator will be iterating over. PYITER is the python iterator actually responsible for the iteration. */ -static void -py_varobj_iter_ctor (struct py_varobj_iter *self, - struct varobj *var, gdbpy_ref<> &&pyiter) +py_varobj_iter::py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter) + : m_var (var), + m_iter (pyiter.release ()) { - self->base.var = var; - self->base.ops = &py_varobj_iter_ops; - self->base.next_raw_index = 0; - self->iter = pyiter.release (); -} - -/* Allocate and construct a pretty-printed varobj iterator. VAR is - the varobj whose children the iterator will be iterating over. - PYITER is the python iterator actually responsible for the - iteration. */ - -static struct py_varobj_iter * -py_varobj_iter_new (struct varobj *var, gdbpy_ref<> &&pyiter) -{ - struct py_varobj_iter *self; - - self = XNEW (struct py_varobj_iter); - py_varobj_iter_ctor (self, var, std::move (pyiter)); - return self; } /* Return a new pretty-printed varobj iterator suitable to iterate @@ -154,8 +136,6 @@ py_varobj_iter_new (struct varobj *var, gdbpy_ref<> &&pyiter) struct varobj_iter * py_varobj_get_iterator (struct varobj *var, PyObject *printer) { - struct py_varobj_iter *py_iter; - gdbpy_enter_varobj enter_py (var); if (!PyObject_HasAttr (printer, gdbpy_children_cst)) @@ -176,7 +156,5 @@ py_varobj_get_iterator (struct varobj *var, PyObject *printer) error (_("Could not get children iterator")); } - py_iter = py_varobj_iter_new (var, std::move (iter)); - - return &py_iter->base; + return new py_varobj_iter (var, std::move (iter)); } diff --git a/gdb/varobj-iter.h b/gdb/varobj-iter.h index 240b686..ed65419 100644 --- a/gdb/varobj-iter.h +++ b/gdb/varobj-iter.h @@ -28,50 +28,15 @@ struct varobj_item struct value *value; }; -struct varobj_iter_ops; - /* A dynamic varobj iterator "class". */ struct varobj_iter { - /* The 'vtable'. */ - const struct varobj_iter_ops *ops; - - /* The varobj this iterator is listing children for. */ - struct varobj *var; - - /* The next raw index we will try to check is available. If it is - equal to number_of_children, then we've already iterated the - whole set. */ - int next_raw_index; -}; - -/* The vtable of the varobj iterator class. */ +public: -struct varobj_iter_ops -{ - /* Destructor. Releases everything from SELF (but not SELF - itself). */ - void (*dtor) (struct varobj_iter *self); + virtual ~varobj_iter () = default; - /* Returns the next object or NULL if it has reached the end. */ - varobj_item *(*next) (struct varobj_iter *self); + virtual varobj_item *next () = 0; }; -/* Returns the next varobj or NULL if it has reached the end. */ - -#define varobj_iter_next(ITER) (ITER)->ops->next (ITER) - -/* Delete a varobj_iter object. */ - -#define varobj_iter_delete(ITER) \ - do \ - { \ - if ((ITER) != NULL) \ - { \ - (ITER)->ops->dtor (ITER); \ - xfree (ITER); \ - } \ - } while (0) - #endif /* VAROBJ_ITER_H */ diff --git a/gdb/varobj.c b/gdb/varobj.c index e4d640f..80de679 100644 --- a/gdb/varobj.c +++ b/gdb/varobj.c @@ -702,7 +702,7 @@ update_dynamic_varobj_children (struct varobj *var, if (update_children || var->dynamic->child_iter == NULL) { - varobj_iter_delete (var->dynamic->child_iter); + delete var->dynamic->child_iter; var->dynamic->child_iter = varobj_get_iterator (var); varobj_clear_saved_item (var->dynamic); @@ -729,7 +729,7 @@ update_dynamic_varobj_children (struct varobj *var, } else { - item = varobj_iter_next (var->dynamic->child_iter); + item = var->dynamic->child_iter->next (); /* Release vitem->value so its lifetime is not bound to the execution of a command. */ if (item != NULL && item->value != NULL) @@ -739,7 +739,7 @@ update_dynamic_varobj_children (struct varobj *var, if (item == NULL) { /* Iteration is done. Remove iterator from VAR. */ - varobj_iter_delete (var->dynamic->child_iter); + delete var->dynamic->child_iter; var->dynamic->child_iter = NULL; break; } @@ -1070,7 +1070,7 @@ install_visualizer (struct varobj_dynamic *var, PyObject *constructor, Py_XDECREF (var->pretty_printer); var->pretty_printer = visualizer; - varobj_iter_delete (var->child_iter); + delete var->child_iter; var->child_iter = NULL; } @@ -1881,7 +1881,7 @@ varobj::~varobj () } #endif - varobj_iter_delete (var->dynamic->child_iter); + delete var->dynamic->child_iter; varobj_clear_saved_item (var->dynamic); if (is_root_p (var)) |