]> git.pld-linux.org Git - packages/gdb.git/blob - gdb-bz562763-pretty-print-2d-vectors.patch
- up to 7.3.1
[packages/gdb.git] / gdb-bz562763-pretty-print-2d-vectors.patch
1 2010-05-31  Chris Moller  <cmoller@redhat.com>
2
3         * python/py-prettyprint.c (print_children): Add formatting for
4         matrices. (apply_val_pretty_printer): Detect and deal with matrix
5         hints. 
6
7
8 2010-05-31  Chris Moller  <cmoller@redhat.com>
9
10         * gdb.python/Makefile.in (EXECUTABLES):  Added pr10659.
11         * gdb.python/pr10659.cc:  New file.
12         * gdb.python/pr10659.exp.  New file.
13         * gdb.python/pr10659.py: New file.
14
15 Index: gdb-7.2.50.20110218/gdb/valprint.h
16 ===================================================================
17 --- gdb-7.2.50.20110218.orig/gdb/valprint.h     2011-02-14 12:35:45.000000000 +0100
18 +++ gdb-7.2.50.20110218/gdb/valprint.h  2011-02-18 10:44:32.000000000 +0100
19 @@ -90,6 +90,9 @@ struct value_print_options
20  
21    /* If nonzero, print the value in "summary" form.  */
22    int summary;
23 +
24 +  /* Affects pretty printing of matrices.  */
25 +  int prettyprint_matrix;
26  };
27  
28  /* The global print options set by the user.  In general this should
29 Index: gdb-7.2.50.20110218/gdb/python/py-prettyprint.c
30 ===================================================================
31 --- gdb-7.2.50.20110218.orig/gdb/python/py-prettyprint.c        2011-02-14 12:10:53.000000000 +0100
32 +++ gdb-7.2.50.20110218/gdb/python/py-prettyprint.c     2011-02-18 10:45:02.000000000 +0100
33 @@ -501,7 +501,7 @@ print_children (PyObject *printer, const
34  
35    /* Use the prettyprint_arrays option if we are printing an array,
36       and the pretty option otherwise.  */
37 -  if (is_array)
38 +  if (is_array || options->prettyprint_matrix)
39      pretty = options->prettyprint_arrays;
40    else
41      {
42 @@ -521,6 +521,9 @@ print_children (PyObject *printer, const
43        goto done;
44      }
45    make_cleanup_py_decref (frame);
46 +  
47 +  if (options->prettyprint_matrix && recurse == 0)
48 +    fputs_filtered ("\n", stream);
49  
50    done_flag = 0;
51    for (i = 0; i < options->print_max; ++i)
52 @@ -555,12 +558,23 @@ print_children (PyObject *printer, const
53          3. Other.  Always print a ",".  */
54        if (i == 0)
55         {
56 -         if (is_py_none)
57 -           fputs_filtered ("{", stream);
58 -         else
59 -           fputs_filtered (" = {", stream);
60 +         if (options->prettyprint_matrix && recurse == 0)
61 +           print_spaces_filtered (2 + 2 * recurse, stream);
62 +         if (is_py_none)
63 +           {
64 +             if (options->prettyprint_matrix && strcmp (hint, "array"))
65 +               {
66 +                 fputs_filtered ("{\n", stream);
67 +                 print_spaces_filtered (4 + 2 * recurse, stream);
68 +               }
69 +             else
70 +               fputs_filtered ("{", stream);
71 +           }
72 +         else
73 +           fputs_filtered (" = {", stream);
74         }
75 -
76 +      else if (options->prettyprint_matrix)
77 +       print_spaces_filtered (4 + 2 * recurse, stream);
78        else if (! is_map || i % 2 == 0)
79         fputs_filtered (pretty ? "," : ", ", stream);
80  
81 @@ -589,6 +603,10 @@ print_children (PyObject *printer, const
82  
83        if (is_map && i % 2 == 0)
84         fputs_filtered ("[", stream);
85 +      else if (options->prettyprint_matrix)
86 +       {
87 +         /* Force a do-nothing.  */
88 +       }
89        else if (is_array)
90         {
91           /* We print the index, not whatever the child method
92 @@ -667,7 +685,12 @@ print_children (PyObject *printer, const
93           fputs_filtered ("\n", stream);
94           print_spaces_filtered (2 * recurse, stream);
95         }
96 -      fputs_filtered ("}", stream);
97 +      if (options->prettyprint_matrix)
98 +      {
99 +       print_spaces_filtered (4 * recurse, stream);
100 +       fputs_filtered ("}\n", stream);
101 +      }
102 +      else fputs_filtered ("}", stream);
103      }
104  
105   done:
106 @@ -689,6 +712,7 @@ apply_val_pretty_printer (struct type *t
107    char *hint = NULL;
108    struct cleanup *cleanups;
109    int result = 0;
110 +  struct value_print_options *options_copy;
111    enum string_repr_result print_result;
112  
113    /* No pretty-printer support for unavailable values.  */
114 @@ -726,9 +750,21 @@ apply_val_pretty_printer (struct type *t
115  
116    /* If we are printing a map, we want some special formatting.  */
117    hint = gdbpy_get_display_hint (printer);
118 +  
119 +  if (recurse == 0)
120 +    {
121 +      options_copy = alloca (sizeof (struct value_print_options));
122 +      memcpy (options_copy, options, sizeof (struct value_print_options));
123 +      options_copy->prettyprint_matrix = hint && !strcmp (hint, "matrix");
124 +    }
125 +  else options_copy = (struct value_print_options *)options;
126 +
127    make_cleanup (free_current_contents, &hint);
128  
129    /* Print the section */
130 +  if (options_copy->prettyprint_matrix)
131 +    print_result = string_repr_none;
132 +else /* Red Hat 2D matrix patch */
133    print_result = print_string_repr (printer, hint, stream, recurse,
134                                     options, language, gdbarch);
135    if (print_result != string_repr_error)
136 Index: gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.cc
137 ===================================================================
138 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
139 +++ gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.cc     2011-02-18 10:44:32.000000000 +0100
140 @@ -0,0 +1,43 @@
141 +#include <list>
142 +#include <vector>  // /usr/include/c++/4.4.1/bits/vector.tcc
143 +#include <iostream>
144 +
145 +using namespace std;
146 +
147 +int use_windows = 9999;
148 +
149 +int
150 +main(){
151 +  vector<int> test1(2,0);
152 +  test1[0]=8;
153 +  test1[1]=9;
154 +  
155 +  vector< vector<int> > test2(3, vector<int>(2,0));
156 +  test2[0][0]=0;
157 +  test2[0][1]=1;
158 +  test2[1][0]=2;
159 +  test2[1][1]=3;
160 +  test2[2][0]=4;
161 +  test2[2][1]=5;
162 +
163 +#define NR_ROWS    2
164 +#define NR_COLS    3
165 +#define NR_PLANES  4
166 +  vector<int> rows(NR_ROWS, 0);
167 +  vector< vector<int> > columns(NR_COLS, rows);
168 +  vector< vector < vector<int> > > test3(NR_PLANES, columns);
169 +
170 +  cout << "rows.size() = " << rows.size()
171 +       << ", columns.size() = " << columns.size()
172 +       << ", test3.size() = " << test3.size() << "\n";
173 +
174 +  for (int i = 0; i < rows.size(); i++) {
175 +    for (int j = 0; j < columns.size(); j++) {
176 +      for (int k = 0; k < test3.size(); k++) {
177 +       test3[k][j][i] = k * 100 + j * 10 + i;
178 +      }
179 +    }
180 +  }
181 +  
182 +  return 0;  // break
183 +}
184 Index: gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.exp
185 ===================================================================
186 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
187 +++ gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.exp    2011-02-18 10:44:32.000000000 +0100
188 @@ -0,0 +1,82 @@
189 +#Copyright 2010 Free Software Foundation, Inc.
190 +
191 +# This program is free software; you can redistribute it and/or modify
192 +# it under the terms of the GNU General Public License as published by
193 +# the Free Software Foundation; either version 3 of the License, or
194 +# (at your option) any later version.
195 +#
196 +# This program is distributed in the hope that it will be useful,
197 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
198 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
199 +# GNU General Public License for more details.
200 +#
201 +# You should have received a copy of the GNU General Public License
202 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
203 +
204 +set nl             "\[\r\n\]+"
205 +
206 +set testfile pr10659
207 +set srcfile ${testfile}.cc
208 +if [prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}] {
209 +    return -1
210 +}
211 +
212 +#if { [skip_python_tests] } { continue }
213 +
214 +gdb_test "python execfile(\"$srcdir/$subdir/pr10659.py\")" ""
215 +gdb_test "python gdb.pretty_printers = \[lookup_function\]" ""
216 +
217 +if ![runto_main] then {
218 +    fail "Can't run to main"
219 +    return
220 +}
221 +
222 +gdb_breakpoint [gdb_get_line_number "break"]
223 +gdb_continue_to_breakpoint "break"
224 +
225 +gdb_test "p test1" "vector of length 2, capacity 2 =.*"
226 +
227 +gdb_test "p test2" "= $nl  {$nl    {.*"
228 +
229 +# Complete result is:
230 +#
231 +# (gdb) p test2
232 +# $2 =
233 +#   {
234 +#     {0      1    }
235 +#     {2      3    }
236 +#     {4      5    }
237 +#  }
238 +
239 +
240 +gdb_test "p test3" "= $nl  {$nl    {$nl      {.*"
241 +
242 +# Complete result is:
243 +#
244 +# (gdb) p test3
245 +# $3 =
246 +#   {
247 +#     {
248 +#       {0        1        }
249 +#       {10        11        }
250 +#       {20        21        }
251 +#     }
252 +#     {
253 +#       {100        101        }
254 +#       {110        111        }
255 +#       {120        121        }
256 +#     }
257 +#     {
258 +#       {200        201        }
259 +#       {210        211        }
260 +#       {220        221        }
261 +#     }
262 +#     {
263 +#       {300        301        }
264 +#       {310        311        }
265 +#       {320        321        }
266 +#     }
267 +#  }
268 +# 
269 +
270 +
271 Index: gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.py
272 ===================================================================
273 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
274 +++ gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.py     2011-02-18 10:44:32.000000000 +0100
275 @@ -0,0 +1,109 @@
276 +# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
277 +
278 +# This program is free software; you can redistribute it and/or modify
279 +# it under the terms of the GNU General Public License as published by
280 +# the Free Software Foundation; either version 3 of the License, or
281 +# (at your option) any later version.
282 +#
283 +# This program is distributed in the hope that it will be useful,
284 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
285 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
286 +# GNU General Public License for more details.
287 +#
288 +# You should have received a copy of the GNU General Public License
289 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
290 +
291 +import gdb
292 +import itertools
293 +import re
294 +
295 +vector_sig = 'std::vector'
296 +vector_regex = re.compile('^' + vector_sig + '<.*>$')
297 +
298 +class FakeVectorPrinter:
299 +    "Print a std::vector"
300 +
301 +    class _iterator:
302 +        def __init__ (self, start, finish):
303 +            self.item = start
304 +            self.finish = finish
305 +            self.count = 0
306 +
307 +        def __iter__(self):
308 +            return self
309 +
310 +        def next(self):
311 +            if self.item == self.finish:
312 +                raise StopIteration
313 +            count = self.count
314 +            self.count = self.count + 1
315 +            elt = self.item.dereference()
316 +            self.item = self.item + 1
317 +            return ('[%d]' % count, elt)
318 +
319 +    def __init__(self, typename, val):
320 +        self.typename = typename
321 +        self.val = val
322 +
323 +    def children(self):
324 +        return self._iterator(self.val['_M_impl']['_M_start'],
325 +                              self.val['_M_impl']['_M_finish'])
326 +
327 +    def to_string(self):
328 +        start = self.val['_M_impl']['_M_start']
329 +        finish = self.val['_M_impl']['_M_finish']
330 +        end = self.val['_M_impl']['_M_end_of_storage']
331 +        return ('std::vector of length %d, capacity %d'
332 +                % (int (finish - start), int (end - start)))
333 +
334 +    def display_hint(self):
335 +        itype0  = self.val.type.template_argument(0)
336 +        itag = itype0.tag
337 +        if itag and re.match(vector_regex, itag):
338 +            rc = 'matrix'
339 +        else:
340 +            rc = 'array'
341 +        return rc
342 +
343 +def register_libstdcxx_printers (obj):
344 +    "Register libstdc++ pretty-printers with objfile Obj."
345 +
346 +    if obj == None:
347 +        obj = gdb
348 +
349 +    obj.pretty_printers.append (lookup_function)
350 +
351 +def lookup_function (val):
352 +    "Look-up and return a pretty-printer that can print val."
353 +
354 +    # Get the type.
355 +    type = val.type;
356 +
357 +    # If it points to a reference, get the reference.
358 +    if type.code == gdb.TYPE_CODE_REF:
359 +        type = type.target ()
360 +
361 +    # Get the unqualified type, stripped of typedefs.
362 +    type = type.unqualified ().strip_typedefs ()
363 +
364 +    # Get the type name.    
365 +    typename = type.tag
366 +    if typename == None:
367 +        return None
368 +
369 +    # Iterate over local dictionary of types to determine
370 +    # if a printer is registered for that type.  Return an
371 +    # instantiation of the printer if found.
372 +    for function in fake_pretty_printers_dict:
373 +        if function.search (typename):
374 +            return fake_pretty_printers_dict[function] (val)
375 +        
376 +    # Cannot find a pretty printer.  Return None.
377 +    return None
378 +
379 +def build_libfakecxx_dictionary ():
380 +    fake_pretty_printers_dict[vector_regex] = lambda val: FakeVectorPrinter(vector_sig, val)
381 +
382 +fake_pretty_printers_dict = {}
383 +
384 +build_libfakecxx_dictionary ()
385 Index: gdb-7.2.50.20110218/gdb/valprint.c
386 ===================================================================
387 --- gdb-7.2.50.20110218.orig/gdb/valprint.c     2011-02-18 10:44:16.000000000 +0100
388 +++ gdb-7.2.50.20110218/gdb/valprint.c  2011-02-18 10:44:32.000000000 +0100
389 @@ -85,7 +85,8 @@ struct value_print_options user_print_op
390    1,                           /* static_field_print */
391    1,                           /* pascal_static_field_print */
392    0,                           /* raw */
393 -  0                            /* summary */
394 +  0,                           /* summary */
395 +  0                            /* prettyprint_matrix */
396  };
397  
398  /* Initialize *OPTS to be a copy of the user print options.  */
This page took 0.06012 seconds and 3 git commands to generate.