2007-09-10 Janis Johnson <janis187@us.ibm.com>
[official-gcc.git] / libcpp / line-map.c
blobc13a82dcafc95a8ed038023bce2d035791834b77
1 /* Map logical line numbers to (source file, line number) pairs.
2 Copyright (C) 2001, 2003, 2004, 2007
3 Free Software Foundation, Inc.
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.
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.
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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! */
23 #include "config.h"
24 #include "system.h"
25 #include "line-map.h"
27 static void trace_include (const struct line_maps *, const struct line_map *);
29 /* Initialize a line map set. */
31 void
32 linemap_init (struct line_maps *set)
34 set->maps = NULL;
35 set->allocated = 0;
36 set->used = 0;
37 set->last_listed = -1;
38 set->trace_includes = false;
39 set->depth = 0;
40 set->cache = 0;
41 set->highest_location = 0;
42 set->highest_line = 0;
43 set->max_column_hint = 0;
46 /* Check for and warn about line_maps entered but not exited. */
48 void
49 linemap_check_files_exited (struct line_maps *set)
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);
60 /* Free a line map set. */
62 void
63 linemap_free (struct line_maps *set)
65 if (set->maps)
67 linemap_check_files_exited (set);
69 free (set->maps);
73 /* Add a mapping of logical source line to physical source file and
74 line number.
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.
82 FROM_LINE should be monotonic increasing across calls to this
83 function. A call to this function can relocate the previous set of
84 A call to this function can relocate the previous set of
85 maps, so any stored line_map pointers should not be used. */
87 const struct line_map *
88 linemap_add (struct line_maps *set, enum lc_reason reason,
89 unsigned int sysp, const char *to_file, unsigned int to_line)
91 struct line_map *map;
92 source_location start_location = set->highest_location + 1;
94 if (set->used && start_location < set->maps[set->used - 1].start_location)
95 abort ();
97 if (set->used == set->allocated)
99 line_map_realloc reallocator
100 = set->reallocator ? set->reallocator : xrealloc;
101 set->allocated = 2 * set->allocated + 256;
102 set->maps
103 = (struct line_map *) (*reallocator) (set->maps,
104 set->allocated
105 * sizeof (struct line_map));
106 memset (&set->maps[set->used], 0, ((set->allocated - set->used)
107 * sizeof (struct line_map)));
110 map = &set->maps[set->used];
112 if (to_file && *to_file == '\0')
113 to_file = "<stdin>";
115 /* If we don't keep our line maps consistent, we can easily
116 segfault. Don't rely on the client to do it for us. */
117 if (set->depth == 0)
118 reason = LC_ENTER;
119 else if (reason == LC_LEAVE)
121 struct line_map *from;
122 bool error;
124 if (MAIN_FILE_P (map - 1))
126 if (to_file == NULL)
128 set->depth--;
129 return NULL;
131 error = true;
132 reason = LC_RENAME;
133 from = map - 1;
135 else
137 from = INCLUDED_FROM (set, map - 1);
138 error = to_file && strcmp (from->to_file, to_file);
141 /* Depending upon whether we are handling preprocessed input or
142 not, this can be a user error or an ICE. */
143 if (error)
144 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
145 to_file);
147 /* A TO_FILE of NULL is special - we use the natural values. */
148 if (error || to_file == NULL)
150 to_file = from->to_file;
151 to_line = SOURCE_LINE (from, from[1].start_location);
152 sysp = from->sysp;
156 map->reason = reason;
157 map->sysp = sysp;
158 map->start_location = start_location;
159 map->to_file = to_file;
160 map->to_line = to_line;
161 set->cache = set->used++;
162 map->column_bits = 0;
163 set->highest_location = start_location;
164 set->highest_line = start_location;
165 set->max_column_hint = 0;
167 if (reason == LC_ENTER)
169 map->included_from = set->depth == 0 ? -1 : (int) (set->used - 2);
170 set->depth++;
171 if (set->trace_includes)
172 trace_include (set, map);
174 else if (reason == LC_RENAME)
175 map->included_from = map[-1].included_from;
176 else if (reason == LC_LEAVE)
178 set->depth--;
179 map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
182 return map;
185 source_location
186 linemap_line_start (struct line_maps *set, unsigned int to_line,
187 unsigned int max_column_hint)
189 struct line_map *map = &set->maps[set->used - 1];
190 source_location highest = set->highest_location;
191 source_location r;
192 unsigned int last_line = SOURCE_LINE (map, set->highest_line);
193 int line_delta = to_line - last_line;
194 bool add_map = false;
195 if (line_delta < 0
196 || (line_delta > 10 && line_delta * map->column_bits > 1000)
197 || (max_column_hint >= (1U << map->column_bits))
198 || (max_column_hint <= 80 && map->column_bits >= 10))
200 add_map = true;
202 else
203 max_column_hint = set->max_column_hint;
204 if (add_map)
206 int column_bits;
207 if (max_column_hint > 100000 || highest > 0xC0000000)
209 /* If the column number is ridiculous or we've allocated a huge
210 number of source_locations, give up on column numbers. */
211 max_column_hint = 0;
212 if (highest >0xF0000000)
213 return 0;
214 column_bits = 0;
216 else
218 column_bits = 7;
219 while (max_column_hint >= (1U << column_bits))
220 column_bits++;
221 max_column_hint = 1U << column_bits;
223 /* Allocate the new line_map. However, if the current map only has a
224 single line we can sometimes just increase its column_bits instead. */
225 if (line_delta < 0
226 || last_line != map->to_line
227 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
228 map = (struct line_map*) linemap_add (set, LC_RENAME, map->sysp,
229 map->to_file, to_line);
230 map->column_bits = column_bits;
231 r = map->start_location + ((to_line - map->to_line) << column_bits);
233 else
234 r = highest - SOURCE_COLUMN (map, highest)
235 + (line_delta << map->column_bits);
236 set->highest_line = r;
237 if (r > set->highest_location)
238 set->highest_location = r;
239 set->max_column_hint = max_column_hint;
240 return r;
243 source_location
244 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
246 source_location r = set->highest_line;
247 if (to_column >= set->max_column_hint)
249 if (r >= 0xC000000 || to_column > 100000)
251 /* Running low on source_locations - disable column numbers. */
252 return r;
254 else
256 struct line_map *map = &set->maps[set->used - 1];
257 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
260 r = r + to_column;
261 if (r >= set->highest_location)
262 set->highest_location = r;
263 return r;
266 /* Given a logical line, returns the map from which the corresponding
267 (source file, line) pair can be deduced. Since the set is built
268 chronologically, the logical lines are monotonic increasing, and so
269 the list is sorted and we can use a binary search. */
271 const struct line_map *
272 linemap_lookup (struct line_maps *set, source_location line)
274 unsigned int md, mn, mx;
275 const struct line_map *cached;
277 mn = set->cache;
278 mx = set->used;
280 cached = &set->maps[mn];
281 /* We should get a segfault if no line_maps have been added yet. */
282 if (line >= cached->start_location)
284 if (mn + 1 == mx || line < cached[1].start_location)
285 return cached;
287 else
289 mx = mn;
290 mn = 0;
293 while (mx - mn > 1)
295 md = (mn + mx) / 2;
296 if (set->maps[md].start_location > line)
297 mx = md;
298 else
299 mn = md;
302 set->cache = mn;
303 return &set->maps[mn];
306 /* Print the file names and line numbers of the #include commands
307 which led to the map MAP, if any, to stderr. Nothing is output if
308 the most recently listed stack is the same as the current one. */
310 void
311 linemap_print_containing_files (struct line_maps *set,
312 const struct line_map *map)
314 if (MAIN_FILE_P (map) || set->last_listed == map->included_from)
315 return;
317 set->last_listed = map->included_from;
318 map = INCLUDED_FROM (set, map);
320 fprintf (stderr, _("In file included from %s:%u"),
321 map->to_file, LAST_SOURCE_LINE (map));
323 while (! MAIN_FILE_P (map))
325 map = INCLUDED_FROM (set, map);
326 /* Translators note: this message is used in conjunction
327 with "In file included from %s:%ld" and some other
328 tricks. We want something like this:
330 | In file included from sys/select.h:123,
331 | from sys/types.h:234,
332 | from userfile.c:31:
333 | bits/select.h:45: <error message here>
335 with all the "from"s lined up.
336 The trailing comma is at the beginning of this message,
337 and the trailing colon is not translated. */
338 fprintf (stderr, _(",\n from %s:%u"),
339 map->to_file, LAST_SOURCE_LINE (map));
342 fputs (":\n", stderr);
345 /* Print an include trace, for e.g. the -H option of the preprocessor. */
347 static void
348 trace_include (const struct line_maps *set, const struct line_map *map)
350 unsigned int i = set->depth;
352 while (--i)
353 putc ('.', stderr);
354 fprintf (stderr, " %s\n", map->to_file);