2013-01-03 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / libcpp / line-map.c
blob49b94871baaf0c34d7c4693cf90098d4c0e41a7a
1 /* Map logical line numbers to (source file, line number) pairs.
2 Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
8 later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding! */
23 #include "config.h"
24 #include "system.h"
25 #include "line-map.h"
26 #include "cpplib.h"
27 #include "internal.h"
28 #include "hashtab.h"
30 static void trace_include (const struct line_maps *, const struct line_map *);
31 static const struct line_map * linemap_ordinary_map_lookup (struct line_maps *,
32 source_location);
33 static const struct line_map* linemap_macro_map_lookup (struct line_maps *,
34 source_location);
35 static source_location linemap_macro_map_loc_to_def_point
36 (const struct line_map*, source_location);
37 static source_location linemap_macro_map_loc_unwind_toward_spelling
38 (const struct line_map*, source_location);
39 static source_location linemap_macro_map_loc_to_exp_point
40 (const struct line_map*, source_location);
41 static source_location linemap_macro_loc_to_spelling_point
42 (struct line_maps *, source_location, const struct line_map **);
43 static source_location linemap_macro_loc_to_def_point (struct line_maps *,
44 source_location,
45 const struct line_map **);
46 static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
47 source_location,
48 const struct line_map **);
50 /* Counters defined in macro.c. */
51 extern unsigned num_expanded_macros_counter;
52 extern unsigned num_macro_tokens_counter;
54 /* Hash function for location_adhoc_data hashtable. */
56 static hashval_t
57 location_adhoc_data_hash (const void *l)
59 const struct location_adhoc_data *lb =
60 (const struct location_adhoc_data *) l;
61 return (hashval_t) lb->locus + (size_t) lb->data;
64 /* Compare function for location_adhoc_data hashtable. */
66 static int
67 location_adhoc_data_eq (const void *l1, const void *l2)
69 const struct location_adhoc_data *lb1 =
70 (const struct location_adhoc_data *) l1;
71 const struct location_adhoc_data *lb2 =
72 (const struct location_adhoc_data *) l2;
73 return lb1->locus == lb2->locus && lb1->data == lb2->data;
76 /* Update the hashtable when location_adhoc_data is reallocated. */
78 static int
79 location_adhoc_data_update (void **slot, void *data)
81 *((char **) slot) += *((long long *) data);
82 return 1;
85 /* Rebuild the hash table from the location adhoc data. */
87 void
88 rebuild_location_adhoc_htab (struct line_maps *set)
90 unsigned i;
91 set->location_adhoc_data_map.htab =
92 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
93 for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
94 htab_find_slot (set->location_adhoc_data_map.htab,
95 set->location_adhoc_data_map.data + i, INSERT);
98 /* Combine LOCUS and DATA to a combined adhoc loc. */
100 source_location
101 get_combined_adhoc_loc (struct line_maps *set,
102 source_location locus, void *data)
104 struct location_adhoc_data lb;
105 struct location_adhoc_data **slot;
107 linemap_assert (data);
109 if (IS_ADHOC_LOC (locus))
110 locus =
111 set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
112 if (locus == 0 && data == NULL)
113 return 0;
114 lb.locus = locus;
115 lb.data = data;
116 slot = (struct location_adhoc_data **)
117 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
118 if (*slot == NULL)
120 if (set->location_adhoc_data_map.curr_loc >=
121 set->location_adhoc_data_map.allocated)
123 char *orig_data = (char *) set->location_adhoc_data_map.data;
124 long long offset;
125 /* Cast away extern "C" from the type of xrealloc. */
126 line_map_realloc reallocator = (set->reallocator
127 ? set->reallocator
128 : (line_map_realloc) xrealloc);
130 if (set->location_adhoc_data_map.allocated == 0)
131 set->location_adhoc_data_map.allocated = 128;
132 else
133 set->location_adhoc_data_map.allocated *= 2;
134 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
135 reallocator (set->location_adhoc_data_map.data,
136 set->location_adhoc_data_map.allocated
137 * sizeof (struct location_adhoc_data));
138 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
139 if (set->location_adhoc_data_map.allocated > 128)
140 htab_traverse (set->location_adhoc_data_map.htab,
141 location_adhoc_data_update, &offset);
143 *slot = set->location_adhoc_data_map.data
144 + set->location_adhoc_data_map.curr_loc;
145 set->location_adhoc_data_map.data[
146 set->location_adhoc_data_map.curr_loc++] = lb;
148 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
151 /* Return the data for the adhoc loc. */
153 void *
154 get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
156 linemap_assert (IS_ADHOC_LOC (loc));
157 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
160 /* Return the location for the adhoc loc. */
162 source_location
163 get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
165 linemap_assert (IS_ADHOC_LOC (loc));
166 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
169 /* Finalize the location_adhoc_data structure. */
170 void
171 location_adhoc_data_fini (struct line_maps *set)
173 htab_delete (set->location_adhoc_data_map.htab);
176 /* Initialize a line map set. */
178 void
179 linemap_init (struct line_maps *set)
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);
188 /* Check for and warn about line_maps entered but not exited. */
190 void
191 linemap_check_files_exited (struct line_maps *set)
193 struct line_map *map;
194 /* Depending upon whether we are handling preprocessed input or
195 not, this can be a user error or an ICE. */
196 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
197 ! MAIN_FILE_P (map);
198 map = INCLUDED_FROM (set, map))
199 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
200 ORDINARY_MAP_FILE_NAME (map));
203 /* Create a new line map in the line map set SET, and return it.
204 REASON is the reason of creating the map. It determines the type
205 of map created (ordinary or macro map). Note that ordinary maps and
206 macro maps are allocated in different memory location. */
208 static struct line_map *
209 new_linemap (struct line_maps *set,
210 enum lc_reason reason)
212 /* Depending on this variable, a macro map would be allocated in a
213 different memory location than an ordinary map. */
214 bool macro_map_p = (reason == LC_ENTER_MACRO);
215 struct line_map *result;
217 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
219 /* We ran out of allocated line maps. Let's allocate more. */
220 unsigned alloc_size;
222 /* Cast away extern "C" from the type of xrealloc. */
223 line_map_realloc reallocator = (set->reallocator
224 ? set->reallocator
225 : (line_map_realloc) xrealloc);
226 line_map_round_alloc_size_func round_alloc_size =
227 set->round_alloc_size;
229 /* We are going to execute some dance to try to reduce the
230 overhead of the memory allocator, in case we are using the
231 ggc-page.c one.
233 The actual size of memory we are going to get back from the
234 allocator is the smallest power of 2 that is greater than the
235 size we requested. So let's consider that size then. */
237 alloc_size =
238 (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
239 * sizeof (struct line_map);
241 /* Get the actual size of memory that is going to be allocated
242 by the allocator. */
243 alloc_size = round_alloc_size (alloc_size);
245 /* Now alloc_size contains the exact memory size we would get if
246 we have asked for the initial alloc_size amount of memory.
247 Let's get back to the number of macro map that amounts
248 to. */
249 LINEMAPS_ALLOCATED (set, macro_map_p) =
250 alloc_size / (sizeof (struct line_map));
252 /* And now let's really do the re-allocation. */
253 LINEMAPS_MAPS (set, macro_map_p) =
254 (struct line_map *) (*reallocator)
255 (LINEMAPS_MAPS (set, macro_map_p),
256 (LINEMAPS_ALLOCATED (set, macro_map_p)
257 * sizeof (struct line_map)));
259 result =
260 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
261 memset (result, 0,
262 ((LINEMAPS_ALLOCATED (set, macro_map_p)
263 - LINEMAPS_USED (set, macro_map_p))
264 * sizeof (struct line_map)));
266 else
267 result =
268 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
270 LINEMAPS_USED (set, macro_map_p)++;
272 result->reason = reason;
273 return result;
276 /* Add a mapping of logical source line to physical source file and
277 line number.
279 The text pointed to by TO_FILE must have a lifetime
280 at least as long as the final call to lookup_line (). An empty
281 TO_FILE means standard input. If reason is LC_LEAVE, and
282 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
283 natural values considering the file we are returning to.
285 FROM_LINE should be monotonic increasing across calls to this
286 function. A call to this function can relocate the previous set of
287 maps, so any stored line_map pointers should not be used. */
289 const struct line_map *
290 linemap_add (struct line_maps *set, enum lc_reason reason,
291 unsigned int sysp, const char *to_file, linenum_type to_line)
293 struct line_map *map;
294 source_location start_location = set->highest_location + 1;
296 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
297 && (start_location
298 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
300 /* When we enter the file for the first time reason cannot be
301 LC_RENAME. */
302 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
304 /* If we are leaving the main file, return a NULL map. */
305 if (reason == LC_LEAVE
306 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
307 && to_file == NULL)
309 set->depth--;
310 return NULL;
313 map = new_linemap (set, reason);
315 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
316 to_file = "<stdin>";
318 if (reason == LC_RENAME_VERBATIM)
319 reason = LC_RENAME;
321 if (reason == LC_LEAVE)
323 /* When we are just leaving an "included" file, and jump to the next
324 location inside the "includer" right after the #include
325 "included", this variable points the map in use right before the
326 #include "included", inside the same "includer" file. */
327 struct line_map *from;
328 bool error;
330 if (MAIN_FILE_P (map - 1))
332 /* So this _should_ means we are leaving the main file --
333 effectively ending the compilation unit. But to_file not
334 being NULL means the caller thinks we are leaving to
335 another file. This is an erroneous behaviour but we'll
336 try to recover from it. Let's pretend we are not leaving
337 the main file. */
338 error = true;
339 reason = LC_RENAME;
340 from = map - 1;
342 else
344 /* (MAP - 1) points to the map we are leaving. The
345 map from which (MAP - 1) got included should be the map
346 that comes right before MAP in the same file. */
347 from = INCLUDED_FROM (set, map - 1);
348 error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
349 to_file);
352 /* Depending upon whether we are handling preprocessed input or
353 not, this can be a user error or an ICE. */
354 if (error)
355 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
356 to_file);
358 /* A TO_FILE of NULL is special - we use the natural values. */
359 if (error || to_file == NULL)
361 to_file = ORDINARY_MAP_FILE_NAME (from);
362 to_line = SOURCE_LINE (from, from[1].start_location);
363 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
367 linemap_assert (reason != LC_ENTER_MACRO);
368 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map) = sysp;
369 MAP_START_LOCATION (map) = start_location;
370 ORDINARY_MAP_FILE_NAME (map) = to_file;
371 ORDINARY_MAP_STARTING_LINE_NUMBER (map) = to_line;
372 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
373 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = 0;
374 set->highest_location = start_location;
375 set->highest_line = start_location;
376 set->max_column_hint = 0;
378 if (reason == LC_ENTER)
380 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
381 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
382 set->depth++;
383 if (set->trace_includes)
384 trace_include (set, map);
386 else if (reason == LC_RENAME)
387 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
388 ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
389 else if (reason == LC_LEAVE)
391 set->depth--;
392 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
393 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
396 return map;
399 /* Returns TRUE if the line table set tracks token locations across
400 macro expansion, FALSE otherwise. */
402 bool
403 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
405 return LINEMAPS_MACRO_MAPS (set) != NULL;
408 /* Create a macro map. A macro map encodes source locations of tokens
409 that are part of a macro replacement-list, at a macro expansion
410 point. See the extensive comments of struct line_map and struct
411 line_map_macro, in line-map.h.
413 This map shall be created when the macro is expanded. The map
414 encodes the source location of the expansion point of the macro as
415 well as the "original" source location of each token that is part
416 of the macro replacement-list. If a macro is defined but never
417 expanded, it has no macro map. SET is the set of maps the macro
418 map should be part of. MACRO_NODE is the macro which the new macro
419 map should encode source locations for. EXPANSION is the location
420 of the expansion point of MACRO. For function-like macros
421 invocations, it's best to make it point to the closing parenthesis
422 of the macro, rather than the the location of the first character
423 of the macro. NUM_TOKENS is the number of tokens that are part of
424 the replacement-list of MACRO.
426 Note that when we run out of the integer space available for source
427 locations, this function returns NULL. In that case, callers of
428 this function cannot encode {line,column} pairs into locations of
429 macro tokens anymore. */
431 const struct line_map *
432 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
433 source_location expansion, unsigned int num_tokens)
435 struct line_map *map;
436 source_location start_location;
437 /* Cast away extern "C" from the type of xrealloc. */
438 line_map_realloc reallocator = (set->reallocator
439 ? set->reallocator
440 : (line_map_realloc) xrealloc);
442 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
444 if (start_location <= set->highest_line
445 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
446 /* We ran out of macro map space. */
447 return NULL;
449 map = new_linemap (set, LC_ENTER_MACRO);
451 MAP_START_LOCATION (map) = start_location;
452 MACRO_MAP_MACRO (map) = macro_node;
453 MACRO_MAP_NUM_MACRO_TOKENS (map) = num_tokens;
454 MACRO_MAP_LOCATIONS (map)
455 = (source_location*) reallocator (NULL,
456 2 * num_tokens
457 * sizeof (source_location));
458 MACRO_MAP_EXPANSION_POINT_LOCATION (map) = expansion;
459 memset (MACRO_MAP_LOCATIONS (map), 0,
460 num_tokens * sizeof (source_location));
462 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
464 return map;
467 /* Create and return a virtual location for a token that is part of a
468 macro expansion-list at a macro expansion point. See the comment
469 inside struct line_map_macro to see what an expansion-list exactly
472 A call to this function must come after a call to
473 linemap_enter_macro.
475 MAP is the map into which the source location is created. TOKEN_NO
476 is the index of the token in the macro replacement-list, starting
477 at number 0.
479 ORIG_LOC is the location of the token outside of this macro
480 expansion. If the token comes originally from the macro
481 definition, it is the locus in the macro definition; otherwise it
482 is a location in the context of the caller of this macro expansion
483 (which is a virtual location or a source location if the caller is
484 itself a macro expansion or not).
486 MACRO_DEFINITION_LOC is the location in the macro definition,
487 either of the token itself or of a macro parameter that it
488 replaces. */
490 source_location
491 linemap_add_macro_token (const struct line_map *map,
492 unsigned int token_no,
493 source_location orig_loc,
494 source_location orig_parm_replacement_loc)
496 source_location result;
498 linemap_assert (linemap_macro_expansion_map_p (map));
499 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
501 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
502 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
504 result = MAP_START_LOCATION (map) + token_no;
505 return result;
508 /* Return a source_location for the start (i.e. column==0) of
509 (physical) line TO_LINE in the current source file (as in the
510 most recent linemap_add). MAX_COLUMN_HINT is the highest column
511 number we expect to use in this line (but it does not change
512 the highest_location). */
514 source_location
515 linemap_line_start (struct line_maps *set, linenum_type to_line,
516 unsigned int max_column_hint)
518 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
519 source_location highest = set->highest_location;
520 source_location r;
521 linenum_type last_line =
522 SOURCE_LINE (map, set->highest_line);
523 int line_delta = to_line - last_line;
524 bool add_map = false;
526 if (line_delta < 0
527 || (line_delta > 10
528 && line_delta * ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) > 1000)
529 || (max_column_hint >= (1U << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)))
530 || (max_column_hint <= 80
531 && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10))
533 add_map = true;
535 else
536 max_column_hint = set->max_column_hint;
537 if (add_map)
539 int column_bits;
540 if (max_column_hint > 100000 || highest > 0x60000000)
542 /* If the column number is ridiculous or we've allocated a huge
543 number of source_locations, give up on column numbers. */
544 max_column_hint = 0;
545 if (highest >0x70000000)
546 return 0;
547 column_bits = 0;
549 else
551 column_bits = 7;
552 while (max_column_hint >= (1U << column_bits))
553 column_bits++;
554 max_column_hint = 1U << column_bits;
556 /* Allocate the new line_map. However, if the current map only has a
557 single line we can sometimes just increase its column_bits instead. */
558 if (line_delta < 0
559 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
560 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
561 map = (struct line_map *) linemap_add (set, LC_RENAME,
562 ORDINARY_MAP_IN_SYSTEM_HEADER_P
563 (map),
564 ORDINARY_MAP_FILE_NAME (map),
565 to_line);
566 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = column_bits;
567 r = (MAP_START_LOCATION (map)
568 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
569 << column_bits));
571 else
572 r = highest - SOURCE_COLUMN (map, highest)
573 + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
575 /* Locations of ordinary tokens are always lower than locations of
576 macro tokens. */
577 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
578 return 0;
580 set->highest_line = r;
581 if (r > set->highest_location)
582 set->highest_location = r;
583 set->max_column_hint = max_column_hint;
584 return r;
587 /* Encode and return a source_location from a column number. The
588 source line considered is the last source line used to call
589 linemap_line_start, i.e, the last source line which a location was
590 encoded from. */
592 source_location
593 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
595 source_location r = set->highest_line;
597 linemap_assert
598 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
600 if (to_column >= set->max_column_hint)
602 if (r >= 0xC000000 || to_column > 100000)
604 /* Running low on source_locations - disable column numbers. */
605 return r;
607 else
609 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
610 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
613 r = r + to_column;
614 if (r >= set->highest_location)
615 set->highest_location = r;
616 return r;
619 /* Encode and return a source location from a given line and
620 column. */
622 source_location
623 linemap_position_for_line_and_column (struct line_map *map,
624 linenum_type line,
625 unsigned column)
627 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (map) <= line);
629 return (MAP_START_LOCATION (map)
630 + ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
631 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
632 + (column & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1)));
635 /* Given a virtual source location yielded by a map (either an
636 ordinary or a macro map), returns that map. */
638 const struct line_map*
639 linemap_lookup (struct line_maps *set, source_location line)
641 if (IS_ADHOC_LOC (line))
642 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
643 if (linemap_location_from_macro_expansion_p (set, line))
644 return linemap_macro_map_lookup (set, line);
645 return linemap_ordinary_map_lookup (set, line);
648 /* Given a source location yielded by an ordinary map, returns that
649 map. Since the set is built chronologically, the logical lines are
650 monotonic increasing, and so the list is sorted and we can use a
651 binary search. */
653 static const struct line_map *
654 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
656 unsigned int md, mn, mx;
657 const struct line_map *cached, *result;
659 if (IS_ADHOC_LOC (line))
660 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
662 if (set == NULL || line < RESERVED_LOCATION_COUNT)
663 return NULL;
665 mn = LINEMAPS_ORDINARY_CACHE (set);
666 mx = LINEMAPS_ORDINARY_USED (set);
668 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
669 /* We should get a segfault if no line_maps have been added yet. */
670 if (line >= MAP_START_LOCATION (cached))
672 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
673 return cached;
675 else
677 mx = mn;
678 mn = 0;
681 while (mx - mn > 1)
683 md = (mn + mx) / 2;
684 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
685 mx = md;
686 else
687 mn = md;
690 LINEMAPS_ORDINARY_CACHE (set) = mn;
691 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
692 linemap_assert (line >= MAP_START_LOCATION (result));
693 return result;
696 /* Given a source location yielded by a macro map, returns that map.
697 Since the set is built chronologically, the logical lines are
698 monotonic decreasing, and so the list is sorted and we can use a
699 binary search. */
701 static const struct line_map*
702 linemap_macro_map_lookup (struct line_maps *set, source_location line)
704 unsigned int md, mn, mx;
705 const struct line_map *cached, *result;
707 if (IS_ADHOC_LOC (line))
708 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
710 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
712 if (set == NULL)
713 return NULL;
715 mn = LINEMAPS_MACRO_CACHE (set);
716 mx = LINEMAPS_MACRO_USED (set);
717 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
719 if (line >= MAP_START_LOCATION (cached))
721 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
722 return cached;
723 mx = mn - 1;
724 mn = 0;
727 while (mn < mx)
729 md = (mx + mn) / 2;
730 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
731 mn = md + 1;
732 else
733 mx = md;
736 LINEMAPS_MACRO_CACHE (set) = mx;
737 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
738 linemap_assert (MAP_START_LOCATION (result) <= line);
740 return result;
743 /* Return TRUE if MAP encodes locations coming from a macro
744 replacement-list at macro expansion point. */
746 bool
747 linemap_macro_expansion_map_p (const struct line_map *map)
749 if (!map)
750 return false;
751 return (map->reason == LC_ENTER_MACRO);
754 /* If LOCATION is the locus of a token in a replacement-list of a
755 macro expansion return the location of the macro expansion point.
757 Read the comments of struct line_map and struct line_map_macro in
758 line-map.h to understand what a macro expansion point is. */
760 static source_location
761 linemap_macro_map_loc_to_exp_point (const struct line_map *map,
762 source_location location ATTRIBUTE_UNUSED)
764 linemap_assert (linemap_macro_expansion_map_p (map)
765 && location >= MAP_START_LOCATION (map));
767 /* Make sure LOCATION is correct. */
768 linemap_assert ((location - MAP_START_LOCATION (map))
769 < MACRO_MAP_NUM_MACRO_TOKENS (map));
771 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
774 /* If LOCATION is the source location of a token that belongs to a
775 macro replacement-list -- as part of a macro expansion -- then
776 return the location of the token at the definition point of the
777 macro. Otherwise, return LOCATION. SET is the set of maps
778 location come from. ORIGINAL_MAP is an output parm. If non NULL,
779 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
780 returned location comes from. */
782 source_location
783 linemap_macro_map_loc_to_def_point (const struct line_map *map,
784 source_location location)
786 unsigned token_no;
788 linemap_assert (linemap_macro_expansion_map_p (map)
789 && location >= MAP_START_LOCATION (map));
790 linemap_assert (location >= RESERVED_LOCATION_COUNT);
792 token_no = location - MAP_START_LOCATION (map);
793 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
795 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
797 return location;
800 /* If LOCATION is the locus of a token that is an argument of a
801 function-like macro M and appears in the expansion of M, return the
802 locus of that argument in the context of the caller of M.
804 In other words, this returns the xI location presented in the
805 comments of line_map_macro above. */
806 source_location
807 linemap_macro_map_loc_unwind_toward_spelling (const struct line_map* map,
808 source_location location)
810 unsigned token_no;
812 linemap_assert (linemap_macro_expansion_map_p (map)
813 && location >= MAP_START_LOCATION (map));
814 linemap_assert (location >= RESERVED_LOCATION_COUNT);
816 token_no = location - MAP_START_LOCATION (map);
817 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
819 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
821 return location;
824 /* Return the source line number corresponding to source location
825 LOCATION. SET is the line map set LOCATION comes from. If
826 LOCATION is the source location of token that is part of the
827 replacement-list of a macro expansion return the line number of the
828 macro expansion point. */
831 linemap_get_expansion_line (struct line_maps *set,
832 source_location location)
834 const struct line_map *map = NULL;
836 if (IS_ADHOC_LOC (location))
837 location = set->location_adhoc_data_map.data[
838 location & MAX_SOURCE_LOCATION].locus;
840 if (location < RESERVED_LOCATION_COUNT)
841 return 0;
843 location =
844 linemap_macro_loc_to_exp_point (set, location, &map);
846 return SOURCE_LINE (map, location);
849 /* Return the path of the file corresponding to source code location
850 LOCATION.
852 If LOCATION is the source location of token that is part of the
853 replacement-list of a macro expansion return the file path of the
854 macro expansion point.
856 SET is the line map set LOCATION comes from. */
858 const char*
859 linemap_get_expansion_filename (struct line_maps *set,
860 source_location location)
862 const struct line_map *map = NULL;
864 if (IS_ADHOC_LOC (location))
865 location = set->location_adhoc_data_map.data[
866 location & MAX_SOURCE_LOCATION].locus;
868 if (location < RESERVED_LOCATION_COUNT)
869 return NULL;
871 location =
872 linemap_macro_loc_to_exp_point (set, location, &map);
874 return LINEMAP_FILE (map);
877 /* Return the name of the macro associated to MACRO_MAP. */
879 const char*
880 linemap_map_get_macro_name (const struct line_map* macro_map)
882 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
883 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
886 /* Return a positive value if LOCATION is the locus of a token that is
887 located in a system header, O otherwise. It returns 1 if LOCATION
888 is the locus of a token that is located in a system header, and 2
889 if LOCATION is the locus of a token located in a C system header
890 that therefore needs to be extern "C" protected in C++.
892 Note that this function returns 1 if LOCATION belongs to a token
893 that is part of a macro replacement-list defined in a system
894 header, but expanded in a non-system file. */
897 linemap_location_in_system_header_p (struct line_maps *set,
898 source_location location)
900 const struct line_map *map = NULL;
902 if (IS_ADHOC_LOC (location))
903 location = set->location_adhoc_data_map.data[
904 location & MAX_SOURCE_LOCATION].locus;
906 if (location < RESERVED_LOCATION_COUNT)
907 return false;
909 /* Let's look at where the token for LOCATION comes from. */
910 while (true)
912 map = linemap_lookup (set, location);
913 if (map != NULL)
915 if (!linemap_macro_expansion_map_p (map))
916 /* It's a normal token. */
917 return LINEMAP_SYSP (map);
918 else
920 /* It's a token resulting from a macro expansion. */
921 source_location loc =
922 linemap_macro_map_loc_unwind_toward_spelling (map, location);
923 if (loc < RESERVED_LOCATION_COUNT)
924 /* This token might come from a built-in macro. Let's
925 look at where that macro got expanded. */
926 location = linemap_macro_map_loc_to_exp_point (map, location);
927 else
928 location = loc;
931 else
932 break;
934 return false;
937 /* Return TRUE if LOCATION is a source code location of a token coming
938 from a macro replacement-list at a macro expansion point, FALSE
939 otherwise. */
941 bool
942 linemap_location_from_macro_expansion_p (struct line_maps *set,
943 source_location location)
945 if (IS_ADHOC_LOC (location))
946 location = set->location_adhoc_data_map.data[
947 location & MAX_SOURCE_LOCATION].locus;
949 linemap_assert (location <= MAX_SOURCE_LOCATION
950 && (set->highest_location
951 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
952 if (set == NULL)
953 return false;
954 return (location > set->highest_location);
957 /* Given two virtual locations *LOC0 and *LOC1, return the first
958 common macro map in their macro expansion histories. Return NULL
959 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
960 virtual location of the token inside the resulting macro. */
962 static const struct line_map*
963 first_map_in_common_1 (struct line_maps *set,
964 source_location *loc0,
965 source_location *loc1)
967 source_location l0 = *loc0, l1 = *loc1;
968 const struct line_map *map0 = linemap_lookup (set, l0),
969 *map1 = linemap_lookup (set, l1);
971 while (linemap_macro_expansion_map_p (map0)
972 && linemap_macro_expansion_map_p (map1)
973 && (map0 != map1))
975 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
977 l0 = linemap_macro_map_loc_to_exp_point (map0, l0);
978 map0 = linemap_lookup (set, l0);
980 else
982 l1 = linemap_macro_map_loc_to_exp_point (map1, l1);
983 map1 = linemap_lookup (set, l1);
987 if (map0 == map1)
989 *loc0 = l0;
990 *loc1 = l1;
991 return map0;
993 return NULL;
996 /* Given two virtual locations LOC0 and LOC1, return the first common
997 macro map in their macro expansion histories. Return NULL if no
998 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
999 virtual location of the token inside the resulting macro, upon
1000 return of a non-NULL result. */
1002 static const struct line_map*
1003 first_map_in_common (struct line_maps *set,
1004 source_location loc0,
1005 source_location loc1,
1006 source_location *res_loc0,
1007 source_location *res_loc1)
1009 *res_loc0 = loc0;
1010 *res_loc1 = loc1;
1012 return first_map_in_common_1 (set, res_loc0, res_loc1);
1015 /* Return a positive value if PRE denotes the location of a token that
1016 comes before the token of POST, 0 if PRE denotes the location of
1017 the same token as the token for POST, and a negative value
1018 otherwise. */
1021 linemap_compare_locations (struct line_maps *set,
1022 source_location pre,
1023 source_location post)
1025 bool pre_virtual_p, post_virtual_p;
1026 source_location l0 = pre, l1 = post;
1028 if (l0 == l1)
1029 return 0;
1031 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1032 l0 = linemap_resolve_location (set, l0,
1033 LRK_MACRO_EXPANSION_POINT,
1034 NULL);
1036 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1037 l1 = linemap_resolve_location (set, l1,
1038 LRK_MACRO_EXPANSION_POINT,
1039 NULL);
1041 if (l0 == l1
1042 && pre_virtual_p
1043 && post_virtual_p)
1045 /* So pre and post represent two tokens that are present in a
1046 same macro expansion. Let's see if the token for pre was
1047 before the token for post in that expansion. */
1048 unsigned i0, i1;
1049 const struct line_map *map =
1050 first_map_in_common (set, pre, post, &l0, &l1);
1052 if (map == NULL)
1053 /* This should not be possible. */
1054 abort ();
1056 i0 = l0 - MAP_START_LOCATION (map);
1057 i1 = l1 - MAP_START_LOCATION (map);
1058 return i1 - i0;
1061 return l1 - l0;
1064 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1066 static void
1067 trace_include (const struct line_maps *set, const struct line_map *map)
1069 unsigned int i = set->depth;
1071 while (--i)
1072 putc ('.', stderr);
1074 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1077 /* Return the spelling location of the token wherever it comes from,
1078 whether part of a macro definition or not.
1080 This is a subroutine for linemap_resolve_location. */
1082 static source_location
1083 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1084 source_location location,
1085 const struct line_map **original_map)
1087 struct line_map *map;
1089 if (IS_ADHOC_LOC (location))
1090 location = set->location_adhoc_data_map.data[
1091 location & MAX_SOURCE_LOCATION].locus;
1093 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1095 while (true)
1097 map = (struct line_map*) linemap_lookup (set, location);
1098 if (!linemap_macro_expansion_map_p (map))
1099 break;
1101 location =
1102 linemap_macro_map_loc_unwind_toward_spelling (map, location);
1105 if (original_map)
1106 *original_map = map;
1107 return location;
1110 /* If LOCATION is the source location of a token that belongs to a
1111 macro replacement-list -- as part of a macro expansion -- then
1112 return the location of the token at the definition point of the
1113 macro. Otherwise, return LOCATION. SET is the set of maps
1114 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1115 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1116 returned location comes from.
1118 This is a subroutine of linemap_resolve_location. */
1120 static source_location
1121 linemap_macro_loc_to_def_point (struct line_maps *set,
1122 source_location location,
1123 const struct line_map **original_map)
1125 struct line_map *map;
1127 if (IS_ADHOC_LOC (location))
1128 location = set->location_adhoc_data_map.data[
1129 location & MAX_SOURCE_LOCATION].locus;
1131 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1133 while (true)
1135 map = (struct line_map*) linemap_lookup (set, location);
1136 if (!linemap_macro_expansion_map_p (map))
1137 break;
1139 location =
1140 linemap_macro_map_loc_to_def_point (map, location);
1143 if (original_map)
1144 *original_map = map;
1145 return location;
1148 /* If LOCATION is the source location of a token that belongs to a
1149 macro replacement-list -- at a macro expansion point -- then return
1150 the location of the topmost expansion point of the macro. We say
1151 topmost because if we are in the context of a nested macro
1152 expansion, the function returns the source location of the first
1153 macro expansion that triggered the nested expansions.
1155 Otherwise, return LOCATION. SET is the set of maps location come
1156 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1157 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1158 location comes from.
1160 This is a subroutine of linemap_resolve_location. */
1162 static source_location
1163 linemap_macro_loc_to_exp_point (struct line_maps *set,
1164 source_location location,
1165 const struct line_map **original_map)
1167 struct line_map *map;
1169 if (IS_ADHOC_LOC (location))
1170 location = set->location_adhoc_data_map.data[
1171 location & MAX_SOURCE_LOCATION].locus;
1173 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1175 while (true)
1177 map = (struct line_map*) linemap_lookup (set, location);
1178 if (!linemap_macro_expansion_map_p (map))
1179 break;
1180 location = linemap_macro_map_loc_to_exp_point (map, location);
1183 if (original_map)
1184 *original_map = map;
1185 return location;
1188 /* Resolve a virtual location into either a spelling location, an
1189 expansion point location or a token argument replacement point
1190 location. Return the map that encodes the virtual location as well
1191 as the resolved location.
1193 If LOC is *NOT* the location of a token resulting from the
1194 expansion of a macro, then the parameter LRK (which stands for
1195 Location Resolution Kind) is ignored and the resulting location
1196 just equals the one given in argument.
1198 Now if LOC *IS* the location of a token resulting from the
1199 expansion of a macro, this is what happens.
1201 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1202 -------------------------------
1204 The virtual location is resolved to the first macro expansion point
1205 that led to this macro expansion.
1207 * If LRK is set to LRK_SPELLING_LOCATION
1208 -------------------------------------
1210 The virtual location is resolved to the locus where the token has
1211 been spelled in the source. This can follow through all the macro
1212 expansions that led to the token.
1214 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1215 --------------------------------------
1217 The virtual location is resolved to the locus of the token in the
1218 context of the macro definition.
1220 If LOC is the locus of a token that is an argument of a
1221 function-like macro [replacing a parameter in the replacement list
1222 of the macro] the virtual location is resolved to the locus of the
1223 parameter that is replaced, in the context of the definition of the
1224 macro.
1226 If LOC is the locus of a token that is not an argument of a
1227 function-like macro, then the function behaves as if LRK was set to
1228 LRK_SPELLING_LOCATION.
1230 If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1231 returned location. Note that if the returned location wasn't originally
1232 encoded by a map, the *MAP is set to NULL. This can happen if LOC
1233 resolves to a location reserved for the client code, like
1234 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1236 source_location
1237 linemap_resolve_location (struct line_maps *set,
1238 source_location loc,
1239 enum location_resolution_kind lrk,
1240 const struct line_map **map)
1242 if (IS_ADHOC_LOC (loc))
1243 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1245 if (loc < RESERVED_LOCATION_COUNT)
1247 /* A reserved location wasn't encoded in a map. Let's return a
1248 NULL map here, just like what linemap_ordinary_map_lookup
1249 does. */
1250 if (map)
1251 *map = NULL;
1252 return loc;
1255 switch (lrk)
1257 case LRK_MACRO_EXPANSION_POINT:
1258 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1259 break;
1260 case LRK_SPELLING_LOCATION:
1261 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1262 break;
1263 case LRK_MACRO_DEFINITION_LOCATION:
1264 loc = linemap_macro_loc_to_def_point (set, loc, map);
1265 break;
1266 default:
1267 abort ();
1269 return loc;
1273 Suppose that LOC is the virtual location of a token T coming from
1274 the expansion of a macro M. This function then steps up to get the
1275 location L of the point where M got expanded. If L is a spelling
1276 location inside a macro expansion M', then this function returns
1277 the locus of the point where M' was expanded. Said otherwise, this
1278 function returns the location of T in the context that triggered
1279 the expansion of M.
1281 *LOC_MAP must be set to the map of LOC. This function then sets it
1282 to the map of the returned location. */
1284 source_location
1285 linemap_unwind_toward_expansion (struct line_maps *set,
1286 source_location loc,
1287 const struct line_map **map)
1289 source_location resolved_location;
1290 const struct line_map *resolved_map;
1292 if (IS_ADHOC_LOC (loc))
1293 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1295 resolved_location =
1296 linemap_macro_map_loc_unwind_toward_spelling (*map, loc);
1297 resolved_map = linemap_lookup (set, resolved_location);
1299 if (!linemap_macro_expansion_map_p (resolved_map))
1301 resolved_location = linemap_macro_map_loc_to_exp_point (*map, loc);
1302 resolved_map = linemap_lookup (set, resolved_location);
1305 *map = resolved_map;
1306 return resolved_location;
1309 /* If LOC is the virtual location of a token coming from the expansion
1310 of a macro M and if its spelling location is reserved (e.g, a
1311 location for a built-in token), then this function unwinds (using
1312 linemap_unwind_toward_expansion) the location until a location that
1313 is not reserved and is not in a system header is reached. In other
1314 words, this unwinds the reserved location until a location that is
1315 in real source code is reached.
1317 Otherwise, if the spelling location for LOC is not reserved or if
1318 LOC doesn't come from the expansion of a macro, the function
1319 returns LOC as is and *MAP is not touched.
1321 *MAP is set to the map of the returned location if the later is
1322 different from LOC. */
1323 source_location
1324 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1325 source_location loc,
1326 const struct line_map **map)
1328 source_location resolved_loc;
1329 const struct line_map *map0 = NULL, *map1 = NULL;
1331 if (IS_ADHOC_LOC (loc))
1332 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1334 map0 = linemap_lookup (set, loc);
1335 if (!linemap_macro_expansion_map_p (map0))
1336 return loc;
1338 resolved_loc = linemap_resolve_location (set, loc,
1339 LRK_SPELLING_LOCATION,
1340 &map1);
1342 if (resolved_loc >= RESERVED_LOCATION_COUNT
1343 && !LINEMAP_SYSP (map1))
1344 return loc;
1346 while (linemap_macro_expansion_map_p (map0)
1347 && (resolved_loc < RESERVED_LOCATION_COUNT
1348 || LINEMAP_SYSP (map1)))
1350 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1351 resolved_loc = linemap_resolve_location (set, loc,
1352 LRK_SPELLING_LOCATION,
1353 &map1);
1356 if (map != NULL)
1357 *map = map0;
1358 return loc;
1361 /* Expand source code location LOC and return a user readable source
1362 code location. LOC must be a spelling (non-virtual) location. If
1363 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1364 location is returned. */
1366 expanded_location
1367 linemap_expand_location (struct line_maps *set,
1368 const struct line_map *map,
1369 source_location loc)
1372 expanded_location xloc;
1374 memset (&xloc, 0, sizeof (xloc));
1375 if (IS_ADHOC_LOC (loc))
1377 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1378 xloc.data = set->location_adhoc_data_map.data[
1379 loc & MAX_SOURCE_LOCATION].data;
1382 if (loc < RESERVED_LOCATION_COUNT)
1383 /* The location for this token wasn't generated from a line map.
1384 It was probably a location for a builtin token, chosen by some
1385 client code. Let's not try to expand the location in that
1386 case. */;
1387 else if (map == NULL)
1388 /* We shouldn't be getting a NULL map with a location that is not
1389 reserved by the client code. */
1390 abort ();
1391 else
1393 /* MAP must be an ordinary map and LOC must be non-virtual,
1394 encoded into this map, obviously; the accessors used on MAP
1395 below ensure it is ordinary. Let's just assert the
1396 non-virtualness of LOC here. */
1397 if (linemap_location_from_macro_expansion_p (set, loc))
1398 abort ();
1400 xloc.file = LINEMAP_FILE (map);
1401 xloc.line = SOURCE_LINE (map, loc);
1402 xloc.column = SOURCE_COLUMN (map, loc);
1403 xloc.sysp = LINEMAP_SYSP (map) != 0;
1406 return xloc;
1410 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1411 is NULL, use stderr. IS_MACRO is true if the caller wants to
1412 dump a macro map, false otherwise. */
1414 void
1415 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1417 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1418 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1419 "LC_ENTER_MACRO" };
1420 const char *reason;
1421 struct line_map *map;
1423 if (stream == NULL)
1424 stream = stderr;
1426 if (!is_macro)
1427 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1428 else
1429 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1431 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1433 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1434 ix, (void *) map, map->start_location, reason,
1435 (!is_macro && ORDINARY_MAP_IN_SYSTEM_HEADER_P (map)) ? "yes" : "no");
1436 if (!is_macro)
1438 unsigned includer_ix;
1439 struct line_map *includer_map;
1441 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (map);
1442 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1443 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1444 : NULL;
1446 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (map),
1447 ORDINARY_MAP_STARTING_LINE_NUMBER (map));
1448 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1449 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1451 else
1452 fprintf (stream, "Macro: %s (%u tokens)\n",
1453 linemap_map_get_macro_name (map),
1454 MACRO_MAP_NUM_MACRO_TOKENS (map));
1456 fprintf (stream, "\n");
1460 /* Dump debugging information about source location LOC into the file
1461 stream STREAM. SET is the line map set LOC comes from. */
1463 void
1464 linemap_dump_location (struct line_maps *set,
1465 source_location loc,
1466 FILE *stream)
1468 const struct line_map *map;
1469 source_location location;
1470 const char *path = "", *from = "";
1471 int l = -1, c = -1, s = -1, e = -1;
1473 if (IS_ADHOC_LOC (loc))
1474 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1476 if (loc == 0)
1477 return;
1479 location =
1480 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1482 if (map == NULL)
1483 /* Only reserved locations can be tolerated in this case. */
1484 linemap_assert (location < RESERVED_LOCATION_COUNT);
1485 else
1487 path = LINEMAP_FILE (map);
1488 l = SOURCE_LINE (map, location);
1489 c = SOURCE_COLUMN (map, location);
1490 s = LINEMAP_SYSP (map) != 0;
1491 e = location != loc;
1492 if (e)
1493 from = "N/A";
1494 else
1495 from = (INCLUDED_FROM (set, map))
1496 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1497 : "<NULL>";
1500 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1501 E: macro expansion?, LOC: original location, R: resolved location */
1502 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1503 path, from, l, c, s, (void*)map, e, loc, location);
1506 /* Compute and return statistics about the memory consumption of some
1507 parts of the line table SET. */
1509 void
1510 linemap_get_statistics (struct line_maps *set,
1511 struct linemap_stats *s)
1513 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1514 macro_maps_allocated_size, macro_maps_used_size,
1515 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1517 struct line_map *cur_map;
1519 ordinary_maps_allocated_size =
1520 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map);
1522 ordinary_maps_used_size =
1523 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map);
1525 macro_maps_allocated_size =
1526 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map);
1528 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1529 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1530 ++cur_map)
1532 unsigned i;
1534 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1536 macro_maps_locations_size +=
1537 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1539 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1541 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1542 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1543 duplicated_macro_maps_locations_size +=
1544 sizeof (source_location);
1548 macro_maps_used_size =
1549 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map);
1551 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1552 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1553 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1554 s->ordinary_maps_used_size = ordinary_maps_used_size;
1555 s->num_expanded_macros = num_expanded_macros_counter;
1556 s->num_macro_tokens = num_macro_tokens_counter;
1557 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1558 s->macro_maps_allocated_size = macro_maps_allocated_size;
1559 s->macro_maps_locations_size = macro_maps_locations_size;
1560 s->macro_maps_used_size = macro_maps_used_size;
1561 s->duplicated_macro_maps_locations_size =
1562 duplicated_macro_maps_locations_size;
1566 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1567 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1568 specifies how many macro maps to dump. */
1570 void
1571 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1572 unsigned int num_macro)
1574 unsigned int i;
1576 if (set == NULL)
1577 return;
1579 if (stream == NULL)
1580 stream = stderr;
1582 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1583 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1584 fprintf (stream, "Include stack depth: %d\n", set->depth);
1585 fprintf (stream, "Highest location: %u\n", set->highest_location);
1587 if (num_ordinary)
1589 fprintf (stream, "\nOrdinary line maps\n");
1590 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1591 linemap_dump (stream, set, i, false);
1592 fprintf (stream, "\n");
1595 if (num_macro)
1597 fprintf (stream, "\nMacro line maps\n");
1598 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1599 linemap_dump (stream, set, i, true);
1600 fprintf (stream, "\n");