public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [RFA/Python] Fix int() builtin with range type gdb.Value objects.
@ 2013-11-18 11:37 Joel Brobecker
  2013-11-18 16:52 ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Joel Brobecker @ 2013-11-18 11:37 UTC (permalink / raw)
  To: gdb-patches

Hello,

Consider the following variable:

    type Small is range -128 .. 127;
    SR : Small := 48;

Trying to get its value as an integer within Python code yields:

    (gdb) python sr = gdb.parse_and_eval('sr')
    (gdb) python print int(sr)
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    gdb.error: Cannot convert value to int.
    Error while executing Python code.

This is happening because our variable is a range type, and
py-value's is_intlike does not handle TYPE_CODE_RANGE. This
patch fixes this.

gdb/ChangeLog:

        * python/py-value.c (is_intlike): Add TYPE_CODE_RANGE handling.

gdb/testsuite/ChangeLog:

        * gdb.ada/py_range: New testcase.

Tested on x86_64-linux. OK to apply?

Looking beyond this issue, I compared this function with is_integral_type,
and found that is_integral_type also accepts TYPE_CODE_FLAGS. It seems
like we could probably do the same in is_intlike?

Thank you,
-- 
Joel

---
 gdb/python/py-value.c                  |  1 +
 gdb/testsuite/gdb.ada/py_range.exp     | 40 ++++++++++++++++++++++++++++++++++
 gdb/testsuite/gdb.ada/py_range/foo.adb | 32 +++++++++++++++++++++++++++
 gdb/testsuite/gdb.ada/py_range/pck.adb | 23 +++++++++++++++++++
 gdb/testsuite/gdb.ada/py_range/pck.ads | 22 +++++++++++++++++++
 5 files changed, 118 insertions(+)
 create mode 100644 gdb/testsuite/gdb.ada/py_range.exp
 create mode 100644 gdb/testsuite/gdb.ada/py_range/foo.adb
 create mode 100644 gdb/testsuite/gdb.ada/py_range/pck.adb
 create mode 100644 gdb/testsuite/gdb.ada/py_range/pck.ads

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 07feaf8..451bfaf 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1137,6 +1137,7 @@ is_intlike (struct type *type, int ptr_ok)
 	  || TYPE_CODE (type) == TYPE_CODE_ENUM
 	  || TYPE_CODE (type) == TYPE_CODE_BOOL
 	  || TYPE_CODE (type) == TYPE_CODE_CHAR
+	  || TYPE_CODE (type) == TYPE_CODE_RANGE
 	  || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
 }
 
