blob: 964a7cd9a779084bba6a9edfaca253d379d912ab [file] [log] [blame]
Neil Boothd82fc102001-08-02 23:03:31 +00001/* Map logical line numbers to (source file, line number) pairs.
Tom Tromey2bf41bf2008-02-20 02:16:43 +00002 Copyright (C) 2001, 2003, 2004, 2007, 2008
Neil Boothd82fc102001-08-02 23:03:31 +00003 Free Software Foundation, Inc.
4
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
7Free Software Foundation; either version 2, or (at your option) any
8later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
Kelley Cook200031d2005-06-29 02:34:39 +000017Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Neil Boothd82fc102001-08-02 23:03:31 +000018
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
Neil Boothe2b98532003-06-20 05:51:13 +000027static void trace_include (const struct line_maps *, const struct line_map *);
Neil Booth59930192001-08-21 21:17:48 +000028
Neil Boothd82fc102001-08-02 23:03:31 +000029/* Initialize a line map set. */
30
31void
Zack Weinberga2f7be92003-07-22 16:24:53 +000032linemap_init (struct line_maps *set)
Neil Boothd82fc102001-08-02 23:03:31 +000033{
Per Bothner9132fbb2004-01-20 05:17:48 +000034 set->maps = NULL;
Neil Boothd82fc102001-08-02 23:03:31 +000035 set->allocated = 0;
36 set->used = 0;
Neil Boothfde84342001-08-06 21:07:41 +000037 set->last_listed = -1;
Neil Booth59930192001-08-21 21:17:48 +000038 set->trace_includes = false;
Neil Boothd8693c62001-08-21 23:05:12 +000039 set->depth = 0;
Per Bothner9132fbb2004-01-20 05:17:48 +000040 set->cache = 0;
Per Bothner12f9df42004-02-11 07:29:30 -080041 set->highest_location = 0;
Per Bothner500bee02004-04-22 19:22:27 -070042 set->highest_line = 0;
Per Bothner12f9df42004-02-11 07:29:30 -080043 set->max_column_hint = 0;
Neil Boothd82fc102001-08-02 23:03:31 +000044}
45
Kazu Hirata9ac97462004-02-16 14:20:10 +000046/* Check for and warn about line_maps entered but not exited. */
Per Bothner12f9df42004-02-11 07:29:30 -080047
48void
49linemap_check_files_exited (struct line_maps *set)
50{
51 struct line_map *map;
52 /* Depending upon whether we are handling preprocessed input or
53 not, this can be a user error or an ICE. */
54 for (map = &set->maps[set->used - 1]; ! MAIN_FILE_P (map);
55 map = INCLUDED_FROM (set, map))
56 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
57 map->to_file);
58}
59
Neil Boothd82fc102001-08-02 23:03:31 +000060/* Free a line map set. */
61
Neil Boothfde84342001-08-06 21:07:41 +000062void
Zack Weinberga2f7be92003-07-22 16:24:53 +000063linemap_free (struct line_maps *set)
Neil Boothd82fc102001-08-02 23:03:31 +000064{
65 if (set->maps)
Neil Boothfde84342001-08-06 21:07:41 +000066 {
Per Bothner12f9df42004-02-11 07:29:30 -080067 linemap_check_files_exited (set);
Neil Booth47d89cf2001-08-11 07:33:39 +000068
Neil Boothfde84342001-08-06 21:07:41 +000069 free (set->maps);
70 }
Neil Boothd82fc102001-08-02 23:03:31 +000071}
72
73/* Add a mapping of logical source line to physical source file and
Geoffrey Keating90744642003-07-30 06:43:33 +000074 line number.
75
76 The text pointed to by TO_FILE must have a lifetime
77 at least as long as the final call to lookup_line (). An empty
78 TO_FILE means standard input. If reason is LC_LEAVE, and
79 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
80 natural values considering the file we are returning to.
Neil Boothd82fc102001-08-02 23:03:31 +000081
82 FROM_LINE should be monotonic increasing across calls to this
Geoffrey Keating90744642003-07-30 06:43:33 +000083 function. A call to this function can relocate the previous set of
84 maps, so any stored line_map pointers should not be used. */
Neil Boothd82fc102001-08-02 23:03:31 +000085
Neil Booth47d89cf2001-08-11 07:33:39 +000086const struct line_map *
Zack Weinberga2f7be92003-07-22 16:24:53 +000087linemap_add (struct line_maps *set, enum lc_reason reason,
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +000088 unsigned int sysp, const char *to_file, linenum_type to_line)
Neil Boothd82fc102001-08-02 23:03:31 +000089{
90 struct line_map *map;
Per Bothner12f9df42004-02-11 07:29:30 -080091 source_location start_location = set->highest_location + 1;
Neil Boothd82fc102001-08-02 23:03:31 +000092
Per Bothner12f9df42004-02-11 07:29:30 -080093 if (set->used && start_location < set->maps[set->used - 1].start_location)
Neil Boothd82fc102001-08-02 23:03:31 +000094 abort ();
95
96 if (set->used == set->allocated)
97 {
Tom Tromey5ffeb9132007-09-06 16:24:05 +000098 line_map_realloc reallocator
99 = set->reallocator ? set->reallocator : xrealloc;
Neil Boothd82fc102001-08-02 23:03:31 +0000100 set->allocated = 2 * set->allocated + 256;
Tom Tromey5ffeb9132007-09-06 16:24:05 +0000101 set->maps
102 = (struct line_map *) (*reallocator) (set->maps,
103 set->allocated
104 * sizeof (struct line_map));
105 memset (&set->maps[set->used], 0, ((set->allocated - set->used)
106 * sizeof (struct line_map)));
Neil Boothd82fc102001-08-02 23:03:31 +0000107 }
108
Per Bothner9132fbb2004-01-20 05:17:48 +0000109 map = &set->maps[set->used];
Neil Boothd82fc102001-08-02 23:03:31 +0000110
Geoffrey Keating90744642003-07-30 06:43:33 +0000111 if (to_file && *to_file == '\0')
112 to_file = "<stdin>";
113
Neil Boothfde84342001-08-06 21:07:41 +0000114 /* If we don't keep our line maps consistent, we can easily
115 segfault. Don't rely on the client to do it for us. */
Neil Boothd8693c62001-08-21 23:05:12 +0000116 if (set->depth == 0)
Neil Boothfde84342001-08-06 21:07:41 +0000117 reason = LC_ENTER;
118 else if (reason == LC_LEAVE)
119 {
Neil Booth47d89cf2001-08-11 07:33:39 +0000120 struct line_map *from;
121 bool error;
122
123 if (MAIN_FILE_P (map - 1))
Neil Boothfde84342001-08-06 21:07:41 +0000124 {
Per Bothnerb3147022003-07-22 23:11:34 +0000125 if (to_file == NULL)
126 {
127 set->depth--;
Per Bothnerb3147022003-07-22 23:11:34 +0000128 return NULL;
129 }
130 error = true;
131 reason = LC_RENAME;
132 from = map - 1;
Neil Booth47d89cf2001-08-11 07:33:39 +0000133 }
134 else
135 {
136 from = INCLUDED_FROM (set, map - 1);
137 error = to_file && strcmp (from->to_file, to_file);
138 }
139
140 /* Depending upon whether we are handling preprocessed input or
141 not, this can be a user error or an ICE. */
142 if (error)
143 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
144 to_file);
145
146 /* A TO_FILE of NULL is special - we use the natural values. */
147 if (error || to_file == NULL)
148 {
149 to_file = from->to_file;
Per Bothner12f9df42004-02-11 07:29:30 -0800150 to_line = SOURCE_LINE (from, from[1].start_location);
Neil Booth47d89cf2001-08-11 07:33:39 +0000151 sysp = from->sysp;
Neil Boothfde84342001-08-06 21:07:41 +0000152 }
153 }
154
Neil Booth47d89cf2001-08-11 07:33:39 +0000155 map->reason = reason;
156 map->sysp = sysp;
Per Bothner12f9df42004-02-11 07:29:30 -0800157 map->start_location = start_location;
Neil Booth47d89cf2001-08-11 07:33:39 +0000158 map->to_file = to_file;
159 map->to_line = to_line;
Per Bothner9132fbb2004-01-20 05:17:48 +0000160 set->cache = set->used++;
Per Bothner12f9df42004-02-11 07:29:30 -0800161 map->column_bits = 0;
162 set->highest_location = start_location;
Per Bothner500bee02004-04-22 19:22:27 -0700163 set->highest_line = start_location;
Per Bothner12f9df42004-02-11 07:29:30 -0800164 set->max_column_hint = 0;
Neil Booth47d89cf2001-08-11 07:33:39 +0000165
Neil Boothfde84342001-08-06 21:07:41 +0000166 if (reason == LC_ENTER)
Neil Boothd8693c62001-08-21 23:05:12 +0000167 {
Per Bothner8826ff02003-03-20 16:43:19 +0000168 map->included_from = set->depth == 0 ? -1 : (int) (set->used - 2);
Neil Boothd8693c62001-08-21 23:05:12 +0000169 set->depth++;
Neil Boothd8693c62001-08-21 23:05:12 +0000170 if (set->trace_includes)
171 trace_include (set, map);
172 }
Neil Boothd82fc102001-08-02 23:03:31 +0000173 else if (reason == LC_RENAME)
174 map->included_from = map[-1].included_from;
175 else if (reason == LC_LEAVE)
Neil Boothd8693c62001-08-21 23:05:12 +0000176 {
177 set->depth--;
178 map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
179 }
Neil Booth59930192001-08-21 21:17:48 +0000180
Neil Boothd82fc102001-08-02 23:03:31 +0000181 return map;
182}
183
Per Bothner12f9df42004-02-11 07:29:30 -0800184source_location
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000185linemap_line_start (struct line_maps *set, linenum_type to_line,
Per Bothner12f9df42004-02-11 07:29:30 -0800186 unsigned int max_column_hint)
187{
188 struct line_map *map = &set->maps[set->used - 1];
189 source_location highest = set->highest_location;
190 source_location r;
Manuel López-Ibáñez1bb64662008-07-21 09:33:38 +0000191 linenum_type last_line = SOURCE_LINE (map, set->highest_line);
Per Bothner12f9df42004-02-11 07:29:30 -0800192 int line_delta = to_line - last_line;
193 bool add_map = false;
194 if (line_delta < 0
195 || (line_delta > 10 && line_delta * map->column_bits > 1000)
196 || (max_column_hint >= (1U << map->column_bits))
197 || (max_column_hint <= 80 && map->column_bits >= 10))
198 {
199 add_map = true;
200 }
201 else
202 max_column_hint = set->max_column_hint;
203 if (add_map)
204 {
205 int column_bits;
Per Bothner500bee02004-04-22 19:22:27 -0700206 if (max_column_hint > 100000 || highest > 0xC0000000)
Per Bothner12f9df42004-02-11 07:29:30 -0800207 {
Per Bothnerc1fc5042005-04-21 10:08:27 -0700208 /* If the column number is ridiculous or we've allocated a huge
209 number of source_locations, give up on column numbers. */
Per Bothner12f9df42004-02-11 07:29:30 -0800210 max_column_hint = 0;
211 if (highest >0xF0000000)
212 return 0;
213 column_bits = 0;
214 }
215 else
216 {
217 column_bits = 7;
218 while (max_column_hint >= (1U << column_bits))
219 column_bits++;
220 max_column_hint = 1U << column_bits;
221 }
Per Bothnerc1fc5042005-04-21 10:08:27 -0700222 /* Allocate the new line_map. However, if the current map only has a
223 single line we can sometimes just increase its column_bits instead. */
Per Bothner12f9df42004-02-11 07:29:30 -0800224 if (line_delta < 0
225 || last_line != map->to_line
226 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
Tom Tromey2bf41bf2008-02-20 02:16:43 +0000227 map = (struct line_map *) linemap_add (set, LC_RENAME, map->sysp,
228 map->to_file, to_line);
Per Bothner12f9df42004-02-11 07:29:30 -0800229 map->column_bits = column_bits;
Per Bothnerc1fc5042005-04-21 10:08:27 -0700230 r = map->start_location + ((to_line - map->to_line) << column_bits);
Per Bothner12f9df42004-02-11 07:29:30 -0800231 }
232 else
233 r = highest - SOURCE_COLUMN (map, highest)
234 + (line_delta << map->column_bits);
Per Bothner500bee02004-04-22 19:22:27 -0700235 set->highest_line = r;
Per Bothner12f9df42004-02-11 07:29:30 -0800236 if (r > set->highest_location)
237 set->highest_location = r;
238 set->max_column_hint = max_column_hint;
239 return r;
240}
241
Per Bothner500bee02004-04-22 19:22:27 -0700242source_location
243linemap_position_for_column (struct line_maps *set, unsigned int to_column)
244{
245 source_location r = set->highest_line;
246 if (to_column >= set->max_column_hint)
247 {
248 if (r >= 0xC000000 || to_column > 100000)
249 {
250 /* Running low on source_locations - disable column numbers. */
251 return r;
252 }
253 else
254 {
255 struct line_map *map = &set->maps[set->used - 1];
256 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
257 }
258 }
259 r = r + to_column;
260 if (r >= set->highest_location)
261 set->highest_location = r;
262 return r;
263}
264
Neil Booth6604e6f2001-08-05 21:31:30 +0000265/* Given a logical line, returns the map from which the corresponding
266 (source file, line) pair can be deduced. Since the set is built
267 chronologically, the logical lines are monotonic increasing, and so
268 the list is sorted and we can use a binary search. */
Neil Boothd82fc102001-08-02 23:03:31 +0000269
Neil Booth47d89cf2001-08-11 07:33:39 +0000270const struct line_map *
Per Bothner7d40b452003-12-05 12:52:39 -0800271linemap_lookup (struct line_maps *set, source_location line)
Neil Boothd82fc102001-08-02 23:03:31 +0000272{
Per Bothner9132fbb2004-01-20 05:17:48 +0000273 unsigned int md, mn, mx;
274 const struct line_map *cached;
Neil Boothd82fc102001-08-02 23:03:31 +0000275
Per Bothner9132fbb2004-01-20 05:17:48 +0000276 mn = set->cache;
277 mx = set->used;
278
279 cached = &set->maps[mn];
Kazu Hirata9ac97462004-02-16 14:20:10 +0000280 /* We should get a segfault if no line_maps have been added yet. */
Per Bothner12f9df42004-02-11 07:29:30 -0800281 if (line >= cached->start_location)
Per Bothner9132fbb2004-01-20 05:17:48 +0000282 {
Per Bothner12f9df42004-02-11 07:29:30 -0800283 if (mn + 1 == mx || line < cached[1].start_location)
Per Bothner9132fbb2004-01-20 05:17:48 +0000284 return cached;
285 }
286 else
287 {
288 mx = mn;
289 mn = 0;
290 }
Neil Boothd82fc102001-08-02 23:03:31 +0000291
292 while (mx - mn > 1)
293 {
294 md = (mn + mx) / 2;
Per Bothner12f9df42004-02-11 07:29:30 -0800295 if (set->maps[md].start_location > line)
Neil Boothd82fc102001-08-02 23:03:31 +0000296 mx = md;
297 else
298 mn = md;
299 }
300
Per Bothner9132fbb2004-01-20 05:17:48 +0000301 set->cache = mn;
Neil Boothd82fc102001-08-02 23:03:31 +0000302 return &set->maps[mn];
303}
Neil Boothfde84342001-08-06 21:07:41 +0000304
305/* Print the file names and line numbers of the #include commands
306 which led to the map MAP, if any, to stderr. Nothing is output if
307 the most recently listed stack is the same as the current one. */
308
309void
Zack Weinberga2f7be92003-07-22 16:24:53 +0000310linemap_print_containing_files (struct line_maps *set,
311 const struct line_map *map)
Neil Boothfde84342001-08-06 21:07:41 +0000312{
313 if (MAIN_FILE_P (map) || set->last_listed == map->included_from)
314 return;
315
316 set->last_listed = map->included_from;
317 map = INCLUDED_FROM (set, map);
318
319 fprintf (stderr, _("In file included from %s:%u"),
320 map->to_file, LAST_SOURCE_LINE (map));
321
322 while (! MAIN_FILE_P (map))
323 {
324 map = INCLUDED_FROM (set, map);
325 /* Translators note: this message is used in conjunction
326 with "In file included from %s:%ld" and some other
327 tricks. We want something like this:
328
329 | In file included from sys/select.h:123,
330 | from sys/types.h:234,
331 | from userfile.c:31:
332 | bits/select.h:45: <error message here>
333
334 with all the "from"s lined up.
335 The trailing comma is at the beginning of this message,
336 and the trailing colon is not translated. */
337 fprintf (stderr, _(",\n from %s:%u"),
338 map->to_file, LAST_SOURCE_LINE (map));
339 }
340
341 fputs (":\n", stderr);
342}
Neil Booth59930192001-08-21 21:17:48 +0000343
344/* Print an include trace, for e.g. the -H option of the preprocessor. */
345
346static void
Neil Boothe2b98532003-06-20 05:51:13 +0000347trace_include (const struct line_maps *set, const struct line_map *map)
Neil Booth59930192001-08-21 21:17:48 +0000348{
Neil Boothd8693c62001-08-21 23:05:12 +0000349 unsigned int i = set->depth;
Neil Booth59930192001-08-21 21:17:48 +0000350
Neil Boothd8693c62001-08-21 23:05:12 +0000351 while (--i)
Neil Booth59930192001-08-21 21:17:48 +0000352 putc ('.', stderr);
353 fprintf (stderr, " %s\n", map->to_file);
354}