[AArch64] Fix bootstrap due to wide_int .elt (0) uninit warning
[official-gcc.git] / libcpp / line-map.c
blob949489eb1a1b645e6b7b58f307a5d27b9a23a0f7
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_assert_fails
885 (!linemap_location_from_macro_expansion_p (set, loc)))
886 return loc;
888 if (column_offset == 0
889 /* Adding an offset to a reserved location (like
890 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
891 sense. So let's leave the location intact in that case. */
892 || loc < RESERVED_LOCATION_COUNT)
893 return loc;
895 /* We find the real location and shift it. */
896 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
897 /* The new location (loc + offset) should be higher than the first
898 location encoded by MAP. This can fail if the line information
899 is messed up because of line directives (see PR66415). */
900 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
901 return loc;
903 linenum_type line = SOURCE_LINE (map, loc);
904 unsigned int column = SOURCE_COLUMN (map, loc);
906 /* If MAP is not the last line map of its set, then the new location
907 (loc + offset) should be less than the first location encoded by
908 the next line map of the set. Otherwise, we try to encode the
909 location in the next map. */
910 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
911 && (loc + (column_offset << map->m_range_bits)
912 >= MAP_START_LOCATION (&map[1])))
914 map = &map[1];
915 /* If the next map starts in a higher line, we cannot encode the
916 location there. */
917 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
918 return loc;
921 column += column_offset;
923 /* Bail out if the column is not representable within the existing
924 linemap. */
925 if (column >= (1u << (map->m_column_and_range_bits - map->m_range_bits)))
926 return loc;
928 source_location r =
929 linemap_position_for_line_and_column (set, map, line, column);
930 if (linemap_assert_fails (r <= set->highest_location)
931 || linemap_assert_fails (map == linemap_lookup (set, r)))
932 return loc;
934 return r;
937 /* Given a virtual source location yielded by a map (either an
938 ordinary or a macro map), returns that map. */
940 const struct line_map*
941 linemap_lookup (struct line_maps *set, source_location line)
943 if (IS_ADHOC_LOC (line))
944 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
945 if (linemap_location_from_macro_expansion_p (set, line))
946 return linemap_macro_map_lookup (set, line);
947 return linemap_ordinary_map_lookup (set, line);
950 /* Given a source location yielded by an ordinary map, returns that
951 map. Since the set is built chronologically, the logical lines are
952 monotonic increasing, and so the list is sorted and we can use a
953 binary search. */
955 static const line_map_ordinary *
956 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
958 unsigned int md, mn, mx;
959 const line_map_ordinary *cached, *result;
961 if (IS_ADHOC_LOC (line))
962 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
964 if (set == NULL || line < RESERVED_LOCATION_COUNT)
965 return NULL;
967 mn = LINEMAPS_ORDINARY_CACHE (set);
968 mx = LINEMAPS_ORDINARY_USED (set);
970 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
971 /* We should get a segfault if no line_maps have been added yet. */
972 if (line >= MAP_START_LOCATION (cached))
974 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
975 return cached;
977 else
979 mx = mn;
980 mn = 0;
983 while (mx - mn > 1)
985 md = (mn + mx) / 2;
986 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
987 mx = md;
988 else
989 mn = md;
992 LINEMAPS_ORDINARY_CACHE (set) = mn;
993 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
994 linemap_assert (line >= MAP_START_LOCATION (result));
995 return result;
998 /* Given a source location yielded by a macro map, returns that map.
999 Since the set is built chronologically, the logical lines are
1000 monotonic decreasing, and so the list is sorted and we can use a
1001 binary search. */
1003 static const line_map_macro *
1004 linemap_macro_map_lookup (struct line_maps *set, source_location line)
1006 unsigned int md, mn, mx;
1007 const struct line_map_macro *cached, *result;
1009 if (IS_ADHOC_LOC (line))
1010 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
1012 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
1014 if (set == NULL)
1015 return NULL;
1017 mn = LINEMAPS_MACRO_CACHE (set);
1018 mx = LINEMAPS_MACRO_USED (set);
1019 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
1021 if (line >= MAP_START_LOCATION (cached))
1023 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
1024 return cached;
1025 mx = mn - 1;
1026 mn = 0;
1029 while (mn < mx)
1031 md = (mx + mn) / 2;
1032 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1033 mn = md + 1;
1034 else
1035 mx = md;
1038 LINEMAPS_MACRO_CACHE (set) = mx;
1039 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
1040 linemap_assert (MAP_START_LOCATION (result) <= line);
1042 return result;
1045 /* Return TRUE if MAP encodes locations coming from a macro
1046 replacement-list at macro expansion point. */
1048 bool
1049 linemap_macro_expansion_map_p (const struct line_map *map)
1051 if (!map)
1052 return false;
1053 return (map->reason == LC_ENTER_MACRO);
1056 /* If LOCATION is the locus of a token in a replacement-list of a
1057 macro expansion return the location of the macro expansion point.
1059 Read the comments of struct line_map and struct line_map_macro in
1060 line-map.h to understand what a macro expansion point is. */
1062 static source_location
1063 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1064 source_location location ATTRIBUTE_UNUSED)
1066 linemap_assert (linemap_macro_expansion_map_p (map)
1067 && location >= MAP_START_LOCATION (map));
1069 /* Make sure LOCATION is correct. */
1070 linemap_assert ((location - MAP_START_LOCATION (map))
1071 < MACRO_MAP_NUM_MACRO_TOKENS (map));
1073 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1076 /* LOCATION is the source location of a token that belongs to a macro
1077 replacement-list as part of the macro expansion denoted by MAP.
1079 Return the location of the token at the definition point of the
1080 macro. */
1082 static source_location
1083 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1084 source_location location)
1086 unsigned token_no;
1088 linemap_assert (linemap_macro_expansion_map_p (map)
1089 && location >= MAP_START_LOCATION (map));
1090 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1092 token_no = location - MAP_START_LOCATION (map);
1093 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1095 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1097 return location;
1100 /* If LOCATION is the locus of a token that is an argument of a
1101 function-like macro M and appears in the expansion of M, return the
1102 locus of that argument in the context of the caller of M.
1104 In other words, this returns the xI location presented in the
1105 comments of line_map_macro above. */
1106 source_location
1107 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1108 const line_map_macro* map,
1109 source_location location)
1111 unsigned token_no;
1113 if (IS_ADHOC_LOC (location))
1114 location = get_location_from_adhoc_loc (set, location);
1116 linemap_assert (linemap_macro_expansion_map_p (map)
1117 && location >= MAP_START_LOCATION (map));
1118 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1119 linemap_assert (!IS_ADHOC_LOC (location));
1121 token_no = location - MAP_START_LOCATION (map);
1122 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1124 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1126 return location;
1129 /* Return the source line number corresponding to source location
1130 LOCATION. SET is the line map set LOCATION comes from. If
1131 LOCATION is the source location of token that is part of the
1132 replacement-list of a macro expansion return the line number of the
1133 macro expansion point. */
1136 linemap_get_expansion_line (struct line_maps *set,
1137 source_location location)
1139 const line_map_ordinary *map = NULL;
1141 if (IS_ADHOC_LOC (location))
1142 location = set->location_adhoc_data_map.data[location
1143 & MAX_SOURCE_LOCATION].locus;
1145 if (location < RESERVED_LOCATION_COUNT)
1146 return 0;
1148 location =
1149 linemap_macro_loc_to_exp_point (set, location, &map);
1151 return SOURCE_LINE (map, location);
1154 /* Return the path of the file corresponding to source code location
1155 LOCATION.
1157 If LOCATION is the source location of token that is part of the
1158 replacement-list of a macro expansion return the file path of the
1159 macro expansion point.
1161 SET is the line map set LOCATION comes from. */
1163 const char*
1164 linemap_get_expansion_filename (struct line_maps *set,
1165 source_location location)
1167 const struct line_map_ordinary *map = NULL;
1169 if (IS_ADHOC_LOC (location))
1170 location = set->location_adhoc_data_map.data[location
1171 & MAX_SOURCE_LOCATION].locus;
1173 if (location < RESERVED_LOCATION_COUNT)
1174 return NULL;
1176 location =
1177 linemap_macro_loc_to_exp_point (set, location, &map);
1179 return LINEMAP_FILE (map);
1182 /* Return the name of the macro associated to MACRO_MAP. */
1184 const char*
1185 linemap_map_get_macro_name (const line_map_macro *macro_map)
1187 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1188 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1191 /* Return a positive value if LOCATION is the locus of a token that is
1192 located in a system header, O otherwise. It returns 1 if LOCATION
1193 is the locus of a token that is located in a system header, and 2
1194 if LOCATION is the locus of a token located in a C system header
1195 that therefore needs to be extern "C" protected in C++.
1197 Note that this function returns 1 if LOCATION belongs to a token
1198 that is part of a macro replacement-list defined in a system
1199 header, but expanded in a non-system file. */
1202 linemap_location_in_system_header_p (struct line_maps *set,
1203 source_location location)
1205 const struct line_map *map = NULL;
1207 if (IS_ADHOC_LOC (location))
1208 location = set->location_adhoc_data_map.data[location
1209 & MAX_SOURCE_LOCATION].locus;
1211 if (location < RESERVED_LOCATION_COUNT)
1212 return false;
1214 /* Let's look at where the token for LOCATION comes from. */
1215 while (true)
1217 map = linemap_lookup (set, location);
1218 if (map != NULL)
1220 if (!linemap_macro_expansion_map_p (map))
1221 /* It's a normal token. */
1222 return LINEMAP_SYSP (linemap_check_ordinary (map));
1223 else
1225 const line_map_macro *macro_map = linemap_check_macro (map);
1227 /* It's a token resulting from a macro expansion. */
1228 source_location loc =
1229 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
1230 if (loc < RESERVED_LOCATION_COUNT)
1231 /* This token might come from a built-in macro. Let's
1232 look at where that macro got expanded. */
1233 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1234 else
1235 location = loc;
1238 else
1239 break;
1241 return false;
1244 /* Return TRUE if LOCATION is a source code location of a token that is part of
1245 a macro expansion, FALSE otherwise. */
1247 bool
1248 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1249 source_location location)
1251 if (IS_ADHOC_LOC (location))
1252 location = set->location_adhoc_data_map.data[location
1253 & MAX_SOURCE_LOCATION].locus;
1255 linemap_assert (location <= MAX_SOURCE_LOCATION
1256 && (set->highest_location
1257 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
1258 if (set == NULL)
1259 return false;
1260 return (location > set->highest_location);
1263 /* Given two virtual locations *LOC0 and *LOC1, return the first
1264 common macro map in their macro expansion histories. Return NULL
1265 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1266 virtual location of the token inside the resulting macro. */
1268 static const struct line_map*
1269 first_map_in_common_1 (struct line_maps *set,
1270 source_location *loc0,
1271 source_location *loc1)
1273 source_location l0 = *loc0, l1 = *loc1;
1274 const struct line_map *map0 = linemap_lookup (set, l0),
1275 *map1 = linemap_lookup (set, l1);
1277 while (linemap_macro_expansion_map_p (map0)
1278 && linemap_macro_expansion_map_p (map1)
1279 && (map0 != map1))
1281 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1283 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1284 l0);
1285 map0 = linemap_lookup (set, l0);
1287 else
1289 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1290 l1);
1291 map1 = linemap_lookup (set, l1);
1295 if (map0 == map1)
1297 *loc0 = l0;
1298 *loc1 = l1;
1299 return map0;
1301 return NULL;
1304 /* Given two virtual locations LOC0 and LOC1, return the first common
1305 macro map in their macro expansion histories. Return NULL if no
1306 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1307 virtual location of the token inside the resulting macro, upon
1308 return of a non-NULL result. */
1310 static const struct line_map*
1311 first_map_in_common (struct line_maps *set,
1312 source_location loc0,
1313 source_location loc1,
1314 source_location *res_loc0,
1315 source_location *res_loc1)
1317 *res_loc0 = loc0;
1318 *res_loc1 = loc1;
1320 return first_map_in_common_1 (set, res_loc0, res_loc1);
1323 /* Return a positive value if PRE denotes the location of a token that
1324 comes before the token of POST, 0 if PRE denotes the location of
1325 the same token as the token for POST, and a negative value
1326 otherwise. */
1329 linemap_compare_locations (struct line_maps *set,
1330 source_location pre,
1331 source_location post)
1333 bool pre_virtual_p, post_virtual_p;
1334 source_location l0 = pre, l1 = post;
1336 if (IS_ADHOC_LOC (l0))
1337 l0 = get_location_from_adhoc_loc (set, l0);
1338 if (IS_ADHOC_LOC (l1))
1339 l1 = get_location_from_adhoc_loc (set, l1);
1341 if (l0 == l1)
1342 return 0;
1344 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1345 l0 = linemap_resolve_location (set, l0,
1346 LRK_MACRO_EXPANSION_POINT,
1347 NULL);
1349 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1350 l1 = linemap_resolve_location (set, l1,
1351 LRK_MACRO_EXPANSION_POINT,
1352 NULL);
1354 if (l0 == l1
1355 && pre_virtual_p
1356 && post_virtual_p)
1358 /* So pre and post represent two tokens that are present in a
1359 same macro expansion. Let's see if the token for pre was
1360 before the token for post in that expansion. */
1361 unsigned i0, i1;
1362 const struct line_map *map =
1363 first_map_in_common (set, pre, post, &l0, &l1);
1365 if (map == NULL)
1366 /* This should not be possible. */
1367 abort ();
1369 i0 = l0 - MAP_START_LOCATION (map);
1370 i1 = l1 - MAP_START_LOCATION (map);
1371 return i1 - i0;
1374 if (IS_ADHOC_LOC (l0))
1375 l0 = get_location_from_adhoc_loc (set, l0);
1376 if (IS_ADHOC_LOC (l1))
1377 l1 = get_location_from_adhoc_loc (set, l1);
1379 return l1 - l0;
1382 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1384 static void
1385 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1387 unsigned int i = set->depth;
1389 while (--i)
1390 putc ('.', stderr);
1392 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1395 /* Return the spelling location of the token wherever it comes from,
1396 whether part of a macro definition or not.
1398 This is a subroutine for linemap_resolve_location. */
1400 static source_location
1401 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1402 source_location location,
1403 const line_map_ordinary **original_map)
1405 struct line_map *map;
1406 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1408 while (true)
1410 map = const_cast <line_map *> (linemap_lookup (set, location));
1411 if (!linemap_macro_expansion_map_p (map))
1412 break;
1414 location
1415 = linemap_macro_map_loc_unwind_toward_spelling
1416 (set, linemap_check_macro (map),
1417 location);
1420 if (original_map)
1421 *original_map = linemap_check_ordinary (map);
1422 return location;
1425 /* If LOCATION is the source location of a token that belongs to a
1426 macro replacement-list -- as part of a macro expansion -- then
1427 return the location of the token at the definition point of the
1428 macro. Otherwise, return LOCATION. SET is the set of maps
1429 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1430 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1431 returned location comes from.
1433 This is a subroutine of linemap_resolve_location. */
1435 static source_location
1436 linemap_macro_loc_to_def_point (struct line_maps *set,
1437 source_location location,
1438 const line_map_ordinary **original_map)
1440 struct line_map *map;
1442 if (IS_ADHOC_LOC (location))
1443 location = set->location_adhoc_data_map.data[location
1444 & MAX_SOURCE_LOCATION].locus;
1446 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1448 while (true)
1450 map = const_cast <line_map *> (linemap_lookup (set, location));
1451 if (!linemap_macro_expansion_map_p (map))
1452 break;
1454 location =
1455 linemap_macro_map_loc_to_def_point (linemap_check_macro (map),
1456 location);
1459 if (original_map)
1460 *original_map = linemap_check_ordinary (map);
1461 return location;
1464 /* If LOCATION is the source location of a token that belongs to a
1465 macro replacement-list -- at a macro expansion point -- then return
1466 the location of the topmost expansion point of the macro. We say
1467 topmost because if we are in the context of a nested macro
1468 expansion, the function returns the source location of the first
1469 macro expansion that triggered the nested expansions.
1471 Otherwise, return LOCATION. SET is the set of maps location come
1472 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1473 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1474 location comes from.
1476 This is a subroutine of linemap_resolve_location. */
1478 static source_location
1479 linemap_macro_loc_to_exp_point (struct line_maps *set,
1480 source_location location,
1481 const line_map_ordinary **original_map)
1483 struct line_map *map;
1485 if (IS_ADHOC_LOC (location))
1486 location = set->location_adhoc_data_map.data[location
1487 & MAX_SOURCE_LOCATION].locus;
1489 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1491 while (true)
1493 map = const_cast <line_map *> (linemap_lookup (set, location));
1494 if (!linemap_macro_expansion_map_p (map))
1495 break;
1496 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1497 location);
1500 if (original_map)
1501 *original_map = linemap_check_ordinary (map);
1502 return location;
1505 /* Resolve a virtual location into either a spelling location, an
1506 expansion point location or a token argument replacement point
1507 location. Return the map that encodes the virtual location as well
1508 as the resolved location.
1510 If LOC is *NOT* the location of a token resulting from the
1511 expansion of a macro, then the parameter LRK (which stands for
1512 Location Resolution Kind) is ignored and the resulting location
1513 just equals the one given in argument.
1515 Now if LOC *IS* the location of a token resulting from the
1516 expansion of a macro, this is what happens.
1518 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1519 -------------------------------
1521 The virtual location is resolved to the first macro expansion point
1522 that led to this macro expansion.
1524 * If LRK is set to LRK_SPELLING_LOCATION
1525 -------------------------------------
1527 The virtual location is resolved to the locus where the token has
1528 been spelled in the source. This can follow through all the macro
1529 expansions that led to the token.
1531 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1532 --------------------------------------
1534 The virtual location is resolved to the locus of the token in the
1535 context of the macro definition.
1537 If LOC is the locus of a token that is an argument of a
1538 function-like macro [replacing a parameter in the replacement list
1539 of the macro] the virtual location is resolved to the locus of the
1540 parameter that is replaced, in the context of the definition of the
1541 macro.
1543 If LOC is the locus of a token that is not an argument of a
1544 function-like macro, then the function behaves as if LRK was set to
1545 LRK_SPELLING_LOCATION.
1547 If MAP is not NULL, *MAP is set to the map encoding the
1548 returned location. Note that if the returned location wasn't originally
1549 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1550 resolves to a location reserved for the client code, like
1551 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1553 source_location
1554 linemap_resolve_location (struct line_maps *set,
1555 source_location loc,
1556 enum location_resolution_kind lrk,
1557 const line_map_ordinary **map)
1559 source_location locus = loc;
1560 if (IS_ADHOC_LOC (loc))
1561 locus = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1563 if (locus < RESERVED_LOCATION_COUNT)
1565 /* A reserved location wasn't encoded in a map. Let's return a
1566 NULL map here, just like what linemap_ordinary_map_lookup
1567 does. */
1568 if (map)
1569 *map = NULL;
1570 return loc;
1573 switch (lrk)
1575 case LRK_MACRO_EXPANSION_POINT:
1576 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1577 break;
1578 case LRK_SPELLING_LOCATION:
1579 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1580 break;
1581 case LRK_MACRO_DEFINITION_LOCATION:
1582 loc = linemap_macro_loc_to_def_point (set, loc, map);
1583 break;
1584 default:
1585 abort ();
1587 return loc;
1590 /* TRUE if LOCATION is a source code location of a token that is part of the
1591 definition of a macro, FALSE otherwise. */
1593 bool
1594 linemap_location_from_macro_definition_p (struct line_maps *set,
1595 source_location loc)
1597 if (IS_ADHOC_LOC (loc))
1598 loc = get_location_from_adhoc_loc (set, loc);
1600 if (!linemap_location_from_macro_expansion_p (set, loc))
1601 return false;
1603 while (true)
1605 const struct line_map_macro *map
1606 = linemap_check_macro (linemap_lookup (set, loc));
1608 source_location s_loc
1609 = linemap_macro_map_loc_unwind_toward_spelling (set, map, loc);
1610 if (linemap_location_from_macro_expansion_p (set, s_loc))
1611 loc = s_loc;
1612 else
1614 source_location def_loc
1615 = linemap_macro_map_loc_to_def_point (map, loc);
1616 return s_loc == def_loc;
1622 Suppose that LOC is the virtual location of a token T coming from
1623 the expansion of a macro M. This function then steps up to get the
1624 location L of the point where M got expanded. If L is a spelling
1625 location inside a macro expansion M', then this function returns
1626 the locus of the point where M' was expanded. Said otherwise, this
1627 function returns the location of T in the context that triggered
1628 the expansion of M.
1630 *LOC_MAP must be set to the map of LOC. This function then sets it
1631 to the map of the returned location. */
1633 source_location
1634 linemap_unwind_toward_expansion (struct line_maps *set,
1635 source_location loc,
1636 const struct line_map **map)
1638 source_location resolved_location;
1639 const line_map_macro *macro_map = linemap_check_macro (*map);
1640 const struct line_map *resolved_map;
1642 if (IS_ADHOC_LOC (loc))
1643 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1645 resolved_location =
1646 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1647 resolved_map = linemap_lookup (set, resolved_location);
1649 if (!linemap_macro_expansion_map_p (resolved_map))
1651 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1652 resolved_map = linemap_lookup (set, resolved_location);
1655 *map = resolved_map;
1656 return resolved_location;
1659 /* If LOC is the virtual location of a token coming from the expansion
1660 of a macro M and if its spelling location is reserved (e.g, a
1661 location for a built-in token), then this function unwinds (using
1662 linemap_unwind_toward_expansion) the location until a location that
1663 is not reserved and is not in a system header is reached. In other
1664 words, this unwinds the reserved location until a location that is
1665 in real source code is reached.
1667 Otherwise, if the spelling location for LOC is not reserved or if
1668 LOC doesn't come from the expansion of a macro, the function
1669 returns LOC as is and *MAP is not touched.
1671 *MAP is set to the map of the returned location if the later is
1672 different from LOC. */
1673 source_location
1674 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1675 source_location loc,
1676 const struct line_map **map)
1678 source_location resolved_loc;
1679 const struct line_map *map0 = NULL;
1680 const line_map_ordinary *map1 = NULL;
1682 if (IS_ADHOC_LOC (loc))
1683 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1685 map0 = linemap_lookup (set, loc);
1686 if (!linemap_macro_expansion_map_p (map0))
1687 return loc;
1689 resolved_loc = linemap_resolve_location (set, loc,
1690 LRK_SPELLING_LOCATION,
1691 &map1);
1693 if (resolved_loc >= RESERVED_LOCATION_COUNT
1694 && !LINEMAP_SYSP (map1))
1695 return loc;
1697 while (linemap_macro_expansion_map_p (map0)
1698 && (resolved_loc < RESERVED_LOCATION_COUNT
1699 || LINEMAP_SYSP (map1)))
1701 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1702 resolved_loc = linemap_resolve_location (set, loc,
1703 LRK_SPELLING_LOCATION,
1704 &map1);
1707 if (map != NULL)
1708 *map = map0;
1709 return loc;
1712 /* Expand source code location LOC and return a user readable source
1713 code location. LOC must be a spelling (non-virtual) location. If
1714 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1715 location is returned. */
1717 expanded_location
1718 linemap_expand_location (struct line_maps *set,
1719 const struct line_map *map,
1720 source_location loc)
1723 expanded_location xloc;
1725 memset (&xloc, 0, sizeof (xloc));
1726 if (IS_ADHOC_LOC (loc))
1728 xloc.data
1729 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1730 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1733 if (loc < RESERVED_LOCATION_COUNT)
1734 /* The location for this token wasn't generated from a line map.
1735 It was probably a location for a builtin token, chosen by some
1736 client code. Let's not try to expand the location in that
1737 case. */;
1738 else if (map == NULL)
1739 /* We shouldn't be getting a NULL map with a location that is not
1740 reserved by the client code. */
1741 abort ();
1742 else
1744 /* MAP must be an ordinary map and LOC must be non-virtual,
1745 encoded into this map, obviously; the accessors used on MAP
1746 below ensure it is ordinary. Let's just assert the
1747 non-virtualness of LOC here. */
1748 if (linemap_location_from_macro_expansion_p (set, loc))
1749 abort ();
1751 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1753 xloc.file = LINEMAP_FILE (ord_map);
1754 xloc.line = SOURCE_LINE (ord_map, loc);
1755 xloc.column = SOURCE_COLUMN (ord_map, loc);
1756 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1759 return xloc;
1763 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1764 is NULL, use stderr. IS_MACRO is true if the caller wants to
1765 dump a macro map, false otherwise. */
1767 void
1768 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1770 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1771 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1772 "LC_ENTER_MACRO" };
1773 const char *reason;
1774 const line_map *map;
1776 if (stream == NULL)
1777 stream = stderr;
1779 if (!is_macro)
1780 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1781 else
1782 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1784 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1786 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1787 ix, (void *) map, map->start_location, reason,
1788 ((!is_macro
1789 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1790 ? "yes" : "no"));
1791 if (!is_macro)
1793 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1794 unsigned includer_ix;
1795 const line_map_ordinary *includer_map;
1797 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
1798 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1799 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1800 : NULL;
1802 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1803 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1804 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1805 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1807 else
1809 const line_map_macro *macro_map = linemap_check_macro (map);
1810 fprintf (stream, "Macro: %s (%u tokens)\n",
1811 linemap_map_get_macro_name (macro_map),
1812 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1815 fprintf (stream, "\n");
1819 /* Dump debugging information about source location LOC into the file
1820 stream STREAM. SET is the line map set LOC comes from. */
1822 void
1823 linemap_dump_location (struct line_maps *set,
1824 source_location loc,
1825 FILE *stream)
1827 const line_map_ordinary *map;
1828 source_location location;
1829 const char *path = "", *from = "";
1830 int l = -1, c = -1, s = -1, e = -1;
1832 if (IS_ADHOC_LOC (loc))
1833 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1835 if (loc == 0)
1836 return;
1838 location =
1839 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1841 if (map == NULL)
1842 /* Only reserved locations can be tolerated in this case. */
1843 linemap_assert (location < RESERVED_LOCATION_COUNT);
1844 else
1846 path = LINEMAP_FILE (map);
1847 l = SOURCE_LINE (map, location);
1848 c = SOURCE_COLUMN (map, location);
1849 s = LINEMAP_SYSP (map) != 0;
1850 e = location != loc;
1851 if (e)
1852 from = "N/A";
1853 else
1854 from = (INCLUDED_FROM (set, map))
1855 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1856 : "<NULL>";
1859 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1860 E: macro expansion?, LOC: original location, R: resolved location */
1861 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1862 path, from, l, c, s, (void*)map, e, loc, location);
1865 /* Return the highest location emitted for a given file for which
1866 there is a line map in SET. FILE_NAME is the file name to
1867 consider. If the function returns TRUE, *LOC is set to the highest
1868 location emitted for that file. */
1870 bool
1871 linemap_get_file_highest_location (struct line_maps *set,
1872 const char *file_name,
1873 source_location *loc)
1875 /* If the set is empty or no ordinary map has been created then
1876 there is no file to look for ... */
1877 if (set == NULL || set->info_ordinary.used == 0)
1878 return false;
1880 /* Now look for the last ordinary map created for FILE_NAME. */
1881 int i;
1882 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1884 const char *fname = set->info_ordinary.maps[i].to_file;
1885 if (fname && !filename_cmp (fname, file_name))
1886 break;
1889 if (i < 0)
1890 return false;
1892 /* The highest location for a given map is either the starting
1893 location of the next map minus one, or -- if the map is the
1894 latest one -- the highest location of the set. */
1895 source_location result;
1896 if (i == (int) set->info_ordinary.used - 1)
1897 result = set->highest_location;
1898 else
1899 result = set->info_ordinary.maps[i + 1].start_location - 1;
1901 *loc = result;
1902 return true;
1905 /* Compute and return statistics about the memory consumption of some
1906 parts of the line table SET. */
1908 void
1909 linemap_get_statistics (struct line_maps *set,
1910 struct linemap_stats *s)
1912 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1913 macro_maps_allocated_size, macro_maps_used_size,
1914 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1916 const line_map_macro *cur_map;
1918 ordinary_maps_allocated_size =
1919 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1921 ordinary_maps_used_size =
1922 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1924 macro_maps_allocated_size =
1925 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1927 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1928 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1929 ++cur_map)
1931 unsigned i;
1933 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1935 macro_maps_locations_size +=
1936 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1938 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1940 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1941 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1942 duplicated_macro_maps_locations_size +=
1943 sizeof (source_location);
1947 macro_maps_used_size =
1948 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1950 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1951 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1952 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1953 s->ordinary_maps_used_size = ordinary_maps_used_size;
1954 s->num_expanded_macros = num_expanded_macros_counter;
1955 s->num_macro_tokens = num_macro_tokens_counter;
1956 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1957 s->macro_maps_allocated_size = macro_maps_allocated_size;
1958 s->macro_maps_locations_size = macro_maps_locations_size;
1959 s->macro_maps_used_size = macro_maps_used_size;
1960 s->duplicated_macro_maps_locations_size =
1961 duplicated_macro_maps_locations_size;
1962 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1963 * sizeof (struct location_adhoc_data));
1964 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
1968 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1969 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1970 specifies how many macro maps to dump. */
1972 void
1973 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1974 unsigned int num_macro)
1976 unsigned int i;
1978 if (set == NULL)
1979 return;
1981 if (stream == NULL)
1982 stream = stderr;
1984 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1985 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1986 fprintf (stream, "Include stack depth: %d\n", set->depth);
1987 fprintf (stream, "Highest location: %u\n", set->highest_location);
1989 if (num_ordinary)
1991 fprintf (stream, "\nOrdinary line maps\n");
1992 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1993 linemap_dump (stream, set, i, false);
1994 fprintf (stream, "\n");
1997 if (num_macro)
1999 fprintf (stream, "\nMacro line maps\n");
2000 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
2001 linemap_dump (stream, set, i, true);
2002 fprintf (stream, "\n");
2006 /* struct source_range. */
2008 /* Is there any part of this range on the given line? */
2010 bool
2011 source_range::intersects_line_p (const char *file, int line) const
2013 expanded_location exploc_start
2014 = linemap_client_expand_location_to_spelling_point (m_start);
2015 if (file != exploc_start.file)
2016 return false;
2017 if (line < exploc_start.line)
2018 return false;
2019 expanded_location exploc_finish
2020 = linemap_client_expand_location_to_spelling_point (m_finish);
2021 if (file != exploc_finish.file)
2022 return false;
2023 if (line > exploc_finish.line)
2024 return false;
2025 return true;
2028 /* class rich_location. */
2030 /* Construct a rich_location with location LOC as its initial range. */
2032 rich_location::rich_location (line_maps *set, source_location loc) :
2033 m_line_table (set),
2034 m_ranges (),
2035 m_column_override (0),
2036 m_have_expanded_location (false),
2037 m_fixit_hints (),
2038 m_seen_impossible_fixit (false)
2040 add_range (loc, true);
2043 /* The destructor for class rich_location. */
2045 rich_location::~rich_location ()
2047 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2048 delete get_fixit_hint (i);
2051 /* Get location IDX within this rich_location. */
2053 source_location
2054 rich_location::get_loc (unsigned int idx) const
2056 const location_range *locrange = get_range (idx);
2057 return locrange->m_loc;
2060 /* Get range IDX within this rich_location. */
2062 const location_range *
2063 rich_location::get_range (unsigned int idx) const
2065 return &m_ranges[idx];
2068 /* Mutable access to range IDX within this rich_location. */
2070 location_range *
2071 rich_location::get_range (unsigned int idx)
2073 return &m_ranges[idx];
2076 /* Expand location IDX within this rich_location. */
2077 /* Get an expanded_location for this rich_location's primary
2078 location. */
2080 expanded_location
2081 rich_location::get_expanded_location (unsigned int idx)
2083 if (idx == 0)
2085 /* Cache the expansion of the primary location. */
2086 if (!m_have_expanded_location)
2088 m_expanded_location
2089 = linemap_client_expand_location_to_spelling_point (get_loc (0));
2090 if (m_column_override)
2091 m_expanded_location.column = m_column_override;
2092 m_have_expanded_location = true;
2095 return m_expanded_location;
2097 else
2098 return linemap_client_expand_location_to_spelling_point (get_loc (idx));
2101 /* Set the column of the primary location, with 0 meaning
2102 "don't override it". */
2104 void
2105 rich_location::override_column (int column)
2107 m_column_override = column;
2108 m_have_expanded_location = false;
2111 /* Add the given range. */
2113 void
2114 rich_location::add_range (source_location loc, bool show_caret_p)
2116 location_range range;
2117 range.m_loc = loc;
2118 range.m_show_caret_p = show_caret_p;
2119 m_ranges.push (range);
2122 /* Add or overwrite the location given by IDX, setting its location to LOC,
2123 and setting its "should my caret be printed" flag to SHOW_CARET_P.
2125 It must either overwrite an existing location, or add one *exactly* on
2126 the end of the array.
2128 This is primarily for use by gcc when implementing diagnostic format
2129 decoders e.g.
2130 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2131 (which writes the source location of a tree back into location 0 of
2132 the rich_location), and
2133 - the "%C" and "%L" format codes in the Fortran frontend. */
2135 void
2136 rich_location::set_range (line_maps * /*set*/, unsigned int idx,
2137 source_location loc, bool show_caret_p)
2139 /* We can either overwrite an existing range, or add one exactly
2140 on the end of the array. */
2141 linemap_assert (idx <= m_ranges.count ());
2143 if (idx == m_ranges.count ())
2144 add_range (loc, show_caret_p);
2145 else
2147 location_range *locrange = get_range (idx);
2148 locrange->m_loc = loc;
2149 locrange->m_show_caret_p = show_caret_p;
2152 if (idx == 0)
2153 /* Mark any cached value here as dirty. */
2154 m_have_expanded_location = false;
2157 /* Methods for adding insertion fix-it hints. */
2159 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2160 immediately before the primary range's start location. */
2162 void
2163 rich_location::add_fixit_insert_before (const char *new_content)
2165 add_fixit_insert_before (get_loc (), new_content);
2168 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2169 immediately before the start of WHERE. */
2171 void
2172 rich_location::add_fixit_insert_before (source_location where,
2173 const char *new_content)
2175 source_location start = get_range_from_loc (m_line_table, where).m_start;
2177 if (reject_impossible_fixit (start))
2178 return;
2179 /* We do not yet support newlines within fix-it hints. */
2180 if (strchr (new_content, '\n'))
2182 stop_supporting_fixits ();
2183 return;
2185 add_fixit (new fixit_insert (start, new_content));
2188 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2189 immediately after the primary range's end-point. */
2191 void
2192 rich_location::add_fixit_insert_after (const char *new_content)
2194 add_fixit_insert_after (get_loc (), new_content);
2197 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2198 immediately after the end-point of WHERE. */
2200 void
2201 rich_location::add_fixit_insert_after (source_location where,
2202 const char *new_content)
2204 source_location finish = get_range_from_loc (m_line_table, where).m_finish;
2206 if (reject_impossible_fixit (finish))
2207 return;
2209 source_location next_loc
2210 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2212 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2213 its input value. */
2214 if (next_loc == finish)
2216 stop_supporting_fixits ();
2217 return;
2220 add_fixit (new fixit_insert (next_loc, new_content));
2223 /* Methods for adding removal fix-it hints. */
2225 /* Add a fixit-hint, suggesting removal of the content covered
2226 by range 0. */
2228 void
2229 rich_location::add_fixit_remove ()
2231 add_fixit_remove (get_loc ());
2234 /* Add a fixit-hint, suggesting removal of the content between
2235 the start and finish of WHERE. */
2237 void
2238 rich_location::add_fixit_remove (source_location where)
2240 source_range range = get_range_from_loc (m_line_table, where);
2241 add_fixit_remove (range);
2244 /* Add a fixit-hint, suggesting removal of the content at
2245 SRC_RANGE. */
2247 void
2248 rich_location::add_fixit_remove (source_range src_range)
2250 add_fixit_replace (src_range, "");
2253 /* Return true iff A is in the column directly before B, on the
2254 same line of the same source file. */
2256 static bool
2257 column_before_p (line_maps *set, source_location a, source_location b)
2259 if (IS_ADHOC_LOC (a))
2260 a = get_location_from_adhoc_loc (set, a);
2261 if (IS_ADHOC_LOC (b))
2262 b = get_location_from_adhoc_loc (set, b);
2264 /* They must both be in ordinary maps. */
2265 const struct line_map *linemap_a = linemap_lookup (set, a);
2266 if (linemap_macro_expansion_map_p (linemap_a))
2267 return false;
2268 const struct line_map *linemap_b = linemap_lookup (set, b);
2269 if (linemap_macro_expansion_map_p (linemap_b))
2270 return false;
2272 /* To be on the same line, they must be in the same ordinary map. */
2273 if (linemap_a != linemap_b)
2274 return false;
2276 linenum_type line_a
2277 = SOURCE_LINE (linemap_check_ordinary (linemap_a), a);
2278 linenum_type line_b
2279 = SOURCE_LINE (linemap_check_ordinary (linemap_b), b);
2280 if (line_a != line_b)
2281 return false;
2283 linenum_type column_a
2284 = SOURCE_COLUMN (linemap_check_ordinary (linemap_a), a);
2285 linenum_type column_b
2286 = SOURCE_COLUMN (linemap_check_ordinary (linemap_b), b);
2288 return column_b == column_a + 1;
2291 /* Add a fixit-hint, suggesting replacement of the content covered
2292 by range 0 with NEW_CONTENT. */
2294 void
2295 rich_location::add_fixit_replace (const char *new_content)
2297 add_fixit_replace (get_loc (), new_content);
2300 /* Methods for adding "replace" fix-it hints. */
2302 /* Add a fixit-hint, suggesting replacement of the content between
2303 the start and finish of WHERE with NEW_CONTENT. */
2305 void
2306 rich_location::add_fixit_replace (source_location where,
2307 const char *new_content)
2309 source_range range = get_range_from_loc (m_line_table, where);
2310 add_fixit_replace (range, new_content);
2313 /* Add a fixit-hint, suggesting replacement of the content at
2314 SRC_RANGE with NEW_CONTENT. */
2316 void
2317 rich_location::add_fixit_replace (source_range src_range,
2318 const char *new_content)
2320 src_range.m_start = get_pure_location (m_line_table, src_range.m_start);
2321 src_range.m_finish = get_pure_location (m_line_table, src_range.m_finish);
2323 if (reject_impossible_fixit (src_range.m_start))
2324 return;
2325 if (reject_impossible_fixit (src_range.m_finish))
2326 return;
2328 /* We do not yet support newlines within fix-it hints. */
2329 if (strchr (new_content, '\n'))
2331 stop_supporting_fixits ();
2332 return;
2335 /* Consolidate neighboring fixits. */
2336 fixit_hint *prev = get_last_fixit_hint ();
2337 if (prev)
2338 if (prev->maybe_append_replace (m_line_table, src_range, new_content))
2339 return;
2341 add_fixit (new fixit_replace (src_range, new_content));
2344 /* Get the last fix-it hint within this rich_location, or NULL if none. */
2346 fixit_hint *
2347 rich_location::get_last_fixit_hint () const
2349 if (m_fixit_hints.count () > 0)
2350 return get_fixit_hint (m_fixit_hints.count () - 1);
2351 else
2352 return NULL;
2355 /* If WHERE is an "awkward" location, then mark this rich_location as not
2356 supporting fixits, purging any thay were already added, and return true.
2358 Otherwise (the common case), return false. */
2360 bool
2361 rich_location::reject_impossible_fixit (source_location where)
2363 /* Fix-its within a rich_location should either all be suggested, or
2364 none of them should be suggested.
2365 Once we've rejected a fixit, we reject any more, even those
2366 with reasonable locations. */
2367 if (m_seen_impossible_fixit)
2368 return true;
2370 if (where <= LINE_MAP_MAX_LOCATION_WITH_COLS)
2371 /* WHERE is a reasonable location for a fix-it; don't reject it. */
2372 return false;
2374 /* Otherwise we have an attempt to add a fix-it with an "awkward"
2375 location: either one that we can't obtain column information
2376 for (within an ordinary map), or one within a macro expansion. */
2377 stop_supporting_fixits ();
2378 return true;
2381 /* Mark this rich_location as not supporting fixits, purging any that were
2382 already added. */
2384 void
2385 rich_location::stop_supporting_fixits ()
2387 m_seen_impossible_fixit = true;
2389 /* Purge the rich_location of any fix-its that were already added. */
2390 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2391 delete get_fixit_hint (i);
2392 m_fixit_hints.truncate (0);
2395 /* Add HINT to the fix-it hints in this rich_location. */
2397 void
2398 rich_location::add_fixit (fixit_hint *hint)
2400 m_fixit_hints.push (hint);
2403 /* class fixit_insert. */
2405 fixit_insert::fixit_insert (source_location where,
2406 const char *new_content)
2407 : m_where (where),
2408 m_bytes (xstrdup (new_content)),
2409 m_len (strlen (new_content))
2413 fixit_insert::~fixit_insert ()
2415 free (m_bytes);
2418 /* Implementation of fixit_hint::affects_line_p for fixit_insert. */
2420 bool
2421 fixit_insert::affects_line_p (const char *file, int line) const
2423 expanded_location exploc
2424 = linemap_client_expand_location_to_spelling_point (m_where);
2425 if (file == exploc.file)
2426 if (line == exploc.line)
2427 return true;
2428 return false;
2431 /* Implementation of maybe_append_replace for fixit_insert. Reject
2432 the attempt to consolidate fix-its. */
2434 bool
2435 fixit_insert::maybe_append_replace (line_maps *, source_range, const char *)
2437 return false;
2440 /* class fixit_replace. */
2442 fixit_replace::fixit_replace (source_range src_range,
2443 const char *new_content)
2444 : m_src_range (src_range),
2445 m_bytes (xstrdup (new_content)),
2446 m_len (strlen (new_content))
2450 fixit_replace::~fixit_replace ()
2452 free (m_bytes);
2455 /* Implementation of fixit_hint::affects_line_p for fixit_replace. */
2457 bool
2458 fixit_replace::affects_line_p (const char *file, int line) const
2460 return m_src_range.intersects_line_p (file, line);
2463 /* Implementation of maybe_append_replace for fixit_replace. If
2464 possible, merge the new replacement into this one and return true.
2465 Otherwise return false. */
2467 bool
2468 fixit_replace::maybe_append_replace (line_maps *set,
2469 source_range src_range,
2470 const char *new_content)
2472 /* Does SRC_RANGE start immediately after this one finishes? */
2473 if (!column_before_p (set, m_src_range.m_finish, src_range.m_start))
2474 return false;
2476 /* We have neighboring replacements; merge them. */
2477 m_src_range.m_finish = src_range.m_finish;
2478 size_t extra_len = strlen (new_content);
2479 m_bytes = (char *)xrealloc (m_bytes, m_len + extra_len + 1);
2480 memcpy (m_bytes + m_len, new_content, extra_len);
2481 m_len += extra_len;
2482 m_bytes[m_len] = '\0';
2483 return true;