File Coverage

File:/tmp/automake/lib/Automake/VarDef.pm
Coverage:96.9%

linestmtbrancondsubpodtimecode
1# Copyright (C) 2003, 2004, 2006, 2009 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2, or (at your option)
6# any later version.
7
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16package Automake::VarDef;
17
1159
1159
1159
3243
1129
3037
use strict;
18
1159
1159
1159
5016
1892
3149
use Carp;
19
1159
1159
1159
4469
1339
2895
use Automake::ChannelDefs;
20
1159
1159
1159
8065
1470
8826
use Automake::ItemDef;
21
22require Exporter;
23
1159
1159
1159
4288
1111
3380
use vars '@ISA', '@EXPORT';
24@ISA = qw/Automake::ItemDef Exporter/;
25@EXPORT = qw (&VAR_AUTOMAKE &VAR_CONFIGURE &VAR_MAKEFILE
26              &VAR_ASIS &VAR_PRETTY &VAR_SILENT &VAR_SORTED);
27
28 - 84
=head1 NAME

Automake::VarDef - a class for variable definitions

=head1 SYNOPSIS

  use Automake::VarDef;
  use Automake::Location;

  # Create a VarDef for a definition such as
  # | # any comment
  # | foo = bar # more comment
  # in Makefile.am
  my $loc = new Automake::Location 'Makefile.am:2';
  my $def = new Automake::VarDef ('foo', 'bar # more comment',
                                  '# any comment',
                                  $loc, '', VAR_MAKEFILE, VAR_ASIS);

  # Appending to a definition.
  $def->append ('value to append', 'comment to append');

  # Accessors.
  my $value    = $def->value;  # with trailing `#' comments and
                               # continuation ("\\\n") omitted.
  my $value    = $def->raw_value; # the real value, as passed to new().
  my $comment  = $def->comment;
  my $location = $def->location;
  my $type     = $def->type;
  my $owner    = $def->owner;
  my $pretty   = $def->pretty;

  # Changing owner.
  $def->set_owner (VAR_CONFIGURE,
                   new Automake::Location 'configure.ac:15');

  # Marking examined definitions.
  $def->set_seen;
  my $seen_p = $def->seen;

  # Printing a variable for debugging.
  print STDERR $def->dump;

=head1 DESCRIPTION

This class gathers data related to one Makefile-variable definition.

=head2 Constants

=over 4

=item C<VAR_AUTOMAKE>, C<VAR_CONFIGURE>, C<VAR_MAKEFILE>

Possible owners for variables.  A variable can be defined
by Automake, in F<configure.ac> (using C<AC_SUBST>), or in
the user's F<Makefile.am>.

=cut
85
86# Defined so that the owner of a variable can only be increased (e.g
87# Automake should not override a configure or Makefile variable).
88
1159
1159
1159
4551
1185
3380
use constant VAR_AUTOMAKE => 0; # Variable defined by Automake.
89
1159
1159
1159
4700
1366
2823
use constant VAR_CONFIGURE => 1;# Variable defined in configure.ac.
90
1159
1159
1159
4363
1150
2896
use constant VAR_MAKEFILE => 2; # Variable defined in Makefile.am.
91
92 - 104
=item C<VAR_ASIS>, C<VAR_PRETTY>, C<VAR_SILENT>, C<VAR_SORTED>

Possible print styles.  C<VAR_ASIS> variables should be output as-is.
C<VAR_PRETTY> variables are wrapped on multiple lines if they cannot
fit on one.  C<VAR_SILENT> variables are not output at all.  Finally,
C<VAR_SORTED> variables should be sorted and then handled as
C<VAR_PRETTY> variables.

C<VAR_SILENT> variables can also be overridden silently (unlike the
other kinds of variables whose overriding may sometimes produce
warnings).

