c++: check DECL_INITIAL for constexpr
[official-gcc.git] / libcpp / line-map.cc
blob50e8043255ecf607bab5b8265d2ea137fd82cdf1
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2022 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 line_maps *, const line_map_ordinary *);
30 static const line_map_ordinary * linemap_ordinary_map_lookup (const line_maps *,
31 location_t);
32 static const line_map_macro* linemap_macro_map_lookup (const 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 (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.cc. */
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
71 + lb->discriminator);
74 /* Compare function for location_adhoc_data hashtable. */
76 static int
77 location_adhoc_data_eq (const void *l1, const void *l2)
79 const struct location_adhoc_data *lb1 =
80 (const struct location_adhoc_data *) l1;
81 const struct location_adhoc_data *lb2 =
82 (const struct location_adhoc_data *) l2;
83 return (lb1->locus == lb2->locus
84 && lb1->src_range.m_start == lb2->src_range.m_start
85 && lb1->src_range.m_finish == lb2->src_range.m_finish
86 && lb1->data == lb2->data
87 && lb1->discriminator == lb2->discriminator);
90 /* Update the hashtable when location_adhoc_data_map::data is reallocated.
91 The param is an array of two pointers, the previous value of the data
92 pointer, and then the new value. The pointers stored in the hash map
93 are then rebased to be relative to the new data pointer instead of the
94 old one. */
96 static int
97 location_adhoc_data_update (void **slot_v, void *param_v)
99 const auto slot = reinterpret_cast<location_adhoc_data **> (slot_v);
100 const auto param = static_cast<location_adhoc_data **> (param_v);
101 *slot = (*slot - param[0]) + param[1];
102 return 1;
105 /* The adhoc data hash table is not part of the GGC infrastructure, so it was
106 not initialized when SET was reconstructed from PCH; take care of that by
107 rebuilding it from scratch. */
109 void
110 rebuild_location_adhoc_htab (line_maps *set)
112 set->location_adhoc_data_map.htab =
113 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
114 for (auto p = set->location_adhoc_data_map.data,
115 end = p + set->location_adhoc_data_map.curr_loc;
116 p != end; ++p)
118 const auto slot = reinterpret_cast<location_adhoc_data **>
119 (htab_find_slot (set->location_adhoc_data_map.htab, p, INSERT));
120 *slot = p;
124 /* Helper function for get_combined_adhoc_loc.
125 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
126 within a location_t, without needing to use an ad-hoc location. */
128 static bool
129 can_be_stored_compactly_p (line_maps *set,
130 location_t locus,
131 source_range src_range,
132 void *data,
133 unsigned discriminator)
135 /* If there's an ad-hoc pointer, we can't store it directly in the
136 location_t, we need the lookaside. */
137 if (data)
138 return false;
140 if (discriminator != 0)
141 return false;
143 /* We only store ranges that begin at the locus and that are sufficiently
144 "sane". */
145 if (src_range.m_start != locus)
146 return false;
148 if (src_range.m_finish < src_range.m_start)
149 return false;
151 if (src_range.m_start < RESERVED_LOCATION_COUNT)
152 return false;
154 if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
155 return false;
157 /* All 3 locations must be within ordinary maps, typically, the same
158 ordinary map. */
159 location_t lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
160 if (locus >= lowest_macro_loc)
161 return false;
162 if (src_range.m_start >= lowest_macro_loc)
163 return false;
164 if (src_range.m_finish >= lowest_macro_loc)
165 return false;
167 /* Passed all tests. */
168 return true;
171 /* Combine LOCUS and DATA to a combined adhoc loc. */
173 location_t
174 get_combined_adhoc_loc (line_maps *set,
175 location_t locus,
176 source_range src_range,
177 void *data,
178 unsigned discriminator)
180 struct location_adhoc_data lb;
181 struct location_adhoc_data **slot;
183 if (IS_ADHOC_LOC (locus))
184 locus = get_location_from_adhoc_loc (set, locus);
185 if (locus == 0 && data == NULL)
186 return 0;
188 /* Any ordinary locations ought to be "pure" at this point: no
189 compressed ranges. */
190 linemap_assert (locus < RESERVED_LOCATION_COUNT
191 || locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
192 || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
193 || pure_location_p (set, locus));
195 /* Consider short-range optimization. */
196 if (can_be_stored_compactly_p (set, locus, src_range, data, discriminator))
198 /* The low bits ought to be clear. */
199 linemap_assert (pure_location_p (set, locus));
200 const line_map *map = linemap_lookup (set, locus);
201 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
202 unsigned int int_diff = src_range.m_finish - src_range.m_start;
203 unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
204 if (col_diff < (1U << ordmap->m_range_bits))
206 location_t packed = locus | col_diff;
207 set->num_optimized_ranges++;
208 return packed;
212 /* We can also compactly store locations
213 when locus == start == finish (and data is NULL). */
214 if (locus == src_range.m_start
215 && locus == src_range.m_finish
216 && !data && discriminator == 0)
217 return locus;
219 if (!data && discriminator == 0)
220 set->num_unoptimized_ranges++;
222 lb.locus = locus;
223 lb.src_range = src_range;
224 lb.data = data;
225 lb.discriminator = discriminator;
226 slot = (struct location_adhoc_data **)
227 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
228 if (*slot == NULL)
230 if (set->location_adhoc_data_map.curr_loc >=
231 set->location_adhoc_data_map.allocated)
233 const auto orig_data = set->location_adhoc_data_map.data;
234 /* Cast away extern "C" from the type of xrealloc. */
235 line_map_realloc reallocator = (set->reallocator
236 ? set->reallocator
237 : (line_map_realloc) xrealloc);
239 if (set->location_adhoc_data_map.allocated == 0)
240 set->location_adhoc_data_map.allocated = 128;
241 else
242 set->location_adhoc_data_map.allocated *= 2;
243 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
244 reallocator (set->location_adhoc_data_map.data,
245 set->location_adhoc_data_map.allocated
246 * sizeof (struct location_adhoc_data));
247 if (set->location_adhoc_data_map.allocated > 128)
249 location_adhoc_data *param[2]
250 = {orig_data, set->location_adhoc_data_map.data};
251 htab_traverse (set->location_adhoc_data_map.htab,
252 location_adhoc_data_update, param);
255 *slot = set->location_adhoc_data_map.data
256 + set->location_adhoc_data_map.curr_loc;
257 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
258 = lb;
260 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
263 /* Return the data for the adhoc loc. */
265 void *
266 get_data_from_adhoc_loc (const class line_maps *set, location_t loc)
268 linemap_assert (IS_ADHOC_LOC (loc));
269 return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].data;
272 unsigned
273 get_discriminator_from_adhoc_loc (const class line_maps *set, location_t loc)
275 linemap_assert (IS_ADHOC_LOC (loc));
276 return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].discriminator;
279 /* Return the location for the adhoc loc. */
281 location_t
282 get_location_from_adhoc_loc (const class line_maps *set, location_t loc)
284 linemap_assert (IS_ADHOC_LOC (loc));
285 return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].locus;
288 /* Return the source_range for adhoc location LOC. */
290 static source_range
291 get_range_from_adhoc_loc (const class line_maps *set, location_t loc)
293 linemap_assert (IS_ADHOC_LOC (loc));
294 return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].src_range;
297 /* Get the source_range of location LOC, either from the ad-hoc
298 lookaside table, or embedded inside LOC itself. */
300 source_range
301 get_range_from_loc (line_maps *set,
302 location_t loc)
304 if (IS_ADHOC_LOC (loc))
305 return get_range_from_adhoc_loc (set, loc);
307 /* For ordinary maps, extract packed range. */
308 if (loc >= RESERVED_LOCATION_COUNT
309 && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
310 && loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
312 const line_map *map = linemap_lookup (set, loc);
313 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
314 source_range result;
315 int offset = loc & ((1 << ordmap->m_range_bits) - 1);
316 result.m_start = loc - offset;
317 result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
318 return result;
321 return source_range::from_location (loc);
324 unsigned
325 get_discriminator_from_loc (line_maps *set,
326 location_t loc)
328 if (IS_ADHOC_LOC (loc))
329 return get_discriminator_from_adhoc_loc (set, loc);
330 return 0;
333 /* Get whether location LOC is a "pure" location, or
334 whether it is an ad-hoc location, or embeds range information. */
336 bool
337 pure_location_p (line_maps *set, location_t loc)
339 if (IS_ADHOC_LOC (loc))
340 return false;
342 const line_map *map = linemap_lookup (set, loc);
343 if (map == NULL)
344 return true;
345 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
347 if (loc & ((1U << ordmap->m_range_bits) - 1))
348 return false;
350 return true;
353 /* Given location LOC within SET, strip away any packed range information
354 or ad-hoc information. */
356 location_t
357 get_pure_location (line_maps *set, location_t loc)
359 if (IS_ADHOC_LOC (loc))
360 loc = get_location_from_adhoc_loc (set, loc);
362 if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
363 return loc;
365 if (loc < RESERVED_LOCATION_COUNT)
366 return loc;
368 const line_map *map = linemap_lookup (set, loc);
369 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
371 return loc & ~((1 << ordmap->m_range_bits) - 1);
374 /* Initialize a line map set. */
376 void
377 linemap_init (line_maps *set,
378 location_t builtin_location)
380 #if __GNUC__ == 4 && __GNUC_MINOR__ == 2 && !defined (__clang__)
381 /* PR33916, needed to fix PR82939. */
382 memset (set, 0, sizeof (line_maps));
383 #else
384 new (set) line_maps();
385 #endif
386 /* Set default reallocator (used for initial alloc too). */
387 set->reallocator = xrealloc;
388 set->highest_location = RESERVED_LOCATION_COUNT - 1;
389 set->highest_line = RESERVED_LOCATION_COUNT - 1;
390 set->location_adhoc_data_map.htab =
391 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
392 set->builtin_location = builtin_location;
395 /* Return the ordinary line map from whence MAP was included. Returns
396 NULL if MAP was not an include. */
398 const line_map_ordinary *
399 linemap_included_from_linemap (line_maps *set, const line_map_ordinary *map)
401 return linemap_ordinary_map_lookup (set, linemap_included_from (map));
404 /* Check for and warn about line_maps entered but not exited. */
406 void
407 linemap_check_files_exited (line_maps *set)
409 /* Depending upon whether we are handling preprocessed input or
410 not, this can be a user error or an ICE. */
411 for (const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
412 ! MAIN_FILE_P (map);
413 map = linemap_included_from_linemap (set, map))
414 fprintf (stderr, "line-map.cc: file \"%s\" entered but not left\n",
415 ORDINARY_MAP_FILE_NAME (map));
418 /* Create NUM zero-initialized maps of type MACRO_P. */
420 line_map *
421 line_map_new_raw (line_maps *set, bool macro_p, unsigned num)
423 unsigned num_maps_allocated = LINEMAPS_ALLOCATED (set, macro_p);
424 unsigned num_maps_used = LINEMAPS_USED (set, macro_p);
426 if (num > num_maps_allocated - num_maps_used)
428 /* We need more space! */
429 if (!num_maps_allocated)
430 num_maps_allocated = 128;
431 if (num_maps_allocated < num_maps_used + num)
432 num_maps_allocated = num_maps_used + num;
433 num_maps_allocated *= 2;
435 size_t size_of_a_map;
436 void *buffer;
437 if (macro_p)
439 size_of_a_map = sizeof (line_map_macro);
440 buffer = set->info_macro.maps;
442 else
444 size_of_a_map = sizeof (line_map_ordinary);
445 buffer = set->info_ordinary.maps;
448 /* We are going to execute some dance to try to reduce the
449 overhead of the memory allocator, in case we are using the
450 ggc-page.cc one.
452 The actual size of memory we are going to get back from the
453 allocator may well be larger than what we ask for. Use this
454 hook to find what that size is. */
455 size_t alloc_size
456 = set->round_alloc_size (num_maps_allocated * size_of_a_map);
458 /* Now alloc_size contains the exact memory size we would get if
459 we have asked for the initial alloc_size amount of memory.
460 Let's get back to the number of map that amounts to. */
461 unsigned num_maps = alloc_size / size_of_a_map;
462 buffer = set->reallocator (buffer, num_maps * size_of_a_map);
463 memset ((char *)buffer + num_maps_used * size_of_a_map, 0,
464 (num_maps - num_maps_used) * size_of_a_map);
465 if (macro_p)
466 set->info_macro.maps = (line_map_macro *)buffer;
467 else
468 set->info_ordinary.maps = (line_map_ordinary *)buffer;
469 LINEMAPS_ALLOCATED (set, macro_p) = num_maps;
472 line_map *result = (macro_p ? (line_map *)&set->info_macro.maps[num_maps_used]
473 : (line_map *)&set->info_ordinary.maps[num_maps_used]);
474 LINEMAPS_USED (set, macro_p) += num;
476 return result;
479 /* Create a new line map in the line map set SET, and return it.
480 REASON is the reason of creating the map. It determines the type
481 of map created (ordinary or macro map). Note that ordinary maps and
482 macro maps are allocated in different memory location. */
484 static struct line_map *
485 new_linemap (line_maps *set, location_t start_location)
487 line_map *result = line_map_new_raw (set,
488 start_location >= LINE_MAP_MAX_LOCATION,
491 result->start_location = start_location;
493 return result;
496 /* Return the location of the last source line within an ordinary
497 map. */
498 inline location_t
499 LAST_SOURCE_LINE_LOCATION (const line_map_ordinary *map)
501 return (((map[1].start_location - 1
502 - map->start_location)
503 & ~((1 << map->m_column_and_range_bits) - 1))
504 + map->start_location);
507 /* Add a mapping of logical source line to physical source file and
508 line number.
510 The text pointed to by TO_FILE must have a lifetime
511 at least as long as the final call to lookup_line (). An empty
512 TO_FILE means standard input. If reason is LC_LEAVE, and
513 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
514 natural values considering the file we are returning to.
516 FROM_LINE should be monotonic increasing across calls to this
517 function. A call to this function can relocate the previous set of
518 maps, so any stored line_map pointers should not be used. */
520 const struct line_map *
521 linemap_add (line_maps *set, enum lc_reason reason,
522 unsigned int sysp, const char *to_file, linenum_type to_line)
524 /* Generate a start_location above the current highest_location.
525 If possible, make the low range bits be zero. */
526 location_t start_location = set->highest_location + 1;
527 unsigned range_bits = 0;
528 if (start_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
529 range_bits = set->default_range_bits;
530 start_location += (1 << range_bits) - 1;
531 start_location &= ~((1 << range_bits) - 1);
533 linemap_assert (!LINEMAPS_ORDINARY_USED (set)
534 || (start_location
535 >= MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set))));
537 /* When we enter the file for the first time reason cannot be
538 LC_RENAME. */
539 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
541 /* If we are leaving the main file, return a NULL map. */
542 if (reason == LC_LEAVE
543 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
544 && to_file == NULL)
546 set->depth--;
547 return NULL;
550 linemap_assert (reason != LC_ENTER_MACRO);
552 if (start_location >= LINE_MAP_MAX_LOCATION)
553 /* We ran out of line map space. */
554 start_location = 0;
556 line_map_ordinary *map
557 = linemap_check_ordinary (new_linemap (set, start_location));
558 map->reason = reason;
560 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
561 to_file = "<stdin>";
563 if (reason == LC_RENAME_VERBATIM)
564 reason = LC_RENAME;
566 const line_map_ordinary *from = NULL;
567 if (reason == LC_LEAVE)
569 /* When we are just leaving an "included" file, and jump to the next
570 location inside the "includer" right after the #include
571 "included", this variable points the map in use right before the
572 #include "included", inside the same "includer" file. */
574 linemap_assert (!MAIN_FILE_P (map - 1));
575 /* (MAP - 1) points to the map we are leaving. The
576 map from which (MAP - 1) got included should be the map
577 that comes right before MAP in the same file. */
578 from = linemap_included_from_linemap (set, map - 1);
580 /* A TO_FILE of NULL is special - we use the natural values. */
581 if (to_file == NULL)
583 to_file = ORDINARY_MAP_FILE_NAME (from);
584 to_line = SOURCE_LINE (from, from[1].start_location);
585 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
587 else
588 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from),
589 to_file) == 0);
592 map->sysp = sysp;
593 map->to_file = to_file;
594 map->to_line = to_line;
595 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
596 /* Do not store range_bits here. That's readjusted in
597 linemap_line_start. */
598 map->m_range_bits = map->m_column_and_range_bits = 0;
599 set->highest_location = start_location;
600 set->highest_line = start_location;
601 set->max_column_hint = 0;
603 /* This assertion is placed after set->highest_location has
604 been updated, since the latter affects
605 linemap_location_from_macro_expansion_p, which ultimately affects
606 pure_location_p. */
607 linemap_assert (pure_location_p (set, start_location));
609 if (reason == LC_ENTER)
611 if (set->depth == 0)
612 map->included_from = 0;
613 else
614 /* The location of the end of the just-closed map. */
615 map->included_from
616 = (((map[0].start_location - 1 - map[-1].start_location)
617 & ~((1 << map[-1].m_column_and_range_bits) - 1))
618 + map[-1].start_location);
619 set->depth++;
620 if (set->trace_includes)
621 trace_include (set, map);
623 else if (reason == LC_RENAME)
624 map->included_from = linemap_included_from (&map[-1]);
625 else if (reason == LC_LEAVE)
627 set->depth--;
628 map->included_from = linemap_included_from (from);
631 return map;
634 /* Create a location for a module NAME imported at FROM. */
636 location_t
637 linemap_module_loc (line_maps *set, location_t from, const char *name)
639 const line_map_ordinary *map
640 = linemap_check_ordinary (linemap_add (set, LC_MODULE, false, name, 0));
641 const_cast <line_map_ordinary *> (map)->included_from = from;
643 location_t loc = linemap_line_start (set, 0, 0);
645 return loc;
648 /* The linemap containing LOC is being reparented to be
649 imported/included from ADOPTOR. This can happen when an
650 indirectly imported module is then directly imported, or when
651 partitions are involved. */
653 void
654 linemap_module_reparent (line_maps *set, location_t loc, location_t adoptor)
656 const line_map_ordinary *map = linemap_ordinary_map_lookup (set, loc);
657 const_cast<line_map_ordinary *> (map)->included_from = adoptor;
660 /* A linemap at LWM-1 was interrupted to insert module locations & imports.
661 Append a new map, continuing the interrupted one. Return the start location
662 of the new map, or 0 if failed (because we ran out of locations. */
664 unsigned
665 linemap_module_restore (line_maps *set, unsigned lwm)
667 linemap_assert (lwm);
669 const line_map_ordinary *pre_map
670 = linemap_check_ordinary (LINEMAPS_MAP_AT (set, false, lwm - 1));
671 unsigned src_line = SOURCE_LINE (pre_map, LAST_SOURCE_LINE_LOCATION (pre_map));
672 location_t inc_at = pre_map->included_from;
673 if (const line_map_ordinary *post_map
674 = (linemap_check_ordinary
675 (linemap_add (set, LC_RENAME_VERBATIM,
676 ORDINARY_MAP_IN_SYSTEM_HEADER_P (pre_map),
677 ORDINARY_MAP_FILE_NAME (pre_map), src_line))))
679 /* linemap_add will think we were included from the same as the preceeding
680 map. */
681 const_cast <line_map_ordinary *> (post_map)->included_from = inc_at;
683 return post_map->start_location;
686 return 0;
689 /* Returns TRUE if the line table set tracks token locations across
690 macro expansion, FALSE otherwise. */
692 bool
693 linemap_tracks_macro_expansion_locs_p (line_maps *set)
695 return LINEMAPS_MACRO_MAPS (set) != NULL;
698 /* Create a macro map. A macro map encodes source locations of tokens
699 that are part of a macro replacement-list, at a macro expansion
700 point. See the extensive comments of struct line_map and struct
701 line_map_macro, in line-map.h.
703 This map shall be created when the macro is expanded. The map
704 encodes the source location of the expansion point of the macro as
705 well as the "original" source location of each token that is part
706 of the macro replacement-list. If a macro is defined but never
707 expanded, it has no macro map. SET is the set of maps the macro
708 map should be part of. MACRO_NODE is the macro which the new macro
709 map should encode source locations for. EXPANSION is the location
710 of the expansion point of MACRO. For function-like macros
711 invocations, it's best to make it point to the closing parenthesis
712 of the macro, rather than the the location of the first character
713 of the macro. NUM_TOKENS is the number of tokens that are part of
714 the replacement-list of MACRO.
716 Note that when we run out of the integer space available for source
717 locations, this function returns NULL. In that case, callers of
718 this function cannot encode {line,column} pairs into locations of
719 macro tokens anymore. */
721 const line_map_macro *
722 linemap_enter_macro (class line_maps *set, struct cpp_hashnode *macro_node,
723 location_t expansion, unsigned int num_tokens)
725 location_t start_location
726 = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
728 if (start_location < LINE_MAP_MAX_LOCATION)
729 /* We ran out of macro map space. */
730 return NULL;
732 line_map_macro *map = linemap_check_macro (new_linemap (set, start_location));
734 map->macro = macro_node;
735 map->n_tokens = num_tokens;
736 map->macro_locations
737 = (location_t*) set->reallocator (NULL,
738 2 * num_tokens
739 * sizeof (location_t));
740 map->expansion = expansion;
741 memset (MACRO_MAP_LOCATIONS (map), 0,
742 2 * num_tokens * sizeof (location_t));
744 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
746 return map;
749 /* Create and return a virtual location for a token that is part of a
750 macro expansion-list at a macro expansion point. See the comment
751 inside struct line_map_macro to see what an expansion-list exactly
754 A call to this function must come after a call to
755 linemap_enter_macro.
757 MAP is the map into which the source location is created. TOKEN_NO
758 is the index of the token in the macro replacement-list, starting
759 at number 0.
761 ORIG_LOC is the location of the token outside of this macro
762 expansion. If the token comes originally from the macro
763 definition, it is the locus in the macro definition; otherwise it
764 is a location in the context of the caller of this macro expansion
765 (which is a virtual location or a source location if the caller is
766 itself a macro expansion or not).
768 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
769 either of the token itself or of a macro parameter that it
770 replaces. */
772 location_t
773 linemap_add_macro_token (const line_map_macro *map,
774 unsigned int token_no,
775 location_t orig_loc,
776 location_t orig_parm_replacement_loc)
778 location_t result;
780 linemap_assert (linemap_macro_expansion_map_p (map));
781 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
783 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
784 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
786 result = MAP_START_LOCATION (map) + token_no;
787 return result;
790 /* Return a location_t for the start (i.e. column==0) of
791 (physical) line TO_LINE in the current source file (as in the
792 most recent linemap_add). MAX_COLUMN_HINT is the highest column
793 number we expect to use in this line (but it does not change
794 the highest_location). */
796 location_t
797 linemap_line_start (line_maps *set, linenum_type to_line,
798 unsigned int max_column_hint)
800 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
801 location_t highest = set->highest_location;
802 location_t r;
803 linenum_type last_line =
804 SOURCE_LINE (map, set->highest_line);
805 int line_delta = to_line - last_line;
806 bool add_map = false;
807 linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
808 int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
810 if (line_delta < 0
811 || (line_delta > 10
812 && line_delta * map->m_column_and_range_bits > 1000)
813 || (max_column_hint >= (1U << effective_column_bits))
814 || (max_column_hint <= 80 && effective_column_bits >= 10)
815 || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
816 && map->m_range_bits > 0)
817 || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
818 && (set->max_column_hint || highest >= LINE_MAP_MAX_LOCATION)))
819 add_map = true;
820 else
821 max_column_hint = set->max_column_hint;
822 if (add_map)
824 int column_bits;
825 int range_bits;
826 if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
827 || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
829 /* If the column number is ridiculous or we've allocated a huge
830 number of location_ts, give up on column numbers
831 (and on packed ranges). */
832 max_column_hint = 1;
833 column_bits = 0;
834 range_bits = 0;
835 if (highest >= LINE_MAP_MAX_LOCATION)
836 goto overflowed;
838 else
840 column_bits = 7;
841 if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
842 range_bits = set->default_range_bits;
843 else
844 range_bits = 0;
845 while (max_column_hint >= (1U << column_bits))
846 column_bits++;
847 max_column_hint = 1U << column_bits;
848 column_bits += range_bits;
851 /* Allocate the new line_map. However, if the current map only has a
852 single line we can sometimes just increase its column_bits instead. */
853 if (line_delta < 0
854 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
855 || SOURCE_COLUMN (map, highest) >= (1U << (column_bits - range_bits))
856 || ( /* We can't reuse the map if the line offset is sufficiently
857 large to cause overflow when computing location_t values. */
858 (to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
859 >= (((uint64_t) 1)
860 << (CHAR_BIT * sizeof (linenum_type) - column_bits)))
861 || range_bits < map->m_range_bits)
862 map = linemap_check_ordinary
863 (const_cast <line_map *>
864 (linemap_add (set, LC_RENAME,
865 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
866 ORDINARY_MAP_FILE_NAME (map),
867 to_line)));
868 map->m_column_and_range_bits = column_bits;
869 map->m_range_bits = range_bits;
870 r = (MAP_START_LOCATION (map)
871 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
872 << column_bits));
874 else
875 r = set->highest_line + (line_delta << map->m_column_and_range_bits);
877 /* Locations of ordinary tokens are always lower than locations of
878 macro tokens. */
879 if (r >= LINE_MAP_MAX_LOCATION)
881 overflowed:
882 /* Remember we overflowed. */
883 set->highest_line = set->highest_location = LINE_MAP_MAX_LOCATION - 1;
884 /* No column numbers! */
885 set->max_column_hint = 1;
886 return 0;
889 set->highest_line = r;
890 if (r > set->highest_location)
891 set->highest_location = r;
892 set->max_column_hint = max_column_hint;
894 /* At this point, we expect one of:
895 (a) the normal case: a "pure" location with 0 range bits, or
896 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
897 columns anymore (or ranges), or
898 (c) we're in a region with a column hint exceeding
899 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
900 with column_bits == 0. */
901 linemap_assert (pure_location_p (set, r)
902 || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
903 || map->m_column_and_range_bits == 0);
904 linemap_assert (SOURCE_LINE (map, r) == to_line);
905 return r;
908 /* Encode and return a location_t from a column number. The
909 source line considered is the last source line used to call
910 linemap_line_start, i.e, the last source line which a location was
911 encoded from. */
913 location_t
914 linemap_position_for_column (line_maps *set, unsigned int to_column)
916 location_t r = set->highest_line;
918 linemap_assert
919 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
921 if (to_column >= set->max_column_hint)
923 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
924 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
926 /* Running low on location_ts - disable column numbers. */
927 return r;
929 else
931 /* Otherwise, attempt to start a new line that can hold TO_COLUMN,
932 with some space to spare. This may or may not lead to a new
933 linemap being created. */
934 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
935 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
936 map = LINEMAPS_LAST_ORDINARY_MAP (set);
937 if (map->m_column_and_range_bits == 0)
939 /* ...then the linemap has column-tracking disabled,
940 presumably due to exceeding either
941 LINE_MAP_MAX_LOCATION_WITH_COLS (overall) or
942 LINE_MAP_MAX_COLUMN_NUMBER (within this line).
943 Return the start of the linemap, which encodes column 0, for
944 the whole line. */
945 return r;
949 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
950 r = r + (to_column << map->m_range_bits);
951 if (r >= set->highest_location)
952 set->highest_location = r;
953 return r;
956 /* Encode and return a source location from a given line and
957 column. */
959 location_t
960 linemap_position_for_line_and_column (line_maps *set,
961 const line_map_ordinary *ord_map,
962 linenum_type line,
963 unsigned column)
965 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
967 location_t r = MAP_START_LOCATION (ord_map);
968 r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
969 << ord_map->m_column_and_range_bits);
970 if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
971 r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
972 << ord_map->m_range_bits);
973 location_t upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
974 if (r >= upper_limit)
975 r = upper_limit - 1;
976 if (r > set->highest_location)
977 set->highest_location = r;
978 return r;
981 /* Encode and return a location_t starting from location LOC and
982 shifting it by COLUMN_OFFSET columns. This function does not support
983 virtual locations. */
985 location_t
986 linemap_position_for_loc_and_offset (line_maps *set,
987 location_t loc,
988 unsigned int column_offset)
990 const line_map_ordinary * map = NULL;
992 if (IS_ADHOC_LOC (loc))
993 loc = get_location_from_adhoc_loc (set, loc);
995 /* This function does not support virtual locations yet. */
996 if (linemap_location_from_macro_expansion_p (set, loc))
997 return loc;
999 if (column_offset == 0
1000 /* Adding an offset to a reserved location (like
1001 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
1002 sense. So let's leave the location intact in that case. */
1003 || loc < RESERVED_LOCATION_COUNT)
1004 return loc;
1006 /* We find the real location and shift it. */
1007 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
1008 /* The new location (loc + offset) should be higher than the first
1009 location encoded by MAP. This can fail if the line information
1010 is messed up because of line directives (see PR66415). */
1011 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
1012 return loc;
1014 linenum_type line = SOURCE_LINE (map, loc);
1015 unsigned int column = SOURCE_COLUMN (map, loc);
1017 /* If MAP is not the last line map of its set, then the new location
1018 (loc + offset) should be less than the first location encoded by
1019 the next line map of the set. Otherwise, we try to encode the
1020 location in the next map. */
1021 for (; map != LINEMAPS_LAST_ORDINARY_MAP (set)
1022 && (loc + (column_offset << map->m_range_bits)
1023 >= MAP_START_LOCATION (map + 1)); map++)
1024 /* If the next map is a different file, or starts in a higher line, we
1025 cannot encode the location there. */
1026 if ((map + 1)->reason != LC_RENAME
1027 || line < ORDINARY_MAP_STARTING_LINE_NUMBER (map + 1)
1028 || 0 != strcmp (LINEMAP_FILE (map + 1), LINEMAP_FILE (map)))
1029 return loc;
1031 column += column_offset;
1033 /* Bail out if the column is not representable within the existing
1034 linemap. */
1035 if (column >= (1u << (map->m_column_and_range_bits - map->m_range_bits)))
1036 return loc;
1038 location_t r =
1039 linemap_position_for_line_and_column (set, map, line, column);
1040 if (linemap_assert_fails (r <= set->highest_location)
1041 || linemap_assert_fails (map == linemap_lookup (set, r)))
1042 return loc;
1044 return r;
1047 /* Given a virtual source location yielded by a map (either an
1048 ordinary or a macro map), returns that map. */
1050 const struct line_map*
1051 linemap_lookup (const line_maps *set, location_t line)
1053 if (IS_ADHOC_LOC (line))
1054 line = get_location_from_adhoc_loc (set, line);
1055 if (linemap_location_from_macro_expansion_p (set, line))
1056 return linemap_macro_map_lookup (set, line);
1057 return linemap_ordinary_map_lookup (set, line);
1060 /* Given a source location yielded by an ordinary map, returns that
1061 map. Since the set is built chronologically, the logical lines are
1062 monotonic increasing, and so the list is sorted and we can use a
1063 binary search. */
1065 static const line_map_ordinary *
1066 linemap_ordinary_map_lookup (const line_maps *set, location_t line)
1068 if (IS_ADHOC_LOC (line))
1069 line = get_location_from_adhoc_loc (set, line);
1071 if (set == NULL || line < RESERVED_LOCATION_COUNT)
1072 return NULL;
1074 unsigned mn = LINEMAPS_ORDINARY_CACHE (set);
1075 unsigned mx = LINEMAPS_ORDINARY_USED (set);
1077 const line_map_ordinary *cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
1078 /* We should get a segfault if no line_maps have been added yet. */
1079 if (line >= MAP_START_LOCATION (cached))
1081 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
1082 return cached;
1084 else
1086 mx = mn;
1087 mn = 0;
1090 while (mx - mn > 1)
1092 unsigned md = (mn + mx) / 2;
1093 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
1094 mx = md;
1095 else
1096 mn = md;
1099 LINEMAPS_ORDINARY_CACHE (set) = mn;
1100 const line_map_ordinary *result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
1101 linemap_assert (line >= MAP_START_LOCATION (result));
1102 return result;
1105 /* Given a source location yielded by a macro map, returns that map.
1106 Since the set is built chronologically, the logical lines are
1107 monotonic decreasing, and so the list is sorted and we can use a
1108 binary search. */
1110 static const line_map_macro *
1111 linemap_macro_map_lookup (const line_maps *set, location_t line)
1113 if (IS_ADHOC_LOC (line))
1114 line = get_location_from_adhoc_loc (set, line);
1116 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
1118 if (set == NULL)
1119 return NULL;
1121 unsigned ix = linemap_lookup_macro_index (set, line);
1122 const struct line_map_macro *result = LINEMAPS_MACRO_MAP_AT (set, ix);
1123 linemap_assert (MAP_START_LOCATION (result) <= line);
1125 return result;
1128 unsigned
1129 linemap_lookup_macro_index (const line_maps *set, location_t line)
1131 unsigned mn = LINEMAPS_MACRO_CACHE (set);
1132 unsigned mx = LINEMAPS_MACRO_USED (set);
1133 const struct line_map_macro *cached = LINEMAPS_MACRO_MAP_AT (set, mn);
1135 if (line >= MAP_START_LOCATION (cached))
1137 if (line < (MAP_START_LOCATION (cached)
1138 + MACRO_MAP_NUM_MACRO_TOKENS (cached)))
1139 return mn;
1140 mx = mn - 1;
1141 mn = 0;
1144 while (mn < mx)
1146 unsigned md = (mx + mn) / 2;
1147 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1148 mn = md + 1;
1149 else
1150 mx = md;
1153 LINEMAPS_MACRO_CACHE (set) = mx;
1154 return mx;
1157 /* Return TRUE if MAP encodes locations coming from a macro
1158 replacement-list at macro expansion point. */
1160 bool
1161 linemap_macro_expansion_map_p (const struct line_map *map)
1163 return map && !MAP_ORDINARY_P (map);
1166 /* If LOCATION is the locus of a token in a replacement-list of a
1167 macro expansion return the location of the macro expansion point.
1169 Read the comments of struct line_map and struct line_map_macro in
1170 line-map.h to understand what a macro expansion point is. */
1172 static location_t
1173 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1174 location_t location ATTRIBUTE_UNUSED)
1176 linemap_assert (linemap_macro_expansion_map_p (map)
1177 && location >= MAP_START_LOCATION (map));
1179 /* Make sure LOCATION is correct. */
1180 linemap_assert ((location - MAP_START_LOCATION (map))
1181 < MACRO_MAP_NUM_MACRO_TOKENS (map));
1183 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1186 /* LOCATION is the source location of a token that belongs to a macro
1187 replacement-list as part of the macro expansion denoted by MAP.
1189 Return the location of the token at the definition point of the
1190 macro. */
1192 static location_t
1193 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1194 location_t location)
1196 unsigned token_no;
1198 linemap_assert (linemap_macro_expansion_map_p (map)
1199 && location >= MAP_START_LOCATION (map));
1200 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1202 token_no = location - MAP_START_LOCATION (map);
1203 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1205 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1207 return location;
1210 /* If LOCATION is the locus of a token that is an argument of a
1211 function-like macro M and appears in the expansion of M, return the
1212 locus of that argument in the context of the caller of M.
1214 In other words, this returns the xI location presented in the
1215 comments of line_map_macro above. */
1216 location_t
1217 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1218 const line_map_macro* map,
1219 location_t location)
1221 unsigned token_no;
1223 if (IS_ADHOC_LOC (location))
1224 location = get_location_from_adhoc_loc (set, location);
1226 linemap_assert (linemap_macro_expansion_map_p (map)
1227 && location >= MAP_START_LOCATION (map));
1228 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1229 linemap_assert (!IS_ADHOC_LOC (location));
1231 token_no = location - MAP_START_LOCATION (map);
1232 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1234 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1236 return location;
1239 /* Return the source line number corresponding to source location
1240 LOCATION. SET is the line map set LOCATION comes from. If
1241 LOCATION is the source location of token that is part of the
1242 replacement-list of a macro expansion return the line number of the
1243 macro expansion point. */
1246 linemap_get_expansion_line (line_maps *set,
1247 location_t location)
1249 const line_map_ordinary *map = NULL;
1251 if (IS_ADHOC_LOC (location))
1252 location = get_location_from_adhoc_loc (set, location);
1254 if (location < RESERVED_LOCATION_COUNT)
1255 return 0;
1257 location =
1258 linemap_macro_loc_to_exp_point (set, location, &map);
1260 return SOURCE_LINE (map, location);
1263 /* Return the path of the file corresponding to source code location
1264 LOCATION.
1266 If LOCATION is the source location of token that is part of the
1267 replacement-list of a macro expansion return the file path of the
1268 macro expansion point.
1270 SET is the line map set LOCATION comes from. */
1272 const char*
1273 linemap_get_expansion_filename (line_maps *set,
1274 location_t location)
1276 const struct line_map_ordinary *map = NULL;
1278 if (IS_ADHOC_LOC (location))
1279 location = get_location_from_adhoc_loc (set, location);
1281 if (location < RESERVED_LOCATION_COUNT)
1282 return NULL;
1284 linemap_macro_loc_to_exp_point (set, location, &map);
1286 return LINEMAP_FILE (map);
1289 /* Return the name of the macro associated to MACRO_MAP. */
1291 const char*
1292 linemap_map_get_macro_name (const line_map_macro *macro_map)
1294 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1295 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1298 /* Return a positive value if LOCATION is the locus of a token that is
1299 located in a system header, O otherwise. It returns 1 if LOCATION
1300 is the locus of a token that is located in a system header, and 2
1301 if LOCATION is the locus of a token located in a C system header
1302 that therefore needs to be extern "C" protected in C++.
1304 Note that this function returns 1 if LOCATION belongs to a token
1305 that is part of a macro replacement-list defined in a system
1306 header, but expanded in a non-system file. */
1309 linemap_location_in_system_header_p (line_maps *set,
1310 location_t location)
1312 const struct line_map *map = NULL;
1314 if (IS_ADHOC_LOC (location))
1315 location = get_location_from_adhoc_loc (set, location);
1317 if (location < RESERVED_LOCATION_COUNT)
1318 return false;
1320 /* Let's look at where the token for LOCATION comes from. */
1321 while (true)
1323 map = linemap_lookup (set, location);
1324 if (map != NULL)
1326 if (!linemap_macro_expansion_map_p (map))
1327 /* It's a normal token. */
1328 return LINEMAP_SYSP (linemap_check_ordinary (map));
1329 else
1331 const line_map_macro *macro_map = linemap_check_macro (map);
1333 /* It's a token resulting from a macro expansion. */
1334 location_t loc =
1335 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
1336 if (loc < RESERVED_LOCATION_COUNT)
1337 /* This token might come from a built-in macro. Let's
1338 look at where that macro got expanded. */
1339 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1340 else
1341 location = loc;
1344 else
1345 break;
1347 return false;
1350 /* Return TRUE if LOCATION is a source code location of a token that is part of
1351 a macro expansion, FALSE otherwise. */
1353 bool
1354 linemap_location_from_macro_expansion_p (const class line_maps *set,
1355 location_t location)
1357 if (IS_ADHOC_LOC (location))
1358 location = get_location_from_adhoc_loc (set, location);
1360 return location >= LINEMAPS_MACRO_LOWEST_LOCATION (set);
1363 /* Given two virtual locations *LOC0 and *LOC1, return the first
1364 common macro map in their macro expansion histories. Return NULL
1365 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1366 virtual location of the token inside the resulting macro. */
1368 static const struct line_map*
1369 first_map_in_common_1 (line_maps *set,
1370 location_t *loc0,
1371 location_t *loc1)
1373 location_t l0 = *loc0, l1 = *loc1;
1374 const struct line_map *map0 = linemap_lookup (set, l0);
1375 if (IS_ADHOC_LOC (l0))
1376 l0 = get_location_from_adhoc_loc (set, l0);
1378 const struct line_map *map1 = linemap_lookup (set, l1);
1379 if (IS_ADHOC_LOC (l1))
1380 l1 = get_location_from_adhoc_loc (set, l1);
1382 while (linemap_macro_expansion_map_p (map0)
1383 && linemap_macro_expansion_map_p (map1)
1384 && (map0 != map1))
1386 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1388 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1389 l0);
1390 map0 = linemap_lookup (set, l0);
1392 else
1394 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1395 l1);
1396 map1 = linemap_lookup (set, l1);
1400 if (map0 == map1)
1402 *loc0 = l0;
1403 *loc1 = l1;
1404 return map0;
1406 return NULL;
1409 /* Given two virtual locations LOC0 and LOC1, return the first common
1410 macro map in their macro expansion histories. Return NULL if no
1411 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1412 virtual location of the token inside the resulting macro, upon
1413 return of a non-NULL result. */
1415 const struct line_map*
1416 first_map_in_common (line_maps *set,
1417 location_t loc0,
1418 location_t loc1,
1419 location_t *res_loc0,
1420 location_t *res_loc1)
1422 *res_loc0 = loc0;
1423 *res_loc1 = loc1;
1425 return first_map_in_common_1 (set, res_loc0, res_loc1);
1428 /* Return a positive value if PRE denotes the location of a token that
1429 comes before the token of POST, 0 if PRE denotes the location of
1430 the same token as the token for POST, and a negative value
1431 otherwise. */
1434 linemap_compare_locations (line_maps *set,
1435 location_t pre,
1436 location_t post)
1438 bool pre_virtual_p, post_virtual_p;
1439 location_t l0 = pre, l1 = post;
1441 if (IS_ADHOC_LOC (l0))
1442 l0 = get_location_from_adhoc_loc (set, l0);
1443 if (IS_ADHOC_LOC (l1))
1444 l1 = get_location_from_adhoc_loc (set, l1);
1446 if (l0 == l1)
1447 return 0;
1449 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1450 l0 = linemap_resolve_location (set, l0,
1451 LRK_MACRO_EXPANSION_POINT,
1452 NULL);
1454 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1455 l1 = linemap_resolve_location (set, l1,
1456 LRK_MACRO_EXPANSION_POINT,
1457 NULL);
1459 if (l0 == l1
1460 && pre_virtual_p
1461 && post_virtual_p)
1463 /* So pre and post represent two tokens that are present in a
1464 same macro expansion. Let's see if the token for pre was
1465 before the token for post in that expansion. */
1466 const struct line_map *map =
1467 first_map_in_common (set, pre, post, &l0, &l1);
1469 if (map == NULL)
1470 /* This should not be possible while we have column information, but if
1471 we don't, the tokens could be from separate macro expansions on the
1472 same line. */
1473 gcc_assert (l0 > LINE_MAP_MAX_LOCATION_WITH_COLS);
1474 else
1476 unsigned i0 = l0 - MAP_START_LOCATION (map);
1477 unsigned i1 = l1 - MAP_START_LOCATION (map);
1478 return i1 - i0;
1482 if (IS_ADHOC_LOC (l0))
1483 l0 = get_location_from_adhoc_loc (set, l0);
1484 if (IS_ADHOC_LOC (l1))
1485 l1 = get_location_from_adhoc_loc (set, l1);
1487 return l1 - l0;
1490 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1492 static void
1493 trace_include (const class line_maps *set, const line_map_ordinary *map)
1495 unsigned int i = set->depth;
1497 while (--i)
1498 putc ('.', stderr);
1500 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1503 /* Return the spelling location of the token wherever it comes from,
1504 whether part of a macro definition or not.
1506 This is a subroutine for linemap_resolve_location. */
1508 static location_t
1509 linemap_macro_loc_to_spelling_point (line_maps *set,
1510 location_t location,
1511 const line_map_ordinary **original_map)
1513 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1515 while (true)
1517 const struct line_map *map = linemap_lookup (set, location);
1518 if (!map || MAP_ORDINARY_P (map))
1520 if (original_map)
1521 *original_map = (const line_map_ordinary *)map;
1522 break;
1525 location = linemap_macro_map_loc_unwind_toward_spelling
1526 (set, linemap_check_macro (map), location);
1529 return location;
1532 /* If LOCATION is the source location of a token that belongs to a
1533 macro replacement-list -- as part of a macro expansion -- then
1534 return the location of the token at the definition point of the
1535 macro. Otherwise, return LOCATION. SET is the set of maps
1536 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1537 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1538 returned location comes from.
1540 This is a subroutine of linemap_resolve_location. */
1542 static location_t
1543 linemap_macro_loc_to_def_point (line_maps *set,
1544 location_t location,
1545 const line_map_ordinary **original_map)
1547 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1549 for (;;)
1551 location_t caret_loc = location;
1552 if (IS_ADHOC_LOC (caret_loc))
1553 caret_loc = get_location_from_adhoc_loc (set, caret_loc);
1555 const line_map *map = linemap_lookup (set, caret_loc);
1556 if (!map || MAP_ORDINARY_P (map))
1558 if (original_map)
1559 *original_map = (const line_map_ordinary *)map;
1560 break;
1563 location = linemap_macro_map_loc_to_def_point
1564 (linemap_check_macro (map), caret_loc);
1567 return location;
1570 /* If LOCATION is the source location of a token that belongs to a
1571 macro replacement-list -- at a macro expansion point -- then return
1572 the location of the topmost expansion point of the macro. We say
1573 topmost because if we are in the context of a nested macro
1574 expansion, the function returns the source location of the first
1575 macro expansion that triggered the nested expansions.
1577 Otherwise, return LOCATION. SET is the set of maps location come
1578 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1579 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1580 location comes from.
1582 This is a subroutine of linemap_resolve_location. */
1584 static location_t
1585 linemap_macro_loc_to_exp_point (line_maps *set,
1586 location_t location,
1587 const line_map_ordinary **original_map)
1589 struct line_map *map;
1591 if (IS_ADHOC_LOC (location))
1592 location = get_location_from_adhoc_loc (set, location);
1594 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1596 while (true)
1598 map = const_cast <line_map *> (linemap_lookup (set, location));
1599 if (!linemap_macro_expansion_map_p (map))
1600 break;
1601 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1602 location);
1605 if (original_map)
1606 *original_map = linemap_check_ordinary (map);
1607 return location;
1610 /* Resolve a virtual location into either a spelling location, an
1611 expansion point location or a token argument replacement point
1612 location. Return the map that encodes the virtual location as well
1613 as the resolved location.
1615 If LOC is *NOT* the location of a token resulting from the
1616 expansion of a macro, then the parameter LRK (which stands for
1617 Location Resolution Kind) is ignored and the resulting location
1618 just equals the one given in argument.
1620 Now if LOC *IS* the location of a token resulting from the
1621 expansion of a macro, this is what happens.
1623 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1624 -------------------------------
1626 The virtual location is resolved to the first macro expansion point
1627 that led to this macro expansion.
1629 * If LRK is set to LRK_SPELLING_LOCATION
1630 -------------------------------------
1632 The virtual location is resolved to the locus where the token has
1633 been spelled in the source. This can follow through all the macro
1634 expansions that led to the token.
1636 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1637 --------------------------------------
1639 The virtual location is resolved to the locus of the token in the
1640 context of the macro definition.
1642 If LOC is the locus of a token that is an argument of a
1643 function-like macro [replacing a parameter in the replacement list
1644 of the macro] the virtual location is resolved to the locus of the
1645 parameter that is replaced, in the context of the definition of the
1646 macro.
1648 If LOC is the locus of a token that is not an argument of a
1649 function-like macro, then the function behaves as if LRK was set to
1650 LRK_SPELLING_LOCATION.
1652 If MAP is not NULL, *MAP is set to the map encoding the
1653 returned location. Note that if the returned location wasn't originally
1654 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1655 resolves to a location reserved for the client code, like
1656 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1658 location_t
1659 linemap_resolve_location (line_maps *set,
1660 location_t loc,
1661 enum location_resolution_kind lrk,
1662 const line_map_ordinary **map)
1664 location_t locus = loc;
1665 if (IS_ADHOC_LOC (loc))
1666 locus = get_location_from_adhoc_loc (set, loc);
1668 if (locus < RESERVED_LOCATION_COUNT)
1670 /* A reserved location wasn't encoded in a map. Let's return a
1671 NULL map here, just like what linemap_ordinary_map_lookup
1672 does. */
1673 if (map)
1674 *map = NULL;
1675 return loc;
1678 switch (lrk)
1680 case LRK_MACRO_EXPANSION_POINT:
1681 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1682 break;
1683 case LRK_SPELLING_LOCATION:
1684 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1685 break;
1686 case LRK_MACRO_DEFINITION_LOCATION:
1687 loc = linemap_macro_loc_to_def_point (set, loc, map);
1688 break;
1689 default:
1690 abort ();
1692 return loc;
1695 /* TRUE if LOCATION is a source code location of a token that is part of the
1696 definition of a macro, FALSE otherwise. */
1698 bool
1699 linemap_location_from_macro_definition_p (line_maps *set,
1700 location_t loc)
1702 if (IS_ADHOC_LOC (loc))
1703 loc = get_location_from_adhoc_loc (set, loc);
1705 if (!linemap_location_from_macro_expansion_p (set, loc))
1706 return false;
1708 while (true)
1710 const struct line_map_macro *map
1711 = linemap_check_macro (linemap_lookup (set, loc));
1713 location_t s_loc
1714 = linemap_macro_map_loc_unwind_toward_spelling (set, map, loc);
1715 if (linemap_location_from_macro_expansion_p (set, s_loc))
1716 loc = s_loc;
1717 else
1719 location_t def_loc
1720 = linemap_macro_map_loc_to_def_point (map, loc);
1721 return s_loc == def_loc;
1727 Suppose that LOC is the virtual location of a token T coming from
1728 the expansion of a macro M. This function then steps up to get the
1729 location L of the point where M got expanded. If L is a spelling
1730 location inside a macro expansion M', then this function returns
1731 the locus of the point where M' was expanded. Said otherwise, this
1732 function returns the location of T in the context that triggered
1733 the expansion of M.
1735 *LOC_MAP must be set to the map of LOC. This function then sets it
1736 to the map of the returned location. */
1738 location_t
1739 linemap_unwind_toward_expansion (line_maps *set,
1740 location_t loc,
1741 const struct line_map **map)
1743 location_t resolved_location;
1744 const line_map_macro *macro_map = linemap_check_macro (*map);
1745 const struct line_map *resolved_map;
1747 if (IS_ADHOC_LOC (loc))
1748 loc = get_location_from_adhoc_loc (set, loc);
1750 resolved_location =
1751 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1752 resolved_map = linemap_lookup (set, resolved_location);
1754 if (!linemap_macro_expansion_map_p (resolved_map))
1756 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1757 resolved_map = linemap_lookup (set, resolved_location);
1760 *map = resolved_map;
1761 return resolved_location;
1764 /* If LOC is the virtual location of a token coming from the expansion
1765 of a macro M and if its spelling location is reserved (e.g, a
1766 location for a built-in token), then this function unwinds (using
1767 linemap_unwind_toward_expansion) the location until a location that
1768 is not reserved and is not in a system header is reached. In other
1769 words, this unwinds the reserved location until a location that is
1770 in real source code is reached.
1772 Otherwise, if the spelling location for LOC is not reserved or if
1773 LOC doesn't come from the expansion of a macro, the function
1774 returns LOC as is and *MAP is not touched.
1776 *MAP is set to the map of the returned location if the later is
1777 different from LOC. */
1778 location_t
1779 linemap_unwind_to_first_non_reserved_loc (line_maps *set,
1780 location_t loc,
1781 const struct line_map **map)
1783 location_t resolved_loc;
1784 const struct line_map *map0 = NULL;
1785 const line_map_ordinary *map1 = NULL;
1787 if (IS_ADHOC_LOC (loc))
1788 loc = get_location_from_adhoc_loc (set, loc);
1790 map0 = linemap_lookup (set, loc);
1791 if (!linemap_macro_expansion_map_p (map0))
1792 return loc;
1794 resolved_loc = linemap_resolve_location (set, loc,
1795 LRK_SPELLING_LOCATION,
1796 &map1);
1798 if (resolved_loc >= RESERVED_LOCATION_COUNT
1799 && !LINEMAP_SYSP (map1))
1800 return loc;
1802 while (linemap_macro_expansion_map_p (map0)
1803 && (resolved_loc < RESERVED_LOCATION_COUNT
1804 || LINEMAP_SYSP (map1)))
1806 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1807 resolved_loc = linemap_resolve_location (set, loc,
1808 LRK_SPELLING_LOCATION,
1809 &map1);
1812 if (map != NULL)
1813 *map = map0;
1814 return loc;
1817 /* Expand source code location LOC and return a user readable source
1818 code location. LOC must be a spelling (non-virtual) location. If
1819 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1820 location is returned. */
1822 expanded_location
1823 linemap_expand_location (line_maps *set,
1824 const struct line_map *map,
1825 location_t loc)
1828 expanded_location xloc;
1830 memset (&xloc, 0, sizeof (xloc));
1831 if (IS_ADHOC_LOC (loc))
1833 xloc.data = get_data_from_adhoc_loc (set, loc);
1834 loc = get_location_from_adhoc_loc (set, loc);
1837 if (loc < RESERVED_LOCATION_COUNT)
1838 /* The location for this token wasn't generated from a line map.
1839 It was probably a location for a builtin token, chosen by some
1840 client code. Let's not try to expand the location in that
1841 case. */;
1842 else if (map == NULL)
1843 /* We shouldn't be getting a NULL map with a location that is not
1844 reserved by the client code. */
1845 abort ();
1846 else
1848 /* MAP must be an ordinary map and LOC must be non-virtual,
1849 encoded into this map, obviously; the accessors used on MAP
1850 below ensure it is ordinary. Let's just assert the
1851 non-virtualness of LOC here. */
1852 if (linemap_location_from_macro_expansion_p (set, loc))
1853 abort ();
1855 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1857 xloc.file = LINEMAP_FILE (ord_map);
1858 xloc.line = SOURCE_LINE (ord_map, loc);
1859 xloc.column = SOURCE_COLUMN (ord_map, loc);
1860 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1863 return xloc;
1867 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1868 is NULL, use stderr. IS_MACRO is true if the caller wants to
1869 dump a macro map, false otherwise. */
1871 void
1872 linemap_dump (FILE *stream, class line_maps *set, unsigned ix, bool is_macro)
1874 const char *const lc_reasons_v[LC_HWM]
1875 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1876 "LC_ENTER_MACRO", "LC_MODULE" };
1877 const line_map *map;
1878 unsigned reason;
1880 if (stream == NULL)
1881 stream = stderr;
1883 if (!is_macro)
1885 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1886 reason = linemap_check_ordinary (map)->reason;
1888 else
1890 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1891 reason = LC_ENTER_MACRO;
1894 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1895 ix, (void *) map, map->start_location,
1896 reason < LC_HWM ? lc_reasons_v[reason] : "???",
1897 ((!is_macro
1898 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1899 ? "yes" : "no"));
1900 if (!is_macro)
1902 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1903 const line_map_ordinary *includer_map
1904 = linemap_included_from_linemap (set, ord_map);
1906 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1907 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1908 fprintf (stream, "Included from: [%d] %s\n",
1909 includer_map ? int (includer_map - set->info_ordinary.maps) : -1,
1910 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1912 else
1914 const line_map_macro *macro_map = linemap_check_macro (map);
1915 fprintf (stream, "Macro: %s (%u tokens)\n",
1916 linemap_map_get_macro_name (macro_map),
1917 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1920 fprintf (stream, "\n");
1924 /* Dump debugging information about source location LOC into the file
1925 stream STREAM. SET is the line map set LOC comes from. */
1927 void
1928 linemap_dump_location (line_maps *set,
1929 location_t loc,
1930 FILE *stream)
1932 const line_map_ordinary *map;
1933 location_t location;
1934 const char *path = "", *from = "";
1935 int l = -1, c = -1, s = -1, e = -1;
1937 if (IS_ADHOC_LOC (loc))
1938 loc = get_location_from_adhoc_loc (set, loc);
1940 if (loc == 0)
1941 return;
1943 location =
1944 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1946 if (map == NULL)
1947 /* Only reserved locations can be tolerated in this case. */
1948 linemap_assert (location < RESERVED_LOCATION_COUNT);
1949 else
1951 path = LINEMAP_FILE (map);
1952 l = SOURCE_LINE (map, location);
1953 c = SOURCE_COLUMN (map, location);
1954 s = LINEMAP_SYSP (map) != 0;
1955 e = location != loc;
1956 if (e)
1957 from = "N/A";
1958 else
1960 const line_map_ordinary *from_map
1961 = linemap_included_from_linemap (set, map);
1962 from = from_map ? LINEMAP_FILE (from_map) : "<NULL>";
1966 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1967 E: macro expansion?, LOC: original location, R: resolved location */
1968 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1969 path, from, l, c, s, (void*)map, e, loc, location);
1972 /* Return the highest location emitted for a given file for which
1973 there is a line map in SET. FILE_NAME is the file name to
1974 consider. If the function returns TRUE, *LOC is set to the highest
1975 location emitted for that file. */
1977 bool
1978 linemap_get_file_highest_location (line_maps *set,
1979 const char *file_name,
1980 location_t *loc)
1982 /* If the set is empty or no ordinary map has been created then
1983 there is no file to look for ... */
1984 if (set == NULL || set->info_ordinary.used == 0)
1985 return false;
1987 /* Now look for the last ordinary map created for FILE_NAME. */
1988 int i;
1989 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1991 const char *fname = set->info_ordinary.maps[i].to_file;
1992 if (fname && !filename_cmp (fname, file_name))
1993 break;
1996 if (i < 0)
1997 return false;
1999 /* The highest location for a given map is either the starting
2000 location of the next map minus one, or -- if the map is the
2001 latest one -- the highest location of the set. */
2002 location_t result;
2003 if (i == (int) set->info_ordinary.used - 1)
2004 result = set->highest_location;
2005 else
2006 result = set->info_ordinary.maps[i + 1].start_location - 1;
2008 *loc = result;
2009 return true;
2012 /* Compute and return statistics about the memory consumption of some
2013 parts of the line table SET. */
2015 void
2016 linemap_get_statistics (line_maps *set,
2017 struct linemap_stats *s)
2019 long ordinary_maps_allocated_size, ordinary_maps_used_size,
2020 macro_maps_allocated_size, macro_maps_used_size,
2021 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
2023 const line_map_macro *cur_map;
2025 ordinary_maps_allocated_size =
2026 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
2028 ordinary_maps_used_size =
2029 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
2031 macro_maps_allocated_size =
2032 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
2034 for (cur_map = LINEMAPS_MACRO_MAPS (set);
2035 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
2036 ++cur_map)
2038 unsigned i;
2040 linemap_assert (linemap_macro_expansion_map_p (cur_map));
2042 macro_maps_locations_size +=
2043 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (location_t);
2045 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
2047 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
2048 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
2049 duplicated_macro_maps_locations_size +=
2050 sizeof (location_t);
2054 macro_maps_used_size =
2055 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
2057 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
2058 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
2059 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
2060 s->ordinary_maps_used_size = ordinary_maps_used_size;
2061 s->num_expanded_macros = num_expanded_macros_counter;
2062 s->num_macro_tokens = num_macro_tokens_counter;
2063 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
2064 s->macro_maps_allocated_size = macro_maps_allocated_size;
2065 s->macro_maps_locations_size = macro_maps_locations_size;
2066 s->macro_maps_used_size = macro_maps_used_size;
2067 s->duplicated_macro_maps_locations_size =
2068 duplicated_macro_maps_locations_size;
2069 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
2070 * sizeof (struct location_adhoc_data));
2071 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
2075 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
2076 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
2077 specifies how many macro maps to dump. */
2079 void
2080 line_table_dump (FILE *stream, class line_maps *set, unsigned int num_ordinary,
2081 unsigned int num_macro)
2083 unsigned int i;
2085 if (set == NULL)
2086 return;
2088 if (stream == NULL)
2089 stream = stderr;
2091 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
2092 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
2093 fprintf (stream, "Include stack depth: %d\n", set->depth);
2094 fprintf (stream, "Highest location: %u\n", set->highest_location);
2096 if (num_ordinary)
2098 fprintf (stream, "\nOrdinary line maps\n");
2099 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
2100 linemap_dump (stream, set, i, false);
2101 fprintf (stream, "\n");
2104 if (num_macro)
2106 fprintf (stream, "\nMacro line maps\n");
2107 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
2108 linemap_dump (stream, set, i, true);
2109 fprintf (stream, "\n");
2113 /* class rich_location. */
2115 /* Construct a rich_location with location LOC as its initial range. */
2117 rich_location::rich_location (line_maps *set, location_t loc,
2118 const range_label *label) :
2119 m_line_table (set),
2120 m_ranges (),
2121 m_column_override (0),
2122 m_have_expanded_location (false),
2123 m_seen_impossible_fixit (false),
2124 m_fixits_cannot_be_auto_applied (false),
2125 m_escape_on_output (false),
2126 m_fixit_hints (),
2127 m_path (NULL)
2129 add_range (loc, SHOW_RANGE_WITH_CARET, label);
2132 /* The destructor for class rich_location. */
2134 rich_location::~rich_location ()
2136 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2137 delete get_fixit_hint (i);
2140 /* Get location IDX within this rich_location. */
2142 location_t
2143 rich_location::get_loc (unsigned int idx) const
2145 const location_range *locrange = get_range (idx);
2146 return locrange->m_loc;
2149 /* Get range IDX within this rich_location. */
2151 const location_range *
2152 rich_location::get_range (unsigned int idx) const
2154 return &m_ranges[idx];
2157 /* Mutable access to range IDX within this rich_location. */
2159 location_range *
2160 rich_location::get_range (unsigned int idx)
2162 return &m_ranges[idx];
2165 /* Expand location IDX within this rich_location. */
2166 /* Get an expanded_location for this rich_location's primary
2167 location. */
2169 expanded_location
2170 rich_location::get_expanded_location (unsigned int idx)
2172 if (idx == 0)
2174 /* Cache the expansion of the primary location. */
2175 if (!m_have_expanded_location)
2177 m_expanded_location
2178 = linemap_client_expand_location_to_spelling_point
2179 (get_loc (0), LOCATION_ASPECT_CARET);
2180 if (m_column_override)
2181 m_expanded_location.column = m_column_override;
2182 m_have_expanded_location = true;
2185 return m_expanded_location;
2187 else
2188 return linemap_client_expand_location_to_spelling_point
2189 (get_loc (idx), LOCATION_ASPECT_CARET);
2192 /* Set the column of the primary location, with 0 meaning
2193 "don't override it". */
2195 void
2196 rich_location::override_column (int column)
2198 m_column_override = column;
2199 m_have_expanded_location = false;
2202 /* Add the given range. */
2204 void
2205 rich_location::add_range (location_t loc,
2206 enum range_display_kind range_display_kind,
2207 const range_label *label)
2209 location_range range;
2210 range.m_loc = loc;
2211 range.m_range_display_kind = range_display_kind;
2212 range.m_label = label;
2213 m_ranges.push (range);
2216 /* Add or overwrite the location given by IDX, setting its location to LOC,
2217 and setting its m_range_display_kind to RANGE_DISPLAY_KIND.
2219 It must either overwrite an existing location, or add one *exactly* on
2220 the end of the array.
2222 This is primarily for use by gcc when implementing diagnostic format
2223 decoders e.g.
2224 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2225 (which writes the source location of a tree back into location 0 of
2226 the rich_location), and
2227 - the "%C" and "%L" format codes in the Fortran frontend. */
2229 void
2230 rich_location::set_range (unsigned int idx, location_t loc,
2231 enum range_display_kind range_display_kind)
2233 /* We can either overwrite an existing range, or add one exactly
2234 on the end of the array. */
2235 linemap_assert (idx <= m_ranges.count ());
2237 if (idx == m_ranges.count ())
2238 add_range (loc, range_display_kind);
2239 else
2241 location_range *locrange = get_range (idx);
2242 locrange->m_loc = loc;
2243 locrange->m_range_display_kind = range_display_kind;
2246 if (idx == 0)
2247 /* Mark any cached value here as dirty. */
2248 m_have_expanded_location = false;
2251 /* Methods for adding insertion fix-it hints. */
2253 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2254 immediately before the primary range's start location. */
2256 void
2257 rich_location::add_fixit_insert_before (const char *new_content)
2259 add_fixit_insert_before (get_loc (), new_content);
2262 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2263 immediately before the start of WHERE. */
2265 void
2266 rich_location::add_fixit_insert_before (location_t where,
2267 const char *new_content)
2269 location_t start = get_range_from_loc (m_line_table, where).m_start;
2270 maybe_add_fixit (start, start, new_content);
2273 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2274 immediately after the primary range's end-point. */
2276 void
2277 rich_location::add_fixit_insert_after (const char *new_content)
2279 add_fixit_insert_after (get_loc (), new_content);
2282 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2283 immediately after the end-point of WHERE. */
2285 void
2286 rich_location::add_fixit_insert_after (location_t where,
2287 const char *new_content)
2289 location_t finish = get_range_from_loc (m_line_table, where).m_finish;
2290 location_t next_loc
2291 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2293 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2294 its input value. */
2295 if (next_loc == finish)
2297 stop_supporting_fixits ();
2298 return;
2301 maybe_add_fixit (next_loc, next_loc, new_content);
2304 /* Methods for adding removal fix-it hints. */
2306 /* Add a fixit-hint, suggesting removal of the content covered
2307 by range 0. */
2309 void
2310 rich_location::add_fixit_remove ()
2312 add_fixit_remove (get_loc ());
2315 /* Add a fixit-hint, suggesting removal of the content between
2316 the start and finish of WHERE. */
2318 void
2319 rich_location::add_fixit_remove (location_t where)
2321 source_range range = get_range_from_loc (m_line_table, where);
2322 add_fixit_remove (range);
2325 /* Add a fixit-hint, suggesting removal of the content at
2326 SRC_RANGE. */
2328 void
2329 rich_location::add_fixit_remove (source_range src_range)
2331 add_fixit_replace (src_range, "");
2334 /* Add a fixit-hint, suggesting replacement of the content covered
2335 by range 0 with NEW_CONTENT. */
2337 void
2338 rich_location::add_fixit_replace (const char *new_content)
2340 add_fixit_replace (get_loc (), new_content);
2343 /* Methods for adding "replace" fix-it hints. */
2345 /* Add a fixit-hint, suggesting replacement of the content between
2346 the start and finish of WHERE with NEW_CONTENT. */
2348 void
2349 rich_location::add_fixit_replace (location_t where,
2350 const char *new_content)
2352 source_range range = get_range_from_loc (m_line_table, where);
2353 add_fixit_replace (range, new_content);
2356 /* Add a fixit-hint, suggesting replacement of the content at
2357 SRC_RANGE with NEW_CONTENT. */
2359 void
2360 rich_location::add_fixit_replace (source_range src_range,
2361 const char *new_content)
2363 location_t start = get_pure_location (m_line_table, src_range.m_start);
2364 location_t finish = get_pure_location (m_line_table, src_range.m_finish);
2366 /* Fix-it hints use half-closed ranges, so attempt to offset the endpoint. */
2367 location_t next_loc
2368 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2369 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2370 its input value. */
2371 if (next_loc == finish)
2373 stop_supporting_fixits ();
2374 return;
2376 finish = next_loc;
2378 maybe_add_fixit (start, finish, new_content);
2381 /* Get the last fix-it hint within this rich_location, or NULL if none. */
2383 fixit_hint *
2384 rich_location::get_last_fixit_hint () const
2386 if (m_fixit_hints.count () > 0)
2387 return get_fixit_hint (m_fixit_hints.count () - 1);
2388 else
2389 return NULL;
2392 /* If WHERE is an "awkward" location, then mark this rich_location as not
2393 supporting fixits, purging any thay were already added, and return true.
2395 Otherwise (the common case), return false. */
2397 bool
2398 rich_location::reject_impossible_fixit (location_t where)
2400 /* Fix-its within a rich_location should either all be suggested, or
2401 none of them should be suggested.
2402 Once we've rejected a fixit, we reject any more, even those
2403 with reasonable locations. */
2404 if (m_seen_impossible_fixit)
2405 return true;
2407 if (where <= LINE_MAP_MAX_LOCATION_WITH_COLS)
2408 /* WHERE is a reasonable location for a fix-it; don't reject it. */
2409 return false;
2411 /* Otherwise we have an attempt to add a fix-it with an "awkward"
2412 location: either one that we can't obtain column information
2413 for (within an ordinary map), or one within a macro expansion. */
2414 stop_supporting_fixits ();
2415 return true;
2418 /* Mark this rich_location as not supporting fixits, purging any that were
2419 already added. */
2421 void
2422 rich_location::stop_supporting_fixits ()
2424 m_seen_impossible_fixit = true;
2426 /* Purge the rich_location of any fix-its that were already added. */
2427 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2428 delete get_fixit_hint (i);
2429 m_fixit_hints.truncate (0);
2432 /* Add HINT to the fix-it hints in this rich_location,
2433 consolidating into the prior fixit if possible. */
2435 void
2436 rich_location::maybe_add_fixit (location_t start,
2437 location_t next_loc,
2438 const char *new_content)
2440 if (reject_impossible_fixit (start))
2441 return;
2442 if (reject_impossible_fixit (next_loc))
2443 return;
2445 /* Only allow fix-it hints that affect a single line in one file.
2446 Compare the end-points. */
2447 expanded_location exploc_start
2448 = linemap_client_expand_location_to_spelling_point (start,
2449 LOCATION_ASPECT_START);
2450 expanded_location exploc_next_loc
2451 = linemap_client_expand_location_to_spelling_point (next_loc,
2452 LOCATION_ASPECT_START);
2453 /* They must be within the same file... */
2454 if (exploc_start.file != exploc_next_loc.file)
2456 stop_supporting_fixits ();
2457 return;
2459 /* ...and on the same line. */
2460 if (exploc_start.line != exploc_next_loc.line)
2462 stop_supporting_fixits ();
2463 return;
2465 /* The columns must be in the correct order. This can fail if the
2466 endpoints straddle the boundary for which the linemap can represent
2467 columns (PR c/82050). */
2468 if (exploc_start.column > exploc_next_loc.column)
2470 stop_supporting_fixits ();
2471 return;
2473 /* If we have very long lines, tokens will eventually fall back to
2474 having column == 0.
2475 We can't handle fix-it hints that use such locations. */
2476 if (exploc_start.column == 0 || exploc_next_loc.column == 0)
2478 stop_supporting_fixits ();
2479 return;
2482 const char *newline = strchr (new_content, '\n');
2483 if (newline)
2485 /* For now, we can only support insertion of whole lines
2486 i.e. starts at start of line, and the newline is at the end of
2487 the insertion point. */
2489 /* It must be an insertion, not a replacement/deletion. */
2490 if (start != next_loc)
2492 stop_supporting_fixits ();
2493 return;
2496 /* The insertion must be at the start of a line. */
2497 if (exploc_start.column != 1)
2499 stop_supporting_fixits ();
2500 return;
2503 /* The newline must be at end of NEW_CONTENT.
2504 We could eventually split up fix-its at newlines if we wanted
2505 to allow more generality (e.g. to allow adding multiple lines
2506 with one add_fixit call. */
2507 if (newline[1] != '\0')
2509 stop_supporting_fixits ();
2510 return;
2514 /* Consolidate neighboring fixits.
2515 Don't consolidate into newline-insertion fixits. */
2516 fixit_hint *prev = get_last_fixit_hint ();
2517 if (prev && !prev->ends_with_newline_p ())
2518 if (prev->maybe_append (start, next_loc, new_content))
2519 return;
2521 m_fixit_hints.push (new fixit_hint (start, next_loc, new_content));
2524 /* class fixit_hint. */
2526 fixit_hint::fixit_hint (location_t start,
2527 location_t next_loc,
2528 const char *new_content)
2529 : m_start (start),
2530 m_next_loc (next_loc),
2531 m_bytes (xstrdup (new_content)),
2532 m_len (strlen (new_content))
2536 /* Does this fix-it hint affect the given line? */
2538 bool
2539 fixit_hint::affects_line_p (const char *file, int line) const
2541 expanded_location exploc_start
2542 = linemap_client_expand_location_to_spelling_point (m_start,
2543 LOCATION_ASPECT_START);
2544 if (file != exploc_start.file)
2545 return false;
2546 if (line < exploc_start.line)
2547 return false;
2548 expanded_location exploc_next_loc
2549 = linemap_client_expand_location_to_spelling_point (m_next_loc,
2550 LOCATION_ASPECT_START);
2551 if (file != exploc_next_loc.file)
2552 return false;
2553 if (line > exploc_next_loc.line)
2554 return false;
2555 return true;
2558 /* Method for consolidating fix-it hints, for use by
2559 rich_location::maybe_add_fixit.
2560 If possible, merge a pending fix-it hint with the given params
2561 into this one and return true.
2562 Otherwise return false. */
2564 bool
2565 fixit_hint::maybe_append (location_t start,
2566 location_t next_loc,
2567 const char *new_content)
2569 /* For consolidation to be possible, START must be at this hint's
2570 m_next_loc. */
2571 if (start != m_next_loc)
2572 return false;
2574 /* If so, we have neighboring replacements; merge them. */
2575 m_next_loc = next_loc;
2576 size_t extra_len = strlen (new_content);
2577 m_bytes = (char *)xrealloc (m_bytes, m_len + extra_len + 1);
2578 memcpy (m_bytes + m_len, new_content, extra_len);
2579 m_len += extra_len;
2580 m_bytes[m_len] = '\0';
2581 return true;
2584 /* Return true iff this hint's content ends with a newline. */
2586 bool
2587 fixit_hint::ends_with_newline_p () const
2589 if (m_len == 0)
2590 return false;
2591 return m_bytes[m_len - 1] == '\n';