rtl.h (LABEL_REF_P): New #define.
[official-gcc.git] / libcpp / line-map.c
blob31439f4aba3d56ce491d470f1ba1a95ecf4f4786
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
7 later version.
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! */
22 #include "config.h"
23 #include "system.h"
24 #include "line-map.h"
25 #include "cpplib.h"
26 #include "internal.h"
27 #include "hashtab.h"
29 static void trace_include (const struct line_maps *, const line_map_ordinary *);
30 static const line_map_ordinary * linemap_ordinary_map_lookup (struct line_maps *,
31 location_t);
32 static const line_map_macro* linemap_macro_map_lookup (struct line_maps *,
33 location_t);
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 (struct line_maps *, location_t, const line_map_ordinary **);
40 static location_t linemap_macro_loc_to_def_point (line_maps *,
41 location_t,
42 const line_map_ordinary **);
43 static location_t linemap_macro_loc_to_exp_point (line_maps *,
44 location_t,
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. */
62 static hashval_t
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
70 + (size_t) lb->data);
73 /* Compare function for location_adhoc_data hashtable. */
75 static int
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. */
90 static int
91 location_adhoc_data_update (void **slot, void *data)
93 *((char **) slot)
94 = (char *) ((uintptr_t) *((char **) slot) + *((ptrdiff_t *) data));
95 return 1;
98 /* Rebuild the hash table from the location adhoc data. */
100 void
101 rebuild_location_adhoc_htab (struct line_maps *set)
103 unsigned i;
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. */
115 static bool
116 can_be_stored_compactly_p (struct line_maps *set,
117 location_t locus,
118 source_range src_range,
119 void *data)
121 /* If there's an ad-hoc pointer, we can't store it directly in the
122 location_t, we need the lookaside. */
123 if (data)
124 return false;
126 /* We only store ranges that begin at the locus and that are sufficiently
127 "sane". */
128 if (src_range.m_start != locus)
129 return false;
131 if (src_range.m_finish < src_range.m_start)
132 return false;
134 if (src_range.m_start < RESERVED_LOCATION_COUNT)
135 return false;
137 if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
138 return false;
140 /* All 3 locations must be within ordinary maps, typically, the same
141 ordinary map. */
142 location_t lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
143 if (locus >= lowest_macro_loc)
144 return false;
145 if (src_range.m_start >= lowest_macro_loc)
146 return false;
147 if (src_range.m_finish >= lowest_macro_loc)
148 return false;
150 /* Passed all tests. */
151 return true;
154 /* Combine LOCUS and DATA to a combined adhoc loc. */
156 location_t
157 get_combined_adhoc_loc (struct line_maps *set,
158 location_t locus,
159 source_range src_range,
160 void *data)
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)
168 return 0;
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++;
190 return packed;
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
198 && !data)
199 return locus;
201 if (!data)
202 set->num_unoptimized_ranges++;
204 lb.locus = locus;
205 lb.src_range = src_range;
206 lb.data = data;
207 slot = (struct location_adhoc_data **)
208 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
209 if (*slot == NULL)
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;
215 ptrdiff_t offset;
216 /* Cast away extern "C" from the type of xrealloc. */
217 line_map_realloc reallocator = (set->reallocator
218 ? set->reallocator
219 : (line_map_realloc) xrealloc);
221 if (set->location_adhoc_data_map.allocated == 0)
222 set->location_adhoc_data_map.allocated = 128;
223 else
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++]
237 = lb;
239 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
242 /* Return the data for the adhoc loc. */
244 void *
245 get_data_from_adhoc_loc (const struct 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. */
253 location_t
254 get_location_from_adhoc_loc (const struct 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. */
262 static source_range
263 get_range_from_adhoc_loc (const struct 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. */
272 source_range
273 get_range_from_loc (struct line_maps *set,
274 location_t loc)
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);
286 source_range result;
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);
290 return result;
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. */
299 bool
300 pure_location_p (line_maps *set, location_t loc)
302 if (IS_ADHOC_LOC (loc))
303 return false;
305 const line_map *map = linemap_lookup (set, loc);
306 if (map == NULL)
307 return true;
308 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
310 if (loc & ((1U << ordmap->m_range_bits) - 1))
311 return false;
313 return true;
316 /* Given location LOC within SET, strip away any packed range information
317 or ad-hoc information. */
319 location_t
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))
326 return loc;
328 if (loc < RESERVED_LOCATION_COUNT)
329 return loc;
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. */
339 void
340 linemap_init (struct 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 (struct line_maps));
346 #else
347 new (set) line_maps();
348 #endif
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. */
369 void
370 linemap_check_files_exited (struct 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);
375 ! MAIN_FILE_P (map);
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 (struct 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;
401 void *buffer;
402 if (macro_p)
404 size_of_a_map = sizeof (line_map_macro);
405 buffer = set->info_macro.maps;
407 else
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
415 ggc-page.c one.
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. */
420 size_t alloc_size
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);
430 if (macro_p)
431 set->info_macro.maps = (line_map_macro *)buffer;
432 else
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;
443 return result;
446 /* Add a mapping of logical source line to physical source file and
447 line number.
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 (struct 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)));
474 else
475 start_location = set->highest_location + 1;
477 linemap_assert (!LINEMAPS_ORDINARY_USED (set)
478 || (start_location
479 >= MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set))));
481 /* When we enter the file for the first time reason cannot be
482 LC_RENAME. */
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))
488 && to_file == NULL)
490 set->depth--;
491 return NULL;
494 linemap_assert (reason != LC_ENTER_MACRO);
496 if (start_location >= LINE_MAP_MAX_LOCATION)
497 /* We ran out of line map space. */
498 start_location = 0;
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)
505 to_file = "<stdin>";
507 if (reason == LC_RENAME_VERBATIM)
508 reason = LC_RENAME;
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. */
525 if (to_file == NULL)
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);
531 else
532 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from),
533 to_file) == 0);
536 map->sysp = sysp;
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
549 pure_location_p. */
550 linemap_assert (pure_location_p (set, start_location));
552 if (reason == LC_ENTER)
554 if (set->depth == 0)
555 map->included_from = 0;
556 else
557 /* The location of the end of the just-closed map. */
558 map->included_from
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);
562 set->depth++;
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)
570 set->depth--;
571 map->included_from = linemap_included_from (from);
574 return map;
577 /* Returns TRUE if the line table set tracks token locations across
578 macro expansion, FALSE otherwise. */
580 bool
581 linemap_tracks_macro_expansion_locs_p (struct 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 (struct 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. */
618 return NULL;
620 line_map_macro *map = linemap_check_macro (new_linemap (set, start_location));
622 map->macro = macro_node;
623 map->n_tokens = num_tokens;
624 map->macro_locations
625 = (location_t*) set->reallocator (NULL,
626 2 * num_tokens
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;
634 return map;
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
643 linemap_enter_macro.
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
647 at number 0.
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
658 replaces. */
660 location_t
661 linemap_add_macro_token (const line_map_macro *map,
662 unsigned int token_no,
663 location_t orig_loc,
664 location_t orig_parm_replacement_loc)
666 location_t result;
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;
675 return result;
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). */
684 location_t
685 linemap_line_start (struct 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;
690 location_t r;
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;
698 if (line_delta < 0
699 || (line_delta > 10
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)))
707 add_map = true;
708 else
709 max_column_hint = set->max_column_hint;
710 if (add_map)
712 int column_bits;
713 int range_bits;
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). */
720 max_column_hint = 0;
721 column_bits = 0;
722 range_bits = 0;
723 if (highest >= LINE_MAP_MAX_LOCATION)
724 return 0;
726 else
728 column_bits = 7;
729 if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
730 range_bits = set->default_range_bits;
731 else
732 range_bits = 0;
733 while (max_column_hint >= (1U << column_bits))
734 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. */
740 if (line_delta < 0
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))
746 >= (((uint64_t) 1)
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),
754 to_line)));
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))
759 << column_bits));
761 else
762 r = set->highest_line + (line_delta << map->m_column_and_range_bits);
764 /* Locations of ordinary tokens are always lower than locations of
765 macro tokens. */
766 if (r >= LINE_MAP_MAX_LOCATION)
767 return 0;
769 set->highest_line = r;
770 if (r > set->highest_location)
771 set->highest_location = r;
772 set->max_column_hint = max_column_hint;
774 /* At this point, we expect one of:
775 (a) the normal case: a "pure" location with 0 range bits, or
776 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
777 columns anymore (or ranges), or
778 (c) we're in a region with a column hint exceeding
779 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
780 with column_bits == 0. */
781 linemap_assert (pure_location_p (set, r)
782 || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
783 || map->m_column_and_range_bits == 0);
784 linemap_assert (SOURCE_LINE (map, r) == to_line);
785 return r;
788 /* Encode and return a location_t from a column number. The
789 source line considered is the last source line used to call
790 linemap_line_start, i.e, the last source line which a location was
791 encoded from. */
793 location_t
794 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
796 location_t r = set->highest_line;
798 linemap_assert
799 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
801 if (to_column >= set->max_column_hint)
803 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
804 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
806 /* Running low on location_ts - disable column numbers. */
807 return r;
809 else
811 /* Otherwise, attempt to start a new line that can hold TO_COLUMN,
812 with some space to spare. This may or may not lead to a new
813 linemap being created. */
814 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
815 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
816 map = LINEMAPS_LAST_ORDINARY_MAP (set);
817 if (map->m_column_and_range_bits == 0)
819 /* ...then the linemap has column-tracking disabled,
820 presumably due to exceeding either
821 LINE_MAP_MAX_LOCATION_WITH_COLS (overall) or
822 LINE_MAP_MAX_COLUMN_NUMBER (within this line).
823 Return the start of the linemap, which encodes column 0, for
824 the whole line. */
825 return r;
829 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
830 r = r + (to_column << map->m_range_bits);
831 if (r >= set->highest_location)
832 set->highest_location = r;
833 return r;
836 /* Encode and return a source location from a given line and
837 column. */
839 location_t
840 linemap_position_for_line_and_column (line_maps *set,
841 const line_map_ordinary *ord_map,
842 linenum_type line,
843 unsigned column)
845 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
847 location_t r = MAP_START_LOCATION (ord_map);
848 r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
849 << ord_map->m_column_and_range_bits);
850 if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
851 r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
852 << ord_map->m_range_bits);
853 location_t upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
854 if (r >= upper_limit)
855 r = upper_limit - 1;
856 if (r > set->highest_location)
857 set->highest_location = r;
858 return r;
861 /* Encode and return a location_t starting from location LOC and
862 shifting it by COLUMN_OFFSET columns. This function does not support
863 virtual locations. */
865 location_t
866 linemap_position_for_loc_and_offset (struct line_maps *set,
867 location_t loc,
868 unsigned int column_offset)
870 const line_map_ordinary * map = NULL;
872 if (IS_ADHOC_LOC (loc))
873 loc = get_location_from_adhoc_loc (set, loc);
875 /* This function does not support virtual locations yet. */
876 if (linemap_location_from_macro_expansion_p (set, loc))
877 return loc;
879 if (column_offset == 0
880 /* Adding an offset to a reserved location (like
881 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
882 sense. So let's leave the location intact in that case. */
883 || loc < RESERVED_LOCATION_COUNT)
884 return loc;
886 /* We find the real location and shift it. */
887 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
888 /* The new location (loc + offset) should be higher than the first
889 location encoded by MAP. This can fail if the line information
890 is messed up because of line directives (see PR66415). */
891 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
892 return loc;
894 linenum_type line = SOURCE_LINE (map, loc);
895 unsigned int column = SOURCE_COLUMN (map, loc);
897 /* If MAP is not the last line map of its set, then the new location
898 (loc + offset) should be less than the first location encoded by
899 the next line map of the set. Otherwise, we try to encode the
900 location in the next map. */
901 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
902 && (loc + (column_offset << map->m_range_bits)
903 >= MAP_START_LOCATION (&map[1])))
905 map = &map[1];
906 /* If the next map starts in a higher line, we cannot encode the
907 location there. */
908 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
909 return loc;
912 column += column_offset;
914 /* Bail out if the column is not representable within the existing
915 linemap. */
916 if (column >= (1u << (map->m_column_and_range_bits - map->m_range_bits)))
917 return loc;
919 location_t r =
920 linemap_position_for_line_and_column (set, map, line, column);
921 if (linemap_assert_fails (r <= set->highest_location)
922 || linemap_assert_fails (map == linemap_lookup (set, r)))
923 return loc;
925 return r;
928 /* Given a virtual source location yielded by a map (either an
929 ordinary or a macro map), returns that map. */
931 const struct line_map*
932 linemap_lookup (struct line_maps *set, location_t line)
934 if (IS_ADHOC_LOC (line))
935 line = get_location_from_adhoc_loc (set, line);
936 if (linemap_location_from_macro_expansion_p (set, line))
937 return linemap_macro_map_lookup (set, line);
938 return linemap_ordinary_map_lookup (set, line);
941 /* Given a source location yielded by an ordinary map, returns that
942 map. Since the set is built chronologically, the logical lines are
943 monotonic increasing, and so the list is sorted and we can use a
944 binary search. */
946 static const line_map_ordinary *
947 linemap_ordinary_map_lookup (struct line_maps *set, location_t line)
949 unsigned int md, mn, mx;
950 const line_map_ordinary *cached, *result;
952 if (IS_ADHOC_LOC (line))
953 line = get_location_from_adhoc_loc (set, line);
955 if (set == NULL || line < RESERVED_LOCATION_COUNT)
956 return NULL;
958 mn = LINEMAPS_ORDINARY_CACHE (set);
959 mx = LINEMAPS_ORDINARY_USED (set);
961 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
962 /* We should get a segfault if no line_maps have been added yet. */
963 if (line >= MAP_START_LOCATION (cached))
965 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
966 return cached;
968 else
970 mx = mn;
971 mn = 0;
974 while (mx - mn > 1)
976 md = (mn + mx) / 2;
977 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
978 mx = md;
979 else
980 mn = md;
983 LINEMAPS_ORDINARY_CACHE (set) = mn;
984 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
985 linemap_assert (line >= MAP_START_LOCATION (result));
986 return result;
989 /* Given a source location yielded by a macro map, returns that map.
990 Since the set is built chronologically, the logical lines are
991 monotonic decreasing, and so the list is sorted and we can use a
992 binary search. */
994 static const line_map_macro *
995 linemap_macro_map_lookup (struct line_maps *set, location_t line)
997 unsigned int md, mn, mx;
998 const struct line_map_macro *cached, *result;
1000 if (IS_ADHOC_LOC (line))
1001 line = get_location_from_adhoc_loc (set, line);
1003 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
1005 if (set == NULL)
1006 return NULL;
1008 mn = LINEMAPS_MACRO_CACHE (set);
1009 mx = LINEMAPS_MACRO_USED (set);
1010 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
1012 if (line >= MAP_START_LOCATION (cached))
1014 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
1015 return cached;
1016 mx = mn - 1;
1017 mn = 0;
1020 while (mn < mx)
1022 md = (mx + mn) / 2;
1023 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1024 mn = md + 1;
1025 else
1026 mx = md;
1029 LINEMAPS_MACRO_CACHE (set) = mx;
1030 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
1031 linemap_assert (MAP_START_LOCATION (result) <= line);
1033 return result;
1036 /* Return TRUE if MAP encodes locations coming from a macro
1037 replacement-list at macro expansion point. */
1039 bool
1040 linemap_macro_expansion_map_p (const struct line_map *map)
1042 return map && !MAP_ORDINARY_P (map);
1045 /* If LOCATION is the locus of a token in a replacement-list of a
1046 macro expansion return the location of the macro expansion point.
1048 Read the comments of struct line_map and struct line_map_macro in
1049 line-map.h to understand what a macro expansion point is. */
1051 static location_t
1052 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1053 location_t location ATTRIBUTE_UNUSED)
1055 linemap_assert (linemap_macro_expansion_map_p (map)
1056 && location >= MAP_START_LOCATION (map));
1058 /* Make sure LOCATION is correct. */
1059 linemap_assert ((location - MAP_START_LOCATION (map))
1060 < MACRO_MAP_NUM_MACRO_TOKENS (map));
1062 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1065 /* LOCATION is the source location of a token that belongs to a macro
1066 replacement-list as part of the macro expansion denoted by MAP.
1068 Return the location of the token at the definition point of the
1069 macro. */
1071 static location_t
1072 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1073 location_t location)
1075 unsigned token_no;
1077 linemap_assert (linemap_macro_expansion_map_p (map)
1078 && location >= MAP_START_LOCATION (map));
1079 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1081 token_no = location - MAP_START_LOCATION (map);
1082 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1084 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1086 return location;
1089 /* If LOCATION is the locus of a token that is an argument of a
1090 function-like macro M and appears in the expansion of M, return the
1091 locus of that argument in the context of the caller of M.
1093 In other words, this returns the xI location presented in the
1094 comments of line_map_macro above. */
1095 location_t
1096 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1097 const line_map_macro* map,
1098 location_t location)
1100 unsigned token_no;
1102 if (IS_ADHOC_LOC (location))
1103 location = get_location_from_adhoc_loc (set, location);
1105 linemap_assert (linemap_macro_expansion_map_p (map)
1106 && location >= MAP_START_LOCATION (map));
1107 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1108 linemap_assert (!IS_ADHOC_LOC (location));
1110 token_no = location - MAP_START_LOCATION (map);
1111 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1113 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1115 return location;
1118 /* Return the source line number corresponding to source location
1119 LOCATION. SET is the line map set LOCATION comes from. If
1120 LOCATION is the source location of token that is part of the
1121 replacement-list of a macro expansion return the line number of the
1122 macro expansion point. */
1125 linemap_get_expansion_line (struct line_maps *set,
1126 location_t location)
1128 const line_map_ordinary *map = NULL;
1130 if (IS_ADHOC_LOC (location))
1131 location = get_location_from_adhoc_loc (set, location);
1133 if (location < RESERVED_LOCATION_COUNT)
1134 return 0;
1136 location =
1137 linemap_macro_loc_to_exp_point (set, location, &map);
1139 return SOURCE_LINE (map, location);
1142 /* Return the path of the file corresponding to source code location
1143 LOCATION.
1145 If LOCATION is the source location of token that is part of the
1146 replacement-list of a macro expansion return the file path of the
1147 macro expansion point.
1149 SET is the line map set LOCATION comes from. */
1151 const char*
1152 linemap_get_expansion_filename (struct line_maps *set,
1153 location_t location)
1155 const struct line_map_ordinary *map = NULL;
1157 if (IS_ADHOC_LOC (location))
1158 location = get_location_from_adhoc_loc (set, location);
1160 if (location < RESERVED_LOCATION_COUNT)
1161 return NULL;
1163 location =
1164 linemap_macro_loc_to_exp_point (set, location, &map);
1166 return LINEMAP_FILE (map);
1169 /* Return the name of the macro associated to MACRO_MAP. */
1171 const char*
1172 linemap_map_get_macro_name (const line_map_macro *macro_map)
1174 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1175 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1178 /* Return a positive value if LOCATION is the locus of a token that is
1179 located in a system header, O otherwise. It returns 1 if LOCATION
1180 is the locus of a token that is located in a system header, and 2
1181 if LOCATION is the locus of a token located in a C system header
1182 that therefore needs to be extern "C" protected in C++.
1184 Note that this function returns 1 if LOCATION belongs to a token
1185 that is part of a macro replacement-list defined in a system
1186 header, but expanded in a non-system file. */
1189 linemap_location_in_system_header_p (struct line_maps *set,
1190 location_t location)
1192 const struct line_map *map = NULL;
1194 if (IS_ADHOC_LOC (location))
1195 location = get_location_from_adhoc_loc (set, location);
1197 if (location < RESERVED_LOCATION_COUNT)
1198 return false;
1200 /* Let's look at where the token for LOCATION comes from. */
1201 while (true)
1203 map = linemap_lookup (set, location);
1204 if (map != NULL)
1206 if (!linemap_macro_expansion_map_p (map))
1207 /* It's a normal token. */
1208 return LINEMAP_SYSP (linemap_check_ordinary (map));
1209 else
1211 const line_map_macro *macro_map = linemap_check_macro (map);
1213 /* It's a token resulting from a macro expansion. */
1214 location_t loc =
1215 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
1216 if (loc < RESERVED_LOCATION_COUNT)
1217 /* This token might come from a built-in macro. Let's
1218 look at where that macro got expanded. */
1219 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1220 else
1221 location = loc;
1224 else
1225 break;
1227 return false;
1230 /* Return TRUE if LOCATION is a source code location of a token that is part of
1231 a macro expansion, FALSE otherwise. */
1233 bool
1234 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1235 location_t location)
1237 if (IS_ADHOC_LOC (location))
1238 location = get_location_from_adhoc_loc (set, location);
1240 return IS_MACRO_LOC (location);
1243 /* Given two virtual locations *LOC0 and *LOC1, return the first
1244 common macro map in their macro expansion histories. Return NULL
1245 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1246 virtual location of the token inside the resulting macro. */
1248 static const struct line_map*
1249 first_map_in_common_1 (struct line_maps *set,
1250 location_t *loc0,
1251 location_t *loc1)
1253 location_t l0 = *loc0, l1 = *loc1;
1254 const struct line_map *map0 = linemap_lookup (set, l0);
1255 if (IS_ADHOC_LOC (l0))
1256 l0 = get_location_from_adhoc_loc (set, l0);
1258 const struct line_map *map1 = linemap_lookup (set, l1);
1259 if (IS_ADHOC_LOC (l1))
1260 l1 = get_location_from_adhoc_loc (set, l1);
1262 while (linemap_macro_expansion_map_p (map0)
1263 && linemap_macro_expansion_map_p (map1)
1264 && (map0 != map1))
1266 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1268 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1269 l0);
1270 map0 = linemap_lookup (set, l0);
1272 else
1274 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1275 l1);
1276 map1 = linemap_lookup (set, l1);
1280 if (map0 == map1)
1282 *loc0 = l0;
1283 *loc1 = l1;
1284 return map0;
1286 return NULL;
1289 /* Given two virtual locations LOC0 and LOC1, return the first common
1290 macro map in their macro expansion histories. Return NULL if no
1291 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1292 virtual location of the token inside the resulting macro, upon
1293 return of a non-NULL result. */
1295 static const struct line_map*
1296 first_map_in_common (struct line_maps *set,
1297 location_t loc0,
1298 location_t loc1,
1299 location_t *res_loc0,
1300 location_t *res_loc1)
1302 *res_loc0 = loc0;
1303 *res_loc1 = loc1;
1305 return first_map_in_common_1 (set, res_loc0, res_loc1);
1308 /* Return a positive value if PRE denotes the location of a token that
1309 comes before the token of POST, 0 if PRE denotes the location of
1310 the same token as the token for POST, and a negative value
1311 otherwise. */
1314 linemap_compare_locations (struct line_maps *set,
1315 location_t pre,
1316 location_t post)
1318 bool pre_virtual_p, post_virtual_p;
1319 location_t l0 = pre, l1 = post;
1321 if (IS_ADHOC_LOC (l0))
1322 l0 = get_location_from_adhoc_loc (set, l0);
1323 if (IS_ADHOC_LOC (l1))
1324 l1 = get_location_from_adhoc_loc (set, l1);
1326 if (l0 == l1)
1327 return 0;
1329 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1330 l0 = linemap_resolve_location (set, l0,
1331 LRK_MACRO_EXPANSION_POINT,
1332 NULL);
1334 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1335 l1 = linemap_resolve_location (set, l1,
1336 LRK_MACRO_EXPANSION_POINT,
1337 NULL);
1339 if (l0 == l1
1340 && pre_virtual_p
1341 && post_virtual_p)
1343 /* So pre and post represent two tokens that are present in a
1344 same macro expansion. Let's see if the token for pre was
1345 before the token for post in that expansion. */
1346 unsigned i0, i1;
1347 const struct line_map *map =
1348 first_map_in_common (set, pre, post, &l0, &l1);
1350 if (map == NULL)
1351 /* This should not be possible. */
1352 abort ();
1354 i0 = l0 - MAP_START_LOCATION (map);
1355 i1 = l1 - MAP_START_LOCATION (map);
1356 return i1 - i0;
1359 if (IS_ADHOC_LOC (l0))
1360 l0 = get_location_from_adhoc_loc (set, l0);
1361 if (IS_ADHOC_LOC (l1))
1362 l1 = get_location_from_adhoc_loc (set, l1);
1364 return l1 - l0;
1367 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1369 static void
1370 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1372 unsigned int i = set->depth;
1374 while (--i)
1375 putc ('.', stderr);
1377 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1380 /* Return the spelling location of the token wherever it comes from,
1381 whether part of a macro definition or not.
1383 This is a subroutine for linemap_resolve_location. */
1385 static location_t
1386 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1387 location_t location,
1388 const line_map_ordinary **original_map)
1390 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1392 while (true)
1394 const struct line_map *map = linemap_lookup (set, location);
1395 if (!map || MAP_ORDINARY_P (map))
1397 if (original_map)
1398 *original_map = (const line_map_ordinary *)map;
1399 break;
1402 location = linemap_macro_map_loc_unwind_toward_spelling
1403 (set, linemap_check_macro (map), location);
1406 return location;
1409 /* If LOCATION is the source location of a token that belongs to a
1410 macro replacement-list -- as part of a macro expansion -- then
1411 return the location of the token at the definition point of the
1412 macro. Otherwise, return LOCATION. SET is the set of maps
1413 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1414 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1415 returned location comes from.
1417 This is a subroutine of linemap_resolve_location. */
1419 static location_t
1420 linemap_macro_loc_to_def_point (struct line_maps *set,
1421 location_t location,
1422 const line_map_ordinary **original_map)
1424 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1426 for (;;)
1428 location_t caret_loc = location;
1429 if (IS_ADHOC_LOC (caret_loc))
1430 caret_loc = get_location_from_adhoc_loc (set, caret_loc);
1432 const line_map *map = linemap_lookup (set, caret_loc);
1433 if (!map || MAP_ORDINARY_P (map))
1435 if (original_map)
1436 *original_map = (const line_map_ordinary *)map;
1437 break;
1440 location = linemap_macro_map_loc_to_def_point
1441 (linemap_check_macro (map), caret_loc);
1444 return location;
1447 /* If LOCATION is the source location of a token that belongs to a
1448 macro replacement-list -- at a macro expansion point -- then return
1449 the location of the topmost expansion point of the macro. We say
1450 topmost because if we are in the context of a nested macro
1451 expansion, the function returns the source location of the first
1452 macro expansion that triggered the nested expansions.
1454 Otherwise, return LOCATION. SET is the set of maps location come
1455 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1456 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1457 location comes from.
1459 This is a subroutine of linemap_resolve_location. */
1461 static location_t
1462 linemap_macro_loc_to_exp_point (struct line_maps *set,
1463 location_t location,
1464 const line_map_ordinary **original_map)
1466 struct line_map *map;
1468 if (IS_ADHOC_LOC (location))
1469 location = get_location_from_adhoc_loc (set, location);
1471 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1473 while (true)
1475 map = const_cast <line_map *> (linemap_lookup (set, location));
1476 if (!linemap_macro_expansion_map_p (map))
1477 break;
1478 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1479 location);
1482 if (original_map)
1483 *original_map = linemap_check_ordinary (map);
1484 return location;
1487 /* Resolve a virtual location into either a spelling location, an
1488 expansion point location or a token argument replacement point
1489 location. Return the map that encodes the virtual location as well
1490 as the resolved location.
1492 If LOC is *NOT* the location of a token resulting from the
1493 expansion of a macro, then the parameter LRK (which stands for
1494 Location Resolution Kind) is ignored and the resulting location
1495 just equals the one given in argument.
1497 Now if LOC *IS* the location of a token resulting from the
1498 expansion of a macro, this is what happens.
1500 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1501 -------------------------------
1503 The virtual location is resolved to the first macro expansion point
1504 that led to this macro expansion.
1506 * If LRK is set to LRK_SPELLING_LOCATION
1507 -------------------------------------
1509 The virtual location is resolved to the locus where the token has
1510 been spelled in the source. This can follow through all the macro
1511 expansions that led to the token.
1513 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1514 --------------------------------------
1516 The virtual location is resolved to the locus of the token in the
1517 context of the macro definition.
1519 If LOC is the locus of a token that is an argument of a
1520 function-like macro [replacing a parameter in the replacement list
1521 of the macro] the virtual location is resolved to the locus of the
1522 parameter that is replaced, in the context of the definition of the
1523 macro.
1525 If LOC is the locus of a token that is not an argument of a
1526 function-like macro, then the function behaves as if LRK was set to
1527 LRK_SPELLING_LOCATION.
1529 If MAP is not NULL, *MAP is set to the map encoding the
1530 returned location. Note that if the returned location wasn't originally
1531 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1532 resolves to a location reserved for the client code, like
1533 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1535 location_t
1536 linemap_resolve_location (struct line_maps *set,
1537 location_t loc,
1538 enum location_resolution_kind lrk,
1539 const line_map_ordinary **map)
1541 location_t locus = loc;
1542 if (IS_ADHOC_LOC (loc))
1543 locus = get_location_from_adhoc_loc (set, loc);
1545 if (locus < RESERVED_LOCATION_COUNT)
1547 /* A reserved location wasn't encoded in a map. Let's return a
1548 NULL map here, just like what linemap_ordinary_map_lookup
1549 does. */
1550 if (map)
1551 *map = NULL;
1552 return loc;
1555 switch (lrk)
1557 case LRK_MACRO_EXPANSION_POINT:
1558 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1559 break;
1560 case LRK_SPELLING_LOCATION:
1561 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1562 break;
1563 case LRK_MACRO_DEFINITION_LOCATION:
1564 loc = linemap_macro_loc_to_def_point (set, loc, map);
1565 break;
1566 default:
1567 abort ();
1569 return loc;
1572 /* TRUE if LOCATION is a source code location of a token that is part of the
1573 definition of a macro, FALSE otherwise. */
1575 bool
1576 linemap_location_from_macro_definition_p (struct line_maps *set,
1577 location_t loc)
1579 if (IS_ADHOC_LOC (loc))
1580 loc = get_location_from_adhoc_loc (set, loc);
1582 if (!linemap_location_from_macro_expansion_p (set, loc))
1583 return false;
1585 while (true)
1587 const struct line_map_macro *map
1588 = linemap_check_macro (linemap_lookup (set, loc));
1590 location_t s_loc
1591 = linemap_macro_map_loc_unwind_toward_spelling (set, map, loc);
1592 if (linemap_location_from_macro_expansion_p (set, s_loc))
1593 loc = s_loc;
1594 else
1596 location_t def_loc
1597 = linemap_macro_map_loc_to_def_point (map, loc);
1598 return s_loc == def_loc;
1604 Suppose that LOC is the virtual location of a token T coming from
1605 the expansion of a macro M. This function then steps up to get the
1606 location L of the point where M got expanded. If L is a spelling
1607 location inside a macro expansion M', then this function returns
1608 the locus of the point where M' was expanded. Said otherwise, this
1609 function returns the location of T in the context that triggered
1610 the expansion of M.
1612 *LOC_MAP must be set to the map of LOC. This function then sets it
1613 to the map of the returned location. */
1615 location_t
1616 linemap_unwind_toward_expansion (struct line_maps *set,
1617 location_t loc,
1618 const struct line_map **map)
1620 location_t resolved_location;
1621 const line_map_macro *macro_map = linemap_check_macro (*map);
1622 const struct line_map *resolved_map;
1624 if (IS_ADHOC_LOC (loc))
1625 loc = get_location_from_adhoc_loc (set, loc);
1627 resolved_location =
1628 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1629 resolved_map = linemap_lookup (set, resolved_location);
1631 if (!linemap_macro_expansion_map_p (resolved_map))
1633 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1634 resolved_map = linemap_lookup (set, resolved_location);
1637 *map = resolved_map;
1638 return resolved_location;
1641 /* If LOC is the virtual location of a token coming from the expansion
1642 of a macro M and if its spelling location is reserved (e.g, a
1643 location for a built-in token), then this function unwinds (using
1644 linemap_unwind_toward_expansion) the location until a location that
1645 is not reserved and is not in a system header is reached. In other
1646 words, this unwinds the reserved location until a location that is
1647 in real source code is reached.
1649 Otherwise, if the spelling location for LOC is not reserved or if
1650 LOC doesn't come from the expansion of a macro, the function
1651 returns LOC as is and *MAP is not touched.
1653 *MAP is set to the map of the returned location if the later is
1654 different from LOC. */
1655 location_t
1656 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1657 location_t loc,
1658 const struct line_map **map)
1660 location_t resolved_loc;
1661 const struct line_map *map0 = NULL;
1662 const line_map_ordinary *map1 = NULL;
1664 if (IS_ADHOC_LOC (loc))
1665 loc = get_location_from_adhoc_loc (set, loc);
1667 map0 = linemap_lookup (set, loc);
1668 if (!linemap_macro_expansion_map_p (map0))
1669 return loc;
1671 resolved_loc = linemap_resolve_location (set, loc,
1672 LRK_SPELLING_LOCATION,
1673 &map1);
1675 if (resolved_loc >= RESERVED_LOCATION_COUNT
1676 && !LINEMAP_SYSP (map1))
1677 return loc;
1679 while (linemap_macro_expansion_map_p (map0)
1680 && (resolved_loc < RESERVED_LOCATION_COUNT
1681 || LINEMAP_SYSP (map1)))
1683 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1684 resolved_loc = linemap_resolve_location (set, loc,
1685 LRK_SPELLING_LOCATION,
1686 &map1);
1689 if (map != NULL)
1690 *map = map0;
1691 return loc;
1694 /* Expand source code location LOC and return a user readable source
1695 code location. LOC must be a spelling (non-virtual) location. If
1696 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1697 location is returned. */
1699 expanded_location
1700 linemap_expand_location (struct line_maps *set,
1701 const struct line_map *map,
1702 location_t loc)
1705 expanded_location xloc;
1707 memset (&xloc, 0, sizeof (xloc));
1708 if (IS_ADHOC_LOC (loc))
1710 xloc.data = get_data_from_adhoc_loc (set, loc);
1711 loc = get_location_from_adhoc_loc (set, loc);
1714 if (loc < RESERVED_LOCATION_COUNT)
1715 /* The location for this token wasn't generated from a line map.
1716 It was probably a location for a builtin token, chosen by some
1717 client code. Let's not try to expand the location in that
1718 case. */;
1719 else if (map == NULL)
1720 /* We shouldn't be getting a NULL map with a location that is not
1721 reserved by the client code. */
1722 abort ();
1723 else
1725 /* MAP must be an ordinary map and LOC must be non-virtual,
1726 encoded into this map, obviously; the accessors used on MAP
1727 below ensure it is ordinary. Let's just assert the
1728 non-virtualness of LOC here. */
1729 if (linemap_location_from_macro_expansion_p (set, loc))
1730 abort ();
1732 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1734 xloc.file = LINEMAP_FILE (ord_map);
1735 xloc.line = SOURCE_LINE (ord_map, loc);
1736 xloc.column = SOURCE_COLUMN (ord_map, loc);
1737 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1740 return xloc;
1744 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1745 is NULL, use stderr. IS_MACRO is true if the caller wants to
1746 dump a macro map, false otherwise. */
1748 void
1749 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1751 const char *const lc_reasons_v[LC_HWM]
1752 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1753 "LC_ENTER_MACRO" };
1754 const line_map *map;
1755 unsigned reason;
1757 if (stream == NULL)
1758 stream = stderr;
1760 if (!is_macro)
1762 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1763 reason = linemap_check_ordinary (map)->reason;
1765 else
1767 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1768 reason = LC_ENTER_MACRO;
1771 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1772 ix, (void *) map, map->start_location,
1773 reason < LC_HWM ? lc_reasons_v[reason] : "???",
1774 ((!is_macro
1775 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1776 ? "yes" : "no"));
1777 if (!is_macro)
1779 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1780 const line_map_ordinary *includer_map
1781 = linemap_included_from_linemap (set, ord_map);
1783 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1784 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1785 fprintf (stream, "Included from: [%d] %s\n",
1786 includer_map ? int (includer_map - set->info_ordinary.maps) : -1,
1787 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1789 else
1791 const line_map_macro *macro_map = linemap_check_macro (map);
1792 fprintf (stream, "Macro: %s (%u tokens)\n",
1793 linemap_map_get_macro_name (macro_map),
1794 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1797 fprintf (stream, "\n");
1801 /* Dump debugging information about source location LOC into the file
1802 stream STREAM. SET is the line map set LOC comes from. */
1804 void
1805 linemap_dump_location (struct line_maps *set,
1806 location_t loc,
1807 FILE *stream)
1809 const line_map_ordinary *map;
1810 location_t location;
1811 const char *path = "", *from = "";
1812 int l = -1, c = -1, s = -1, e = -1;
1814 if (IS_ADHOC_LOC (loc))
1815 loc = get_location_from_adhoc_loc (set, loc);
1817 if (loc == 0)
1818 return;
1820 location =
1821 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1823 if (map == NULL)
1824 /* Only reserved locations can be tolerated in this case. */
1825 linemap_assert (location < RESERVED_LOCATION_COUNT);
1826 else
1828 path = LINEMAP_FILE (map);
1829 l = SOURCE_LINE (map, location);
1830 c = SOURCE_COLUMN (map, location);
1831 s = LINEMAP_SYSP (map) != 0;
1832 e = location != loc;
1833 if (e)
1834 from = "N/A";
1835 else
1837 const line_map_ordinary *from_map
1838 = linemap_included_from_linemap (set, map);
1839 from = from_map ? LINEMAP_FILE (from_map) : "<NULL>";
1843 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1844 E: macro expansion?, LOC: original location, R: resolved location */
1845 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1846 path, from, l, c, s, (void*)map, e, loc, location);
1849 /* Return the highest location emitted for a given file for which
1850 there is a line map in SET. FILE_NAME is the file name to
1851 consider. If the function returns TRUE, *LOC is set to the highest
1852 location emitted for that file. */
1854 bool
1855 linemap_get_file_highest_location (struct line_maps *set,
1856 const char *file_name,
1857 location_t *loc)
1859 /* If the set is empty or no ordinary map has been created then
1860 there is no file to look for ... */
1861 if (set == NULL || set->info_ordinary.used == 0)
1862 return false;
1864 /* Now look for the last ordinary map created for FILE_NAME. */
1865 int i;
1866 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1868 const char *fname = set->info_ordinary.maps[i].to_file;
1869 if (fname && !filename_cmp (fname, file_name))
1870 break;
1873 if (i < 0)
1874 return false;
1876 /* The highest location for a given map is either the starting
1877 location of the next map minus one, or -- if the map is the
1878 latest one -- the highest location of the set. */
1879 location_t result;
1880 if (i == (int) set->info_ordinary.used - 1)
1881 result = set->highest_location;
1882 else
1883 result = set->info_ordinary.maps[i + 1].start_location - 1;
1885 *loc = result;
1886 return true;
1889 /* Compute and return statistics about the memory consumption of some
1890 parts of the line table SET. */
1892 void
1893 linemap_get_statistics (struct line_maps *set,
1894 struct linemap_stats *s)
1896 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1897 macro_maps_allocated_size, macro_maps_used_size,
1898 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1900 const line_map_macro *cur_map;
1902 ordinary_maps_allocated_size =
1903 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1905 ordinary_maps_used_size =
1906 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1908 macro_maps_allocated_size =
1909 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1911 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1912 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1913 ++cur_map)
1915 unsigned i;
1917 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1919 macro_maps_locations_size +=
1920 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (location_t);
1922 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1924 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1925 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1926 duplicated_macro_maps_locations_size +=
1927 sizeof (location_t);
1931 macro_maps_used_size =
1932 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1934 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1935 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1936 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1937 s->ordinary_maps_used_size = ordinary_maps_used_size;
1938 s->num_expanded_macros = num_expanded_macros_counter;
1939 s->num_macro_tokens = num_macro_tokens_counter;
1940 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1941 s->macro_maps_allocated_size = macro_maps_allocated_size;
1942 s->macro_maps_locations_size = macro_maps_locations_size;
1943 s->macro_maps_used_size = macro_maps_used_size;
1944 s->duplicated_macro_maps_locations_size =
1945 duplicated_macro_maps_locations_size;
1946 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1947 * sizeof (struct location_adhoc_data));
1948 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
1952 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1953 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1954 specifies how many macro maps to dump. */
1956 void
1957 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1958 unsigned int num_macro)
1960 unsigned int i;
1962 if (set == NULL)
1963 return;
1965 if (stream == NULL)
1966 stream = stderr;
1968 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1969 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1970 fprintf (stream, "Include stack depth: %d\n", set->depth);
1971 fprintf (stream, "Highest location: %u\n", set->highest_location);
1973 if (num_ordinary)
1975 fprintf (stream, "\nOrdinary line maps\n");
1976 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1977 linemap_dump (stream, set, i, false);
1978 fprintf (stream, "\n");
1981 if (num_macro)
1983 fprintf (stream, "\nMacro line maps\n");
1984 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1985 linemap_dump (stream, set, i, true);
1986 fprintf (stream, "\n");
1990 /* class rich_location. */
1992 /* Construct a rich_location with location LOC as its initial range. */
1994 rich_location::rich_location (line_maps *set, location_t loc,
1995 const range_label *label) :
1996 m_line_table (set),
1997 m_ranges (),
1998 m_column_override (0),
1999 m_have_expanded_location (false),
2000 m_fixit_hints (),
2001 m_seen_impossible_fixit (false),
2002 m_fixits_cannot_be_auto_applied (false)
2004 add_range (loc, SHOW_RANGE_WITH_CARET, label);
2007 /* The destructor for class rich_location. */
2009 rich_location::~rich_location ()
2011 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2012 delete get_fixit_hint (i);
2015 /* Get location IDX within this rich_location. */
2017 location_t
2018 rich_location::get_loc (unsigned int idx) const
2020 const location_range *locrange = get_range (idx);
2021 return locrange->m_loc;
2024 /* Get range IDX within this rich_location. */
2026 const location_range *
2027 rich_location::get_range (unsigned int idx) const
2029 return &m_ranges[idx];
2032 /* Mutable access to range IDX within this rich_location. */
2034 location_range *
2035 rich_location::get_range (unsigned int idx)
2037 return &m_ranges[idx];
2040 /* Expand location IDX within this rich_location. */
2041 /* Get an expanded_location for this rich_location's primary
2042 location. */
2044 expanded_location
2045 rich_location::get_expanded_location (unsigned int idx)
2047 if (idx == 0)
2049 /* Cache the expansion of the primary location. */
2050 if (!m_have_expanded_location)
2052 m_expanded_location
2053 = linemap_client_expand_location_to_spelling_point
2054 (get_loc (0), LOCATION_ASPECT_CARET);
2055 if (m_column_override)
2056 m_expanded_location.column = m_column_override;
2057 m_have_expanded_location = true;
2060 return m_expanded_location;
2062 else
2063 return linemap_client_expand_location_to_spelling_point
2064 (get_loc (idx), LOCATION_ASPECT_CARET);
2067 /* Set the column of the primary location, with 0 meaning
2068 "don't override it". */
2070 void
2071 rich_location::override_column (int column)
2073 m_column_override = column;
2074 m_have_expanded_location = false;
2077 /* Add the given range. */
2079 void
2080 rich_location::add_range (location_t loc,
2081 enum range_display_kind range_display_kind,
2082 const range_label *label)
2084 location_range range;
2085 range.m_loc = loc;
2086 range.m_range_display_kind = range_display_kind;
2087 range.m_label = label;
2088 m_ranges.push (range);
2091 /* Add or overwrite the location given by IDX, setting its location to LOC,
2092 and setting its m_range_display_kind to RANGE_DISPLAY_KIND.
2094 It must either overwrite an existing location, or add one *exactly* on
2095 the end of the array.
2097 This is primarily for use by gcc when implementing diagnostic format
2098 decoders e.g.
2099 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2100 (which writes the source location of a tree back into location 0 of
2101 the rich_location), and
2102 - the "%C" and "%L" format codes in the Fortran frontend. */
2104 void
2105 rich_location::set_range (unsigned int idx, location_t loc,
2106 enum range_display_kind range_display_kind)
2108 /* We can either overwrite an existing range, or add one exactly
2109 on the end of the array. */
2110 linemap_assert (idx <= m_ranges.count ());
2112 if (idx == m_ranges.count ())
2113 add_range (loc, range_display_kind);
2114 else
2116 location_range *locrange = get_range (idx);
2117 locrange->m_loc = loc;
2118 locrange->m_range_display_kind = range_display_kind;
2121 if (idx == 0)
2122 /* Mark any cached value here as dirty. */
2123 m_have_expanded_location = false;
2126 /* Methods for adding insertion fix-it hints. */
2128 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2129 immediately before the primary range's start location. */
2131 void
2132 rich_location::add_fixit_insert_before (const char *new_content)
2134 add_fixit_insert_before (get_loc (), new_content);
2137 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2138 immediately before the start of WHERE. */
2140 void
2141 rich_location::add_fixit_insert_before (location_t where,
2142 const char *new_content)
2144 location_t start = get_range_from_loc (m_line_table, where).m_start;
2145 maybe_add_fixit (start, start, new_content);
2148 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2149 immediately after the primary range's end-point. */
2151 void
2152 rich_location::add_fixit_insert_after (const char *new_content)
2154 add_fixit_insert_after (get_loc (), new_content);
2157 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2158 immediately after the end-point of WHERE. */
2160 void
2161 rich_location::add_fixit_insert_after (location_t where,
2162 const char *new_content)
2164 location_t finish = get_range_from_loc (m_line_table, where).m_finish;
2165 location_t next_loc
2166 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2168 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2169 its input value. */
2170 if (next_loc == finish)
2172 stop_supporting_fixits ();
2173 return;
2176 maybe_add_fixit (next_loc, next_loc, new_content);
2179 /* Methods for adding removal fix-it hints. */
2181 /* Add a fixit-hint, suggesting removal of the content covered
2182 by range 0. */
2184 void
2185 rich_location::add_fixit_remove ()
2187 add_fixit_remove (get_loc ());
2190 /* Add a fixit-hint, suggesting removal of the content between
2191 the start and finish of WHERE. */
2193 void
2194 rich_location::add_fixit_remove (location_t where)
2196 source_range range = get_range_from_loc (m_line_table, where);
2197 add_fixit_remove (range);
2200 /* Add a fixit-hint, suggesting removal of the content at
2201 SRC_RANGE. */
2203 void
2204 rich_location::add_fixit_remove (source_range src_range)
2206 add_fixit_replace (src_range, "");
2209 /* Add a fixit-hint, suggesting replacement of the content covered
2210 by range 0 with NEW_CONTENT. */
2212 void
2213 rich_location::add_fixit_replace (const char *new_content)
2215 add_fixit_replace (get_loc (), new_content);
2218 /* Methods for adding "replace" fix-it hints. */
2220 /* Add a fixit-hint, suggesting replacement of the content between
2221 the start and finish of WHERE with NEW_CONTENT. */
2223 void
2224 rich_location::add_fixit_replace (location_t where,
2225 const char *new_content)
2227 source_range range = get_range_from_loc (m_line_table, where);
2228 add_fixit_replace (range, new_content);
2231 /* Add a fixit-hint, suggesting replacement of the content at
2232 SRC_RANGE with NEW_CONTENT. */
2234 void
2235 rich_location::add_fixit_replace (source_range src_range,
2236 const char *new_content)
2238 location_t start = get_pure_location (m_line_table, src_range.m_start);
2239 location_t finish = get_pure_location (m_line_table, src_range.m_finish);
2241 /* Fix-it hints use half-closed ranges, so attempt to offset the endpoint. */
2242 location_t next_loc
2243 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2244 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2245 its input value. */
2246 if (next_loc == finish)
2248 stop_supporting_fixits ();
2249 return;
2251 finish = next_loc;
2253 maybe_add_fixit (start, finish, new_content);
2256 /* Get the last fix-it hint within this rich_location, or NULL if none. */
2258 fixit_hint *
2259 rich_location::get_last_fixit_hint () const
2261 if (m_fixit_hints.count () > 0)
2262 return get_fixit_hint (m_fixit_hints.count () - 1);
2263 else
2264 return NULL;
2267 /* If WHERE is an "awkward" location, then mark this rich_location as not
2268 supporting fixits, purging any thay were already added, and return true.
2270 Otherwise (the common case), return false. */
2272 bool
2273 rich_location::reject_impossible_fixit (location_t where)
2275 /* Fix-its within a rich_location should either all be suggested, or
2276 none of them should be suggested.
2277 Once we've rejected a fixit, we reject any more, even those
2278 with reasonable locations. */
2279 if (m_seen_impossible_fixit)
2280 return true;
2282 if (where <= LINE_MAP_MAX_LOCATION_WITH_COLS)
2283 /* WHERE is a reasonable location for a fix-it; don't reject it. */
2284 return false;
2286 /* Otherwise we have an attempt to add a fix-it with an "awkward"
2287 location: either one that we can't obtain column information
2288 for (within an ordinary map), or one within a macro expansion. */
2289 stop_supporting_fixits ();
2290 return true;
2293 /* Mark this rich_location as not supporting fixits, purging any that were
2294 already added. */
2296 void
2297 rich_location::stop_supporting_fixits ()
2299 m_seen_impossible_fixit = true;
2301 /* Purge the rich_location of any fix-its that were already added. */
2302 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2303 delete get_fixit_hint (i);
2304 m_fixit_hints.truncate (0);
2307 /* Add HINT to the fix-it hints in this rich_location,
2308 consolidating into the prior fixit if possible. */
2310 void
2311 rich_location::maybe_add_fixit (location_t start,
2312 location_t next_loc,
2313 const char *new_content)
2315 if (reject_impossible_fixit (start))
2316 return;
2317 if (reject_impossible_fixit (next_loc))
2318 return;
2320 /* Only allow fix-it hints that affect a single line in one file.
2321 Compare the end-points. */
2322 expanded_location exploc_start
2323 = linemap_client_expand_location_to_spelling_point (start,
2324 LOCATION_ASPECT_START);
2325 expanded_location exploc_next_loc
2326 = linemap_client_expand_location_to_spelling_point (next_loc,
2327 LOCATION_ASPECT_START);
2328 /* They must be within the same file... */
2329 if (exploc_start.file != exploc_next_loc.file)
2331 stop_supporting_fixits ();
2332 return;
2334 /* ...and on the same line. */
2335 if (exploc_start.line != exploc_next_loc.line)
2337 stop_supporting_fixits ();
2338 return;
2340 /* The columns must be in the correct order. This can fail if the
2341 endpoints straddle the boundary for which the linemap can represent
2342 columns (PR c/82050). */
2343 if (exploc_start.column > exploc_next_loc.column)
2345 stop_supporting_fixits ();
2346 return;
2349 const char *newline = strchr (new_content, '\n');
2350 if (newline)
2352 /* For now, we can only support insertion of whole lines
2353 i.e. starts at start of line, and the newline is at the end of
2354 the insertion point. */
2356 /* It must be an insertion, not a replacement/deletion. */
2357 if (start != next_loc)
2359 stop_supporting_fixits ();
2360 return;
2363 /* The insertion must be at the start of a line. */
2364 if (exploc_start.column != 1)
2366 stop_supporting_fixits ();
2367 return;
2370 /* The newline must be at end of NEW_CONTENT.
2371 We could eventually split up fix-its at newlines if we wanted
2372 to allow more generality (e.g. to allow adding multiple lines
2373 with one add_fixit call. */
2374 if (newline[1] != '\0')
2376 stop_supporting_fixits ();
2377 return;
2381 /* Consolidate neighboring fixits.
2382 Don't consolidate into newline-insertion fixits. */
2383 fixit_hint *prev = get_last_fixit_hint ();
2384 if (prev && !prev->ends_with_newline_p ())
2385 if (prev->maybe_append (start, next_loc, new_content))
2386 return;
2388 m_fixit_hints.push (new fixit_hint (start, next_loc, new_content));
2391 /* class fixit_hint. */
2393 fixit_hint::fixit_hint (location_t start,
2394 location_t next_loc,
2395 const char *new_content)
2396 : m_start (start),
2397 m_next_loc (next_loc),
2398 m_bytes (xstrdup (new_content)),
2399 m_len (strlen (new_content))
2403 /* Does this fix-it hint affect the given line? */
2405 bool
2406 fixit_hint::affects_line_p (const char *file, int line) const
2408 expanded_location exploc_start
2409 = linemap_client_expand_location_to_spelling_point (m_start,
2410 LOCATION_ASPECT_START);
2411 if (file != exploc_start.file)
2412 return false;
2413 if (line < exploc_start.line)
2414 return false;
2415 expanded_location exploc_next_loc
2416 = linemap_client_expand_location_to_spelling_point (m_next_loc,
2417 LOCATION_ASPECT_START);
2418 if (file != exploc_next_loc.file)
2419 return false;
2420 if (line > exploc_next_loc.line)
2421 return false;
2422 return true;
2425 /* Method for consolidating fix-it hints, for use by
2426 rich_location::maybe_add_fixit.
2427 If possible, merge a pending fix-it hint with the given params
2428 into this one and return true.
2429 Otherwise return false. */
2431 bool
2432 fixit_hint::maybe_append (location_t start,
2433 location_t next_loc,
2434 const char *new_content)
2436 /* For consolidation to be possible, START must be at this hint's
2437 m_next_loc. */
2438 if (start != m_next_loc)
2439 return false;
2441 /* If so, we have neighboring replacements; merge them. */
2442 m_next_loc = next_loc;
2443 size_t extra_len = strlen (new_content);
2444 m_bytes = (char *)xrealloc (m_bytes, m_len + extra_len + 1);
2445 memcpy (m_bytes + m_len, new_content, extra_len);
2446 m_len += extra_len;
2447 m_bytes[m_len] = '\0';
2448 return true;
2451 /* Return true iff this hint's content ends with a newline. */
2453 bool
2454 fixit_hint::ends_with_newline_p () const
2456 if (m_len == 0)
2457 return false;
2458 return m_bytes[m_len - 1] == '\n';