Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Phil Muldoon <pmuldoon@redhat.com>
To: tromey@redhat.com
Cc: gdb-patches ml <gdb-patches@sourceware.org>
Subject: Re: [python][patch] And range method to type
Date: Sat, 05 Dec 2009 09:19:00 -0000	[thread overview]
Message-ID: <4B1A25B0.1090903@redhat.com> (raw)
In-Reply-To: <m34oo6fw0h.fsf@fleche.redhat.com>

On 12/04/2009 06:13 PM, Tom Tromey wrote:
>>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
> 
> Phil> This patch adds a "range" method for GDB.Types.  It only supports types
> Phil> that support a range, or the range type itself.
> 
> Phil> +  if (TYPE_CODE (type) != TYPE_CODE_ARRAY &&
> Phil> +      TYPE_CODE (type) != TYPE_CODE_STRING &&
> Phil> +      TYPE_CODE (type) != TYPE_CODE_RANGE)
> 
> In the GNU style, the "&&"s go at the start of the line.


OK.


> 
> I think this code should check for failures from PyLong_FromLong and
> PyTuple_SetItem.  I realize that is a pain, but if one of those does
> fail, a failure to check will yield weird problems.


OK, the new code has more robust error checking.


> Phil>  Return a type of pointer to this type." },
> Phil> +  { "range", typy_range, METH_NOARGS,
> Phil> +    "range () -> Tuple\n\
> 
> It should be "tuple", as that is the name of the returned type.


OK.

I've regenerated the patch. Thanks for the review! What do you think?

Cheers,

Phil

--

Index: doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.647
diff -u -r1.647 gdb.texinfo
--- doc/gdb.texinfo	3 Dec 2009 21:19:48 -0000	1.647
+++ doc/gdb.texinfo	5 Dec 2009 09:11:25 -0000
@@ -19654,6 +19654,13 @@
 @code{volatile}.
 @end defmethod
 
+@defmethod Type range
+Return a Python @code{Tuple} object that contains two elements: the
+low bound of the argument type and the high bound of that type.  If
+the type does not have a range, @value{GDBN} will raise a
+@code{RuntimeError} exception.
+@end defmethod
+
 @defmethod Type reference
 Return a new @code{gdb.Type} object which represents a reference to this
 type.
Index: python/py-type.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-type.c,v
retrieving revision 1.3
diff -u -r1.3 py-type.c
--- python/py-type.c	3 Dec 2009 21:19:49 -0000	1.3
+++ python/py-type.c	5 Dec 2009 09:11:25 -0000
@@ -273,6 +273,70 @@
   return type_to_type_object (type);
 }
 
+/* Return the range of a type represented by SELF.  The return type is
+   a tuple.  The first element of the tuple contains the low bound,
+   while the second element of the tuple contains the high bound.  */
+static PyObject *
+typy_range (PyObject *self, PyObject *args)
+{
+  struct type *type = ((type_object *) self)->type;
+  PyObject *result;
+  PyObject *low_bound = NULL, *high_bound = NULL;
+  LONGEST low, high;
+
+  if (TYPE_CODE (type) != TYPE_CODE_ARRAY
+      && TYPE_CODE (type) != TYPE_CODE_STRING
+      && TYPE_CODE (type) != TYPE_CODE_RANGE)
+    {
+      PyErr_SetString (PyExc_RuntimeError,
+		       "This type does not have a range.");
+      return NULL;
+    }
+
+  switch (TYPE_CODE (type))
+    {
+    case TYPE_CODE_ARRAY:
+    case TYPE_CODE_STRING:
+      low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
+      high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type));
+      break;
+    case TYPE_CODE_RANGE:
+      low = TYPE_LOW_BOUND (type);
+      high = TYPE_HIGH_BOUND (type);
+      break;
+    }
+
+  low_bound = PyLong_FromLong (low);
+  if (!low_bound)
+    goto failarg;
+
+  high_bound = PyLong_FromLong (high);
+  if (!high_bound)
+    goto failarg;
+
+  result = PyTuple_New (2);
+  if (!result)
+    goto failarg;
+
+  if (PyTuple_SetItem (result, 0, low_bound) != 0)
+    {
+      Py_DECREF (result);
+      goto failarg;
+    }
+  if (PyTuple_SetItem (result, 1, high_bound) != 0)
+    {
+      Py_DECREF (high_bound);
+      Py_DECREF (result);
+      return NULL;
+    }
+  return result;
+
+ failarg:
+  Py_XDECREF (high_bound);
+  Py_XDECREF (low_bound);
+  return NULL;
+}
+
 /* Return a Type object which represents a reference to SELF.  */
 static PyObject *
 typy_reference (PyObject *self, PyObject *args)
