Missed one in last change.
[official-gcc.git] / gcc / line-map.c
blob8bbe863693be55546700fcd7f55fa8169ad28aeb
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 (const struct line_maps *, const struct line_map *);
32 /* Initialize a line map set. */
34 void
35 init_line_maps (struct line_maps *set)
37 set->maps = 0;
38 set->allocated = 0;
39 set->used = 0;
40 set->last_listed = -1;
41 set->trace_includes = false;
42 set->depth = 0;
45 /* Free a line map set. */
47 void
48 free_line_maps (struct line_maps *set)
50 if (set->maps)
52 struct line_map *map;
54 /* Depending upon whether we are handling preprocessed input or
55 not, this can be a user error or an ICE. */
56 for (map = CURRENT_LINE_MAP (set); ! MAIN_FILE_P (map);
57 map = INCLUDED_FROM (set, map))
58 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
59 map->to_file);
61 free (set->maps);
65 /* Add a mapping of logical source line to physical source file and
66 line number. The text pointed to by TO_FILE must have a lifetime
67 at least as long as the final call to lookup_line ().
69 FROM_LINE should be monotonic increasing across calls to this
70 function. */
72 const struct line_map *
73 add_line_map (struct line_maps *set, enum lc_reason reason,
74 unsigned int sysp, unsigned int from_line,
75 const char *to_file, unsigned int to_line)
77 struct line_map *map;
79 if (set->used && from_line < set->maps[set->used - 1].from_line)
80 abort ();
82 if (set->used == set->allocated)
84 set->allocated = 2 * set->allocated + 256;
85 set->maps = (struct line_map *)
86 xrealloc (set->maps, set->allocated * sizeof (struct line_map));
89 map = &set->maps[set->used++];
91 /* If we don't keep our line maps consistent, we can easily
92 segfault. Don't rely on the client to do it for us. */
93 if (set->depth == 0)
94 reason = LC_ENTER;
95 else if (reason == LC_LEAVE)
97 struct line_map *from;
98 bool error;
100 if (MAIN_FILE_P (map - 1))
102 set->depth--;
103 set->used--;
104 return NULL;
106 else
108 from = INCLUDED_FROM (set, map - 1);
109 error = to_file && strcmp (from->to_file, to_file);
112 /* Depending upon whether we are handling preprocessed input or
113 not, this can be a user error or an ICE. */
114 if (error)
115 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
116 to_file);
118 /* A TO_FILE of NULL is special - we use the natural values. */
119 if (error || to_file == NULL)
121 to_file = from->to_file;
122 to_line = LAST_SOURCE_LINE (from) + 1;
123 sysp = from->sysp;
127 map->reason = reason;
128 map->sysp = sysp;
129 map->from_line = from_line;
130 map->to_file = to_file;
131 map->to_line = to_line;
133 if (reason == LC_ENTER)
135 map->included_from = set->depth == 0 ? -1 : (int) (set->used - 2);
136 set->depth++;
137 if (set->trace_includes)
138 trace_include (set, map);
140 else if (reason == LC_RENAME)
141 map->included_from = map[-1].included_from;
142 else if (reason == LC_LEAVE)
144 set->depth--;
145 map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
148 return map;
151 /* Given a logical line, returns the map from which the corresponding
152 (source file, line) pair can be deduced. Since the set is built
153 chronologically, the logical lines are monotonic increasing, and so
154 the list is sorted and we can use a binary search. */
156 const struct line_map *
157 lookup_line (struct line_maps *set, unsigned int line)
159 unsigned int md, mn = 0, mx = set->used;
161 if (mx == 0)
162 abort ();
164 while (mx - mn > 1)
166 md = (mn + mx) / 2;
167 if (set->maps[md].from_line > line)
168 mx = md;
169 else
170 mn = md;
173 return &set->maps[mn];
176 /* Print the file names and line numbers of the #include commands
177 which led to the map MAP, if any, to stderr. Nothing is output if
178 the most recently listed stack is the same as the current one. */
180 void
181 print_containing_files (struct line_maps *set, const struct line_map *map)
183 if (MAIN_FILE_P (map) || set->last_listed == map->included_from)
184 return;
186 set->last_listed = map->included_from;
187 map = INCLUDED_FROM (set, map);
189 fprintf (stderr, _("In file included from %s:%u"),
190 map->to_file, LAST_SOURCE_LINE (map));
192 while (! MAIN_FILE_P (map))
194 map = INCLUDED_FROM (set, map);
195 /* Translators note: this message is used in conjunction
196 with "In file included from %s:%ld" and some other
197 tricks. We want something like this:
199 | In file included from sys/select.h:123,
200 | from sys/types.h:234,
201 | from userfile.c:31:
202 | bits/select.h:45: <error message here>
204 with all the "from"s lined up.
205 The trailing comma is at the beginning of this message,
206 and the trailing colon is not translated. */
207 fprintf (stderr, _(",\n from %s:%u"),
208 map->to_file, LAST_SOURCE_LINE (map));
211 fputs (":\n", stderr);
214 /* Print an include trace, for e.g. the -H option of the preprocessor. */
216 static void
217 trace_include (const struct line_maps *set, const struct line_map *map)
219 unsigned int i = set->depth;
221 while (--i)
222 putc ('.', stderr);
223 fprintf (stderr, " %s\n", map->to_file);