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