Support -fdebug-cpp option
[official-gcc.git] / libcpp / line-map.c
blob3dbaeaba7c92937dda1e013b9552c4120e3daaa4
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 /* Initialize a line map set. */
51 void
52 linemap_init (struct line_maps *set)
54 memset (set, 0, sizeof (struct line_maps));
55 set->highest_location = RESERVED_LOCATION_COUNT - 1;
56 set->highest_line = RESERVED_LOCATION_COUNT - 1;
59 /* Check for and warn about line_maps entered but not exited. */
61 void
62 linemap_check_files_exited (struct line_maps *set)
64 struct line_map *map;
65 /* Depending upon whether we are handling preprocessed input or
66 not, this can be a user error or an ICE. */
67 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
68 ! MAIN_FILE_P (map);
69 map = INCLUDED_FROM (set, map))
70 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
71 ORDINARY_MAP_FILE_NAME (map));
74 /* Create a new line map in the line map set SET, and return it.
75 REASON is the reason of creating the map. It determines the type
76 of map created (ordinary or macro map). Note that ordinary maps and
77 macro maps are allocated in different memory location. */
79 static struct line_map *
80 new_linemap (struct line_maps *set,
81 enum lc_reason reason)
83 /* Depending on this variable, a macro map would be allocated in a
84 different memory location than an ordinary map. */
85 bool macro_map_p = (reason == LC_ENTER_MACRO);
86 struct line_map *result;
88 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
90 /* We ran out of allocated line maps. Let's allocate more. */
92 line_map_realloc reallocator
93 = set->reallocator ? set->reallocator : xrealloc;
94 LINEMAPS_ALLOCATED (set, macro_map_p) =
95 2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256;
96 LINEMAPS_MAPS (set, macro_map_p)
97 = (struct line_map *) (*reallocator) (LINEMAPS_MAPS (set, macro_map_p),
98 LINEMAPS_ALLOCATED (set,
99 macro_map_p)
100 * sizeof (struct line_map));
101 result =
102 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
103 memset (result, 0,
104 ((LINEMAPS_ALLOCATED (set, macro_map_p)
105 - LINEMAPS_USED (set, macro_map_p))
106 * sizeof (struct line_map)));
108 else
109 result =
110 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
112 LINEMAPS_USED (set, macro_map_p)++;
114 result->reason = reason;
115 return result;
118 /* Add a mapping of logical source line to physical source file and
119 line number.
121 The text pointed to by TO_FILE must have a lifetime
122 at least as long as the final call to lookup_line (). An empty
123 TO_FILE means standard input. If reason is LC_LEAVE, and
124 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
125 natural values considering the file we are returning to.
127 FROM_LINE should be monotonic increasing across calls to this
128 function. A call to this function can relocate the previous set of
129 maps, so any stored line_map pointers should not be used. */
131 const struct line_map *
132 linemap_add (struct line_maps *set, enum lc_reason reason,
133 unsigned int sysp, const char *to_file, linenum_type to_line)
135 struct line_map *map;
136 source_location start_location = set->highest_location + 1;
138 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
139 && (start_location
140 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
142 /* When we enter the file for the first time reason cannot be
143 LC_RENAME. */
144 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
146 /* If we are leaving the main file, return a NULL map. */
147 if (reason == LC_LEAVE
148 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
149 && to_file == NULL)
151 set->depth--;
152 return NULL;
155 map = new_linemap (set, reason);
157 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
158 to_file = "<stdin>";
160 if (reason == LC_RENAME_VERBATIM)
161 reason = LC_RENAME;
163 if (reason == LC_LEAVE)
165 /* When we are just leaving an "included" file, and jump to the next
166 location inside the "includer" right after the #include
167 "included", this variable points the map in use right before the
168 #include "included", inside the same "includer" file. */
169 struct line_map *from;
170 bool error;
172 if (MAIN_FILE_P (map - 1))
174 /* So this _should_ means we are leaving the main file --
175 effectively ending the compilation unit. But to_file not
176 being NULL means the caller thinks we are leaving to
177 another file. This is an erroneous behaviour but we'll
178 try to recover from it. Let's pretend we are not leaving
179 the main file. */
180 error = true;
181 reason = LC_RENAME;
182 from = map - 1;
184 else
186 /* (MAP - 1) points to the map we are leaving. The
187 map from which (MAP - 1) got included should be the map
188 that comes right before MAP in the same file. */
189 from = INCLUDED_FROM (set, map - 1);
190 error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
191 to_file);
194 /* Depending upon whether we are handling preprocessed input or
195 not, this can be a user error or an ICE. */
196 if (error)
197 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
198 to_file);
200 /* A TO_FILE of NULL is special - we use the natural values. */
201 if (error || to_file == NULL)
203 to_file = ORDINARY_MAP_FILE_NAME (from);
204 to_line = SOURCE_LINE (from, from[1].start_location);
205 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
209 linemap_assert (reason != LC_ENTER_MACRO);
210 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map) = sysp;
211 MAP_START_LOCATION (map) = start_location;
212 ORDINARY_MAP_FILE_NAME (map) = to_file;
213 ORDINARY_MAP_STARTING_LINE_NUMBER (map) = to_line;
214 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
215 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = 0;
216 set->highest_location = start_location;
217 set->highest_line = start_location;
218 set->max_column_hint = 0;
220 if (reason == LC_ENTER)
222 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
223 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
224 set->depth++;
225 if (set->trace_includes)
226 trace_include (set, map);
228 else if (reason == LC_RENAME)
229 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
230 ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
231 else if (reason == LC_LEAVE)
233 set->depth--;
234 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
235 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
238 return map;
241 /* Returns TRUE if the line table set tracks token locations accross
242 macro expansion, FALSE otherwise. */
244 bool
245 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
247 return LINEMAPS_MACRO_MAPS (set) != NULL;
250 /* Create a macro map. A macro map encodes source locations of tokens
251 that are part of a macro replacement-list, at a macro expansion
252 point. See the extensive comments of struct line_map and struct
253 line_map_macro, in line-map.h.
255 This map shall be created when the macro is expanded. The map
256 encodes the source location of the expansion point of the macro as
257 well as the "original" source location of each token that is part
258 of the macro replacement-list. If a macro is defined but never
259 expanded, it has no macro map. SET is the set of maps the macro
260 map should be part of. MACRO_NODE is the macro which the new macro
261 map should encode source locations for. EXPANSION is the location
262 of the expansion point of MACRO. For function-like macros
263 invocations, it's best to make it point to the closing parenthesis
264 of the macro, rather than the the location of the first character
265 of the macro. NUM_TOKENS is the number of tokens that are part of
266 the replacement-list of MACRO.
268 Note that when we run out of the integer space available for source
269 locations, this function returns NULL. In that case, callers of
270 this function cannot encode {line,column} pairs into locations of
271 macro tokens anymore. */
273 const struct line_map *
274 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
275 source_location expansion, unsigned int num_tokens)
277 struct line_map *map;
278 source_location start_location;
279 line_map_realloc reallocator
280 = set->reallocator ? set->reallocator : xrealloc;
282 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
284 if (start_location <= set->highest_line
285 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
286 /* We ran out of macro map space. */
287 return NULL;
289 map = new_linemap (set, LC_ENTER_MACRO);
291 MAP_START_LOCATION (map) = start_location;
292 MACRO_MAP_MACRO (map) = macro_node;
293 MACRO_MAP_NUM_MACRO_TOKENS (map) = num_tokens;
294 MACRO_MAP_LOCATIONS (map)
295 = (source_location*) reallocator (NULL,
296 2 * num_tokens
297 * sizeof (source_location));
298 MACRO_MAP_EXPANSION_POINT_LOCATION (map) = expansion;
299 memset (MACRO_MAP_LOCATIONS (map), 0,
300 num_tokens * sizeof (source_location));
302 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
303 set->max_column_hint = 0;
305 return map;
308 /* Create and return a virtual location for a token that is part of a
309 macro expansion-list at a macro expansion point. See the comment
310 inside struct line_map_macro to see what an expansion-list exactly
313 A call to this function must come after a call to
314 linemap_enter_macro.
316 MAP is the map into which the source location is created. TOKEN_NO
317 is the index of the token in the macro replacement-list, starting
318 at number 0.
320 ORIG_LOC is the location of the token outside of this macro
321 expansion. If the token comes originally from the macro
322 definition, it is the locus in the macro definition; otherwise it
323 is a location in the context of the caller of this macro expansion
324 (which is a virtual location or a source location if the caller is
325 itself a macro expansion or not).
327 MACRO_DEFINITION_LOC is the location in the macro definition,
328 either of the token itself or of a macro parameter that it
329 replaces. */
331 source_location
332 linemap_add_macro_token (const struct line_map *map,
333 unsigned int token_no,
334 source_location orig_loc,
335 source_location orig_parm_replacement_loc)
337 source_location result;
339 linemap_assert (linemap_macro_expansion_map_p (map));
340 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
342 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
343 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
345 result = MAP_START_LOCATION (map) + token_no;
346 return result;
349 /* Return a source_location for the start (i.e. column==0) of
350 (physical) line TO_LINE in the current source file (as in the
351 most recent linemap_add). MAX_COLUMN_HINT is the highest column
352 number we expect to use in this line (but it does not change
353 the highest_location). */
355 source_location
356 linemap_line_start (struct line_maps *set, linenum_type to_line,
357 unsigned int max_column_hint)
359 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
360 source_location highest = set->highest_location;
361 source_location r;
362 linenum_type last_line =
363 SOURCE_LINE (map, set->highest_line);
364 int line_delta = to_line - last_line;
365 bool add_map = false;
367 if (line_delta < 0
368 || (line_delta > 10
369 && line_delta * ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) > 1000)
370 || (max_column_hint >= (1U << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)))
371 || (max_column_hint <= 80
372 && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10))
374 add_map = true;
376 else
377 max_column_hint = set->max_column_hint;
378 if (add_map)
380 int column_bits;
381 if (max_column_hint > 100000 || highest > 0xC0000000)
383 /* If the column number is ridiculous or we've allocated a huge
384 number of source_locations, give up on column numbers. */
385 max_column_hint = 0;
386 if (highest >0xF0000000)
387 return 0;
388 column_bits = 0;
390 else
392 column_bits = 7;
393 while (max_column_hint >= (1U << column_bits))
394 column_bits++;
395 max_column_hint = 1U << column_bits;
397 /* Allocate the new line_map. However, if the current map only has a
398 single line we can sometimes just increase its column_bits instead. */
399 if (line_delta < 0
400 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
401 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
402 map = (struct line_map *) linemap_add (set, LC_RENAME,
403 ORDINARY_MAP_IN_SYSTEM_HEADER_P
404 (map),
405 ORDINARY_MAP_FILE_NAME (map),
406 to_line);
407 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = column_bits;
408 r = (MAP_START_LOCATION (map)
409 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
410 << column_bits));
412 else
413 r = highest - SOURCE_COLUMN (map, highest)
414 + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
416 /* Locations of ordinary tokens are always lower than locations of
417 macro tokens. */
418 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
419 return 0;
421 set->highest_line = r;
422 if (r > set->highest_location)
423 set->highest_location = r;
424 set->max_column_hint = max_column_hint;
425 return r;
428 /* Encode and return a source_location from a column number. The
429 source line considered is the last source line used to call
430 linemap_line_start, i.e, the last source line which a location was
431 encoded from. */
433 source_location
434 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
436 source_location r = set->highest_line;
438 linemap_assert
439 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
441 if (to_column >= set->max_column_hint)
443 if (r >= 0xC000000 || to_column > 100000)
445 /* Running low on source_locations - disable column numbers. */
446 return r;
448 else
450 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
451 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
454 r = r + to_column;
455 if (r >= set->highest_location)
456 set->highest_location = r;
457 return r;
460 /* Encode and return a source location from a given line and
461 column. */
463 source_location
464 linemap_position_for_line_and_column (struct line_map *map,
465 linenum_type line,
466 unsigned column)
468 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (map) <= line);
470 return (MAP_START_LOCATION (map)
471 + ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
472 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
473 + (column & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1)));
476 /* Given a virtual source location yielded by a map (either an
477 ordinary or a macro map), returns that map. */
479 const struct line_map*
480 linemap_lookup (struct line_maps *set, source_location line)
482 if (linemap_location_from_macro_expansion_p (set, line))
483 return linemap_macro_map_lookup (set, line);
484 return linemap_ordinary_map_lookup (set, line);
487 /* Given a source location yielded by an ordinary map, returns that
488 map. Since the set is built chronologically, the logical lines are
489 monotonic increasing, and so the list is sorted and we can use a
490 binary search. */
492 static const struct line_map *
493 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
495 unsigned int md, mn, mx;
496 const struct line_map *cached, *result;
498 if (set == NULL || line < RESERVED_LOCATION_COUNT)
499 return NULL;
501 mn = LINEMAPS_ORDINARY_CACHE (set);
502 mx = LINEMAPS_ORDINARY_USED (set);
504 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
505 /* We should get a segfault if no line_maps have been added yet. */
506 if (line >= MAP_START_LOCATION (cached))
508 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
509 return cached;
511 else
513 mx = mn;
514 mn = 0;
517 while (mx - mn > 1)
519 md = (mn + mx) / 2;
520 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
521 mx = md;
522 else
523 mn = md;
526 LINEMAPS_ORDINARY_CACHE (set) = mn;
527 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
528 linemap_assert (line >= MAP_START_LOCATION (result));
529 return result;
532 /* Given a source location yielded by a macro map, returns that map.
533 Since the set is built chronologically, the logical lines are
534 monotonic decreasing, and so the list is sorted and we can use a
535 binary search. */
537 static const struct line_map*
538 linemap_macro_map_lookup (struct line_maps *set, source_location line)
540 unsigned int md, mn, mx;
541 const struct line_map *cached, *result;
543 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
545 if (set == NULL)
546 return NULL;
548 mn = LINEMAPS_MACRO_CACHE (set);
549 mx = LINEMAPS_MACRO_USED (set);
550 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
552 if (line >= MAP_START_LOCATION (cached))
554 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
555 return cached;
556 mx = mn - 1;
557 mn = 0;
562 md = (mx + mn) / 2;
563 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
564 mn = md;
565 else
566 mx = md;
567 } while (mx - mn > 1);
569 LINEMAPS_MACRO_CACHE (set) = mx;
570 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
571 linemap_assert (MAP_START_LOCATION (result) <= line);
573 return result;
576 /* Return TRUE if MAP encodes locations coming from a macro
577 replacement-list at macro expansion point. */
579 bool
580 linemap_macro_expansion_map_p (const struct line_map *map)
582 if (!map)
583 return false;
584 return (map->reason == LC_ENTER_MACRO);
587 /* If LOCATION is the locus of a token in a replacement-list of a
588 macro expansion return the location of the macro expansion point.
590 Read the comments of struct line_map and struct line_map_macro in
591 line-map.h to understand what a macro expansion point is. */
593 source_location
594 linemap_macro_map_loc_to_exp_point (const struct line_map *map,
595 source_location location)
597 unsigned token_no;
599 linemap_assert (linemap_macro_expansion_map_p (map)
600 && location >= MAP_START_LOCATION (map));
602 /* Make sure LOCATION is correct. */
603 token_no = location - MAP_START_LOCATION (map);
604 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
606 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
609 /* If LOCATION is the source location of a token that belongs to a
610 macro replacement-list -- as part of a macro expansion -- then
611 return the location of the token at the definition point of the
612 macro. Otherwise, return LOCATION. SET is the set of maps
613 location come from. ORIGINAL_MAP is an output parm. If non NULL,
614 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
615 returned location comes from. */
617 source_location
618 linemap_macro_map_loc_to_def_point (const struct line_map *map,
619 source_location location)
621 unsigned token_no;
623 linemap_assert (linemap_macro_expansion_map_p (map)
624 && location >= MAP_START_LOCATION (map));
625 linemap_assert (location >= RESERVED_LOCATION_COUNT);
627 token_no = location - MAP_START_LOCATION (map);
628 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
630 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
632 return location;
635 /* If LOCATION is the locus of a token that is an argument of a
636 function-like macro M and appears in the expansion of M, return the
637 locus of that argument in the context of the caller of M.
639 In other words, this returns the xI location presented in the
640 comments of line_map_macro above. */
641 source_location
642 linemap_macro_map_loc_unwind_toward_spelling (const struct line_map* map,
643 source_location location)
645 unsigned token_no;
647 linemap_assert (linemap_macro_expansion_map_p (map)
648 && location >= MAP_START_LOCATION (map));
649 linemap_assert (location >= RESERVED_LOCATION_COUNT);
651 token_no = location - MAP_START_LOCATION (map);
652 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
654 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
656 return location;
659 /* Return the source line number corresponding to source location
660 LOCATION. SET is the line map set LOCATION comes from. If
661 LOCATION is the source location of token that is part of the
662 replacement-list of a macro expansion return the line number of the
663 macro expansion point. */
666 linemap_get_expansion_line (struct line_maps *set,
667 source_location location)
669 const struct line_map *map = NULL;
671 if (location < RESERVED_LOCATION_COUNT)
672 return 0;
674 location =
675 linemap_macro_loc_to_exp_point (set, location, &map);
677 return SOURCE_LINE (map, location);
680 /* Return the path of the file corresponding to source code location
681 LOCATION.
683 If LOCATION is the source location of token that is part of the
684 replacement-list of a macro expansion return the file path of the
685 macro expansion point.
687 SET is the line map set LOCATION comes from. */
689 const char*
690 linemap_get_expansion_filename (struct line_maps *set,
691 source_location location)
693 const struct line_map *map = NULL;
695 if (location < RESERVED_LOCATION_COUNT)
696 return NULL;
698 location =
699 linemap_macro_loc_to_exp_point (set, location, &map);
701 return LINEMAP_FILE (map);
704 /* Return the name of the macro associated to MACRO_MAP. */
706 const char*
707 linemap_map_get_macro_name (const struct line_map* macro_map)
709 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
710 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
713 /* Return a positive value if LOCATION is the locus of a token that is
714 located in a system header, O otherwise. It returns 1 if LOCATION
715 is the locus of a token that is located in a system header, and 2
716 if LOCATION is the locus of a token located in a C system header
717 that therefore needs to be extern "C" protected in C++.
719 Note that this function returns 1 if LOCATION belongs to a token
720 that is part of a macro replacement-list defined in a system
721 header, but expanded in a non-system file. */
724 linemap_location_in_system_header_p (struct line_maps *set,
725 source_location location)
727 const struct line_map *map = NULL;
729 if (location < RESERVED_LOCATION_COUNT)
730 return false;
732 location =
733 linemap_resolve_location (set, location, LRK_SPELLING_LOCATION, &map);
735 return LINEMAP_SYSP (map);
738 /* Return TRUE if LOCATION is a source code location of a token coming
739 from a macro replacement-list at a macro expansion point, FALSE
740 otherwise. */
742 bool
743 linemap_location_from_macro_expansion_p (struct line_maps *set,
744 source_location location)
746 linemap_assert (location <= MAX_SOURCE_LOCATION
747 && (set->highest_location
748 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
749 if (set == NULL)
750 return false;
751 return (location > set->highest_location);
754 /* Given two virtual locations *LOC0 and *LOC1, return the first
755 common macro map in their macro expansion histories. Return NULL
756 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
757 virtual location of the token inside the resulting macro. */
759 static const struct line_map*
760 first_map_in_common_1 (struct line_maps *set,
761 source_location *loc0,
762 source_location *loc1)
764 source_location l0 = *loc0, l1 = *loc1;
765 const struct line_map *map0 = linemap_lookup (set, l0),
766 *map1 = linemap_lookup (set, l1);
768 while (linemap_macro_expansion_map_p (map0)
769 && linemap_macro_expansion_map_p (map1)
770 && (map0 != map1))
772 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
774 l0 = linemap_macro_map_loc_to_exp_point (map0, l0);
775 map0 = linemap_lookup (set, l0);
777 else
779 l1 = linemap_macro_map_loc_to_exp_point (map1, l1);
780 map1 = linemap_lookup (set, l1);
784 if (map0 == map1)
786 *loc0 = l0;
787 *loc1 = l1;
788 return map0;
790 return NULL;
793 /* Given two virtual locations LOC0 and LOC1, return the first common
794 macro map in their macro expansion histories. Return NULL if no
795 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
796 virtual location of the token inside the resulting macro, upon
797 return of a non-NULL result. */
799 static const struct line_map*
800 first_map_in_common (struct line_maps *set,
801 source_location loc0,
802 source_location loc1,
803 source_location *res_loc0,
804 source_location *res_loc1)
806 *res_loc0 = loc0;
807 *res_loc1 = loc1;
809 return first_map_in_common_1 (set, res_loc0, res_loc1);
812 /* Return a positive value if PRE denotes the location of a token that
813 comes before the token of POST, 0 if PRE denotes the location of
814 the same token as the token for POST, and a negative value
815 otherwise. */
818 linemap_compare_locations (struct line_maps *set,
819 source_location pre,
820 source_location post)
822 bool pre_virtual_p, post_virtual_p;
823 source_location l0 = pre, l1 = post;
825 if (l0 == l1)
826 return 0;
828 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
829 l0 = linemap_resolve_location (set, l0,
830 LRK_MACRO_EXPANSION_POINT,
831 NULL);
833 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
834 l1 = linemap_resolve_location (set, l1,
835 LRK_MACRO_EXPANSION_POINT,
836 NULL);
838 if (l0 == l1
839 && pre_virtual_p
840 && post_virtual_p)
842 /* So pre and post represent two tokens that are present in a
843 same macro expansion. Let's see if the token for pre was
844 before the token for post in that expansion. */
845 unsigned i0, i1;
846 const struct line_map *map =
847 first_map_in_common (set, pre, post, &l0, &l1);
849 if (map == NULL)
850 /* This should not be possible. */
851 abort ();
853 i0 = l0 - MAP_START_LOCATION (map);
854 i1 = l1 - MAP_START_LOCATION (map);
855 return i1 - i0;
858 return l1 - l0;
861 /* Print an include trace, for e.g. the -H option of the preprocessor. */
863 static void
864 trace_include (const struct line_maps *set, const struct line_map *map)
866 unsigned int i = set->depth;
868 while (--i)
869 putc ('.', stderr);
871 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
874 /* Return the spelling location of the token wherever it comes from,
875 whether part of a macro definition or not.
877 This is a subroutine for linemap_resolve_location. */
879 static source_location
880 linemap_macro_loc_to_spelling_point (struct line_maps *set,
881 source_location location,
882 const struct line_map **original_map)
884 struct line_map *map;
886 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
888 while (true)
890 map = (struct line_map*) linemap_lookup (set, location);
891 if (!linemap_macro_expansion_map_p (map))
892 break;
894 location =
895 linemap_macro_map_loc_unwind_toward_spelling (map, location);
898 if (original_map)
899 *original_map = map;
900 return location;
903 /* If LOCATION is the source location of a token that belongs to a
904 macro replacement-list -- as part of a macro expansion -- then
905 return the location of the token at the definition point of the
906 macro. Otherwise, return LOCATION. SET is the set of maps
907 location come from. ORIGINAL_MAP is an output parm. If non NULL,
908 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
909 returned location comes from.
911 This is a subroutine of linemap_resolve_location. */
913 static source_location
914 linemap_macro_loc_to_def_point (struct line_maps *set,
915 source_location location,
916 const struct line_map **original_map)
918 struct line_map *map;
920 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
922 while (true)
924 map = (struct line_map*) linemap_lookup (set, location);
925 if (!linemap_macro_expansion_map_p (map))
926 break;
928 location =
929 linemap_macro_map_loc_to_def_point (map, location);
932 if (original_map)
933 *original_map = map;
934 return location;
937 /* If LOCATION is the source location of a token that belongs to a
938 macro replacement-list -- at a macro expansion point -- then return
939 the location of the topmost expansion point of the macro. We say
940 topmost because if we are in the context of a nested macro
941 expansion, the function returns the source location of the first
942 macro expansion that triggered the nested expansions.
944 Otherwise, return LOCATION. SET is the set of maps location come
945 from. ORIGINAL_MAP is an output parm. If non NULL, the function
946 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
947 location comes from.
949 This is a subroutine of linemap_resolve_location. */
951 static source_location
952 linemap_macro_loc_to_exp_point (struct line_maps *set,
953 source_location location,
954 const struct line_map **original_map)
956 struct line_map *map;
958 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
960 while (true)
962 map = (struct line_map*) linemap_lookup (set, location);
963 if (!linemap_macro_expansion_map_p (map))
964 break;
965 location = linemap_macro_map_loc_to_exp_point (map, location);
968 if (original_map)
969 *original_map = map;
970 return location;
973 /* Resolve a virtual location into either a spelling location, an
974 expansion point location or a token argument replacement point
975 location. Return the map that encodes the virtual location as well
976 as the resolved location.
978 If LOC is *NOT* the location of a token resulting from the
979 expansion of a macro, then the parameter LRK (which stands for
980 Location Resolution Kind) is ignored and the resulting location
981 just equals the one given in argument.
983 Now if LOC *IS* the location of a token resulting from the
984 expansion of a macro, this is what happens.
986 * If LRK is set to LRK_MACRO_EXPANSION_POINT
987 -------------------------------
989 The virtual location is resolved to the location to the locus of
990 the expansion point of the macro.
992 * If LRK is set to LRK_SPELLING_LOCATION
993 -------------------------------------
995 The virtual location is resolved to the location to the locus where
996 the token has been spelled in the source. This can follow through
997 all the macro expansions that led to the token.
999 * If LRK is set to LRK_MACRO_PARM_REPLACEMENT_POINT
1000 --------------------------------------
1002 If LOC is the locus of a token that is an argument of a
1003 function-like macro [replacing a parameter in the replacement list
1004 of the macro] the virtual location is resolved to the locus of the
1005 parameter that is replaced, in the context of the definition of the
1006 macro.
1008 If LOC is the locus of a token that is not an argument of a
1009 function-like macro, then the function behaves as if LRK was set to
1010 LRK_SPELLING_LOCATION.
1012 If MAP is non-NULL, *MAP is set to the map of the resolved
1013 location. */
1015 source_location
1016 linemap_resolve_location (struct line_maps *set,
1017 source_location loc,
1018 enum location_resolution_kind lrk,
1019 const struct line_map **map)
1021 linemap_assert (set && loc >= RESERVED_LOCATION_COUNT);
1023 switch (lrk)
1025 case LRK_MACRO_EXPANSION_POINT:
1026 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1027 break;
1028 case LRK_SPELLING_LOCATION:
1029 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1030 break;
1031 case LRK_MACRO_DEFINITION_LOCATION:
1032 loc = linemap_macro_loc_to_def_point (set, loc, map);
1033 break;
1034 default:
1035 abort ();
1037 return loc;
1041 Suppose that LOC is the virtual location of a token T coming from
1042 the expansion of a macro M. This function then steps up to get the
1043 location L of the point where M got expanded. If L is a spelling
1044 location inside a macro expansion M', then this function returns
1045 the locus of the point where M' was expanded. Said otherwise, this
1046 function returns the location of T in the context that triggered
1047 the expansion of M.
1049 *LOC_MAP must be set to the map of LOC. This function then sets it
1050 to the map of the returned location. */
1052 source_location
1053 linemap_unwind_toward_expansion (struct line_maps *set,
1054 source_location loc,
1055 const struct line_map **map)
1057 source_location resolved_location;
1058 const struct line_map *resolved_map;
1060 resolved_location =
1061 linemap_macro_map_loc_unwind_toward_spelling (*map, loc);
1062 resolved_map = linemap_lookup (set, resolved_location);
1064 if (!linemap_macro_expansion_map_p (resolved_map))
1066 resolved_location = linemap_macro_map_loc_to_exp_point (*map, loc);
1067 resolved_map = linemap_lookup (set, resolved_location);
1070 *map = resolved_map;
1071 return resolved_location;
1074 /* Expand source code location LOC and return a user readable source
1075 code location. LOC must be a spelling (non-virtual) location. */
1077 expanded_location
1078 linemap_expand_location (const struct line_map *map,
1079 source_location loc)
1082 expanded_location xloc;
1084 xloc.file = LINEMAP_FILE (map);
1085 xloc.line = SOURCE_LINE (map, loc);
1086 xloc.column = SOURCE_COLUMN (map, loc);
1087 xloc.sysp = LINEMAP_SYSP (map) != 0;
1089 return xloc;
1092 /* Expand source code location LOC and return a user readable source
1093 code location. LOC can be a virtual location. The LRK parameter
1094 is the same as for linemap_resolve_location. */
1096 expanded_location
1097 linemap_expand_location_full (struct line_maps *set,
1098 source_location loc,
1099 enum location_resolution_kind lrk)
1101 const struct line_map *map;
1102 expanded_location xloc;
1104 loc = linemap_resolve_location (set, loc, lrk, &map);
1105 xloc = linemap_expand_location (map, loc);
1106 return xloc;
1109 /* Dump debugging information about source location LOC into the file
1110 stream STREAM. SET is the line map set LOC comes from. */
1112 void
1113 linemap_dump_location (struct line_maps *set,
1114 source_location loc,
1115 FILE *stream)
1117 const struct line_map *map;
1118 source_location location;
1119 const char *path, *from;
1120 int l,c,s,e;
1122 if (loc == 0)
1123 return;
1125 location =
1126 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1127 path = LINEMAP_FILE (map);
1129 l = SOURCE_LINE (map, location);
1130 c = SOURCE_COLUMN (map, location);
1131 s = LINEMAP_SYSP (map) != 0;
1132 e = location != loc;
1134 if (e)
1135 from = "N/A";
1136 else
1137 from = (INCLUDED_FROM (set, map))
1138 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1139 : "<NULL>";
1141 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1142 E: macro expansion?. */
1143 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d}",
1144 path, from, l, c, s, (void*)map, e, loc);