]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind/Tool.pm
Tools can only be run in one session at a time by default
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind / Tool.pm
1 package Test::Valgrind::Tool;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Test::Valgrind::Tool - Base class for Test::Valgrind tools.
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 is the base for L<Test::Valgrind> tools.
21
22 They wrap around C<valgrind> tools by parsing its output and sending reports to the parent session whenever an error occurs.
23 They are expected to function both in suppressions generation and in analysis mode.
24
25 =cut
26
27 use base qw/Test::Valgrind::Carp/;
28
29 =head1 METHODS
30
31 =head2 C<requires_version>
32
33 The minimum C<valgrind> version needed to run this tool.
34 Defaults to C<3.1.0>.
35
36 =cut
37
38 sub requires_version { '3.1.0' }
39
40 =head2 C<< new tool => $tool >>
41
42 Creates a new tool object of type C<$tool> by requiring and redispatching the method call to the module named C<$tool> if it contains C<'::'> or to C<Test::Valgrind::Tool::$tool> otherwise.
43 The class represented by C<$tool> must inherit this class.
44
45 =cut
46
47 sub new {
48  my $class = shift;
49  $class = ref($class) || $class;
50
51  my %args = @_;
52
53  if ($class eq __PACKAGE__) {
54   my $tool = delete $args{tool} || 'memcheck';
55   $tool =~ s/[^\w:]//g;
56   $tool = __PACKAGE__ . "::$tool" if $tool !~ /::/;
57   $class->_croak("Couldn't load tool $tool: $@") unless eval "require $tool; 1";
58   return $tool->new(%args);
59  }
60
61  my $self = bless { }, $class;
62
63  $self->started(undef);
64
65  $self;
66 }
67
68 =head2 C<new_trainer>
69
70 Creates a new tool object suitable for generating suppressions.
71
72 Defaults to return C<undef>, which skips suppression generation.
73
74 =cut
75
76 sub new_trainer { }
77
78 =head2 C<report_class $session>
79
80 Wraps around either L</report_class_suppressions> or L</report_class_analysis> depending on the running mode of the C<$session>.
81
82 =cut
83
84 sub report_class {
85  my ($self, $sess) = @_;
86
87  if ($sess->do_suppressions) {
88   $self->report_class_suppressions($sess);
89  } else {
90   $self->report_class_analysis($sess);
91  }
92 }
93
94 =head2 C<report_class_suppressions $session>
95
96 Returns the class in which suppression reports generated by this tool will be blessed.
97
98 This method must be implemented when subclassing.
99
100 =cut
101
102 sub report_class_suppression;
103
104 =head2 C<report_class_analysis $session>
105
106 Returns the class in which error reports generated by this tool will be blessed.
107
108 This method must be implemented when subclassing.
109
110 =cut
111
112 sub report_class_analysis;
113
114 =head2 C<args $session>
115
116 Returns the list of tool-specific arguments that are to be passed to C<valgrind>.
117 All the suppression arguments are already handled by the session.
118
119 Defaults to the empty list.
120
121 =cut
122
123 sub args { }
124
125 =head2 C<suppressions_tag $session>
126
127 Returns a identifier that will be used to pick up the right suppressions for running the tool, or C<undef> to indicate that no special suppressions are needed.
128
129 This method must be implemented when subclassing.
130
131 =cut
132
133 sub suppressions_tag;
134
135 =head2 C<started>
136
137 Specifies whether the tool is running (C<1>), stopped (C<0>) or was never started (C<undef>).
138
139 =cut
140
141 sub started { @_ <= 1 ? $_[0]->{started} : ($_[0]->{started} = $_[1]) }
142
143 =head2 C<start $session>
144
145 Called when the C<$session> starts.
146
147 Defaults to set L</started>.
148
149 =cut
150
151 sub start {
152  my ($self) = @_;
153
154  $self->_croak('Tool already started') if $self->started;
155  $self->started(1);
156
157  return;
158 }
159
160 =head2 C<parse $session, $fh>
161
162 Wraps around either L</parse_suppressions> or L</parse_analysis> depending on the running mode of the C<$session>.
163 Croaks if the tool isn't started.
164
165 =cut
166
167 sub parse {
168  my ($self, $sess, $fh) = @_;
169
170  $self->_croak('Tool isn\'t started') unless $self->started;
171
172  if ($sess->do_suppressions) {
173   $self->parse_suppressions($sess, $fh);
174  } else {
175   $self->parse_analysis($sess, $fh);
176  }
177 }
178
179 =head2 C<parse_suppressions $sesssion, $fh>
180
181 Parse the suppression reports that the C<valgrind> process attached to the session C<$session> send through the filehandle C<$fh>.
182
183 This method must be implemented when subclassing.
184
185 =cut
186
187 sub parse_suppressions;
188
189 =head2 C<parse_analysis $sesssion, $fh>
190
191 Parse the error reports sent by the C<valgrind> process attached to the session C<$session> through the filehandle C<$fh>.
192
193 This method must be implemented when subclassing.
194
195 =cut
196
197 sub parse_analysis;
198
199 =head2 C<finish $session>
200
201 Called when the C<$session> finishes.
202
203 Defaults to clear L</started>.
204
205 =cut
206
207 sub finish {
208  my ($self) = @_;
209
210  return unless $self->started;
211  $self->started(0);
212
213  return;
214 }
215
216 =head1 SEE ALSO
217
218 L<Test::Valgrind>, L<Test::Valgrind::Session>.
219
220 =head1 AUTHOR
221
222 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
223
224 You can contact me by mail or on C<irc.perl.org> (vincent).
225
226 =head1 BUGS
227
228 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>.
229 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
230
231 =head1 SUPPORT
232
233 You can find documentation for this module with the perldoc command.
234
235     perldoc Test::Valgrind::Tool
236
237 =head1 COPYRIGHT & LICENSE
238
239 Copyright 2009 Vincent Pit, all rights reserved.
240
241 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
242
243 =cut
244
245 1; # End of Test::Valgrind::Tool