diff --git a/gdb/testsuite/gdb.ada/py_range.exp b/gdb/testsuite/gdb.ada/py_range.exp
new file mode 100644
index 0000000..f24a06a
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/py_range.exp
@@ -0,0 +1,40 @@
+# Copyright 2013 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+load_lib "ada.exp"
+load_lib gdb-python.exp
+
+standard_ada_testfile foo
+
+if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug]] != "" } {
+  return -1
+}
+
+clean_restart ${testfile}
+
+# Skip this testcase if Python scripting is not enabled.
+if { [skip_python_tests] } { continue }
+
+set bp_location [gdb_get_line_number "STOP" ${testdir}/foo.adb]
+runto "foo.adb:$bp_location"
+
+gdb_test "python print int(gdb.parse_and_eval('sr'))" \
+         "48"
+
+gdb_test "python print int(gdb.parse_and_eval('si'))" \
+         "740804"
+
+gdb_test "python print int(gdb.parse_and_eval('ir'))" \
+         "974"
diff --git a/gdb/testsuite/gdb.ada/py_range/foo.adb b/gdb/testsuite/gdb.ada/py_range/foo.adb
new file mode 100644
index 0000000..850b10f
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/py_range/foo.adb
@@ -0,0 +1,32 @@
+--  Copyright 2013 Free Software Foundation, Inc.
+--
+--  This program is free software; you can redistribute it and/or modify
+--  it under the terms of the GNU General Public License as published by
+--  the Free Software Foundation; either version 3 of the License, or
+--  (at your option) any later version.
+--
+--  This program is distributed in the hope that it will be useful,
+--  but WITHOUT ANY WARRANTY; without even the implied warranty of
+--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+--  GNU General Public License for more details.
+--
+--  You should have received a copy of the GNU General Public License
+--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+with Pck; use Pck;
+
+procedure Foo is
+   type Small is range -128 .. 127;
+   SR : Small := 48;
+
+   type Small_Integer is range -2 ** 31 .. 2 ** 31 - 1;
+   SI : Small_Integer := 740804;
+
+   type Integer4_T is range -2 ** 31 .. 2 ** 31 - 1;
+   for Integer4_T'Size use 32;
+   IR : Integer4_T := 974;
+begin
+   Do_Nothing (SR'Address);  -- STOP
+   Do_Nothing (SI'Address);
+   Do_Nothing (IR'Address);
+end Foo;
diff --git a/gdb/testsuite/gdb.ada/py_range/pck.adb b/gdb/testsuite/gdb.ada/py_range/pck.adb
new file mode 100644
index 0000000..6b3d3c0
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/py_range/pck.adb
@@ -0,0 +1,23 @@
+--  Copyright 2007-2013 Free Software Foundation, Inc.
+--
+--  This program is free software; you can redistribute it and/or modify
+--  it under the terms of the GNU General Public License as published by
+--  the Free Software Foundation; either version 3 of the License, or
+--  (at your option) any later version.
+--
+--  This program is distributed in the hope that it will be useful,
+--  but WITHOUT ANY WARRANTY; without even the implied warranty of
+--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+--  GNU General Public License for more details.
+--
+--  You should have received a copy of the GNU General Public License
+--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+package body Pck is
+
+   procedure Do_Nothing (A : System.Address) is
+   begin
+      null;
+   end Do_Nothing;
+
+end Pck;
diff --git a/gdb/testsuite/gdb.ada/py_range/pck.ads b/gdb/testsuite/gdb.ada/py_range/pck.ads
new file mode 100644
index 0000000..e5f3fc0
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/py_range/pck.ads
@@ -0,0 +1,22 @@
+--  Copyright 2007-2013 Free Software Foundation, Inc.
+--
+--  This program is free software; you can redistribute it and/or modify
+--  it under the terms of the GNU General Public License as published by
+--  the Free Software Foundation; either version 3 of the License, or
+--  (at your option) any later version.
+--
+--  This program is distributed in the hope that it will be useful,
+--  but WITHOUT ANY WARRANTY; without even the implied warranty of
+--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+--  GNU General Public License for more details.
+--
+--  You should have received a copy of the GNU General Public License
+--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+with System;
+
+package Pck is
+
+   procedure Do_Nothing (A : System.Address);
+
+end Pck;
-- 
1.8.1.2

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFA/Python] Fix int() builtin with range type gdb.Value objects.
  2013-11-18 11:37 [RFA/Python] Fix int() builtin with range type gdb.Value objects Joel Brobecker
@ 2013-11-18 16:52 ` Tom Tromey
  2013-11-19  4:10   ` Joel Brobecker
  0 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2013-11-18 16:52 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: gdb-patches

>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:

Joel>         * python/py-value.c (is_intlike): Add TYPE_CODE_RANGE handling.

Joel>         * gdb.ada/py_range: New testcase.

Joel> Tested on x86_64-linux. OK to apply?

Yeah, it's ok.

Joel> Looking beyond this issue, I compared this function with is_integral_type,
Joel> and found that is_integral_type also accepts TYPE_CODE_FLAGS. It seems
Joel> like we could probably do the same in is_intlike?

I think we should just drop is_intlike and use is_integral_type.
The pointer type check can be inlined into valpy_long.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFA/Python] Fix int() builtin with range type gdb.Value objects.
  2013-11-18 16:52 ` Tom Tromey
@ 2013-11-19  4:10   ` Joel Brobecker
  2013-11-19 15:09     ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Joel Brobecker @ 2013-11-19  4:10 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1240 bytes --]

> >>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:
> 
> Joel>         * python/py-value.c (is_intlike): Add TYPE_CODE_RANGE handling.
> 
> Joel>         * gdb.ada/py_range: New testcase.
> 
> Joel> Tested on x86_64-linux. OK to apply?
> 
> Yeah, it's ok.

