1 /* Map logical line numbers to (source file, line number) pairs.
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
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! */
28 /* Initialize a line map set. */
32 struct line_maps *set;
37 set->last_listed = -1;
40 /* Free a line map set. */
44 struct line_maps *set;
50 /* Depending upon whether we are handling preprocessed input or
51 not, this can be a user error or an ICE. */
52 for (map = CURRENT_LINE_MAP (set); ! MAIN_FILE_P (map);
53 map = INCLUDED_FROM (set, map))
54 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
61 /* Add a mapping of logical source line to physical source file and
62 line number. Ther text pointed to by TO_FILE must have a lifetime
63 at least as long as the final call to lookup_line ().
65 FROM_LINE should be monotonic increasing across calls to this
68 const struct line_map *
69 add_line_map (set, reason, sysp, from_line, to_file, to_line)
70 struct line_maps *set;
71 enum lc_reason reason;
73 unsigned int from_line;
79 if (set->used && from_line < set->maps[set->used - 1].from_line)
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. */
95 else if (reason == LC_LEAVE)
97 struct line_map *from;
100 if (MAIN_FILE_P (map - 1))
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. */
115 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
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;
127 map->reason = reason;
129 map->from_line = from_line;
130 map->to_file = to_file;
131 map->to_line = to_line;
133 if (reason == LC_ENTER)
134 map->included_from = set->used - 1;
135 else if (reason == LC_RENAME)
136 map->included_from = map[-1].included_from;
137 else if (reason == LC_LEAVE)
138 map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
144 /* Given a logical line, returns the map from which the corresponding
145 (source file, line) pair can be deduced. Since the set is built
146 chronologically, the logical lines are monotonic increasing, and so
147 the list is sorted and we can use a binary search. */
149 const struct line_map *
150 lookup_line (set, line)
151 struct line_maps *set;
154 unsigned int md, mn = 0, mx = set->used;
162 if (set->maps[md].from_line > line)
168 return &set->maps[mn];
171 /* Print the file names and line numbers of the #include commands
172 which led to the map MAP, if any, to stderr. Nothing is output if
173 the most recently listed stack is the same as the current one. */
176 print_containing_files (set, map)
177 struct line_maps *set;
178 const struct line_map *map;
180 if (MAIN_FILE_P (map) || set->last_listed == map->included_from)
183 set->last_listed = map->included_from;
184 map = INCLUDED_FROM (set, map);
186 fprintf (stderr, _("In file included from %s:%u"),
187 map->to_file, LAST_SOURCE_LINE (map));
189 while (! MAIN_FILE_P (map))
191 map = INCLUDED_FROM (set, map);
192 /* Translators note: this message is used in conjunction
193 with "In file included from %s:%ld" and some other
194 tricks. We want something like this:
196 | In file included from sys/select.h:123,
197 | from sys/types.h:234,
198 | from userfile.c:31:
199 | bits/select.h:45: <error message here>
201 with all the "from"s lined up.
202 The trailing comma is at the beginning of this message,
203 and the trailing colon is not translated. */
204 fprintf (stderr, _(",\n from %s:%u"),
205 map->to_file, LAST_SOURCE_LINE (map));
208 fputs (":\n", stderr);