Avoid is_constant calls in vectorizable_bswap
[official-gcc.git] / libcpp / line-map.c
blobf0e6318e412cd8823ee5fcdd08fe86d6d950efaa
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2018 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 source_location);
32 static const line_map_macro* linemap_macro_map_lookup (struct line_maps *,
33 source_location);
34 static source_location linemap_macro_map_loc_to_def_point
35 (const line_map_macro *, source_location);
36 static source_location linemap_macro_map_loc_to_exp_point
37 (const line_map_macro *, source_location);
38 static source_location linemap_macro_loc_to_spelling_point
39 (struct line_maps *, source_location, const line_map_ordinary **);
40 static source_location linemap_macro_loc_to_def_point (struct line_maps *,
41 source_location,
42 const line_map_ordinary **);
43 static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
44 source_location,
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 source_location, without needing to use an ad-hoc location. */
115 static bool
116 can_be_stored_compactly_p (struct line_maps *set,
117 source_location 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 source_location, 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 source_location 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 source_location
157 get_combined_adhoc_loc (struct line_maps *set,
158 source_location 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
167 = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
168 if (locus == 0 && data == NULL)
169 return 0;
171 /* Any ordinary locations ought to be "pure" at this point: no
172 compressed ranges. */
173 linemap_assert (locus < RESERVED_LOCATION_COUNT
174 || locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
175 || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
176 || pure_location_p (set, locus));
178 /* Consider short-range optimization. */
179 if (can_be_stored_compactly_p (set, locus, src_range, data))
181 /* The low bits ought to be clear. */
182 linemap_assert (pure_location_p (set, locus));
183 const line_map *map = linemap_lookup (set, locus);
184 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
185 unsigned int int_diff = src_range.m_finish - src_range.m_start;
186 unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
187 if (col_diff < (1U << ordmap->m_range_bits))
189 source_location packed = locus | col_diff;
190 set->num_optimized_ranges++;
191 return packed;
195 /* We can also compactly store locations
196 when locus == start == finish (and data is NULL). */
197 if (locus == src_range.m_start
198 && locus == src_range.m_finish
199 && !data)
200 return locus;
202 if (!data)
203 set->num_unoptimized_ranges++;
205 lb.locus = locus;
206 lb.src_range = src_range;
207 lb.data = data;
208 slot = (struct location_adhoc_data **)
209 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
210 if (*slot == NULL)
212 if (set->location_adhoc_data_map.curr_loc >=
213 set->location_adhoc_data_map.allocated)
215 char *orig_data = (char *) set->location_adhoc_data_map.data;
216 ptrdiff_t offset;
217 /* Cast away extern "C" from the type of xrealloc. */
218 line_map_realloc reallocator = (set->reallocator
219 ? set->reallocator
220 : (line_map_realloc) xrealloc);
222 if (set->location_adhoc_data_map.allocated == 0)
223 set->location_adhoc_data_map.allocated = 128;
224 else
225 set->location_adhoc_data_map.allocated *= 2;
226 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
227 reallocator (set->location_adhoc_data_map.data,
228 set->location_adhoc_data_map.allocated
229 * sizeof (struct location_adhoc_data));
230 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
231 if (set->location_adhoc_data_map.allocated > 128)
232 htab_traverse (set->location_adhoc_data_map.htab,
233 location_adhoc_data_update, &offset);
235 *slot = set->location_adhoc_data_map.data
236 + set->location_adhoc_data_map.curr_loc;
237 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
238 = lb;
240 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
243 /* Return the data for the adhoc loc. */
245 void *
246 get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
248 linemap_assert (IS_ADHOC_LOC (loc));
249 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
252 /* Return the location for the adhoc loc. */
254 source_location
255 get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
257 linemap_assert (IS_ADHOC_LOC (loc));
258 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
261 /* Return the source_range for adhoc location LOC. */
263 static source_range
264 get_range_from_adhoc_loc (struct line_maps *set, source_location loc)
266 linemap_assert (IS_ADHOC_LOC (loc));
267 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].src_range;
270 /* Get the source_range of location LOC, either from the ad-hoc
271 lookaside table, or embedded inside LOC itself. */
273 source_range
274 get_range_from_loc (struct line_maps *set,
275 source_location loc)
277 if (IS_ADHOC_LOC (loc))
278 return get_range_from_adhoc_loc (set, loc);
280 /* For ordinary maps, extract packed range. */
281 if (loc >= RESERVED_LOCATION_COUNT
282 && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
283 && loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
285 const line_map *map = linemap_lookup (set, loc);
286 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
287 source_range result;
288 int offset = loc & ((1 << ordmap->m_range_bits) - 1);
289 result.m_start = loc - offset;
290 result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
291 return result;
294 return source_range::from_location (loc);
297 /* Get whether location LOC is a "pure" location, or
298 whether it is an ad-hoc location, or embeds range information. */
300 bool
301 pure_location_p (line_maps *set, source_location loc)
303 if (IS_ADHOC_LOC (loc))
304 return false;
306 const line_map *map = linemap_lookup (set, loc);
307 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
309 if (loc & ((1U << ordmap->m_range_bits) - 1))
310 return false;
312 return true;
315 /* Given location LOC within SET, strip away any packed range information
316 or ad-hoc information. */
318 source_location
319 get_pure_location (line_maps *set, source_location loc)
321 if (IS_ADHOC_LOC (loc))
323 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
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 source_location 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, source_location 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 source_location 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);
495 line_map_ordinary *map
496 = linemap_check_ordinary (new_linemap (set, start_location));
497 map->reason = reason;
499 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
500 to_file = "<stdin>";
502 if (reason == LC_RENAME_VERBATIM)
503 reason = LC_RENAME;
505 const line_map_ordinary *from = NULL;
506 if (reason == LC_LEAVE)
508 /* When we are just leaving an "included" file, and jump to the next
509 location inside the "includer" right after the #include
510 "included", this variable points the map in use right before the
511 #include "included", inside the same "includer" file. */
513 linemap_assert (!MAIN_FILE_P (map - 1));
514 /* (MAP - 1) points to the map we are leaving. The
515 map from which (MAP - 1) got included should be the map
516 that comes right before MAP in the same file. */
517 from = linemap_included_from_linemap (set, map - 1);
519 /* A TO_FILE of NULL is special - we use the natural values. */
520 if (to_file == NULL)
522 to_file = ORDINARY_MAP_FILE_NAME (from);
523 to_line = SOURCE_LINE (from, from[1].start_location);
524 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
526 else
527 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from),
528 to_file) == 0);
531 map->sysp = sysp;
532 map->to_file = to_file;
533 map->to_line = to_line;
534 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
535 map->m_column_and_range_bits = 0;
536 map->m_range_bits = 0;
537 set->highest_location = start_location;
538 set->highest_line = start_location;
539 set->max_column_hint = 0;
541 /* This assertion is placed after set->highest_location has
542 been updated, since the latter affects
543 linemap_location_from_macro_expansion_p, which ultimately affects
544 pure_location_p. */
545 linemap_assert (pure_location_p (set, start_location));
547 if (reason == LC_ENTER)
549 if (set->depth == 0)
550 map->included_from = 0;
551 else
552 /* The location of the end of the just-closed map. */
553 map->included_from
554 = (((map[0].start_location - 1 - map[-1].start_location)
555 & ~((1 << map[-1].m_column_and_range_bits) - 1))
556 + map[-1].start_location);
557 set->depth++;
558 if (set->trace_includes)
559 trace_include (set, map);
561 else if (reason == LC_RENAME)
562 map->included_from = linemap_included_from (&map[-1]);
563 else if (reason == LC_LEAVE)
565 set->depth--;
566 map->included_from = linemap_included_from (from);
569 return map;
572 /* Returns TRUE if the line table set tracks token locations across
573 macro expansion, FALSE otherwise. */
575 bool
576 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
578 return LINEMAPS_MACRO_MAPS (set) != NULL;
581 /* Create a macro map. A macro map encodes source locations of tokens
582 that are part of a macro replacement-list, at a macro expansion
583 point. See the extensive comments of struct line_map and struct
584 line_map_macro, in line-map.h.
586 This map shall be created when the macro is expanded. The map
587 encodes the source location of the expansion point of the macro as
588 well as the "original" source location of each token that is part
589 of the macro replacement-list. If a macro is defined but never
590 expanded, it has no macro map. SET is the set of maps the macro
591 map should be part of. MACRO_NODE is the macro which the new macro
592 map should encode source locations for. EXPANSION is the location
593 of the expansion point of MACRO. For function-like macros
594 invocations, it's best to make it point to the closing parenthesis
595 of the macro, rather than the the location of the first character
596 of the macro. NUM_TOKENS is the number of tokens that are part of
597 the replacement-list of MACRO.
599 Note that when we run out of the integer space available for source
600 locations, this function returns NULL. In that case, callers of
601 this function cannot encode {line,column} pairs into locations of
602 macro tokens anymore. */
604 const line_map_macro *
605 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
606 source_location expansion, unsigned int num_tokens)
608 line_map_macro *map;
609 source_location start_location;
610 /* Cast away extern "C" from the type of xrealloc. */
611 line_map_realloc reallocator = (set->reallocator
612 ? set->reallocator
613 : (line_map_realloc) xrealloc);
615 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
617 if (start_location < LINE_MAP_MAX_LOCATION)
618 /* We ran out of macro map space. */
619 return NULL;
621 map = linemap_check_macro (new_linemap (set, start_location));
623 map->macro = macro_node;
624 map->n_tokens = num_tokens;
625 map->macro_locations
626 = (source_location*) reallocator (NULL,
627 2 * num_tokens
628 * sizeof (source_location));
629 map->expansion = expansion;
630 memset (MACRO_MAP_LOCATIONS (map), 0,
631 num_tokens * sizeof (source_location));
633 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
635 return map;
638 /* Create and return a virtual location for a token that is part of a
639 macro expansion-list at a macro expansion point. See the comment
640 inside struct line_map_macro to see what an expansion-list exactly
643 A call to this function must come after a call to
644 linemap_enter_macro.
646 MAP is the map into which the source location is created. TOKEN_NO
647 is the index of the token in the macro replacement-list, starting
648 at number 0.
650 ORIG_LOC is the location of the token outside of this macro
651 expansion. If the token comes originally from the macro
652 definition, it is the locus in the macro definition; otherwise it
653 is a location in the context of the caller of this macro expansion
654 (which is a virtual location or a source location if the caller is
655 itself a macro expansion or not).
657 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
658 either of the token itself or of a macro parameter that it
659 replaces. */
661 source_location
662 linemap_add_macro_token (const line_map_macro *map,
663 unsigned int token_no,
664 source_location orig_loc,
665 source_location orig_parm_replacement_loc)
667 source_location result;
669 linemap_assert (linemap_macro_expansion_map_p (map));
670 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
672 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
673 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
675 result = MAP_START_LOCATION (map) + token_no;
676 return result;
679 /* Return a source_location for the start (i.e. column==0) of
680 (physical) line TO_LINE in the current source file (as in the
681 most recent linemap_add). MAX_COLUMN_HINT is the highest column
682 number we expect to use in this line (but it does not change
683 the highest_location). */
685 source_location
686 linemap_line_start (struct line_maps *set, linenum_type to_line,
687 unsigned int max_column_hint)
689 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
690 source_location highest = set->highest_location;
691 source_location r;
692 linenum_type last_line =
693 SOURCE_LINE (map, set->highest_line);
694 int line_delta = to_line - last_line;
695 bool add_map = false;
696 linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
697 int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
699 if (line_delta < 0
700 || (line_delta > 10
701 && line_delta * map->m_column_and_range_bits > 1000)
702 || (max_column_hint >= (1U << effective_column_bits))
703 || (max_column_hint <= 80 && effective_column_bits >= 10)
704 || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
705 && map->m_range_bits > 0)
706 || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
707 && (set->max_column_hint || highest >= LINE_MAP_MAX_LOCATION)))
708 add_map = true;
709 else
710 max_column_hint = set->max_column_hint;
711 if (add_map)
713 int column_bits;
714 int range_bits;
715 if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
716 || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
718 /* If the column number is ridiculous or we've allocated a huge
719 number of source_locations, give up on column numbers
720 (and on packed ranges). */
721 max_column_hint = 0;
722 column_bits = 0;
723 range_bits = 0;
724 if (highest >= LINE_MAP_MAX_LOCATION)
725 return 0;
727 else
729 column_bits = 7;
730 if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
731 range_bits = set->default_range_bits;
732 else
733 range_bits = 0;
734 while (max_column_hint >= (1U << column_bits))
735 column_bits++;
736 max_column_hint = 1U << column_bits;
737 column_bits += range_bits;
739 /* Allocate the new line_map. However, if the current map only has a
740 single line we can sometimes just increase its column_bits instead. */
741 if (line_delta < 0
742 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
743 || SOURCE_COLUMN (map, highest) >= (1U << (column_bits - range_bits))
744 || range_bits < map->m_range_bits)
745 map = linemap_check_ordinary
746 (const_cast <line_map *>
747 (linemap_add (set, LC_RENAME,
748 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
749 ORDINARY_MAP_FILE_NAME (map),
750 to_line)));
751 map->m_column_and_range_bits = column_bits;
752 map->m_range_bits = range_bits;
753 r = (MAP_START_LOCATION (map)
754 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
755 << column_bits));
757 else
758 r = set->highest_line + (line_delta << map->m_column_and_range_bits);
760 /* Locations of ordinary tokens are always lower than locations of
761 macro tokens. */
762 if (r >= LINE_MAP_MAX_LOCATION)
763 return 0;
765 set->highest_line = r;
766 if (r > set->highest_location)
767 set->highest_location = r;
768 set->max_column_hint = max_column_hint;
770 /* At this point, we expect one of:
771 (a) the normal case: a "pure" location with 0 range bits, or
772 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
773 columns anymore (or ranges), or
774 (c) we're in a region with a column hint exceeding
775 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
776 with column_bits == 0. */
777 linemap_assert (pure_location_p (set, r)
778 || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
779 || map->m_column_and_range_bits == 0);
780 linemap_assert (SOURCE_LINE (map, r) == to_line);
781 return r;
784 /* Encode and return a source_location from a column number. The
785 source line considered is the last source line used to call
786 linemap_line_start, i.e, the last source line which a location was
787 encoded from. */
789 source_location
790 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
792 source_location r = set->highest_line;
794 linemap_assert
795 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
797 if (to_column >= set->max_column_hint)
799 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
800 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
802 /* Running low on source_locations - disable column numbers. */
803 return r;
805 else
807 /* Otherwise, attempt to start a new line that can hold TO_COLUMN,
808 with some space to spare. This may or may not lead to a new
809 linemap being created. */
810 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
811 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
812 map = LINEMAPS_LAST_ORDINARY_MAP (set);
813 if (map->m_column_and_range_bits == 0)
815 /* ...then the linemap has column-tracking disabled,
816 presumably due to exceeding either
817 LINE_MAP_MAX_LOCATION_WITH_COLS (overall) or
818 LINE_MAP_MAX_COLUMN_NUMBER (within this line).
819 Return the start of the linemap, which encodes column 0, for
820 the whole line. */
821 return r;
825 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
826 r = r + (to_column << map->m_range_bits);
827 if (r >= set->highest_location)
828 set->highest_location = r;
829 return r;
832 /* Encode and return a source location from a given line and
833 column. */
835 source_location
836 linemap_position_for_line_and_column (line_maps *set,
837 const line_map_ordinary *ord_map,
838 linenum_type line,
839 unsigned column)
841 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
843 source_location r = MAP_START_LOCATION (ord_map);
844 r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
845 << ord_map->m_column_and_range_bits);
846 if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
847 r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
848 << ord_map->m_range_bits);
849 source_location upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
850 if (r >= upper_limit)
851 r = upper_limit - 1;
852 if (r > set->highest_location)
853 set->highest_location = r;
854 return r;
857 /* Encode and return a source_location starting from location LOC and
858 shifting it by COLUMN_OFFSET columns. This function does not support
859 virtual locations. */
861 source_location
862 linemap_position_for_loc_and_offset (struct line_maps *set,
863 source_location loc,
864 unsigned int column_offset)
866 const line_map_ordinary * map = NULL;
868 if (IS_ADHOC_LOC (loc))
869 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
871 /* This function does not support virtual locations yet. */
872 if (linemap_location_from_macro_expansion_p (set, loc))
873 return loc;
875 if (column_offset == 0
876 /* Adding an offset to a reserved location (like
877 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
878 sense. So let's leave the location intact in that case. */
879 || loc < RESERVED_LOCATION_COUNT)
880 return loc;
882 /* We find the real location and shift it. */
883 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
884 /* The new location (loc + offset) should be higher than the first
885 location encoded by MAP. This can fail if the line information
886 is messed up because of line directives (see PR66415). */
887 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
888 return loc;
890 linenum_type line = SOURCE_LINE (map, loc);
891 unsigned int column = SOURCE_COLUMN (map, loc);
893 /* If MAP is not the last line map of its set, then the new location
894 (loc + offset) should be less than the first location encoded by
895 the next line map of the set. Otherwise, we try to encode the
896 location in the next map. */
897 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
898 && (loc + (column_offset << map->m_range_bits)
899 >= MAP_START_LOCATION (&map[1])))
901 map = &map[1];
902 /* If the next map starts in a higher line, we cannot encode the
903 location there. */
904 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
905 return loc;
908 column += column_offset;
910 /* Bail out if the column is not representable within the existing
911 linemap. */
912 if (column >= (1u << (map->m_column_and_range_bits - map->m_range_bits)))
913 return loc;
915 source_location r =
916 linemap_position_for_line_and_column (set, map, line, column);
917 if (linemap_assert_fails (r <= set->highest_location)
918 || linemap_assert_fails (map == linemap_lookup (set, r)))
919 return loc;
921 return r;
924 /* Given a virtual source location yielded by a map (either an
925 ordinary or a macro map), returns that map. */
927 const struct line_map*
928 linemap_lookup (struct line_maps *set, source_location line)
930 if (IS_ADHOC_LOC (line))
931 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
932 if (linemap_location_from_macro_expansion_p (set, line))
933 return linemap_macro_map_lookup (set, line);
934 return linemap_ordinary_map_lookup (set, line);
937 /* Given a source location yielded by an ordinary map, returns that
938 map. Since the set is built chronologically, the logical lines are
939 monotonic increasing, and so the list is sorted and we can use a
940 binary search. */
942 static const line_map_ordinary *
943 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
945 unsigned int md, mn, mx;
946 const line_map_ordinary *cached, *result;
948 if (IS_ADHOC_LOC (line))
949 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
951 if (set == NULL || line < RESERVED_LOCATION_COUNT)
952 return NULL;
954 mn = LINEMAPS_ORDINARY_CACHE (set);
955 mx = LINEMAPS_ORDINARY_USED (set);
957 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
958 /* We should get a segfault if no line_maps have been added yet. */
959 if (line >= MAP_START_LOCATION (cached))
961 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
962 return cached;
964 else
966 mx = mn;
967 mn = 0;
970 while (mx - mn > 1)
972 md = (mn + mx) / 2;
973 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
974 mx = md;
975 else
976 mn = md;
979 LINEMAPS_ORDINARY_CACHE (set) = mn;
980 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
981 linemap_assert (line >= MAP_START_LOCATION (result));
982 return result;
985 /* Given a source location yielded by a macro map, returns that map.
986 Since the set is built chronologically, the logical lines are
987 monotonic decreasing, and so the list is sorted and we can use a
988 binary search. */
990 static const line_map_macro *
991 linemap_macro_map_lookup (struct line_maps *set, source_location line)
993 unsigned int md, mn, mx;
994 const struct line_map_macro *cached, *result;
996 if (IS_ADHOC_LOC (line))
997 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
999 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
1001 if (set == NULL)
1002 return NULL;
1004 mn = LINEMAPS_MACRO_CACHE (set);
1005 mx = LINEMAPS_MACRO_USED (set);
1006 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
1008 if (line >= MAP_START_LOCATION (cached))
1010 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
1011 return cached;
1012 mx = mn - 1;
1013 mn = 0;
1016 while (mn < mx)
1018 md = (mx + mn) / 2;
1019 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1020 mn = md + 1;
1021 else
1022 mx = md;
1025 LINEMAPS_MACRO_CACHE (set) = mx;
1026 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
1027 linemap_assert (MAP_START_LOCATION (result) <= line);
1029 return result;
1032 /* Return TRUE if MAP encodes locations coming from a macro
1033 replacement-list at macro expansion point. */
1035 bool
1036 linemap_macro_expansion_map_p (const struct line_map *map)
1038 return map && !MAP_ORDINARY_P (map);
1041 /* If LOCATION is the locus of a token in a replacement-list of a
1042 macro expansion return the location of the macro expansion point.
1044 Read the comments of struct line_map and struct line_map_macro in
1045 line-map.h to understand what a macro expansion point is. */
1047 static source_location
1048 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1049 source_location location ATTRIBUTE_UNUSED)
1051 linemap_assert (linemap_macro_expansion_map_p (map)
1052 && location >= MAP_START_LOCATION (map));
1054 /* Make sure LOCATION is correct. */
1055 linemap_assert ((location - MAP_START_LOCATION (map))
1056 < MACRO_MAP_NUM_MACRO_TOKENS (map));
1058 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1061 /* LOCATION is the source location of a token that belongs to a macro
1062 replacement-list as part of the macro expansion denoted by MAP.
1064 Return the location of the token at the definition point of the
1065 macro. */
1067 static source_location
1068 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1069 source_location location)
1071 unsigned token_no;
1073 linemap_assert (linemap_macro_expansion_map_p (map)
1074 && location >= MAP_START_LOCATION (map));
1075 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1077 token_no = location - MAP_START_LOCATION (map);
1078 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1080 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1082 return location;
1085 /* If LOCATION is the locus of a token that is an argument of a
1086 function-like macro M and appears in the expansion of M, return the
1087 locus of that argument in the context of the caller of M.
1089 In other words, this returns the xI location presented in the
1090 comments of line_map_macro above. */
1091 source_location
1092 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1093 const line_map_macro* map,
1094 source_location location)
1096 unsigned token_no;
1098 if (IS_ADHOC_LOC (location))
1099 location = get_location_from_adhoc_loc (set, location);
1101 linemap_assert (linemap_macro_expansion_map_p (map)
1102 && location >= MAP_START_LOCATION (map));
1103 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1104 linemap_assert (!IS_ADHOC_LOC (location));
1106 token_no = location - MAP_START_LOCATION (map);
1107 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1109 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1111 return location;
1114 /* Return the source line number corresponding to source location
1115 LOCATION. SET is the line map set LOCATION comes from. If
1116 LOCATION is the source location of token that is part of the
1117 replacement-list of a macro expansion return the line number of the
1118 macro expansion point. */
1121 linemap_get_expansion_line (struct line_maps *set,
1122 source_location location)
1124 const line_map_ordinary *map = NULL;
1126 if (IS_ADHOC_LOC (location))
1127 location = set->location_adhoc_data_map.data[location
1128 & MAX_SOURCE_LOCATION].locus;
1130 if (location < RESERVED_LOCATION_COUNT)
1131 return 0;
1133 location =
1134 linemap_macro_loc_to_exp_point (set, location, &map);
1136 return SOURCE_LINE (map, location);
1139 /* Return the path of the file corresponding to source code location
1140 LOCATION.
1142 If LOCATION is the source location of token that is part of the
1143 replacement-list of a macro expansion return the file path of the
1144 macro expansion point.
1146 SET is the line map set LOCATION comes from. */
1148 const char*
1149 linemap_get_expansion_filename (struct line_maps *set,
1150 source_location location)
1152 const struct line_map_ordinary *map = NULL;
1154 if (IS_ADHOC_LOC (location))
1155 location = set->location_adhoc_data_map.data[location
1156 & MAX_SOURCE_LOCATION].locus;
1158 if (location < RESERVED_LOCATION_COUNT)
1159 return NULL;
1161 location =
1162 linemap_macro_loc_to_exp_point (set, location, &map);
1164 return LINEMAP_FILE (map);
1167 /* Return the name of the macro associated to MACRO_MAP. */
1169 const char*
1170 linemap_map_get_macro_name (const line_map_macro *macro_map)
1172 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1173 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1176 /* Return a positive value if LOCATION is the locus of a token that is
1177 located in a system header, O otherwise. It returns 1 if LOCATION
1178 is the locus of a token that is located in a system header, and 2
1179 if LOCATION is the locus of a token located in a C system header
1180 that therefore needs to be extern "C" protected in C++.
1182 Note that this function returns 1 if LOCATION belongs to a token
1183 that is part of a macro replacement-list defined in a system
1184 header, but expanded in a non-system file. */
1187 linemap_location_in_system_header_p (struct line_maps *set,
1188 source_location location)
1190 const struct line_map *map = NULL;
1192 if (IS_ADHOC_LOC (location))
1193 location = set->location_adhoc_data_map.data[location
1194 & MAX_SOURCE_LOCATION].locus;
1196 if (location < RESERVED_LOCATION_COUNT)
1197 return false;
1199 /* Let's look at where the token for LOCATION comes from. */
1200 while (true)
1202 map = linemap_lookup (set, location);
1203 if (map != NULL)
1205 if (!linemap_macro_expansion_map_p (map))
1206 /* It's a normal token. */
1207 return LINEMAP_SYSP (linemap_check_ordinary (map));
1208 else
1210 const line_map_macro *macro_map = linemap_check_macro (map);
1212 /* It's a token resulting from a macro expansion. */
1213 source_location loc =
1214 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
1215 if (loc < RESERVED_LOCATION_COUNT)
1216 /* This token might come from a built-in macro. Let's
1217 look at where that macro got expanded. */
1218 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1219 else
1220 location = loc;
1223 else
1224 break;
1226 return false;
1229 /* Return TRUE if LOCATION is a source code location of a token that is part of
1230 a macro expansion, FALSE otherwise. */
1232 bool
1233 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1234 source_location location)
1236 if (IS_ADHOC_LOC (location))
1237 location = set->location_adhoc_data_map.data[location
1238 & MAX_SOURCE_LOCATION].locus;
1240 return location >= LINE_MAP_MAX_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 source_location *loc0,
1251 source_location *loc1)
1253 source_location l0 = *loc0, l1 = *loc1;
1254 const struct line_map *map0 = linemap_lookup (set, l0),
1255 *map1 = linemap_lookup (set, l1);
1257 while (linemap_macro_expansion_map_p (map0)
1258 && linemap_macro_expansion_map_p (map1)
1259 && (map0 != map1))
1261 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1263 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1264 l0);
1265 map0 = linemap_lookup (set, l0);
1267 else
1269 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1270 l1);
1271 map1 = linemap_lookup (set, l1);
1275 if (map0 == map1)
1277 *loc0 = l0;
1278 *loc1 = l1;
1279 return map0;
1281 return NULL;
1284 /* Given two virtual locations LOC0 and LOC1, return the first common
1285 macro map in their macro expansion histories. Return NULL if no
1286 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1287 virtual location of the token inside the resulting macro, upon
1288 return of a non-NULL result. */
1290 static const struct line_map*
1291 first_map_in_common (struct line_maps *set,
1292 source_location loc0,
1293 source_location loc1,
1294 source_location *res_loc0,
1295 source_location *res_loc1)
1297 *res_loc0 = loc0;
1298 *res_loc1 = loc1;
1300 return first_map_in_common_1 (set, res_loc0, res_loc1);
1303 /* Return a positive value if PRE denotes the location of a token that
1304 comes before the token of POST, 0 if PRE denotes the location of
1305 the same token as the token for POST, and a negative value
1306 otherwise. */
1309 linemap_compare_locations (struct line_maps *set,
1310 source_location pre,
1311 source_location post)
1313 bool pre_virtual_p, post_virtual_p;
1314 source_location l0 = pre, l1 = post;
1316 if (IS_ADHOC_LOC (l0))
1317 l0 = get_location_from_adhoc_loc (set, l0);
1318 if (IS_ADHOC_LOC (l1))
1319 l1 = get_location_from_adhoc_loc (set, l1);
1321 if (l0 == l1)
1322 return 0;
1324 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1325 l0 = linemap_resolve_location (set, l0,
1326 LRK_MACRO_EXPANSION_POINT,
1327 NULL);
1329 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1330 l1 = linemap_resolve_location (set, l1,
1331 LRK_MACRO_EXPANSION_POINT,
1332 NULL);
1334 if (l0 == l1
1335 && pre_virtual_p
1336 && post_virtual_p)
1338 /* So pre and post represent two tokens that are present in a
1339 same macro expansion. Let's see if the token for pre was
1340 before the token for post in that expansion. */
1341 unsigned i0, i1;
1342 const struct line_map *map =
1343 first_map_in_common (set, pre, post, &l0, &l1);
1345 if (map == NULL)
1346 /* This should not be possible. */
1347 abort ();
1349 i0 = l0 - MAP_START_LOCATION (map);
1350 i1 = l1 - MAP_START_LOCATION (map);
1351 return i1 - i0;
1354 if (IS_ADHOC_LOC (l0))
1355 l0 = get_location_from_adhoc_loc (set, l0);
1356 if (IS_ADHOC_LOC (l1))
1357 l1 = get_location_from_adhoc_loc (set, l1);
1359 return l1 - l0;
1362 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1364 static void
1365 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1367 unsigned int i = set->depth;
1369 while (--i)
1370 putc ('.', stderr);
1372 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1375 /* Return the spelling location of the token wherever it comes from,
1376 whether part of a macro definition or not.
1378 This is a subroutine for linemap_resolve_location. */
1380 static source_location
1381 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1382 source_location location,
1383 const line_map_ordinary **original_map)
1385 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1387 while (true)
1389 const struct line_map *map = linemap_lookup (set, location);
1390 if (!map || MAP_ORDINARY_P (map))
1392 if (original_map)
1393 *original_map = (const line_map_ordinary *)map;
1394 break;
1397 location = linemap_macro_map_loc_unwind_toward_spelling
1398 (set, linemap_check_macro (map), location);
1401 return location;
1404 /* If LOCATION is the source location of a token that belongs to a
1405 macro replacement-list -- as part of a macro expansion -- then
1406 return the location of the token at the definition point of the
1407 macro. Otherwise, return LOCATION. SET is the set of maps
1408 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1409 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1410 returned location comes from.
1412 This is a subroutine of linemap_resolve_location. */
1414 static source_location
1415 linemap_macro_loc_to_def_point (struct line_maps *set,
1416 source_location location,
1417 const line_map_ordinary **original_map)
1419 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1421 for (;;)
1423 source_location caret_loc = location;
1424 if (IS_ADHOC_LOC (caret_loc))
1425 caret_loc = get_location_from_adhoc_loc (set, caret_loc);
1427 const line_map *map = linemap_lookup (set, caret_loc);
1428 if (!map || MAP_ORDINARY_P (map))
1430 if (original_map)
1431 *original_map = (const line_map_ordinary *)map;
1432 break;
1435 location = linemap_macro_map_loc_to_def_point
1436 (linemap_check_macro (map), caret_loc);
1439 return location;
1442 /* If LOCATION is the source location of a token that belongs to a
1443 macro replacement-list -- at a macro expansion point -- then return
1444 the location of the topmost expansion point of the macro. We say
1445 topmost because if we are in the context of a nested macro
1446 expansion, the function returns the source location of the first
1447 macro expansion that triggered the nested expansions.
1449 Otherwise, return LOCATION. SET is the set of maps location come
1450 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1451 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1452 location comes from.
1454 This is a subroutine of linemap_resolve_location. */
1456 static source_location
1457 linemap_macro_loc_to_exp_point (struct line_maps *set,
1458 source_location location,
1459 const line_map_ordinary **original_map)
1461 struct line_map *map;
1463 if (IS_ADHOC_LOC (location))
1464 location = set->location_adhoc_data_map.data[location
1465 & MAX_SOURCE_LOCATION].locus;
1467 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1469 while (true)
1471 map = const_cast <line_map *> (linemap_lookup (set, location));
1472 if (!linemap_macro_expansion_map_p (map))
1473 break;
1474 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1475 location);
1478 if (original_map)
1479 *original_map = linemap_check_ordinary (map);
1480 return location;
1483 /* Resolve a virtual location into either a spelling location, an
1484 expansion point location or a token argument replacement point
1485 location. Return the map that encodes the virtual location as well
1486 as the resolved location.
1488 If LOC is *NOT* the location of a token resulting from the
1489 expansion of a macro, then the parameter LRK (which stands for
1490 Location Resolution Kind) is ignored and the resulting location
1491 just equals the one given in argument.
1493 Now if LOC *IS* the location of a token resulting from the
1494 expansion of a macro, this is what happens.
1496 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1497 -------------------------------
1499 The virtual location is resolved to the first macro expansion point
1500 that led to this macro expansion.
1502 * If LRK is set to LRK_SPELLING_LOCATION
1503 -------------------------------------
1505 The virtual location is resolved to the locus where the token has
1506 been spelled in the source. This can follow through all the macro
1507 expansions that led to the token.
1509 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1510 --------------------------------------
1512 The virtual location is resolved to the locus of the token in the
1513 context of the macro definition.
1515 If LOC is the locus of a token that is an argument of a
1516 function-like macro [replacing a parameter in the replacement list
1517 of the macro] the virtual location is resolved to the locus of the
1518 parameter that is replaced, in the context of the definition of the
1519 macro.
1521 If LOC is the locus of a token that is not an argument of a
1522 function-like macro, then the function behaves as if LRK was set to
1523 LRK_SPELLING_LOCATION.
1525 If MAP is not NULL, *MAP is set to the map encoding the
1526 returned location. Note that if the returned location wasn't originally
1527 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1528 resolves to a location reserved for the client code, like
1529 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1531 source_location
1532 linemap_resolve_location (struct line_maps *set,
1533 source_location loc,
1534 enum location_resolution_kind lrk,
1535 const line_map_ordinary **map)
1537 source_location locus = loc;
1538 if (IS_ADHOC_LOC (loc))
1539 locus = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1541 if (locus < RESERVED_LOCATION_COUNT)
1543 /* A reserved location wasn't encoded in a map. Let's return a
1544 NULL map here, just like what linemap_ordinary_map_lookup
1545 does. */
1546 if (map)
1547 *map = NULL;
1548 return loc;
1551 switch (lrk)
1553 case LRK_MACRO_EXPANSION_POINT:
1554 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1555 break;
1556 case LRK_SPELLING_LOCATION:
1557 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1558 break;
1559 case LRK_MACRO_DEFINITION_LOCATION:
1560 loc = linemap_macro_loc_to_def_point (set, loc, map);
1561 break;
1562 default:
1563 abort ();
1565 return loc;
1568 /* TRUE if LOCATION is a source code location of a token that is part of the
1569 definition of a macro, FALSE otherwise. */
1571 bool
1572 linemap_location_from_macro_definition_p (struct line_maps *set,
1573 source_location loc)
1575 if (IS_ADHOC_LOC (loc))
1576 loc = get_location_from_adhoc_loc (set, loc);
1578 if (!linemap_location_from_macro_expansion_p (set, loc))
1579 return false;
1581 while (true)
1583 const struct line_map_macro *map
1584 = linemap_check_macro (linemap_lookup (set, loc));
1586 source_location s_loc
1587 = linemap_macro_map_loc_unwind_toward_spelling (set, map, loc);
1588 if (linemap_location_from_macro_expansion_p (set, s_loc))
1589 loc = s_loc;
1590 else
1592 source_location def_loc
1593 = linemap_macro_map_loc_to_def_point (map, loc);
1594 return s_loc == def_loc;
1600 Suppose that LOC is the virtual location of a token T coming from
1601 the expansion of a macro M. This function then steps up to get the
1602 location L of the point where M got expanded. If L is a spelling
1603 location inside a macro expansion M', then this function returns
1604 the locus of the point where M' was expanded. Said otherwise, this
1605 function returns the location of T in the context that triggered
1606 the expansion of M.
1608 *LOC_MAP must be set to the map of LOC. This function then sets it
1609 to the map of the returned location. */
1611 source_location
1612 linemap_unwind_toward_expansion (struct line_maps *set,
1613 source_location loc,
1614 const struct line_map **map)
1616 source_location resolved_location;
1617 const line_map_macro *macro_map = linemap_check_macro (*map);
1618 const struct line_map *resolved_map;
1620 if (IS_ADHOC_LOC (loc))
1621 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1623 resolved_location =
1624 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1625 resolved_map = linemap_lookup (set, resolved_location);
1627 if (!linemap_macro_expansion_map_p (resolved_map))
1629 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1630 resolved_map = linemap_lookup (set, resolved_location);
1633 *map = resolved_map;
1634 return resolved_location;
1637 /* If LOC is the virtual location of a token coming from the expansion
1638 of a macro M and if its spelling location is reserved (e.g, a
1639 location for a built-in token), then this function unwinds (using
1640 linemap_unwind_toward_expansion) the location until a location that
1641 is not reserved and is not in a system header is reached. In other
1642 words, this unwinds the reserved location until a location that is
1643 in real source code is reached.
1645 Otherwise, if the spelling location for LOC is not reserved or if
1646 LOC doesn't come from the expansion of a macro, the function
1647 returns LOC as is and *MAP is not touched.
1649 *MAP is set to the map of the returned location if the later is
1650 different from LOC. */
1651 source_location
1652 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1653 source_location loc,
1654 const struct line_map **map)
1656 source_location resolved_loc;
1657 const struct line_map *map0 = NULL;
1658 const line_map_ordinary *map1 = NULL;
1660 if (IS_ADHOC_LOC (loc))
1661 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1663 map0 = linemap_lookup (set, loc);
1664 if (!linemap_macro_expansion_map_p (map0))
1665 return loc;
1667 resolved_loc = linemap_resolve_location (set, loc,
1668 LRK_SPELLING_LOCATION,
1669 &map1);
1671 if (resolved_loc >= RESERVED_LOCATION_COUNT
1672 && !LINEMAP_SYSP (map1))
1673 return loc;
1675 while (linemap_macro_expansion_map_p (map0)
1676 && (resolved_loc < RESERVED_LOCATION_COUNT
1677 || LINEMAP_SYSP (map1)))
1679 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1680 resolved_loc = linemap_resolve_location (set, loc,
1681 LRK_SPELLING_LOCATION,
1682 &map1);
1685 if (map != NULL)
1686 *map = map0;
1687 return loc;
1690 /* Expand source code location LOC and return a user readable source
1691 code location. LOC must be a spelling (non-virtual) location. If
1692 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1693 location is returned. */
1695 expanded_location
1696 linemap_expand_location (struct line_maps *set,
1697 const struct line_map *map,
1698 source_location loc)
1701 expanded_location xloc;
1703 memset (&xloc, 0, sizeof (xloc));
1704 if (IS_ADHOC_LOC (loc))
1706 xloc.data
1707 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1708 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1711 if (loc < RESERVED_LOCATION_COUNT)
1712 /* The location for this token wasn't generated from a line map.
1713 It was probably a location for a builtin token, chosen by some
1714 client code. Let's not try to expand the location in that
1715 case. */;
1716 else if (map == NULL)
1717 /* We shouldn't be getting a NULL map with a location that is not
1718 reserved by the client code. */
1719 abort ();
1720 else
1722 /* MAP must be an ordinary map and LOC must be non-virtual,
1723 encoded into this map, obviously; the accessors used on MAP
1724 below ensure it is ordinary. Let's just assert the
1725 non-virtualness of LOC here. */
1726 if (linemap_location_from_macro_expansion_p (set, loc))
1727 abort ();
1729 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1731 xloc.file = LINEMAP_FILE (ord_map);
1732 xloc.line = SOURCE_LINE (ord_map, loc);
1733 xloc.column = SOURCE_COLUMN (ord_map, loc);
1734 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1737 return xloc;
1741 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1742 is NULL, use stderr. IS_MACRO is true if the caller wants to
1743 dump a macro map, false otherwise. */
1745 void
1746 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1748 const char *const lc_reasons_v[LC_HWM]
1749 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1750 "LC_ENTER_MACRO" };
1751 const line_map *map;
1752 unsigned reason;
1754 if (stream == NULL)
1755 stream = stderr;
1757 if (!is_macro)
1759 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1760 reason = linemap_check_ordinary (map)->reason;
1762 else
1764 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1765 reason = LC_ENTER_MACRO;
1768 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1769 ix, (void *) map, map->start_location,
1770 reason < LC_HWM ? lc_reasons_v[reason] : "???",
1771 ((!is_macro
1772 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1773 ? "yes" : "no"));
1774 if (!is_macro)
1776 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1777 const line_map_ordinary *includer_map
1778 = linemap_included_from_linemap (set, ord_map);
1780 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1781 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1782 fprintf (stream, "Included from: [%d] %s\n",
1783 includer_map ? int (includer_map - set->info_ordinary.maps) : -1,
1784 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1786 else
1788 const line_map_macro *macro_map = linemap_check_macro (map);
1789 fprintf (stream, "Macro: %s (%u tokens)\n",
1790 linemap_map_get_macro_name (macro_map),
1791 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1794 fprintf (stream, "\n");
1798 /* Dump debugging information about source location LOC into the file
1799 stream STREAM. SET is the line map set LOC comes from. */
1801 void
1802 linemap_dump_location (struct line_maps *set,
1803 source_location loc,
1804 FILE *stream)
1806 const line_map_ordinary *map;
1807 source_location location;
1808 const char *path = "", *from = "";
1809 int l = -1, c = -1, s = -1, e = -1;
1811 if (IS_ADHOC_LOC (loc))
1812 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1814 if (loc == 0)
1815 return;
1817 location =
1818 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1820 if (map == NULL)
1821 /* Only reserved locations can be tolerated in this case. */
1822 linemap_assert (location < RESERVED_LOCATION_COUNT);
1823 else
1825 path = LINEMAP_FILE (map);
1826 l = SOURCE_LINE (map, location);
1827 c = SOURCE_COLUMN (map, location);
1828 s = LINEMAP_SYSP (map) != 0;
1829 e = location != loc;
1830 if (e)
1831 from = "N/A";
1832 else
1834 const line_map_ordinary *from_map
1835 = linemap_included_from_linemap (set, map);
1836 from = from_map ? LINEMAP_FILE (from_map) : "<NULL>";
1840 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1841 E: macro expansion?, LOC: original location, R: resolved location */
1842 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1843 path, from, l, c, s, (void*)map, e, loc, location);
1846 /* Return the highest location emitted for a given file for which
1847 there is a line map in SET. FILE_NAME is the file name to
1848 consider. If the function returns TRUE, *LOC is set to the highest
1849 location emitted for that file. */
1851 bool
1852 linemap_get_file_highest_location (struct line_maps *set,
1853 const char *file_name,
1854 source_location *loc)
1856 /* If the set is empty or no ordinary map has been created then
1857 there is no file to look for ... */
1858 if (set == NULL || set->info_ordinary.used == 0)
1859 return false;
1861 /* Now look for the last ordinary map created for FILE_NAME. */
1862 int i;
1863 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1865 const char *fname = set->info_ordinary.maps[i].to_file;
1866 if (fname && !filename_cmp (fname, file_name))
1867 break;
1870 if (i < 0)
1871 return false;
1873 /* The highest location for a given map is either the starting
1874 location of the next map minus one, or -- if the map is the
1875 latest one -- the highest location of the set. */
1876 source_location result;
1877 if (i == (int) set->info_ordinary.used - 1)
1878 result = set->highest_location;
1879 else
1880 result = set->info_ordinary.maps[i + 1].start_location - 1;
1882 *loc = result;
1883 return true;
1886 /* Compute and return statistics about the memory consumption of some
1887 parts of the line table SET. */
1889 void
1890 linemap_get_statistics (struct line_maps *set,
1891 struct linemap_stats *s)
1893 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1894 macro_maps_allocated_size, macro_maps_used_size,
1895 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1897 const line_map_macro *cur_map;
1899 ordinary_maps_allocated_size =
1900 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1902 ordinary_maps_used_size =
1903 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1905 macro_maps_allocated_size =
1906 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1908 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1909 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1910 ++cur_map)
1912 unsigned i;
1914 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1916 macro_maps_locations_size +=
1917 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1919 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1921 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1922 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1923 duplicated_macro_maps_locations_size +=
1924 sizeof (source_location);
1928 macro_maps_used_size =
1929 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1931 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1932 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1933 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1934 s->ordinary_maps_used_size = ordinary_maps_used_size;
1935 s->num_expanded_macros = num_expanded_macros_counter;
1936 s->num_macro_tokens = num_macro_tokens_counter;
1937 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1938 s->macro_maps_allocated_size = macro_maps_allocated_size;
1939 s->macro_maps_locations_size = macro_maps_locations_size;
1940 s->macro_maps_used_size = macro_maps_used_size;
1941 s->duplicated_macro_maps_locations_size =
1942 duplicated_macro_maps_locations_size;
1943 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1944 * sizeof (struct location_adhoc_data));
1945 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
1949 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1950 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1951 specifies how many macro maps to dump. */
1953 void
1954 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1955 unsigned int num_macro)
1957 unsigned int i;
1959 if (set == NULL)
1960 return;
1962 if (stream == NULL)
1963 stream = stderr;
1965 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1966 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1967 fprintf (stream, "Include stack depth: %d\n", set->depth);
1968 fprintf (stream, "Highest location: %u\n", set->highest_location);
1970 if (num_ordinary)
1972 fprintf (stream, "\nOrdinary line maps\n");
1973 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1974 linemap_dump (stream, set, i, false);
1975 fprintf (stream, "\n");
1978 if (num_macro)
1980 fprintf (stream, "\nMacro line maps\n");
1981 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1982 linemap_dump (stream, set, i, true);
1983 fprintf (stream, "\n");
1987 /* class rich_location. */
1989 /* Construct a rich_location with location LOC as its initial range. */
1991 rich_location::rich_location (line_maps *set, source_location loc,
1992 const range_label *label) :
1993 m_line_table (set),
1994 m_ranges (),
1995 m_column_override (0),
1996 m_have_expanded_location (false),
1997 m_fixit_hints (),
1998 m_seen_impossible_fixit (false),
1999 m_fixits_cannot_be_auto_applied (false)
2001 add_range (loc, true, label);
2004 /* The destructor for class rich_location. */
2006 rich_location::~rich_location ()
2008 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2009 delete get_fixit_hint (i);
2012 /* Get location IDX within this rich_location. */
2014 source_location
2015 rich_location::get_loc (unsigned int idx) const
2017 const location_range *locrange = get_range (idx);
2018 return locrange->m_loc;
2021 /* Get range IDX within this rich_location. */
2023 const location_range *
2024 rich_location::get_range (unsigned int idx) const
2026 return &m_ranges[idx];
2029 /* Mutable access to range IDX within this rich_location. */
2031 location_range *
2032 rich_location::get_range (unsigned int idx)
2034 return &m_ranges[idx];
2037 /* Expand location IDX within this rich_location. */
2038 /* Get an expanded_location for this rich_location's primary
2039 location. */
2041 expanded_location
2042 rich_location::get_expanded_location (unsigned int idx)
2044 if (idx == 0)
2046 /* Cache the expansion of the primary location. */
2047 if (!m_have_expanded_location)
2049 m_expanded_location
2050 = linemap_client_expand_location_to_spelling_point
2051 (get_loc (0), LOCATION_ASPECT_CARET);
2052 if (m_column_override)
2053 m_expanded_location.column = m_column_override;
2054 m_have_expanded_location = true;
2057 return m_expanded_location;
2059 else
2060 return linemap_client_expand_location_to_spelling_point
2061 (get_loc (idx), LOCATION_ASPECT_CARET);
2064 /* Set the column of the primary location, with 0 meaning
2065 "don't override it". */
2067 void
2068 rich_location::override_column (int column)
2070 m_column_override = column;
2071 m_have_expanded_location = false;
2074 /* Add the given range. */
2076 void
2077 rich_location::add_range (source_location loc, bool show_caret_p,
2078 const range_label *label)
2080 location_range range;
2081 range.m_loc = loc;
2082 range.m_show_caret_p = show_caret_p;
2083 range.m_label = label;
2084 m_ranges.push (range);
2087 /* Add or overwrite the location given by IDX, setting its location to LOC,
2088 and setting its "should my caret be printed" flag to SHOW_CARET_P.
2090 It must either overwrite an existing location, or add one *exactly* on
2091 the end of the array.
2093 This is primarily for use by gcc when implementing diagnostic format
2094 decoders e.g.
2095 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2096 (which writes the source location of a tree back into location 0 of
2097 the rich_location), and
2098 - the "%C" and "%L" format codes in the Fortran frontend. */
2100 void
2101 rich_location::set_range (unsigned int idx, source_location loc,
2102 bool show_caret_p)
2104 /* We can either overwrite an existing range, or add one exactly
2105 on the end of the array. */
2106 linemap_assert (idx <= m_ranges.count ());
2108 if (idx == m_ranges.count ())
2109 add_range (loc, show_caret_p);
2110 else
2112 location_range *locrange = get_range (idx);
2113 locrange->m_loc = loc;
2114 locrange->m_show_caret_p = show_caret_p;
2117 if (idx == 0)
2118 /* Mark any cached value here as dirty. */
2119 m_have_expanded_location = false;
2122 /* Methods for adding insertion fix-it hints. */
2124 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2125 immediately before the primary range's start location. */
2127 void
2128 rich_location::add_fixit_insert_before (const char *new_content)
2130 add_fixit_insert_before (get_loc (), new_content);
2133 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2134 immediately before the start of WHERE. */
2136 void
2137 rich_location::add_fixit_insert_before (source_location where,
2138 const char *new_content)
2140 source_location start = get_range_from_loc (m_line_table, where).m_start;
2141 maybe_add_fixit (start, start, new_content);
2144 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2145 immediately after the primary range's end-point. */
2147 void
2148 rich_location::add_fixit_insert_after (const char *new_content)
2150 add_fixit_insert_after (get_loc (), new_content);
2153 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2154 immediately after the end-point of WHERE. */
2156 void
2157 rich_location::add_fixit_insert_after (source_location where,
2158 const char *new_content)
2160 source_location finish = get_range_from_loc (m_line_table, where).m_finish;
2161 source_location next_loc
2162 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2164 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2165 its input value. */
2166 if (next_loc == finish)
2168 stop_supporting_fixits ();
2169 return;
2172 maybe_add_fixit (next_loc, next_loc, new_content);
2175 /* Methods for adding removal fix-it hints. */
2177 /* Add a fixit-hint, suggesting removal of the content covered
2178 by range 0. */
2180 void
2181 rich_location::add_fixit_remove ()
2183 add_fixit_remove (get_loc ());
2186 /* Add a fixit-hint, suggesting removal of the content between
2187 the start and finish of WHERE. */
2189 void
2190 rich_location::add_fixit_remove (source_location where)
2192 source_range range = get_range_from_loc (m_line_table, where);
2193 add_fixit_remove (range);
2196 /* Add a fixit-hint, suggesting removal of the content at
2197 SRC_RANGE. */
2199 void
2200 rich_location::add_fixit_remove (source_range src_range)
2202 add_fixit_replace (src_range, "");
2205 /* Add a fixit-hint, suggesting replacement of the content covered
2206 by range 0 with NEW_CONTENT. */
2208 void
2209 rich_location::add_fixit_replace (const char *new_content)
2211 add_fixit_replace (get_loc (), new_content);
2214 /* Methods for adding "replace" fix-it hints. */
2216 /* Add a fixit-hint, suggesting replacement of the content between
2217 the start and finish of WHERE with NEW_CONTENT. */
2219 void
2220 rich_location::add_fixit_replace (source_location where,
2221 const char *new_content)
2223 source_range range = get_range_from_loc (m_line_table, where);
2224 add_fixit_replace (range, new_content);
2227 /* Add a fixit-hint, suggesting replacement of the content at
2228 SRC_RANGE with NEW_CONTENT. */
2230 void
2231 rich_location::add_fixit_replace (source_range src_range,
2232 const char *new_content)
2234 source_location start = get_pure_location (m_line_table, src_range.m_start);
2235 source_location finish = get_pure_location (m_line_table, src_range.m_finish);
2237 /* Fix-it hints use half-closed ranges, so attempt to offset the endpoint. */
2238 source_location next_loc
2239 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2240 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2241 its input value. */
2242 if (next_loc == finish)
2244 stop_supporting_fixits ();
2245 return;
2247 finish = next_loc;
2249 maybe_add_fixit (start, finish, new_content);
2252 /* Get the last fix-it hint within this rich_location, or NULL if none. */
2254 fixit_hint *
2255 rich_location::get_last_fixit_hint () const
2257 if (m_fixit_hints.count () > 0)
2258 return get_fixit_hint (m_fixit_hints.count () - 1);
2259 else
2260 return NULL;
2263 /* If WHERE is an "awkward" location, then mark this rich_location as not
2264 supporting fixits, purging any thay were already added, and return true.
2266 Otherwise (the common case), return false. */
2268 bool
2269 rich_location::reject_impossible_fixit (source_location where)
2271 /* Fix-its within a rich_location should either all be suggested, or
2272 none of them should be suggested.
2273 Once we've rejected a fixit, we reject any more, even those
2274 with reasonable locations. */
2275 if (m_seen_impossible_fixit)
2276 return true;
2278 if (where <= LINE_MAP_MAX_LOCATION_WITH_COLS)
2279 /* WHERE is a reasonable location for a fix-it; don't reject it. */
2280 return false;
2282 /* Otherwise we have an attempt to add a fix-it with an "awkward"
2283 location: either one that we can't obtain column information
2284 for (within an ordinary map), or one within a macro expansion. */
2285 stop_supporting_fixits ();
2286 return true;
2289 /* Mark this rich_location as not supporting fixits, purging any that were
2290 already added. */
2292 void
2293 rich_location::stop_supporting_fixits ()
2295 m_seen_impossible_fixit = true;
2297 /* Purge the rich_location of any fix-its that were already added. */
2298 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2299 delete get_fixit_hint (i);
2300 m_fixit_hints.truncate (0);
2303 /* Add HINT to the fix-it hints in this rich_location,
2304 consolidating into the prior fixit if possible. */
2306 void
2307 rich_location::maybe_add_fixit (source_location start,
2308 source_location next_loc,
2309 const char *new_content)
2311 if (reject_impossible_fixit (start))
2312 return;
2313 if (reject_impossible_fixit (next_loc))
2314 return;
2316 /* Only allow fix-it hints that affect a single line in one file.
2317 Compare the end-points. */
2318 expanded_location exploc_start
2319 = linemap_client_expand_location_to_spelling_point (start,
2320 LOCATION_ASPECT_START);
2321 expanded_location exploc_next_loc
2322 = linemap_client_expand_location_to_spelling_point (next_loc,
2323 LOCATION_ASPECT_START);
2324 /* They must be within the same file... */
2325 if (exploc_start.file != exploc_next_loc.file)
2327 stop_supporting_fixits ();
2328 return;
2330 /* ...and on the same line. */
2331 if (exploc_start.line != exploc_next_loc.line)
2333 stop_supporting_fixits ();
2334 return;
2336 /* The columns must be in the correct order. This can fail if the
2337 endpoints straddle the boundary for which the linemap can represent
2338 columns (PR c/82050). */
2339 if (exploc_start.column > exploc_next_loc.column)
2341 stop_supporting_fixits ();
2342 return;
2345 const char *newline = strchr (new_content, '\n');
2346 if (newline)
2348 /* For now, we can only support insertion of whole lines
2349 i.e. starts at start of line, and the newline is at the end of
2350 the insertion point. */
2352 /* It must be an insertion, not a replacement/deletion. */
2353 if (start != next_loc)
2355 stop_supporting_fixits ();
2356 return;
2359 /* The insertion must be at the start of a line. */
2360 if (exploc_start.column != 1)
2362 stop_supporting_fixits ();
2363 return;
2366 /* The newline must be at end of NEW_CONTENT.
2367 We could eventually split up fix-its at newlines if we wanted
2368 to allow more generality (e.g. to allow adding multiple lines
2369 with one add_fixit call. */
2370 if (newline[1] != '\0')
2372 stop_supporting_fixits ();
2373 return;
2377 /* Consolidate neighboring fixits.
2378 Don't consolidate into newline-insertion fixits. */
2379 fixit_hint *prev = get_last_fixit_hint ();
2380 if (prev && !prev->ends_with_newline_p ())
2381 if (prev->maybe_append (start, next_loc, new_content))
2382 return;
2384 m_fixit_hints.push (new fixit_hint (start, next_loc, new_content));
2387 /* class fixit_hint. */
2389 fixit_hint::fixit_hint (source_location start,
2390 source_location next_loc,
2391 const char *new_content)
2392 : m_start (start),
2393 m_next_loc (next_loc),
2394 m_bytes (xstrdup (new_content)),
2395 m_len (strlen (new_content))
2399 /* Does this fix-it hint affect the given line? */
2401 bool
2402 fixit_hint::affects_line_p (const char *file, int line) const
2404 expanded_location exploc_start
2405 = linemap_client_expand_location_to_spelling_point (m_start,
2406 LOCATION_ASPECT_START);
2407 if (file != exploc_start.file)
2408 return false;
2409 if (line < exploc_start.line)
2410 return false;
2411 expanded_location exploc_next_loc
2412 = linemap_client_expand_location_to_spelling_point (m_next_loc,
2413 LOCATION_ASPECT_START);
2414 if (file != exploc_next_loc.file)
2415 return false;
2416 if (line > exploc_next_loc.line)
2417 return false;
2418 return true;
2421 /* Method for consolidating fix-it hints, for use by
2422 rich_location::maybe_add_fixit.
2423 If possible, merge a pending fix-it hint with the given params
2424 into this one and return true.
2425 Otherwise return false. */
2427 bool
2428 fixit_hint::maybe_append (source_location start,
2429 source_location next_loc,
2430 const char *new_content)
2432 /* For consolidation to be possible, START must be at this hint's
2433 m_next_loc. */
2434 if (start != m_next_loc)
2435 return false;
2437 /* If so, we have neighboring replacements; merge them. */
2438 m_next_loc = next_loc;
2439 size_t extra_len = strlen (new_content);
2440 m_bytes = (char *)xrealloc (m_bytes, m_len + extra_len + 1);
2441 memcpy (m_bytes + m_len, new_content, extra_len);
2442 m_len += extra_len;
2443 m_bytes[m_len] = '\0';
2444 return true;
2447 /* Return true iff this hint's content ends with a newline. */
2449 bool
2450 fixit_hint::ends_with_newline_p () const
2452 if (m_len == 0)
2453 return false;
2454 return m_bytes[m_len - 1] == '\n';