Thanks, Tom. Checked in.

> Joel> Looking beyond this issue, I compared this function with is_integral_type,
> Joel> and found that is_integral_type also accepts TYPE_CODE_FLAGS. It seems
> Joel> like we could probably do the same in is_intlike?
> 
> I think we should just drop is_intlike and use is_integral_type.
> The pointer type check can be inlined into valpy_long.

I was wondering why we werent' using is_integral_type... Attached is
a patch that does just that. An alternative could have been to
implement is_intlike on top of is_integral_type, but this seems
unnecessary at the moment.

gdb/ChangeLog:

        * python/py-value.c (is_intlike): Delete.
        (valpy_int): Replace use of CHECK_TYPEDEF and is_intlike
        by use of is_integral_type.
        (valpy_long): Replace use of CHECK_TYPEDEF and is_intlike
        by use of is_integral_type and check for TYPE_CODE_PTR.

Tested on x86_64-linux, no regression. OK to commit?

Thank you,
-- 
Joel

[-- Attachment #2: 0001-get-rid-of-py-value.c-is_intlike-use-is_integral_typ.patch --]
[-- Type: text/x-diff, Size: 2301 bytes --]

From eb3e1316137af3b88ec4a7208eabc2a048607331 Mon Sep 17 00:00:00 2001
From: Joel Brobecker <brobecker@adacore.com>
Date: Tue, 19 Nov 2013 06:56:00 +0400
Subject: [PATCH] get rid of py-value.c:is_intlike (use is_integral_type
 instead)

is_intlike was mostly duplicating is_integral_type, with the exception
of the handling of TYPE_CODE_PTR when parameter PTR_OK is nonzero.
This patches deletes the is_intlike function, using is_integral_type
instead, and adjusting the two locations where this function gets
called.

The code should remain strictly equivalent.

gdb/ChangeLog:

        * python/py-value.c (is_intlike): Delete.
        (valpy_int): Replace use of CHECK_TYPEDEF and is_intlike
        by use of is_integral_type.
        (valpy_long): Replace use of CHECK_TYPEDEF and is_intlike
        by use of is_integral_type and check for TYPE_CODE_PTR.
---
 gdb/python/py-value.c | 20 +++-----------------
 1 file changed, 3 insertions(+), 17 deletions(-)

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 451bfaf..95f9586 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1129,18 +1129,6 @@ valpy_richcompare (PyObject *self, PyObject *other, int op)
   Py_RETURN_FALSE;
 }
 
