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