2016-09-21 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libcpp / line-map.c
blob07e3acb78a5fba04ef23df2e9de7276fe7c01956
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 /* Hash function for location_adhoc_data hashtable. */
62 static hashval_t
63 location_adhoc_data_hash (const void *l)
65 const struct location_adhoc_data *lb =
66 (const struct location_adhoc_data *) l;
67 return ((hashval_t) lb->locus
68 + (hashval_t) lb->src_range.m_start
69 + (hashval_t) lb->src_range.m_finish
70 + (size_t) lb->data);
73 /* Compare function for location_adhoc_data hashtable. */
75 static int
76 location_adhoc_data_eq (const void *l1, const void *l2)
78 const struct location_adhoc_data *lb1 =
79 (const struct location_adhoc_data *) l1;
80 const struct location_adhoc_data *lb2 =
81 (const struct location_adhoc_data *) l2;
82 return (lb1->locus == lb2->locus
83 && lb1->src_range.m_start == lb2->src_range.m_start
84 && lb1->src_range.m_finish == lb2->src_range.m_finish
85 && lb1->data == lb2->data);
88 /* Update the hashtable when location_adhoc_data is reallocated. */
90 static int
91 location_adhoc_data_update (void **slot, void *data)
93 *((char **) slot) += *((int64_t *) data);
94 return 1;
97 /* Rebuild the hash table from the location adhoc data. */
99 void
100 rebuild_location_adhoc_htab (struct line_maps *set)
102 unsigned i;
103 set->location_adhoc_data_map.htab =
104 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
105 for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
106 htab_find_slot (set->location_adhoc_data_map.htab,
107 set->location_adhoc_data_map.data + i, INSERT);
110 /* Helper function for get_combined_adhoc_loc.
111 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
112 within a source_location, without needing to use an ad-hoc location. */
114 static bool
115 can_be_stored_compactly_p (struct line_maps *set,
116 source_location locus,
117 source_range src_range,
118 void *data)
120 /* If there's an ad-hoc pointer, we can't store it directly in the
121 source_location, we need the lookaside. */
122 if (data)
123 return false;
125 /* We only store ranges that begin at the locus and that are sufficiently
126 "sane". */
127 if (src_range.m_start != locus)
128 return false;
130 if (src_range.m_finish < src_range.m_start)
131 return false;
133 if (src_range.m_start < RESERVED_LOCATION_COUNT)
134 return false;
136 if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
137 return false;
139 /* All 3 locations must be within ordinary maps, typically, the same
140 ordinary map. */
141 source_location lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
142 if (locus >= lowest_macro_loc)
143 return false;
144 if (src_range.m_start >= lowest_macro_loc)
145 return false;
146 if (src_range.m_finish >= lowest_macro_loc)
147 return false;
149 /* Passed all tests. */
150 return true;
153 /* Combine LOCUS and DATA to a combined adhoc loc. */
155 source_location
156 get_combined_adhoc_loc (struct line_maps *set,
157 source_location locus,
158 source_range src_range,
159 void *data)
161 struct location_adhoc_data lb;
162 struct location_adhoc_data **slot;
164 if (IS_ADHOC_LOC (locus))
165 locus
166 = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
167 if (locus == 0 && data == NULL)
168 return 0;
170 /* Any ordinary locations ought to be "pure" at this point: no
171 compressed ranges. */
172 linemap_assert (locus < RESERVED_LOCATION_COUNT
173 || locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
174 || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
175 || pure_location_p (set, locus));
177 /* Consider short-range optimization. */
178 if (can_be_stored_compactly_p (set, locus, src_range, data))
180 /* The low bits ought to be clear. */
181 linemap_assert (pure_location_p (set, locus));
182 const line_map *map = linemap_lookup (set, locus);
183 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
184 unsigned int int_diff = src_range.m_finish - src_range.m_start;
185 unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
186 if (col_diff < (1U << ordmap->m_range_bits))
188 source_location packed = locus | col_diff;
189 set->num_optimized_ranges++;
190 return packed;
194 /* We can also compactly store locations
195 when locus == start == finish (and data is NULL). */
196 if (locus == src_range.m_start
197 && locus == src_range.m_finish
198 && !data)
199 return locus;
201 if (!data)
202 set->num_unoptimized_ranges++;
204 lb.locus = locus;
205 lb.src_range = src_range;
206 lb.data = data;
207 slot = (struct location_adhoc_data **)
208 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
209 if (*slot == NULL)
211 if (set->location_adhoc_data_map.curr_loc >=
212 set->location_adhoc_data_map.allocated)
214 char *orig_data = (char *) set->location_adhoc_data_map.data;
215 int64_t offset;
216 /* Cast away extern "C" from the type of xrealloc. */
217 line_map_realloc reallocator = (set->reallocator
218 ? set->reallocator
219 : (line_map_realloc) xrealloc);
221 if (set->location_adhoc_data_map.allocated == 0)
222 set->location_adhoc_data_map.allocated = 128;
223 else
224 set->location_adhoc_data_map.allocated *= 2;
225 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
226 reallocator (set->location_adhoc_data_map.data,
227 set->location_adhoc_data_map.allocated
228 * sizeof (struct location_adhoc_data));
229 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
230 if (set->location_adhoc_data_map.allocated > 128)
231 htab_traverse (set->location_adhoc_data_map.htab,
232 location_adhoc_data_update, &offset);
234 *slot = set->location_adhoc_data_map.data
235 + set->location_adhoc_data_map.curr_loc;
236 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
237 = lb;
239 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
242 /* Return the data for the adhoc loc. */
244 void *
245 get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
247 linemap_assert (IS_ADHOC_LOC (loc));
248 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
251 /* Return the location for the adhoc loc. */
253 source_location
254 get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
256 linemap_assert (IS_ADHOC_LOC (loc));
257 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
260 /* Return the source_range for adhoc location LOC. */
262 static source_range
263 get_range_from_adhoc_loc (struct line_maps *set, source_location loc)
265 linemap_assert (IS_ADHOC_LOC (loc));
266 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].src_range;
269 /* Get the source_range of location LOC, either from the ad-hoc
270 lookaside table, or embedded inside LOC itself. */
272 source_range
273 get_range_from_loc (struct line_maps *set,
274 source_location loc)
276 if (IS_ADHOC_LOC (loc))
277 return get_range_from_adhoc_loc (set, loc);
279 /* For ordinary maps, extract packed range. */
280 if (loc >= RESERVED_LOCATION_COUNT
281 && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
282 && loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
284 const line_map *map = linemap_lookup (set, loc);
285 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
286 source_range result;
287 int offset = loc & ((1 << ordmap->m_range_bits) - 1);
288 result.m_start = loc - offset;
289 result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
290 return result;
293 return source_range::from_location (loc);
296 /* Get whether location LOC is a "pure" location, or
297 whether it is an ad-hoc location, or embeds range information. */
299 bool
300 pure_location_p (line_maps *set, source_location loc)
302 if (IS_ADHOC_LOC (loc))
303 return false;
305 const line_map *map = linemap_lookup (set, loc);
306 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
308 if (loc & ((1U << ordmap->m_range_bits) - 1))
309 return false;
311 return true;
314 /* Given location LOC within SET, strip away any packed range information
315 or ad-hoc information. */
317 source_location
318 get_pure_location (line_maps *set, source_location loc)
320 if (IS_ADHOC_LOC (loc))
322 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
324 if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
325 return loc;
327 if (loc < RESERVED_LOCATION_COUNT)
328 return loc;
330 const line_map *map = linemap_lookup (set, loc);
331 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
333 return loc & ~((1 << ordmap->m_range_bits) - 1);
336 /* Finalize the location_adhoc_data structure. */
337 void
338 location_adhoc_data_fini (struct line_maps *set)
340 htab_delete (set->location_adhoc_data_map.htab);
343 /* Initialize a line map set. */
345 void
346 linemap_init (struct line_maps *set,
347 source_location builtin_location)
349 memset (set, 0, sizeof (struct line_maps));
350 set->highest_location = RESERVED_LOCATION_COUNT - 1;
351 set->highest_line = RESERVED_LOCATION_COUNT - 1;
352 set->location_adhoc_data_map.htab =
353 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
354 set->builtin_location = builtin_location;
357 /* Check for and warn about line_maps entered but not exited. */
359 void
360 linemap_check_files_exited (struct line_maps *set)
362 const line_map_ordinary *map;
363 /* Depending upon whether we are handling preprocessed input or
364 not, this can be a user error or an ICE. */
365 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
366 ! MAIN_FILE_P (map);
367 map = INCLUDED_FROM (set, map))
368 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
369 ORDINARY_MAP_FILE_NAME (map));
372 /* Create a new line map in the line map set SET, and return it.
373 REASON is the reason of creating the map. It determines the type
374 of map created (ordinary or macro map). Note that ordinary maps and
375 macro maps are allocated in different memory location. */
377 static struct line_map *
378 new_linemap (struct line_maps *set,
379 enum lc_reason reason)
381 /* Depending on this variable, a macro map would be allocated in a
382 different memory location than an ordinary map. */
383 bool macro_map_p = (reason == LC_ENTER_MACRO);
384 struct line_map *result;
386 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
388 /* We ran out of allocated line maps. Let's allocate more. */
389 size_t alloc_size;
391 /* Cast away extern "C" from the type of xrealloc. */
392 line_map_realloc reallocator = (set->reallocator
393 ? set->reallocator
394 : (line_map_realloc) xrealloc);
395 line_map_round_alloc_size_func round_alloc_size =
396 set->round_alloc_size;
398 size_t map_size = (macro_map_p
399 ? sizeof (line_map_macro)
400 : sizeof (line_map_ordinary));
402 /* We are going to execute some dance to try to reduce the
403 overhead of the memory allocator, in case we are using the
404 ggc-page.c one.
406 The actual size of memory we are going to get back from the
407 allocator is the smallest power of 2 that is greater than the
408 size we requested. So let's consider that size then. */
410 alloc_size =
411 (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
412 * map_size;
414 /* Get the actual size of memory that is going to be allocated
415 by the allocator. */
416 alloc_size = round_alloc_size (alloc_size);
418 /* Now alloc_size contains the exact memory size we would get if
419 we have asked for the initial alloc_size amount of memory.
420 Let's get back to the number of macro map that amounts
421 to. */
422 LINEMAPS_ALLOCATED (set, macro_map_p) =
423 alloc_size / map_size;
425 /* And now let's really do the re-allocation. */
426 if (macro_map_p)
428 set->info_macro.maps
429 = (line_map_macro *) (*reallocator) (set->info_macro.maps,
430 (LINEMAPS_ALLOCATED (set, macro_map_p)
431 * map_size));
432 result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
434 else
436 set->info_ordinary.maps =
437 (line_map_ordinary *) (*reallocator) (set->info_ordinary.maps,
438 (LINEMAPS_ALLOCATED (set, macro_map_p)
439 * map_size));
440 result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
442 memset (result, 0,
443 ((LINEMAPS_ALLOCATED (set, macro_map_p)
444 - LINEMAPS_USED (set, macro_map_p))
445 * map_size));
447 else
449 if (macro_map_p)
450 result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
451 else
452 result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
455 LINEMAPS_USED (set, macro_map_p)++;
457 result->reason = reason;
458 return result;
461 /* Add a mapping of logical source line to physical source file and
462 line number.
464 The text pointed to by TO_FILE must have a lifetime
465 at least as long as the final call to lookup_line (). An empty
466 TO_FILE means standard input. If reason is LC_LEAVE, and
467 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
468 natural values considering the file we are returning to.
470 FROM_LINE should be monotonic increasing across calls to this
471 function. A call to this function can relocate the previous set of
472 maps, so any stored line_map pointers should not be used. */
474 const struct line_map *
475 linemap_add (struct line_maps *set, enum lc_reason reason,
476 unsigned int sysp, const char *to_file, linenum_type to_line)
478 /* Generate a start_location above the current highest_location.
479 If possible, make the low range bits be zero. */
480 source_location start_location;
481 if (set->highest_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
483 start_location = set->highest_location + (1 << set->default_range_bits);
484 if (set->default_range_bits)
485 start_location &= ~((1 << set->default_range_bits) - 1);
486 linemap_assert (0 == (start_location
487 & ((1 << set->default_range_bits) - 1)));
489 else
490 start_location = set->highest_location + 1;
492 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
493 && (start_location
494 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
496 /* When we enter the file for the first time reason cannot be
497 LC_RENAME. */
498 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
500 /* If we are leaving the main file, return a NULL map. */
501 if (reason == LC_LEAVE
502 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
503 && to_file == NULL)
505 set->depth--;
506 return NULL;
509 linemap_assert (reason != LC_ENTER_MACRO);
510 line_map_ordinary *map = linemap_check_ordinary (new_linemap (set, reason));
512 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
513 to_file = "<stdin>";
515 if (reason == LC_RENAME_VERBATIM)
516 reason = LC_RENAME;
518 if (reason == LC_LEAVE)
520 /* When we are just leaving an "included" file, and jump to the next
521 location inside the "includer" right after the #include
522 "included", this variable points the map in use right before the
523 #include "included", inside the same "includer" file. */
524 line_map_ordinary *from;
526 linemap_assert (!MAIN_FILE_P (map - 1));
527 /* (MAP - 1) points to the map we are leaving. The
528 map from which (MAP - 1) got included should be the map
529 that comes right before MAP in the same file. */
530 from = INCLUDED_FROM (set, map - 1);
532 /* A TO_FILE of NULL is special - we use the natural values. */
533 if (to_file == NULL)
535 to_file = ORDINARY_MAP_FILE_NAME (from);
536 to_line = SOURCE_LINE (from, from[1].start_location);
537 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
539 else
540 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from),
541 to_file) == 0);
544 map->sysp = sysp;
545 map->start_location = start_location;
546 map->to_file = to_file;
547 map->to_line = to_line;
548 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
549 map->m_column_and_range_bits = 0;
550 map->m_range_bits = 0;
551 set->highest_location = start_location;
552 set->highest_line = start_location;
553 set->max_column_hint = 0;
555 /* This assertion is placed after set->highest_location has
556 been updated, since the latter affects
557 linemap_location_from_macro_expansion_p, which ultimately affects
558 pure_location_p. */
559 linemap_assert (pure_location_p (set, start_location));
561 if (reason == LC_ENTER)
563 map->included_from =
564 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
565 set->depth++;
566 if (set->trace_includes)
567 trace_include (set, map);
569 else if (reason == LC_RENAME)
570 map->included_from = ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
571 else if (reason == LC_LEAVE)
573 set->depth--;
574 map->included_from =
575 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
578 return map;
581 /* Returns TRUE if the line table set tracks token locations across
582 macro expansion, FALSE otherwise. */
584 bool
585 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
587 return LINEMAPS_MACRO_MAPS (set) != NULL;
590 /* Create a macro map. A macro map encodes source locations of tokens
591 that are part of a macro replacement-list, at a macro expansion
592 point. See the extensive comments of struct line_map and struct
593 line_map_macro, in line-map.h.
595 This map shall be created when the macro is expanded. The map
596 encodes the source location of the expansion point of the macro as
597 well as the "original" source location of each token that is part
598 of the macro replacement-list. If a macro is defined but never
599 expanded, it has no macro map. SET is the set of maps the macro
600 map should be part of. MACRO_NODE is the macro which the new macro
601 map should encode source locations for. EXPANSION is the location
602 of the expansion point of MACRO. For function-like macros
603 invocations, it's best to make it point to the closing parenthesis
604 of the macro, rather than the the location of the first character
605 of the macro. NUM_TOKENS is the number of tokens that are part of
606 the replacement-list of MACRO.
608 Note that when we run out of the integer space available for source
609 locations, this function returns NULL. In that case, callers of
610 this function cannot encode {line,column} pairs into locations of
611 macro tokens anymore. */
613 const line_map_macro *
614 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
615 source_location expansion, unsigned int num_tokens)
617 line_map_macro *map;
618 source_location start_location;
619 /* Cast away extern "C" from the type of xrealloc. */
620 line_map_realloc reallocator = (set->reallocator
621 ? set->reallocator
622 : (line_map_realloc) xrealloc);
624 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
626 if (start_location <= set->highest_line
627 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
628 /* We ran out of macro map space. */
629 return NULL;
631 map = linemap_check_macro (new_linemap (set, LC_ENTER_MACRO));
633 map->start_location = start_location;
634 map->macro = macro_node;
635 map->n_tokens = num_tokens;
636 map->macro_locations
637 = (source_location*) reallocator (NULL,
638 2 * num_tokens
639 * sizeof (source_location));
640 map->expansion = expansion;
641 memset (MACRO_MAP_LOCATIONS (map), 0,
642 num_tokens * sizeof (source_location));
644 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
646 return map;
649 /* Create and return a virtual location for a token that is part of a
650 macro expansion-list at a macro expansion point. See the comment
651 inside struct line_map_macro to see what an expansion-list exactly
654 A call to this function must come after a call to
655 linemap_enter_macro.
657 MAP is the map into which the source location is created. TOKEN_NO
658 is the index of the token in the macro replacement-list, starting
659 at number 0.
661 ORIG_LOC is the location of the token outside of this macro
662 expansion. If the token comes originally from the macro
663 definition, it is the locus in the macro definition; otherwise it
664 is a location in the context of the caller of this macro expansion
665 (which is a virtual location or a source location if the caller is
666 itself a macro expansion or not).
668 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
669 either of the token itself or of a macro parameter that it
670 replaces. */
672 source_location
673 linemap_add_macro_token (const line_map_macro *map,
674 unsigned int token_no,
675 source_location orig_loc,
676 source_location orig_parm_replacement_loc)
678 source_location result;
680 linemap_assert (linemap_macro_expansion_map_p (map));
681 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
683 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
684 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
686 result = MAP_START_LOCATION (map) + token_no;
687 return result;
690 /* Return a source_location for the start (i.e. column==0) of
691 (physical) line TO_LINE in the current source file (as in the
692 most recent linemap_add). MAX_COLUMN_HINT is the highest column
693 number we expect to use in this line (but it does not change
694 the highest_location). */
696 source_location
697 linemap_line_start (struct line_maps *set, linenum_type to_line,
698 unsigned int max_column_hint)
700 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
701 source_location highest = set->highest_location;
702 source_location r;
703 linenum_type last_line =
704 SOURCE_LINE (map, set->highest_line);
705 int line_delta = to_line - last_line;
706 bool add_map = false;
707 linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
708 int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
710 if (line_delta < 0
711 || (line_delta > 10
712 && line_delta * map->m_column_and_range_bits > 1000)
713 || (max_column_hint >= (1U << effective_column_bits))
714 || (max_column_hint <= 80 && effective_column_bits >= 10)
715 || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
716 && map->m_range_bits > 0)
717 || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
718 && (set->max_column_hint || highest >= LINE_MAP_MAX_SOURCE_LOCATION)))
719 add_map = true;
720 else
721 max_column_hint = set->max_column_hint;
722 if (add_map)
724 int column_bits;
725 int range_bits;
726 if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
727 || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
729 /* If the column number is ridiculous or we've allocated a huge
730 number of source_locations, give up on column numbers
731 (and on packed ranges). */
732 max_column_hint = 0;
733 column_bits = 0;
734 range_bits = 0;
735 if (highest > LINE_MAP_MAX_SOURCE_LOCATION)
736 return 0;
738 else
740 column_bits = 7;
741 if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
742 range_bits = set->default_range_bits;
743 else
744 range_bits = 0;
745 while (max_column_hint >= (1U << column_bits))
746 column_bits++;
747 max_column_hint = 1U << column_bits;
748 column_bits += range_bits;
750 /* Allocate the new line_map. However, if the current map only has a
751 single line we can sometimes just increase its column_bits instead. */
752 if (line_delta < 0
753 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
754 || SOURCE_COLUMN (map, highest) >= (1U << column_bits)
755 || range_bits < map->m_range_bits)
756 map = linemap_check_ordinary
757 (const_cast <line_map *>
758 (linemap_add (set, LC_RENAME,
759 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
760 ORDINARY_MAP_FILE_NAME (map),
761 to_line)));
762 map->m_column_and_range_bits = column_bits;
763 map->m_range_bits = range_bits;
764 r = (MAP_START_LOCATION (map)
765 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
766 << column_bits));
768 else
769 r = set->highest_line + (line_delta << map->m_column_and_range_bits);
771 /* Locations of ordinary tokens are always lower than locations of
772 macro tokens. */
773 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
774 return 0;
776 set->highest_line = r;
777 if (r > set->highest_location)
778 set->highest_location = r;
779 set->max_column_hint = max_column_hint;
781 /* At this point, we expect one of:
782 (a) the normal case: a "pure" location with 0 range bits, or
783 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
784 columns anymore (or ranges), or
785 (c) we're in a region with a column hint exceeding
786 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
787 with column_bits == 0. */
788 linemap_assert (pure_location_p (set, r)
789 || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
790 || map->m_column_and_range_bits == 0);
791 linemap_assert (SOURCE_LINE (map, r) == to_line);
792 return r;
795 /* Encode and return a source_location from a column number. The
796 source line considered is the last source line used to call
797 linemap_line_start, i.e, the last source line which a location was
798 encoded from. */
800 source_location
801 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
803 source_location r = set->highest_line;
805 linemap_assert
806 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
808 if (to_column >= set->max_column_hint)
810 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
811 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
813 /* Running low on source_locations - disable column numbers. */
814 return r;
816 else
818 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
819 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
822 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
823 r = r + (to_column << map->m_range_bits);
824 if (r >= set->highest_location)
825 set->highest_location = r;
826 return r;
829 /* Encode and return a source location from a given line and
830 column. */
832 source_location
833 linemap_position_for_line_and_column (line_maps *set,
834 const line_map_ordinary *ord_map,
835 linenum_type line,
836 unsigned column)
838 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
840 source_location r = MAP_START_LOCATION (ord_map);
841 r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
842 << ord_map->m_column_and_range_bits);
843 if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
844 r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
845 << ord_map->m_range_bits);
846 source_location upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
847 if (r >= upper_limit)
848 r = upper_limit - 1;
849 if (r > set->highest_location)
850 set->highest_location = r;
851 return r;
854 /* Encode and return a source_location starting from location LOC and
855 shifting it by COLUMN_OFFSET columns. This function does not support
856 virtual locations. */
858 source_location
859 linemap_position_for_loc_and_offset (struct line_maps *set,
860 source_location loc,
861 unsigned int column_offset)
863 const line_map_ordinary * map = NULL;
865 if (IS_ADHOC_LOC (loc))
866 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
868 /* This function does not support virtual locations yet. */
869 if (linemap_assert_fails
870 (!linemap_location_from_macro_expansion_p (set, loc)))
871 return loc;
873 if (column_offset == 0
874 /* Adding an offset to a reserved location (like
875 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
876 sense. So let's leave the location intact in that case. */
877 || loc < RESERVED_LOCATION_COUNT)
878 return loc;
880 /* We find the real location and shift it. */
881 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
882 /* The new location (loc + offset) should be higher than the first
883 location encoded by MAP. This can fail if the line information
884 is messed up because of line directives (see PR66415). */
885 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
886 return loc;
888 linenum_type line = SOURCE_LINE (map, loc);
889 unsigned int column = SOURCE_COLUMN (map, loc);
891 /* If MAP is not the last line map of its set, then the new location
892 (loc + offset) should be less than the first location encoded by
893 the next line map of the set. Otherwise, we try to encode the
894 location in the next map. */
895 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
896 && (loc + (column_offset << map->m_range_bits)
897 >= MAP_START_LOCATION (&map[1])))
899 map = &map[1];
900 /* If the next map starts in a higher line, we cannot encode the
901 location there. */
902 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
903 return loc;
906 column += column_offset;
907 if (linemap_assert_fails (column < (1u << map->m_column_and_range_bits)))
908 return loc;
910 source_location r =
911 linemap_position_for_line_and_column (set, map, line, column);
912 if (linemap_assert_fails (r <= set->highest_location)
913 || linemap_assert_fails (map == linemap_lookup (set, r)))
914 return loc;
916 return r;
919 /* Given a virtual source location yielded by a map (either an
920 ordinary or a macro map), returns that map. */
922 const struct line_map*
923 linemap_lookup (struct line_maps *set, source_location line)
925 if (IS_ADHOC_LOC (line))
926 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
927 if (linemap_location_from_macro_expansion_p (set, line))
928 return linemap_macro_map_lookup (set, line);
929 return linemap_ordinary_map_lookup (set, line);
932 /* Given a source location yielded by an ordinary map, returns that
933 map. Since the set is built chronologically, the logical lines are
934 monotonic increasing, and so the list is sorted and we can use a
935 binary search. */
937 static const line_map_ordinary *
938 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
940 unsigned int md, mn, mx;
941 const line_map_ordinary *cached, *result;
943 if (IS_ADHOC_LOC (line))
944 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
946 if (set == NULL || line < RESERVED_LOCATION_COUNT)
947 return NULL;
949 mn = LINEMAPS_ORDINARY_CACHE (set);
950 mx = LINEMAPS_ORDINARY_USED (set);
952 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
953 /* We should get a segfault if no line_maps have been added yet. */
954 if (line >= MAP_START_LOCATION (cached))
956 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
957 return cached;
959 else
961 mx = mn;
962 mn = 0;
965 while (mx - mn > 1)
967 md = (mn + mx) / 2;
968 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
969 mx = md;
970 else
971 mn = md;
974 LINEMAPS_ORDINARY_CACHE (set) = mn;
975 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
976 linemap_assert (line >= MAP_START_LOCATION (result));
977 return result;
980 /* Given a source location yielded by a macro map, returns that map.
981 Since the set is built chronologically, the logical lines are
982 monotonic decreasing, and so the list is sorted and we can use a
983 binary search. */
985 static const line_map_macro *
986 linemap_macro_map_lookup (struct line_maps *set, source_location line)
988 unsigned int md, mn, mx;
989 const struct line_map_macro *cached, *result;
991 if (IS_ADHOC_LOC (line))
992 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
994 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
996 if (set == NULL)
997 return NULL;
999 mn = LINEMAPS_MACRO_CACHE (set);
1000 mx = LINEMAPS_MACRO_USED (set);
1001 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
1003 if (line >= MAP_START_LOCATION (cached))
1005 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
1006 return cached;
1007 mx = mn - 1;
1008 mn = 0;
1011 while (mn < mx)
1013 md = (mx + mn) / 2;
1014 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1015 mn = md + 1;
1016 else
1017 mx = md;
1020 LINEMAPS_MACRO_CACHE (set) = mx;
1021 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
1022 linemap_assert (MAP_START_LOCATION (result) <= line);
1024 return result;
1027 /* Return TRUE if MAP encodes locations coming from a macro
1028 replacement-list at macro expansion point. */
1030 bool
1031 linemap_macro_expansion_map_p (const struct line_map *map)
1033 if (!map)
1034 return false;
1035 return (map->reason == LC_ENTER_MACRO);
1038 /* If LOCATION is the locus of a token in a replacement-list of a
1039 macro expansion return the location of the macro expansion point.
1041 Read the comments of struct line_map and struct line_map_macro in
1042 line-map.h to understand what a macro expansion point is. */
1044 static source_location
1045 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1046 source_location location ATTRIBUTE_UNUSED)
1048 linemap_assert (linemap_macro_expansion_map_p (map)
1049 && location >= MAP_START_LOCATION (map));
1051 /* Make sure LOCATION is correct. */
1052 linemap_assert ((location - MAP_START_LOCATION (map))
1053 < MACRO_MAP_NUM_MACRO_TOKENS (map));
1055 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1058 /* LOCATION is the source location of a token that belongs to a macro
1059 replacement-list as part of the macro expansion denoted by MAP.
1061 Return the location of the token at the definition point of the
1062 macro. */
1064 static source_location
1065 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1066 source_location location)
1068 unsigned token_no;
1070 linemap_assert (linemap_macro_expansion_map_p (map)
1071 && location >= MAP_START_LOCATION (map));
1072 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1074 token_no = location - MAP_START_LOCATION (map);
1075 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1077 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1079 return location;
1082 /* If LOCATION is the locus of a token that is an argument of a
1083 function-like macro M and appears in the expansion of M, return the
1084 locus of that argument in the context of the caller of M.
1086 In other words, this returns the xI location presented in the
1087 comments of line_map_macro above. */
1088 source_location
1089 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1090 const line_map_macro* map,
1091 source_location location)
1093 unsigned token_no;
1095 if (IS_ADHOC_LOC (location))
1096 location = get_location_from_adhoc_loc (set, location);
1098 linemap_assert (linemap_macro_expansion_map_p (map)
1099 && location >= MAP_START_LOCATION (map));
1100 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1101 linemap_assert (!IS_ADHOC_LOC (location));
1103 token_no = location - MAP_START_LOCATION (map);
1104 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1106 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1108 return location;
1111 /* Return the source line number corresponding to source location
1112 LOCATION. SET is the line map set LOCATION comes from. If
1113 LOCATION is the source location of token that is part of the
1114 replacement-list of a macro expansion return the line number of the
1115 macro expansion point. */
1118 linemap_get_expansion_line (struct line_maps *set,
1119 source_location location)
1121 const line_map_ordinary *map = NULL;
1123 if (IS_ADHOC_LOC (location))
1124 location = set->location_adhoc_data_map.data[location
1125 & MAX_SOURCE_LOCATION].locus;
1127 if (location < RESERVED_LOCATION_COUNT)
1128 return 0;
1130 location =
1131 linemap_macro_loc_to_exp_point (set, location, &map);
1133 return SOURCE_LINE (map, location);
1136 /* Return the path of the file corresponding to source code location
1137 LOCATION.
1139 If LOCATION is the source location of token that is part of the
1140 replacement-list of a macro expansion return the file path of the
1141 macro expansion point.
1143 SET is the line map set LOCATION comes from. */
1145 const char*
1146 linemap_get_expansion_filename (struct line_maps *set,
1147 source_location location)
1149 const struct line_map_ordinary *map = NULL;
1151 if (IS_ADHOC_LOC (location))
1152 location = set->location_adhoc_data_map.data[location
1153 & MAX_SOURCE_LOCATION].locus;
1155 if (location < RESERVED_LOCATION_COUNT)
1156 return NULL;
1158 location =
1159 linemap_macro_loc_to_exp_point (set, location, &map);
1161 return LINEMAP_FILE (map);
1164 /* Return the name of the macro associated to MACRO_MAP. */
1166 const char*
1167 linemap_map_get_macro_name (const line_map_macro *macro_map)
1169 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1170 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1173 /* Return a positive value if LOCATION is the locus of a token that is
1174 located in a system header, O otherwise. It returns 1 if LOCATION
1175 is the locus of a token that is located in a system header, and 2
1176 if LOCATION is the locus of a token located in a C system header
1177 that therefore needs to be extern "C" protected in C++.
1179 Note that this function returns 1 if LOCATION belongs to a token
1180 that is part of a macro replacement-list defined in a system
1181 header, but expanded in a non-system file. */
1184 linemap_location_in_system_header_p (struct line_maps *set,
1185 source_location location)
1187 const struct line_map *map = NULL;
1189 if (IS_ADHOC_LOC (location))
1190 location = set->location_adhoc_data_map.data[location
1191 & MAX_SOURCE_LOCATION].locus;
1193 if (location < RESERVED_LOCATION_COUNT)
1194 return false;
1196 /* Let's look at where the token for LOCATION comes from. */
1197 while (true)
1199 map = linemap_lookup (set, location);
1200 if (map != NULL)
1202 if (!linemap_macro_expansion_map_p (map))
1203 /* It's a normal token. */
1204 return LINEMAP_SYSP (linemap_check_ordinary (map));
1205 else
1207 const line_map_macro *macro_map = linemap_check_macro (map);
1209 /* It's a token resulting from a macro expansion. */
1210 source_location loc =
1211 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
1212 if (loc < RESERVED_LOCATION_COUNT)
1213 /* This token might come from a built-in macro. Let's
1214 look at where that macro got expanded. */
1215 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1216 else
1217 location = loc;
1220 else
1221 break;
1223 return false;
1226 /* Return TRUE if LOCATION is a source code location of a token coming
1227 from a macro replacement-list at a macro expansion point, FALSE
1228 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;
1574 Suppose that LOC is the virtual location of a token T coming from
1575 the expansion of a macro M. This function then steps up to get the
1576 location L of the point where M got expanded. If L is a spelling
1577 location inside a macro expansion M', then this function returns
1578 the locus of the point where M' was expanded. Said otherwise, this
1579 function returns the location of T in the context that triggered
1580 the expansion of M.
1582 *LOC_MAP must be set to the map of LOC. This function then sets it
1583 to the map of the returned location. */
1585 source_location
1586 linemap_unwind_toward_expansion (struct line_maps *set,
1587 source_location loc,
1588 const struct line_map **map)
1590 source_location resolved_location;
1591 const line_map_macro *macro_map = linemap_check_macro (*map);
1592 const struct line_map *resolved_map;
1594 if (IS_ADHOC_LOC (loc))
1595 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1597 resolved_location =
1598 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1599 resolved_map = linemap_lookup (set, resolved_location);
1601 if (!linemap_macro_expansion_map_p (resolved_map))
1603 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1604 resolved_map = linemap_lookup (set, resolved_location);
1607 *map = resolved_map;
1608 return resolved_location;
1611 /* If LOC is the virtual location of a token coming from the expansion
1612 of a macro M and if its spelling location is reserved (e.g, a
1613 location for a built-in token), then this function unwinds (using
1614 linemap_unwind_toward_expansion) the location until a location that
1615 is not reserved and is not in a system header is reached. In other
1616 words, this unwinds the reserved location until a location that is
1617 in real source code is reached.
1619 Otherwise, if the spelling location for LOC is not reserved or if
1620 LOC doesn't come from the expansion of a macro, the function
1621 returns LOC as is and *MAP is not touched.
1623 *MAP is set to the map of the returned location if the later is
1624 different from LOC. */
1625 source_location
1626 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1627 source_location loc,
1628 const struct line_map **map)
1630 source_location resolved_loc;
1631 const struct line_map *map0 = NULL;
1632 const line_map_ordinary *map1 = NULL;
1634 if (IS_ADHOC_LOC (loc))
1635 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1637 map0 = linemap_lookup (set, loc);
1638 if (!linemap_macro_expansion_map_p (map0))
1639 return loc;
1641 resolved_loc = linemap_resolve_location (set, loc,
1642 LRK_SPELLING_LOCATION,
1643 &map1);
1645 if (resolved_loc >= RESERVED_LOCATION_COUNT
1646 && !LINEMAP_SYSP (map1))
1647 return loc;
1649 while (linemap_macro_expansion_map_p (map0)
1650 && (resolved_loc < RESERVED_LOCATION_COUNT
1651 || LINEMAP_SYSP (map1)))
1653 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1654 resolved_loc = linemap_resolve_location (set, loc,
1655 LRK_SPELLING_LOCATION,
1656 &map1);
1659 if (map != NULL)
1660 *map = map0;
1661 return loc;
1664 /* Expand source code location LOC and return a user readable source
1665 code location. LOC must be a spelling (non-virtual) location. If
1666 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1667 location is returned. */
1669 expanded_location
1670 linemap_expand_location (struct line_maps *set,
1671 const struct line_map *map,
1672 source_location loc)
1675 expanded_location xloc;
1677 memset (&xloc, 0, sizeof (xloc));
1678 if (IS_ADHOC_LOC (loc))
1680 xloc.data
1681 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1682 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1685 if (loc < RESERVED_LOCATION_COUNT)
1686 /* The location for this token wasn't generated from a line map.
1687 It was probably a location for a builtin token, chosen by some
1688 client code. Let's not try to expand the location in that
1689 case. */;
1690 else if (map == NULL)
1691 /* We shouldn't be getting a NULL map with a location that is not
1692 reserved by the client code. */
1693 abort ();
1694 else
1696 /* MAP must be an ordinary map and LOC must be non-virtual,
1697 encoded into this map, obviously; the accessors used on MAP
1698 below ensure it is ordinary. Let's just assert the
1699 non-virtualness of LOC here. */
1700 if (linemap_location_from_macro_expansion_p (set, loc))
1701 abort ();
1703 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1705 xloc.file = LINEMAP_FILE (ord_map);
1706 xloc.line = SOURCE_LINE (ord_map, loc);
1707 xloc.column = SOURCE_COLUMN (ord_map, loc);
1708 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1711 return xloc;
1715 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1716 is NULL, use stderr. IS_MACRO is true if the caller wants to
1717 dump a macro map, false otherwise. */
1719 void
1720 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1722 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1723 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1724 "LC_ENTER_MACRO" };
1725 const char *reason;
1726 const line_map *map;
1728 if (stream == NULL)
1729 stream = stderr;
1731 if (!is_macro)
1732 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1733 else
1734 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1736 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1738 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1739 ix, (void *) map, map->start_location, reason,
1740 ((!is_macro
1741 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1742 ? "yes" : "no"));
1743 if (!is_macro)
1745 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1746 unsigned includer_ix;
1747 const line_map_ordinary *includer_map;
1749 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
1750 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1751 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1752 : NULL;
1754 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1755 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1756 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1757 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1759 else
1761 const line_map_macro *macro_map = linemap_check_macro (map);
1762 fprintf (stream, "Macro: %s (%u tokens)\n",
1763 linemap_map_get_macro_name (macro_map),
1764 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1767 fprintf (stream, "\n");
1771 /* Dump debugging information about source location LOC into the file
1772 stream STREAM. SET is the line map set LOC comes from. */
1774 void
1775 linemap_dump_location (struct line_maps *set,
1776 source_location loc,
1777 FILE *stream)
1779 const line_map_ordinary *map;
1780 source_location location;
1781 const char *path = "", *from = "";
1782 int l = -1, c = -1, s = -1, e = -1;
1784 if (IS_ADHOC_LOC (loc))
1785 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1787 if (loc == 0)
1788 return;
1790 location =
1791 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1793 if (map == NULL)
1794 /* Only reserved locations can be tolerated in this case. */
1795 linemap_assert (location < RESERVED_LOCATION_COUNT);
1796 else
1798 path = LINEMAP_FILE (map);
1799 l = SOURCE_LINE (map, location);
1800 c = SOURCE_COLUMN (map, location);
1801 s = LINEMAP_SYSP (map) != 0;
1802 e = location != loc;
1803 if (e)
1804 from = "N/A";
1805 else
1806 from = (INCLUDED_FROM (set, map))
1807 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1808 : "<NULL>";
1811 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1812 E: macro expansion?, LOC: original location, R: resolved location */
1813 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1814 path, from, l, c, s, (void*)map, e, loc, location);
1817 /* Return the highest location emitted for a given file for which
1818 there is a line map in SET. FILE_NAME is the file name to
1819 consider. If the function returns TRUE, *LOC is set to the highest
1820 location emitted for that file. */
1822 bool
1823 linemap_get_file_highest_location (struct line_maps *set,
1824 const char *file_name,
1825 source_location *loc)
1827 /* If the set is empty or no ordinary map has been created then
1828 there is no file to look for ... */
1829 if (set == NULL || set->info_ordinary.used == 0)
1830 return false;
1832 /* Now look for the last ordinary map created for FILE_NAME. */
1833 int i;
1834 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1836 const char *fname = set->info_ordinary.maps[i].to_file;
1837 if (fname && !filename_cmp (fname, file_name))
1838 break;
1841 if (i < 0)
1842 return false;
1844 /* The highest location for a given map is either the starting
1845 location of the next map minus one, or -- if the map is the
1846 latest one -- the highest location of the set. */
1847 source_location result;
1848 if (i == (int) set->info_ordinary.used - 1)
1849 result = set->highest_location;
1850 else
1851 result = set->info_ordinary.maps[i + 1].start_location - 1;
1853 *loc = result;
1854 return true;
1857 /* Compute and return statistics about the memory consumption of some
1858 parts of the line table SET. */
1860 void
1861 linemap_get_statistics (struct line_maps *set,
1862 struct linemap_stats *s)
1864 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1865 macro_maps_allocated_size, macro_maps_used_size,
1866 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1868 const line_map_macro *cur_map;
1870 ordinary_maps_allocated_size =
1871 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1873 ordinary_maps_used_size =
1874 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1876 macro_maps_allocated_size =
1877 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1879 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1880 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1881 ++cur_map)
1883 unsigned i;
1885 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1887 macro_maps_locations_size +=
1888 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1890 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1892 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1893 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1894 duplicated_macro_maps_locations_size +=
1895 sizeof (source_location);
1899 macro_maps_used_size =
1900 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1902 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1903 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1904 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1905 s->ordinary_maps_used_size = ordinary_maps_used_size;
1906 s->num_expanded_macros = num_expanded_macros_counter;
1907 s->num_macro_tokens = num_macro_tokens_counter;
1908 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1909 s->macro_maps_allocated_size = macro_maps_allocated_size;
1910 s->macro_maps_locations_size = macro_maps_locations_size;
1911 s->macro_maps_used_size = macro_maps_used_size;
1912 s->duplicated_macro_maps_locations_size =
1913 duplicated_macro_maps_locations_size;
1914 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1915 * sizeof (struct location_adhoc_data));
1916 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
1920 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1921 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1922 specifies how many macro maps to dump. */
1924 void
1925 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1926 unsigned int num_macro)
1928 unsigned int i;
1930 if (set == NULL)
1931 return;
1933 if (stream == NULL)
1934 stream = stderr;
1936 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1937 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1938 fprintf (stream, "Include stack depth: %d\n", set->depth);
1939 fprintf (stream, "Highest location: %u\n", set->highest_location);
1941 if (num_ordinary)
1943 fprintf (stream, "\nOrdinary line maps\n");
1944 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1945 linemap_dump (stream, set, i, false);
1946 fprintf (stream, "\n");
1949 if (num_macro)
1951 fprintf (stream, "\nMacro line maps\n");
1952 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1953 linemap_dump (stream, set, i, true);
1954 fprintf (stream, "\n");
1958 /* struct source_range. */
1960 /* Is there any part of this range on the given line? */
1962 bool
1963 source_range::intersects_line_p (const char *file, int line) const
1965 expanded_location exploc_start
1966 = linemap_client_expand_location_to_spelling_point (m_start);
1967 if (file != exploc_start.file)
1968 return false;
1969 if (line < exploc_start.line)
1970 return false;
1971 expanded_location exploc_finish
1972 = linemap_client_expand_location_to_spelling_point (m_finish);
1973 if (file != exploc_finish.file)
1974 return false;
1975 if (line > exploc_finish.line)
1976 return false;
1977 return true;
1980 /* class rich_location. */
1982 /* Construct a rich_location with location LOC as its initial range. */
1984 rich_location::rich_location (line_maps *set, source_location loc) :
1985 m_line_table (set),
1986 m_ranges (),
1987 m_column_override (0),
1988 m_have_expanded_location (false),
1989 m_fixit_hints (),
1990 m_seen_impossible_fixit (false)
1992 add_range (loc, true);
1995 /* The destructor for class rich_location. */
1997 rich_location::~rich_location ()
1999 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2000 delete get_fixit_hint (i);
2003 /* Get location IDX within this rich_location. */
2005 source_location
2006 rich_location::get_loc (unsigned int idx) const
2008 const location_range *locrange = get_range (idx);
2009 return locrange->m_loc;
2012 /* Get range IDX within this rich_location. */
2014 const location_range *
2015 rich_location::get_range (unsigned int idx) const
2017 return &m_ranges[idx];
2020 /* Mutable access to range IDX within this rich_location. */
2022 location_range *
2023 rich_location::get_range (unsigned int idx)
2025 return &m_ranges[idx];
2028 /* Expand location IDX within this rich_location. */
2029 /* Get an expanded_location for this rich_location's primary
2030 location. */
2032 expanded_location
2033 rich_location::get_expanded_location (unsigned int idx)
2035 if (idx == 0)
2037 /* Cache the expansion of the primary location. */
2038 if (!m_have_expanded_location)
2040 m_expanded_location
2041 = linemap_client_expand_location_to_spelling_point (get_loc (0));
2042 if (m_column_override)
2043 m_expanded_location.column = m_column_override;
2044 m_have_expanded_location = true;
2047 return m_expanded_location;
2049 else
2050 return linemap_client_expand_location_to_spelling_point (get_loc (idx));
2053 /* Set the column of the primary location, with 0 meaning
2054 "don't override it". */
2056 void
2057 rich_location::override_column (int column)
2059 m_column_override = column;
2060 m_have_expanded_location = false;
2063 /* Add the given range. */
2065 void
2066 rich_location::add_range (source_location loc, bool show_caret_p)
2068 location_range range;
2069 range.m_loc = loc;
2070 range.m_show_caret_p = show_caret_p;
2071 m_ranges.push (range);
2074 /* Add or overwrite the location given by IDX, setting its location to LOC,
2075 and setting its "should my caret be printed" flag to SHOW_CARET_P.
2077 It must either overwrite an existing location, or add one *exactly* on
2078 the end of the array.
2080 This is primarily for use by gcc when implementing diagnostic format
2081 decoders e.g.
2082 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2083 (which writes the source location of a tree back into location 0 of
2084 the rich_location), and
2085 - the "%C" and "%L" format codes in the Fortran frontend. */
2087 void
2088 rich_location::set_range (line_maps * /*set*/, unsigned int idx,
2089 source_location loc, bool show_caret_p)
2091 /* We can either overwrite an existing range, or add one exactly
2092 on the end of the array. */
2093 linemap_assert (idx <= m_ranges.count ());
2095 if (idx == m_ranges.count ())
2096 add_range (loc, show_caret_p);
2097 else
2099 location_range *locrange = get_range (idx);
2100 locrange->m_loc = loc;
2101 locrange->m_show_caret_p = show_caret_p;
2104 if (idx == 0)
2105 /* Mark any cached value here as dirty. */
2106 m_have_expanded_location = false;
2109 /* Methods for adding insertion fix-it hints. */
2111 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2112 immediately before the primary range's start location. */
2114 void
2115 rich_location::add_fixit_insert_before (const char *new_content)
2117 add_fixit_insert_before (get_loc (), new_content);
2120 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2121 immediately before the start of WHERE. */
2123 void
2124 rich_location::add_fixit_insert_before (source_location where,
2125 const char *new_content)
2127 source_location start = get_range_from_loc (m_line_table, where).m_start;
2129 if (reject_impossible_fixit (start))
2130 return;
2131 /* We do not yet support newlines within fix-it hints. */
2132 if (strchr (new_content, '\n'))
2134 stop_supporting_fixits ();
2135 return;
2137 add_fixit (new fixit_insert (start, new_content));
2140 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2141 immediately after the primary range's end-point. */
2143 void
2144 rich_location::add_fixit_insert_after (const char *new_content)
2146 add_fixit_insert_after (get_loc (), new_content);
2149 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2150 immediately after the end-point of WHERE. */
2152 void
2153 rich_location::add_fixit_insert_after (source_location where,
2154 const char *new_content)
2156 source_location finish = get_range_from_loc (m_line_table, where).m_finish;
2158 if (reject_impossible_fixit (finish))
2159 return;
2161 source_location next_loc
2162 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
2164 /* linemap_position_for_loc_and_offset can fail, if so, it returns
2165 its input value. */
2166 if (next_loc == finish)
2168 stop_supporting_fixits ();
2169 return;
2172 add_fixit (new fixit_insert (next_loc, new_content));
2175 /* Methods for adding removal fix-it hints. */
2177 /* Add a fixit-hint, suggesting removal of the content covered
2178 by range 0. */
2180 void
2181 rich_location::add_fixit_remove ()
2183 add_fixit_remove (get_loc ());
2186 /* Add a fixit-hint, suggesting removal of the content between
2187 the start and finish of WHERE. */
2189 void
2190 rich_location::add_fixit_remove (source_location where)
2192 source_range range = get_range_from_loc (m_line_table, where);
2193 add_fixit_remove (range);
2196 /* Add a fixit-hint, suggesting removal of the content at
2197 SRC_RANGE. */
2199 void
2200 rich_location::add_fixit_remove (source_range src_range)
2202 add_fixit_replace (src_range, "");
2205 /* Return true iff A is in the column directly before B, on the
2206 same line of the same source file. */
2208 static bool
2209 column_before_p (line_maps *set, source_location a, source_location b)
2211 if (IS_ADHOC_LOC (a))
2212 a = get_location_from_adhoc_loc (set, a);
2213 if (IS_ADHOC_LOC (b))
2214 b = get_location_from_adhoc_loc (set, b);
2216 /* They must both be in ordinary maps. */
2217 const struct line_map *linemap_a = linemap_lookup (set, a);
2218 if (linemap_macro_expansion_map_p (linemap_a))
2219 return false;
2220 const struct line_map *linemap_b = linemap_lookup (set, b);
2221 if (linemap_macro_expansion_map_p (linemap_b))
2222 return false;
2224 /* To be on the same line, they must be in the same ordinary map. */
2225 if (linemap_a != linemap_b)
2226 return false;
2228 linenum_type line_a
2229 = SOURCE_LINE (linemap_check_ordinary (linemap_a), a);
2230 linenum_type line_b
2231 = SOURCE_LINE (linemap_check_ordinary (linemap_b), b);
2232 if (line_a != line_b)
2233 return false;
2235 linenum_type column_a
2236 = SOURCE_COLUMN (linemap_check_ordinary (linemap_a), a);
2237 linenum_type column_b
2238 = SOURCE_COLUMN (linemap_check_ordinary (linemap_b), b);
2240 return column_b == column_a + 1;
2243 /* Add a fixit-hint, suggesting replacement of the content covered
2244 by range 0 with NEW_CONTENT. */
2246 void
2247 rich_location::add_fixit_replace (const char *new_content)
2249 add_fixit_replace (get_loc (), new_content);
2252 /* Methods for adding "replace" fix-it hints. */
2254 /* Add a fixit-hint, suggesting replacement of the content between
2255 the start and finish of WHERE with NEW_CONTENT. */
2257 void
2258 rich_location::add_fixit_replace (source_location where,
2259 const char *new_content)
2261 source_range range = get_range_from_loc (m_line_table, where);
2262 add_fixit_replace (range, new_content);
2265 /* Add a fixit-hint, suggesting replacement of the content at
2266 SRC_RANGE with NEW_CONTENT. */
2268 void
2269 rich_location::add_fixit_replace (source_range src_range,
2270 const char *new_content)
2272 src_range.m_start = get_pure_location (m_line_table, src_range.m_start);
2273 src_range.m_finish = get_pure_location (m_line_table, src_range.m_finish);
2275 if (reject_impossible_fixit (src_range.m_start))
2276 return;
2277 if (reject_impossible_fixit (src_range.m_finish))
2278 return;
2280 /* We do not yet support newlines within fix-it hints. */
2281 if (strchr (new_content, '\n'))
2283 stop_supporting_fixits ();
2284 return;
2287 /* Consolidate neighboring fixits. */
2288 fixit_hint *prev = get_last_fixit_hint ();
2289 if (prev)
2290 if (prev->maybe_append_replace (m_line_table, src_range, new_content))
2291 return;
2293 add_fixit (new fixit_replace (src_range, new_content));
2296 /* Get the last fix-it hint within this rich_location, or NULL if none. */
2298 fixit_hint *
2299 rich_location::get_last_fixit_hint () const
2301 if (m_fixit_hints.count () > 0)
2302 return get_fixit_hint (m_fixit_hints.count () - 1);
2303 else
2304 return NULL;
2307 /* If WHERE is an "awkward" location, then mark this rich_location as not
2308 supporting fixits, purging any thay were already added, and return true.
2310 Otherwise (the common case), return false. */
2312 bool
2313 rich_location::reject_impossible_fixit (source_location where)
2315 /* Fix-its within a rich_location should either all be suggested, or
2316 none of them should be suggested.
2317 Once we've rejected a fixit, we reject any more, even those
2318 with reasonable locations. */
2319 if (m_seen_impossible_fixit)
2320 return true;
2322 if (where <= LINE_MAP_MAX_LOCATION_WITH_COLS)
2323 /* WHERE is a reasonable location for a fix-it; don't reject it. */
2324 return false;
2326 /* Otherwise we have an attempt to add a fix-it with an "awkward"
2327 location: either one that we can't obtain column information
2328 for (within an ordinary map), or one within a macro expansion. */
2329 stop_supporting_fixits ();
2330 return true;
2333 /* Mark this rich_location as not supporting fixits, purging any that were
2334 already added. */
2336 void
2337 rich_location::stop_supporting_fixits ()
2339 m_seen_impossible_fixit = true;
2341 /* Purge the rich_location of any fix-its that were already added. */
2342 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
2343 delete get_fixit_hint (i);
2344 m_fixit_hints.truncate (0);
2347 /* Add HINT to the fix-it hints in this rich_location. */
2349 void
2350 rich_location::add_fixit (fixit_hint *hint)
2352 m_fixit_hints.push (hint);
2355 /* class fixit_insert. */
2357 fixit_insert::fixit_insert (source_location where,
2358 const char *new_content)
2359 : m_where (where),
2360 m_bytes (xstrdup (new_content)),
2361 m_len (strlen (new_content))
2365 fixit_insert::~fixit_insert ()
2367 free (m_bytes);
2370 /* Implementation of fixit_hint::affects_line_p for fixit_insert. */
2372 bool
2373 fixit_insert::affects_line_p (const char *file, int line) const
2375 expanded_location exploc
2376 = linemap_client_expand_location_to_spelling_point (m_where);
2377 if (file == exploc.file)
2378 if (line == exploc.line)
2379 return true;
2380 return false;
2383 /* Implementation of maybe_append_replace for fixit_insert. Reject
2384 the attempt to consolidate fix-its. */
2386 bool
2387 fixit_insert::maybe_append_replace (line_maps *, source_range, const char *)
2389 return false;
2392 /* class fixit_replace. */
2394 fixit_replace::fixit_replace (source_range src_range,
2395 const char *new_content)
2396 : m_src_range (src_range),
2397 m_bytes (xstrdup (new_content)),
2398 m_len (strlen (new_content))
2402 fixit_replace::~fixit_replace ()
2404 free (m_bytes);
2407 /* Implementation of fixit_hint::affects_line_p for fixit_replace. */
2409 bool
2410 fixit_replace::affects_line_p (const char *file, int line) const
2412 return m_src_range.intersects_line_p (file, line);
2415 /* Implementation of maybe_append_replace for fixit_replace. If
2416 possible, merge the new replacement into this one and return true.
2417 Otherwise return false. */
2419 bool
2420 fixit_replace::maybe_append_replace (line_maps *set,
2421 source_range src_range,
2422 const char *new_content)
2424 /* Does SRC_RANGE start immediately after this one finishes? */
2425 if (!column_before_p (set, m_src_range.m_finish, src_range.m_start))
2426 return false;
2428 /* We have neighboring replacements; merge them. */
2429 m_src_range.m_finish = src_range.m_finish;
2430 size_t extra_len = strlen (new_content);
2431 m_bytes = (char *)xrealloc (m_bytes, m_len + extra_len + 1);
2432 memcpy (m_bytes + m_len, new_content, extra_len);
2433 m_len += extra_len;
2434 m_bytes[m_len] = '\0';
2435 return true;