6068e63
2010-05-31  Chris Moller  <cmoller@redhat.com>
6068e63
6068e63
	* python/py-prettyprint.c (print_children): Add formatting for
6068e63
	matrices. (apply_val_pretty_printer): Detect and deal with matrix
6068e63
	hints. 
6068e63
6068e63
6068e63
2010-05-31  Chris Moller  <cmoller@redhat.com>
6068e63
6068e63
	* gdb.python/Makefile.in (EXECUTABLES):  Added pr10659.
6068e63
	* gdb.python/pr10659.cc:  New file.
6068e63
	* gdb.python/pr10659.exp.  New file.
6068e63
	* gdb.python/pr10659.py: New file.
6068e63
6068e63
Index: gdb-7.1/gdb/valprint.h
6068e63
===================================================================
6068e63
--- gdb-7.1.orig/gdb/valprint.h	2010-06-30 14:02:16.000000000 +0200
6068e63
+++ gdb-7.1/gdb/valprint.h	2010-06-30 14:35:24.000000000 +0200
6068e63
@@ -90,6 +90,9 @@ struct value_print_options
6068e63
 
6068e63
   /* If nonzero, print the value in "summary" form.  */
6068e63
   int summary;
6068e63
+
6068e63
+  /* Affects pretty printing of matrices.  */
6068e63
+  int prettyprint_matrix;
6068e63
 };
6068e63
 
6068e63
 /* The global print options set by the user.  In general this should
6068e63
Index: gdb-7.1/gdb/python/py-prettyprint.c
6068e63
===================================================================
6068e63
--- gdb-7.1.orig/gdb/python/py-prettyprint.c	2010-06-30 14:01:40.000000000 +0200
6068e63
+++ gdb-7.1/gdb/python/py-prettyprint.c	2010-06-30 14:34:49.000000000 +0200
6068e63
@@ -385,7 +385,8 @@ print_children (PyObject *printer, const
6068e63
 
6068e63
   /* Use the prettyprint_arrays option if we are printing an array,
6068e63
      and the pretty option otherwise.  */
6068e63
-  pretty = is_array ? options->prettyprint_arrays : options->pretty;
6068e63
+  pretty = (is_array || options->prettyprint_matrix) ?
6068e63
+    options->prettyprint_arrays : options->pretty;
6068e63
 
6068e63
   /* Manufacture a dummy Python frame to work around Python 2.4 bug,
6068e63
      where it insists on having a non-NULL tstate->frame when
6068e63
@@ -397,6 +398,9 @@ print_children (PyObject *printer, const
6068e63
       goto done;
6068e63
     }
6068e63
   make_cleanup_py_decref (frame);
6068e63
+  
6068e63
+  if (options->prettyprint_matrix && recurse == 0)
6068e63
+    fputs_filtered ("\n", stream);
6068e63
 
6068e63
   done_flag = 0;
6068e63
   for (i = 0; i < options->print_max; ++i)
6068e63
@@ -431,12 +435,23 @@ print_children (PyObject *printer, const
6068e63
 	 3. Other.  Always print a ",".  */
6068e63
       if (i == 0)
6068e63
 	{
6068e63
-         if (is_py_none)
6068e63
-           fputs_filtered ("{", stream);
6068e63
-         else
6068e63
-           fputs_filtered (" = {", stream);
6068e63
+	  if (options->prettyprint_matrix && recurse == 0)
6068e63
+	    print_spaces_filtered (2 + 2 * recurse, stream);
6068e63
+	  if (is_py_none)
6068e63
+	    {
6068e63
+	      if (options->prettyprint_matrix && strcmp (hint, "array"))
6068e63
+		{
6068e63
+		  fputs_filtered ("{\n", stream);
6068e63
+		  print_spaces_filtered (4 + 2 * recurse, stream);
6068e63
+		}
6068e63
+	      else
6068e63
+		fputs_filtered ("{", stream);
6068e63
+	    }
6068e63
+	  else
6068e63
+	    fputs_filtered (" = {", stream);
6068e63
        }
6068e63
-
6068e63
+      else if (options->prettyprint_matrix)
6068e63
+	print_spaces_filtered (4 + 2 * recurse, stream);
6068e63
       else if (! is_map || i % 2 == 0)
6068e63
 	fputs_filtered (pretty ? "," : ", ", stream);
6068e63
 
