+=head2 C<maybe_generalize>
+
+ my $mangled_suppression = Test::Valgrind::Suppressions->maybe_generalize(
+ $session,
+ $suppression,
+ );
+
+Removes all wildcard frames at the end of the suppression.
+It also replaces sequences of wildcard frames by C<'...'> when C<valgrind> C<3.4.0> or higher is used.
+Returns the mangled suppression.
+
+=cut
+
+sub maybe_generalize {
+ shift;
+
+ my ($sess, $supp) = @_;
+
+ 1 while $supp =~ s/[^\r\n]*:\s*\*\s*$//;
+
+ # With valgrind 3.4.0, we can replace unknown series of frames by '...'
+ my $can_ellipsis = $sess->version >= '3.4.0';
+
+ my $did_length_check;
+
+ ELLIPSIS: {
+ if ($can_ellipsis) {
+ $supp .= "...\n";
+ $supp =~ s/(?:^\s*(?:\.{3}|\*:\S*|obj:\*)\s*(?:\n|\z))+/...\n/mg;
+ }
+
+ last if $did_length_check++;
+
+ my $frames_count =()= $supp =~ m/^(?:(?:obj|fun|\*):|\.{3}\s*$)/mg;
+ if ($frames_count > 24) {
+ # Keep only 24 frames, and even sacrifice one more if we can do ellipsis.
+ my $last = $can_ellipsis ? 23 : 24;
+ my $len = length $supp;
+ $supp =~ m/^(?:(?:obj|fun|\*):\S*|\.{3})\s*\n/mg for 1 .. $last;
+ my $p = pos $supp;
+ substr $supp, $p, $len - $p, '';
+ redo ELLIPSIS if $can_ellipsis;
+ }
+ }
+
+ $supp;
+}
+
+=head2 C<maybe_z_demangle>
+
+ my $demangled_symbol = Test::Valgrind::Suppressions->maybe_z_demangle(
+ $symbol,
+ );
+
+If C<$symbol> is Z-encoded as described in C<valgrind>'s F<include/pub_tool_redir.h>, extract and decode its function name part.
+Otherwise, C<$symbol> is returned as is.
+
+This routine follows C<valgrind>'s F<coregrind/m_demangle/demangle.c:maybe_Z_demangle>.
+
+=cut
+
+my %z_escapes = (
+ a => '*',
+ c => ':',
+ d => '.',
+ h => '-',
+ p => '+',
+ s => ' ',
+ u => '_',
+ A => '@',
+ D => '$',
+ L => '(',
+ R => ')',
+ Z => 'Z',
+);
+
+sub maybe_z_demangle {
+ my ($self, $sym) = @_;
+
+ $sym =~ s/^_vg[rwn]Z([ZU])_// or return $sym;
+
+ my $fn_is_encoded = $1 eq 'Z';
+
+ $sym =~ /^VG_Z_/ and $self->_croak('Symbol with a "VG_Z_" prefix is invalid');
+ $sym =~ s/^[^_]*_//
+ or $self->_croak('Symbol doesn\'t contain a function name');
+
+ if ($fn_is_encoded) {
+ $sym =~ s/Z(.)/
+ my $c = $z_escapes{$1};
+ $self->_croak('Invalid escape sequence') unless defined $c;
+ $c;
+ /ge;
+ }
+
+ $self->_croak('Empty symbol') unless length $sym;
+
+ return $sym;
+}
+