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