]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Parser/Suppressions/Text.pm
5beae17409a53666f542943a19b8167c15b58984
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind / Parser / Suppressions / Text.pm
1 package Test::Valgrind::Parser::Suppressions::Text;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Test::Valgrind::Parser::Suppressions::Text - Parse valgrind suppressions output as text blocks.
9
10 =head1 VERSION
11
12 Version 1.18
13
14 =cut
15
16 our $VERSION = '1.18';
17
18 =head1 DESCRIPTION
19
20 This is a L<Test::Valgrind::Parser::Text> object that can extract suppressions from C<valgrind>'s text output.
21
22 =cut
23
24 use Test::Valgrind::Suppressions;
25
26 use base qw<Test::Valgrind::Parser::Text Test::Valgrind::Carp>;
27
28 =head1 METHODS
29
30 =head2 C<report_class>
31
32 Generated reports are C<Test::Valgrind::Report::Suppressions> objects.
33 Their C<data> member contains the raw text of the suppression.
34
35 =cut
36
37 sub report_class { 'Test::Valgrind::Report::Suppressions' }
38
39 sub parse {
40  my ($self, $sess, $fh) = @_;
41
42  my ($s, $in) = ('', 0);
43  my @supps;
44
45  while (<$fh>) {
46   s/^\s*#\s//;        # Strip comments
47
48   if (/^==/) {        # Valgrind info line
49    if (/Signal 11 being dropped from thread/) {
50     # This might loop endlessly
51     return 1;
52    }
53    next;
54   }
55
56   s/^\s*//;           # Strip leading spaces
57   s/<[^>]+>//;        # Strip tags
58   s/\s*$//;           # Strip trailing spaces
59   next unless length;
60
61   if ($_ eq '{') {      # A suppression block begins
62    $in = 1;
63   } elsif ($_ eq '}') { # A suppression block ends
64    push @supps, $s;     # Add the suppression that just ended to the list
65    $s  = '';            # Reset the state
66    $in = 0;
67   } elsif ($in) {       # We're inside a suppresion block
68    if (/^fun\s*:\s*(.*)/) {
69     # Sometimes valgrind seems to forget to Z-demangle the symbol names.
70     # Make sure it's done and append the result to the state.
71     my $sym = $1;
72     $s .= 'fun:' . Test::Valgrind::Suppressions->maybe_z_demangle($sym) . "\n";
73    } else {
74     $s .= "$_\n";
75    }
76   }
77  }
78
79  my @extra;
80
81  for (@supps) {
82   if (/\bfun:(m|c|re)alloc\b/) {
83    my $t = $1;
84
85    my %call; # Frames to append (if the value is 1) or to prepend (if it's 0)
86    if ($t eq 'm') {       # malloc can also be called by calloc or realloc
87     $call{$_} = 1 for qw<calloc realloc>;
88    } elsif ($t eq 're') { # realloc can also call malloc or free
89     $call{$_} = 0 for qw<malloc free>;
90    } elsif ($t eq 'c') {  # calloc can also call malloc
91     $call{$_} = 0 for qw<malloc>;
92    }
93
94    my $c = $_;
95    for (keys %call) {
96     my $d = $c;
97     $d =~ s/\b(fun:${t}alloc)\b/$call{$_} ? "$1\nfun:$_" : "fun:$_\n$1"/e;
98     # Remove one line for each line added or valgrind will hate us
99     $d =~ s/\n(.+?)\s*$/\n/;
100     push @extra, $d;
101    }
102   }
103  }
104
105  my $num;
106  $sess->report($self->report_class($sess)->new(
107   id   => ++$num,
108   kind => 'Suppression',
109   data => $_,
110  )) for @supps, @extra;
111
112  return 0;
113 }
114
115 =head1 SEE ALSO
116
117 L<Test::Valgrind>, L<Test::Valgrind::Parser::Text>.
118
119 =head1 AUTHOR
120
121 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
122
123 You can contact me by mail or on C<irc.perl.org> (vincent).
124
125 =head1 BUGS
126
127 Please report any bugs or feature requests to C<bug-test-valgrind at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Valgrind>.
128 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
129
130 =head1 SUPPORT
131
132 You can find documentation for this module with the perldoc command.
133
134     perldoc Test::Valgrind::Parser::Suppressions::Text
135
136 =head1 COPYRIGHT & LICENSE
137
138 Copyright 2009,2010,2011,2013,2015,2016 Vincent Pit, all rights reserved.
139
140 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
141
142 =cut
143
144 # End of Test::Valgrind::Parser::Suppressions::Text
145
146 package Test::Valgrind::Report::Suppressions;
147
148 use base qw<Test::Valgrind::Report>;
149
150 sub kinds { shift->SUPER::kinds(), 'Suppression' }
151
152 sub valid_kind {
153  my ($self, $kind) = @_;
154
155  $self->SUPER::valid_kind($kind) or $kind eq 'Suppression'
156 }
157
158 1; # End of Test::Valgrind::Report::Suppressions