tree-ssa-live.c (set_is_used): Return a bool.
[official-gcc.git] / libcpp / line-map.c
blob7c4e389794d087df6a29c545ccf0d830dab3c8b0
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"
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 /* Initialize a line map set. */
55 void
56 linemap_init (struct line_maps *set)
58 memset (set, 0, sizeof (struct line_maps));
59 set->highest_location = RESERVED_LOCATION_COUNT - 1;
60 set->highest_line = RESERVED_LOCATION_COUNT - 1;
63 /* Check for and warn about line_maps entered but not exited. */
65 void
66 linemap_check_files_exited (struct line_maps *set)
68 struct line_map *map;
69 /* Depending upon whether we are handling preprocessed input or
70 not, this can be a user error or an ICE. */
71 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
72 ! MAIN_FILE_P (map);
73 map = INCLUDED_FROM (set, map))
74 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
75 ORDINARY_MAP_FILE_NAME (map));
78 /* Create a new line map in the line map set SET, and return it.
79 REASON is the reason of creating the map. It determines the type
80 of map created (ordinary or macro map). Note that ordinary maps and
81 macro maps are allocated in different memory location. */
83 static struct line_map *
84 new_linemap (struct line_maps *set,
85 enum lc_reason reason)
87 /* Depending on this variable, a macro map would be allocated in a
88 different memory location than an ordinary map. */
89 bool macro_map_p = (reason == LC_ENTER_MACRO);
90 struct line_map *result;
92 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
94 /* We ran out of allocated line maps. Let's allocate more. */
95 unsigned alloc_size;
97 line_map_realloc reallocator
98 = set->reallocator ? set->reallocator : xrealloc;
99 line_map_round_alloc_size_func round_alloc_size =
100 set->round_alloc_size;
102 /* We are going to execute some dance to try to reduce the
103 overhead of the memory allocator, in case we are using the
104 ggc-page.c one.
106 The actual size of memory we are going to get back from the
107 allocator is the smallest power of 2 that is greater than the
108 size we requested. So let's consider that size then. */
110 alloc_size =
111 (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
112 * sizeof (struct line_map);
114 /* Get the actual size of memory that is going to be allocated
115 by the allocator. */
116 alloc_size = round_alloc_size (alloc_size);
118 /* Now alloc_size contains the exact memory size we would get if
119 we have asked for the initial alloc_size amount of memory.
120 Let's get back to the number of macro map that amounts
121 to. */
122 LINEMAPS_ALLOCATED (set, macro_map_p) =
123 alloc_size / (sizeof (struct line_map));
125 /* And now let's really do the re-allocation. */
126 LINEMAPS_MAPS (set, macro_map_p) =
127 (struct line_map *) (*reallocator)
128 (LINEMAPS_MAPS (set, macro_map_p),
129 (LINEMAPS_ALLOCATED (set, macro_map_p)
130 * sizeof (struct line_map)));
132 result =
133 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
134 memset (result, 0,
135 ((LINEMAPS_ALLOCATED (set, macro_map_p)
136 - LINEMAPS_USED (set, macro_map_p))
137 * sizeof (struct line_map)));
139 else
140 result =
141 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
143 LINEMAPS_USED (set, macro_map_p)++;
145 result->reason = reason;
146 return result;
149 /* Add a mapping of logical source line to physical source file and
150 line number.
152 The text pointed to by TO_FILE must have a lifetime
153 at least as long as the final call to lookup_line (). An empty
154 TO_FILE means standard input. If reason is LC_LEAVE, and
155 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
156 natural values considering the file we are returning to.
158 FROM_LINE should be monotonic increasing across calls to this
159 function. A call to this function can relocate the previous set of
160 maps, so any stored line_map pointers should not be used. */
162 const struct line_map *
163 linemap_add (struct line_maps *set, enum lc_reason reason,
164 unsigned int sysp, const char *to_file, linenum_type to_line)
166 struct line_map *map;
167 source_location start_location = set->highest_location + 1;
169 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
170 && (start_location
171 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
173 /* When we enter the file for the first time reason cannot be
174 LC_RENAME. */
175 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
177 /* If we are leaving the main file, return a NULL map. */
178 if (reason == LC_LEAVE
179 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
180 && to_file == NULL)
182 set->depth--;
183 return NULL;
186 map = new_linemap (set, reason);
188 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
189 to_file = "<stdin>";
191 if (reason == LC_RENAME_VERBATIM)
192 reason = LC_RENAME;
194 if (reason == LC_LEAVE)
196 /* When we are just leaving an "included" file, and jump to the next
197 location inside the "includer" right after the #include
198 "included", this variable points the map in use right before the
199 #include "included", inside the same "includer" file. */
200 struct line_map *from;
201 bool error;
203 if (MAIN_FILE_P (map - 1))
205 /* So this _should_ means we are leaving the main file --
206 effectively ending the compilation unit. But to_file not
207 being NULL means the caller thinks we are leaving to
208 another file. This is an erroneous behaviour but we'll
209 try to recover from it. Let's pretend we are not leaving
210 the main file. */
211 error = true;
212 reason = LC_RENAME;
213 from = map - 1;
215 else
217 /* (MAP - 1) points to the map we are leaving. The
218 map from which (MAP - 1) got included should be the map
219 that comes right before MAP in the same file. */
220 from = INCLUDED_FROM (set, map - 1);
221 error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
222 to_file);
225 /* Depending upon whether we are handling preprocessed input or
226 not, this can be a user error or an ICE. */
227 if (error)
228 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
229 to_file);
231 /* A TO_FILE of NULL is special - we use the natural values. */
232 if (error || to_file == NULL)
234 to_file = ORDINARY_MAP_FILE_NAME (from);
235 to_line = SOURCE_LINE (from, from[1].start_location);
236 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
240 linemap_assert (reason != LC_ENTER_MACRO);
241 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map) = sysp;
242 MAP_START_LOCATION (map) = start_location;
243 ORDINARY_MAP_FILE_NAME (map) = to_file;
244 ORDINARY_MAP_STARTING_LINE_NUMBER (map) = to_line;
245 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
246 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = 0;
247 set->highest_location = start_location;
248 set->highest_line = start_location;
249 set->max_column_hint = 0;
251 if (reason == LC_ENTER)
253 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
254 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
255 set->depth++;
256 if (set->trace_includes)
257 trace_include (set, map);
259 else if (reason == LC_RENAME)
260 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
261 ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
262 else if (reason == LC_LEAVE)
264 set->depth--;
265 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
266 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
269 return map;
272 /* Returns TRUE if the line table set tracks token locations across
273 macro expansion, FALSE otherwise. */
275 bool
276 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
278 return LINEMAPS_MACRO_MAPS (set) != NULL;
281 /* Create a macro map. A macro map encodes source locations of tokens
282 that are part of a macro replacement-list, at a macro expansion
283 point. See the extensive comments of struct line_map and struct
284 line_map_macro, in line-map.h.
286 This map shall be created when the macro is expanded. The map
287 encodes the source location of the expansion point of the macro as
288 well as the "original" source location of each token that is part
289 of the macro replacement-list. If a macro is defined but never
290 expanded, it has no macro map. SET is the set of maps the macro
291 map should be part of. MACRO_NODE is the macro which the new macro
292 map should encode source locations for. EXPANSION is the location
293 of the expansion point of MACRO. For function-like macros
294 invocations, it's best to make it point to the closing parenthesis
295 of the macro, rather than the the location of the first character
296 of the macro. NUM_TOKENS is the number of tokens that are part of
297 the replacement-list of MACRO.
299 Note that when we run out of the integer space available for source
300 locations, this function returns NULL. In that case, callers of
301 this function cannot encode {line,column} pairs into locations of
302 macro tokens anymore. */
304 const struct line_map *
305 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
306 source_location expansion, unsigned int num_tokens)
308 struct line_map *map;
309 source_location start_location;
310 line_map_realloc reallocator
311 = set->reallocator ? set->reallocator : xrealloc;
313 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
315 if (start_location <= set->highest_line
316 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
317 /* We ran out of macro map space. */
318 return NULL;
320 map = new_linemap (set, LC_ENTER_MACRO);
322 MAP_START_LOCATION (map) = start_location;
323 MACRO_MAP_MACRO (map) = macro_node;
324 MACRO_MAP_NUM_MACRO_TOKENS (map) = num_tokens;
325 MACRO_MAP_LOCATIONS (map)
326 = (source_location*) reallocator (NULL,
327 2 * num_tokens
328 * sizeof (source_location));
329 MACRO_MAP_EXPANSION_POINT_LOCATION (map) = expansion;
330 memset (MACRO_MAP_LOCATIONS (map), 0,
331 num_tokens * sizeof (source_location));
333 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
335 return map;
338 /* Create and return a virtual location for a token that is part of a
339 macro expansion-list at a macro expansion point. See the comment
340 inside struct line_map_macro to see what an expansion-list exactly
343 A call to this function must come after a call to
344 linemap_enter_macro.
346 MAP is the map into which the source location is created. TOKEN_NO
347 is the index of the token in the macro replacement-list, starting
348 at number 0.
350 ORIG_LOC is the location of the token outside of this macro
351 expansion. If the token comes originally from the macro
352 definition, it is the locus in the macro definition; otherwise it
353 is a location in the context of the caller of this macro expansion
354 (which is a virtual location or a source location if the caller is
355 itself a macro expansion or not).
357 MACRO_DEFINITION_LOC is the location in the macro definition,
358 either of the token itself or of a macro parameter that it
359 replaces. */
361 source_location
362 linemap_add_macro_token (const struct line_map *map,
363 unsigned int token_no,
364 source_location orig_loc,
365 source_location orig_parm_replacement_loc)
367 source_location result;
369 linemap_assert (linemap_macro_expansion_map_p (map));
370 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
372 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
373 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
375 result = MAP_START_LOCATION (map) + token_no;
376 return result;
379 /* Return a source_location for the start (i.e. column==0) of
380 (physical) line TO_LINE in the current source file (as in the
381 most recent linemap_add). MAX_COLUMN_HINT is the highest column
382 number we expect to use in this line (but it does not change
383 the highest_location). */
385 source_location
386 linemap_line_start (struct line_maps *set, linenum_type to_line,
387 unsigned int max_column_hint)
389 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
390 source_location highest = set->highest_location;
391 source_location r;
392 linenum_type last_line =
393 SOURCE_LINE (map, set->highest_line);
394 int line_delta = to_line - last_line;
395 bool add_map = false;
397 if (line_delta < 0
398 || (line_delta > 10
399 && line_delta * ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) > 1000)
400 || (max_column_hint >= (1U << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)))
401 || (max_column_hint <= 80
402 && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10))
404 add_map = true;
406 else
407 max_column_hint = set->max_column_hint;
408 if (add_map)
410 int column_bits;
411 if (max_column_hint > 100000 || highest > 0xC0000000)
413 /* If the column number is ridiculous or we've allocated a huge
414 number of source_locations, give up on column numbers. */
415 max_column_hint = 0;
416 if (highest >0xF0000000)
417 return 0;
418 column_bits = 0;
420 else
422 column_bits = 7;
423 while (max_column_hint >= (1U << column_bits))
424 column_bits++;
425 max_column_hint = 1U << column_bits;
427 /* Allocate the new line_map. However, if the current map only has a
428 single line we can sometimes just increase its column_bits instead. */
429 if (line_delta < 0
430 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
431 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
432 map = (struct line_map *) linemap_add (set, LC_RENAME,
433 ORDINARY_MAP_IN_SYSTEM_HEADER_P
434 (map),
435 ORDINARY_MAP_FILE_NAME (map),
436 to_line);
437 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = column_bits;
438 r = (MAP_START_LOCATION (map)
439 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
440 << column_bits));
442 else
443 r = highest - SOURCE_COLUMN (map, highest)
444 + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
446 /* Locations of ordinary tokens are always lower than locations of
447 macro tokens. */
448 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
449 return 0;
451 set->highest_line = r;
452 if (r > set->highest_location)
453 set->highest_location = r;
454 set->max_column_hint = max_column_hint;
455 return r;
458 /* Encode and return a source_location from a column number. The
459 source line considered is the last source line used to call
460 linemap_line_start, i.e, the last source line which a location was
461 encoded from. */
463 source_location
464 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
466 source_location r = set->highest_line;
468 linemap_assert
469 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
471 if (to_column >= set->max_column_hint)
473 if (r >= 0xC000000 || to_column > 100000)
475 /* Running low on source_locations - disable column numbers. */
476 return r;
478 else
480 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
481 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
484 r = r + to_column;
485 if (r >= set->highest_location)
486 set->highest_location = r;
487 return r;
490 /* Encode and return a source location from a given line and
491 column. */
493 source_location
494 linemap_position_for_line_and_column (struct line_map *map,
495 linenum_type line,
496 unsigned column)
498 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (map) <= line);
500 return (MAP_START_LOCATION (map)
501 + ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
502 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
503 + (column & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1)));
506 /* Given a virtual source location yielded by a map (either an
507 ordinary or a macro map), returns that map. */
509 const struct line_map*
510 linemap_lookup (struct line_maps *set, source_location line)
512 if (linemap_location_from_macro_expansion_p (set, line))
513 return linemap_macro_map_lookup (set, line);
514 return linemap_ordinary_map_lookup (set, line);
517 /* Given a source location yielded by an ordinary map, returns that
518 map. Since the set is built chronologically, the logical lines are
519 monotonic increasing, and so the list is sorted and we can use a
520 binary search. */
522 static const struct line_map *
523 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
525 unsigned int md, mn, mx;
526 const struct line_map *cached, *result;
528 if (set == NULL || line < RESERVED_LOCATION_COUNT)
529 return NULL;
531 mn = LINEMAPS_ORDINARY_CACHE (set);
532 mx = LINEMAPS_ORDINARY_USED (set);
534 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
535 /* We should get a segfault if no line_maps have been added yet. */
536 if (line >= MAP_START_LOCATION (cached))
538 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
539 return cached;
541 else
543 mx = mn;
544 mn = 0;
547 while (mx - mn > 1)
549 md = (mn + mx) / 2;
550 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
551 mx = md;
552 else
553 mn = md;
556 LINEMAPS_ORDINARY_CACHE (set) = mn;
557 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
558 linemap_assert (line >= MAP_START_LOCATION (result));
559 return result;
562 /* Given a source location yielded by a macro map, returns that map.
563 Since the set is built chronologically, the logical lines are
564 monotonic decreasing, and so the list is sorted and we can use a
565 binary search. */
567 static const struct line_map*
568 linemap_macro_map_lookup (struct line_maps *set, source_location line)
570 unsigned int md, mn, mx;
571 const struct line_map *cached, *result;
573 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
575 if (set == NULL)
576 return NULL;
578 mn = LINEMAPS_MACRO_CACHE (set);
579 mx = LINEMAPS_MACRO_USED (set);
580 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
582 if (line >= MAP_START_LOCATION (cached))
584 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
585 return cached;
586 mx = mn - 1;
587 mn = 0;
590 while (mn < mx)
592 md = (mx + mn) / 2;
593 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
594 mn = md + 1;
595 else
596 mx = md;
599 LINEMAPS_MACRO_CACHE (set) = mx;
600 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
601 linemap_assert (MAP_START_LOCATION (result) <= line);
603 return result;
606 /* Return TRUE if MAP encodes locations coming from a macro
607 replacement-list at macro expansion point. */
609 bool
610 linemap_macro_expansion_map_p (const struct line_map *map)
612 if (!map)
613 return false;
614 return (map->reason == LC_ENTER_MACRO);
617 /* If LOCATION is the locus of a token in a replacement-list of a
618 macro expansion return the location of the macro expansion point.
620 Read the comments of struct line_map and struct line_map_macro in
621 line-map.h to understand what a macro expansion point is. */
623 static source_location
624 linemap_macro_map_loc_to_exp_point (const struct line_map *map,
625 source_location location ATTRIBUTE_UNUSED)
627 linemap_assert (linemap_macro_expansion_map_p (map)
628 && location >= MAP_START_LOCATION (map));
630 /* Make sure LOCATION is correct. */
631 linemap_assert ((location - MAP_START_LOCATION (map))
632 < MACRO_MAP_NUM_MACRO_TOKENS (map));
634 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
637 /* If LOCATION is the source location of a token that belongs to a
638 macro replacement-list -- as part of a macro expansion -- then
639 return the location of the token at the definition point of the
640 macro. Otherwise, return LOCATION. SET is the set of maps
641 location come from. ORIGINAL_MAP is an output parm. If non NULL,
642 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
643 returned location comes from. */
645 source_location
646 linemap_macro_map_loc_to_def_point (const struct line_map *map,
647 source_location location)
649 unsigned token_no;
651 linemap_assert (linemap_macro_expansion_map_p (map)
652 && location >= MAP_START_LOCATION (map));
653 linemap_assert (location >= RESERVED_LOCATION_COUNT);
655 token_no = location - MAP_START_LOCATION (map);
656 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
658 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
660 return location;
663 /* If LOCATION is the locus of a token that is an argument of a
664 function-like macro M and appears in the expansion of M, return the
665 locus of that argument in the context of the caller of M.
667 In other words, this returns the xI location presented in the
668 comments of line_map_macro above. */
669 source_location
670 linemap_macro_map_loc_unwind_toward_spelling (const struct line_map* map,
671 source_location location)
673 unsigned token_no;
675 linemap_assert (linemap_macro_expansion_map_p (map)
676 && location >= MAP_START_LOCATION (map));
677 linemap_assert (location >= RESERVED_LOCATION_COUNT);
679 token_no = location - MAP_START_LOCATION (map);
680 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
682 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
684 return location;
687 /* Return the source line number corresponding to source location
688 LOCATION. SET is the line map set LOCATION comes from. If
689 LOCATION is the source location of token that is part of the
690 replacement-list of a macro expansion return the line number of the
691 macro expansion point. */
694 linemap_get_expansion_line (struct line_maps *set,
695 source_location location)
697 const struct line_map *map = NULL;
699 if (location < RESERVED_LOCATION_COUNT)
700 return 0;
702 location =
703 linemap_macro_loc_to_exp_point (set, location, &map);
705 return SOURCE_LINE (map, location);
708 /* Return the path of the file corresponding to source code location
709 LOCATION.
711 If LOCATION is the source location of token that is part of the
712 replacement-list of a macro expansion return the file path of the
713 macro expansion point.
715 SET is the line map set LOCATION comes from. */
717 const char*
718 linemap_get_expansion_filename (struct line_maps *set,
719 source_location location)
721 const struct line_map *map = NULL;
723 if (location < RESERVED_LOCATION_COUNT)
724 return NULL;
726 location =
727 linemap_macro_loc_to_exp_point (set, location, &map);
729 return LINEMAP_FILE (map);
732 /* Return the name of the macro associated to MACRO_MAP. */
734 const char*
735 linemap_map_get_macro_name (const struct line_map* macro_map)
737 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
738 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
741 /* Return a positive value if LOCATION is the locus of a token that is
742 located in a system header, O otherwise. It returns 1 if LOCATION
743 is the locus of a token that is located in a system header, and 2
744 if LOCATION is the locus of a token located in a C system header
745 that therefore needs to be extern "C" protected in C++.
747 Note that this function returns 1 if LOCATION belongs to a token
748 that is part of a macro replacement-list defined in a system
749 header, but expanded in a non-system file. */
752 linemap_location_in_system_header_p (struct line_maps *set,
753 source_location location)
755 const struct line_map *map = NULL;
757 if (location < RESERVED_LOCATION_COUNT)
758 return false;
760 /* Let's look at where the token for LOCATION comes from. */
761 while (true)
763 map = linemap_lookup (set, location);
764 if (map != NULL)
766 if (!linemap_macro_expansion_map_p (map))
767 /* It's a normal token. */
768 return LINEMAP_SYSP (map);
769 else
771 /* It's a token resulting from a macro expansion. */
772 source_location loc =
773 linemap_macro_map_loc_unwind_toward_spelling (map, location);
774 if (loc < RESERVED_LOCATION_COUNT)
775 /* This token might come from a built-in macro. Let's
776 look at where that macro got expanded. */
777 location = linemap_macro_map_loc_to_exp_point (map, location);
778 else
779 location = loc;
782 else
783 break;
785 return false;
788 /* Return TRUE if LOCATION is a source code location of a token coming
789 from a macro replacement-list at a macro expansion point, FALSE
790 otherwise. */
792 bool
793 linemap_location_from_macro_expansion_p (struct line_maps *set,
794 source_location location)
796 linemap_assert (location <= MAX_SOURCE_LOCATION
797 && (set->highest_location
798 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
799 if (set == NULL)
800 return false;
801 return (location > set->highest_location);
804 /* Given two virtual locations *LOC0 and *LOC1, return the first
805 common macro map in their macro expansion histories. Return NULL
806 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
807 virtual location of the token inside the resulting macro. */
809 static const struct line_map*
810 first_map_in_common_1 (struct line_maps *set,
811 source_location *loc0,
812 source_location *loc1)
814 source_location l0 = *loc0, l1 = *loc1;
815 const struct line_map *map0 = linemap_lookup (set, l0),
816 *map1 = linemap_lookup (set, l1);
818 while (linemap_macro_expansion_map_p (map0)
819 && linemap_macro_expansion_map_p (map1)
820 && (map0 != map1))
822 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
824 l0 = linemap_macro_map_loc_to_exp_point (map0, l0);
825 map0 = linemap_lookup (set, l0);
827 else
829 l1 = linemap_macro_map_loc_to_exp_point (map1, l1);
830 map1 = linemap_lookup (set, l1);
834 if (map0 == map1)
836 *loc0 = l0;
837 *loc1 = l1;
838 return map0;
840 return NULL;
843 /* Given two virtual locations LOC0 and LOC1, return the first common
844 macro map in their macro expansion histories. Return NULL if no
845 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
846 virtual location of the token inside the resulting macro, upon
847 return of a non-NULL result. */
849 static const struct line_map*
850 first_map_in_common (struct line_maps *set,
851 source_location loc0,
852 source_location loc1,
853 source_location *res_loc0,
854 source_location *res_loc1)
856 *res_loc0 = loc0;
857 *res_loc1 = loc1;
859 return first_map_in_common_1 (set, res_loc0, res_loc1);
862 /* Return a positive value if PRE denotes the location of a token that
863 comes before the token of POST, 0 if PRE denotes the location of
864 the same token as the token for POST, and a negative value
865 otherwise. */
868 linemap_compare_locations (struct line_maps *set,
869 source_location pre,
870 source_location post)
872 bool pre_virtual_p, post_virtual_p;
873 source_location l0 = pre, l1 = post;
875 if (l0 == l1)
876 return 0;
878 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
879 l0 = linemap_resolve_location (set, l0,
880 LRK_MACRO_EXPANSION_POINT,
881 NULL);
883 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
884 l1 = linemap_resolve_location (set, l1,
885 LRK_MACRO_EXPANSION_POINT,
886 NULL);
888 if (l0 == l1
889 && pre_virtual_p
890 && post_virtual_p)
892 /* So pre and post represent two tokens that are present in a
893 same macro expansion. Let's see if the token for pre was
894 before the token for post in that expansion. */
895 unsigned i0, i1;
896 const struct line_map *map =
897 first_map_in_common (set, pre, post, &l0, &l1);
899 if (map == NULL)
900 /* This should not be possible. */
901 abort ();
903 i0 = l0 - MAP_START_LOCATION (map);
904 i1 = l1 - MAP_START_LOCATION (map);
905 return i1 - i0;
908 return l1 - l0;
911 /* Print an include trace, for e.g. the -H option of the preprocessor. */
913 static void
914 trace_include (const struct line_maps *set, const struct line_map *map)
916 unsigned int i = set->depth;
918 while (--i)
919 putc ('.', stderr);
921 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
924 /* Return the spelling location of the token wherever it comes from,
925 whether part of a macro definition or not.
927 This is a subroutine for linemap_resolve_location. */
929 static source_location
930 linemap_macro_loc_to_spelling_point (struct line_maps *set,
931 source_location location,
932 const struct line_map **original_map)
934 struct line_map *map;
936 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
938 while (true)
940 map = (struct line_map*) linemap_lookup (set, location);
941 if (!linemap_macro_expansion_map_p (map))
942 break;
944 location =
945 linemap_macro_map_loc_unwind_toward_spelling (map, location);
948 if (original_map)
949 *original_map = map;
950 return location;
953 /* If LOCATION is the source location of a token that belongs to a
954 macro replacement-list -- as part of a macro expansion -- then
955 return the location of the token at the definition point of the
956 macro. Otherwise, return LOCATION. SET is the set of maps
957 location come from. ORIGINAL_MAP is an output parm. If non NULL,
958 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
959 returned location comes from.
961 This is a subroutine of linemap_resolve_location. */
963 static source_location
964 linemap_macro_loc_to_def_point (struct line_maps *set,
965 source_location location,
966 const struct line_map **original_map)
968 struct line_map *map;
970 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
972 while (true)
974 map = (struct line_map*) linemap_lookup (set, location);
975 if (!linemap_macro_expansion_map_p (map))
976 break;
978 location =
979 linemap_macro_map_loc_to_def_point (map, location);
982 if (original_map)
983 *original_map = map;
984 return location;
987 /* If LOCATION is the source location of a token that belongs to a
988 macro replacement-list -- at a macro expansion point -- then return
989 the location of the topmost expansion point of the macro. We say
990 topmost because if we are in the context of a nested macro
991 expansion, the function returns the source location of the first
992 macro expansion that triggered the nested expansions.
994 Otherwise, return LOCATION. SET is the set of maps location come
995 from. ORIGINAL_MAP is an output parm. If non NULL, the function
996 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
997 location comes from.
999 This is a subroutine of linemap_resolve_location. */
1001 static source_location
1002 linemap_macro_loc_to_exp_point (struct line_maps *set,
1003 source_location location,
1004 const struct line_map **original_map)
1006 struct line_map *map;
1008 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1010 while (true)
1012 map = (struct line_map*) linemap_lookup (set, location);
1013 if (!linemap_macro_expansion_map_p (map))
1014 break;
1015 location = linemap_macro_map_loc_to_exp_point (map, location);
1018 if (original_map)
1019 *original_map = map;
1020 return location;
1023 /* Resolve a virtual location into either a spelling location, an
1024 expansion point location or a token argument replacement point
1025 location. Return the map that encodes the virtual location as well
1026 as the resolved location.
1028 If LOC is *NOT* the location of a token resulting from the
1029 expansion of a macro, then the parameter LRK (which stands for
1030 Location Resolution Kind) is ignored and the resulting location
1031 just equals the one given in argument.
1033 Now if LOC *IS* the location of a token resulting from the
1034 expansion of a macro, this is what happens.
1036 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1037 -------------------------------
1039 The virtual location is resolved to the first macro expansion point
1040 that led to this macro expansion.
1042 * If LRK is set to LRK_SPELLING_LOCATION
1043 -------------------------------------
1045 The virtual location is resolved to the locus where the token has
1046 been spelled in the source. This can follow through all the macro
1047 expansions that led to the token.
1049 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1050 --------------------------------------
1052 The virtual location is resolved to the locus of the token in the
1053 context of the macro definition.
1055 If LOC is the locus of a token that is an argument of a
1056 function-like macro [replacing a parameter in the replacement list
1057 of the macro] the virtual location is resolved to the locus of the
1058 parameter that is replaced, in the context of the definition of the
1059 macro.
1061 If LOC is the locus of a token that is not an argument of a
1062 function-like macro, then the function behaves as if LRK was set to
1063 LRK_SPELLING_LOCATION.
1065 If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1066 returned location. Note that if the returned location wasn't originally
1067 encoded by a map, the *MAP is set to NULL. This can happen if LOC
1068 resolves to a location reserved for the client code, like
1069 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1071 source_location
1072 linemap_resolve_location (struct line_maps *set,
1073 source_location loc,
1074 enum location_resolution_kind lrk,
1075 const struct line_map **map)
1077 if (loc < RESERVED_LOCATION_COUNT)
1079 /* A reserved location wasn't encoded in a map. Let's return a
1080 NULL map here, just like what linemap_ordinary_map_lookup
1081 does. */
1082 if (map)
1083 *map = NULL;
1084 return loc;
1087 switch (lrk)
1089 case LRK_MACRO_EXPANSION_POINT:
1090 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1091 break;
1092 case LRK_SPELLING_LOCATION:
1093 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1094 break;
1095 case LRK_MACRO_DEFINITION_LOCATION:
1096 loc = linemap_macro_loc_to_def_point (set, loc, map);
1097 break;
1098 default:
1099 abort ();
1101 return loc;
1105 Suppose that LOC is the virtual location of a token T coming from
1106 the expansion of a macro M. This function then steps up to get the
1107 location L of the point where M got expanded. If L is a spelling
1108 location inside a macro expansion M', then this function returns
1109 the locus of the point where M' was expanded. Said otherwise, this
1110 function returns the location of T in the context that triggered
1111 the expansion of M.
1113 *LOC_MAP must be set to the map of LOC. This function then sets it
1114 to the map of the returned location. */
1116 source_location
1117 linemap_unwind_toward_expansion (struct line_maps *set,
1118 source_location loc,
1119 const struct line_map **map)
1121 source_location resolved_location;
1122 const struct line_map *resolved_map;
1124 resolved_location =
1125 linemap_macro_map_loc_unwind_toward_spelling (*map, loc);
1126 resolved_map = linemap_lookup (set, resolved_location);
1128 if (!linemap_macro_expansion_map_p (resolved_map))
1130 resolved_location = linemap_macro_map_loc_to_exp_point (*map, loc);
1131 resolved_map = linemap_lookup (set, resolved_location);
1134 *map = resolved_map;
1135 return resolved_location;
1138 /* If LOC is the virtual location of a token coming from the expansion
1139 of a macro M and if its spelling location is reserved (e.g, a
1140 location for a built-in token), then this function unwinds (using
1141 linemap_unwind_toward_expansion) the location until a location that
1142 is not reserved and is not in a system header is reached. In other
1143 words, this unwinds the reserved location until a location that is
1144 in real source code is reached.
1146 Otherwise, if the spelling location for LOC is not reserved or if
1147 LOC doesn't come from the expansion of a macro, the function
1148 returns LOC as is and *MAP is not touched.
1150 *MAP is set to the map of the returned location if the later is
1151 different from LOC. */
1152 source_location
1153 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1154 source_location loc,
1155 const struct line_map **map)
1157 source_location resolved_loc;
1158 const struct line_map *map0 = NULL, *map1 = NULL;
1160 map0 = linemap_lookup (set, loc);
1161 if (!linemap_macro_expansion_map_p (map0))
1162 return loc;
1164 resolved_loc = linemap_resolve_location (set, loc,
1165 LRK_SPELLING_LOCATION,
1166 &map1);
1168 if (resolved_loc >= RESERVED_LOCATION_COUNT
1169 && !LINEMAP_SYSP (map1))
1170 return loc;
1172 while (linemap_macro_expansion_map_p (map0)
1173 && (resolved_loc < RESERVED_LOCATION_COUNT
1174 || LINEMAP_SYSP (map1)))
1176 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1177 resolved_loc = linemap_resolve_location (set, loc,
1178 LRK_SPELLING_LOCATION,
1179 &map1);
1182 if (map != NULL)
1183 *map = map0;
1184 return loc;
1187 /* Expand source code location LOC and return a user readable source
1188 code location. LOC must be a spelling (non-virtual) location. If
1189 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1190 location is returned. */
1192 expanded_location
1193 linemap_expand_location (struct line_maps *set,
1194 const struct line_map *map,
1195 source_location loc)
1198 expanded_location xloc;
1200 memset (&xloc, 0, sizeof (xloc));
1202 if (loc < RESERVED_LOCATION_COUNT)
1203 /* The location for this token wasn't generated from a line map.
1204 It was probably a location for a builtin token, chosen by some
1205 client code. Let's not try to expand the location in that
1206 case. */;
1207 else if (map == NULL)
1208 /* We shouldn't be getting a NULL map with a location that is not
1209 reserved by the client code. */
1210 abort ();
1211 else
1213 /* MAP must be an ordinary map and LOC must be non-virtual,
1214 encoded into this map, obviously; the accessors used on MAP
1215 below ensure it is ordinary. Let's just assert the
1216 non-virtualness of LOC here. */
1217 if (linemap_location_from_macro_expansion_p (set, loc))
1218 abort ();
1220 xloc.file = LINEMAP_FILE (map);
1221 xloc.line = SOURCE_LINE (map, loc);
1222 xloc.column = SOURCE_COLUMN (map, loc);
1223 xloc.sysp = LINEMAP_SYSP (map) != 0;
1226 return xloc;
1230 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1231 is NULL, use stderr. IS_MACRO is true if the caller wants to
1232 dump a macro map, false otherwise. */
1234 void
1235 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1237 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1238 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1239 "LC_ENTER_MACRO" };
1240 const char *reason;
1241 struct line_map *map;
1243 if (stream == NULL)
1244 stream = stderr;
1246 if (!is_macro)
1247 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1248 else
1249 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1251 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1253 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1254 ix, (void *) map, map->start_location, reason,
1255 (!is_macro && ORDINARY_MAP_IN_SYSTEM_HEADER_P (map)) ? "yes" : "no");
1256 if (!is_macro)
1258 unsigned includer_ix;
1259 struct line_map *includer_map;
1261 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (map);
1262 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1263 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1264 : NULL;
1266 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (map),
1267 ORDINARY_MAP_STARTING_LINE_NUMBER (map));
1268 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1269 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1271 else
1272 fprintf (stream, "Macro: %s (%u tokens)\n",
1273 linemap_map_get_macro_name (map),
1274 MACRO_MAP_NUM_MACRO_TOKENS (map));
1276 fprintf (stream, "\n");
1280 /* Dump debugging information about source location LOC into the file
1281 stream STREAM. SET is the line map set LOC comes from. */
1283 void
1284 linemap_dump_location (struct line_maps *set,
1285 source_location loc,
1286 FILE *stream)
1288 const struct line_map *map;
1289 source_location location;
1290 const char *path = "", *from = "";
1291 int l = -1, c = -1, s = -1, e = -1;
1293 if (loc == 0)
1294 return;
1296 location =
1297 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1299 if (map == NULL)
1300 /* Only reserved locations can be tolerated in this case. */
1301 linemap_assert (location < RESERVED_LOCATION_COUNT);
1302 else
1304 path = LINEMAP_FILE (map);
1305 l = SOURCE_LINE (map, location);
1306 c = SOURCE_COLUMN (map, location);
1307 s = LINEMAP_SYSP (map) != 0;
1308 e = location != loc;
1309 if (e)
1310 from = "N/A";
1311 else
1312 from = (INCLUDED_FROM (set, map))
1313 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1314 : "<NULL>";
1317 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1318 E: macro expansion?, LOC: original location, R: resolved location */
1319 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1320 path, from, l, c, s, (void*)map, e, loc, location);
1323 /* Compute and return statistics about the memory consumption of some
1324 parts of the line table SET. */
1326 void
1327 linemap_get_statistics (struct line_maps *set,
1328 struct linemap_stats *s)
1330 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1331 macro_maps_allocated_size, macro_maps_used_size,
1332 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1334 struct line_map *cur_map;
1336 ordinary_maps_allocated_size =
1337 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map);
1339 ordinary_maps_used_size =
1340 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map);
1342 macro_maps_allocated_size =
1343 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map);
1345 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1346 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1347 ++cur_map)
1349 unsigned i;
1351 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1353 macro_maps_locations_size +=
1354 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1356 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1358 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1359 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1360 duplicated_macro_maps_locations_size +=
1361 sizeof (source_location);
1365 macro_maps_used_size =
1366 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map);
1368 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1369 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1370 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1371 s->ordinary_maps_used_size = ordinary_maps_used_size;
1372 s->num_expanded_macros = num_expanded_macros_counter;
1373 s->num_macro_tokens = num_macro_tokens_counter;
1374 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1375 s->macro_maps_allocated_size = macro_maps_allocated_size;
1376 s->macro_maps_locations_size = macro_maps_locations_size;
1377 s->macro_maps_used_size = macro_maps_used_size;
1378 s->duplicated_macro_maps_locations_size =
1379 duplicated_macro_maps_locations_size;
1383 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1384 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1385 specifies how many macro maps to dump. */
1387 void
1388 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1389 unsigned int num_macro)
1391 unsigned int i;
1393 if (set == NULL)
1394 return;
1396 if (stream == NULL)
1397 stream = stderr;
1399 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1400 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1401 fprintf (stream, "Include stack depth: %d\n", set->depth);
1402 fprintf (stream, "Highest location: %u\n", set->highest_location);
1404 if (num_ordinary)
1406 fprintf (stream, "\nOrdinary line maps\n");
1407 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1408 linemap_dump (stream, set, i, false);
1409 fprintf (stream, "\n");
1412 if (num_macro)
1414 fprintf (stream, "\nMacro line maps\n");
1415 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1416 linemap_dump (stream, set, i, true);
1417 fprintf (stream, "\n");