From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21042 invoked by alias); 31 Dec 2009 15:00:29 -0000 Mailing-List: contact archer-help@sourceware.org; run by ezmlm Sender: Precedence: bulk List-Post: List-Help: List-Subscribe: List-Id: Received: (qmail 21028 invoked by uid 22791); 31 Dec 2009 15:00:27 -0000 X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=BAYES_00,SPF_PASS X-Spam-Check-By: sourceware.org From: Matt McCormick To: archer@sourceware.org Cc: Matt McCormick Subject: [PATCH 3/3] [python] Create pretty-printer lookup_function classes. Date: Thu, 31 Dec 2009 15:00:00 -0000 Message-Id: <1262271582-23248-3-git-send-email-matt@mmmccormick.com> In-Reply-To: <1262271582-23248-2-git-send-email-matt@mmmccormick.com> References: <1262271582-23248-1-git-send-email-matt@mmmccormick.com> <1262271582-23248-2-git-send-email-matt@mmmccormick.com> X-SW-Source: 2009-q4/txt/msg00141.txt.bz2 These prevent repeated code and are extensible. gdb/ChangeLog 2009-31-12 Matt McCormick * Makefile.in: Install gdb/pretty/lookup_function.py. * python/lib/gdb/pretty/lookup_function.py) (RELookupFunction, RELookupFunctionTag): New lookup_function classes. gdb/testsuite/ChangeLog 2009-31-12 Matt McCormick * testsuite/gdb.python/py-prettyprint.py (lookup_function): Use RELookupFunctionTag to create the lookup_function. --- gdb/Makefile.in | 2 +- gdb/python/lib/gdb/pretty/lookup_function.py | 74 ++++++++++++++++++++++++++ gdb/testsuite/gdb.python/py-prettyprint.py | 35 +----------- 3 files changed, 78 insertions(+), 33 deletions(-) create mode 100644 gdb/python/lib/gdb/pretty/lookup_function.py diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 80b4dbe..15afc12 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -2021,7 +2021,7 @@ PY_FILES = gdb/FrameIterator.py gdb/FrameWrapper.py gdb/command/alias.py \ gdb/command/ignore_errors.py gdb/command/save_breakpoints.py \ gdb/function/caller_is.py gdb/function/in_scope.py \ gdb/function/__init__.py gdb/pretty/__init__.py \ - gdb/backtrace.py gdb/__init__.py + gdb/pretty/lookup_function.py gdb/backtrace.py gdb/__init__.py # Install the Python library. Python library files go under # $(pythondir). diff --git a/gdb/python/lib/gdb/pretty/lookup_function.py b/gdb/python/lib/gdb/pretty/lookup_function.py new file mode 100644 index 0000000..3883f8d --- /dev/null +++ b/gdb/python/lib/gdb/pretty/lookup_function.py @@ -0,0 +1,74 @@ +# Pretty-printer lookup function classes. + +# Copyright (C) 2009 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import gdb + + +class RELookupFunction (object): + """Returns a pretty-printer based on some type filtering followed by a search + in a dictionary of key:value of typename_regex:pretty-printer.""" + + def __init__ (self, pretty_printers_dict): + self.pretty_printers_dict = pretty_printers_dict + + + def _filter_types (self, gdb_type): + """Filters the given type and returns a name to pass to the + pretty_printers_dict or None.""" +# If it points to a reference, get the reference. + if gdb_type.code == gdb.TYPE_CODE_REF: + gdb_type = gdb_type.target () + +# Get the unqualified type, stripped of typedefs. + gdb_type = gdb_type.unqualified ().strip_typedefs () + + return str (gdb_type) + + + def __call__ (self, val): + "Returns a pretty-printer that can print val or None." + typename = self._filter_types (val.type) + if typename == None: + return None + + for regex in self.pretty_printers_dict: + if regex.match (typename): + return self.pretty_printers_dict[regex] (val) + + return None + + +class RELookupFunctionTag (RELookupFunction): + """Similar to RELookupFunction, but when trying to match to type.tag. + Useful for ``struct``, ``union``, or ``enum`` in C or C++.""" + + def __init__ (self, pretty_printers_dict): + super (RELookupFunctionTag, self).__init__ (pretty_printers_dict) + + def _filter_types (self, gdb_type): + """Filters the given type and returns a name to pass to the + pretty_printers_dict or None.""" +# If it points to a reference, get the reference. + if gdb_type.code == gdb.TYPE_CODE_REF: + gdb_type = gdb_type.target () + +# Get the unqualified type, stripped of typedefs. + gdb_type = gdb_type.unqualified ().strip_typedefs() + + return str (gdb_type.tag) + + diff --git a/gdb/testsuite/gdb.python/py-prettyprint.py b/gdb/testsuite/gdb.python/py-prettyprint.py index 2f070d8..efe242c 100644 --- a/gdb/testsuite/gdb.python/py-prettyprint.py +++ b/gdb/testsuite/gdb.python/py-prettyprint.py @@ -18,6 +18,8 @@ import re +from gdb.pretty.lookup_function import RELookupFunctionTag + # Test returning a Value from a printer. class string_print: def __init__(self, val): @@ -125,37 +127,6 @@ class pp_outer: yield 's', self.val['s'] yield 'x', self.val['x'] -def lookup_function (val): - "Look-up and return a pretty-printer that can print val." - - # Get the type. - type = val.type - - # If it points to a reference, get the reference. - if type.code == gdb.TYPE_CODE_REF: - type = type.target () - - # Get the unqualified type, stripped of typedefs. - type = type.unqualified ().strip_typedefs () - - # Get the type name. - typename = type.tag - - if typename == None: - return None - - # 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 pretty_printers_dict: - if function.match (typename): - return pretty_printers_dict[function] (val) - - # 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 @@ -188,6 +159,6 @@ def register_pretty_printers (): pretty_printers_dict[re.compile ('^outerstruct$')] = pp_outer pretty_printers_dict = {} - register_pretty_printers () +lookup_function = RELookupFunctionTag (pretty_printers_dict) gdb.pretty_printers.append (lookup_function) -- 1.6.6