* sv.po: Update.
[official-gcc.git] / libcpp / line-map.c
blob2e61895bb35bf6814b8040fdb0859772e28adf19
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2016 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>.
18 In other words, you are welcome to use, share and improve this program.
19 You are forbidden to forbid anyone else to use, share and improve
20 what you give them. Help stamp out software-hoarding! */
22 #include "config.h"
23 #include "system.h"
24 #include "line-map.h"
25 #include "cpplib.h"
26 #include "internal.h"
27 #include "hashtab.h"
29 /* 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);
34 /* Do not pack ranges if locations get higher than this.
35 If you change this, update:
36 gcc.dg/plugin/location_overflow_plugin.c
37 gcc.dg/plugin/location-overflow-test-*.c. */
38 const source_location LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES = 0x50000000;
40 /* Do not track column numbers if locations get higher than this.
41 If you change this, update:
42 gcc.dg/plugin/location_overflow_plugin.c
43 gcc.dg/plugin/location-overflow-test-*.c. */
44 const source_location LINE_MAP_MAX_LOCATION_WITH_COLS = 0x60000000;
46 /* Highest possible source location encoded within an ordinary or
47 macro map. */
48 const source_location LINE_MAP_MAX_SOURCE_LOCATION = 0x70000000;
50 static void trace_include (const struct line_maps *, const line_map_ordinary *);
51 static const line_map_ordinary * linemap_ordinary_map_lookup (struct line_maps *,
52 source_location);
53 static const line_map_macro* linemap_macro_map_lookup (struct line_maps *,
54 source_location);
55 static source_location linemap_macro_map_loc_to_def_point
56 (const line_map_macro *, source_location);
57 static source_location linemap_macro_map_loc_to_exp_point
58 (const line_map_macro *, source_location);
59 static source_location linemap_macro_loc_to_spelling_point
60 (struct line_maps *, source_location, const line_map_ordinary **);
61 static source_location linemap_macro_loc_to_def_point (struct line_maps *,
62 source_location,
63 const line_map_ordinary **);
64 static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
65 source_location,
66 const line_map_ordinary **);
68 /* Counters defined in macro.c. */
69 extern unsigned num_expanded_macros_counter;
70 extern unsigned num_macro_tokens_counter;
72 /* Hash function for location_adhoc_data hashtable. */
74 static hashval_t
75 location_adhoc_data_hash (const void *l)
77 const struct location_adhoc_data *lb =
78 (const struct location_adhoc_data *) l;
79 return ((hashval_t) lb->locus
80 + (hashval_t) lb->src_range.m_start
81 + (hashval_t) lb->src_range.m_finish
82 + (size_t) lb->data);
85 /* Compare function for location_adhoc_data hashtable. */
87 static int
88 location_adhoc_data_eq (const void *l1, const void *l2)
90 const struct location_adhoc_data *lb1 =
91 (const struct location_adhoc_data *) l1;
92 const struct location_adhoc_data *lb2 =
93 (const struct location_adhoc_data *) l2;
94 return (lb1->locus == lb2->locus
95 && lb1->src_range.m_start == lb2->src_range.m_start
96 && lb1->src_range.m_finish == lb2->src_range.m_finish
97 && lb1->data == lb2->data);
100 /* Update the hashtable when location_adhoc_data is reallocated. */
102 static int
103 location_adhoc_data_update (void **slot, void *data)
105 *((char **) slot) += *((long long *) data);
106 return 1;
109 /* Rebuild the hash table from the location adhoc data. */
111 void
112 rebuild_location_adhoc_htab (struct line_maps *set)
114 unsigned i;
115 set->location_adhoc_data_map.htab =
116 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
117 for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
118 htab_find_slot (set->location_adhoc_data_map.htab,
119 set->location_adhoc_data_map.data + i, INSERT);
122 /* Helper function for get_combined_adhoc_loc.
123 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
124 within a source_location, without needing to use an ad-hoc location. */
126 static bool
127 can_be_stored_compactly_p (struct line_maps *set,
128 source_location locus,
129 source_range src_range,
130 void *data)
132 /* If there's an ad-hoc pointer, we can't store it directly in the
133 source_location, we need the lookaside. */
134 if (data)
135 return false;
137 /* We only store ranges that begin at the locus and that are sufficiently
138 "sane". */
139 if (src_range.m_start != locus)
140 return false;
142 if (src_range.m_finish < src_range.m_start)
143 return false;
145 if (src_range.m_start < RESERVED_LOCATION_COUNT)
146 return false;
148 if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
149 return false;
151 /* All 3 locations must be within ordinary maps, typically, the same
152 ordinary map. */
153 source_location lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
154 if (locus >= lowest_macro_loc)
155 return false;
156 if (src_range.m_start >= lowest_macro_loc)
157 return false;
158 if (src_range.m_finish >= lowest_macro_loc)
159 return false;
161 /* Passed all tests. */
162 return true;
165 /* Combine LOCUS and DATA to a combined adhoc loc. */
167 source_location
168 get_combined_adhoc_loc (struct line_maps *set,
169 source_location locus,
170 source_range src_range,
171 void *data)
173 struct location_adhoc_data lb;
174 struct location_adhoc_data **slot;
176 if (IS_ADHOC_LOC (locus))
177 locus
178 = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
179 if (locus == 0 && data == NULL)
180 return 0;
182 /* Any ordinary locations ought to be "pure" at this point: no
183 compressed ranges. */
184 linemap_assert (locus < RESERVED_LOCATION_COUNT
185 || locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
186 || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
187 || pure_location_p (set, locus));
189 /* Consider short-range optimization. */
190 if (can_be_stored_compactly_p (set, locus, src_range, data))
192 /* The low bits ought to be clear. */
193 linemap_assert (pure_location_p (set, locus));
194 const line_map *map = linemap_lookup (set, locus);
195 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
196 unsigned int int_diff = src_range.m_finish - src_range.m_start;
197 unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
198 if (col_diff < (1U << ordmap->m_range_bits))
200 source_location packed = locus | col_diff;
201 set->num_optimized_ranges++;
202 return packed;
206 /* We can also compactly store locations
207 when locus == start == finish (and data is NULL). */
208 if (locus == src_range.m_start
209 && locus == src_range.m_finish
210 && !data)
211 return locus;
213 if (!data)
214 set->num_unoptimized_ranges++;
216 lb.locus = locus;
217 lb.src_range = src_range;
218 lb.data = data;
219 slot = (struct location_adhoc_data **)
220 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
221 if (*slot == NULL)
223 if (set->location_adhoc_data_map.curr_loc >=
224 set->location_adhoc_data_map.allocated)
226 char *orig_data = (char *) set->location_adhoc_data_map.data;
227 long long offset;
228 /* Cast away extern "C" from the type of xrealloc. */
229 line_map_realloc reallocator = (set->reallocator
230 ? set->reallocator
231 : (line_map_realloc) xrealloc);
233 if (set->location_adhoc_data_map.allocated == 0)
234 set->location_adhoc_data_map.allocated = 128;
235 else
236 set->location_adhoc_data_map.allocated *= 2;
237 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
238 reallocator (set->location_adhoc_data_map.data,
239 set->location_adhoc_data_map.allocated
240 * sizeof (struct location_adhoc_data));
241 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
242 if (set->location_adhoc_data_map.allocated > 128)
243 htab_traverse (set->location_adhoc_data_map.htab,
244 location_adhoc_data_update, &offset);
246 *slot = set->location_adhoc_data_map.data
247 + set->location_adhoc_data_map.curr_loc;
248 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
249 = lb;
251 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
254 /* Return the data for the adhoc loc. */
256 void *
257 get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
259 linemap_assert (IS_ADHOC_LOC (loc));
260 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
263 /* Return the location for the adhoc loc. */
265 source_location
266 get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
268 linemap_assert (IS_ADHOC_LOC (loc));
269 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
272 /* Return the source_range for adhoc location LOC. */
274 static source_range
275 get_range_from_adhoc_loc (struct line_maps *set, source_location loc)
277 linemap_assert (IS_ADHOC_LOC (loc));
278 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].src_range;
281 /* Get the source_range of location LOC, either from the ad-hoc
282 lookaside table, or embedded inside LOC itself. */
284 source_range
285 get_range_from_loc (struct line_maps *set,
286 source_location loc)
288 if (IS_ADHOC_LOC (loc))
289 return get_range_from_adhoc_loc (set, loc);
291 /* For ordinary maps, extract packed range. */
292 if (loc >= RESERVED_LOCATION_COUNT
293 && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
294 && loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
296 const line_map *map = linemap_lookup (set, loc);
297 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
298 source_range result;
299 int offset = loc & ((1 << ordmap->m_range_bits) - 1);
300 result.m_start = loc - offset;
301 result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
302 return result;
305 return source_range::from_location (loc);
308 /* Get whether location LOC is a "pure" location, or
309 whether it is an ad-hoc location, or embeds range information. */
311 bool
312 pure_location_p (line_maps *set, source_location loc)
314 if (IS_ADHOC_LOC (loc))
315 return false;
317 const line_map *map = linemap_lookup (set, loc);
318 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
320 if (loc & ((1U << ordmap->m_range_bits) - 1))
321 return false;
323 return true;
326 /* Finalize the location_adhoc_data structure. */
327 void
328 location_adhoc_data_fini (struct line_maps *set)
330 htab_delete (set->location_adhoc_data_map.htab);
333 /* Initialize a line map set. */
335 void
336 linemap_init (struct line_maps *set,
337 source_location builtin_location)
339 memset (set, 0, sizeof (struct line_maps));
340 set->highest_location = RESERVED_LOCATION_COUNT - 1;
341 set->highest_line = RESERVED_LOCATION_COUNT - 1;
342 set->location_adhoc_data_map.htab =
343 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
344 set->builtin_location = builtin_location;
347 /* Check for and warn about line_maps entered but not exited. */
349 void
350 linemap_check_files_exited (struct line_maps *set)
352 const line_map_ordinary *map;
353 /* Depending upon whether we are handling preprocessed input or
354 not, this can be a user error or an ICE. */
355 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
356 ! MAIN_FILE_P (map);
357 map = INCLUDED_FROM (set, map))
358 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
359 ORDINARY_MAP_FILE_NAME (map));
362 /* Create a new line map in the line map set SET, and return it.
363 REASON is the reason of creating the map. It determines the type
364 of map created (ordinary or macro map). Note that ordinary maps and
365 macro maps are allocated in different memory location. */
367 static struct line_map *
368 new_linemap (struct line_maps *set,
369 enum lc_reason reason)
371 /* Depending on this variable, a macro map would be allocated in a
372 different memory location than an ordinary map. */
373 bool macro_map_p = (reason == LC_ENTER_MACRO);
374 struct line_map *result;
376 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
378 /* We ran out of allocated line maps. Let's allocate more. */
379 size_t alloc_size;
381 /* Cast away extern "C" from the type of xrealloc. */
382 line_map_realloc reallocator = (set->reallocator
383 ? set->reallocator
384 : (line_map_realloc) xrealloc);
385 line_map_round_alloc_size_func round_alloc_size =
386 set->round_alloc_size;
388 size_t map_size = (macro_map_p
389 ? sizeof (line_map_macro)
390 : sizeof (line_map_ordinary));
392 /* We are going to execute some dance to try to reduce the
393 overhead of the memory allocator, in case we are using the
394 ggc-page.c one.
396 The actual size of memory we are going to get back from the
397 allocator is the smallest power of 2 that is greater than the
398 size we requested. So let's consider that size then. */
400 alloc_size =
401 (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
402 * map_size;
404 /* Get the actual size of memory that is going to be allocated
405 by the allocator. */
406 alloc_size = round_alloc_size (alloc_size);
408 /* Now alloc_size contains the exact memory size we would get if
409 we have asked for the initial alloc_size amount of memory.
410 Let's get back to the number of macro map that amounts
411 to. */
412 LINEMAPS_ALLOCATED (set, macro_map_p) =
413 alloc_size / map_size;
415 /* And now let's really do the re-allocation. */
416 if (macro_map_p)
418 set->info_macro.maps
419 = (line_map_macro *) (*reallocator) (set->info_macro.maps,
420 (LINEMAPS_ALLOCATED (set, macro_map_p)
421 * map_size));
422 result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
424 else
426 set->info_ordinary.maps =
427 (line_map_ordinary *) (*reallocator) (set->info_ordinary.maps,
428 (LINEMAPS_ALLOCATED (set, macro_map_p)
429 * map_size));
430 result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
432 memset (result, 0,
433 ((LINEMAPS_ALLOCATED (set, macro_map_p)
434 - LINEMAPS_USED (set, macro_map_p))
435 * map_size));
437 else
439 if (macro_map_p)
440 result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
441 else
442 result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
445 LINEMAPS_USED (set, macro_map_p)++;
447 result->reason = reason;
448 return result;
451 /* Add a mapping of logical source line to physical source file and
452 line number.
454 The text pointed to by TO_FILE must have a lifetime
455 at least as long as the final call to lookup_line (). An empty
456 TO_FILE means standard input. If reason is LC_LEAVE, and
457 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
458 natural values considering the file we are returning to.
460 FROM_LINE should be monotonic increasing across calls to this
461 function. A call to this function can relocate the previous set of
462 maps, so any stored line_map pointers should not be used. */
464 const struct line_map *
465 linemap_add (struct line_maps *set, enum lc_reason reason,
466 unsigned int sysp, const char *to_file, linenum_type to_line)
468 /* Generate a start_location above the current highest_location.
469 If possible, make the low range bits be zero. */
470 source_location start_location;
471 if (set->highest_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
473 start_location = set->highest_location + (1 << set->default_range_bits);
474 if (set->default_range_bits)
475 start_location &= ~((1 << set->default_range_bits) - 1);
476 linemap_assert (0 == (start_location
477 & ((1 << set->default_range_bits) - 1)));
479 else
480 start_location = set->highest_location + 1;
482 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
483 && (start_location
484 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
486 /* When we enter the file for the first time reason cannot be
487 LC_RENAME. */
488 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
490 /* If we are leaving the main file, return a NULL map. */
491 if (reason == LC_LEAVE
492 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
493 && to_file == NULL)
495 set->depth--;
496 return NULL;
499 linemap_assert (reason != LC_ENTER_MACRO);
500 line_map_ordinary *map = linemap_check_ordinary (new_linemap (set, reason));
502 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
503 to_file = "<stdin>";
505 if (reason == LC_RENAME_VERBATIM)
506 reason = LC_RENAME;
508 if (reason == LC_LEAVE)
510 /* When we are just leaving an "included" file, and jump to the next
511 location inside the "includer" right after the #include
512 "included", this variable points the map in use right before the
513 #include "included", inside the same "includer" file. */
514 line_map_ordinary *from;
516 linemap_assert (!MAIN_FILE_P (map - 1));
517 /* (MAP - 1) points to the map we are leaving. The
518 map from which (MAP - 1) got included should be the map
519 that comes right before MAP in the same file. */
520 from = INCLUDED_FROM (set, map - 1);
522 /* A TO_FILE of NULL is special - we use the natural values. */
523 if (to_file == NULL)
525 to_file = ORDINARY_MAP_FILE_NAME (from);
526 to_line = SOURCE_LINE (from, from[1].start_location);
527 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
529 else
530 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from),
531 to_file) == 0);
534 map->sysp = sysp;
535 map->start_location = start_location;
536 map->to_file = to_file;
537 map->to_line = to_line;
538 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
539 map->m_column_and_range_bits = 0;
540 map->m_range_bits = 0;
541 set->highest_location = start_location;
542 set->highest_line = start_location;
543 set->max_column_hint = 0;
545 /* This assertion is placed after set->highest_location has
546 been updated, since the latter affects
547 linemap_location_from_macro_expansion_p, which ultimately affects
548 pure_location_p. */
549 linemap_assert (pure_location_p (set, start_location));
551 if (reason == LC_ENTER)
553 map->included_from =
554 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
555 set->depth++;
556 if (set->trace_includes)
557 trace_include (set, map);
559 else if (reason == LC_RENAME)
560 map->included_from = ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
561 else if (reason == LC_LEAVE)
563 set->depth--;
564 map->included_from =
565 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
568 return map;
571 /* Returns TRUE if the line table set tracks token locations across
572 macro expansion, FALSE otherwise. */
574 bool
575 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
577 return LINEMAPS_MACRO_MAPS (set) != NULL;
580 /* Create a macro map. A macro map encodes source locations of tokens
581 that are part of a macro replacement-list, at a macro expansion
582 point. See the extensive comments of struct line_map and struct
583 line_map_macro, in line-map.h.
585 This map shall be created when the macro is expanded. The map
586 encodes the source location of the expansion point of the macro as
587 well as the "original" source location of each token that is part
588 of the macro replacement-list. If a macro is defined but never
589 expanded, it has no macro map. SET is the set of maps the macro
590 map should be part of. MACRO_NODE is the macro which the new macro
591 map should encode source locations for. EXPANSION is the location
592 of the expansion point of MACRO. For function-like macros
593 invocations, it's best to make it point to the closing parenthesis
594 of the macro, rather than the the location of the first character
595 of the macro. NUM_TOKENS is the number of tokens that are part of
596 the replacement-list of MACRO.
598 Note that when we run out of the integer space available for source
599 locations, this function returns NULL. In that case, callers of
600 this function cannot encode {line,column} pairs into locations of
601 macro tokens anymore. */
603 const line_map_macro *
604 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
605 source_location expansion, unsigned int num_tokens)
607 line_map_macro *map;
608 source_location start_location;
609 /* Cast away extern "C" from the type of xrealloc. */
610 line_map_realloc reallocator = (set->reallocator
611 ? set->reallocator
612 : (line_map_realloc) xrealloc);
614 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
616 if (start_location <= set->highest_line
617 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
618 /* We ran out of macro map space. */
619 return NULL;
621 map = linemap_check_macro (new_linemap (set, LC_ENTER_MACRO));
623 map->start_location = start_location;
624 map->macro = macro_node;
625 map->n_tokens = num_tokens;
626 map->macro_locations
627 = (source_location*) reallocator (NULL,
628 2 * num_tokens
629 * sizeof (source_location));
630 map->expansion = expansion;
631 memset (MACRO_MAP_LOCATIONS (map), 0,
632 num_tokens * sizeof (source_location));
634 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
636 return map;
639 /* Create and return a virtual location for a token that is part of a
640 macro expansion-list at a macro expansion point. See the comment
641 inside struct line_map_macro to see what an expansion-list exactly
644 A call to this function must come after a call to
645 linemap_enter_macro.
647 MAP is the map into which the source location is created. TOKEN_NO
648 is the index of the token in the macro replacement-list, starting
649 at number 0.
651 ORIG_LOC is the location of the token outside of this macro
652 expansion. If the token comes originally from the macro
653 definition, it is the locus in the macro definition; otherwise it
654 is a location in the context of the caller of this macro expansion
655 (which is a virtual location or a source location if the caller is
656 itself a macro expansion or not).
658 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
659 either of the token itself or of a macro parameter that it
660 replaces. */
662 source_location
663 linemap_add_macro_token (const line_map_macro *map,
664 unsigned int token_no,
665 source_location orig_loc,
666 source_location orig_parm_replacement_loc)
668 source_location result;
670 linemap_assert (linemap_macro_expansion_map_p (map));
671 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
673 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
674 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
676 result = MAP_START_LOCATION (map) + token_no;
677 return result;
680 /* Return a source_location for the start (i.e. column==0) of
681 (physical) line TO_LINE in the current source file (as in the
682 most recent linemap_add). MAX_COLUMN_HINT is the highest column
683 number we expect to use in this line (but it does not change
684 the highest_location). */
686 source_location
687 linemap_line_start (struct line_maps *set, linenum_type to_line,
688 unsigned int max_column_hint)
690 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
691 source_location highest = set->highest_location;
692 source_location r;
693 linenum_type last_line =
694 SOURCE_LINE (map, set->highest_line);
695 int line_delta = to_line - last_line;
696 bool add_map = false;
697 linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
698 int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
700 if (line_delta < 0
701 || (line_delta > 10
702 && line_delta * map->m_column_and_range_bits > 1000)
703 || (max_column_hint >= (1U << effective_column_bits))
704 || (max_column_hint <= 80 && effective_column_bits >= 10)
705 || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
706 && map->m_range_bits > 0)
707 || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
708 && (set->max_column_hint || highest >= LINE_MAP_MAX_SOURCE_LOCATION)))
709 add_map = true;
710 else
711 max_column_hint = set->max_column_hint;
712 if (add_map)
714 int column_bits;
715 int range_bits;
716 if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
717 || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
719 /* If the column number is ridiculous or we've allocated a huge
720 number of source_locations, give up on column numbers
721 (and on packed ranges). */
722 max_column_hint = 0;
723 column_bits = 0;
724 range_bits = 0;
725 if (highest > LINE_MAP_MAX_SOURCE_LOCATION)
726 return 0;
728 else
730 column_bits = 7;
731 if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
732 range_bits = set->default_range_bits;
733 else
734 range_bits = 0;
735 while (max_column_hint >= (1U << column_bits))
736 column_bits++;
737 max_column_hint = 1U << column_bits;
738 column_bits += range_bits;
740 /* Allocate the new line_map. However, if the current map only has a
741 single line we can sometimes just increase its column_bits instead. */
742 if (line_delta < 0
743 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
744 || SOURCE_COLUMN (map, highest) >= (1U << column_bits)
745 || range_bits < map->m_range_bits)
746 map = linemap_check_ordinary
747 (const_cast <line_map *>
748 (linemap_add (set, LC_RENAME,
749 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
750 ORDINARY_MAP_FILE_NAME (map),
751 to_line)));
752 map->m_column_and_range_bits = column_bits;
753 map->m_range_bits = range_bits;
754 r = (MAP_START_LOCATION (map)
755 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
756 << column_bits));
758 else
759 r = set->highest_line + (line_delta << map->m_column_and_range_bits);
761 /* Locations of ordinary tokens are always lower than locations of
762 macro tokens. */
763 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
764 return 0;
766 set->highest_line = r;
767 if (r > set->highest_location)
768 set->highest_location = r;
769 set->max_column_hint = max_column_hint;
771 /* At this point, we expect one of:
772 (a) the normal case: a "pure" location with 0 range bits, or
773 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
774 columns anymore (or ranges), or
775 (c) we're in a region with a column hint exceeding
776 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
777 with column_bits == 0. */
778 linemap_assert (pure_location_p (set, r)
779 || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
780 || map->m_column_and_range_bits == 0);
781 linemap_assert (SOURCE_LINE (map, r) == to_line);
782 return r;
785 /* Encode and return a source_location from a column number. The
786 source line considered is the last source line used to call
787 linemap_line_start, i.e, the last source line which a location was
788 encoded from. */
790 source_location
791 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
793 source_location r = set->highest_line;
795 linemap_assert
796 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
798 if (to_column >= set->max_column_hint)
800 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
801 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
803 /* Running low on source_locations - disable column numbers. */
804 return r;
806 else
808 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
809 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
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;
819 /* Encode and return a source location from a given line and
820 column. */
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)
828 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
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;
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. */
848 source_location
849 linemap_position_for_loc_and_offset (struct line_maps *set,
850 source_location loc,
851 unsigned int column_offset)
853 const line_map_ordinary * map = NULL;
855 if (IS_ADHOC_LOC (loc))
856 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
858 /* This function does not support virtual locations yet. */
859 if (linemap_assert_fails
860 (!linemap_location_from_macro_expansion_p (set, loc)))
861 return loc;
863 if (column_offset == 0
864 /* Adding an offset to a reserved location (like
865 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
866 sense. So let's leave the location intact in that case. */
867 || loc < RESERVED_LOCATION_COUNT)
868 return loc;
870 /* We find the real location and shift it. */
871 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
872 /* The new location (loc + offset) should be higher than the first
873 location encoded by MAP. This can fail if the line information
874 is messed up because of line directives (see PR66415). */
875 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
876 return loc;
878 linenum_type line = SOURCE_LINE (map, loc);
879 unsigned int column = SOURCE_COLUMN (map, loc);
881 /* If MAP is not the last line map of its set, then the new location
882 (loc + offset) should be less than the first location encoded by
883 the next line map of the set. Otherwise, we try to encode the
884 location in the next map. */
885 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
886 && (loc + (column_offset << map->m_range_bits)
887 >= MAP_START_LOCATION (&map[1])))
889 map = &map[1];
890 /* If the next map starts in a higher line, we cannot encode the
891 location there. */
892 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
893 return loc;
896 column += column_offset;
897 if (linemap_assert_fails (column < (1u << map->m_column_and_range_bits)))
898 return loc;
900 source_location r =
901 linemap_position_for_line_and_column (set, map, line, column);
902 if (linemap_assert_fails (r <= set->highest_location)
903 || linemap_assert_fails (map == linemap_lookup (set, r)))
904 return loc;
906 return r;
909 /* Given a virtual source location yielded by a map (either an
910 ordinary or a macro map), returns that map. */
912 const struct line_map*
913 linemap_lookup (struct line_maps *set, source_location line)
915 if (IS_ADHOC_LOC (line))
916 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
917 if (linemap_location_from_macro_expansion_p (set, line))
918 return linemap_macro_map_lookup (set, line);
919 return linemap_ordinary_map_lookup (set, line);
922 /* Given a source location yielded by an ordinary map, returns that
923 map. Since the set is built chronologically, the logical lines are
924 monotonic increasing, and so the list is sorted and we can use a
925 binary search. */
927 static const line_map_ordinary *
928 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
930 unsigned int md, mn, mx;
931 const line_map_ordinary *cached, *result;
933 if (IS_ADHOC_LOC (line))
934 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
936 if (set == NULL || line < RESERVED_LOCATION_COUNT)
937 return NULL;
939 mn = LINEMAPS_ORDINARY_CACHE (set);
940 mx = LINEMAPS_ORDINARY_USED (set);
942 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
943 /* We should get a segfault if no line_maps have been added yet. */
944 if (line >= MAP_START_LOCATION (cached))
946 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
947 return cached;
949 else
951 mx = mn;
952 mn = 0;
955 while (mx - mn > 1)
957 md = (mn + mx) / 2;
958 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
959 mx = md;
960 else
961 mn = md;
964 LINEMAPS_ORDINARY_CACHE (set) = mn;
965 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
966 linemap_assert (line >= MAP_START_LOCATION (result));
967 return result;
970 /* Given a source location yielded by a macro map, returns that map.
971 Since the set is built chronologically, the logical lines are
972 monotonic decreasing, and so the list is sorted and we can use a
973 binary search. */
975 static const line_map_macro *
976 linemap_macro_map_lookup (struct line_maps *set, source_location line)
978 unsigned int md, mn, mx;
979 const struct line_map_macro *cached, *result;
981 if (IS_ADHOC_LOC (line))
982 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
984 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
986 if (set == NULL)
987 return NULL;
989 mn = LINEMAPS_MACRO_CACHE (set);
990 mx = LINEMAPS_MACRO_USED (set);
991 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
993 if (line >= MAP_START_LOCATION (cached))
995 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
996 return cached;
997 mx = mn - 1;
998 mn = 0;
1001 while (mn < mx)
1003 md = (mx + mn) / 2;
1004 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1005 mn = md + 1;
1006 else
1007 mx = md;
1010 LINEMAPS_MACRO_CACHE (set) = mx;
1011 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
1012 linemap_assert (MAP_START_LOCATION (result) <= line);
1014 return result;
1017 /* Return TRUE if MAP encodes locations coming from a macro
1018 replacement-list at macro expansion point. */
1020 bool
1021 linemap_macro_expansion_map_p (const struct line_map *map)
1023 if (!map)
1024 return false;
1025 return (map->reason == LC_ENTER_MACRO);
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.
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. */
1034 static source_location
1035 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1036 source_location location ATTRIBUTE_UNUSED)
1038 linemap_assert (linemap_macro_expansion_map_p (map)
1039 && location >= MAP_START_LOCATION (map));
1041 /* Make sure LOCATION is correct. */
1042 linemap_assert ((location - MAP_START_LOCATION (map))
1043 < MACRO_MAP_NUM_MACRO_TOKENS (map));
1045 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
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.
1051 Return the location of the token at the definition point of the
1052 macro. */
1054 static source_location
1055 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1056 source_location location)
1058 unsigned token_no;
1060 linemap_assert (linemap_macro_expansion_map_p (map)
1061 && location >= MAP_START_LOCATION (map));
1062 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1064 token_no = location - MAP_START_LOCATION (map);
1065 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1067 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1069 return location;
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.
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)
1083 unsigned token_no;
1085 if (IS_ADHOC_LOC (location))
1086 location = get_location_from_adhoc_loc (set, location);
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));
1093 token_no = location - MAP_START_LOCATION (map);
1094 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1096 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1098 return location;
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. */
1108 linemap_get_expansion_line (struct line_maps *set,
1109 source_location location)
1111 const line_map_ordinary *map = NULL;
1113 if (IS_ADHOC_LOC (location))
1114 location = set->location_adhoc_data_map.data[location
1115 & MAX_SOURCE_LOCATION].locus;
1117 if (location < RESERVED_LOCATION_COUNT)
1118 return 0;
1120 location =
1121 linemap_macro_loc_to_exp_point (set, location, &map);
1123 return SOURCE_LINE (map, location);
1126 /* Return the path of the file corresponding to source code location
1127 LOCATION.
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.
1133 SET is the line map set LOCATION comes from. */
1135 const char*
1136 linemap_get_expansion_filename (struct line_maps *set,
1137 source_location location)
1139 const struct line_map_ordinary *map = NULL;
1141 if (IS_ADHOC_LOC (location))
1142 location = set->location_adhoc_data_map.data[location
1143 & MAX_SOURCE_LOCATION].locus;
1145 if (location < RESERVED_LOCATION_COUNT)
1146 return NULL;
1148 location =
1149 linemap_macro_loc_to_exp_point (set, location, &map);
1151 return LINEMAP_FILE (map);
1154 /* Return the name of the macro associated to MACRO_MAP. */
1156 const char*
1157 linemap_map_get_macro_name (const line_map_macro *macro_map)
1159 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1160 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
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++.
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. */
1174 linemap_location_in_system_header_p (struct line_maps *set,
1175 source_location location)
1177 const struct line_map *map = NULL;
1179 if (IS_ADHOC_LOC (location))
1180 location = set->location_adhoc_data_map.data[location
1181 & MAX_SOURCE_LOCATION].locus;
1183 if (location < RESERVED_LOCATION_COUNT)
1184 return false;
1186 /* Let's look at where the token for LOCATION comes from. */
1187 while (true)
1189 map = linemap_lookup (set, location);
1190 if (map != NULL)
1192 if (!linemap_macro_expansion_map_p (map))
1193 /* It's a normal token. */
1194 return LINEMAP_SYSP (linemap_check_ordinary (map));
1195 else
1197 const line_map_macro *macro_map = linemap_check_macro (map);
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;
1210 else
1211 break;
1213 return false;
1216 /* Return TRUE if LOCATION is a source code location of a token coming
1217 from a macro replacement-list at a macro expansion point, FALSE
1218 otherwise. */
1220 bool
1221 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1222 source_location location)
1224 if (IS_ADHOC_LOC (location))
1225 location = set->location_adhoc_data_map.data[location
1226 & MAX_SOURCE_LOCATION].locus;
1228 linemap_assert (location <= MAX_SOURCE_LOCATION
1229 && (set->highest_location
1230 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
1231 if (set == NULL)
1232 return false;
1233 return (location > set->highest_location);
1236 /* Given two virtual locations *LOC0 and *LOC1, return the first
1237 common macro map in their macro expansion histories. Return NULL
1238 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1239 virtual location of the token inside the resulting macro. */
1241 static const struct line_map*
1242 first_map_in_common_1 (struct line_maps *set,
1243 source_location *loc0,
1244 source_location *loc1)
1246 source_location l0 = *loc0, l1 = *loc1;
1247 const struct line_map *map0 = linemap_lookup (set, l0),
1248 *map1 = linemap_lookup (set, l1);
1250 while (linemap_macro_expansion_map_p (map0)
1251 && linemap_macro_expansion_map_p (map1)
1252 && (map0 != map1))
1254 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1256 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1257 l0);
1258 map0 = linemap_lookup (set, l0);
1260 else
1262 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1263 l1);
1264 map1 = linemap_lookup (set, l1);
1268 if (map0 == map1)
1270 *loc0 = l0;
1271 *loc1 = l1;
1272 return map0;
1274 return NULL;
1277 /* Given two virtual locations LOC0 and LOC1, return the first common
1278 macro map in their macro expansion histories. Return NULL if no
1279 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1280 virtual location of the token inside the resulting macro, upon
1281 return of a non-NULL result. */
1283 static const struct line_map*
1284 first_map_in_common (struct line_maps *set,
1285 source_location loc0,
1286 source_location loc1,
1287 source_location *res_loc0,
1288 source_location *res_loc1)
1290 *res_loc0 = loc0;
1291 *res_loc1 = loc1;
1293 return first_map_in_common_1 (set, res_loc0, res_loc1);
1296 /* Return a positive value if PRE denotes the location of a token that
1297 comes before the token of POST, 0 if PRE denotes the location of
1298 the same token as the token for POST, and a negative value
1299 otherwise. */
1302 linemap_compare_locations (struct line_maps *set,
1303 source_location pre,
1304 source_location post)
1306 bool pre_virtual_p, post_virtual_p;
1307 source_location l0 = pre, l1 = post;
1309 if (IS_ADHOC_LOC (l0))
1310 l0 = get_location_from_adhoc_loc (set, l0);
1311 if (IS_ADHOC_LOC (l1))
1312 l1 = get_location_from_adhoc_loc (set, l1);
1314 if (l0 == l1)
1315 return 0;
1317 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1318 l0 = linemap_resolve_location (set, l0,
1319 LRK_MACRO_EXPANSION_POINT,
1320 NULL);
1322 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1323 l1 = linemap_resolve_location (set, l1,
1324 LRK_MACRO_EXPANSION_POINT,
1325 NULL);
1327 if (l0 == l1
1328 && pre_virtual_p
1329 && post_virtual_p)
1331 /* So pre and post represent two tokens that are present in a
1332 same macro expansion. Let's see if the token for pre was
1333 before the token for post in that expansion. */
1334 unsigned i0, i1;
1335 const struct line_map *map =
1336 first_map_in_common (set, pre, post, &l0, &l1);
1338 if (map == NULL)
1339 /* This should not be possible. */
1340 abort ();
1342 i0 = l0 - MAP_START_LOCATION (map);
1343 i1 = l1 - MAP_START_LOCATION (map);
1344 return i1 - i0;
1347 if (IS_ADHOC_LOC (l0))
1348 l0 = get_location_from_adhoc_loc (set, l0);
1349 if (IS_ADHOC_LOC (l1))
1350 l1 = get_location_from_adhoc_loc (set, l1);
1352 return l1 - l0;
1355 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1357 static void
1358 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1360 unsigned int i = set->depth;
1362 while (--i)
1363 putc ('.', stderr);
1365 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1368 /* Return the spelling location of the token wherever it comes from,
1369 whether part of a macro definition or not.
1371 This is a subroutine for linemap_resolve_location. */
1373 static source_location
1374 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1375 source_location location,
1376 const line_map_ordinary **original_map)
1378 struct line_map *map;
1379 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1381 while (true)
1383 map = const_cast <line_map *> (linemap_lookup (set, location));
1384 if (!linemap_macro_expansion_map_p (map))
1385 break;
1387 location
1388 = linemap_macro_map_loc_unwind_toward_spelling
1389 (set, linemap_check_macro (map),
1390 location);
1393 if (original_map)
1394 *original_map = linemap_check_ordinary (map);
1395 return location;
1398 /* If LOCATION is the source location of a token that belongs to a
1399 macro replacement-list -- as part of a macro expansion -- then
1400 return the location of the token at the definition point of the
1401 macro. Otherwise, return LOCATION. SET is the set of maps
1402 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1403 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1404 returned location comes from.
1406 This is a subroutine of linemap_resolve_location. */
1408 static source_location
1409 linemap_macro_loc_to_def_point (struct line_maps *set,
1410 source_location location,
1411 const line_map_ordinary **original_map)
1413 struct line_map *map;
1415 if (IS_ADHOC_LOC (location))
1416 location = set->location_adhoc_data_map.data[location
1417 & MAX_SOURCE_LOCATION].locus;
1419 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1421 while (true)
1423 map = const_cast <line_map *> (linemap_lookup (set, location));
1424 if (!linemap_macro_expansion_map_p (map))
1425 break;
1427 location =
1428 linemap_macro_map_loc_to_def_point (linemap_check_macro (map),
1429 location);
1432 if (original_map)
1433 *original_map = linemap_check_ordinary (map);
1434 return location;
1437 /* If LOCATION is the source location of a token that belongs to a
1438 macro replacement-list -- at a macro expansion point -- then return
1439 the location of the topmost expansion point of the macro. We say
1440 topmost because if we are in the context of a nested macro
1441 expansion, the function returns the source location of the first
1442 macro expansion that triggered the nested expansions.
1444 Otherwise, return LOCATION. SET is the set of maps location come
1445 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1446 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1447 location comes from.
1449 This is a subroutine of linemap_resolve_location. */
1451 static source_location
1452 linemap_macro_loc_to_exp_point (struct line_maps *set,
1453 source_location location,
1454 const line_map_ordinary **original_map)
1456 struct line_map *map;
1458 if (IS_ADHOC_LOC (location))
1459 location = set->location_adhoc_data_map.data[location
1460 & MAX_SOURCE_LOCATION].locus;
1462 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1464 while (true)
1466 map = const_cast <line_map *> (linemap_lookup (set, location));
1467 if (!linemap_macro_expansion_map_p (map))
1468 break;
1469 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1470 location);
1473 if (original_map)
1474 *original_map = linemap_check_ordinary (map);
1475 return location;
1478 /* Resolve a virtual location into either a spelling location, an
1479 expansion point location or a token argument replacement point
1480 location. Return the map that encodes the virtual location as well
1481 as the resolved location.
1483 If LOC is *NOT* the location of a token resulting from the
1484 expansion of a macro, then the parameter LRK (which stands for
1485 Location Resolution Kind) is ignored and the resulting location
1486 just equals the one given in argument.
1488 Now if LOC *IS* the location of a token resulting from the
1489 expansion of a macro, this is what happens.
1491 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1492 -------------------------------
1494 The virtual location is resolved to the first macro expansion point
1495 that led to this macro expansion.
1497 * If LRK is set to LRK_SPELLING_LOCATION
1498 -------------------------------------
1500 The virtual location is resolved to the locus where the token has
1501 been spelled in the source. This can follow through all the macro
1502 expansions that led to the token.
1504 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1505 --------------------------------------
1507 The virtual location is resolved to the locus of the token in the
1508 context of the macro definition.
1510 If LOC is the locus of a token that is an argument of a
1511 function-like macro [replacing a parameter in the replacement list
1512 of the macro] the virtual location is resolved to the locus of the
1513 parameter that is replaced, in the context of the definition of the
1514 macro.
1516 If LOC is the locus of a token that is not an argument of a
1517 function-like macro, then the function behaves as if LRK was set to
1518 LRK_SPELLING_LOCATION.
1520 If MAP is not NULL, *MAP is set to the map encoding the
1521 returned location. Note that if the returned location wasn't originally
1522 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1523 resolves to a location reserved for the client code, like
1524 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1526 source_location
1527 linemap_resolve_location (struct line_maps *set,
1528 source_location loc,
1529 enum location_resolution_kind lrk,
1530 const line_map_ordinary **map)
1532 source_location locus = loc;
1533 if (IS_ADHOC_LOC (loc))
1534 locus = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1536 if (locus < RESERVED_LOCATION_COUNT)
1538 /* A reserved location wasn't encoded in a map. Let's return a
1539 NULL map here, just like what linemap_ordinary_map_lookup
1540 does. */
1541 if (map)
1542 *map = NULL;
1543 return loc;
1546 switch (lrk)
1548 case LRK_MACRO_EXPANSION_POINT:
1549 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1550 break;
1551 case LRK_SPELLING_LOCATION:
1552 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1553 break;
1554 case LRK_MACRO_DEFINITION_LOCATION:
1555 loc = linemap_macro_loc_to_def_point (set, loc, map);
1556 break;
1557 default:
1558 abort ();
1560 return loc;
1564 Suppose that LOC is the virtual location of a token T coming from
1565 the expansion of a macro M. This function then steps up to get the
1566 location L of the point where M got expanded. If L is a spelling
1567 location inside a macro expansion M', then this function returns
1568 the locus of the point where M' was expanded. Said otherwise, this
1569 function returns the location of T in the context that triggered
1570 the expansion of M.
1572 *LOC_MAP must be set to the map of LOC. This function then sets it
1573 to the map of the returned location. */
1575 source_location
1576 linemap_unwind_toward_expansion (struct line_maps *set,
1577 source_location loc,
1578 const struct line_map **map)
1580 source_location resolved_location;
1581 const line_map_macro *macro_map = linemap_check_macro (*map);
1582 const struct line_map *resolved_map;
1584 if (IS_ADHOC_LOC (loc))
1585 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1587 resolved_location =
1588 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1589 resolved_map = linemap_lookup (set, resolved_location);
1591 if (!linemap_macro_expansion_map_p (resolved_map))
1593 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1594 resolved_map = linemap_lookup (set, resolved_location);
1597 *map = resolved_map;
1598 return resolved_location;
1601 /* If LOC is the virtual location of a token coming from the expansion
1602 of a macro M and if its spelling location is reserved (e.g, a
1603 location for a built-in token), then this function unwinds (using
1604 linemap_unwind_toward_expansion) the location until a location that
1605 is not reserved and is not in a system header is reached. In other
1606 words, this unwinds the reserved location until a location that is
1607 in real source code is reached.
1609 Otherwise, if the spelling location for LOC is not reserved or if
1610 LOC doesn't come from the expansion of a macro, the function
1611 returns LOC as is and *MAP is not touched.
1613 *MAP is set to the map of the returned location if the later is
1614 different from LOC. */
1615 source_location
1616 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1617 source_location loc,
1618 const struct line_map **map)
1620 source_location resolved_loc;
1621 const struct line_map *map0 = NULL;
1622 const line_map_ordinary *map1 = NULL;
1624 if (IS_ADHOC_LOC (loc))
1625 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1627 map0 = linemap_lookup (set, loc);
1628 if (!linemap_macro_expansion_map_p (map0))
1629 return loc;
1631 resolved_loc = linemap_resolve_location (set, loc,
1632 LRK_SPELLING_LOCATION,
1633 &map1);
1635 if (resolved_loc >= RESERVED_LOCATION_COUNT
1636 && !LINEMAP_SYSP (map1))
1637 return loc;
1639 while (linemap_macro_expansion_map_p (map0)
1640 && (resolved_loc < RESERVED_LOCATION_COUNT
1641 || LINEMAP_SYSP (map1)))
1643 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1644 resolved_loc = linemap_resolve_location (set, loc,
1645 LRK_SPELLING_LOCATION,
1646 &map1);
1649 if (map != NULL)
1650 *map = map0;
1651 return loc;
1654 /* Expand source code location LOC and return a user readable source
1655 code location. LOC must be a spelling (non-virtual) location. If
1656 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1657 location is returned. */
1659 expanded_location
1660 linemap_expand_location (struct line_maps *set,
1661 const struct line_map *map,
1662 source_location loc)
1665 expanded_location xloc;
1667 memset (&xloc, 0, sizeof (xloc));
1668 if (IS_ADHOC_LOC (loc))
1670 xloc.data
1671 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1672 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1675 if (loc < RESERVED_LOCATION_COUNT)
1676 /* The location for this token wasn't generated from a line map.
1677 It was probably a location for a builtin token, chosen by some
1678 client code. Let's not try to expand the location in that
1679 case. */;
1680 else if (map == NULL)
1681 /* We shouldn't be getting a NULL map with a location that is not
1682 reserved by the client code. */
1683 abort ();
1684 else
1686 /* MAP must be an ordinary map and LOC must be non-virtual,
1687 encoded into this map, obviously; the accessors used on MAP
1688 below ensure it is ordinary. Let's just assert the
1689 non-virtualness of LOC here. */
1690 if (linemap_location_from_macro_expansion_p (set, loc))
1691 abort ();
1693 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1695 xloc.file = LINEMAP_FILE (ord_map);
1696 xloc.line = SOURCE_LINE (ord_map, loc);
1697 xloc.column = SOURCE_COLUMN (ord_map, loc);
1698 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1701 return xloc;
1705 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1706 is NULL, use stderr. IS_MACRO is true if the caller wants to
1707 dump a macro map, false otherwise. */
1709 void
1710 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1712 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1713 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1714 "LC_ENTER_MACRO" };
1715 const char *reason;
1716 const line_map *map;
1718 if (stream == NULL)
1719 stream = stderr;
1721 if (!is_macro)
1722 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1723 else
1724 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1726 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1728 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1729 ix, (void *) map, map->start_location, reason,
1730 ((!is_macro
1731 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1732 ? "yes" : "no"));
1733 if (!is_macro)
1735 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1736 unsigned includer_ix;
1737 const line_map_ordinary *includer_map;
1739 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
1740 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1741 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1742 : NULL;
1744 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1745 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1746 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1747 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1749 else
1751 const line_map_macro *macro_map = linemap_check_macro (map);
1752 fprintf (stream, "Macro: %s (%u tokens)\n",
1753 linemap_map_get_macro_name (macro_map),
1754 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1757 fprintf (stream, "\n");
1761 /* Dump debugging information about source location LOC into the file
1762 stream STREAM. SET is the line map set LOC comes from. */
1764 void
1765 linemap_dump_location (struct line_maps *set,
1766 source_location loc,
1767 FILE *stream)
1769 const line_map_ordinary *map;
1770 source_location location;
1771 const char *path = "", *from = "";
1772 int l = -1, c = -1, s = -1, e = -1;
1774 if (IS_ADHOC_LOC (loc))
1775 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1777 if (loc == 0)
1778 return;
1780 location =
1781 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1783 if (map == NULL)
1784 /* Only reserved locations can be tolerated in this case. */
1785 linemap_assert (location < RESERVED_LOCATION_COUNT);
1786 else
1788 path = LINEMAP_FILE (map);
1789 l = SOURCE_LINE (map, location);
1790 c = SOURCE_COLUMN (map, location);
1791 s = LINEMAP_SYSP (map) != 0;
1792 e = location != loc;
1793 if (e)
1794 from = "N/A";
1795 else
1796 from = (INCLUDED_FROM (set, map))
1797 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1798 : "<NULL>";
1801 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1802 E: macro expansion?, LOC: original location, R: resolved location */
1803 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1804 path, from, l, c, s, (void*)map, e, loc, location);
1807 /* Return the highest location emitted for a given file for which
1808 there is a line map in SET. FILE_NAME is the file name to
1809 consider. If the function returns TRUE, *LOC is set to the highest
1810 location emitted for that file. */
1812 bool
1813 linemap_get_file_highest_location (struct line_maps *set,
1814 const char *file_name,
1815 source_location *loc)
1817 /* If the set is empty or no ordinary map has been created then
1818 there is no file to look for ... */
1819 if (set == NULL || set->info_ordinary.used == 0)
1820 return false;
1822 /* Now look for the last ordinary map created for FILE_NAME. */
1823 int i;
1824 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1826 const char *fname = set->info_ordinary.maps[i].to_file;
1827 if (fname && !filename_cmp (fname, file_name))
1828 break;
1831 if (i < 0)
1832 return false;
1834 /* The highest location for a given map is either the starting
1835 location of the next map minus one, or -- if the map is the
1836 latest one -- the highest location of the set. */
1837 source_location result;
1838 if (i == (int) set->info_ordinary.used - 1)
1839 result = set->highest_location;
1840 else
1841 result = set->info_ordinary.maps[i + 1].start_location - 1;
1843 *loc = result;
1844 return true;
1847 /* Compute and return statistics about the memory consumption of some
1848 parts of the line table SET. */
1850 void
1851 linemap_get_statistics (struct line_maps *set,
1852 struct linemap_stats *s)
1854 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1855 macro_maps_allocated_size, macro_maps_used_size,
1856 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1858 const line_map_macro *cur_map;
1860 ordinary_maps_allocated_size =
1861 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1863 ordinary_maps_used_size =
1864 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1866 macro_maps_allocated_size =
1867 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1869 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1870 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1871 ++cur_map)
1873 unsigned i;
1875 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1877 macro_maps_locations_size +=
1878 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1880 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1882 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1883 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1884 duplicated_macro_maps_locations_size +=
1885 sizeof (source_location);
1889 macro_maps_used_size =
1890 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1892 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1893 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1894 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1895 s->ordinary_maps_used_size = ordinary_maps_used_size;
1896 s->num_expanded_macros = num_expanded_macros_counter;
1897 s->num_macro_tokens = num_macro_tokens_counter;
1898 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1899 s->macro_maps_allocated_size = macro_maps_allocated_size;
1900 s->macro_maps_locations_size = macro_maps_locations_size;
1901 s->macro_maps_used_size = macro_maps_used_size;
1902 s->duplicated_macro_maps_locations_size =
1903 duplicated_macro_maps_locations_size;
1904 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1905 * sizeof (struct location_adhoc_data));
1906 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
1910 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1911 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1912 specifies how many macro maps to dump. */
1914 void
1915 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1916 unsigned int num_macro)
1918 unsigned int i;
1920 if (set == NULL)
1921 return;
1923 if (stream == NULL)
1924 stream = stderr;
1926 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1927 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1928 fprintf (stream, "Include stack depth: %d\n", set->depth);
1929 fprintf (stream, "Highest location: %u\n", set->highest_location);
1931 if (num_ordinary)
1933 fprintf (stream, "\nOrdinary line maps\n");
1934 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1935 linemap_dump (stream, set, i, false);
1936 fprintf (stream, "\n");
1939 if (num_macro)
1941 fprintf (stream, "\nMacro line maps\n");
1942 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1943 linemap_dump (stream, set, i, true);
1944 fprintf (stream, "\n");
1948 /* struct source_range. */
1950 /* Is there any part of this range on the given line? */
1952 bool
1953 source_range::intersects_line_p (const char *file, int line) const
1955 expanded_location exploc_start
1956 = linemap_client_expand_location_to_spelling_point (m_start);
1957 if (file != exploc_start.file)
1958 return false;
1959 if (line < exploc_start.line)
1960 return false;
1961 expanded_location exploc_finish
1962 = linemap_client_expand_location_to_spelling_point (m_finish);
1963 if (file != exploc_finish.file)
1964 return false;
1965 if (line > exploc_finish.line)
1966 return false;
1967 return true;
1970 /* class rich_location. */
1972 /* Construct a rich_location with location LOC as its initial range. */
1974 rich_location::rich_location (line_maps */*set*/, source_location loc) :
1975 m_num_ranges (0),
1976 m_column_override (0),
1977 m_have_expanded_location (false),
1978 m_num_fixit_hints (0)
1980 add_range (loc, true);
1983 /* The destructor for class rich_location. */
1985 rich_location::~rich_location ()
1987 for (unsigned int i = 0; i < m_num_fixit_hints; i++)
1988 delete m_fixit_hints[i];
1991 /* Get location IDX within this rich_location. */
1993 source_location
1994 rich_location::get_loc (unsigned int idx) const
1996 linemap_assert (idx < m_num_ranges);
1997 return m_ranges[idx].m_loc;
2000 /* Expand location IDX within this rich_location. */
2001 /* Get an expanded_location for this rich_location's primary
2002 location. */
2004 expanded_location
2005 rich_location::get_expanded_location (unsigned int idx)
2007 if (idx == 0)
2009 /* Cache the expansion of the primary location. */
2010 if (!m_have_expanded_location)
2012 m_expanded_location
2013 = linemap_client_expand_location_to_spelling_point (get_loc (0));
2014 if (m_column_override)
2015 m_expanded_location.column = m_column_override;
2016 m_have_expanded_location = true;
2019 return m_expanded_location;
2021 else
2022 return linemap_client_expand_location_to_spelling_point (get_loc (idx));
2025 /* Set the column of the primary location, with 0 meaning
2026 "don't override it". */
2028 void
2029 rich_location::override_column (int column)
2031 m_column_override = column;
2032 m_have_expanded_location = false;
2035 /* Add the given range. */
2037 void
2038 rich_location::add_range (source_location loc, bool show_caret_p)
2040 linemap_assert (m_num_ranges < MAX_RANGES);
2042 location_range *range = &m_ranges[m_num_ranges++];
2043 range->m_loc = loc;
2044 range->m_show_caret_p = show_caret_p;
2047 /* Add or overwrite the location given by IDX, setting its location to LOC,
2048 and setting its "should my caret be printed" flag to SHOW_CARET_P.
2050 It must either overwrite an existing location, or add one *exactly* on
2051 the end of the array.
2053 This is primarily for use by gcc when implementing diagnostic format
2054 decoders e.g.
2055 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2056 (which writes the source location of a tree back into location 0 of
2057 the rich_location), and
2058 - the "%C" and "%L" format codes in the Fortran frontend. */
2060 void
2061 rich_location::set_range (line_maps * /*set*/, unsigned int idx,
2062 source_location loc, bool show_caret_p)
2064 linemap_assert (idx < MAX_RANGES);
2066 /* We can either overwrite an existing range, or add one exactly
2067 on the end of the array. */
2068 linemap_assert (idx <= m_num_ranges);
2070 location_range *locrange = &m_ranges[idx];
2071 locrange->m_loc = loc;
2072 locrange->m_show_caret_p = show_caret_p;
2074 /* Are we adding a range onto the end? */
2075 if (idx == m_num_ranges)
2076 m_num_ranges = idx + 1;
2078 if (idx == 0)
2079 /* Mark any cached value here as dirty. */
2080 m_have_expanded_location = false;
2083 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2084 at WHERE. */
2086 void
2087 rich_location::add_fixit_insert (source_location where,
2088 const char *new_content)
2090 linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2091 m_fixit_hints[m_num_fixit_hints++]
2092 = new fixit_insert (where, new_content);
2095 /* Add a fixit-hint, suggesting removal of the content at
2096 SRC_RANGE. */
2098 void
2099 rich_location::add_fixit_remove (source_range src_range)
2101 linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2102 m_fixit_hints[m_num_fixit_hints++] = new fixit_remove (src_range);
2105 /* Add a fixit-hint, suggesting replacement of the content at
2106 SRC_RANGE with NEW_CONTENT. */
2108 void
2109 rich_location::add_fixit_replace (source_range src_range,
2110 const char *new_content)
2112 linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2113 m_fixit_hints[m_num_fixit_hints++]
2114 = new fixit_replace (src_range, new_content);
2117 /* class fixit_insert. */
2119 fixit_insert::fixit_insert (source_location where,
2120 const char *new_content)
2121 : m_where (where),
2122 m_bytes (xstrdup (new_content)),
2123 m_len (strlen (new_content))
2127 fixit_insert::~fixit_insert ()
2129 free (m_bytes);
2132 /* Implementation of fixit_hint::affects_line_p for fixit_insert. */
2134 bool
2135 fixit_insert::affects_line_p (const char *file, int line)
2137 expanded_location exploc
2138 = linemap_client_expand_location_to_spelling_point (m_where);
2139 if (file == exploc.file)
2140 if (line == exploc.line)
2141 return true;
2142 return false;
2145 /* class fixit_remove. */
2147 fixit_remove::fixit_remove (source_range src_range)
2148 : m_src_range (src_range)
2152 /* Implementation of fixit_hint::affects_line_p for fixit_remove. */
2154 bool
2155 fixit_remove::affects_line_p (const char *file, int line)
2157 return m_src_range.intersects_line_p (file, line);
2160 /* class fixit_replace. */
2162 fixit_replace::fixit_replace (source_range src_range,
2163 const char *new_content)
2164 : m_src_range (src_range),
2165 m_bytes (xstrdup (new_content)),
2166 m_len (strlen (new_content))
2170 fixit_replace::~fixit_replace ()
2172 free (m_bytes);
2175 /* Implementation of fixit_hint::affects_line_p for fixit_replace. */
2177 bool
2178 fixit_replace::affects_line_p (const char *file, int line)
2180 return m_src_range.intersects_line_p (file, line);