@@ -699,6 +763,9 @@
   { "pointer", typy_pointer, METH_NOARGS,
     "pointer () -> Type\n\
 Return a type of pointer to this type." },
+  { "range", typy_range, METH_NOARGS,
+    "range () -> tuple\n\
+Return a tuple containing the lower and upper range for this type."},
   { "reference", typy_reference, METH_NOARGS,
     "reference () -> Type\n\
 Return a type of reference to this type." },
Index: testsuite/gdb.python/py-type.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.python/py-type.exp,v
retrieving revision 1.1
diff -u -r1.1 py-type.exp
--- testsuite/gdb.python/py-type.exp	3 Dec 2009 21:19:49 -0000	1.1
+++ testsuite/gdb.python/py-type.exp	5 Dec 2009 09:11:26 -0000
@@ -102,6 +102,28 @@
   gdb_test "python print fields\[1\].is_base_class" "False" "Check base class"
 }
 
+proc test_range {} {
+
+  # Test a valid range request.
+  gdb_py_test_silent_cmd "print ar" "print value" 1
+  gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
+  gdb_test "python print len(ar.type.range())" "2" "Check correct tuple length"
+  gdb_test "python print ar.type.range()\[0\]" "0" "Check low range"
+  gdb_test "python print ar.type.range()\[1\]" "1" "Check high range"
+
+  # Test a range request on a ranged type.
+  gdb_py_test_silent_cmd "print ar" "print value" 1
+  gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from  history" 1
+  gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1
+  gdb_test "python print fields\[0\].type.range()\[0\]" "0" "Check range type low bound"
+  gdb_test "python print fields\[0\].type.range()\[1\]" "1" "Check range type high bound"
+
+  # Test where a range does not exist.
+  gdb_py_test_silent_cmd "print st" "print value" 1
+  gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
+  gdb_test "python print st.type.range()" "RuntimeError: This type does not have a range.*" "Check range for non ranged type."
+}
+
 # Perform C Tests.
 build_inferior "c"
 restart_gdb "break to inspect struct and array."
@@ -112,3 +134,4 @@
 restart_gdb "break to inspect struct and array."
 test_fields "c++"
 test_base_class
+test_range


  reply	other threads:[~2009-12-05  9:19 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-04 15:25 Phil Muldoon
2009-12-04 15:32 ` Eli Zaretskii
2009-12-04 17:10   ` Phil Muldoon
2009-12-04 19:30     ` Eli Zaretskii
2009-12-04 17:19 ` Joel Brobecker
2009-12-04 18:13 ` Tom Tromey
2009-12-05  9:19   ` Phil Muldoon [this message]
2009-12-05  9:22     ` Eli Zaretskii
2009-12-07 19:54     ` Tom Tromey
2009-12-08 14:14       ` Phil Muldoon
2009-12-09  3:00         ` Hui Zhu
2009-12-09  7:47           ` Phil Muldoon
2009-12-09 11:27             ` Eli Zaretskii
2009-12-09 11:41               ` Andreas Schwab
2009-12-09  7:52           ` Phil Muldoon
2009-12-09  7:56             ` Hui Zhu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B1A25B0.1090903@redhat.com \
    --to=pmuldoon@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox