]>
Commit | Line | Data |
---|---|---|
78536ab7 | 1 | /* Map (unsigned int) keys to (source file, line, column) triples. |
d353bf18 | 2 | Copyright (C) 2001-2015 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 | |
551e34da | 29 | static void trace_include (const struct line_maps *, const line_map_ordinary *); |
30 | static const line_map_ordinary * linemap_ordinary_map_lookup (struct line_maps *, | |
31 | source_location); | |
32 | static const line_map_macro* linemap_macro_map_lookup (struct line_maps *, | |
33 | source_location); | |
97bfb9ef | 34 | static source_location linemap_macro_map_loc_to_def_point |
551e34da | 35 | (const line_map_macro *, source_location); |
97bfb9ef | 36 | static source_location linemap_macro_map_loc_unwind_toward_spelling |
551e34da | 37 | (const line_map_macro *, source_location); |
97bfb9ef | 38 | static source_location linemap_macro_map_loc_to_exp_point |
551e34da | 39 | (const line_map_macro *, source_location); |
97bfb9ef | 40 | static source_location linemap_macro_loc_to_spelling_point |
551e34da | 41 | (struct line_maps *, source_location, const line_map_ordinary **); |
97bfb9ef | 42 | static source_location linemap_macro_loc_to_def_point (struct line_maps *, |
43 | source_location, | |
551e34da | 44 | const line_map_ordinary **); |
97bfb9ef | 45 | static source_location linemap_macro_loc_to_exp_point (struct line_maps *, |
46 | source_location, | |
551e34da | 47 | const line_map_ordinary **); |
438ac94c | 48 | |
e77b8253 | 49 | /* Counters defined in macro.c. */ |
50 | extern unsigned num_expanded_macros_counter; | |
51 | extern unsigned num_macro_tokens_counter; | |
52 | ||
5169661d | 53 | /* Hash function for location_adhoc_data hashtable. */ |
54 | ||
55 | static hashval_t | |
56 | location_adhoc_data_hash (const void *l) | |
57 | { | |
58 | const struct location_adhoc_data *lb = | |
59 | (const struct location_adhoc_data *) l; | |
bda16c7c | 60 | return (hashval_t) lb->locus + (size_t) lb->data; |
5169661d | 61 | } |
62 | ||
63 | /* Compare function for location_adhoc_data hashtable. */ | |
64 | ||
65 | static int | |
66 | location_adhoc_data_eq (const void *l1, const void *l2) | |
67 | { | |
68 | const struct location_adhoc_data *lb1 = | |
69 | (const struct location_adhoc_data *) l1; | |
70 | const struct location_adhoc_data *lb2 = | |
71 | (const struct location_adhoc_data *) l2; | |
72 | return lb1->locus == lb2->locus && lb1->data == lb2->data; | |
73 | } | |
74 | ||
75 | /* Update the hashtable when location_adhoc_data is reallocated. */ | |
76 | ||
77 | static int | |
78 | location_adhoc_data_update (void **slot, void *data) | |
79 | { | |
80 | *((char **) slot) += *((long long *) data); | |
81 | return 1; | |
82 | } | |
83 | ||
5c1946c8 | 84 | /* Rebuild the hash table from the location adhoc data. */ |
85 | ||
86 | void | |
87 | rebuild_location_adhoc_htab (struct line_maps *set) | |
88 | { | |
89 | unsigned i; | |
90 | set->location_adhoc_data_map.htab = | |
91 | htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL); | |
92 | for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++) | |
93 | htab_find_slot (set->location_adhoc_data_map.htab, | |
94 | set->location_adhoc_data_map.data + i, INSERT); | |
95 | } | |
96 | ||
5169661d | 97 | /* Combine LOCUS and DATA to a combined adhoc loc. */ |
98 | ||
99 | source_location | |
100 | get_combined_adhoc_loc (struct line_maps *set, | |
101 | source_location locus, void *data) | |
102 | { | |
103 | struct location_adhoc_data lb; | |
104 | struct location_adhoc_data **slot; | |
105 | ||
106 | linemap_assert (data); | |
107 | ||
108 | if (IS_ADHOC_LOC (locus)) | |
6e5a7913 | 109 | locus |
110 | = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus; | |
5169661d | 111 | if (locus == 0 && data == NULL) |
112 | return 0; | |
113 | lb.locus = locus; | |
114 | lb.data = data; | |
115 | slot = (struct location_adhoc_data **) | |
116 | htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT); | |
117 | if (*slot == NULL) | |
118 | { | |
119 | if (set->location_adhoc_data_map.curr_loc >= | |
120 | set->location_adhoc_data_map.allocated) | |
121 | { | |
122 | char *orig_data = (char *) set->location_adhoc_data_map.data; | |
123 | long long offset; | |
de2e6b8a | 124 | /* Cast away extern "C" from the type of xrealloc. */ |
125 | line_map_realloc reallocator = (set->reallocator | |
126 | ? set->reallocator | |
127 | : (line_map_realloc) xrealloc); | |
5c1946c8 | 128 | |
129 | if (set->location_adhoc_data_map.allocated == 0) | |
130 | set->location_adhoc_data_map.allocated = 128; | |
131 | else | |
132 | set->location_adhoc_data_map.allocated *= 2; | |
133 | set->location_adhoc_data_map.data = (struct location_adhoc_data *) | |
134 | reallocator (set->location_adhoc_data_map.data, | |
135 | set->location_adhoc_data_map.allocated | |
136 | * sizeof (struct location_adhoc_data)); | |
5169661d | 137 | offset = (char *) (set->location_adhoc_data_map.data) - orig_data; |
5c1946c8 | 138 | if (set->location_adhoc_data_map.allocated > 128) |
139 | htab_traverse (set->location_adhoc_data_map.htab, | |
140 | location_adhoc_data_update, &offset); | |
5169661d | 141 | } |
142 | *slot = set->location_adhoc_data_map.data | |
143 | + set->location_adhoc_data_map.curr_loc; | |
6e5a7913 | 144 | set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++] |
145 | = lb; | |
5169661d | 146 | } |
147 | return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000; | |
148 | } | |
149 | ||
150 | /* Return the data for the adhoc loc. */ | |
151 | ||
152 | void * | |
153 | get_data_from_adhoc_loc (struct line_maps *set, source_location loc) | |
154 | { | |
155 | linemap_assert (IS_ADHOC_LOC (loc)); | |
156 | return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data; | |
157 | } | |
158 | ||
159 | /* Return the location for the adhoc loc. */ | |
160 | ||
161 | source_location | |
162 | get_location_from_adhoc_loc (struct line_maps *set, source_location loc) | |
163 | { | |
164 | linemap_assert (IS_ADHOC_LOC (loc)); | |
165 | return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
166 | } | |
167 | ||
5169661d | 168 | /* Finalize the location_adhoc_data structure. */ |
169 | void | |
170 | location_adhoc_data_fini (struct line_maps *set) | |
171 | { | |
5169661d | 172 | htab_delete (set->location_adhoc_data_map.htab); |
173 | } | |
174 | ||
38692459 | 175 | /* Initialize a line map set. */ |
176 | ||
177 | void | |
a4cfdfed | 178 | linemap_init (struct line_maps *set, |
179 | source_location builtin_location) | |
38692459 | 180 | { |
97bfb9ef | 181 | memset (set, 0, sizeof (struct line_maps)); |
af4d2883 | 182 | set->highest_location = RESERVED_LOCATION_COUNT - 1; |
183 | set->highest_line = RESERVED_LOCATION_COUNT - 1; | |
5c1946c8 | 184 | set->location_adhoc_data_map.htab = |
185 | htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL); | |
a4cfdfed | 186 | set->builtin_location = builtin_location; |
38692459 | 187 | } |
188 | ||
7c2df241 | 189 | /* Check for and warn about line_maps entered but not exited. */ |
610625e3 | 190 | |
191 | void | |
192 | linemap_check_files_exited (struct line_maps *set) | |
193 | { | |
551e34da | 194 | const line_map_ordinary *map; |
610625e3 | 195 | /* Depending upon whether we are handling preprocessed input or |
196 | not, this can be a user error or an ICE. */ | |
97bfb9ef | 197 | for (map = LINEMAPS_LAST_ORDINARY_MAP (set); |
198 | ! MAIN_FILE_P (map); | |
610625e3 | 199 | map = INCLUDED_FROM (set, map)) |
200 | fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n", | |
97bfb9ef | 201 | ORDINARY_MAP_FILE_NAME (map)); |
610625e3 | 202 | } |
38692459 | 203 | |
97bfb9ef | 204 | /* Create a new line map in the line map set SET, and return it. |
205 | REASON is the reason of creating the map. It determines the type | |
206 | of map created (ordinary or macro map). Note that ordinary maps and | |
207 | macro maps are allocated in different memory location. */ | |
208 | ||
209 | static struct line_map * | |
210 | new_linemap (struct line_maps *set, | |
211 | enum lc_reason reason) | |
38692459 | 212 | { |
97bfb9ef | 213 | /* Depending on this variable, a macro map would be allocated in a |
214 | different memory location than an ordinary map. */ | |
215 | bool macro_map_p = (reason == LC_ENTER_MACRO); | |
216 | struct line_map *result; | |
217 | ||
218 | if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p)) | |
bd507c05 | 219 | { |
97bfb9ef | 220 | /* We ran out of allocated line maps. Let's allocate more. */ |
1ae3520e | 221 | unsigned alloc_size; |
f85fcf2b | 222 | |
de2e6b8a | 223 | /* Cast away extern "C" from the type of xrealloc. */ |
224 | line_map_realloc reallocator = (set->reallocator | |
225 | ? set->reallocator | |
226 | : (line_map_realloc) xrealloc); | |
1ae3520e | 227 | line_map_round_alloc_size_func round_alloc_size = |
228 | set->round_alloc_size; | |
229 | ||
551e34da | 230 | size_t map_size = (macro_map_p |
231 | ? sizeof (line_map_macro) | |
232 | : sizeof (line_map_ordinary)); | |
233 | ||
1ae3520e | 234 | /* We are going to execute some dance to try to reduce the |
235 | overhead of the memory allocator, in case we are using the | |
236 | ggc-page.c one. | |
237 | ||
238 | The actual size of memory we are going to get back from the | |
239 | allocator is the smallest power of 2 that is greater than the | |
240 | size we requested. So let's consider that size then. */ | |
241 | ||
242 | alloc_size = | |
243 | (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256) | |
551e34da | 244 | * map_size; |
1ae3520e | 245 | |
246 | /* Get the actual size of memory that is going to be allocated | |
247 | by the allocator. */ | |
248 | alloc_size = round_alloc_size (alloc_size); | |
249 | ||
250 | /* Now alloc_size contains the exact memory size we would get if | |
251 | we have asked for the initial alloc_size amount of memory. | |
252 | Let's get back to the number of macro map that amounts | |
253 | to. */ | |
97bfb9ef | 254 | LINEMAPS_ALLOCATED (set, macro_map_p) = |
551e34da | 255 | alloc_size / map_size; |
1ae3520e | 256 | |
257 | /* And now let's really do the re-allocation. */ | |
551e34da | 258 | if (macro_map_p) |
259 | { | |
260 | set->info_macro.maps | |
261 | = (line_map_macro *) (*reallocator) (set->info_macro.maps, | |
262 | (LINEMAPS_ALLOCATED (set, macro_map_p) | |
263 | * map_size)); | |
264 | result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)]; | |
265 | } | |
266 | else | |
267 | { | |
268 | set->info_ordinary.maps = | |
269 | (line_map_ordinary *) (*reallocator) (set->info_ordinary.maps, | |
270 | (LINEMAPS_ALLOCATED (set, macro_map_p) | |
271 | * map_size)); | |
272 | result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)]; | |
273 | } | |
97bfb9ef | 274 | memset (result, 0, |
275 | ((LINEMAPS_ALLOCATED (set, macro_map_p) | |
276 | - LINEMAPS_USED (set, macro_map_p)) | |
551e34da | 277 | * map_size)); |
bd507c05 | 278 | } |
97bfb9ef | 279 | else |
551e34da | 280 | { |
281 | if (macro_map_p) | |
282 | result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)]; | |
283 | else | |
284 | result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)]; | |
285 | } | |
97bfb9ef | 286 | |
287 | LINEMAPS_USED (set, macro_map_p)++; | |
288 | ||
289 | result->reason = reason; | |
290 | return result; | |
38692459 | 291 | } |
292 | ||
293 | /* Add a mapping of logical source line to physical source file and | |
748b50a3 | 294 | line number. |
295 | ||
296 | The text pointed to by TO_FILE must have a lifetime | |
297 | at least as long as the final call to lookup_line (). An empty | |
298 | TO_FILE means standard input. If reason is LC_LEAVE, and | |
299 | TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their | |
300 | natural values considering the file we are returning to. | |
38692459 | 301 | |
302 | FROM_LINE should be monotonic increasing across calls to this | |
748b50a3 | 303 | function. A call to this function can relocate the previous set of |
304 | maps, so any stored line_map pointers should not be used. */ | |
38692459 | 305 | |
f85fcf2b | 306 | const struct line_map * |
196ce2be | 307 | linemap_add (struct line_maps *set, enum lc_reason reason, |
4999c35b | 308 | unsigned int sysp, const char *to_file, linenum_type to_line) |
38692459 | 309 | { |
610625e3 | 310 | source_location start_location = set->highest_location + 1; |
38692459 | 311 | |
97bfb9ef | 312 | linemap_assert (!(LINEMAPS_ORDINARY_USED (set) |
313 | && (start_location | |
314 | < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set))))); | |
315 | ||
316 | /* When we enter the file for the first time reason cannot be | |
317 | LC_RENAME. */ | |
318 | linemap_assert (!(set->depth == 0 && reason == LC_RENAME)); | |
38692459 | 319 | |
97bfb9ef | 320 | /* If we are leaving the main file, return a NULL map. */ |
321 | if (reason == LC_LEAVE | |
322 | && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set)) | |
323 | && to_file == NULL) | |
38692459 | 324 | { |
97bfb9ef | 325 | set->depth--; |
326 | return NULL; | |
38692459 | 327 | } |
328 | ||
551e34da | 329 | linemap_assert (reason != LC_ENTER_MACRO); |
330 | line_map_ordinary *map = linemap_check_ordinary (new_linemap (set, reason)); | |
38692459 | 331 | |
1eecdb28 | 332 | if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM) |
748b50a3 | 333 | to_file = "<stdin>"; |
334 | ||
1eecdb28 | 335 | if (reason == LC_RENAME_VERBATIM) |
336 | reason = LC_RENAME; | |
337 | ||
1dc92c59 | 338 | if (reason == LC_LEAVE) |
bd507c05 | 339 | { |
97bfb9ef | 340 | /* When we are just leaving an "included" file, and jump to the next |
341 | location inside the "includer" right after the #include | |
342 | "included", this variable points the map in use right before the | |
343 | #include "included", inside the same "includer" file. */ | |
551e34da | 344 | line_map_ordinary *from; |
f85fcf2b | 345 | bool error; |
346 | ||
347 | if (MAIN_FILE_P (map - 1)) | |
bd507c05 | 348 | { |
351855a5 | 349 | /* So this _should_ mean we are leaving the main file -- |
97bfb9ef | 350 | effectively ending the compilation unit. But to_file not |
351 | being NULL means the caller thinks we are leaving to | |
352 | another file. This is an erroneous behaviour but we'll | |
353 | try to recover from it. Let's pretend we are not leaving | |
354 | the main file. */ | |
4d1574ae | 355 | error = true; |
356 | reason = LC_RENAME; | |
357 | from = map - 1; | |
f85fcf2b | 358 | } |
359 | else | |
360 | { | |
97bfb9ef | 361 | /* (MAP - 1) points to the map we are leaving. The |
362 | map from which (MAP - 1) got included should be the map | |
363 | that comes right before MAP in the same file. */ | |
f85fcf2b | 364 | from = INCLUDED_FROM (set, map - 1); |
97bfb9ef | 365 | error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from), |
366 | to_file); | |
f85fcf2b | 367 | } |
368 | ||
369 | /* Depending upon whether we are handling preprocessed input or | |
370 | not, this can be a user error or an ICE. */ | |
371 | if (error) | |
372 | fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n", | |
373 | to_file); | |
374 | ||
375 | /* A TO_FILE of NULL is special - we use the natural values. */ | |
376 | if (error || to_file == NULL) | |
377 | { | |
97bfb9ef | 378 | to_file = ORDINARY_MAP_FILE_NAME (from); |
610625e3 | 379 | to_line = SOURCE_LINE (from, from[1].start_location); |
97bfb9ef | 380 | sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from); |
bd507c05 | 381 | } |
382 | } | |
383 | ||
3ce1a212 | 384 | map->sysp = sysp; |
385 | map->start_location = start_location; | |
386 | map->to_file = to_file; | |
387 | map->to_line = to_line; | |
97bfb9ef | 388 | LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1; |
3ce1a212 | 389 | map->column_bits = 0; |
610625e3 | 390 | set->highest_location = start_location; |
dbddc569 | 391 | set->highest_line = start_location; |
610625e3 | 392 | set->max_column_hint = 0; |
f85fcf2b | 393 | |
bd507c05 | 394 | if (reason == LC_ENTER) |
4087823d | 395 | { |
3ce1a212 | 396 | map->included_from = |
97bfb9ef | 397 | set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2); |
4087823d | 398 | set->depth++; |
4087823d | 399 | if (set->trace_includes) |
400 | trace_include (set, map); | |
401 | } | |
38692459 | 402 | else if (reason == LC_RENAME) |
3ce1a212 | 403 | map->included_from = ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]); |
38692459 | 404 | else if (reason == LC_LEAVE) |
4087823d | 405 | { |
406 | set->depth--; | |
3ce1a212 | 407 | map->included_from = |
97bfb9ef | 408 | ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1)); |
4087823d | 409 | } |
438ac94c | 410 | |
38692459 | 411 | return map; |
412 | } | |
413 | ||
e8aa7eaf | 414 | /* Returns TRUE if the line table set tracks token locations across |
97bfb9ef | 415 | macro expansion, FALSE otherwise. */ |
416 | ||
417 | bool | |
418 | linemap_tracks_macro_expansion_locs_p (struct line_maps *set) | |
419 | { | |
420 | return LINEMAPS_MACRO_MAPS (set) != NULL; | |
421 | } | |
422 | ||
423 | /* Create a macro map. A macro map encodes source locations of tokens | |
424 | that are part of a macro replacement-list, at a macro expansion | |
425 | point. See the extensive comments of struct line_map and struct | |
426 | line_map_macro, in line-map.h. | |
427 | ||
428 | This map shall be created when the macro is expanded. The map | |
429 | encodes the source location of the expansion point of the macro as | |
430 | well as the "original" source location of each token that is part | |
431 | of the macro replacement-list. If a macro is defined but never | |
432 | expanded, it has no macro map. SET is the set of maps the macro | |
433 | map should be part of. MACRO_NODE is the macro which the new macro | |
434 | map should encode source locations for. EXPANSION is the location | |
435 | of the expansion point of MACRO. For function-like macros | |
436 | invocations, it's best to make it point to the closing parenthesis | |
437 | of the macro, rather than the the location of the first character | |
438 | of the macro. NUM_TOKENS is the number of tokens that are part of | |
439 | the replacement-list of MACRO. | |
440 | ||
441 | Note that when we run out of the integer space available for source | |
442 | locations, this function returns NULL. In that case, callers of | |
443 | this function cannot encode {line,column} pairs into locations of | |
444 | macro tokens anymore. */ | |
445 | ||
551e34da | 446 | const line_map_macro * |
97bfb9ef | 447 | linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node, |
448 | source_location expansion, unsigned int num_tokens) | |
449 | { | |
551e34da | 450 | line_map_macro *map; |
97bfb9ef | 451 | source_location start_location; |
de2e6b8a | 452 | /* Cast away extern "C" from the type of xrealloc. */ |
453 | line_map_realloc reallocator = (set->reallocator | |
454 | ? set->reallocator | |
455 | : (line_map_realloc) xrealloc); | |
97bfb9ef | 456 | |
457 | start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens; | |
458 | ||
459 | if (start_location <= set->highest_line | |
460 | || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set)) | |
461 | /* We ran out of macro map space. */ | |
462 | return NULL; | |
463 | ||
551e34da | 464 | map = linemap_check_macro (new_linemap (set, LC_ENTER_MACRO)); |
97bfb9ef | 465 | |
3ce1a212 | 466 | map->start_location = start_location; |
467 | map->macro = macro_node; | |
468 | map->n_tokens = num_tokens; | |
469 | map->macro_locations | |
97bfb9ef | 470 | = (source_location*) reallocator (NULL, |
471 | 2 * num_tokens | |
472 | * sizeof (source_location)); | |
3ce1a212 | 473 | map->expansion = expansion; |
97bfb9ef | 474 | memset (MACRO_MAP_LOCATIONS (map), 0, |
475 | num_tokens * sizeof (source_location)); | |
476 | ||
477 | LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1; | |
97bfb9ef | 478 | |
479 | return map; | |
480 | } | |
481 | ||
482 | /* Create and return a virtual location for a token that is part of a | |
483 | macro expansion-list at a macro expansion point. See the comment | |
484 | inside struct line_map_macro to see what an expansion-list exactly | |
485 | is. | |
486 | ||
487 | A call to this function must come after a call to | |
488 | linemap_enter_macro. | |
489 | ||
490 | MAP is the map into which the source location is created. TOKEN_NO | |
491 | is the index of the token in the macro replacement-list, starting | |
492 | at number 0. | |
493 | ||
494 | ORIG_LOC is the location of the token outside of this macro | |
495 | expansion. If the token comes originally from the macro | |
496 | definition, it is the locus in the macro definition; otherwise it | |
497 | is a location in the context of the caller of this macro expansion | |
498 | (which is a virtual location or a source location if the caller is | |
499 | itself a macro expansion or not). | |
500 | ||
1fcf9669 | 501 | ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition, |
97bfb9ef | 502 | either of the token itself or of a macro parameter that it |
503 | replaces. */ | |
504 | ||
505 | source_location | |
551e34da | 506 | linemap_add_macro_token (const line_map_macro *map, |
97bfb9ef | 507 | unsigned int token_no, |
508 | source_location orig_loc, | |
509 | source_location orig_parm_replacement_loc) | |
510 | { | |
511 | source_location result; | |
512 | ||
513 | linemap_assert (linemap_macro_expansion_map_p (map)); | |
514 | linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map)); | |
515 | ||
516 | MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc; | |
517 | MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc; | |
518 | ||
519 | result = MAP_START_LOCATION (map) + token_no; | |
520 | return result; | |
521 | } | |
522 | ||
523 | /* Return a source_location for the start (i.e. column==0) of | |
524 | (physical) line TO_LINE in the current source file (as in the | |
525 | most recent linemap_add). MAX_COLUMN_HINT is the highest column | |
526 | number we expect to use in this line (but it does not change | |
527 | the highest_location). */ | |
528 | ||
610625e3 | 529 | source_location |
4999c35b | 530 | linemap_line_start (struct line_maps *set, linenum_type to_line, |
610625e3 | 531 | unsigned int max_column_hint) |
532 | { | |
551e34da | 533 | line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set); |
610625e3 | 534 | source_location highest = set->highest_location; |
535 | source_location r; | |
97bfb9ef | 536 | linenum_type last_line = |
537 | SOURCE_LINE (map, set->highest_line); | |
610625e3 | 538 | int line_delta = to_line - last_line; |
539 | bool add_map = false; | |
97bfb9ef | 540 | |
610625e3 | 541 | if (line_delta < 0 |
97bfb9ef | 542 | || (line_delta > 10 |
543 | && line_delta * ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) > 1000) | |
544 | || (max_column_hint >= (1U << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))) | |
545 | || (max_column_hint <= 80 | |
1feb1b2d | 546 | && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10) |
547 | || (highest > 0x60000000 | |
548 | && (set->max_column_hint || highest > 0x70000000))) | |
549 | add_map = true; | |
610625e3 | 550 | else |
551 | max_column_hint = set->max_column_hint; | |
552 | if (add_map) | |
553 | { | |
554 | int column_bits; | |
5169661d | 555 | if (max_column_hint > 100000 || highest > 0x60000000) |
610625e3 | 556 | { |
0b7f838f | 557 | /* If the column number is ridiculous or we've allocated a huge |
558 | number of source_locations, give up on column numbers. */ | |
610625e3 | 559 | max_column_hint = 0; |
1feb1b2d | 560 | if (highest > 0x70000000) |
610625e3 | 561 | return 0; |
562 | column_bits = 0; | |
563 | } | |
564 | else | |
565 | { | |
566 | column_bits = 7; | |
567 | while (max_column_hint >= (1U << column_bits)) | |
568 | column_bits++; | |
569 | max_column_hint = 1U << column_bits; | |
570 | } | |
0b7f838f | 571 | /* Allocate the new line_map. However, if the current map only has a |
572 | single line we can sometimes just increase its column_bits instead. */ | |
610625e3 | 573 | if (line_delta < 0 |
97bfb9ef | 574 | || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map) |
610625e3 | 575 | || SOURCE_COLUMN (map, highest) >= (1U << column_bits)) |
551e34da | 576 | map = linemap_check_ordinary |
577 | (const_cast <line_map *> | |
578 | (linemap_add (set, LC_RENAME, | |
579 | ORDINARY_MAP_IN_SYSTEM_HEADER_P (map), | |
580 | ORDINARY_MAP_FILE_NAME (map), | |
581 | to_line))); | |
3ce1a212 | 582 | map->column_bits = column_bits; |
97bfb9ef | 583 | r = (MAP_START_LOCATION (map) |
584 | + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map)) | |
585 | << column_bits)); | |
610625e3 | 586 | } |
587 | else | |
588 | r = highest - SOURCE_COLUMN (map, highest) | |
97bfb9ef | 589 | + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)); |
590 | ||
591 | /* Locations of ordinary tokens are always lower than locations of | |
592 | macro tokens. */ | |
593 | if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set)) | |
594 | return 0; | |
595 | ||
dbddc569 | 596 | set->highest_line = r; |
610625e3 | 597 | if (r > set->highest_location) |
598 | set->highest_location = r; | |
599 | set->max_column_hint = max_column_hint; | |
600 | return r; | |
601 | } | |
602 | ||
97bfb9ef | 603 | /* Encode and return a source_location from a column number. The |
604 | source line considered is the last source line used to call | |
605 | linemap_line_start, i.e, the last source line which a location was | |
606 | encoded from. */ | |
607 | ||
dbddc569 | 608 | source_location |
609 | linemap_position_for_column (struct line_maps *set, unsigned int to_column) | |
610 | { | |
611 | source_location r = set->highest_line; | |
97bfb9ef | 612 | |
613 | linemap_assert | |
614 | (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set))); | |
615 | ||
dbddc569 | 616 | if (to_column >= set->max_column_hint) |
617 | { | |
618 | if (r >= 0xC000000 || to_column > 100000) | |
619 | { | |
620 | /* Running low on source_locations - disable column numbers. */ | |
621 | return r; | |
622 | } | |
623 | else | |
624 | { | |
551e34da | 625 | line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set); |
dbddc569 | 626 | r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50); |
627 | } | |
628 | } | |
629 | r = r + to_column; | |
630 | if (r >= set->highest_location) | |
631 | set->highest_location = r; | |
632 | return r; | |
633 | } | |
634 | ||
97bfb9ef | 635 | /* Encode and return a source location from a given line and |
636 | column. */ | |
38692459 | 637 | |
97bfb9ef | 638 | source_location |
551e34da | 639 | linemap_position_for_line_and_column (const line_map_ordinary *ord_map, |
97bfb9ef | 640 | linenum_type line, |
641 | unsigned column) | |
642 | { | |
551e34da | 643 | linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line); |
97bfb9ef | 644 | |
551e34da | 645 | return (MAP_START_LOCATION (ord_map) |
646 | + ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map)) | |
647 | << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (ord_map)) | |
648 | + (column & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (ord_map)) - 1))); | |
97bfb9ef | 649 | } |
650 | ||
766928aa | 651 | /* Encode and return a source_location starting from location LOC and |
652 | shifting it by OFFSET columns. This function does not support | |
653 | virtual locations. */ | |
654 | ||
655 | source_location | |
656 | linemap_position_for_loc_and_offset (struct line_maps *set, | |
657 | source_location loc, | |
658 | unsigned int offset) | |
659 | { | |
551e34da | 660 | const line_map_ordinary * map = NULL; |
766928aa | 661 | |
662 | /* This function does not support virtual locations yet. */ | |
324da92d | 663 | if (linemap_assert_fails |
664 | (!linemap_location_from_macro_expansion_p (set, loc))) | |
665 | return loc; | |
766928aa | 666 | |
667 | if (offset == 0 | |
668 | /* Adding an offset to a reserved location (like | |
669 | UNKNOWN_LOCATION for the C/C++ FEs) does not really make | |
670 | sense. So let's leave the location intact in that case. */ | |
671 | || loc < RESERVED_LOCATION_COUNT) | |
672 | return loc; | |
673 | ||
674 | /* We find the real location and shift it. */ | |
675 | loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map); | |
676 | /* The new location (loc + offset) should be higher than the first | |
677 | location encoded by MAP. */ | |
324da92d | 678 | if (linemap_assert_fails (MAP_START_LOCATION (map) < loc + offset)) |
679 | return loc; | |
766928aa | 680 | |
681 | /* If MAP is not the last line map of its set, then the new location | |
682 | (loc + offset) should be less than the first location encoded by | |
683 | the next line map of the set. */ | |
684 | if (map != LINEMAPS_LAST_ORDINARY_MAP (set)) | |
324da92d | 685 | if (linemap_assert_fails (loc + offset < MAP_START_LOCATION (&map[1]))) |
686 | return loc; | |
766928aa | 687 | |
688 | offset += SOURCE_COLUMN (map, loc); | |
551e34da | 689 | if (linemap_assert_fails |
690 | (offset < (1u << map->column_bits))) | |
324da92d | 691 | return loc; |
766928aa | 692 | |
693 | source_location r = | |
694 | linemap_position_for_line_and_column (map, | |
695 | SOURCE_LINE (map, loc), | |
696 | offset); | |
6d6bdc28 | 697 | if (linemap_assert_fails (r <= set->highest_location) |
698 | || linemap_assert_fails (map == linemap_lookup (set, r))) | |
324da92d | 699 | return loc; |
700 | ||
766928aa | 701 | return r; |
702 | } | |
703 | ||
97bfb9ef | 704 | /* Given a virtual source location yielded by a map (either an |
705 | ordinary or a macro map), returns that map. */ | |
706 | ||
707 | const struct line_map* | |
fd3638df | 708 | linemap_lookup (struct line_maps *set, source_location line) |
97bfb9ef | 709 | { |
5169661d | 710 | if (IS_ADHOC_LOC (line)) |
711 | line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus; | |
97bfb9ef | 712 | if (linemap_location_from_macro_expansion_p (set, line)) |
713 | return linemap_macro_map_lookup (set, line); | |
714 | return linemap_ordinary_map_lookup (set, line); | |
715 | } | |
716 | ||
717 | /* Given a source location yielded by an ordinary map, returns that | |
718 | map. Since the set is built chronologically, the logical lines are | |
719 | monotonic increasing, and so the list is sorted and we can use a | |
720 | binary search. */ | |
721 | ||
551e34da | 722 | static const line_map_ordinary * |
97bfb9ef | 723 | linemap_ordinary_map_lookup (struct line_maps *set, source_location line) |
38692459 | 724 | { |
088db31b | 725 | unsigned int md, mn, mx; |
551e34da | 726 | const line_map_ordinary *cached, *result; |
088db31b | 727 | |
5169661d | 728 | if (IS_ADHOC_LOC (line)) |
729 | line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus; | |
730 | ||
97bfb9ef | 731 | if (set == NULL || line < RESERVED_LOCATION_COUNT) |
732 | return NULL; | |
733 | ||
734 | mn = LINEMAPS_ORDINARY_CACHE (set); | |
735 | mx = LINEMAPS_ORDINARY_USED (set); | |
088db31b | 736 | |
97bfb9ef | 737 | cached = LINEMAPS_ORDINARY_MAP_AT (set, mn); |
7c2df241 | 738 | /* We should get a segfault if no line_maps have been added yet. */ |
97bfb9ef | 739 | if (line >= MAP_START_LOCATION (cached)) |
088db31b | 740 | { |
97bfb9ef | 741 | if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1])) |
088db31b | 742 | return cached; |
743 | } | |
744 | else | |
745 | { | |
746 | mx = mn; | |
747 | mn = 0; | |
748 | } | |
38692459 | 749 | |
750 | while (mx - mn > 1) | |
751 | { | |
752 | md = (mn + mx) / 2; | |
97bfb9ef | 753 | if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line) |
38692459 | 754 | mx = md; |
755 | else | |
756 | mn = md; | |
757 | } | |
758 | ||
97bfb9ef | 759 | LINEMAPS_ORDINARY_CACHE (set) = mn; |
760 | result = LINEMAPS_ORDINARY_MAP_AT (set, mn); | |
761 | linemap_assert (line >= MAP_START_LOCATION (result)); | |
762 | return result; | |
763 | } | |
764 | ||
765 | /* Given a source location yielded by a macro map, returns that map. | |
766 | Since the set is built chronologically, the logical lines are | |
767 | monotonic decreasing, and so the list is sorted and we can use a | |
768 | binary search. */ | |
769 | ||
551e34da | 770 | static const line_map_macro * |
97bfb9ef | 771 | linemap_macro_map_lookup (struct line_maps *set, source_location line) |
772 | { | |
773 | unsigned int md, mn, mx; | |
551e34da | 774 | const struct line_map_macro *cached, *result; |
97bfb9ef | 775 | |
5169661d | 776 | if (IS_ADHOC_LOC (line)) |
777 | line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus; | |
778 | ||
97bfb9ef | 779 | linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set)); |
780 | ||
781 | if (set == NULL) | |
782 | return NULL; | |
783 | ||
784 | mn = LINEMAPS_MACRO_CACHE (set); | |
785 | mx = LINEMAPS_MACRO_USED (set); | |
786 | cached = LINEMAPS_MACRO_MAP_AT (set, mn); | |
787 | ||
788 | if (line >= MAP_START_LOCATION (cached)) | |
789 | { | |
790 | if (mn == 0 || line < MAP_START_LOCATION (&cached[-1])) | |
791 | return cached; | |
792 | mx = mn - 1; | |
793 | mn = 0; | |
794 | } | |
795 | ||
68928989 | 796 | while (mn < mx) |
97bfb9ef | 797 | { |
798 | md = (mx + mn) / 2; | |
799 | if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line) | |
68928989 | 800 | mn = md + 1; |
97bfb9ef | 801 | else |
802 | mx = md; | |
68928989 | 803 | } |
97bfb9ef | 804 | |
805 | LINEMAPS_MACRO_CACHE (set) = mx; | |
806 | result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set)); | |
807 | linemap_assert (MAP_START_LOCATION (result) <= line); | |
808 | ||
809 | return result; | |
810 | } | |
811 | ||
812 | /* Return TRUE if MAP encodes locations coming from a macro | |
813 | replacement-list at macro expansion point. */ | |
814 | ||
815 | bool | |
816 | linemap_macro_expansion_map_p (const struct line_map *map) | |
817 | { | |
818 | if (!map) | |
819 | return false; | |
820 | return (map->reason == LC_ENTER_MACRO); | |
821 | } | |
822 | ||
823 | /* If LOCATION is the locus of a token in a replacement-list of a | |
824 | macro expansion return the location of the macro expansion point. | |
825 | ||
826 | Read the comments of struct line_map and struct line_map_macro in | |
827 | line-map.h to understand what a macro expansion point is. */ | |
828 | ||
a67520a9 | 829 | static source_location |
551e34da | 830 | linemap_macro_map_loc_to_exp_point (const line_map_macro *map, |
a67520a9 | 831 | source_location location ATTRIBUTE_UNUSED) |
97bfb9ef | 832 | { |
97bfb9ef | 833 | linemap_assert (linemap_macro_expansion_map_p (map) |
834 | && location >= MAP_START_LOCATION (map)); | |
835 | ||
836 | /* Make sure LOCATION is correct. */ | |
a67520a9 | 837 | linemap_assert ((location - MAP_START_LOCATION (map)) |
838 | < MACRO_MAP_NUM_MACRO_TOKENS (map)); | |
97bfb9ef | 839 | |
840 | return MACRO_MAP_EXPANSION_POINT_LOCATION (map); | |
841 | } | |
842 | ||
1fcf9669 | 843 | /* LOCATION is the source location of a token that belongs to a macro |
844 | replacement-list as part of the macro expansion denoted by MAP. | |
97bfb9ef | 845 | |
1fcf9669 | 846 | Return the location of the token at the definition point of the |
847 | macro. */ | |
848 | ||
849 | static source_location | |
551e34da | 850 | linemap_macro_map_loc_to_def_point (const line_map_macro *map, |
97bfb9ef | 851 | source_location location) |
852 | { | |
853 | unsigned token_no; | |
854 | ||
855 | linemap_assert (linemap_macro_expansion_map_p (map) | |
856 | && location >= MAP_START_LOCATION (map)); | |
857 | linemap_assert (location >= RESERVED_LOCATION_COUNT); | |
858 | ||
859 | token_no = location - MAP_START_LOCATION (map); | |
860 | linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map)); | |
861 | ||
862 | location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1]; | |
863 | ||
864 | return location; | |
865 | } | |
866 | ||
867 | /* If LOCATION is the locus of a token that is an argument of a | |
868 | function-like macro M and appears in the expansion of M, return the | |
869 | locus of that argument in the context of the caller of M. | |
870 | ||
871 | In other words, this returns the xI location presented in the | |
872 | comments of line_map_macro above. */ | |
873 | source_location | |
551e34da | 874 | linemap_macro_map_loc_unwind_toward_spelling (const line_map_macro* map, |
97bfb9ef | 875 | source_location location) |
876 | { | |
877 | unsigned token_no; | |
878 | ||
879 | linemap_assert (linemap_macro_expansion_map_p (map) | |
880 | && location >= MAP_START_LOCATION (map)); | |
881 | linemap_assert (location >= RESERVED_LOCATION_COUNT); | |
882 | ||
883 | token_no = location - MAP_START_LOCATION (map); | |
884 | linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map)); | |
885 | ||
886 | location = MACRO_MAP_LOCATIONS (map)[2 * token_no]; | |
887 | ||
888 | return location; | |
889 | } | |
890 | ||
891 | /* Return the source line number corresponding to source location | |
892 | LOCATION. SET is the line map set LOCATION comes from. If | |
893 | LOCATION is the source location of token that is part of the | |
894 | replacement-list of a macro expansion return the line number of the | |
895 | macro expansion point. */ | |
896 | ||
897 | int | |
898 | linemap_get_expansion_line (struct line_maps *set, | |
899 | source_location location) | |
900 | { | |
551e34da | 901 | const line_map_ordinary *map = NULL; |
97bfb9ef | 902 | |
5169661d | 903 | if (IS_ADHOC_LOC (location)) |
6e5a7913 | 904 | location = set->location_adhoc_data_map.data[location |
905 | & MAX_SOURCE_LOCATION].locus; | |
5169661d | 906 | |
97bfb9ef | 907 | if (location < RESERVED_LOCATION_COUNT) |
908 | return 0; | |
909 | ||
910 | location = | |
911 | linemap_macro_loc_to_exp_point (set, location, &map); | |
912 | ||
913 | return SOURCE_LINE (map, location); | |
914 | } | |
915 | ||
916 | /* Return the path of the file corresponding to source code location | |
917 | LOCATION. | |
918 | ||
919 | If LOCATION is the source location of token that is part of the | |
920 | replacement-list of a macro expansion return the file path of the | |
921 | macro expansion point. | |
922 | ||
923 | SET is the line map set LOCATION comes from. */ | |
924 | ||
925 | const char* | |
926 | linemap_get_expansion_filename (struct line_maps *set, | |
927 | source_location location) | |
928 | { | |
551e34da | 929 | const struct line_map_ordinary *map = NULL; |
97bfb9ef | 930 | |
5169661d | 931 | if (IS_ADHOC_LOC (location)) |
6e5a7913 | 932 | location = set->location_adhoc_data_map.data[location |
933 | & MAX_SOURCE_LOCATION].locus; | |
5169661d | 934 | |
97bfb9ef | 935 | if (location < RESERVED_LOCATION_COUNT) |
936 | return NULL; | |
937 | ||
938 | location = | |
939 | linemap_macro_loc_to_exp_point (set, location, &map); | |
940 | ||
941 | return LINEMAP_FILE (map); | |
942 | } | |
943 | ||
944 | /* Return the name of the macro associated to MACRO_MAP. */ | |
945 | ||
946 | const char* | |
551e34da | 947 | linemap_map_get_macro_name (const line_map_macro *macro_map) |
97bfb9ef | 948 | { |
949 | linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map)); | |
950 | return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map)); | |
951 | } | |
952 | ||
953 | /* Return a positive value if LOCATION is the locus of a token that is | |
954 | located in a system header, O otherwise. It returns 1 if LOCATION | |
955 | is the locus of a token that is located in a system header, and 2 | |
956 | if LOCATION is the locus of a token located in a C system header | |
957 | that therefore needs to be extern "C" protected in C++. | |
958 | ||
959 | Note that this function returns 1 if LOCATION belongs to a token | |
960 | that is part of a macro replacement-list defined in a system | |
961 | header, but expanded in a non-system file. */ | |
962 | ||
963 | int | |
964 | linemap_location_in_system_header_p (struct line_maps *set, | |
965 | source_location location) | |
966 | { | |
967 | const struct line_map *map = NULL; | |
968 | ||
5169661d | 969 | if (IS_ADHOC_LOC (location)) |
6e5a7913 | 970 | location = set->location_adhoc_data_map.data[location |
971 | & MAX_SOURCE_LOCATION].locus; | |
5169661d | 972 | |
5ebe2143 | 973 | if (location < RESERVED_LOCATION_COUNT) |
974 | return false; | |
975 | ||
0aa42a53 | 976 | /* Let's look at where the token for LOCATION comes from. */ |
977 | while (true) | |
978 | { | |
979 | map = linemap_lookup (set, location); | |
980 | if (map != NULL) | |
981 | { | |
982 | if (!linemap_macro_expansion_map_p (map)) | |
983 | /* It's a normal token. */ | |
551e34da | 984 | return LINEMAP_SYSP (linemap_check_ordinary (map)); |
0aa42a53 | 985 | else |
986 | { | |
551e34da | 987 | const line_map_macro *macro_map = linemap_check_macro (map); |
988 | ||
0aa42a53 | 989 | /* It's a token resulting from a macro expansion. */ |
990 | source_location loc = | |
551e34da | 991 | linemap_macro_map_loc_unwind_toward_spelling (macro_map, location); |
0aa42a53 | 992 | if (loc < RESERVED_LOCATION_COUNT) |
993 | /* This token might come from a built-in macro. Let's | |
994 | look at where that macro got expanded. */ | |
551e34da | 995 | location = linemap_macro_map_loc_to_exp_point (macro_map, location); |
0aa42a53 | 996 | else |
997 | location = loc; | |
998 | } | |
999 | } | |
1000 | else | |
1001 | break; | |
1002 | } | |
1003 | return false; | |
97bfb9ef | 1004 | } |
1005 | ||
1006 | /* Return TRUE if LOCATION is a source code location of a token coming | |
1007 | from a macro replacement-list at a macro expansion point, FALSE | |
1008 | otherwise. */ | |
1009 | ||
1010 | bool | |
1fcf9669 | 1011 | linemap_location_from_macro_expansion_p (const struct line_maps *set, |
97bfb9ef | 1012 | source_location location) |
1013 | { | |
5169661d | 1014 | if (IS_ADHOC_LOC (location)) |
6e5a7913 | 1015 | location = set->location_adhoc_data_map.data[location |
1016 | & MAX_SOURCE_LOCATION].locus; | |
5169661d | 1017 | |
97bfb9ef | 1018 | linemap_assert (location <= MAX_SOURCE_LOCATION |
1019 | && (set->highest_location | |
1020 | < LINEMAPS_MACRO_LOWEST_LOCATION (set))); | |
1021 | if (set == NULL) | |
1022 | return false; | |
1023 | return (location > set->highest_location); | |
1024 | } | |
1025 | ||
1026 | /* Given two virtual locations *LOC0 and *LOC1, return the first | |
1027 | common macro map in their macro expansion histories. Return NULL | |
1028 | if no common macro was found. *LOC0 (resp. *LOC1) is set to the | |
1029 | virtual location of the token inside the resulting macro. */ | |
1030 | ||
1031 | static const struct line_map* | |
1032 | first_map_in_common_1 (struct line_maps *set, | |
1033 | source_location *loc0, | |
1034 | source_location *loc1) | |
1035 | { | |
1036 | source_location l0 = *loc0, l1 = *loc1; | |
1037 | const struct line_map *map0 = linemap_lookup (set, l0), | |
1038 | *map1 = linemap_lookup (set, l1); | |
1039 | ||
1040 | while (linemap_macro_expansion_map_p (map0) | |
1041 | && linemap_macro_expansion_map_p (map1) | |
1042 | && (map0 != map1)) | |
1043 | { | |
1044 | if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1)) | |
1045 | { | |
551e34da | 1046 | l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0), |
1047 | l0); | |
97bfb9ef | 1048 | map0 = linemap_lookup (set, l0); |
1049 | } | |
1050 | else | |
1051 | { | |
551e34da | 1052 | l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1), |
1053 | l1); | |
97bfb9ef | 1054 | map1 = linemap_lookup (set, l1); |
1055 | } | |
1056 | } | |
1057 | ||
1058 | if (map0 == map1) | |
1059 | { | |
1060 | *loc0 = l0; | |
1061 | *loc1 = l1; | |
1062 | return map0; | |
1063 | } | |
1064 | return NULL; | |
1065 | } | |
1066 | ||
1067 | /* Given two virtual locations LOC0 and LOC1, return the first common | |
1068 | macro map in their macro expansion histories. Return NULL if no | |
1069 | common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the | |
1070 | virtual location of the token inside the resulting macro, upon | |
1071 | return of a non-NULL result. */ | |
1072 | ||
1073 | static const struct line_map* | |
1074 | first_map_in_common (struct line_maps *set, | |
1075 | source_location loc0, | |
1076 | source_location loc1, | |
1077 | source_location *res_loc0, | |
1078 | source_location *res_loc1) | |
1079 | { | |
1080 | *res_loc0 = loc0; | |
1081 | *res_loc1 = loc1; | |
1082 | ||
1083 | return first_map_in_common_1 (set, res_loc0, res_loc1); | |
1084 | } | |
1085 | ||
1086 | /* Return a positive value if PRE denotes the location of a token that | |
1087 | comes before the token of POST, 0 if PRE denotes the location of | |
1088 | the same token as the token for POST, and a negative value | |
1089 | otherwise. */ | |
1090 | ||
1091 | int | |
1092 | linemap_compare_locations (struct line_maps *set, | |
1093 | source_location pre, | |
1094 | source_location post) | |
1095 | { | |
1096 | bool pre_virtual_p, post_virtual_p; | |
1097 | source_location l0 = pre, l1 = post; | |
1098 | ||
6e5a7913 | 1099 | if (IS_ADHOC_LOC (l0)) |
1100 | l0 = set->location_adhoc_data_map.data[l0 & MAX_SOURCE_LOCATION].locus; | |
1101 | if (IS_ADHOC_LOC (l1)) | |
1102 | l1 = set->location_adhoc_data_map.data[l1 & MAX_SOURCE_LOCATION].locus; | |
1103 | ||
97bfb9ef | 1104 | if (l0 == l1) |
1105 | return 0; | |
1106 | ||
1107 | if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0))) | |
1108 | l0 = linemap_resolve_location (set, l0, | |
1109 | LRK_MACRO_EXPANSION_POINT, | |
1110 | NULL); | |
1111 | ||
1112 | if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1))) | |
1113 | l1 = linemap_resolve_location (set, l1, | |
1114 | LRK_MACRO_EXPANSION_POINT, | |
1115 | NULL); | |
1116 | ||
1117 | if (l0 == l1 | |
1118 | && pre_virtual_p | |
1119 | && post_virtual_p) | |
1120 | { | |
1121 | /* So pre and post represent two tokens that are present in a | |
1122 | same macro expansion. Let's see if the token for pre was | |
1123 | before the token for post in that expansion. */ | |
1124 | unsigned i0, i1; | |
1125 | const struct line_map *map = | |
1126 | first_map_in_common (set, pre, post, &l0, &l1); | |
1127 | ||
1128 | if (map == NULL) | |
1129 | /* This should not be possible. */ | |
1130 | abort (); | |
1131 | ||
1132 | i0 = l0 - MAP_START_LOCATION (map); | |
1133 | i1 = l1 - MAP_START_LOCATION (map); | |
1134 | return i1 - i0; | |
1135 | } | |
1136 | ||
1137 | return l1 - l0; | |
38692459 | 1138 | } |
bd507c05 | 1139 | |
438ac94c | 1140 | /* Print an include trace, for e.g. the -H option of the preprocessor. */ |
1141 | ||
1142 | static void | |
551e34da | 1143 | trace_include (const struct line_maps *set, const line_map_ordinary *map) |
438ac94c | 1144 | { |
4087823d | 1145 | unsigned int i = set->depth; |
438ac94c | 1146 | |
4087823d | 1147 | while (--i) |
438ac94c | 1148 | putc ('.', stderr); |
97bfb9ef | 1149 | |
1150 | fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map)); | |
1151 | } | |
1152 | ||
1153 | /* Return the spelling location of the token wherever it comes from, | |
1154 | whether part of a macro definition or not. | |
1155 | ||
1156 | This is a subroutine for linemap_resolve_location. */ | |
1157 | ||
1158 | static source_location | |
1159 | linemap_macro_loc_to_spelling_point (struct line_maps *set, | |
1160 | source_location location, | |
551e34da | 1161 | const line_map_ordinary **original_map) |
97bfb9ef | 1162 | { |
1163 | struct line_map *map; | |
1164 | ||
5169661d | 1165 | if (IS_ADHOC_LOC (location)) |
6e5a7913 | 1166 | location = set->location_adhoc_data_map.data[location |
1167 | & MAX_SOURCE_LOCATION].locus; | |
5169661d | 1168 | |
97bfb9ef | 1169 | linemap_assert (set && location >= RESERVED_LOCATION_COUNT); |
1170 | ||
1171 | while (true) | |
1172 | { | |
551e34da | 1173 | map = const_cast <line_map *> (linemap_lookup (set, location)); |
97bfb9ef | 1174 | if (!linemap_macro_expansion_map_p (map)) |
1175 | break; | |
1176 | ||
551e34da | 1177 | location |
1178 | = linemap_macro_map_loc_unwind_toward_spelling | |
1179 | (linemap_check_macro (map), | |
1180 | location); | |
97bfb9ef | 1181 | } |
1182 | ||
1183 | if (original_map) | |
551e34da | 1184 | *original_map = linemap_check_ordinary (map); |
97bfb9ef | 1185 | return location; |
1186 | } | |
1187 | ||
1188 | /* If LOCATION is the source location of a token that belongs to a | |
1189 | macro replacement-list -- as part of a macro expansion -- then | |
1190 | return the location of the token at the definition point of the | |
1191 | macro. Otherwise, return LOCATION. SET is the set of maps | |
1192 | location come from. ORIGINAL_MAP is an output parm. If non NULL, | |
1193 | the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the | |
1194 | returned location comes from. | |
1195 | ||
1196 | This is a subroutine of linemap_resolve_location. */ | |
1197 | ||
1198 | static source_location | |
1199 | linemap_macro_loc_to_def_point (struct line_maps *set, | |
1200 | source_location location, | |
551e34da | 1201 | const line_map_ordinary **original_map) |
97bfb9ef | 1202 | { |
1203 | struct line_map *map; | |
1204 | ||
5169661d | 1205 | if (IS_ADHOC_LOC (location)) |
6e5a7913 | 1206 | location = set->location_adhoc_data_map.data[location |
1207 | & MAX_SOURCE_LOCATION].locus; | |
5169661d | 1208 | |
97bfb9ef | 1209 | linemap_assert (set && location >= RESERVED_LOCATION_COUNT); |
1210 | ||
1211 | while (true) | |
1212 | { | |
551e34da | 1213 | map = const_cast <line_map *> (linemap_lookup (set, location)); |
97bfb9ef | 1214 | if (!linemap_macro_expansion_map_p (map)) |
1215 | break; | |
1216 | ||
1217 | location = | |
551e34da | 1218 | linemap_macro_map_loc_to_def_point (linemap_check_macro (map), |
1219 | location); | |
97bfb9ef | 1220 | } |
1221 | ||
1222 | if (original_map) | |
551e34da | 1223 | *original_map = linemap_check_ordinary (map); |
97bfb9ef | 1224 | return location; |
1225 | } | |
1226 | ||
1227 | /* If LOCATION is the source location of a token that belongs to a | |
1228 | macro replacement-list -- at a macro expansion point -- then return | |
1229 | the location of the topmost expansion point of the macro. We say | |
1230 | topmost because if we are in the context of a nested macro | |
1231 | expansion, the function returns the source location of the first | |
1232 | macro expansion that triggered the nested expansions. | |
1233 | ||
1234 | Otherwise, return LOCATION. SET is the set of maps location come | |
1235 | from. ORIGINAL_MAP is an output parm. If non NULL, the function | |
1236 | sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned | |
1237 | location comes from. | |
1238 | ||
1239 | This is a subroutine of linemap_resolve_location. */ | |
1240 | ||
1241 | static source_location | |
1242 | linemap_macro_loc_to_exp_point (struct line_maps *set, | |
1243 | source_location location, | |
551e34da | 1244 | const line_map_ordinary **original_map) |
97bfb9ef | 1245 | { |
1246 | struct line_map *map; | |
1247 | ||
5169661d | 1248 | if (IS_ADHOC_LOC (location)) |
6e5a7913 | 1249 | location = set->location_adhoc_data_map.data[location |
1250 | & MAX_SOURCE_LOCATION].locus; | |
5169661d | 1251 | |
97bfb9ef | 1252 | linemap_assert (set && location >= RESERVED_LOCATION_COUNT); |
1253 | ||
1254 | while (true) | |
1255 | { | |
551e34da | 1256 | map = const_cast <line_map *> (linemap_lookup (set, location)); |
97bfb9ef | 1257 | if (!linemap_macro_expansion_map_p (map)) |
1258 | break; | |
551e34da | 1259 | location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map), |
1260 | location); | |
97bfb9ef | 1261 | } |
1262 | ||
1263 | if (original_map) | |
551e34da | 1264 | *original_map = linemap_check_ordinary (map); |
97bfb9ef | 1265 | return location; |
1266 | } | |
1267 | ||
1268 | /* Resolve a virtual location into either a spelling location, an | |
1269 | expansion point location or a token argument replacement point | |
1270 | location. Return the map that encodes the virtual location as well | |
1271 | as the resolved location. | |
1272 | ||
1273 | If LOC is *NOT* the location of a token resulting from the | |
1274 | expansion of a macro, then the parameter LRK (which stands for | |
1275 | Location Resolution Kind) is ignored and the resulting location | |
1276 | just equals the one given in argument. | |
1277 | ||
1278 | Now if LOC *IS* the location of a token resulting from the | |
1279 | expansion of a macro, this is what happens. | |
1280 | ||
1281 | * If LRK is set to LRK_MACRO_EXPANSION_POINT | |
1282 | ------------------------------- | |
1283 | ||
3f898bd2 | 1284 | The virtual location is resolved to the first macro expansion point |
1285 | that led to this macro expansion. | |
97bfb9ef | 1286 | |
1287 | * If LRK is set to LRK_SPELLING_LOCATION | |
1288 | ------------------------------------- | |
1289 | ||
3f898bd2 | 1290 | The virtual location is resolved to the locus where the token has |
1291 | been spelled in the source. This can follow through all the macro | |
1292 | expansions that led to the token. | |
97bfb9ef | 1293 | |
3f898bd2 | 1294 | * If LRK is set to LRK_MACRO_DEFINITION_LOCATION |
97bfb9ef | 1295 | -------------------------------------- |
1296 | ||
3f898bd2 | 1297 | The virtual location is resolved to the locus of the token in the |
1298 | context of the macro definition. | |
1299 | ||
97bfb9ef | 1300 | If LOC is the locus of a token that is an argument of a |
1301 | function-like macro [replacing a parameter in the replacement list | |
1302 | of the macro] the virtual location is resolved to the locus of the | |
1303 | parameter that is replaced, in the context of the definition of the | |
1304 | macro. | |
1305 | ||
1306 | If LOC is the locus of a token that is not an argument of a | |
1307 | function-like macro, then the function behaves as if LRK was set to | |
1308 | LRK_SPELLING_LOCATION. | |
1309 | ||
1fcf9669 | 1310 | If MAP is not NULL, *MAP is set to the map encoding the |
3f898bd2 | 1311 | returned location. Note that if the returned location wasn't originally |
1fcf9669 | 1312 | encoded by a map, then *MAP is set to NULL. This can happen if LOC |
5ebe2143 | 1313 | resolves to a location reserved for the client code, like |
1314 | UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */ | |
97bfb9ef | 1315 | |
1316 | source_location | |
1317 | linemap_resolve_location (struct line_maps *set, | |
1318 | source_location loc, | |
1319 | enum location_resolution_kind lrk, | |
551e34da | 1320 | const line_map_ordinary **map) |
97bfb9ef | 1321 | { |
5169661d | 1322 | if (IS_ADHOC_LOC (loc)) |
1323 | loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
1324 | ||
5ebe2143 | 1325 | if (loc < RESERVED_LOCATION_COUNT) |
1326 | { | |
1327 | /* A reserved location wasn't encoded in a map. Let's return a | |
1328 | NULL map here, just like what linemap_ordinary_map_lookup | |
1329 | does. */ | |
1330 | if (map) | |
1331 | *map = NULL; | |
1332 | return loc; | |
1333 | } | |
97bfb9ef | 1334 | |
1335 | switch (lrk) | |
1336 | { | |
1337 | case LRK_MACRO_EXPANSION_POINT: | |
1338 | loc = linemap_macro_loc_to_exp_point (set, loc, map); | |
1339 | break; | |
1340 | case LRK_SPELLING_LOCATION: | |
1341 | loc = linemap_macro_loc_to_spelling_point (set, loc, map); | |
1342 | break; | |
1343 | case LRK_MACRO_DEFINITION_LOCATION: | |
1344 | loc = linemap_macro_loc_to_def_point (set, loc, map); | |
1345 | break; | |
1346 | default: | |
1347 | abort (); | |
1348 | } | |
1349 | return loc; | |
1350 | } | |
1351 | ||
1352 | /* | |
1353 | Suppose that LOC is the virtual location of a token T coming from | |
1354 | the expansion of a macro M. This function then steps up to get the | |
1355 | location L of the point where M got expanded. If L is a spelling | |
1356 | location inside a macro expansion M', then this function returns | |
1357 | the locus of the point where M' was expanded. Said otherwise, this | |
1358 | function returns the location of T in the context that triggered | |
1359 | the expansion of M. | |
1360 | ||
1361 | *LOC_MAP must be set to the map of LOC. This function then sets it | |
1362 | to the map of the returned location. */ | |
1363 | ||
1364 | source_location | |
1365 | linemap_unwind_toward_expansion (struct line_maps *set, | |
1366 | source_location loc, | |
1367 | const struct line_map **map) | |
1368 | { | |
1369 | source_location resolved_location; | |
551e34da | 1370 | const line_map_macro *macro_map = linemap_check_macro (*map); |
97bfb9ef | 1371 | const struct line_map *resolved_map; |
1372 | ||
5169661d | 1373 | if (IS_ADHOC_LOC (loc)) |
1374 | loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
1375 | ||
97bfb9ef | 1376 | resolved_location = |
551e34da | 1377 | linemap_macro_map_loc_unwind_toward_spelling (macro_map, loc); |
97bfb9ef | 1378 | resolved_map = linemap_lookup (set, resolved_location); |
1379 | ||
1380 | if (!linemap_macro_expansion_map_p (resolved_map)) | |
1381 | { | |
551e34da | 1382 | resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc); |
97bfb9ef | 1383 | resolved_map = linemap_lookup (set, resolved_location); |
1384 | } | |
1385 | ||
1386 | *map = resolved_map; | |
1387 | return resolved_location; | |
1388 | } | |
1389 | ||
bd172d61 | 1390 | /* If LOC is the virtual location of a token coming from the expansion |
1391 | of a macro M and if its spelling location is reserved (e.g, a | |
1392 | location for a built-in token), then this function unwinds (using | |
1393 | linemap_unwind_toward_expansion) the location until a location that | |
e8aa7eaf | 1394 | is not reserved and is not in a system header is reached. In other |
bd172d61 | 1395 | words, this unwinds the reserved location until a location that is |
1396 | in real source code is reached. | |
1397 | ||
1398 | Otherwise, if the spelling location for LOC is not reserved or if | |
1399 | LOC doesn't come from the expansion of a macro, the function | |
1400 | returns LOC as is and *MAP is not touched. | |
1401 | ||
1402 | *MAP is set to the map of the returned location if the later is | |
1403 | different from LOC. */ | |
1404 | source_location | |
1405 | linemap_unwind_to_first_non_reserved_loc (struct line_maps *set, | |
1406 | source_location loc, | |
1407 | const struct line_map **map) | |
1408 | { | |
1409 | source_location resolved_loc; | |
551e34da | 1410 | const struct line_map *map0 = NULL; |
1411 | const line_map_ordinary *map1 = NULL; | |
bd172d61 | 1412 | |
5169661d | 1413 | if (IS_ADHOC_LOC (loc)) |
1414 | loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
1415 | ||
bd172d61 | 1416 | map0 = linemap_lookup (set, loc); |
1417 | if (!linemap_macro_expansion_map_p (map0)) | |
1418 | return loc; | |
1419 | ||
1420 | resolved_loc = linemap_resolve_location (set, loc, | |
1421 | LRK_SPELLING_LOCATION, | |
1422 | &map1); | |
1423 | ||
1424 | if (resolved_loc >= RESERVED_LOCATION_COUNT | |
1425 | && !LINEMAP_SYSP (map1)) | |
1426 | return loc; | |
1427 | ||
1428 | while (linemap_macro_expansion_map_p (map0) | |
1429 | && (resolved_loc < RESERVED_LOCATION_COUNT | |
1430 | || LINEMAP_SYSP (map1))) | |
1431 | { | |
1432 | loc = linemap_unwind_toward_expansion (set, loc, &map0); | |
1433 | resolved_loc = linemap_resolve_location (set, loc, | |
1434 | LRK_SPELLING_LOCATION, | |
1435 | &map1); | |
1436 | } | |
1437 | ||
1438 | if (map != NULL) | |
1439 | *map = map0; | |
1440 | return loc; | |
1441 | } | |
1442 | ||
97bfb9ef | 1443 | /* Expand source code location LOC and return a user readable source |
5ebe2143 | 1444 | code location. LOC must be a spelling (non-virtual) location. If |
1445 | it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source | |
1446 | location is returned. */ | |
97bfb9ef | 1447 | |
1448 | expanded_location | |
5ebe2143 | 1449 | linemap_expand_location (struct line_maps *set, |
1450 | const struct line_map *map, | |
97bfb9ef | 1451 | source_location loc) |
1452 | ||
1453 | { | |
1454 | expanded_location xloc; | |
1455 | ||
5ebe2143 | 1456 | memset (&xloc, 0, sizeof (xloc)); |
5169661d | 1457 | if (IS_ADHOC_LOC (loc)) |
1458 | { | |
1459 | loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
6e5a7913 | 1460 | xloc.data |
1461 | = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data; | |
5169661d | 1462 | } |
5ebe2143 | 1463 | |
1464 | if (loc < RESERVED_LOCATION_COUNT) | |
1465 | /* The location for this token wasn't generated from a line map. | |
1466 | It was probably a location for a builtin token, chosen by some | |
1467 | client code. Let's not try to expand the location in that | |
1468 | case. */; | |
1469 | else if (map == NULL) | |
1470 | /* We shouldn't be getting a NULL map with a location that is not | |
1471 | reserved by the client code. */ | |
1472 | abort (); | |
1473 | else | |
1474 | { | |
1475 | /* MAP must be an ordinary map and LOC must be non-virtual, | |
1476 | encoded into this map, obviously; the accessors used on MAP | |
1477 | below ensure it is ordinary. Let's just assert the | |
1478 | non-virtualness of LOC here. */ | |
1479 | if (linemap_location_from_macro_expansion_p (set, loc)) | |
1480 | abort (); | |
97bfb9ef | 1481 | |
551e34da | 1482 | const line_map_ordinary *ord_map = linemap_check_ordinary (map); |
1483 | ||
1484 | xloc.file = LINEMAP_FILE (ord_map); | |
1485 | xloc.line = SOURCE_LINE (ord_map, loc); | |
1486 | xloc.column = SOURCE_COLUMN (ord_map, loc); | |
1487 | xloc.sysp = LINEMAP_SYSP (ord_map) != 0; | |
5ebe2143 | 1488 | } |
97bfb9ef | 1489 | |
97bfb9ef | 1490 | return xloc; |
438ac94c | 1491 | } |
62db153a | 1492 | |
b082215e | 1493 | |
1494 | /* Dump line map at index IX in line table SET to STREAM. If STREAM | |
1495 | is NULL, use stderr. IS_MACRO is true if the caller wants to | |
1496 | dump a macro map, false otherwise. */ | |
1497 | ||
1498 | void | |
1499 | linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro) | |
1500 | { | |
1501 | const char *lc_reasons_v[LC_ENTER_MACRO + 1] | |
1502 | = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM", | |
1503 | "LC_ENTER_MACRO" }; | |
1504 | const char *reason; | |
551e34da | 1505 | const line_map *map; |
b082215e | 1506 | |
1507 | if (stream == NULL) | |
1508 | stream = stderr; | |
1509 | ||
1510 | if (!is_macro) | |
1511 | map = LINEMAPS_ORDINARY_MAP_AT (set, ix); | |
1512 | else | |
1513 | map = LINEMAPS_MACRO_MAP_AT (set, ix); | |
1514 | ||
1515 | reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???"; | |
1516 | ||
1517 | fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n", | |
1518 | ix, (void *) map, map->start_location, reason, | |
551e34da | 1519 | ((!is_macro |
1520 | && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map))) | |
1521 | ? "yes" : "no")); | |
b082215e | 1522 | if (!is_macro) |
1523 | { | |
551e34da | 1524 | const line_map_ordinary *ord_map = linemap_check_ordinary (map); |
b082215e | 1525 | unsigned includer_ix; |
551e34da | 1526 | const line_map_ordinary *includer_map; |
b082215e | 1527 | |
551e34da | 1528 | includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map); |
b082215e | 1529 | includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set) |
1530 | ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix) | |
1531 | : NULL; | |
1532 | ||
551e34da | 1533 | fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map), |
1534 | ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map)); | |
b082215e | 1535 | fprintf (stream, "Included from: [%d] %s\n", includer_ix, |
1536 | includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None"); | |
1537 | } | |
1538 | else | |
551e34da | 1539 | { |
1540 | const line_map_macro *macro_map = linemap_check_macro (map); | |
1541 | fprintf (stream, "Macro: %s (%u tokens)\n", | |
1542 | linemap_map_get_macro_name (macro_map), | |
1543 | MACRO_MAP_NUM_MACRO_TOKENS (macro_map)); | |
1544 | } | |
b082215e | 1545 | |
1546 | fprintf (stream, "\n"); | |
1547 | } | |
1548 | ||
1549 | ||
62db153a | 1550 | /* Dump debugging information about source location LOC into the file |
1551 | stream STREAM. SET is the line map set LOC comes from. */ | |
1552 | ||
1553 | void | |
1554 | linemap_dump_location (struct line_maps *set, | |
1555 | source_location loc, | |
1556 | FILE *stream) | |
1557 | { | |
551e34da | 1558 | const line_map_ordinary *map; |
62db153a | 1559 | source_location location; |
5ebe2143 | 1560 | const char *path = "", *from = ""; |
1561 | int l = -1, c = -1, s = -1, e = -1; | |
62db153a | 1562 | |
5169661d | 1563 | if (IS_ADHOC_LOC (loc)) |
1564 | loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus; | |
1565 | ||
62db153a | 1566 | if (loc == 0) |
1567 | return; | |
1568 | ||
1569 | location = | |
1570 | linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map); | |
62db153a | 1571 | |
5ebe2143 | 1572 | if (map == NULL) |
1573 | /* Only reserved locations can be tolerated in this case. */ | |
1574 | linemap_assert (location < RESERVED_LOCATION_COUNT); | |
62db153a | 1575 | else |
5ebe2143 | 1576 | { |
1577 | path = LINEMAP_FILE (map); | |
1578 | l = SOURCE_LINE (map, location); | |
1579 | c = SOURCE_COLUMN (map, location); | |
1580 | s = LINEMAP_SYSP (map) != 0; | |
1581 | e = location != loc; | |
1582 | if (e) | |
1583 | from = "N/A"; | |
1584 | else | |
1585 | from = (INCLUDED_FROM (set, map)) | |
1586 | ? LINEMAP_FILE (INCLUDED_FROM (set, map)) | |
1587 | : "<NULL>"; | |
1588 | } | |
62db153a | 1589 | |
1590 | /* P: path, L: line, C: column, S: in-system-header, M: map address, | |
5ebe2143 | 1591 | E: macro expansion?, LOC: original location, R: resolved location */ |
1592 | fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}", | |
1593 | path, from, l, c, s, (void*)map, e, loc, location); | |
62db153a | 1594 | } |
e77b8253 | 1595 | |
ffc2c526 | 1596 | /* Return the highest location emitted for a given file for which |
1597 | there is a line map in SET. FILE_NAME is the file name to | |
1598 | consider. If the function returns TRUE, *LOC is set to the highest | |
1599 | location emitted for that file. */ | |
1600 | ||
1601 | bool | |
1602 | linemap_get_file_highest_location (struct line_maps *set, | |
1603 | const char *file_name, | |
1604 | source_location *loc) | |
1605 | { | |
1606 | /* If the set is empty or no ordinary map has been created then | |
1607 | there is no file to look for ... */ | |
1608 | if (set == NULL || set->info_ordinary.used == 0) | |
1609 | return false; | |
1610 | ||
1611 | /* Now look for the last ordinary map created for FILE_NAME. */ | |
1612 | int i; | |
1613 | for (i = set->info_ordinary.used - 1; i >= 0; --i) | |
1614 | { | |
551e34da | 1615 | const char *fname = set->info_ordinary.maps[i].to_file; |
ffc2c526 | 1616 | if (fname && !filename_cmp (fname, file_name)) |
1617 | break; | |
1618 | } | |
1619 | ||
1620 | if (i < 0) | |
1621 | return false; | |
1622 | ||
1623 | /* The highest location for a given map is either the starting | |
1624 | location of the next map minus one, or -- if the map is the | |
1625 | latest one -- the highest location of the set. */ | |
1626 | source_location result; | |
1627 | if (i == (int) set->info_ordinary.used - 1) | |
1628 | result = set->highest_location; | |
1629 | else | |
1630 | result = set->info_ordinary.maps[i + 1].start_location - 1; | |
1631 | ||
1632 | *loc = result; | |
1633 | return true; | |
1634 | } | |
1635 | ||
e77b8253 | 1636 | /* Compute and return statistics about the memory consumption of some |
1637 | parts of the line table SET. */ | |
1638 | ||
1639 | void | |
1640 | linemap_get_statistics (struct line_maps *set, | |
1641 | struct linemap_stats *s) | |
1642 | { | |
2a688977 | 1643 | long ordinary_maps_allocated_size, ordinary_maps_used_size, |
e77b8253 | 1644 | macro_maps_allocated_size, macro_maps_used_size, |
1645 | macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0; | |
1646 | ||
551e34da | 1647 | const line_map_macro *cur_map; |
e77b8253 | 1648 | |
1649 | ordinary_maps_allocated_size = | |
551e34da | 1650 | LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary); |
e77b8253 | 1651 | |
1652 | ordinary_maps_used_size = | |
551e34da | 1653 | LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary); |
e77b8253 | 1654 | |
1655 | macro_maps_allocated_size = | |
551e34da | 1656 | LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro); |
e77b8253 | 1657 | |
1658 | for (cur_map = LINEMAPS_MACRO_MAPS (set); | |
1659 | cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set); | |
1660 | ++cur_map) | |
1661 | { | |
1662 | unsigned i; | |
1663 | ||
1664 | linemap_assert (linemap_macro_expansion_map_p (cur_map)); | |
1665 | ||
1666 | macro_maps_locations_size += | |
1667 | 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location); | |
1668 | ||
1669 | for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2) | |
1670 | { | |
1671 | if (MACRO_MAP_LOCATIONS (cur_map)[i] == | |
1672 | MACRO_MAP_LOCATIONS (cur_map)[i + 1]) | |
1673 | duplicated_macro_maps_locations_size += | |
1674 | sizeof (source_location); | |
1675 | } | |
1676 | } | |
1677 | ||
1678 | macro_maps_used_size = | |
551e34da | 1679 | LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro); |
e77b8253 | 1680 | |
1681 | s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set); | |
1682 | s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set); | |
1683 | s->ordinary_maps_allocated_size = ordinary_maps_allocated_size; | |
1684 | s->ordinary_maps_used_size = ordinary_maps_used_size; | |
1685 | s->num_expanded_macros = num_expanded_macros_counter; | |
1686 | s->num_macro_tokens = num_macro_tokens_counter; | |
1687 | s->num_macro_maps_used = LINEMAPS_MACRO_USED (set); | |
1688 | s->macro_maps_allocated_size = macro_maps_allocated_size; | |
1689 | s->macro_maps_locations_size = macro_maps_locations_size; | |
1690 | s->macro_maps_used_size = macro_maps_used_size; | |
1691 | s->duplicated_macro_maps_locations_size = | |
1692 | duplicated_macro_maps_locations_size; | |
1693 | } | |
b082215e | 1694 | |
1695 | ||
1696 | /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used. | |
1697 | NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO | |
1698 | specifies how many macro maps to dump. */ | |
1699 | ||
1700 | void | |
1701 | line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary, | |
1702 | unsigned int num_macro) | |
1703 | { | |
1704 | unsigned int i; | |
1705 | ||
1706 | if (set == NULL) | |
1707 | return; | |
1708 | ||
1709 | if (stream == NULL) | |
1710 | stream = stderr; | |
1711 | ||
1712 | fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set)); | |
1713 | fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set)); | |
1714 | fprintf (stream, "Include stack depth: %d\n", set->depth); | |
1715 | fprintf (stream, "Highest location: %u\n", set->highest_location); | |
1716 | ||
1717 | if (num_ordinary) | |
1718 | { | |
1719 | fprintf (stream, "\nOrdinary line maps\n"); | |
1720 | for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++) | |
1721 | linemap_dump (stream, set, i, false); | |
1722 | fprintf (stream, "\n"); | |
1723 | } | |
1724 | ||
1725 | if (num_macro) | |
1726 | { | |
1727 | fprintf (stream, "\nMacro line maps\n"); | |
1728 | for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++) | |
1729 | linemap_dump (stream, set, i, true); | |
1730 | fprintf (stream, "\n"); | |
1731 | } | |
1732 | } |