=cut
105
106# Possible values for pretty.
107
1159
1159
1159
4376
1225
2845
use constant VAR_ASIS => 0; # Output as-is.
108
1159
1159
1159
4261
1144
2870
use constant VAR_PRETTY => 1; # Pretty printed on output.
109
1159
1159
1159
4339
1137
2994
use constant VAR_SILENT => 2; # Not output. (Can also be
110                                # overridden silently.)
111
1159
1159
1159
4221
1202
2955
use constant VAR_SORTED => 3; # Sorted and pretty-printed.
112
113=back
114
115 - 145
=head2 Methods

C<VarDef> defines the following methods in addition to those inherited
from L<Automake::ItemDef>.

=over 4

=item C<my $def = new Automake::VarDef ($varname, $value, $comment, $location, $type, $owner, $pretty)>

Create a new Makefile-variable definition.  C<$varname> is the name of
the variable being defined and C<$value> its value.

C<$comment> is any comment preceding the definition.  (Because
Automake reorders variable definitions in the output, it also tries to
carry comments around.)

C<$location> is the place where the definition occurred, it should be
an instance of L<Automake::Location>.

C<$type> should be C<''> for definitions made with C<=>, and C<':'>
for those made with C<:=>.

C<$owner> specifies who owns the variables, it can be one of
C<VAR_AUTOMAKE>, C<VAR_CONFIGURE>, or C<VAR_MAKEFILE> (see these
definitions).

Finally, C<$pretty> tells how the variable should be output, and can
be one of C<VAR_ASIS>, C<VAR_PRETTY>, or C<VAR_SILENT>, or
C<VAR_SORTED> (see these definitions).

=cut
146
147sub new ($$$$$$$$)
148{
149
181015
1
441331
  my ($class, $var, $value, $comment, $location, $type, $owner, $pretty) = @_;
150
151  # A user variable must be set by either `=' or `:=', and later
152  # promoted to `+='.
153
181015
737199
  if ($owner != VAR_AUTOMAKE && $type eq '+')
154    {
155
1
8
      error $location, "$var must be set with `=' before using `+='";
156    }
157
158
181015
429744
  my $self = Automake::ItemDef::new ($class, $comment, $location, $owner);
159
181015
332157
  $self->{'value'} = $value;
160
181015
293061
  $self->{'type'} = $type;
161
181015
271749
  $self->{'pretty'} = $pretty;
162
181015
250522
  $self->{'seen'} = 0;
163
181015
401248
  return $self;
164}
165
166 - 171
=item C<$def-E<gt>append ($value, $comment)>

Append C<$value> and <$comment> to the existing value and comment of
C<$def>.  This is normally called on C<+=> definitions.

=cut
172
173sub append ($$$)
174{
175
14300
1
29583
  my ($self, $value, $comment) = @_;
176
14300
25546
  $self->{'comment'} .= $comment;
177
178
14300
26409
  my $val = $self->{'value'};
179
180  # Strip comments from augmented variables. This is so that
181  # VAR = foo # com
182  # VAR += bar
183  # does not become
184  # VAR = foo # com bar
185  # Furthermore keeping `#' would not be portable if the variable is
186  # output on multiple lines.
187
14300
23026
  $val =~ s/ ?#.*//;
188  # Insert a separator, if required.
189
14300
29295
  $val .= ' ' if $val;
190
14300
33128
  $self->{'value'} = $val . $value;
191  # Turn ASIS appended variables into PRETTY variables. This is to
192  # cope with `make' implementation that cannot read very long lines.
193
14300
48287
  $self->{'pretty'} = VAR_PRETTY if $self->{'pretty'} == VAR_ASIS;
194}
195
196 - 207
=item C<$def-E<gt>value>

=item C<$def-E<gt>raw_value>

=item C<$def-E<gt>type>

=item C<$def-E<gt>pretty>

Accessors to the various constituents of a C<VarDef>.  See the
documentation of C<new>'s arguments for a description of these.

