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 'string' display hint


I haven't liked how std::string is displayed "naked" -- no quotes, no
escaping.

This patch fixes the problem by adding a "string" display hint, which
gdb then uses to quote the string.  This latter part was very easy due
to Thiago's addition of the language printstr method.

I am still considering whether this treatment ought to be done for MI.
I'm leaning toward yes, but I haven't done it yet.

FWIW I'm also planning to add an "array" hint.  The CLI will use this
to omit indexes from the child output.

These are both items that came from Graydon's suggestions.  I still
haven't looked into the "brief" formatting thing.

Tom

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

	* python/lib/gdb/libstdcxx/v6/printers.py
	(StdStringPrinter.display_hint): New method.
	* python/python.c (apply_val_pretty_printer): Compute printer's
	hint.
	(print_string_repr): Add "hint" argument.  Recognize "string"
	hint.
	(print_children): Add "hint" argument.

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

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

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index ea6698c..fd17158 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -18603,9 +18603,17 @@ Some display hints are predefined by @value{GDBN}:
 
 @table @samp
 @item map
-Indicate to the MI consumer that the object being printed is
-``map-like'', and that the children of this value can be assumed to
-alternate between keys and values.
+Indicate that the object being printed is ``map-like'', and that the
+children of this value can be assumed to alternate between keys and
+values.
+
+@item string
+Indicate that the object being printed is ``string-like''.  If the
+printer's @code{to_string} method returns a Python string of some
+kind, then @value{GDBN} will call its internal language-specific
+string-printing function to format the string.  For the CLI this means
+adding quotation marks, possibly escaping some characters, respecting
+@code{set print elements}, and the like.
 @end table
 @end defop
 
diff --git a/gdb/python/lib/gdb/libstdcxx/v6/printers.py b/gdb/python/lib/gdb/libstdcxx/v6/printers.py
index a7964c5..1ea4dd2 100644
--- a/gdb/python/lib/gdb/libstdcxx/v6/printers.py
+++ b/gdb/python/lib/gdb/libstdcxx/v6/printers.py
@@ -393,6 +393,9 @@ class StdStringPrinter:
             encoding = encoding.value
         return self.val['_M_dataplus']['_M_p'].string(encoding)
 
+    def display_hint (self):
+        return 'string'
+
 class Tr1HashtableIterator:
     def __init__ (self, hash):
         self.count = 0
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 1432e5e..c2d4897 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -991,7 +991,8 @@ gdbpy_get_display_hint (PyObject *printer)
 /* Helper for apply_val_pretty_printer which calls to_string and
    formats the result.  */
 static void
-print_string_repr (PyObject *printer, struct ui_file *stream, int recurse,
+print_string_repr (PyObject *printer, const char *hint,
+		   struct ui_file *stream, int recurse,
 		   const struct value_print_options *options,
 		   const struct language_defn *language)
 {
@@ -1001,7 +1002,11 @@ print_string_repr (PyObject *printer, struct ui_file *stream, int recurse,
   output = pretty_print_one_value (printer, &replacement);
   if (output)
     {
-      fputs_filtered (output, stream);
+      if (hint && !strcmp (hint, "string"))
+	LA_PRINT_STRING (stream, (gdb_byte *) output, strlen (output),
+			 1, 0, options);
+      else
+	fputs_filtered (output, stream);
       xfree (output);
     }
   else if (replacement)
@@ -1013,12 +1018,12 @@ print_string_repr (PyObject *printer, struct ui_file *stream, int recurse,
 /* Helper for apply_val_pretty_printer that formats children of the
    printer, if any exist.  */
 static void
-print_children (PyObject *printer, struct ui_file *stream, int recurse,
+print_children (PyObject *printer, const char *hint,
+		struct ui_file *stream, int recurse,
 		const struct value_print_options *options,
 		const struct language_defn *language)
 {
-  char *hint;
-  int is_map = 0, i;
+  int is_map, i;
   PyObject *children, *iter;
   struct cleanup *cleanups;
 
@@ -1026,12 +1031,7 @@ print_children (PyObject *printer, struct ui_file *stream, int recurse,
     return;
 
   /* If we are printing a map, we want some special formatting.  */
-  hint = gdbpy_get_display_hint (printer);
-  if (hint)
-    {
-      is_map = ! strcmp (hint, "map");
-      xfree (hint);
-    }
+  is_map = hint && ! strcmp (hint, "map");
 
   children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
 					 NULL);
@@ -1135,7 +1135,7 @@ apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
 {
   PyObject *func, *printer;
   struct value *value;
-  char *hint;
+  char *hint = NULL;
   struct cleanup *cleanups;
   int result = 0;
   PyGILState_STATE state;
@@ -1160,11 +1160,15 @@ apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
       goto done;
     }
 
+  /* If we are printing a map, we want some special formatting.  */
+  hint = gdbpy_get_display_hint (printer);
+  make_cleanup (free_current_contents, &hint);
+
   make_cleanup_py_decref (printer);
   if (printer != Py_None)
     {
-      print_string_repr (printer, stream, recurse, options, language);
-      print_children (printer, stream, recurse, options, language);
+      print_string_repr (printer, hint, stream, recurse, options, language);
+      print_children (printer, hint, stream, recurse, options, language);
 
       result = 1;
     }


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