]>
Commit | Line | Data |
---|---|---|
c87b25e6 | 1 | /* Map (unsigned int) keys to (source file, line, column) triples. |
818ab71a | 2 | Copyright (C) 2001-2016 Free Software Foundation, Inc. |
d82fc108 NB |
3 | |
4 | This program is free software; you can redistribute it and/or modify it | |
5 | under the terms of the GNU General Public License as published by the | |
748086b7 | 6 | Free Software Foundation; either version 3, or (at your option) any |
d82fc108 NB |
7 | later version. |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
748086b7 JJ |
15 | along with this program; see the file COPYING3. If not see |
16 | <http://www.gnu.org/licenses/>. | |
d82fc108 NB |
17 | |
18 | In other words, you are welcome to use, share and improve this program. | |
19 | You are forbidden to forbid anyone else to use, share and improve | |
20 | what you give them. Help stamp out software-hoarding! */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "line-map.h" | |
46427374 TT |
25 | #include "cpplib.h" |
26 | #include "internal.h" | |
5368224f | 27 | #include "hashtab.h" |
d82fc108 | 28 | |
815facd3 | 29 | /* Do not track column numbers higher than this one. As a result, the |
ebedc9a3 | 30 | range of column_bits is [12, 18] (or 0 if column numbers are |
815facd3 | 31 | disabled). */ |
ebedc9a3 | 32 | const unsigned int LINE_MAP_MAX_COLUMN_NUMBER = (1U << 12); |
815facd3 | 33 | |
815facd3 MLI |
34 | /* Highest possible source location encoded within an ordinary or |
35 | macro map. */ | |
36 | const source_location LINE_MAP_MAX_SOURCE_LOCATION = 0x70000000; | |
37 | ||
0e50b624 DM |
38 | static void trace_include (const struct line_maps *, const line_map_ordinary *); |
39 | static const line_map_ordinary * linemap_ordinary_map_lookup (struct line_maps *, | |
40 | source_location); | |
41 | static const line_map_macro* linemap_macro_map_lookup (struct line_maps *, | |
42 | source_location); | |
46427374 | 43 | static source_location linemap_macro_map_loc_to_def_point |
0e50b624 | 44 | (const line_map_macro *, source_location); |
46427374 | 45 | static source_location linemap_macro_map_loc_to_exp_point |
0e50b624 | 46 | (const line_map_macro *, source_location); |
46427374 | 47 | static source_location linemap_macro_loc_to_spelling_point |
0e50b624 | 48 | (struct line_maps *, source_location, const line_map_ordinary **); |
46427374 TT |
49 | static source_location linemap_macro_loc_to_def_point (struct line_maps *, |
50 | source_location, | |
0e50b624 | 51 | const line_map_ordinary **); |
46427374 TT |
52 | static source_location linemap_macro_loc_to_exp_point (struct line_maps *, |
53 | source_location, | |
0e50b624 | 54 | const line_map_ordinary **); |
5993019d | 55 | |
64a1a422 TT |
56 | /* Counters defined in macro.c. */ |
57 | extern unsigned num_expanded_macros_counter; | |
58 | extern unsigned num_macro_tokens_counter; | |
59 | ||
5368224f DC |
60 | /* Hash function for location_adhoc_data hashtable. */ |
61 | ||
62 | static hashval_t | |
63 | location_adhoc_data_hash (const void *l) | |
64 | { | |
65 | const struct location_adhoc_data *lb = | |
66 | (const struct location_adhoc_data *) l; | |
ebedc9a3 DM |
67 | return ((hashval_t) lb->locus |
68 | + (hashval_t) lb->src_range.m_start | |
69 | + (hashval_t) lb->src_range.m_finish | |
70 | + (size_t) lb->data); | |
5368224f DC |
71 | } |
72 | ||
73 | /* Compare function for location_adhoc_data hashtable. */ | |
74 | ||
75 | static int | |
76 | location_adhoc_data_eq (const void *l1, const void *l2) | |
77 | { | |
78 | const struct location_adhoc_data *lb1 = | |
79 | (const struct location_adhoc_data *) l1; | |
80 | const struct location_adhoc_data *lb2 = | |
81 | (const struct location_adhoc_data *) l2; | |
ebedc9a3 DM |
82 | return (lb1->locus == lb2->locus |
83 | && lb1->src_range.m_start == lb2->src_range.m_start | |
84 | && lb1->src_range.m_finish == lb2->src_range.m_finish | |
85 | && lb1->data == lb2->data); | |
5368224f DC |
86 | } |
87 | ||
88 | /* Update the hashtable when location_adhoc_data is reallocated. */ | |
89 | ||
90 | static int | |
91 | location_adhoc_data_update (void **slot, void *data) | |
92 | { | |
fe55692c | 93 | *((char **) slot) += *((int64_t *) data); |
5368224f DC |
94 | return 1; |
95 | } | |
96 | ||
52187008 DC |
97 | /* Rebuild the hash table from the location adhoc data. */ |
98 | ||
99 | void | |
100 | rebuild_location_adhoc_htab (struct line_maps *set) | |
101 | { | |
102 | unsigned i; | |
103 | set->location_adhoc_data_map.htab = | |
104 | htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL); | |
105 | for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++) | |
106 | htab_find_slot (set->location_adhoc_data_map.htab, | |
107 | set->location_adhoc_data_map.data + i, INSERT); | |
108 | } | |
109 | ||
ebedc9a3 DM |
110 | /* Helper function for get_combined_adhoc_loc. |
111 | Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly | |
112 | within a source_location, without needing to use an ad-hoc location. */ | |
113 | ||
114 | static bool | |
115 | can_be_stored_compactly_p (struct line_maps *set, | |
116 | source_location locus, | |
117 | source_range src_range, | |
118 | void *data) | |
119 | { | |
120 | /* If there's an ad-hoc pointer, we can't store it directly in the | |
121 | source_location, we need the lookaside. */ | |
122 | if (data) | |
123 | return false; | |
124 | ||
125 | /* We only store ranges that begin at the locus and that are sufficiently | |
126 | "sane". */ | |
127 | if (src_range.m_start != locus) | |
128 | return false; | |
129 | ||
130 | if (src_range.m_finish < src_range.m_start) | |
131 | return false; | |
132 | ||
133 | if (src_range.m_start < RESERVED_LOCATION_COUNT) | |
134 | return false; | |
135 | ||
c7df95d8 | 136 | if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES) |
ebedc9a3 DM |
137 | return false; |
138 | ||
139 | /* All 3 locations must be within ordinary maps, typically, the same | |
140 | ordinary map. */ | |
141 | source_location lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set); | |
142 | if (locus >= lowest_macro_loc) | |
143 | return false; | |
144 | if (src_range.m_start >= lowest_macro_loc) | |
145 | return false; | |
146 | if (src_range.m_finish >= lowest_macro_loc) | |
147 | return false; | |
148 | ||
149 | /* Passed all tests. */ | |
150 | return true; | |
151 | } | |
152 | ||
5368224f DC |
153 | /* Combine LOCUS and DATA to a combined adhoc loc. */ |
154 | ||
155 | source_location | |
156 | get_combined_adhoc_loc (struct line_maps *set, | |
ebedc9a3 DM |
157 | source_location locus, |
158 | source_range src_range, | |
159 | void *data) | |
5368224f DC |
160 | { |
161 | struct location_adhoc_data lb; | |
162 | struct location_adhoc_data **slot; | |
163 | ||
5368224f | 164 | if (IS_ADHOC_LOC (locus)) |
acf601ae JJ |
165 | locus |
166 | = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus; | |
5368224f DC |
167 | if (locus == 0 && data == NULL) |
168 | return 0; | |
ebedc9a3 DM |
169 | |
170 | /* Any ordinary locations ought to be "pure" at this point: no | |
171 | compressed ranges. */ | |
172 | linemap_assert (locus < RESERVED_LOCATION_COUNT | |
c7df95d8 | 173 | || locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES |
ebedc9a3 DM |
174 | || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set) |
175 | || pure_location_p (set, locus)); | |
176 | ||
177 | /* Consider short-range optimization. */ | |
178 | if (can_be_stored_compactly_p (set, locus, src_range, data)) | |
179 | { | |
180 | /* The low bits ought to be clear. */ | |
181 | linemap_assert (pure_location_p (set, locus)); | |
182 | const line_map *map = linemap_lookup (set, locus); | |
183 | const line_map_ordinary *ordmap = linemap_check_ordinary (map); | |
184 | unsigned int int_diff = src_range.m_finish - src_range.m_start; | |
185 | unsigned int col_diff = (int_diff >> ordmap->m_range_bits); | |
186 | if (col_diff < (1U << ordmap->m_range_bits)) | |
187 | { | |
188 | source_location packed = locus | col_diff; | |
189 | set->num_optimized_ranges++; | |
190 | return packed; | |
191 | } | |
192 | } | |
193 | ||
329590d7 | 194 | /* We can also compactly store locations |
ebedc9a3 | 195 | when locus == start == finish (and data is NULL). */ |
329590d7 | 196 | if (locus == src_range.m_start |
ebedc9a3 DM |
197 | && locus == src_range.m_finish |
198 | && !data) | |
199 | return locus; | |
200 | ||
201 | if (!data) | |
202 | set->num_unoptimized_ranges++; | |
203 | ||
5368224f | 204 | lb.locus = locus; |
ebedc9a3 | 205 | lb.src_range = src_range; |
5368224f DC |
206 | lb.data = data; |
207 | slot = (struct location_adhoc_data **) | |
208 | htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT); | |
209 | if (*slot == NULL) | |
210 | { | |
211 | if (set->location_adhoc_data_map.curr_loc >= | |
212 | set->location_adhoc_data_map.allocated) | |
213 | { | |
214 | char *orig_data = (char *) set->location_adhoc_data_map.data; | |
fe55692c | 215 | int64_t offset; |
8ac16127 MG |
216 | /* Cast away extern "C" from the type of xrealloc. */ |
217 | line_map_realloc reallocator = (set->reallocator | |
218 | ? set->reallocator | |
219 | : (line_map_realloc) xrealloc); | |
52187008 DC |
220 | |
221 | if (set->location_adhoc_data_map.allocated == 0) | |
222 | set->location_adhoc_data_map.allocated = 128; | |
223 | else | |
224 | set->location_adhoc_data_map.allocated *= 2; | |
225 | set->location_adhoc_data_map.data = (struct location_adhoc_data *) | |
226 | reallocator (set->location_adhoc_data_map.data, | |
227 | set->location_adhoc_data_map.allocated | |
228 | * sizeof (struct location_adhoc_data)); | |
5368224f | 229 | offset = (char *) (set->location_adhoc_data_map.data) - orig_data; |
52187008 DC |
230 | if (set->location_adhoc_data_map.allocated > 128) |
231 | htab_traverse (set->location_adhoc_data_map.htab, | |
232 | location_adhoc_data_update, &offset); | |
5368224f DC |
233 | } |
234 | *slot = set->location_adhoc_data_map.data | |
235 | + set->location_adhoc_data_map.curr_loc; | |
acf601ae JJ |
236 | set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++] |
237 | = lb; | |
5368224f DC |
238 | } |
239 | return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000; | |
240 | } | |
241 | ||
242 | /* Return the data for the adhoc loc. */ | |
243 | ||
244 | void * | |
245 | get_data_from_adhoc_loc (struct line_maps *set, source_location loc) | |
246 | { | |
247 | linemap_assert (IS_ADHOC_LOC (loc)); | |
248 | return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data; | |
249 | } | |
250 | ||
251 | /* Return the location for the adhoc loc. */ | |
252 | ||
253 | source_location | |
254 | get_location_from_adhoc_loc (struct line_maps *set, source_location loc) | |
255 | { | |
256 | linemap_assert (IS_ADHOC_LOC (loc)); | |
257 | return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
258 | } | |
259 | ||
ebedc9a3 DM |
260 | /* Return the source_range for adhoc location LOC. */ |
261 | ||
262 | static source_range | |
263 | get_range_from_adhoc_loc (struct line_maps *set, source_location loc) | |
264 | { | |
265 | linemap_assert (IS_ADHOC_LOC (loc)); | |
266 | return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].src_range; | |
267 | } | |
268 | ||
269 | /* Get the source_range of location LOC, either from the ad-hoc | |
270 | lookaside table, or embedded inside LOC itself. */ | |
271 | ||
272 | source_range | |
273 | get_range_from_loc (struct line_maps *set, | |
274 | source_location loc) | |
275 | { | |
276 | if (IS_ADHOC_LOC (loc)) | |
277 | return get_range_from_adhoc_loc (set, loc); | |
278 | ||
279 | /* For ordinary maps, extract packed range. */ | |
280 | if (loc >= RESERVED_LOCATION_COUNT | |
281 | && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set) | |
c7df95d8 | 282 | && loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES) |
ebedc9a3 DM |
283 | { |
284 | const line_map *map = linemap_lookup (set, loc); | |
285 | const line_map_ordinary *ordmap = linemap_check_ordinary (map); | |
286 | source_range result; | |
287 | int offset = loc & ((1 << ordmap->m_range_bits) - 1); | |
288 | result.m_start = loc - offset; | |
289 | result.m_finish = result.m_start + (offset << ordmap->m_range_bits); | |
290 | return result; | |
291 | } | |
292 | ||
293 | return source_range::from_location (loc); | |
294 | } | |
295 | ||
296 | /* Get whether location LOC is a "pure" location, or | |
297 | whether it is an ad-hoc location, or embeds range information. */ | |
298 | ||
299 | bool | |
300 | pure_location_p (line_maps *set, source_location loc) | |
301 | { | |
302 | if (IS_ADHOC_LOC (loc)) | |
303 | return false; | |
304 | ||
305 | const line_map *map = linemap_lookup (set, loc); | |
306 | const line_map_ordinary *ordmap = linemap_check_ordinary (map); | |
307 | ||
308 | if (loc & ((1U << ordmap->m_range_bits) - 1)) | |
309 | return false; | |
310 | ||
311 | return true; | |
312 | } | |
313 | ||
2aa51413 DM |
314 | /* Given location LOC within SET, strip away any packed range information |
315 | or ad-hoc information. */ | |
316 | ||
317 | source_location | |
318 | get_pure_location (line_maps *set, source_location loc) | |
319 | { | |
320 | if (IS_ADHOC_LOC (loc)) | |
321 | loc | |
322 | = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
323 | ||
324 | if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (set)) | |
325 | return loc; | |
326 | ||
327 | if (loc < RESERVED_LOCATION_COUNT) | |
328 | return loc; | |
329 | ||
330 | const line_map *map = linemap_lookup (set, loc); | |
331 | const line_map_ordinary *ordmap = linemap_check_ordinary (map); | |
332 | ||
333 | return loc & ~((1 << ordmap->m_range_bits) - 1); | |
334 | } | |
335 | ||
5368224f DC |
336 | /* Finalize the location_adhoc_data structure. */ |
337 | void | |
338 | location_adhoc_data_fini (struct line_maps *set) | |
339 | { | |
5368224f DC |
340 | htab_delete (set->location_adhoc_data_map.htab); |
341 | } | |
342 | ||
d82fc108 NB |
343 | /* Initialize a line map set. */ |
344 | ||
345 | void | |
c468587a DS |
346 | linemap_init (struct line_maps *set, |
347 | source_location builtin_location) | |
d82fc108 | 348 | { |
46427374 | 349 | memset (set, 0, sizeof (struct line_maps)); |
96c169e1 JJ |
350 | set->highest_location = RESERVED_LOCATION_COUNT - 1; |
351 | set->highest_line = RESERVED_LOCATION_COUNT - 1; | |
52187008 DC |
352 | set->location_adhoc_data_map.htab = |
353 | htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL); | |
c468587a | 354 | set->builtin_location = builtin_location; |
d82fc108 NB |
355 | } |
356 | ||
9ac97460 | 357 | /* Check for and warn about line_maps entered but not exited. */ |
12f9df4e PB |
358 | |
359 | void | |
360 | linemap_check_files_exited (struct line_maps *set) | |
361 | { | |
0e50b624 | 362 | const line_map_ordinary *map; |
12f9df4e PB |
363 | /* Depending upon whether we are handling preprocessed input or |
364 | not, this can be a user error or an ICE. */ | |
46427374 TT |
365 | for (map = LINEMAPS_LAST_ORDINARY_MAP (set); |
366 | ! MAIN_FILE_P (map); | |
12f9df4e PB |
367 | map = INCLUDED_FROM (set, map)) |
368 | fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n", | |
46427374 | 369 | ORDINARY_MAP_FILE_NAME (map)); |
12f9df4e | 370 | } |
d82fc108 | 371 | |
46427374 TT |
372 | /* Create a new line map in the line map set SET, and return it. |
373 | REASON is the reason of creating the map. It determines the type | |
374 | of map created (ordinary or macro map). Note that ordinary maps and | |
375 | macro maps are allocated in different memory location. */ | |
376 | ||
377 | static struct line_map * | |
378 | new_linemap (struct line_maps *set, | |
379 | enum lc_reason reason) | |
d82fc108 | 380 | { |
46427374 TT |
381 | /* Depending on this variable, a macro map would be allocated in a |
382 | different memory location than an ordinary map. */ | |
383 | bool macro_map_p = (reason == LC_ENTER_MACRO); | |
384 | struct line_map *result; | |
385 | ||
386 | if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p)) | |
fde84349 | 387 | { |
46427374 | 388 | /* We ran out of allocated line maps. Let's allocate more. */ |
64567cfd | 389 | size_t alloc_size; |
47d89cf3 | 390 | |
8ac16127 MG |
391 | /* Cast away extern "C" from the type of xrealloc. */ |
392 | line_map_realloc reallocator = (set->reallocator | |
393 | ? set->reallocator | |
394 | : (line_map_realloc) xrealloc); | |
b9bd6f74 TT |
395 | line_map_round_alloc_size_func round_alloc_size = |
396 | set->round_alloc_size; | |
397 | ||
0e50b624 DM |
398 | size_t map_size = (macro_map_p |
399 | ? sizeof (line_map_macro) | |
400 | : sizeof (line_map_ordinary)); | |
401 | ||
b9bd6f74 TT |
402 | /* We are going to execute some dance to try to reduce the |
403 | overhead of the memory allocator, in case we are using the | |
404 | ggc-page.c one. | |
405 | ||
406 | The actual size of memory we are going to get back from the | |
407 | allocator is the smallest power of 2 that is greater than the | |
408 | size we requested. So let's consider that size then. */ | |
409 | ||
410 | alloc_size = | |
411 | (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256) | |
0e50b624 | 412 | * map_size; |
b9bd6f74 TT |
413 | |
414 | /* Get the actual size of memory that is going to be allocated | |
415 | by the allocator. */ | |
416 | alloc_size = round_alloc_size (alloc_size); | |
417 | ||
418 | /* Now alloc_size contains the exact memory size we would get if | |
419 | we have asked for the initial alloc_size amount of memory. | |
420 | Let's get back to the number of macro map that amounts | |
421 | to. */ | |
46427374 | 422 | LINEMAPS_ALLOCATED (set, macro_map_p) = |
0e50b624 | 423 | alloc_size / map_size; |
b9bd6f74 TT |
424 | |
425 | /* And now let's really do the re-allocation. */ | |
0e50b624 DM |
426 | if (macro_map_p) |
427 | { | |
428 | set->info_macro.maps | |
429 | = (line_map_macro *) (*reallocator) (set->info_macro.maps, | |
430 | (LINEMAPS_ALLOCATED (set, macro_map_p) | |
431 | * map_size)); | |
432 | result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)]; | |
433 | } | |
434 | else | |
435 | { | |
436 | set->info_ordinary.maps = | |
437 | (line_map_ordinary *) (*reallocator) (set->info_ordinary.maps, | |
438 | (LINEMAPS_ALLOCATED (set, macro_map_p) | |
439 | * map_size)); | |
440 | result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)]; | |
441 | } | |
46427374 TT |
442 | memset (result, 0, |
443 | ((LINEMAPS_ALLOCATED (set, macro_map_p) | |
444 | - LINEMAPS_USED (set, macro_map_p)) | |
0e50b624 | 445 | * map_size)); |
fde84349 | 446 | } |
46427374 | 447 | else |
0e50b624 DM |
448 | { |
449 | if (macro_map_p) | |
450 | result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)]; | |
451 | else | |
452 | result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)]; | |
453 | } | |
46427374 TT |
454 | |
455 | LINEMAPS_USED (set, macro_map_p)++; | |
456 | ||
457 | result->reason = reason; | |
458 | return result; | |
d82fc108 NB |
459 | } |
460 | ||
461 | /* Add a mapping of logical source line to physical source file and | |
9074464c GK |
462 | line number. |
463 | ||
464 | The text pointed to by TO_FILE must have a lifetime | |
465 | at least as long as the final call to lookup_line (). An empty | |
466 | TO_FILE means standard input. If reason is LC_LEAVE, and | |
467 | TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their | |
468 | natural values considering the file we are returning to. | |
d82fc108 NB |
469 | |
470 | FROM_LINE should be monotonic increasing across calls to this | |
9074464c GK |
471 | function. A call to this function can relocate the previous set of |
472 | maps, so any stored line_map pointers should not be used. */ | |
d82fc108 | 473 | |
47d89cf3 | 474 | const struct line_map * |
a2f7be91 | 475 | linemap_add (struct line_maps *set, enum lc_reason reason, |
1bb64668 | 476 | unsigned int sysp, const char *to_file, linenum_type to_line) |
d82fc108 | 477 | { |
ebedc9a3 DM |
478 | /* Generate a start_location above the current highest_location. |
479 | If possible, make the low range bits be zero. */ | |
480 | source_location start_location; | |
481 | if (set->highest_location < LINE_MAP_MAX_LOCATION_WITH_COLS) | |
482 | { | |
483 | start_location = set->highest_location + (1 << set->default_range_bits); | |
484 | if (set->default_range_bits) | |
485 | start_location &= ~((1 << set->default_range_bits) - 1); | |
486 | linemap_assert (0 == (start_location | |
487 | & ((1 << set->default_range_bits) - 1))); | |
488 | } | |
489 | else | |
490 | start_location = set->highest_location + 1; | |
d82fc108 | 491 | |
46427374 TT |
492 | linemap_assert (!(LINEMAPS_ORDINARY_USED (set) |
493 | && (start_location | |
494 | < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set))))); | |
495 | ||
496 | /* When we enter the file for the first time reason cannot be | |
497 | LC_RENAME. */ | |
498 | linemap_assert (!(set->depth == 0 && reason == LC_RENAME)); | |
d82fc108 | 499 | |
46427374 TT |
500 | /* If we are leaving the main file, return a NULL map. */ |
501 | if (reason == LC_LEAVE | |
502 | && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set)) | |
503 | && to_file == NULL) | |
d82fc108 | 504 | { |
46427374 TT |
505 | set->depth--; |
506 | return NULL; | |
d82fc108 NB |
507 | } |
508 | ||
0e50b624 DM |
509 | linemap_assert (reason != LC_ENTER_MACRO); |
510 | line_map_ordinary *map = linemap_check_ordinary (new_linemap (set, reason)); | |
d82fc108 | 511 | |
c7f9c0b9 | 512 | if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM) |
9074464c GK |
513 | to_file = "<stdin>"; |
514 | ||
c7f9c0b9 JM |
515 | if (reason == LC_RENAME_VERBATIM) |
516 | reason = LC_RENAME; | |
517 | ||
892a371f | 518 | if (reason == LC_LEAVE) |
fde84349 | 519 | { |
46427374 TT |
520 | /* When we are just leaving an "included" file, and jump to the next |
521 | location inside the "includer" right after the #include | |
522 | "included", this variable points the map in use right before the | |
523 | #include "included", inside the same "includer" file. */ | |
0e50b624 | 524 | line_map_ordinary *from; |
47d89cf3 | 525 | |
3caf0ca1 BS |
526 | linemap_assert (!MAIN_FILE_P (map - 1)); |
527 | /* (MAP - 1) points to the map we are leaving. The | |
528 | map from which (MAP - 1) got included should be the map | |
529 | that comes right before MAP in the same file. */ | |
530 | from = INCLUDED_FROM (set, map - 1); | |
47d89cf3 NB |
531 | |
532 | /* A TO_FILE of NULL is special - we use the natural values. */ | |
3caf0ca1 | 533 | if (to_file == NULL) |
47d89cf3 | 534 | { |
46427374 | 535 | to_file = ORDINARY_MAP_FILE_NAME (from); |
12f9df4e | 536 | to_line = SOURCE_LINE (from, from[1].start_location); |
46427374 | 537 | sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from); |
fde84349 | 538 | } |
3caf0ca1 BS |
539 | else |
540 | linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from), | |
541 | to_file) == 0); | |
fde84349 NB |
542 | } |
543 | ||
c819ed29 DM |
544 | map->sysp = sysp; |
545 | map->start_location = start_location; | |
546 | map->to_file = to_file; | |
547 | map->to_line = to_line; | |
46427374 | 548 | LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1; |
ebedc9a3 DM |
549 | map->m_column_and_range_bits = 0; |
550 | map->m_range_bits = 0; | |
12f9df4e | 551 | set->highest_location = start_location; |
500bee0a | 552 | set->highest_line = start_location; |
12f9df4e | 553 | set->max_column_hint = 0; |
47d89cf3 | 554 | |
ebedc9a3 DM |
555 | /* This assertion is placed after set->highest_location has |
556 | been updated, since the latter affects | |
557 | linemap_location_from_macro_expansion_p, which ultimately affects | |
558 | pure_location_p. */ | |
559 | linemap_assert (pure_location_p (set, start_location)); | |
560 | ||
fde84349 | 561 | if (reason == LC_ENTER) |
d8693c6f | 562 | { |
c819ed29 | 563 | map->included_from = |
46427374 | 564 | set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2); |
d8693c6f | 565 | set->depth++; |
d8693c6f NB |
566 | if (set->trace_includes) |
567 | trace_include (set, map); | |
568 | } | |
d82fc108 | 569 | else if (reason == LC_RENAME) |
c819ed29 | 570 | map->included_from = ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]); |
d82fc108 | 571 | else if (reason == LC_LEAVE) |
d8693c6f NB |
572 | { |
573 | set->depth--; | |
c819ed29 | 574 | map->included_from = |
46427374 | 575 | ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1)); |
d8693c6f | 576 | } |
5993019d | 577 | |
d82fc108 NB |
578 | return map; |
579 | } | |
580 | ||
7d9641cc | 581 | /* Returns TRUE if the line table set tracks token locations across |
46427374 TT |
582 | macro expansion, FALSE otherwise. */ |
583 | ||
584 | bool | |
585 | linemap_tracks_macro_expansion_locs_p (struct line_maps *set) | |
586 | { | |
587 | return LINEMAPS_MACRO_MAPS (set) != NULL; | |
588 | } | |
589 | ||
590 | /* Create a macro map. A macro map encodes source locations of tokens | |
591 | that are part of a macro replacement-list, at a macro expansion | |
592 | point. See the extensive comments of struct line_map and struct | |
593 | line_map_macro, in line-map.h. | |
594 | ||
595 | This map shall be created when the macro is expanded. The map | |
596 | encodes the source location of the expansion point of the macro as | |
597 | well as the "original" source location of each token that is part | |
598 | of the macro replacement-list. If a macro is defined but never | |
599 | expanded, it has no macro map. SET is the set of maps the macro | |
600 | map should be part of. MACRO_NODE is the macro which the new macro | |
601 | map should encode source locations for. EXPANSION is the location | |
602 | of the expansion point of MACRO. For function-like macros | |
603 | invocations, it's best to make it point to the closing parenthesis | |
604 | of the macro, rather than the the location of the first character | |
605 | of the macro. NUM_TOKENS is the number of tokens that are part of | |
606 | the replacement-list of MACRO. | |
607 | ||
608 | Note that when we run out of the integer space available for source | |
609 | locations, this function returns NULL. In that case, callers of | |
610 | this function cannot encode {line,column} pairs into locations of | |
611 | macro tokens anymore. */ | |
612 | ||
0e50b624 | 613 | const line_map_macro * |
46427374 TT |
614 | linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node, |
615 | source_location expansion, unsigned int num_tokens) | |
616 | { | |
0e50b624 | 617 | line_map_macro *map; |
46427374 | 618 | source_location start_location; |
8ac16127 MG |
619 | /* Cast away extern "C" from the type of xrealloc. */ |
620 | line_map_realloc reallocator = (set->reallocator | |
621 | ? set->reallocator | |
622 | : (line_map_realloc) xrealloc); | |
46427374 TT |
623 | |
624 | start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens; | |
625 | ||
626 | if (start_location <= set->highest_line | |
627 | || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set)) | |
628 | /* We ran out of macro map space. */ | |
629 | return NULL; | |
630 | ||
0e50b624 | 631 | map = linemap_check_macro (new_linemap (set, LC_ENTER_MACRO)); |
46427374 | 632 | |
c819ed29 DM |
633 | map->start_location = start_location; |
634 | map->macro = macro_node; | |
635 | map->n_tokens = num_tokens; | |
636 | map->macro_locations | |
46427374 TT |
637 | = (source_location*) reallocator (NULL, |
638 | 2 * num_tokens | |
639 | * sizeof (source_location)); | |
c819ed29 | 640 | map->expansion = expansion; |
46427374 TT |
641 | memset (MACRO_MAP_LOCATIONS (map), 0, |
642 | num_tokens * sizeof (source_location)); | |
643 | ||
644 | LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1; | |
46427374 TT |
645 | |
646 | return map; | |
647 | } | |
648 | ||
649 | /* Create and return a virtual location for a token that is part of a | |
650 | macro expansion-list at a macro expansion point. See the comment | |
651 | inside struct line_map_macro to see what an expansion-list exactly | |
652 | is. | |
653 | ||
654 | A call to this function must come after a call to | |
655 | linemap_enter_macro. | |
656 | ||
657 | MAP is the map into which the source location is created. TOKEN_NO | |
658 | is the index of the token in the macro replacement-list, starting | |
659 | at number 0. | |
660 | ||
661 | ORIG_LOC is the location of the token outside of this macro | |
662 | expansion. If the token comes originally from the macro | |
663 | definition, it is the locus in the macro definition; otherwise it | |
664 | is a location in the context of the caller of this macro expansion | |
665 | (which is a virtual location or a source location if the caller is | |
666 | itself a macro expansion or not). | |
667 | ||
3aac0952 | 668 | ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition, |
46427374 TT |
669 | either of the token itself or of a macro parameter that it |
670 | replaces. */ | |
671 | ||
672 | source_location | |
0e50b624 | 673 | linemap_add_macro_token (const line_map_macro *map, |
46427374 TT |
674 | unsigned int token_no, |
675 | source_location orig_loc, | |
676 | source_location orig_parm_replacement_loc) | |
677 | { | |
678 | source_location result; | |
679 | ||
680 | linemap_assert (linemap_macro_expansion_map_p (map)); | |
681 | linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map)); | |
682 | ||
683 | MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc; | |
684 | MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc; | |
685 | ||
686 | result = MAP_START_LOCATION (map) + token_no; | |
687 | return result; | |
688 | } | |
689 | ||
690 | /* Return a source_location for the start (i.e. column==0) of | |
691 | (physical) line TO_LINE in the current source file (as in the | |
692 | most recent linemap_add). MAX_COLUMN_HINT is the highest column | |
693 | number we expect to use in this line (but it does not change | |
694 | the highest_location). */ | |
695 | ||
12f9df4e | 696 | source_location |
1bb64668 | 697 | linemap_line_start (struct line_maps *set, linenum_type to_line, |
12f9df4e PB |
698 | unsigned int max_column_hint) |
699 | { | |
0e50b624 | 700 | line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set); |
12f9df4e PB |
701 | source_location highest = set->highest_location; |
702 | source_location r; | |
46427374 TT |
703 | linenum_type last_line = |
704 | SOURCE_LINE (map, set->highest_line); | |
12f9df4e PB |
705 | int line_delta = to_line - last_line; |
706 | bool add_map = false; | |
ebedc9a3 DM |
707 | linemap_assert (map->m_column_and_range_bits >= map->m_range_bits); |
708 | int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits; | |
46427374 | 709 | |
12f9df4e | 710 | if (line_delta < 0 |
46427374 | 711 | || (line_delta > 10 |
ebedc9a3 DM |
712 | && line_delta * map->m_column_and_range_bits > 1000) |
713 | || (max_column_hint >= (1U << effective_column_bits)) | |
714 | || (max_column_hint <= 80 && effective_column_bits >= 10) | |
c7df95d8 DM |
715 | || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES |
716 | && map->m_range_bits > 0) | |
815facd3 MLI |
717 | || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS |
718 | && (set->max_column_hint || highest >= LINE_MAP_MAX_SOURCE_LOCATION))) | |
43ba1c6c | 719 | add_map = true; |
12f9df4e PB |
720 | else |
721 | max_column_hint = set->max_column_hint; | |
722 | if (add_map) | |
723 | { | |
724 | int column_bits; | |
ebedc9a3 | 725 | int range_bits; |
815facd3 MLI |
726 | if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER |
727 | || highest > LINE_MAP_MAX_LOCATION_WITH_COLS) | |
12f9df4e | 728 | { |
c1fc5047 | 729 | /* If the column number is ridiculous or we've allocated a huge |
ebedc9a3 DM |
730 | number of source_locations, give up on column numbers |
731 | (and on packed ranges). */ | |
12f9df4e | 732 | max_column_hint = 0; |
12f9df4e | 733 | column_bits = 0; |
ebedc9a3 | 734 | range_bits = 0; |
815facd3 MLI |
735 | if (highest > LINE_MAP_MAX_SOURCE_LOCATION) |
736 | return 0; | |
12f9df4e PB |
737 | } |
738 | else | |
739 | { | |
740 | column_bits = 7; | |
c7df95d8 DM |
741 | if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES) |
742 | range_bits = set->default_range_bits; | |
743 | else | |
744 | range_bits = 0; | |
12f9df4e PB |
745 | while (max_column_hint >= (1U << column_bits)) |
746 | column_bits++; | |
747 | max_column_hint = 1U << column_bits; | |
ebedc9a3 | 748 | column_bits += range_bits; |
12f9df4e | 749 | } |
c1fc5047 PB |
750 | /* Allocate the new line_map. However, if the current map only has a |
751 | single line we can sometimes just increase its column_bits instead. */ | |
12f9df4e | 752 | if (line_delta < 0 |
46427374 | 753 | || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map) |
c7df95d8 DM |
754 | || SOURCE_COLUMN (map, highest) >= (1U << column_bits) |
755 | || range_bits < map->m_range_bits) | |
0e50b624 DM |
756 | map = linemap_check_ordinary |
757 | (const_cast <line_map *> | |
758 | (linemap_add (set, LC_RENAME, | |
759 | ORDINARY_MAP_IN_SYSTEM_HEADER_P (map), | |
760 | ORDINARY_MAP_FILE_NAME (map), | |
761 | to_line))); | |
ebedc9a3 DM |
762 | map->m_column_and_range_bits = column_bits; |
763 | map->m_range_bits = range_bits; | |
46427374 TT |
764 | r = (MAP_START_LOCATION (map) |
765 | + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map)) | |
766 | << column_bits)); | |
12f9df4e PB |
767 | } |
768 | else | |
ebedc9a3 | 769 | r = set->highest_line + (line_delta << map->m_column_and_range_bits); |
46427374 TT |
770 | |
771 | /* Locations of ordinary tokens are always lower than locations of | |
772 | macro tokens. */ | |
773 | if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set)) | |
774 | return 0; | |
775 | ||
500bee0a | 776 | set->highest_line = r; |
12f9df4e PB |
777 | if (r > set->highest_location) |
778 | set->highest_location = r; | |
779 | set->max_column_hint = max_column_hint; | |
ebedc9a3 DM |
780 | |
781 | /* At this point, we expect one of: | |
782 | (a) the normal case: a "pure" location with 0 range bits, or | |
783 | (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track | |
784 | columns anymore (or ranges), or | |
785 | (c) we're in a region with a column hint exceeding | |
786 | LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off, | |
787 | with column_bits == 0. */ | |
788 | linemap_assert (pure_location_p (set, r) | |
789 | || r >= LINE_MAP_MAX_LOCATION_WITH_COLS | |
790 | || map->m_column_and_range_bits == 0); | |
791 | linemap_assert (SOURCE_LINE (map, r) == to_line); | |
12f9df4e PB |
792 | return r; |
793 | } | |
794 | ||
46427374 TT |
795 | /* Encode and return a source_location from a column number. The |
796 | source line considered is the last source line used to call | |
797 | linemap_line_start, i.e, the last source line which a location was | |
798 | encoded from. */ | |
799 | ||
500bee0a PB |
800 | source_location |
801 | linemap_position_for_column (struct line_maps *set, unsigned int to_column) | |
802 | { | |
803 | source_location r = set->highest_line; | |
46427374 TT |
804 | |
805 | linemap_assert | |
806 | (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set))); | |
807 | ||
500bee0a PB |
808 | if (to_column >= set->max_column_hint) |
809 | { | |
815facd3 MLI |
810 | if (r > LINE_MAP_MAX_LOCATION_WITH_COLS |
811 | || to_column > LINE_MAP_MAX_COLUMN_NUMBER) | |
500bee0a PB |
812 | { |
813 | /* Running low on source_locations - disable column numbers. */ | |
814 | return r; | |
815 | } | |
816 | else | |
817 | { | |
0e50b624 | 818 | line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set); |
500bee0a PB |
819 | r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50); |
820 | } | |
821 | } | |
ebedc9a3 DM |
822 | line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set); |
823 | r = r + (to_column << map->m_range_bits); | |
500bee0a PB |
824 | if (r >= set->highest_location) |
825 | set->highest_location = r; | |
826 | return r; | |
827 | } | |
828 | ||
46427374 TT |
829 | /* Encode and return a source location from a given line and |
830 | column. */ | |
d82fc108 | 831 | |
46427374 | 832 | source_location |
ebedc9a3 DM |
833 | linemap_position_for_line_and_column (line_maps *set, |
834 | const line_map_ordinary *ord_map, | |
46427374 TT |
835 | linenum_type line, |
836 | unsigned column) | |
837 | { | |
0e50b624 | 838 | linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line); |
46427374 | 839 | |
ebedc9a3 DM |
840 | source_location r = MAP_START_LOCATION (ord_map); |
841 | r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map)) | |
842 | << ord_map->m_column_and_range_bits); | |
843 | if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS) | |
844 | r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1)) | |
845 | << ord_map->m_range_bits); | |
846 | source_location upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set); | |
847 | if (r >= upper_limit) | |
848 | r = upper_limit - 1; | |
849 | if (r > set->highest_location) | |
850 | set->highest_location = r; | |
851 | return r; | |
46427374 TT |
852 | } |
853 | ||
3aa34c1d | 854 | /* Encode and return a source_location starting from location LOC and |
7168133a | 855 | shifting it by COLUMN_OFFSET columns. This function does not support |
3aa34c1d MLI |
856 | virtual locations. */ |
857 | ||
858 | source_location | |
859 | linemap_position_for_loc_and_offset (struct line_maps *set, | |
860 | source_location loc, | |
7168133a | 861 | unsigned int column_offset) |
3aa34c1d | 862 | { |
0e50b624 | 863 | const line_map_ordinary * map = NULL; |
3aa34c1d | 864 | |
ebedc9a3 DM |
865 | if (IS_ADHOC_LOC (loc)) |
866 | loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
867 | ||
3aa34c1d | 868 | /* This function does not support virtual locations yet. */ |
73bd8329 MLI |
869 | if (linemap_assert_fails |
870 | (!linemap_location_from_macro_expansion_p (set, loc))) | |
871 | return loc; | |
3aa34c1d | 872 | |
7168133a | 873 | if (column_offset == 0 |
3aa34c1d MLI |
874 | /* Adding an offset to a reserved location (like |
875 | UNKNOWN_LOCATION for the C/C++ FEs) does not really make | |
876 | sense. So let's leave the location intact in that case. */ | |
877 | || loc < RESERVED_LOCATION_COUNT) | |
878 | return loc; | |
879 | ||
880 | /* We find the real location and shift it. */ | |
881 | loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map); | |
882 | /* The new location (loc + offset) should be higher than the first | |
87b470b3 MLI |
883 | location encoded by MAP. This can fail if the line information |
884 | is messed up because of line directives (see PR66415). */ | |
7168133a | 885 | if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits)) |
73bd8329 | 886 | return loc; |
3aa34c1d | 887 | |
87b470b3 MLI |
888 | linenum_type line = SOURCE_LINE (map, loc); |
889 | unsigned int column = SOURCE_COLUMN (map, loc); | |
890 | ||
3aa34c1d MLI |
891 | /* If MAP is not the last line map of its set, then the new location |
892 | (loc + offset) should be less than the first location encoded by | |
87b470b3 MLI |
893 | the next line map of the set. Otherwise, we try to encode the |
894 | location in the next map. */ | |
895 | while (map != LINEMAPS_LAST_ORDINARY_MAP (set) | |
7168133a DM |
896 | && (loc + (column_offset << map->m_range_bits) |
897 | >= MAP_START_LOCATION (&map[1]))) | |
87b470b3 MLI |
898 | { |
899 | map = &map[1]; | |
900 | /* If the next map starts in a higher line, we cannot encode the | |
901 | location there. */ | |
902 | if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map)) | |
903 | return loc; | |
904 | } | |
3aa34c1d | 905 | |
7168133a DM |
906 | column += column_offset; |
907 | if (linemap_assert_fails (column < (1u << map->m_column_and_range_bits))) | |
73bd8329 | 908 | return loc; |
3aa34c1d MLI |
909 | |
910 | source_location r = | |
7168133a | 911 | linemap_position_for_line_and_column (set, map, line, column); |
b93c0722 MLI |
912 | if (linemap_assert_fails (r <= set->highest_location) |
913 | || linemap_assert_fails (map == linemap_lookup (set, r))) | |
73bd8329 MLI |
914 | return loc; |
915 | ||
3aa34c1d MLI |
916 | return r; |
917 | } | |
918 | ||
46427374 TT |
919 | /* Given a virtual source location yielded by a map (either an |
920 | ordinary or a macro map), returns that map. */ | |
921 | ||
922 | const struct line_map* | |
7d40b45f | 923 | linemap_lookup (struct line_maps *set, source_location line) |
46427374 | 924 | { |
5368224f DC |
925 | if (IS_ADHOC_LOC (line)) |
926 | line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus; | |
46427374 TT |
927 | if (linemap_location_from_macro_expansion_p (set, line)) |
928 | return linemap_macro_map_lookup (set, line); | |
929 | return linemap_ordinary_map_lookup (set, line); | |
930 | } | |
931 | ||
932 | /* Given a source location yielded by an ordinary map, returns that | |
933 | map. Since the set is built chronologically, the logical lines are | |
934 | monotonic increasing, and so the list is sorted and we can use a | |
935 | binary search. */ | |
936 | ||
0e50b624 | 937 | static const line_map_ordinary * |
46427374 | 938 | linemap_ordinary_map_lookup (struct line_maps *set, source_location line) |
d82fc108 | 939 | { |
9132fbb7 | 940 | unsigned int md, mn, mx; |
0e50b624 | 941 | const line_map_ordinary *cached, *result; |
9132fbb7 | 942 | |
5368224f DC |
943 | if (IS_ADHOC_LOC (line)) |
944 | line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus; | |
945 | ||
46427374 TT |
946 | if (set == NULL || line < RESERVED_LOCATION_COUNT) |
947 | return NULL; | |
948 | ||
949 | mn = LINEMAPS_ORDINARY_CACHE (set); | |
950 | mx = LINEMAPS_ORDINARY_USED (set); | |
9132fbb7 | 951 | |
46427374 | 952 | cached = LINEMAPS_ORDINARY_MAP_AT (set, mn); |
9ac97460 | 953 | /* We should get a segfault if no line_maps have been added yet. */ |
46427374 | 954 | if (line >= MAP_START_LOCATION (cached)) |
9132fbb7 | 955 | { |
46427374 | 956 | if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1])) |
9132fbb7 PB |
957 | return cached; |
958 | } | |
959 | else | |
960 | { | |
961 | mx = mn; | |
962 | mn = 0; | |
963 | } | |
d82fc108 NB |
964 | |
965 | while (mx - mn > 1) | |
966 | { | |
967 | md = (mn + mx) / 2; | |
46427374 | 968 | if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line) |
d82fc108 NB |
969 | mx = md; |
970 | else | |
971 | mn = md; | |
972 | } | |
973 | ||
46427374 TT |
974 | LINEMAPS_ORDINARY_CACHE (set) = mn; |
975 | result = LINEMAPS_ORDINARY_MAP_AT (set, mn); | |
976 | linemap_assert (line >= MAP_START_LOCATION (result)); | |
977 | return result; | |
978 | } | |
979 | ||
980 | /* Given a source location yielded by a macro map, returns that map. | |
981 | Since the set is built chronologically, the logical lines are | |
982 | monotonic decreasing, and so the list is sorted and we can use a | |
983 | binary search. */ | |
984 | ||
0e50b624 | 985 | static const line_map_macro * |
46427374 TT |
986 | linemap_macro_map_lookup (struct line_maps *set, source_location line) |
987 | { | |
988 | unsigned int md, mn, mx; | |
0e50b624 | 989 | const struct line_map_macro *cached, *result; |
46427374 | 990 | |
5368224f DC |
991 | if (IS_ADHOC_LOC (line)) |
992 | line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus; | |
993 | ||
46427374 TT |
994 | linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set)); |
995 | ||
996 | if (set == NULL) | |
997 | return NULL; | |
998 | ||
999 | mn = LINEMAPS_MACRO_CACHE (set); | |
1000 | mx = LINEMAPS_MACRO_USED (set); | |
1001 | cached = LINEMAPS_MACRO_MAP_AT (set, mn); | |
1002 | ||
1003 | if (line >= MAP_START_LOCATION (cached)) | |
1004 | { | |
1005 | if (mn == 0 || line < MAP_START_LOCATION (&cached[-1])) | |
1006 | return cached; | |
1007 | mx = mn - 1; | |
1008 | mn = 0; | |
1009 | } | |
1010 | ||
3bb0c8db | 1011 | while (mn < mx) |
46427374 TT |
1012 | { |
1013 | md = (mx + mn) / 2; | |
1014 | if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line) | |
3bb0c8db | 1015 | mn = md + 1; |
46427374 TT |
1016 | else |
1017 | mx = md; | |
3bb0c8db | 1018 | } |
46427374 TT |
1019 | |
1020 | LINEMAPS_MACRO_CACHE (set) = mx; | |
1021 | result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set)); | |
1022 | linemap_assert (MAP_START_LOCATION (result) <= line); | |
1023 | ||
1024 | return result; | |
1025 | } | |
1026 | ||
1027 | /* Return TRUE if MAP encodes locations coming from a macro | |
1028 | replacement-list at macro expansion point. */ | |
1029 | ||
1030 | bool | |
1031 | linemap_macro_expansion_map_p (const struct line_map *map) | |
1032 | { | |
1033 | if (!map) | |
1034 | return false; | |
1035 | return (map->reason == LC_ENTER_MACRO); | |
1036 | } | |
1037 | ||
1038 | /* If LOCATION is the locus of a token in a replacement-list of a | |
1039 | macro expansion return the location of the macro expansion point. | |
1040 | ||
1041 | Read the comments of struct line_map and struct line_map_macro in | |
1042 | line-map.h to understand what a macro expansion point is. */ | |
1043 | ||
411f92de | 1044 | static source_location |
0e50b624 | 1045 | linemap_macro_map_loc_to_exp_point (const line_map_macro *map, |
411f92de | 1046 | source_location location ATTRIBUTE_UNUSED) |
46427374 | 1047 | { |
46427374 TT |
1048 | linemap_assert (linemap_macro_expansion_map_p (map) |
1049 | && location >= MAP_START_LOCATION (map)); | |
1050 | ||
1051 | /* Make sure LOCATION is correct. */ | |
411f92de DS |
1052 | linemap_assert ((location - MAP_START_LOCATION (map)) |
1053 | < MACRO_MAP_NUM_MACRO_TOKENS (map)); | |
46427374 TT |
1054 | |
1055 | return MACRO_MAP_EXPANSION_POINT_LOCATION (map); | |
1056 | } | |
1057 | ||
3aac0952 MLI |
1058 | /* LOCATION is the source location of a token that belongs to a macro |
1059 | replacement-list as part of the macro expansion denoted by MAP. | |
46427374 | 1060 | |
3aac0952 MLI |
1061 | Return the location of the token at the definition point of the |
1062 | macro. */ | |
1063 | ||
1064 | static source_location | |
0e50b624 | 1065 | linemap_macro_map_loc_to_def_point (const line_map_macro *map, |
46427374 TT |
1066 | source_location location) |
1067 | { | |
1068 | unsigned token_no; | |
1069 | ||
1070 | linemap_assert (linemap_macro_expansion_map_p (map) | |
1071 | && location >= MAP_START_LOCATION (map)); | |
1072 | linemap_assert (location >= RESERVED_LOCATION_COUNT); | |
1073 | ||
1074 | token_no = location - MAP_START_LOCATION (map); | |
1075 | linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map)); | |
1076 | ||
1077 | location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1]; | |
1078 | ||
1079 | return location; | |
1080 | } | |
1081 | ||
1082 | /* If LOCATION is the locus of a token that is an argument of a | |
1083 | function-like macro M and appears in the expansion of M, return the | |
1084 | locus of that argument in the context of the caller of M. | |
1085 | ||
1086 | In other words, this returns the xI location presented in the | |
1087 | comments of line_map_macro above. */ | |
1088 | source_location | |
ebedc9a3 DM |
1089 | linemap_macro_map_loc_unwind_toward_spelling (line_maps *set, |
1090 | const line_map_macro* map, | |
46427374 TT |
1091 | source_location location) |
1092 | { | |
1093 | unsigned token_no; | |
1094 | ||
ebedc9a3 DM |
1095 | if (IS_ADHOC_LOC (location)) |
1096 | location = get_location_from_adhoc_loc (set, location); | |
1097 | ||
46427374 TT |
1098 | linemap_assert (linemap_macro_expansion_map_p (map) |
1099 | && location >= MAP_START_LOCATION (map)); | |
1100 | linemap_assert (location >= RESERVED_LOCATION_COUNT); | |
ebedc9a3 | 1101 | linemap_assert (!IS_ADHOC_LOC (location)); |
46427374 TT |
1102 | |
1103 | token_no = location - MAP_START_LOCATION (map); | |
1104 | linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map)); | |
1105 | ||
1106 | location = MACRO_MAP_LOCATIONS (map)[2 * token_no]; | |
1107 | ||
1108 | return location; | |
1109 | } | |
1110 | ||
1111 | /* Return the source line number corresponding to source location | |
1112 | LOCATION. SET is the line map set LOCATION comes from. If | |
1113 | LOCATION is the source location of token that is part of the | |
1114 | replacement-list of a macro expansion return the line number of the | |
1115 | macro expansion point. */ | |
1116 | ||
1117 | int | |
1118 | linemap_get_expansion_line (struct line_maps *set, | |
1119 | source_location location) | |
1120 | { | |
0e50b624 | 1121 | const line_map_ordinary *map = NULL; |
46427374 | 1122 | |
5368224f | 1123 | if (IS_ADHOC_LOC (location)) |
acf601ae JJ |
1124 | location = set->location_adhoc_data_map.data[location |
1125 | & MAX_SOURCE_LOCATION].locus; | |
5368224f | 1126 | |
46427374 TT |
1127 | if (location < RESERVED_LOCATION_COUNT) |
1128 | return 0; | |
1129 | ||
1130 | location = | |
1131 | linemap_macro_loc_to_exp_point (set, location, &map); | |
1132 | ||
1133 | return SOURCE_LINE (map, location); | |
1134 | } | |
1135 | ||
1136 | /* Return the path of the file corresponding to source code location | |
1137 | LOCATION. | |
1138 | ||
1139 | If LOCATION is the source location of token that is part of the | |
1140 | replacement-list of a macro expansion return the file path of the | |
1141 | macro expansion point. | |
1142 | ||
1143 | SET is the line map set LOCATION comes from. */ | |
1144 | ||
1145 | const char* | |
1146 | linemap_get_expansion_filename (struct line_maps *set, | |
1147 | source_location location) | |
1148 | { | |
0e50b624 | 1149 | const struct line_map_ordinary *map = NULL; |
46427374 | 1150 | |
5368224f | 1151 | if (IS_ADHOC_LOC (location)) |
acf601ae JJ |
1152 | location = set->location_adhoc_data_map.data[location |
1153 | & MAX_SOURCE_LOCATION].locus; | |
5368224f | 1154 | |
46427374 TT |
1155 | if (location < RESERVED_LOCATION_COUNT) |
1156 | return NULL; | |
1157 | ||
1158 | location = | |
1159 | linemap_macro_loc_to_exp_point (set, location, &map); | |
1160 | ||
1161 | return LINEMAP_FILE (map); | |
1162 | } | |
1163 | ||
1164 | /* Return the name of the macro associated to MACRO_MAP. */ | |
1165 | ||
1166 | const char* | |
0e50b624 | 1167 | linemap_map_get_macro_name (const line_map_macro *macro_map) |
46427374 TT |
1168 | { |
1169 | linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map)); | |
1170 | return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map)); | |
1171 | } | |
1172 | ||
1173 | /* Return a positive value if LOCATION is the locus of a token that is | |
1174 | located in a system header, O otherwise. It returns 1 if LOCATION | |
1175 | is the locus of a token that is located in a system header, and 2 | |
1176 | if LOCATION is the locus of a token located in a C system header | |
1177 | that therefore needs to be extern "C" protected in C++. | |
1178 | ||
1179 | Note that this function returns 1 if LOCATION belongs to a token | |
1180 | that is part of a macro replacement-list defined in a system | |
1181 | header, but expanded in a non-system file. */ | |
1182 | ||
1183 | int | |
1184 | linemap_location_in_system_header_p (struct line_maps *set, | |
1185 | source_location location) | |
1186 | { | |
1187 | const struct line_map *map = NULL; | |
1188 | ||
5368224f | 1189 | if (IS_ADHOC_LOC (location)) |
acf601ae JJ |
1190 | location = set->location_adhoc_data_map.data[location |
1191 | & MAX_SOURCE_LOCATION].locus; | |
5368224f | 1192 | |
84756fd4 DS |
1193 | if (location < RESERVED_LOCATION_COUNT) |
1194 | return false; | |
1195 | ||
7ca643e1 DS |
1196 | /* Let's look at where the token for LOCATION comes from. */ |
1197 | while (true) | |
1198 | { | |
1199 | map = linemap_lookup (set, location); | |
1200 | if (map != NULL) | |
1201 | { | |
1202 | if (!linemap_macro_expansion_map_p (map)) | |
1203 | /* It's a normal token. */ | |
0e50b624 | 1204 | return LINEMAP_SYSP (linemap_check_ordinary (map)); |
7ca643e1 DS |
1205 | else |
1206 | { | |
0e50b624 DM |
1207 | const line_map_macro *macro_map = linemap_check_macro (map); |
1208 | ||
7ca643e1 DS |
1209 | /* It's a token resulting from a macro expansion. */ |
1210 | source_location loc = | |
ebedc9a3 | 1211 | linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location); |
7ca643e1 DS |
1212 | if (loc < RESERVED_LOCATION_COUNT) |
1213 | /* This token might come from a built-in macro. Let's | |
1214 | look at where that macro got expanded. */ | |
0e50b624 | 1215 | location = linemap_macro_map_loc_to_exp_point (macro_map, location); |
7ca643e1 DS |
1216 | else |
1217 | location = loc; | |
1218 | } | |
1219 | } | |
1220 | else | |
1221 | break; | |
1222 | } | |
1223 | return false; | |
46427374 TT |
1224 | } |
1225 | ||
63cb3926 JM |
1226 | /* Return TRUE if LOCATION is a source code location of a token that is part of |
1227 | a macro expansion, FALSE otherwise. */ | |
46427374 TT |
1228 | |
1229 | bool | |
3aac0952 | 1230 | linemap_location_from_macro_expansion_p (const struct line_maps *set, |
46427374 TT |
1231 | source_location location) |
1232 | { | |
5368224f | 1233 | if (IS_ADHOC_LOC (location)) |
acf601ae JJ |
1234 | location = set->location_adhoc_data_map.data[location |
1235 | & MAX_SOURCE_LOCATION].locus; | |
5368224f | 1236 | |
46427374 TT |
1237 | linemap_assert (location <= MAX_SOURCE_LOCATION |
1238 | && (set->highest_location | |
1239 | < LINEMAPS_MACRO_LOWEST_LOCATION (set))); | |
1240 | if (set == NULL) | |
1241 | return false; | |
1242 | return (location > set->highest_location); | |
1243 | } | |
1244 | ||
1245 | /* Given two virtual locations *LOC0 and *LOC1, return the first | |
1246 | common macro map in their macro expansion histories. Return NULL | |
1247 | if no common macro was found. *LOC0 (resp. *LOC1) is set to the | |
1248 | virtual location of the token inside the resulting macro. */ | |
1249 | ||
1250 | static const struct line_map* | |
1251 | first_map_in_common_1 (struct line_maps *set, | |
1252 | source_location *loc0, | |
1253 | source_location *loc1) | |
1254 | { | |
1255 | source_location l0 = *loc0, l1 = *loc1; | |
1256 | const struct line_map *map0 = linemap_lookup (set, l0), | |
1257 | *map1 = linemap_lookup (set, l1); | |
1258 | ||
1259 | while (linemap_macro_expansion_map_p (map0) | |
1260 | && linemap_macro_expansion_map_p (map1) | |
1261 | && (map0 != map1)) | |
1262 | { | |
1263 | if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1)) | |
1264 | { | |
0e50b624 DM |
1265 | l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0), |
1266 | l0); | |
46427374 TT |
1267 | map0 = linemap_lookup (set, l0); |
1268 | } | |
1269 | else | |
1270 | { | |
0e50b624 DM |
1271 | l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1), |
1272 | l1); | |
46427374 TT |
1273 | map1 = linemap_lookup (set, l1); |
1274 | } | |
1275 | } | |
1276 | ||
1277 | if (map0 == map1) | |
1278 | { | |
1279 | *loc0 = l0; | |
1280 | *loc1 = l1; | |
1281 | return map0; | |
1282 | } | |
1283 | return NULL; | |
1284 | } | |
1285 | ||
1286 | /* Given two virtual locations LOC0 and LOC1, return the first common | |
1287 | macro map in their macro expansion histories. Return NULL if no | |
1288 | common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the | |
1289 | virtual location of the token inside the resulting macro, upon | |
1290 | return of a non-NULL result. */ | |
1291 | ||
1292 | static const struct line_map* | |
1293 | first_map_in_common (struct line_maps *set, | |
1294 | source_location loc0, | |
1295 | source_location loc1, | |
1296 | source_location *res_loc0, | |
1297 | source_location *res_loc1) | |
1298 | { | |
1299 | *res_loc0 = loc0; | |
1300 | *res_loc1 = loc1; | |
1301 | ||
1302 | return first_map_in_common_1 (set, res_loc0, res_loc1); | |
1303 | } | |
1304 | ||
1305 | /* Return a positive value if PRE denotes the location of a token that | |
1306 | comes before the token of POST, 0 if PRE denotes the location of | |
1307 | the same token as the token for POST, and a negative value | |
1308 | otherwise. */ | |
1309 | ||
1310 | int | |
1311 | linemap_compare_locations (struct line_maps *set, | |
1312 | source_location pre, | |
1313 | source_location post) | |
1314 | { | |
1315 | bool pre_virtual_p, post_virtual_p; | |
1316 | source_location l0 = pre, l1 = post; | |
1317 | ||
acf601ae | 1318 | if (IS_ADHOC_LOC (l0)) |
196440f8 | 1319 | l0 = get_location_from_adhoc_loc (set, l0); |
acf601ae | 1320 | if (IS_ADHOC_LOC (l1)) |
196440f8 | 1321 | l1 = get_location_from_adhoc_loc (set, l1); |
acf601ae | 1322 | |
46427374 TT |
1323 | if (l0 == l1) |
1324 | return 0; | |
1325 | ||
1326 | if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0))) | |
1327 | l0 = linemap_resolve_location (set, l0, | |
1328 | LRK_MACRO_EXPANSION_POINT, | |
1329 | NULL); | |
1330 | ||
1331 | if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1))) | |
1332 | l1 = linemap_resolve_location (set, l1, | |
1333 | LRK_MACRO_EXPANSION_POINT, | |
1334 | NULL); | |
1335 | ||
1336 | if (l0 == l1 | |
1337 | && pre_virtual_p | |
1338 | && post_virtual_p) | |
1339 | { | |
1340 | /* So pre and post represent two tokens that are present in a | |
1341 | same macro expansion. Let's see if the token for pre was | |
1342 | before the token for post in that expansion. */ | |
1343 | unsigned i0, i1; | |
1344 | const struct line_map *map = | |
1345 | first_map_in_common (set, pre, post, &l0, &l1); | |
1346 | ||
1347 | if (map == NULL) | |
1348 | /* This should not be possible. */ | |
1349 | abort (); | |
1350 | ||
1351 | i0 = l0 - MAP_START_LOCATION (map); | |
1352 | i1 = l1 - MAP_START_LOCATION (map); | |
1353 | return i1 - i0; | |
1354 | } | |
1355 | ||
196440f8 DM |
1356 | if (IS_ADHOC_LOC (l0)) |
1357 | l0 = get_location_from_adhoc_loc (set, l0); | |
1358 | if (IS_ADHOC_LOC (l1)) | |
1359 | l1 = get_location_from_adhoc_loc (set, l1); | |
1360 | ||
46427374 | 1361 | return l1 - l0; |
d82fc108 | 1362 | } |
fde84349 | 1363 | |
5993019d NB |
1364 | /* Print an include trace, for e.g. the -H option of the preprocessor. */ |
1365 | ||
1366 | static void | |
0e50b624 | 1367 | trace_include (const struct line_maps *set, const line_map_ordinary *map) |
5993019d | 1368 | { |
d8693c6f | 1369 | unsigned int i = set->depth; |
5993019d | 1370 | |
d8693c6f | 1371 | while (--i) |
5993019d | 1372 | putc ('.', stderr); |
46427374 TT |
1373 | |
1374 | fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map)); | |
1375 | } | |
1376 | ||
1377 | /* Return the spelling location of the token wherever it comes from, | |
1378 | whether part of a macro definition or not. | |
1379 | ||
1380 | This is a subroutine for linemap_resolve_location. */ | |
1381 | ||
1382 | static source_location | |
1383 | linemap_macro_loc_to_spelling_point (struct line_maps *set, | |
1384 | source_location location, | |
0e50b624 | 1385 | const line_map_ordinary **original_map) |
46427374 TT |
1386 | { |
1387 | struct line_map *map; | |
46427374 TT |
1388 | linemap_assert (set && location >= RESERVED_LOCATION_COUNT); |
1389 | ||
1390 | while (true) | |
1391 | { | |
0e50b624 | 1392 | map = const_cast <line_map *> (linemap_lookup (set, location)); |
46427374 TT |
1393 | if (!linemap_macro_expansion_map_p (map)) |
1394 | break; | |
1395 | ||
0e50b624 DM |
1396 | location |
1397 | = linemap_macro_map_loc_unwind_toward_spelling | |
ebedc9a3 | 1398 | (set, linemap_check_macro (map), |
0e50b624 | 1399 | location); |
46427374 TT |
1400 | } |
1401 | ||
1402 | if (original_map) | |
0e50b624 | 1403 | *original_map = linemap_check_ordinary (map); |
46427374 TT |
1404 | return location; |
1405 | } | |
1406 | ||
1407 | /* If LOCATION is the source location of a token that belongs to a | |
1408 | macro replacement-list -- as part of a macro expansion -- then | |
1409 | return the location of the token at the definition point of the | |
1410 | macro. Otherwise, return LOCATION. SET is the set of maps | |
1411 | location come from. ORIGINAL_MAP is an output parm. If non NULL, | |
1412 | the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the | |
1413 | returned location comes from. | |
1414 | ||
1415 | This is a subroutine of linemap_resolve_location. */ | |
1416 | ||
1417 | static source_location | |
1418 | linemap_macro_loc_to_def_point (struct line_maps *set, | |
1419 | source_location location, | |
0e50b624 | 1420 | const line_map_ordinary **original_map) |
46427374 TT |
1421 | { |
1422 | struct line_map *map; | |
1423 | ||
5368224f | 1424 | if (IS_ADHOC_LOC (location)) |
acf601ae JJ |
1425 | location = set->location_adhoc_data_map.data[location |
1426 | & MAX_SOURCE_LOCATION].locus; | |
5368224f | 1427 | |
46427374 TT |
1428 | linemap_assert (set && location >= RESERVED_LOCATION_COUNT); |
1429 | ||
1430 | while (true) | |
1431 | { | |
0e50b624 | 1432 | map = const_cast <line_map *> (linemap_lookup (set, location)); |
46427374 TT |
1433 | if (!linemap_macro_expansion_map_p (map)) |
1434 | break; | |
1435 | ||
1436 | location = | |
0e50b624 DM |
1437 | linemap_macro_map_loc_to_def_point (linemap_check_macro (map), |
1438 | location); | |
46427374 TT |
1439 | } |
1440 | ||
1441 | if (original_map) | |
0e50b624 | 1442 | *original_map = linemap_check_ordinary (map); |
46427374 TT |
1443 | return location; |
1444 | } | |
1445 | ||
1446 | /* If LOCATION is the source location of a token that belongs to a | |
1447 | macro replacement-list -- at a macro expansion point -- then return | |
1448 | the location of the topmost expansion point of the macro. We say | |
1449 | topmost because if we are in the context of a nested macro | |
1450 | expansion, the function returns the source location of the first | |
1451 | macro expansion that triggered the nested expansions. | |
1452 | ||
1453 | Otherwise, return LOCATION. SET is the set of maps location come | |
1454 | from. ORIGINAL_MAP is an output parm. If non NULL, the function | |
1455 | sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned | |
1456 | location comes from. | |
1457 | ||
1458 | This is a subroutine of linemap_resolve_location. */ | |
1459 | ||
1460 | static source_location | |
1461 | linemap_macro_loc_to_exp_point (struct line_maps *set, | |
1462 | source_location location, | |
0e50b624 | 1463 | const line_map_ordinary **original_map) |
46427374 TT |
1464 | { |
1465 | struct line_map *map; | |
1466 | ||
5368224f | 1467 | if (IS_ADHOC_LOC (location)) |
acf601ae JJ |
1468 | location = set->location_adhoc_data_map.data[location |
1469 | & MAX_SOURCE_LOCATION].locus; | |
5368224f | 1470 | |
46427374 TT |
1471 | linemap_assert (set && location >= RESERVED_LOCATION_COUNT); |
1472 | ||
1473 | while (true) | |
1474 | { | |
0e50b624 | 1475 | map = const_cast <line_map *> (linemap_lookup (set, location)); |
46427374 TT |
1476 | if (!linemap_macro_expansion_map_p (map)) |
1477 | break; | |
0e50b624 DM |
1478 | location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map), |
1479 | location); | |
46427374 TT |
1480 | } |
1481 | ||
1482 | if (original_map) | |
0e50b624 | 1483 | *original_map = linemap_check_ordinary (map); |
46427374 TT |
1484 | return location; |
1485 | } | |
1486 | ||
1487 | /* Resolve a virtual location into either a spelling location, an | |
1488 | expansion point location or a token argument replacement point | |
1489 | location. Return the map that encodes the virtual location as well | |
1490 | as the resolved location. | |
1491 | ||
1492 | If LOC is *NOT* the location of a token resulting from the | |
1493 | expansion of a macro, then the parameter LRK (which stands for | |
1494 | Location Resolution Kind) is ignored and the resulting location | |
1495 | just equals the one given in argument. | |
1496 | ||
1497 | Now if LOC *IS* the location of a token resulting from the | |
1498 | expansion of a macro, this is what happens. | |
1499 | ||
1500 | * If LRK is set to LRK_MACRO_EXPANSION_POINT | |
1501 | ------------------------------- | |
1502 | ||
1d72e96f MLI |
1503 | The virtual location is resolved to the first macro expansion point |
1504 | that led to this macro expansion. | |
46427374 TT |
1505 | |
1506 | * If LRK is set to LRK_SPELLING_LOCATION | |
1507 | ------------------------------------- | |
1508 | ||
1d72e96f MLI |
1509 | The virtual location is resolved to the locus where the token has |
1510 | been spelled in the source. This can follow through all the macro | |
1511 | expansions that led to the token. | |
46427374 | 1512 | |
1d72e96f | 1513 | * If LRK is set to LRK_MACRO_DEFINITION_LOCATION |
46427374 TT |
1514 | -------------------------------------- |
1515 | ||
1d72e96f MLI |
1516 | The virtual location is resolved to the locus of the token in the |
1517 | context of the macro definition. | |
1518 | ||
46427374 TT |
1519 | If LOC is the locus of a token that is an argument of a |
1520 | function-like macro [replacing a parameter in the replacement list | |
1521 | of the macro] the virtual location is resolved to the locus of the | |
1522 | parameter that is replaced, in the context of the definition of the | |
1523 | macro. | |
1524 | ||
1525 | If LOC is the locus of a token that is not an argument of a | |
1526 | function-like macro, then the function behaves as if LRK was set to | |
1527 | LRK_SPELLING_LOCATION. | |
1528 | ||
3aac0952 | 1529 | If MAP is not NULL, *MAP is set to the map encoding the |
1d72e96f | 1530 | returned location. Note that if the returned location wasn't originally |
3aac0952 | 1531 | encoded by a map, then *MAP is set to NULL. This can happen if LOC |
84756fd4 DS |
1532 | resolves to a location reserved for the client code, like |
1533 | UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */ | |
46427374 TT |
1534 | |
1535 | source_location | |
1536 | linemap_resolve_location (struct line_maps *set, | |
1537 | source_location loc, | |
1538 | enum location_resolution_kind lrk, | |
0e50b624 | 1539 | const line_map_ordinary **map) |
46427374 | 1540 | { |
ebedc9a3 | 1541 | source_location locus = loc; |
5368224f | 1542 | if (IS_ADHOC_LOC (loc)) |
ebedc9a3 | 1543 | locus = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; |
5368224f | 1544 | |
ebedc9a3 | 1545 | if (locus < RESERVED_LOCATION_COUNT) |
84756fd4 DS |
1546 | { |
1547 | /* A reserved location wasn't encoded in a map. Let's return a | |
1548 | NULL map here, just like what linemap_ordinary_map_lookup | |
1549 | does. */ | |
1550 | if (map) | |
1551 | *map = NULL; | |
1552 | return loc; | |
1553 | } | |
46427374 TT |
1554 | |
1555 | switch (lrk) | |
1556 | { | |
1557 | case LRK_MACRO_EXPANSION_POINT: | |
1558 | loc = linemap_macro_loc_to_exp_point (set, loc, map); | |
1559 | break; | |
1560 | case LRK_SPELLING_LOCATION: | |
1561 | loc = linemap_macro_loc_to_spelling_point (set, loc, map); | |
1562 | break; | |
1563 | case LRK_MACRO_DEFINITION_LOCATION: | |
1564 | loc = linemap_macro_loc_to_def_point (set, loc, map); | |
1565 | break; | |
1566 | default: | |
1567 | abort (); | |
1568 | } | |
1569 | return loc; | |
1570 | } | |
1571 | ||
63cb3926 JM |
1572 | /* TRUE if LOCATION is a source code location of a token that is part of the |
1573 | definition of a macro, FALSE otherwise. */ | |
1574 | ||
1575 | bool | |
1576 | linemap_location_from_macro_definition_p (struct line_maps *set, | |
1577 | source_location loc) | |
1578 | { | |
1579 | if (IS_ADHOC_LOC (loc)) | |
1580 | loc = get_location_from_adhoc_loc (set, loc); | |
1581 | ||
1582 | if (!linemap_location_from_macro_expansion_p (set, loc)) | |
1583 | return false; | |
1584 | ||
1585 | while (true) | |
1586 | { | |
1587 | const struct line_map_macro *map | |
1588 | = linemap_check_macro (linemap_lookup (set, loc)); | |
1589 | ||
1590 | source_location s_loc | |
1591 | = linemap_macro_map_loc_unwind_toward_spelling (set, map, loc); | |
1592 | if (linemap_location_from_macro_expansion_p (set, s_loc)) | |
1593 | loc = s_loc; | |
1594 | else | |
1595 | { | |
1596 | source_location def_loc | |
1597 | = linemap_macro_map_loc_to_def_point (map, loc); | |
1598 | return s_loc == def_loc; | |
1599 | } | |
1600 | } | |
1601 | } | |
1602 | ||
46427374 TT |
1603 | /* |
1604 | Suppose that LOC is the virtual location of a token T coming from | |
1605 | the expansion of a macro M. This function then steps up to get the | |
1606 | location L of the point where M got expanded. If L is a spelling | |
1607 | location inside a macro expansion M', then this function returns | |
1608 | the locus of the point where M' was expanded. Said otherwise, this | |
1609 | function returns the location of T in the context that triggered | |
1610 | the expansion of M. | |
1611 | ||
1612 | *LOC_MAP must be set to the map of LOC. This function then sets it | |
1613 | to the map of the returned location. */ | |
1614 | ||
1615 | source_location | |
1616 | linemap_unwind_toward_expansion (struct line_maps *set, | |
1617 | source_location loc, | |
1618 | const struct line_map **map) | |
1619 | { | |
1620 | source_location resolved_location; | |
0e50b624 | 1621 | const line_map_macro *macro_map = linemap_check_macro (*map); |
46427374 TT |
1622 | const struct line_map *resolved_map; |
1623 | ||
5368224f DC |
1624 | if (IS_ADHOC_LOC (loc)) |
1625 | loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
1626 | ||
46427374 | 1627 | resolved_location = |
ebedc9a3 | 1628 | linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc); |
46427374 TT |
1629 | resolved_map = linemap_lookup (set, resolved_location); |
1630 | ||
1631 | if (!linemap_macro_expansion_map_p (resolved_map)) | |
1632 | { | |
0e50b624 | 1633 | resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc); |
46427374 TT |
1634 | resolved_map = linemap_lookup (set, resolved_location); |
1635 | } | |
1636 | ||
1637 | *map = resolved_map; | |
1638 | return resolved_location; | |
1639 | } | |
1640 | ||
c4ca1a09 DS |
1641 | /* If LOC is the virtual location of a token coming from the expansion |
1642 | of a macro M and if its spelling location is reserved (e.g, a | |
1643 | location for a built-in token), then this function unwinds (using | |
1644 | linemap_unwind_toward_expansion) the location until a location that | |
7d9641cc | 1645 | is not reserved and is not in a system header is reached. In other |
c4ca1a09 DS |
1646 | words, this unwinds the reserved location until a location that is |
1647 | in real source code is reached. | |
1648 | ||
1649 | Otherwise, if the spelling location for LOC is not reserved or if | |
1650 | LOC doesn't come from the expansion of a macro, the function | |
1651 | returns LOC as is and *MAP is not touched. | |
1652 | ||
1653 | *MAP is set to the map of the returned location if the later is | |
1654 | different from LOC. */ | |
1655 | source_location | |
1656 | linemap_unwind_to_first_non_reserved_loc (struct line_maps *set, | |
1657 | source_location loc, | |
1658 | const struct line_map **map) | |
1659 | { | |
1660 | source_location resolved_loc; | |
0e50b624 DM |
1661 | const struct line_map *map0 = NULL; |
1662 | const line_map_ordinary *map1 = NULL; | |
c4ca1a09 | 1663 | |
5368224f DC |
1664 | if (IS_ADHOC_LOC (loc)) |
1665 | loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
1666 | ||
c4ca1a09 DS |
1667 | map0 = linemap_lookup (set, loc); |
1668 | if (!linemap_macro_expansion_map_p (map0)) | |
1669 | return loc; | |
1670 | ||
1671 | resolved_loc = linemap_resolve_location (set, loc, | |
1672 | LRK_SPELLING_LOCATION, | |
1673 | &map1); | |
1674 | ||
1675 | if (resolved_loc >= RESERVED_LOCATION_COUNT | |
1676 | && !LINEMAP_SYSP (map1)) | |
1677 | return loc; | |
1678 | ||
1679 | while (linemap_macro_expansion_map_p (map0) | |
1680 | && (resolved_loc < RESERVED_LOCATION_COUNT | |
1681 | || LINEMAP_SYSP (map1))) | |
1682 | { | |
1683 | loc = linemap_unwind_toward_expansion (set, loc, &map0); | |
1684 | resolved_loc = linemap_resolve_location (set, loc, | |
1685 | LRK_SPELLING_LOCATION, | |
1686 | &map1); | |
1687 | } | |
1688 | ||
1689 | if (map != NULL) | |
1690 | *map = map0; | |
1691 | return loc; | |
1692 | } | |
1693 | ||
46427374 | 1694 | /* Expand source code location LOC and return a user readable source |
84756fd4 DS |
1695 | code location. LOC must be a spelling (non-virtual) location. If |
1696 | it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source | |
1697 | location is returned. */ | |
46427374 TT |
1698 | |
1699 | expanded_location | |
84756fd4 DS |
1700 | linemap_expand_location (struct line_maps *set, |
1701 | const struct line_map *map, | |
46427374 TT |
1702 | source_location loc) |
1703 | ||
1704 | { | |
1705 | expanded_location xloc; | |
1706 | ||
84756fd4 | 1707 | memset (&xloc, 0, sizeof (xloc)); |
5368224f DC |
1708 | if (IS_ADHOC_LOC (loc)) |
1709 | { | |
acf601ae JJ |
1710 | xloc.data |
1711 | = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data; | |
ebedc9a3 | 1712 | loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; |
5368224f | 1713 | } |
84756fd4 DS |
1714 | |
1715 | if (loc < RESERVED_LOCATION_COUNT) | |
1716 | /* The location for this token wasn't generated from a line map. | |
1717 | It was probably a location for a builtin token, chosen by some | |
1718 | client code. Let's not try to expand the location in that | |
1719 | case. */; | |
1720 | else if (map == NULL) | |
1721 | /* We shouldn't be getting a NULL map with a location that is not | |
1722 | reserved by the client code. */ | |
1723 | abort (); | |
1724 | else | |
1725 | { | |
1726 | /* MAP must be an ordinary map and LOC must be non-virtual, | |
1727 | encoded into this map, obviously; the accessors used on MAP | |
1728 | below ensure it is ordinary. Let's just assert the | |
1729 | non-virtualness of LOC here. */ | |
1730 | if (linemap_location_from_macro_expansion_p (set, loc)) | |
1731 | abort (); | |
46427374 | 1732 | |
0e50b624 DM |
1733 | const line_map_ordinary *ord_map = linemap_check_ordinary (map); |
1734 | ||
1735 | xloc.file = LINEMAP_FILE (ord_map); | |
1736 | xloc.line = SOURCE_LINE (ord_map, loc); | |
1737 | xloc.column = SOURCE_COLUMN (ord_map, loc); | |
1738 | xloc.sysp = LINEMAP_SYSP (ord_map) != 0; | |
84756fd4 | 1739 | } |
46427374 | 1740 | |
46427374 | 1741 | return xloc; |
5993019d | 1742 | } |
847e697a | 1743 | |
8dcf72a8 DN |
1744 | |
1745 | /* Dump line map at index IX in line table SET to STREAM. If STREAM | |
1746 | is NULL, use stderr. IS_MACRO is true if the caller wants to | |
1747 | dump a macro map, false otherwise. */ | |
1748 | ||
1749 | void | |
1750 | linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro) | |
1751 | { | |
1752 | const char *lc_reasons_v[LC_ENTER_MACRO + 1] | |
1753 | = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM", | |
1754 | "LC_ENTER_MACRO" }; | |
1755 | const char *reason; | |
0e50b624 | 1756 | const line_map *map; |
8dcf72a8 DN |
1757 | |
1758 | if (stream == NULL) | |
1759 | stream = stderr; | |
1760 | ||
1761 | if (!is_macro) | |
1762 | map = LINEMAPS_ORDINARY_MAP_AT (set, ix); | |
1763 | else | |
1764 | map = LINEMAPS_MACRO_MAP_AT (set, ix); | |
1765 | ||
1766 | reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???"; | |
1767 | ||
1768 | fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n", | |
1769 | ix, (void *) map, map->start_location, reason, | |
0e50b624 DM |
1770 | ((!is_macro |
1771 | && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map))) | |
1772 | ? "yes" : "no")); | |
8dcf72a8 DN |
1773 | if (!is_macro) |
1774 | { | |
0e50b624 | 1775 | const line_map_ordinary *ord_map = linemap_check_ordinary (map); |
8dcf72a8 | 1776 | unsigned includer_ix; |
0e50b624 | 1777 | const line_map_ordinary *includer_map; |
8dcf72a8 | 1778 | |
0e50b624 | 1779 | includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map); |
8dcf72a8 DN |
1780 | includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set) |
1781 | ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix) | |
1782 | : NULL; | |
1783 | ||
0e50b624 DM |
1784 | fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map), |
1785 | ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map)); | |
8dcf72a8 DN |
1786 | fprintf (stream, "Included from: [%d] %s\n", includer_ix, |
1787 | includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None"); | |
1788 | } | |
1789 | else | |
0e50b624 DM |
1790 | { |
1791 | const line_map_macro *macro_map = linemap_check_macro (map); | |
1792 | fprintf (stream, "Macro: %s (%u tokens)\n", | |
1793 | linemap_map_get_macro_name (macro_map), | |
1794 | MACRO_MAP_NUM_MACRO_TOKENS (macro_map)); | |
1795 | } | |
8dcf72a8 DN |
1796 | |
1797 | fprintf (stream, "\n"); | |
1798 | } | |
1799 | ||
1800 | ||
847e697a TT |
1801 | /* Dump debugging information about source location LOC into the file |
1802 | stream STREAM. SET is the line map set LOC comes from. */ | |
1803 | ||
1804 | void | |
1805 | linemap_dump_location (struct line_maps *set, | |
1806 | source_location loc, | |
1807 | FILE *stream) | |
1808 | { | |
0e50b624 | 1809 | const line_map_ordinary *map; |
847e697a | 1810 | source_location location; |
84756fd4 DS |
1811 | const char *path = "", *from = ""; |
1812 | int l = -1, c = -1, s = -1, e = -1; | |
847e697a | 1813 | |
5368224f DC |
1814 | if (IS_ADHOC_LOC (loc)) |
1815 | loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
1816 | ||
847e697a TT |
1817 | if (loc == 0) |
1818 | return; | |
1819 | ||
1820 | location = | |
1821 | linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map); | |
847e697a | 1822 | |
84756fd4 DS |
1823 | if (map == NULL) |
1824 | /* Only reserved locations can be tolerated in this case. */ | |
1825 | linemap_assert (location < RESERVED_LOCATION_COUNT); | |
847e697a | 1826 | else |
84756fd4 DS |
1827 | { |
1828 | path = LINEMAP_FILE (map); | |
1829 | l = SOURCE_LINE (map, location); | |
1830 | c = SOURCE_COLUMN (map, location); | |
1831 | s = LINEMAP_SYSP (map) != 0; | |
1832 | e = location != loc; | |
1833 | if (e) | |
1834 | from = "N/A"; | |
1835 | else | |
1836 | from = (INCLUDED_FROM (set, map)) | |
1837 | ? LINEMAP_FILE (INCLUDED_FROM (set, map)) | |
1838 | : "<NULL>"; | |
1839 | } | |
847e697a TT |
1840 | |
1841 | /* P: path, L: line, C: column, S: in-system-header, M: map address, | |
84756fd4 DS |
1842 | E: macro expansion?, LOC: original location, R: resolved location */ |
1843 | fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}", | |
1844 | path, from, l, c, s, (void*)map, e, loc, location); | |
847e697a | 1845 | } |
64a1a422 | 1846 | |
7ecc3eb9 DS |
1847 | /* Return the highest location emitted for a given file for which |
1848 | there is a line map in SET. FILE_NAME is the file name to | |
1849 | consider. If the function returns TRUE, *LOC is set to the highest | |
1850 | location emitted for that file. */ | |
1851 | ||
1852 | bool | |
1853 | linemap_get_file_highest_location (struct line_maps *set, | |
1854 | const char *file_name, | |
1855 | source_location *loc) | |
1856 | { | |
1857 | /* If the set is empty or no ordinary map has been created then | |
1858 | there is no file to look for ... */ | |
1859 | if (set == NULL || set->info_ordinary.used == 0) | |
1860 | return false; | |
1861 | ||
1862 | /* Now look for the last ordinary map created for FILE_NAME. */ | |
1863 | int i; | |
1864 | for (i = set->info_ordinary.used - 1; i >= 0; --i) | |
1865 | { | |
0e50b624 | 1866 | const char *fname = set->info_ordinary.maps[i].to_file; |
7ecc3eb9 DS |
1867 | if (fname && !filename_cmp (fname, file_name)) |
1868 | break; | |
1869 | } | |
1870 | ||
1871 | if (i < 0) | |
1872 | return false; | |
1873 | ||
1874 | /* The highest location for a given map is either the starting | |
1875 | location of the next map minus one, or -- if the map is the | |
1876 | latest one -- the highest location of the set. */ | |
1877 | source_location result; | |
1878 | if (i == (int) set->info_ordinary.used - 1) | |
1879 | result = set->highest_location; | |
1880 | else | |
1881 | result = set->info_ordinary.maps[i + 1].start_location - 1; | |
1882 | ||
1883 | *loc = result; | |
1884 | return true; | |
1885 | } | |
1886 | ||
64a1a422 TT |
1887 | /* Compute and return statistics about the memory consumption of some |
1888 | parts of the line table SET. */ | |
1889 | ||
1890 | void | |
1891 | linemap_get_statistics (struct line_maps *set, | |
1892 | struct linemap_stats *s) | |
1893 | { | |
d17687f6 | 1894 | long ordinary_maps_allocated_size, ordinary_maps_used_size, |
64a1a422 TT |
1895 | macro_maps_allocated_size, macro_maps_used_size, |
1896 | macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0; | |
1897 | ||
0e50b624 | 1898 | const line_map_macro *cur_map; |
64a1a422 TT |
1899 | |
1900 | ordinary_maps_allocated_size = | |
0e50b624 | 1901 | LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary); |
64a1a422 TT |
1902 | |
1903 | ordinary_maps_used_size = | |
0e50b624 | 1904 | LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary); |
64a1a422 TT |
1905 | |
1906 | macro_maps_allocated_size = | |
0e50b624 | 1907 | LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro); |
64a1a422 TT |
1908 | |
1909 | for (cur_map = LINEMAPS_MACRO_MAPS (set); | |
1910 | cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set); | |
1911 | ++cur_map) | |
1912 | { | |
1913 | unsigned i; | |
1914 | ||
1915 | linemap_assert (linemap_macro_expansion_map_p (cur_map)); | |
1916 | ||
1917 | macro_maps_locations_size += | |
1918 | 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location); | |
1919 | ||
1920 | for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2) | |
1921 | { | |
1922 | if (MACRO_MAP_LOCATIONS (cur_map)[i] == | |
1923 | MACRO_MAP_LOCATIONS (cur_map)[i + 1]) | |
1924 | duplicated_macro_maps_locations_size += | |
1925 | sizeof (source_location); | |
1926 | } | |
1927 | } | |
1928 | ||
1929 | macro_maps_used_size = | |
0e50b624 | 1930 | LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro); |
64a1a422 TT |
1931 | |
1932 | s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set); | |
1933 | s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set); | |
1934 | s->ordinary_maps_allocated_size = ordinary_maps_allocated_size; | |
1935 | s->ordinary_maps_used_size = ordinary_maps_used_size; | |
1936 | s->num_expanded_macros = num_expanded_macros_counter; | |
1937 | s->num_macro_tokens = num_macro_tokens_counter; | |
1938 | s->num_macro_maps_used = LINEMAPS_MACRO_USED (set); | |
1939 | s->macro_maps_allocated_size = macro_maps_allocated_size; | |
1940 | s->macro_maps_locations_size = macro_maps_locations_size; | |
1941 | s->macro_maps_used_size = macro_maps_used_size; | |
1942 | s->duplicated_macro_maps_locations_size = | |
1943 | duplicated_macro_maps_locations_size; | |
ee015909 DM |
1944 | s->adhoc_table_size = (set->location_adhoc_data_map.allocated |
1945 | * sizeof (struct location_adhoc_data)); | |
1946 | s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc; | |
64a1a422 | 1947 | } |
8dcf72a8 DN |
1948 | |
1949 | ||
1950 | /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used. | |
1951 | NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO | |
1952 | specifies how many macro maps to dump. */ | |
1953 | ||
1954 | void | |
1955 | line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary, | |
1956 | unsigned int num_macro) | |
1957 | { | |
1958 | unsigned int i; | |
1959 | ||
1960 | if (set == NULL) | |
1961 | return; | |
1962 | ||
1963 | if (stream == NULL) | |
1964 | stream = stderr; | |
1965 | ||
1966 | fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set)); | |
1967 | fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set)); | |
1968 | fprintf (stream, "Include stack depth: %d\n", set->depth); | |
1969 | fprintf (stream, "Highest location: %u\n", set->highest_location); | |
1970 | ||
1971 | if (num_ordinary) | |
1972 | { | |
1973 | fprintf (stream, "\nOrdinary line maps\n"); | |
1974 | for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++) | |
1975 | linemap_dump (stream, set, i, false); | |
1976 | fprintf (stream, "\n"); | |
1977 | } | |
1978 | ||
1979 | if (num_macro) | |
1980 | { | |
1981 | fprintf (stream, "\nMacro line maps\n"); | |
1982 | for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++) | |
1983 | linemap_dump (stream, set, i, true); | |
1984 | fprintf (stream, "\n"); | |
1985 | } | |
1986 | } | |
8a645150 | 1987 | |
a87a86e1 DM |
1988 | /* struct source_range. */ |
1989 | ||
1990 | /* Is there any part of this range on the given line? */ | |
1991 | ||
1992 | bool | |
1993 | source_range::intersects_line_p (const char *file, int line) const | |
1994 | { | |
1995 | expanded_location exploc_start | |
1996 | = linemap_client_expand_location_to_spelling_point (m_start); | |
1997 | if (file != exploc_start.file) | |
1998 | return false; | |
1999 | if (line < exploc_start.line) | |
2000 | return false; | |
2001 | expanded_location exploc_finish | |
2002 | = linemap_client_expand_location_to_spelling_point (m_finish); | |
2003 | if (file != exploc_finish.file) | |
2004 | return false; | |
2005 | if (line > exploc_finish.line) | |
2006 | return false; | |
2007 | return true; | |
2008 | } | |
2009 | ||
8a645150 DM |
2010 | /* class rich_location. */ |
2011 | ||
2012 | /* Construct a rich_location with location LOC as its initial range. */ | |
2013 | ||
ee908516 DM |
2014 | rich_location::rich_location (line_maps *set, source_location loc) : |
2015 | m_line_table (set), | |
b816477a | 2016 | m_ranges (), |
40499f81 | 2017 | m_column_override (0), |
a87a86e1 | 2018 | m_have_expanded_location (false), |
b816477a | 2019 | m_fixit_hints (), |
ee908516 | 2020 | m_seen_impossible_fixit (false) |
8a645150 | 2021 | { |
40499f81 | 2022 | add_range (loc, true); |
8a645150 DM |
2023 | } |
2024 | ||
a87a86e1 DM |
2025 | /* The destructor for class rich_location. */ |
2026 | ||
2027 | rich_location::~rich_location () | |
2028 | { | |
b816477a DM |
2029 | for (unsigned int i = 0; i < m_fixit_hints.count (); i++) |
2030 | delete get_fixit_hint (i); | |
a87a86e1 DM |
2031 | } |
2032 | ||
40499f81 DM |
2033 | /* Get location IDX within this rich_location. */ |
2034 | ||
2035 | source_location | |
2036 | rich_location::get_loc (unsigned int idx) const | |
2037 | { | |
b816477a DM |
2038 | const location_range *locrange = get_range (idx); |
2039 | return locrange->m_loc; | |
2040 | } | |
2041 | ||
2042 | /* Get range IDX within this rich_location. */ | |
2043 | ||
2044 | const location_range * | |
2045 | rich_location::get_range (unsigned int idx) const | |
2046 | { | |
2047 | return &m_ranges[idx]; | |
2048 | } | |
2049 | ||
2050 | /* Mutable access to range IDX within this rich_location. */ | |
2051 | ||
2052 | location_range * | |
2053 | rich_location::get_range (unsigned int idx) | |
2054 | { | |
2055 | return &m_ranges[idx]; | |
40499f81 DM |
2056 | } |
2057 | ||
2058 | /* Expand location IDX within this rich_location. */ | |
8a645150 DM |
2059 | /* Get an expanded_location for this rich_location's primary |
2060 | location. */ | |
2061 | ||
2062 | expanded_location | |
40499f81 | 2063 | rich_location::get_expanded_location (unsigned int idx) |
8a645150 | 2064 | { |
40499f81 DM |
2065 | if (idx == 0) |
2066 | { | |
2067 | /* Cache the expansion of the primary location. */ | |
2068 | if (!m_have_expanded_location) | |
2069 | { | |
2070 | m_expanded_location | |
2071 | = linemap_client_expand_location_to_spelling_point (get_loc (0)); | |
2072 | if (m_column_override) | |
2073 | m_expanded_location.column = m_column_override; | |
2074 | m_have_expanded_location = true; | |
2075 | } | |
2076 | ||
2077 | return m_expanded_location; | |
2078 | } | |
2079 | else | |
2080 | return linemap_client_expand_location_to_spelling_point (get_loc (idx)); | |
8a645150 DM |
2081 | } |
2082 | ||
40499f81 DM |
2083 | /* Set the column of the primary location, with 0 meaning |
2084 | "don't override it". */ | |
8a645150 DM |
2085 | |
2086 | void | |
2087 | rich_location::override_column (int column) | |
2088 | { | |
40499f81 DM |
2089 | m_column_override = column; |
2090 | m_have_expanded_location = false; | |
8a645150 DM |
2091 | } |
2092 | ||
2093 | /* Add the given range. */ | |
2094 | ||
2095 | void | |
40499f81 | 2096 | rich_location::add_range (source_location loc, bool show_caret_p) |
8a645150 | 2097 | { |
b816477a DM |
2098 | location_range range; |
2099 | range.m_loc = loc; | |
2100 | range.m_show_caret_p = show_caret_p; | |
2101 | m_ranges.push (range); | |
8a645150 DM |
2102 | } |
2103 | ||
f79520bb DM |
2104 | /* Add or overwrite the location given by IDX, setting its location to LOC, |
2105 | and setting its "should my caret be printed" flag to SHOW_CARET_P. | |
8a645150 | 2106 | |
f79520bb DM |
2107 | It must either overwrite an existing location, or add one *exactly* on |
2108 | the end of the array. | |
8a645150 | 2109 | |
f79520bb DM |
2110 | This is primarily for use by gcc when implementing diagnostic format |
2111 | decoders e.g. | |
2112 | - the "+" in the C/C++ frontends, for handling format codes like "%q+D" | |
2113 | (which writes the source location of a tree back into location 0 of | |
2114 | the rich_location), and | |
2115 | - the "%C" and "%L" format codes in the Fortran frontend. */ | |
8a645150 DM |
2116 | |
2117 | void | |
40499f81 | 2118 | rich_location::set_range (line_maps * /*set*/, unsigned int idx, |
f79520bb | 2119 | source_location loc, bool show_caret_p) |
8a645150 | 2120 | { |
8a645150 DM |
2121 | /* We can either overwrite an existing range, or add one exactly |
2122 | on the end of the array. */ | |
b816477a | 2123 | linemap_assert (idx <= m_ranges.count ()); |
8a645150 | 2124 | |
b816477a DM |
2125 | if (idx == m_ranges.count ()) |
2126 | add_range (loc, show_caret_p); | |
2127 | else | |
2128 | { | |
2129 | location_range *locrange = get_range (idx); | |
2130 | locrange->m_loc = loc; | |
2131 | locrange->m_show_caret_p = show_caret_p; | |
2132 | } | |
8a645150 | 2133 | |
f79520bb | 2134 | if (idx == 0) |
40499f81 DM |
2135 | /* Mark any cached value here as dirty. */ |
2136 | m_have_expanded_location = false; | |
8a645150 | 2137 | } |
a87a86e1 | 2138 | |
f9087798 DM |
2139 | /* Methods for adding insertion fix-it hints. */ |
2140 | ||
2141 | /* Add a fixit-hint, suggesting insertion of NEW_CONTENT | |
254830ba | 2142 | immediately before the primary range's start location. */ |
f9087798 DM |
2143 | |
2144 | void | |
254830ba | 2145 | rich_location::add_fixit_insert_before (const char *new_content) |
f9087798 | 2146 | { |
254830ba | 2147 | add_fixit_insert_before (get_loc (), new_content); |
f9087798 DM |
2148 | } |
2149 | ||
a87a86e1 | 2150 | /* Add a fixit-hint, suggesting insertion of NEW_CONTENT |
254830ba | 2151 | immediately before the start of WHERE. */ |
a87a86e1 DM |
2152 | |
2153 | void | |
254830ba DM |
2154 | rich_location::add_fixit_insert_before (source_location where, |
2155 | const char *new_content) | |
a87a86e1 | 2156 | { |
254830ba | 2157 | source_location start = get_range_from_loc (m_line_table, where).m_start; |
2aa51413 | 2158 | |
254830ba | 2159 | if (reject_impossible_fixit (start)) |
ee908516 | 2160 | return; |
31316208 DM |
2161 | /* We do not yet support newlines within fix-it hints. */ |
2162 | if (strchr (new_content, '\n')) | |
2163 | { | |
2164 | stop_supporting_fixits (); | |
2165 | return; | |
2166 | } | |
254830ba DM |
2167 | add_fixit (new fixit_insert (start, new_content)); |
2168 | } | |
2169 | ||
2170 | /* Add a fixit-hint, suggesting insertion of NEW_CONTENT | |
2171 | immediately after the primary range's end-point. */ | |
2172 | ||
2173 | void | |
2174 | rich_location::add_fixit_insert_after (const char *new_content) | |
2175 | { | |
2176 | add_fixit_insert_after (get_loc (), new_content); | |
2177 | } | |
2178 | ||
2179 | /* Add a fixit-hint, suggesting insertion of NEW_CONTENT | |
2180 | immediately after the end-point of WHERE. */ | |
2181 | ||
2182 | void | |
2183 | rich_location::add_fixit_insert_after (source_location where, | |
2184 | const char *new_content) | |
2185 | { | |
2186 | source_location finish = get_range_from_loc (m_line_table, where).m_finish; | |
2187 | ||
2188 | if (reject_impossible_fixit (finish)) | |
2189 | return; | |
2190 | ||
2191 | source_location next_loc | |
2192 | = linemap_position_for_loc_and_offset (m_line_table, finish, 1); | |
2193 | ||
2194 | /* linemap_position_for_loc_and_offset can fail, if so, it returns | |
2195 | its input value. */ | |
2196 | if (next_loc == finish) | |
2197 | { | |
2198 | stop_supporting_fixits (); | |
2199 | return; | |
2200 | } | |
2201 | ||
2202 | add_fixit (new fixit_insert (next_loc, new_content)); | |
a87a86e1 DM |
2203 | } |
2204 | ||
f9087798 DM |
2205 | /* Methods for adding removal fix-it hints. */ |
2206 | ||
2207 | /* Add a fixit-hint, suggesting removal of the content covered | |
2208 | by range 0. */ | |
2209 | ||
2210 | void | |
2211 | rich_location::add_fixit_remove () | |
2212 | { | |
2213 | add_fixit_remove (get_loc ()); | |
2214 | } | |
2215 | ||
2216 | /* Add a fixit-hint, suggesting removal of the content between | |
2217 | the start and finish of WHERE. */ | |
2218 | ||
2219 | void | |
2220 | rich_location::add_fixit_remove (source_location where) | |
2221 | { | |
2222 | source_range range = get_range_from_loc (m_line_table, where); | |
2223 | add_fixit_remove (range); | |
2224 | } | |
2225 | ||
a87a86e1 DM |
2226 | /* Add a fixit-hint, suggesting removal of the content at |
2227 | SRC_RANGE. */ | |
2228 | ||
2229 | void | |
2230 | rich_location::add_fixit_remove (source_range src_range) | |
2231 | { | |
2ffe0809 | 2232 | add_fixit_replace (src_range, ""); |
a87a86e1 DM |
2233 | } |
2234 | ||
ee908516 DM |
2235 | /* Return true iff A is in the column directly before B, on the |
2236 | same line of the same source file. */ | |
2237 | ||
2238 | static bool | |
2239 | column_before_p (line_maps *set, source_location a, source_location b) | |
2240 | { | |
2241 | if (IS_ADHOC_LOC (a)) | |
2242 | a = get_location_from_adhoc_loc (set, a); | |
2243 | if (IS_ADHOC_LOC (b)) | |
2244 | b = get_location_from_adhoc_loc (set, b); | |
2245 | ||
2246 | /* They must both be in ordinary maps. */ | |
2247 | const struct line_map *linemap_a = linemap_lookup (set, a); | |
2248 | if (linemap_macro_expansion_map_p (linemap_a)) | |
2249 | return false; | |
2250 | const struct line_map *linemap_b = linemap_lookup (set, b); | |
2251 | if (linemap_macro_expansion_map_p (linemap_b)) | |
2252 | return false; | |
2253 | ||
2254 | /* To be on the same line, they must be in the same ordinary map. */ | |
2255 | if (linemap_a != linemap_b) | |
2256 | return false; | |
2257 | ||
2258 | linenum_type line_a | |
2259 | = SOURCE_LINE (linemap_check_ordinary (linemap_a), a); | |
2260 | linenum_type line_b | |
2261 | = SOURCE_LINE (linemap_check_ordinary (linemap_b), b); | |
2262 | if (line_a != line_b) | |
2263 | return false; | |
2264 | ||
2265 | linenum_type column_a | |
2266 | = SOURCE_COLUMN (linemap_check_ordinary (linemap_a), a); | |
2267 | linenum_type column_b | |
2268 | = SOURCE_COLUMN (linemap_check_ordinary (linemap_b), b); | |
2269 | ||
2270 | return column_b == column_a + 1; | |
2271 | } | |
2272 | ||
f9087798 DM |
2273 | /* Add a fixit-hint, suggesting replacement of the content covered |
2274 | by range 0 with NEW_CONTENT. */ | |
2275 | ||
2276 | void | |
2277 | rich_location::add_fixit_replace (const char *new_content) | |
2278 | { | |
2279 | add_fixit_replace (get_loc (), new_content); | |
2280 | } | |
2281 | ||
2282 | /* Methods for adding "replace" fix-it hints. */ | |
2283 | ||
2284 | /* Add a fixit-hint, suggesting replacement of the content between | |
2285 | the start and finish of WHERE with NEW_CONTENT. */ | |
2286 | ||
2287 | void | |
2288 | rich_location::add_fixit_replace (source_location where, | |
2289 | const char *new_content) | |
2290 | { | |
2291 | source_range range = get_range_from_loc (m_line_table, where); | |
2292 | add_fixit_replace (range, new_content); | |
2293 | } | |
2294 | ||
a87a86e1 DM |
2295 | /* Add a fixit-hint, suggesting replacement of the content at |
2296 | SRC_RANGE with NEW_CONTENT. */ | |
2297 | ||
2298 | void | |
2299 | rich_location::add_fixit_replace (source_range src_range, | |
2300 | const char *new_content) | |
2301 | { | |
2aa51413 DM |
2302 | src_range.m_start = get_pure_location (m_line_table, src_range.m_start); |
2303 | src_range.m_finish = get_pure_location (m_line_table, src_range.m_finish); | |
2304 | ||
ee908516 DM |
2305 | if (reject_impossible_fixit (src_range.m_start)) |
2306 | return; | |
2307 | if (reject_impossible_fixit (src_range.m_finish)) | |
2308 | return; | |
2309 | ||
31316208 DM |
2310 | /* We do not yet support newlines within fix-it hints. */ |
2311 | if (strchr (new_content, '\n')) | |
2312 | { | |
2313 | stop_supporting_fixits (); | |
2314 | return; | |
2315 | } | |
2316 | ||
ee908516 DM |
2317 | /* Consolidate neighboring fixits. */ |
2318 | fixit_hint *prev = get_last_fixit_hint (); | |
b816477a DM |
2319 | if (prev) |
2320 | if (prev->maybe_append_replace (m_line_table, src_range, new_content)) | |
2321 | return; | |
ee908516 | 2322 | |
b816477a | 2323 | add_fixit (new fixit_replace (src_range, new_content)); |
a87a86e1 DM |
2324 | } |
2325 | ||
ee908516 DM |
2326 | /* Get the last fix-it hint within this rich_location, or NULL if none. */ |
2327 | ||
2328 | fixit_hint * | |
2329 | rich_location::get_last_fixit_hint () const | |
2330 | { | |
b816477a DM |
2331 | if (m_fixit_hints.count () > 0) |
2332 | return get_fixit_hint (m_fixit_hints.count () - 1); | |
ee908516 DM |
2333 | else |
2334 | return NULL; | |
2335 | } | |
2336 | ||
2337 | /* If WHERE is an "awkward" location, then mark this rich_location as not | |
2338 | supporting fixits, purging any thay were already added, and return true. | |
2339 | ||
2340 | Otherwise (the common case), return false. */ | |
2341 | ||
2342 | bool | |
2343 | rich_location::reject_impossible_fixit (source_location where) | |
2344 | { | |
2345 | /* Fix-its within a rich_location should either all be suggested, or | |
2346 | none of them should be suggested. | |
2347 | Once we've rejected a fixit, we reject any more, even those | |
2348 | with reasonable locations. */ | |
2349 | if (m_seen_impossible_fixit) | |
2350 | return true; | |
2351 | ||
2352 | if (where <= LINE_MAP_MAX_LOCATION_WITH_COLS) | |
2353 | /* WHERE is a reasonable location for a fix-it; don't reject it. */ | |
2354 | return false; | |
2355 | ||
2356 | /* Otherwise we have an attempt to add a fix-it with an "awkward" | |
2357 | location: either one that we can't obtain column information | |
2358 | for (within an ordinary map), or one within a macro expansion. */ | |
254830ba DM |
2359 | stop_supporting_fixits (); |
2360 | return true; | |
2361 | } | |
2362 | ||
2363 | /* Mark this rich_location as not supporting fixits, purging any that were | |
2364 | already added. */ | |
2365 | ||
2366 | void | |
2367 | rich_location::stop_supporting_fixits () | |
2368 | { | |
ee908516 DM |
2369 | m_seen_impossible_fixit = true; |
2370 | ||
2371 | /* Purge the rich_location of any fix-its that were already added. */ | |
b816477a DM |
2372 | for (unsigned int i = 0; i < m_fixit_hints.count (); i++) |
2373 | delete get_fixit_hint (i); | |
2374 | m_fixit_hints.truncate (0); | |
ee908516 DM |
2375 | } |
2376 | ||
b816477a DM |
2377 | /* Add HINT to the fix-it hints in this rich_location. */ |
2378 | ||
2379 | void | |
2380 | rich_location::add_fixit (fixit_hint *hint) | |
2381 | { | |
2382 | m_fixit_hints.push (hint); | |
2383 | } | |
2384 | ||
a87a86e1 DM |
2385 | /* class fixit_insert. */ |
2386 | ||
2387 | fixit_insert::fixit_insert (source_location where, | |
2388 | const char *new_content) | |
2389 | : m_where (where), | |
2390 | m_bytes (xstrdup (new_content)), | |
2391 | m_len (strlen (new_content)) | |
2392 | { | |
2393 | } | |
2394 | ||
2395 | fixit_insert::~fixit_insert () | |
2396 | { | |
2397 | free (m_bytes); | |
2398 | } | |
2399 | ||
2400 | /* Implementation of fixit_hint::affects_line_p for fixit_insert. */ | |
2401 | ||
2402 | bool | |
3d4f9f87 | 2403 | fixit_insert::affects_line_p (const char *file, int line) const |
a87a86e1 DM |
2404 | { |
2405 | expanded_location exploc | |
2406 | = linemap_client_expand_location_to_spelling_point (m_where); | |
2407 | if (file == exploc.file) | |
2408 | if (line == exploc.line) | |
2409 | return true; | |
2410 | return false; | |
2411 | } | |
2412 | ||
ee908516 DM |
2413 | /* Implementation of maybe_append_replace for fixit_insert. Reject |
2414 | the attempt to consolidate fix-its. */ | |
2415 | ||
2416 | bool | |
2417 | fixit_insert::maybe_append_replace (line_maps *, source_range, const char *) | |
2418 | { | |
2419 | return false; | |
2420 | } | |
2421 | ||
a87a86e1 DM |
2422 | /* class fixit_replace. */ |
2423 | ||
2424 | fixit_replace::fixit_replace (source_range src_range, | |
2425 | const char *new_content) | |
2426 | : m_src_range (src_range), | |
2427 | m_bytes (xstrdup (new_content)), | |
2428 | m_len (strlen (new_content)) | |
2429 | { | |
2430 | } | |
2431 | ||
2432 | fixit_replace::~fixit_replace () | |
2433 | { | |
2434 | free (m_bytes); | |
2435 | } | |
2436 | ||
2437 | /* Implementation of fixit_hint::affects_line_p for fixit_replace. */ | |
2438 | ||
2439 | bool | |
3d4f9f87 | 2440 | fixit_replace::affects_line_p (const char *file, int line) const |
a87a86e1 DM |
2441 | { |
2442 | return m_src_range.intersects_line_p (file, line); | |
2443 | } | |
ee908516 DM |
2444 | |
2445 | /* Implementation of maybe_append_replace for fixit_replace. If | |
2446 | possible, merge the new replacement into this one and return true. | |
2447 | Otherwise return false. */ | |
2448 | ||
2449 | bool | |
2450 | fixit_replace::maybe_append_replace (line_maps *set, | |
2451 | source_range src_range, | |
2452 | const char *new_content) | |
2453 | { | |
2454 | /* Does SRC_RANGE start immediately after this one finishes? */ | |
2455 | if (!column_before_p (set, m_src_range.m_finish, src_range.m_start)) | |
2456 | return false; | |
2457 | ||
2458 | /* We have neighboring replacements; merge them. */ | |
2459 | m_src_range.m_finish = src_range.m_finish; | |
2460 | size_t extra_len = strlen (new_content); | |
2461 | m_bytes = (char *)xrealloc (m_bytes, m_len + extra_len + 1); | |
2462 | memcpy (m_bytes + m_len, new_content, extra_len); | |
2463 | m_len += extra_len; | |
2464 | m_bytes[m_len] = '\0'; | |
2465 | return true; | |
2466 | } |