public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-4080] libstdc++: Fix gdb FilteringTypePrinter
@ 2022-11-16  5:55 Franथईois Dumont
  0 siblings, 0 replies; only message in thread
From: Franथईois Dumont @ 2022-11-16  5:55 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:2b7f0378b915a6a294b330bea00e50069f181bd7

commit r13-4080-g2b7f0378b915a6a294b330bea00e50069f181bd7
Author: François Dumont <fdumont@gcc.gnu.org>
Date:   Wed Oct 5 19:24:55 2022 +0200

    libstdc++: Fix gdb FilteringTypePrinter
    
    Once we found a matching FilteringTypePrinter instance we look for the associated
    typedef and check that the returned Python Type is equal to the Type to recognize.
    But gdb Python Type includes properties to distinguish a typedef from the actual
    type. So use gdb.types.get_basic_type to check if we are indeed on the same type.
    
    Additionnaly enhance FilteringTypePrinter matching mecanism by introducing targ1 that,
    if not None, will be used as the 1st template parameter.
    
    libstdc++-v3/ChangeLog:
    
            * python/libstdcxx/v6/printers.py (FilteringTypePrinter): Rename 'match' field
            'template'. Add self.targ1 to specify the first template parameter of the instantiation
            to match.
            (add_one_type_printer): Add targ1 optional parameter, default to None.
            Use gdb.types.get_basic_type to compare the type to recognize and the type
            returned from the typedef lookup.
            (register_type_printers): Adapt calls to add_one_type_printers.

Diff:
---
 libstdc++-v3/python/libstdcxx/v6/printers.py | 84 ++++++++++++++++------------
 1 file changed, 49 insertions(+), 35 deletions(-)

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 09dffad3c6f..eaaf5e817c0 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -2069,59 +2069,65 @@ class FilteringTypePrinter(object):
     A type printer that uses typedef names for common template specializations.
 
     Args:
-        match (str): The class template to recognize.
+        template (str): The class template to recognize.
         name (str): The typedef-name that will be used instead.
+        targ1 (str): The first template argument.
+            If arg1 is provided (not None), match only template specializations
+            with this type as the first template argument,
+            e.g. if template='basic_string<targ1'
 
-    Checks if a specialization of the class template 'match' is the same type
+    Checks if a specialization of the class template 'template' is the same type
     as the typedef 'name', and prints it as 'name' instead.
 
     e.g. if an instantiation of std::basic_istream<C, T> is the same type as
     std::istream then print it as std::istream.
     """
 
-    def __init__(self, match, name):
-        self.match = match
+    def __init__(self, template, name, targ1):
+        self.template = template
         self.name = name
+        self.targ1 = targ1
         self.enabled = True
 
     class _recognizer(object):
         "The recognizer class for FilteringTypePrinter."
 
-        def __init__(self, match, name):
-            self.match = match
+        def __init__(self, template, name, targ1):
+            self.template = template
             self.name = name
+            self.targ1 = targ1
             self.type_obj = None
 
         def recognize(self, type_obj):
             """
