1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2019 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 static void trace_include (const line_maps
*, const line_map_ordinary
*);
30 static const line_map_ordinary
* linemap_ordinary_map_lookup (line_maps
*,
32 static const line_map_macro
* linemap_macro_map_lookup (line_maps
*,
34 static location_t linemap_macro_map_loc_to_def_point
35 (const line_map_macro
*, location_t
);
36 static location_t linemap_macro_map_loc_to_exp_point
37 (const line_map_macro
*, location_t
);
38 static location_t linemap_macro_loc_to_spelling_point
39 (line_maps
*, location_t
, const line_map_ordinary
**);
40 static location_t
linemap_macro_loc_to_def_point (line_maps
*,
42 const line_map_ordinary
**);
43 static location_t
linemap_macro_loc_to_exp_point (line_maps
*,
45 const line_map_ordinary
**);
47 /* Counters defined in macro.c. */
48 extern unsigned num_expanded_macros_counter
;
49 extern unsigned num_macro_tokens_counter
;
51 /* Destructor for class line_maps.
52 Ensure non-GC-managed memory is released. */
54 line_maps::~line_maps ()
56 if (location_adhoc_data_map
.htab
)
57 htab_delete (location_adhoc_data_map
.htab
);
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
)
94 = (char *) ((uintptr_t) *((char **) slot
) + *((ptrdiff_t *) data
));
98 /* Rebuild the hash table from the location adhoc data. */
101 rebuild_location_adhoc_htab (line_maps
*set
)
104 set
->location_adhoc_data_map
.htab
=
105 htab_create (100, location_adhoc_data_hash
, location_adhoc_data_eq
, NULL
);
106 for (i
= 0; i
< set
->location_adhoc_data_map
.curr_loc
; i
++)
107 htab_find_slot (set
->location_adhoc_data_map
.htab
,
108 set
->location_adhoc_data_map
.data
+ i
, INSERT
);
111 /* Helper function for get_combined_adhoc_loc.
112 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
113 within a location_t, without needing to use an ad-hoc location. */
116 can_be_stored_compactly_p (line_maps
*set
,
118 source_range src_range
,
121 /* If there's an ad-hoc pointer, we can't store it directly in the
122 location_t, we need the lookaside. */
126 /* We only store ranges that begin at the locus and that are sufficiently
128 if (src_range
.m_start
!= locus
)
131 if (src_range
.m_finish
< src_range
.m_start
)
134 if (src_range
.m_start
< RESERVED_LOCATION_COUNT
)
137 if (locus
>= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
)
140 /* All 3 locations must be within ordinary maps, typically, the same
142 location_t lowest_macro_loc
= LINEMAPS_MACRO_LOWEST_LOCATION (set
);
143 if (locus
>= lowest_macro_loc
)
145 if (src_range
.m_start
>= lowest_macro_loc
)
147 if (src_range
.m_finish
>= lowest_macro_loc
)
150 /* Passed all tests. */
154 /* Combine LOCUS and DATA to a combined adhoc loc. */
157 get_combined_adhoc_loc (line_maps
*set
,
159 source_range src_range
,
162 struct location_adhoc_data lb
;
163 struct location_adhoc_data
**slot
;
165 if (IS_ADHOC_LOC (locus
))
166 locus
= get_location_from_adhoc_loc (set
, 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 location_t 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 (const class line_maps
*set
, location_t loc
)
247 linemap_assert (IS_ADHOC_LOC (loc
));
248 return set
->location_adhoc_data_map
.data
[loc
& MAX_LOCATION_T
].data
;
251 /* Return the location for the adhoc loc. */
254 get_location_from_adhoc_loc (const class line_maps
*set
, location_t loc
)
256 linemap_assert (IS_ADHOC_LOC (loc
));
257 return set
->location_adhoc_data_map
.data
[loc
& MAX_LOCATION_T
].locus
;
260 /* Return the source_range for adhoc location LOC. */
263 get_range_from_adhoc_loc (const class line_maps
*set
, location_t loc
)
265 linemap_assert (IS_ADHOC_LOC (loc
));
266 return set
->location_adhoc_data_map
.data
[loc
& MAX_LOCATION_T
].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 (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
, location_t loc
)
302 if (IS_ADHOC_LOC (loc
))
305 const line_map
*map
= linemap_lookup (set
, loc
);
308 const line_map_ordinary
*ordmap
= linemap_check_ordinary (map
);
310 if (loc
& ((1U << ordmap
->m_range_bits
) - 1))
316 /* Given location LOC within SET, strip away any packed range information
317 or ad-hoc information. */
320 get_pure_location (line_maps
*set
, location_t loc
)
322 if (IS_ADHOC_LOC (loc
))
323 loc
= get_location_from_adhoc_loc (set
, loc
);
325 if (loc
>= LINEMAPS_MACRO_LOWEST_LOCATION (set
))
328 if (loc
< RESERVED_LOCATION_COUNT
)
331 const line_map
*map
= linemap_lookup (set
, loc
);
332 const line_map_ordinary
*ordmap
= linemap_check_ordinary (map
);
334 return loc
& ~((1 << ordmap
->m_range_bits
) - 1);
337 /* Initialize a line map set. */
340 linemap_init (line_maps
*set
,
341 location_t builtin_location
)
343 #if __GNUC__ == 4 && __GNUC_MINOR__ == 2 && !defined (__clang__)
344 /* PR33916, needed to fix PR82939. */
345 memset (set
, 0, sizeof (line_maps
));
347 new (set
) line_maps();
349 /* Set default reallocator (used for initial alloc too). */
350 set
->reallocator
= xrealloc
;
351 set
->highest_location
= RESERVED_LOCATION_COUNT
- 1;
352 set
->highest_line
= RESERVED_LOCATION_COUNT
- 1;
353 set
->location_adhoc_data_map
.htab
=
354 htab_create (100, location_adhoc_data_hash
, location_adhoc_data_eq
, NULL
);
355 set
->builtin_location
= builtin_location
;
358 /* Return the ordinary line map from whence MAP was included. Returns
359 NULL if MAP was not an include. */
361 const line_map_ordinary
*
362 linemap_included_from_linemap (line_maps
*set
, const line_map_ordinary
*map
)
364 return linemap_ordinary_map_lookup (set
, linemap_included_from (map
));
367 /* Check for and warn about line_maps entered but not exited. */
370 linemap_check_files_exited (line_maps
*set
)
372 /* Depending upon whether we are handling preprocessed input or
373 not, this can be a user error or an ICE. */
374 for (const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
376 map
= linemap_included_from_linemap (set
, map
))
377 fprintf (stderr
, "line-map.c: file \"%s\" entered but not left\n",
378 ORDINARY_MAP_FILE_NAME (map
));
381 /* Create a new line map in the line map set SET, and return it.
382 REASON is the reason of creating the map. It determines the type
383 of map created (ordinary or macro map). Note that ordinary maps and
384 macro maps are allocated in different memory location. */
386 static struct line_map
*
387 new_linemap (line_maps
*set
, location_t start_location
)
389 bool macro_p
= start_location
>= LINE_MAP_MAX_LOCATION
;
390 unsigned num_maps_allocated
= LINEMAPS_ALLOCATED (set
, macro_p
);
391 unsigned num_maps_used
= LINEMAPS_USED (set
, macro_p
);
393 if (num_maps_used
== num_maps_allocated
)
395 /* We need more space! */
396 if (!num_maps_allocated
)
397 num_maps_allocated
= 128;
398 num_maps_allocated
*= 2;
400 size_t size_of_a_map
;
404 size_of_a_map
= sizeof (line_map_macro
);
405 buffer
= set
->info_macro
.maps
;
409 size_of_a_map
= sizeof (line_map_ordinary
);
410 buffer
= set
->info_ordinary
.maps
;
413 /* We are going to execute some dance to try to reduce the
414 overhead of the memory allocator, in case we are using the
417 The actual size of memory we are going to get back from the
418 allocator may well be larger than what we ask for. Use this
419 hook to find what that size is. */
421 = set
->round_alloc_size (num_maps_allocated
* size_of_a_map
);
423 /* Now alloc_size contains the exact memory size we would get if
424 we have asked for the initial alloc_size amount of memory.
425 Let's get back to the number of map that amounts to. */
426 unsigned num_maps
= alloc_size
/ size_of_a_map
;
427 buffer
= set
->reallocator (buffer
, num_maps
* size_of_a_map
);
428 memset ((char *)buffer
+ num_maps_used
* size_of_a_map
, 0,
429 (num_maps
- num_maps_used
) * size_of_a_map
);
431 set
->info_macro
.maps
= (line_map_macro
*)buffer
;
433 set
->info_ordinary
.maps
= (line_map_ordinary
*)buffer
;
434 LINEMAPS_ALLOCATED (set
, macro_p
) = num_maps
;
437 line_map
*result
= (macro_p
? (line_map
*)&set
->info_macro
.maps
[num_maps_used
]
438 : (line_map
*)&set
->info_ordinary
.maps
[num_maps_used
]);
439 LINEMAPS_USED (set
, macro_p
)++;
441 result
->start_location
= start_location
;
446 /* Add a mapping of logical source line to physical source file and
449 The text pointed to by TO_FILE must have a lifetime
450 at least as long as the final call to lookup_line (). An empty
451 TO_FILE means standard input. If reason is LC_LEAVE, and
452 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
453 natural values considering the file we are returning to.
455 FROM_LINE should be monotonic increasing across calls to this
456 function. A call to this function can relocate the previous set of
457 maps, so any stored line_map pointers should not be used. */
459 const struct line_map
*
460 linemap_add (line_maps
*set
, enum lc_reason reason
,
461 unsigned int sysp
, const char *to_file
, linenum_type to_line
)
463 /* Generate a start_location above the current highest_location.
464 If possible, make the low range bits be zero. */
465 location_t start_location
;
466 if (set
->highest_location
< LINE_MAP_MAX_LOCATION_WITH_COLS
)
468 start_location
= set
->highest_location
+ (1 << set
->default_range_bits
);
469 if (set
->default_range_bits
)
470 start_location
&= ~((1 << set
->default_range_bits
) - 1);
471 linemap_assert (0 == (start_location
472 & ((1 << set
->default_range_bits
) - 1)));
475 start_location
= set
->highest_location
+ 1;
477 linemap_assert (!LINEMAPS_ORDINARY_USED (set
)
479 >= MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set
))));
481 /* When we enter the file for the first time reason cannot be
483 linemap_assert (!(set
->depth
== 0 && reason
== LC_RENAME
));
485 /* If we are leaving the main file, return a NULL map. */
486 if (reason
== LC_LEAVE
487 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set
))
494 linemap_assert (reason
!= LC_ENTER_MACRO
);
496 if (start_location
>= LINE_MAP_MAX_LOCATION
)
497 /* We ran out of line map space. */
500 line_map_ordinary
*map
501 = linemap_check_ordinary (new_linemap (set
, start_location
));
502 map
->reason
= reason
;
504 if (to_file
&& *to_file
== '\0' && reason
!= LC_RENAME_VERBATIM
)
507 if (reason
== LC_RENAME_VERBATIM
)
510 const line_map_ordinary
*from
= NULL
;
511 if (reason
== LC_LEAVE
)
513 /* When we are just leaving an "included" file, and jump to the next
514 location inside the "includer" right after the #include
515 "included", this variable points the map in use right before the
516 #include "included", inside the same "includer" file. */
518 linemap_assert (!MAIN_FILE_P (map
- 1));
519 /* (MAP - 1) points to the map we are leaving. The
520 map from which (MAP - 1) got included should be the map
521 that comes right before MAP in the same file. */
522 from
= linemap_included_from_linemap (set
, map
- 1);
524 /* A TO_FILE of NULL is special - we use the natural values. */
527 to_file
= ORDINARY_MAP_FILE_NAME (from
);
528 to_line
= SOURCE_LINE (from
, from
[1].start_location
);
529 sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (from
);
532 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from
),
537 map
->to_file
= to_file
;
538 map
->to_line
= to_line
;
539 LINEMAPS_ORDINARY_CACHE (set
) = LINEMAPS_ORDINARY_USED (set
) - 1;
540 map
->m_column_and_range_bits
= 0;
541 map
->m_range_bits
= 0;
542 set
->highest_location
= start_location
;
543 set
->highest_line
= start_location
;
544 set
->max_column_hint
= 0;
546 /* This assertion is placed after set->highest_location has
547 been updated, since the latter affects
548 linemap_location_from_macro_expansion_p, which ultimately affects
550 linemap_assert (pure_location_p (set
, start_location
));
552 if (reason
== LC_ENTER
)
555 map
->included_from
= 0;
557 /* The location of the end of the just-closed map. */
559 = (((map
[0].start_location
- 1 - map
[-1].start_location
)
560 & ~((1 << map
[-1].m_column_and_range_bits
) - 1))
561 + map
[-1].start_location
);
563 if (set
->trace_includes
)
564 trace_include (set
, map
);
566 else if (reason
== LC_RENAME
)
567 map
->included_from
= linemap_included_from (&map
[-1]);
568 else if (reason
== LC_LEAVE
)
571 map
->included_from
= linemap_included_from (from
);
577 /* Returns TRUE if the line table set tracks token locations across
578 macro expansion, FALSE otherwise. */
581 linemap_tracks_macro_expansion_locs_p (line_maps
*set
)
583 return LINEMAPS_MACRO_MAPS (set
) != NULL
;
586 /* Create a macro map. A macro map encodes source locations of tokens
587 that are part of a macro replacement-list, at a macro expansion
588 point. See the extensive comments of struct line_map and struct
589 line_map_macro, in line-map.h.
591 This map shall be created when the macro is expanded. The map
592 encodes the source location of the expansion point of the macro as
593 well as the "original" source location of each token that is part
594 of the macro replacement-list. If a macro is defined but never
595 expanded, it has no macro map. SET is the set of maps the macro
596 map should be part of. MACRO_NODE is the macro which the new macro
597 map should encode source locations for. EXPANSION is the location
598 of the expansion point of MACRO. For function-like macros
599 invocations, it's best to make it point to the closing parenthesis
600 of the macro, rather than the the location of the first character
601 of the macro. NUM_TOKENS is the number of tokens that are part of
602 the replacement-list of MACRO.
604 Note that when we run out of the integer space available for source
605 locations, this function returns NULL. In that case, callers of
606 this function cannot encode {line,column} pairs into locations of
607 macro tokens anymore. */
609 const line_map_macro
*
610 linemap_enter_macro (class line_maps
*set
, struct cpp_hashnode
*macro_node
,
611 location_t expansion
, unsigned int num_tokens
)
613 location_t start_location
614 = LINEMAPS_MACRO_LOWEST_LOCATION (set
) - num_tokens
;
616 if (start_location
< LINE_MAP_MAX_LOCATION
)
617 /* We ran out of macro map space. */
620 line_map_macro
*map
= linemap_check_macro (new_linemap (set
, start_location
));
622 map
->macro
= macro_node
;
623 map
->n_tokens
= num_tokens
;
625 = (location_t
*) set
->reallocator (NULL
,
627 * sizeof (location_t
));
628 map
->expansion
= expansion
;
629 memset (MACRO_MAP_LOCATIONS (map
), 0,
630 2 * num_tokens
* sizeof (location_t
));
632 LINEMAPS_MACRO_CACHE (set
) = LINEMAPS_MACRO_USED (set
) - 1;
637 /* Create and return a virtual location for a token that is part of a
638 macro expansion-list at a macro expansion point. See the comment
639 inside struct line_map_macro to see what an expansion-list exactly
642 A call to this function must come after a call to
645 MAP is the map into which the source location is created. TOKEN_NO
646 is the index of the token in the macro replacement-list, starting
649 ORIG_LOC is the location of the token outside of this macro
650 expansion. If the token comes originally from the macro
651 definition, it is the locus in the macro definition; otherwise it
652 is a location in the context of the caller of this macro expansion
653 (which is a virtual location or a source location if the caller is
654 itself a macro expansion or not).
656 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
657 either of the token itself or of a macro parameter that it
661 linemap_add_macro_token (const line_map_macro
*map
,
662 unsigned int token_no
,
664 location_t orig_parm_replacement_loc
)
668 linemap_assert (linemap_macro_expansion_map_p (map
));
669 linemap_assert (token_no
< MACRO_MAP_NUM_MACRO_TOKENS (map
));
671 MACRO_MAP_LOCATIONS (map
)[2 * token_no
] = orig_loc
;
672 MACRO_MAP_LOCATIONS (map
)[2 * token_no
+ 1] = orig_parm_replacement_loc
;
674 result
= MAP_START_LOCATION (map
) + token_no
;
678 /* Return a location_t for the start (i.e. column==0) of
679 (physical) line TO_LINE in the current source file (as in the
680 most recent linemap_add). MAX_COLUMN_HINT is the highest column
681 number we expect to use in this line (but it does not change
682 the highest_location). */
685 linemap_line_start (line_maps
*set
, linenum_type to_line
,
686 unsigned int max_column_hint
)
688 line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
689 location_t highest
= set
->highest_location
;
691 linenum_type last_line
=
692 SOURCE_LINE (map
, set
->highest_line
);
693 int line_delta
= to_line
- last_line
;
694 bool add_map
= false;
695 linemap_assert (map
->m_column_and_range_bits
>= map
->m_range_bits
);
696 int effective_column_bits
= map
->m_column_and_range_bits
- map
->m_range_bits
;
700 && line_delta
* map
->m_column_and_range_bits
> 1000)
701 || (max_column_hint
>= (1U << effective_column_bits
))
702 || (max_column_hint
<= 80 && effective_column_bits
>= 10)
703 || (highest
> LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
704 && map
->m_range_bits
> 0)
705 || (highest
> LINE_MAP_MAX_LOCATION_WITH_COLS
706 && (set
->max_column_hint
|| highest
>= LINE_MAP_MAX_LOCATION
)))
709 max_column_hint
= set
->max_column_hint
;
714 if (max_column_hint
> LINE_MAP_MAX_COLUMN_NUMBER
715 || highest
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
717 /* If the column number is ridiculous or we've allocated a huge
718 number of location_ts, give up on column numbers
719 (and on packed ranges). */
723 if (highest
>= LINE_MAP_MAX_LOCATION
)
729 if (highest
<= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
)
730 range_bits
= set
->default_range_bits
;
733 while (max_column_hint
>= (1U << column_bits
))
735 max_column_hint
= 1U << column_bits
;
736 column_bits
+= range_bits
;
738 /* Allocate the new line_map. However, if the current map only has a
739 single line we can sometimes just increase its column_bits instead. */
741 || last_line
!= ORDINARY_MAP_STARTING_LINE_NUMBER (map
)
742 || SOURCE_COLUMN (map
, highest
) >= (1U << (column_bits
- range_bits
))
743 || ( /* We can't reuse the map if the line offset is sufficiently
744 large to cause overflow when computing location_t values. */
745 (to_line
- ORDINARY_MAP_STARTING_LINE_NUMBER (map
))
747 << (CHAR_BIT
* sizeof (linenum_type
) - column_bits
)))
748 || range_bits
< map
->m_range_bits
)
749 map
= linemap_check_ordinary
750 (const_cast <line_map
*>
751 (linemap_add (set
, LC_RENAME
,
752 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
),
753 ORDINARY_MAP_FILE_NAME (map
),
755 map
->m_column_and_range_bits
= column_bits
;
756 map
->m_range_bits
= range_bits
;
757 r
= (MAP_START_LOCATION (map
)
758 + ((to_line
- ORDINARY_MAP_STARTING_LINE_NUMBER (map
))
762 r
= set
->highest_line
+ (line_delta
<< map
->m_column_and_range_bits
);
764 /* Locations of ordinary tokens are always lower than locations of
766 if (r
>= LINE_MAP_MAX_LOCATION
)
768 /* Remember we overflowed. */
769 set
->highest_line
= set
->highest_location
= LINE_MAP_MAX_LOCATION
- 1;
773 set
->highest_line
= r
;
774 if (r
> set
->highest_location
)
775 set
->highest_location
= r
;
776 set
->max_column_hint
= max_column_hint
;
778 /* At this point, we expect one of:
779 (a) the normal case: a "pure" location with 0 range bits, or
780 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
781 columns anymore (or ranges), or
782 (c) we're in a region with a column hint exceeding
783 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
784 with column_bits == 0. */
785 linemap_assert (pure_location_p (set
, r
)
786 || r
>= LINE_MAP_MAX_LOCATION_WITH_COLS
787 || map
->m_column_and_range_bits
== 0);
788 linemap_assert (SOURCE_LINE (map
, r
) == to_line
);
792 /* Encode and return a location_t from a column number. The
793 source line considered is the last source line used to call
794 linemap_line_start, i.e, the last source line which a location was
798 linemap_position_for_column (line_maps
*set
, unsigned int to_column
)
800 location_t r
= set
->highest_line
;
803 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set
)));
805 if (to_column
>= set
->max_column_hint
)
807 if (r
> LINE_MAP_MAX_LOCATION_WITH_COLS
808 || to_column
> LINE_MAP_MAX_COLUMN_NUMBER
)
810 /* Running low on location_ts - disable column numbers. */
815 /* Otherwise, attempt to start a new line that can hold TO_COLUMN,
816 with some space to spare. This may or may not lead to a new
817 linemap being created. */
818 line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
819 r
= linemap_line_start (set
, SOURCE_LINE (map
, r
), to_column
+ 50);
820 map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
821 if (map
->m_column_and_range_bits
== 0)
823 /* ...then the linemap has column-tracking disabled,
824 presumably due to exceeding either
825 LINE_MAP_MAX_LOCATION_WITH_COLS (overall) or
826 LINE_MAP_MAX_COLUMN_NUMBER (within this line).
827 Return the start of the linemap, which encodes column 0, for
833 line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (set
);
834 r
= r
+ (to_column
<< map
->m_range_bits
);
835 if (r
>= set
->highest_location
)
836 set
->highest_location
= r
;
840 /* Encode and return a source location from a given line and
844 linemap_position_for_line_and_column (line_maps
*set
,
845 const line_map_ordinary
*ord_map
,
849 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
) <= line
);
851 location_t r
= MAP_START_LOCATION (ord_map
);
852 r
+= ((line
- ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
))
853 << ord_map
->m_column_and_range_bits
);
854 if (r
<= LINE_MAP_MAX_LOCATION_WITH_COLS
)
855 r
+= ((column
& ((1 << ord_map
->m_column_and_range_bits
) - 1))
856 << ord_map
->m_range_bits
);
857 location_t upper_limit
= LINEMAPS_MACRO_LOWEST_LOCATION (set
);
858 if (r
>= upper_limit
)
860 if (r
> set
->highest_location
)
861 set
->highest_location
= r
;
865 /* Encode and return a location_t starting from location LOC and
866 shifting it by COLUMN_OFFSET columns. This function does not support
867 virtual locations. */
870 linemap_position_for_loc_and_offset (line_maps
*set
,
872 unsigned int column_offset
)
874 const line_map_ordinary
* map
= NULL
;
876 if (IS_ADHOC_LOC (loc
))
877 loc
= get_location_from_adhoc_loc (set
, loc
);
879 /* This function does not support virtual locations yet. */
880 if (linemap_location_from_macro_expansion_p (set
, loc
))
883 if (column_offset
== 0
884 /* Adding an offset to a reserved location (like
885 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
886 sense. So let's leave the location intact in that case. */
887 || loc
< RESERVED_LOCATION_COUNT
)
890 /* We find the real location and shift it. */
891 loc
= linemap_resolve_location (set
, loc
, LRK_SPELLING_LOCATION
, &map
);
892 /* The new location (loc + offset) should be higher than the first
893 location encoded by MAP. This can fail if the line information
894 is messed up because of line directives (see PR66415). */
895 if (MAP_START_LOCATION (map
) >= loc
+ (column_offset
<< map
->m_range_bits
))
898 linenum_type line
= SOURCE_LINE (map
, loc
);
899 unsigned int column
= SOURCE_COLUMN (map
, loc
);
901 /* If MAP is not the last line map of its set, then the new location
902 (loc + offset) should be less than the first location encoded by
903 the next line map of the set. Otherwise, we try to encode the
904 location in the next map. */
905 while (map
!= LINEMAPS_LAST_ORDINARY_MAP (set
)
906 && (loc
+ (column_offset
<< map
->m_range_bits
)
907 >= MAP_START_LOCATION (&map
[1])))
910 /* If the next map starts in a higher line, we cannot encode the
912 if (line
< ORDINARY_MAP_STARTING_LINE_NUMBER (map
))
916 column
+= column_offset
;
918 /* Bail out if the column is not representable within the existing
920 if (column
>= (1u << (map
->m_column_and_range_bits
- map
->m_range_bits
)))
924 linemap_position_for_line_and_column (set
, map
, line
, column
);
925 if (linemap_assert_fails (r
<= set
->highest_location
)
926 || linemap_assert_fails (map
== linemap_lookup (set
, r
)))
932 /* Given a virtual source location yielded by a map (either an
933 ordinary or a macro map), returns that map. */
935 const struct line_map
*
936 linemap_lookup (line_maps
*set
, location_t line
)
938 if (IS_ADHOC_LOC (line
))
939 line
= get_location_from_adhoc_loc (set
, line
);
940 if (linemap_location_from_macro_expansion_p (set
, line
))
941 return linemap_macro_map_lookup (set
, line
);
942 return linemap_ordinary_map_lookup (set
, line
);
945 /* Given a source location yielded by an ordinary map, returns that
946 map. Since the set is built chronologically, the logical lines are
947 monotonic increasing, and so the list is sorted and we can use a
950 static const line_map_ordinary
*
951 linemap_ordinary_map_lookup (line_maps
*set
, location_t line
)
953 unsigned int md
, mn
, mx
;
954 const line_map_ordinary
*cached
, *result
;
956 if (IS_ADHOC_LOC (line
))
957 line
= get_location_from_adhoc_loc (set
, line
);
959 if (set
== NULL
|| line
< RESERVED_LOCATION_COUNT
)
962 mn
= LINEMAPS_ORDINARY_CACHE (set
);
963 mx
= LINEMAPS_ORDINARY_USED (set
);
965 cached
= LINEMAPS_ORDINARY_MAP_AT (set
, mn
);
966 /* We should get a segfault if no line_maps have been added yet. */
967 if (line
>= MAP_START_LOCATION (cached
))
969 if (mn
+ 1 == mx
|| line
< MAP_START_LOCATION (&cached
[1]))
981 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set
, md
)) > line
)
987 LINEMAPS_ORDINARY_CACHE (set
) = mn
;
988 result
= LINEMAPS_ORDINARY_MAP_AT (set
, mn
);
989 linemap_assert (line
>= MAP_START_LOCATION (result
));
993 /* Given a source location yielded by a macro map, returns that map.
994 Since the set is built chronologically, the logical lines are
995 monotonic decreasing, and so the list is sorted and we can use a
998 static const line_map_macro
*
999 linemap_macro_map_lookup (line_maps
*set
, location_t line
)
1001 unsigned int md
, mn
, mx
;
1002 const struct line_map_macro
*cached
, *result
;
1004 if (IS_ADHOC_LOC (line
))
1005 line
= get_location_from_adhoc_loc (set
, line
);
1007 linemap_assert (line
>= LINEMAPS_MACRO_LOWEST_LOCATION (set
));
1012 mn
= LINEMAPS_MACRO_CACHE (set
);
1013 mx
= LINEMAPS_MACRO_USED (set
);
1014 cached
= LINEMAPS_MACRO_MAP_AT (set
, mn
);
1016 if (line
>= MAP_START_LOCATION (cached
))
1018 if (mn
== 0 || line
< MAP_START_LOCATION (&cached
[-1]))
1027 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set
, md
)) > line
)
1033 LINEMAPS_MACRO_CACHE (set
) = mx
;
1034 result
= LINEMAPS_MACRO_MAP_AT (set
, LINEMAPS_MACRO_CACHE (set
));
1035 linemap_assert (MAP_START_LOCATION (result
) <= line
);
1040 /* Return TRUE if MAP encodes locations coming from a macro
1041 replacement-list at macro expansion point. */
1044 linemap_macro_expansion_map_p (const struct line_map
*map
)
1046 return map
&& !MAP_ORDINARY_P (map
);
1049 /* If LOCATION is the locus of a token in a replacement-list of a
1050 macro expansion return the location of the macro expansion point.
1052 Read the comments of struct line_map and struct line_map_macro in
1053 line-map.h to understand what a macro expansion point is. */
1056 linemap_macro_map_loc_to_exp_point (const line_map_macro
*map
,
1057 location_t location ATTRIBUTE_UNUSED
)
1059 linemap_assert (linemap_macro_expansion_map_p (map
)
1060 && location
>= MAP_START_LOCATION (map
));
1062 /* Make sure LOCATION is correct. */
1063 linemap_assert ((location
- MAP_START_LOCATION (map
))
1064 < MACRO_MAP_NUM_MACRO_TOKENS (map
));
1066 return MACRO_MAP_EXPANSION_POINT_LOCATION (map
);
1069 /* LOCATION is the source location of a token that belongs to a macro
1070 replacement-list as part of the macro expansion denoted by MAP.
1072 Return the location of the token at the definition point of the
1076 linemap_macro_map_loc_to_def_point (const line_map_macro
*map
,
1077 location_t location
)
1081 linemap_assert (linemap_macro_expansion_map_p (map
)
1082 && location
>= MAP_START_LOCATION (map
));
1083 linemap_assert (location
>= RESERVED_LOCATION_COUNT
);
1085 token_no
= location
- MAP_START_LOCATION (map
);
1086 linemap_assert (token_no
< MACRO_MAP_NUM_MACRO_TOKENS (map
));
1088 location
= MACRO_MAP_LOCATIONS (map
)[2 * token_no
+ 1];
1093 /* If LOCATION is the locus of a token that is an argument of a
1094 function-like macro M and appears in the expansion of M, return the
1095 locus of that argument in the context of the caller of M.
1097 In other words, this returns the xI location presented in the
1098 comments of line_map_macro above. */
1100 linemap_macro_map_loc_unwind_toward_spelling (line_maps
*set
,
1101 const line_map_macro
* map
,
1102 location_t location
)
1106 if (IS_ADHOC_LOC (location
))
1107 location
= get_location_from_adhoc_loc (set
, location
);
1109 linemap_assert (linemap_macro_expansion_map_p (map
)
1110 && location
>= MAP_START_LOCATION (map
));
1111 linemap_assert (location
>= RESERVED_LOCATION_COUNT
);
1112 linemap_assert (!IS_ADHOC_LOC (location
));
1114 token_no
= location
- MAP_START_LOCATION (map
);
1115 linemap_assert (token_no
< MACRO_MAP_NUM_MACRO_TOKENS (map
));
1117 location
= MACRO_MAP_LOCATIONS (map
)[2 * token_no
];
1122 /* Return the source line number corresponding to source location
1123 LOCATION. SET is the line map set LOCATION comes from. If
1124 LOCATION is the source location of token that is part of the
1125 replacement-list of a macro expansion return the line number of the
1126 macro expansion point. */
1129 linemap_get_expansion_line (line_maps
*set
,
1130 location_t location
)
1132 const line_map_ordinary
*map
= NULL
;
1134 if (IS_ADHOC_LOC (location
))
1135 location
= get_location_from_adhoc_loc (set
, location
);
1137 if (location
< RESERVED_LOCATION_COUNT
)
1141 linemap_macro_loc_to_exp_point (set
, location
, &map
);
1143 return SOURCE_LINE (map
, location
);
1146 /* Return the path of the file corresponding to source code location
1149 If LOCATION is the source location of token that is part of the
1150 replacement-list of a macro expansion return the file path of the
1151 macro expansion point.
1153 SET is the line map set LOCATION comes from. */
1156 linemap_get_expansion_filename (line_maps
*set
,
1157 location_t location
)
1159 const struct line_map_ordinary
*map
= NULL
;
1161 if (IS_ADHOC_LOC (location
))
1162 location
= get_location_from_adhoc_loc (set
, location
);
1164 if (location
< RESERVED_LOCATION_COUNT
)
1167 linemap_macro_loc_to_exp_point (set
, location
, &map
);
1169 return LINEMAP_FILE (map
);
1172 /* Return the name of the macro associated to MACRO_MAP. */
1175 linemap_map_get_macro_name (const line_map_macro
*macro_map
)
1177 linemap_assert (macro_map
&& linemap_macro_expansion_map_p (macro_map
));
1178 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map
));
1181 /* Return a positive value if LOCATION is the locus of a token that is
1182 located in a system header, O otherwise. It returns 1 if LOCATION
1183 is the locus of a token that is located in a system header, and 2
1184 if LOCATION is the locus of a token located in a C system header
1185 that therefore needs to be extern "C" protected in C++.
1187 Note that this function returns 1 if LOCATION belongs to a token
1188 that is part of a macro replacement-list defined in a system
1189 header, but expanded in a non-system file. */
1192 linemap_location_in_system_header_p (line_maps
*set
,
1193 location_t location
)
1195 const struct line_map
*map
= NULL
;
1197 if (IS_ADHOC_LOC (location
))
1198 location
= get_location_from_adhoc_loc (set
, location
);
1200 if (location
< RESERVED_LOCATION_COUNT
)
1203 /* Let's look at where the token for LOCATION comes from. */
1206 map
= linemap_lookup (set
, location
);
1209 if (!linemap_macro_expansion_map_p (map
))
1210 /* It's a normal token. */
1211 return LINEMAP_SYSP (linemap_check_ordinary (map
));
1214 const line_map_macro
*macro_map
= linemap_check_macro (map
);
1216 /* It's a token resulting from a macro expansion. */
1218 linemap_macro_map_loc_unwind_toward_spelling (set
, macro_map
, location
);
1219 if (loc
< RESERVED_LOCATION_COUNT
)
1220 /* This token might come from a built-in macro. Let's
1221 look at where that macro got expanded. */
1222 location
= linemap_macro_map_loc_to_exp_point (macro_map
, location
);
1233 /* Return TRUE if LOCATION is a source code location of a token that is part of
1234 a macro expansion, FALSE otherwise. */
1237 linemap_location_from_macro_expansion_p (const class line_maps
*set
,
1238 location_t location
)
1240 if (IS_ADHOC_LOC (location
))
1241 location
= get_location_from_adhoc_loc (set
, location
);
1243 return IS_MACRO_LOC (location
);
1246 /* Given two virtual locations *LOC0 and *LOC1, return the first
1247 common macro map in their macro expansion histories. Return NULL
1248 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1249 virtual location of the token inside the resulting macro. */
1251 static const struct line_map
*
1252 first_map_in_common_1 (line_maps
*set
,
1256 location_t l0
= *loc0
, l1
= *loc1
;
1257 const struct line_map
*map0
= linemap_lookup (set
, l0
);
1258 if (IS_ADHOC_LOC (l0
))
1259 l0
= get_location_from_adhoc_loc (set
, l0
);
1261 const struct line_map
*map1
= linemap_lookup (set
, l1
);
1262 if (IS_ADHOC_LOC (l1
))
1263 l1
= get_location_from_adhoc_loc (set
, l1
);
1265 while (linemap_macro_expansion_map_p (map0
)
1266 && linemap_macro_expansion_map_p (map1
)
1269 if (MAP_START_LOCATION (map0
) < MAP_START_LOCATION (map1
))
1271 l0
= linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0
),
1273 map0
= linemap_lookup (set
, l0
);
1277 l1
= linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1
),
1279 map1
= linemap_lookup (set
, l1
);
1292 /* Given two virtual locations LOC0 and LOC1, return the first common
1293 macro map in their macro expansion histories. Return NULL if no
1294 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1295 virtual location of the token inside the resulting macro, upon
1296 return of a non-NULL result. */
1298 static const struct line_map
*
1299 first_map_in_common (line_maps
*set
,
1302 location_t
*res_loc0
,
1303 location_t
*res_loc1
)
1308 return first_map_in_common_1 (set
, res_loc0
, res_loc1
);
1311 /* Return a positive value if PRE denotes the location of a token that
1312 comes before the token of POST, 0 if PRE denotes the location of
1313 the same token as the token for POST, and a negative value
1317 linemap_compare_locations (line_maps
*set
,
1321 bool pre_virtual_p
, post_virtual_p
;
1322 location_t l0
= pre
, l1
= post
;
1324 if (IS_ADHOC_LOC (l0
))
1325 l0
= get_location_from_adhoc_loc (set
, l0
);
1326 if (IS_ADHOC_LOC (l1
))
1327 l1
= get_location_from_adhoc_loc (set
, l1
);
1332 if ((pre_virtual_p
= linemap_location_from_macro_expansion_p (set
, l0
)))
1333 l0
= linemap_resolve_location (set
, l0
,
1334 LRK_MACRO_EXPANSION_POINT
,
1337 if ((post_virtual_p
= linemap_location_from_macro_expansion_p (set
, l1
)))
1338 l1
= linemap_resolve_location (set
, l1
,
1339 LRK_MACRO_EXPANSION_POINT
,
1346 /* So pre and post represent two tokens that are present in a
1347 same macro expansion. Let's see if the token for pre was
1348 before the token for post in that expansion. */
1350 const struct line_map
*map
=
1351 first_map_in_common (set
, pre
, post
, &l0
, &l1
);
1354 /* This should not be possible. */
1357 i0
= l0
- MAP_START_LOCATION (map
);
1358 i1
= l1
- MAP_START_LOCATION (map
);
1362 if (IS_ADHOC_LOC (l0
))
1363 l0
= get_location_from_adhoc_loc (set
, l0
);
1364 if (IS_ADHOC_LOC (l1
))
1365 l1
= get_location_from_adhoc_loc (set
, l1
);
1370 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1373 trace_include (const class line_maps
*set
, const line_map_ordinary
*map
)
1375 unsigned int i
= set
->depth
;
1380 fprintf (stderr
, " %s\n", ORDINARY_MAP_FILE_NAME (map
));
1383 /* Return the spelling location of the token wherever it comes from,
1384 whether part of a macro definition or not.
1386 This is a subroutine for linemap_resolve_location. */
1389 linemap_macro_loc_to_spelling_point (line_maps
*set
,
1390 location_t location
,
1391 const line_map_ordinary
**original_map
)
1393 linemap_assert (set
&& location
>= RESERVED_LOCATION_COUNT
);
1397 const struct line_map
*map
= linemap_lookup (set
, location
);
1398 if (!map
|| MAP_ORDINARY_P (map
))
1401 *original_map
= (const line_map_ordinary
*)map
;
1405 location
= linemap_macro_map_loc_unwind_toward_spelling
1406 (set
, linemap_check_macro (map
), location
);
1412 /* If LOCATION is the source location of a token that belongs to a
1413 macro replacement-list -- as part of a macro expansion -- then
1414 return the location of the token at the definition point of the
1415 macro. Otherwise, return LOCATION. SET is the set of maps
1416 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1417 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1418 returned location comes from.
1420 This is a subroutine of linemap_resolve_location. */
1423 linemap_macro_loc_to_def_point (line_maps
*set
,
1424 location_t location
,
1425 const line_map_ordinary
**original_map
)
1427 linemap_assert (set
&& location
>= RESERVED_LOCATION_COUNT
);
1431 location_t caret_loc
= location
;
1432 if (IS_ADHOC_LOC (caret_loc
))
1433 caret_loc
= get_location_from_adhoc_loc (set
, caret_loc
);
1435 const line_map
*map
= linemap_lookup (set
, caret_loc
);
1436 if (!map
|| MAP_ORDINARY_P (map
))
1439 *original_map
= (const line_map_ordinary
*)map
;
1443 location
= linemap_macro_map_loc_to_def_point
1444 (linemap_check_macro (map
), caret_loc
);
1450 /* If LOCATION is the source location of a token that belongs to a
1451 macro replacement-list -- at a macro expansion point -- then return
1452 the location of the topmost expansion point of the macro. We say
1453 topmost because if we are in the context of a nested macro
1454 expansion, the function returns the source location of the first
1455 macro expansion that triggered the nested expansions.
1457 Otherwise, return LOCATION. SET is the set of maps location come
1458 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1459 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1460 location comes from.
1462 This is a subroutine of linemap_resolve_location. */
1465 linemap_macro_loc_to_exp_point (line_maps
*set
,
1466 location_t location
,
1467 const line_map_ordinary
**original_map
)
1469 struct line_map
*map
;
1471 if (IS_ADHOC_LOC (location
))
1472 location
= get_location_from_adhoc_loc (set
, location
);
1474 linemap_assert (set
&& location
>= RESERVED_LOCATION_COUNT
);
1478 map
= const_cast <line_map
*> (linemap_lookup (set
, location
));
1479 if (!linemap_macro_expansion_map_p (map
))
1481 location
= linemap_macro_map_loc_to_exp_point (linemap_check_macro (map
),
1486 *original_map
= linemap_check_ordinary (map
);
1490 /* Resolve a virtual location into either a spelling location, an
1491 expansion point location or a token argument replacement point
1492 location. Return the map that encodes the virtual location as well
1493 as the resolved location.
1495 If LOC is *NOT* the location of a token resulting from the
1496 expansion of a macro, then the parameter LRK (which stands for
1497 Location Resolution Kind) is ignored and the resulting location
1498 just equals the one given in argument.
1500 Now if LOC *IS* the location of a token resulting from the
1501 expansion of a macro, this is what happens.
1503 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1504 -------------------------------
1506 The virtual location is resolved to the first macro expansion point
1507 that led to this macro expansion.
1509 * If LRK is set to LRK_SPELLING_LOCATION
1510 -------------------------------------
1512 The virtual location is resolved to the locus where the token has
1513 been spelled in the source. This can follow through all the macro
1514 expansions that led to the token.
1516 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1517 --------------------------------------
1519 The virtual location is resolved to the locus of the token in the
1520 context of the macro definition.
1522 If LOC is the locus of a token that is an argument of a
1523 function-like macro [replacing a parameter in the replacement list
1524 of the macro] the virtual location is resolved to the locus of the
1525 parameter that is replaced, in the context of the definition of the
1528 If LOC is the locus of a token that is not an argument of a
1529 function-like macro, then the function behaves as if LRK was set to
1530 LRK_SPELLING_LOCATION.
1532 If MAP is not NULL, *MAP is set to the map encoding the
1533 returned location. Note that if the returned location wasn't originally
1534 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1535 resolves to a location reserved for the client code, like
1536 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1539 linemap_resolve_location (line_maps
*set
,
1541 enum location_resolution_kind lrk
,
1542 const line_map_ordinary
**map
)
1544 location_t locus
= loc
;
1545 if (IS_ADHOC_LOC (loc
))
1546 locus
= get_location_from_adhoc_loc (set
, loc
);
1548 if (locus
< RESERVED_LOCATION_COUNT
)
1550 /* A reserved location wasn't encoded in a map. Let's return a
1551 NULL map here, just like what linemap_ordinary_map_lookup
1560 case LRK_MACRO_EXPANSION_POINT
:
1561 loc
= linemap_macro_loc_to_exp_point (set
, loc
, map
);
1563 case LRK_SPELLING_LOCATION
:
1564 loc
= linemap_macro_loc_to_spelling_point (set
, loc
, map
);
1566 case LRK_MACRO_DEFINITION_LOCATION
:
1567 loc
= linemap_macro_loc_to_def_point (set
, loc
, map
);
1575 /* TRUE if LOCATION is a source code location of a token that is part of the
1576 definition of a macro, FALSE otherwise. */
1579 linemap_location_from_macro_definition_p (line_maps
*set
,
1582 if (IS_ADHOC_LOC (loc
))
1583 loc
= get_location_from_adhoc_loc (set
, loc
);
1585 if (!linemap_location_from_macro_expansion_p (set
, loc
))
1590 const struct line_map_macro
*map
1591 = linemap_check_macro (linemap_lookup (set
, loc
));
1594 = linemap_macro_map_loc_unwind_toward_spelling (set
, map
, loc
);
1595 if (linemap_location_from_macro_expansion_p (set
, s_loc
))
1600 = linemap_macro_map_loc_to_def_point (map
, loc
);
1601 return s_loc
== def_loc
;
1607 Suppose that LOC is the virtual location of a token T coming from
1608 the expansion of a macro M. This function then steps up to get the
1609 location L of the point where M got expanded. If L is a spelling
1610 location inside a macro expansion M', then this function returns
1611 the locus of the point where M' was expanded. Said otherwise, this
1612 function returns the location of T in the context that triggered
1615 *LOC_MAP must be set to the map of LOC. This function then sets it
1616 to the map of the returned location. */
1619 linemap_unwind_toward_expansion (line_maps
*set
,
1621 const struct line_map
**map
)
1623 location_t resolved_location
;
1624 const line_map_macro
*macro_map
= linemap_check_macro (*map
);
1625 const struct line_map
*resolved_map
;
1627 if (IS_ADHOC_LOC (loc
))
1628 loc
= get_location_from_adhoc_loc (set
, loc
);
1631 linemap_macro_map_loc_unwind_toward_spelling (set
, macro_map
, loc
);
1632 resolved_map
= linemap_lookup (set
, resolved_location
);
1634 if (!linemap_macro_expansion_map_p (resolved_map
))
1636 resolved_location
= linemap_macro_map_loc_to_exp_point (macro_map
, loc
);
1637 resolved_map
= linemap_lookup (set
, resolved_location
);
1640 *map
= resolved_map
;
1641 return resolved_location
;
1644 /* If LOC is the virtual location of a token coming from the expansion
1645 of a macro M and if its spelling location is reserved (e.g, a
1646 location for a built-in token), then this function unwinds (using
1647 linemap_unwind_toward_expansion) the location until a location that
1648 is not reserved and is not in a system header is reached. In other
1649 words, this unwinds the reserved location until a location that is
1650 in real source code is reached.
1652 Otherwise, if the spelling location for LOC is not reserved or if
1653 LOC doesn't come from the expansion of a macro, the function
1654 returns LOC as is and *MAP is not touched.
1656 *MAP is set to the map of the returned location if the later is
1657 different from LOC. */
1659 linemap_unwind_to_first_non_reserved_loc (line_maps
*set
,
1661 const struct line_map
**map
)
1663 location_t resolved_loc
;
1664 const struct line_map
*map0
= NULL
;
1665 const line_map_ordinary
*map1
= NULL
;
1667 if (IS_ADHOC_LOC (loc
))
1668 loc
= get_location_from_adhoc_loc (set
, loc
);
1670 map0
= linemap_lookup (set
, loc
);
1671 if (!linemap_macro_expansion_map_p (map0
))
1674 resolved_loc
= linemap_resolve_location (set
, loc
,
1675 LRK_SPELLING_LOCATION
,
1678 if (resolved_loc
>= RESERVED_LOCATION_COUNT
1679 && !LINEMAP_SYSP (map1
))
1682 while (linemap_macro_expansion_map_p (map0
)
1683 && (resolved_loc
< RESERVED_LOCATION_COUNT
1684 || LINEMAP_SYSP (map1
)))
1686 loc
= linemap_unwind_toward_expansion (set
, loc
, &map0
);
1687 resolved_loc
= linemap_resolve_location (set
, loc
,
1688 LRK_SPELLING_LOCATION
,
1697 /* Expand source code location LOC and return a user readable source
1698 code location. LOC must be a spelling (non-virtual) location. If
1699 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1700 location is returned. */
1703 linemap_expand_location (line_maps
*set
,
1704 const struct line_map
*map
,
1708 expanded_location xloc
;
1710 memset (&xloc
, 0, sizeof (xloc
));
1711 if (IS_ADHOC_LOC (loc
))
1713 xloc
.data
= get_data_from_adhoc_loc (set
, loc
);
1714 loc
= get_location_from_adhoc_loc (set
, loc
);
1717 if (loc
< RESERVED_LOCATION_COUNT
)
1718 /* The location for this token wasn't generated from a line map.
1719 It was probably a location for a builtin token, chosen by some
1720 client code. Let's not try to expand the location in that
1722 else if (map
== NULL
)
1723 /* We shouldn't be getting a NULL map with a location that is not
1724 reserved by the client code. */
1728 /* MAP must be an ordinary map and LOC must be non-virtual,
1729 encoded into this map, obviously; the accessors used on MAP
1730 below ensure it is ordinary. Let's just assert the
1731 non-virtualness of LOC here. */
1732 if (linemap_location_from_macro_expansion_p (set
, loc
))
1735 const line_map_ordinary
*ord_map
= linemap_check_ordinary (map
);
1737 xloc
.file
= LINEMAP_FILE (ord_map
);
1738 xloc
.line
= SOURCE_LINE (ord_map
, loc
);
1739 xloc
.column
= SOURCE_COLUMN (ord_map
, loc
);
1740 xloc
.sysp
= LINEMAP_SYSP (ord_map
) != 0;
1747 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1748 is NULL, use stderr. IS_MACRO is true if the caller wants to
1749 dump a macro map, false otherwise. */
1752 linemap_dump (FILE *stream
, class line_maps
*set
, unsigned ix
, bool is_macro
)
1754 const char *const lc_reasons_v
[LC_HWM
]
1755 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1757 const line_map
*map
;
1765 map
= LINEMAPS_ORDINARY_MAP_AT (set
, ix
);
1766 reason
= linemap_check_ordinary (map
)->reason
;
1770 map
= LINEMAPS_MACRO_MAP_AT (set
, ix
);
1771 reason
= LC_ENTER_MACRO
;
1774 fprintf (stream
, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1775 ix
, (void *) map
, map
->start_location
,
1776 reason
< LC_HWM
? lc_reasons_v
[reason
] : "???",
1778 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map
)))
1782 const line_map_ordinary
*ord_map
= linemap_check_ordinary (map
);
1783 const line_map_ordinary
*includer_map
1784 = linemap_included_from_linemap (set
, ord_map
);
1786 fprintf (stream
, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map
),
1787 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
));
1788 fprintf (stream
, "Included from: [%d] %s\n",
1789 includer_map
? int (includer_map
- set
->info_ordinary
.maps
) : -1,
1790 includer_map
? ORDINARY_MAP_FILE_NAME (includer_map
) : "None");
1794 const line_map_macro
*macro_map
= linemap_check_macro (map
);
1795 fprintf (stream
, "Macro: %s (%u tokens)\n",
1796 linemap_map_get_macro_name (macro_map
),
1797 MACRO_MAP_NUM_MACRO_TOKENS (macro_map
));
1800 fprintf (stream
, "\n");
1804 /* Dump debugging information about source location LOC into the file
1805 stream STREAM. SET is the line map set LOC comes from. */
1808 linemap_dump_location (line_maps
*set
,
1812 const line_map_ordinary
*map
;
1813 location_t location
;
1814 const char *path
= "", *from
= "";
1815 int l
= -1, c
= -1, s
= -1, e
= -1;
1817 if (IS_ADHOC_LOC (loc
))
1818 loc
= get_location_from_adhoc_loc (set
, loc
);
1824 linemap_resolve_location (set
, loc
, LRK_MACRO_DEFINITION_LOCATION
, &map
);
1827 /* Only reserved locations can be tolerated in this case. */
1828 linemap_assert (location
< RESERVED_LOCATION_COUNT
);
1831 path
= LINEMAP_FILE (map
);
1832 l
= SOURCE_LINE (map
, location
);
1833 c
= SOURCE_COLUMN (map
, location
);
1834 s
= LINEMAP_SYSP (map
) != 0;
1835 e
= location
!= loc
;
1840 const line_map_ordinary
*from_map
1841 = linemap_included_from_linemap (set
, map
);
1842 from
= from_map
? LINEMAP_FILE (from_map
) : "<NULL>";
1846 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1847 E: macro expansion?, LOC: original location, R: resolved location */
1848 fprintf (stream
, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1849 path
, from
, l
, c
, s
, (void*)map
, e
, loc
, location
);
1852 /* Return the highest location emitted for a given file for which
1853 there is a line map in SET. FILE_NAME is the file name to
1854 consider. If the function returns TRUE, *LOC is set to the highest
1855 location emitted for that file. */
1858 linemap_get_file_highest_location (line_maps
*set
,
1859 const char *file_name
,
1862 /* If the set is empty or no ordinary map has been created then
1863 there is no file to look for ... */
1864 if (set
== NULL
|| set
->info_ordinary
.used
== 0)
1867 /* Now look for the last ordinary map created for FILE_NAME. */
1869 for (i
= set
->info_ordinary
.used
- 1; i
>= 0; --i
)
1871 const char *fname
= set
->info_ordinary
.maps
[i
].to_file
;
1872 if (fname
&& !filename_cmp (fname
, file_name
))
1879 /* The highest location for a given map is either the starting
1880 location of the next map minus one, or -- if the map is the
1881 latest one -- the highest location of the set. */
1883 if (i
== (int) set
->info_ordinary
.used
- 1)
1884 result
= set
->highest_location
;
1886 result
= set
->info_ordinary
.maps
[i
+ 1].start_location
- 1;
1892 /* Compute and return statistics about the memory consumption of some
1893 parts of the line table SET. */
1896 linemap_get_statistics (line_maps
*set
,
1897 struct linemap_stats
*s
)
1899 long ordinary_maps_allocated_size
, ordinary_maps_used_size
,
1900 macro_maps_allocated_size
, macro_maps_used_size
,
1901 macro_maps_locations_size
= 0, duplicated_macro_maps_locations_size
= 0;
1903 const line_map_macro
*cur_map
;
1905 ordinary_maps_allocated_size
=
1906 LINEMAPS_ORDINARY_ALLOCATED (set
) * sizeof (struct line_map_ordinary
);
1908 ordinary_maps_used_size
=
1909 LINEMAPS_ORDINARY_USED (set
) * sizeof (struct line_map_ordinary
);
1911 macro_maps_allocated_size
=
1912 LINEMAPS_MACRO_ALLOCATED (set
) * sizeof (struct line_map_macro
);
1914 for (cur_map
= LINEMAPS_MACRO_MAPS (set
);
1915 cur_map
&& cur_map
<= LINEMAPS_LAST_MACRO_MAP (set
);
1920 linemap_assert (linemap_macro_expansion_map_p (cur_map
));
1922 macro_maps_locations_size
+=
1923 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map
) * sizeof (location_t
);
1925 for (i
= 0; i
< 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map
); i
+= 2)
1927 if (MACRO_MAP_LOCATIONS (cur_map
)[i
] ==
1928 MACRO_MAP_LOCATIONS (cur_map
)[i
+ 1])
1929 duplicated_macro_maps_locations_size
+=
1930 sizeof (location_t
);
1934 macro_maps_used_size
=
1935 LINEMAPS_MACRO_USED (set
) * sizeof (struct line_map_macro
);
1937 s
->num_ordinary_maps_allocated
= LINEMAPS_ORDINARY_ALLOCATED (set
);
1938 s
->num_ordinary_maps_used
= LINEMAPS_ORDINARY_USED (set
);
1939 s
->ordinary_maps_allocated_size
= ordinary_maps_allocated_size
;
1940 s
->ordinary_maps_used_size
= ordinary_maps_used_size
;
1941 s
->num_expanded_macros
= num_expanded_macros_counter
;
1942 s
->num_macro_tokens
= num_macro_tokens_counter
;
1943 s
->num_macro_maps_used
= LINEMAPS_MACRO_USED (set
);
1944 s
->macro_maps_allocated_size
= macro_maps_allocated_size
;
1945 s
->macro_maps_locations_size
= macro_maps_locations_size
;
1946 s
->macro_maps_used_size
= macro_maps_used_size
;
1947 s
->duplicated_macro_maps_locations_size
=
1948 duplicated_macro_maps_locations_size
;
1949 s
->adhoc_table_size
= (set
->location_adhoc_data_map
.allocated
1950 * sizeof (struct location_adhoc_data
));
1951 s
->adhoc_table_entries_used
= set
->location_adhoc_data_map
.curr_loc
;
1955 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1956 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1957 specifies how many macro maps to dump. */
1960 line_table_dump (FILE *stream
, class line_maps
*set
, unsigned int num_ordinary
,
1961 unsigned int num_macro
)
1971 fprintf (stream
, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set
));
1972 fprintf (stream
, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set
));
1973 fprintf (stream
, "Include stack depth: %d\n", set
->depth
);
1974 fprintf (stream
, "Highest location: %u\n", set
->highest_location
);
1978 fprintf (stream
, "\nOrdinary line maps\n");
1979 for (i
= 0; i
< num_ordinary
&& i
< LINEMAPS_ORDINARY_USED (set
); i
++)
1980 linemap_dump (stream
, set
, i
, false);
1981 fprintf (stream
, "\n");
1986 fprintf (stream
, "\nMacro line maps\n");
1987 for (i
= 0; i
< num_macro
&& i
< LINEMAPS_MACRO_USED (set
); i
++)
1988 linemap_dump (stream
, set
, i
, true);
1989 fprintf (stream
, "\n");
1993 /* class rich_location. */
1995 /* Construct a rich_location with location LOC as its initial range. */
1997 rich_location::rich_location (line_maps
*set
, location_t loc
,
1998 const range_label
*label
) :
2001 m_column_override (0),
2002 m_have_expanded_location (false),
2004 m_seen_impossible_fixit (false),
2005 m_fixits_cannot_be_auto_applied (false)
2007 add_range (loc
, SHOW_RANGE_WITH_CARET
, label
);
2010 /* The destructor for class rich_location. */
2012 rich_location::~rich_location ()
2014 for (unsigned int i
= 0; i
< m_fixit_hints
.count (); i
++)
2015 delete get_fixit_hint (i
);
2018 /* Get location IDX within this rich_location. */
2021 rich_location::get_loc (unsigned int idx
) const
2023 const location_range
*locrange
= get_range (idx
);
2024 return locrange
->m_loc
;
2027 /* Get range IDX within this rich_location. */
2029 const location_range
*
2030 rich_location::get_range (unsigned int idx
) const
2032 return &m_ranges
[idx
];
2035 /* Mutable access to range IDX within this rich_location. */
2038 rich_location::get_range (unsigned int idx
)
2040 return &m_ranges
[idx
];
2043 /* Expand location IDX within this rich_location. */
2044 /* Get an expanded_location for this rich_location's primary
2048 rich_location::get_expanded_location (unsigned int idx
)
2052 /* Cache the expansion of the primary location. */
2053 if (!m_have_expanded_location
)
2056 = linemap_client_expand_location_to_spelling_point
2057 (get_loc (0), LOCATION_ASPECT_CARET
);
2058 if (m_column_override
)
2059 m_expanded_location
.column
= m_column_override
;
2060 m_have_expanded_location
= true;
2063 return m_expanded_location
;
2066 return linemap_client_expand_location_to_spelling_point
2067 (get_loc (idx
), LOCATION_ASPECT_CARET
);
2070 /* Set the column of the primary location, with 0 meaning
2071 "don't override it". */
2074 rich_location::override_column (int column
)
2076 m_column_override
= column
;
2077 m_have_expanded_location
= false;
2080 /* Add the given range. */
2083 rich_location::add_range (location_t loc
,
2084 enum range_display_kind range_display_kind
,
2085 const range_label
*label
)
2087 location_range range
;
2089 range
.m_range_display_kind
= range_display_kind
;
2090 range
.m_label
= label
;
2091 m_ranges
.push (range
);
2094 /* Add or overwrite the location given by IDX, setting its location to LOC,
2095 and setting its m_range_display_kind to RANGE_DISPLAY_KIND.
2097 It must either overwrite an existing location, or add one *exactly* on
2098 the end of the array.
2100 This is primarily for use by gcc when implementing diagnostic format
2102 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2103 (which writes the source location of a tree back into location 0 of
2104 the rich_location), and
2105 - the "%C" and "%L" format codes in the Fortran frontend. */
2108 rich_location::set_range (unsigned int idx
, location_t loc
,
2109 enum range_display_kind range_display_kind
)
2111 /* We can either overwrite an existing range, or add one exactly
2112 on the end of the array. */
2113 linemap_assert (idx
<= m_ranges
.count ());
2115 if (idx
== m_ranges
.count ())
2116 add_range (loc
, range_display_kind
);
2119 location_range
*locrange
= get_range (idx
);
2120 locrange
->m_loc
= loc
;
2121 locrange
->m_range_display_kind
= range_display_kind
;
2125 /* Mark any cached value here as dirty. */
2126 m_have_expanded_location
= false;
2129 /* Methods for adding insertion fix-it hints. */
2131 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2132 immediately before the primary range's start location. */
2135 rich_location::add_fixit_insert_before (const char *new_content
)
2137 add_fixit_insert_before (get_loc (), new_content
);
2140 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2141 immediately before the start of WHERE. */
2144 rich_location::add_fixit_insert_before (location_t where
,
2145 const char *new_content
)
2147 location_t start
= get_range_from_loc (m_line_table
, where
).m_start
;
2148 maybe_add_fixit (start
, start
, new_content
);
2151 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2152 immediately after the primary range's end-point. */
2155 rich_location::add_fixit_insert_after (const char *new_content
)
2157 add_fixit_insert_after (get_loc (), new_content
);
2160 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2161 immediately after the end-point of WHERE. */
2164 rich_location::add_fixit_insert_after (location_t where
,
2165 const char *new_content
)
2167 location_t finish
= get_range_from_loc (m_line_table
, where
).m_finish
;
2169 = linemap_position_for_loc_and_offset (m_line_table
, finish
, 1);
2171 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2173 if (next_loc
== finish
)
2175 stop_supporting_fixits ();
2179 maybe_add_fixit (next_loc
, next_loc
, new_content
);
2182 /* Methods for adding removal fix-it hints. */
2184 /* Add a fixit-hint, suggesting removal of the content covered
2188 rich_location::add_fixit_remove ()
2190 add_fixit_remove (get_loc ());
2193 /* Add a fixit-hint, suggesting removal of the content between
2194 the start and finish of WHERE. */
2197 rich_location::add_fixit_remove (location_t where
)
2199 source_range range
= get_range_from_loc (m_line_table
, where
);
2200 add_fixit_remove (range
);
2203 /* Add a fixit-hint, suggesting removal of the content at
2207 rich_location::add_fixit_remove (source_range src_range
)
2209 add_fixit_replace (src_range
, "");
2212 /* Add a fixit-hint, suggesting replacement of the content covered
2213 by range 0 with NEW_CONTENT. */
2216 rich_location::add_fixit_replace (const char *new_content
)
2218 add_fixit_replace (get_loc (), new_content
);
2221 /* Methods for adding "replace" fix-it hints. */
2223 /* Add a fixit-hint, suggesting replacement of the content between
2224 the start and finish of WHERE with NEW_CONTENT. */
2227 rich_location::add_fixit_replace (location_t where
,
2228 const char *new_content
)
2230 source_range range
= get_range_from_loc (m_line_table
, where
);
2231 add_fixit_replace (range
, new_content
);
2234 /* Add a fixit-hint, suggesting replacement of the content at
2235 SRC_RANGE with NEW_CONTENT. */
2238 rich_location::add_fixit_replace (source_range src_range
,
2239 const char *new_content
)
2241 location_t start
= get_pure_location (m_line_table
, src_range
.m_start
);
2242 location_t finish
= get_pure_location (m_line_table
, src_range
.m_finish
);
2244 /* Fix-it hints use half-closed ranges, so attempt to offset the endpoint. */
2246 = linemap_position_for_loc_and_offset (m_line_table
, finish
, 1);
2247 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2249 if (next_loc
== finish
)
2251 stop_supporting_fixits ();
2256 maybe_add_fixit (start
, finish
, new_content
);
2259 /* Get the last fix-it hint within this rich_location, or NULL if none. */
2262 rich_location::get_last_fixit_hint () const
2264 if (m_fixit_hints
.count () > 0)
2265 return get_fixit_hint (m_fixit_hints
.count () - 1);
2270 /* If WHERE is an "awkward" location, then mark this rich_location as not
2271 supporting fixits, purging any thay were already added, and return true.
2273 Otherwise (the common case), return false. */
2276 rich_location::reject_impossible_fixit (location_t where
)
2278 /* Fix-its within a rich_location should either all be suggested, or
2279 none of them should be suggested.
2280 Once we've rejected a fixit, we reject any more, even those
2281 with reasonable locations. */
2282 if (m_seen_impossible_fixit
)
2285 if (where
<= LINE_MAP_MAX_LOCATION_WITH_COLS
)
2286 /* WHERE is a reasonable location for a fix-it; don't reject it. */
2289 /* Otherwise we have an attempt to add a fix-it with an "awkward"
2290 location: either one that we can't obtain column information
2291 for (within an ordinary map), or one within a macro expansion. */
2292 stop_supporting_fixits ();
2296 /* Mark this rich_location as not supporting fixits, purging any that were
2300 rich_location::stop_supporting_fixits ()
2302 m_seen_impossible_fixit
= true;
2304 /* Purge the rich_location of any fix-its that were already added. */
2305 for (unsigned int i
= 0; i
< m_fixit_hints
.count (); i
++)
2306 delete get_fixit_hint (i
);
2307 m_fixit_hints
.truncate (0);
2310 /* Add HINT to the fix-it hints in this rich_location,
2311 consolidating into the prior fixit if possible. */
2314 rich_location::maybe_add_fixit (location_t start
,
2315 location_t next_loc
,
2316 const char *new_content
)
2318 if (reject_impossible_fixit (start
))
2320 if (reject_impossible_fixit (next_loc
))
2323 /* Only allow fix-it hints that affect a single line in one file.
2324 Compare the end-points. */
2325 expanded_location exploc_start
2326 = linemap_client_expand_location_to_spelling_point (start
,
2327 LOCATION_ASPECT_START
);
2328 expanded_location exploc_next_loc
2329 = linemap_client_expand_location_to_spelling_point (next_loc
,
2330 LOCATION_ASPECT_START
);
2331 /* They must be within the same file... */
2332 if (exploc_start
.file
!= exploc_next_loc
.file
)
2334 stop_supporting_fixits ();
2337 /* ...and on the same line. */
2338 if (exploc_start
.line
!= exploc_next_loc
.line
)
2340 stop_supporting_fixits ();
2343 /* The columns must be in the correct order. This can fail if the
2344 endpoints straddle the boundary for which the linemap can represent
2345 columns (PR c/82050). */
2346 if (exploc_start
.column
> exploc_next_loc
.column
)
2348 stop_supporting_fixits ();
2352 const char *newline
= strchr (new_content
, '\n');
2355 /* For now, we can only support insertion of whole lines
2356 i.e. starts at start of line, and the newline is at the end of
2357 the insertion point. */
2359 /* It must be an insertion, not a replacement/deletion. */
2360 if (start
!= next_loc
)
2362 stop_supporting_fixits ();
2366 /* The insertion must be at the start of a line. */
2367 if (exploc_start
.column
!= 1)
2369 stop_supporting_fixits ();
2373 /* The newline must be at end of NEW_CONTENT.
2374 We could eventually split up fix-its at newlines if we wanted
2375 to allow more generality (e.g. to allow adding multiple lines
2376 with one add_fixit call. */
2377 if (newline
[1] != '\0')
2379 stop_supporting_fixits ();
2384 /* Consolidate neighboring fixits.
2385 Don't consolidate into newline-insertion fixits. */
2386 fixit_hint
*prev
= get_last_fixit_hint ();
2387 if (prev
&& !prev
->ends_with_newline_p ())
2388 if (prev
->maybe_append (start
, next_loc
, new_content
))
2391 m_fixit_hints
.push (new fixit_hint (start
, next_loc
, new_content
));
2394 /* class fixit_hint. */
2396 fixit_hint::fixit_hint (location_t start
,
2397 location_t next_loc
,
2398 const char *new_content
)
2400 m_next_loc (next_loc
),
2401 m_bytes (xstrdup (new_content
)),
2402 m_len (strlen (new_content
))
2406 /* Does this fix-it hint affect the given line? */
2409 fixit_hint::affects_line_p (const char *file
, int line
) const
2411 expanded_location exploc_start
2412 = linemap_client_expand_location_to_spelling_point (m_start
,
2413 LOCATION_ASPECT_START
);
2414 if (file
!= exploc_start
.file
)
2416 if (line
< exploc_start
.line
)
2418 expanded_location exploc_next_loc
2419 = linemap_client_expand_location_to_spelling_point (m_next_loc
,
2420 LOCATION_ASPECT_START
);
2421 if (file
!= exploc_next_loc
.file
)
2423 if (line
> exploc_next_loc
.line
)
2428 /* Method for consolidating fix-it hints, for use by
2429 rich_location::maybe_add_fixit.
2430 If possible, merge a pending fix-it hint with the given params
2431 into this one and return true.
2432 Otherwise return false. */
2435 fixit_hint::maybe_append (location_t start
,
2436 location_t next_loc
,
2437 const char *new_content
)
2439 /* For consolidation to be possible, START must be at this hint's
2441 if (start
!= m_next_loc
)
2444 /* If so, we have neighboring replacements; merge them. */
2445 m_next_loc
= next_loc
;
2446 size_t extra_len
= strlen (new_content
);
2447 m_bytes
= (char *)xrealloc (m_bytes
, m_len
+ extra_len
+ 1);
2448 memcpy (m_bytes
+ m_len
, new_content
, extra_len
);
2450 m_bytes
[m_len
] = '\0';
2454 /* Return true iff this hint's content ends with a newline. */
2457 fixit_hint::ends_with_newline_p () const
2461 return m_bytes
[m_len
- 1] == '\n';