[Python] Add methods reference_value and const_value to gdb.Value.
authorSiva Chandra <sivachandra@chromium.org>
Sat, 25 Apr 2015 14:04:40 +0000 (07:04 -0700)
committerSiva Chandra <sivachandra@chromium.org>
Sun, 10 May 2015 00:30:35 +0000 (17:30 -0700)
gdb/ChangeLog:

* NEWS (Python Scripting): Mention the new gdb.Value methods.
* python/py-value.c (valpy_reference_value): New function.
(valpy_const_value): Likewise.
(value_object_methods): Add new methods.
* value.c (make_cv_value): New function.
* value.h (make_cv_value): Declare.

gdb/doc/ChangeLog:

* python.texi (Values From Inferior): Add descriptions of new
methods gdb.Value.reference_value and gdb.Value.const_value.

gdb/testsuite/ChangeLog:

* gdb.python/py-xmethods.cc: Enhance test case.
* gdb.python/py-xmethods.exp: New tests.
* gdb.python/py-xmethods.py (A_indexoper): New xmethod worker
function.
(B_indexoper): Likewise.
(global_dm_list) : Add new xmethod worker functions.

gdb/ChangeLog
gdb/NEWS
gdb/doc/ChangeLog
gdb/doc/python.texi
gdb/python/py-value.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-xmethods.cc
gdb/testsuite/gdb.python/py-xmethods.exp
gdb/testsuite/gdb.python/py-xmethods.py
gdb/value.c
gdb/value.h

index 1004e114555f92f87a0a56ccf19fa7b1e9caeef4..b58f04ab5e4c9e9e73b84158972ac6600af8af80 100644 (file)
@@ -1,3 +1,12 @@
+2015-05-09  Siva Chandra Reddy  <sivachandra@google.com>
+
+       * NEWS (Python Scripting): Mention the new gdb.Value methods.
+       * python/py-value.c (valpy_reference_value): New function.
+       (valpy_const_value): Likewise.
+       (value_object_methods): Add new methods.
+       * value.c (make_cv_value): New function.
+       * value.h (make_cv_value): Declare.
+
 2015-05-08  Yao Qi  <yao@codesourcery.com>
            Sandra Loosemore  <sandra@codesourcery.com>
 
index 0c7084aa158b0f9683eab03e7c931e0dc2daf963..51e4f4e9b93efab33590ba7feaf990b55a404fcd 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -57,6 +57,9 @@
   ** gdb.Type objects have a new method "optimized_out",
      returning optimized out gdb.Value instance of this type.
   ** Xmethods can now specify a result type.
+  ** gdb.Value objects have new methods "reference_value" and
+     "const_value" which return a reference to the value and a
+     "const" version of the value respectively.
 
 * New commands
 
index f374fea6656ed2c8860428787b11864980bf1d72..727d0ad05ed83d176e8479f8be4de0e43c5d2496 100644 (file)
@@ -1,3 +1,8 @@
+2015-05-09  Siva Chandra Reddy  <sivachandra@google.com>
+
+       * python.texi (Values From Inferior): Add descriptions of new
+       methods gdb.Value.reference_value and gdb.Value.const_value.
+
 2015-05-06  Joel Brobecker  <brobecker@adacore.com>
 
        * gdb.texinfo (Files): Add "info dll" documentation.
index fc3c74518046832df4256adbffa52b259c3084b8..57ec22efae4b6f0b89808b1730e27c698d34cb68 100644 (file)
@@ -781,6 +781,16 @@ The @code{gdb.Value} object @code{py_val} is identical to that
 corresponding to @code{val}.
 @end defun
 
+@defun Value.reference_value ()
+Return a @code{gdb.Value} object which is a reference to the value
+encapsulated by this instance.
+@end defun
+
+@defun Value.const_value ()
+Return a @code{gdb.Value} object which is a @code{const} version of the
+value encapsulated by this instance.
+@end defun
+
 @defun Value.dynamic_cast (type)
 Like @code{Value.cast}, but works as if the C@t{++} @code{dynamic_cast}
 operator were used.  Consult a C@t{++} reference for details.
index 6622d11e8dc877f29d17d253f45545f12ea50814..97eb66ae7d63bf0045d3b4a1b5b5266bc39eb810 100644 (file)
@@ -236,6 +236,59 @@ valpy_referenced_value (PyObject *self, PyObject *args)
   return result;
 }
 
+/* Return a value which is a reference to the value.  */
+
+static PyObject *
+valpy_reference_value (PyObject *self, PyObject *args)
+{
+  PyObject *result = NULL;
+
+  TRY
+    {
+      struct value *self_val;
+      struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
+
+      self_val = ((value_object *) self)->value;
+      result = value_to_value_object (value_ref (self_val));
+
+      do_cleanups (cleanup);
+    }
+  CATCH (except, RETURN_MASK_ALL)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+    }
+  END_CATCH
+
+  return result;
+}
+
+/* Return a "const" qualified version of the value.  */
+
+static PyObject *
+valpy_const_value (PyObject *self, PyObject *args)
+{
+  PyObject *result = NULL;
+
+  TRY
+    {
+      struct value *self_val, *res_val;
+      struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
+
+      self_val = ((value_object *) self)->value;
+      res_val = make_cv_value (1, 0, self_val);
+      result = value_to_value_object (res_val);
+
+      do_cleanups (cleanup);
+    }
+  CATCH (except, RETURN_MASK_ALL)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+    }
+  END_CATCH
+
+  return result;
+}
+
 /* Return "&value".  */
 static PyObject *
 valpy_get_address (PyObject *self, void *closure)
