configure.in (GLIBCPP_ENABLE_CXX_FLAGS): Do not pass arguments, let the defaults...
[official-gcc.git] / gcc / line-map.c
blobae26e76a1efbad377ea7a8ca6deecb256e1bb66f
1 /* Map logical line numbers to (source file, line number) pairs.
2 Copyright (C) 2001
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 "coretypes.h"
26 #include "tm.h"
27 #include "line-map.h"
28 #include "intl.h"
30 static void trace_include
31 PARAMS ((const struct line_maps *, const struct line_map *));
33 /* Initialize a line map set. */
35 void
36 init_line_maps (set)
37 struct line_maps *set;
39 set->maps = 0;
40 set->allocated = 0;
41 set->used = 0;
42 set->last_listed = -1;
43 set->trace_includes = false;
44 set->depth = 0;
47 /* Free a line map set. */
49 void
50 free_line_maps (set)
51 struct line_maps *set;
53 if (set->maps)
55 struct line_map *map;
57 /* Depending upon whether we are handling preprocessed input or
58 not, this can be a user error or an ICE. */
59 for (map = CURRENT_LINE_MAP (set); ! MAIN_FILE_P (map);
60 map = INCLUDED_FROM (set, map))
61 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
62 map->to_file);
64 free (set->maps);
68 /* Add a mapping of logical source line to physical source file and
69 line number. Ther text pointed to by TO_FILE must have a lifetime
70 at least as long as the final call to lookup_line ().
72 FROM_LINE should be monotonic increasing across calls to this
73 function. */
75 const struct line_map *
76 add_line_map (set, reason, sysp, from_line, to_file, to_line)
77 struct line_maps *set;
78 enum lc_reason reason;
79 unsigned int sysp;
80 unsigned int from_line;
81 const char *to_file;
82 unsigned int to_line;
84 struct line_map *map;
86 if (set->used && from_line < set->maps[set->used - 1].from_line)
87 abort ();
89 if (set->used == set->allocated)
91 set->allocated = 2 * set->allocated + 256;
92 set->maps = (struct line_map *)
93 xrealloc (set->maps, set->allocated * sizeof (struct line_map));
96 map = &set->maps[set->used++];
98 /* If we don't keep our line maps consistent, we can easily
99 segfault. Don't rely on the client to do it for us. */
100 if (set->depth == 0)
101 reason = LC_ENTER;
102 else if (reason == LC_LEAVE)
104 struct line_map *from;
105 bool error;
107 if (MAIN_FILE_P (map - 1))
109 error = true;
110 reason = LC_RENAME;
111 from = map - 1;
113 else
115 from = INCLUDED_FROM (set, map - 1);
116 error = to_file && strcmp (from->to_file, to_file);
119 /* Depending upon whether we are handling preprocessed input or
120 not, this can be a user error or an ICE. */
121 if (error)
122 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
123 to_file);
125 /* A TO_FILE of NULL is special - we use the natural values. */
126 if (error || to_file == NULL)
128 to_file = from->to_file;
129 to_line = LAST_SOURCE_LINE (from) + 1;
130 sysp = from->sysp;
134 map->reason = reason;
135 map->sysp = sysp;
136 map->from_line = from_line;
137 map->to_file = to_file;
138 map->to_line = to_line;
140 if (reason == LC_ENTER)
142 set->depth++;
143 map->included_from = set->used - 2;
144 if (set->trace_includes)
145 trace_include (set, map);
147 else if (reason == LC_RENAME)
148 map->included_from = map[-1].included_from;
149 else if (reason == LC_LEAVE)
151 set->depth--;
152 map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
155 return map;
158 /* Given a logical line, returns the map from which the corresponding
159 (source file, line) pair can be deduced. Since the set is built
160 chronologically, the logical lines are monotonic increasing, and so
161 the list is sorted and we can use a binary search. */
163 const struct line_map *
164 lookup_line (set, line)
165 struct line_maps *set;
166 unsigned int line;
168 unsigned int md, mn = 0, mx = set->used;
170 if (mx == 0)
171 abort ();
173 while (mx - mn > 1)
175 md = (mn + mx) / 2;
176 if (set->maps[md].from_line > line)
177 mx = md;
178 else
179 mn = md;
182 return &set->maps[mn];
185 /* Print the file names and line numbers of the #include commands
186 which led to the map MAP, if any, to stderr. Nothing is output if
187 the most recently listed stack is the same as the current one. */
189 void
190 print_containing_files (set, map)
191 struct line_maps *set;
192 const struct line_map *map;
194 if (MAIN_FILE_P (map) || set->last_listed == map->included_from)
195 return;
197 set->last_listed = map->included_from;
198 map = INCLUDED_FROM (set, map);
200 fprintf (stderr, _("In file included from %s:%u"),
201 map->to_file, LAST_SOURCE_LINE (map));
203 while (! MAIN_FILE_P (map))
205 map = INCLUDED_FROM (set, map);
206 /* Translators note: this message is used in conjunction
207 with "In file included from %s:%ld" and some other
208 tricks. We want something like this:
210 | In file included from sys/select.h:123,
211 | from sys/types.h:234,
212 | from userfile.c:31:
213 | bits/select.h:45: <error message here>
215 with all the "from"s lined up.
216 The trailing comma is at the beginning of this message,
217 and the trailing colon is not translated. */
218 fprintf (stderr, _(",\n from %s:%u"),
219 map->to_file, LAST_SOURCE_LINE (map));
222 fputs (":\n", stderr);
225 /* Print an include trace, for e.g. the -H option of the preprocessor. */
227 static void
228 trace_include (set, map)
229 const struct line_maps *set;
230 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);