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