]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcpp/line-map.c
* include/debug/formater.h: Include bits/c++config.h.
[thirdparty/gcc.git] / libcpp / line-map.c
CommitLineData
38692459 1/* Map logical line numbers to (source file, line number) pairs.
7f5f3953 2 Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009
38692459 3 Free Software Foundation, Inc.
4
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
6bc9506f 7Free Software Foundation; either version 3, or (at your option) any
38692459 8later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
6bc9506f 16along with this program; see the file COPYING3. If not see
17<http://www.gnu.org/licenses/>.
38692459 18
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! */
22
23#include "config.h"
24#include "system.h"
25#include "line-map.h"
26
6d0a3bf2 27static void trace_include (const struct line_maps *, const struct line_map *);
438ac94c 28
38692459 29/* Initialize a line map set. */
30
31void
196ce2be 32linemap_init (struct line_maps *set)
38692459 33{
088db31b 34 set->maps = NULL;
38692459 35 set->allocated = 0;
36 set->used = 0;
bd507c05 37 set->last_listed = -1;
438ac94c 38 set->trace_includes = false;
4087823d 39 set->depth = 0;
088db31b 40 set->cache = 0;
610625e3 41 set->highest_location = 0;
dbddc569 42 set->highest_line = 0;
610625e3 43 set->max_column_hint = 0;
102a7306 44 set->reallocator = 0;
38692459 45}
46
7c2df241 47/* Check for and warn about line_maps entered but not exited. */
610625e3 48
49void
50linemap_check_files_exited (struct line_maps *set)
51{
52 struct line_map *map;
53 /* Depending upon whether we are handling preprocessed input or
54 not, this can be a user error or an ICE. */
55 for (map = &set->maps[set->used - 1]; ! MAIN_FILE_P (map);
56 map = INCLUDED_FROM (set, map))
57 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
58 map->to_file);
59}
60
38692459 61/* Free a line map set. */
62
bd507c05 63void
196ce2be 64linemap_free (struct line_maps *set)
38692459 65{
66 if (set->maps)
bd507c05 67 {
610625e3 68 linemap_check_files_exited (set);
f85fcf2b 69
bd507c05 70 free (set->maps);
71 }
38692459 72}
73
74/* Add a mapping of logical source line to physical source file and
748b50a3 75 line number.
76
77 The text pointed to by TO_FILE must have a lifetime
78 at least as long as the final call to lookup_line (). An empty
79 TO_FILE means standard input. If reason is LC_LEAVE, and
80 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
81 natural values considering the file we are returning to.
38692459 82
83 FROM_LINE should be monotonic increasing across calls to this
748b50a3 84 function. A call to this function can relocate the previous set of
85 maps, so any stored line_map pointers should not be used. */
38692459 86
f85fcf2b 87const struct line_map *
196ce2be 88linemap_add (struct line_maps *set, enum lc_reason reason,
4999c35b 89 unsigned int sysp, const char *to_file, linenum_type to_line)
38692459 90{
91 struct line_map *map;
610625e3 92 source_location start_location = set->highest_location + 1;
38692459 93
610625e3 94 if (set->used && start_location < set->maps[set->used - 1].start_location)
38692459 95 abort ();
96
97 if (set->used == set->allocated)
98 {
931b0a0f 99 line_map_realloc reallocator
100 = set->reallocator ? set->reallocator : xrealloc;
38692459 101 set->allocated = 2 * set->allocated + 256;
931b0a0f 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)));
38692459 108 }
109
088db31b 110 map = &set->maps[set->used];
38692459 111
748b50a3 112 if (to_file && *to_file == '\0')
113 to_file = "<stdin>";
114
bd507c05 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. */
4087823d 117 if (set->depth == 0)
bd507c05 118 reason = LC_ENTER;
119 else if (reason == LC_LEAVE)
120 {
f85fcf2b 121 struct line_map *from;
122 bool error;
123
124 if (MAIN_FILE_P (map - 1))
bd507c05 125 {
4d1574ae 126 if (to_file == NULL)
127 {
128 set->depth--;
4d1574ae 129 return NULL;
130 }
131 error = true;
132 reason = LC_RENAME;
133 from = map - 1;
f85fcf2b 134 }
135 else
136 {
137 from = INCLUDED_FROM (set, map - 1);
138 error = to_file && strcmp (from->to_file, to_file);
139 }
140
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);
146
147 /* A TO_FILE of NULL is special - we use the natural values. */
148 if (error || to_file == NULL)
149 {
150 to_file = from->to_file;
610625e3 151 to_line = SOURCE_LINE (from, from[1].start_location);
f85fcf2b 152 sysp = from->sysp;
bd507c05 153 }
154 }
155
f85fcf2b 156 map->reason = reason;
157 map->sysp = sysp;
610625e3 158 map->start_location = start_location;
f85fcf2b 159 map->to_file = to_file;
160 map->to_line = to_line;
088db31b 161 set->cache = set->used++;
610625e3 162 map->column_bits = 0;
163 set->highest_location = start_location;
dbddc569 164 set->highest_line = start_location;
610625e3 165 set->max_column_hint = 0;
f85fcf2b 166
bd507c05 167 if (reason == LC_ENTER)
4087823d 168 {
e9f0d687 169 map->included_from = set->depth == 0 ? -1 : (int) (set->used - 2);
4087823d 170 set->depth++;
4087823d 171 if (set->trace_includes)
172 trace_include (set, map);
173 }
38692459 174 else if (reason == LC_RENAME)
175 map->included_from = map[-1].included_from;
176 else if (reason == LC_LEAVE)
4087823d 177 {
178 set->depth--;
179 map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
180 }
438ac94c 181
38692459 182 return map;
183}
184
610625e3 185source_location
4999c35b 186linemap_line_start (struct line_maps *set, linenum_type to_line,
610625e3 187 unsigned int max_column_hint)
188{
189 struct line_map *map = &set->maps[set->used - 1];
190 source_location highest = set->highest_location;
191 source_location r;
4999c35b 192 linenum_type last_line = SOURCE_LINE (map, set->highest_line);
610625e3 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))
199 {
200 add_map = true;
201 }
202 else
203 max_column_hint = set->max_column_hint;
204 if (add_map)
205 {
206 int column_bits;
dbddc569 207 if (max_column_hint > 100000 || highest > 0xC0000000)
610625e3 208 {
0b7f838f 209 /* If the column number is ridiculous or we've allocated a huge
210 number of source_locations, give up on column numbers. */
610625e3 211 max_column_hint = 0;
212 if (highest >0xF0000000)
213 return 0;
214 column_bits = 0;
215 }
216 else
217 {
218 column_bits = 7;
219 while (max_column_hint >= (1U << column_bits))
220 column_bits++;
221 max_column_hint = 1U << column_bits;
222 }
0b7f838f 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. */
610625e3 225 if (line_delta < 0
226 || last_line != map->to_line
227 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
e297899b 228 map = (struct line_map *) linemap_add (set, LC_RENAME, map->sysp,
229 map->to_file, to_line);
610625e3 230 map->column_bits = column_bits;
0b7f838f 231 r = map->start_location + ((to_line - map->to_line) << column_bits);
610625e3 232 }
233 else
234 r = highest - SOURCE_COLUMN (map, highest)
235 + (line_delta << map->column_bits);
dbddc569 236 set->highest_line = r;
610625e3 237 if (r > set->highest_location)
238 set->highest_location = r;
239 set->max_column_hint = max_column_hint;
240 return r;
241}
242
dbddc569 243source_location
244linemap_position_for_column (struct line_maps *set, unsigned int to_column)
245{
246 source_location r = set->highest_line;
247 if (to_column >= set->max_column_hint)
248 {
249 if (r >= 0xC000000 || to_column > 100000)
250 {
251 /* Running low on source_locations - disable column numbers. */
252 return r;
253 }
254 else
255 {
256 struct line_map *map = &set->maps[set->used - 1];
257 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
258 }
259 }
260 r = r + to_column;
261 if (r >= set->highest_location)
262 set->highest_location = r;
263 return r;
264}
265
a98af3e0 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. */
38692459 270
f85fcf2b 271const struct line_map *
fd3638df 272linemap_lookup (struct line_maps *set, source_location line)
38692459 273{
088db31b 274 unsigned int md, mn, mx;
275 const struct line_map *cached;
276
277 mn = set->cache;
278 mx = set->used;
279
280 cached = &set->maps[mn];
7c2df241 281 /* We should get a segfault if no line_maps have been added yet. */
610625e3 282 if (line >= cached->start_location)
088db31b 283 {
610625e3 284 if (mn + 1 == mx || line < cached[1].start_location)
088db31b 285 return cached;
286 }
287 else
288 {
289 mx = mn;
290 mn = 0;
291 }
38692459 292
293 while (mx - mn > 1)
294 {
295 md = (mn + mx) / 2;
610625e3 296 if (set->maps[md].start_location > line)
38692459 297 mx = md;
298 else
299 mn = md;
300 }
301
088db31b 302 set->cache = mn;
38692459 303 return &set->maps[mn];
304}
bd507c05 305
438ac94c 306/* Print an include trace, for e.g. the -H option of the preprocessor. */
307
308static void
6d0a3bf2 309trace_include (const struct line_maps *set, const struct line_map *map)
438ac94c 310{
4087823d 311 unsigned int i = set->depth;
438ac94c 312
4087823d 313 while (--i)
438ac94c 314 putc ('.', stderr);
315 fprintf (stderr, " %s\n", map->to_file);
316}