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