* ipa-inline-analysis.c: Include gimplify.h
[official-gcc.git] / libcpp / line-map.c
blob42843038e3bf2c89fa25f78647f269a8f7c94999
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2015 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 track column numbers if locations get higher than this. */
35 const source_location LINE_MAP_MAX_LOCATION_WITH_COLS = 0x60000000;
37 /* Highest possible source location encoded within an ordinary or
38 macro map. */
39 const source_location LINE_MAP_MAX_SOURCE_LOCATION = 0x70000000;
41 static void trace_include (const struct line_maps *, const line_map_ordinary *);
42 static const line_map_ordinary * linemap_ordinary_map_lookup (struct line_maps *,
43 source_location);
44 static const line_map_macro* linemap_macro_map_lookup (struct line_maps *,
45 source_location);
46 static source_location linemap_macro_map_loc_to_def_point
47 (const line_map_macro *, source_location);
48 static source_location linemap_macro_map_loc_unwind_toward_spelling
49 (line_maps *set, const line_map_macro *, source_location);
50 static source_location linemap_macro_map_loc_to_exp_point
51 (const line_map_macro *, source_location);
52 static source_location linemap_macro_loc_to_spelling_point
53 (struct line_maps *, source_location, const line_map_ordinary **);
54 static source_location linemap_macro_loc_to_def_point (struct line_maps *,
55 source_location,
56 const line_map_ordinary **);
57 static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
58 source_location,
59 const line_map_ordinary **);
61 /* Counters defined in macro.c. */
62 extern unsigned num_expanded_macros_counter;
63 extern unsigned num_macro_tokens_counter;
65 /* Hash function for location_adhoc_data hashtable. */
67 static hashval_t
68 location_adhoc_data_hash (const void *l)
70 const struct location_adhoc_data *lb =
71 (const struct location_adhoc_data *) l;
72 return ((hashval_t) lb->locus
73 + (hashval_t) lb->src_range.m_start
74 + (hashval_t) lb->src_range.m_finish
75 + (size_t) lb->data);
78 /* Compare function for location_adhoc_data hashtable. */
80 static int
81 location_adhoc_data_eq (const void *l1, const void *l2)
83 const struct location_adhoc_data *lb1 =
84 (const struct location_adhoc_data *) l1;
85 const struct location_adhoc_data *lb2 =
86 (const struct location_adhoc_data *) l2;
87 return (lb1->locus == lb2->locus
88 && lb1->src_range.m_start == lb2->src_range.m_start
89 && lb1->src_range.m_finish == lb2->src_range.m_finish
90 && lb1->data == lb2->data);
93 /* Update the hashtable when location_adhoc_data is reallocated. */
95 static int
96 location_adhoc_data_update (void **slot, void *data)
98 *((char **) slot) += *((long long *) data);
99 return 1;
102 /* Rebuild the hash table from the location adhoc data. */
104 void
105 rebuild_location_adhoc_htab (struct line_maps *set)
107 unsigned i;
108 set->location_adhoc_data_map.htab =
109 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
110 for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
111 htab_find_slot (set->location_adhoc_data_map.htab,
112 set->location_adhoc_data_map.data + i, INSERT);
115 /* Helper function for get_combined_adhoc_loc.
116 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
117 within a source_location, without needing to use an ad-hoc location. */
119 static bool
120 can_be_stored_compactly_p (struct line_maps *set,
121 source_location locus,
122 source_range src_range,
123 void *data)
125 /* If there's an ad-hoc pointer, we can't store it directly in the
126 source_location, we need the lookaside. */
127 if (data)
128 return false;
130 /* We only store ranges that begin at the locus and that are sufficiently
131 "sane". */
132 if (src_range.m_start != locus)
133 return false;
135 if (src_range.m_finish < src_range.m_start)
136 return false;
138 if (src_range.m_start < RESERVED_LOCATION_COUNT)
139 return false;
141 if (locus >= LINE_MAP_MAX_LOCATION_WITH_COLS)
142 return false;
144 /* All 3 locations must be within ordinary maps, typically, the same
145 ordinary map. */
146 source_location lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
147 if (locus >= lowest_macro_loc)
148 return false;
149 if (src_range.m_start >= lowest_macro_loc)
150 return false;
151 if (src_range.m_finish >= lowest_macro_loc)
152 return false;
154 /* Passed all tests. */
155 return true;
158 /* Combine LOCUS and DATA to a combined adhoc loc. */
160 source_location
161 get_combined_adhoc_loc (struct line_maps *set,
162 source_location locus,
163 source_range src_range,
164 void *data)
166 struct location_adhoc_data lb;
167 struct location_adhoc_data **slot;
169 if (IS_ADHOC_LOC (locus))
170 locus
171 = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
172 if (locus == 0 && data == NULL)
173 return 0;
175 /* Any ordinary locations ought to be "pure" at this point: no
176 compressed ranges. */
177 linemap_assert (locus < RESERVED_LOCATION_COUNT
178 || locus >= LINE_MAP_MAX_LOCATION_WITH_COLS
179 || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
180 || pure_location_p (set, locus));
182 /* Consider short-range optimization. */
183 if (can_be_stored_compactly_p (set, locus, src_range, data))
185 /* The low bits ought to be clear. */
186 linemap_assert (pure_location_p (set, locus));
187 const line_map *map = linemap_lookup (set, locus);
188 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
189 unsigned int int_diff = src_range.m_finish - src_range.m_start;
190 unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
191 if (col_diff < (1U << ordmap->m_range_bits))
193 source_location packed = locus | col_diff;
194 set->num_optimized_ranges++;
195 return packed;
199 /* We can also compactly store the reserved locations
200 when locus == start == finish (and data is NULL). */
201 if (locus < RESERVED_LOCATION_COUNT
202 && locus == src_range.m_start
203 && locus == src_range.m_finish
204 && !data)
205 return locus;
207 if (!data)
208 set->num_unoptimized_ranges++;
210 lb.locus = locus;
211 lb.src_range = src_range;
212 lb.data = data;
213 slot = (struct location_adhoc_data **)
214 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
215 if (*slot == NULL)
217 if (set->location_adhoc_data_map.curr_loc >=
218 set->location_adhoc_data_map.allocated)
220 char *orig_data = (char *) set->location_adhoc_data_map.data;
221 long long offset;
222 /* Cast away extern "C" from the type of xrealloc. */
223 line_map_realloc reallocator = (set->reallocator
224 ? set->reallocator
225 : (line_map_realloc) xrealloc);
227 if (set->location_adhoc_data_map.allocated == 0)
228 set->location_adhoc_data_map.allocated = 128;
229 else
230 set->location_adhoc_data_map.allocated *= 2;
231 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
232 reallocator (set->location_adhoc_data_map.data,
233 set->location_adhoc_data_map.allocated
234 * sizeof (struct location_adhoc_data));
235 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
236 if (set->location_adhoc_data_map.allocated > 128)
237 htab_traverse (set->location_adhoc_data_map.htab,
238 location_adhoc_data_update, &offset);
240 *slot = set->location_adhoc_data_map.data
241 + set->location_adhoc_data_map.curr_loc;
242 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
243 = lb;
245 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
248 /* Return the data for the adhoc loc. */
250 void *
251 get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
253 linemap_assert (IS_ADHOC_LOC (loc));
254 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
257 /* Return the location for the adhoc loc. */
259 source_location
260 get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
262 linemap_assert (IS_ADHOC_LOC (loc));
263 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
266 /* Return the source_range for adhoc location LOC. */
268 static source_range
269 get_range_from_adhoc_loc (struct line_maps *set, source_location loc)
271 linemap_assert (IS_ADHOC_LOC (loc));
272 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].src_range;
275 /* Get the source_range of location LOC, either from the ad-hoc
276 lookaside table, or embedded inside LOC itself. */
278 source_range
279 get_range_from_loc (struct line_maps *set,
280 source_location loc)
282 if (IS_ADHOC_LOC (loc))
283 return get_range_from_adhoc_loc (set, loc);
285 /* For ordinary maps, extract packed range. */
286 if (loc >= RESERVED_LOCATION_COUNT
287 && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
288 && loc <= LINE_MAP_MAX_LOCATION_WITH_COLS)
290 const line_map *map = linemap_lookup (set, loc);
291 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
292 source_range result;
293 int offset = loc & ((1 << ordmap->m_range_bits) - 1);
294 result.m_start = loc - offset;
295 result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
296 return result;
299 return source_range::from_location (loc);
302 /* Get whether location LOC is a "pure" location, or
303 whether it is an ad-hoc location, or embeds range information. */
305 bool
306 pure_location_p (line_maps *set, source_location loc)
308 if (IS_ADHOC_LOC (loc))
309 return false;
311 const line_map *map = linemap_lookup (set, loc);
312 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
314 if (loc & ((1U << ordmap->m_range_bits) - 1))
315 return false;
317 return true;
320 /* Finalize the location_adhoc_data structure. */
321 void
322 location_adhoc_data_fini (struct line_maps *set)
324 htab_delete (set->location_adhoc_data_map.htab);
327 /* Initialize a line map set. */
329 void
330 linemap_init (struct line_maps *set,
331 source_location builtin_location)
333 memset (set, 0, sizeof (struct line_maps));
334 set->highest_location = RESERVED_LOCATION_COUNT - 1;
335 set->highest_line = RESERVED_LOCATION_COUNT - 1;
336 set->location_adhoc_data_map.htab =
337 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
338 set->builtin_location = builtin_location;
341 /* Check for and warn about line_maps entered but not exited. */
343 void
344 linemap_check_files_exited (struct line_maps *set)
346 const line_map_ordinary *map;
347 /* Depending upon whether we are handling preprocessed input or
348 not, this can be a user error or an ICE. */
349 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
350 ! MAIN_FILE_P (map);
351 map = INCLUDED_FROM (set, map))
352 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
353 ORDINARY_MAP_FILE_NAME (map));
356 /* Create a new line map in the line map set SET, and return it.
357 REASON is the reason of creating the map. It determines the type
358 of map created (ordinary or macro map). Note that ordinary maps and
359 macro maps are allocated in different memory location. */
361 static struct line_map *
362 new_linemap (struct line_maps *set,
363 enum lc_reason reason)
365 /* Depending on this variable, a macro map would be allocated in a
366 different memory location than an ordinary map. */
367 bool macro_map_p = (reason == LC_ENTER_MACRO);
368 struct line_map *result;
370 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
372 /* We ran out of allocated line maps. Let's allocate more. */
373 unsigned alloc_size;
375 /* Cast away extern "C" from the type of xrealloc. */
376 line_map_realloc reallocator = (set->reallocator
377 ? set->reallocator
378 : (line_map_realloc) xrealloc);
379 line_map_round_alloc_size_func round_alloc_size =
380 set->round_alloc_size;
382 size_t map_size = (macro_map_p
383 ? sizeof (line_map_macro)
384 : sizeof (line_map_ordinary));
386 /* We are going to execute some dance to try to reduce the
387 overhead of the memory allocator, in case we are using the
388 ggc-page.c one.
390 The actual size of memory we are going to get back from the
391 allocator is the smallest power of 2 that is greater than the
392 size we requested. So let's consider that size then. */
394 alloc_size =
395 (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
396 * map_size;
398 /* Get the actual size of memory that is going to be allocated
399 by the allocator. */
400 alloc_size = round_alloc_size (alloc_size);
402 /* Now alloc_size contains the exact memory size we would get if
403 we have asked for the initial alloc_size amount of memory.
404 Let's get back to the number of macro map that amounts
405 to. */
406 LINEMAPS_ALLOCATED (set, macro_map_p) =
407 alloc_size / map_size;
409 /* And now let's really do the re-allocation. */
410 if (macro_map_p)
412 set->info_macro.maps
413 = (line_map_macro *) (*reallocator) (set->info_macro.maps,
414 (LINEMAPS_ALLOCATED (set, macro_map_p)
415 * map_size));
416 result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
418 else
420 set->info_ordinary.maps =
421 (line_map_ordinary *) (*reallocator) (set->info_ordinary.maps,
422 (LINEMAPS_ALLOCATED (set, macro_map_p)
423 * map_size));
424 result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
426 memset (result, 0,
427 ((LINEMAPS_ALLOCATED (set, macro_map_p)
428 - LINEMAPS_USED (set, macro_map_p))
429 * map_size));
431 else
433 if (macro_map_p)
434 result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
435 else
436 result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
439 LINEMAPS_USED (set, macro_map_p)++;
441 result->reason = reason;
442 return result;
445 /* Add a mapping of logical source line to physical source file and
446 line number.
448 The text pointed to by TO_FILE must have a lifetime
449 at least as long as the final call to lookup_line (). An empty
450 TO_FILE means standard input. If reason is LC_LEAVE, and
451 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
452 natural values considering the file we are returning to.
454 FROM_LINE should be monotonic increasing across calls to this
455 function. A call to this function can relocate the previous set of
456 maps, so any stored line_map pointers should not be used. */
458 const struct line_map *
459 linemap_add (struct line_maps *set, enum lc_reason reason,
460 unsigned int sysp, const char *to_file, linenum_type to_line)
462 /* Generate a start_location above the current highest_location.
463 If possible, make the low range bits be zero. */
464 source_location start_location;
465 if (set->highest_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
467 start_location = set->highest_location + (1 << set->default_range_bits);
468 if (set->default_range_bits)
469 start_location &= ~((1 << set->default_range_bits) - 1);
470 linemap_assert (0 == (start_location
471 & ((1 << set->default_range_bits) - 1)));
473 else
474 start_location = set->highest_location + 1;
476 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
477 && (start_location
478 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
480 /* When we enter the file for the first time reason cannot be
481 LC_RENAME. */
482 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
484 /* If we are leaving the main file, return a NULL map. */
485 if (reason == LC_LEAVE
486 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
487 && to_file == NULL)
489 set->depth--;
490 return NULL;
493 linemap_assert (reason != LC_ENTER_MACRO);
494 line_map_ordinary *map = linemap_check_ordinary (new_linemap (set, reason));
496 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
497 to_file = "<stdin>";
499 if (reason == LC_RENAME_VERBATIM)
500 reason = LC_RENAME;
502 if (reason == LC_LEAVE)
504 /* When we are just leaving an "included" file, and jump to the next
505 location inside the "includer" right after the #include
506 "included", this variable points the map in use right before the
507 #include "included", inside the same "includer" file. */
508 line_map_ordinary *from;
509 bool error;
511 if (MAIN_FILE_P (map - 1))
513 /* So this _should_ mean we are leaving the main file --
514 effectively ending the compilation unit. But to_file not
515 being NULL means the caller thinks we are leaving to
516 another file. This is an erroneous behaviour but we'll
517 try to recover from it. Let's pretend we are not leaving
518 the main file. */
519 error = true;
520 reason = LC_RENAME;
521 from = map - 1;
523 else
525 /* (MAP - 1) points to the map we are leaving. The
526 map from which (MAP - 1) got included should be the map
527 that comes right before MAP in the same file. */
528 from = INCLUDED_FROM (set, map - 1);
529 error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
530 to_file);
533 /* Depending upon whether we are handling preprocessed input or
534 not, this can be a user error or an ICE. */
535 if (error)
536 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
537 to_file);
539 /* A TO_FILE of NULL is special - we use the natural values. */
540 if (error || to_file == NULL)
542 to_file = ORDINARY_MAP_FILE_NAME (from);
543 to_line = SOURCE_LINE (from, from[1].start_location);
544 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
548 map->sysp = sysp;
549 map->start_location = start_location;
550 map->to_file = to_file;
551 map->to_line = to_line;
552 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
553 map->m_column_and_range_bits = 0;
554 map->m_range_bits = 0;
555 set->highest_location = start_location;
556 set->highest_line = start_location;
557 set->max_column_hint = 0;
559 /* This assertion is placed after set->highest_location has
560 been updated, since the latter affects
561 linemap_location_from_macro_expansion_p, which ultimately affects
562 pure_location_p. */
563 linemap_assert (pure_location_p (set, start_location));
565 if (reason == LC_ENTER)
567 map->included_from =
568 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
569 set->depth++;
570 if (set->trace_includes)
571 trace_include (set, map);
573 else if (reason == LC_RENAME)
574 map->included_from = ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
575 else if (reason == LC_LEAVE)
577 set->depth--;
578 map->included_from =
579 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
582 return map;
585 /* Returns TRUE if the line table set tracks token locations across
586 macro expansion, FALSE otherwise. */
588 bool
589 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
591 return LINEMAPS_MACRO_MAPS (set) != NULL;
594 /* Create a macro map. A macro map encodes source locations of tokens
595 that are part of a macro replacement-list, at a macro expansion
596 point. See the extensive comments of struct line_map and struct
597 line_map_macro, in line-map.h.
599 This map shall be created when the macro is expanded. The map
600 encodes the source location of the expansion point of the macro as
601 well as the "original" source location of each token that is part
602 of the macro replacement-list. If a macro is defined but never
603 expanded, it has no macro map. SET is the set of maps the macro
604 map should be part of. MACRO_NODE is the macro which the new macro
605 map should encode source locations for. EXPANSION is the location
606 of the expansion point of MACRO. For function-like macros
607 invocations, it's best to make it point to the closing parenthesis
608 of the macro, rather than the the location of the first character
609 of the macro. NUM_TOKENS is the number of tokens that are part of
610 the replacement-list of MACRO.
612 Note that when we run out of the integer space available for source
613 locations, this function returns NULL. In that case, callers of
614 this function cannot encode {line,column} pairs into locations of
615 macro tokens anymore. */
617 const line_map_macro *
618 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
619 source_location expansion, unsigned int num_tokens)
621 line_map_macro *map;
622 source_location start_location;
623 /* Cast away extern "C" from the type of xrealloc. */
624 line_map_realloc reallocator = (set->reallocator
625 ? set->reallocator
626 : (line_map_realloc) xrealloc);
628 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
630 if (start_location <= set->highest_line
631 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
632 /* We ran out of macro map space. */
633 return NULL;
635 map = linemap_check_macro (new_linemap (set, LC_ENTER_MACRO));
637 map->start_location = start_location;
638 map->macro = macro_node;
639 map->n_tokens = num_tokens;
640 map->macro_locations
641 = (source_location*) reallocator (NULL,
642 2 * num_tokens
643 * sizeof (source_location));
644 map->expansion = expansion;
645 memset (MACRO_MAP_LOCATIONS (map), 0,
646 num_tokens * sizeof (source_location));
648 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
650 return map;
653 /* Create and return a virtual location for a token that is part of a
654 macro expansion-list at a macro expansion point. See the comment
655 inside struct line_map_macro to see what an expansion-list exactly
658 A call to this function must come after a call to
659 linemap_enter_macro.
661 MAP is the map into which the source location is created. TOKEN_NO
662 is the index of the token in the macro replacement-list, starting
663 at number 0.
665 ORIG_LOC is the location of the token outside of this macro
666 expansion. If the token comes originally from the macro
667 definition, it is the locus in the macro definition; otherwise it
668 is a location in the context of the caller of this macro expansion
669 (which is a virtual location or a source location if the caller is
670 itself a macro expansion or not).
672 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
673 either of the token itself or of a macro parameter that it
674 replaces. */
676 source_location
677 linemap_add_macro_token (const line_map_macro *map,
678 unsigned int token_no,
679 source_location orig_loc,
680 source_location orig_parm_replacement_loc)
682 source_location result;
684 linemap_assert (linemap_macro_expansion_map_p (map));
685 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
687 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
688 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
690 result = MAP_START_LOCATION (map) + token_no;
691 return result;
694 /* Return a source_location for the start (i.e. column==0) of
695 (physical) line TO_LINE in the current source file (as in the
696 most recent linemap_add). MAX_COLUMN_HINT is the highest column
697 number we expect to use in this line (but it does not change
698 the highest_location). */
700 source_location
701 linemap_line_start (struct line_maps *set, linenum_type to_line,
702 unsigned int max_column_hint)
704 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
705 source_location highest = set->highest_location;
706 source_location r;
707 linenum_type last_line =
708 SOURCE_LINE (map, set->highest_line);
709 int line_delta = to_line - last_line;
710 bool add_map = false;
711 linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
712 int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
714 if (line_delta < 0
715 || (line_delta > 10
716 && line_delta * map->m_column_and_range_bits > 1000)
717 || (max_column_hint >= (1U << effective_column_bits))
718 || (max_column_hint <= 80 && effective_column_bits >= 10)
719 || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
720 && (set->max_column_hint || highest >= LINE_MAP_MAX_SOURCE_LOCATION)))
721 add_map = true;
722 else
723 max_column_hint = set->max_column_hint;
724 if (add_map)
726 int column_bits;
727 int range_bits;
728 if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
729 || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
731 /* If the column number is ridiculous or we've allocated a huge
732 number of source_locations, give up on column numbers
733 (and on packed ranges). */
734 max_column_hint = 0;
735 column_bits = 0;
736 range_bits = 0;
737 if (highest > LINE_MAP_MAX_SOURCE_LOCATION)
738 return 0;
740 else
742 column_bits = 7;
743 range_bits = set->default_range_bits;
744 while (max_column_hint >= (1U << column_bits))
745 column_bits++;
746 max_column_hint = 1U << column_bits;
747 column_bits += range_bits;
749 /* Allocate the new line_map. However, if the current map only has a
750 single line we can sometimes just increase its column_bits instead. */
751 if (line_delta < 0
752 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
753 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
754 map = linemap_check_ordinary
755 (const_cast <line_map *>
756 (linemap_add (set, LC_RENAME,
757 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
758 ORDINARY_MAP_FILE_NAME (map),
759 to_line)));
760 map->m_column_and_range_bits = column_bits;
761 map->m_range_bits = range_bits;
762 r = (MAP_START_LOCATION (map)
763 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
764 << column_bits));
766 else
767 r = set->highest_line + (line_delta << map->m_column_and_range_bits);
769 /* Locations of ordinary tokens are always lower than locations of
770 macro tokens. */
771 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
772 return 0;
774 set->highest_line = r;
775 if (r > set->highest_location)
776 set->highest_location = r;
777 set->max_column_hint = max_column_hint;
779 /* At this point, we expect one of:
780 (a) the normal case: a "pure" location with 0 range bits, or
781 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
782 columns anymore (or ranges), or
783 (c) we're in a region with a column hint exceeding
784 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
785 with column_bits == 0. */
786 linemap_assert (pure_location_p (set, r)
787 || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
788 || map->m_column_and_range_bits == 0);
789 linemap_assert (SOURCE_LINE (map, r) == to_line);
790 return r;
793 /* Encode and return a source_location from a column number. The
794 source line considered is the last source line used to call
795 linemap_line_start, i.e, the last source line which a location was
796 encoded from. */
798 source_location
799 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
801 source_location r = set->highest_line;
803 linemap_assert
804 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
806 if (to_column >= set->max_column_hint)
808 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
809 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
811 /* Running low on source_locations - disable column numbers. */
812 return r;
814 else
816 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
817 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
820 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
821 r = r + (to_column << map->m_range_bits);
822 if (r >= set->highest_location)
823 set->highest_location = r;
824 return r;
827 /* Encode and return a source location from a given line and
828 column. */
830 source_location
831 linemap_position_for_line_and_column (line_maps *set,
832 const line_map_ordinary *ord_map,
833 linenum_type line,
834 unsigned column)
836 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
838 source_location r = MAP_START_LOCATION (ord_map);
839 r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
840 << ord_map->m_column_and_range_bits);
841 if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
842 r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
843 << ord_map->m_range_bits);
844 source_location upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
845 if (r >= upper_limit)
846 r = upper_limit - 1;
847 if (r > set->highest_location)
848 set->highest_location = r;
849 return r;
852 /* Encode and return a source_location starting from location LOC and
853 shifting it by OFFSET columns. This function does not support
854 virtual locations. */
856 source_location
857 linemap_position_for_loc_and_offset (struct line_maps *set,
858 source_location loc,
859 unsigned int offset)
861 const line_map_ordinary * map = NULL;
863 if (IS_ADHOC_LOC (loc))
864 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
866 /* This function does not support virtual locations yet. */
867 if (linemap_assert_fails
868 (!linemap_location_from_macro_expansion_p (set, loc)))
869 return loc;
871 if (offset == 0
872 /* Adding an offset to a reserved location (like
873 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
874 sense. So let's leave the location intact in that case. */
875 || loc < RESERVED_LOCATION_COUNT)
876 return loc;
878 /* We find the real location and shift it. */
879 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
880 /* The new location (loc + offset) should be higher than the first
881 location encoded by MAP. This can fail if the line information
882 is messed up because of line directives (see PR66415). */
883 if (MAP_START_LOCATION (map) >= loc + offset)
884 return loc;
886 linenum_type line = SOURCE_LINE (map, loc);
887 unsigned int column = SOURCE_COLUMN (map, loc);
889 /* If MAP is not the last line map of its set, then the new location
890 (loc + offset) should be less than the first location encoded by
891 the next line map of the set. Otherwise, we try to encode the
892 location in the next map. */
893 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
894 && loc + offset >= MAP_START_LOCATION (&map[1]))
896 map = &map[1];
897 /* If the next map starts in a higher line, we cannot encode the
898 location there. */
899 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
900 return loc;
903 offset += column;
904 if (linemap_assert_fails (offset < (1u << map->m_column_and_range_bits)))
905 return loc;
907 source_location r =
908 linemap_position_for_line_and_column (set, map, line, offset);
909 if (linemap_assert_fails (r <= set->highest_location)
910 || linemap_assert_fails (map == linemap_lookup (set, r)))
911 return loc;
913 return r;
916 /* Given a virtual source location yielded by a map (either an
917 ordinary or a macro map), returns that map. */
919 const struct line_map*
920 linemap_lookup (struct line_maps *set, source_location line)
922 if (IS_ADHOC_LOC (line))
923 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
924 if (linemap_location_from_macro_expansion_p (set, line))
925 return linemap_macro_map_lookup (set, line);
926 return linemap_ordinary_map_lookup (set, line);
929 /* Given a source location yielded by an ordinary map, returns that
930 map. Since the set is built chronologically, the logical lines are
931 monotonic increasing, and so the list is sorted and we can use a
932 binary search. */
934 static const line_map_ordinary *
935 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
937 unsigned int md, mn, mx;
938 const line_map_ordinary *cached, *result;
940 if (IS_ADHOC_LOC (line))
941 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
943 if (set == NULL || line < RESERVED_LOCATION_COUNT)
944 return NULL;
946 mn = LINEMAPS_ORDINARY_CACHE (set);
947 mx = LINEMAPS_ORDINARY_USED (set);
949 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
950 /* We should get a segfault if no line_maps have been added yet. */
951 if (line >= MAP_START_LOCATION (cached))
953 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
954 return cached;
956 else
958 mx = mn;
959 mn = 0;
962 while (mx - mn > 1)
964 md = (mn + mx) / 2;
965 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
966 mx = md;
967 else
968 mn = md;
971 LINEMAPS_ORDINARY_CACHE (set) = mn;
972 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
973 linemap_assert (line >= MAP_START_LOCATION (result));
974 return result;
977 /* Given a source location yielded by a macro map, returns that map.
978 Since the set is built chronologically, the logical lines are
979 monotonic decreasing, and so the list is sorted and we can use a
980 binary search. */
982 static const line_map_macro *
983 linemap_macro_map_lookup (struct line_maps *set, source_location line)
985 unsigned int md, mn, mx;
986 const struct line_map_macro *cached, *result;
988 if (IS_ADHOC_LOC (line))
989 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
991 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
993 if (set == NULL)
994 return NULL;
996 mn = LINEMAPS_MACRO_CACHE (set);
997 mx = LINEMAPS_MACRO_USED (set);
998 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
1000 if (line >= MAP_START_LOCATION (cached))
1002 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
1003 return cached;
1004 mx = mn - 1;
1005 mn = 0;
1008 while (mn < mx)
1010 md = (mx + mn) / 2;
1011 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1012 mn = md + 1;
1013 else
1014 mx = md;
1017 LINEMAPS_MACRO_CACHE (set) = mx;
1018 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
1019 linemap_assert (MAP_START_LOCATION (result) <= line);
1021 return result;
1024 /* Return TRUE if MAP encodes locations coming from a macro
1025 replacement-list at macro expansion point. */
1027 bool
1028 linemap_macro_expansion_map_p (const struct line_map *map)
1030 if (!map)
1031 return false;
1032 return (map->reason == LC_ENTER_MACRO);
1035 /* If LOCATION is the locus of a token in a replacement-list of a
1036 macro expansion return the location of the macro expansion point.
1038 Read the comments of struct line_map and struct line_map_macro in
1039 line-map.h to understand what a macro expansion point is. */
1041 static source_location
1042 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1043 source_location location ATTRIBUTE_UNUSED)
1045 linemap_assert (linemap_macro_expansion_map_p (map)
1046 && location >= MAP_START_LOCATION (map));
1048 /* Make sure LOCATION is correct. */
1049 linemap_assert ((location - MAP_START_LOCATION (map))
1050 < MACRO_MAP_NUM_MACRO_TOKENS (map));
1052 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1055 /* LOCATION is the source location of a token that belongs to a macro
1056 replacement-list as part of the macro expansion denoted by MAP.
1058 Return the location of the token at the definition point of the
1059 macro. */
1061 static source_location
1062 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1063 source_location location)
1065 unsigned token_no;
1067 linemap_assert (linemap_macro_expansion_map_p (map)
1068 && location >= MAP_START_LOCATION (map));
1069 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1071 token_no = location - MAP_START_LOCATION (map);
1072 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1074 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1076 return location;
1079 /* If LOCATION is the locus of a token that is an argument of a
1080 function-like macro M and appears in the expansion of M, return the
1081 locus of that argument in the context of the caller of M.
1083 In other words, this returns the xI location presented in the
1084 comments of line_map_macro above. */
1085 source_location
1086 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1087 const line_map_macro* map,
1088 source_location location)
1090 unsigned token_no;
1092 if (IS_ADHOC_LOC (location))
1093 location = get_location_from_adhoc_loc (set, location);
1095 linemap_assert (linemap_macro_expansion_map_p (map)
1096 && location >= MAP_START_LOCATION (map));
1097 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1098 linemap_assert (!IS_ADHOC_LOC (location));
1100 token_no = location - MAP_START_LOCATION (map);
1101 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1103 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1105 return location;
1108 /* Return the source line number corresponding to source location
1109 LOCATION. SET is the line map set LOCATION comes from. If
1110 LOCATION is the source location of token that is part of the
1111 replacement-list of a macro expansion return the line number of the
1112 macro expansion point. */
1115 linemap_get_expansion_line (struct line_maps *set,
1116 source_location location)
1118 const line_map_ordinary *map = NULL;
1120 if (IS_ADHOC_LOC (location))
1121 location = set->location_adhoc_data_map.data[location
1122 & MAX_SOURCE_LOCATION].locus;
1124 if (location < RESERVED_LOCATION_COUNT)
1125 return 0;
1127 location =
1128 linemap_macro_loc_to_exp_point (set, location, &map);
1130 return SOURCE_LINE (map, location);
1133 /* Return the path of the file corresponding to source code location
1134 LOCATION.
1136 If LOCATION is the source location of token that is part of the
1137 replacement-list of a macro expansion return the file path of the
1138 macro expansion point.
1140 SET is the line map set LOCATION comes from. */
1142 const char*
1143 linemap_get_expansion_filename (struct line_maps *set,
1144 source_location location)
1146 const struct line_map_ordinary *map = NULL;
1148 if (IS_ADHOC_LOC (location))
1149 location = set->location_adhoc_data_map.data[location
1150 & MAX_SOURCE_LOCATION].locus;
1152 if (location < RESERVED_LOCATION_COUNT)
1153 return NULL;
1155 location =
1156 linemap_macro_loc_to_exp_point (set, location, &map);
1158 return LINEMAP_FILE (map);
1161 /* Return the name of the macro associated to MACRO_MAP. */
1163 const char*
1164 linemap_map_get_macro_name (const line_map_macro *macro_map)
1166 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1167 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1170 /* Return a positive value if LOCATION is the locus of a token that is
1171 located in a system header, O otherwise. It returns 1 if LOCATION
1172 is the locus of a token that is located in a system header, and 2
1173 if LOCATION is the locus of a token located in a C system header
1174 that therefore needs to be extern "C" protected in C++.
1176 Note that this function returns 1 if LOCATION belongs to a token
1177 that is part of a macro replacement-list defined in a system
1178 header, but expanded in a non-system file. */
1181 linemap_location_in_system_header_p (struct line_maps *set,
1182 source_location location)
1184 const struct line_map *map = NULL;
1186 if (IS_ADHOC_LOC (location))
1187 location = set->location_adhoc_data_map.data[location
1188 & MAX_SOURCE_LOCATION].locus;
1190 if (location < RESERVED_LOCATION_COUNT)
1191 return false;
1193 /* Let's look at where the token for LOCATION comes from. */
1194 while (true)
1196 map = linemap_lookup (set, location);
1197 if (map != NULL)
1199 if (!linemap_macro_expansion_map_p (map))
1200 /* It's a normal token. */
1201 return LINEMAP_SYSP (linemap_check_ordinary (map));
1202 else
1204 const line_map_macro *macro_map = linemap_check_macro (map);
1206 /* It's a token resulting from a macro expansion. */
1207 source_location loc =
1208 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
1209 if (loc < RESERVED_LOCATION_COUNT)
1210 /* This token might come from a built-in macro. Let's
1211 look at where that macro got expanded. */
1212 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1213 else
1214 location = loc;
1217 else
1218 break;
1220 return false;
1223 /* Return TRUE if LOCATION is a source code location of a token coming
1224 from a macro replacement-list at a macro expansion point, FALSE
1225 otherwise. */
1227 bool
1228 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1229 source_location location)
1231 if (IS_ADHOC_LOC (location))
1232 location = set->location_adhoc_data_map.data[location
1233 & MAX_SOURCE_LOCATION].locus;
1235 linemap_assert (location <= MAX_SOURCE_LOCATION
1236 && (set->highest_location
1237 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
1238 if (set == NULL)
1239 return false;
1240 return (location > set->highest_location);
1243 /* Given two virtual locations *LOC0 and *LOC1, return the first
1244 common macro map in their macro expansion histories. Return NULL
1245 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1246 virtual location of the token inside the resulting macro. */
1248 static const struct line_map*
1249 first_map_in_common_1 (struct line_maps *set,
1250 source_location *loc0,
1251 source_location *loc1)
1253 source_location l0 = *loc0, l1 = *loc1;
1254 const struct line_map *map0 = linemap_lookup (set, l0),
1255 *map1 = linemap_lookup (set, l1);
1257 while (linemap_macro_expansion_map_p (map0)
1258 && linemap_macro_expansion_map_p (map1)
1259 && (map0 != map1))
1261 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1263 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1264 l0);
1265 map0 = linemap_lookup (set, l0);
1267 else
1269 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1270 l1);
1271 map1 = linemap_lookup (set, l1);
1275 if (map0 == map1)
1277 *loc0 = l0;
1278 *loc1 = l1;
1279 return map0;
1281 return NULL;
1284 /* Given two virtual locations LOC0 and LOC1, return the first common
1285 macro map in their macro expansion histories. Return NULL if no
1286 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1287 virtual location of the token inside the resulting macro, upon
1288 return of a non-NULL result. */
1290 static const struct line_map*
1291 first_map_in_common (struct line_maps *set,
1292 source_location loc0,
1293 source_location loc1,
1294 source_location *res_loc0,
1295 source_location *res_loc1)
1297 *res_loc0 = loc0;
1298 *res_loc1 = loc1;
1300 return first_map_in_common_1 (set, res_loc0, res_loc1);
1303 /* Return a positive value if PRE denotes the location of a token that
1304 comes before the token of POST, 0 if PRE denotes the location of
1305 the same token as the token for POST, and a negative value
1306 otherwise. */
1309 linemap_compare_locations (struct line_maps *set,
1310 source_location pre,
1311 source_location post)
1313 bool pre_virtual_p, post_virtual_p;
1314 source_location l0 = pre, l1 = post;
1316 if (IS_ADHOC_LOC (l0))
1317 l0 = set->location_adhoc_data_map.data[l0 & MAX_SOURCE_LOCATION].locus;
1318 if (IS_ADHOC_LOC (l1))
1319 l1 = set->location_adhoc_data_map.data[l1 & MAX_SOURCE_LOCATION].locus;
1321 if (l0 == l1)
1322 return 0;
1324 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1325 l0 = linemap_resolve_location (set, l0,
1326 LRK_MACRO_EXPANSION_POINT,
1327 NULL);
1329 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1330 l1 = linemap_resolve_location (set, l1,
1331 LRK_MACRO_EXPANSION_POINT,
1332 NULL);
1334 if (l0 == l1
1335 && pre_virtual_p
1336 && post_virtual_p)
1338 /* So pre and post represent two tokens that are present in a
1339 same macro expansion. Let's see if the token for pre was
1340 before the token for post in that expansion. */
1341 unsigned i0, i1;
1342 const struct line_map *map =
1343 first_map_in_common (set, pre, post, &l0, &l1);
1345 if (map == NULL)
1346 /* This should not be possible. */
1347 abort ();
1349 i0 = l0 - MAP_START_LOCATION (map);
1350 i1 = l1 - MAP_START_LOCATION (map);
1351 return i1 - i0;
1354 return l1 - l0;
1357 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1359 static void
1360 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1362 unsigned int i = set->depth;
1364 while (--i)
1365 putc ('.', stderr);
1367 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1370 /* Return the spelling location of the token wherever it comes from,
1371 whether part of a macro definition or not.
1373 This is a subroutine for linemap_resolve_location. */
1375 static source_location
1376 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1377 source_location location,
1378 const line_map_ordinary **original_map)
1380 struct line_map *map;
1381 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1383 while (true)
1385 map = const_cast <line_map *> (linemap_lookup (set, location));
1386 if (!linemap_macro_expansion_map_p (map))
1387 break;
1389 location
1390 = linemap_macro_map_loc_unwind_toward_spelling
1391 (set, linemap_check_macro (map),
1392 location);
1395 if (original_map)
1396 *original_map = linemap_check_ordinary (map);
1397 return location;
1400 /* If LOCATION is the source location of a token that belongs to a
1401 macro replacement-list -- as part of a macro expansion -- then
1402 return the location of the token at the definition point of the
1403 macro. Otherwise, return LOCATION. SET is the set of maps
1404 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1405 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1406 returned location comes from.
1408 This is a subroutine of linemap_resolve_location. */
1410 static source_location
1411 linemap_macro_loc_to_def_point (struct line_maps *set,
1412 source_location location,
1413 const line_map_ordinary **original_map)
1415 struct line_map *map;
1417 if (IS_ADHOC_LOC (location))
1418 location = set->location_adhoc_data_map.data[location
1419 & MAX_SOURCE_LOCATION].locus;
1421 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1423 while (true)
1425 map = const_cast <line_map *> (linemap_lookup (set, location));
1426 if (!linemap_macro_expansion_map_p (map))
1427 break;
1429 location =
1430 linemap_macro_map_loc_to_def_point (linemap_check_macro (map),
1431 location);
1434 if (original_map)
1435 *original_map = linemap_check_ordinary (map);
1436 return location;
1439 /* If LOCATION is the source location of a token that belongs to a
1440 macro replacement-list -- at a macro expansion point -- then return
1441 the location of the topmost expansion point of the macro. We say
1442 topmost because if we are in the context of a nested macro
1443 expansion, the function returns the source location of the first
1444 macro expansion that triggered the nested expansions.
1446 Otherwise, return LOCATION. SET is the set of maps location come
1447 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1448 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1449 location comes from.
1451 This is a subroutine of linemap_resolve_location. */
1453 static source_location
1454 linemap_macro_loc_to_exp_point (struct line_maps *set,
1455 source_location location,
1456 const line_map_ordinary **original_map)
1458 struct line_map *map;
1460 if (IS_ADHOC_LOC (location))
1461 location = set->location_adhoc_data_map.data[location
1462 & MAX_SOURCE_LOCATION].locus;
1464 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1466 while (true)
1468 map = const_cast <line_map *> (linemap_lookup (set, location));
1469 if (!linemap_macro_expansion_map_p (map))
1470 break;
1471 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1472 location);
1475 if (original_map)
1476 *original_map = linemap_check_ordinary (map);
1477 return location;
1480 /* Resolve a virtual location into either a spelling location, an
1481 expansion point location or a token argument replacement point
1482 location. Return the map that encodes the virtual location as well
1483 as the resolved location.
1485 If LOC is *NOT* the location of a token resulting from the
1486 expansion of a macro, then the parameter LRK (which stands for
1487 Location Resolution Kind) is ignored and the resulting location
1488 just equals the one given in argument.
1490 Now if LOC *IS* the location of a token resulting from the
1491 expansion of a macro, this is what happens.
1493 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1494 -------------------------------
1496 The virtual location is resolved to the first macro expansion point
1497 that led to this macro expansion.
1499 * If LRK is set to LRK_SPELLING_LOCATION
1500 -------------------------------------
1502 The virtual location is resolved to the locus where the token has
1503 been spelled in the source. This can follow through all the macro
1504 expansions that led to the token.
1506 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1507 --------------------------------------
1509 The virtual location is resolved to the locus of the token in the
1510 context of the macro definition.
1512 If LOC is the locus of a token that is an argument of a
1513 function-like macro [replacing a parameter in the replacement list
1514 of the macro] the virtual location is resolved to the locus of the
1515 parameter that is replaced, in the context of the definition of the
1516 macro.
1518 If LOC is the locus of a token that is not an argument of a
1519 function-like macro, then the function behaves as if LRK was set to
1520 LRK_SPELLING_LOCATION.
1522 If MAP is not NULL, *MAP is set to the map encoding the
1523 returned location. Note that if the returned location wasn't originally
1524 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1525 resolves to a location reserved for the client code, like
1526 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1528 source_location
1529 linemap_resolve_location (struct line_maps *set,
1530 source_location loc,
1531 enum location_resolution_kind lrk,
1532 const line_map_ordinary **map)
1534 source_location locus = loc;
1535 if (IS_ADHOC_LOC (loc))
1536 locus = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1538 if (locus < RESERVED_LOCATION_COUNT)
1540 /* A reserved location wasn't encoded in a map. Let's return a
1541 NULL map here, just like what linemap_ordinary_map_lookup
1542 does. */
1543 if (map)
1544 *map = NULL;
1545 return loc;
1548 switch (lrk)
1550 case LRK_MACRO_EXPANSION_POINT:
1551 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1552 break;
1553 case LRK_SPELLING_LOCATION:
1554 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1555 break;
1556 case LRK_MACRO_DEFINITION_LOCATION:
1557 loc = linemap_macro_loc_to_def_point (set, loc, map);
1558 break;
1559 default:
1560 abort ();
1562 return loc;
1566 Suppose that LOC is the virtual location of a token T coming from
1567 the expansion of a macro M. This function then steps up to get the
1568 location L of the point where M got expanded. If L is a spelling
1569 location inside a macro expansion M', then this function returns
1570 the locus of the point where M' was expanded. Said otherwise, this
1571 function returns the location of T in the context that triggered
1572 the expansion of M.
1574 *LOC_MAP must be set to the map of LOC. This function then sets it
1575 to the map of the returned location. */
1577 source_location
1578 linemap_unwind_toward_expansion (struct line_maps *set,
1579 source_location loc,
1580 const struct line_map **map)
1582 source_location resolved_location;
1583 const line_map_macro *macro_map = linemap_check_macro (*map);
1584 const struct line_map *resolved_map;
1586 if (IS_ADHOC_LOC (loc))
1587 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1589 resolved_location =
1590 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1591 resolved_map = linemap_lookup (set, resolved_location);
1593 if (!linemap_macro_expansion_map_p (resolved_map))
1595 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1596 resolved_map = linemap_lookup (set, resolved_location);
1599 *map = resolved_map;
1600 return resolved_location;
1603 /* If LOC is the virtual location of a token coming from the expansion
1604 of a macro M and if its spelling location is reserved (e.g, a
1605 location for a built-in token), then this function unwinds (using
1606 linemap_unwind_toward_expansion) the location until a location that
1607 is not reserved and is not in a system header is reached. In other
1608 words, this unwinds the reserved location until a location that is
1609 in real source code is reached.
1611 Otherwise, if the spelling location for LOC is not reserved or if
1612 LOC doesn't come from the expansion of a macro, the function
1613 returns LOC as is and *MAP is not touched.
1615 *MAP is set to the map of the returned location if the later is
1616 different from LOC. */
1617 source_location
1618 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1619 source_location loc,
1620 const struct line_map **map)
1622 source_location resolved_loc;
1623 const struct line_map *map0 = NULL;
1624 const line_map_ordinary *map1 = NULL;
1626 if (IS_ADHOC_LOC (loc))
1627 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1629 map0 = linemap_lookup (set, loc);
1630 if (!linemap_macro_expansion_map_p (map0))
1631 return loc;
1633 resolved_loc = linemap_resolve_location (set, loc,
1634 LRK_SPELLING_LOCATION,
1635 &map1);
1637 if (resolved_loc >= RESERVED_LOCATION_COUNT
1638 && !LINEMAP_SYSP (map1))
1639 return loc;
1641 while (linemap_macro_expansion_map_p (map0)
1642 && (resolved_loc < RESERVED_LOCATION_COUNT
1643 || LINEMAP_SYSP (map1)))
1645 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1646 resolved_loc = linemap_resolve_location (set, loc,
1647 LRK_SPELLING_LOCATION,
1648 &map1);
1651 if (map != NULL)
1652 *map = map0;
1653 return loc;
1656 /* Expand source code location LOC and return a user readable source
1657 code location. LOC must be a spelling (non-virtual) location. If
1658 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1659 location is returned. */
1661 expanded_location
1662 linemap_expand_location (struct line_maps *set,
1663 const struct line_map *map,
1664 source_location loc)
1667 expanded_location xloc;
1669 memset (&xloc, 0, sizeof (xloc));
1670 if (IS_ADHOC_LOC (loc))
1672 xloc.data
1673 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1674 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1677 if (loc < RESERVED_LOCATION_COUNT)
1678 /* The location for this token wasn't generated from a line map.
1679 It was probably a location for a builtin token, chosen by some
1680 client code. Let's not try to expand the location in that
1681 case. */;
1682 else if (map == NULL)
1683 /* We shouldn't be getting a NULL map with a location that is not
1684 reserved by the client code. */
1685 abort ();
1686 else
1688 /* MAP must be an ordinary map and LOC must be non-virtual,
1689 encoded into this map, obviously; the accessors used on MAP
1690 below ensure it is ordinary. Let's just assert the
1691 non-virtualness of LOC here. */
1692 if (linemap_location_from_macro_expansion_p (set, loc))
1693 abort ();
1695 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1697 xloc.file = LINEMAP_FILE (ord_map);
1698 xloc.line = SOURCE_LINE (ord_map, loc);
1699 xloc.column = SOURCE_COLUMN (ord_map, loc);
1700 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1703 return xloc;
1707 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1708 is NULL, use stderr. IS_MACRO is true if the caller wants to
1709 dump a macro map, false otherwise. */
1711 void
1712 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1714 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1715 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1716 "LC_ENTER_MACRO" };
1717 const char *reason;
1718 const line_map *map;
1720 if (stream == NULL)
1721 stream = stderr;
1723 if (!is_macro)
1724 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1725 else
1726 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1728 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1730 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1731 ix, (void *) map, map->start_location, reason,
1732 ((!is_macro
1733 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1734 ? "yes" : "no"));
1735 if (!is_macro)
1737 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1738 unsigned includer_ix;
1739 const line_map_ordinary *includer_map;
1741 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
1742 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1743 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1744 : NULL;
1746 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1747 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1748 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1749 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1751 else
1753 const line_map_macro *macro_map = linemap_check_macro (map);
1754 fprintf (stream, "Macro: %s (%u tokens)\n",
1755 linemap_map_get_macro_name (macro_map),
1756 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1759 fprintf (stream, "\n");
1763 /* Dump debugging information about source location LOC into the file
1764 stream STREAM. SET is the line map set LOC comes from. */
1766 void
1767 linemap_dump_location (struct line_maps *set,
1768 source_location loc,
1769 FILE *stream)
1771 const line_map_ordinary *map;
1772 source_location location;
1773 const char *path = "", *from = "";
1774 int l = -1, c = -1, s = -1, e = -1;
1776 if (IS_ADHOC_LOC (loc))
1777 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1779 if (loc == 0)
1780 return;
1782 location =
1783 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1785 if (map == NULL)
1786 /* Only reserved locations can be tolerated in this case. */
1787 linemap_assert (location < RESERVED_LOCATION_COUNT);
1788 else
1790 path = LINEMAP_FILE (map);
1791 l = SOURCE_LINE (map, location);
1792 c = SOURCE_COLUMN (map, location);
1793 s = LINEMAP_SYSP (map) != 0;
1794 e = location != loc;
1795 if (e)
1796 from = "N/A";
1797 else
1798 from = (INCLUDED_FROM (set, map))
1799 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1800 : "<NULL>";
1803 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1804 E: macro expansion?, LOC: original location, R: resolved location */
1805 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1806 path, from, l, c, s, (void*)map, e, loc, location);
1809 /* Return the highest location emitted for a given file for which
1810 there is a line map in SET. FILE_NAME is the file name to
1811 consider. If the function returns TRUE, *LOC is set to the highest
1812 location emitted for that file. */
1814 bool
1815 linemap_get_file_highest_location (struct line_maps *set,
1816 const char *file_name,
1817 source_location *loc)
1819 /* If the set is empty or no ordinary map has been created then
1820 there is no file to look for ... */
1821 if (set == NULL || set->info_ordinary.used == 0)
1822 return false;
1824 /* Now look for the last ordinary map created for FILE_NAME. */
1825 int i;
1826 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1828 const char *fname = set->info_ordinary.maps[i].to_file;
1829 if (fname && !filename_cmp (fname, file_name))
1830 break;
1833 if (i < 0)
1834 return false;
1836 /* The highest location for a given map is either the starting
1837 location of the next map minus one, or -- if the map is the
1838 latest one -- the highest location of the set. */
1839 source_location result;
1840 if (i == (int) set->info_ordinary.used - 1)
1841 result = set->highest_location;
1842 else
1843 result = set->info_ordinary.maps[i + 1].start_location - 1;
1845 *loc = result;
1846 return true;
1849 /* Compute and return statistics about the memory consumption of some
1850 parts of the line table SET. */
1852 void
1853 linemap_get_statistics (struct line_maps *set,
1854 struct linemap_stats *s)
1856 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1857 macro_maps_allocated_size, macro_maps_used_size,
1858 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1860 const line_map_macro *cur_map;
1862 ordinary_maps_allocated_size =
1863 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1865 ordinary_maps_used_size =
1866 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1868 macro_maps_allocated_size =
1869 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1871 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1872 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1873 ++cur_map)
1875 unsigned i;
1877 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1879 macro_maps_locations_size +=
1880 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1882 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1884 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1885 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1886 duplicated_macro_maps_locations_size +=
1887 sizeof (source_location);
1891 macro_maps_used_size =
1892 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1894 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1895 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1896 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1897 s->ordinary_maps_used_size = ordinary_maps_used_size;
1898 s->num_expanded_macros = num_expanded_macros_counter;
1899 s->num_macro_tokens = num_macro_tokens_counter;
1900 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1901 s->macro_maps_allocated_size = macro_maps_allocated_size;
1902 s->macro_maps_locations_size = macro_maps_locations_size;
1903 s->macro_maps_used_size = macro_maps_used_size;
1904 s->duplicated_macro_maps_locations_size =
1905 duplicated_macro_maps_locations_size;
1906 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1907 * sizeof (struct location_adhoc_data));
1908 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
1912 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1913 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1914 specifies how many macro maps to dump. */
1916 void
1917 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1918 unsigned int num_macro)
1920 unsigned int i;
1922 if (set == NULL)
1923 return;
1925 if (stream == NULL)
1926 stream = stderr;
1928 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1929 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1930 fprintf (stream, "Include stack depth: %d\n", set->depth);
1931 fprintf (stream, "Highest location: %u\n", set->highest_location);
1933 if (num_ordinary)
1935 fprintf (stream, "\nOrdinary line maps\n");
1936 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1937 linemap_dump (stream, set, i, false);
1938 fprintf (stream, "\n");
1941 if (num_macro)
1943 fprintf (stream, "\nMacro line maps\n");
1944 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1945 linemap_dump (stream, set, i, true);
1946 fprintf (stream, "\n");
1950 /* struct source_range. */
1952 /* Is there any part of this range on the given line? */
1954 bool
1955 source_range::intersects_line_p (const char *file, int line) const
1957 expanded_location exploc_start
1958 = linemap_client_expand_location_to_spelling_point (m_start);
1959 if (file != exploc_start.file)
1960 return false;
1961 if (line < exploc_start.line)
1962 return false;
1963 expanded_location exploc_finish
1964 = linemap_client_expand_location_to_spelling_point (m_finish);
1965 if (file != exploc_finish.file)
1966 return false;
1967 if (line > exploc_finish.line)
1968 return false;
1969 return true;
1972 /* class rich_location. */
1974 /* Construct a rich_location with location LOC as its initial range. */
1976 rich_location::rich_location (line_maps *set, source_location loc) :
1977 m_loc (loc),
1978 m_num_ranges (0),
1979 m_have_expanded_location (false),
1980 m_num_fixit_hints (0)
1982 /* Set up the 0th range, extracting any range from LOC. */
1983 source_range src_range = get_range_from_loc (set, loc);
1984 add_range (src_range, true);
1985 m_ranges[0].m_caret = lazily_expand_location ();
1988 /* Construct a rich_location with source_range SRC_RANGE as its
1989 initial range. */
1991 rich_location::rich_location (source_range src_range)
1992 : m_loc (src_range.m_start),
1993 m_num_ranges (0),
1994 m_have_expanded_location (false),
1995 m_num_fixit_hints (0)
1997 /* Set up the 0th range: */
1998 add_range (src_range, true);
2001 /* The destructor for class rich_location. */
2003 rich_location::~rich_location ()
2005 for (unsigned int i = 0; i < m_num_fixit_hints; i++)
2006 delete m_fixit_hints[i];
2009 /* Get an expanded_location for this rich_location's primary
2010 location. */
2012 expanded_location
2013 rich_location::lazily_expand_location ()
2015 if (!m_have_expanded_location)
2017 m_expanded_location
2018 = linemap_client_expand_location_to_spelling_point (m_loc);
2019 m_have_expanded_location = true;
2022 return m_expanded_location;
2025 /* Set the column of the primary location. */
2027 void
2028 rich_location::override_column (int column)
2030 lazily_expand_location ();
2031 m_expanded_location.column = column;
2034 /* Add the given range. */
2036 void
2037 rich_location::add_range (source_location start, source_location finish,
2038 bool show_caret_p)
2040 linemap_assert (m_num_ranges < MAX_RANGES);
2042 location_range *range = &m_ranges[m_num_ranges++];
2043 range->m_start = linemap_client_expand_location_to_spelling_point (start);
2044 range->m_finish = linemap_client_expand_location_to_spelling_point (finish);
2045 range->m_caret = range->m_start;
2046 range->m_show_caret_p = show_caret_p;
2049 /* Add the given range. */
2051 void
2052 rich_location::add_range (source_range src_range, bool show_caret_p)
2054 linemap_assert (m_num_ranges < MAX_RANGES);
2056 add_range (src_range.m_start, src_range.m_finish, show_caret_p);
2059 void
2060 rich_location::add_range (location_range *src_range)
2062 linemap_assert (m_num_ranges < MAX_RANGES);
2064 m_ranges[m_num_ranges++] = *src_range;
2067 /* Add or overwrite the range given by IDX. It must either
2068 overwrite an existing range, or add one *exactly* on the end of
2069 the array.
2071 This is primarily for use by gcc when implementing diagnostic
2072 format decoders e.g. the "+" in the C/C++ frontends, for handling
2073 format codes like "%q+D" (which writes the source location of a
2074 tree back into range 0 of the rich_location).
2076 If SHOW_CARET_P is true, then the range should be rendered with
2077 a caret at its starting location. This
2078 is for use by the Fortran frontend, for implementing the
2079 "%C" and "%L" format codes. */
2081 void
2082 rich_location::set_range (unsigned int idx, source_range src_range,
2083 bool show_caret_p, bool overwrite_loc_p)
2085 linemap_assert (idx < MAX_RANGES);
2087 /* We can either overwrite an existing range, or add one exactly
2088 on the end of the array. */
2089 linemap_assert (idx <= m_num_ranges);
2091 location_range *locrange = &m_ranges[idx];
2092 locrange->m_start
2093 = linemap_client_expand_location_to_spelling_point (src_range.m_start);
2094 locrange->m_finish
2095 = linemap_client_expand_location_to_spelling_point (src_range.m_finish);
2097 locrange->m_show_caret_p = show_caret_p;
2098 if (overwrite_loc_p)
2099 locrange->m_caret = locrange->m_start;
2101 /* Are we adding a range onto the end? */
2102 if (idx == m_num_ranges)
2103 m_num_ranges = idx + 1;
2105 if (idx == 0 && overwrite_loc_p)
2107 m_loc = src_range.m_start;
2108 /* Mark any cached value here as dirty. */
2109 m_have_expanded_location = false;
2113 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2114 at WHERE. */
2116 void
2117 rich_location::add_fixit_insert (source_location where,
2118 const char *new_content)
2120 linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2121 m_fixit_hints[m_num_fixit_hints++]
2122 = new fixit_insert (where, new_content);
2125 /* Add a fixit-hint, suggesting removal of the content at
2126 SRC_RANGE. */
2128 void
2129 rich_location::add_fixit_remove (source_range src_range)
2131 linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2132 m_fixit_hints[m_num_fixit_hints++] = new fixit_remove (src_range);
2135 /* Add a fixit-hint, suggesting replacement of the content at
2136 SRC_RANGE with NEW_CONTENT. */
2138 void
2139 rich_location::add_fixit_replace (source_range src_range,
2140 const char *new_content)
2142 linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2143 m_fixit_hints[m_num_fixit_hints++]
2144 = new fixit_replace (src_range, new_content);
2147 /* class fixit_insert. */
2149 fixit_insert::fixit_insert (source_location where,
2150 const char *new_content)
2151 : m_where (where),
2152 m_bytes (xstrdup (new_content)),
2153 m_len (strlen (new_content))
2157 fixit_insert::~fixit_insert ()
2159 free (m_bytes);
2162 /* Implementation of fixit_hint::affects_line_p for fixit_insert. */
2164 bool
2165 fixit_insert::affects_line_p (const char *file, int line)
2167 expanded_location exploc
2168 = linemap_client_expand_location_to_spelling_point (m_where);
2169 if (file == exploc.file)
2170 if (line == exploc.line)
2171 return true;
2172 return false;
2175 /* class fixit_remove. */
2177 fixit_remove::fixit_remove (source_range src_range)
2178 : m_src_range (src_range)
2182 /* Implementation of fixit_hint::affects_line_p for fixit_remove. */
2184 bool
2185 fixit_remove::affects_line_p (const char *file, int line)
2187 return m_src_range.intersects_line_p (file, line);
2190 /* class fixit_replace. */
2192 fixit_replace::fixit_replace (source_range src_range,
2193 const char *new_content)
2194 : m_src_range (src_range),
2195 m_bytes (xstrdup (new_content)),
2196 m_len (strlen (new_content))
2200 fixit_replace::~fixit_replace ()
2202 free (m_bytes);
2205 /* Implementation of fixit_hint::affects_line_p for fixit_replace. */
2207 bool
2208 fixit_replace::affects_line_p (const char *file, int line)
2210 return m_src_range.intersects_line_p (file, line);