pa.c (pa_print_operand): Use HOST_WIDE_INT_PRINT_DEC instead of "%d" for 'o' operand.
[official-gcc.git] / libcpp / line-map.c
blob972f66cb90bc3c4d1c8f3cd45f9ee16e724b80ce
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 if (linemap_assert_fails (MAP_START_LOCATION (map) < loc + offset))
693 return loc;
695 /* If MAP is not the last line map of its set, then the new location
696 (loc + offset) should be less than the first location encoded by
697 the next line map of the set. */
698 if (map != LINEMAPS_LAST_ORDINARY_MAP (set))
699 if (linemap_assert_fails (loc + offset < MAP_START_LOCATION (&map[1])))
700 return loc;
702 offset += SOURCE_COLUMN (map, loc);
703 if (linemap_assert_fails
704 (offset < (1u << map->column_bits)))
705 return loc;
707 source_location r =
708 linemap_position_for_line_and_column (map,
709 SOURCE_LINE (map, loc),
710 offset);
711 if (linemap_assert_fails (r <= set->highest_location)
712 || linemap_assert_fails (map == linemap_lookup (set, r)))
713 return loc;
715 return r;
718 /* Given a virtual source location yielded by a map (either an
719 ordinary or a macro map), returns that map. */
721 const struct line_map*
722 linemap_lookup (struct line_maps *set, source_location line)
724 if (IS_ADHOC_LOC (line))
725 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
726 if (linemap_location_from_macro_expansion_p (set, line))
727 return linemap_macro_map_lookup (set, line);
728 return linemap_ordinary_map_lookup (set, line);
731 /* Given a source location yielded by an ordinary map, returns that
732 map. Since the set is built chronologically, the logical lines are
733 monotonic increasing, and so the list is sorted and we can use a
734 binary search. */
736 static const line_map_ordinary *
737 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
739 unsigned int md, mn, mx;
740 const line_map_ordinary *cached, *result;
742 if (IS_ADHOC_LOC (line))
743 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
745 if (set == NULL || line < RESERVED_LOCATION_COUNT)
746 return NULL;
748 mn = LINEMAPS_ORDINARY_CACHE (set);
749 mx = LINEMAPS_ORDINARY_USED (set);
751 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
752 /* We should get a segfault if no line_maps have been added yet. */
753 if (line >= MAP_START_LOCATION (cached))
755 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
756 return cached;
758 else
760 mx = mn;
761 mn = 0;
764 while (mx - mn > 1)
766 md = (mn + mx) / 2;
767 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
768 mx = md;
769 else
770 mn = md;
773 LINEMAPS_ORDINARY_CACHE (set) = mn;
774 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
775 linemap_assert (line >= MAP_START_LOCATION (result));
776 return result;
779 /* Given a source location yielded by a macro map, returns that map.
780 Since the set is built chronologically, the logical lines are
781 monotonic decreasing, and so the list is sorted and we can use a
782 binary search. */
784 static const line_map_macro *
785 linemap_macro_map_lookup (struct line_maps *set, source_location line)
787 unsigned int md, mn, mx;
788 const struct line_map_macro *cached, *result;
790 if (IS_ADHOC_LOC (line))
791 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
793 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
795 if (set == NULL)
796 return NULL;
798 mn = LINEMAPS_MACRO_CACHE (set);
799 mx = LINEMAPS_MACRO_USED (set);
800 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
802 if (line >= MAP_START_LOCATION (cached))
804 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
805 return cached;
806 mx = mn - 1;
807 mn = 0;
810 while (mn < mx)
812 md = (mx + mn) / 2;
813 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
814 mn = md + 1;
815 else
816 mx = md;
819 LINEMAPS_MACRO_CACHE (set) = mx;
820 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
821 linemap_assert (MAP_START_LOCATION (result) <= line);
823 return result;
826 /* Return TRUE if MAP encodes locations coming from a macro
827 replacement-list at macro expansion point. */
829 bool
830 linemap_macro_expansion_map_p (const struct line_map *map)
832 if (!map)
833 return false;
834 return (map->reason == LC_ENTER_MACRO);
837 /* If LOCATION is the locus of a token in a replacement-list of a
838 macro expansion return the location of the macro expansion point.
840 Read the comments of struct line_map and struct line_map_macro in
841 line-map.h to understand what a macro expansion point is. */
843 static source_location
844 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
845 source_location location ATTRIBUTE_UNUSED)
847 linemap_assert (linemap_macro_expansion_map_p (map)
848 && location >= MAP_START_LOCATION (map));
850 /* Make sure LOCATION is correct. */
851 linemap_assert ((location - MAP_START_LOCATION (map))
852 < MACRO_MAP_NUM_MACRO_TOKENS (map));
854 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
857 /* LOCATION is the source location of a token that belongs to a macro
858 replacement-list as part of the macro expansion denoted by MAP.
860 Return the location of the token at the definition point of the
861 macro. */
863 static source_location
864 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
865 source_location location)
867 unsigned token_no;
869 linemap_assert (linemap_macro_expansion_map_p (map)
870 && location >= MAP_START_LOCATION (map));
871 linemap_assert (location >= RESERVED_LOCATION_COUNT);
873 token_no = location - MAP_START_LOCATION (map);
874 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
876 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
878 return location;
881 /* If LOCATION is the locus of a token that is an argument of a
882 function-like macro M and appears in the expansion of M, return the
883 locus of that argument in the context of the caller of M.
885 In other words, this returns the xI location presented in the
886 comments of line_map_macro above. */
887 source_location
888 linemap_macro_map_loc_unwind_toward_spelling (const line_map_macro* map,
889 source_location location)
891 unsigned token_no;
893 linemap_assert (linemap_macro_expansion_map_p (map)
894 && location >= MAP_START_LOCATION (map));
895 linemap_assert (location >= RESERVED_LOCATION_COUNT);
897 token_no = location - MAP_START_LOCATION (map);
898 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
900 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
902 return location;
905 /* Return the source line number corresponding to source location
906 LOCATION. SET is the line map set LOCATION comes from. If
907 LOCATION is the source location of token that is part of the
908 replacement-list of a macro expansion return the line number of the
909 macro expansion point. */
912 linemap_get_expansion_line (struct line_maps *set,
913 source_location location)
915 const line_map_ordinary *map = NULL;
917 if (IS_ADHOC_LOC (location))
918 location = set->location_adhoc_data_map.data[location
919 & MAX_SOURCE_LOCATION].locus;
921 if (location < RESERVED_LOCATION_COUNT)
922 return 0;
924 location =
925 linemap_macro_loc_to_exp_point (set, location, &map);
927 return SOURCE_LINE (map, location);
930 /* Return the path of the file corresponding to source code location
931 LOCATION.
933 If LOCATION is the source location of token that is part of the
934 replacement-list of a macro expansion return the file path of the
935 macro expansion point.
937 SET is the line map set LOCATION comes from. */
939 const char*
940 linemap_get_expansion_filename (struct line_maps *set,
941 source_location location)
943 const struct line_map_ordinary *map = NULL;
945 if (IS_ADHOC_LOC (location))
946 location = set->location_adhoc_data_map.data[location
947 & MAX_SOURCE_LOCATION].locus;
949 if (location < RESERVED_LOCATION_COUNT)
950 return NULL;
952 location =
953 linemap_macro_loc_to_exp_point (set, location, &map);
955 return LINEMAP_FILE (map);
958 /* Return the name of the macro associated to MACRO_MAP. */
960 const char*
961 linemap_map_get_macro_name (const line_map_macro *macro_map)
963 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
964 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
967 /* Return a positive value if LOCATION is the locus of a token that is
968 located in a system header, O otherwise. It returns 1 if LOCATION
969 is the locus of a token that is located in a system header, and 2
970 if LOCATION is the locus of a token located in a C system header
971 that therefore needs to be extern "C" protected in C++.
973 Note that this function returns 1 if LOCATION belongs to a token
974 that is part of a macro replacement-list defined in a system
975 header, but expanded in a non-system file. */
978 linemap_location_in_system_header_p (struct line_maps *set,
979 source_location location)
981 const struct line_map *map = NULL;
983 if (IS_ADHOC_LOC (location))
984 location = set->location_adhoc_data_map.data[location
985 & MAX_SOURCE_LOCATION].locus;
987 if (location < RESERVED_LOCATION_COUNT)
988 return false;
990 /* Let's look at where the token for LOCATION comes from. */
991 while (true)
993 map = linemap_lookup (set, location);
994 if (map != NULL)
996 if (!linemap_macro_expansion_map_p (map))
997 /* It's a normal token. */
998 return LINEMAP_SYSP (linemap_check_ordinary (map));
999 else
1001 const line_map_macro *macro_map = linemap_check_macro (map);
1003 /* It's a token resulting from a macro expansion. */
1004 source_location loc =
1005 linemap_macro_map_loc_unwind_toward_spelling (macro_map, location);
1006 if (loc < RESERVED_LOCATION_COUNT)
1007 /* This token might come from a built-in macro. Let's
1008 look at where that macro got expanded. */
1009 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1010 else
1011 location = loc;
1014 else
1015 break;
1017 return false;
1020 /* Return TRUE if LOCATION is a source code location of a token coming
1021 from a macro replacement-list at a macro expansion point, FALSE
1022 otherwise. */
1024 bool
1025 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1026 source_location location)
1028 if (IS_ADHOC_LOC (location))
1029 location = set->location_adhoc_data_map.data[location
1030 & MAX_SOURCE_LOCATION].locus;
1032 linemap_assert (location <= MAX_SOURCE_LOCATION
1033 && (set->highest_location
1034 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
1035 if (set == NULL)
1036 return false;
1037 return (location > set->highest_location);
1040 /* Given two virtual locations *LOC0 and *LOC1, return the first
1041 common macro map in their macro expansion histories. Return NULL
1042 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1043 virtual location of the token inside the resulting macro. */
1045 static const struct line_map*
1046 first_map_in_common_1 (struct line_maps *set,
1047 source_location *loc0,
1048 source_location *loc1)
1050 source_location l0 = *loc0, l1 = *loc1;
1051 const struct line_map *map0 = linemap_lookup (set, l0),
1052 *map1 = linemap_lookup (set, l1);
1054 while (linemap_macro_expansion_map_p (map0)
1055 && linemap_macro_expansion_map_p (map1)
1056 && (map0 != map1))
1058 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1060 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1061 l0);
1062 map0 = linemap_lookup (set, l0);
1064 else
1066 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1067 l1);
1068 map1 = linemap_lookup (set, l1);
1072 if (map0 == map1)
1074 *loc0 = l0;
1075 *loc1 = l1;
1076 return map0;
1078 return NULL;
1081 /* Given two virtual locations LOC0 and LOC1, return the first common
1082 macro map in their macro expansion histories. Return NULL if no
1083 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1084 virtual location of the token inside the resulting macro, upon
1085 return of a non-NULL result. */
1087 static const struct line_map*
1088 first_map_in_common (struct line_maps *set,
1089 source_location loc0,
1090 source_location loc1,
1091 source_location *res_loc0,
1092 source_location *res_loc1)
1094 *res_loc0 = loc0;
1095 *res_loc1 = loc1;
1097 return first_map_in_common_1 (set, res_loc0, res_loc1);
1100 /* Return a positive value if PRE denotes the location of a token that
1101 comes before the token of POST, 0 if PRE denotes the location of
1102 the same token as the token for POST, and a negative value
1103 otherwise. */
1106 linemap_compare_locations (struct line_maps *set,
1107 source_location pre,
1108 source_location post)
1110 bool pre_virtual_p, post_virtual_p;
1111 source_location l0 = pre, l1 = post;
1113 if (IS_ADHOC_LOC (l0))
1114 l0 = set->location_adhoc_data_map.data[l0 & MAX_SOURCE_LOCATION].locus;
1115 if (IS_ADHOC_LOC (l1))
1116 l1 = set->location_adhoc_data_map.data[l1 & MAX_SOURCE_LOCATION].locus;
1118 if (l0 == l1)
1119 return 0;
1121 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1122 l0 = linemap_resolve_location (set, l0,
1123 LRK_MACRO_EXPANSION_POINT,
1124 NULL);
1126 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1127 l1 = linemap_resolve_location (set, l1,
1128 LRK_MACRO_EXPANSION_POINT,
1129 NULL);
1131 if (l0 == l1
1132 && pre_virtual_p
1133 && post_virtual_p)
1135 /* So pre and post represent two tokens that are present in a
1136 same macro expansion. Let's see if the token for pre was
1137 before the token for post in that expansion. */
1138 unsigned i0, i1;
1139 const struct line_map *map =
1140 first_map_in_common (set, pre, post, &l0, &l1);
1142 if (map == NULL)
1143 /* This should not be possible. */
1144 abort ();
1146 i0 = l0 - MAP_START_LOCATION (map);
1147 i1 = l1 - MAP_START_LOCATION (map);
1148 return i1 - i0;
1151 return l1 - l0;
1154 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1156 static void
1157 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1159 unsigned int i = set->depth;
1161 while (--i)
1162 putc ('.', stderr);
1164 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1167 /* Return the spelling location of the token wherever it comes from,
1168 whether part of a macro definition or not.
1170 This is a subroutine for linemap_resolve_location. */
1172 static source_location
1173 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1174 source_location location,
1175 const line_map_ordinary **original_map)
1177 struct line_map *map;
1179 if (IS_ADHOC_LOC (location))
1180 location = set->location_adhoc_data_map.data[location
1181 & MAX_SOURCE_LOCATION].locus;
1183 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1185 while (true)
1187 map = const_cast <line_map *> (linemap_lookup (set, location));
1188 if (!linemap_macro_expansion_map_p (map))
1189 break;
1191 location
1192 = linemap_macro_map_loc_unwind_toward_spelling
1193 (linemap_check_macro (map),
1194 location);
1197 if (original_map)
1198 *original_map = linemap_check_ordinary (map);
1199 return location;
1202 /* If LOCATION is the source location of a token that belongs to a
1203 macro replacement-list -- as part of a macro expansion -- then
1204 return the location of the token at the definition point of the
1205 macro. Otherwise, return LOCATION. SET is the set of maps
1206 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1207 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1208 returned location comes from.
1210 This is a subroutine of linemap_resolve_location. */
1212 static source_location
1213 linemap_macro_loc_to_def_point (struct line_maps *set,
1214 source_location location,
1215 const line_map_ordinary **original_map)
1217 struct line_map *map;
1219 if (IS_ADHOC_LOC (location))
1220 location = set->location_adhoc_data_map.data[location
1221 & MAX_SOURCE_LOCATION].locus;
1223 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1225 while (true)
1227 map = const_cast <line_map *> (linemap_lookup (set, location));
1228 if (!linemap_macro_expansion_map_p (map))
1229 break;
1231 location =
1232 linemap_macro_map_loc_to_def_point (linemap_check_macro (map),
1233 location);
1236 if (original_map)
1237 *original_map = linemap_check_ordinary (map);
1238 return location;
1241 /* If LOCATION is the source location of a token that belongs to a
1242 macro replacement-list -- at a macro expansion point -- then return
1243 the location of the topmost expansion point of the macro. We say
1244 topmost because if we are in the context of a nested macro
1245 expansion, the function returns the source location of the first
1246 macro expansion that triggered the nested expansions.
1248 Otherwise, return LOCATION. SET is the set of maps location come
1249 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1250 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1251 location comes from.
1253 This is a subroutine of linemap_resolve_location. */
1255 static source_location
1256 linemap_macro_loc_to_exp_point (struct line_maps *set,
1257 source_location location,
1258 const line_map_ordinary **original_map)
1260 struct line_map *map;
1262 if (IS_ADHOC_LOC (location))
1263 location = set->location_adhoc_data_map.data[location
1264 & MAX_SOURCE_LOCATION].locus;
1266 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1268 while (true)
1270 map = const_cast <line_map *> (linemap_lookup (set, location));
1271 if (!linemap_macro_expansion_map_p (map))
1272 break;
1273 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1274 location);
1277 if (original_map)
1278 *original_map = linemap_check_ordinary (map);
1279 return location;
1282 /* Resolve a virtual location into either a spelling location, an
1283 expansion point location or a token argument replacement point
1284 location. Return the map that encodes the virtual location as well
1285 as the resolved location.
1287 If LOC is *NOT* the location of a token resulting from the
1288 expansion of a macro, then the parameter LRK (which stands for
1289 Location Resolution Kind) is ignored and the resulting location
1290 just equals the one given in argument.
1292 Now if LOC *IS* the location of a token resulting from the
1293 expansion of a macro, this is what happens.
1295 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1296 -------------------------------
1298 The virtual location is resolved to the first macro expansion point
1299 that led to this macro expansion.
1301 * If LRK is set to LRK_SPELLING_LOCATION
1302 -------------------------------------
1304 The virtual location is resolved to the locus where the token has
1305 been spelled in the source. This can follow through all the macro
1306 expansions that led to the token.
1308 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1309 --------------------------------------
1311 The virtual location is resolved to the locus of the token in the
1312 context of the macro definition.
1314 If LOC is the locus of a token that is an argument of a
1315 function-like macro [replacing a parameter in the replacement list
1316 of the macro] the virtual location is resolved to the locus of the
1317 parameter that is replaced, in the context of the definition of the
1318 macro.
1320 If LOC is the locus of a token that is not an argument of a
1321 function-like macro, then the function behaves as if LRK was set to
1322 LRK_SPELLING_LOCATION.
1324 If MAP is not NULL, *MAP is set to the map encoding the
1325 returned location. Note that if the returned location wasn't originally
1326 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1327 resolves to a location reserved for the client code, like
1328 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1330 source_location
1331 linemap_resolve_location (struct line_maps *set,
1332 source_location loc,
1333 enum location_resolution_kind lrk,
1334 const line_map_ordinary **map)
1336 if (IS_ADHOC_LOC (loc))
1337 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1339 if (loc < RESERVED_LOCATION_COUNT)
1341 /* A reserved location wasn't encoded in a map. Let's return a
1342 NULL map here, just like what linemap_ordinary_map_lookup
1343 does. */
1344 if (map)
1345 *map = NULL;
1346 return loc;
1349 switch (lrk)
1351 case LRK_MACRO_EXPANSION_POINT:
1352 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1353 break;
1354 case LRK_SPELLING_LOCATION:
1355 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1356 break;
1357 case LRK_MACRO_DEFINITION_LOCATION:
1358 loc = linemap_macro_loc_to_def_point (set, loc, map);
1359 break;
1360 default:
1361 abort ();
1363 return loc;
1367 Suppose that LOC is the virtual location of a token T coming from
1368 the expansion of a macro M. This function then steps up to get the
1369 location L of the point where M got expanded. If L is a spelling
1370 location inside a macro expansion M', then this function returns
1371 the locus of the point where M' was expanded. Said otherwise, this
1372 function returns the location of T in the context that triggered
1373 the expansion of M.
1375 *LOC_MAP must be set to the map of LOC. This function then sets it
1376 to the map of the returned location. */
1378 source_location
1379 linemap_unwind_toward_expansion (struct line_maps *set,
1380 source_location loc,
1381 const struct line_map **map)
1383 source_location resolved_location;
1384 const line_map_macro *macro_map = linemap_check_macro (*map);
1385 const struct line_map *resolved_map;
1387 if (IS_ADHOC_LOC (loc))
1388 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1390 resolved_location =
1391 linemap_macro_map_loc_unwind_toward_spelling (macro_map, loc);
1392 resolved_map = linemap_lookup (set, resolved_location);
1394 if (!linemap_macro_expansion_map_p (resolved_map))
1396 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1397 resolved_map = linemap_lookup (set, resolved_location);
1400 *map = resolved_map;
1401 return resolved_location;
1404 /* If LOC is the virtual location of a token coming from the expansion
1405 of a macro M and if its spelling location is reserved (e.g, a
1406 location for a built-in token), then this function unwinds (using
1407 linemap_unwind_toward_expansion) the location until a location that
1408 is not reserved and is not in a system header is reached. In other
1409 words, this unwinds the reserved location until a location that is
1410 in real source code is reached.
1412 Otherwise, if the spelling location for LOC is not reserved or if
1413 LOC doesn't come from the expansion of a macro, the function
1414 returns LOC as is and *MAP is not touched.
1416 *MAP is set to the map of the returned location if the later is
1417 different from LOC. */
1418 source_location
1419 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1420 source_location loc,
1421 const struct line_map **map)
1423 source_location resolved_loc;
1424 const struct line_map *map0 = NULL;
1425 const line_map_ordinary *map1 = NULL;
1427 if (IS_ADHOC_LOC (loc))
1428 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1430 map0 = linemap_lookup (set, loc);
1431 if (!linemap_macro_expansion_map_p (map0))
1432 return loc;
1434 resolved_loc = linemap_resolve_location (set, loc,
1435 LRK_SPELLING_LOCATION,
1436 &map1);
1438 if (resolved_loc >= RESERVED_LOCATION_COUNT
1439 && !LINEMAP_SYSP (map1))
1440 return loc;
1442 while (linemap_macro_expansion_map_p (map0)
1443 && (resolved_loc < RESERVED_LOCATION_COUNT
1444 || LINEMAP_SYSP (map1)))
1446 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1447 resolved_loc = linemap_resolve_location (set, loc,
1448 LRK_SPELLING_LOCATION,
1449 &map1);
1452 if (map != NULL)
1453 *map = map0;
1454 return loc;
1457 /* Expand source code location LOC and return a user readable source
1458 code location. LOC must be a spelling (non-virtual) location. If
1459 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1460 location is returned. */
1462 expanded_location
1463 linemap_expand_location (struct line_maps *set,
1464 const struct line_map *map,
1465 source_location loc)
1468 expanded_location xloc;
1470 memset (&xloc, 0, sizeof (xloc));
1471 if (IS_ADHOC_LOC (loc))
1473 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1474 xloc.data
1475 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1478 if (loc < RESERVED_LOCATION_COUNT)
1479 /* The location for this token wasn't generated from a line map.
1480 It was probably a location for a builtin token, chosen by some
1481 client code. Let's not try to expand the location in that
1482 case. */;
1483 else if (map == NULL)
1484 /* We shouldn't be getting a NULL map with a location that is not
1485 reserved by the client code. */
1486 abort ();
1487 else
1489 /* MAP must be an ordinary map and LOC must be non-virtual,
1490 encoded into this map, obviously; the accessors used on MAP
1491 below ensure it is ordinary. Let's just assert the
1492 non-virtualness of LOC here. */
1493 if (linemap_location_from_macro_expansion_p (set, loc))
1494 abort ();
1496 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1498 xloc.file = LINEMAP_FILE (ord_map);
1499 xloc.line = SOURCE_LINE (ord_map, loc);
1500 xloc.column = SOURCE_COLUMN (ord_map, loc);
1501 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1504 return xloc;
1508 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1509 is NULL, use stderr. IS_MACRO is true if the caller wants to
1510 dump a macro map, false otherwise. */
1512 void
1513 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1515 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1516 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1517 "LC_ENTER_MACRO" };
1518 const char *reason;
1519 const line_map *map;
1521 if (stream == NULL)
1522 stream = stderr;
1524 if (!is_macro)
1525 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1526 else
1527 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1529 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1531 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1532 ix, (void *) map, map->start_location, reason,
1533 ((!is_macro
1534 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1535 ? "yes" : "no"));
1536 if (!is_macro)
1538 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1539 unsigned includer_ix;
1540 const line_map_ordinary *includer_map;
1542 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
1543 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1544 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1545 : NULL;
1547 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1548 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1549 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1550 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1552 else
1554 const line_map_macro *macro_map = linemap_check_macro (map);
1555 fprintf (stream, "Macro: %s (%u tokens)\n",
1556 linemap_map_get_macro_name (macro_map),
1557 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1560 fprintf (stream, "\n");
1564 /* Dump debugging information about source location LOC into the file
1565 stream STREAM. SET is the line map set LOC comes from. */
1567 void
1568 linemap_dump_location (struct line_maps *set,
1569 source_location loc,
1570 FILE *stream)
1572 const line_map_ordinary *map;
1573 source_location location;
1574 const char *path = "", *from = "";
1575 int l = -1, c = -1, s = -1, e = -1;
1577 if (IS_ADHOC_LOC (loc))
1578 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1580 if (loc == 0)
1581 return;
1583 location =
1584 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1586 if (map == NULL)
1587 /* Only reserved locations can be tolerated in this case. */
1588 linemap_assert (location < RESERVED_LOCATION_COUNT);
1589 else
1591 path = LINEMAP_FILE (map);
1592 l = SOURCE_LINE (map, location);
1593 c = SOURCE_COLUMN (map, location);
1594 s = LINEMAP_SYSP (map) != 0;
1595 e = location != loc;
1596 if (e)
1597 from = "N/A";
1598 else
1599 from = (INCLUDED_FROM (set, map))
1600 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1601 : "<NULL>";
1604 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1605 E: macro expansion?, LOC: original location, R: resolved location */
1606 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1607 path, from, l, c, s, (void*)map, e, loc, location);
1610 /* Return the highest location emitted for a given file for which
1611 there is a line map in SET. FILE_NAME is the file name to
1612 consider. If the function returns TRUE, *LOC is set to the highest
1613 location emitted for that file. */
1615 bool
1616 linemap_get_file_highest_location (struct line_maps *set,
1617 const char *file_name,
1618 source_location *loc)
1620 /* If the set is empty or no ordinary map has been created then
1621 there is no file to look for ... */
1622 if (set == NULL || set->info_ordinary.used == 0)
1623 return false;
1625 /* Now look for the last ordinary map created for FILE_NAME. */
1626 int i;
1627 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1629 const char *fname = set->info_ordinary.maps[i].to_file;
1630 if (fname && !filename_cmp (fname, file_name))
1631 break;
1634 if (i < 0)
1635 return false;
1637 /* The highest location for a given map is either the starting
1638 location of the next map minus one, or -- if the map is the
1639 latest one -- the highest location of the set. */
1640 source_location result;
1641 if (i == (int) set->info_ordinary.used - 1)
1642 result = set->highest_location;
1643 else
1644 result = set->info_ordinary.maps[i + 1].start_location - 1;
1646 *loc = result;
1647 return true;
1650 /* Compute and return statistics about the memory consumption of some
1651 parts of the line table SET. */
1653 void
1654 linemap_get_statistics (struct line_maps *set,
1655 struct linemap_stats *s)
1657 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1658 macro_maps_allocated_size, macro_maps_used_size,
1659 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1661 const line_map_macro *cur_map;
1663 ordinary_maps_allocated_size =
1664 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1666 ordinary_maps_used_size =
1667 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1669 macro_maps_allocated_size =
1670 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1672 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1673 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1674 ++cur_map)
1676 unsigned i;
1678 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1680 macro_maps_locations_size +=
1681 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1683 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1685 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1686 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1687 duplicated_macro_maps_locations_size +=
1688 sizeof (source_location);
1692 macro_maps_used_size =
1693 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1695 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1696 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1697 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1698 s->ordinary_maps_used_size = ordinary_maps_used_size;
1699 s->num_expanded_macros = num_expanded_macros_counter;
1700 s->num_macro_tokens = num_macro_tokens_counter;
1701 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1702 s->macro_maps_allocated_size = macro_maps_allocated_size;
1703 s->macro_maps_locations_size = macro_maps_locations_size;
1704 s->macro_maps_used_size = macro_maps_used_size;
1705 s->duplicated_macro_maps_locations_size =
1706 duplicated_macro_maps_locations_size;
1710 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1711 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1712 specifies how many macro maps to dump. */
1714 void
1715 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1716 unsigned int num_macro)
1718 unsigned int i;
1720 if (set == NULL)
1721 return;
1723 if (stream == NULL)
1724 stream = stderr;
1726 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1727 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1728 fprintf (stream, "Include stack depth: %d\n", set->depth);
1729 fprintf (stream, "Highest location: %u\n", set->highest_location);
1731 if (num_ordinary)
1733 fprintf (stream, "\nOrdinary line maps\n");
1734 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1735 linemap_dump (stream, set, i, false);
1736 fprintf (stream, "\n");
1739 if (num_macro)
1741 fprintf (stream, "\nMacro line maps\n");
1742 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1743 linemap_dump (stream, set, i, true);
1744 fprintf (stream, "\n");