* tree-vect-data-refs.c (compare_tree): Rename and move ...
[official-gcc.git] / libcpp / line-map.c
blobc4b7cb2365126d51bc5ad861eba41c6ee807555a
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2017 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 /* Highest possible source location encoded within an ordinary or
35 macro map. */
36 const source_location LINE_MAP_MAX_SOURCE_LOCATION = 0x70000000;
38 static void trace_include (const struct line_maps *, const line_map_ordinary *);
39 static const line_map_ordinary * linemap_ordinary_map_lookup (struct line_maps *,
40 source_location);
41 static const line_map_macro* linemap_macro_map_lookup (struct line_maps *,
42 source_location);
43 static source_location linemap_macro_map_loc_to_def_point
44 (const line_map_macro *, source_location);
45 static source_location linemap_macro_map_loc_to_exp_point
46 (const line_map_macro *, source_location);
47 static source_location linemap_macro_loc_to_spelling_point
48 (struct line_maps *, source_location, const line_map_ordinary **);
49 static source_location linemap_macro_loc_to_def_point (struct line_maps *,
50 source_location,
51 const line_map_ordinary **);
52 static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
53 source_location,
54 const line_map_ordinary **);
56 /* Counters defined in macro.c. */
57 extern unsigned num_expanded_macros_counter;
58 extern unsigned num_macro_tokens_counter;
60 /* Destructor for class line_maps.
61 Ensure non-GC-managed memory is released. */
63 line_maps::~line_maps ()
65 htab_delete (location_adhoc_data_map.htab);
68 /* Hash function for location_adhoc_data hashtable. */
70 static hashval_t
71 location_adhoc_data_hash (const void *l)
73 const struct location_adhoc_data *lb =
74 (const struct location_adhoc_data *) l;
75 return ((hashval_t) lb->locus
76 + (hashval_t) lb->src_range.m_start
77 + (hashval_t) lb->src_range.m_finish
78 + (size_t) lb->data);
81 /* Compare function for location_adhoc_data hashtable. */
83 static int
84 location_adhoc_data_eq (const void *l1, const void *l2)
86 const struct location_adhoc_data *lb1 =
87 (const struct location_adhoc_data *) l1;
88 const struct location_adhoc_data *lb2 =
89 (const struct location_adhoc_data *) l2;
90 return (lb1->locus == lb2->locus
91 && lb1->src_range.m_start == lb2->src_range.m_start
92 && lb1->src_range.m_finish == lb2->src_range.m_finish
93 && lb1->data == lb2->data);
96 /* Update the hashtable when location_adhoc_data is reallocated. */
98 static int
99 location_adhoc_data_update (void **slot, void *data)
101 *((char **) slot) += *((int64_t *) data);
102 return 1;
105 /* Rebuild the hash table from the location adhoc data. */
107 void
108 rebuild_location_adhoc_htab (struct line_maps *set)
110 unsigned i;
111 set->location_adhoc_data_map.htab =
112 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
113 for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
114 htab_find_slot (set->location_adhoc_data_map.htab,
115 set->location_adhoc_data_map.data + i, INSERT);
118 /* Helper function for get_combined_adhoc_loc.
119 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
120 within a source_location, without needing to use an ad-hoc location. */
122 static bool
123 can_be_stored_compactly_p (struct line_maps *set,
124 source_location locus,
125 source_range src_range,
126 void *data)
128 /* If there's an ad-hoc pointer, we can't store it directly in the
129 source_location, we need the lookaside. */
130 if (data)
131 return false;
133 /* We only store ranges that begin at the locus and that are sufficiently
134 "sane". */
135 if (src_range.m_start != locus)
136 return false;
138 if (src_range.m_finish < src_range.m_start)
139 return false;
141 if (src_range.m_start < RESERVED_LOCATION_COUNT)
142 return false;
144 if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
145 return false;
147 /* All 3 locations must be within ordinary maps, typically, the same
148 ordinary map. */
149 source_location lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
150 if (locus >= lowest_macro_loc)
151 return false;
152 if (src_range.m_start >= lowest_macro_loc)
153 return false;
154 if (src_range.m_finish >= lowest_macro_loc)
155 return false;
157 /* Passed all tests. */
158 return true;
161 /* Combine LOCUS and DATA to a combined adhoc loc. */
163 source_location
164 get_combined_adhoc_loc (struct line_maps *set,
165 source_location locus,
166 source_range src_range,
167 void *data)
169 struct location_adhoc_data lb;
170 struct location_adhoc_data **slot;
172 if (IS_ADHOC_LOC (locus))
173 locus
174 = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
175 if (locus == 0 && data == NULL)
176 return 0;
178 /* Any ordinary locations ought to be "pure" at this point: no
179 compressed ranges. */
180 linemap_assert (locus < RESERVED_LOCATION_COUNT
181 || locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
182 || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
183 || pure_location_p (set, locus));
185 /* Consider short-range optimization. */
186 if (can_be_stored_compactly_p (set, locus, src_range, data))
188 /* The low bits ought to be clear. */
189 linemap_assert (pure_location_p (set, locus));
190 const line_map *map = linemap_lookup (set, locus);
191 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
192 unsigned int int_diff = src_range.m_finish - src_range.m_start;
193 unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
194 if (col_diff < (1U << ordmap->m_range_bits))
196 source_location packed = locus | col_diff;
197 set->num_optimized_ranges++;
198 return packed;
202 /* We can also compactly store locations
203 when locus == start == finish (and data is NULL). */
204 if (locus == src_range.m_start
205 && locus == src_range.m_finish
206 && !data)
207 return locus;
209 if (!data)
210 set->num_unoptimized_ranges++;
212 lb.locus = locus;
213 lb.src_range = src_range;
214 lb.data = data;
215 slot = (struct location_adhoc_data **)
216 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
217 if (*slot == NULL)
219 if (set->location_adhoc_data_map.curr_loc >=
220 set->location_adhoc_data_map.allocated)
222 char *orig_data = (char *) set->location_adhoc_data_map.data;
223 int64_t offset;
224 /* Cast away extern "C" from the type of xrealloc. */
225 line_map_realloc reallocator = (set->reallocator
226 ? set->reallocator
227 : (line_map_realloc) xrealloc);
229 if (set->location_adhoc_data_map.allocated == 0)
230 set->location_adhoc_data_map.allocated = 128;
231 else
232 set->location_adhoc_data_map.allocated *= 2;
233 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
234 reallocator (set->location_adhoc_data_map.data,
235 set->location_adhoc_data_map.allocated
236 * sizeof (struct location_adhoc_data));
237 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
238 if (set->location_adhoc_data_map.allocated > 128)
239 htab_traverse (set->location_adhoc_data_map.htab,
240 location_adhoc_data_update, &offset);
242 *slot = set->location_adhoc_data_map.data
243 + set->location_adhoc_data_map.curr_loc;
244 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
245 = lb;
247 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
250 /* Return the data for the adhoc loc. */
252 void *
253 get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
255 linemap_assert (IS_ADHOC_LOC (loc));
256 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
259 /* Return the location for the adhoc loc. */
261 source_location
262 get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
264 linemap_assert (IS_ADHOC_LOC (loc));
265 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
268 /* Return the source_range for adhoc location LOC. */
270 static source_range
271 get_range_from_adhoc_loc (struct line_maps *set, source_location loc)
273 linemap_assert (IS_ADHOC_LOC (loc));
274 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].src_range;
277 /* Get the source_range of location LOC, either from the ad-hoc
278 lookaside table, or embedded inside LOC itself. */
280 source_range
281 get_range_from_loc (struct line_maps *set,
282 source_location loc)
284 if (IS_ADHOC_LOC (loc))
285 return get_range_from_adhoc_loc (set, loc);
287 /* For ordinary maps, extract packed range. */
288 if (loc >= RESERVED_LOCATION_COUNT
289 && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
290 && loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
292 const line_map *map = linemap_lookup (set, loc);
293 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
294 source_range result;
295 int offset = loc & ((1 << ordmap->m_range_bits) - 1);
296 result.m_start = loc - offset;
297 result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
298 return result;
301 return source_range::from_location (loc);
304 /* Get whether location LOC is a "pure" location, or
305 whether it is an ad-hoc location, or embeds range information. */
307 bool
308 pure_location_p (line_maps *set, source_location loc)
310 if (IS_ADHOC_LOC (loc))
311 return false;
313 const line_map *map = linemap_lookup (set, loc);
314 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
316 if (loc & ((1U << ordmap->m_range_bits) - 1))
317 return false;
319 return true;
322 /* Given location LOC within SET, strip away any packed range information
323 or ad-hoc information. */
325 source_location
326 get_pure_location (line_maps *set, source_location loc)
328 if (IS_ADHOC_LOC (loc))
330 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
332 if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
333 return loc;
335 if (loc < RESERVED_LOCATION_COUNT)
336 return loc;
338 const line_map *map = linemap_lookup (set, loc);
339 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
341 return loc & ~((1 << ordmap->m_range_bits) - 1);
344 /* Initialize a line map set. */
346 void
347 linemap_init (struct line_maps *set,
348 source_location builtin_location)
350 memset (set, 0, sizeof (struct line_maps));
351 set->highest_location = RESERVED_LOCATION_COUNT - 1;
352 set->highest_line = RESERVED_LOCATION_COUNT - 1;
353 set->location_adhoc_data_map.htab =
354 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
355 set->builtin_location = builtin_location;
358 /* Check for and warn about line_maps entered but not exited. */
360 void
361 linemap_check_files_exited (struct line_maps *set)
363 const line_map_ordinary *map;
364 /* Depending upon whether we are handling preprocessed input or
365 not, this can be a user error or an ICE. */
366 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
367 ! MAIN_FILE_P (map);
368 map = INCLUDED_FROM (set, map))
369 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
370 ORDINARY_MAP_FILE_NAME (map));
373 /* Create a new line map in the line map set SET, and return it.
374 REASON is the reason of creating the map. It determines the type
375 of map created (ordinary or macro map). Note that ordinary maps and
376 macro maps are allocated in different memory location. */
378 static struct line_map *
379 new_linemap (struct line_maps *set,
380 enum lc_reason reason)
382 /* Depending on this variable, a macro map would be allocated in a
383 different memory location than an ordinary map. */
384 bool macro_map_p = (reason == LC_ENTER_MACRO);
385 struct line_map *result;
387 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
389 /* We ran out of allocated line maps. Let's allocate more. */
390 size_t alloc_size;
392 /* Cast away extern "C" from the type of xrealloc. */
393 line_map_realloc reallocator = (set->reallocator
394 ? set->reallocator
395 : (line_map_realloc) xrealloc);
396 line_map_round_alloc_size_func round_alloc_size =
397 set->round_alloc_size;
399 size_t map_size = (macro_map_p
400 ? sizeof (line_map_macro)
401 : sizeof (line_map_ordinary));
403 /* We are going to execute some dance to try to reduce the
404 overhead of the memory allocator, in case we are using the
405 ggc-page.c one.
407 The actual size of memory we are going to get back from the
408 allocator is the smallest power of 2 that is greater than the
409 size we requested. So let's consider that size then. */
411 alloc_size =
412 (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
413 * map_size;
415 /* Get the actual size of memory that is going to be allocated
416 by the allocator. */
417 alloc_size = round_alloc_size (alloc_size);
419 /* Now alloc_size contains the exact memory size we would get if
420 we have asked for the initial alloc_size amount of memory.
421 Let's get back to the number of macro map that amounts
422 to. */
423 LINEMAPS_ALLOCATED (set, macro_map_p) =
424 alloc_size / map_size;
426 /* And now let's really do the re-allocation. */
427 if (macro_map_p)
429 set->info_macro.maps
430 = (line_map_macro *) (*reallocator) (set->info_macro.maps,
431 (LINEMAPS_ALLOCATED (set, macro_map_p)
432 * map_size));
433 result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
435 else
437 set->info_ordinary.maps =
438 (line_map_ordinary *) (*reallocator) (set->info_ordinary.maps,
439 (LINEMAPS_ALLOCATED (set, macro_map_p)
440 * map_size));
441 result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
443 memset (result, 0,
444 ((LINEMAPS_ALLOCATED (set, macro_map_p)
445 - LINEMAPS_USED (set, macro_map_p))
446 * map_size));
448 else
450 if (macro_map_p)
451 result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
452 else
453 result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
456 LINEMAPS_USED (set, macro_map_p)++;
458 result->reason = reason;
459 return result;
462 /* Add a mapping of logical source line to physical source file and
463 line number.
465 The text pointed to by TO_FILE must have a lifetime
466 at least as long as the final call to lookup_line (). An empty
467 TO_FILE means standard input. If reason is LC_LEAVE, and
468 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
469 natural values considering the file we are returning to.
471 FROM_LINE should be monotonic increasing across calls to this
472 function. A call to this function can relocate the previous set of
473 maps, so any stored line_map pointers should not be used. */
475 const struct line_map *
476 linemap_add (struct line_maps *set, enum lc_reason reason,
477 unsigned int sysp, const char *to_file, linenum_type to_line)
479 /* Generate a start_location above the current highest_location.
480 If possible, make the low range bits be zero. */
481 source_location start_location;
482 if (set->highest_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
484 start_location = set->highest_location + (1 << set->default_range_bits);
485 if (set->default_range_bits)
486 start_location &= ~((1 << set->default_range_bits) - 1);
487 linemap_assert (0 == (start_location
488 & ((1 << set->default_range_bits) - 1)));
490 else
491 start_location = set->highest_location + 1;
493 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
494 && (start_location
495 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
497 /* When we enter the file for the first time reason cannot be
498 LC_RENAME. */
499 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
501 /* If we are leaving the main file, return a NULL map. */
502 if (reason == LC_LEAVE
503 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
504 && to_file == NULL)
506 set->depth--;
507 return NULL;
510 linemap_assert (reason != LC_ENTER_MACRO);
511 line_map_ordinary *map = linemap_check_ordinary (new_linemap (set, reason));
513 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
514 to_file = "<stdin>";
516 if (reason == LC_RENAME_VERBATIM)
517 reason = LC_RENAME;
519 if (reason == LC_LEAVE)
521 /* When we are just leaving an "included" file, and jump to the next
522 location inside the "includer" right after the #include
523 "included", this variable points the map in use right before the
524 #include "included", inside the same "includer" file. */
525 line_map_ordinary *from;
527 linemap_assert (!MAIN_FILE_P (map - 1));
528 /* (MAP - 1) points to the map we are leaving. The
529 map from which (MAP - 1) got included should be the map
530 that comes right before MAP in the same file. */
531 from = INCLUDED_FROM (set, map - 1);
533 /* A TO_FILE of NULL is special - we use the natural values. */
534 if (to_file == NULL)
536 to_file = ORDINARY_MAP_FILE_NAME (from);
537 to_line = SOURCE_LINE (from, from[1].start_location);
538 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
540 else
541 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from),
542 to_file) == 0);
545 map->sysp = sysp;
546 map->start_location = start_location;
547 map->to_file = to_file;
548 map->to_line = to_line;
549 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
550 map->m_column_and_range_bits = 0;
551 map->m_range_bits = 0;
552 set->highest_location = start_location;
553 set->highest_line = start_location;
554 set->max_column_hint = 0;
556 /* This assertion is placed after set->highest_location has
557 been updated, since the latter affects
558 linemap_location_from_macro_expansion_p, which ultimately affects
559 pure_location_p. */
560 linemap_assert (pure_location_p (set, start_location));
562 if (reason == LC_ENTER)
564 map->included_from =
565 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
566 set->depth++;
567 if (set->trace_includes)
568 trace_include (set, map);
570 else if (reason == LC_RENAME)
571 map->included_from = ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
572 else if (reason == LC_LEAVE)
574 set->depth--;
575 map->included_from =
576 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
579 return map;
582 /* Returns TRUE if the line table set tracks token locations across
583 macro expansion, FALSE otherwise. */
585 bool
586 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
588 return LINEMAPS_MACRO_MAPS (set) != NULL;
591 /* Create a macro map. A macro map encodes source locations of tokens
592 that are part of a macro replacement-list, at a macro expansion
593 point. See the extensive comments of struct line_map and struct
594 line_map_macro, in line-map.h.
596 This map shall be created when the macro is expanded. The map
597 encodes the source location of the expansion point of the macro as
598 well as the "original" source location of each token that is part
599 of the macro replacement-list. If a macro is defined but never
600 expanded, it has no macro map. SET is the set of maps the macro
601 map should be part of. MACRO_NODE is the macro which the new macro
602 map should encode source locations for. EXPANSION is the location
603 of the expansion point of MACRO. For function-like macros
604 invocations, it's best to make it point to the closing parenthesis
605 of the macro, rather than the the location of the first character
606 of the macro. NUM_TOKENS is the number of tokens that are part of
607 the replacement-list of MACRO.
609 Note that when we run out of the integer space available for source
610 locations, this function returns NULL. In that case, callers of
611 this function cannot encode {line,column} pairs into locations of
612 macro tokens anymore. */
614 const line_map_macro *
615 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
616 source_location expansion, unsigned int num_tokens)
618 line_map_macro *map;
619 source_location start_location;
620 /* Cast away extern "C" from the type of xrealloc. */
621 line_map_realloc reallocator = (set->reallocator
622 ? set->reallocator
623 : (line_map_realloc) xrealloc);
625 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
627 if (start_location <= set->highest_line
628 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
629 /* We ran out of macro map space. */
630 return NULL;
632 map = linemap_check_macro (new_linemap (set, LC_ENTER_MACRO));
634 map->start_location = start_location;
635 map->macro = macro_node;
636 map->n_tokens = num_tokens;
637 map->macro_locations
638 = (source_location*) reallocator (NULL,
639 2 * num_tokens
640 * sizeof (source_location));
641 map->expansion = expansion;
642 memset (MACRO_MAP_LOCATIONS (map), 0,
643 num_tokens * sizeof (source_location));
645 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
647 return map;
650 /* Create and return a virtual location for a token that is part of a
651 macro expansion-list at a macro expansion point. See the comment
652 inside struct line_map_macro to see what an expansion-list exactly
655 A call to this function must come after a call to
656 linemap_enter_macro.
658 MAP is the map into which the source location is created. TOKEN_NO
659 is the index of the token in the macro replacement-list, starting
660 at number 0.
662 ORIG_LOC is the location of the token outside of this macro
663 expansion. If the token comes originally from the macro
664 definition, it is the locus in the macro definition; otherwise it
665 is a location in the context of the caller of this macro expansion
666 (which is a virtual location or a source location if the caller is
667 itself a macro expansion or not).
669 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
670 either of the token itself or of a macro parameter that it
671 replaces. */
673 source_location
674 linemap_add_macro_token (const line_map_macro *map,
675 unsigned int token_no,
676 source_location orig_loc,
677 source_location orig_parm_replacement_loc)
679 source_location result;
681 linemap_assert (linemap_macro_expansion_map_p (map));
682 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
684 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
685 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
687 result = MAP_START_LOCATION (map) + token_no;
688 return result;
691 /* Return a source_location for the start (i.e. column==0) of
692 (physical) line TO_LINE in the current source file (as in the
693 most recent linemap_add). MAX_COLUMN_HINT is the highest column
694 number we expect to use in this line (but it does not change
695 the highest_location). */
697 source_location
698 linemap_line_start (struct line_maps *set, linenum_type to_line,
699 unsigned int max_column_hint)
701 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
702 source_location highest = set->highest_location;
703 source_location r;
704 linenum_type last_line =
705 SOURCE_LINE (map, set->highest_line);
706 int line_delta = to_line - last_line;
707 bool add_map = false;
708 linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
709 int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
711 if (line_delta < 0
712 || (line_delta > 10
713 && line_delta * map->m_column_and_range_bits > 1000)
714 || (max_column_hint >= (1U << effective_column_bits))
715 || (max_column_hint <= 80 && effective_column_bits >= 10)
716 || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
717 && map->m_range_bits > 0)
718 || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
719 && (set->max_column_hint || highest >= LINE_MAP_MAX_SOURCE_LOCATION)))
720 add_map = true;
721 else
722 max_column_hint = set->max_column_hint;
723 if (add_map)
725 int column_bits;
726 int range_bits;
727 if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
728 || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
730 /* If the column number is ridiculous or we've allocated a huge
731 number of source_locations, give up on column numbers
732 (and on packed ranges). */
733 max_column_hint = 0;
734 column_bits = 0;
735 range_bits = 0;
736 if (highest > LINE_MAP_MAX_SOURCE_LOCATION)
737 return 0;
739 else
741 column_bits = 7;
742 if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
743 range_bits = set->default_range_bits;
744 else
745 range_bits = 0;
746 while (max_column_hint >= (1U << column_bits))
747 column_bits++;
748 max_column_hint = 1U << column_bits;
749 column_bits += range_bits;
751 /* Allocate the new line_map. However, if the current map only has a
752 single line we can sometimes just increase its column_bits instead. */
753 if (line_delta < 0
754 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
755 || SOURCE_COLUMN (map, highest) >= (1U << (column_bits - range_bits))
756 || range_bits < map->m_range_bits)
757 map = linemap_check_ordinary
758 (const_cast <line_map *>
759 (linemap_add (set, LC_RENAME,
760 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
761 ORDINARY_MAP_FILE_NAME (map),
762 to_line)));
763 map->m_column_and_range_bits = column_bits;
764 map->m_range_bits = range_bits;
765 r = (MAP_START_LOCATION (map)
766 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
767 << column_bits));
769 else
770 r = set->highest_line + (line_delta << map->m_column_and_range_bits);
772 /* Locations of ordinary tokens are always lower than locations of
773 macro tokens. */
774 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
775 return 0;
777 set->highest_line = r;
778 if (r > set->highest_location)
779 set->highest_location = r;
780 set->max_column_hint = max_column_hint;
782 /* At this point, we expect one of:
783 (a) the normal case: a "pure" location with 0 range bits, or
784 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
785 columns anymore (or ranges), or
786 (c) we're in a region with a column hint exceeding
787 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
788 with column_bits == 0. */
789 linemap_assert (pure_location_p (set, r)
790 || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
791 || map->m_column_and_range_bits == 0);
792 linemap_assert (SOURCE_LINE (map, r) == to_line);
793 return r;
796 /* Encode and return a source_location from a column number. The
797 source line considered is the last source line used to call
798 linemap_line_start, i.e, the last source line which a location was
799 encoded from. */
801 source_location
802 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
804 source_location r = set->highest_line;
806 linemap_assert
807 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
809 if (to_column >= set->max_column_hint)
811 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
812 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
814 /* Running low on source_locations - disable column numbers. */
815 return r;
817 else
819 /* Otherwise, attempt to start a new line that can hold TO_COLUMN,
820 with some space to spare. This may or may not lead to a new
821 linemap being created. */
822 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
823 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
824 map = LINEMAPS_LAST_ORDINARY_MAP (set);
825 if (map->m_column_and_range_bits == 0)
827 /* ...then the linemap has column-tracking disabled,
828 presumably due to exceeding either
829 LINE_MAP_MAX_LOCATION_WITH_COLS (overall) or
830 LINE_MAP_MAX_COLUMN_NUMBER (within this line).
831 Return the start of the linemap, which encodes column 0, for
832 the whole line. */
833 return r;
837 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
838 r = r + (to_column << map->m_range_bits);
839 if (r >= set->highest_location)
840 set->highest_location = r;
841 return r;
844 /* Encode and return a source location from a given line and
845 column. */
847 source_location
848 linemap_position_for_line_and_column (line_maps *set,
849 const line_map_ordinary *ord_map,
850 linenum_type line,
851 unsigned column)
853 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
855 source_location r = MAP_START_LOCATION (ord_map);
856 r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
857 << ord_map->m_column_and_range_bits);
858 if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
859 r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
860 << ord_map->m_range_bits);
861 source_location upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
862 if (r >= upper_limit)
863 r = upper_limit - 1;
864 if (r > set->highest_location)
865 set->highest_location = r;
866 return r;
869 /* Encode and return a source_location starting from location LOC and
870 shifting it by COLUMN_OFFSET columns. This function does not support
871 virtual locations. */
873 source_location
874 linemap_position_for_loc_and_offset (struct line_maps *set,
875 source_location loc,
876 unsigned int column_offset)
878 const line_map_ordinary * map = NULL;
880 if (IS_ADHOC_LOC (loc))
881 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
883 /* This function does not support virtual locations yet. */
884 if (linemap_location_from_macro_expansion_p (set, loc))
885 return loc;
887 if (column_offset == 0
888 /* Adding an offset to a reserved location (like
889 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
890 sense. So let's leave the location intact in that case. */
891 || loc < RESERVED_LOCATION_COUNT)
892 return loc;
894 /* We find the real location and shift it. */
895 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
896 /* The new location (loc + offset) should be higher than the first
897 location encoded by MAP. This can fail if the line information
898 is messed up because of line directives (see PR66415). */
899 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
900 return loc;
902 linenum_type line = SOURCE_LINE (map, loc);
903 unsigned int column = SOURCE_COLUMN (map, loc);
905 /* If MAP is not the last line map of its set, then the new location
906 (loc + offset) should be less than the first location encoded by
907 the next line map of the set. Otherwise, we try to encode the
908 location in the next map. */
909 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
910 && (loc + (column_offset << map->m_range_bits)
911 >= MAP_START_LOCATION (&map[1])))
913 map = &map[1];
914 /* If the next map starts in a higher line, we cannot encode the
915 location there. */
916 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
917 return loc;
920 column += column_offset;
922 /* Bail out if the column is not representable within the existing
923 linemap. */
924 if (column >= (1u << (map->m_column_and_range_bits - map->m_range_bits)))
925 return loc;
927 source_location r =
928 linemap_position_for_line_and_column (set, map, line, column);
929 if (linemap_assert_fails (r <= set->highest_location)
930 || linemap_assert_fails (map == linemap_lookup (set, r)))
931 return loc;
933 return r;
936 /* Given a virtual source location yielded by a map (either an
937 ordinary or a macro map), returns that map. */
939 const struct line_map*
940 linemap_lookup (struct line_maps *set, source_location line)
942 if (IS_ADHOC_LOC (line))
943 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
944 if (linemap_location_from_macro_expansion_p (set, line))
945 return linemap_macro_map_lookup (set, line);
946 return linemap_ordinary_map_lookup (set, line);
949 /* Given a source location yielded by an ordinary map, returns that
950 map. Since the set is built chronologically, the logical lines are
951 monotonic increasing, and so the list is sorted and we can use a
952 binary search. */
954 static const line_map_ordinary *
955 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
957 unsigned int md, mn, mx;
958 const line_map_ordinary *cached, *result;
960 if (IS_ADHOC_LOC (line))
961 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
963 if (set == NULL || line < RESERVED_LOCATION_COUNT)
964 return NULL;
966 mn = LINEMAPS_ORDINARY_CACHE (set);
967 mx = LINEMAPS_ORDINARY_USED (set);
969 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
970 /* We should get a segfault if no line_maps have been added yet. */
971 if (line >= MAP_START_LOCATION (cached))
973 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
974 return cached;
976 else
978 mx = mn;
979 mn = 0;
982 while (mx - mn > 1)
984 md = (mn + mx) / 2;
985 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
986 mx = md;
987 else
988 mn = md;
991 LINEMAPS_ORDINARY_CACHE (set) = mn;
992 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
993 linemap_assert (line >= MAP_START_LOCATION (result));
994 return result;
997 /* Given a source location yielded by a macro map, returns that map.
998 Since the set is built chronologically, the logical lines are
999 monotonic decreasing, and so the list is sorted and we can use a
1000 binary search. */
1002 static const line_map_macro *
1003 linemap_macro_map_lookup (struct line_maps *set, source_location line)
1005 unsigned int md, mn, mx;
1006 const struct line_map_macro *cached, *result;
1008 if (IS_ADHOC_LOC (line))
1009 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
1011 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
1013 if (set == NULL)
1014 return NULL;
1016 mn = LINEMAPS_MACRO_CACHE (set);
1017 mx = LINEMAPS_MACRO_USED (set);
1018 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
1020 if (line >= MAP_START_LOCATION (cached))
1022 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
1023 return cached;
1024 mx = mn - 1;
1025 mn = 0;
1028 while (mn < mx)
1030 md = (mx + mn) / 2;
1031 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1032 mn = md + 1;
1033 else
1034 mx = md;
1037 LINEMAPS_MACRO_CACHE (set) = mx;
1038 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
1039 linemap_assert (MAP_START_LOCATION (result) <= line);
1041 return result;
1044 /* Return TRUE if MAP encodes locations coming from a macro
1045 replacement-list at macro expansion point. */
1047 bool
1048 linemap_macro_expansion_map_p (const struct line_map *map)
1050 if (!map)
1051 return false;
1052 return (map->reason == LC_ENTER_MACRO);
1055 /* If LOCATION is the locus of a token in a replacement-list of a
1056 macro expansion return the location of the macro expansion point.
1058 Read the comments of struct line_map and struct line_map_macro in
1059 line-map.h to understand what a macro expansion point is. */
1061 static source_location
1062 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1063 source_location location ATTRIBUTE_UNUSED)
1065 linemap_assert (linemap_macro_expansion_map_p (map)
1066 && location >= MAP_START_LOCATION (map));
1068 /* Make sure LOCATION is correct. */
1069 linemap_assert ((location - MAP_START_LOCATION (map))
1070 < MACRO_MAP_NUM_MACRO_TOKENS (map));
1072 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1075 /* LOCATION is the source location of a token that belongs to a macro
1076 replacement-list as part of the macro expansion denoted by MAP.
1078 Return the location of the token at the definition point of the
1079 macro. */
1081 static source_location
1082 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1083 source_location location)
1085 unsigned token_no;
1087 linemap_assert (linemap_macro_expansion_map_p (map)
1088 && location >= MAP_START_LOCATION (map));
1089 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1091 token_no = location - MAP_START_LOCATION (map);
1092 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1094 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1096 return location;
1099 /* If LOCATION is the locus of a token that is an argument of a
1100 function-like macro M and appears in the expansion of M, return the
1101 locus of that argument in the context of the caller of M.
1103 In other words, this returns the xI location presented in the
1104 comments of line_map_macro above. */
1105 source_location
1106 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1107 const line_map_macro* map,
1108 source_location location)
1110 unsigned token_no;
1112 if (IS_ADHOC_LOC (location))
1113 location = get_location_from_adhoc_loc (set, location);
1115 linemap_assert (linemap_macro_expansion_map_p (map)
1116 && location >= MAP_START_LOCATION (map));
1117 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1118 linemap_assert (!IS_ADHOC_LOC (location));
1120 token_no = location - MAP_START_LOCATION (map);
1121 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1123 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1125 return location;
1128 /* Return the source line number corresponding to source location
1129 LOCATION. SET is the line map set LOCATION comes from. If
1130 LOCATION is the source location of token that is part of the
1131 replacement-list of a macro expansion return the line number of the
1132 macro expansion point. */
1135 linemap_get_expansion_line (struct line_maps *set,
1136 source_location location)
1138 const line_map_ordinary *map = NULL;
1140 if (IS_ADHOC_LOC (location))
1141 location = set->location_adhoc_data_map.data[location
1142 & MAX_SOURCE_LOCATION].locus;
1144 if (location < RESERVED_LOCATION_COUNT)
1145 return 0;
1147 location =
1148 linemap_macro_loc_to_exp_point (set, location, &map);
1150 return SOURCE_LINE (map, location);
1153 /* Return the path of the file corresponding to source code location
1154 LOCATION.
1156 If LOCATION is the source location of token that is part of the
1157 replacement-list of a macro expansion return the file path of the
1158 macro expansion point.
1160 SET is the line map set LOCATION comes from. */
1162 const char*
1163 linemap_get_expansion_filename (struct line_maps *set,
1164 source_location location)
1166 const struct line_map_ordinary *map = NULL;
1168 if (IS_ADHOC_LOC (location))
1169 location = set->location_adhoc_data_map.data[location
1170 & MAX_SOURCE_LOCATION].locus;
1172 if (location < RESERVED_LOCATION_COUNT)
1173 return NULL;
1175 location =
1176 linemap_macro_loc_to_exp_point (set, location, &map);
1178 return LINEMAP_FILE (map);
1181 /* Return the name of the macro associated to MACRO_MAP. */
1183 const char*
1184 linemap_map_get_macro_name (const line_map_macro *macro_map)
1186 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1187 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1190 /* Return a positive value if LOCATION is the locus of a token that is
1191 located in a system header, O otherwise. It returns 1 if LOCATION
1192 is the locus of a token that is located in a system header, and 2
1193 if LOCATION is the locus of a token located in a C system header
1194 that therefore needs to be extern "C" protected in C++.
1196 Note that this function returns 1 if LOCATION belongs to a token
1197 that is part of a macro replacement-list defined in a system
1198 header, but expanded in a non-system file. */
1201 linemap_location_in_system_header_p (struct line_maps *set,
1202 source_location location)
1204 const struct line_map *map = NULL;
1206 if (IS_ADHOC_LOC (location))
1207 location = set->location_adhoc_data_map.data[location
1208 & MAX_SOURCE_LOCATION].locus;
1210 if (location < RESERVED_LOCATION_COUNT)
1211 return false;
1213 /* Let's look at where the token for LOCATION comes from. */
1214 while (true)
1216 map = linemap_lookup (set, location);
1217 if (map != NULL)
1219 if (!linemap_macro_expansion_map_p (map))
1220 /* It's a normal token. */
1221 return LINEMAP_SYSP (linemap_check_ordinary (map));
1222 else
1224 const line_map_macro *macro_map = linemap_check_macro (map);
1226 /* It's a token resulting from a macro expansion. */
1227 source_location loc =
1228 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
1229 if (loc < RESERVED_LOCATION_COUNT)
1230 /* This token might come from a built-in macro. Let's
1231 look at where that macro got expanded. */
1232 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1233 else
1234 location = loc;
1237 else
1238 break;
1240 return false;
1243 /* Return TRUE if LOCATION is a source code location of a token that is part of
1244 a macro expansion, FALSE otherwise. */
1246 bool
1247 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1248 source_location location)
1250 if (IS_ADHOC_LOC (location))
1251 location = set->location_adhoc_data_map.data[location
1252 & MAX_SOURCE_LOCATION].locus;
1254 linemap_assert (location <= MAX_SOURCE_LOCATION
1255 && (set->highest_location
1256 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
1257 if (set == NULL)
1258 return false;
1259 return (location > set->highest_location);
1262 /* Given two virtual locations *LOC0 and *LOC1, return the first
1263 common macro map in their macro expansion histories. Return NULL
1264 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1265 virtual location of the token inside the resulting macro. */
1267 static const struct line_map*
1268 first_map_in_common_1 (struct line_maps *set,
1269 source_location *loc0,
1270 source_location *loc1)
1272 source_location l0 = *loc0, l1 = *loc1;
1273 const struct line_map *map0 = linemap_lookup (set, l0),
1274 *map1 = linemap_lookup (set, l1);
1276 while (linemap_macro_expansion_map_p (map0)
1277 && linemap_macro_expansion_map_p (map1)
1278 && (map0 != map1))
1280 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1282 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1283 l0);
1284 map0 = linemap_lookup (set, l0);
1286 else
1288 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1289 l1);
1290 map1 = linemap_lookup (set, l1);
1294 if (map0 == map1)
1296 *loc0 = l0;
1297 *loc1 = l1;
1298 return map0;
1300 return NULL;
1303 /* Given two virtual locations LOC0 and LOC1, return the first common
1304 macro map in their macro expansion histories. Return NULL if no
1305 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1306 virtual location of the token inside the resulting macro, upon
1307 return of a non-NULL result. */
1309 static const struct line_map*
1310 first_map_in_common (struct line_maps *set,
1311 source_location loc0,
1312 source_location loc1,
1313 source_location *res_loc0,
1314 source_location *res_loc1)
1316 *res_loc0 = loc0;
1317 *res_loc1 = loc1;
1319 return first_map_in_common_1 (set, res_loc0, res_loc1);
1322 /* Return a positive value if PRE denotes the location of a token that
1323 comes before the token of POST, 0 if PRE denotes the location of
1324 the same token as the token for POST, and a negative value
1325 otherwise. */
1328 linemap_compare_locations (struct line_maps *set,
1329 source_location pre,
1330 source_location post)
1332 bool pre_virtual_p, post_virtual_p;
1333 source_location l0 = pre, l1 = post;
1335 if (IS_ADHOC_LOC (l0))
1336 l0 = get_location_from_adhoc_loc (set, l0);
1337 if (IS_ADHOC_LOC (l1))
1338 l1 = get_location_from_adhoc_loc (set, l1);
1340 if (l0 == l1)
1341 return 0;
1343 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1344 l0 = linemap_resolve_location (set, l0,
1345 LRK_MACRO_EXPANSION_POINT,
1346 NULL);
1348 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1349 l1 = linemap_resolve_location (set, l1,
1350 LRK_MACRO_EXPANSION_POINT,
1351 NULL);
1353 if (l0 == l1
1354 && pre_virtual_p
1355 && post_virtual_p)
1357 /* So pre and post represent two tokens that are present in a
1358 same macro expansion. Let's see if the token for pre was
1359 before the token for post in that expansion. */
1360 unsigned i0, i1;
1361 const struct line_map *map =
1362 first_map_in_common (set, pre, post, &l0, &l1);
1364 if (map == NULL)
1365 /* This should not be possible. */
1366 abort ();
1368 i0 = l0 - MAP_START_LOCATION (map);
1369 i1 = l1 - MAP_START_LOCATION (map);
1370 return i1 - i0;
1373 if (IS_ADHOC_LOC (l0))
1374 l0 = get_location_from_adhoc_loc (set, l0);
1375 if (IS_ADHOC_LOC (l1))
1376 l1 = get_location_from_adhoc_loc (set, l1);
1378 return l1 - l0;
1381 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1383 static void
1384 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1386 unsigned int i = set->depth;
1388 while (--i)
1389 putc ('.', stderr);
1391 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1394 /* Return the spelling location of the token wherever it comes from,
1395 whether part of a macro definition or not.
1397 This is a subroutine for linemap_resolve_location. */
1399 static source_location
1400 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1401 source_location location,
1402 const line_map_ordinary **original_map)
1404 struct line_map *map;
1405 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1407 while (true)
1409 map = const_cast <line_map *> (linemap_lookup (set, location));
1410 if (!linemap_macro_expansion_map_p (map))
1411 break;
1413 location
1414 = linemap_macro_map_loc_unwind_toward_spelling
1415 (set, linemap_check_macro (map),
1416 location);
1419 if (original_map)
1420 *original_map = linemap_check_ordinary (map);
1421 return location;
1424 /* If LOCATION is the source location of a token that belongs to a
1425 macro replacement-list -- as part of a macro expansion -- then
1426 return the location of the token at the definition point of the
1427 macro. Otherwise, return LOCATION. SET is the set of maps
1428 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1429 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1430 returned location comes from.
1432 This is a subroutine of linemap_resolve_location. */
1434 static source_location
1435 linemap_macro_loc_to_def_point (struct line_maps *set,
1436 source_location location,
1437 const line_map_ordinary **original_map)
1439 struct line_map *map;
1441 if (IS_ADHOC_LOC (location))
1442 location = set->location_adhoc_data_map.data[location
1443 & MAX_SOURCE_LOCATION].locus;
1445 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1447 while (true)
1449 map = const_cast <line_map *> (linemap_lookup (set, location));
1450 if (!linemap_macro_expansion_map_p (map))
1451 break;
1453 location =
1454 linemap_macro_map_loc_to_def_point (linemap_check_macro (map),
1455 location);
1458 if (original_map)
1459 *original_map = linemap_check_ordinary (map);
1460 return location;
1463 /* If LOCATION is the source location of a token that belongs to a
1464 macro replacement-list -- at a macro expansion point -- then return
1465 the location of the topmost expansion point of the macro. We say
1466 topmost because if we are in the context of a nested macro
1467 expansion, the function returns the source location of the first
1468 macro expansion that triggered the nested expansions.
1470 Otherwise, return LOCATION. SET is the set of maps location come
1471 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1472 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1473 location comes from.
1475 This is a subroutine of linemap_resolve_location. */
1477 static source_location
1478 linemap_macro_loc_to_exp_point (struct line_maps *set,
1479 source_location location,
1480 const line_map_ordinary **original_map)
1482 struct line_map *map;
1484 if (IS_ADHOC_LOC (location))
1485 location = set->location_adhoc_data_map.data[location
1486 & MAX_SOURCE_LOCATION].locus;
1488 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1490 while (true)
1492 map = const_cast <line_map *> (linemap_lookup (set, location));
1493 if (!linemap_macro_expansion_map_p (map))
1494 break;
1495 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1496 location);
1499 if (original_map)
1500 *original_map = linemap_check_ordinary (map);
1501 return location;
1504 /* Resolve a virtual location into either a spelling location, an
1505 expansion point location or a token argument replacement point
1506 location. Return the map that encodes the virtual location as well
1507 as the resolved location.
1509 If LOC is *NOT* the location of a token resulting from the
1510 expansion of a macro, then the parameter LRK (which stands for
1511 Location Resolution Kind) is ignored and the resulting location
1512 just equals the one given in argument.
1514 Now if LOC *IS* the location of a token resulting from the
1515 expansion of a macro, this is what happens.
1517 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1518 -------------------------------
1520 The virtual location is resolved to the first macro expansion point
1521 that led to this macro expansion.
1523 * If LRK is set to LRK_SPELLING_LOCATION
1524 -------------------------------------
1526 The virtual location is resolved to the locus where the token has
1527 been spelled in the source. This can follow through all the macro
1528 expansions that led to the token.
1530 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1531 --------------------------------------
1533 The virtual location is resolved to the locus of the token in the
1534 context of the macro definition.
1536 If LOC is the locus of a token that is an argument of a
1537 function-like macro [replacing a parameter in the replacement list
1538 of the macro] the virtual location is resolved to the locus of the
1539 parameter that is replaced, in the context of the definition of the
1540 macro.
1542 If LOC is the locus of a token that is not an argument of a
1543 function-like macro, then the function behaves as if LRK was set to
1544 LRK_SPELLING_LOCATION.
1546 If MAP is not NULL, *MAP is set to the map encoding the
1547 returned location. Note that if the returned location wasn't originally
1548 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1549 resolves to a location reserved for the client code, like
1550 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1552 source_location
1553 linemap_resolve_location (struct line_maps *set,
1554 source_location loc,
1555 enum location_resolution_kind lrk,
1556 const line_map_ordinary **map)
1558 source_location locus = loc;
1559 if (IS_ADHOC_LOC (loc))
1560 locus = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1562 if (locus < RESERVED_LOCATION_COUNT)
1564 /* A reserved location wasn't encoded in a map. Let's return a
1565 NULL map here, just like what linemap_ordinary_map_lookup
1566 does. */
1567 if (map)
1568 *map = NULL;
1569 return loc;
1572 switch (lrk)
1574 case LRK_MACRO_EXPANSION_POINT:
1575 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1576 break;
1577 case LRK_SPELLING_LOCATION:
1578 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1579 break;
1580 case LRK_MACRO_DEFINITION_LOCATION:
1581 loc = linemap_macro_loc_to_def_point (set, loc, map);
1582 break;
1583 default:
1584 abort ();
1586 return loc;
1589 /* TRUE if LOCATION is a source code location of a token that is part of the
1590 definition of a macro, FALSE otherwise. */
1592 bool
1593 linemap_location_from_macro_definition_p (struct line_maps *set,
1594 source_location loc)
1596 if (IS_ADHOC_LOC (loc))
1597 loc = get_location_from_adhoc_loc (set, loc);
1599 if (!linemap_location_from_macro_expansion_p (set, loc))
1600 return false;
1602 while (true)
1604 const struct line_map_macro *map
1605 = linemap_check_macro (linemap_lookup (set, loc));
1607 source_location s_loc
1608 = linemap_macro_map_loc_unwind_toward_spelling (set, map, loc);
1609 if (linemap_location_from_macro_expansion_p (set, s_loc))
1610 loc = s_loc;
1611 else
1613 source_location def_loc
1614 = linemap_macro_map_loc_to_def_point (map, loc);
1615 return s_loc == def_loc;
1621 Suppose that LOC is the virtual location of a token T coming from
1622 the expansion of a macro M. This function then steps up to get the
1623 location L of the point where M got expanded. If L is a spelling
1624 location inside a macro expansion M', then this function returns
1625 the locus of the point where M' was expanded. Said otherwise, this
1626 function returns the location of T in the context that triggered
1627 the expansion of M.
1629 *LOC_MAP must be set to the map of LOC. This function then sets it
1630 to the map of the returned location. */
1632 source_location
1633 linemap_unwind_toward_expansion (struct line_maps *set,
1634 source_location loc,
1635 const struct line_map **map)
1637 source_location resolved_location;
1638 const line_map_macro *macro_map = linemap_check_macro (*map);
1639 const struct line_map *resolved_map;
1641 if (IS_ADHOC_LOC (loc))
1642 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1644 resolved_location =
1645 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1646 resolved_map = linemap_lookup (set, resolved_location);
1648 if (!linemap_macro_expansion_map_p (resolved_map))
1650 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1651 resolved_map = linemap_lookup (set, resolved_location);
1654 *map = resolved_map;
1655 return resolved_location;
1658 /* If LOC is the virtual location of a token coming from the expansion
1659 of a macro M and if its spelling location is reserved (e.g, a
1660 location for a built-in token), then this function unwinds (using
1661 linemap_unwind_toward_expansion) the location until a location that
1662 is not reserved and is not in a system header is reached. In other
1663 words, this unwinds the reserved location until a location that is
1664 in real source code is reached.
1666 Otherwise, if the spelling location for LOC is not reserved or if
1667 LOC doesn't come from the expansion of a macro, the function
1668 returns LOC as is and *MAP is not touched.
1670 *MAP is set to the map of the returned location if the later is
1671 different from LOC. */
1672 source_location
1673 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1674 source_location loc,
1675 const struct line_map **map)
1677 source_location resolved_loc;
1678 const struct line_map *map0 = NULL;
1679 const line_map_ordinary *map1 = NULL;
1681 if (IS_ADHOC_LOC (loc))
1682 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1684 map0 = linemap_lookup (set, loc);
1685 if (!linemap_macro_expansion_map_p (map0))
1686 return loc;
1688 resolved_loc = linemap_resolve_location (set, loc,
1689 LRK_SPELLING_LOCATION,
1690 &map1);
1692 if (resolved_loc >= RESERVED_LOCATION_COUNT
1693 && !LINEMAP_SYSP (map1))
1694 return loc;
1696 while (linemap_macro_expansion_map_p (map0)
1697 && (resolved_loc < RESERVED_LOCATION_COUNT
1698 || LINEMAP_SYSP (map1)))
1700 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1701 resolved_loc = linemap_resolve_location (set, loc,
1702 LRK_SPELLING_LOCATION,
1703 &map1);
1706 if (map != NULL)
1707 *map = map0;
1708 return loc;
1711 /* Expand source code location LOC and return a user readable source
1712 code location. LOC must be a spelling (non-virtual) location. If
1713 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1714 location is returned. */
1716 expanded_location
1717 linemap_expand_location (struct line_maps *set,
1718 const struct line_map *map,
1719 source_location loc)
1722 expanded_location xloc;
1724 memset (&xloc, 0, sizeof (xloc));
1725 if (IS_ADHOC_LOC (loc))
1727 xloc.data
1728 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1729 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1732 if (loc < RESERVED_LOCATION_COUNT)
1733 /* The location for this token wasn't generated from a line map.
1734 It was probably a location for a builtin token, chosen by some
1735 client code. Let's not try to expand the location in that
1736 case. */;
1737 else if (map == NULL)
1738 /* We shouldn't be getting a NULL map with a location that is not
1739 reserved by the client code. */
1740 abort ();
1741 else
1743 /* MAP must be an ordinary map and LOC must be non-virtual,
1744 encoded into this map, obviously; the accessors used on MAP
1745 below ensure it is ordinary. Let's just assert the
1746 non-virtualness of LOC here. */
1747 if (linemap_location_from_macro_expansion_p (set, loc))
1748 abort ();
1750 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1752 xloc.file = LINEMAP_FILE (ord_map);
1753 xloc.line = SOURCE_LINE (ord_map, loc);
1754 xloc.column = SOURCE_COLUMN (ord_map, loc);
1755 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1758 return xloc;
1762 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1763 is NULL, use stderr. IS_MACRO is true if the caller wants to
1764 dump a macro map, false otherwise. */
1766 void
1767 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1769 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1770 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1771 "LC_ENTER_MACRO" };
1772 const char *reason;
1773 const line_map *map;
1775 if (stream == NULL)
1776 stream = stderr;
1778 if (!is_macro)
1779 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1780 else
1781 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1783 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1785 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1786 ix, (void *) map, map->start_location, reason,
1787 ((!is_macro
1788 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1789 ? "yes" : "no"));
1790 if (!is_macro)
1792 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1793 unsigned includer_ix;
1794 const line_map_ordinary *includer_map;
1796 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
1797 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1798 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1799 : NULL;
1801 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1802 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1803 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1804 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1806 else
1808 const line_map_macro *macro_map = linemap_check_macro (map);
1809 fprintf (stream, "Macro: %s (%u tokens)\n",
1810 linemap_map_get_macro_name (macro_map),
1811 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1814 fprintf (stream, "\n");
1818 /* Dump debugging information about source location LOC into the file
1819 stream STREAM. SET is the line map set LOC comes from. */
1821 void
1822 linemap_dump_location (struct line_maps *set,
1823 source_location loc,
1824 FILE *stream)
1826 const line_map_ordinary *map;
1827 source_location location;
1828 const char *path = "", *from = "";
1829 int l = -1, c = -1, s = -1, e = -1;
1831 if (IS_ADHOC_LOC (loc))
1832 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1834 if (loc == 0)
1835 return;
1837 location =
1838 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1840 if (map == NULL)
1841 /* Only reserved locations can be tolerated in this case. */
1842 linemap_assert (location < RESERVED_LOCATION_COUNT);
1843 else
1845 path = LINEMAP_FILE (map);
1846 l = SOURCE_LINE (map, location);
1847 c = SOURCE_COLUMN (map, location);
1848 s = LINEMAP_SYSP (map) != 0;
1849 e = location != loc;
1850 if (e)
1851 from = "N/A";
1852 else
1853 from = (INCLUDED_FROM (set, map))
1854 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1855 : "<NULL>";
1858 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1859 E: macro expansion?, LOC: original location, R: resolved location */
1860 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1861 path, from, l, c, s, (void*)map, e, loc, location);
1864 /* Return the highest location emitted for a given file for which
1865 there is a line map in SET. FILE_NAME is the file name to
1866 consider. If the function returns TRUE, *LOC is set to the highest
1867 location emitted for that file. */
1869 bool
1870 linemap_get_file_highest_location (struct line_maps *set,
1871 const char *file_name,
1872 source_location *loc)
1874 /* If the set is empty or no ordinary map has been created then
1875 there is no file to look for ... */
1876 if (set == NULL || set->info_ordinary.used == 0)
1877 return false;
1879 /* Now look for the last ordinary map created for FILE_NAME. */
1880 int i;
1881 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1883 const char *fname = set->info_ordinary.maps[i].to_file;
1884 if (fname && !filename_cmp (fname, file_name))
1885 break;
1888 if (i < 0)
1889 return false;
1891 /* The highest location for a given map is either the starting
1892 location of the next map minus one, or -- if the map is the
1893 latest one -- the highest location of the set. */
1894 source_location result;
1895 if (i == (int) set->info_ordinary.used - 1)
1896 result = set->highest_location;
1897 else
1898 result = set->info_ordinary.maps[i + 1].start_location - 1;
1900 *loc = result;
1901 return true;
1904 /* Compute and return statistics about the memory consumption of some
1905 parts of the line table SET. */
1907 void
1908 linemap_get_statistics (struct line_maps *set,
1909 struct linemap_stats *s)
1911 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1912 macro_maps_allocated_size, macro_maps_used_size,
1913 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1915 const line_map_macro *cur_map;
1917 ordinary_maps_allocated_size =
1918 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1920 ordinary_maps_used_size =
1921 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1923 macro_maps_allocated_size =
1924 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1926 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1927 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1928 ++cur_map)
1930 unsigned i;
1932 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1934 macro_maps_locations_size +=
1935 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1937 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1939 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1940 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1941 duplicated_macro_maps_locations_size +=
1942 sizeof (source_location);
1946 macro_maps_used_size =
1947 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1949 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1950 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1951 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1952 s->ordinary_maps_used_size = ordinary_maps_used_size;
1953 s->num_expanded_macros = num_expanded_macros_counter;
1954 s->num_macro_tokens = num_macro_tokens_counter;
1955 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1956 s->macro_maps_allocated_size = macro_maps_allocated_size;
1957 s->macro_maps_locations_size = macro_maps_locations_size;
1958 s->macro_maps_used_size = macro_maps_used_size;
1959 s->duplicated_macro_maps_locations_size =
1960 duplicated_macro_maps_locations_size;
1961 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1962 * sizeof (struct location_adhoc_data));
1963 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
1967 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1968 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1969 specifies how many macro maps to dump. */
1971 void
1972 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1973 unsigned int num_macro)
1975 unsigned int i;
1977 if (set == NULL)
1978 return;
1980 if (stream == NULL)
1981 stream = stderr;
1983 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1984 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1985 fprintf (stream, "Include stack depth: %d\n", set->depth);
1986 fprintf (stream, "Highest location: %u\n", set->highest_location);
1988 if (num_ordinary)
1990 fprintf (stream, "\nOrdinary line maps\n");
1991 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1992 linemap_dump (stream, set, i, false);
1993 fprintf (stream, "\n");
1996 if (num_macro)
1998 fprintf (stream, "\nMacro line maps\n");
1999 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
2000 linemap_dump (stream, set, i, true);
2001 fprintf (stream, "\n");
2005 /* class rich_location. */
2007 /* Construct a rich_location with location LOC as its initial range. */
2009 rich_location::rich_location (line_maps *set, source_location loc) :
2010 m_line_table (set),
2011 m_ranges (),
2012 m_column_override (0),
2013 m_have_expanded_location (false),
2014 m_fixit_hints (),
2015 m_seen_impossible_fixit (false)
2017 add_range (loc, true);
2020 /* The destructor for class rich_location. */
2022 rich_location::~rich_location ()
2024 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2025 delete get_fixit_hint (i);
2028 /* Get location IDX within this rich_location. */
2030 source_location
2031 rich_location::get_loc (unsigned int idx) const
2033 const location_range *locrange = get_range (idx);
2034 return locrange->m_loc;
2037 /* Get range IDX within this rich_location. */
2039 const location_range *
2040 rich_location::get_range (unsigned int idx) const
2042 return &m_ranges[idx];
2045 /* Mutable access to range IDX within this rich_location. */
2047 location_range *
2048 rich_location::get_range (unsigned int idx)
2050 return &m_ranges[idx];
2053 /* Expand location IDX within this rich_location. */
2054 /* Get an expanded_location for this rich_location's primary
2055 location. */
2057 expanded_location
2058 rich_location::get_expanded_location (unsigned int idx)
2060 if (idx == 0)
2062 /* Cache the expansion of the primary location. */
2063 if (!m_have_expanded_location)
2065 m_expanded_location
2066 = linemap_client_expand_location_to_spelling_point (get_loc (0));
2067 if (m_column_override)
2068 m_expanded_location.column = m_column_override;
2069 m_have_expanded_location = true;
2072 return m_expanded_location;
2074 else
2075 return linemap_client_expand_location_to_spelling_point (get_loc (idx));
2078 /* Set the column of the primary location, with 0 meaning
2079 "don't override it". */
2081 void
2082 rich_location::override_column (int column)
2084 m_column_override = column;
2085 m_have_expanded_location = false;
2088 /* Add the given range. */
2090 void
2091 rich_location::add_range (source_location loc, bool show_caret_p)
2093 location_range range;
2094 range.m_loc = loc;
2095 range.m_show_caret_p = show_caret_p;
2096 m_ranges.push (range);
2099 /* Add or overwrite the location given by IDX, setting its location to LOC,
2100 and setting its "should my caret be printed" flag to SHOW_CARET_P.
2102 It must either overwrite an existing location, or add one *exactly* on
2103 the end of the array.
2105 This is primarily for use by gcc when implementing diagnostic format
2106 decoders e.g.
2107 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2108 (which writes the source location of a tree back into location 0 of
2109 the rich_location), and
2110 - the "%C" and "%L" format codes in the Fortran frontend. */
2112 void
2113 rich_location::set_range (line_maps * /*set*/, unsigned int idx,
2114 source_location loc, bool show_caret_p)
2116 /* We can either overwrite an existing range, or add one exactly
2117 on the end of the array. */
2118 linemap_assert (idx <= m_ranges.count ());
2120 if (idx == m_ranges.count ())
2121 add_range (loc, show_caret_p);
2122 else
2124 location_range *locrange = get_range (idx);
2125 locrange->m_loc = loc;
2126 locrange->m_show_caret_p = show_caret_p;
2129 if (idx == 0)
2130 /* Mark any cached value here as dirty. */
2131 m_have_expanded_location = false;
2134 /* Methods for adding insertion fix-it hints. */
2136 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2137 immediately before the primary range's start location. */
2139 void
2140 rich_location::add_fixit_insert_before (const char *new_content)
2142 add_fixit_insert_before (get_loc (), new_content);
2145 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2146 immediately before the start of WHERE. */
2148 void
2149 rich_location::add_fixit_insert_before (source_location where,
2150 const char *new_content)
2152 source_location start = get_range_from_loc (m_line_table, where).m_start;
2153 maybe_add_fixit (start, start, new_content);
2156 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2157 immediately after the primary range's end-point. */
2159 void
2160 rich_location::add_fixit_insert_after (const char *new_content)
2162 add_fixit_insert_after (get_loc (), new_content);
2165 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2166 immediately after the end-point of WHERE. */
2168 void
2169 rich_location::add_fixit_insert_after (source_location where,
2170 const char *new_content)
2172 source_location finish = get_range_from_loc (m_line_table, where).m_finish;
2173 source_location next_loc
2174 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2176 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2177 its input value. */
2178 if (next_loc == finish)
2180 stop_supporting_fixits ();
2181 return;
2184 maybe_add_fixit (next_loc, next_loc, new_content);
2187 /* Methods for adding removal fix-it hints. */
2189 /* Add a fixit-hint, suggesting removal of the content covered
2190 by range 0. */
2192 void
2193 rich_location::add_fixit_remove ()
2195 add_fixit_remove (get_loc ());
2198 /* Add a fixit-hint, suggesting removal of the content between
2199 the start and finish of WHERE. */
2201 void
2202 rich_location::add_fixit_remove (source_location where)
2204 source_range range = get_range_from_loc (m_line_table, where);
2205 add_fixit_remove (range);
2208 /* Add a fixit-hint, suggesting removal of the content at
2209 SRC_RANGE. */
2211 void
2212 rich_location::add_fixit_remove (source_range src_range)
2214 add_fixit_replace (src_range, "");
2217 /* Add a fixit-hint, suggesting replacement of the content covered
2218 by range 0 with NEW_CONTENT. */
2220 void
2221 rich_location::add_fixit_replace (const char *new_content)
2223 add_fixit_replace (get_loc (), new_content);
2226 /* Methods for adding "replace" fix-it hints. */
2228 /* Add a fixit-hint, suggesting replacement of the content between
2229 the start and finish of WHERE with NEW_CONTENT. */
2231 void
2232 rich_location::add_fixit_replace (source_location where,
2233 const char *new_content)
2235 source_range range = get_range_from_loc (m_line_table, where);
2236 add_fixit_replace (range, new_content);
2239 /* Add a fixit-hint, suggesting replacement of the content at
2240 SRC_RANGE with NEW_CONTENT. */
2242 void
2243 rich_location::add_fixit_replace (source_range src_range,
2244 const char *new_content)
2246 source_location start = get_pure_location (m_line_table, src_range.m_start);
2247 source_location finish = get_pure_location (m_line_table, src_range.m_finish);
2249 /* Fix-it hints use half-closed ranges, so attempt to offset the endpoint. */
2250 source_location next_loc
2251 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2252 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2253 its input value. */
2254 if (next_loc == finish)
2256 stop_supporting_fixits ();
2257 return;
2259 finish = next_loc;
2261 maybe_add_fixit (start, finish, new_content);
2264 /* Get the last fix-it hint within this rich_location, or NULL if none. */
2266 fixit_hint *
2267 rich_location::get_last_fixit_hint () const
2269 if (m_fixit_hints.count () > 0)
2270 return get_fixit_hint (m_fixit_hints.count () - 1);
2271 else
2272 return NULL;
2275 /* If WHERE is an "awkward" location, then mark this rich_location as not
2276 supporting fixits, purging any thay were already added, and return true.
2278 Otherwise (the common case), return false. */
2280 bool
2281 rich_location::reject_impossible_fixit (source_location where)
2283 /* Fix-its within a rich_location should either all be suggested, or
2284 none of them should be suggested.
2285 Once we've rejected a fixit, we reject any more, even those
2286 with reasonable locations. */
2287 if (m_seen_impossible_fixit)
2288 return true;
2290 if (where <= LINE_MAP_MAX_LOCATION_WITH_COLS)
2291 /* WHERE is a reasonable location for a fix-it; don't reject it. */
2292 return false;
2294 /* Otherwise we have an attempt to add a fix-it with an "awkward"
2295 location: either one that we can't obtain column information
2296 for (within an ordinary map), or one within a macro expansion. */
2297 stop_supporting_fixits ();
2298 return true;
2301 /* Mark this rich_location as not supporting fixits, purging any that were
2302 already added. */
2304 void
2305 rich_location::stop_supporting_fixits ()
2307 m_seen_impossible_fixit = true;
2309 /* Purge the rich_location of any fix-its that were already added. */
2310 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2311 delete get_fixit_hint (i);
2312 m_fixit_hints.truncate (0);
2315 /* Add HINT to the fix-it hints in this rich_location,
2316 consolidating into the prior fixit if possible. */
2318 void
2319 rich_location::maybe_add_fixit (source_location start,
2320 source_location next_loc,
2321 const char *new_content)
2323 if (reject_impossible_fixit (start))
2324 return;
2325 if (reject_impossible_fixit (next_loc))
2326 return;
2328 const char *newline = strchr (new_content, '\n');
2329 if (newline)
2331 /* For now, we can only support insertion of whole lines
2332 i.e. starts at start of line, and the newline is at the end of
2333 the insertion point. */
2335 /* It must be an insertion, not a replacement/deletion. */
2336 if (start != next_loc)
2338 stop_supporting_fixits ();
2339 return;
2342 /* The insertion must be at the start of a line. */
2343 expanded_location exploc_start
2344 = linemap_client_expand_location_to_spelling_point (start);
2345 if (exploc_start.column != 1)
2347 stop_supporting_fixits ();
2348 return;
2351 /* The newline must be at end of NEW_CONTENT.
2352 We could eventually split up fix-its at newlines if we wanted
2353 to allow more generality (e.g. to allow adding multiple lines
2354 with one add_fixit call. */
2355 if (newline[1] != '\0')
2357 stop_supporting_fixits ();
2358 return;
2362 /* Consolidate neighboring fixits.
2363 Don't consolidate into newline-insertion fixits. */
2364 fixit_hint *prev = get_last_fixit_hint ();
2365 if (prev && !prev->ends_with_newline_p ())
2366 if (prev->maybe_append (start, next_loc, new_content))
2367 return;
2369 m_fixit_hints.push (new fixit_hint (start, next_loc, new_content));
2372 /* class fixit_hint. */
2374 fixit_hint::fixit_hint (source_location start,
2375 source_location next_loc,
2376 const char *new_content)
2377 : m_start (start),
2378 m_next_loc (next_loc),
2379 m_bytes (xstrdup (new_content)),
2380 m_len (strlen (new_content))
2384 /* Does this fix-it hint affect the given line? */
2386 bool
2387 fixit_hint::affects_line_p (const char *file, int line) const
2389 expanded_location exploc_start
2390 = linemap_client_expand_location_to_spelling_point (m_start);
2391 if (file != exploc_start.file)
2392 return false;
2393 if (line < exploc_start.line)
2394 return false;
2395 expanded_location exploc_next_loc
2396 = linemap_client_expand_location_to_spelling_point (m_next_loc);
2397 if (file != exploc_next_loc.file)
2398 return false;
2399 if (line > exploc_next_loc.line)
2400 return false;
2401 return true;
2404 /* Method for consolidating fix-it hints, for use by
2405 rich_location::maybe_add_fixit.
2406 If possible, merge a pending fix-it hint with the given params
2407 into this one and return true.
2408 Otherwise return false. */
2410 bool
2411 fixit_hint::maybe_append (source_location start,
2412 source_location next_loc,
2413 const char *new_content)
2415 /* For consolidation to be possible, START must be at this hint's
2416 m_next_loc. */
2417 if (start != m_next_loc)
2418 return false;
2420 /* If so, we have neighboring replacements; merge them. */
2421 m_next_loc = next_loc;
2422 size_t extra_len = strlen (new_content);
2423 m_bytes = (char *)xrealloc (m_bytes, m_len + extra_len + 1);
2424 memcpy (m_bytes + m_len, new_content, extra_len);
2425 m_len += extra_len;
2426 m_bytes[m_len] = '\0';
2427 return true;
2430 /* Return true iff this hint's content ends with a newline. */
2432 bool
2433 fixit_hint::ends_with_newline_p () const
2435 if (m_len == 0)
2436 return false;
2437 return m_bytes[m_len - 1] == '\n';