1 # Copyright (C) 2002, 2003, 2008, 2009 Free Software Foundation, Inc.
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)
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.
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/>.
16 package Automake::Location;
20 Automake::Location - a class for location tracking, with a stack of contexts
24 use Automake::Location;
26 # Create a new Location object
27 my $where = new Automake::Location "foo.c:13";
30 $where->set ("foo.c:14");
32 # Get the location (without context).
33 # Here this should print "foo.c:14"
34 print $where->get, "\n";
36 # Push a context, and change the location
37 $where->push_context ("included from here");
38 $where->set ("bar.h:1");
40 # Print the location and the stack of context (for debugging)
44 # foo.c:14: included from here
46 # Get the contexts (list of [$location_string, $description])
47 for my $pair (reverse $where->contexts)
49 my ($loc, $descr) = @{$pair};
53 # Pop a context, and reset the location to the previous context.
56 # Clone a Location. Use this when storing the state of a location
57 # that would otherwise be modified.
58 my $where_copy = $where->clone;
60 # Serialize a Location object (for passing through a thread queue,
62 my @array = $where->serialize ();
64 # De-serialize: recreate a Location object from a queue.
65 my $where = new Automake::Location::deserialize ($queue);
69 C<Location> objects are used to keep track of locations in Automake,
70 and used to produce diagnostics.
72 A C<Location> object is made of two parts: a location string, and
75 For instance if C<VAR> is defined at line 1 in F<bar.h> which was
76 included at line 14 in F<foo.c>, then the location string should be
77 C<"bar.h:10"> and the context should be the pair (C<"foo.c:14">,
78 C<"included from here">).
80 Section I<SYNOPSIS> shows how to setup such a C<Location>, and access
81 the location string or the stack of contexts.
83 You can pass a C<Location> to C<Automake::Channels::msg>.
91 =item C<$where = new Automake::Location ([$position])>
93 Create and return a new Location object.
99 my ($class, $position) = @_;
101 position => $position,
108 =item C<$location-E<gt>set ($position)>
110 Change the location to be C<$position>.
116 my ($self, $position) = @_;
117 $self->{'position'} = $position;
120 =item C<$location-E<gt>get>
122 Get the location (without context).
129 return $self->{'position'};
132 =item C<$location-E<gt>push_context ($context)>
134 Push a context to the location.
138 sub push_context ($$)
140 my ($self, $context) = @_;
141 push @{$self->{'contexts'}}, [$self->get, $context];
145 =item C<$where = $location-E<gt>pop_context ($context)>
147 Pop a context, and reset the location to the previous context.
154 my $pair = pop @{$self->{'contexts'}};
155 $self->set ($pair->[0]);
159 =item C<@contexts = $location-E<gt>get_contexts>
161 Return the array of contexts.
168 return @{$self->{'contexts'}};
171 =item C<$location = $location-E<gt>clone>
173 Clone a Location. Use this when storing the state of a location
174 that would otherwise be modified.
181 my $other = new Automake::Location ($self->get);
182 my @contexts = $self->get_contexts;
183 for my $pair (@contexts)
185 push @{$other->{'contexts'}}, [@{$pair}];
190 =item C<$res = $location-E<gt>dump>
192 Print the location and the stack of context (for debugging).
199 my $res = ($self->get || 'INTERNAL') . ":\n";
200 for my $pair (reverse $self->get_contexts)
202 $res .= $pair->[0] || 'INTERNAL';
203 $res .= ": $pair->[1]\n";
208 =item C<@array = $location-E<gt>serialize>
210 Serialize a Location object (for passing through a thread queue,
219 push @serial, $self->get;
220 my @contexts = $self->get_contexts;
221 for my $pair (@contexts)
223 push @serial, @{$pair};
229 =item C<new Automake::Location::deserialize ($queue)>
231 De-serialize: recreate a Location object from a queue.
238 my $position = $queue->dequeue ();
239 my $self = new Automake::Location $position;
240 while (my $position = $queue->dequeue ())
242 my $context = $queue->dequeue ();
243 push @{$self->{'contexts'}}, [$position, $context];
252 L<Automake::Channels>
256 Written by Alexandre Duret-Lutz E<lt>F<adl@gnu.org>E<gt>.
262 ### Setup "GNU" style for perl-mode and cperl-mode.
264 ## perl-indent-level: 2
265 ## perl-continued-statement-offset: 2
266 ## perl-continued-brace-offset: 0
267 ## perl-brace-offset: 0
268 ## perl-brace-imaginary-offset: 0
269 ## perl-label-offset: -2
270 ## cperl-indent-level: 2
271 ## cperl-brace-offset: 0
272 ## cperl-continued-brace-offset: 0
273 ## cperl-label-offset: -2
274 ## cperl-extra-newline-before-brace: t
275 ## cperl-merge-trailing-else: nil
276 ## cperl-continued-statement-offset: 2