gcc/
[official-gcc.git] / libcpp / line-map.c
blobd58cad23fdbf440273ed346bd5ef7d56d70d52ef
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.
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))
695 return loc;
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))
701 if ((loc + offset >= MAP_START_LOCATION (&map[1])))
702 return loc;
704 offset += SOURCE_COLUMN (map, loc);
705 if (linemap_assert_fails
706 (offset < (1u << map->column_bits)))
707 return loc;
709 source_location r =
710 linemap_position_for_line_and_column (map,
711 SOURCE_LINE (map, loc),
712 offset);
713 if (linemap_assert_fails (r <= set->highest_location)
714 || linemap_assert_fails (map == linemap_lookup (set, r)))
715 return loc;
717 return r;
720 /* Given a virtual source location yielded by a map (either an
721 ordinary or a macro map), returns that map. */
723 const struct line_map*
724 linemap_lookup (struct line_maps *set, source_location line)
726 if (IS_ADHOC_LOC (line))
727 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
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);
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. */
738 static const line_map_ordinary *
739 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
741 unsigned int md, mn, mx;
742 const line_map_ordinary *cached, *result;
744 if (IS_ADHOC_LOC (line))
745 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
747 if (set == NULL || line < RESERVED_LOCATION_COUNT)
748 return NULL;
750 mn = LINEMAPS_ORDINARY_CACHE (set);
751 mx = LINEMAPS_ORDINARY_USED (set);
753 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
754 /* We should get a segfault if no line_maps have been added yet. */
755 if (line >= MAP_START_LOCATION (cached))
757 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
758 return cached;
760 else
762 mx = mn;
763 mn = 0;
766 while (mx - mn > 1)
768 md = (mn + mx) / 2;
769 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
770 mx = md;
771 else
772 mn = md;
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;
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. */
786 static const line_map_macro *
787 linemap_macro_map_lookup (struct line_maps *set, source_location line)
789 unsigned int md, mn, mx;
790 const struct line_map_macro *cached, *result;
792 if (IS_ADHOC_LOC (line))
793 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
795 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
797 if (set == NULL)
798 return NULL;
800 mn = LINEMAPS_MACRO_CACHE (set);
801 mx = LINEMAPS_MACRO_USED (set);
802 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
804 if (line >= MAP_START_LOCATION (cached))
806 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
807 return cached;
808 mx = mn - 1;
809 mn = 0;
812 while (mn < mx)
814 md = (mx + mn) / 2;
815 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
816 mn = md + 1;
817 else
818 mx = md;
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);
825 return result;
828 /* Return TRUE if MAP encodes locations coming from a macro
829 replacement-list at macro expansion point. */
831 bool
832 linemap_macro_expansion_map_p (const struct line_map *map)
834 if (!map)
835 return false;
836 return (map->reason == LC_ENTER_MACRO);
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.
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. */
845 static source_location
846 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
847 source_location location ATTRIBUTE_UNUSED)
849 linemap_assert (linemap_macro_expansion_map_p (map)
850 && location >= MAP_START_LOCATION (map));
852 /* Make sure LOCATION is correct. */
853 linemap_assert ((location - MAP_START_LOCATION (map))
854 < MACRO_MAP_NUM_MACRO_TOKENS (map));
856 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
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.
862 Return the location of the token at the definition point of the
863 macro. */
865 static source_location
866 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
867 source_location location)
869 unsigned token_no;
871 linemap_assert (linemap_macro_expansion_map_p (map)
872 && location >= MAP_START_LOCATION (map));
873 linemap_assert (location >= RESERVED_LOCATION_COUNT);
875 token_no = location - MAP_START_LOCATION (map);
876 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
878 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
880 return location;
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.
887 In other words, this returns the xI location presented in the
888 comments of line_map_macro above. */
889 source_location
890 linemap_macro_map_loc_unwind_toward_spelling (const line_map_macro* map,
891 source_location location)
893 unsigned token_no;
895 linemap_assert (linemap_macro_expansion_map_p (map)
896 && location >= MAP_START_LOCATION (map));
897 linemap_assert (location >= RESERVED_LOCATION_COUNT);
899 token_no = location - MAP_START_LOCATION (map);
900 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
902 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
904 return location;
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. */
914 linemap_get_expansion_line (struct line_maps *set,
915 source_location location)
917 const line_map_ordinary *map = NULL;
919 if (IS_ADHOC_LOC (location))
920 location = set->location_adhoc_data_map.data[location
921 & MAX_SOURCE_LOCATION].locus;
923 if (location < RESERVED_LOCATION_COUNT)
924 return 0;
926 location =
927 linemap_macro_loc_to_exp_point (set, location, &map);
929 return SOURCE_LINE (map, location);
932 /* Return the path of the file corresponding to source code location
933 LOCATION.
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.
939 SET is the line map set LOCATION comes from. */
941 const char*
942 linemap_get_expansion_filename (struct line_maps *set,
943 source_location location)
945 const struct line_map_ordinary *map = NULL;
947 if (IS_ADHOC_LOC (location))
948 location = set->location_adhoc_data_map.data[location
949 & MAX_SOURCE_LOCATION].locus;
951 if (location < RESERVED_LOCATION_COUNT)
952 return NULL;
954 location =
955 linemap_macro_loc_to_exp_point (set, location, &map);
957 return LINEMAP_FILE (map);
960 /* Return the name of the macro associated to MACRO_MAP. */
962 const char*
963 linemap_map_get_macro_name (const line_map_macro *macro_map)
965 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
966 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
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++.
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. */
980 linemap_location_in_system_header_p (struct line_maps *set,
981 source_location location)
983 const struct line_map *map = NULL;
985 if (IS_ADHOC_LOC (location))
986 location = set->location_adhoc_data_map.data[location
987 & MAX_SOURCE_LOCATION].locus;
989 if (location < RESERVED_LOCATION_COUNT)
990 return false;
992 /* Let's look at where the token for LOCATION comes from. */
993 while (true)
995 map = linemap_lookup (set, location);
996 if (map != NULL)
998 if (!linemap_macro_expansion_map_p (map))
999 /* It's a normal token. */
1000 return LINEMAP_SYSP (linemap_check_ordinary (map));
1001 else
1003 const line_map_macro *macro_map = linemap_check_macro (map);
1005 /* It's a token resulting from a macro expansion. */
1006 source_location loc =
1007 linemap_macro_map_loc_unwind_toward_spelling (macro_map, location);
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. */
1011 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1012 else
1013 location = loc;
1016 else
1017 break;
1019 return false;
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. */
1026 bool
1027 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1028 source_location location)
1030 if (IS_ADHOC_LOC (location))
1031 location = set->location_adhoc_data_map.data[location
1032 & MAX_SOURCE_LOCATION].locus;
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);
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. */
1047 static const struct line_map*
1048 first_map_in_common_1 (struct line_maps *set,
1049 source_location *loc0,
1050 source_location *loc1)
1052 source_location l0 = *loc0, l1 = *loc1;
1053 const struct line_map *map0 = linemap_lookup (set, l0),
1054 *map1 = linemap_lookup (set, l1);
1056 while (linemap_macro_expansion_map_p (map0)
1057 && linemap_macro_expansion_map_p (map1)
1058 && (map0 != map1))
1060 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1062 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1063 l0);
1064 map0 = linemap_lookup (set, l0);
1066 else
1068 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1069 l1);
1070 map1 = linemap_lookup (set, l1);
1074 if (map0 == map1)
1076 *loc0 = l0;
1077 *loc1 = l1;
1078 return map0;
1080 return NULL;
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. */
1089 static const struct line_map*
1090 first_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)
1096 *res_loc0 = loc0;
1097 *res_loc1 = loc1;
1099 return first_map_in_common_1 (set, res_loc0, res_loc1);
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. */
1108 linemap_compare_locations (struct line_maps *set,
1109 source_location pre,
1110 source_location post)
1112 bool pre_virtual_p, post_virtual_p;
1113 source_location l0 = pre, l1 = post;
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;
1120 if (l0 == l1)
1121 return 0;
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);
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);
1133 if (l0 == l1
1134 && pre_virtual_p
1135 && post_virtual_p)
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);
1144 if (map == NULL)
1145 /* This should not be possible. */
1146 abort ();
1148 i0 = l0 - MAP_START_LOCATION (map);
1149 i1 = l1 - MAP_START_LOCATION (map);
1150 return i1 - i0;
1153 return l1 - l0;
1156 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1158 static void
1159 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1161 unsigned int i = set->depth;
1163 while (--i)
1164 putc ('.', stderr);
1166 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1169 /* Return the spelling location of the token wherever it comes from,
1170 whether part of a macro definition or not.
1172 This is a subroutine for linemap_resolve_location. */
1174 static source_location
1175 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1176 source_location location,
1177 const line_map_ordinary **original_map)
1179 struct line_map *map;
1181 if (IS_ADHOC_LOC (location))
1182 location = set->location_adhoc_data_map.data[location
1183 & MAX_SOURCE_LOCATION].locus;
1185 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1187 while (true)
1189 map = const_cast <line_map *> (linemap_lookup (set, location));
1190 if (!linemap_macro_expansion_map_p (map))
1191 break;
1193 location
1194 = linemap_macro_map_loc_unwind_toward_spelling
1195 (linemap_check_macro (map),
1196 location);
1199 if (original_map)
1200 *original_map = linemap_check_ordinary (map);
1201 return location;
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.
1212 This is a subroutine of linemap_resolve_location. */
1214 static source_location
1215 linemap_macro_loc_to_def_point (struct line_maps *set,
1216 source_location location,
1217 const line_map_ordinary **original_map)
1219 struct line_map *map;
1221 if (IS_ADHOC_LOC (location))
1222 location = set->location_adhoc_data_map.data[location
1223 & MAX_SOURCE_LOCATION].locus;
1225 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1227 while (true)
1229 map = const_cast <line_map *> (linemap_lookup (set, location));
1230 if (!linemap_macro_expansion_map_p (map))
1231 break;
1233 location =
1234 linemap_macro_map_loc_to_def_point (linemap_check_macro (map),
1235 location);
1238 if (original_map)
1239 *original_map = linemap_check_ordinary (map);
1240 return location;
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.
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.
1255 This is a subroutine of linemap_resolve_location. */
1257 static source_location
1258 linemap_macro_loc_to_exp_point (struct line_maps *set,
1259 source_location location,
1260 const line_map_ordinary **original_map)
1262 struct line_map *map;
1264 if (IS_ADHOC_LOC (location))
1265 location = set->location_adhoc_data_map.data[location
1266 & MAX_SOURCE_LOCATION].locus;
1268 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1270 while (true)
1272 map = const_cast <line_map *> (linemap_lookup (set, location));
1273 if (!linemap_macro_expansion_map_p (map))
1274 break;
1275 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1276 location);
1279 if (original_map)
1280 *original_map = linemap_check_ordinary (map);
1281 return location;
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.
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.
1294 Now if LOC *IS* the location of a token resulting from the
1295 expansion of a macro, this is what happens.
1297 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1298 -------------------------------
1300 The virtual location is resolved to the first macro expansion point
1301 that led to this macro expansion.
1303 * If LRK is set to LRK_SPELLING_LOCATION
1304 -------------------------------------
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.
1310 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1311 --------------------------------------
1313 The virtual location is resolved to the locus of the token in the
1314 context of the macro definition.
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.
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.
1326 If MAP is not NULL, *MAP is set to the map encoding the
1327 returned location. Note that if the returned location wasn't originally
1328 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1329 resolves to a location reserved for the client code, like
1330 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1332 source_location
1333 linemap_resolve_location (struct line_maps *set,
1334 source_location loc,
1335 enum location_resolution_kind lrk,
1336 const line_map_ordinary **map)
1338 if (IS_ADHOC_LOC (loc))
1339 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1341 if (loc < RESERVED_LOCATION_COUNT)
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;
1351 switch (lrk)
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 ();
1365 return loc;
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.
1377 *LOC_MAP must be set to the map of LOC. This function then sets it
1378 to the map of the returned location. */
1380 source_location
1381 linemap_unwind_toward_expansion (struct line_maps *set,
1382 source_location loc,
1383 const struct line_map **map)
1385 source_location resolved_location;
1386 const line_map_macro *macro_map = linemap_check_macro (*map);
1387 const struct line_map *resolved_map;
1389 if (IS_ADHOC_LOC (loc))
1390 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1392 resolved_location =
1393 linemap_macro_map_loc_unwind_toward_spelling (macro_map, loc);
1394 resolved_map = linemap_lookup (set, resolved_location);
1396 if (!linemap_macro_expansion_map_p (resolved_map))
1398 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1399 resolved_map = linemap_lookup (set, resolved_location);
1402 *map = resolved_map;
1403 return resolved_location;
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
1410 is not reserved and is not in a system header is reached. In other
1411 words, this unwinds the reserved location until a location that is
1412 in real source code is reached.
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.
1418 *MAP is set to the map of the returned location if the later is
1419 different from LOC. */
1420 source_location
1421 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1422 source_location loc,
1423 const struct line_map **map)
1425 source_location resolved_loc;
1426 const struct line_map *map0 = NULL;
1427 const line_map_ordinary *map1 = NULL;
1429 if (IS_ADHOC_LOC (loc))
1430 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1432 map0 = linemap_lookup (set, loc);
1433 if (!linemap_macro_expansion_map_p (map0))
1434 return loc;
1436 resolved_loc = linemap_resolve_location (set, loc,
1437 LRK_SPELLING_LOCATION,
1438 &map1);
1440 if (resolved_loc >= RESERVED_LOCATION_COUNT
1441 && !LINEMAP_SYSP (map1))
1442 return loc;
1444 while (linemap_macro_expansion_map_p (map0)
1445 && (resolved_loc < RESERVED_LOCATION_COUNT
1446 || LINEMAP_SYSP (map1)))
1448 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1449 resolved_loc = linemap_resolve_location (set, loc,
1450 LRK_SPELLING_LOCATION,
1451 &map1);
1454 if (map != NULL)
1455 *map = map0;
1456 return loc;
1459 /* Expand source code location LOC and return a user readable source
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. */
1464 expanded_location
1465 linemap_expand_location (struct line_maps *set,
1466 const struct line_map *map,
1467 source_location loc)
1470 expanded_location xloc;
1472 memset (&xloc, 0, sizeof (xloc));
1473 if (IS_ADHOC_LOC (loc))
1475 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1476 xloc.data
1477 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
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
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 ();
1498 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
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;
1506 return xloc;
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. */
1514 void
1515 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
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;
1521 const line_map *map;
1523 if (stream == NULL)
1524 stream = stderr;
1526 if (!is_macro)
1527 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1528 else
1529 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1531 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1533 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1534 ix, (void *) map, map->start_location, reason,
1535 ((!is_macro
1536 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1537 ? "yes" : "no"));
1538 if (!is_macro)
1540 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1541 unsigned includer_ix;
1542 const line_map_ordinary *includer_map;
1544 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
1545 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1546 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1547 : NULL;
1549 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1550 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1551 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1552 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1554 else
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));
1562 fprintf (stream, "\n");
1566 /* Dump debugging information about source location LOC into the file
1567 stream STREAM. SET is the line map set LOC comes from. */
1569 void
1570 linemap_dump_location (struct line_maps *set,
1571 source_location loc,
1572 FILE *stream)
1574 const line_map_ordinary *map;
1575 source_location location;
1576 const char *path = "", *from = "";
1577 int l = -1, c = -1, s = -1, e = -1;
1579 if (IS_ADHOC_LOC (loc))
1580 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1582 if (loc == 0)
1583 return;
1585 location =
1586 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1588 if (map == NULL)
1589 /* Only reserved locations can be tolerated in this case. */
1590 linemap_assert (location < RESERVED_LOCATION_COUNT);
1591 else
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>";
1606 /* P: path, L: line, C: column, S: in-system-header, M: map address,
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);
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. */
1617 bool
1618 linemap_get_file_highest_location (struct line_maps *set,
1619 const char *file_name,
1620 source_location *loc)
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;
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)
1631 const char *fname = set->info_ordinary.maps[i].to_file;
1632 if (fname && !filename_cmp (fname, file_name))
1633 break;
1636 if (i < 0)
1637 return false;
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;
1648 *loc = result;
1649 return true;
1652 /* Compute and return statistics about the memory consumption of some
1653 parts of the line table SET. */
1655 void
1656 linemap_get_statistics (struct line_maps *set,
1657 struct linemap_stats *s)
1659 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1660 macro_maps_allocated_size, macro_maps_used_size,
1661 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1663 const line_map_macro *cur_map;
1665 ordinary_maps_allocated_size =
1666 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1668 ordinary_maps_used_size =
1669 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1671 macro_maps_allocated_size =
1672 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1674 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1675 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1676 ++cur_map)
1678 unsigned i;
1680 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1682 macro_maps_locations_size +=
1683 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1685 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
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);
1694 macro_maps_used_size =
1695 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
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;
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. */
1716 void
1717 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1718 unsigned int num_macro)
1720 unsigned int i;
1722 if (set == NULL)
1723 return;
1725 if (stream == NULL)
1726 stream = stderr;
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);
1733 if (num_ordinary)
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");
1741 if (num_macro)
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");