[Ada] Consistently use explicit Entity_Id type instead of alias
[gcc.git] / libcpp / line-map.c
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2020 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.
17
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! */
21
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"
28
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 **);
46
47 /* Counters defined in macro.c. */
48 extern unsigned num_expanded_macros_counter;
49 extern unsigned num_macro_tokens_counter;
50
51 /* Destructor for class line_maps.
52 Ensure non-GC-managed memory is released. */
53
54 line_maps::~line_maps ()
55 {
56 if (location_adhoc_data_map.htab)
57 htab_delete (location_adhoc_data_map.htab);
58 }
59
60 /* Hash function for location_adhoc_data hashtable. */
61
62 static hashval_t
63 location_adhoc_data_hash (const void *l)
64 {
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 }
72
73 /* Compare function for location_adhoc_data hashtable. */
74
75 static int
76 location_adhoc_data_eq (const void *l1, const void *l2)
77 {
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);
86 }
87
88 /* Update the hashtable when location_adhoc_data is reallocated. */
89
90 static int
91 location_adhoc_data_update (void **slot, void *data)
92 {
93 *((char **) slot)
94 = (char *) ((uintptr_t) *((char **) slot) + *((ptrdiff_t *) data));
95 return 1;
96 }
97
98 /* Rebuild the hash table from the location adhoc data. */
99
100 void
101 rebuild_location_adhoc_htab (line_maps *set)
102 {
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);
109 }
110
111 /* Helper function for get_combined_adhoc_loc.
112 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
113 within a location_t, without needing to use an ad-hoc location. */
114
115 static bool
116 can_be_stored_compactly_p (line_maps *set,
117 location_t locus,
118 source_range src_range,
119 void *data)
120 {
121 /* If there's an ad-hoc pointer, we can't store it directly in the
122 location_t, we need the lookaside. */
123 if (data)
124 return false;
125
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;
130
131 if (src_range.m_finish < src_range.m_start)
132 return false;
133
134 if (src_range.m_start < RESERVED_LOCATION_COUNT)
135 return false;
136
137 if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
138 return false;
139
140 /* All 3 locations must be within ordinary maps, typically, the same
141 ordinary map. */
142 location_t lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
143 if (locus >= lowest_macro_loc)
144 return false;
145 if (src_range.m_start >= lowest_macro_loc)
146 return false;
147 if (src_range.m_finish >= lowest_macro_loc)
148 return false;
149
150 /* Passed all tests. */
151 return true;
152 }
153
154 /* Combine LOCUS and DATA to a combined adhoc loc. */
155
156 location_t
157 get_combined_adhoc_loc (line_maps *set,
158 location_t locus,
159 source_range src_range,
160 void *data)
161 {
162 struct location_adhoc_data lb;
163 struct location_adhoc_data **slot;
164
165 if (IS_ADHOC_LOC (locus))
166 locus = get_location_from_adhoc_loc (set, locus);
167 if (locus == 0 && data == NULL)
168 return 0;
169
170 /* Any ordinary locations ought to be "pure" at this point: no
171 compressed ranges. */
172 linemap_assert (locus < RESERVED_LOCATION_COUNT
173 || locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
174 || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
175 || pure_location_p (set, locus));
176
177 /* Consider short-range optimization. */
178 if (can_be_stored_compactly_p (set, locus, src_range, data))
179 {
180 /* The low bits ought to be clear. */
181 linemap_assert (pure_location_p (set, locus));
182 const line_map *map = linemap_lookup (set, locus);
183 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
184 unsigned int int_diff = src_range.m_finish - src_range.m_start;
185 unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
186 if (col_diff < (1U << ordmap->m_range_bits))
187 {
188 location_t packed = locus | col_diff;
189 set->num_optimized_ranges++;
190 return packed;
191 }
192 }
193
194 /* We can also compactly store locations
195 when locus == start == finish (and data is NULL). */
196 if (locus == src_range.m_start
197 && locus == src_range.m_finish
198 && !data)
199 return locus;
200
201 if (!data)
202 set->num_unoptimized_ranges++;
203
204 lb.locus = locus;
205 lb.src_range = src_range;
206 lb.data = data;
207 slot = (struct location_adhoc_data **)
208 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
209 if (*slot == NULL)
210 {
211 if (set->location_adhoc_data_map.curr_loc >=
212 set->location_adhoc_data_map.allocated)
213 {
214 char *orig_data = (char *) set->location_adhoc_data_map.data;
215 ptrdiff_t offset;
216 /* Cast away extern "C" from the type of xrealloc. */
217 line_map_realloc reallocator = (set->reallocator
218 ? set->reallocator
219 : (line_map_realloc) xrealloc);
220
221 if (set->location_adhoc_data_map.allocated == 0)
222 set->location_adhoc_data_map.allocated = 128;
223 else
224 set->location_adhoc_data_map.allocated *= 2;
225 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
226 reallocator (set->location_adhoc_data_map.data,
227 set->location_adhoc_data_map.allocated
228 * sizeof (struct location_adhoc_data));
229 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
230 if (set->location_adhoc_data_map.allocated > 128)
231 htab_traverse (set->location_adhoc_data_map.htab,
232 location_adhoc_data_update, &offset);
233 }
234 *slot = set->location_adhoc_data_map.data
235 + set->location_adhoc_data_map.curr_loc;
236 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
237 = lb;
238 }
239 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
240 }
241
242 /* Return the data for the adhoc loc. */
243
244 void *
245 get_data_from_adhoc_loc (const class line_maps *set, location_t loc)
246 {
247 linemap_assert (IS_ADHOC_LOC (loc));
248 return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].data;
249 }
250
251 /* Return the location for the adhoc loc. */
252
253 location_t
254 get_location_from_adhoc_loc (const class line_maps *set, location_t loc)
255 {
256 linemap_assert (IS_ADHOC_LOC (loc));
257 return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].locus;
258 }
259
260 /* Return the source_range for adhoc location LOC. */
261
262 static source_range
263 get_range_from_adhoc_loc (const class line_maps *set, location_t loc)
264 {
265 linemap_assert (IS_ADHOC_LOC (loc));
266 return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].src_range;
267 }
268
269 /* Get the source_range of location LOC, either from the ad-hoc
270 lookaside table, or embedded inside LOC itself. */
271
272 source_range
273 get_range_from_loc (line_maps *set,
274 location_t loc)
275 {
276 if (IS_ADHOC_LOC (loc))
277 return get_range_from_adhoc_loc (set, loc);
278
279 /* For ordinary maps, extract packed range. */
280 if (loc >= RESERVED_LOCATION_COUNT
281 && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
282 && loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
283 {
284 const line_map *map = linemap_lookup (set, loc);
285 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
286 source_range result;
287 int offset = loc & ((1 << ordmap->m_range_bits) - 1);
288 result.m_start = loc - offset;
289 result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
290 return result;
291 }
292
293 return source_range::from_location (loc);
294 }
295
296 /* Get whether location LOC is a "pure" location, or
297 whether it is an ad-hoc location, or embeds range information. */
298
299 bool
300 pure_location_p (line_maps *set, location_t loc)
301 {
302 if (IS_ADHOC_LOC (loc))
303 return false;
304
305 const line_map *map = linemap_lookup (set, loc);
306 if (map == NULL)
307 return true;
308 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
309
310 if (loc & ((1U << ordmap->m_range_bits) - 1))
311 return false;
312
313 return true;
314 }
315
316 /* Given location LOC within SET, strip away any packed range information
317 or ad-hoc information. */
318
319 location_t
320 get_pure_location (line_maps *set, location_t loc)
321 {
322 if (IS_ADHOC_LOC (loc))
323 loc = get_location_from_adhoc_loc (set, loc);
324
325 if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
326 return loc;
327
328 if (loc < RESERVED_LOCATION_COUNT)
329 return loc;
330
331 const line_map *map = linemap_lookup (set, loc);
332 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
333
334 return loc & ~((1 << ordmap->m_range_bits) - 1);
335 }
336
337 /* Initialize a line map set. */
338
339 void
340 linemap_init (line_maps *set,
341 location_t builtin_location)
342 {
343 #if __GNUC__ == 4 && __GNUC_MINOR__ == 2 && !defined (__clang__)
344 /* PR33916, needed to fix PR82939. */
345 memset (set, 0, sizeof (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;
356 }
357
358 /* Return the ordinary line map from whence MAP was included. Returns
359 NULL if MAP was not an include. */
360
361 const line_map_ordinary *
362 linemap_included_from_linemap (line_maps *set, const line_map_ordinary *map)
363 {
364 return linemap_ordinary_map_lookup (set, linemap_included_from (map));
365 }
366
367 /* Check for and warn about line_maps entered but not exited. */
368
369 void
370 linemap_check_files_exited (line_maps *set)
371 {
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));
379 }
380
381 /* Create NUM zero-initialized maps of type MACRO_P. */
382
383 line_map *
384 line_map_new_raw (line_maps *set, bool macro_p, unsigned num)
385 {
386 unsigned num_maps_allocated = LINEMAPS_ALLOCATED (set, macro_p);
387 unsigned num_maps_used = LINEMAPS_USED (set, macro_p);
388
389 if (num > num_maps_allocated - num_maps_used)
390 {
391 /* We need more space! */
392 if (!num_maps_allocated)
393 num_maps_allocated = 128;
394 if (num_maps_allocated < num_maps_used + num)
395 num_maps_allocated = num_maps_used + num;
396 num_maps_allocated *= 2;
397
398 size_t size_of_a_map;
399 void *buffer;
400 if (macro_p)
401 {
402 size_of_a_map = sizeof (line_map_macro);
403 buffer = set->info_macro.maps;
404 }
405 else
406 {
407 size_of_a_map = sizeof (line_map_ordinary);
408 buffer = set->info_ordinary.maps;
409 }
410
411 /* We are going to execute some dance to try to reduce the
412 overhead of the memory allocator, in case we are using the
413 ggc-page.c one.
414
415 The actual size of memory we are going to get back from the
416 allocator may well be larger than what we ask for. Use this
417 hook to find what that size is. */
418 size_t alloc_size
419 = set->round_alloc_size (num_maps_allocated * size_of_a_map);
420
421 /* Now alloc_size contains the exact memory size we would get if
422 we have asked for the initial alloc_size amount of memory.
423 Let's get back to the number of map that amounts to. */
424 unsigned num_maps = alloc_size / size_of_a_map;
425 buffer = set->reallocator (buffer, num_maps * size_of_a_map);
426 memset ((char *)buffer + num_maps_used * size_of_a_map, 0,
427 (num_maps - num_maps_used) * size_of_a_map);
428 if (macro_p)
429 set->info_macro.maps = (line_map_macro *)buffer;
430 else
431 set->info_ordinary.maps = (line_map_ordinary *)buffer;
432 LINEMAPS_ALLOCATED (set, macro_p) = num_maps;
433 }
434
435 line_map *result = (macro_p ? (line_map *)&set->info_macro.maps[num_maps_used]
436 : (line_map *)&set->info_ordinary.maps[num_maps_used]);
437 LINEMAPS_USED (set, macro_p) += num;
438
439 return result;
440 }
441
442 /* Create a new line map in the line map set SET, and return it.
443 REASON is the reason of creating the map. It determines the type
444 of map created (ordinary or macro map). Note that ordinary maps and
445 macro maps are allocated in different memory location. */
446
447 static struct line_map *
448 new_linemap (line_maps *set, location_t start_location)
449 {
450 line_map *result = line_map_new_raw (set,
451 start_location >= LINE_MAP_MAX_LOCATION,
452 1);
453
454 result->start_location = start_location;
455
456 return result;
457 }
458
459 /* Return the location of the last source line within an ordinary
460 map. */
461 inline location_t
462 LAST_SOURCE_LINE_LOCATION (const line_map_ordinary *map)
463 {
464 return (((map[1].start_location - 1
465 - map->start_location)
466 & ~((1 << map->m_column_and_range_bits) - 1))
467 + map->start_location);
468 }
469
470 /* Add a mapping of logical source line to physical source file and
471 line number.
472
473 The text pointed to by TO_FILE must have a lifetime
474 at least as long as the final call to lookup_line (). An empty
475 TO_FILE means standard input. If reason is LC_LEAVE, and
476 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
477 natural values considering the file we are returning to.
478
479 FROM_LINE should be monotonic increasing across calls to this
480 function. A call to this function can relocate the previous set of
481 maps, so any stored line_map pointers should not be used. */
482
483 const struct line_map *
484 linemap_add (line_maps *set, enum lc_reason reason,
485 unsigned int sysp, const char *to_file, linenum_type to_line)
486 {
487 /* Generate a start_location above the current highest_location.
488 If possible, make the low range bits be zero. */
489 location_t start_location = set->highest_location + 1;
490 unsigned range_bits = 0;
491 if (start_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
492 range_bits = set->default_range_bits;
493 start_location += (1 << range_bits) - 1;
494 start_location &= ~((1 << range_bits) - 1);
495
496 linemap_assert (!LINEMAPS_ORDINARY_USED (set)
497 || (start_location
498 >= MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set))));
499
500 /* When we enter the file for the first time reason cannot be
501 LC_RENAME. */
502 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
503
504 /* If we are leaving the main file, return a NULL map. */
505 if (reason == LC_LEAVE
506 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
507 && to_file == NULL)
508 {
509 set->depth--;
510 return NULL;
511 }
512
513 linemap_assert (reason != LC_ENTER_MACRO);
514
515 if (start_location >= LINE_MAP_MAX_LOCATION)
516 /* We ran out of line map space. */
517 start_location = 0;
518
519 line_map_ordinary *map
520 = linemap_check_ordinary (new_linemap (set, start_location));
521 map->reason = reason;
522
523 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
524 to_file = "<stdin>";
525
526 if (reason == LC_RENAME_VERBATIM)
527 reason = LC_RENAME;
528
529 const line_map_ordinary *from = NULL;
530 if (reason == LC_LEAVE)
531 {
532 /* When we are just leaving an "included" file, and jump to the next
533 location inside the "includer" right after the #include
534 "included", this variable points the map in use right before the
535 #include "included", inside the same "includer" file. */
536
537 linemap_assert (!MAIN_FILE_P (map - 1));
538 /* (MAP - 1) points to the map we are leaving. The
539 map from which (MAP - 1) got included should be the map
540 that comes right before MAP in the same file. */
541 from = linemap_included_from_linemap (set, map - 1);
542
543 /* A TO_FILE of NULL is special - we use the natural values. */
544 if (to_file == NULL)
545 {
546 to_file = ORDINARY_MAP_FILE_NAME (from);
547 to_line = SOURCE_LINE (from, from[1].start_location);
548 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
549 }
550 else
551 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from),
552 to_file) == 0);
553 }
554
555 map->sysp = sysp;
556 map->to_file = to_file;
557 map->to_line = to_line;
558 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
559 /* Do not store range_bits here. That's readjusted in
560 linemap_line_start. */
561 map->m_range_bits = map->m_column_and_range_bits = 0;
562 set->highest_location = start_location;
563 set->highest_line = start_location;
564 set->max_column_hint = 0;
565
566 /* This assertion is placed after set->highest_location has
567 been updated, since the latter affects
568 linemap_location_from_macro_expansion_p, which ultimately affects
569 pure_location_p. */
570 linemap_assert (pure_location_p (set, start_location));
571
572 if (reason == LC_ENTER)
573 {
574 if (set->depth == 0)
575 map->included_from = 0;
576 else
577 /* The location of the end of the just-closed map. */
578 map->included_from
579 = (((map[0].start_location - 1 - map[-1].start_location)
580 & ~((1 << map[-1].m_column_and_range_bits) - 1))
581 + map[-1].start_location);
582 set->depth++;
583 if (set->trace_includes)
584 trace_include (set, map);
585 }
586 else if (reason == LC_RENAME)
587 map->included_from = linemap_included_from (&map[-1]);
588 else if (reason == LC_LEAVE)
589 {
590 set->depth--;
591 map->included_from = linemap_included_from (from);
592 }
593
594 return map;
595 }
596
597 /* Create a location for a module NAME imported at FROM. */
598
599 location_t
600 linemap_module_loc (line_maps *set, location_t from, const char *name)
601 {
602 const line_map_ordinary *map
603 = linemap_check_ordinary (linemap_add (set, LC_MODULE, false, name, 0));
604 const_cast <line_map_ordinary *> (map)->included_from = from;
605
606 location_t loc = linemap_line_start (set, 0, 0);
607
608 return loc;
609 }
610
611 /* The linemap containing LOC is being reparented to be
612 imported/included from ADOPTOR. This can happen when an
613 indirectly imported module is then directly imported, or when
614 partitions are involved. */
615
616 void
617 linemap_module_reparent (line_maps *set, location_t loc, location_t adoptor)
618 {
619 const line_map_ordinary *map = linemap_ordinary_map_lookup (set, loc);
620 const_cast<line_map_ordinary *> (map)->included_from = adoptor;
621 }
622
623 /* A linemap at LWM-1 was interrupted to insert module locations & imports.
624 Append a new map, continuing the interrupted one. */
625
626 void
627 linemap_module_restore (line_maps *set, unsigned lwm)
628 {
629 if (lwm && lwm != LINEMAPS_USED (set, false))
630 {
631 const line_map_ordinary *pre_map
632 = linemap_check_ordinary (LINEMAPS_MAP_AT (set, false, lwm - 1));
633 unsigned src_line = SOURCE_LINE (pre_map,
634 LAST_SOURCE_LINE_LOCATION (pre_map));
635 location_t inc_at = pre_map->included_from;
636 if (const line_map_ordinary *post_map
637 = (linemap_check_ordinary
638 (linemap_add (set, LC_RENAME_VERBATIM,
639 ORDINARY_MAP_IN_SYSTEM_HEADER_P (pre_map),
640 ORDINARY_MAP_FILE_NAME (pre_map), src_line))))
641 /* linemap_add will think we were included from the same as
642 the preceeding map. */
643 const_cast <line_map_ordinary *> (post_map)->included_from = inc_at;
644 }
645 }
646
647 /* Returns TRUE if the line table set tracks token locations across
648 macro expansion, FALSE otherwise. */
649
650 bool
651 linemap_tracks_macro_expansion_locs_p (line_maps *set)
652 {
653 return LINEMAPS_MACRO_MAPS (set) != NULL;
654 }
655
656 /* Create a macro map. A macro map encodes source locations of tokens
657 that are part of a macro replacement-list, at a macro expansion
658 point. See the extensive comments of struct line_map and struct
659 line_map_macro, in line-map.h.
660
661 This map shall be created when the macro is expanded. The map
662 encodes the source location of the expansion point of the macro as
663 well as the "original" source location of each token that is part
664 of the macro replacement-list. If a macro is defined but never
665 expanded, it has no macro map. SET is the set of maps the macro
666 map should be part of. MACRO_NODE is the macro which the new macro
667 map should encode source locations for. EXPANSION is the location
668 of the expansion point of MACRO. For function-like macros
669 invocations, it's best to make it point to the closing parenthesis
670 of the macro, rather than the the location of the first character
671 of the macro. NUM_TOKENS is the number of tokens that are part of
672 the replacement-list of MACRO.
673
674 Note that when we run out of the integer space available for source
675 locations, this function returns NULL. In that case, callers of
676 this function cannot encode {line,column} pairs into locations of
677 macro tokens anymore. */
678
679 const line_map_macro *
680 linemap_enter_macro (class line_maps *set, struct cpp_hashnode *macro_node,
681 location_t expansion, unsigned int num_tokens)
682 {
683 location_t start_location
684 = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
685
686 if (start_location < LINE_MAP_MAX_LOCATION)
687 /* We ran out of macro map space. */
688 return NULL;
689
690 line_map_macro *map = linemap_check_macro (new_linemap (set, start_location));
691
692 map->macro = macro_node;
693 map->n_tokens = num_tokens;
694 map->macro_locations
695 = (location_t*) set->reallocator (NULL,
696 2 * num_tokens
697 * sizeof (location_t));
698 map->expansion = expansion;
699 memset (MACRO_MAP_LOCATIONS (map), 0,
700 2 * num_tokens * sizeof (location_t));
701
702 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
703
704 return map;
705 }
706
707 /* Create and return a virtual location for a token that is part of a
708 macro expansion-list at a macro expansion point. See the comment
709 inside struct line_map_macro to see what an expansion-list exactly
710 is.
711
712 A call to this function must come after a call to
713 linemap_enter_macro.
714
715 MAP is the map into which the source location is created. TOKEN_NO
716 is the index of the token in the macro replacement-list, starting
717 at number 0.
718
719 ORIG_LOC is the location of the token outside of this macro
720 expansion. If the token comes originally from the macro
721 definition, it is the locus in the macro definition; otherwise it
722 is a location in the context of the caller of this macro expansion
723 (which is a virtual location or a source location if the caller is
724 itself a macro expansion or not).
725
726 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
727 either of the token itself or of a macro parameter that it
728 replaces. */
729
730 location_t
731 linemap_add_macro_token (const line_map_macro *map,
732 unsigned int token_no,
733 location_t orig_loc,
734 location_t orig_parm_replacement_loc)
735 {
736 location_t result;
737
738 linemap_assert (linemap_macro_expansion_map_p (map));
739 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
740
741 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
742 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
743
744 result = MAP_START_LOCATION (map) + token_no;
745 return result;
746 }
747
748 /* Return a location_t for the start (i.e. column==0) of
749 (physical) line TO_LINE in the current source file (as in the
750 most recent linemap_add). MAX_COLUMN_HINT is the highest column
751 number we expect to use in this line (but it does not change
752 the highest_location). */
753
754 location_t
755 linemap_line_start (line_maps *set, linenum_type to_line,
756 unsigned int max_column_hint)
757 {
758 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
759 location_t highest = set->highest_location;
760 location_t r;
761 linenum_type last_line =
762 SOURCE_LINE (map, set->highest_line);
763 int line_delta = to_line - last_line;
764 bool add_map = false;
765 linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
766 int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
767
768 if (line_delta < 0
769 || (line_delta > 10
770 && line_delta * map->m_column_and_range_bits > 1000)
771 || (max_column_hint >= (1U << effective_column_bits))
772 || (max_column_hint <= 80 && effective_column_bits >= 10)
773 || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
774 && map->m_range_bits > 0)
775 || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
776 && (set->max_column_hint || highest >= LINE_MAP_MAX_LOCATION)))
777 add_map = true;
778 else
779 max_column_hint = set->max_column_hint;
780 if (add_map)
781 {
782 int column_bits;
783 int range_bits;
784 if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
785 || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
786 {
787 /* If the column number is ridiculous or we've allocated a huge
788 number of location_ts, give up on column numbers
789 (and on packed ranges). */
790 max_column_hint = 1;
791 column_bits = 0;
792 range_bits = 0;
793 if (highest >= LINE_MAP_MAX_LOCATION)
794 goto overflowed;
795 }
796 else
797 {
798 column_bits = 7;
799 if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
800 range_bits = set->default_range_bits;
801 else
802 range_bits = 0;
803 while (max_column_hint >= (1U << column_bits))
804 column_bits++;
805 max_column_hint = 1U << column_bits;
806 column_bits += range_bits;
807 }
808
809 /* Allocate the new line_map. However, if the current map only has a
810 single line we can sometimes just increase its column_bits instead. */
811 if (line_delta < 0
812 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
813 || SOURCE_COLUMN (map, highest) >= (1U << (column_bits - range_bits))
814 || ( /* We can't reuse the map if the line offset is sufficiently
815 large to cause overflow when computing location_t values. */
816 (to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
817 >= (((uint64_t) 1)
818 << (CHAR_BIT * sizeof (linenum_type) - column_bits)))
819 || range_bits < map->m_range_bits)
820 map = linemap_check_ordinary
821 (const_cast <line_map *>
822 (linemap_add (set, LC_RENAME,
823 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
824 ORDINARY_MAP_FILE_NAME (map),
825 to_line)));
826 map->m_column_and_range_bits = column_bits;
827 map->m_range_bits = range_bits;
828 r = (MAP_START_LOCATION (map)
829 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
830 << column_bits));
831 }
832 else
833 r = set->highest_line + (line_delta << map->m_column_and_range_bits);
834
835 /* Locations of ordinary tokens are always lower than locations of
836 macro tokens. */
837 if (r >= LINE_MAP_MAX_LOCATION)
838 {
839 overflowed:
840 /* Remember we overflowed. */
841 set->highest_line = set->highest_location = LINE_MAP_MAX_LOCATION - 1;
842 /* No column numbers! */
843 set->max_column_hint = 1;
844 return 0;
845 }
846
847 set->highest_line = r;
848 if (r > set->highest_location)
849 set->highest_location = r;
850 set->max_column_hint = max_column_hint;
851
852 /* At this point, we expect one of:
853 (a) the normal case: a "pure" location with 0 range bits, or
854 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
855 columns anymore (or ranges), or
856 (c) we're in a region with a column hint exceeding
857 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
858 with column_bits == 0. */
859 linemap_assert (pure_location_p (set, r)
860 || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
861 || map->m_column_and_range_bits == 0);
862 linemap_assert (SOURCE_LINE (map, r) == to_line);
863 return r;
864 }
865
866 /* Encode and return a location_t from a column number. The
867 source line considered is the last source line used to call
868 linemap_line_start, i.e, the last source line which a location was
869 encoded from. */
870
871 location_t
872 linemap_position_for_column (line_maps *set, unsigned int to_column)
873 {
874 location_t r = set->highest_line;
875
876 linemap_assert
877 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
878
879 if (to_column >= set->max_column_hint)
880 {
881 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
882 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
883 {
884 /* Running low on location_ts - disable column numbers. */
885 return r;
886 }
887 else
888 {
889 /* Otherwise, attempt to start a new line that can hold TO_COLUMN,
890 with some space to spare. This may or may not lead to a new
891 linemap being created. */
892 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
893 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
894 map = LINEMAPS_LAST_ORDINARY_MAP (set);
895 if (map->m_column_and_range_bits == 0)
896 {
897 /* ...then the linemap has column-tracking disabled,
898 presumably due to exceeding either
899 LINE_MAP_MAX_LOCATION_WITH_COLS (overall) or
900 LINE_MAP_MAX_COLUMN_NUMBER (within this line).
901 Return the start of the linemap, which encodes column 0, for
902 the whole line. */
903 return r;
904 }
905 }
906 }
907 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
908 r = r + (to_column << map->m_range_bits);
909 if (r >= set->highest_location)
910 set->highest_location = r;
911 return r;
912 }
913
914 /* Encode and return a source location from a given line and
915 column. */
916
917 location_t
918 linemap_position_for_line_and_column (line_maps *set,
919 const line_map_ordinary *ord_map,
920 linenum_type line,
921 unsigned column)
922 {
923 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
924
925 location_t r = MAP_START_LOCATION (ord_map);
926 r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
927 << ord_map->m_column_and_range_bits);
928 if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
929 r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
930 << ord_map->m_range_bits);
931 location_t upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
932 if (r >= upper_limit)
933 r = upper_limit - 1;
934 if (r > set->highest_location)
935 set->highest_location = r;
936 return r;
937 }
938
939 /* Encode and return a location_t starting from location LOC and
940 shifting it by COLUMN_OFFSET columns. This function does not support
941 virtual locations. */
942
943 location_t
944 linemap_position_for_loc_and_offset (line_maps *set,
945 location_t loc,
946 unsigned int column_offset)
947 {
948 const line_map_ordinary * map = NULL;
949
950 if (IS_ADHOC_LOC (loc))
951 loc = get_location_from_adhoc_loc (set, loc);
952
953 /* This function does not support virtual locations yet. */
954 if (linemap_location_from_macro_expansion_p (set, loc))
955 return loc;
956
957 if (column_offset == 0
958 /* Adding an offset to a reserved location (like
959 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
960 sense. So let's leave the location intact in that case. */
961 || loc < RESERVED_LOCATION_COUNT)
962 return loc;
963
964 /* We find the real location and shift it. */
965 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
966 /* The new location (loc + offset) should be higher than the first
967 location encoded by MAP. This can fail if the line information
968 is messed up because of line directives (see PR66415). */
969 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
970 return loc;
971
972 linenum_type line = SOURCE_LINE (map, loc);
973 unsigned int column = SOURCE_COLUMN (map, loc);
974
975 /* If MAP is not the last line map of its set, then the new location
976 (loc + offset) should be less than the first location encoded by
977 the next line map of the set. Otherwise, we try to encode the
978 location in the next map. */
979 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
980 && (loc + (column_offset << map->m_range_bits)
981 >= MAP_START_LOCATION (&map[1])))
982 {
983 map = &map[1];
984 /* If the next map starts in a higher line, we cannot encode the
985 location there. */
986 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
987 return loc;
988 }
989
990 column += column_offset;
991
992 /* Bail out if the column is not representable within the existing
993 linemap. */
994 if (column >= (1u << (map->m_column_and_range_bits - map->m_range_bits)))
995 return loc;
996
997 location_t r =
998 linemap_position_for_line_and_column (set, map, line, column);
999 if (linemap_assert_fails (r <= set->highest_location)
1000 || linemap_assert_fails (map == linemap_lookup (set, r)))
1001 return loc;
1002
1003 return r;
1004 }
1005
1006 /* Given a virtual source location yielded by a map (either an
1007 ordinary or a macro map), returns that map. */
1008
1009 const struct line_map*
1010 linemap_lookup (const line_maps *set, location_t line)
1011 {
1012 if (IS_ADHOC_LOC (line))
1013 line = get_location_from_adhoc_loc (set, line);
1014 if (linemap_location_from_macro_expansion_p (set, line))
1015 return linemap_macro_map_lookup (set, line);
1016 return linemap_ordinary_map_lookup (set, line);
1017 }
1018
1019 /* Given a source location yielded by an ordinary map, returns that
1020 map. Since the set is built chronologically, the logical lines are
1021 monotonic increasing, and so the list is sorted and we can use a
1022 binary search. */
1023
1024 static const line_map_ordinary *
1025 linemap_ordinary_map_lookup (const line_maps *set, location_t line)
1026 {
1027 if (IS_ADHOC_LOC (line))
1028 line = get_location_from_adhoc_loc (set, line);
1029
1030 if (set == NULL || line < RESERVED_LOCATION_COUNT)
1031 return NULL;
1032
1033 unsigned mn = LINEMAPS_ORDINARY_CACHE (set);
1034 unsigned mx = LINEMAPS_ORDINARY_USED (set);
1035
1036 const line_map_ordinary *cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
1037 /* We should get a segfault if no line_maps have been added yet. */
1038 if (line >= MAP_START_LOCATION (cached))
1039 {
1040 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
1041 return cached;
1042 }
1043 else
1044 {
1045 mx = mn;
1046 mn = 0;
1047 }
1048
1049 while (mx - mn > 1)
1050 {
1051 unsigned md = (mn + mx) / 2;
1052 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
1053 mx = md;
1054 else
1055 mn = md;
1056 }
1057
1058 LINEMAPS_ORDINARY_CACHE (set) = mn;
1059 const line_map_ordinary *result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
1060 linemap_assert (line >= MAP_START_LOCATION (result));
1061 return result;
1062 }
1063
1064 /* Given a source location yielded by a macro map, returns that map.
1065 Since the set is built chronologically, the logical lines are
1066 monotonic decreasing, and so the list is sorted and we can use a
1067 binary search. */
1068
1069 static const line_map_macro *
1070 linemap_macro_map_lookup (const line_maps *set, location_t line)
1071 {
1072 if (IS_ADHOC_LOC (line))
1073 line = get_location_from_adhoc_loc (set, line);
1074
1075 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
1076
1077 if (set == NULL)
1078 return NULL;
1079
1080 unsigned ix = linemap_lookup_macro_index (set, line);
1081 const struct line_map_macro *result = LINEMAPS_MACRO_MAP_AT (set, ix);
1082 linemap_assert (MAP_START_LOCATION (result) <= line);
1083
1084 return result;
1085 }
1086
1087 unsigned
1088 linemap_lookup_macro_index (const line_maps *set, location_t line)
1089 {
1090 unsigned mn = LINEMAPS_MACRO_CACHE (set);
1091 unsigned mx = LINEMAPS_MACRO_USED (set);
1092 const struct line_map_macro *cached = LINEMAPS_MACRO_MAP_AT (set, mn);
1093
1094 if (line >= MAP_START_LOCATION (cached))
1095 {
1096 if (line < (MAP_START_LOCATION (cached)
1097 + MACRO_MAP_NUM_MACRO_TOKENS (cached)))
1098 return mn;
1099 mx = mn - 1;
1100 mn = 0;
1101 }
1102
1103 while (mn < mx)
1104 {
1105 unsigned md = (mx + mn) / 2;
1106 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1107 mn = md + 1;
1108 else
1109 mx = md;
1110 }
1111
1112 LINEMAPS_MACRO_CACHE (set) = mx;
1113 return mx;
1114 }
1115
1116 /* Return TRUE if MAP encodes locations coming from a macro
1117 replacement-list at macro expansion point. */
1118
1119 bool
1120 linemap_macro_expansion_map_p (const struct line_map *map)
1121 {
1122 return map && !MAP_ORDINARY_P (map);
1123 }
1124
1125 /* If LOCATION is the locus of a token in a replacement-list of a
1126 macro expansion return the location of the macro expansion point.
1127
1128 Read the comments of struct line_map and struct line_map_macro in
1129 line-map.h to understand what a macro expansion point is. */
1130
1131 static location_t
1132 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1133 location_t location ATTRIBUTE_UNUSED)
1134 {
1135 linemap_assert (linemap_macro_expansion_map_p (map)
1136 && location >= MAP_START_LOCATION (map));
1137
1138 /* Make sure LOCATION is correct. */
1139 linemap_assert ((location - MAP_START_LOCATION (map))
1140 < MACRO_MAP_NUM_MACRO_TOKENS (map));
1141
1142 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1143 }
1144
1145 /* LOCATION is the source location of a token that belongs to a macro
1146 replacement-list as part of the macro expansion denoted by MAP.
1147
1148 Return the location of the token at the definition point of the
1149 macro. */
1150
1151 static location_t
1152 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1153 location_t location)
1154 {
1155 unsigned token_no;
1156
1157 linemap_assert (linemap_macro_expansion_map_p (map)
1158 && location >= MAP_START_LOCATION (map));
1159 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1160
1161 token_no = location - MAP_START_LOCATION (map);
1162 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1163
1164 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1165
1166 return location;
1167 }
1168
1169 /* If LOCATION is the locus of a token that is an argument of a
1170 function-like macro M and appears in the expansion of M, return the
1171 locus of that argument in the context of the caller of M.
1172
1173 In other words, this returns the xI location presented in the
1174 comments of line_map_macro above. */
1175 location_t
1176 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1177 const line_map_macro* map,
1178 location_t location)
1179 {
1180 unsigned token_no;
1181
1182 if (IS_ADHOC_LOC (location))
1183 location = get_location_from_adhoc_loc (set, location);
1184
1185 linemap_assert (linemap_macro_expansion_map_p (map)
1186 && location >= MAP_START_LOCATION (map));
1187 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1188 linemap_assert (!IS_ADHOC_LOC (location));
1189
1190 token_no = location - MAP_START_LOCATION (map);
1191 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1192
1193 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1194
1195 return location;
1196 }
1197
1198 /* Return the source line number corresponding to source location
1199 LOCATION. SET is the line map set LOCATION comes from. If
1200 LOCATION is the source location of token that is part of the
1201 replacement-list of a macro expansion return the line number of the
1202 macro expansion point. */
1203
1204 int
1205 linemap_get_expansion_line (line_maps *set,
1206 location_t location)
1207 {
1208 const line_map_ordinary *map = NULL;
1209
1210 if (IS_ADHOC_LOC (location))
1211 location = get_location_from_adhoc_loc (set, location);
1212
1213 if (location < RESERVED_LOCATION_COUNT)
1214 return 0;
1215
1216 location =
1217 linemap_macro_loc_to_exp_point (set, location, &map);
1218
1219 return SOURCE_LINE (map, location);
1220 }
1221
1222 /* Return the path of the file corresponding to source code location
1223 LOCATION.
1224
1225 If LOCATION is the source location of token that is part of the
1226 replacement-list of a macro expansion return the file path of the
1227 macro expansion point.
1228
1229 SET is the line map set LOCATION comes from. */
1230
1231 const char*
1232 linemap_get_expansion_filename (line_maps *set,
1233 location_t location)
1234 {
1235 const struct line_map_ordinary *map = NULL;
1236
1237 if (IS_ADHOC_LOC (location))
1238 location = get_location_from_adhoc_loc (set, location);
1239
1240 if (location < RESERVED_LOCATION_COUNT)
1241 return NULL;
1242
1243 linemap_macro_loc_to_exp_point (set, location, &map);
1244
1245 return LINEMAP_FILE (map);
1246 }
1247
1248 /* Return the name of the macro associated to MACRO_MAP. */
1249
1250 const char*
1251 linemap_map_get_macro_name (const line_map_macro *macro_map)
1252 {
1253 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1254 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1255 }
1256
1257 /* Return a positive value if LOCATION is the locus of a token that is
1258 located in a system header, O otherwise. It returns 1 if LOCATION
1259 is the locus of a token that is located in a system header, and 2
1260 if LOCATION is the locus of a token located in a C system header
1261 that therefore needs to be extern "C" protected in C++.
1262
1263 Note that this function returns 1 if LOCATION belongs to a token
1264 that is part of a macro replacement-list defined in a system
1265 header, but expanded in a non-system file. */
1266
1267 int
1268 linemap_location_in_system_header_p (line_maps *set,
1269 location_t location)
1270 {
1271 const struct line_map *map = NULL;
1272
1273 if (IS_ADHOC_LOC (location))
1274 location = get_location_from_adhoc_loc (set, location);
1275
1276 if (location < RESERVED_LOCATION_COUNT)
1277 return false;
1278
1279 /* Let's look at where the token for LOCATION comes from. */
1280 while (true)
1281 {
1282 map = linemap_lookup (set, location);
1283 if (map != NULL)
1284 {
1285 if (!linemap_macro_expansion_map_p (map))
1286 /* It's a normal token. */
1287 return LINEMAP_SYSP (linemap_check_ordinary (map));
1288 else
1289 {
1290 const line_map_macro *macro_map = linemap_check_macro (map);
1291
1292 /* It's a token resulting from a macro expansion. */
1293 location_t loc =
1294 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
1295 if (loc < RESERVED_LOCATION_COUNT)
1296 /* This token might come from a built-in macro. Let's
1297 look at where that macro got expanded. */
1298 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1299 else
1300 location = loc;
1301 }
1302 }
1303 else
1304 break;
1305 }
1306 return false;
1307 }
1308
1309 /* Return TRUE if LOCATION is a source code location of a token that is part of
1310 a macro expansion, FALSE otherwise. */
1311
1312 bool
1313 linemap_location_from_macro_expansion_p (const class line_maps *set,
1314 location_t location)
1315 {
1316 if (IS_ADHOC_LOC (location))
1317 location = get_location_from_adhoc_loc (set, location);
1318
1319 return IS_MACRO_LOC (location);
1320 }
1321
1322 /* Given two virtual locations *LOC0 and *LOC1, return the first
1323 common macro map in their macro expansion histories. Return NULL
1324 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1325 virtual location of the token inside the resulting macro. */
1326
1327 static const struct line_map*
1328 first_map_in_common_1 (line_maps *set,
1329 location_t *loc0,
1330 location_t *loc1)
1331 {
1332 location_t l0 = *loc0, l1 = *loc1;
1333 const struct line_map *map0 = linemap_lookup (set, l0);
1334 if (IS_ADHOC_LOC (l0))
1335 l0 = get_location_from_adhoc_loc (set, l0);
1336
1337 const struct line_map *map1 = linemap_lookup (set, l1);
1338 if (IS_ADHOC_LOC (l1))
1339 l1 = get_location_from_adhoc_loc (set, l1);
1340
1341 while (linemap_macro_expansion_map_p (map0)
1342 && linemap_macro_expansion_map_p (map1)
1343 && (map0 != map1))
1344 {
1345 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1346 {
1347 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1348 l0);
1349 map0 = linemap_lookup (set, l0);
1350 }
1351 else
1352 {
1353 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1354 l1);
1355 map1 = linemap_lookup (set, l1);
1356 }
1357 }
1358
1359 if (map0 == map1)
1360 {
1361 *loc0 = l0;
1362 *loc1 = l1;
1363 return map0;
1364 }
1365 return NULL;
1366 }
1367
1368 /* Given two virtual locations LOC0 and LOC1, return the first common
1369 macro map in their macro expansion histories. Return NULL if no
1370 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1371 virtual location of the token inside the resulting macro, upon
1372 return of a non-NULL result. */
1373
1374 const struct line_map*
1375 first_map_in_common (line_maps *set,
1376 location_t loc0,
1377 location_t loc1,
1378 location_t *res_loc0,
1379 location_t *res_loc1)
1380 {
1381 *res_loc0 = loc0;
1382 *res_loc1 = loc1;
1383
1384 return first_map_in_common_1 (set, res_loc0, res_loc1);
1385 }
1386
1387 /* Return a positive value if PRE denotes the location of a token that
1388 comes before the token of POST, 0 if PRE denotes the location of
1389 the same token as the token for POST, and a negative value
1390 otherwise. */
1391
1392 int
1393 linemap_compare_locations (line_maps *set,
1394 location_t pre,
1395 location_t post)
1396 {
1397 bool pre_virtual_p, post_virtual_p;
1398 location_t l0 = pre, l1 = post;
1399
1400 if (IS_ADHOC_LOC (l0))
1401 l0 = get_location_from_adhoc_loc (set, l0);
1402 if (IS_ADHOC_LOC (l1))
1403 l1 = get_location_from_adhoc_loc (set, l1);
1404
1405 if (l0 == l1)
1406 return 0;
1407
1408 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1409 l0 = linemap_resolve_location (set, l0,
1410 LRK_MACRO_EXPANSION_POINT,
1411 NULL);
1412
1413 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1414 l1 = linemap_resolve_location (set, l1,
1415 LRK_MACRO_EXPANSION_POINT,
1416 NULL);
1417
1418 if (l0 == l1
1419 && pre_virtual_p
1420 && post_virtual_p)
1421 {
1422 /* So pre and post represent two tokens that are present in a
1423 same macro expansion. Let's see if the token for pre was
1424 before the token for post in that expansion. */
1425 unsigned i0, i1;
1426 const struct line_map *map =
1427 first_map_in_common (set, pre, post, &l0, &l1);
1428
1429 if (map == NULL)
1430 /* This should not be possible. */
1431 abort ();
1432
1433 i0 = l0 - MAP_START_LOCATION (map);
1434 i1 = l1 - MAP_START_LOCATION (map);
1435 return i1 - i0;
1436 }
1437
1438 if (IS_ADHOC_LOC (l0))
1439 l0 = get_location_from_adhoc_loc (set, l0);
1440 if (IS_ADHOC_LOC (l1))
1441 l1 = get_location_from_adhoc_loc (set, l1);
1442
1443 return l1 - l0;
1444 }
1445
1446 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1447
1448 static void
1449 trace_include (const class line_maps *set, const line_map_ordinary *map)
1450 {
1451 unsigned int i = set->depth;
1452
1453 while (--i)
1454 putc ('.', stderr);
1455
1456 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1457 }
1458
1459 /* Return the spelling location of the token wherever it comes from,
1460 whether part of a macro definition or not.
1461
1462 This is a subroutine for linemap_resolve_location. */
1463
1464 static location_t
1465 linemap_macro_loc_to_spelling_point (line_maps *set,
1466 location_t location,
1467 const line_map_ordinary **original_map)
1468 {
1469 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1470
1471 while (true)
1472 {
1473 const struct line_map *map = linemap_lookup (set, location);
1474 if (!map || MAP_ORDINARY_P (map))
1475 {
1476 if (original_map)
1477 *original_map = (const line_map_ordinary *)map;
1478 break;
1479 }
1480
1481 location = linemap_macro_map_loc_unwind_toward_spelling
1482 (set, linemap_check_macro (map), location);
1483 }
1484
1485 return location;
1486 }
1487
1488 /* If LOCATION is the source location of a token that belongs to a
1489 macro replacement-list -- as part of a macro expansion -- then
1490 return the location of the token at the definition point of the
1491 macro. Otherwise, return LOCATION. SET is the set of maps
1492 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1493 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1494 returned location comes from.
1495
1496 This is a subroutine of linemap_resolve_location. */
1497
1498 static location_t
1499 linemap_macro_loc_to_def_point (line_maps *set,
1500 location_t location,
1501 const line_map_ordinary **original_map)
1502 {
1503 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1504
1505 for (;;)
1506 {
1507 location_t caret_loc = location;
1508 if (IS_ADHOC_LOC (caret_loc))
1509 caret_loc = get_location_from_adhoc_loc (set, caret_loc);
1510
1511 const line_map *map = linemap_lookup (set, caret_loc);
1512 if (!map || MAP_ORDINARY_P (map))
1513 {
1514 if (original_map)
1515 *original_map = (const line_map_ordinary *)map;
1516 break;
1517 }
1518
1519 location = linemap_macro_map_loc_to_def_point
1520 (linemap_check_macro (map), caret_loc);
1521 }
1522
1523 return location;
1524 }
1525
1526 /* If LOCATION is the source location of a token that belongs to a
1527 macro replacement-list -- at a macro expansion point -- then return
1528 the location of the topmost expansion point of the macro. We say
1529 topmost because if we are in the context of a nested macro
1530 expansion, the function returns the source location of the first
1531 macro expansion that triggered the nested expansions.
1532
1533 Otherwise, return LOCATION. SET is the set of maps location come
1534 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1535 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1536 location comes from.
1537
1538 This is a subroutine of linemap_resolve_location. */
1539
1540 static location_t
1541 linemap_macro_loc_to_exp_point (line_maps *set,
1542 location_t location,
1543 const line_map_ordinary **original_map)
1544 {
1545 struct line_map *map;
1546
1547 if (IS_ADHOC_LOC (location))
1548 location = get_location_from_adhoc_loc (set, location);
1549
1550 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1551
1552 while (true)
1553 {
1554 map = const_cast <line_map *> (linemap_lookup (set, location));
1555 if (!linemap_macro_expansion_map_p (map))
1556 break;
1557 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1558 location);
1559 }
1560
1561 if (original_map)
1562 *original_map = linemap_check_ordinary (map);
1563 return location;
1564 }
1565
1566 /* Resolve a virtual location into either a spelling location, an
1567 expansion point location or a token argument replacement point
1568 location. Return the map that encodes the virtual location as well
1569 as the resolved location.
1570
1571 If LOC is *NOT* the location of a token resulting from the
1572 expansion of a macro, then the parameter LRK (which stands for
1573 Location Resolution Kind) is ignored and the resulting location
1574 just equals the one given in argument.
1575
1576 Now if LOC *IS* the location of a token resulting from the
1577 expansion of a macro, this is what happens.
1578
1579 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1580 -------------------------------
1581
1582 The virtual location is resolved to the first macro expansion point
1583 that led to this macro expansion.
1584
1585 * If LRK is set to LRK_SPELLING_LOCATION
1586 -------------------------------------
1587
1588 The virtual location is resolved to the locus where the token has
1589 been spelled in the source. This can follow through all the macro
1590 expansions that led to the token.
1591
1592 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1593 --------------------------------------
1594
1595 The virtual location is resolved to the locus of the token in the
1596 context of the macro definition.
1597
1598 If LOC is the locus of a token that is an argument of a
1599 function-like macro [replacing a parameter in the replacement list
1600 of the macro] the virtual location is resolved to the locus of the
1601 parameter that is replaced, in the context of the definition of the
1602 macro.
1603
1604 If LOC is the locus of a token that is not an argument of a
1605 function-like macro, then the function behaves as if LRK was set to
1606 LRK_SPELLING_LOCATION.
1607
1608 If MAP is not NULL, *MAP is set to the map encoding the
1609 returned location. Note that if the returned location wasn't originally
1610 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1611 resolves to a location reserved for the client code, like
1612 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1613
1614 location_t
1615 linemap_resolve_location (line_maps *set,
1616 location_t loc,
1617 enum location_resolution_kind lrk,
1618 const line_map_ordinary **map)
1619 {
1620 location_t locus = loc;
1621 if (IS_ADHOC_LOC (loc))
1622 locus = get_location_from_adhoc_loc (set, loc);
1623
1624 if (locus < RESERVED_LOCATION_COUNT)
1625 {
1626 /* A reserved location wasn't encoded in a map. Let's return a
1627 NULL map here, just like what linemap_ordinary_map_lookup
1628 does. */
1629 if (map)
1630 *map = NULL;
1631 return loc;
1632 }
1633
1634 switch (lrk)
1635 {
1636 case LRK_MACRO_EXPANSION_POINT:
1637 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1638 break;
1639 case LRK_SPELLING_LOCATION:
1640 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1641 break;
1642 case LRK_MACRO_DEFINITION_LOCATION:
1643 loc = linemap_macro_loc_to_def_point (set, loc, map);
1644 break;
1645 default:
1646 abort ();
1647 }
1648 return loc;
1649 }
1650
1651 /* TRUE if LOCATION is a source code location of a token that is part of the
1652 definition of a macro, FALSE otherwise. */
1653
1654 bool
1655 linemap_location_from_macro_definition_p (line_maps *set,
1656 location_t loc)
1657 {
1658 if (IS_ADHOC_LOC (loc))
1659 loc = get_location_from_adhoc_loc (set, loc);
1660
1661 if (!linemap_location_from_macro_expansion_p (set, loc))
1662 return false;
1663
1664 while (true)
1665 {
1666 const struct line_map_macro *map
1667 = linemap_check_macro (linemap_lookup (set, loc));
1668
1669 location_t s_loc
1670 = linemap_macro_map_loc_unwind_toward_spelling (set, map, loc);
1671 if (linemap_location_from_macro_expansion_p (set, s_loc))
1672 loc = s_loc;
1673 else
1674 {
1675 location_t def_loc
1676 = linemap_macro_map_loc_to_def_point (map, loc);
1677 return s_loc == def_loc;
1678 }
1679 }
1680 }
1681
1682 /*
1683 Suppose that LOC is the virtual location of a token T coming from
1684 the expansion of a macro M. This function then steps up to get the
1685 location L of the point where M got expanded. If L is a spelling
1686 location inside a macro expansion M', then this function returns
1687 the locus of the point where M' was expanded. Said otherwise, this
1688 function returns the location of T in the context that triggered
1689 the expansion of M.
1690
1691 *LOC_MAP must be set to the map of LOC. This function then sets it
1692 to the map of the returned location. */
1693
1694 location_t
1695 linemap_unwind_toward_expansion (line_maps *set,
1696 location_t loc,
1697 const struct line_map **map)
1698 {
1699 location_t resolved_location;
1700 const line_map_macro *macro_map = linemap_check_macro (*map);
1701 const struct line_map *resolved_map;
1702
1703 if (IS_ADHOC_LOC (loc))
1704 loc = get_location_from_adhoc_loc (set, loc);
1705
1706 resolved_location =
1707 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1708 resolved_map = linemap_lookup (set, resolved_location);
1709
1710 if (!linemap_macro_expansion_map_p (resolved_map))
1711 {
1712 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1713 resolved_map = linemap_lookup (set, resolved_location);
1714 }
1715
1716 *map = resolved_map;
1717 return resolved_location;
1718 }
1719
1720 /* If LOC is the virtual location of a token coming from the expansion
1721 of a macro M and if its spelling location is reserved (e.g, a
1722 location for a built-in token), then this function unwinds (using
1723 linemap_unwind_toward_expansion) the location until a location that
1724 is not reserved and is not in a system header is reached. In other
1725 words, this unwinds the reserved location until a location that is
1726 in real source code is reached.
1727
1728 Otherwise, if the spelling location for LOC is not reserved or if
1729 LOC doesn't come from the expansion of a macro, the function
1730 returns LOC as is and *MAP is not touched.
1731
1732 *MAP is set to the map of the returned location if the later is
1733 different from LOC. */
1734 location_t
1735 linemap_unwind_to_first_non_reserved_loc (line_maps *set,
1736 location_t loc,
1737 const struct line_map **map)
1738 {
1739 location_t resolved_loc;
1740 const struct line_map *map0 = NULL;
1741 const line_map_ordinary *map1 = NULL;
1742
1743 if (IS_ADHOC_LOC (loc))
1744 loc = get_location_from_adhoc_loc (set, loc);
1745
1746 map0 = linemap_lookup (set, loc);
1747 if (!linemap_macro_expansion_map_p (map0))
1748 return loc;
1749
1750 resolved_loc = linemap_resolve_location (set, loc,
1751 LRK_SPELLING_LOCATION,
1752 &map1);
1753
1754 if (resolved_loc >= RESERVED_LOCATION_COUNT
1755 && !LINEMAP_SYSP (map1))
1756 return loc;
1757
1758 while (linemap_macro_expansion_map_p (map0)
1759 && (resolved_loc < RESERVED_LOCATION_COUNT
1760 || LINEMAP_SYSP (map1)))
1761 {
1762 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1763 resolved_loc = linemap_resolve_location (set, loc,
1764 LRK_SPELLING_LOCATION,
1765 &map1);
1766 }
1767
1768 if (map != NULL)
1769 *map = map0;
1770 return loc;
1771 }
1772
1773 /* Expand source code location LOC and return a user readable source
1774 code location. LOC must be a spelling (non-virtual) location. If
1775 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1776 location is returned. */
1777
1778 expanded_location
1779 linemap_expand_location (line_maps *set,
1780 const struct line_map *map,
1781 location_t loc)
1782
1783 {
1784 expanded_location xloc;
1785
1786 memset (&xloc, 0, sizeof (xloc));
1787 if (IS_ADHOC_LOC (loc))
1788 {
1789 xloc.data = get_data_from_adhoc_loc (set, loc);
1790 loc = get_location_from_adhoc_loc (set, loc);
1791 }
1792
1793 if (loc < RESERVED_LOCATION_COUNT)
1794 /* The location for this token wasn't generated from a line map.
1795 It was probably a location for a builtin token, chosen by some
1796 client code. Let's not try to expand the location in that
1797 case. */;
1798 else if (map == NULL)
1799 /* We shouldn't be getting a NULL map with a location that is not
1800 reserved by the client code. */
1801 abort ();
1802 else
1803 {
1804 /* MAP must be an ordinary map and LOC must be non-virtual,
1805 encoded into this map, obviously; the accessors used on MAP
1806 below ensure it is ordinary. Let's just assert the
1807 non-virtualness of LOC here. */
1808 if (linemap_location_from_macro_expansion_p (set, loc))
1809 abort ();
1810
1811 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1812
1813 xloc.file = LINEMAP_FILE (ord_map);
1814 xloc.line = SOURCE_LINE (ord_map, loc);
1815 xloc.column = SOURCE_COLUMN (ord_map, loc);
1816 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1817 }
1818
1819 return xloc;
1820 }
1821
1822
1823 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1824 is NULL, use stderr. IS_MACRO is true if the caller wants to
1825 dump a macro map, false otherwise. */
1826
1827 void
1828 linemap_dump (FILE *stream, class line_maps *set, unsigned ix, bool is_macro)
1829 {
1830 const char *const lc_reasons_v[LC_HWM]
1831 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1832 "LC_ENTER_MACRO", "LC_MODULE" };
1833 const line_map *map;
1834 unsigned reason;
1835
1836 if (stream == NULL)
1837 stream = stderr;
1838
1839 if (!is_macro)
1840 {
1841 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1842 reason = linemap_check_ordinary (map)->reason;
1843 }
1844 else
1845 {
1846 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1847 reason = LC_ENTER_MACRO;
1848 }
1849
1850 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1851 ix, (void *) map, map->start_location,
1852 reason < LC_HWM ? lc_reasons_v[reason] : "???",
1853 ((!is_macro
1854 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1855 ? "yes" : "no"));
1856 if (!is_macro)
1857 {
1858 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1859 const line_map_ordinary *includer_map
1860 = linemap_included_from_linemap (set, ord_map);
1861
1862 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1863 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1864 fprintf (stream, "Included from: [%d] %s\n",
1865 includer_map ? int (includer_map - set->info_ordinary.maps) : -1,
1866 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1867 }
1868 else
1869 {
1870 const line_map_macro *macro_map = linemap_check_macro (map);
1871 fprintf (stream, "Macro: %s (%u tokens)\n",
1872 linemap_map_get_macro_name (macro_map),
1873 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1874 }
1875
1876 fprintf (stream, "\n");
1877 }
1878
1879
1880 /* Dump debugging information about source location LOC into the file
1881 stream STREAM. SET is the line map set LOC comes from. */
1882
1883 void
1884 linemap_dump_location (line_maps *set,
1885 location_t loc,
1886 FILE *stream)
1887 {
1888 const line_map_ordinary *map;
1889 location_t location;
1890 const char *path = "", *from = "";
1891 int l = -1, c = -1, s = -1, e = -1;
1892
1893 if (IS_ADHOC_LOC (loc))
1894 loc = get_location_from_adhoc_loc (set, loc);
1895
1896 if (loc == 0)
1897 return;
1898
1899 location =
1900 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1901
1902 if (map == NULL)
1903 /* Only reserved locations can be tolerated in this case. */
1904 linemap_assert (location < RESERVED_LOCATION_COUNT);
1905 else
1906 {
1907 path = LINEMAP_FILE (map);
1908 l = SOURCE_LINE (map, location);
1909 c = SOURCE_COLUMN (map, location);
1910 s = LINEMAP_SYSP (map) != 0;
1911 e = location != loc;
1912 if (e)
1913 from = "N/A";
1914 else
1915 {
1916 const line_map_ordinary *from_map
1917 = linemap_included_from_linemap (set, map);
1918 from = from_map ? LINEMAP_FILE (from_map) : "<NULL>";
1919 }
1920 }
1921
1922 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1923 E: macro expansion?, LOC: original location, R: resolved location */
1924 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1925 path, from, l, c, s, (void*)map, e, loc, location);
1926 }
1927
1928 /* Return the highest location emitted for a given file for which
1929 there is a line map in SET. FILE_NAME is the file name to
1930 consider. If the function returns TRUE, *LOC is set to the highest
1931 location emitted for that file. */
1932
1933 bool
1934 linemap_get_file_highest_location (line_maps *set,
1935 const char *file_name,
1936 location_t *loc)
1937 {
1938 /* If the set is empty or no ordinary map has been created then
1939 there is no file to look for ... */
1940 if (set == NULL || set->info_ordinary.used == 0)
1941 return false;
1942
1943 /* Now look for the last ordinary map created for FILE_NAME. */
1944 int i;
1945 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1946 {
1947 const char *fname = set->info_ordinary.maps[i].to_file;
1948 if (fname && !filename_cmp (fname, file_name))
1949 break;
1950 }
1951
1952 if (i < 0)
1953 return false;
1954
1955 /* The highest location for a given map is either the starting
1956 location of the next map minus one, or -- if the map is the
1957 latest one -- the highest location of the set. */
1958 location_t result;
1959 if (i == (int) set->info_ordinary.used - 1)
1960 result = set->highest_location;
1961 else
1962 result = set->info_ordinary.maps[i + 1].start_location - 1;
1963
1964 *loc = result;
1965 return true;
1966 }
1967
1968 /* Compute and return statistics about the memory consumption of some
1969 parts of the line table SET. */
1970
1971 void
1972 linemap_get_statistics (line_maps *set,
1973 struct linemap_stats *s)
1974 {
1975 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1976 macro_maps_allocated_size, macro_maps_used_size,
1977 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1978
1979 const line_map_macro *cur_map;
1980
1981 ordinary_maps_allocated_size =
1982 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1983
1984 ordinary_maps_used_size =
1985 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1986
1987 macro_maps_allocated_size =
1988 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1989
1990 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1991 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1992 ++cur_map)
1993 {
1994 unsigned i;
1995
1996 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1997
1998 macro_maps_locations_size +=
1999 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (location_t);
2000
2001 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
2002 {
2003 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
2004 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
2005 duplicated_macro_maps_locations_size +=
2006 sizeof (location_t);
2007 }
2008 }
2009
2010 macro_maps_used_size =
2011 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
2012
2013 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
2014 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
2015 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
2016 s->ordinary_maps_used_size = ordinary_maps_used_size;
2017 s->num_expanded_macros = num_expanded_macros_counter;
2018 s->num_macro_tokens = num_macro_tokens_counter;
2019 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
2020 s->macro_maps_allocated_size = macro_maps_allocated_size;
2021 s->macro_maps_locations_size = macro_maps_locations_size;
2022 s->macro_maps_used_size = macro_maps_used_size;
2023 s->duplicated_macro_maps_locations_size =
2024 duplicated_macro_maps_locations_size;
2025 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
2026 * sizeof (struct location_adhoc_data));
2027 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
2028 }
2029
2030
2031 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
2032 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
2033 specifies how many macro maps to dump. */
2034
2035 void
2036 line_table_dump (FILE *stream, class line_maps *set, unsigned int num_ordinary,
2037 unsigned int num_macro)
2038 {
2039 unsigned int i;
2040
2041 if (set == NULL)
2042 return;
2043
2044 if (stream == NULL)
2045 stream = stderr;
2046
2047 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
2048 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
2049 fprintf (stream, "Include stack depth: %d\n", set->depth);
2050 fprintf (stream, "Highest location: %u\n", set->highest_location);
2051
2052 if (num_ordinary)
2053 {
2054 fprintf (stream, "\nOrdinary line maps\n");
2055 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
2056 linemap_dump (stream, set, i, false);
2057 fprintf (stream, "\n");
2058 }
2059
2060 if (num_macro)
2061 {
2062 fprintf (stream, "\nMacro line maps\n");
2063 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
2064 linemap_dump (stream, set, i, true);
2065 fprintf (stream, "\n");
2066 }
2067 }
2068
2069 /* class rich_location. */
2070
2071 /* Construct a rich_location with location LOC as its initial range. */
2072
2073 rich_location::rich_location (line_maps *set, location_t loc,
2074 const range_label *label) :
2075 m_line_table (set),
2076 m_ranges (),
2077 m_column_override (0),
2078 m_have_expanded_location (false),
2079 m_fixit_hints (),
2080 m_seen_impossible_fixit (false),
2081 m_fixits_cannot_be_auto_applied (false),
2082 m_path (NULL)
2083 {
2084 add_range (loc, SHOW_RANGE_WITH_CARET, label);
2085 }
2086
2087 /* The destructor for class rich_location. */
2088
2089 rich_location::~rich_location ()
2090 {
2091 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2092 delete get_fixit_hint (i);
2093 }
2094
2095 /* Get location IDX within this rich_location. */
2096
2097 location_t
2098 rich_location::get_loc (unsigned int idx) const
2099 {
2100 const location_range *locrange = get_range (idx);
2101 return locrange->m_loc;
2102 }
2103
2104 /* Get range IDX within this rich_location. */
2105
2106 const location_range *
2107 rich_location::get_range (unsigned int idx) const
2108 {
2109 return &m_ranges[idx];
2110 }
2111
2112 /* Mutable access to range IDX within this rich_location. */
2113
2114 location_range *
2115 rich_location::get_range (unsigned int idx)
2116 {
2117 return &m_ranges[idx];
2118 }
2119
2120 /* Expand location IDX within this rich_location. */
2121 /* Get an expanded_location for this rich_location's primary
2122 location. */
2123
2124 expanded_location
2125 rich_location::get_expanded_location (unsigned int idx)
2126 {
2127 if (idx == 0)
2128 {
2129 /* Cache the expansion of the primary location. */
2130 if (!m_have_expanded_location)
2131 {
2132 m_expanded_location
2133 = linemap_client_expand_location_to_spelling_point
2134 (get_loc (0), LOCATION_ASPECT_CARET);
2135 if (m_column_override)
2136 m_expanded_location.column = m_column_override;
2137 m_have_expanded_location = true;
2138 }
2139
2140 return m_expanded_location;
2141 }
2142 else
2143 return linemap_client_expand_location_to_spelling_point
2144 (get_loc (idx), LOCATION_ASPECT_CARET);
2145 }
2146
2147 /* Set the column of the primary location, with 0 meaning
2148 "don't override it". */
2149
2150 void
2151 rich_location::override_column (int column)
2152 {
2153 m_column_override = column;
2154 m_have_expanded_location = false;
2155 }
2156
2157 /* Add the given range. */
2158
2159 void
2160 rich_location::add_range (location_t loc,
2161 enum range_display_kind range_display_kind,
2162 const range_label *label)
2163 {
2164 location_range range;
2165 range.m_loc = loc;
2166 range.m_range_display_kind = range_display_kind;
2167 range.m_label = label;
2168 m_ranges.push (range);
2169 }
2170
2171 /* Add or overwrite the location given by IDX, setting its location to LOC,
2172 and setting its m_range_display_kind to RANGE_DISPLAY_KIND.
2173
2174 It must either overwrite an existing location, or add one *exactly* on
2175 the end of the array.
2176
2177 This is primarily for use by gcc when implementing diagnostic format
2178 decoders e.g.
2179 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2180 (which writes the source location of a tree back into location 0 of
2181 the rich_location), and
2182 - the "%C" and "%L" format codes in the Fortran frontend. */
2183
2184 void
2185 rich_location::set_range (unsigned int idx, location_t loc,
2186 enum range_display_kind range_display_kind)
2187 {
2188 /* We can either overwrite an existing range, or add one exactly
2189 on the end of the array. */
2190 linemap_assert (idx <= m_ranges.count ());
2191
2192 if (idx == m_ranges.count ())
2193 add_range (loc, range_display_kind);
2194 else
2195 {
2196 location_range *locrange = get_range (idx);
2197 locrange->m_loc = loc;
2198 locrange->m_range_display_kind = range_display_kind;
2199 }
2200
2201 if (idx == 0)
2202 /* Mark any cached value here as dirty. */
2203 m_have_expanded_location = false;
2204 }
2205
2206 /* Methods for adding insertion fix-it hints. */
2207
2208 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2209 immediately before the primary range's start location. */
2210
2211 void
2212 rich_location::add_fixit_insert_before (const char *new_content)
2213 {
2214 add_fixit_insert_before (get_loc (), new_content);
2215 }
2216
2217 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2218 immediately before the start of WHERE. */
2219
2220 void
2221 rich_location::add_fixit_insert_before (location_t where,
2222 const char *new_content)
2223 {
2224 location_t start = get_range_from_loc (m_line_table, where).m_start;
2225 maybe_add_fixit (start, start, new_content);
2226 }
2227
2228 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2229 immediately after the primary range's end-point. */
2230
2231 void
2232 rich_location::add_fixit_insert_after (const char *new_content)
2233 {
2234 add_fixit_insert_after (get_loc (), new_content);
2235 }
2236
2237 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2238 immediately after the end-point of WHERE. */
2239
2240 void
2241 rich_location::add_fixit_insert_after (location_t where,
2242 const char *new_content)
2243 {
2244 location_t finish = get_range_from_loc (m_line_table, where).m_finish;
2245 location_t next_loc
2246 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2247
2248 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2249 its input value. */
2250 if (next_loc == finish)
2251 {
2252 stop_supporting_fixits ();
2253 return;
2254 }
2255
2256 maybe_add_fixit (next_loc, next_loc, new_content);
2257 }
2258
2259 /* Methods for adding removal fix-it hints. */
2260
2261 /* Add a fixit-hint, suggesting removal of the content covered
2262 by range 0. */
2263
2264 void
2265 rich_location::add_fixit_remove ()
2266 {
2267 add_fixit_remove (get_loc ());
2268 }
2269
2270 /* Add a fixit-hint, suggesting removal of the content between
2271 the start and finish of WHERE. */
2272
2273 void
2274 rich_location::add_fixit_remove (location_t where)
2275 {
2276 source_range range = get_range_from_loc (m_line_table, where);
2277 add_fixit_remove (range);
2278 }
2279
2280 /* Add a fixit-hint, suggesting removal of the content at
2281 SRC_RANGE. */
2282
2283 void
2284 rich_location::add_fixit_remove (source_range src_range)
2285 {
2286 add_fixit_replace (src_range, "");
2287 }
2288
2289 /* Add a fixit-hint, suggesting replacement of the content covered
2290 by range 0 with NEW_CONTENT. */
2291
2292 void
2293 rich_location::add_fixit_replace (const char *new_content)
2294 {
2295 add_fixit_replace (get_loc (), new_content);
2296 }
2297
2298 /* Methods for adding "replace" fix-it hints. */
2299
2300 /* Add a fixit-hint, suggesting replacement of the content between
2301 the start and finish of WHERE with NEW_CONTENT. */
2302
2303 void
2304 rich_location::add_fixit_replace (location_t where,
2305 const char *new_content)
2306 {
2307 source_range range = get_range_from_loc (m_line_table, where);
2308 add_fixit_replace (range, new_content);
2309 }
2310
2311 /* Add a fixit-hint, suggesting replacement of the content at
2312 SRC_RANGE with NEW_CONTENT. */
2313
2314 void
2315 rich_location::add_fixit_replace (source_range src_range,
2316 const char *new_content)
2317 {
2318 location_t start = get_pure_location (m_line_table, src_range.m_start);
2319 location_t finish = get_pure_location (m_line_table, src_range.m_finish);
2320
2321 /* Fix-it hints use half-closed ranges, so attempt to offset the endpoint. */
2322 location_t next_loc
2323 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2324 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2325 its input value. */
2326 if (next_loc == finish)
2327 {
2328 stop_supporting_fixits ();
2329 return;
2330 }
2331 finish = next_loc;
2332
2333 maybe_add_fixit (start, finish, new_content);
2334 }
2335
2336 /* Get the last fix-it hint within this rich_location, or NULL if none. */
2337
2338 fixit_hint *
2339 rich_location::get_last_fixit_hint () const
2340 {
2341 if (m_fixit_hints.count () > 0)
2342 return get_fixit_hint (m_fixit_hints.count () - 1);
2343 else
2344 return NULL;
2345 }
2346
2347 /* If WHERE is an "awkward" location, then mark this rich_location as not
2348 supporting fixits, purging any thay were already added, and return true.
2349
2350 Otherwise (the common case), return false. */
2351
2352 bool
2353 rich_location::reject_impossible_fixit (location_t where)
2354 {
2355 /* Fix-its within a rich_location should either all be suggested, or
2356 none of them should be suggested.
2357 Once we've rejected a fixit, we reject any more, even those
2358 with reasonable locations. */
2359 if (m_seen_impossible_fixit)
2360 return true;
2361
2362 if (where <= LINE_MAP_MAX_LOCATION_WITH_COLS)
2363 /* WHERE is a reasonable location for a fix-it; don't reject it. */
2364 return false;
2365
2366 /* Otherwise we have an attempt to add a fix-it with an "awkward"
2367 location: either one that we can't obtain column information
2368 for (within an ordinary map), or one within a macro expansion. */
2369 stop_supporting_fixits ();
2370 return true;
2371 }
2372
2373 /* Mark this rich_location as not supporting fixits, purging any that were
2374 already added. */
2375
2376 void
2377 rich_location::stop_supporting_fixits ()
2378 {
2379 m_seen_impossible_fixit = true;
2380
2381 /* Purge the rich_location of any fix-its that were already added. */
2382 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2383 delete get_fixit_hint (i);
2384 m_fixit_hints.truncate (0);
2385 }
2386
2387 /* Add HINT to the fix-it hints in this rich_location,
2388 consolidating into the prior fixit if possible. */
2389
2390 void
2391 rich_location::maybe_add_fixit (location_t start,
2392 location_t next_loc,
2393 const char *new_content)
2394 {
2395 if (reject_impossible_fixit (start))
2396 return;
2397 if (reject_impossible_fixit (next_loc))
2398 return;
2399
2400 /* Only allow fix-it hints that affect a single line in one file.
2401 Compare the end-points. */
2402 expanded_location exploc_start
2403 = linemap_client_expand_location_to_spelling_point (start,
2404 LOCATION_ASPECT_START);
2405 expanded_location exploc_next_loc
2406 = linemap_client_expand_location_to_spelling_point (next_loc,
2407 LOCATION_ASPECT_START);
2408 /* They must be within the same file... */
2409 if (exploc_start.file != exploc_next_loc.file)
2410 {
2411 stop_supporting_fixits ();
2412 return;
2413 }
2414 /* ...and on the same line. */
2415 if (exploc_start.line != exploc_next_loc.line)
2416 {
2417 stop_supporting_fixits ();
2418 return;
2419 }
2420 /* The columns must be in the correct order. This can fail if the
2421 endpoints straddle the boundary for which the linemap can represent
2422 columns (PR c/82050). */
2423 if (exploc_start.column > exploc_next_loc.column)
2424 {
2425 stop_supporting_fixits ();
2426 return;
2427 }
2428
2429 const char *newline = strchr (new_content, '\n');
2430 if (newline)
2431 {
2432 /* For now, we can only support insertion of whole lines
2433 i.e. starts at start of line, and the newline is at the end of
2434 the insertion point. */
2435
2436 /* It must be an insertion, not a replacement/deletion. */
2437 if (start != next_loc)
2438 {
2439 stop_supporting_fixits ();
2440 return;
2441 }
2442
2443 /* The insertion must be at the start of a line. */
2444 if (exploc_start.column != 1)
2445 {
2446 stop_supporting_fixits ();
2447 return;
2448 }
2449
2450 /* The newline must be at end of NEW_CONTENT.
2451 We could eventually split up fix-its at newlines if we wanted
2452 to allow more generality (e.g. to allow adding multiple lines
2453 with one add_fixit call. */
2454 if (newline[1] != '\0')
2455 {
2456 stop_supporting_fixits ();
2457 return;
2458 }
2459 }
2460
2461 /* Consolidate neighboring fixits.
2462 Don't consolidate into newline-insertion fixits. */
2463 fixit_hint *prev = get_last_fixit_hint ();
2464 if (prev && !prev->ends_with_newline_p ())
2465 if (prev->maybe_append (start, next_loc, new_content))
2466 return;
2467
2468 m_fixit_hints.push (new fixit_hint (start, next_loc, new_content));
2469 }
2470
2471 /* class fixit_hint. */
2472
2473 fixit_hint::fixit_hint (location_t start,
2474 location_t next_loc,
2475 const char *new_content)
2476 : m_start (start),
2477 m_next_loc (next_loc),
2478 m_bytes (xstrdup (new_content)),
2479 m_len (strlen (new_content))
2480 {
2481 }
2482
2483 /* Does this fix-it hint affect the given line? */
2484
2485 bool
2486 fixit_hint::affects_line_p (const char *file, int line) const
2487 {
2488 expanded_location exploc_start
2489 = linemap_client_expand_location_to_spelling_point (m_start,
2490 LOCATION_ASPECT_START);
2491 if (file != exploc_start.file)
2492 return false;
2493 if (line < exploc_start.line)
2494 return false;
2495 expanded_location exploc_next_loc
2496 = linemap_client_expand_location_to_spelling_point (m_next_loc,
2497 LOCATION_ASPECT_START);
2498 if (file != exploc_next_loc.file)
2499 return false;
2500 if (line > exploc_next_loc.line)
2501 return false;
2502 return true;
2503 }
2504
2505 /* Method for consolidating fix-it hints, for use by
2506 rich_location::maybe_add_fixit.
2507 If possible, merge a pending fix-it hint with the given params
2508 into this one and return true.
2509 Otherwise return false. */
2510
2511 bool
2512 fixit_hint::maybe_append (location_t start,
2513 location_t next_loc,
2514 const char *new_content)
2515 {
2516 /* For consolidation to be possible, START must be at this hint's
2517 m_next_loc. */
2518 if (start != m_next_loc)
2519 return false;
2520
2521 /* If so, we have neighboring replacements; merge them. */
2522 m_next_loc = next_loc;
2523 size_t extra_len = strlen (new_content);
2524 m_bytes = (char *)xrealloc (m_bytes, m_len + extra_len + 1);
2525 memcpy (m_bytes + m_len, new_content, extra_len);
2526 m_len += extra_len;
2527 m_bytes[m_len] = '\0';
2528 return true;
2529 }
2530
2531 /* Return true iff this hint's content ends with a newline. */
2532
2533 bool
2534 fixit_hint::ends_with_newline_p () const
2535 {
2536 if (m_len == 0)
2537 return false;
2538 return m_bytes[m_len - 1] == '\n';
2539 }