1 # Copyright (C) 2002, 2003, 2008 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 from 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>.
89 my ($class, $position) = @_;
91 position => $position,
100 my ($self, $position) = @_;
101 $self->{'position'} = $position;
107 return $self->{'position'};
110 sub push_context ($$)
112 my ($self, $context) = @_;
113 push @{$self->{'contexts'}}, [$self->get, $context];
120 my $pair = pop @{$self->{'contexts'}};
121 $self->set ($pair->[0]);
128 return @{$self->{'contexts'}};
134 my $other = new Automake::Location ($self->get);
135 my @contexts = $self->get_contexts;
136 for my $pair (@contexts)
138 push @{$other->{'contexts'}}, [@{$pair}];
146 my $res = ($self->get || 'INTERNAL') . ":\n";
147 for my $pair (reverse $self->get_contexts)
149 $res .= $pair->[0] || 'INTERNAL';
150 $res .= ": $pair->[1]\n";
159 push @serial, $self->get;
160 my @contexts = $self->get_contexts;
161 for my $pair (@contexts)
163 push @serial, @{$pair};
172 my $position = $queue->dequeue ();
173 my $self = new Automake::Location $position;
174 while (my $position = $queue->dequeue ())
176 my $context = $queue->dequeue ();
177 push @{$self->{'contexts'}}, [$position, $context];
184 L<Automake::Channels>
188 Written by Alexandre Duret-Lutz E<lt>F<adl@gnu.org>E<gt>.
194 ### Setup "GNU" style for perl-mode and cperl-mode.
196 ## perl-indent-level: 2
197 ## perl-continued-statement-offset: 2
198 ## perl-continued-brace-offset: 0
199 ## perl-brace-offset: 0
200 ## perl-brace-imaginary-offset: 0
201 ## perl-label-offset: -2
202 ## cperl-indent-level: 2
203 ## cperl-brace-offset: 0
204 ## cperl-continued-brace-offset: 0
205 ## cperl-label-offset: -2
206 ## cperl-extra-newline-before-brace: t
207 ## cperl-merge-trailing-else: nil
208 ## cperl-continued-statement-offset: 2