2014-11-12 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libcpp / line-map.c
blobaff0294936c3e2526bdfd01440ac192f811a54b2
1 /* Map logical line numbers to (source file, line number) pairs.
2 Copyright (C) 2001-2014 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 static void trace_include (const struct line_maps *, const struct line_map *);
30 static const struct line_map * linemap_ordinary_map_lookup (struct line_maps *,
31 source_location);
32 static const struct line_map* linemap_macro_map_lookup (struct line_maps *,
33 source_location);
34 static source_location linemap_macro_map_loc_to_def_point
35 (const struct line_map*, source_location);
36 static source_location linemap_macro_map_loc_unwind_toward_spelling
37 (const struct line_map*, source_location);
38 static source_location linemap_macro_map_loc_to_exp_point
39 (const struct line_map*, source_location);
40 static source_location linemap_macro_loc_to_spelling_point
41 (struct line_maps *, source_location, const struct line_map **);
42 static source_location linemap_macro_loc_to_def_point (struct line_maps *,
43 source_location,
44 const struct line_map **);
45 static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
46 source_location,
47 const struct line_map **);
49 /* Counters defined in macro.c. */
50 extern unsigned num_expanded_macros_counter;
51 extern unsigned num_macro_tokens_counter;
53 /* Hash function for location_adhoc_data hashtable. */
55 static hashval_t
56 location_adhoc_data_hash (const void *l)
58 const struct location_adhoc_data *lb =
59 (const struct location_adhoc_data *) l;
60 return (hashval_t) lb->locus + (size_t) lb->data;
63 /* Compare function for location_adhoc_data hashtable. */
65 static int
66 location_adhoc_data_eq (const void *l1, const void *l2)
68 const struct location_adhoc_data *lb1 =
69 (const struct location_adhoc_data *) l1;
70 const struct location_adhoc_data *lb2 =
71 (const struct location_adhoc_data *) l2;
72 return lb1->locus == lb2->locus && lb1->data == lb2->data;
75 /* Update the hashtable when location_adhoc_data is reallocated. */
77 static int
78 location_adhoc_data_update (void **slot, void *data)
80 *((char **) slot) += *((long long *) data);
81 return 1;
84 /* Rebuild the hash table from the location adhoc data. */
86 void
87 rebuild_location_adhoc_htab (struct line_maps *set)
89 unsigned i;
90 set->location_adhoc_data_map.htab =
91 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
92 for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
93 htab_find_slot (set->location_adhoc_data_map.htab,
94 set->location_adhoc_data_map.data + i, INSERT);
97 /* Combine LOCUS and DATA to a combined adhoc loc. */
99 source_location
100 get_combined_adhoc_loc (struct line_maps *set,
101 source_location locus, void *data)
103 struct location_adhoc_data lb;
104 struct location_adhoc_data **slot;
106 linemap_assert (data);
108 if (IS_ADHOC_LOC (locus))
109 locus
110 = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
111 if (locus == 0 && data == NULL)
112 return 0;
113 lb.locus = locus;
114 lb.data = data;
115 slot = (struct location_adhoc_data **)
116 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
117 if (*slot == NULL)
119 if (set->location_adhoc_data_map.curr_loc >=
120 set->location_adhoc_data_map.allocated)
122 char *orig_data = (char *) set->location_adhoc_data_map.data;
123 long long offset;
124 /* Cast away extern "C" from the type of xrealloc. */
125 line_map_realloc reallocator = (set->reallocator
126 ? set->reallocator
127 : (line_map_realloc) xrealloc);
129 if (set->location_adhoc_data_map.allocated == 0)
130 set->location_adhoc_data_map.allocated = 128;
131 else
132 set->location_adhoc_data_map.allocated *= 2;
133 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
134 reallocator (set->location_adhoc_data_map.data,
135 set->location_adhoc_data_map.allocated
136 * sizeof (struct location_adhoc_data));
137 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
138 if (set->location_adhoc_data_map.allocated > 128)
139 htab_traverse (set->location_adhoc_data_map.htab,
140 location_adhoc_data_update, &offset);
142 *slot = set->location_adhoc_data_map.data
143 + set->location_adhoc_data_map.curr_loc;
144 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
145 = lb;
147 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
150 /* Return the data for the adhoc loc. */
152 void *
153 get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
155 linemap_assert (IS_ADHOC_LOC (loc));
156 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
159 /* Return the location for the adhoc loc. */
161 source_location
162 get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
164 linemap_assert (IS_ADHOC_LOC (loc));
165 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
168 /* Finalize the location_adhoc_data structure. */
169 void
170 location_adhoc_data_fini (struct line_maps *set)
172 htab_delete (set->location_adhoc_data_map.htab);
175 /* Initialize a line map set. */
177 void
178 linemap_init (struct line_maps *set,
179 source_location builtin_location)
181 memset (set, 0, sizeof (struct line_maps));
182 set->highest_location = RESERVED_LOCATION_COUNT - 1;
183 set->highest_line = RESERVED_LOCATION_COUNT - 1;
184 set->location_adhoc_data_map.htab =
185 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
186 set->builtin_location = builtin_location;
189 /* Check for and warn about line_maps entered but not exited. */
191 void
192 linemap_check_files_exited (struct line_maps *set)
194 struct line_map *map;
195 /* Depending upon whether we are handling preprocessed input or
196 not, this can be a user error or an ICE. */
197 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
198 ! MAIN_FILE_P (map);
199 map = INCLUDED_FROM (set, map))
200 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
201 ORDINARY_MAP_FILE_NAME (map));
204 /* Create a new line map in the line map set SET, and return it.
205 REASON is the reason of creating the map. It determines the type
206 of map created (ordinary or macro map). Note that ordinary maps and
207 macro maps are allocated in different memory location. */
209 static struct line_map *
210 new_linemap (struct line_maps *set,
211 enum lc_reason reason)
213 /* Depending on this variable, a macro map would be allocated in a
214 different memory location than an ordinary map. */
215 bool macro_map_p = (reason == LC_ENTER_MACRO);
216 struct line_map *result;
218 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
220 /* We ran out of allocated line maps. Let's allocate more. */
221 unsigned alloc_size;
223 /* Cast away extern "C" from the type of xrealloc. */
224 line_map_realloc reallocator = (set->reallocator
225 ? set->reallocator
226 : (line_map_realloc) xrealloc);
227 line_map_round_alloc_size_func round_alloc_size =
228 set->round_alloc_size;
230 /* We are going to execute some dance to try to reduce the
231 overhead of the memory allocator, in case we are using the
232 ggc-page.c one.
234 The actual size of memory we are going to get back from the
235 allocator is the smallest power of 2 that is greater than the
236 size we requested. So let's consider that size then. */
238 alloc_size =
239 (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
240 * sizeof (struct line_map);
242 /* Get the actual size of memory that is going to be allocated
243 by the allocator. */
244 alloc_size = round_alloc_size (alloc_size);
246 /* Now alloc_size contains the exact memory size we would get if
247 we have asked for the initial alloc_size amount of memory.
248 Let's get back to the number of macro map that amounts
249 to. */
250 LINEMAPS_ALLOCATED (set, macro_map_p) =
251 alloc_size / (sizeof (struct line_map));
253 /* And now let's really do the re-allocation. */
254 LINEMAPS_MAPS (set, macro_map_p) =
255 (struct line_map *) (*reallocator)
256 (LINEMAPS_MAPS (set, macro_map_p),
257 (LINEMAPS_ALLOCATED (set, macro_map_p)
258 * sizeof (struct line_map)));
260 result =
261 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
262 memset (result, 0,
263 ((LINEMAPS_ALLOCATED (set, macro_map_p)
264 - LINEMAPS_USED (set, macro_map_p))
265 * sizeof (struct line_map)));
267 else
268 result =
269 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
271 LINEMAPS_USED (set, macro_map_p)++;
273 result->reason = reason;
274 return result;
277 /* Add a mapping of logical source line to physical source file and
278 line number.
280 The text pointed to by TO_FILE must have a lifetime
281 at least as long as the final call to lookup_line (). An empty
282 TO_FILE means standard input. If reason is LC_LEAVE, and
283 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
284 natural values considering the file we are returning to.
286 FROM_LINE should be monotonic increasing across calls to this
287 function. A call to this function can relocate the previous set of
288 maps, so any stored line_map pointers should not be used. */
290 const struct line_map *
291 linemap_add (struct line_maps *set, enum lc_reason reason,
292 unsigned int sysp, const char *to_file, linenum_type to_line)
294 struct line_map *map;
295 source_location start_location = set->highest_location + 1;
297 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
298 && (start_location
299 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
301 /* When we enter the file for the first time reason cannot be
302 LC_RENAME. */
303 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
305 /* If we are leaving the main file, return a NULL map. */
306 if (reason == LC_LEAVE
307 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
308 && to_file == NULL)
310 set->depth--;
311 return NULL;
314 map = new_linemap (set, reason);
316 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
317 to_file = "<stdin>";
319 if (reason == LC_RENAME_VERBATIM)
320 reason = LC_RENAME;
322 if (reason == LC_LEAVE)
324 /* When we are just leaving an "included" file, and jump to the next
325 location inside the "includer" right after the #include
326 "included", this variable points the map in use right before the
327 #include "included", inside the same "includer" file. */
328 struct line_map *from;
329 bool error;
331 if (MAIN_FILE_P (map - 1))
333 /* So this _should_ means we are leaving the main file --
334 effectively ending the compilation unit. But to_file not
335 being NULL means the caller thinks we are leaving to
336 another file. This is an erroneous behaviour but we'll
337 try to recover from it. Let's pretend we are not leaving
338 the main file. */
339 error = true;
340 reason = LC_RENAME;
341 from = map - 1;
343 else
345 /* (MAP - 1) points to the map we are leaving. The
346 map from which (MAP - 1) got included should be the map
347 that comes right before MAP in the same file. */
348 from = INCLUDED_FROM (set, map - 1);
349 error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
350 to_file);
353 /* Depending upon whether we are handling preprocessed input or
354 not, this can be a user error or an ICE. */
355 if (error)
356 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
357 to_file);
359 /* A TO_FILE of NULL is special - we use the natural values. */
360 if (error || to_file == NULL)
362 to_file = ORDINARY_MAP_FILE_NAME (from);
363 to_line = SOURCE_LINE (from, from[1].start_location);
364 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
368 linemap_assert (reason != LC_ENTER_MACRO);
369 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map) = sysp;
370 MAP_START_LOCATION (map) = start_location;
371 ORDINARY_MAP_FILE_NAME (map) = to_file;
372 ORDINARY_MAP_STARTING_LINE_NUMBER (map) = to_line;
373 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
374 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = 0;
375 set->highest_location = start_location;
376 set->highest_line = start_location;
377 set->max_column_hint = 0;
379 if (reason == LC_ENTER)
381 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
382 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
383 set->depth++;
384 if (set->trace_includes)
385 trace_include (set, map);
387 else if (reason == LC_RENAME)
388 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
389 ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
390 else if (reason == LC_LEAVE)
392 set->depth--;
393 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
394 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
397 return map;
400 /* Returns TRUE if the line table set tracks token locations across
401 macro expansion, FALSE otherwise. */
403 bool
404 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
406 return LINEMAPS_MACRO_MAPS (set) != NULL;
409 /* Create a macro map. A macro map encodes source locations of tokens
410 that are part of a macro replacement-list, at a macro expansion
411 point. See the extensive comments of struct line_map and struct
412 line_map_macro, in line-map.h.
414 This map shall be created when the macro is expanded. The map
415 encodes the source location of the expansion point of the macro as
416 well as the "original" source location of each token that is part
417 of the macro replacement-list. If a macro is defined but never
418 expanded, it has no macro map. SET is the set of maps the macro
419 map should be part of. MACRO_NODE is the macro which the new macro
420 map should encode source locations for. EXPANSION is the location
421 of the expansion point of MACRO. For function-like macros
422 invocations, it's best to make it point to the closing parenthesis
423 of the macro, rather than the the location of the first character
424 of the macro. NUM_TOKENS is the number of tokens that are part of
425 the replacement-list of MACRO.
427 Note that when we run out of the integer space available for source
428 locations, this function returns NULL. In that case, callers of
429 this function cannot encode {line,column} pairs into locations of
430 macro tokens anymore. */
432 const struct line_map *
433 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
434 source_location expansion, unsigned int num_tokens)
436 struct line_map *map;
437 source_location start_location;
438 /* Cast away extern "C" from the type of xrealloc. */
439 line_map_realloc reallocator = (set->reallocator
440 ? set->reallocator
441 : (line_map_realloc) xrealloc);
443 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
445 if (start_location <= set->highest_line
446 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
447 /* We ran out of macro map space. */
448 return NULL;
450 map = new_linemap (set, LC_ENTER_MACRO);
452 MAP_START_LOCATION (map) = start_location;
453 MACRO_MAP_MACRO (map) = macro_node;
454 MACRO_MAP_NUM_MACRO_TOKENS (map) = num_tokens;
455 MACRO_MAP_LOCATIONS (map)
456 = (source_location*) reallocator (NULL,
457 2 * num_tokens
458 * sizeof (source_location));
459 MACRO_MAP_EXPANSION_POINT_LOCATION (map) = expansion;
460 memset (MACRO_MAP_LOCATIONS (map), 0,
461 num_tokens * sizeof (source_location));
463 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
465 return map;
468 /* Create and return a virtual location for a token that is part of a
469 macro expansion-list at a macro expansion point. See the comment
470 inside struct line_map_macro to see what an expansion-list exactly
473 A call to this function must come after a call to
474 linemap_enter_macro.
476 MAP is the map into which the source location is created. TOKEN_NO
477 is the index of the token in the macro replacement-list, starting
478 at number 0.
480 ORIG_LOC is the location of the token outside of this macro
481 expansion. If the token comes originally from the macro
482 definition, it is the locus in the macro definition; otherwise it
483 is a location in the context of the caller of this macro expansion
484 (which is a virtual location or a source location if the caller is
485 itself a macro expansion or not).
487 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
488 either of the token itself or of a macro parameter that it
489 replaces. */
491 source_location
492 linemap_add_macro_token (const struct line_map *map,
493 unsigned int token_no,
494 source_location orig_loc,
495 source_location orig_parm_replacement_loc)
497 source_location result;
499 linemap_assert (linemap_macro_expansion_map_p (map));
500 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
502 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
503 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
505 result = MAP_START_LOCATION (map) + token_no;
506 return result;
509 /* Return a source_location for the start (i.e. column==0) of
510 (physical) line TO_LINE in the current source file (as in the
511 most recent linemap_add). MAX_COLUMN_HINT is the highest column
512 number we expect to use in this line (but it does not change
513 the highest_location). */
515 source_location
516 linemap_line_start (struct line_maps *set, linenum_type to_line,
517 unsigned int max_column_hint)
519 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
520 source_location highest = set->highest_location;
521 source_location r;
522 linenum_type last_line =
523 SOURCE_LINE (map, set->highest_line);
524 int line_delta = to_line - last_line;
525 bool add_map = false;
527 if (line_delta < 0
528 || (line_delta > 10
529 && line_delta * ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) > 1000)
530 || (max_column_hint >= (1U << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)))
531 || (max_column_hint <= 80
532 && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10))
534 add_map = true;
536 else
537 max_column_hint = set->max_column_hint;
538 if (add_map)
540 int column_bits;
541 if (max_column_hint > 100000 || highest > 0x60000000)
543 /* If the column number is ridiculous or we've allocated a huge
544 number of source_locations, give up on column numbers. */
545 max_column_hint = 0;
546 if (highest >0x70000000)
547 return 0;
548 column_bits = 0;
550 else
552 column_bits = 7;
553 while (max_column_hint >= (1U << column_bits))
554 column_bits++;
555 max_column_hint = 1U << column_bits;
557 /* Allocate the new line_map. However, if the current map only has a
558 single line we can sometimes just increase its column_bits instead. */
559 if (line_delta < 0
560 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
561 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
562 map = (struct line_map *) linemap_add (set, LC_RENAME,
563 ORDINARY_MAP_IN_SYSTEM_HEADER_P
564 (map),
565 ORDINARY_MAP_FILE_NAME (map),
566 to_line);
567 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = column_bits;
568 r = (MAP_START_LOCATION (map)
569 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
570 << column_bits));
572 else
573 r = highest - SOURCE_COLUMN (map, highest)
574 + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
576 /* Locations of ordinary tokens are always lower than locations of
577 macro tokens. */
578 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
579 return 0;
581 set->highest_line = r;
582 if (r > set->highest_location)
583 set->highest_location = r;
584 set->max_column_hint = max_column_hint;
585 return r;
588 /* Encode and return a source_location from a column number. The
589 source line considered is the last source line used to call
590 linemap_line_start, i.e, the last source line which a location was
591 encoded from. */
593 source_location
594 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
596 source_location r = set->highest_line;
598 linemap_assert
599 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
601 if (to_column >= set->max_column_hint)
603 if (r >= 0xC000000 || to_column > 100000)
605 /* Running low on source_locations - disable column numbers. */
606 return r;
608 else
610 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
611 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
614 r = r + to_column;
615 if (r >= set->highest_location)
616 set->highest_location = r;
617 return r;
620 /* Encode and return a source location from a given line and
621 column. */
623 source_location
624 linemap_position_for_line_and_column (const struct line_map *map,
625 linenum_type line,
626 unsigned column)
628 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (map) <= line);
630 return (MAP_START_LOCATION (map)
631 + ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
632 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
633 + (column & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1)));
636 /* Encode and return a source_location starting from location LOC and
637 shifting it by OFFSET columns. This function does not support
638 virtual locations. */
640 source_location
641 linemap_position_for_loc_and_offset (struct line_maps *set,
642 source_location loc,
643 unsigned int offset)
645 const struct line_map * map = NULL;
647 /* This function does not support virtual locations yet. */
648 linemap_assert (!linemap_location_from_macro_expansion_p (set, loc));
650 if (offset == 0
651 /* Adding an offset to a reserved location (like
652 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
653 sense. So let's leave the location intact in that case. */
654 || loc < RESERVED_LOCATION_COUNT)
655 return loc;
657 /* We find the real location and shift it. */
658 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
659 /* The new location (loc + offset) should be higher than the first
660 location encoded by MAP. */
661 linemap_assert (MAP_START_LOCATION (map) < loc + offset);
663 /* If MAP is not the last line map of its set, then the new location
664 (loc + offset) should be less than the first location encoded by
665 the next line map of the set. */
666 if (map != LINEMAPS_LAST_ORDINARY_MAP (set))
667 linemap_assert (loc + offset < MAP_START_LOCATION (&map[1]));
669 offset += SOURCE_COLUMN (map, loc);
670 linemap_assert (offset < (1u << map->d.ordinary.column_bits));
672 source_location r =
673 linemap_position_for_line_and_column (map,
674 SOURCE_LINE (map, loc),
675 offset);
676 linemap_assert (map == linemap_lookup (set, r));
677 return r;
680 /* Given a virtual source location yielded by a map (either an
681 ordinary or a macro map), returns that map. */
683 const struct line_map*
684 linemap_lookup (struct line_maps *set, source_location line)
686 if (IS_ADHOC_LOC (line))
687 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
688 if (linemap_location_from_macro_expansion_p (set, line))
689 return linemap_macro_map_lookup (set, line);
690 return linemap_ordinary_map_lookup (set, line);
693 /* Given a source location yielded by an ordinary map, returns that
694 map. Since the set is built chronologically, the logical lines are
695 monotonic increasing, and so the list is sorted and we can use a
696 binary search. */
698 static const struct line_map *
699 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
701 unsigned int md, mn, mx;
702 const struct line_map *cached, *result;
704 if (IS_ADHOC_LOC (line))
705 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
707 if (set == NULL || line < RESERVED_LOCATION_COUNT)
708 return NULL;
710 mn = LINEMAPS_ORDINARY_CACHE (set);
711 mx = LINEMAPS_ORDINARY_USED (set);
713 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
714 /* We should get a segfault if no line_maps have been added yet. */
715 if (line >= MAP_START_LOCATION (cached))
717 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
718 return cached;
720 else
722 mx = mn;
723 mn = 0;
726 while (mx - mn > 1)
728 md = (mn + mx) / 2;
729 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
730 mx = md;
731 else
732 mn = md;
735 LINEMAPS_ORDINARY_CACHE (set) = mn;
736 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
737 linemap_assert (line >= MAP_START_LOCATION (result));
738 return result;
741 /* Given a source location yielded by a macro map, returns that map.
742 Since the set is built chronologically, the logical lines are
743 monotonic decreasing, and so the list is sorted and we can use a
744 binary search. */
746 static const struct line_map*
747 linemap_macro_map_lookup (struct line_maps *set, source_location line)
749 unsigned int md, mn, mx;
750 const struct line_map *cached, *result;
752 if (IS_ADHOC_LOC (line))
753 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
755 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
757 if (set == NULL)
758 return NULL;
760 mn = LINEMAPS_MACRO_CACHE (set);
761 mx = LINEMAPS_MACRO_USED (set);
762 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
764 if (line >= MAP_START_LOCATION (cached))
766 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
767 return cached;
768 mx = mn - 1;
769 mn = 0;
772 while (mn < mx)
774 md = (mx + mn) / 2;
775 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
776 mn = md + 1;
777 else
778 mx = md;
781 LINEMAPS_MACRO_CACHE (set) = mx;
782 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
783 linemap_assert (MAP_START_LOCATION (result) <= line);
785 return result;
788 /* Return TRUE if MAP encodes locations coming from a macro
789 replacement-list at macro expansion point. */
791 bool
792 linemap_macro_expansion_map_p (const struct line_map *map)
794 if (!map)
795 return false;
796 return (map->reason == LC_ENTER_MACRO);
799 /* If LOCATION is the locus of a token in a replacement-list of a
800 macro expansion return the location of the macro expansion point.
802 Read the comments of struct line_map and struct line_map_macro in
803 line-map.h to understand what a macro expansion point is. */
805 static source_location
806 linemap_macro_map_loc_to_exp_point (const struct line_map *map,
807 source_location location ATTRIBUTE_UNUSED)
809 linemap_assert (linemap_macro_expansion_map_p (map)
810 && location >= MAP_START_LOCATION (map));
812 /* Make sure LOCATION is correct. */
813 linemap_assert ((location - MAP_START_LOCATION (map))
814 < MACRO_MAP_NUM_MACRO_TOKENS (map));
816 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
819 /* LOCATION is the source location of a token that belongs to a macro
820 replacement-list as part of the macro expansion denoted by MAP.
822 Return the location of the token at the definition point of the
823 macro. */
825 static source_location
826 linemap_macro_map_loc_to_def_point (const struct line_map *map,
827 source_location location)
829 unsigned token_no;
831 linemap_assert (linemap_macro_expansion_map_p (map)
832 && location >= MAP_START_LOCATION (map));
833 linemap_assert (location >= RESERVED_LOCATION_COUNT);
835 token_no = location - MAP_START_LOCATION (map);
836 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
838 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
840 return location;
843 /* If LOCATION is the locus of a token that is an argument of a
844 function-like macro M and appears in the expansion of M, return the
845 locus of that argument in the context of the caller of M.
847 In other words, this returns the xI location presented in the
848 comments of line_map_macro above. */
849 source_location
850 linemap_macro_map_loc_unwind_toward_spelling (const struct line_map* map,
851 source_location location)
853 unsigned token_no;
855 linemap_assert (linemap_macro_expansion_map_p (map)
856 && location >= MAP_START_LOCATION (map));
857 linemap_assert (location >= RESERVED_LOCATION_COUNT);
859 token_no = location - MAP_START_LOCATION (map);
860 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
862 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
864 return location;
867 /* Return the source line number corresponding to source location
868 LOCATION. SET is the line map set LOCATION comes from. If
869 LOCATION is the source location of token that is part of the
870 replacement-list of a macro expansion return the line number of the
871 macro expansion point. */
874 linemap_get_expansion_line (struct line_maps *set,
875 source_location location)
877 const struct line_map *map = NULL;
879 if (IS_ADHOC_LOC (location))
880 location = set->location_adhoc_data_map.data[location
881 & MAX_SOURCE_LOCATION].locus;
883 if (location < RESERVED_LOCATION_COUNT)
884 return 0;
886 location =
887 linemap_macro_loc_to_exp_point (set, location, &map);
889 return SOURCE_LINE (map, location);
892 /* Return the path of the file corresponding to source code location
893 LOCATION.
895 If LOCATION is the source location of token that is part of the
896 replacement-list of a macro expansion return the file path of the
897 macro expansion point.
899 SET is the line map set LOCATION comes from. */
901 const char*
902 linemap_get_expansion_filename (struct line_maps *set,
903 source_location location)
905 const struct line_map *map = NULL;
907 if (IS_ADHOC_LOC (location))
908 location = set->location_adhoc_data_map.data[location
909 & MAX_SOURCE_LOCATION].locus;
911 if (location < RESERVED_LOCATION_COUNT)
912 return NULL;
914 location =
915 linemap_macro_loc_to_exp_point (set, location, &map);
917 return LINEMAP_FILE (map);
920 /* Return the name of the macro associated to MACRO_MAP. */
922 const char*
923 linemap_map_get_macro_name (const struct line_map* macro_map)
925 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
926 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
929 /* Return a positive value if LOCATION is the locus of a token that is
930 located in a system header, O otherwise. It returns 1 if LOCATION
931 is the locus of a token that is located in a system header, and 2
932 if LOCATION is the locus of a token located in a C system header
933 that therefore needs to be extern "C" protected in C++.
935 Note that this function returns 1 if LOCATION belongs to a token
936 that is part of a macro replacement-list defined in a system
937 header, but expanded in a non-system file. */
940 linemap_location_in_system_header_p (struct line_maps *set,
941 source_location location)
943 const struct line_map *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 false;
952 /* Let's look at where the token for LOCATION comes from. */
953 while (true)
955 map = linemap_lookup (set, location);
956 if (map != NULL)
958 if (!linemap_macro_expansion_map_p (map))
959 /* It's a normal token. */
960 return LINEMAP_SYSP (map);
961 else
963 /* It's a token resulting from a macro expansion. */
964 source_location loc =
965 linemap_macro_map_loc_unwind_toward_spelling (map, location);
966 if (loc < RESERVED_LOCATION_COUNT)
967 /* This token might come from a built-in macro. Let's
968 look at where that macro got expanded. */
969 location = linemap_macro_map_loc_to_exp_point (map, location);
970 else
971 location = loc;
974 else
975 break;
977 return false;
980 /* Return TRUE if LOCATION is a source code location of a token coming
981 from a macro replacement-list at a macro expansion point, FALSE
982 otherwise. */
984 bool
985 linemap_location_from_macro_expansion_p (const struct line_maps *set,
986 source_location location)
988 if (IS_ADHOC_LOC (location))
989 location = set->location_adhoc_data_map.data[location
990 & MAX_SOURCE_LOCATION].locus;
992 linemap_assert (location <= MAX_SOURCE_LOCATION
993 && (set->highest_location
994 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
995 if (set == NULL)
996 return false;
997 return (location > set->highest_location);
1000 /* Given two virtual locations *LOC0 and *LOC1, return the first
1001 common macro map in their macro expansion histories. Return NULL
1002 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1003 virtual location of the token inside the resulting macro. */
1005 static const struct line_map*
1006 first_map_in_common_1 (struct line_maps *set,
1007 source_location *loc0,
1008 source_location *loc1)
1010 source_location l0 = *loc0, l1 = *loc1;
1011 const struct line_map *map0 = linemap_lookup (set, l0),
1012 *map1 = linemap_lookup (set, l1);
1014 while (linemap_macro_expansion_map_p (map0)
1015 && linemap_macro_expansion_map_p (map1)
1016 && (map0 != map1))
1018 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1020 l0 = linemap_macro_map_loc_to_exp_point (map0, l0);
1021 map0 = linemap_lookup (set, l0);
1023 else
1025 l1 = linemap_macro_map_loc_to_exp_point (map1, l1);
1026 map1 = linemap_lookup (set, l1);
1030 if (map0 == map1)
1032 *loc0 = l0;
1033 *loc1 = l1;
1034 return map0;
1036 return NULL;
1039 /* Given two virtual locations LOC0 and LOC1, return the first common
1040 macro map in their macro expansion histories. Return NULL if no
1041 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1042 virtual location of the token inside the resulting macro, upon
1043 return of a non-NULL result. */
1045 static const struct line_map*
1046 first_map_in_common (struct line_maps *set,
1047 source_location loc0,
1048 source_location loc1,
1049 source_location *res_loc0,
1050 source_location *res_loc1)
1052 *res_loc0 = loc0;
1053 *res_loc1 = loc1;
1055 return first_map_in_common_1 (set, res_loc0, res_loc1);
1058 /* Return a positive value if PRE denotes the location of a token that
1059 comes before the token of POST, 0 if PRE denotes the location of
1060 the same token as the token for POST, and a negative value
1061 otherwise. */
1064 linemap_compare_locations (struct line_maps *set,
1065 source_location pre,
1066 source_location post)
1068 bool pre_virtual_p, post_virtual_p;
1069 source_location l0 = pre, l1 = post;
1071 if (IS_ADHOC_LOC (l0))
1072 l0 = set->location_adhoc_data_map.data[l0 & MAX_SOURCE_LOCATION].locus;
1073 if (IS_ADHOC_LOC (l1))
1074 l1 = set->location_adhoc_data_map.data[l1 & MAX_SOURCE_LOCATION].locus;
1076 if (l0 == l1)
1077 return 0;
1079 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1080 l0 = linemap_resolve_location (set, l0,
1081 LRK_MACRO_EXPANSION_POINT,
1082 NULL);
1084 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1085 l1 = linemap_resolve_location (set, l1,
1086 LRK_MACRO_EXPANSION_POINT,
1087 NULL);
1089 if (l0 == l1
1090 && pre_virtual_p
1091 && post_virtual_p)
1093 /* So pre and post represent two tokens that are present in a
1094 same macro expansion. Let's see if the token for pre was
1095 before the token for post in that expansion. */
1096 unsigned i0, i1;
1097 const struct line_map *map =
1098 first_map_in_common (set, pre, post, &l0, &l1);
1100 if (map == NULL)
1101 /* This should not be possible. */
1102 abort ();
1104 i0 = l0 - MAP_START_LOCATION (map);
1105 i1 = l1 - MAP_START_LOCATION (map);
1106 return i1 - i0;
1109 return l1 - l0;
1112 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1114 static void
1115 trace_include (const struct line_maps *set, const struct line_map *map)
1117 unsigned int i = set->depth;
1119 while (--i)
1120 putc ('.', stderr);
1122 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1125 /* Return the spelling location of the token wherever it comes from,
1126 whether part of a macro definition or not.
1128 This is a subroutine for linemap_resolve_location. */
1130 static source_location
1131 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1132 source_location location,
1133 const struct line_map **original_map)
1135 struct line_map *map;
1137 if (IS_ADHOC_LOC (location))
1138 location = set->location_adhoc_data_map.data[location
1139 & MAX_SOURCE_LOCATION].locus;
1141 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1143 while (true)
1145 map = (struct line_map*) linemap_lookup (set, location);
1146 if (!linemap_macro_expansion_map_p (map))
1147 break;
1149 location =
1150 linemap_macro_map_loc_unwind_toward_spelling (map, location);
1153 if (original_map)
1154 *original_map = map;
1155 return location;
1158 /* If LOCATION is the source location of a token that belongs to a
1159 macro replacement-list -- as part of a macro expansion -- then
1160 return the location of the token at the definition point of the
1161 macro. Otherwise, return LOCATION. SET is the set of maps
1162 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1163 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1164 returned location comes from.
1166 This is a subroutine of linemap_resolve_location. */
1168 static source_location
1169 linemap_macro_loc_to_def_point (struct line_maps *set,
1170 source_location location,
1171 const struct line_map **original_map)
1173 struct line_map *map;
1175 if (IS_ADHOC_LOC (location))
1176 location = set->location_adhoc_data_map.data[location
1177 & MAX_SOURCE_LOCATION].locus;
1179 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1181 while (true)
1183 map = (struct line_map*) linemap_lookup (set, location);
1184 if (!linemap_macro_expansion_map_p (map))
1185 break;
1187 location =
1188 linemap_macro_map_loc_to_def_point (map, location);
1191 if (original_map)
1192 *original_map = map;
1193 return location;
1196 /* If LOCATION is the source location of a token that belongs to a
1197 macro replacement-list -- at a macro expansion point -- then return
1198 the location of the topmost expansion point of the macro. We say
1199 topmost because if we are in the context of a nested macro
1200 expansion, the function returns the source location of the first
1201 macro expansion that triggered the nested expansions.
1203 Otherwise, return LOCATION. SET is the set of maps location come
1204 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1205 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1206 location comes from.
1208 This is a subroutine of linemap_resolve_location. */
1210 static source_location
1211 linemap_macro_loc_to_exp_point (struct line_maps *set,
1212 source_location location,
1213 const struct line_map **original_map)
1215 struct line_map *map;
1217 if (IS_ADHOC_LOC (location))
1218 location = set->location_adhoc_data_map.data[location
1219 & MAX_SOURCE_LOCATION].locus;
1221 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1223 while (true)
1225 map = (struct line_map*) linemap_lookup (set, location);
1226 if (!linemap_macro_expansion_map_p (map))
1227 break;
1228 location = linemap_macro_map_loc_to_exp_point (map, location);
1231 if (original_map)
1232 *original_map = map;
1233 return location;
1236 /* Resolve a virtual location into either a spelling location, an
1237 expansion point location or a token argument replacement point
1238 location. Return the map that encodes the virtual location as well
1239 as the resolved location.
1241 If LOC is *NOT* the location of a token resulting from the
1242 expansion of a macro, then the parameter LRK (which stands for
1243 Location Resolution Kind) is ignored and the resulting location
1244 just equals the one given in argument.
1246 Now if LOC *IS* the location of a token resulting from the
1247 expansion of a macro, this is what happens.
1249 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1250 -------------------------------
1252 The virtual location is resolved to the first macro expansion point
1253 that led to this macro expansion.
1255 * If LRK is set to LRK_SPELLING_LOCATION
1256 -------------------------------------
1258 The virtual location is resolved to the locus where the token has
1259 been spelled in the source. This can follow through all the macro
1260 expansions that led to the token.
1262 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1263 --------------------------------------
1265 The virtual location is resolved to the locus of the token in the
1266 context of the macro definition.
1268 If LOC is the locus of a token that is an argument of a
1269 function-like macro [replacing a parameter in the replacement list
1270 of the macro] the virtual location is resolved to the locus of the
1271 parameter that is replaced, in the context of the definition of the
1272 macro.
1274 If LOC is the locus of a token that is not an argument of a
1275 function-like macro, then the function behaves as if LRK was set to
1276 LRK_SPELLING_LOCATION.
1278 If MAP is not NULL, *MAP is set to the map encoding the
1279 returned location. Note that if the returned location wasn't originally
1280 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1281 resolves to a location reserved for the client code, like
1282 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1284 source_location
1285 linemap_resolve_location (struct line_maps *set,
1286 source_location loc,
1287 enum location_resolution_kind lrk,
1288 const struct line_map **map)
1290 if (IS_ADHOC_LOC (loc))
1291 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1293 if (loc < RESERVED_LOCATION_COUNT)
1295 /* A reserved location wasn't encoded in a map. Let's return a
1296 NULL map here, just like what linemap_ordinary_map_lookup
1297 does. */
1298 if (map)
1299 *map = NULL;
1300 return loc;
1303 switch (lrk)
1305 case LRK_MACRO_EXPANSION_POINT:
1306 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1307 break;
1308 case LRK_SPELLING_LOCATION:
1309 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1310 break;
1311 case LRK_MACRO_DEFINITION_LOCATION:
1312 loc = linemap_macro_loc_to_def_point (set, loc, map);
1313 break;
1314 default:
1315 abort ();
1317 return loc;
1321 Suppose that LOC is the virtual location of a token T coming from
1322 the expansion of a macro M. This function then steps up to get the
1323 location L of the point where M got expanded. If L is a spelling
1324 location inside a macro expansion M', then this function returns
1325 the locus of the point where M' was expanded. Said otherwise, this
1326 function returns the location of T in the context that triggered
1327 the expansion of M.
1329 *LOC_MAP must be set to the map of LOC. This function then sets it
1330 to the map of the returned location. */
1332 source_location
1333 linemap_unwind_toward_expansion (struct line_maps *set,
1334 source_location loc,
1335 const struct line_map **map)
1337 source_location resolved_location;
1338 const struct line_map *resolved_map;
1340 if (IS_ADHOC_LOC (loc))
1341 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1343 resolved_location =
1344 linemap_macro_map_loc_unwind_toward_spelling (*map, loc);
1345 resolved_map = linemap_lookup (set, resolved_location);
1347 if (!linemap_macro_expansion_map_p (resolved_map))
1349 resolved_location = linemap_macro_map_loc_to_exp_point (*map, loc);
1350 resolved_map = linemap_lookup (set, resolved_location);
1353 *map = resolved_map;
1354 return resolved_location;
1357 /* If LOC is the virtual location of a token coming from the expansion
1358 of a macro M and if its spelling location is reserved (e.g, a
1359 location for a built-in token), then this function unwinds (using
1360 linemap_unwind_toward_expansion) the location until a location that
1361 is not reserved and is not in a system header is reached. In other
1362 words, this unwinds the reserved location until a location that is
1363 in real source code is reached.
1365 Otherwise, if the spelling location for LOC is not reserved or if
1366 LOC doesn't come from the expansion of a macro, the function
1367 returns LOC as is and *MAP is not touched.
1369 *MAP is set to the map of the returned location if the later is
1370 different from LOC. */
1371 source_location
1372 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1373 source_location loc,
1374 const struct line_map **map)
1376 source_location resolved_loc;
1377 const struct line_map *map0 = NULL, *map1 = NULL;
1379 if (IS_ADHOC_LOC (loc))
1380 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1382 map0 = linemap_lookup (set, loc);
1383 if (!linemap_macro_expansion_map_p (map0))
1384 return loc;
1386 resolved_loc = linemap_resolve_location (set, loc,
1387 LRK_SPELLING_LOCATION,
1388 &map1);
1390 if (resolved_loc >= RESERVED_LOCATION_COUNT
1391 && !LINEMAP_SYSP (map1))
1392 return loc;
1394 while (linemap_macro_expansion_map_p (map0)
1395 && (resolved_loc < RESERVED_LOCATION_COUNT
1396 || LINEMAP_SYSP (map1)))
1398 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1399 resolved_loc = linemap_resolve_location (set, loc,
1400 LRK_SPELLING_LOCATION,
1401 &map1);
1404 if (map != NULL)
1405 *map = map0;
1406 return loc;
1409 /* Expand source code location LOC and return a user readable source
1410 code location. LOC must be a spelling (non-virtual) location. If
1411 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1412 location is returned. */
1414 expanded_location
1415 linemap_expand_location (struct line_maps *set,
1416 const struct line_map *map,
1417 source_location loc)
1420 expanded_location xloc;
1422 memset (&xloc, 0, sizeof (xloc));
1423 if (IS_ADHOC_LOC (loc))
1425 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1426 xloc.data
1427 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1430 if (loc < RESERVED_LOCATION_COUNT)
1431 /* The location for this token wasn't generated from a line map.
1432 It was probably a location for a builtin token, chosen by some
1433 client code. Let's not try to expand the location in that
1434 case. */;
1435 else if (map == NULL)
1436 /* We shouldn't be getting a NULL map with a location that is not
1437 reserved by the client code. */
1438 abort ();
1439 else
1441 /* MAP must be an ordinary map and LOC must be non-virtual,
1442 encoded into this map, obviously; the accessors used on MAP
1443 below ensure it is ordinary. Let's just assert the
1444 non-virtualness of LOC here. */
1445 if (linemap_location_from_macro_expansion_p (set, loc))
1446 abort ();
1448 xloc.file = LINEMAP_FILE (map);
1449 xloc.line = SOURCE_LINE (map, loc);
1450 xloc.column = SOURCE_COLUMN (map, loc);
1451 xloc.sysp = LINEMAP_SYSP (map) != 0;
1454 return xloc;
1458 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1459 is NULL, use stderr. IS_MACRO is true if the caller wants to
1460 dump a macro map, false otherwise. */
1462 void
1463 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1465 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1466 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1467 "LC_ENTER_MACRO" };
1468 const char *reason;
1469 struct line_map *map;
1471 if (stream == NULL)
1472 stream = stderr;
1474 if (!is_macro)
1475 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1476 else
1477 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1479 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1481 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1482 ix, (void *) map, map->start_location, reason,
1483 (!is_macro && ORDINARY_MAP_IN_SYSTEM_HEADER_P (map)) ? "yes" : "no");
1484 if (!is_macro)
1486 unsigned includer_ix;
1487 struct line_map *includer_map;
1489 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (map);
1490 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1491 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1492 : NULL;
1494 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (map),
1495 ORDINARY_MAP_STARTING_LINE_NUMBER (map));
1496 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1497 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1499 else
1500 fprintf (stream, "Macro: %s (%u tokens)\n",
1501 linemap_map_get_macro_name (map),
1502 MACRO_MAP_NUM_MACRO_TOKENS (map));
1504 fprintf (stream, "\n");
1508 /* Dump debugging information about source location LOC into the file
1509 stream STREAM. SET is the line map set LOC comes from. */
1511 void
1512 linemap_dump_location (struct line_maps *set,
1513 source_location loc,
1514 FILE *stream)
1516 const struct line_map *map;
1517 source_location location;
1518 const char *path = "", *from = "";
1519 int l = -1, c = -1, s = -1, e = -1;
1521 if (IS_ADHOC_LOC (loc))
1522 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1524 if (loc == 0)
1525 return;
1527 location =
1528 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1530 if (map == NULL)
1531 /* Only reserved locations can be tolerated in this case. */
1532 linemap_assert (location < RESERVED_LOCATION_COUNT);
1533 else
1535 path = LINEMAP_FILE (map);
1536 l = SOURCE_LINE (map, location);
1537 c = SOURCE_COLUMN (map, location);
1538 s = LINEMAP_SYSP (map) != 0;
1539 e = location != loc;
1540 if (e)
1541 from = "N/A";
1542 else
1543 from = (INCLUDED_FROM (set, map))
1544 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1545 : "<NULL>";
1548 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1549 E: macro expansion?, LOC: original location, R: resolved location */
1550 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1551 path, from, l, c, s, (void*)map, e, loc, location);
1554 /* Return the highest location emitted for a given file for which
1555 there is a line map in SET. FILE_NAME is the file name to
1556 consider. If the function returns TRUE, *LOC is set to the highest
1557 location emitted for that file. */
1559 bool
1560 linemap_get_file_highest_location (struct line_maps *set,
1561 const char *file_name,
1562 source_location *loc)
1564 /* If the set is empty or no ordinary map has been created then
1565 there is no file to look for ... */
1566 if (set == NULL || set->info_ordinary.used == 0)
1567 return false;
1569 /* Now look for the last ordinary map created for FILE_NAME. */
1570 int i;
1571 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1573 const char *fname = set->info_ordinary.maps[i].d.ordinary.to_file;
1574 if (fname && !filename_cmp (fname, file_name))
1575 break;
1578 if (i < 0)
1579 return false;
1581 /* The highest location for a given map is either the starting
1582 location of the next map minus one, or -- if the map is the
1583 latest one -- the highest location of the set. */
1584 source_location result;
1585 if (i == (int) set->info_ordinary.used - 1)
1586 result = set->highest_location;
1587 else
1588 result = set->info_ordinary.maps[i + 1].start_location - 1;
1590 *loc = result;
1591 return true;
1594 /* Compute and return statistics about the memory consumption of some
1595 parts of the line table SET. */
1597 void
1598 linemap_get_statistics (struct line_maps *set,
1599 struct linemap_stats *s)
1601 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1602 macro_maps_allocated_size, macro_maps_used_size,
1603 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1605 struct line_map *cur_map;
1607 ordinary_maps_allocated_size =
1608 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map);
1610 ordinary_maps_used_size =
1611 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map);
1613 macro_maps_allocated_size =
1614 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map);
1616 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1617 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1618 ++cur_map)
1620 unsigned i;
1622 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1624 macro_maps_locations_size +=
1625 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1627 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1629 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1630 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1631 duplicated_macro_maps_locations_size +=
1632 sizeof (source_location);
1636 macro_maps_used_size =
1637 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map);
1639 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1640 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1641 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1642 s->ordinary_maps_used_size = ordinary_maps_used_size;
1643 s->num_expanded_macros = num_expanded_macros_counter;
1644 s->num_macro_tokens = num_macro_tokens_counter;
1645 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1646 s->macro_maps_allocated_size = macro_maps_allocated_size;
1647 s->macro_maps_locations_size = macro_maps_locations_size;
1648 s->macro_maps_used_size = macro_maps_used_size;
1649 s->duplicated_macro_maps_locations_size =
1650 duplicated_macro_maps_locations_size;
1654 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1655 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1656 specifies how many macro maps to dump. */
1658 void
1659 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1660 unsigned int num_macro)
1662 unsigned int i;
1664 if (set == NULL)
1665 return;
1667 if (stream == NULL)
1668 stream = stderr;
1670 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1671 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1672 fprintf (stream, "Include stack depth: %d\n", set->depth);
1673 fprintf (stream, "Highest location: %u\n", set->highest_location);
1675 if (num_ordinary)
1677 fprintf (stream, "\nOrdinary line maps\n");
1678 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1679 linemap_dump (stream, set, i, false);
1680 fprintf (stream, "\n");
1683 if (num_macro)
1685 fprintf (stream, "\nMacro line maps\n");
1686 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1687 linemap_dump (stream, set, i, true);
1688 fprintf (stream, "\n");