PR c++/3478
[official-gcc.git] / gcc / line-map.c
blob521c4e5c843e76ab9f6cf7b33f9a4cc2a8e405b9
1 /* Map logical line numbers to (source file, line number) pairs.
2 Copyright (C) 2001, 2003
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
26 #include "intl.h"
28 static void trace_include (const struct line_maps *, const struct line_map *);
30 /* Initialize a line map set. */
32 void
33 linemap_init (struct line_maps *set)
35 set->maps = 0;
36 set->allocated = 0;
37 set->used = 0;
38 set->last_listed = -1;
39 set->trace_includes = false;
40 set->depth = 0;
43 /* Free a line map set. */
45 void
46 linemap_free (struct line_maps *set)
48 if (set->maps)
50 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 = CURRENT_LINE_MAP (set); ! 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);
59 free (set->maps);
63 /* Add a mapping of logical source line to physical source file and
64 line number.
66 The text pointed to by TO_FILE must have a lifetime
67 at least as long as the final call to lookup_line (). An empty
68 TO_FILE means standard input. If reason is LC_LEAVE, and
69 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
70 natural values considering the file we are returning to.
72 FROM_LINE should be monotonic increasing across calls to this
73 function. A call to this function can relocate the previous set of
74 maps, so any stored line_map pointers should not be used. */
76 const struct line_map *
77 linemap_add (struct line_maps *set, enum lc_reason reason,
78 unsigned int sysp, source_location from_line,
79 const char *to_file, unsigned int to_line)
81 struct line_map *map;
83 if (set->used && from_line < set->maps[set->used - 1].from_line)
84 abort ();
86 if (set->used == set->allocated)
88 set->allocated = 2 * set->allocated + 256;
89 set->maps = xrealloc (set->maps, set->allocated * sizeof (struct line_map));
92 map = &set->maps[set->used++];
94 if (to_file && *to_file == '\0')
95 to_file = "<stdin>";
97 /* If we don't keep our line maps consistent, we can easily
98 segfault. Don't rely on the client to do it for us. */
99 if (set->depth == 0)
100 reason = LC_ENTER;
101 else if (reason == LC_LEAVE)
103 struct line_map *from;
104 bool error;
106 if (MAIN_FILE_P (map - 1))
108 if (to_file == NULL)
110 set->depth--;
111 set->used--;
112 return NULL;
114 error = true;
115 reason = LC_RENAME;
116 from = map - 1;
118 else
120 from = INCLUDED_FROM (set, map - 1);
121 error = to_file && strcmp (from->to_file, to_file);
124 /* Depending upon whether we are handling preprocessed input or
125 not, this can be a user error or an ICE. */
126 if (error)
127 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
128 to_file);
130 /* A TO_FILE of NULL is special - we use the natural values. */
131 if (error || to_file == NULL)
133 to_file = from->to_file;
134 to_line = LAST_SOURCE_LINE (from) + 1;
135 sysp = from->sysp;
139 map->reason = reason;
140 map->sysp = sysp;
141 map->from_line = from_line;
142 map->to_file = to_file;
143 map->to_line = to_line;
145 if (reason == LC_ENTER)
147 map->included_from = set->depth == 0 ? -1 : (int) (set->used - 2);
148 set->depth++;
149 if (set->trace_includes)
150 trace_include (set, map);
152 else if (reason == LC_RENAME)
153 map->included_from = map[-1].included_from;
154 else if (reason == LC_LEAVE)
156 set->depth--;
157 map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
160 return map;
163 /* Given a logical line, returns the map from which the corresponding
164 (source file, line) pair can be deduced. Since the set is built
165 chronologically, the logical lines are monotonic increasing, and so
166 the list is sorted and we can use a binary search. */
168 const struct line_map *
169 linemap_lookup (struct line_maps *set, source_location line)
171 unsigned int md, mn = 0, mx = set->used;
173 if (mx == 0)
174 abort ();
176 while (mx - mn > 1)
178 md = (mn + mx) / 2;
179 if (set->maps[md].from_line > line)
180 mx = md;
181 else
182 mn = md;
185 return &set->maps[mn];
188 /* Print the file names and line numbers of the #include commands
189 which led to the map MAP, if any, to stderr. Nothing is output if
190 the most recently listed stack is the same as the current one. */
192 void
193 linemap_print_containing_files (struct line_maps *set,
194 const struct line_map *map)
196 if (MAIN_FILE_P (map) || set->last_listed == map->included_from)
197 return;
199 set->last_listed = map->included_from;
200 map = INCLUDED_FROM (set, map);
202 fprintf (stderr, _("In file included from %s:%u"),
203 map->to_file, LAST_SOURCE_LINE (map));
205 while (! MAIN_FILE_P (map))
207 map = INCLUDED_FROM (set, map);
208 /* Translators note: this message is used in conjunction
209 with "In file included from %s:%ld" and some other
210 tricks. We want something like this:
212 | In file included from sys/select.h:123,
213 | from sys/types.h:234,
214 | from userfile.c:31:
215 | bits/select.h:45: <error message here>
217 with all the "from"s lined up.
218 The trailing comma is at the beginning of this message,
219 and the trailing colon is not translated. */
220 fprintf (stderr, _(",\n from %s:%u"),
221 map->to_file, LAST_SOURCE_LINE (map));
224 fputs (":\n", stderr);
227 /* Print an include trace, for e.g. the -H option of the preprocessor. */
229 static void
230 trace_include (const struct line_maps *set, const struct line_map *map)
232 unsigned int i = set->depth;
234 while (--i)
235 putc ('.', stderr);
236 fprintf (stderr, " %s\n", map->to_file);