[RFC][PATCH v2 1/2] libiberty: implement option to drop templates during demangling

Mikołaj Piróg mikolajpirog@gmail.com
Fri Mar 8 18:08:44 GMT 2024


This patch introduces option that when given to cplus_demangle, drops
template from a symbol during demangling (in printing stage)

diff --git a/include/demangle.h b/include/demangle.h
index 49b84d4de88..89a3b64e436 100644
--- a/include/demangle.h
+++ b/include/demangle.h
@@ -51,6 +51,8 @@ extern "C" {
    types, even if present.  It applies
    only to the toplevel function type.
    */
+#define DMGL_TMPL_DROP   (1 << 7)       /* Supress printing of templates,
even
+   if present. */

 #define DMGL_AUTO (1 << 8)
 #define DMGL_GNU_V3 (1 << 14)
diff --git a/libiberty/cp-demangle.c b/libiberty/cp-demangle.c
index fc2cf64e6e0..ad873ed6177 100644
--- a/libiberty/cp-demangle.c
+++ b/libiberty/cp-demangle.c
@@ -5315,6 +5315,9 @@ d_print_comp_inner (struct d_print_info *dpi, int
options,
         else
           {
     d_print_comp (dpi, options, dcl);
+    if ((options & DMGL_TMPL_DROP) != 0)
+      return;
+
     if (d_last_char (dpi) == '<')
       d_append_char (dpi, ' ');
     d_append_char (dpi, '<');
@@ -5333,6 +5336,9 @@ d_print_comp_inner (struct d_print_info *dpi, int
options,
       }

     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
+      if ((options & DMGL_TMPL_DROP) != 0)
+ break;
+
       if (dpi->lambda_tpl_parms > dc->u.s_number.number + 1)
  {
   const struct demangle_component *a
@@ -5492,6 +5498,23 @@ d_print_comp_inner (struct d_print_info *dpi, int
options,
       return;

     case DEMANGLE_COMPONENT_SUB_STD:
+      /* Special-case substitions are stored wholly as a string. They need
+ to be stripped of templates manually */
+      if ((options & DMGL_TMPL_DROP) != 0)
+      {
+ char *template_begin = strchr(dc->u.s_string.string, '<');
+ if (template_begin != NULL)
+ {
+  d_append_buffer(dpi, dc->u.s_string.string, (template_begin -
dc->u.s_string.string));
+
+  char *template_end= strrchr(dc->u.s_string.string, '>');
+  if (template_end == NULL)
+    return;
+
+  d_append_buffer(dpi, template_end, dc->u.s_string.len - (template_end -
dc->u.s_string.string) - 1);
+  return;
+ }
+      }
       d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
       return;



pt., 8 mar 2024 o 19:04 Mikołaj Piróg <mikolajpirog@gmail.com> napisał(a):

> This patch refines my previous attempt of implementing short name search (
> https://sourceware.org/pipermail/binutils/2024-February/132713.html). The
> idea is to enable such usage:
>
> objdump --disassemble=ns::foo -C file
>
> The objdump will then output all symbols matching the name "ns::foo".
> I introduced new demangling option to remove template qualification, so for
> example, "--dissassemble=std::vector::push_back" will match all instances
> of push_back in the file.
>
> The motivation behind this usage is to ease a process of a quick
> disassembly of a binary in search for a particular function. Providing a
> function signature isn't comfortable.
>
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/binutils/attachments/20240308/950dd17f/attachment.htm>


More information about the Binutils mailing list