=cut
208
209sub value ($)
210{
211
11767
1
20697
  my ($self) = @_;
212
11767
21945
  my $val = $self->raw_value;
213  # Strip anything past `#'. `#' characters cannot be escaped
214  # in Makefiles, so we don't have to be smart.
215
11767
19803
  $val =~ s/#.*$//s;
216  # Strip backslashes.
217
11767
14185
  $val =~ s/\\$/ /mg;
218
11767
42098
  return $val;
219}
220
221sub raw_value ($)
222{
223
182754
1
250189
  my ($self) = @_;
224
182754
455606
  return $self->{'value'};
225}
226
227sub type ($)
228{
229
184144
1
247226
  my ($self) = @_;
230
184144
596434
  return $self->{'type'};
231}
232
233sub pretty ($)
234{
235
370388
1
491527
  my ($self) = @_;
236
370388
1077396
  return $self->{'pretty'};
237}
238
239 - 246
=item C<$def-E<gt>set_owner ($owner, $location)>

Change the owner of a definition.  This usually happens because
the user used C<+=> on an Automake variable, so (s)he now owns
the content.  C<$location> should be an instance of L<Automake::Location>
indicating where the change took place.

=cut
247
248sub set_owner ($$$)
249{
250
5
1
18
  my ($self, $owner, $location) = @_;
251  # We always adjust the location when the owner changes (even for
252  # `+=' statements). The risk otherwise is to warn about
253  # a VAR_MAKEFILE variable and locate it in configure.ac...
254
5
9
  $self->{'owner'} = $owner;
255
5
18
  $self->{'location'} = $location;
256}
257
258 - 266
=item C<$def-E<gt>set_seen>

=item C<$bool = $def-E<gt>seen>

These function allows Automake to mark (C<set_seen>) variable that
it has examined in some way, and latter check (using C<seen>) for
unused variables.  Unused variables usually indicate typos.

=cut
267
268sub set_seen ($)
269{
270
28449
1
43018
  my ($self) = @_;
271
28449
87186
  $self->{'seen'} = 1;
272}
273
274sub seen ($)
275{
276
3898
1
6895
  my ($self) = @_;
277
3898
20373
  return $self->{'seen'};
278}
279
280 - 285
=item C<$str = $def-E<gt>dump>

Format the contents of C<$def> as a human-readable string,
for debugging.

=cut
286
287sub dump ($)
288{
289
37
1
88
  my ($self) = @_;
290
37
106
  my $owner = $self->owner;
291
292
37
186
  if ($owner == VAR_AUTOMAKE)
293    {
294
0
0
      $owner = 'Automake';
295    }
296  elsif ($owner == VAR_CONFIGURE)
297    {
298
8
18
      $owner = 'Configure';
299    }
300  elsif ($owner == VAR_MAKEFILE)
301    {
302
29
55
      $owner = 'Makefile';
303    }
304  else
305    {
306
0
0
      prog_error ("unexpected owner");
307    }
308
309
37
109
  my $where = $self->location->dump;
310
37
198
  my $comment = $self->comment;
311
37
100
  my $value = $self->raw_value;
312
37
79
  my $type = $self->type;
313
314
37
277
  return "{
315      type: $type=
316      where: $where comment: $comment
317      value: $value
318      owner: $owner
319    }\n";
320}
321
322=back
323
324 - 328
=head1 SEE ALSO

L<Automake::Variable>, L<Automake::ItemDef>.

=cut
329
3301;
331
332### Setup "GNU" style for perl-mode and cperl-mode.
333## Local Variables:
334## perl-indent-level: 2
335## perl-continued-statement-offset: 2
336## perl-continued-brace-offset: 0
337## perl-brace-offset: 0
338## perl-brace-imaginary-offset: 0
339## perl-label-offset: -2
340## cperl-indent-level: 2
341## cperl-brace-offset: 0
342## cperl-continued-brace-offset: 0
343## cperl-label-offset: -2
344## cperl-extra-newline-before-brace: t
345## cperl-merge-trailing-else: nil
346## cperl-continued-statement-offset: 2
347## End: