This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[python] add 'array' display hint


This adds an "array" display hint.  This lets the CLI printer code
format things more nicely for the user.

This still isn't perfect.  We don't respect "set print array on" --
instead we use "set print pretty"'s value.  I'll deal with this in a
followup patch, probably next week.

This patch also makes the CLI children formatter respect "set print
elements".

Tom

2008-12-12  Tom Tromey  <tromey@redhat.com>

	* python/lib/gdb/libstdcxx/v6/printers.py
	(StdVectorPrinter.display_hint): Return 'array'.
	(StdDequePrinter.display_hint): New method.
	(StdStackOrQueuePrinter.display_hint): Likewise.
	* python/python.c (print_children): Handle "array" hint.  Respect
	"print elements".

2008-12-12  Tom Tromey  <tromey@redhat.com>

	* gdb.texinfo (Pretty Printing): Document 'array' hint.

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index fd17158..eeaaf61 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -18602,6 +18602,11 @@ being printed.
 Some display hints are predefined by @value{GDBN}:
 
 @table @samp
+@item array
+Indicate that the object being printed is ``array-like''.  The CLI
+uses this to respect parameters such as @code{set print elements} and
+@code{set print array}.
+
 @item map
 Indicate that the object being printed is ``map-like'', and that the
 children of this value can be assumed to alternate between keys and
diff --git a/gdb/python/lib/gdb/libstdcxx/v6/printers.py b/gdb/python/lib/gdb/libstdcxx/v6/printers.py
index 1ea4dd2..c03c682 100644
--- a/gdb/python/lib/gdb/libstdcxx/v6/printers.py
+++ b/gdb/python/lib/gdb/libstdcxx/v6/printers.py
@@ -143,7 +143,7 @@ class StdVectorPrinter:
                 % (int (finish - start), int (end - start)))
 
     def display_hint(self):
-        return 'whatever'
+        return 'array'
 
 class StdStackOrQueuePrinter:
     "Print a std::stack or std::queue"
@@ -159,6 +159,11 @@ class StdStackOrQueuePrinter:
         return '%s wrapping: %s' % (self.typename,
                                     self.visualizer.to_string())
 
+    def display_hint (self):
+        if hasattr (self.visualizer, 'display_hint'):
+            return self.visualizer.display_hint ()
+        return None
+
 class RbtreeIterator:
     def __init__(self, rbtree):
         self.size = rbtree['_M_t']['_M_impl']['_M_node_count']
@@ -361,6 +366,9 @@ class StdDequePrinter:
         return self._iter(start['_M_node'], start['_M_cur'], start['_M_last'],
                           end['_M_cur'], self.buffer_size)
 
+    def display_hint (self):
+        return 'array'
+
 class WideEncoding (gdb.Parameter):
     """The target wide character set is the encoding used for wchar_t."""
 
diff --git a/gdb/python/python.c b/gdb/python/python.c
index c2d4897..36e8356 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1023,15 +1023,18 @@ print_children (PyObject *printer, const char *hint,
 		const struct value_print_options *options,
 		const struct language_defn *language)
 {
-  int is_map, i;
+  int is_map, is_array, done_flag;
+  unsigned int i;
   PyObject *children, *iter;
   struct cleanup *cleanups;
 
   if (! PyObject_HasAttr (printer, gdbpy_children_cst))
     return;
 
-  /* If we are printing a map, we want some special formatting.  */
+  /* If we are printing a map or an array, we want some special
+     formatting.  */
   is_map = hint && ! strcmp (hint, "map");
+  is_array = hint && ! strcmp (hint, "array");
 
   children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
 					 NULL);
@@ -1051,14 +1054,20 @@ print_children (PyObject *printer, const char *hint,
     }
   make_cleanup_py_decref (iter);
 
-  for (i = 0; ; ++i)
+  done_flag = 0;
+  for (i = 0; i < options->print_max; ++i)
     {
       PyObject *py_v, *item = PyIter_Next (iter);
       char *name;
       struct cleanup *inner_cleanup;
 
       if (! item)
-	break;
+	{
+	  /* Set a flag so we can know whether we printed all the
+	     available elements.  */
+	  done_flag = 1;
+	  break;
+	}
 
       if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
 	{
@@ -1068,6 +1077,11 @@ print_children (PyObject *printer, const char *hint,
 	}
       inner_cleanup = make_cleanup_py_decref (item);
 
+      /* Print initial "{".  For other elements, there are three
+	 cases:
+	 1. Maps.  Print a "," after each value element.
+	 2. Arrays.  Always print a ",".
+	 3. Other.  Always print a ",".  */
       if (i == 0)
 	fputs_filtered (" = {", stream);
       else if (! is_map || i % 2 == 0)
@@ -1081,6 +1095,13 @@ print_children (PyObject *printer, const char *hint,
 
       if (is_map && i % 2 == 0)
 	fputs_filtered ("[", stream);
+      else if (is_array)
+	{
+	  /* We print the index, not whatever the child method
+	     returned as the name.  */
+	  if (options->print_array_indexes)
+	    fprintf_filtered (stream, "[%d] = ", i);
+	}
       else if (! is_map)
 	{
 	  fputs_filtered (name, stream);
@@ -1114,6 +1135,15 @@ print_children (PyObject *printer, const char *hint,
 
   if (i)
     {
+      if (!done_flag)
+	{
+	  if (options->pretty)
+	    {
+	      fputs_filtered ("\n", stream);
+	      print_spaces_filtered (2 + 2 * recurse, stream);
+	    }
+	  fputs_filtered ("...", stream);
+	}
       if (options->pretty)
 	{
 	  fputs_filtered ("\n", stream);


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]