-            If type_obj starts with self.match and is the same type as
+            If type_obj starts with self.template and is the same type as
             self.name then return self.name, otherwise None.
             """
             if type_obj.tag is None:
                 return None
 
             if self.type_obj is None:
-                if not type_obj.tag.startswith(self.match):
+                if self.targ1 is not None:
+                    if not type_obj.tag.startswith('{}<{}'.format(self.template, self.targ1)):
+                        # Filter didn't match.
+                        return None
+                elif not type_obj.tag.startswith(self.template):
                     # Filter didn't match.
                     return None
+
                 try:
                     self.type_obj = gdb.lookup_type(self.name).strip_typedefs()
                 except:
                     pass
-            if self.type_obj == type_obj:
-                return strip_inline_namespaces(self.name)
 
             if self.type_obj is None:
                 return None
 
-            # Workaround ambiguous typedefs matching both std:: and std::__cxx11:: symbols.
-            ambiguous = False
-            for ch in ('', 'w', 'u8', 'u16', 'u32'):
-                if self.name == 'std::' + ch + 'string':
-                    ambiguous = True
-                    break
+            if gdb.types.get_basic_type(self.type_obj) == gdb.types.get_basic_type(type_obj):
+                return strip_inline_namespaces(self.name)
 
-            if ambiguous:
+            # Workaround ambiguous typedefs matching both std:: and std::__cxx11:: symbols.
+            if self.template.split('::')[-1] == 'basic_string':
                 if self.type_obj.tag.replace('__cxx11::', '') == type_obj.tag.replace('__cxx11::', ''):
                     return strip_inline_namespaces(self.name)
 
@@ -2129,14 +2135,14 @@ class FilteringTypePrinter(object):
 
     def instantiate(self):
         "Return a recognizer object for this type printer."
-        return self._recognizer(self.match, self.name)
+        return self._recognizer(self.template, self.name, self.targ1)
 
-def add_one_type_printer(obj, match, name):
-    printer = FilteringTypePrinter('std::' + match, 'std::' + name)
+def add_one_type_printer(obj, template, name, targ1 = None):
+    printer = FilteringTypePrinter('std::' + template, 'std::' + name, targ1)
     gdb.types.register_type_printer(obj, printer)
-    if _versioned_namespace and not '__cxx11' in match:
+    if _versioned_namespace and not '__cxx11' in template:
         ns = 'std::' + _versioned_namespace
-        printer = FilteringTypePrinter(ns + match, ns + name)
+        printer = FilteringTypePrinter(ns + template, ns + name, targ1)
         gdb.types.register_type_printer(obj, printer)
 
 def register_type_printers(obj):
@@ -2146,29 +2152,33 @@ def register_type_printers(obj):
         return
 
     # Add type printers for typedefs std::string, std::wstring etc.
-    for ch in ('', 'w', 'u8', 'u16', 'u32'):
-        add_one_type_printer(obj, 'basic_string', ch + 'string')
-        add_one_type_printer(obj, '__cxx11::basic_string', ch + 'string')
+    for ch in (('', 'char'),
+               ('w', 'wchar_t'),
+               ('u8', 'char8_t'),
+               ('u16', 'char16_t'),
+               ('u32', 'char32_t')):
+        add_one_type_printer(obj, 'basic_string', ch[0] + 'string', ch[1])
+        add_one_type_printer(obj, '__cxx11::basic_string', ch[0] + 'string', ch[1])
         # Typedefs for __cxx11::basic_string used to be in namespace __cxx11:
         add_one_type_printer(obj, '__cxx11::basic_string',
-                             '__cxx11::' + ch + 'string')
-        add_one_type_printer(obj, 'basic_string_view', ch + 'string_view')
+                             '__cxx11::' + ch[0] + 'string', ch[1])
+        add_one_type_printer(obj, 'basic_string_view', ch[0] + 'string_view', ch[1])
 
     # Add type printers for typedefs std::istream, std::wistream etc.
-    for ch in ('', 'w'):
+    for ch in (('', 'char'), ('w', 'wchar_t')):
         for x in ('ios', 'streambuf', 'istream', 'ostream', 'iostream',
                   'filebuf', 'ifstream', 'ofstream', 'fstream'):
-            add_one_type_printer(obj, 'basic_' + x, ch + x)
+            add_one_type_printer(obj, 'basic_' + x, ch[0] + x, ch[1])
         for x in ('stringbuf', 'istringstream', 'ostringstream',
                   'stringstream'):
-            add_one_type_printer(obj, 'basic_' + x, ch + x)
+            add_one_type_printer(obj, 'basic_' + x, ch[0] + x, ch[1])
             # <sstream> types are in __cxx11 namespace, but typedefs aren't:
-            add_one_type_printer(obj, '__cxx11::basic_' + x, ch + x)
+            add_one_type_printer(obj, '__cxx11::basic_' + x, ch[0] + x, ch[1])
 
     # Add type printers for typedefs regex, wregex, cmatch, wcmatch etc.
     for abi in ('', '__cxx11::'):
-        for ch in ('', 'w'):
-            add_one_type_printer(obj, abi + 'basic_regex', abi + ch + 'regex')
+        for ch in (('', 'char'), ('w', 'wchar_t')):
+            add_one_type_printer(obj, abi + 'basic_regex', abi + ch[0] + 'regex', ch[1])
         for ch in ('c', 's', 'wc', 'ws'):
             add_one_type_printer(obj, abi + 'match_results', abi + ch + 'match')
             for x in ('sub_match', 'regex_iterator', 'regex_token_iterator'):
@@ -2196,9 +2206,13 @@ def register_type_printers(obj):
 
     # Add type printers for experimental::basic_string_view typedefs.
     ns = 'experimental::fundamentals_v1::'
-    for ch in ('', 'w', 'u8', 'u16', 'u32'):
+    for ch in (('', 'char'),
+               ('w', 'wchar_t'),
+               ('u8', 'char8_t'),
+               ('u16', 'char16_t'),
+               ('u32', 'char32_t')):
         add_one_type_printer(obj, ns + 'basic_string_view',
-                             ns + ch + 'string_view')
+                             ns + ch[0] + 'string_view', ch[1])
 
     # Do not show defaulted template arguments in class templates.
     add_one_template_type_printer(obj, 'unique_ptr',

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-16  5:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-16  5:55 [gcc r13-4080] libstdc++: Fix gdb FilteringTypePrinter Franथईois Dumont

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).