Fix type casts losing typedefs and reimplement "whatis" typedef stripping

Message ID d727c2c0-9e19-9e7e-c1c0-38b3345f0499@redhat.com
State New, archived
Headers

Commit Message

Pedro Alves Aug. 16, 2017, 8:11 p.m. UTC
  On 06/30/2017 04:48 PM, Pedro Alves wrote:
> This prevents a type printer for "int_t" kicking in, with e.g.:

I've finally written a pretty printer test that actually tests
this.

Phil, WDYT?

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* gdb.python/py-prettyprint.c (int_type, int_type2): New typedefs.
	(an_int, an_int_type, an_int_type2): New globals.
	* gdb.python/py-prettyprint.exp (run_lang_tests): Add tests
	involving typedefs and cast expressions.
	* gdb.python/py-prettyprint.py (class pp_int_type): New.
	(lookup_typedefs_function): New.
	(typedefs_pretty_printers_dict): New.
	(top level): Register lookup_typedefs_function in gdb.pretty_printers.
---

 gdb/testsuite/gdb.python/py-prettyprint.c   |    9 ++++++
 gdb/testsuite/gdb.python/py-prettyprint.exp |   13 +++++++++
 gdb/testsuite/gdb.python/py-prettyprint.py  |   40 +++++++++++++++++++++++++++
 3 files changed, 62 insertions(+)
  

Patch

diff --git a/gdb/testsuite/gdb.python/py-prettyprint.c b/gdb/testsuite/gdb.python/py-prettyprint.c
index fd58358..82f9fe7 100644
--- a/gdb/testsuite/gdb.python/py-prettyprint.c
+++ b/gdb/testsuite/gdb.python/py-prettyprint.c
@@ -257,6 +257,15 @@  bug_14741()
   set_item(&c, 0, 5);
 }
 
+/* Some typedefs/variables for checking that GDB doesn't lose typedefs
+   when looking for a printer.  */
+typedef int int_type;
+typedef int_type int_type2;
+
+int an_int = -1;
+int_type an_int_type = 1;
+int_type2 an_int_type2 = 2;
+
 int
 main ()
 {
diff --git a/gdb/testsuite/gdb.python/py-prettyprint.exp b/gdb/testsuite/gdb.python/py-prettyprint.exp
index b0a9e32..9c52e9c 100644
--- a/gdb/testsuite/gdb.python/py-prettyprint.exp
+++ b/gdb/testsuite/gdb.python/py-prettyprint.exp
@@ -110,6 +110,19 @@  proc run_lang_tests {exefile lang} {
     gdb_test "print nstype" " = {.0. = 7, .1. = 42}" \
 	"print nstype on one line"
 
+    # Check that GDB doesn't lose typedefs when looking for a printer.
+    gdb_test "print an_int" " = -1"
+    gdb_test "print (int) an_int" " = -1"
+    gdb_test "print (int_type) an_int" " = an int_type, val=-1"
+
+    gdb_test "print an_int_type" " = an int_type, val=1"
+    gdb_test "print (int_type) an_int_type" " = an int_type, val=1"
+
+    gdb_test "print an_int_type2" " = an int_type, val=2"
+    gdb_test "print (int) an_int_type2" " = 2"
+    gdb_test "print (int_type) an_int_type2" " = an int_type, val=2"
+    gdb_test "print (int_type2) an_int_type2" " = an int_type, val=2"
+
     gdb_continue_to_end
 }
 
diff --git a/gdb/testsuite/gdb.python/py-prettyprint.py b/gdb/testsuite/gdb.python/py-prettyprint.py
index c56f564..96ad0f9 100644
--- a/gdb/testsuite/gdb.python/py-prettyprint.py
+++ b/gdb/testsuite/gdb.python/py-prettyprint.py
@@ -227,6 +227,13 @@  class pp_eval_type (object):
         gdb.execute("bt", to_string=True)
         return "eval=<" + str(gdb.parse_and_eval("eval_func (123456789, 2, 3, 4, 5, 6, 7, 8)")) + ">"
 
+class pp_int_type (object):
+    def __init__(self, val):
+        self.val = val
+
+    def to_string(self):
+        return "an int_type, val=%s" % int(self.val)
+
 def lookup_function (val):
     "Look-up and return a pretty-printer that can print val."
 
@@ -263,6 +270,33 @@  def disable_lookup_function ():
 def enable_lookup_function ():
     lookup_function.enabled = True
 
+# Lookup a printer for VAL in the typedefs dict.
+def lookup_typedefs_function (val):
+    "Look-up and return a pretty-printer that can print val (typedefs)."
+
+    # Get the type.
+    type = val.type
+
+    # Get the unqualified type.
+    type = type.unqualified ()
+
+    # Iterate over local dictionary of types to determine if a printer
+    # is registered for that type.  Return an instantiation of the
+    # printer if found.
+    for function in typedefs_pretty_printers_dict:
+        # Compare the type name, stripping typedefs, one layer at a
+        # time.
+        while type != None:
+            if type.name != None and function.match (type.name):
+                return typedefs_pretty_printers_dict[function] (val)
+            if type.code != gdb.TYPE_CODE_TYPEDEF:
+                break
+            type = type.target ()
+
+    # Cannot find a pretty printer.  Return None.
+
+    return None
+
 def register_pretty_printers ():
     pretty_printers_dict[re.compile ('^struct s$')]   = pp_s
     pretty_printers_dict[re.compile ('^s$')]   = pp_s
@@ -309,7 +343,13 @@  def register_pretty_printers ():
 
     pretty_printers_dict[re.compile ('^eval_type_s$')] = pp_eval_type
 
+    typedefs_pretty_printers_dict[re.compile ('^int_type$')] = pp_int_type
+
+# Dict for struct types with typedefs fully stripped.
 pretty_printers_dict = {}
+# Dict for typedef types.
+typedefs_pretty_printers_dict = {}
 
 register_pretty_printers ()
 gdb.pretty_printers.append (lookup_function)
+gdb.pretty_printers.append (lookup_typedefs_function)