Start adding target-specific selftests
[official-gcc.git] / libcpp / line-map.c
blobc98ee45965a60010f99f6c65eaed594a8aa8e896
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2016 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>.
18 In other words, you are welcome to use, share and improve this program.
19 You are forbidden to forbid anyone else to use, share and improve
20 what you give them. Help stamp out software-hoarding! */
22 #include "config.h"
23 #include "system.h"
24 #include "line-map.h"
25 #include "cpplib.h"
26 #include "internal.h"
27 #include "hashtab.h"
29 /* Do not track column numbers higher than this one. As a result, the
30 range of column_bits is [12, 18] (or 0 if column numbers are
31 disabled). */
32 const unsigned int LINE_MAP_MAX_COLUMN_NUMBER = (1U << 12);
34 /* 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)
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 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
820 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
823 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
824 r = r + (to_column << map->m_range_bits);
825 if (r >= set->highest_location)
826 set->highest_location = r;
827 return r;
830 /* Encode and return a source location from a given line and
831 column. */
833 source_location
834 linemap_position_for_line_and_column (line_maps *set,
835 const line_map_ordinary *ord_map,
836 linenum_type line,
837 unsigned column)
839 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
841 source_location r = MAP_START_LOCATION (ord_map);
842 r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
843 << ord_map->m_column_and_range_bits);
844 if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
845 r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
846 << ord_map->m_range_bits);
847 source_location upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
848 if (r >= upper_limit)
849 r = upper_limit - 1;
850 if (r > set->highest_location)
851 set->highest_location = r;
852 return r;
855 /* Encode and return a source_location starting from location LOC and
856 shifting it by COLUMN_OFFSET columns. This function does not support
857 virtual locations. */
859 source_location
860 linemap_position_for_loc_and_offset (struct line_maps *set,
861 source_location loc,
862 unsigned int column_offset)
864 const line_map_ordinary * map = NULL;
866 if (IS_ADHOC_LOC (loc))
867 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
869 /* This function does not support virtual locations yet. */
870 if (linemap_assert_fails
871 (!linemap_location_from_macro_expansion_p (set, loc)))
872 return loc;
874 if (column_offset == 0
875 /* Adding an offset to a reserved location (like
876 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
877 sense. So let's leave the location intact in that case. */
878 || loc < RESERVED_LOCATION_COUNT)
879 return loc;
881 /* We find the real location and shift it. */
882 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
883 /* The new location (loc + offset) should be higher than the first
884 location encoded by MAP. This can fail if the line information
885 is messed up because of line directives (see PR66415). */
886 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
887 return loc;
889 linenum_type line = SOURCE_LINE (map, loc);
890 unsigned int column = SOURCE_COLUMN (map, loc);
892 /* If MAP is not the last line map of its set, then the new location
893 (loc + offset) should be less than the first location encoded by
894 the next line map of the set. Otherwise, we try to encode the
895 location in the next map. */
896 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
897 && (loc + (column_offset << map->m_range_bits)
898 >= MAP_START_LOCATION (&map[1])))
900 map = &map[1];
901 /* If the next map starts in a higher line, we cannot encode the
902 location there. */
903 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
904 return loc;
907 column += column_offset;
908 if (linemap_assert_fails (column < (1u << map->m_column_and_range_bits)))
909 return loc;
911 source_location r =
912 linemap_position_for_line_and_column (set, map, line, column);
913 if (linemap_assert_fails (r <= set->highest_location)
914 || linemap_assert_fails (map == linemap_lookup (set, r)))
915 return loc;
917 return r;
920 /* Given a virtual source location yielded by a map (either an
921 ordinary or a macro map), returns that map. */
923 const struct line_map*
924 linemap_lookup (struct line_maps *set, source_location line)
926 if (IS_ADHOC_LOC (line))
927 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
928 if (linemap_location_from_macro_expansion_p (set, line))
929 return linemap_macro_map_lookup (set, line);
930 return linemap_ordinary_map_lookup (set, line);
933 /* Given a source location yielded by an ordinary map, returns that
934 map. Since the set is built chronologically, the logical lines are
935 monotonic increasing, and so the list is sorted and we can use a
936 binary search. */
938 static const line_map_ordinary *
939 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
941 unsigned int md, mn, mx;
942 const line_map_ordinary *cached, *result;
944 if (IS_ADHOC_LOC (line))
945 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
947 if (set == NULL || line < RESERVED_LOCATION_COUNT)
948 return NULL;
950 mn = LINEMAPS_ORDINARY_CACHE (set);
951 mx = LINEMAPS_ORDINARY_USED (set);
953 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
954 /* We should get a segfault if no line_maps have been added yet. */
955 if (line >= MAP_START_LOCATION (cached))
957 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
958 return cached;
960 else
962 mx = mn;
963 mn = 0;
966 while (mx - mn > 1)
968 md = (mn + mx) / 2;
969 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
970 mx = md;
971 else
972 mn = md;
975 LINEMAPS_ORDINARY_CACHE (set) = mn;
976 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
977 linemap_assert (line >= MAP_START_LOCATION (result));
978 return result;
981 /* Given a source location yielded by a macro map, returns that map.
982 Since the set is built chronologically, the logical lines are
983 monotonic decreasing, and so the list is sorted and we can use a
984 binary search. */
986 static const line_map_macro *
987 linemap_macro_map_lookup (struct line_maps *set, source_location line)
989 unsigned int md, mn, mx;
990 const struct line_map_macro *cached, *result;
992 if (IS_ADHOC_LOC (line))
993 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
995 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
997 if (set == NULL)
998 return NULL;
1000 mn = LINEMAPS_MACRO_CACHE (set);
1001 mx = LINEMAPS_MACRO_USED (set);
1002 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
1004 if (line >= MAP_START_LOCATION (cached))
1006 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
1007 return cached;
1008 mx = mn - 1;
1009 mn = 0;
1012 while (mn < mx)
1014 md = (mx + mn) / 2;
1015 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1016 mn = md + 1;
1017 else
1018 mx = md;
1021 LINEMAPS_MACRO_CACHE (set) = mx;
1022 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
1023 linemap_assert (MAP_START_LOCATION (result) <= line);
1025 return result;
1028 /* Return TRUE if MAP encodes locations coming from a macro
1029 replacement-list at macro expansion point. */
1031 bool
1032 linemap_macro_expansion_map_p (const struct line_map *map)
1034 if (!map)
1035 return false;
1036 return (map->reason == LC_ENTER_MACRO);
1039 /* If LOCATION is the locus of a token in a replacement-list of a
1040 macro expansion return the location of the macro expansion point.
1042 Read the comments of struct line_map and struct line_map_macro in
1043 line-map.h to understand what a macro expansion point is. */
1045 static source_location
1046 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1047 source_location location ATTRIBUTE_UNUSED)
1049 linemap_assert (linemap_macro_expansion_map_p (map)
1050 && location >= MAP_START_LOCATION (map));
1052 /* Make sure LOCATION is correct. */
1053 linemap_assert ((location - MAP_START_LOCATION (map))
1054 < MACRO_MAP_NUM_MACRO_TOKENS (map));
1056 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1059 /* LOCATION is the source location of a token that belongs to a macro
1060 replacement-list as part of the macro expansion denoted by MAP.
1062 Return the location of the token at the definition point of the
1063 macro. */
1065 static source_location
1066 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1067 source_location location)
1069 unsigned token_no;
1071 linemap_assert (linemap_macro_expansion_map_p (map)
1072 && location >= MAP_START_LOCATION (map));
1073 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1075 token_no = location - MAP_START_LOCATION (map);
1076 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1078 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1080 return location;
1083 /* If LOCATION is the locus of a token that is an argument of a
1084 function-like macro M and appears in the expansion of M, return the
1085 locus of that argument in the context of the caller of M.
1087 In other words, this returns the xI location presented in the
1088 comments of line_map_macro above. */
1089 source_location
1090 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1091 const line_map_macro* map,
1092 source_location location)
1094 unsigned token_no;
1096 if (IS_ADHOC_LOC (location))
1097 location = get_location_from_adhoc_loc (set, location);
1099 linemap_assert (linemap_macro_expansion_map_p (map)
1100 && location >= MAP_START_LOCATION (map));
1101 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1102 linemap_assert (!IS_ADHOC_LOC (location));
1104 token_no = location - MAP_START_LOCATION (map);
1105 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1107 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1109 return location;
1112 /* Return the source line number corresponding to source location
1113 LOCATION. SET is the line map set LOCATION comes from. If
1114 LOCATION is the source location of token that is part of the
1115 replacement-list of a macro expansion return the line number of the
1116 macro expansion point. */
1119 linemap_get_expansion_line (struct line_maps *set,
1120 source_location location)
1122 const line_map_ordinary *map = NULL;
1124 if (IS_ADHOC_LOC (location))
1125 location = set->location_adhoc_data_map.data[location
1126 & MAX_SOURCE_LOCATION].locus;
1128 if (location < RESERVED_LOCATION_COUNT)
1129 return 0;
1131 location =
1132 linemap_macro_loc_to_exp_point (set, location, &map);
1134 return SOURCE_LINE (map, location);
1137 /* Return the path of the file corresponding to source code location
1138 LOCATION.
1140 If LOCATION is the source location of token that is part of the
1141 replacement-list of a macro expansion return the file path of the
1142 macro expansion point.
1144 SET is the line map set LOCATION comes from. */
1146 const char*
1147 linemap_get_expansion_filename (struct line_maps *set,
1148 source_location location)
1150 const struct line_map_ordinary *map = NULL;
1152 if (IS_ADHOC_LOC (location))
1153 location = set->location_adhoc_data_map.data[location
1154 & MAX_SOURCE_LOCATION].locus;
1156 if (location < RESERVED_LOCATION_COUNT)
1157 return NULL;
1159 location =
1160 linemap_macro_loc_to_exp_point (set, location, &map);
1162 return LINEMAP_FILE (map);
1165 /* Return the name of the macro associated to MACRO_MAP. */
1167 const char*
1168 linemap_map_get_macro_name (const line_map_macro *macro_map)
1170 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1171 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1174 /* Return a positive value if LOCATION is the locus of a token that is
1175 located in a system header, O otherwise. It returns 1 if LOCATION
1176 is the locus of a token that is located in a system header, and 2
1177 if LOCATION is the locus of a token located in a C system header
1178 that therefore needs to be extern "C" protected in C++.
1180 Note that this function returns 1 if LOCATION belongs to a token
1181 that is part of a macro replacement-list defined in a system
1182 header, but expanded in a non-system file. */
1185 linemap_location_in_system_header_p (struct line_maps *set,
1186 source_location location)
1188 const struct line_map *map = NULL;
1190 if (IS_ADHOC_LOC (location))
1191 location = set->location_adhoc_data_map.data[location
1192 & MAX_SOURCE_LOCATION].locus;
1194 if (location < RESERVED_LOCATION_COUNT)
1195 return false;
1197 /* Let's look at where the token for LOCATION comes from. */
1198 while (true)
1200 map = linemap_lookup (set, location);
1201 if (map != NULL)
1203 if (!linemap_macro_expansion_map_p (map))
1204 /* It's a normal token. */
1205 return LINEMAP_SYSP (linemap_check_ordinary (map));
1206 else
1208 const line_map_macro *macro_map = linemap_check_macro (map);
1210 /* It's a token resulting from a macro expansion. */
1211 source_location loc =
1212 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
1213 if (loc < RESERVED_LOCATION_COUNT)
1214 /* This token might come from a built-in macro. Let's
1215 look at where that macro got expanded. */
1216 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1217 else
1218 location = loc;
1221 else
1222 break;
1224 return false;
1227 /* Return TRUE if LOCATION is a source code location of a token that is part of
1228 a macro expansion, FALSE otherwise. */
1230 bool
1231 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1232 source_location location)
1234 if (IS_ADHOC_LOC (location))
1235 location = set->location_adhoc_data_map.data[location
1236 & MAX_SOURCE_LOCATION].locus;
1238 linemap_assert (location <= MAX_SOURCE_LOCATION
1239 && (set->highest_location
1240 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
1241 if (set == NULL)
1242 return false;
1243 return (location > set->highest_location);
1246 /* Given two virtual locations *LOC0 and *LOC1, return the first
1247 common macro map in their macro expansion histories. Return NULL
1248 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1249 virtual location of the token inside the resulting macro. */
1251 static const struct line_map*
1252 first_map_in_common_1 (struct line_maps *set,
1253 source_location *loc0,
1254 source_location *loc1)
1256 source_location l0 = *loc0, l1 = *loc1;
1257 const struct line_map *map0 = linemap_lookup (set, l0),
1258 *map1 = linemap_lookup (set, l1);
1260 while (linemap_macro_expansion_map_p (map0)
1261 && linemap_macro_expansion_map_p (map1)
1262 && (map0 != map1))
1264 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1266 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1267 l0);
1268 map0 = linemap_lookup (set, l0);
1270 else
1272 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1273 l1);
1274 map1 = linemap_lookup (set, l1);
1278 if (map0 == map1)
1280 *loc0 = l0;
1281 *loc1 = l1;
1282 return map0;
1284 return NULL;
1287 /* Given two virtual locations LOC0 and LOC1, return the first common
1288 macro map in their macro expansion histories. Return NULL if no
1289 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1290 virtual location of the token inside the resulting macro, upon
1291 return of a non-NULL result. */
1293 static const struct line_map*
1294 first_map_in_common (struct line_maps *set,
1295 source_location loc0,
1296 source_location loc1,
1297 source_location *res_loc0,
1298 source_location *res_loc1)
1300 *res_loc0 = loc0;
1301 *res_loc1 = loc1;
1303 return first_map_in_common_1 (set, res_loc0, res_loc1);
1306 /* Return a positive value if PRE denotes the location of a token that
1307 comes before the token of POST, 0 if PRE denotes the location of
1308 the same token as the token for POST, and a negative value
1309 otherwise. */
1312 linemap_compare_locations (struct line_maps *set,
1313 source_location pre,
1314 source_location post)
1316 bool pre_virtual_p, post_virtual_p;
1317 source_location l0 = pre, l1 = post;
1319 if (IS_ADHOC_LOC (l0))
1320 l0 = get_location_from_adhoc_loc (set, l0);
1321 if (IS_ADHOC_LOC (l1))
1322 l1 = get_location_from_adhoc_loc (set, l1);
1324 if (l0 == l1)
1325 return 0;
1327 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1328 l0 = linemap_resolve_location (set, l0,
1329 LRK_MACRO_EXPANSION_POINT,
1330 NULL);
1332 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1333 l1 = linemap_resolve_location (set, l1,
1334 LRK_MACRO_EXPANSION_POINT,
1335 NULL);
1337 if (l0 == l1
1338 && pre_virtual_p
1339 && post_virtual_p)
1341 /* So pre and post represent two tokens that are present in a
1342 same macro expansion. Let's see if the token for pre was
1343 before the token for post in that expansion. */
1344 unsigned i0, i1;
1345 const struct line_map *map =
1346 first_map_in_common (set, pre, post, &l0, &l1);
1348 if (map == NULL)
1349 /* This should not be possible. */
1350 abort ();
1352 i0 = l0 - MAP_START_LOCATION (map);
1353 i1 = l1 - MAP_START_LOCATION (map);
1354 return i1 - i0;
1357 if (IS_ADHOC_LOC (l0))
1358 l0 = get_location_from_adhoc_loc (set, l0);
1359 if (IS_ADHOC_LOC (l1))
1360 l1 = get_location_from_adhoc_loc (set, l1);
1362 return l1 - l0;
1365 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1367 static void
1368 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1370 unsigned int i = set->depth;
1372 while (--i)
1373 putc ('.', stderr);
1375 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1378 /* Return the spelling location of the token wherever it comes from,
1379 whether part of a macro definition or not.
1381 This is a subroutine for linemap_resolve_location. */
1383 static source_location
1384 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1385 source_location location,
1386 const line_map_ordinary **original_map)
1388 struct line_map *map;
1389 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1391 while (true)
1393 map = const_cast <line_map *> (linemap_lookup (set, location));
1394 if (!linemap_macro_expansion_map_p (map))
1395 break;
1397 location
1398 = linemap_macro_map_loc_unwind_toward_spelling
1399 (set, linemap_check_macro (map),
1400 location);
1403 if (original_map)
1404 *original_map = linemap_check_ordinary (map);
1405 return location;
1408 /* If LOCATION is the source location of a token that belongs to a
1409 macro replacement-list -- as part of a macro expansion -- then
1410 return the location of the token at the definition point of the
1411 macro. Otherwise, return LOCATION. SET is the set of maps
1412 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1413 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1414 returned location comes from.
1416 This is a subroutine of linemap_resolve_location. */
1418 static source_location
1419 linemap_macro_loc_to_def_point (struct line_maps *set,
1420 source_location location,
1421 const line_map_ordinary **original_map)
1423 struct line_map *map;
1425 if (IS_ADHOC_LOC (location))
1426 location = set->location_adhoc_data_map.data[location
1427 & MAX_SOURCE_LOCATION].locus;
1429 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1431 while (true)
1433 map = const_cast <line_map *> (linemap_lookup (set, location));
1434 if (!linemap_macro_expansion_map_p (map))
1435 break;
1437 location =
1438 linemap_macro_map_loc_to_def_point (linemap_check_macro (map),
1439 location);
1442 if (original_map)
1443 *original_map = linemap_check_ordinary (map);
1444 return location;
1447 /* If LOCATION is the source location of a token that belongs to a
1448 macro replacement-list -- at a macro expansion point -- then return
1449 the location of the topmost expansion point of the macro. We say
1450 topmost because if we are in the context of a nested macro
1451 expansion, the function returns the source location of the first
1452 macro expansion that triggered the nested expansions.
1454 Otherwise, return LOCATION. SET is the set of maps location come
1455 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1456 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1457 location comes from.
1459 This is a subroutine of linemap_resolve_location. */
1461 static source_location
1462 linemap_macro_loc_to_exp_point (struct line_maps *set,
1463 source_location location,
1464 const line_map_ordinary **original_map)
1466 struct line_map *map;
1468 if (IS_ADHOC_LOC (location))
1469 location = set->location_adhoc_data_map.data[location
1470 & MAX_SOURCE_LOCATION].locus;
1472 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1474 while (true)
1476 map = const_cast <line_map *> (linemap_lookup (set, location));
1477 if (!linemap_macro_expansion_map_p (map))
1478 break;
1479 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1480 location);
1483 if (original_map)
1484 *original_map = linemap_check_ordinary (map);
1485 return location;
1488 /* Resolve a virtual location into either a spelling location, an
1489 expansion point location or a token argument replacement point
1490 location. Return the map that encodes the virtual location as well
1491 as the resolved location.
1493 If LOC is *NOT* the location of a token resulting from the
1494 expansion of a macro, then the parameter LRK (which stands for
1495 Location Resolution Kind) is ignored and the resulting location
1496 just equals the one given in argument.
1498 Now if LOC *IS* the location of a token resulting from the
1499 expansion of a macro, this is what happens.
1501 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1502 -------------------------------
1504 The virtual location is resolved to the first macro expansion point
1505 that led to this macro expansion.
1507 * If LRK is set to LRK_SPELLING_LOCATION
1508 -------------------------------------
1510 The virtual location is resolved to the locus where the token has
1511 been spelled in the source. This can follow through all the macro
1512 expansions that led to the token.
1514 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1515 --------------------------------------
1517 The virtual location is resolved to the locus of the token in the
1518 context of the macro definition.
1520 If LOC is the locus of a token that is an argument of a
1521 function-like macro [replacing a parameter in the replacement list
1522 of the macro] the virtual location is resolved to the locus of the
1523 parameter that is replaced, in the context of the definition of the
1524 macro.
1526 If LOC is the locus of a token that is not an argument of a
1527 function-like macro, then the function behaves as if LRK was set to
1528 LRK_SPELLING_LOCATION.
1530 If MAP is not NULL, *MAP is set to the map encoding the
1531 returned location. Note that if the returned location wasn't originally
1532 encoded by a map, then *MAP is set to NULL. This can happen if LOC
1533 resolves to a location reserved for the client code, like
1534 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1536 source_location
1537 linemap_resolve_location (struct line_maps *set,
1538 source_location loc,
1539 enum location_resolution_kind lrk,
1540 const line_map_ordinary **map)
1542 source_location locus = loc;
1543 if (IS_ADHOC_LOC (loc))
1544 locus = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1546 if (locus < RESERVED_LOCATION_COUNT)
1548 /* A reserved location wasn't encoded in a map. Let's return a
1549 NULL map here, just like what linemap_ordinary_map_lookup
1550 does. */
1551 if (map)
1552 *map = NULL;
1553 return loc;
1556 switch (lrk)
1558 case LRK_MACRO_EXPANSION_POINT:
1559 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1560 break;
1561 case LRK_SPELLING_LOCATION:
1562 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1563 break;
1564 case LRK_MACRO_DEFINITION_LOCATION:
1565 loc = linemap_macro_loc_to_def_point (set, loc, map);
1566 break;
1567 default:
1568 abort ();
1570 return loc;
1573 /* TRUE if LOCATION is a source code location of a token that is part of the
1574 definition of a macro, FALSE otherwise. */
1576 bool
1577 linemap_location_from_macro_definition_p (struct line_maps *set,
1578 source_location loc)
1580 if (IS_ADHOC_LOC (loc))
1581 loc = get_location_from_adhoc_loc (set, loc);
1583 if (!linemap_location_from_macro_expansion_p (set, loc))
1584 return false;
1586 while (true)
1588 const struct line_map_macro *map
1589 = linemap_check_macro (linemap_lookup (set, loc));
1591 source_location s_loc
1592 = linemap_macro_map_loc_unwind_toward_spelling (set, map, loc);
1593 if (linemap_location_from_macro_expansion_p (set, s_loc))
1594 loc = s_loc;
1595 else
1597 source_location def_loc
1598 = linemap_macro_map_loc_to_def_point (map, loc);
1599 return s_loc == def_loc;
1605 Suppose that LOC is the virtual location of a token T coming from
1606 the expansion of a macro M. This function then steps up to get the
1607 location L of the point where M got expanded. If L is a spelling
1608 location inside a macro expansion M', then this function returns
1609 the locus of the point where M' was expanded. Said otherwise, this
1610 function returns the location of T in the context that triggered
1611 the expansion of M.
1613 *LOC_MAP must be set to the map of LOC. This function then sets it
1614 to the map of the returned location. */
1616 source_location
1617 linemap_unwind_toward_expansion (struct line_maps *set,
1618 source_location loc,
1619 const struct line_map **map)
1621 source_location resolved_location;
1622 const line_map_macro *macro_map = linemap_check_macro (*map);
1623 const struct line_map *resolved_map;
1625 if (IS_ADHOC_LOC (loc))
1626 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1628 resolved_location =
1629 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1630 resolved_map = linemap_lookup (set, resolved_location);
1632 if (!linemap_macro_expansion_map_p (resolved_map))
1634 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1635 resolved_map = linemap_lookup (set, resolved_location);
1638 *map = resolved_map;
1639 return resolved_location;
1642 /* If LOC is the virtual location of a token coming from the expansion
1643 of a macro M and if its spelling location is reserved (e.g, a
1644 location for a built-in token), then this function unwinds (using
1645 linemap_unwind_toward_expansion) the location until a location that
1646 is not reserved and is not in a system header is reached. In other
1647 words, this unwinds the reserved location until a location that is
1648 in real source code is reached.
1650 Otherwise, if the spelling location for LOC is not reserved or if
1651 LOC doesn't come from the expansion of a macro, the function
1652 returns LOC as is and *MAP is not touched.
1654 *MAP is set to the map of the returned location if the later is
1655 different from LOC. */
1656 source_location
1657 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1658 source_location loc,
1659 const struct line_map **map)
1661 source_location resolved_loc;
1662 const struct line_map *map0 = NULL;
1663 const line_map_ordinary *map1 = NULL;
1665 if (IS_ADHOC_LOC (loc))
1666 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1668 map0 = linemap_lookup (set, loc);
1669 if (!linemap_macro_expansion_map_p (map0))
1670 return loc;
1672 resolved_loc = linemap_resolve_location (set, loc,
1673 LRK_SPELLING_LOCATION,
1674 &map1);
1676 if (resolved_loc >= RESERVED_LOCATION_COUNT
1677 && !LINEMAP_SYSP (map1))
1678 return loc;
1680 while (linemap_macro_expansion_map_p (map0)
1681 && (resolved_loc < RESERVED_LOCATION_COUNT
1682 || LINEMAP_SYSP (map1)))
1684 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1685 resolved_loc = linemap_resolve_location (set, loc,
1686 LRK_SPELLING_LOCATION,
1687 &map1);
1690 if (map != NULL)
1691 *map = map0;
1692 return loc;
1695 /* Expand source code location LOC and return a user readable source
1696 code location. LOC must be a spelling (non-virtual) location. If
1697 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1698 location is returned. */
1700 expanded_location
1701 linemap_expand_location (struct line_maps *set,
1702 const struct line_map *map,
1703 source_location loc)
1706 expanded_location xloc;
1708 memset (&xloc, 0, sizeof (xloc));
1709 if (IS_ADHOC_LOC (loc))
1711 xloc.data
1712 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1713 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1716 if (loc < RESERVED_LOCATION_COUNT)
1717 /* The location for this token wasn't generated from a line map.
1718 It was probably a location for a builtin token, chosen by some
1719 client code. Let's not try to expand the location in that
1720 case. */;
1721 else if (map == NULL)
1722 /* We shouldn't be getting a NULL map with a location that is not
1723 reserved by the client code. */
1724 abort ();
1725 else
1727 /* MAP must be an ordinary map and LOC must be non-virtual,
1728 encoded into this map, obviously; the accessors used on MAP
1729 below ensure it is ordinary. Let's just assert the
1730 non-virtualness of LOC here. */
1731 if (linemap_location_from_macro_expansion_p (set, loc))
1732 abort ();
1734 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1736 xloc.file = LINEMAP_FILE (ord_map);
1737 xloc.line = SOURCE_LINE (ord_map, loc);
1738 xloc.column = SOURCE_COLUMN (ord_map, loc);
1739 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1742 return xloc;
1746 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1747 is NULL, use stderr. IS_MACRO is true if the caller wants to
1748 dump a macro map, false otherwise. */
1750 void
1751 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1753 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1754 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1755 "LC_ENTER_MACRO" };
1756 const char *reason;
1757 const line_map *map;
1759 if (stream == NULL)
1760 stream = stderr;
1762 if (!is_macro)
1763 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1764 else
1765 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1767 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1769 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1770 ix, (void *) map, map->start_location, reason,
1771 ((!is_macro
1772 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1773 ? "yes" : "no"));
1774 if (!is_macro)
1776 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1777 unsigned includer_ix;
1778 const line_map_ordinary *includer_map;
1780 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
1781 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1782 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1783 : NULL;
1785 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1786 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1787 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1788 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1790 else
1792 const line_map_macro *macro_map = linemap_check_macro (map);
1793 fprintf (stream, "Macro: %s (%u tokens)\n",
1794 linemap_map_get_macro_name (macro_map),
1795 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1798 fprintf (stream, "\n");
1802 /* Dump debugging information about source location LOC into the file
1803 stream STREAM. SET is the line map set LOC comes from. */
1805 void
1806 linemap_dump_location (struct line_maps *set,
1807 source_location loc,
1808 FILE *stream)
1810 const line_map_ordinary *map;
1811 source_location location;
1812 const char *path = "", *from = "";
1813 int l = -1, c = -1, s = -1, e = -1;
1815 if (IS_ADHOC_LOC (loc))
1816 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1818 if (loc == 0)
1819 return;
1821 location =
1822 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1824 if (map == NULL)
1825 /* Only reserved locations can be tolerated in this case. */
1826 linemap_assert (location < RESERVED_LOCATION_COUNT);
1827 else
1829 path = LINEMAP_FILE (map);
1830 l = SOURCE_LINE (map, location);
1831 c = SOURCE_COLUMN (map, location);
1832 s = LINEMAP_SYSP (map) != 0;
1833 e = location != loc;
1834 if (e)
1835 from = "N/A";
1836 else
1837 from = (INCLUDED_FROM (set, map))
1838 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1839 : "<NULL>";
1842 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1843 E: macro expansion?, LOC: original location, R: resolved location */
1844 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1845 path, from, l, c, s, (void*)map, e, loc, location);
1848 /* Return the highest location emitted for a given file for which
1849 there is a line map in SET. FILE_NAME is the file name to
1850 consider. If the function returns TRUE, *LOC is set to the highest
1851 location emitted for that file. */
1853 bool
1854 linemap_get_file_highest_location (struct line_maps *set,
1855 const char *file_name,
1856 source_location *loc)
1858 /* If the set is empty or no ordinary map has been created then
1859 there is no file to look for ... */
1860 if (set == NULL || set->info_ordinary.used == 0)
1861 return false;
1863 /* Now look for the last ordinary map created for FILE_NAME. */
1864 int i;
1865 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1867 const char *fname = set->info_ordinary.maps[i].to_file;
1868 if (fname && !filename_cmp (fname, file_name))
1869 break;
1872 if (i < 0)
1873 return false;
1875 /* The highest location for a given map is either the starting
1876 location of the next map minus one, or -- if the map is the
1877 latest one -- the highest location of the set. */
1878 source_location result;
1879 if (i == (int) set->info_ordinary.used - 1)
1880 result = set->highest_location;
1881 else
1882 result = set->info_ordinary.maps[i + 1].start_location - 1;
1884 *loc = result;
1885 return true;
1888 /* Compute and return statistics about the memory consumption of some
1889 parts of the line table SET. */
1891 void
1892 linemap_get_statistics (struct line_maps *set,
1893 struct linemap_stats *s)
1895 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1896 macro_maps_allocated_size, macro_maps_used_size,
1897 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1899 const line_map_macro *cur_map;
1901 ordinary_maps_allocated_size =
1902 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1904 ordinary_maps_used_size =
1905 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1907 macro_maps_allocated_size =
1908 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1910 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1911 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1912 ++cur_map)
1914 unsigned i;
1916 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1918 macro_maps_locations_size +=
1919 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1921 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1923 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1924 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1925 duplicated_macro_maps_locations_size +=
1926 sizeof (source_location);
1930 macro_maps_used_size =
1931 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1933 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1934 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1935 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1936 s->ordinary_maps_used_size = ordinary_maps_used_size;
1937 s->num_expanded_macros = num_expanded_macros_counter;
1938 s->num_macro_tokens = num_macro_tokens_counter;
1939 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1940 s->macro_maps_allocated_size = macro_maps_allocated_size;
1941 s->macro_maps_locations_size = macro_maps_locations_size;
1942 s->macro_maps_used_size = macro_maps_used_size;
1943 s->duplicated_macro_maps_locations_size =
1944 duplicated_macro_maps_locations_size;
1945 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1946 * sizeof (struct location_adhoc_data));
1947 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
1951 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1952 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1953 specifies how many macro maps to dump. */
1955 void
1956 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1957 unsigned int num_macro)
1959 unsigned int i;
1961 if (set == NULL)
1962 return;
1964 if (stream == NULL)
1965 stream = stderr;
1967 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1968 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1969 fprintf (stream, "Include stack depth: %d\n", set->depth);
1970 fprintf (stream, "Highest location: %u\n", set->highest_location);
1972 if (num_ordinary)
1974 fprintf (stream, "\nOrdinary line maps\n");
1975 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1976 linemap_dump (stream, set, i, false);
1977 fprintf (stream, "\n");
1980 if (num_macro)
1982 fprintf (stream, "\nMacro line maps\n");
1983 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1984 linemap_dump (stream, set, i, true);
1985 fprintf (stream, "\n");
1989 /* struct source_range. */
1991 /* Is there any part of this range on the given line? */
1993 bool
1994 source_range::intersects_line_p (const char *file, int line) const
1996 expanded_location exploc_start
1997 = linemap_client_expand_location_to_spelling_point (m_start);
1998 if (file != exploc_start.file)
1999 return false;
2000 if (line < exploc_start.line)
2001 return false;
2002 expanded_location exploc_finish
2003 = linemap_client_expand_location_to_spelling_point (m_finish);
2004 if (file != exploc_finish.file)
2005 return false;
2006 if (line > exploc_finish.line)
2007 return false;
2008 return true;
2011 /* class rich_location. */
2013 /* Construct a rich_location with location LOC as its initial range. */
2015 rich_location::rich_location (line_maps *set, source_location loc) :
2016 m_line_table (set),
2017 m_ranges (),
2018 m_column_override (0),
2019 m_have_expanded_location (false),
2020 m_fixit_hints (),
2021 m_seen_impossible_fixit (false)
2023 add_range (loc, true);
2026 /* The destructor for class rich_location. */
2028 rich_location::~rich_location ()
2030 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2031 delete get_fixit_hint (i);
2034 /* Get location IDX within this rich_location. */
2036 source_location
2037 rich_location::get_loc (unsigned int idx) const
2039 const location_range *locrange = get_range (idx);
2040 return locrange->m_loc;
2043 /* Get range IDX within this rich_location. */
2045 const location_range *
2046 rich_location::get_range (unsigned int idx) const
2048 return &m_ranges[idx];
2051 /* Mutable access to range IDX within this rich_location. */
2053 location_range *
2054 rich_location::get_range (unsigned int idx)
2056 return &m_ranges[idx];
2059 /* Expand location IDX within this rich_location. */
2060 /* Get an expanded_location for this rich_location's primary
2061 location. */
2063 expanded_location
2064 rich_location::get_expanded_location (unsigned int idx)
2066 if (idx == 0)
2068 /* Cache the expansion of the primary location. */
2069 if (!m_have_expanded_location)
2071 m_expanded_location
2072 = linemap_client_expand_location_to_spelling_point (get_loc (0));
2073 if (m_column_override)
2074 m_expanded_location.column = m_column_override;
2075 m_have_expanded_location = true;
2078 return m_expanded_location;
2080 else
2081 return linemap_client_expand_location_to_spelling_point (get_loc (idx));
2084 /* Set the column of the primary location, with 0 meaning
2085 "don't override it". */
2087 void
2088 rich_location::override_column (int column)
2090 m_column_override = column;
2091 m_have_expanded_location = false;
2094 /* Add the given range. */
2096 void
2097 rich_location::add_range (source_location loc, bool show_caret_p)
2099 location_range range;
2100 range.m_loc = loc;
2101 range.m_show_caret_p = show_caret_p;
2102 m_ranges.push (range);
2105 /* Add or overwrite the location given by IDX, setting its location to LOC,
2106 and setting its "should my caret be printed" flag to SHOW_CARET_P.
2108 It must either overwrite an existing location, or add one *exactly* on
2109 the end of the array.
2111 This is primarily for use by gcc when implementing diagnostic format
2112 decoders e.g.
2113 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2114 (which writes the source location of a tree back into location 0 of
2115 the rich_location), and
2116 - the "%C" and "%L" format codes in the Fortran frontend. */
2118 void
2119 rich_location::set_range (line_maps * /*set*/, unsigned int idx,
2120 source_location loc, bool show_caret_p)
2122 /* We can either overwrite an existing range, or add one exactly
2123 on the end of the array. */
2124 linemap_assert (idx <= m_ranges.count ());
2126 if (idx == m_ranges.count ())
2127 add_range (loc, show_caret_p);
2128 else
2130 location_range *locrange = get_range (idx);
2131 locrange->m_loc = loc;
2132 locrange->m_show_caret_p = show_caret_p;
2135 if (idx == 0)
2136 /* Mark any cached value here as dirty. */
2137 m_have_expanded_location = false;
2140 /* Methods for adding insertion fix-it hints. */
2142 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2143 immediately before the primary range's start location. */
2145 void
2146 rich_location::add_fixit_insert_before (const char *new_content)
2148 add_fixit_insert_before (get_loc (), new_content);
2151 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2152 immediately before the start of WHERE. */
2154 void
2155 rich_location::add_fixit_insert_before (source_location where,
2156 const char *new_content)
2158 source_location start = get_range_from_loc (m_line_table, where).m_start;
2160 if (reject_impossible_fixit (start))
2161 return;
2162 /* We do not yet support newlines within fix-it hints. */
2163 if (strchr (new_content, '\n'))
2165 stop_supporting_fixits ();
2166 return;
2168 add_fixit (new fixit_insert (start, new_content));
2171 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2172 immediately after the primary range's end-point. */
2174 void
2175 rich_location::add_fixit_insert_after (const char *new_content)
2177 add_fixit_insert_after (get_loc (), new_content);
2180 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2181 immediately after the end-point of WHERE. */
2183 void
2184 rich_location::add_fixit_insert_after (source_location where,
2185 const char *new_content)
2187 source_location finish = get_range_from_loc (m_line_table, where).m_finish;
2189 if (reject_impossible_fixit (finish))
2190 return;
2192 source_location next_loc
2193 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2195 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2196 its input value. */
2197 if (next_loc == finish)
2199 stop_supporting_fixits ();
2200 return;
2203 add_fixit (new fixit_insert (next_loc, new_content));
2206 /* Methods for adding removal fix-it hints. */
2208 /* Add a fixit-hint, suggesting removal of the content covered
2209 by range 0. */
2211 void
2212 rich_location::add_fixit_remove ()
2214 add_fixit_remove (get_loc ());
2217 /* Add a fixit-hint, suggesting removal of the content between
2218 the start and finish of WHERE. */
2220 void
2221 rich_location::add_fixit_remove (source_location where)
2223 source_range range = get_range_from_loc (m_line_table, where);
2224 add_fixit_remove (range);
2227 /* Add a fixit-hint, suggesting removal of the content at
2228 SRC_RANGE. */
2230 void
2231 rich_location::add_fixit_remove (source_range src_range)
2233 add_fixit_replace (src_range, "");
2236 /* Return true iff A is in the column directly before B, on the
2237 same line of the same source file. */
2239 static bool
2240 column_before_p (line_maps *set, source_location a, source_location b)
2242 if (IS_ADHOC_LOC (a))
2243 a = get_location_from_adhoc_loc (set, a);
2244 if (IS_ADHOC_LOC (b))
2245 b = get_location_from_adhoc_loc (set, b);
2247 /* They must both be in ordinary maps. */
2248 const struct line_map *linemap_a = linemap_lookup (set, a);
2249 if (linemap_macro_expansion_map_p (linemap_a))
2250 return false;
2251 const struct line_map *linemap_b = linemap_lookup (set, b);
2252 if (linemap_macro_expansion_map_p (linemap_b))
2253 return false;
2255 /* To be on the same line, they must be in the same ordinary map. */
2256 if (linemap_a != linemap_b)
2257 return false;
2259 linenum_type line_a
2260 = SOURCE_LINE (linemap_check_ordinary (linemap_a), a);
2261 linenum_type line_b
2262 = SOURCE_LINE (linemap_check_ordinary (linemap_b), b);
2263 if (line_a != line_b)
2264 return false;
2266 linenum_type column_a
2267 = SOURCE_COLUMN (linemap_check_ordinary (linemap_a), a);
2268 linenum_type column_b
2269 = SOURCE_COLUMN (linemap_check_ordinary (linemap_b), b);
2271 return column_b == column_a + 1;
2274 /* Add a fixit-hint, suggesting replacement of the content covered
2275 by range 0 with NEW_CONTENT. */
2277 void
2278 rich_location::add_fixit_replace (const char *new_content)
2280 add_fixit_replace (get_loc (), new_content);
2283 /* Methods for adding "replace" fix-it hints. */
2285 /* Add a fixit-hint, suggesting replacement of the content between
2286 the start and finish of WHERE with NEW_CONTENT. */
2288 void
2289 rich_location::add_fixit_replace (source_location where,
2290 const char *new_content)
2292 source_range range = get_range_from_loc (m_line_table, where);
2293 add_fixit_replace (range, new_content);
2296 /* Add a fixit-hint, suggesting replacement of the content at
2297 SRC_RANGE with NEW_CONTENT. */
2299 void
2300 rich_location::add_fixit_replace (source_range src_range,
2301 const char *new_content)
2303 src_range.m_start = get_pure_location (m_line_table, src_range.m_start);
2304 src_range.m_finish = get_pure_location (m_line_table, src_range.m_finish);
2306 if (reject_impossible_fixit (src_range.m_start))
2307 return;
2308 if (reject_impossible_fixit (src_range.m_finish))
2309 return;
2311 /* We do not yet support newlines within fix-it hints. */
2312 if (strchr (new_content, '\n'))
2314 stop_supporting_fixits ();
2315 return;
2318 /* Consolidate neighboring fixits. */
2319 fixit_hint *prev = get_last_fixit_hint ();
2320 if (prev)
2321 if (prev->maybe_append_replace (m_line_table, src_range, new_content))
2322 return;
2324 add_fixit (new fixit_replace (src_range, new_content));
2327 /* Get the last fix-it hint within this rich_location, or NULL if none. */
2329 fixit_hint *
2330 rich_location::get_last_fixit_hint () const
2332 if (m_fixit_hints.count () > 0)
2333 return get_fixit_hint (m_fixit_hints.count () - 1);
2334 else
2335 return NULL;
2338 /* If WHERE is an "awkward" location, then mark this rich_location as not
2339 supporting fixits, purging any thay were already added, and return true.
2341 Otherwise (the common case), return false. */
2343 bool
2344 rich_location::reject_impossible_fixit (source_location where)
2346 /* Fix-its within a rich_location should either all be suggested, or
2347 none of them should be suggested.
2348 Once we've rejected a fixit, we reject any more, even those
2349 with reasonable locations. */
2350 if (m_seen_impossible_fixit)
2351 return true;
2353 if (where <= LINE_MAP_MAX_LOCATION_WITH_COLS)
2354 /* WHERE is a reasonable location for a fix-it; don't reject it. */
2355 return false;
2357 /* Otherwise we have an attempt to add a fix-it with an "awkward"
2358 location: either one that we can't obtain column information
2359 for (within an ordinary map), or one within a macro expansion. */
2360 stop_supporting_fixits ();
2361 return true;
2364 /* Mark this rich_location as not supporting fixits, purging any that were
2365 already added. */
2367 void
2368 rich_location::stop_supporting_fixits ()
2370 m_seen_impossible_fixit = true;
2372 /* Purge the rich_location of any fix-its that were already added. */
2373 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2374 delete get_fixit_hint (i);
2375 m_fixit_hints.truncate (0);
2378 /* Add HINT to the fix-it hints in this rich_location. */
2380 void
2381 rich_location::add_fixit (fixit_hint *hint)
2383 m_fixit_hints.push (hint);
2386 /* class fixit_insert. */
2388 fixit_insert::fixit_insert (source_location where,
2389 const char *new_content)
2390 : m_where (where),
2391 m_bytes (xstrdup (new_content)),
2392 m_len (strlen (new_content))
2396 fixit_insert::~fixit_insert ()
2398 free (m_bytes);
2401 /* Implementation of fixit_hint::affects_line_p for fixit_insert. */
2403 bool
2404 fixit_insert::affects_line_p (const char *file, int line) const
2406 expanded_location exploc
2407 = linemap_client_expand_location_to_spelling_point (m_where);
2408 if (file == exploc.file)
2409 if (line == exploc.line)
2410 return true;
2411 return false;
2414 /* Implementation of maybe_append_replace for fixit_insert. Reject
2415 the attempt to consolidate fix-its. */
2417 bool
2418 fixit_insert::maybe_append_replace (line_maps *, source_range, const char *)
2420 return false;
2423 /* class fixit_replace. */
2425 fixit_replace::fixit_replace (source_range src_range,
2426 const char *new_content)
2427 : m_src_range (src_range),
2428 m_bytes (xstrdup (new_content)),
2429 m_len (strlen (new_content))
2433 fixit_replace::~fixit_replace ()
2435 free (m_bytes);
2438 /* Implementation of fixit_hint::affects_line_p for fixit_replace. */
2440 bool
2441 fixit_replace::affects_line_p (const char *file, int line) const
2443 return m_src_range.intersects_line_p (file, line);
2446 /* Implementation of maybe_append_replace for fixit_replace. If
2447 possible, merge the new replacement into this one and return true.
2448 Otherwise return false. */
2450 bool
2451 fixit_replace::maybe_append_replace (line_maps *set,
2452 source_range src_range,
2453 const char *new_content)
2455 /* Does SRC_RANGE start immediately after this one finishes? */
2456 if (!column_before_p (set, m_src_range.m_finish, src_range.m_start))
2457 return false;
2459 /* We have neighboring replacements; merge them. */
2460 m_src_range.m_finish = src_range.m_finish;
2461 size_t extra_len = strlen (new_content);
2462 m_bytes = (char *)xrealloc (m_bytes, m_len + extra_len + 1);
2463 memcpy (m_bytes + m_len, new_content, extra_len);
2464 m_len += extra_len;
2465 m_bytes[m_len] = '\0';
2466 return true;