@@ -1692,6 +1745,10 @@ reinterpret_cast operator."
   { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
   { "referenced_value", valpy_referenced_value, METH_NOARGS,
     "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
+  { "reference_value", valpy_reference_value, METH_NOARGS,
+    "Return a value of type TYPE_CODE_REF referencing this value." },
+  { "const_value", valpy_const_value, METH_NOARGS,
+    "Return a 'const' qualied version of the same value." },
   { "lazy_string", (PyCFunction) valpy_lazy_string,
     METH_VARARGS | METH_KEYWORDS,
     "lazy_string ([encoding]  [, length]) -> lazy_string\n\
index f4ee04a48be498537bbb435a28b10bdda840ac53..52140bd9536805e17efb22e19db83980deb50a68 100644 (file)
@@ -1,3 +1,12 @@
+2015-05-09  Siva Chandra Reddy  <sivachandra@google.com>
+
+       * gdb.python/py-xmethods.cc: Enhance test case.
+       * gdb.python/py-xmethods.exp: New tests.
+       * gdb.python/py-xmethods.py (A_indexoper): New xmethod worker
+       function.
+       (B_indexoper): Likewise.
+       (global_dm_list) : Add new xmethod worker functions.
+
 2015-05-08  Sergio Durigan Junior  <sergiodj@redhat.com>
 
        * gdb.base/coredump-filter.exp: Correctly unset
index aedd1de1043b1eafa0fe41a0821130494ccdf2f9..98bdb728aa0a88a66480a9be3a27b1d9631240db 100644 (file)
@@ -173,7 +173,7 @@ int main(void)
 
   for (int i = 0; i < 10; i++)
     {
-      a1.array[i] = a2.array[i] = i;
+      a1.array[i] = a2.array[i] = b1.array[i] = i;
     }
 
   return 0; /* Break here.  */
index 712f271be22eabf9beb0e9c03cec91b7ce921a36..274b06e512432ba52cb814ad1b8fe7466fa75489 100644 (file)
@@ -100,6 +100,8 @@ gdb_test "p a1.geta()" "From Python <A_geta>.*5" "After: a1.geta()"
 gdb_test "p ++a1" "From Python <plus_plus_A>.*6" "After: ++a1"
 gdb_test "p a1.getarrayind(5)" "From Python <A_getarrayind>.*5" \
   "After: a1.getarrayind(5)"
+gdb_test "P a1\[6\]" ".*int &.*6" "After a1\[\]"
+gdb_test "P b1\[7\]" ".*const int &.*7" "After b1\[\]"
 # Note the following test.  Xmethods on dynamc types are not looked up
 # currently.  Hence, even though a_ptr points to a B object, the xmethod
 # defined for A objects is invoked.
index 3a2f100f3b5c947468cd6a191a46e3482de99785..a32781acd9df042e2d583a69250c316e5ba2e69c 100644 (file)
@@ -43,6 +43,12 @@ def A_getarrayind(obj, index):
   print('From Python <A_getarrayind>:')
   return obj['array'][index]
 
+def A_indexoper(obj, index):
+  return obj['array'][index].reference_value()
+
+def B_indexoper(obj, index):
+  return obj['array'][index].const_value().reference_value()
+
 
 type_A = gdb.parse_and_eval('(dop::A *) 0').type.target()
 type_B = gdb.parse_and_eval('(dop::B *) 0').type.target()
@@ -213,6 +219,16 @@ global_dm_list = [
                          '^getarrayind$',
                          A_getarrayind,
                          type_int),
+    SimpleXMethodMatcher('A_indexoper',
+                         '^dop::A$',
+                         'operator\\[\\]',
+                         A_indexoper,
+                         type_int),
+    SimpleXMethodMatcher('B_indexoper',
+                         '^dop::B$',
+                         'operator\\[\\]',
+                         B_indexoper,
+                         type_int)
 ]
 
 for matcher in global_dm_list:
index 2b3288185db0786dcf54571a809ca03159a6e3ff..86d7d0029d2d680887e4b06cda7b3763b4362b5e 100644 (file)
@@ -1704,6 +1704,27 @@ value_copy (struct value *arg)
   return val;
 }
 
+/* Return a "const" and/or "volatile" qualified version of the value V.
+   If CNST is true, then the returned value will be qualified with
+   "const".
+   if VOLTL is true, then the returned value will be qualified with
+   "volatile".  */
+
+struct value *
+make_cv_value (int cnst, int voltl, struct value *v)
+{
+  struct type *val_type = value_type (v);
+  struct type *enclosing_type = value_enclosing_type (v);
+  struct value *cv_val = value_copy (v);
+
+  deprecated_set_value_type (cv_val,
+                            make_cv_type (cnst, voltl, val_type, NULL));
+  set_value_enclosing_type (cv_val,
+                           make_cv_type (cnst, voltl, enclosing_type, NULL));
+
+  return cv_val;
+}
+
 /* Return a version of ARG that is non-lvalue.  */
 
 struct value *
index 25107a4b12c60613729601c621b92d2a70ef1385..957bcd494636e6abf318a6d50341d92a5005de75 100644 (file)
@@ -1040,6 +1040,8 @@ extern struct value *value_non_lval (struct value *);
 
 extern void value_force_lval (struct value *, CORE_ADDR);
 
+extern struct value *make_cv_value (int, int, struct value *);
+
 extern void preserve_one_value (struct value *, struct objfile *, htab_t);
 
 /* From valops.c */
This page took 0.080363 seconds and 4 git commands to generate.