1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2016 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>.
18 In other words, you are welcome to use, share and improve this program.
19 You are forbidden to forbid anyone else to use, share and improve
20 what you give them. Help stamp out software-hoarding! */
29 /* Do not track column numbers higher than this one. As a result, the
30 range of column_bits is [12, 18] (or 0 if column numbers are
32 const unsigned int LINE_MAP_MAX_COLUMN_NUMBER
= (1U << 12);
34 /* Highest possible source location encoded within an ordinary or
36 const source_location LINE_MAP_MAX_SOURCE_LOCATION
= 0x70000000;
38 static void trace_include (const struct line_maps
*, const line_map_ordinary
*);
39 static const line_map_ordinary
* linemap_ordinary_map_lookup (struct line_maps
*,
41 static const line_map_macro
* linemap_macro_map_lookup (struct line_maps
*,
43 static source_location linemap_macro_map_loc_to_def_point
44 (const line_map_macro
*, source_location
);
45 static source_location linemap_macro_map_loc_to_exp_point
46 (const line_map_macro
*, source_location
);
47 static source_location linemap_macro_loc_to_spelling_point
48 (struct line_maps
*, source_location
, const line_map_ordinary
**);
49 static source_location
linemap_macro_loc_to_def_point (struct line_maps
*,
51 const line_map_ordinary
**);
52 static source_location
linemap_macro_loc_to_exp_point (struct line_maps
*,
54 const line_map_ordinary
**);
56 /* Counters defined in macro.c. */
57 extern unsigned num_expanded_macros_counter
;
58 extern unsigned num_macro_tokens_counter
;
60 /* Hash function for location_adhoc_data hashtable. */
63 location_adhoc_data_hash (const void *l
)
65 const struct location_adhoc_data
*lb
=
66 (const struct location_adhoc_data
*) l
;
67 return ((hashval_t
) lb
->locus
68 + (hashval_t
) lb
->src_range
.m_start
69 + (hashval_t
) lb
->src_range
.m_finish
73 /* Compare function for location_adhoc_data hashtable. */
76 location_adhoc_data_eq (const void *l1
, const void *l2
)
78 const struct location_adhoc_data
*lb1
=
79 (const struct location_adhoc_data
*) l1
;
80 const struct location_adhoc_data
*lb2
=
81 (const struct location_adhoc_data
*) l2
;
82 return (lb1
->locus
== lb2
->locus
83 && lb1
->src_range
.m_start
== lb2
->src_range
.m_start
84 && lb1
->src_range
.m_finish
== lb2
->src_range
.m_finish
85 && lb1
->data
== lb2
->data
);
88 /* Update the hashtable when location_adhoc_data is reallocated. */
91 location_adhoc_data_update (void **slot
, void *data
)
93 *((char **) slot
) += *((int64_t *) data
);
97 /* Rebuild the hash table from the location adhoc data. */
100 rebuild_location_adhoc_htab (struct line_maps
*set
)
103 set
->location_adhoc_data_map
.htab
=
104 htab_create (100, location_adhoc_data_hash
, location_adhoc_data_eq
, NULL
);
105 for (i
= 0; i
< set
->location_adhoc_data_map
.curr_loc
; i
++)
106 htab_find_slot (set
->location_adhoc_data_map
.htab
,
107 set
->location_adhoc_data_map
.data
+ i
, INSERT
);
110 /* Helper function for get_combined_adhoc_loc.
111 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
112 within a source_location, without needing to use an ad-hoc location. */
115 can_be_stored_compactly_p (struct line_maps
*set
,
116 source_location locus
,
117 source_range src_range
,
120 /* If there's an ad-hoc pointer, we can't store it directly in the
121 source_location, we need the lookaside. */
125 /* We only store ranges that begin at the locus and that are sufficiently
127 if (src_range
.m_start
!= locus
)
130 if (src_range
.m_finish
< src_range
.m_start
)
133 if (src_range
.m_start
< RESERVED_LOCATION_COUNT
)
136 if (locus
>= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
)
139 /* All 3 locations must be within ordinary maps, typically, the same
141 source_location lowest_macro_loc
= LINEMAPS_MACRO_LOWEST_LOCATION (set
);
142 if (locus
>= lowest_macro_loc
)
144 if (src_range
.m_start
>= lowest_macro_loc
)
146 if (src_range
.m_finish
>= lowest_macro_loc
)
149 /* Passed all tests. */
153 /* Combine LOCUS and DATA to a combined adhoc loc. */
156 get_combined_adhoc_loc (struct line_maps
*set
,
157 source_location locus
,
158 source_range src_range
,
161 struct location_adhoc_data lb
;
162 struct location_adhoc_data
**slot
;
164 if (IS_ADHOC_LOC (locus
))
166 = set
->location_adhoc_data_map
.data
[locus
& MAX_SOURCE_LOCATION
].locus
;
167 if (locus
== 0 && data
== NULL
)
170 /* Any ordinary locations ought to be "pure" at this point: no
171 compressed ranges. */
172 linemap_assert (locus
< RESERVED_LOCATION_COUNT
173 || locus
>= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
174 || locus
>= LINEMAPS_MACRO_LOWEST_LOCATION (set
)
175 || pure_location_p (set
, locus
));
177 /* Consider short-range optimization. */
178 if (can_be_stored_compactly_p (set
, locus
, src_range
, data
))
180 /* The low bits ought to be clear. */
181 linemap_assert (pure_location_p (set
, locus
));
182 const line_map
*map
= linemap_lookup (set
, locus
);
183 const line_map_ordinary
*ordmap
= linemap_check_ordinary (map
);
184 unsigned int int_diff
= src_range
.m_finish
- src_range
.m_start
;
185 unsigned int col_diff
= (int_diff
>> ordmap
->m_range_bits
);
186 if (col_diff
< (1U << ordmap
->m_range_bits
))
188 source_location packed
= locus
| col_diff
;
189 set
->num_optimized_ranges
++;
194 /* We can also compactly store locations
195 when locus == start == finish (and data is NULL). */
196 if (locus
== src_range
.m_start
197 && locus
== src_range
.m_finish
202 set
->num_unoptimized_ranges
++;
205 lb
.src_range
= src_range
;
207 slot
= (struct location_adhoc_data
**)
208 htab_find_slot (set
->location_adhoc_data_map
.htab
, &lb
, INSERT
);
211 if (set
->location_adhoc_data_map
.curr_loc
>=
212 set
->location_adhoc_data_map
.allocated
)
214 char *orig_data
= (char *) set
->location_adhoc_data_map
.data
;
216 /* Cast away extern "C" from the type of xrealloc. */
217 line_map_realloc reallocator
= (set
->reallocator
219 : (line_map_realloc
) xrealloc
);
221 if (set
->location_adhoc_data_map
.allocated
== 0)
222 set
->location_adhoc_data_map
.allocated
= 128;
224 set
->location_adhoc_data_map
.allocated
*= 2;
225 set
->location_adhoc_data_map
.data
= (struct location_adhoc_data
*)
226 reallocator (set
->location_adhoc_data_map
.data
,
227 set
->location_adhoc_data_map
.allocated
228 * sizeof (struct location_adhoc_data
));
229 offset
= (char *) (set
->location_adhoc_data_map
.data
) - orig_data
;
230 if (set
->location_adhoc_data_map
.allocated
> 128)
231 htab_traverse (set
->location_adhoc_data_map
.htab
,
232 location_adhoc_data_update
, &offset
);
234 *slot
= set
->location_adhoc_data_map
.data
235 + set
->location_adhoc_data_map
.curr_loc
;
236 set
->location_adhoc_data_map
.data
[set
->location_adhoc_data_map
.curr_loc
++]
239 return ((*slot
) - set
->location_adhoc_data_map
.data
) | 0x80000000;
242 /* Return the data for the adhoc loc. */
245 get_data_from_adhoc_loc (struct line_maps
*set
, source_location loc
)
247 linemap_assert (IS_ADHOC_LOC (loc
));
248 return set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].data
;
251 /* Return the location for the adhoc loc. */
254 get_location_from_adhoc_loc (struct line_maps
*set
, source_location loc
)
256 linemap_assert (IS_ADHOC_LOC (loc
));
257 return set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
260 /* Return the source_range for adhoc location LOC. */
263 get_range_from_adhoc_loc (struct line_maps
*set
, source_location loc
)
265 linemap_assert (IS_ADHOC_LOC (loc
));
266 return set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].src_range
;
269 /* Get the source_range of location LOC, either from the ad-hoc
270 lookaside table, or embedded inside LOC itself. */
273 get_range_from_loc (struct line_maps
*set
,
276 if (IS_ADHOC_LOC (loc
))
277 return get_range_from_adhoc_loc (set
, loc
);
279 /* For ordinary maps, extract packed range. */
280 if (loc
>= RESERVED_LOCATION_COUNT
281 && loc
< LINEMAPS_MACRO_LOWEST_LOCATION (set
)
282 && loc
<= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
)
284 const line_map
*map
= linemap_lookup (set
, loc
);
285 const line_map_ordinary
*ordmap
= linemap_check_ordinary (map
);
287 int offset
= loc
& ((1 << ordmap
->m_range_bits
) - 1);
288 result
.m_start
= loc
- offset
;
289 result
.m_finish
= result
.m_start
+ (offset
<< ordmap
->m_range_bits
);
293 return source_range::from_location (loc
);
296 /* Get whether location LOC is a "pure" location, or
297 whether it is an ad-hoc location, or embeds range information. */
300 pure_location_p (line_maps
*set
, source_location loc
)
302 if (IS_ADHOC_LOC (loc
))
305 const line_map
*map
= linemap_lookup (set
, loc
);
306 const line_map_ordinary
*ordmap
= linemap_check_ordinary (map
);
308 if (loc
& ((1U << ordmap
->m_range_bits
) - 1))
314 /* Finalize the location_adhoc_data structure. */
316 location_adhoc_data_fini (struct line_maps
*set
)
318 htab_delete (set
->location_adhoc_data_map
.htab
);
321 /* Initialize a line map set. */
324 linemap_init (struct line_maps
*set
,
325 source_location builtin_location
)
327 memset (set
, 0, sizeof (struct line_maps
));
328 set
->highest_location
= RESERVED_LOCATION_COUNT
- 1;
329 set
->highest_line
= RESERVED_LOCATION_COUNT
- 1;
330 set
->location_adhoc_data_map
.htab
=
331 htab_create (100, location_adhoc_data_hash
, location_adhoc_data_eq
, NULL
);
332 set
->builtin_location
= builtin_location
;
335 /* Check for and warn about line_maps entered but not exited. */
338 linemap_check_files_exited (struct line_maps
*set
)
340 const line_map_ordinary
*map
;
341 /* Depending upon whether we are handling preprocessed input or
342 not, this can be a user error or an ICE. */
343 for (map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
345 map
= INCLUDED_FROM (set
, map
))
346 fprintf (stderr
, "line-map.c: file \"%s\" entered but not left\n",
347 ORDINARY_MAP_FILE_NAME (map
));
350 /* Create a new line map in the line map set SET, and return it.
351 REASON is the reason of creating the map. It determines the type
352 of map created (ordinary or macro map). Note that ordinary maps and
353 macro maps are allocated in different memory location. */
355 static struct line_map
*
356 new_linemap (struct line_maps
*set
,
357 enum lc_reason reason
)
359 /* Depending on this variable, a macro map would be allocated in a
360 different memory location than an ordinary map. */
361 bool macro_map_p
= (reason
== LC_ENTER_MACRO
);
362 struct line_map
*result
;
364 if (LINEMAPS_USED (set
, macro_map_p
) == LINEMAPS_ALLOCATED (set
, macro_map_p
))
366 /* We ran out of allocated line maps. Let's allocate more. */
369 /* Cast away extern "C" from the type of xrealloc. */
370 line_map_realloc reallocator
= (set
->reallocator
372 : (line_map_realloc
) xrealloc
);
373 line_map_round_alloc_size_func round_alloc_size
=
374 set
->round_alloc_size
;
376 size_t map_size
= (macro_map_p
377 ? sizeof (line_map_macro
)
378 : sizeof (line_map_ordinary
));
380 /* We are going to execute some dance to try to reduce the
381 overhead of the memory allocator, in case we are using the
384 The actual size of memory we are going to get back from the
385 allocator is the smallest power of 2 that is greater than the
386 size we requested. So let's consider that size then. */
389 (2 * LINEMAPS_ALLOCATED (set
, macro_map_p
) + 256)
392 /* Get the actual size of memory that is going to be allocated
394 alloc_size
= round_alloc_size (alloc_size
);
396 /* Now alloc_size contains the exact memory size we would get if
397 we have asked for the initial alloc_size amount of memory.
398 Let's get back to the number of macro map that amounts
400 LINEMAPS_ALLOCATED (set
, macro_map_p
) =
401 alloc_size
/ map_size
;
403 /* And now let's really do the re-allocation. */
407 = (line_map_macro
*) (*reallocator
) (set
->info_macro
.maps
,
408 (LINEMAPS_ALLOCATED (set
, macro_map_p
)
410 result
= &set
->info_macro
.maps
[LINEMAPS_USED (set
, macro_map_p
)];
414 set
->info_ordinary
.maps
=
415 (line_map_ordinary
*) (*reallocator
) (set
->info_ordinary
.maps
,
416 (LINEMAPS_ALLOCATED (set
, macro_map_p
)
418 result
= &set
->info_ordinary
.maps
[LINEMAPS_USED (set
, macro_map_p
)];
421 ((LINEMAPS_ALLOCATED (set
, macro_map_p
)
422 - LINEMAPS_USED (set
, macro_map_p
))
428 result
= &set
->info_macro
.maps
[LINEMAPS_USED (set
, macro_map_p
)];
430 result
= &set
->info_ordinary
.maps
[LINEMAPS_USED (set
, macro_map_p
)];
433 LINEMAPS_USED (set
, macro_map_p
)++;
435 result
->reason
= reason
;
439 /* Add a mapping of logical source line to physical source file and
442 The text pointed to by TO_FILE must have a lifetime
443 at least as long as the final call to lookup_line (). An empty
444 TO_FILE means standard input. If reason is LC_LEAVE, and
445 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
446 natural values considering the file we are returning to.
448 FROM_LINE should be monotonic increasing across calls to this
449 function. A call to this function can relocate the previous set of
450 maps, so any stored line_map pointers should not be used. */
452 const struct line_map
*
453 linemap_add (struct line_maps
*set
, enum lc_reason reason
,
454 unsigned int sysp
, const char *to_file
, linenum_type to_line
)
456 /* Generate a start_location above the current highest_location.
457 If possible, make the low range bits be zero. */
458 source_location start_location
;
459 if (set
->highest_location
< LINE_MAP_MAX_LOCATION_WITH_COLS
)
461 start_location
= set
->highest_location
+ (1 << set
->default_range_bits
);
462 if (set
->default_range_bits
)
463 start_location
&= ~((1 << set
->default_range_bits
) - 1);
464 linemap_assert (0 == (start_location
465 & ((1 << set
->default_range_bits
) - 1)));
468 start_location
= set
->highest_location
+ 1;
470 linemap_assert (!(LINEMAPS_ORDINARY_USED (set
)
472 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set
)))));
474 /* When we enter the file for the first time reason cannot be
476 linemap_assert (!(set
->depth
== 0 && reason
== LC_RENAME
));
478 /* If we are leaving the main file, return a NULL map. */
479 if (reason
== LC_LEAVE
480 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set
))
487 linemap_assert (reason
!= LC_ENTER_MACRO
);
488 line_map_ordinary
*map
= linemap_check_ordinary (new_linemap (set
, reason
));
490 if (to_file
&& *to_file
== '\0' && reason
!= LC_RENAME_VERBATIM
)
493 if (reason
== LC_RENAME_VERBATIM
)
496 if (reason
== LC_LEAVE
)
498 /* When we are just leaving an "included" file, and jump to the next
499 location inside the "includer" right after the #include
500 "included", this variable points the map in use right before the
501 #include "included", inside the same "includer" file. */
502 line_map_ordinary
*from
;
504 linemap_assert (!MAIN_FILE_P (map
- 1));
505 /* (MAP - 1) points to the map we are leaving. The
506 map from which (MAP - 1) got included should be the map
507 that comes right before MAP in the same file. */
508 from
= INCLUDED_FROM (set
, map
- 1);
510 /* A TO_FILE of NULL is special - we use the natural values. */
513 to_file
= ORDINARY_MAP_FILE_NAME (from
);
514 to_line
= SOURCE_LINE (from
, from
[1].start_location
);
515 sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (from
);
518 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from
),
523 map
->start_location
= start_location
;
524 map
->to_file
= to_file
;
525 map
->to_line
= to_line
;
526 LINEMAPS_ORDINARY_CACHE (set
) = LINEMAPS_ORDINARY_USED (set
) - 1;
527 map
->m_column_and_range_bits
= 0;
528 map
->m_range_bits
= 0;
529 set
->highest_location
= start_location
;
530 set
->highest_line
= start_location
;
531 set
->max_column_hint
= 0;
533 /* This assertion is placed after set->highest_location has
534 been updated, since the latter affects
535 linemap_location_from_macro_expansion_p, which ultimately affects
537 linemap_assert (pure_location_p (set
, start_location
));
539 if (reason
== LC_ENTER
)
542 set
->depth
== 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set
) - 2);
544 if (set
->trace_includes
)
545 trace_include (set
, map
);
547 else if (reason
== LC_RENAME
)
548 map
->included_from
= ORDINARY_MAP_INCLUDER_FILE_INDEX (&map
[-1]);
549 else if (reason
== LC_LEAVE
)
553 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set
, map
- 1));
559 /* Returns TRUE if the line table set tracks token locations across
560 macro expansion, FALSE otherwise. */
563 linemap_tracks_macro_expansion_locs_p (struct line_maps
*set
)
565 return LINEMAPS_MACRO_MAPS (set
) != NULL
;
568 /* Create a macro map. A macro map encodes source locations of tokens
569 that are part of a macro replacement-list, at a macro expansion
570 point. See the extensive comments of struct line_map and struct
571 line_map_macro, in line-map.h.
573 This map shall be created when the macro is expanded. The map
574 encodes the source location of the expansion point of the macro as
575 well as the "original" source location of each token that is part
576 of the macro replacement-list. If a macro is defined but never
577 expanded, it has no macro map. SET is the set of maps the macro
578 map should be part of. MACRO_NODE is the macro which the new macro
579 map should encode source locations for. EXPANSION is the location
580 of the expansion point of MACRO. For function-like macros
581 invocations, it's best to make it point to the closing parenthesis
582 of the macro, rather than the the location of the first character
583 of the macro. NUM_TOKENS is the number of tokens that are part of
584 the replacement-list of MACRO.
586 Note that when we run out of the integer space available for source
587 locations, this function returns NULL. In that case, callers of
588 this function cannot encode {line,column} pairs into locations of
589 macro tokens anymore. */
591 const line_map_macro
*
592 linemap_enter_macro (struct line_maps
*set
, struct cpp_hashnode
*macro_node
,
593 source_location expansion
, unsigned int num_tokens
)
596 source_location start_location
;
597 /* Cast away extern "C" from the type of xrealloc. */
598 line_map_realloc reallocator
= (set
->reallocator
600 : (line_map_realloc
) xrealloc
);
602 start_location
= LINEMAPS_MACRO_LOWEST_LOCATION (set
) - num_tokens
;
604 if (start_location
<= set
->highest_line
605 || start_location
> LINEMAPS_MACRO_LOWEST_LOCATION (set
))
606 /* We ran out of macro map space. */
609 map
= linemap_check_macro (new_linemap (set
, LC_ENTER_MACRO
));
611 map
->start_location
= start_location
;
612 map
->macro
= macro_node
;
613 map
->n_tokens
= num_tokens
;
615 = (source_location
*) reallocator (NULL
,
617 * sizeof (source_location
));
618 map
->expansion
= expansion
;
619 memset (MACRO_MAP_LOCATIONS (map
), 0,
620 num_tokens
* sizeof (source_location
));
622 LINEMAPS_MACRO_CACHE (set
) = LINEMAPS_MACRO_USED (set
) - 1;
627 /* Create and return a virtual location for a token that is part of a
628 macro expansion-list at a macro expansion point. See the comment
629 inside struct line_map_macro to see what an expansion-list exactly
632 A call to this function must come after a call to
635 MAP is the map into which the source location is created. TOKEN_NO
636 is the index of the token in the macro replacement-list, starting
639 ORIG_LOC is the location of the token outside of this macro
640 expansion. If the token comes originally from the macro
641 definition, it is the locus in the macro definition; otherwise it
642 is a location in the context of the caller of this macro expansion
643 (which is a virtual location or a source location if the caller is
644 itself a macro expansion or not).
646 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
647 either of the token itself or of a macro parameter that it
651 linemap_add_macro_token (const line_map_macro
*map
,
652 unsigned int token_no
,
653 source_location orig_loc
,
654 source_location orig_parm_replacement_loc
)
656 source_location result
;
658 linemap_assert (linemap_macro_expansion_map_p (map
));
659 linemap_assert (token_no
< MACRO_MAP_NUM_MACRO_TOKENS (map
));
661 MACRO_MAP_LOCATIONS (map
)[2 * token_no
] = orig_loc
;
662 MACRO_MAP_LOCATIONS (map
)[2 * token_no
+ 1] = orig_parm_replacement_loc
;
664 result
= MAP_START_LOCATION (map
) + token_no
;
668 /* Return a source_location for the start (i.e. column==0) of
669 (physical) line TO_LINE in the current source file (as in the
670 most recent linemap_add). MAX_COLUMN_HINT is the highest column
671 number we expect to use in this line (but it does not change
672 the highest_location). */
675 linemap_line_start (struct line_maps
*set
, linenum_type to_line
,
676 unsigned int max_column_hint
)
678 line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
679 source_location highest
= set
->highest_location
;
681 linenum_type last_line
=
682 SOURCE_LINE (map
, set
->highest_line
);
683 int line_delta
= to_line
- last_line
;
684 bool add_map
= false;
685 linemap_assert (map
->m_column_and_range_bits
>= map
->m_range_bits
);
686 int effective_column_bits
= map
->m_column_and_range_bits
- map
->m_range_bits
;
690 && line_delta
* map
->m_column_and_range_bits
> 1000)
691 || (max_column_hint
>= (1U << effective_column_bits
))
692 || (max_column_hint
<= 80 && effective_column_bits
>= 10)
693 || (highest
> LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
694 && map
->m_range_bits
> 0)
695 || (highest
> LINE_MAP_MAX_LOCATION_WITH_COLS
696 && (set
->max_column_hint
|| highest
>= LINE_MAP_MAX_SOURCE_LOCATION
)))
699 max_column_hint
= set
->max_column_hint
;
704 if (max_column_hint
> LINE_MAP_MAX_COLUMN_NUMBER
705 || highest
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
707 /* If the column number is ridiculous or we've allocated a huge
708 number of source_locations, give up on column numbers
709 (and on packed ranges). */
713 if (highest
> LINE_MAP_MAX_SOURCE_LOCATION
)
719 if (highest
<= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
)
720 range_bits
= set
->default_range_bits
;
723 while (max_column_hint
>= (1U << column_bits
))
725 max_column_hint
= 1U << column_bits
;
726 column_bits
+= range_bits
;
728 /* Allocate the new line_map. However, if the current map only has a
729 single line we can sometimes just increase its column_bits instead. */
731 || last_line
!= ORDINARY_MAP_STARTING_LINE_NUMBER (map
)
732 || SOURCE_COLUMN (map
, highest
) >= (1U << column_bits
)
733 || range_bits
< map
->m_range_bits
)
734 map
= linemap_check_ordinary
735 (const_cast <line_map
*>
736 (linemap_add (set
, LC_RENAME
,
737 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
),
738 ORDINARY_MAP_FILE_NAME (map
),
740 map
->m_column_and_range_bits
= column_bits
;
741 map
->m_range_bits
= range_bits
;
742 r
= (MAP_START_LOCATION (map
)
743 + ((to_line
- ORDINARY_MAP_STARTING_LINE_NUMBER (map
))
747 r
= set
->highest_line
+ (line_delta
<< map
->m_column_and_range_bits
);
749 /* Locations of ordinary tokens are always lower than locations of
751 if (r
>= LINEMAPS_MACRO_LOWEST_LOCATION (set
))
754 set
->highest_line
= r
;
755 if (r
> set
->highest_location
)
756 set
->highest_location
= r
;
757 set
->max_column_hint
= max_column_hint
;
759 /* At this point, we expect one of:
760 (a) the normal case: a "pure" location with 0 range bits, or
761 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
762 columns anymore (or ranges), or
763 (c) we're in a region with a column hint exceeding
764 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
765 with column_bits == 0. */
766 linemap_assert (pure_location_p (set
, r
)
767 || r
>= LINE_MAP_MAX_LOCATION_WITH_COLS
768 || map
->m_column_and_range_bits
== 0);
769 linemap_assert (SOURCE_LINE (map
, r
) == to_line
);
773 /* Encode and return a source_location from a column number. The
774 source line considered is the last source line used to call
775 linemap_line_start, i.e, the last source line which a location was
779 linemap_position_for_column (struct line_maps
*set
, unsigned int to_column
)
781 source_location r
= set
->highest_line
;
784 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set
)));
786 if (to_column
>= set
->max_column_hint
)
788 if (r
> LINE_MAP_MAX_LOCATION_WITH_COLS
789 || to_column
> LINE_MAP_MAX_COLUMN_NUMBER
)
791 /* Running low on source_locations - disable column numbers. */
796 line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
797 r
= linemap_line_start (set
, SOURCE_LINE (map
, r
), to_column
+ 50);
800 line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
801 r
= r
+ (to_column
<< map
->m_range_bits
);
802 if (r
>= set
->highest_location
)
803 set
->highest_location
= r
;
807 /* Encode and return a source location from a given line and
811 linemap_position_for_line_and_column (line_maps
*set
,
812 const line_map_ordinary
*ord_map
,
816 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
) <= line
);
818 source_location r
= MAP_START_LOCATION (ord_map
);
819 r
+= ((line
- ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
))
820 << ord_map
->m_column_and_range_bits
);
821 if (r
<= LINE_MAP_MAX_LOCATION_WITH_COLS
)
822 r
+= ((column
& ((1 << ord_map
->m_column_and_range_bits
) - 1))
823 << ord_map
->m_range_bits
);
824 source_location upper_limit
= LINEMAPS_MACRO_LOWEST_LOCATION (set
);
825 if (r
>= upper_limit
)
827 if (r
> set
->highest_location
)
828 set
->highest_location
= r
;
832 /* Encode and return a source_location starting from location LOC and
833 shifting it by COLUMN_OFFSET columns. This function does not support
834 virtual locations. */
837 linemap_position_for_loc_and_offset (struct line_maps
*set
,
839 unsigned int column_offset
)
841 const line_map_ordinary
* map
= NULL
;
843 if (IS_ADHOC_LOC (loc
))
844 loc
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
846 /* This function does not support virtual locations yet. */
847 if (linemap_assert_fails
848 (!linemap_location_from_macro_expansion_p (set
, loc
)))
851 if (column_offset
== 0
852 /* Adding an offset to a reserved location (like
853 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
854 sense. So let's leave the location intact in that case. */
855 || loc
< RESERVED_LOCATION_COUNT
)
858 /* We find the real location and shift it. */
859 loc
= linemap_resolve_location (set
, loc
, LRK_SPELLING_LOCATION
, &map
);
860 /* The new location (loc + offset) should be higher than the first
861 location encoded by MAP. This can fail if the line information
862 is messed up because of line directives (see PR66415). */
863 if (MAP_START_LOCATION (map
) >= loc
+ (column_offset
<< map
->m_range_bits
))
866 linenum_type line
= SOURCE_LINE (map
, loc
);
867 unsigned int column
= SOURCE_COLUMN (map
, loc
);
869 /* If MAP is not the last line map of its set, then the new location
870 (loc + offset) should be less than the first location encoded by
871 the next line map of the set. Otherwise, we try to encode the
872 location in the next map. */
873 while (map
!= LINEMAPS_LAST_ORDINARY_MAP (set
)
874 && (loc
+ (column_offset
<< map
->m_range_bits
)
875 >= MAP_START_LOCATION (&map
[1])))
878 /* If the next map starts in a higher line, we cannot encode the
880 if (line
< ORDINARY_MAP_STARTING_LINE_NUMBER (map
))
884 column
+= column_offset
;
885 if (linemap_assert_fails (column
< (1u << map
->m_column_and_range_bits
)))
889 linemap_position_for_line_and_column (set
, map
, line
, column
);
890 if (linemap_assert_fails (r
<= set
->highest_location
)
891 || linemap_assert_fails (map
== linemap_lookup (set
, r
)))
897 /* Given a virtual source location yielded by a map (either an
898 ordinary or a macro map), returns that map. */
900 const struct line_map
*
901 linemap_lookup (struct line_maps
*set
, source_location line
)
903 if (IS_ADHOC_LOC (line
))
904 line
= set
->location_adhoc_data_map
.data
[line
& MAX_SOURCE_LOCATION
].locus
;
905 if (linemap_location_from_macro_expansion_p (set
, line
))
906 return linemap_macro_map_lookup (set
, line
);
907 return linemap_ordinary_map_lookup (set
, line
);
910 /* Given a source location yielded by an ordinary map, returns that
911 map. Since the set is built chronologically, the logical lines are
912 monotonic increasing, and so the list is sorted and we can use a
915 static const line_map_ordinary
*
916 linemap_ordinary_map_lookup (struct line_maps
*set
, source_location line
)
918 unsigned int md
, mn
, mx
;
919 const line_map_ordinary
*cached
, *result
;
921 if (IS_ADHOC_LOC (line
))
922 line
= set
->location_adhoc_data_map
.data
[line
& MAX_SOURCE_LOCATION
].locus
;
924 if (set
== NULL
|| line
< RESERVED_LOCATION_COUNT
)
927 mn
= LINEMAPS_ORDINARY_CACHE (set
);
928 mx
= LINEMAPS_ORDINARY_USED (set
);
930 cached
= LINEMAPS_ORDINARY_MAP_AT (set
, mn
);
931 /* We should get a segfault if no line_maps have been added yet. */
932 if (line
>= MAP_START_LOCATION (cached
))
934 if (mn
+ 1 == mx
|| line
< MAP_START_LOCATION (&cached
[1]))
946 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set
, md
)) > line
)
952 LINEMAPS_ORDINARY_CACHE (set
) = mn
;
953 result
= LINEMAPS_ORDINARY_MAP_AT (set
, mn
);
954 linemap_assert (line
>= MAP_START_LOCATION (result
));
958 /* Given a source location yielded by a macro map, returns that map.
959 Since the set is built chronologically, the logical lines are
960 monotonic decreasing, and so the list is sorted and we can use a
963 static const line_map_macro
*
964 linemap_macro_map_lookup (struct line_maps
*set
, source_location line
)
966 unsigned int md
, mn
, mx
;
967 const struct line_map_macro
*cached
, *result
;
969 if (IS_ADHOC_LOC (line
))
970 line
= set
->location_adhoc_data_map
.data
[line
& MAX_SOURCE_LOCATION
].locus
;
972 linemap_assert (line
>= LINEMAPS_MACRO_LOWEST_LOCATION (set
));
977 mn
= LINEMAPS_MACRO_CACHE (set
);
978 mx
= LINEMAPS_MACRO_USED (set
);
979 cached
= LINEMAPS_MACRO_MAP_AT (set
, mn
);
981 if (line
>= MAP_START_LOCATION (cached
))
983 if (mn
== 0 || line
< MAP_START_LOCATION (&cached
[-1]))
992 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set
, md
)) > line
)
998 LINEMAPS_MACRO_CACHE (set
) = mx
;
999 result
= LINEMAPS_MACRO_MAP_AT (set
, LINEMAPS_MACRO_CACHE (set
));
1000 linemap_assert (MAP_START_LOCATION (result
) <= line
);
1005 /* Return TRUE if MAP encodes locations coming from a macro
1006 replacement-list at macro expansion point. */
1009 linemap_macro_expansion_map_p (const struct line_map
*map
)
1013 return (map
->reason
== LC_ENTER_MACRO
);
1016 /* If LOCATION is the locus of a token in a replacement-list of a
1017 macro expansion return the location of the macro expansion point.
1019 Read the comments of struct line_map and struct line_map_macro in
1020 line-map.h to understand what a macro expansion point is. */
1022 static source_location
1023 linemap_macro_map_loc_to_exp_point (const line_map_macro
*map
,
1024 source_location location ATTRIBUTE_UNUSED
)
1026 linemap_assert (linemap_macro_expansion_map_p (map
)
1027 && location
>= MAP_START_LOCATION (map
));
1029 /* Make sure LOCATION is correct. */
1030 linemap_assert ((location
- MAP_START_LOCATION (map
))
1031 < MACRO_MAP_NUM_MACRO_TOKENS (map
));
1033 return MACRO_MAP_EXPANSION_POINT_LOCATION (map
);
1036 /* LOCATION is the source location of a token that belongs to a macro
1037 replacement-list as part of the macro expansion denoted by MAP.
1039 Return the location of the token at the definition point of the
1042 static source_location
1043 linemap_macro_map_loc_to_def_point (const line_map_macro
*map
,
1044 source_location location
)
1048 linemap_assert (linemap_macro_expansion_map_p (map
)
1049 && location
>= MAP_START_LOCATION (map
));
1050 linemap_assert (location
>= RESERVED_LOCATION_COUNT
);
1052 token_no
= location
- MAP_START_LOCATION (map
);
1053 linemap_assert (token_no
< MACRO_MAP_NUM_MACRO_TOKENS (map
));
1055 location
= MACRO_MAP_LOCATIONS (map
)[2 * token_no
+ 1];
1060 /* If LOCATION is the locus of a token that is an argument of a
1061 function-like macro M and appears in the expansion of M, return the
1062 locus of that argument in the context of the caller of M.
1064 In other words, this returns the xI location presented in the
1065 comments of line_map_macro above. */
1067 linemap_macro_map_loc_unwind_toward_spelling (line_maps
*set
,
1068 const line_map_macro
* map
,
1069 source_location location
)
1073 if (IS_ADHOC_LOC (location
))
1074 location
= get_location_from_adhoc_loc (set
, location
);
1076 linemap_assert (linemap_macro_expansion_map_p (map
)
1077 && location
>= MAP_START_LOCATION (map
));
1078 linemap_assert (location
>= RESERVED_LOCATION_COUNT
);
1079 linemap_assert (!IS_ADHOC_LOC (location
));
1081 token_no
= location
- MAP_START_LOCATION (map
);
1082 linemap_assert (token_no
< MACRO_MAP_NUM_MACRO_TOKENS (map
));
1084 location
= MACRO_MAP_LOCATIONS (map
)[2 * token_no
];
1089 /* Return the source line number corresponding to source location
1090 LOCATION. SET is the line map set LOCATION comes from. If
1091 LOCATION is the source location of token that is part of the
1092 replacement-list of a macro expansion return the line number of the
1093 macro expansion point. */
1096 linemap_get_expansion_line (struct line_maps
*set
,
1097 source_location location
)
1099 const line_map_ordinary
*map
= NULL
;
1101 if (IS_ADHOC_LOC (location
))
1102 location
= set
->location_adhoc_data_map
.data
[location
1103 & MAX_SOURCE_LOCATION
].locus
;
1105 if (location
< RESERVED_LOCATION_COUNT
)
1109 linemap_macro_loc_to_exp_point (set
, location
, &map
);
1111 return SOURCE_LINE (map
, location
);
1114 /* Return the path of the file corresponding to source code location
1117 If LOCATION is the source location of token that is part of the
1118 replacement-list of a macro expansion return the file path of the
1119 macro expansion point.
1121 SET is the line map set LOCATION comes from. */
1124 linemap_get_expansion_filename (struct line_maps
*set
,
1125 source_location location
)
1127 const struct line_map_ordinary
*map
= NULL
;
1129 if (IS_ADHOC_LOC (location
))
1130 location
= set
->location_adhoc_data_map
.data
[location
1131 & MAX_SOURCE_LOCATION
].locus
;
1133 if (location
< RESERVED_LOCATION_COUNT
)
1137 linemap_macro_loc_to_exp_point (set
, location
, &map
);
1139 return LINEMAP_FILE (map
);
1142 /* Return the name of the macro associated to MACRO_MAP. */
1145 linemap_map_get_macro_name (const line_map_macro
*macro_map
)
1147 linemap_assert (macro_map
&& linemap_macro_expansion_map_p (macro_map
));
1148 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map
));
1151 /* Return a positive value if LOCATION is the locus of a token that is
1152 located in a system header, O otherwise. It returns 1 if LOCATION
1153 is the locus of a token that is located in a system header, and 2
1154 if LOCATION is the locus of a token located in a C system header
1155 that therefore needs to be extern "C" protected in C++.
1157 Note that this function returns 1 if LOCATION belongs to a token
1158 that is part of a macro replacement-list defined in a system
1159 header, but expanded in a non-system file. */
1162 linemap_location_in_system_header_p (struct line_maps
*set
,
1163 source_location location
)
1165 const struct line_map
*map
= NULL
;
1167 if (IS_ADHOC_LOC (location
))
1168 location
= set
->location_adhoc_data_map
.data
[location
1169 & MAX_SOURCE_LOCATION
].locus
;
1171 if (location
< RESERVED_LOCATION_COUNT
)
1174 /* Let's look at where the token for LOCATION comes from. */
1177 map
= linemap_lookup (set
, location
);
1180 if (!linemap_macro_expansion_map_p (map
))
1181 /* It's a normal token. */
1182 return LINEMAP_SYSP (linemap_check_ordinary (map
));
1185 const line_map_macro
*macro_map
= linemap_check_macro (map
);
1187 /* It's a token resulting from a macro expansion. */
1188 source_location loc
=
1189 linemap_macro_map_loc_unwind_toward_spelling (set
, macro_map
, location
);
1190 if (loc
< RESERVED_LOCATION_COUNT
)
1191 /* This token might come from a built-in macro. Let's
1192 look at where that macro got expanded. */
1193 location
= linemap_macro_map_loc_to_exp_point (macro_map
, location
);
1204 /* Return TRUE if LOCATION is a source code location of a token coming
1205 from a macro replacement-list at a macro expansion point, FALSE
1209 linemap_location_from_macro_expansion_p (const struct line_maps
*set
,
1210 source_location location
)
1212 if (IS_ADHOC_LOC (location
))
1213 location
= set
->location_adhoc_data_map
.data
[location
1214 & MAX_SOURCE_LOCATION
].locus
;
1216 linemap_assert (location
<= MAX_SOURCE_LOCATION
1217 && (set
->highest_location
1218 < LINEMAPS_MACRO_LOWEST_LOCATION (set
)));
1221 return (location
> set
->highest_location
);
1224 /* Given two virtual locations *LOC0 and *LOC1, return the first
1225 common macro map in their macro expansion histories. Return NULL
1226 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1227 virtual location of the token inside the resulting macro. */
1229 static const struct line_map
*
1230 first_map_in_common_1 (struct line_maps
*set
,
1231 source_location
*loc0
,
1232 source_location
*loc1
)
1234 source_location l0
= *loc0
, l1
= *loc1
;
1235 const struct line_map
*map0
= linemap_lookup (set
, l0
),
1236 *map1
= linemap_lookup (set
, l1
);
1238 while (linemap_macro_expansion_map_p (map0
)
1239 && linemap_macro_expansion_map_p (map1
)
1242 if (MAP_START_LOCATION (map0
) < MAP_START_LOCATION (map1
))
1244 l0
= linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0
),
1246 map0
= linemap_lookup (set
, l0
);
1250 l1
= linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1
),
1252 map1
= linemap_lookup (set
, l1
);
1265 /* Given two virtual locations LOC0 and LOC1, return the first common
1266 macro map in their macro expansion histories. Return NULL if no
1267 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1268 virtual location of the token inside the resulting macro, upon
1269 return of a non-NULL result. */
1271 static const struct line_map
*
1272 first_map_in_common (struct line_maps
*set
,
1273 source_location loc0
,
1274 source_location loc1
,
1275 source_location
*res_loc0
,
1276 source_location
*res_loc1
)
1281 return first_map_in_common_1 (set
, res_loc0
, res_loc1
);
1284 /* Return a positive value if PRE denotes the location of a token that
1285 comes before the token of POST, 0 if PRE denotes the location of
1286 the same token as the token for POST, and a negative value
1290 linemap_compare_locations (struct line_maps
*set
,
1291 source_location pre
,
1292 source_location post
)
1294 bool pre_virtual_p
, post_virtual_p
;
1295 source_location l0
= pre
, l1
= post
;
1297 if (IS_ADHOC_LOC (l0
))
1298 l0
= get_location_from_adhoc_loc (set
, l0
);
1299 if (IS_ADHOC_LOC (l1
))
1300 l1
= get_location_from_adhoc_loc (set
, l1
);
1305 if ((pre_virtual_p
= linemap_location_from_macro_expansion_p (set
, l0
)))
1306 l0
= linemap_resolve_location (set
, l0
,
1307 LRK_MACRO_EXPANSION_POINT
,
1310 if ((post_virtual_p
= linemap_location_from_macro_expansion_p (set
, l1
)))
1311 l1
= linemap_resolve_location (set
, l1
,
1312 LRK_MACRO_EXPANSION_POINT
,
1319 /* So pre and post represent two tokens that are present in a
1320 same macro expansion. Let's see if the token for pre was
1321 before the token for post in that expansion. */
1323 const struct line_map
*map
=
1324 first_map_in_common (set
, pre
, post
, &l0
, &l1
);
1327 /* This should not be possible. */
1330 i0
= l0
- MAP_START_LOCATION (map
);
1331 i1
= l1
- MAP_START_LOCATION (map
);
1335 if (IS_ADHOC_LOC (l0
))
1336 l0
= get_location_from_adhoc_loc (set
, l0
);
1337 if (IS_ADHOC_LOC (l1
))
1338 l1
= get_location_from_adhoc_loc (set
, l1
);
1343 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1346 trace_include (const struct line_maps
*set
, const line_map_ordinary
*map
)
1348 unsigned int i
= set
->depth
;
1353 fprintf (stderr
, " %s\n", ORDINARY_MAP_FILE_NAME (map
));
1356 /* Return the spelling location of the token wherever it comes from,
1357 whether part of a macro definition or not.
1359 This is a subroutine for linemap_resolve_location. */
1361 static source_location
1362 linemap_macro_loc_to_spelling_point (struct line_maps
*set
,
1363 source_location location
,
1364 const line_map_ordinary
**original_map
)
1366 struct line_map
*map
;
1367 linemap_assert (set
&& location
>= RESERVED_LOCATION_COUNT
);
1371 map
= const_cast <line_map
*> (linemap_lookup (set
, location
));
1372 if (!linemap_macro_expansion_map_p (map
))
1376 = linemap_macro_map_loc_unwind_toward_spelling
1377 (set
, linemap_check_macro (map
),
1382 *original_map
= linemap_check_ordinary (map
);
1386 /* If LOCATION is the source location of a token that belongs to a
1387 macro replacement-list -- as part of a macro expansion -- then
1388 return the location of the token at the definition point of the
1389 macro. Otherwise, return LOCATION. SET is the set of maps
1390 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1391 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1392 returned location comes from.
1394 This is a subroutine of linemap_resolve_location. */
1396 static source_location
1397 linemap_macro_loc_to_def_point (struct line_maps
*set
,
1398 source_location location
,
1399 const line_map_ordinary
**original_map
)
1401 struct line_map
*map
;
1403 if (IS_ADHOC_LOC (location
))
1404 location
= set
->location_adhoc_data_map
.data
[location
1405 & MAX_SOURCE_LOCATION
].locus
;
1407 linemap_assert (set
&& location
>= RESERVED_LOCATION_COUNT
);
1411 map
= const_cast <line_map
*> (linemap_lookup (set
, location
));
1412 if (!linemap_macro_expansion_map_p (map
))
1416 linemap_macro_map_loc_to_def_point (linemap_check_macro (map
),
1421 *original_map
= linemap_check_ordinary (map
);
1425 /* If LOCATION is the source location of a token that belongs to a
1426 macro replacement-list -- at a macro expansion point -- then return
1427 the location of the topmost expansion point of the macro. We say
1428 topmost because if we are in the context of a nested macro
1429 expansion, the function returns the source location of the first
1430 macro expansion that triggered the nested expansions.
1432 Otherwise, return LOCATION. SET is the set of maps location come
1433 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1434 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1435 location comes from.
1437 This is a subroutine of linemap_resolve_location. */
1439 static source_location
1440 linemap_macro_loc_to_exp_point (struct line_maps
*set
,
1441 source_location location
,
1442 const line_map_ordinary
**original_map
)
1444 struct line_map
*map
;
1446 if (IS_ADHOC_LOC (location
))
1447 location
= set
->location_adhoc_data_map
.data
[location
1448 & MAX_SOURCE_LOCATION
].locus
;
1450 linemap_assert (set
&& location
>= RESERVED_LOCATION_COUNT
);
1454 map
= const_cast <line_map
*> (linemap_lookup (set
, location
));
1455 if (!linemap_macro_expansion_map_p (map
))
1457 location
= linemap_macro_map_loc_to_exp_point (linemap_check_macro (map
),
1462 *original_map
= linemap_check_ordinary (map
);
1466 /* Resolve a virtual location into either a spelling location, an
1467 expansion point location or a token argument replacement point
1468 location. Return the map that encodes the virtual location as well
1469 as the resolved location.
1471 If LOC is *NOT* the location of a token resulting from the
1472 expansion of a macro, then the parameter LRK (which stands for
1473 Location Resolution Kind) is ignored and the resulting location
1474 just equals the one given in argument.
1476 Now if LOC *IS* the location of a token resulting from the
1477 expansion of a macro, this is what happens.
1479 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1480 -------------------------------
1482 The virtual location is resolved to the first macro expansion point
1483 that led to this macro expansion.
1485 * If LRK is set to LRK_SPELLING_LOCATION
1486 -------------------------------------
1488 The virtual location is resolved to the locus where the token has
1489 been spelled in the source. This can follow through all the macro
1490 expansions that led to the token.
1492 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1493 --------------------------------------
1495 The virtual location is resolved to the locus of the token in the
1496 context of the macro definition.
1498 If LOC is the locus of a token that is an argument of a
1499 function-like macro [replacing a parameter in the replacement list
1500 of the macro] the virtual location is resolved to the locus of the
1501 parameter that is replaced, in the context of the definition of the
1504 If LOC is the locus of a token that is not an argument of a
1505 function-like macro, then the function behaves as if LRK was set to
1506 LRK_SPELLING_LOCATION.
1508 If MAP is not NULL, *MAP is set to the map encoding the
1509 returned location. Note that if the returned location wasn't originally
1510 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1511 resolves to a location reserved for the client code, like
1512 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1515 linemap_resolve_location (struct line_maps
*set
,
1516 source_location loc
,
1517 enum location_resolution_kind lrk
,
1518 const line_map_ordinary
**map
)
1520 source_location locus
= loc
;
1521 if (IS_ADHOC_LOC (loc
))
1522 locus
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
1524 if (locus
< RESERVED_LOCATION_COUNT
)
1526 /* A reserved location wasn't encoded in a map. Let's return a
1527 NULL map here, just like what linemap_ordinary_map_lookup
1536 case LRK_MACRO_EXPANSION_POINT
:
1537 loc
= linemap_macro_loc_to_exp_point (set
, loc
, map
);
1539 case LRK_SPELLING_LOCATION
:
1540 loc
= linemap_macro_loc_to_spelling_point (set
, loc
, map
);
1542 case LRK_MACRO_DEFINITION_LOCATION
:
1543 loc
= linemap_macro_loc_to_def_point (set
, loc
, map
);
1552 Suppose that LOC is the virtual location of a token T coming from
1553 the expansion of a macro M. This function then steps up to get the
1554 location L of the point where M got expanded. If L is a spelling
1555 location inside a macro expansion M', then this function returns
1556 the locus of the point where M' was expanded. Said otherwise, this
1557 function returns the location of T in the context that triggered
1560 *LOC_MAP must be set to the map of LOC. This function then sets it
1561 to the map of the returned location. */
1564 linemap_unwind_toward_expansion (struct line_maps
*set
,
1565 source_location loc
,
1566 const struct line_map
**map
)
1568 source_location resolved_location
;
1569 const line_map_macro
*macro_map
= linemap_check_macro (*map
);
1570 const struct line_map
*resolved_map
;
1572 if (IS_ADHOC_LOC (loc
))
1573 loc
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
1576 linemap_macro_map_loc_unwind_toward_spelling (set
, macro_map
, loc
);
1577 resolved_map
= linemap_lookup (set
, resolved_location
);
1579 if (!linemap_macro_expansion_map_p (resolved_map
))
1581 resolved_location
= linemap_macro_map_loc_to_exp_point (macro_map
, loc
);
1582 resolved_map
= linemap_lookup (set
, resolved_location
);
1585 *map
= resolved_map
;
1586 return resolved_location
;
1589 /* If LOC is the virtual location of a token coming from the expansion
1590 of a macro M and if its spelling location is reserved (e.g, a
1591 location for a built-in token), then this function unwinds (using
1592 linemap_unwind_toward_expansion) the location until a location that
1593 is not reserved and is not in a system header is reached. In other
1594 words, this unwinds the reserved location until a location that is
1595 in real source code is reached.
1597 Otherwise, if the spelling location for LOC is not reserved or if
1598 LOC doesn't come from the expansion of a macro, the function
1599 returns LOC as is and *MAP is not touched.
1601 *MAP is set to the map of the returned location if the later is
1602 different from LOC. */
1604 linemap_unwind_to_first_non_reserved_loc (struct line_maps
*set
,
1605 source_location loc
,
1606 const struct line_map
**map
)
1608 source_location resolved_loc
;
1609 const struct line_map
*map0
= NULL
;
1610 const line_map_ordinary
*map1
= NULL
;
1612 if (IS_ADHOC_LOC (loc
))
1613 loc
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
1615 map0
= linemap_lookup (set
, loc
);
1616 if (!linemap_macro_expansion_map_p (map0
))
1619 resolved_loc
= linemap_resolve_location (set
, loc
,
1620 LRK_SPELLING_LOCATION
,
1623 if (resolved_loc
>= RESERVED_LOCATION_COUNT
1624 && !LINEMAP_SYSP (map1
))
1627 while (linemap_macro_expansion_map_p (map0
)
1628 && (resolved_loc
< RESERVED_LOCATION_COUNT
1629 || LINEMAP_SYSP (map1
)))
1631 loc
= linemap_unwind_toward_expansion (set
, loc
, &map0
);
1632 resolved_loc
= linemap_resolve_location (set
, loc
,
1633 LRK_SPELLING_LOCATION
,
1642 /* Expand source code location LOC and return a user readable source
1643 code location. LOC must be a spelling (non-virtual) location. If
1644 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1645 location is returned. */
1648 linemap_expand_location (struct line_maps
*set
,
1649 const struct line_map
*map
,
1650 source_location loc
)
1653 expanded_location xloc
;
1655 memset (&xloc
, 0, sizeof (xloc
));
1656 if (IS_ADHOC_LOC (loc
))
1659 = set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].data
;
1660 loc
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
1663 if (loc
< RESERVED_LOCATION_COUNT
)
1664 /* The location for this token wasn't generated from a line map.
1665 It was probably a location for a builtin token, chosen by some
1666 client code. Let's not try to expand the location in that
1668 else if (map
== NULL
)
1669 /* We shouldn't be getting a NULL map with a location that is not
1670 reserved by the client code. */
1674 /* MAP must be an ordinary map and LOC must be non-virtual,
1675 encoded into this map, obviously; the accessors used on MAP
1676 below ensure it is ordinary. Let's just assert the
1677 non-virtualness of LOC here. */
1678 if (linemap_location_from_macro_expansion_p (set
, loc
))
1681 const line_map_ordinary
*ord_map
= linemap_check_ordinary (map
);
1683 xloc
.file
= LINEMAP_FILE (ord_map
);
1684 xloc
.line
= SOURCE_LINE (ord_map
, loc
);
1685 xloc
.column
= SOURCE_COLUMN (ord_map
, loc
);
1686 xloc
.sysp
= LINEMAP_SYSP (ord_map
) != 0;
1693 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1694 is NULL, use stderr. IS_MACRO is true if the caller wants to
1695 dump a macro map, false otherwise. */
1698 linemap_dump (FILE *stream
, struct line_maps
*set
, unsigned ix
, bool is_macro
)
1700 const char *lc_reasons_v
[LC_ENTER_MACRO
+ 1]
1701 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1704 const line_map
*map
;
1710 map
= LINEMAPS_ORDINARY_MAP_AT (set
, ix
);
1712 map
= LINEMAPS_MACRO_MAP_AT (set
, ix
);
1714 reason
= (map
->reason
<= LC_ENTER_MACRO
) ? lc_reasons_v
[map
->reason
] : "???";
1716 fprintf (stream
, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1717 ix
, (void *) map
, map
->start_location
, reason
,
1719 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map
)))
1723 const line_map_ordinary
*ord_map
= linemap_check_ordinary (map
);
1724 unsigned includer_ix
;
1725 const line_map_ordinary
*includer_map
;
1727 includer_ix
= ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map
);
1728 includer_map
= includer_ix
< LINEMAPS_ORDINARY_USED (set
)
1729 ? LINEMAPS_ORDINARY_MAP_AT (set
, includer_ix
)
1732 fprintf (stream
, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map
),
1733 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
));
1734 fprintf (stream
, "Included from: [%d] %s\n", includer_ix
,
1735 includer_map
? ORDINARY_MAP_FILE_NAME (includer_map
) : "None");
1739 const line_map_macro
*macro_map
= linemap_check_macro (map
);
1740 fprintf (stream
, "Macro: %s (%u tokens)\n",
1741 linemap_map_get_macro_name (macro_map
),
1742 MACRO_MAP_NUM_MACRO_TOKENS (macro_map
));
1745 fprintf (stream
, "\n");
1749 /* Dump debugging information about source location LOC into the file
1750 stream STREAM. SET is the line map set LOC comes from. */
1753 linemap_dump_location (struct line_maps
*set
,
1754 source_location loc
,
1757 const line_map_ordinary
*map
;
1758 source_location location
;
1759 const char *path
= "", *from
= "";
1760 int l
= -1, c
= -1, s
= -1, e
= -1;
1762 if (IS_ADHOC_LOC (loc
))
1763 loc
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
1769 linemap_resolve_location (set
, loc
, LRK_MACRO_DEFINITION_LOCATION
, &map
);
1772 /* Only reserved locations can be tolerated in this case. */
1773 linemap_assert (location
< RESERVED_LOCATION_COUNT
);
1776 path
= LINEMAP_FILE (map
);
1777 l
= SOURCE_LINE (map
, location
);
1778 c
= SOURCE_COLUMN (map
, location
);
1779 s
= LINEMAP_SYSP (map
) != 0;
1780 e
= location
!= loc
;
1784 from
= (INCLUDED_FROM (set
, map
))
1785 ? LINEMAP_FILE (INCLUDED_FROM (set
, map
))
1789 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1790 E: macro expansion?, LOC: original location, R: resolved location */
1791 fprintf (stream
, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1792 path
, from
, l
, c
, s
, (void*)map
, e
, loc
, location
);
1795 /* Return the highest location emitted for a given file for which
1796 there is a line map in SET. FILE_NAME is the file name to
1797 consider. If the function returns TRUE, *LOC is set to the highest
1798 location emitted for that file. */
1801 linemap_get_file_highest_location (struct line_maps
*set
,
1802 const char *file_name
,
1803 source_location
*loc
)
1805 /* If the set is empty or no ordinary map has been created then
1806 there is no file to look for ... */
1807 if (set
== NULL
|| set
->info_ordinary
.used
== 0)
1810 /* Now look for the last ordinary map created for FILE_NAME. */
1812 for (i
= set
->info_ordinary
.used
- 1; i
>= 0; --i
)
1814 const char *fname
= set
->info_ordinary
.maps
[i
].to_file
;
1815 if (fname
&& !filename_cmp (fname
, file_name
))
1822 /* The highest location for a given map is either the starting
1823 location of the next map minus one, or -- if the map is the
1824 latest one -- the highest location of the set. */
1825 source_location result
;
1826 if (i
== (int) set
->info_ordinary
.used
- 1)
1827 result
= set
->highest_location
;
1829 result
= set
->info_ordinary
.maps
[i
+ 1].start_location
- 1;
1835 /* Compute and return statistics about the memory consumption of some
1836 parts of the line table SET. */
1839 linemap_get_statistics (struct line_maps
*set
,
1840 struct linemap_stats
*s
)
1842 long ordinary_maps_allocated_size
, ordinary_maps_used_size
,
1843 macro_maps_allocated_size
, macro_maps_used_size
,
1844 macro_maps_locations_size
= 0, duplicated_macro_maps_locations_size
= 0;
1846 const line_map_macro
*cur_map
;
1848 ordinary_maps_allocated_size
=
1849 LINEMAPS_ORDINARY_ALLOCATED (set
) * sizeof (struct line_map_ordinary
);
1851 ordinary_maps_used_size
=
1852 LINEMAPS_ORDINARY_USED (set
) * sizeof (struct line_map_ordinary
);
1854 macro_maps_allocated_size
=
1855 LINEMAPS_MACRO_ALLOCATED (set
) * sizeof (struct line_map_macro
);
1857 for (cur_map
= LINEMAPS_MACRO_MAPS (set
);
1858 cur_map
&& cur_map
<= LINEMAPS_LAST_MACRO_MAP (set
);
1863 linemap_assert (linemap_macro_expansion_map_p (cur_map
));
1865 macro_maps_locations_size
+=
1866 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map
) * sizeof (source_location
);
1868 for (i
= 0; i
< 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map
); i
+= 2)
1870 if (MACRO_MAP_LOCATIONS (cur_map
)[i
] ==
1871 MACRO_MAP_LOCATIONS (cur_map
)[i
+ 1])
1872 duplicated_macro_maps_locations_size
+=
1873 sizeof (source_location
);
1877 macro_maps_used_size
=
1878 LINEMAPS_MACRO_USED (set
) * sizeof (struct line_map_macro
);
1880 s
->num_ordinary_maps_allocated
= LINEMAPS_ORDINARY_ALLOCATED (set
);
1881 s
->num_ordinary_maps_used
= LINEMAPS_ORDINARY_USED (set
);
1882 s
->ordinary_maps_allocated_size
= ordinary_maps_allocated_size
;
1883 s
->ordinary_maps_used_size
= ordinary_maps_used_size
;
1884 s
->num_expanded_macros
= num_expanded_macros_counter
;
1885 s
->num_macro_tokens
= num_macro_tokens_counter
;
1886 s
->num_macro_maps_used
= LINEMAPS_MACRO_USED (set
);
1887 s
->macro_maps_allocated_size
= macro_maps_allocated_size
;
1888 s
->macro_maps_locations_size
= macro_maps_locations_size
;
1889 s
->macro_maps_used_size
= macro_maps_used_size
;
1890 s
->duplicated_macro_maps_locations_size
=
1891 duplicated_macro_maps_locations_size
;
1892 s
->adhoc_table_size
= (set
->location_adhoc_data_map
.allocated
1893 * sizeof (struct location_adhoc_data
));
1894 s
->adhoc_table_entries_used
= set
->location_adhoc_data_map
.curr_loc
;
1898 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1899 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1900 specifies how many macro maps to dump. */
1903 line_table_dump (FILE *stream
, struct line_maps
*set
, unsigned int num_ordinary
,
1904 unsigned int num_macro
)
1914 fprintf (stream
, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set
));
1915 fprintf (stream
, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set
));
1916 fprintf (stream
, "Include stack depth: %d\n", set
->depth
);
1917 fprintf (stream
, "Highest location: %u\n", set
->highest_location
);
1921 fprintf (stream
, "\nOrdinary line maps\n");
1922 for (i
= 0; i
< num_ordinary
&& i
< LINEMAPS_ORDINARY_USED (set
); i
++)
1923 linemap_dump (stream
, set
, i
, false);
1924 fprintf (stream
, "\n");
1929 fprintf (stream
, "\nMacro line maps\n");
1930 for (i
= 0; i
< num_macro
&& i
< LINEMAPS_MACRO_USED (set
); i
++)
1931 linemap_dump (stream
, set
, i
, true);
1932 fprintf (stream
, "\n");
1936 /* struct source_range. */
1938 /* Is there any part of this range on the given line? */
1941 source_range::intersects_line_p (const char *file
, int line
) const
1943 expanded_location exploc_start
1944 = linemap_client_expand_location_to_spelling_point (m_start
);
1945 if (file
!= exploc_start
.file
)
1947 if (line
< exploc_start
.line
)
1949 expanded_location exploc_finish
1950 = linemap_client_expand_location_to_spelling_point (m_finish
);
1951 if (file
!= exploc_finish
.file
)
1953 if (line
> exploc_finish
.line
)
1958 /* class rich_location. */
1960 /* Construct a rich_location with location LOC as its initial range. */
1962 rich_location::rich_location (line_maps */
*set*/
, source_location loc
) :
1964 m_column_override (0),
1965 m_have_expanded_location (false),
1966 m_num_fixit_hints (0)
1968 add_range (loc
, true);
1971 /* The destructor for class rich_location. */
1973 rich_location::~rich_location ()
1975 for (unsigned int i
= 0; i
< m_num_fixit_hints
; i
++)
1976 delete m_fixit_hints
[i
];
1979 /* Get location IDX within this rich_location. */
1982 rich_location::get_loc (unsigned int idx
) const
1984 linemap_assert (idx
< m_num_ranges
);
1985 return m_ranges
[idx
].m_loc
;
1988 /* Expand location IDX within this rich_location. */
1989 /* Get an expanded_location for this rich_location's primary
1993 rich_location::get_expanded_location (unsigned int idx
)
1997 /* Cache the expansion of the primary location. */
1998 if (!m_have_expanded_location
)
2001 = linemap_client_expand_location_to_spelling_point (get_loc (0));
2002 if (m_column_override
)
2003 m_expanded_location
.column
= m_column_override
;
2004 m_have_expanded_location
= true;
2007 return m_expanded_location
;
2010 return linemap_client_expand_location_to_spelling_point (get_loc (idx
));
2013 /* Set the column of the primary location, with 0 meaning
2014 "don't override it". */
2017 rich_location::override_column (int column
)
2019 m_column_override
= column
;
2020 m_have_expanded_location
= false;
2023 /* Add the given range. */
2026 rich_location::add_range (source_location loc
, bool show_caret_p
)
2028 linemap_assert (m_num_ranges
< MAX_RANGES
);
2030 location_range
*range
= &m_ranges
[m_num_ranges
++];
2032 range
->m_show_caret_p
= show_caret_p
;
2035 /* Add or overwrite the location given by IDX, setting its location to LOC,
2036 and setting its "should my caret be printed" flag to SHOW_CARET_P.
2038 It must either overwrite an existing location, or add one *exactly* on
2039 the end of the array.
2041 This is primarily for use by gcc when implementing diagnostic format
2043 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2044 (which writes the source location of a tree back into location 0 of
2045 the rich_location), and
2046 - the "%C" and "%L" format codes in the Fortran frontend. */
2049 rich_location::set_range (line_maps
* /*set*/, unsigned int idx
,
2050 source_location loc
, bool show_caret_p
)
2052 linemap_assert (idx
< MAX_RANGES
);
2054 /* We can either overwrite an existing range, or add one exactly
2055 on the end of the array. */
2056 linemap_assert (idx
<= m_num_ranges
);
2058 location_range
*locrange
= &m_ranges
[idx
];
2059 locrange
->m_loc
= loc
;
2060 locrange
->m_show_caret_p
= show_caret_p
;
2062 /* Are we adding a range onto the end? */
2063 if (idx
== m_num_ranges
)
2064 m_num_ranges
= idx
+ 1;
2067 /* Mark any cached value here as dirty. */
2068 m_have_expanded_location
= false;
2071 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2075 rich_location::add_fixit_insert (source_location where
,
2076 const char *new_content
)
2078 linemap_assert (m_num_fixit_hints
< MAX_FIXIT_HINTS
);
2079 m_fixit_hints
[m_num_fixit_hints
++]
2080 = new fixit_insert (where
, new_content
);
2083 /* Add a fixit-hint, suggesting removal of the content at
2087 rich_location::add_fixit_remove (source_range src_range
)
2089 linemap_assert (m_num_fixit_hints
< MAX_FIXIT_HINTS
);
2090 m_fixit_hints
[m_num_fixit_hints
++] = new fixit_remove (src_range
);
2093 /* Add a fixit-hint, suggesting replacement of the content at
2094 SRC_RANGE with NEW_CONTENT. */
2097 rich_location::add_fixit_replace (source_range src_range
,
2098 const char *new_content
)
2100 linemap_assert (m_num_fixit_hints
< MAX_FIXIT_HINTS
);
2101 m_fixit_hints
[m_num_fixit_hints
++]
2102 = new fixit_replace (src_range
, new_content
);
2105 /* class fixit_insert. */
2107 fixit_insert::fixit_insert (source_location where
,
2108 const char *new_content
)
2110 m_bytes (xstrdup (new_content
)),
2111 m_len (strlen (new_content
))
2115 fixit_insert::~fixit_insert ()
2120 /* Implementation of fixit_hint::affects_line_p for fixit_insert. */
2123 fixit_insert::affects_line_p (const char *file
, int line
)
2125 expanded_location exploc
2126 = linemap_client_expand_location_to_spelling_point (m_where
);
2127 if (file
== exploc
.file
)
2128 if (line
== exploc
.line
)
2133 /* class fixit_remove. */
2135 fixit_remove::fixit_remove (source_range src_range
)
2136 : m_src_range (src_range
)
2140 /* Implementation of fixit_hint::affects_line_p for fixit_remove. */
2143 fixit_remove::affects_line_p (const char *file
, int line
)
2145 return m_src_range
.intersects_line_p (file
, line
);
2148 /* class fixit_replace. */
2150 fixit_replace::fixit_replace (source_range src_range
,
2151 const char *new_content
)
2152 : m_src_range (src_range
),
2153 m_bytes (xstrdup (new_content
)),
2154 m_len (strlen (new_content
))
2158 fixit_replace::~fixit_replace ()
2163 /* Implementation of fixit_hint::affects_line_p for fixit_replace. */
2166 fixit_replace::affects_line_p (const char *file
, int line
)
2168 return m_src_range
.intersects_line_p (file
, line
);