[AArch64] PR target/68129: Define TARGET_SUPPORTS_WIDE_INT
[official-gcc.git] / libcpp / line-map.c
blob3c19f93b7a5a671781775cd4493ae687361877d9
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
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
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>.
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! */
22 #include "config.h"
23 #include "system.h"
24 #include "line-map.h"
25 #include "cpplib.h"
26 #include "internal.h"
27 #include "hashtab.h"
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). */
32 const unsigned int LINE_MAP_MAX_COLUMN_NUMBER = (1U << 17);
34 /* Do not track column numbers if locations get higher than this. */
35 const source_location LINE_MAP_MAX_LOCATION_WITH_COLS = 0x60000000;
37 /* Highest possible source location encoded within an ordinary or
38 macro map. */
39 const source_location LINE_MAP_MAX_SOURCE_LOCATION = 0x70000000;
41 static void trace_include (const struct line_maps *, const line_map_ordinary *);
42 static const line_map_ordinary * linemap_ordinary_map_lookup (struct line_maps *,
43 source_location);
44 static const line_map_macro* linemap_macro_map_lookup (struct line_maps *,
45 source_location);
46 static source_location linemap_macro_map_loc_to_def_point
47 (const line_map_macro *, source_location);
48 static source_location linemap_macro_map_loc_unwind_toward_spelling
49 (const line_map_macro *, source_location);
50 static source_location linemap_macro_map_loc_to_exp_point
51 (const line_map_macro *, source_location);
52 static source_location linemap_macro_loc_to_spelling_point
53 (struct line_maps *, source_location, const line_map_ordinary **);
54 static source_location linemap_macro_loc_to_def_point (struct line_maps *,
55 source_location,
56 const line_map_ordinary **);
57 static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
58 source_location,
59 const line_map_ordinary **);
61 /* Counters defined in macro.c. */
62 extern unsigned num_expanded_macros_counter;
63 extern unsigned num_macro_tokens_counter;
65 /* Hash function for location_adhoc_data hashtable. */
67 static hashval_t
68 location_adhoc_data_hash (const void *l)
70 const struct location_adhoc_data *lb =
71 (const struct location_adhoc_data *) l;
72 return (hashval_t) lb->locus + (size_t) lb->data;
75 /* Compare function for location_adhoc_data hashtable. */
77 static int
78 location_adhoc_data_eq (const void *l1, const void *l2)
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;
87 /* Update the hashtable when location_adhoc_data is reallocated. */
89 static int
90 location_adhoc_data_update (void **slot, void *data)
92 *((char **) slot) += *((long long *) data);
93 return 1;
96 /* Rebuild the hash table from the location adhoc data. */
98 void
99 rebuild_location_adhoc_htab (struct line_maps *set)
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);
109 /* Combine LOCUS and DATA to a combined adhoc loc. */
111 source_location
112 get_combined_adhoc_loc (struct line_maps *set,
113 source_location locus, void *data)
115 struct location_adhoc_data lb;
116 struct location_adhoc_data **slot;
118 linemap_assert (data);
120 if (IS_ADHOC_LOC (locus))
121 locus
122 = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
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)
131 if (set->location_adhoc_data_map.curr_loc >=
132 set->location_adhoc_data_map.allocated)
134 char *orig_data = (char *) set->location_adhoc_data_map.data;
135 long long offset;
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);
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));
149 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
150 if (set->location_adhoc_data_map.allocated > 128)
151 htab_traverse (set->location_adhoc_data_map.htab,
152 location_adhoc_data_update, &offset);
154 *slot = set->location_adhoc_data_map.data
155 + set->location_adhoc_data_map.curr_loc;
156 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
157 = lb;
159 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
162 /* Return the data for the adhoc loc. */
164 void *
165 get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
167 linemap_assert (IS_ADHOC_LOC (loc));
168 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
171 /* Return the location for the adhoc loc. */
173 source_location
174 get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
176 linemap_assert (IS_ADHOC_LOC (loc));
177 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
180 /* Finalize the location_adhoc_data structure. */
181 void
182 location_adhoc_data_fini (struct line_maps *set)
184 htab_delete (set->location_adhoc_data_map.htab);
187 /* Initialize a line map set. */
189 void
190 linemap_init (struct line_maps *set,
191 source_location builtin_location)
193 memset (set, 0, sizeof (struct line_maps));
194 set->highest_location = RESERVED_LOCATION_COUNT - 1;
195 set->highest_line = RESERVED_LOCATION_COUNT - 1;
196 set->location_adhoc_data_map.htab =
197 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
198 set->builtin_location = builtin_location;
201 /* Check for and warn about line_maps entered but not exited. */
203 void
204 linemap_check_files_exited (struct line_maps *set)
206 const line_map_ordinary *map;
207 /* Depending upon whether we are handling preprocessed input or
208 not, this can be a user error or an ICE. */
209 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
210 ! MAIN_FILE_P (map);
211 map = INCLUDED_FROM (set, map))
212 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
213 ORDINARY_MAP_FILE_NAME (map));
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. */
221 static struct line_map *
222 new_linemap (struct line_maps *set,
223 enum lc_reason reason)
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;
230 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
232 /* We ran out of allocated line maps. Let's allocate more. */
233 unsigned alloc_size;
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);
239 line_map_round_alloc_size_func round_alloc_size =
240 set->round_alloc_size;
242 size_t map_size = (macro_map_p
243 ? sizeof (line_map_macro)
244 : sizeof (line_map_ordinary));
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.
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. */
254 alloc_size =
255 (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
256 * map_size;
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);
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. */
266 LINEMAPS_ALLOCATED (set, macro_map_p) =
267 alloc_size / map_size;
269 /* And now let's really do the re-allocation. */
270 if (macro_map_p)
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)];
278 else
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)];
286 memset (result, 0,
287 ((LINEMAPS_ALLOCATED (set, macro_map_p)
288 - LINEMAPS_USED (set, macro_map_p))
289 * map_size));
291 else
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)];
299 LINEMAPS_USED (set, macro_map_p)++;
301 result->reason = reason;
302 return result;
305 /* Add a mapping of logical source line to physical source file and
306 line number.
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.
314 FROM_LINE should be monotonic increasing across calls to this
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. */
318 const struct line_map *
319 linemap_add (struct line_maps *set, enum lc_reason reason,
320 unsigned int sysp, const char *to_file, linenum_type to_line)
322 source_location start_location = set->highest_location + 1;
324 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
325 && (start_location
326 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
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));
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)
337 set->depth--;
338 return NULL;
341 linemap_assert (reason != LC_ENTER_MACRO);
342 line_map_ordinary *map = linemap_check_ordinary (new_linemap (set, reason));
344 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
345 to_file = "<stdin>";
347 if (reason == LC_RENAME_VERBATIM)
348 reason = LC_RENAME;
350 if (reason == LC_LEAVE)
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. */
356 line_map_ordinary *from;
357 bool error;
359 if (MAIN_FILE_P (map - 1))
361 /* So this _should_ mean we are leaving the main file --
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. */
367 error = true;
368 reason = LC_RENAME;
369 from = map - 1;
371 else
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. */
376 from = INCLUDED_FROM (set, map - 1);
377 error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
378 to_file);
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);
387 /* A TO_FILE of NULL is special - we use the natural values. */
388 if (error || to_file == NULL)
390 to_file = ORDINARY_MAP_FILE_NAME (from);
391 to_line = SOURCE_LINE (from, from[1].start_location);
392 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
396 map->sysp = sysp;
397 map->start_location = start_location;
398 map->to_file = to_file;
399 map->to_line = to_line;
400 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
401 map->column_bits = 0;
402 set->highest_location = start_location;
403 set->highest_line = start_location;
404 set->max_column_hint = 0;
406 if (reason == LC_ENTER)
408 map->included_from =
409 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
410 set->depth++;
411 if (set->trace_includes)
412 trace_include (set, map);
414 else if (reason == LC_RENAME)
415 map->included_from = ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
416 else if (reason == LC_LEAVE)
418 set->depth--;
419 map->included_from =
420 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
423 return map;
426 /* Returns TRUE if the line table set tracks token locations across
427 macro expansion, FALSE otherwise. */
429 bool
430 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
432 return LINEMAPS_MACRO_MAPS (set) != NULL;
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.
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.
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. */
458 const line_map_macro *
459 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
460 source_location expansion, unsigned int num_tokens)
462 line_map_macro *map;
463 source_location start_location;
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);
469 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
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;
476 map = linemap_check_macro (new_linemap (set, LC_ENTER_MACRO));
478 map->start_location = start_location;
479 map->macro = macro_node;
480 map->n_tokens = num_tokens;
481 map->macro_locations
482 = (source_location*) reallocator (NULL,
483 2 * num_tokens
484 * sizeof (source_location));
485 map->expansion = expansion;
486 memset (MACRO_MAP_LOCATIONS (map), 0,
487 num_tokens * sizeof (source_location));
489 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
491 return map;
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
499 A call to this function must come after a call to
500 linemap_enter_macro.
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.
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).
513 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
514 either of the token itself or of a macro parameter that it
515 replaces. */
517 source_location
518 linemap_add_macro_token (const line_map_macro *map,
519 unsigned int token_no,
520 source_location orig_loc,
521 source_location orig_parm_replacement_loc)
523 source_location result;
525 linemap_assert (linemap_macro_expansion_map_p (map));
526 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
528 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
529 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
531 result = MAP_START_LOCATION (map) + token_no;
532 return result;
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). */
541 source_location
542 linemap_line_start (struct line_maps *set, linenum_type to_line,
543 unsigned int max_column_hint)
545 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
546 source_location highest = set->highest_location;
547 source_location r;
548 linenum_type last_line =
549 SOURCE_LINE (map, set->highest_line);
550 int line_delta = to_line - last_line;
551 bool add_map = false;
553 if (line_delta < 0
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
558 && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10)
559 || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
560 && (set->max_column_hint || highest >= LINE_MAP_MAX_SOURCE_LOCATION)))
561 add_map = true;
562 else
563 max_column_hint = set->max_column_hint;
564 if (add_map)
566 int column_bits;
567 if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
568 || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
570 /* If the column number is ridiculous or we've allocated a huge
571 number of source_locations, give up on column numbers. */
572 max_column_hint = 0;
573 column_bits = 0;
574 if (highest > LINE_MAP_MAX_SOURCE_LOCATION)
575 return 0;
577 else
579 column_bits = 7;
580 while (max_column_hint >= (1U << column_bits))
581 column_bits++;
582 max_column_hint = 1U << column_bits;
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. */
586 if (line_delta < 0
587 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
588 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
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)));
595 map->column_bits = column_bits;
596 r = (MAP_START_LOCATION (map)
597 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
598 << column_bits));
600 else
601 r = highest - SOURCE_COLUMN (map, highest)
602 + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
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;
609 set->highest_line = r;
610 if (r > set->highest_location)
611 set->highest_location = r;
612 set->max_column_hint = max_column_hint;
613 return r;
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. */
621 source_location
622 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
624 source_location r = set->highest_line;
626 linemap_assert
627 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
629 if (to_column >= set->max_column_hint)
631 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
632 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
634 /* Running low on source_locations - disable column numbers. */
635 return r;
637 else
639 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
640 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
643 r = r + to_column;
644 if (r >= set->highest_location)
645 set->highest_location = r;
646 return r;
649 /* Encode and return a source location from a given line and
650 column. */
652 source_location
653 linemap_position_for_line_and_column (const line_map_ordinary *ord_map,
654 linenum_type line,
655 unsigned column)
657 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
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)));
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. */
669 source_location
670 linemap_position_for_loc_and_offset (struct line_maps *set,
671 source_location loc,
672 unsigned int offset)
674 const line_map_ordinary * map = NULL;
676 /* This function does not support virtual locations yet. */
677 if (linemap_assert_fails
678 (!linemap_location_from_macro_expansion_p (set, loc)))
679 return loc;
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;
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
691 location encoded by MAP. This can fail if the line information
692 is messed up because of line directives (see PR66415). */
693 if (MAP_START_LOCATION (map) >= loc + offset)
694 return loc;
696 linenum_type line = SOURCE_LINE (map, loc);
697 unsigned int column = SOURCE_COLUMN (map, loc);
699 /* If MAP is not the last line map of its set, then the new location
700 (loc + offset) should be less than the first location encoded by
701 the next line map of the set. Otherwise, we try to encode the
702 location in the next map. */
703 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
704 && loc + offset >= MAP_START_LOCATION (&map[1]))
706 map = &map[1];
707 /* If the next map starts in a higher line, we cannot encode the
708 location there. */
709 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
710 return loc;
713 offset += column;
714 if (linemap_assert_fails (offset < (1u << map->column_bits)))
715 return loc;
717 source_location r =
718 linemap_position_for_line_and_column (map, line, offset);
719 if (linemap_assert_fails (r <= set->highest_location)
720 || linemap_assert_fails (map == linemap_lookup (set, r)))
721 return loc;
723 return r;
726 /* Given a virtual source location yielded by a map (either an
727 ordinary or a macro map), returns that map. */
729 const struct line_map*
730 linemap_lookup (struct line_maps *set, source_location line)
732 if (IS_ADHOC_LOC (line))
733 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
734 if (linemap_location_from_macro_expansion_p (set, line))
735 return linemap_macro_map_lookup (set, line);
736 return linemap_ordinary_map_lookup (set, line);
739 /* Given a source location yielded by an ordinary map, returns that
740 map. Since the set is built chronologically, the logical lines are
741 monotonic increasing, and so the list is sorted and we can use a
742 binary search. */
744 static const line_map_ordinary *
745 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
747 unsigned int md, mn, mx;
748 const line_map_ordinary *cached, *result;
750 if (IS_ADHOC_LOC (line))
751 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
753 if (set == NULL || line < RESERVED_LOCATION_COUNT)
754 return NULL;
756 mn = LINEMAPS_ORDINARY_CACHE (set);
757 mx = LINEMAPS_ORDINARY_USED (set);
759 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
760 /* We should get a segfault if no line_maps have been added yet. */
761 if (line >= MAP_START_LOCATION (cached))
763 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
764 return cached;
766 else
768 mx = mn;
769 mn = 0;
772 while (mx - mn > 1)
774 md = (mn + mx) / 2;
775 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
776 mx = md;
777 else
778 mn = md;
781 LINEMAPS_ORDINARY_CACHE (set) = mn;
782 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
783 linemap_assert (line >= MAP_START_LOCATION (result));
784 return result;
787 /* Given a source location yielded by a macro map, returns that map.
788 Since the set is built chronologically, the logical lines are
789 monotonic decreasing, and so the list is sorted and we can use a
790 binary search. */
792 static const line_map_macro *
793 linemap_macro_map_lookup (struct line_maps *set, source_location line)
795 unsigned int md, mn, mx;
796 const struct line_map_macro *cached, *result;
798 if (IS_ADHOC_LOC (line))
799 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
801 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
803 if (set == NULL)
804 return NULL;
806 mn = LINEMAPS_MACRO_CACHE (set);
807 mx = LINEMAPS_MACRO_USED (set);
808 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
810 if (line >= MAP_START_LOCATION (cached))
812 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
813 return cached;
814 mx = mn - 1;
815 mn = 0;
818 while (mn < mx)
820 md = (mx + mn) / 2;
821 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
822 mn = md + 1;
823 else
824 mx = md;
827 LINEMAPS_MACRO_CACHE (set) = mx;
828 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
829 linemap_assert (MAP_START_LOCATION (result) <= line);
831 return result;
834 /* Return TRUE if MAP encodes locations coming from a macro
835 replacement-list at macro expansion point. */
837 bool
838 linemap_macro_expansion_map_p (const struct line_map *map)
840 if (!map)
841 return false;
842 return (map->reason == LC_ENTER_MACRO);
845 /* If LOCATION is the locus of a token in a replacement-list of a
846 macro expansion return the location of the macro expansion point.
848 Read the comments of struct line_map and struct line_map_macro in
849 line-map.h to understand what a macro expansion point is. */
851 static source_location
852 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
853 source_location location ATTRIBUTE_UNUSED)
855 linemap_assert (linemap_macro_expansion_map_p (map)
856 && location >= MAP_START_LOCATION (map));
858 /* Make sure LOCATION is correct. */
859 linemap_assert ((location - MAP_START_LOCATION (map))
860 < MACRO_MAP_NUM_MACRO_TOKENS (map));
862 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
865 /* LOCATION is the source location of a token that belongs to a macro
866 replacement-list as part of the macro expansion denoted by MAP.
868 Return the location of the token at the definition point of the
869 macro. */
871 static source_location
872 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
873 source_location location)
875 unsigned token_no;
877 linemap_assert (linemap_macro_expansion_map_p (map)
878 && location >= MAP_START_LOCATION (map));
879 linemap_assert (location >= RESERVED_LOCATION_COUNT);
881 token_no = location - MAP_START_LOCATION (map);
882 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
884 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
886 return location;
889 /* If LOCATION is the locus of a token that is an argument of a
890 function-like macro M and appears in the expansion of M, return the
891 locus of that argument in the context of the caller of M.
893 In other words, this returns the xI location presented in the
894 comments of line_map_macro above. */
895 source_location
896 linemap_macro_map_loc_unwind_toward_spelling (const line_map_macro* map,
897 source_location location)
899 unsigned token_no;
901 linemap_assert (linemap_macro_expansion_map_p (map)
902 && location >= MAP_START_LOCATION (map));
903 linemap_assert (location >= RESERVED_LOCATION_COUNT);
905 token_no = location - MAP_START_LOCATION (map);
906 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
908 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
910 return location;
913 /* Return the source line number corresponding to source location
914 LOCATION. SET is the line map set LOCATION comes from. If
915 LOCATION is the source location of token that is part of the
916 replacement-list of a macro expansion return the line number of the
917 macro expansion point. */
920 linemap_get_expansion_line (struct line_maps *set,
921 source_location location)
923 const line_map_ordinary *map = NULL;
925 if (IS_ADHOC_LOC (location))
926 location = set->location_adhoc_data_map.data[location
927 & MAX_SOURCE_LOCATION].locus;
929 if (location < RESERVED_LOCATION_COUNT)
930 return 0;
932 location =
933 linemap_macro_loc_to_exp_point (set, location, &map);
935 return SOURCE_LINE (map, location);
938 /* Return the path of the file corresponding to source code location
939 LOCATION.
941 If LOCATION is the source location of token that is part of the
942 replacement-list of a macro expansion return the file path of the
943 macro expansion point.
945 SET is the line map set LOCATION comes from. */
947 const char*
948 linemap_get_expansion_filename (struct line_maps *set,
949 source_location location)
951 const struct line_map_ordinary *map = NULL;
953 if (IS_ADHOC_LOC (location))
954 location = set->location_adhoc_data_map.data[location
955 & MAX_SOURCE_LOCATION].locus;
957 if (location < RESERVED_LOCATION_COUNT)
958 return NULL;
960 location =
961 linemap_macro_loc_to_exp_point (set, location, &map);
963 return LINEMAP_FILE (map);
966 /* Return the name of the macro associated to MACRO_MAP. */
968 const char*
969 linemap_map_get_macro_name (const line_map_macro *macro_map)
971 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
972 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
975 /* Return a positive value if LOCATION is the locus of a token that is
976 located in a system header, O otherwise. It returns 1 if LOCATION
977 is the locus of a token that is located in a system header, and 2
978 if LOCATION is the locus of a token located in a C system header
979 that therefore needs to be extern "C" protected in C++.
981 Note that this function returns 1 if LOCATION belongs to a token
982 that is part of a macro replacement-list defined in a system
983 header, but expanded in a non-system file. */
986 linemap_location_in_system_header_p (struct line_maps *set,
987 source_location location)
989 const struct line_map *map = NULL;
991 if (IS_ADHOC_LOC (location))
992 location = set->location_adhoc_data_map.data[location
993 & MAX_SOURCE_LOCATION].locus;
995 if (location < RESERVED_LOCATION_COUNT)
996 return false;
998 /* Let's look at where the token for LOCATION comes from. */
999 while (true)
1001 map = linemap_lookup (set, location);
1002 if (map != NULL)
1004 if (!linemap_macro_expansion_map_p (map))
1005 /* It's a normal token. */
1006 return LINEMAP_SYSP (linemap_check_ordinary (map));
1007 else
1009 const line_map_macro *macro_map = linemap_check_macro (map);
1011 /* It's a token resulting from a macro expansion. */
1012 source_location loc =
1013 linemap_macro_map_loc_unwind_toward_spelling (macro_map, location);
1014 if (loc < RESERVED_LOCATION_COUNT)
1015 /* This token might come from a built-in macro. Let's
1016 look at where that macro got expanded. */
1017 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1018 else
1019 location = loc;
1022 else
1023 break;
1025 return false;
1028 /* Return TRUE if LOCATION is a source code location of a token coming
1029 from a macro replacement-list at a macro expansion point, FALSE
1030 otherwise. */
1032 bool
1033 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1034 source_location location)
1036 if (IS_ADHOC_LOC (location))
1037 location = set->location_adhoc_data_map.data[location
1038 & MAX_SOURCE_LOCATION].locus;
1040 linemap_assert (location <= MAX_SOURCE_LOCATION
1041 && (set->highest_location
1042 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
1043 if (set == NULL)
1044 return false;
1045 return (location > set->highest_location);
1048 /* Given two virtual locations *LOC0 and *LOC1, return the first
1049 common macro map in their macro expansion histories. Return NULL
1050 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1051 virtual location of the token inside the resulting macro. */
1053 static const struct line_map*
1054 first_map_in_common_1 (struct line_maps *set,
1055 source_location *loc0,
1056 source_location *loc1)
1058 source_location l0 = *loc0, l1 = *loc1;
1059 const struct line_map *map0 = linemap_lookup (set, l0),
1060 *map1 = linemap_lookup (set, l1);
1062 while (linemap_macro_expansion_map_p (map0)
1063 && linemap_macro_expansion_map_p (map1)
1064 && (map0 != map1))
1066 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1068 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1069 l0);
1070 map0 = linemap_lookup (set, l0);
1072 else
1074 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1075 l1);
1076 map1 = linemap_lookup (set, l1);
1080 if (map0 == map1)
1082 *loc0 = l0;
1083 *loc1 = l1;
1084 return map0;
1086 return NULL;
1089 /* Given two virtual locations LOC0 and LOC1, return the first common
1090 macro map in their macro expansion histories. Return NULL if no
1091 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1092 virtual location of the token inside the resulting macro, upon
1093 return of a non-NULL result. */
1095 static const struct line_map*
1096 first_map_in_common (struct line_maps *set,
1097 source_location loc0,
1098 source_location loc1,
1099 source_location *res_loc0,
1100 source_location *res_loc1)
1102 *res_loc0 = loc0;
1103 *res_loc1 = loc1;
1105 return first_map_in_common_1 (set, res_loc0, res_loc1);
1108 /* Return a positive value if PRE denotes the location of a token that
1109 comes before the token of POST, 0 if PRE denotes the location of
1110 the same token as the token for POST, and a negative value
1111 otherwise. */
1114 linemap_compare_locations (struct line_maps *set,
1115 source_location pre,
1116 source_location post)
1118 bool pre_virtual_p, post_virtual_p;
1119 source_location l0 = pre, l1 = post;
1121 if (IS_ADHOC_LOC (l0))
1122 l0 = set->location_adhoc_data_map.data[l0 & MAX_SOURCE_LOCATION].locus;
1123 if (IS_ADHOC_LOC (l1))
1124 l1 = set->location_adhoc_data_map.data[l1 & MAX_SOURCE_LOCATION].locus;
1126 if (l0 == l1)
1127 return 0;
1129 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1130 l0 = linemap_resolve_location (set, l0,
1131 LRK_MACRO_EXPANSION_POINT,
1132 NULL);
1134 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1135 l1 = linemap_resolve_location (set, l1,
1136 LRK_MACRO_EXPANSION_POINT,
1137 NULL);
1139 if (l0 == l1
1140 && pre_virtual_p
1141 && post_virtual_p)
1143 /* So pre and post represent two tokens that are present in a
1144 same macro expansion. Let's see if the token for pre was
1145 before the token for post in that expansion. */
1146 unsigned i0, i1;
1147 const struct line_map *map =
1148 first_map_in_common (set, pre, post, &l0, &l1);
1150 if (map == NULL)
1151 /* This should not be possible. */
1152 abort ();
1154 i0 = l0 - MAP_START_LOCATION (map);
1155 i1 = l1 - MAP_START_LOCATION (map);
1156 return i1 - i0;
1159 return l1 - l0;
1162 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1164 static void
1165 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1167 unsigned int i = set->depth;
1169 while (--i)
1170 putc ('.', stderr);
1172 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1175 /* Return the spelling location of the token wherever it comes from,
1176 whether part of a macro definition or not.
1178 This is a subroutine for linemap_resolve_location. */
1180 static source_location
1181 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1182 source_location location,
1183 const line_map_ordinary **original_map)
1185 struct line_map *map;
1187 if (IS_ADHOC_LOC (location))
1188 location = set->location_adhoc_data_map.data[location
1189 & MAX_SOURCE_LOCATION].locus;
1191 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1193 while (true)
1195 map = const_cast <line_map *> (linemap_lookup (set, location));
1196 if (!linemap_macro_expansion_map_p (map))
1197 break;
1199 location
1200 = linemap_macro_map_loc_unwind_toward_spelling
1201 (linemap_check_macro (map),
1202 location);
1205 if (original_map)
1206 *original_map = linemap_check_ordinary (map);
1207 return location;
1210 /* If LOCATION is the source location of a token that belongs to a
1211 macro replacement-list -- as part of a macro expansion -- then
1212 return the location of the token at the definition point of the
1213 macro. Otherwise, return LOCATION. SET is the set of maps
1214 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1215 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1216 returned location comes from.
1218 This is a subroutine of linemap_resolve_location. */
1220 static source_location
1221 linemap_macro_loc_to_def_point (struct line_maps *set,
1222 source_location location,
1223 const line_map_ordinary **original_map)
1225 struct line_map *map;
1227 if (IS_ADHOC_LOC (location))
1228 location = set->location_adhoc_data_map.data[location
1229 & MAX_SOURCE_LOCATION].locus;
1231 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1233 while (true)
1235 map = const_cast <line_map *> (linemap_lookup (set, location));
1236 if (!linemap_macro_expansion_map_p (map))
1237 break;
1239 location =
1240 linemap_macro_map_loc_to_def_point (linemap_check_macro (map),
1241 location);
1244 if (original_map)
1245 *original_map = linemap_check_ordinary (map);
1246 return location;
1249 /* If LOCATION is the source location of a token that belongs to a
1250 macro replacement-list -- at a macro expansion point -- then return
1251 the location of the topmost expansion point of the macro. We say
1252 topmost because if we are in the context of a nested macro
1253 expansion, the function returns the source location of the first
1254 macro expansion that triggered the nested expansions.
1256 Otherwise, return LOCATION. SET is the set of maps location come
1257 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1258 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1259 location comes from.
1261 This is a subroutine of linemap_resolve_location. */
1263 static source_location
1264 linemap_macro_loc_to_exp_point (struct line_maps *set,
1265 source_location location,
1266 const line_map_ordinary **original_map)
1268 struct line_map *map;
1270 if (IS_ADHOC_LOC (location))
1271 location = set->location_adhoc_data_map.data[location
1272 & MAX_SOURCE_LOCATION].locus;
1274 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1276 while (true)
1278 map = const_cast <line_map *> (linemap_lookup (set, location));
1279 if (!linemap_macro_expansion_map_p (map))
1280 break;
1281 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1282 location);
1285 if (original_map)
1286 *original_map = linemap_check_ordinary (map);
1287 return location;
1290 /* Resolve a virtual location into either a spelling location, an
1291 expansion point location or a token argument replacement point
1292 location. Return the map that encodes the virtual location as well
1293 as the resolved location.
1295 If LOC is *NOT* the location of a token resulting from the
1296 expansion of a macro, then the parameter LRK (which stands for
1297 Location Resolution Kind) is ignored and the resulting location
1298 just equals the one given in argument.
1300 Now if LOC *IS* the location of a token resulting from the
1301 expansion of a macro, this is what happens.
1303 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1304 -------------------------------
1306 The virtual location is resolved to the first macro expansion point
1307 that led to this macro expansion.
1309 * If LRK is set to LRK_SPELLING_LOCATION
1310 -------------------------------------
1312 The virtual location is resolved to the locus where the token has
1313 been spelled in the source. This can follow through all the macro
1314 expansions that led to the token.
1316 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1317 --------------------------------------
1319 The virtual location is resolved to the locus of the token in the
1320 context of the macro definition.
1322 If LOC is the locus of a token that is an argument of a
1323 function-like macro [replacing a parameter in the replacement list
1324 of the macro] the virtual location is resolved to the locus of the
1325 parameter that is replaced, in the context of the definition of the
1326 macro.
1328 If LOC is the locus of a token that is not an argument of a
1329 function-like macro, then the function behaves as if LRK was set to
1330 LRK_SPELLING_LOCATION.
1332 If MAP is not NULL, *MAP is set to the map encoding the
1333 returned location. Note that if the returned location wasn't originally
1334 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1335 resolves to a location reserved for the client code, like
1336 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1338 source_location
1339 linemap_resolve_location (struct line_maps *set,
1340 source_location loc,
1341 enum location_resolution_kind lrk,
1342 const line_map_ordinary **map)
1344 if (IS_ADHOC_LOC (loc))
1345 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1347 if (loc < RESERVED_LOCATION_COUNT)
1349 /* A reserved location wasn't encoded in a map. Let's return a
1350 NULL map here, just like what linemap_ordinary_map_lookup
1351 does. */
1352 if (map)
1353 *map = NULL;
1354 return loc;
1357 switch (lrk)
1359 case LRK_MACRO_EXPANSION_POINT:
1360 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1361 break;
1362 case LRK_SPELLING_LOCATION:
1363 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1364 break;
1365 case LRK_MACRO_DEFINITION_LOCATION:
1366 loc = linemap_macro_loc_to_def_point (set, loc, map);
1367 break;
1368 default:
1369 abort ();
1371 return loc;
1375 Suppose that LOC is the virtual location of a token T coming from
1376 the expansion of a macro M. This function then steps up to get the
1377 location L of the point where M got expanded. If L is a spelling
1378 location inside a macro expansion M', then this function returns
1379 the locus of the point where M' was expanded. Said otherwise, this
1380 function returns the location of T in the context that triggered
1381 the expansion of M.
1383 *LOC_MAP must be set to the map of LOC. This function then sets it
1384 to the map of the returned location. */
1386 source_location
1387 linemap_unwind_toward_expansion (struct line_maps *set,
1388 source_location loc,
1389 const struct line_map **map)
1391 source_location resolved_location;
1392 const line_map_macro *macro_map = linemap_check_macro (*map);
1393 const struct line_map *resolved_map;
1395 if (IS_ADHOC_LOC (loc))
1396 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1398 resolved_location =
1399 linemap_macro_map_loc_unwind_toward_spelling (macro_map, loc);
1400 resolved_map = linemap_lookup (set, resolved_location);
1402 if (!linemap_macro_expansion_map_p (resolved_map))
1404 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1405 resolved_map = linemap_lookup (set, resolved_location);
1408 *map = resolved_map;
1409 return resolved_location;
1412 /* If LOC is the virtual location of a token coming from the expansion
1413 of a macro M and if its spelling location is reserved (e.g, a
1414 location for a built-in token), then this function unwinds (using
1415 linemap_unwind_toward_expansion) the location until a location that
1416 is not reserved and is not in a system header is reached. In other
1417 words, this unwinds the reserved location until a location that is
1418 in real source code is reached.
1420 Otherwise, if the spelling location for LOC is not reserved or if
1421 LOC doesn't come from the expansion of a macro, the function
1422 returns LOC as is and *MAP is not touched.
1424 *MAP is set to the map of the returned location if the later is
1425 different from LOC. */
1426 source_location
1427 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1428 source_location loc,
1429 const struct line_map **map)
1431 source_location resolved_loc;
1432 const struct line_map *map0 = NULL;
1433 const line_map_ordinary *map1 = NULL;
1435 if (IS_ADHOC_LOC (loc))
1436 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1438 map0 = linemap_lookup (set, loc);
1439 if (!linemap_macro_expansion_map_p (map0))
1440 return loc;
1442 resolved_loc = linemap_resolve_location (set, loc,
1443 LRK_SPELLING_LOCATION,
1444 &map1);
1446 if (resolved_loc >= RESERVED_LOCATION_COUNT
1447 && !LINEMAP_SYSP (map1))
1448 return loc;
1450 while (linemap_macro_expansion_map_p (map0)
1451 && (resolved_loc < RESERVED_LOCATION_COUNT
1452 || LINEMAP_SYSP (map1)))
1454 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1455 resolved_loc = linemap_resolve_location (set, loc,
1456 LRK_SPELLING_LOCATION,
1457 &map1);
1460 if (map != NULL)
1461 *map = map0;
1462 return loc;
1465 /* Expand source code location LOC and return a user readable source
1466 code location. LOC must be a spelling (non-virtual) location. If
1467 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1468 location is returned. */
1470 expanded_location
1471 linemap_expand_location (struct line_maps *set,
1472 const struct line_map *map,
1473 source_location loc)
1476 expanded_location xloc;
1478 memset (&xloc, 0, sizeof (xloc));
1479 if (IS_ADHOC_LOC (loc))
1481 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1482 xloc.data
1483 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1486 if (loc < RESERVED_LOCATION_COUNT)
1487 /* The location for this token wasn't generated from a line map.
1488 It was probably a location for a builtin token, chosen by some
1489 client code. Let's not try to expand the location in that
1490 case. */;
1491 else if (map == NULL)
1492 /* We shouldn't be getting a NULL map with a location that is not
1493 reserved by the client code. */
1494 abort ();
1495 else
1497 /* MAP must be an ordinary map and LOC must be non-virtual,
1498 encoded into this map, obviously; the accessors used on MAP
1499 below ensure it is ordinary. Let's just assert the
1500 non-virtualness of LOC here. */
1501 if (linemap_location_from_macro_expansion_p (set, loc))
1502 abort ();
1504 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1506 xloc.file = LINEMAP_FILE (ord_map);
1507 xloc.line = SOURCE_LINE (ord_map, loc);
1508 xloc.column = SOURCE_COLUMN (ord_map, loc);
1509 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1512 return xloc;
1516 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1517 is NULL, use stderr. IS_MACRO is true if the caller wants to
1518 dump a macro map, false otherwise. */
1520 void
1521 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1523 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1524 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1525 "LC_ENTER_MACRO" };
1526 const char *reason;
1527 const line_map *map;
1529 if (stream == NULL)
1530 stream = stderr;
1532 if (!is_macro)
1533 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1534 else
1535 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1537 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1539 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1540 ix, (void *) map, map->start_location, reason,
1541 ((!is_macro
1542 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1543 ? "yes" : "no"));
1544 if (!is_macro)
1546 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1547 unsigned includer_ix;
1548 const line_map_ordinary *includer_map;
1550 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
1551 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1552 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1553 : NULL;
1555 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1556 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1557 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1558 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1560 else
1562 const line_map_macro *macro_map = linemap_check_macro (map);
1563 fprintf (stream, "Macro: %s (%u tokens)\n",
1564 linemap_map_get_macro_name (macro_map),
1565 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1568 fprintf (stream, "\n");
1572 /* Dump debugging information about source location LOC into the file
1573 stream STREAM. SET is the line map set LOC comes from. */
1575 void
1576 linemap_dump_location (struct line_maps *set,
1577 source_location loc,
1578 FILE *stream)
1580 const line_map_ordinary *map;
1581 source_location location;
1582 const char *path = "", *from = "";
1583 int l = -1, c = -1, s = -1, e = -1;
1585 if (IS_ADHOC_LOC (loc))
1586 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1588 if (loc == 0)
1589 return;
1591 location =
1592 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1594 if (map == NULL)
1595 /* Only reserved locations can be tolerated in this case. */
1596 linemap_assert (location < RESERVED_LOCATION_COUNT);
1597 else
1599 path = LINEMAP_FILE (map);
1600 l = SOURCE_LINE (map, location);
1601 c = SOURCE_COLUMN (map, location);
1602 s = LINEMAP_SYSP (map) != 0;
1603 e = location != loc;
1604 if (e)
1605 from = "N/A";
1606 else
1607 from = (INCLUDED_FROM (set, map))
1608 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1609 : "<NULL>";
1612 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1613 E: macro expansion?, LOC: original location, R: resolved location */
1614 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1615 path, from, l, c, s, (void*)map, e, loc, location);
1618 /* Return the highest location emitted for a given file for which
1619 there is a line map in SET. FILE_NAME is the file name to
1620 consider. If the function returns TRUE, *LOC is set to the highest
1621 location emitted for that file. */
1623 bool
1624 linemap_get_file_highest_location (struct line_maps *set,
1625 const char *file_name,
1626 source_location *loc)
1628 /* If the set is empty or no ordinary map has been created then
1629 there is no file to look for ... */
1630 if (set == NULL || set->info_ordinary.used == 0)
1631 return false;
1633 /* Now look for the last ordinary map created for FILE_NAME. */
1634 int i;
1635 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1637 const char *fname = set->info_ordinary.maps[i].to_file;
1638 if (fname && !filename_cmp (fname, file_name))
1639 break;
1642 if (i < 0)
1643 return false;
1645 /* The highest location for a given map is either the starting
1646 location of the next map minus one, or -- if the map is the
1647 latest one -- the highest location of the set. */
1648 source_location result;
1649 if (i == (int) set->info_ordinary.used - 1)
1650 result = set->highest_location;
1651 else
1652 result = set->info_ordinary.maps[i + 1].start_location - 1;
1654 *loc = result;
1655 return true;
1658 /* Compute and return statistics about the memory consumption of some
1659 parts of the line table SET. */
1661 void
1662 linemap_get_statistics (struct line_maps *set,
1663 struct linemap_stats *s)
1665 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1666 macro_maps_allocated_size, macro_maps_used_size,
1667 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1669 const line_map_macro *cur_map;
1671 ordinary_maps_allocated_size =
1672 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1674 ordinary_maps_used_size =
1675 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1677 macro_maps_allocated_size =
1678 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1680 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1681 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1682 ++cur_map)
1684 unsigned i;
1686 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1688 macro_maps_locations_size +=
1689 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1691 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1693 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1694 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1695 duplicated_macro_maps_locations_size +=
1696 sizeof (source_location);
1700 macro_maps_used_size =
1701 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1703 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1704 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1705 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1706 s->ordinary_maps_used_size = ordinary_maps_used_size;
1707 s->num_expanded_macros = num_expanded_macros_counter;
1708 s->num_macro_tokens = num_macro_tokens_counter;
1709 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1710 s->macro_maps_allocated_size = macro_maps_allocated_size;
1711 s->macro_maps_locations_size = macro_maps_locations_size;
1712 s->macro_maps_used_size = macro_maps_used_size;
1713 s->duplicated_macro_maps_locations_size =
1714 duplicated_macro_maps_locations_size;
1715 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1716 * sizeof (struct location_adhoc_data));
1717 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
1721 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1722 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1723 specifies how many macro maps to dump. */
1725 void
1726 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1727 unsigned int num_macro)
1729 unsigned int i;
1731 if (set == NULL)
1732 return;
1734 if (stream == NULL)
1735 stream = stderr;
1737 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1738 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1739 fprintf (stream, "Include stack depth: %d\n", set->depth);
1740 fprintf (stream, "Highest location: %u\n", set->highest_location);
1742 if (num_ordinary)
1744 fprintf (stream, "\nOrdinary line maps\n");
1745 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1746 linemap_dump (stream, set, i, false);
1747 fprintf (stream, "\n");
1750 if (num_macro)
1752 fprintf (stream, "\nMacro line maps\n");
1753 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1754 linemap_dump (stream, set, i, true);
1755 fprintf (stream, "\n");
1759 /* class rich_location. */
1761 /* Construct a rich_location with location LOC as its initial range. */
1763 rich_location::rich_location (source_location loc) :
1764 m_loc (loc),
1765 m_num_ranges (0),
1766 m_have_expanded_location (false)
1768 /* Set up the 0th range: */
1769 add_range (loc, loc, true);
1770 m_ranges[0].m_caret = lazily_expand_location ();
1773 /* Construct a rich_location with source_range SRC_RANGE as its
1774 initial range. */
1776 rich_location::rich_location (source_range src_range)
1777 : m_loc (src_range.m_start),
1778 m_num_ranges (0),
1779 m_have_expanded_location (false)
1781 /* Set up the 0th range: */
1782 add_range (src_range, true);
1785 /* Get an expanded_location for this rich_location's primary
1786 location. */
1788 expanded_location
1789 rich_location::lazily_expand_location ()
1791 if (!m_have_expanded_location)
1793 m_expanded_location
1794 = linemap_client_expand_location_to_spelling_point (m_loc);
1795 m_have_expanded_location = true;
1798 return m_expanded_location;
1801 /* Set the column of the primary location. */
1803 void
1804 rich_location::override_column (int column)
1806 lazily_expand_location ();
1807 m_expanded_location.column = column;
1810 /* Add the given range. */
1812 void
1813 rich_location::add_range (source_location start, source_location finish,
1814 bool show_caret_p)
1816 linemap_assert (m_num_ranges < MAX_RANGES);
1818 location_range *range = &m_ranges[m_num_ranges++];
1819 range->m_start = linemap_client_expand_location_to_spelling_point (start);
1820 range->m_finish = linemap_client_expand_location_to_spelling_point (finish);
1821 range->m_caret = range->m_start;
1822 range->m_show_caret_p = show_caret_p;
1825 /* Add the given range. */
1827 void
1828 rich_location::add_range (source_range src_range, bool show_caret_p)
1830 linemap_assert (m_num_ranges < MAX_RANGES);
1832 add_range (src_range.m_start, src_range.m_finish, show_caret_p);
1835 void
1836 rich_location::add_range (location_range *src_range)
1838 linemap_assert (m_num_ranges < MAX_RANGES);
1840 m_ranges[m_num_ranges++] = *src_range;
1843 /* Add or overwrite the range given by IDX. It must either
1844 overwrite an existing range, or add one *exactly* on the end of
1845 the array.
1847 This is primarily for use by gcc when implementing diagnostic
1848 format decoders e.g. the "+" in the C/C++ frontends, for handling
1849 format codes like "%q+D" (which writes the source location of a
1850 tree back into range 0 of the rich_location).
1852 If SHOW_CARET_P is true, then the range should be rendered with
1853 a caret at its starting location. This
1854 is for use by the Fortran frontend, for implementing the
1855 "%C" and "%L" format codes. */
1857 void
1858 rich_location::set_range (unsigned int idx, source_range src_range,
1859 bool show_caret_p, bool overwrite_loc_p)
1861 linemap_assert (idx < MAX_RANGES);
1863 /* We can either overwrite an existing range, or add one exactly
1864 on the end of the array. */
1865 linemap_assert (idx <= m_num_ranges);
1867 location_range *locrange = &m_ranges[idx];
1868 locrange->m_start
1869 = linemap_client_expand_location_to_spelling_point (src_range.m_start);
1870 locrange->m_finish
1871 = linemap_client_expand_location_to_spelling_point (src_range.m_finish);
1873 locrange->m_show_caret_p = show_caret_p;
1874 if (overwrite_loc_p)
1875 locrange->m_caret = locrange->m_start;
1877 /* Are we adding a range onto the end? */
1878 if (idx == m_num_ranges)
1879 m_num_ranges = idx + 1;
1881 if (idx == 0 && overwrite_loc_p)
1883 m_loc = src_range.m_start;
1884 /* Mark any cached value here as dirty. */
1885 m_have_expanded_location = false;