1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2017 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 /* Destructor for class line_maps.
61 Ensure non-GC-managed memory is released. */
63 line_maps::~line_maps ()
65 if (location_adhoc_data_map
.htab
)
66 htab_delete (location_adhoc_data_map
.htab
);
69 /* Hash function for location_adhoc_data hashtable. */
72 location_adhoc_data_hash (const void *l
)
74 const struct location_adhoc_data
*lb
=
75 (const struct location_adhoc_data
*) l
;
76 return ((hashval_t
) lb
->locus
77 + (hashval_t
) lb
->src_range
.m_start
78 + (hashval_t
) lb
->src_range
.m_finish
82 /* Compare function for location_adhoc_data hashtable. */
85 location_adhoc_data_eq (const void *l1
, const void *l2
)
87 const struct location_adhoc_data
*lb1
=
88 (const struct location_adhoc_data
*) l1
;
89 const struct location_adhoc_data
*lb2
=
90 (const struct location_adhoc_data
*) l2
;
91 return (lb1
->locus
== lb2
->locus
92 && lb1
->src_range
.m_start
== lb2
->src_range
.m_start
93 && lb1
->src_range
.m_finish
== lb2
->src_range
.m_finish
94 && lb1
->data
== lb2
->data
);
97 /* Update the hashtable when location_adhoc_data is reallocated. */
100 location_adhoc_data_update (void **slot
, void *data
)
103 = (char *) ((uintptr_t) *((char **) slot
) + *((ptrdiff_t *) data
));
107 /* Rebuild the hash table from the location adhoc data. */
110 rebuild_location_adhoc_htab (struct line_maps
*set
)
113 set
->location_adhoc_data_map
.htab
=
114 htab_create (100, location_adhoc_data_hash
, location_adhoc_data_eq
, NULL
);
115 for (i
= 0; i
< set
->location_adhoc_data_map
.curr_loc
; i
++)
116 htab_find_slot (set
->location_adhoc_data_map
.htab
,
117 set
->location_adhoc_data_map
.data
+ i
, INSERT
);
120 /* Helper function for get_combined_adhoc_loc.
121 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
122 within a source_location, without needing to use an ad-hoc location. */
125 can_be_stored_compactly_p (struct line_maps
*set
,
126 source_location locus
,
127 source_range src_range
,
130 /* If there's an ad-hoc pointer, we can't store it directly in the
131 source_location, we need the lookaside. */
135 /* We only store ranges that begin at the locus and that are sufficiently
137 if (src_range
.m_start
!= locus
)
140 if (src_range
.m_finish
< src_range
.m_start
)
143 if (src_range
.m_start
< RESERVED_LOCATION_COUNT
)
146 if (locus
>= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
)
149 /* All 3 locations must be within ordinary maps, typically, the same
151 source_location lowest_macro_loc
= LINEMAPS_MACRO_LOWEST_LOCATION (set
);
152 if (locus
>= lowest_macro_loc
)
154 if (src_range
.m_start
>= lowest_macro_loc
)
156 if (src_range
.m_finish
>= lowest_macro_loc
)
159 /* Passed all tests. */
163 /* Combine LOCUS and DATA to a combined adhoc loc. */
166 get_combined_adhoc_loc (struct line_maps
*set
,
167 source_location locus
,
168 source_range src_range
,
171 struct location_adhoc_data lb
;
172 struct location_adhoc_data
**slot
;
174 if (IS_ADHOC_LOC (locus
))
176 = set
->location_adhoc_data_map
.data
[locus
& MAX_SOURCE_LOCATION
].locus
;
177 if (locus
== 0 && data
== NULL
)
180 /* Any ordinary locations ought to be "pure" at this point: no
181 compressed ranges. */
182 linemap_assert (locus
< RESERVED_LOCATION_COUNT
183 || locus
>= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
184 || locus
>= LINEMAPS_MACRO_LOWEST_LOCATION (set
)
185 || pure_location_p (set
, locus
));
187 /* Consider short-range optimization. */
188 if (can_be_stored_compactly_p (set
, locus
, src_range
, data
))
190 /* The low bits ought to be clear. */
191 linemap_assert (pure_location_p (set
, locus
));
192 const line_map
*map
= linemap_lookup (set
, locus
);
193 const line_map_ordinary
*ordmap
= linemap_check_ordinary (map
);
194 unsigned int int_diff
= src_range
.m_finish
- src_range
.m_start
;
195 unsigned int col_diff
= (int_diff
>> ordmap
->m_range_bits
);
196 if (col_diff
< (1U << ordmap
->m_range_bits
))
198 source_location packed
= locus
| col_diff
;
199 set
->num_optimized_ranges
++;
204 /* We can also compactly store locations
205 when locus == start == finish (and data is NULL). */
206 if (locus
== src_range
.m_start
207 && locus
== src_range
.m_finish
212 set
->num_unoptimized_ranges
++;
215 lb
.src_range
= src_range
;
217 slot
= (struct location_adhoc_data
**)
218 htab_find_slot (set
->location_adhoc_data_map
.htab
, &lb
, INSERT
);
221 if (set
->location_adhoc_data_map
.curr_loc
>=
222 set
->location_adhoc_data_map
.allocated
)
224 char *orig_data
= (char *) set
->location_adhoc_data_map
.data
;
226 /* Cast away extern "C" from the type of xrealloc. */
227 line_map_realloc reallocator
= (set
->reallocator
229 : (line_map_realloc
) xrealloc
);
231 if (set
->location_adhoc_data_map
.allocated
== 0)
232 set
->location_adhoc_data_map
.allocated
= 128;
234 set
->location_adhoc_data_map
.allocated
*= 2;
235 set
->location_adhoc_data_map
.data
= (struct location_adhoc_data
*)
236 reallocator (set
->location_adhoc_data_map
.data
,
237 set
->location_adhoc_data_map
.allocated
238 * sizeof (struct location_adhoc_data
));
239 offset
= (char *) (set
->location_adhoc_data_map
.data
) - orig_data
;
240 if (set
->location_adhoc_data_map
.allocated
> 128)
241 htab_traverse (set
->location_adhoc_data_map
.htab
,
242 location_adhoc_data_update
, &offset
);
244 *slot
= set
->location_adhoc_data_map
.data
245 + set
->location_adhoc_data_map
.curr_loc
;
246 set
->location_adhoc_data_map
.data
[set
->location_adhoc_data_map
.curr_loc
++]
249 return ((*slot
) - set
->location_adhoc_data_map
.data
) | 0x80000000;
252 /* Return the data for the adhoc loc. */
255 get_data_from_adhoc_loc (struct line_maps
*set
, source_location loc
)
257 linemap_assert (IS_ADHOC_LOC (loc
));
258 return set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].data
;
261 /* Return the location for the adhoc loc. */
264 get_location_from_adhoc_loc (struct line_maps
*set
, source_location loc
)
266 linemap_assert (IS_ADHOC_LOC (loc
));
267 return set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
270 /* Return the source_range for adhoc location LOC. */
273 get_range_from_adhoc_loc (struct line_maps
*set
, source_location loc
)
275 linemap_assert (IS_ADHOC_LOC (loc
));
276 return set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].src_range
;
279 /* Get the source_range of location LOC, either from the ad-hoc
280 lookaside table, or embedded inside LOC itself. */
283 get_range_from_loc (struct line_maps
*set
,
286 if (IS_ADHOC_LOC (loc
))
287 return get_range_from_adhoc_loc (set
, loc
);
289 /* For ordinary maps, extract packed range. */
290 if (loc
>= RESERVED_LOCATION_COUNT
291 && loc
< LINEMAPS_MACRO_LOWEST_LOCATION (set
)
292 && loc
<= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
)
294 const line_map
*map
= linemap_lookup (set
, loc
);
295 const line_map_ordinary
*ordmap
= linemap_check_ordinary (map
);
297 int offset
= loc
& ((1 << ordmap
->m_range_bits
) - 1);
298 result
.m_start
= loc
- offset
;
299 result
.m_finish
= result
.m_start
+ (offset
<< ordmap
->m_range_bits
);
303 return source_range::from_location (loc
);
306 /* Get whether location LOC is a "pure" location, or
307 whether it is an ad-hoc location, or embeds range information. */
310 pure_location_p (line_maps
*set
, source_location loc
)
312 if (IS_ADHOC_LOC (loc
))
315 const line_map
*map
= linemap_lookup (set
, loc
);
316 const line_map_ordinary
*ordmap
= linemap_check_ordinary (map
);
318 if (loc
& ((1U << ordmap
->m_range_bits
) - 1))
324 /* Given location LOC within SET, strip away any packed range information
325 or ad-hoc information. */
328 get_pure_location (line_maps
*set
, source_location loc
)
330 if (IS_ADHOC_LOC (loc
))
332 = set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
334 if (loc
>= LINEMAPS_MACRO_LOWEST_LOCATION (set
))
337 if (loc
< RESERVED_LOCATION_COUNT
)
340 const line_map
*map
= linemap_lookup (set
, loc
);
341 const line_map_ordinary
*ordmap
= linemap_check_ordinary (map
);
343 return loc
& ~((1 << ordmap
->m_range_bits
) - 1);
346 /* Initialize a line map set. */
349 linemap_init (struct line_maps
*set
,
350 source_location builtin_location
)
353 set
->highest_location
= RESERVED_LOCATION_COUNT
- 1;
354 set
->highest_line
= RESERVED_LOCATION_COUNT
- 1;
355 set
->location_adhoc_data_map
.htab
=
356 htab_create (100, location_adhoc_data_hash
, location_adhoc_data_eq
, NULL
);
357 set
->builtin_location
= builtin_location
;
360 /* Check for and warn about line_maps entered but not exited. */
363 linemap_check_files_exited (struct line_maps
*set
)
365 const line_map_ordinary
*map
;
366 /* Depending upon whether we are handling preprocessed input or
367 not, this can be a user error or an ICE. */
368 for (map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
370 map
= INCLUDED_FROM (set
, map
))
371 fprintf (stderr
, "line-map.c: file \"%s\" entered but not left\n",
372 ORDINARY_MAP_FILE_NAME (map
));
375 /* Create a new line map in the line map set SET, and return it.
376 REASON is the reason of creating the map. It determines the type
377 of map created (ordinary or macro map). Note that ordinary maps and
378 macro maps are allocated in different memory location. */
380 static struct line_map
*
381 new_linemap (struct line_maps
*set
,
382 enum lc_reason reason
)
384 /* Depending on this variable, a macro map would be allocated in a
385 different memory location than an ordinary map. */
386 bool macro_map_p
= (reason
== LC_ENTER_MACRO
);
387 struct line_map
*result
;
389 if (LINEMAPS_USED (set
, macro_map_p
) == LINEMAPS_ALLOCATED (set
, macro_map_p
))
391 /* We ran out of allocated line maps. Let's allocate more. */
394 /* Cast away extern "C" from the type of xrealloc. */
395 line_map_realloc reallocator
= (set
->reallocator
397 : (line_map_realloc
) xrealloc
);
398 line_map_round_alloc_size_func round_alloc_size
=
399 set
->round_alloc_size
;
401 size_t map_size
= (macro_map_p
402 ? sizeof (line_map_macro
)
403 : sizeof (line_map_ordinary
));
405 /* We are going to execute some dance to try to reduce the
406 overhead of the memory allocator, in case we are using the
409 The actual size of memory we are going to get back from the
410 allocator is the smallest power of 2 that is greater than the
411 size we requested. So let's consider that size then. */
414 (2 * LINEMAPS_ALLOCATED (set
, macro_map_p
) + 256)
417 /* Get the actual size of memory that is going to be allocated
419 alloc_size
= round_alloc_size (alloc_size
);
421 /* Now alloc_size contains the exact memory size we would get if
422 we have asked for the initial alloc_size amount of memory.
423 Let's get back to the number of macro map that amounts
425 LINEMAPS_ALLOCATED (set
, macro_map_p
) =
426 alloc_size
/ map_size
;
428 /* And now let's really do the re-allocation. */
432 = (line_map_macro
*) (*reallocator
) (set
->info_macro
.maps
,
433 (LINEMAPS_ALLOCATED (set
, macro_map_p
)
435 result
= &set
->info_macro
.maps
[LINEMAPS_USED (set
, macro_map_p
)];
439 set
->info_ordinary
.maps
=
440 (line_map_ordinary
*) (*reallocator
) (set
->info_ordinary
.maps
,
441 (LINEMAPS_ALLOCATED (set
, macro_map_p
)
443 result
= &set
->info_ordinary
.maps
[LINEMAPS_USED (set
, macro_map_p
)];
446 ((LINEMAPS_ALLOCATED (set
, macro_map_p
)
447 - LINEMAPS_USED (set
, macro_map_p
))
453 result
= &set
->info_macro
.maps
[LINEMAPS_USED (set
, macro_map_p
)];
455 result
= &set
->info_ordinary
.maps
[LINEMAPS_USED (set
, macro_map_p
)];
458 LINEMAPS_USED (set
, macro_map_p
)++;
460 result
->reason
= reason
;
464 /* Add a mapping of logical source line to physical source file and
467 The text pointed to by TO_FILE must have a lifetime
468 at least as long as the final call to lookup_line (). An empty
469 TO_FILE means standard input. If reason is LC_LEAVE, and
470 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
471 natural values considering the file we are returning to.
473 FROM_LINE should be monotonic increasing across calls to this
474 function. A call to this function can relocate the previous set of
475 maps, so any stored line_map pointers should not be used. */
477 const struct line_map
*
478 linemap_add (struct line_maps
*set
, enum lc_reason reason
,
479 unsigned int sysp
, const char *to_file
, linenum_type to_line
)
481 /* Generate a start_location above the current highest_location.
482 If possible, make the low range bits be zero. */
483 source_location start_location
;
484 if (set
->highest_location
< LINE_MAP_MAX_LOCATION_WITH_COLS
)
486 start_location
= set
->highest_location
+ (1 << set
->default_range_bits
);
487 if (set
->default_range_bits
)
488 start_location
&= ~((1 << set
->default_range_bits
) - 1);
489 linemap_assert (0 == (start_location
490 & ((1 << set
->default_range_bits
) - 1)));
493 start_location
= set
->highest_location
+ 1;
495 linemap_assert (!(LINEMAPS_ORDINARY_USED (set
)
497 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set
)))));
499 /* When we enter the file for the first time reason cannot be
501 linemap_assert (!(set
->depth
== 0 && reason
== LC_RENAME
));
503 /* If we are leaving the main file, return a NULL map. */
504 if (reason
== LC_LEAVE
505 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set
))
512 linemap_assert (reason
!= LC_ENTER_MACRO
);
513 line_map_ordinary
*map
= linemap_check_ordinary (new_linemap (set
, reason
));
515 if (to_file
&& *to_file
== '\0' && reason
!= LC_RENAME_VERBATIM
)
518 if (reason
== LC_RENAME_VERBATIM
)
521 if (reason
== LC_LEAVE
)
523 /* When we are just leaving an "included" file, and jump to the next
524 location inside the "includer" right after the #include
525 "included", this variable points the map in use right before the
526 #include "included", inside the same "includer" file. */
527 line_map_ordinary
*from
;
529 linemap_assert (!MAIN_FILE_P (map
- 1));
530 /* (MAP - 1) points to the map we are leaving. The
531 map from which (MAP - 1) got included should be the map
532 that comes right before MAP in the same file. */
533 from
= INCLUDED_FROM (set
, map
- 1);
535 /* A TO_FILE of NULL is special - we use the natural values. */
538 to_file
= ORDINARY_MAP_FILE_NAME (from
);
539 to_line
= SOURCE_LINE (from
, from
[1].start_location
);
540 sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (from
);
543 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from
),
548 map
->start_location
= start_location
;
549 map
->to_file
= to_file
;
550 map
->to_line
= to_line
;
551 LINEMAPS_ORDINARY_CACHE (set
) = LINEMAPS_ORDINARY_USED (set
) - 1;
552 map
->m_column_and_range_bits
= 0;
553 map
->m_range_bits
= 0;
554 set
->highest_location
= start_location
;
555 set
->highest_line
= start_location
;
556 set
->max_column_hint
= 0;
558 /* This assertion is placed after set->highest_location has
559 been updated, since the latter affects
560 linemap_location_from_macro_expansion_p, which ultimately affects
562 linemap_assert (pure_location_p (set
, start_location
));
564 if (reason
== LC_ENTER
)
567 set
->depth
== 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set
) - 2);
569 if (set
->trace_includes
)
570 trace_include (set
, map
);
572 else if (reason
== LC_RENAME
)
573 map
->included_from
= ORDINARY_MAP_INCLUDER_FILE_INDEX (&map
[-1]);
574 else if (reason
== LC_LEAVE
)
578 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set
, map
- 1));
584 /* Returns TRUE if the line table set tracks token locations across
585 macro expansion, FALSE otherwise. */
588 linemap_tracks_macro_expansion_locs_p (struct line_maps
*set
)
590 return LINEMAPS_MACRO_MAPS (set
) != NULL
;
593 /* Create a macro map. A macro map encodes source locations of tokens
594 that are part of a macro replacement-list, at a macro expansion
595 point. See the extensive comments of struct line_map and struct
596 line_map_macro, in line-map.h.
598 This map shall be created when the macro is expanded. The map
599 encodes the source location of the expansion point of the macro as
600 well as the "original" source location of each token that is part
601 of the macro replacement-list. If a macro is defined but never
602 expanded, it has no macro map. SET is the set of maps the macro
603 map should be part of. MACRO_NODE is the macro which the new macro
604 map should encode source locations for. EXPANSION is the location
605 of the expansion point of MACRO. For function-like macros
606 invocations, it's best to make it point to the closing parenthesis
607 of the macro, rather than the the location of the first character
608 of the macro. NUM_TOKENS is the number of tokens that are part of
609 the replacement-list of MACRO.
611 Note that when we run out of the integer space available for source
612 locations, this function returns NULL. In that case, callers of
613 this function cannot encode {line,column} pairs into locations of
614 macro tokens anymore. */
616 const line_map_macro
*
617 linemap_enter_macro (struct line_maps
*set
, struct cpp_hashnode
*macro_node
,
618 source_location expansion
, unsigned int num_tokens
)
621 source_location start_location
;
622 /* Cast away extern "C" from the type of xrealloc. */
623 line_map_realloc reallocator
= (set
->reallocator
625 : (line_map_realloc
) xrealloc
);
627 start_location
= LINEMAPS_MACRO_LOWEST_LOCATION (set
) - num_tokens
;
629 if (start_location
<= set
->highest_line
630 || start_location
> LINEMAPS_MACRO_LOWEST_LOCATION (set
))
631 /* We ran out of macro map space. */
634 map
= linemap_check_macro (new_linemap (set
, LC_ENTER_MACRO
));
636 map
->start_location
= start_location
;
637 map
->macro
= macro_node
;
638 map
->n_tokens
= num_tokens
;
640 = (source_location
*) reallocator (NULL
,
642 * sizeof (source_location
));
643 map
->expansion
= expansion
;
644 memset (MACRO_MAP_LOCATIONS (map
), 0,
645 num_tokens
* sizeof (source_location
));
647 LINEMAPS_MACRO_CACHE (set
) = LINEMAPS_MACRO_USED (set
) - 1;
652 /* Create and return a virtual location for a token that is part of a
653 macro expansion-list at a macro expansion point. See the comment
654 inside struct line_map_macro to see what an expansion-list exactly
657 A call to this function must come after a call to
660 MAP is the map into which the source location is created. TOKEN_NO
661 is the index of the token in the macro replacement-list, starting
664 ORIG_LOC is the location of the token outside of this macro
665 expansion. If the token comes originally from the macro
666 definition, it is the locus in the macro definition; otherwise it
667 is a location in the context of the caller of this macro expansion
668 (which is a virtual location or a source location if the caller is
669 itself a macro expansion or not).
671 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
672 either of the token itself or of a macro parameter that it
676 linemap_add_macro_token (const line_map_macro
*map
,
677 unsigned int token_no
,
678 source_location orig_loc
,
679 source_location orig_parm_replacement_loc
)
681 source_location result
;
683 linemap_assert (linemap_macro_expansion_map_p (map
));
684 linemap_assert (token_no
< MACRO_MAP_NUM_MACRO_TOKENS (map
));
686 MACRO_MAP_LOCATIONS (map
)[2 * token_no
] = orig_loc
;
687 MACRO_MAP_LOCATIONS (map
)[2 * token_no
+ 1] = orig_parm_replacement_loc
;
689 result
= MAP_START_LOCATION (map
) + token_no
;
693 /* Return a source_location for the start (i.e. column==0) of
694 (physical) line TO_LINE in the current source file (as in the
695 most recent linemap_add). MAX_COLUMN_HINT is the highest column
696 number we expect to use in this line (but it does not change
697 the highest_location). */
700 linemap_line_start (struct line_maps
*set
, linenum_type to_line
,
701 unsigned int max_column_hint
)
703 line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
704 source_location highest
= set
->highest_location
;
706 linenum_type last_line
=
707 SOURCE_LINE (map
, set
->highest_line
);
708 int line_delta
= to_line
- last_line
;
709 bool add_map
= false;
710 linemap_assert (map
->m_column_and_range_bits
>= map
->m_range_bits
);
711 int effective_column_bits
= map
->m_column_and_range_bits
- map
->m_range_bits
;
715 && line_delta
* map
->m_column_and_range_bits
> 1000)
716 || (max_column_hint
>= (1U << effective_column_bits
))
717 || (max_column_hint
<= 80 && effective_column_bits
>= 10)
718 || (highest
> LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
719 && map
->m_range_bits
> 0)
720 || (highest
> LINE_MAP_MAX_LOCATION_WITH_COLS
721 && (set
->max_column_hint
|| highest
>= LINE_MAP_MAX_SOURCE_LOCATION
)))
724 max_column_hint
= set
->max_column_hint
;
729 if (max_column_hint
> LINE_MAP_MAX_COLUMN_NUMBER
730 || highest
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
732 /* If the column number is ridiculous or we've allocated a huge
733 number of source_locations, give up on column numbers
734 (and on packed ranges). */
738 if (highest
> LINE_MAP_MAX_SOURCE_LOCATION
)
744 if (highest
<= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
)
745 range_bits
= set
->default_range_bits
;
748 while (max_column_hint
>= (1U << column_bits
))
750 max_column_hint
= 1U << column_bits
;
751 column_bits
+= range_bits
;
753 /* Allocate the new line_map. However, if the current map only has a
754 single line we can sometimes just increase its column_bits instead. */
756 || last_line
!= ORDINARY_MAP_STARTING_LINE_NUMBER (map
)
757 || SOURCE_COLUMN (map
, highest
) >= (1U << (column_bits
- range_bits
))
758 || range_bits
< map
->m_range_bits
)
759 map
= linemap_check_ordinary
760 (const_cast <line_map
*>
761 (linemap_add (set
, LC_RENAME
,
762 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
),
763 ORDINARY_MAP_FILE_NAME (map
),
765 map
->m_column_and_range_bits
= column_bits
;
766 map
->m_range_bits
= range_bits
;
767 r
= (MAP_START_LOCATION (map
)
768 + ((to_line
- ORDINARY_MAP_STARTING_LINE_NUMBER (map
))
772 r
= set
->highest_line
+ (line_delta
<< map
->m_column_and_range_bits
);
774 /* Locations of ordinary tokens are always lower than locations of
776 if (r
>= LINEMAPS_MACRO_LOWEST_LOCATION (set
))
779 set
->highest_line
= r
;
780 if (r
> set
->highest_location
)
781 set
->highest_location
= r
;
782 set
->max_column_hint
= max_column_hint
;
784 /* At this point, we expect one of:
785 (a) the normal case: a "pure" location with 0 range bits, or
786 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
787 columns anymore (or ranges), or
788 (c) we're in a region with a column hint exceeding
789 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
790 with column_bits == 0. */
791 linemap_assert (pure_location_p (set
, r
)
792 || r
>= LINE_MAP_MAX_LOCATION_WITH_COLS
793 || map
->m_column_and_range_bits
== 0);
794 linemap_assert (SOURCE_LINE (map
, r
) == to_line
);
798 /* Encode and return a source_location from a column number. The
799 source line considered is the last source line used to call
800 linemap_line_start, i.e, the last source line which a location was
804 linemap_position_for_column (struct line_maps
*set
, unsigned int to_column
)
806 source_location r
= set
->highest_line
;
809 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set
)));
811 if (to_column
>= set
->max_column_hint
)
813 if (r
> LINE_MAP_MAX_LOCATION_WITH_COLS
814 || to_column
> LINE_MAP_MAX_COLUMN_NUMBER
)
816 /* Running low on source_locations - disable column numbers. */
821 /* Otherwise, attempt to start a new line that can hold TO_COLUMN,
822 with some space to spare. This may or may not lead to a new
823 linemap being created. */
824 line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
825 r
= linemap_line_start (set
, SOURCE_LINE (map
, r
), to_column
+ 50);
826 map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
827 if (map
->m_column_and_range_bits
== 0)
829 /* ...then the linemap has column-tracking disabled,
830 presumably due to exceeding either
831 LINE_MAP_MAX_LOCATION_WITH_COLS (overall) or
832 LINE_MAP_MAX_COLUMN_NUMBER (within this line).
833 Return the start of the linemap, which encodes column 0, for
839 line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
840 r
= r
+ (to_column
<< map
->m_range_bits
);
841 if (r
>= set
->highest_location
)
842 set
->highest_location
= r
;
846 /* Encode and return a source location from a given line and
850 linemap_position_for_line_and_column (line_maps
*set
,
851 const line_map_ordinary
*ord_map
,
855 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
) <= line
);
857 source_location r
= MAP_START_LOCATION (ord_map
);
858 r
+= ((line
- ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
))
859 << ord_map
->m_column_and_range_bits
);
860 if (r
<= LINE_MAP_MAX_LOCATION_WITH_COLS
)
861 r
+= ((column
& ((1 << ord_map
->m_column_and_range_bits
) - 1))
862 << ord_map
->m_range_bits
);
863 source_location upper_limit
= LINEMAPS_MACRO_LOWEST_LOCATION (set
);
864 if (r
>= upper_limit
)
866 if (r
> set
->highest_location
)
867 set
->highest_location
= r
;
871 /* Encode and return a source_location starting from location LOC and
872 shifting it by COLUMN_OFFSET columns. This function does not support
873 virtual locations. */
876 linemap_position_for_loc_and_offset (struct line_maps
*set
,
878 unsigned int column_offset
)
880 const line_map_ordinary
* map
= NULL
;
882 if (IS_ADHOC_LOC (loc
))
883 loc
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
885 /* This function does not support virtual locations yet. */
886 if (linemap_location_from_macro_expansion_p (set
, loc
))
889 if (column_offset
== 0
890 /* Adding an offset to a reserved location (like
891 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
892 sense. So let's leave the location intact in that case. */
893 || loc
< RESERVED_LOCATION_COUNT
)
896 /* We find the real location and shift it. */
897 loc
= linemap_resolve_location (set
, loc
, LRK_SPELLING_LOCATION
, &map
);
898 /* The new location (loc + offset) should be higher than the first
899 location encoded by MAP. This can fail if the line information
900 is messed up because of line directives (see PR66415). */
901 if (MAP_START_LOCATION (map
) >= loc
+ (column_offset
<< map
->m_range_bits
))
904 linenum_type line
= SOURCE_LINE (map
, loc
);
905 unsigned int column
= SOURCE_COLUMN (map
, loc
);
907 /* If MAP is not the last line map of its set, then the new location
908 (loc + offset) should be less than the first location encoded by
909 the next line map of the set. Otherwise, we try to encode the
910 location in the next map. */
911 while (map
!= LINEMAPS_LAST_ORDINARY_MAP (set
)
912 && (loc
+ (column_offset
<< map
->m_range_bits
)
913 >= MAP_START_LOCATION (&map
[1])))
916 /* If the next map starts in a higher line, we cannot encode the
918 if (line
< ORDINARY_MAP_STARTING_LINE_NUMBER (map
))
922 column
+= column_offset
;
924 /* Bail out if the column is not representable within the existing
926 if (column
>= (1u << (map
->m_column_and_range_bits
- map
->m_range_bits
)))
930 linemap_position_for_line_and_column (set
, map
, line
, column
);
931 if (linemap_assert_fails (r
<= set
->highest_location
)
932 || linemap_assert_fails (map
== linemap_lookup (set
, r
)))
938 /* Given a virtual source location yielded by a map (either an
939 ordinary or a macro map), returns that map. */
941 const struct line_map
*
942 linemap_lookup (struct line_maps
*set
, source_location line
)
944 if (IS_ADHOC_LOC (line
))
945 line
= set
->location_adhoc_data_map
.data
[line
& MAX_SOURCE_LOCATION
].locus
;
946 if (linemap_location_from_macro_expansion_p (set
, line
))
947 return linemap_macro_map_lookup (set
, line
);
948 return linemap_ordinary_map_lookup (set
, line
);
951 /* Given a source location yielded by an ordinary map, returns that
952 map. Since the set is built chronologically, the logical lines are
953 monotonic increasing, and so the list is sorted and we can use a
956 static const line_map_ordinary
*
957 linemap_ordinary_map_lookup (struct line_maps
*set
, source_location line
)
959 unsigned int md
, mn
, mx
;
960 const line_map_ordinary
*cached
, *result
;
962 if (IS_ADHOC_LOC (line
))
963 line
= set
->location_adhoc_data_map
.data
[line
& MAX_SOURCE_LOCATION
].locus
;
965 if (set
== NULL
|| line
< RESERVED_LOCATION_COUNT
)
968 mn
= LINEMAPS_ORDINARY_CACHE (set
);
969 mx
= LINEMAPS_ORDINARY_USED (set
);
971 cached
= LINEMAPS_ORDINARY_MAP_AT (set
, mn
);
972 /* We should get a segfault if no line_maps have been added yet. */
973 if (line
>= MAP_START_LOCATION (cached
))
975 if (mn
+ 1 == mx
|| line
< MAP_START_LOCATION (&cached
[1]))
987 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set
, md
)) > line
)
993 LINEMAPS_ORDINARY_CACHE (set
) = mn
;
994 result
= LINEMAPS_ORDINARY_MAP_AT (set
, mn
);
995 linemap_assert (line
>= MAP_START_LOCATION (result
));
999 /* Given a source location yielded by a macro map, returns that map.
1000 Since the set is built chronologically, the logical lines are
1001 monotonic decreasing, and so the list is sorted and we can use a
1004 static const line_map_macro
*
1005 linemap_macro_map_lookup (struct line_maps
*set
, source_location line
)
1007 unsigned int md
, mn
, mx
;
1008 const struct line_map_macro
*cached
, *result
;
1010 if (IS_ADHOC_LOC (line
))
1011 line
= set
->location_adhoc_data_map
.data
[line
& MAX_SOURCE_LOCATION
].locus
;
1013 linemap_assert (line
>= LINEMAPS_MACRO_LOWEST_LOCATION (set
));
1018 mn
= LINEMAPS_MACRO_CACHE (set
);
1019 mx
= LINEMAPS_MACRO_USED (set
);
1020 cached
= LINEMAPS_MACRO_MAP_AT (set
, mn
);
1022 if (line
>= MAP_START_LOCATION (cached
))
1024 if (mn
== 0 || line
< MAP_START_LOCATION (&cached
[-1]))
1033 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set
, md
)) > line
)
1039 LINEMAPS_MACRO_CACHE (set
) = mx
;
1040 result
= LINEMAPS_MACRO_MAP_AT (set
, LINEMAPS_MACRO_CACHE (set
));
1041 linemap_assert (MAP_START_LOCATION (result
) <= line
);
1046 /* Return TRUE if MAP encodes locations coming from a macro
1047 replacement-list at macro expansion point. */
1050 linemap_macro_expansion_map_p (const struct line_map
*map
)
1054 return (map
->reason
== LC_ENTER_MACRO
);
1057 /* If LOCATION is the locus of a token in a replacement-list of a
1058 macro expansion return the location of the macro expansion point.
1060 Read the comments of struct line_map and struct line_map_macro in
1061 line-map.h to understand what a macro expansion point is. */
1063 static source_location
1064 linemap_macro_map_loc_to_exp_point (const line_map_macro
*map
,
1065 source_location location ATTRIBUTE_UNUSED
)
1067 linemap_assert (linemap_macro_expansion_map_p (map
)
1068 && location
>= MAP_START_LOCATION (map
));
1070 /* Make sure LOCATION is correct. */
1071 linemap_assert ((location
- MAP_START_LOCATION (map
))
1072 < MACRO_MAP_NUM_MACRO_TOKENS (map
));
1074 return MACRO_MAP_EXPANSION_POINT_LOCATION (map
);
1077 /* LOCATION is the source location of a token that belongs to a macro
1078 replacement-list as part of the macro expansion denoted by MAP.
1080 Return the location of the token at the definition point of the
1083 static source_location
1084 linemap_macro_map_loc_to_def_point (const line_map_macro
*map
,
1085 source_location location
)
1089 linemap_assert (linemap_macro_expansion_map_p (map
)
1090 && location
>= MAP_START_LOCATION (map
));
1091 linemap_assert (location
>= RESERVED_LOCATION_COUNT
);
1093 token_no
= location
- MAP_START_LOCATION (map
);
1094 linemap_assert (token_no
< MACRO_MAP_NUM_MACRO_TOKENS (map
));
1096 location
= MACRO_MAP_LOCATIONS (map
)[2 * token_no
+ 1];
1101 /* If LOCATION is the locus of a token that is an argument of a
1102 function-like macro M and appears in the expansion of M, return the
1103 locus of that argument in the context of the caller of M.
1105 In other words, this returns the xI location presented in the
1106 comments of line_map_macro above. */
1108 linemap_macro_map_loc_unwind_toward_spelling (line_maps
*set
,
1109 const line_map_macro
* map
,
1110 source_location location
)
1114 if (IS_ADHOC_LOC (location
))
1115 location
= get_location_from_adhoc_loc (set
, location
);
1117 linemap_assert (linemap_macro_expansion_map_p (map
)
1118 && location
>= MAP_START_LOCATION (map
));
1119 linemap_assert (location
>= RESERVED_LOCATION_COUNT
);
1120 linemap_assert (!IS_ADHOC_LOC (location
));
1122 token_no
= location
- MAP_START_LOCATION (map
);
1123 linemap_assert (token_no
< MACRO_MAP_NUM_MACRO_TOKENS (map
));
1125 location
= MACRO_MAP_LOCATIONS (map
)[2 * token_no
];
1130 /* Return the source line number corresponding to source location
1131 LOCATION. SET is the line map set LOCATION comes from. If
1132 LOCATION is the source location of token that is part of the
1133 replacement-list of a macro expansion return the line number of the
1134 macro expansion point. */
1137 linemap_get_expansion_line (struct line_maps
*set
,
1138 source_location location
)
1140 const line_map_ordinary
*map
= NULL
;
1142 if (IS_ADHOC_LOC (location
))
1143 location
= set
->location_adhoc_data_map
.data
[location
1144 & MAX_SOURCE_LOCATION
].locus
;
1146 if (location
< RESERVED_LOCATION_COUNT
)
1150 linemap_macro_loc_to_exp_point (set
, location
, &map
);
1152 return SOURCE_LINE (map
, location
);
1155 /* Return the path of the file corresponding to source code location
1158 If LOCATION is the source location of token that is part of the
1159 replacement-list of a macro expansion return the file path of the
1160 macro expansion point.
1162 SET is the line map set LOCATION comes from. */
1165 linemap_get_expansion_filename (struct line_maps
*set
,
1166 source_location location
)
1168 const struct line_map_ordinary
*map
= NULL
;
1170 if (IS_ADHOC_LOC (location
))
1171 location
= set
->location_adhoc_data_map
.data
[location
1172 & MAX_SOURCE_LOCATION
].locus
;
1174 if (location
< RESERVED_LOCATION_COUNT
)
1178 linemap_macro_loc_to_exp_point (set
, location
, &map
);
1180 return LINEMAP_FILE (map
);
1183 /* Return the name of the macro associated to MACRO_MAP. */
1186 linemap_map_get_macro_name (const line_map_macro
*macro_map
)
1188 linemap_assert (macro_map
&& linemap_macro_expansion_map_p (macro_map
));
1189 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map
));
1192 /* Return a positive value if LOCATION is the locus of a token that is
1193 located in a system header, O otherwise. It returns 1 if LOCATION
1194 is the locus of a token that is located in a system header, and 2
1195 if LOCATION is the locus of a token located in a C system header
1196 that therefore needs to be extern "C" protected in C++.
1198 Note that this function returns 1 if LOCATION belongs to a token
1199 that is part of a macro replacement-list defined in a system
1200 header, but expanded in a non-system file. */
1203 linemap_location_in_system_header_p (struct line_maps
*set
,
1204 source_location location
)
1206 const struct line_map
*map
= NULL
;
1208 if (IS_ADHOC_LOC (location
))
1209 location
= set
->location_adhoc_data_map
.data
[location
1210 & MAX_SOURCE_LOCATION
].locus
;
1212 if (location
< RESERVED_LOCATION_COUNT
)
1215 /* Let's look at where the token for LOCATION comes from. */
1218 map
= linemap_lookup (set
, location
);
1221 if (!linemap_macro_expansion_map_p (map
))
1222 /* It's a normal token. */
1223 return LINEMAP_SYSP (linemap_check_ordinary (map
));
1226 const line_map_macro
*macro_map
= linemap_check_macro (map
);
1228 /* It's a token resulting from a macro expansion. */
1229 source_location loc
=
1230 linemap_macro_map_loc_unwind_toward_spelling (set
, macro_map
, location
);
1231 if (loc
< RESERVED_LOCATION_COUNT
)
1232 /* This token might come from a built-in macro. Let's
1233 look at where that macro got expanded. */
1234 location
= linemap_macro_map_loc_to_exp_point (macro_map
, location
);
1245 /* Return TRUE if LOCATION is a source code location of a token that is part of
1246 a macro expansion, FALSE otherwise. */
1249 linemap_location_from_macro_expansion_p (const struct line_maps
*set
,
1250 source_location location
)
1252 if (IS_ADHOC_LOC (location
))
1253 location
= set
->location_adhoc_data_map
.data
[location
1254 & MAX_SOURCE_LOCATION
].locus
;
1256 linemap_assert (location
<= MAX_SOURCE_LOCATION
1257 && (set
->highest_location
1258 < LINEMAPS_MACRO_LOWEST_LOCATION (set
)));
1261 return (location
> set
->highest_location
);
1264 /* Given two virtual locations *LOC0 and *LOC1, return the first
1265 common macro map in their macro expansion histories. Return NULL
1266 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1267 virtual location of the token inside the resulting macro. */
1269 static const struct line_map
*
1270 first_map_in_common_1 (struct line_maps
*set
,
1271 source_location
*loc0
,
1272 source_location
*loc1
)
1274 source_location l0
= *loc0
, l1
= *loc1
;
1275 const struct line_map
*map0
= linemap_lookup (set
, l0
),
1276 *map1
= linemap_lookup (set
, l1
);
1278 while (linemap_macro_expansion_map_p (map0
)
1279 && linemap_macro_expansion_map_p (map1
)
1282 if (MAP_START_LOCATION (map0
) < MAP_START_LOCATION (map1
))
1284 l0
= linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0
),
1286 map0
= linemap_lookup (set
, l0
);
1290 l1
= linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1
),
1292 map1
= linemap_lookup (set
, l1
);
1305 /* Given two virtual locations LOC0 and LOC1, return the first common
1306 macro map in their macro expansion histories. Return NULL if no
1307 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1308 virtual location of the token inside the resulting macro, upon
1309 return of a non-NULL result. */
1311 static const struct line_map
*
1312 first_map_in_common (struct line_maps
*set
,
1313 source_location loc0
,
1314 source_location loc1
,
1315 source_location
*res_loc0
,
1316 source_location
*res_loc1
)
1321 return first_map_in_common_1 (set
, res_loc0
, res_loc1
);
1324 /* Return a positive value if PRE denotes the location of a token that
1325 comes before the token of POST, 0 if PRE denotes the location of
1326 the same token as the token for POST, and a negative value
1330 linemap_compare_locations (struct line_maps
*set
,
1331 source_location pre
,
1332 source_location post
)
1334 bool pre_virtual_p
, post_virtual_p
;
1335 source_location l0
= pre
, l1
= post
;
1337 if (IS_ADHOC_LOC (l0
))
1338 l0
= get_location_from_adhoc_loc (set
, l0
);
1339 if (IS_ADHOC_LOC (l1
))
1340 l1
= get_location_from_adhoc_loc (set
, l1
);
1345 if ((pre_virtual_p
= linemap_location_from_macro_expansion_p (set
, l0
)))
1346 l0
= linemap_resolve_location (set
, l0
,
1347 LRK_MACRO_EXPANSION_POINT
,
1350 if ((post_virtual_p
= linemap_location_from_macro_expansion_p (set
, l1
)))
1351 l1
= linemap_resolve_location (set
, l1
,
1352 LRK_MACRO_EXPANSION_POINT
,
1359 /* So pre and post represent two tokens that are present in a
1360 same macro expansion. Let's see if the token for pre was
1361 before the token for post in that expansion. */
1363 const struct line_map
*map
=
1364 first_map_in_common (set
, pre
, post
, &l0
, &l1
);
1367 /* This should not be possible. */
1370 i0
= l0
- MAP_START_LOCATION (map
);
1371 i1
= l1
- MAP_START_LOCATION (map
);
1375 if (IS_ADHOC_LOC (l0
))
1376 l0
= get_location_from_adhoc_loc (set
, l0
);
1377 if (IS_ADHOC_LOC (l1
))
1378 l1
= get_location_from_adhoc_loc (set
, l1
);
1383 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1386 trace_include (const struct line_maps
*set
, const line_map_ordinary
*map
)
1388 unsigned int i
= set
->depth
;
1393 fprintf (stderr
, " %s\n", ORDINARY_MAP_FILE_NAME (map
));
1396 /* Return the spelling location of the token wherever it comes from,
1397 whether part of a macro definition or not.
1399 This is a subroutine for linemap_resolve_location. */
1401 static source_location
1402 linemap_macro_loc_to_spelling_point (struct line_maps
*set
,
1403 source_location location
,
1404 const line_map_ordinary
**original_map
)
1406 struct line_map
*map
;
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_unwind_toward_spelling
1417 (set
, linemap_check_macro (map
),
1422 *original_map
= linemap_check_ordinary (map
);
1426 /* If LOCATION is the source location of a token that belongs to a
1427 macro replacement-list -- as part of a macro expansion -- then
1428 return the location of the token at the definition point of the
1429 macro. Otherwise, return LOCATION. SET is the set of maps
1430 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1431 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1432 returned location comes from.
1434 This is a subroutine of linemap_resolve_location. */
1436 static source_location
1437 linemap_macro_loc_to_def_point (struct line_maps
*set
,
1438 source_location location
,
1439 const line_map_ordinary
**original_map
)
1441 struct line_map
*map
;
1443 if (IS_ADHOC_LOC (location
))
1444 location
= set
->location_adhoc_data_map
.data
[location
1445 & MAX_SOURCE_LOCATION
].locus
;
1447 linemap_assert (set
&& location
>= RESERVED_LOCATION_COUNT
);
1451 map
= const_cast <line_map
*> (linemap_lookup (set
, location
));
1452 if (!linemap_macro_expansion_map_p (map
))
1456 linemap_macro_map_loc_to_def_point (linemap_check_macro (map
),
1461 *original_map
= linemap_check_ordinary (map
);
1465 /* If LOCATION is the source location of a token that belongs to a
1466 macro replacement-list -- at a macro expansion point -- then return
1467 the location of the topmost expansion point of the macro. We say
1468 topmost because if we are in the context of a nested macro
1469 expansion, the function returns the source location of the first
1470 macro expansion that triggered the nested expansions.
1472 Otherwise, return LOCATION. SET is the set of maps location come
1473 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1474 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1475 location comes from.
1477 This is a subroutine of linemap_resolve_location. */
1479 static source_location
1480 linemap_macro_loc_to_exp_point (struct line_maps
*set
,
1481 source_location location
,
1482 const line_map_ordinary
**original_map
)
1484 struct line_map
*map
;
1486 if (IS_ADHOC_LOC (location
))
1487 location
= set
->location_adhoc_data_map
.data
[location
1488 & MAX_SOURCE_LOCATION
].locus
;
1490 linemap_assert (set
&& location
>= RESERVED_LOCATION_COUNT
);
1494 map
= const_cast <line_map
*> (linemap_lookup (set
, location
));
1495 if (!linemap_macro_expansion_map_p (map
))
1497 location
= linemap_macro_map_loc_to_exp_point (linemap_check_macro (map
),
1502 *original_map
= linemap_check_ordinary (map
);
1506 /* Resolve a virtual location into either a spelling location, an
1507 expansion point location or a token argument replacement point
1508 location. Return the map that encodes the virtual location as well
1509 as the resolved location.
1511 If LOC is *NOT* the location of a token resulting from the
1512 expansion of a macro, then the parameter LRK (which stands for
1513 Location Resolution Kind) is ignored and the resulting location
1514 just equals the one given in argument.
1516 Now if LOC *IS* the location of a token resulting from the
1517 expansion of a macro, this is what happens.
1519 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1520 -------------------------------
1522 The virtual location is resolved to the first macro expansion point
1523 that led to this macro expansion.
1525 * If LRK is set to LRK_SPELLING_LOCATION
1526 -------------------------------------
1528 The virtual location is resolved to the locus where the token has
1529 been spelled in the source. This can follow through all the macro
1530 expansions that led to the token.
1532 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1533 --------------------------------------
1535 The virtual location is resolved to the locus of the token in the
1536 context of the macro definition.
1538 If LOC is the locus of a token that is an argument of a
1539 function-like macro [replacing a parameter in the replacement list
1540 of the macro] the virtual location is resolved to the locus of the
1541 parameter that is replaced, in the context of the definition of the
1544 If LOC is the locus of a token that is not an argument of a
1545 function-like macro, then the function behaves as if LRK was set to
1546 LRK_SPELLING_LOCATION.
1548 If MAP is not NULL, *MAP is set to the map encoding the
1549 returned location. Note that if the returned location wasn't originally
1550 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1551 resolves to a location reserved for the client code, like
1552 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1555 linemap_resolve_location (struct line_maps
*set
,
1556 source_location loc
,
1557 enum location_resolution_kind lrk
,
1558 const line_map_ordinary
**map
)
1560 source_location locus
= loc
;
1561 if (IS_ADHOC_LOC (loc
))
1562 locus
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
1564 if (locus
< RESERVED_LOCATION_COUNT
)
1566 /* A reserved location wasn't encoded in a map. Let's return a
1567 NULL map here, just like what linemap_ordinary_map_lookup
1576 case LRK_MACRO_EXPANSION_POINT
:
1577 loc
= linemap_macro_loc_to_exp_point (set
, loc
, map
);
1579 case LRK_SPELLING_LOCATION
:
1580 loc
= linemap_macro_loc_to_spelling_point (set
, loc
, map
);
1582 case LRK_MACRO_DEFINITION_LOCATION
:
1583 loc
= linemap_macro_loc_to_def_point (set
, loc
, map
);
1591 /* TRUE if LOCATION is a source code location of a token that is part of the
1592 definition of a macro, FALSE otherwise. */
1595 linemap_location_from_macro_definition_p (struct line_maps
*set
,
1596 source_location loc
)
1598 if (IS_ADHOC_LOC (loc
))
1599 loc
= get_location_from_adhoc_loc (set
, loc
);
1601 if (!linemap_location_from_macro_expansion_p (set
, loc
))
1606 const struct line_map_macro
*map
1607 = linemap_check_macro (linemap_lookup (set
, loc
));
1609 source_location s_loc
1610 = linemap_macro_map_loc_unwind_toward_spelling (set
, map
, loc
);
1611 if (linemap_location_from_macro_expansion_p (set
, s_loc
))
1615 source_location def_loc
1616 = linemap_macro_map_loc_to_def_point (map
, loc
);
1617 return s_loc
== def_loc
;
1623 Suppose that LOC is the virtual location of a token T coming from
1624 the expansion of a macro M. This function then steps up to get the
1625 location L of the point where M got expanded. If L is a spelling
1626 location inside a macro expansion M', then this function returns
1627 the locus of the point where M' was expanded. Said otherwise, this
1628 function returns the location of T in the context that triggered
1631 *LOC_MAP must be set to the map of LOC. This function then sets it
1632 to the map of the returned location. */
1635 linemap_unwind_toward_expansion (struct line_maps
*set
,
1636 source_location loc
,
1637 const struct line_map
**map
)
1639 source_location resolved_location
;
1640 const line_map_macro
*macro_map
= linemap_check_macro (*map
);
1641 const struct line_map
*resolved_map
;
1643 if (IS_ADHOC_LOC (loc
))
1644 loc
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
1647 linemap_macro_map_loc_unwind_toward_spelling (set
, macro_map
, loc
);
1648 resolved_map
= linemap_lookup (set
, resolved_location
);
1650 if (!linemap_macro_expansion_map_p (resolved_map
))
1652 resolved_location
= linemap_macro_map_loc_to_exp_point (macro_map
, loc
);
1653 resolved_map
= linemap_lookup (set
, resolved_location
);
1656 *map
= resolved_map
;
1657 return resolved_location
;
1660 /* If LOC is the virtual location of a token coming from the expansion
1661 of a macro M and if its spelling location is reserved (e.g, a
1662 location for a built-in token), then this function unwinds (using
1663 linemap_unwind_toward_expansion) the location until a location that
1664 is not reserved and is not in a system header is reached. In other
1665 words, this unwinds the reserved location until a location that is
1666 in real source code is reached.
1668 Otherwise, if the spelling location for LOC is not reserved or if
1669 LOC doesn't come from the expansion of a macro, the function
1670 returns LOC as is and *MAP is not touched.
1672 *MAP is set to the map of the returned location if the later is
1673 different from LOC. */
1675 linemap_unwind_to_first_non_reserved_loc (struct line_maps
*set
,
1676 source_location loc
,
1677 const struct line_map
**map
)
1679 source_location resolved_loc
;
1680 const struct line_map
*map0
= NULL
;
1681 const line_map_ordinary
*map1
= NULL
;
1683 if (IS_ADHOC_LOC (loc
))
1684 loc
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
1686 map0
= linemap_lookup (set
, loc
);
1687 if (!linemap_macro_expansion_map_p (map0
))
1690 resolved_loc
= linemap_resolve_location (set
, loc
,
1691 LRK_SPELLING_LOCATION
,
1694 if (resolved_loc
>= RESERVED_LOCATION_COUNT
1695 && !LINEMAP_SYSP (map1
))
1698 while (linemap_macro_expansion_map_p (map0
)
1699 && (resolved_loc
< RESERVED_LOCATION_COUNT
1700 || LINEMAP_SYSP (map1
)))
1702 loc
= linemap_unwind_toward_expansion (set
, loc
, &map0
);
1703 resolved_loc
= linemap_resolve_location (set
, loc
,
1704 LRK_SPELLING_LOCATION
,
1713 /* Expand source code location LOC and return a user readable source
1714 code location. LOC must be a spelling (non-virtual) location. If
1715 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1716 location is returned. */
1719 linemap_expand_location (struct line_maps
*set
,
1720 const struct line_map
*map
,
1721 source_location loc
)
1724 expanded_location xloc
;
1726 memset (&xloc
, 0, sizeof (xloc
));
1727 if (IS_ADHOC_LOC (loc
))
1730 = set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].data
;
1731 loc
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
1734 if (loc
< RESERVED_LOCATION_COUNT
)
1735 /* The location for this token wasn't generated from a line map.
1736 It was probably a location for a builtin token, chosen by some
1737 client code. Let's not try to expand the location in that
1739 else if (map
== NULL
)
1740 /* We shouldn't be getting a NULL map with a location that is not
1741 reserved by the client code. */
1745 /* MAP must be an ordinary map and LOC must be non-virtual,
1746 encoded into this map, obviously; the accessors used on MAP
1747 below ensure it is ordinary. Let's just assert the
1748 non-virtualness of LOC here. */
1749 if (linemap_location_from_macro_expansion_p (set
, loc
))
1752 const line_map_ordinary
*ord_map
= linemap_check_ordinary (map
);
1754 xloc
.file
= LINEMAP_FILE (ord_map
);
1755 xloc
.line
= SOURCE_LINE (ord_map
, loc
);
1756 xloc
.column
= SOURCE_COLUMN (ord_map
, loc
);
1757 xloc
.sysp
= LINEMAP_SYSP (ord_map
) != 0;
1764 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1765 is NULL, use stderr. IS_MACRO is true if the caller wants to
1766 dump a macro map, false otherwise. */
1769 linemap_dump (FILE *stream
, struct line_maps
*set
, unsigned ix
, bool is_macro
)
1771 const char *lc_reasons_v
[LC_ENTER_MACRO
+ 1]
1772 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1775 const line_map
*map
;
1781 map
= LINEMAPS_ORDINARY_MAP_AT (set
, ix
);
1783 map
= LINEMAPS_MACRO_MAP_AT (set
, ix
);
1785 reason
= (map
->reason
<= LC_ENTER_MACRO
) ? lc_reasons_v
[map
->reason
] : "???";
1787 fprintf (stream
, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1788 ix
, (void *) map
, map
->start_location
, reason
,
1790 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map
)))
1794 const line_map_ordinary
*ord_map
= linemap_check_ordinary (map
);
1795 unsigned includer_ix
;
1796 const line_map_ordinary
*includer_map
;
1798 includer_ix
= ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map
);
1799 includer_map
= includer_ix
< LINEMAPS_ORDINARY_USED (set
)
1800 ? LINEMAPS_ORDINARY_MAP_AT (set
, includer_ix
)
1803 fprintf (stream
, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map
),
1804 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
));
1805 fprintf (stream
, "Included from: [%d] %s\n", includer_ix
,
1806 includer_map
? ORDINARY_MAP_FILE_NAME (includer_map
) : "None");
1810 const line_map_macro
*macro_map
= linemap_check_macro (map
);
1811 fprintf (stream
, "Macro: %s (%u tokens)\n",
1812 linemap_map_get_macro_name (macro_map
),
1813 MACRO_MAP_NUM_MACRO_TOKENS (macro_map
));
1816 fprintf (stream
, "\n");
1820 /* Dump debugging information about source location LOC into the file
1821 stream STREAM. SET is the line map set LOC comes from. */
1824 linemap_dump_location (struct line_maps
*set
,
1825 source_location loc
,
1828 const line_map_ordinary
*map
;
1829 source_location location
;
1830 const char *path
= "", *from
= "";
1831 int l
= -1, c
= -1, s
= -1, e
= -1;
1833 if (IS_ADHOC_LOC (loc
))
1834 loc
= set
->location_adhoc_data_map
.data
[loc
& MAX_SOURCE_LOCATION
].locus
;
1840 linemap_resolve_location (set
, loc
, LRK_MACRO_DEFINITION_LOCATION
, &map
);
1843 /* Only reserved locations can be tolerated in this case. */
1844 linemap_assert (location
< RESERVED_LOCATION_COUNT
);
1847 path
= LINEMAP_FILE (map
);
1848 l
= SOURCE_LINE (map
, location
);
1849 c
= SOURCE_COLUMN (map
, location
);
1850 s
= LINEMAP_SYSP (map
) != 0;
1851 e
= location
!= loc
;
1855 from
= (INCLUDED_FROM (set
, map
))
1856 ? LINEMAP_FILE (INCLUDED_FROM (set
, map
))
1860 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1861 E: macro expansion?, LOC: original location, R: resolved location */
1862 fprintf (stream
, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1863 path
, from
, l
, c
, s
, (void*)map
, e
, loc
, location
);
1866 /* Return the highest location emitted for a given file for which
1867 there is a line map in SET. FILE_NAME is the file name to
1868 consider. If the function returns TRUE, *LOC is set to the highest
1869 location emitted for that file. */
1872 linemap_get_file_highest_location (struct line_maps
*set
,
1873 const char *file_name
,
1874 source_location
*loc
)
1876 /* If the set is empty or no ordinary map has been created then
1877 there is no file to look for ... */
1878 if (set
== NULL
|| set
->info_ordinary
.used
== 0)
1881 /* Now look for the last ordinary map created for FILE_NAME. */
1883 for (i
= set
->info_ordinary
.used
- 1; i
>= 0; --i
)
1885 const char *fname
= set
->info_ordinary
.maps
[i
].to_file
;
1886 if (fname
&& !filename_cmp (fname
, file_name
))
1893 /* The highest location for a given map is either the starting
1894 location of the next map minus one, or -- if the map is the
1895 latest one -- the highest location of the set. */
1896 source_location result
;
1897 if (i
== (int) set
->info_ordinary
.used
- 1)
1898 result
= set
->highest_location
;
1900 result
= set
->info_ordinary
.maps
[i
+ 1].start_location
- 1;
1906 /* Compute and return statistics about the memory consumption of some
1907 parts of the line table SET. */
1910 linemap_get_statistics (struct line_maps
*set
,
1911 struct linemap_stats
*s
)
1913 long ordinary_maps_allocated_size
, ordinary_maps_used_size
,
1914 macro_maps_allocated_size
, macro_maps_used_size
,
1915 macro_maps_locations_size
= 0, duplicated_macro_maps_locations_size
= 0;
1917 const line_map_macro
*cur_map
;
1919 ordinary_maps_allocated_size
=
1920 LINEMAPS_ORDINARY_ALLOCATED (set
) * sizeof (struct line_map_ordinary
);
1922 ordinary_maps_used_size
=
1923 LINEMAPS_ORDINARY_USED (set
) * sizeof (struct line_map_ordinary
);
1925 macro_maps_allocated_size
=
1926 LINEMAPS_MACRO_ALLOCATED (set
) * sizeof (struct line_map_macro
);
1928 for (cur_map
= LINEMAPS_MACRO_MAPS (set
);
1929 cur_map
&& cur_map
<= LINEMAPS_LAST_MACRO_MAP (set
);
1934 linemap_assert (linemap_macro_expansion_map_p (cur_map
));
1936 macro_maps_locations_size
+=
1937 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map
) * sizeof (source_location
);
1939 for (i
= 0; i
< 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map
); i
+= 2)
1941 if (MACRO_MAP_LOCATIONS (cur_map
)[i
] ==
1942 MACRO_MAP_LOCATIONS (cur_map
)[i
+ 1])
1943 duplicated_macro_maps_locations_size
+=
1944 sizeof (source_location
);
1948 macro_maps_used_size
=
1949 LINEMAPS_MACRO_USED (set
) * sizeof (struct line_map_macro
);
1951 s
->num_ordinary_maps_allocated
= LINEMAPS_ORDINARY_ALLOCATED (set
);
1952 s
->num_ordinary_maps_used
= LINEMAPS_ORDINARY_USED (set
);
1953 s
->ordinary_maps_allocated_size
= ordinary_maps_allocated_size
;
1954 s
->ordinary_maps_used_size
= ordinary_maps_used_size
;
1955 s
->num_expanded_macros
= num_expanded_macros_counter
;
1956 s
->num_macro_tokens
= num_macro_tokens_counter
;
1957 s
->num_macro_maps_used
= LINEMAPS_MACRO_USED (set
);
1958 s
->macro_maps_allocated_size
= macro_maps_allocated_size
;
1959 s
->macro_maps_locations_size
= macro_maps_locations_size
;
1960 s
->macro_maps_used_size
= macro_maps_used_size
;
1961 s
->duplicated_macro_maps_locations_size
=
1962 duplicated_macro_maps_locations_size
;
1963 s
->adhoc_table_size
= (set
->location_adhoc_data_map
.allocated
1964 * sizeof (struct location_adhoc_data
));
1965 s
->adhoc_table_entries_used
= set
->location_adhoc_data_map
.curr_loc
;
1969 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1970 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1971 specifies how many macro maps to dump. */
1974 line_table_dump (FILE *stream
, struct line_maps
*set
, unsigned int num_ordinary
,
1975 unsigned int num_macro
)
1985 fprintf (stream
, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set
));
1986 fprintf (stream
, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set
));
1987 fprintf (stream
, "Include stack depth: %d\n", set
->depth
);
1988 fprintf (stream
, "Highest location: %u\n", set
->highest_location
);
1992 fprintf (stream
, "\nOrdinary line maps\n");
1993 for (i
= 0; i
< num_ordinary
&& i
< LINEMAPS_ORDINARY_USED (set
); i
++)
1994 linemap_dump (stream
, set
, i
, false);
1995 fprintf (stream
, "\n");
2000 fprintf (stream
, "\nMacro line maps\n");
2001 for (i
= 0; i
< num_macro
&& i
< LINEMAPS_MACRO_USED (set
); i
++)
2002 linemap_dump (stream
, set
, i
, true);
2003 fprintf (stream
, "\n");
2007 /* class rich_location. */
2009 /* Construct a rich_location with location LOC as its initial range. */
2011 rich_location::rich_location (line_maps
*set
, source_location loc
) :
2014 m_column_override (0),
2015 m_have_expanded_location (false),
2017 m_seen_impossible_fixit (false),
2018 m_fixits_cannot_be_auto_applied (false)
2020 add_range (loc
, true);
2023 /* The destructor for class rich_location. */
2025 rich_location::~rich_location ()
2027 for (unsigned int i
= 0; i
< m_fixit_hints
.count (); i
++)
2028 delete get_fixit_hint (i
);
2031 /* Get location IDX within this rich_location. */
2034 rich_location::get_loc (unsigned int idx
) const
2036 const location_range
*locrange
= get_range (idx
);
2037 return locrange
->m_loc
;
2040 /* Get range IDX within this rich_location. */
2042 const location_range
*
2043 rich_location::get_range (unsigned int idx
) const
2045 return &m_ranges
[idx
];
2048 /* Mutable access to range IDX within this rich_location. */
2051 rich_location::get_range (unsigned int idx
)
2053 return &m_ranges
[idx
];
2056 /* Expand location IDX within this rich_location. */
2057 /* Get an expanded_location for this rich_location's primary
2061 rich_location::get_expanded_location (unsigned int idx
)
2065 /* Cache the expansion of the primary location. */
2066 if (!m_have_expanded_location
)
2069 = linemap_client_expand_location_to_spelling_point (get_loc (0));
2070 if (m_column_override
)
2071 m_expanded_location
.column
= m_column_override
;
2072 m_have_expanded_location
= true;
2075 return m_expanded_location
;
2078 return linemap_client_expand_location_to_spelling_point (get_loc (idx
));
2081 /* Set the column of the primary location, with 0 meaning
2082 "don't override it". */
2085 rich_location::override_column (int column
)
2087 m_column_override
= column
;
2088 m_have_expanded_location
= false;
2091 /* Add the given range. */
2094 rich_location::add_range (source_location loc
, bool show_caret_p
)
2096 location_range range
;
2098 range
.m_show_caret_p
= show_caret_p
;
2099 m_ranges
.push (range
);
2102 /* Add or overwrite the location given by IDX, setting its location to LOC,
2103 and setting its "should my caret be printed" flag to SHOW_CARET_P.
2105 It must either overwrite an existing location, or add one *exactly* on
2106 the end of the array.
2108 This is primarily for use by gcc when implementing diagnostic format
2110 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2111 (which writes the source location of a tree back into location 0 of
2112 the rich_location), and
2113 - the "%C" and "%L" format codes in the Fortran frontend. */
2116 rich_location::set_range (line_maps
* /*set*/, unsigned int idx
,
2117 source_location loc
, bool show_caret_p
)
2119 /* We can either overwrite an existing range, or add one exactly
2120 on the end of the array. */
2121 linemap_assert (idx
<= m_ranges
.count ());
2123 if (idx
== m_ranges
.count ())
2124 add_range (loc
, show_caret_p
);
2127 location_range
*locrange
= get_range (idx
);
2128 locrange
->m_loc
= loc
;
2129 locrange
->m_show_caret_p
= show_caret_p
;
2133 /* Mark any cached value here as dirty. */
2134 m_have_expanded_location
= false;
2137 /* Methods for adding insertion fix-it hints. */
2139 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2140 immediately before the primary range's start location. */
2143 rich_location::add_fixit_insert_before (const char *new_content
)
2145 add_fixit_insert_before (get_loc (), new_content
);
2148 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2149 immediately before the start of WHERE. */
2152 rich_location::add_fixit_insert_before (source_location where
,
2153 const char *new_content
)
2155 source_location start
= get_range_from_loc (m_line_table
, where
).m_start
;
2156 maybe_add_fixit (start
, start
, new_content
);
2159 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2160 immediately after the primary range's end-point. */
2163 rich_location::add_fixit_insert_after (const char *new_content
)
2165 add_fixit_insert_after (get_loc (), new_content
);
2168 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2169 immediately after the end-point of WHERE. */
2172 rich_location::add_fixit_insert_after (source_location where
,
2173 const char *new_content
)
2175 source_location finish
= get_range_from_loc (m_line_table
, where
).m_finish
;
2176 source_location next_loc
2177 = linemap_position_for_loc_and_offset (m_line_table
, finish
, 1);
2179 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2181 if (next_loc
== finish
)
2183 stop_supporting_fixits ();
2187 maybe_add_fixit (next_loc
, next_loc
, new_content
);
2190 /* Methods for adding removal fix-it hints. */
2192 /* Add a fixit-hint, suggesting removal of the content covered
2196 rich_location::add_fixit_remove ()
2198 add_fixit_remove (get_loc ());
2201 /* Add a fixit-hint, suggesting removal of the content between
2202 the start and finish of WHERE. */
2205 rich_location::add_fixit_remove (source_location where
)
2207 source_range range
= get_range_from_loc (m_line_table
, where
);
2208 add_fixit_remove (range
);
2211 /* Add a fixit-hint, suggesting removal of the content at
2215 rich_location::add_fixit_remove (source_range src_range
)
2217 add_fixit_replace (src_range
, "");
2220 /* Add a fixit-hint, suggesting replacement of the content covered
2221 by range 0 with NEW_CONTENT. */
2224 rich_location::add_fixit_replace (const char *new_content
)
2226 add_fixit_replace (get_loc (), new_content
);
2229 /* Methods for adding "replace" fix-it hints. */
2231 /* Add a fixit-hint, suggesting replacement of the content between
2232 the start and finish of WHERE with NEW_CONTENT. */
2235 rich_location::add_fixit_replace (source_location where
,
2236 const char *new_content
)
2238 source_range range
= get_range_from_loc (m_line_table
, where
);
2239 add_fixit_replace (range
, new_content
);
2242 /* Add a fixit-hint, suggesting replacement of the content at
2243 SRC_RANGE with NEW_CONTENT. */
2246 rich_location::add_fixit_replace (source_range src_range
,
2247 const char *new_content
)
2249 source_location start
= get_pure_location (m_line_table
, src_range
.m_start
);
2250 source_location finish
= get_pure_location (m_line_table
, src_range
.m_finish
);
2252 /* Fix-it hints use half-closed ranges, so attempt to offset the endpoint. */
2253 source_location next_loc
2254 = linemap_position_for_loc_and_offset (m_line_table
, finish
, 1);
2255 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2257 if (next_loc
== finish
)
2259 stop_supporting_fixits ();
2264 maybe_add_fixit (start
, finish
, new_content
);
2267 /* Get the last fix-it hint within this rich_location, or NULL if none. */
2270 rich_location::get_last_fixit_hint () const
2272 if (m_fixit_hints
.count () > 0)
2273 return get_fixit_hint (m_fixit_hints
.count () - 1);
2278 /* If WHERE is an "awkward" location, then mark this rich_location as not
2279 supporting fixits, purging any thay were already added, and return true.
2281 Otherwise (the common case), return false. */
2284 rich_location::reject_impossible_fixit (source_location where
)
2286 /* Fix-its within a rich_location should either all be suggested, or
2287 none of them should be suggested.
2288 Once we've rejected a fixit, we reject any more, even those
2289 with reasonable locations. */
2290 if (m_seen_impossible_fixit
)
2293 if (where
<= LINE_MAP_MAX_LOCATION_WITH_COLS
)
2294 /* WHERE is a reasonable location for a fix-it; don't reject it. */
2297 /* Otherwise we have an attempt to add a fix-it with an "awkward"
2298 location: either one that we can't obtain column information
2299 for (within an ordinary map), or one within a macro expansion. */
2300 stop_supporting_fixits ();
2304 /* Mark this rich_location as not supporting fixits, purging any that were
2308 rich_location::stop_supporting_fixits ()
2310 m_seen_impossible_fixit
= true;
2312 /* Purge the rich_location of any fix-its that were already added. */
2313 for (unsigned int i
= 0; i
< m_fixit_hints
.count (); i
++)
2314 delete get_fixit_hint (i
);
2315 m_fixit_hints
.truncate (0);
2318 /* Add HINT to the fix-it hints in this rich_location,
2319 consolidating into the prior fixit if possible. */
2322 rich_location::maybe_add_fixit (source_location start
,
2323 source_location next_loc
,
2324 const char *new_content
)
2326 if (reject_impossible_fixit (start
))
2328 if (reject_impossible_fixit (next_loc
))
2331 /* Only allow fix-it hints that affect a single line in one file.
2332 Compare the end-points. */
2333 expanded_location exploc_start
2334 = linemap_client_expand_location_to_spelling_point (start
);
2335 expanded_location exploc_next_loc
2336 = linemap_client_expand_location_to_spelling_point (next_loc
);
2337 /* They must be within the same file... */
2338 if (exploc_start
.file
!= exploc_next_loc
.file
)
2340 stop_supporting_fixits ();
2343 /* ...and on the same line. */
2344 if (exploc_start
.line
!= exploc_next_loc
.line
)
2346 stop_supporting_fixits ();
2350 const char *newline
= strchr (new_content
, '\n');
2353 /* For now, we can only support insertion of whole lines
2354 i.e. starts at start of line, and the newline is at the end of
2355 the insertion point. */
2357 /* It must be an insertion, not a replacement/deletion. */
2358 if (start
!= next_loc
)
2360 stop_supporting_fixits ();
2364 /* The insertion must be at the start of a line. */
2365 if (exploc_start
.column
!= 1)
2367 stop_supporting_fixits ();
2371 /* The newline must be at end of NEW_CONTENT.
2372 We could eventually split up fix-its at newlines if we wanted
2373 to allow more generality (e.g. to allow adding multiple lines
2374 with one add_fixit call. */
2375 if (newline
[1] != '\0')
2377 stop_supporting_fixits ();
2382 /* Consolidate neighboring fixits.
2383 Don't consolidate into newline-insertion fixits. */
2384 fixit_hint
*prev
= get_last_fixit_hint ();
2385 if (prev
&& !prev
->ends_with_newline_p ())
2386 if (prev
->maybe_append (start
, next_loc
, new_content
))
2389 m_fixit_hints
.push (new fixit_hint (start
, next_loc
, new_content
));
2392 /* class fixit_hint. */
2394 fixit_hint::fixit_hint (source_location start
,
2395 source_location next_loc
,
2396 const char *new_content
)
2398 m_next_loc (next_loc
),
2399 m_bytes (xstrdup (new_content
)),
2400 m_len (strlen (new_content
))
2404 /* Does this fix-it hint affect the given line? */
2407 fixit_hint::affects_line_p (const char *file
, int line
) const
2409 expanded_location exploc_start
2410 = linemap_client_expand_location_to_spelling_point (m_start
);
2411 if (file
!= exploc_start
.file
)
2413 if (line
< exploc_start
.line
)
2415 expanded_location exploc_next_loc
2416 = linemap_client_expand_location_to_spelling_point (m_next_loc
);
2417 if (file
!= exploc_next_loc
.file
)
2419 if (line
> exploc_next_loc
.line
)
2424 /* Method for consolidating fix-it hints, for use by
2425 rich_location::maybe_add_fixit.
2426 If possible, merge a pending fix-it hint with the given params
2427 into this one and return true.
2428 Otherwise return false. */
2431 fixit_hint::maybe_append (source_location start
,
2432 source_location next_loc
,
2433 const char *new_content
)
2435 /* For consolidation to be possible, START must be at this hint's
2437 if (start
!= m_next_loc
)
2440 /* If so, we have neighboring replacements; merge them. */
2441 m_next_loc
= next_loc
;
2442 size_t extra_len
= strlen (new_content
);
2443 m_bytes
= (char *)xrealloc (m_bytes
, m_len
+ extra_len
+ 1);
2444 memcpy (m_bytes
+ m_len
, new_content
, extra_len
);
2446 m_bytes
[m_len
] = '\0';
2450 /* Return true iff this hint's content ends with a newline. */
2453 fixit_hint::ends_with_newline_p () const
2457 return m_bytes
[m_len
- 1] == '\n';