]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Parser/Suppressions/Text.pm
Get rid of all pointers to Test::Valgrind::Tool::SuppressionsParser
[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.02
13
14 =cut
15
16 our $VERSION = '1.02';
17
18 =head1 DESCRIPTION
19
20 This class provides a default C<parse_suppressions> method, so that real tools for which suppressions are meaningful can exploit it by inheriting.
21
22 It's not meant to be used directly as a tool.
23
24 =cut
25
26 use base qw/Test::Valgrind::Carp/;
27
28 =head1 METHODS
29
30 =head2 C<new>
31
32 Just a croaking stub to remind you not to use this class as a real tool.
33
34 If your tool both inherit from this class and from C<Test::Valgrind::Tool>, and that you want to dispatch the call to your C<new> to its ancestors', be careful with C<SUPER> which may end up calling this dieing version of C<new>.
35 The solution is to either put C<Test::Valgrind::Tool> first in the C<@ISA> list or to explicitely call C<Test::Valgrind::Tool::new> instead of C<SUPER::new>.
36
37 =cut
38
39 sub new { shift->_croak('This mock tool isn\'t meant to be used directly') }
40
41 =head2 C<report_class>
42
43 Generated reports are C<Test::Valgrind::Report::Suppressions> objects.
44 Their C<data> member contains the raw text of the suppression.
45
46 =cut
47
48 sub report_class { 'Test::Valgrind::Report::Suppressions' }
49
50 =head2 C<parse $session, $fh>
51
52 Parses the filehandle C<$fh> fed with the output of F<valgrind --gen-suppressions=all> and sends a report to the session C<$session> for each suppression.
53
54 =cut
55
56 sub parse {
57  my ($self, $sess, $fh) = @_;
58
59  my ($s, $in) = ('', 0);
60  my @supps;
61
62  while (<$fh>) {
63   s/^\s*#\s//;        # Strip comments
64
65   next if /^==/;      # Valgrind info line
66   next if /valgrind/; # and /\Q$file\E/;
67
68   s/^\s*//;           # Strip leading spaces
69   s/<[^>]+>//;        # Strip tags
70   s/\s*$//;           # Strip trailing spaces
71   next unless length;
72
73   if ($_ eq '{') {      # A suppression block begins
74    $in = 1;
75   } elsif ($_ eq '}') { # A suppression block ends
76    # With valgrind 3.4.0, we can replace unknown series of frames by '...'
77    if ($sess->version ge '3.4.0') {
78     my $unknown_tail;
79     ++$unknown_tail while $s =~ s/(\n)\s*obj:\*\s*$/$1/;
80     $s .= "...\n" if $unknown_tail;
81    }
82
83    push @supps, $s;     # Add the suppression that just ended to the list
84    $s  = '';            # Reset the state
85    $in = 0;
86   } elsif ($in) {       # We're inside a suppresion block
87    $s .= "$_\n";        # Append the current line to the state
88   }
89  }
90
91  my @extra;
92
93  for (@supps) {
94   if (/\bfun:(m|c|re)alloc\b/) {
95    my $t = $1;
96
97    my %call; # Frames to append (if the value is 1) or to prepend (if it's 0)
98    if ($t eq 'm') {       # malloc can also be called by calloc or realloc
99     $call{$_} = 1 for qw/calloc realloc/;
100    } elsif ($t eq 're') { # realloc can also call malloc or free
101     $call{$_} = 0 for qw/malloc free/;
102    } elsif ($t eq 'c') {  # calloc can also call malloc
103     $call{$_} = 0 for qw/malloc/;
104    }
105
106    my $c = $_;
107    for (keys %call) {
108     my $d = $c;
109     $d =~ s/\b(fun:${t}alloc)\b/$call{$_} ? "$1\nfun:$_" : "fun:$_\n$1"/e;
110     # Remove one line for each line added or valgrind will hate us
111     $d =~ s/\n(.+?)\s*$/\n/;
112     push @extra, $d;
113    }
114   }
115  }
116
117  my $num;
118  $sess->report($self->report_class($sess)->new(
119   id   => ++$num,
120   kind => 'Suppression',
121   data => $_,
122  )) for @supps, @extra;
123 }
124
125 =head1 SEE ALSO
126
127 L<Test::Valgrind>, L<Test::Valgrind::Tool>.
128
129 =head1 AUTHOR
130
131 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
132
133 You can contact me by mail or on C<irc.perl.org> (vincent).
134
135 =head1 BUGS
136
137 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>.
138 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
139
140 =head1 SUPPORT
141
142 You can find documentation for this module with the perldoc command.
143
144     perldoc Test::Valgrind::Parser::Suppressions::Text
145
146 =head1 COPYRIGHT & LICENSE
147
148 Copyright 2009 Vincent Pit, all rights reserved.
149
150 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
151
152 =cut
153
154 # End of Test::Valgrind::Parser::Suppressions::Text
155
156 package Test::Valgrind::Report::Suppressions;
157
158 use base qw/Test::Valgrind::Report/;
159
160 sub kinds { shift->SUPER::kinds(), 'Suppression' }
161
162 sub valid_kind {
163  my ($self, $kind) = @_;
164
165  $self->SUPER::valid_kind($kind) or $kind eq 'Suppression'
166 }
167
168 1; # End of Test::Valgrind::Report::Suppressions