-/* Helper function to determine if a type is "int-like".  */
-static int
-is_intlike (struct type *type, int ptr_ok)
-{
-  return (TYPE_CODE (type) == TYPE_CODE_INT
-	  || TYPE_CODE (type) == TYPE_CODE_ENUM
-	  || TYPE_CODE (type) == TYPE_CODE_BOOL
-	  || TYPE_CODE (type) == TYPE_CODE_CHAR
-	  || TYPE_CODE (type) == TYPE_CODE_RANGE
-	  || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
-}
-
 #ifndef IS_PY3K
 /* Implements conversion to int.  */
 static PyObject *
@@ -1153,8 +1141,7 @@ valpy_int (PyObject *self)
 
   TRY_CATCH (except, RETURN_MASK_ALL)
     {
-      CHECK_TYPEDEF (type);
-      if (!is_intlike (type, 0))
+      if (!is_integral_type (type))
 	error (_("Cannot convert value to int."));
 
       l = value_as_long (value);
@@ -1176,9 +1163,8 @@ valpy_long (PyObject *self)
 
   TRY_CATCH (except, RETURN_MASK_ALL)
     {
-      CHECK_TYPEDEF (type);
-
-      if (!is_intlike (type, 1))
+      if (!is_integral_type (type)
+	  && TYPE_CODE (type) != TYPE_CODE_PTR)
 	error (_("Cannot convert value to long."));
 
       l = value_as_long (value);
-- 
1.8.1.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFA/Python] Fix int() builtin with range type gdb.Value objects.
  2013-11-19  4:10   ` Joel Brobecker
@ 2013-11-19 15:09     ` Tom Tromey
  2013-11-20  2:52       ` Joel Brobecker
  0 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2013-11-19 15:09 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: gdb-patches

>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:

Joel>         * python/py-value.c (is_intlike): Delete.
Joel>         (valpy_int): Replace use of CHECK_TYPEDEF and is_intlike
Joel>         by use of is_integral_type.
Joel>         (valpy_long): Replace use of CHECK_TYPEDEF and is_intlike
Joel>         by use of is_integral_type and check for TYPE_CODE_PTR.

Thanks, Joel.

Joel>    TRY_CATCH (except, RETURN_MASK_ALL)
Joel>      {
Joel> -      CHECK_TYPEDEF (type);
Joel> -
Joel> -      if (!is_intlike (type, 1))
Joel> +      if (!is_integral_type (type)
Joel> +	  && TYPE_CODE (type) != TYPE_CODE_PTR)
Joel>  	error (_("Cannot convert value to long."));

I think the CHECK_TYPEDEF is still needed in this hunk (but not the
other one), since we examine the TYPE_CODE of the type.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFA/Python] Fix int() builtin with range type gdb.Value objects.
  2013-11-19 15:09     ` Tom Tromey
@ 2013-11-20  2:52       ` Joel Brobecker
  2013-11-20 17:25         ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Joel Brobecker @ 2013-11-20  2:52 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1240 bytes --]

> >>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:
> 
> Joel>         * python/py-value.c (is_intlike): Delete.
> Joel>         (valpy_int): Replace use of CHECK_TYPEDEF and is_intlike
> Joel>         by use of is_integral_type.
> Joel>         (valpy_long): Replace use of CHECK_TYPEDEF and is_intlike
> Joel>         by use of is_integral_type and check for TYPE_CODE_PTR.
> 
> Thanks, Joel.
> 
> Joel>    TRY_CATCH (except, RETURN_MASK_ALL)
> Joel>      {
> Joel> -      CHECK_TYPEDEF (type);
> Joel> -
> Joel> -      if (!is_intlike (type, 1))
> Joel> +      if (!is_integral_type (type)
> Joel> +	  && TYPE_CODE (type) != TYPE_CODE_PTR)
> Joel>  	error (_("Cannot convert value to long."));
> 
> I think the CHECK_TYPEDEF is still needed in this hunk (but not the
> other one), since we examine the TYPE_CODE of the type.

I missed that, you're right! New patch attached.

gdb/ChangeLog:

        * python/py-value.c (is_intlike): Delete.
        (valpy_int): Replace use of CHECK_TYPEDEF and is_intlike
        by use of is_integral_type.
        (valpy_long): Replace use of CHECK_TYPEDEF and is_intlike
        by use of is_integral_type and check for TYPE_CODE_PTR.

Re-tested on x86_64-linux, JIC.

Thanks!
-- 
Joel

[-- Attachment #2: 0001-get-rid-of-py-value.c-is_intlike-use-is_integral_typ.patch --]
[-- Type: text/x-diff, Size: 2258 bytes --]

From 4ac78121f75057f3e4ccbbd22c57d373ae60bd05 Mon Sep 17 00:00:00 2001
From: Joel Brobecker <brobecker@adacore.com>
Date: Tue, 19 Nov 2013 06:56:00 +0400
Subject: [PATCH] get rid of py-value.c:is_intlike (use is_integral_type
 instead)

is_intlike was mostly duplicating is_integral_type, with the exception
of the handling of TYPE_CODE_PTR when parameter PTR_OK is nonzero.
This patches deletes the is_intlike function, using is_integral_type
instead, and adjusting the two locations where this function gets
called.

The code should remain strictly equivalent.

gdb/ChangeLog:

        * python/py-value.c (is_intlike): Delete.
        (valpy_int): Replace use of CHECK_TYPEDEF and is_intlike
        by use of is_integral_type.
        (valpy_long): Replace use of CHECK_TYPEDEF and is_intlike
        by use of is_integral_type and check for TYPE_CODE_PTR.
---
 gdb/python/py-value.c | 18 +++---------------
 1 file changed, 3 insertions(+), 15 deletions(-)

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 451bfaf..4054808 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1129,18 +1129,6 @@ valpy_richcompare (PyObject *self, PyObject *other, int op)
   Py_RETURN_FALSE;
 }
 
-/* Helper function to determine if a type is "int-like".  */
-static int
-is_intlike (struct type *type, int ptr_ok)
-{
-  return (TYPE_CODE (type) == TYPE_CODE_INT
-	  || TYPE_CODE (type) == TYPE_CODE_ENUM
-	  || TYPE_CODE (type) == TYPE_CODE_BOOL
-	  || TYPE_CODE (type) == TYPE_CODE_CHAR
-	  || TYPE_CODE (type) == TYPE_CODE_RANGE
-	  || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
-}
-
 #ifndef IS_PY3K
 /* Implements conversion to int.  */
 static PyObject *
@@ -1153,8 +1141,7 @@ valpy_int (PyObject *self)
 
   TRY_CATCH (except, RETURN_MASK_ALL)
     {
-      CHECK_TYPEDEF (type);
-      if (!is_intlike (type, 0))
+      if (!is_integral_type (type))
 	error (_("Cannot convert value to int."));
 
       l = value_as_long (value);
@@ -1178,7 +1165,8 @@ valpy_long (PyObject *self)
     {
       CHECK_TYPEDEF (type);
 
-      if (!is_intlike (type, 1))
+      if (!is_integral_type (type)
+	  && TYPE_CODE (type) != TYPE_CODE_PTR)
 	error (_("Cannot convert value to long."));
 
       l = value_as_long (value);
-- 
1.8.1.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFA/Python] Fix int() builtin with range type gdb.Value objects.
  2013-11-20  2:52       ` Joel Brobecker
@ 2013-11-20 17:25         ` Tom Tromey
  2013-11-20 17:28           ` pushed: " Joel Brobecker
  0 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2013-11-20 17:25 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: gdb-patches

>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:

Joel> I missed that, you're right! New patch attached.

Joel> gdb/ChangeLog:

Joel>         * python/py-value.c (is_intlike): Delete.
Joel>         (valpy_int): Replace use of CHECK_TYPEDEF and is_intlike
Joel>         by use of is_integral_type.
Joel>         (valpy_long): Replace use of CHECK_TYPEDEF and is_intlike
Joel>         by use of is_integral_type and check for TYPE_CODE_PTR.

Joel> Re-tested on x86_64-linux, JIC.

Not sure if you are waiting for me, but just in case, this looks good --
thanks for cleaning this up.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* pushed: [RFA/Python] Fix int() builtin with range type gdb.Value objects.
  2013-11-20 17:25         ` Tom Tromey
@ 2013-11-20 17:28           ` Joel Brobecker
  0 siblings, 0 replies; 7+ messages in thread
From: Joel Brobecker @ 2013-11-20 17:28 UTC (permalink / raw)
  To: gdb-patches

> Joel> gdb/ChangeLog:
> 
> Joel>         * python/py-value.c (is_intlike): Delete.
> Joel>         (valpy_int): Replace use of CHECK_TYPEDEF and is_intlike
> Joel>         by use of is_integral_type.
> Joel>         (valpy_long): Replace use of CHECK_TYPEDEF and is_intlike
> Joel>         by use of is_integral_type and check for TYPE_CODE_PTR.
> 
> Joel> Re-tested on x86_64-linux, JIC.
> 
> Not sure if you are waiting for me, but just in case, this looks good --
> thanks for cleaning this up.

Thanks Tom. I was playing it safe, since there was no actual visible
issues besides the cleanup itself.  Now pushed!

-- 
Joel

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-11-20 17:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-18 11:37 [RFA/Python] Fix int() builtin with range type gdb.Value objects Joel Brobecker
2013-11-18 16:52 ` Tom Tromey
2013-11-19  4:10   ` Joel Brobecker
2013-11-19 15:09     ` Tom Tromey
2013-11-20  2:52       ` Joel Brobecker
2013-11-20 17:25         ` Tom Tromey
2013-11-20 17:28           ` pushed: " Joel Brobecker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).