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