6068e63
@@ -465,6 +480,10 @@ print_children (PyObject *printer, const
6068e63
 
6068e63
       if (is_map && i % 2 == 0)
6068e63
 	fputs_filtered ("[", stream);
6068e63
+      else if (options->prettyprint_matrix)
6068e63
+	{
6068e63
+	  /* Force a do-nothing.  */
6068e63
+	}
6068e63
       else if (is_array)
6068e63
 	{
6068e63
 	  /* We print the index, not whatever the child method
6068e63
@@ -539,7 +558,12 @@ print_children (PyObject *printer, const
6068e63
 	  fputs_filtered ("\n", stream);
6068e63
 	  print_spaces_filtered (2 * recurse, stream);
6068e63
 	}
6068e63
-      fputs_filtered ("}", stream);
6068e63
+      if (options->prettyprint_matrix)
6068e63
+      {
6068e63
+	print_spaces_filtered (4 * recurse, stream);
6068e63
+	fputs_filtered ("}\n", stream);
6068e63
+      }
6068e63
+      else fputs_filtered ("}", stream);
6068e63
     }
6068e63
 
6068e63
  done:
6068e63
@@ -561,6 +585,7 @@ apply_val_pretty_printer (struct type *t
6068e63
   struct cleanup *cleanups;
6068e63
   int result = 0;
6068e63
   int is_py_none = 0;
6068e63
+  struct value_print_options *options_copy;
6068e63
   cleanups = ensure_python_env (gdbarch, language);
6068e63
 
6068e63
   /* Instantiate the printer.  */
6068e63
@@ -582,12 +607,23 @@ apply_val_pretty_printer (struct type *t
6068e63
 
6068e63
   /* If we are printing a map, we want some special formatting.  */
6068e63
   hint = gdbpy_get_display_hint (printer);
6068e63
+  
6068e63
+  if (recurse == 0)
6068e63
+    {
6068e63
+      options_copy = alloca (sizeof (struct value_print_options));
6068e63
+      memcpy (options_copy, options, sizeof (struct value_print_options));
6068e63
+      options_copy->prettyprint_matrix = hint && !strcmp (hint, "matrix");
6068e63
+    }
6068e63
+  else options_copy = (struct value_print_options *)options;
6068e63
+
6068e63
   make_cleanup (free_current_contents, &hint);
6068e63
 
6068e63
   /* Print the section */
6068e63
-  is_py_none = print_string_repr (printer, hint, stream, recurse,
6068e63
-				  options, language, gdbarch);
6068e63
-  print_children (printer, hint, stream, recurse, options, language,
6068e63
+  is_py_none = options_copy->prettyprint_matrix ?
6068e63
+    1 : print_string_repr (printer, hint, stream,
6068e63
+			   recurse, options_copy,
6068e63
+			   language, gdbarch);
6068e63
+  print_children (printer, hint, stream, recurse, options_copy, language,
6068e63
 		  is_py_none);
6068e63
 
6068e63
   result = 1;
6068e63
Index: gdb-7.1/gdb/testsuite/gdb.python/pr10659.cc
6068e63
===================================================================
6068e63
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
6068e63
+++ gdb-7.1/gdb/testsuite/gdb.python/pr10659.cc	2010-06-30 14:34:49.000000000 +0200
6068e63
@@ -0,0 +1,43 @@
6068e63
+#include <list>
6068e63
+#include <vector>  // /usr/include/c++/4.4.1/bits/vector.tcc
6068e63
+#include <iostream>
6068e63
+
6068e63
+using namespace std;
6068e63
+
6068e63
+int use_windows = 9999;
6068e63
+
6068e63
+int
6068e63
+main(){
6068e63
+  vector<int> test1(2,0);
6068e63
+  test1[0]=8;
6068e63
+  test1[1]=9;
6068e63
+  
6068e63
+  vector< vector<int> > test2(3, vector<int>(2,0));
6068e63
+  test2[0][0]=0;
6068e63
+  test2[0][1]=1;
6068e63
+  test2[1][0]=2;
6068e63
+  test2[1][1]=3;
6068e63
+  test2[2][0]=4;
6068e63
+  test2[2][1]=5;
6068e63
+
6068e63
+#define NR_ROWS    2
6068e63
+#define NR_COLS    3
6068e63
+#define NR_PLANES  4
6068e63
+  vector<int> rows(NR_ROWS, 0);
6068e63
+  vector< vector<int> > columns(NR_COLS, rows);
6068e63
+  vector< vector < vector<int> > > test3(NR_PLANES, columns);
6068e63
+
6068e63
+  cout << "rows.size() = " << rows.size()
6068e63
+       << ", columns.size() = " << columns.size()
6068e63
+       << ", test3.size() = " << test3.size() << "\n";
6068e63
+
6068e63
+  for (int i = 0; i < rows.size(); i++) {
6068e63
+    for (int j = 0; j < columns.size(); j++) {
6068e63
+      for (int k = 0; k < test3.size(); k++) {
6068e63
+	test3[k][j][i] = k * 100 + j * 10 + i;
6068e63
+      }
6068e63
+    }
6068e63
+  }
6068e63
+  
6068e63
+  return 0;  // break
6068e63
+}
6068e63
Index: gdb-7.1/gdb/testsuite/gdb.python/pr10659.exp
6068e63
===================================================================
6068e63
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
6068e63
+++ gdb-7.1/gdb/testsuite/gdb.python/pr10659.exp	2010-06-30 14:34:49.000000000 +0200
6068e63
@@ -0,0 +1,82 @@
6068e63
+#Copyright 2010 Free Software Foundation, Inc.
6068e63
+
6068e63
+# This program is free software; you can redistribute it and/or modify
6068e63
+# it under the terms of the GNU General Public License as published by
6068e63
+# the Free Software Foundation; either version 3 of the License, or
6068e63
+# (at your option) any later version.
6068e63
+#
6068e63
+# This program is distributed in the hope that it will be useful,
6068e63
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
6068e63
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6068e63
+# GNU General Public License for more details.
6068e63
+#
6068e63
+# You should have received a copy of the GNU General Public License
6068e63
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
6068e63
+
6068e63
+set nl             "\[\r\n\]+"
6068e63
+
6068e63
+set testfile pr10659
6068e63
+set srcfile ${testfile}.cc
6068e63
+if [prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}] {
6068e63
+    return -1
6068e63
+}
6068e63
+
6068e63
+#if { [skip_python_tests] } { continue }
6068e63
+
6068e63
+gdb_test "python execfile(\"$srcdir/$subdir/pr10659.py\")" ""
6068e63
+gdb_test "python gdb.pretty_printers = \[lookup_function\]" ""
6068e63
+
6068e63
+if ![runto_main] then {
6068e63
+    fail "Can't run to main"
6068e63
+    return
6068e63
+}
6068e63
+
6068e63
+gdb_breakpoint [gdb_get_line_number "break"]
6068e63
+gdb_continue_to_breakpoint "break"
6068e63
+
6068e63
+gdb_test "p test1" "vector of length 2, capacity 2 =.*"
6068e63
+
6068e63
+gdb_test "p test2" "= $nl  {$nl    {.*"
6068e63
+
6068e63
+# Complete result is:
6068e63
+#
6068e63
+# (gdb) p test2
6068e63
+# $2 =
6068e63
+#   {
6068e63
+#     {0      1    }
6068e63
+#     {2      3    }
6068e63
+#     {4      5    }
6068e63
+#  }
6068e63
+
6068e63
+
6068e63
+gdb_test "p test3" "= $nl  {$nl    {$nl      {.*"
6068e63
+
6068e63
+# Complete result is:
6068e63
+#
6068e63
+# (gdb) p test3
6068e63
+# $3 =
6068e63
+#   {
6068e63
+#     {
6068e63
+#       {0        1        }
6068e63
+#       {10        11        }
6068e63
+#       {20        21        }
6068e63
+#     }
6068e63
+#     {
6068e63
+#       {100        101        }
6068e63
+#       {110        111        }
6068e63
+#       {120        121        }
6068e63
+#     }
6068e63
+#     {
6068e63
+#       {200        201        }
6068e63
+#       {210        211        }
6068e63
+#       {220        221        }
6068e63
+#     }
6068e63
+#     {
6068e63
+#       {300        301        }
6068e63
+#       {310        311        }
6068e63
+#       {320        321        }
6068e63
+#     }
6068e63
+#  }
6068e63
+# 
6068e63
+
6068e63
+
6068e63
Index: gdb-7.1/gdb/testsuite/gdb.python/pr10659.py
6068e63
===================================================================
6068e63
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
6068e63
+++ gdb-7.1/gdb/testsuite/gdb.python/pr10659.py	2010-06-30 14:34:49.000000000 +0200
6068e63
@@ -0,0 +1,109 @@
6068e63
+# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
6068e63
+
6068e63
+# This program is free software; you can redistribute it and/or modify
6068e63
+# it under the terms of the GNU General Public License as published by
6068e63
+# the Free Software Foundation; either version 3 of the License, or
6068e63
+# (at your option) any later version.
6068e63
+#
6068e63
+# This program is distributed in the hope that it will be useful,
6068e63
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
6068e63
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6068e63
+# GNU General Public License for more details.
6068e63
+#
6068e63
+# You should have received a copy of the GNU General Public License
6068e63
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
6068e63
+
6068e63
+import gdb
6068e63
+import itertools
6068e63
+import re
6068e63
+
6068e63
+vector_sig = 'std::vector'
6068e63
+vector_regex = re.compile('^' + vector_sig + '<.*>$')
6068e63
+
6068e63
+class FakeVectorPrinter:
6068e63
+    "Print a std::vector"
6068e63
+
6068e63
+    class _iterator:
6068e63
+        def __init__ (self, start, finish):
6068e63
+            self.item = start
6068e63
+            self.finish = finish
6068e63
+            self.count = 0
6068e63
+
6068e63
+        def __iter__(self):
6068e63
+            return self
6068e63
+
6068e63
+        def next(self):
6068e63
+            if self.item == self.finish:
6068e63
+                raise StopIteration
6068e63
+            count = self.count
6068e63
+            self.count = self.count + 1
6068e63
+            elt = self.item.dereference()
6068e63
+            self.item = self.item + 1
6068e63
+            return ('[%d]' % count, elt)
6068e63
+
6068e63
+    def __init__(self, typename, val):
6068e63
+        self.typename = typename
6068e63
+        self.val = val
6068e63
+
6068e63
+    def children(self):
6068e63
+        return self._iterator(self.val['_M_impl']['_M_start'],
6068e63
+                              self.val['_M_impl']['_M_finish'])
6068e63
+
6068e63
+    def to_string(self):
6068e63
+        start = self.val['_M_impl']['_M_start']
6068e63
+        finish = self.val['_M_impl']['_M_finish']
6068e63
+        end = self.val['_M_impl']['_M_end_of_storage']
6068e63
+        return ('std::vector of length %d, capacity %d'
6068e63
+                % (int (finish - start), int (end - start)))
6068e63
+
6068e63
+    def display_hint(self):
6068e63
+        itype0  = self.val.type.template_argument(0)
6068e63
+        itag = itype0.tag
6068e63
+        if itag and re.match(vector_regex, itag):
6068e63
+            rc = 'matrix'
6068e63
+        else:
6068e63
+            rc = 'array'
6068e63
+        return rc
6068e63
+
6068e63
+def register_libstdcxx_printers (obj):
6068e63
+    "Register libstdc++ pretty-printers with objfile Obj."
6068e63
+
6068e63
+    if obj == None:
6068e63
+        obj = gdb
6068e63
+
6068e63
+    obj.pretty_printers.append (lookup_function)
6068e63
+
6068e63
+def lookup_function (val):
6068e63
+    "Look-up and return a pretty-printer that can print val."
6068e63
+
6068e63
+    # Get the type.
6068e63
+    type = val.type;
6068e63
+
6068e63
+    # If it points to a reference, get the reference.
6068e63
+    if type.code == gdb.TYPE_CODE_REF:
6068e63
+        type = type.target ()
6068e63
+
6068e63
+    # Get the unqualified type, stripped of typedefs.
6068e63
+    type = type.unqualified ().strip_typedefs ()
6068e63
+
6068e63
+    # Get the type name.    
6068e63
+    typename = type.tag
6068e63
+    if typename == None:
6068e63
+        return None
6068e63
+
6068e63
+    # Iterate over local dictionary of types to determine
6068e63
+    # if a printer is registered for that type.  Return an
6068e63
+    # instantiation of the printer if found.
6068e63
+    for function in fake_pretty_printers_dict:
6068e63
+        if function.search (typename):
6068e63
+            return fake_pretty_printers_dict[function] (val)
6068e63
+        
6068e63
+    # Cannot find a pretty printer.  Return None.
6068e63
+    return None
6068e63
+
6068e63
+def build_libfakecxx_dictionary ():
6068e63
+    fake_pretty_printers_dict[vector_regex] = lambda val: FakeVectorPrinter(vector_sig, val)
6068e63
+
6068e63
+fake_pretty_printers_dict = {}
6068e63
+
6068e63
+build_libfakecxx_dictionary ()
6068e63
Index: gdb-7.1/gdb/valprint.c
6068e63
===================================================================
6068e63
--- gdb-7.1.orig/gdb/valprint.c	2010-06-30 13:51:26.000000000 +0200
6068e63
+++ gdb-7.1/gdb/valprint.c	2010-06-30 14:35:41.000000000 +0200
6068e63
@@ -83,7 +83,8 @@ struct value_print_options user_print_op
6068e63
   1,				/* static_field_print */
6068e63
   1,				/* pascal_static_field_print */
6068e63
   0,				/* raw */
6068e63
-  0				/* summary */
6068e63
+  0,				/* summary */
6068e63
+  0				/* prettyprint_matrix */
6068e63
 };
6068e63
 
6068e63
 /* Initialize *OPTS to be a copy of the user print options.  */