1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/* Map logical line numbers to (source file, line number) pairs.
Copyright (C) 2001
Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding! */
#include "config.h"
#include "system.h"
#include "line-map.h"
/* Initialize a line map set. */
void
init_line_maps (set)
struct line_maps *set;
{
set->maps = 0;
set->allocated = 0;
set->used = 0;
}
/* Free a line map set. */
void free_line_maps (set)
struct line_maps *set;
{
if (set->maps)
free (set->maps);
}
/* Add a mapping of logical source line to physical source file and
line number. Ther text pointed to by TO_FILE must have a lifetime
at least as long as the final call to lookup_line ().
FROM_LINE should be monotonic increasing across calls to this
function. */
struct line_map *
add_line_map (set, reason, from_line, to_file, to_line)
struct line_maps *set;
enum lc_reason reason;
unsigned int from_line;
const char *to_file;
unsigned int to_line;
{
struct line_map *map;
if (set->used && from_line < set->maps[set->used - 1].from_line)
abort ();
if (set->used == set->allocated)
{
set->allocated = 2 * set->allocated + 256;
set->maps = (struct line_map *)
xrealloc (set->maps, set->allocated * sizeof (struct line_map));
}
map = &set->maps[set->used];
map->from_line = from_line;
map->to_file = to_file;
map->to_line = to_line;
if (set->used == 0)
map->included_from = -1;
else if (reason == LC_ENTER)
map->included_from = set->used - 1;
else if (reason == LC_RENAME)
map->included_from = map[-1].included_from;
else if (reason == LC_LEAVE)
{
if (map[-1].included_from < 0)
abort ();
map->included_from = set->maps[map[-1].included_from].included_from;
}
set->used++;
return map;
}
/* Given a logical line, returns the map from which the corresponding
(source file, line) pair can be deduced. Since the set is built
chronologically, the logical lines are monotonic increasing, and so
the list is sorted and we can use a binary search. */
struct line_map *
lookup_line (set, line)
struct line_maps *set;
unsigned int line;
{
unsigned int md, mn = 0, mx = set->used;
if (mx == 0)
abort ();
while (mx - mn > 1)
{
md = (mn + mx) / 2;
if (set->maps[md].from_line > line)
mx = md;
else
mn = md;
}
return &set->maps[mn];
}
|