xref: /netbsd-src/external/gpl3/gcc.old/dist/libcpp/line-map.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2    Copyright (C) 2001-2016 Free Software Foundation, Inc.
3 
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.
8 
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.
13 
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/>.
17 
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!  */
21 
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"
28 
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);
33 
34 /* Do not pack ranges if locations get higher than this.
35    If you change this, update:
36      gcc.dg/plugin/location_overflow_plugin.c
37      gcc.dg/plugin/location-overflow-test-*.c.  */
38 const source_location LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES = 0x50000000;
39 
40 /* Do not track column numbers if locations get higher than this.
41    If you change this, update:
42      gcc.dg/plugin/location_overflow_plugin.c
43      gcc.dg/plugin/location-overflow-test-*.c.  */
44 const source_location LINE_MAP_MAX_LOCATION_WITH_COLS = 0x60000000;
45 
46 /* Highest possible source location encoded within an ordinary or
47    macro map.  */
48 const source_location LINE_MAP_MAX_SOURCE_LOCATION = 0x70000000;
49 
50 static void trace_include (const struct line_maps *, const line_map_ordinary *);
51 static const line_map_ordinary * linemap_ordinary_map_lookup (struct line_maps *,
52 							      source_location);
53 static const line_map_macro* linemap_macro_map_lookup (struct line_maps *,
54 						       source_location);
55 static source_location linemap_macro_map_loc_to_def_point
56 (const line_map_macro *, source_location);
57 static source_location linemap_macro_map_loc_to_exp_point
58 (const line_map_macro *, source_location);
59 static source_location linemap_macro_loc_to_spelling_point
60 (struct line_maps *, source_location, const line_map_ordinary **);
61 static source_location linemap_macro_loc_to_def_point (struct line_maps *,
62 						       source_location,
63 						       const line_map_ordinary **);
64 static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
65 						       source_location,
66 						       const line_map_ordinary **);
67 
68 /* Counters defined in macro.c.  */
69 extern unsigned num_expanded_macros_counter;
70 extern unsigned num_macro_tokens_counter;
71 
72 /* Hash function for location_adhoc_data hashtable.  */
73 
74 static hashval_t
75 location_adhoc_data_hash (const void *l)
76 {
77   const struct location_adhoc_data *lb =
78       (const struct location_adhoc_data *) l;
79   return ((hashval_t) lb->locus
80 	  + (hashval_t) lb->src_range.m_start
81 	  + (hashval_t) lb->src_range.m_finish
82 	  + (size_t) lb->data);
83 }
84 
85 /* Compare function for location_adhoc_data hashtable.  */
86 
87 static int
88 location_adhoc_data_eq (const void *l1, const void *l2)
89 {
90   const struct location_adhoc_data *lb1 =
91       (const struct location_adhoc_data *) l1;
92   const struct location_adhoc_data *lb2 =
93       (const struct location_adhoc_data *) l2;
94   return (lb1->locus == lb2->locus
95 	  && lb1->src_range.m_start == lb2->src_range.m_start
96 	  && lb1->src_range.m_finish == lb2->src_range.m_finish
97 	  && lb1->data == lb2->data);
98 }
99 
100 /* Update the hashtable when location_adhoc_data is reallocated.  */
101 
102 static int
103 location_adhoc_data_update (void **slot, void *data)
104 {
105   *((char **) slot) += *((long long *) data);
106   return 1;
107 }
108 
109 /* Rebuild the hash table from the location adhoc data.  */
110 
111 void
112 rebuild_location_adhoc_htab (struct line_maps *set)
113 {
114   unsigned i;
115   set->location_adhoc_data_map.htab =
116       htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
117   for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
118     htab_find_slot (set->location_adhoc_data_map.htab,
119 		    set->location_adhoc_data_map.data + i, INSERT);
120 }
121 
122 /* Helper function for get_combined_adhoc_loc.
123    Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
124    within a source_location, without needing to use an ad-hoc location.  */
125 
126 static bool
127 can_be_stored_compactly_p (struct line_maps *set,
128 			   source_location locus,
129 			   source_range src_range,
130 			   void *data)
131 {
132   /* If there's an ad-hoc pointer, we can't store it directly in the
133      source_location, we need the lookaside.  */
134   if (data)
135     return false;
136 
137   /* We only store ranges that begin at the locus and that are sufficiently
138      "sane".  */
139   if (src_range.m_start != locus)
140     return false;
141 
142   if (src_range.m_finish < src_range.m_start)
143     return false;
144 
145   if (src_range.m_start < RESERVED_LOCATION_COUNT)
146     return false;
147 
148   if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
149     return false;
150 
151   /* All 3 locations must be within ordinary maps, typically, the same
152      ordinary map.  */
153   source_location lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
154   if (locus >= lowest_macro_loc)
155     return false;
156   if (src_range.m_start >= lowest_macro_loc)
157     return false;
158   if (src_range.m_finish >= lowest_macro_loc)
159     return false;
160 
161   /* Passed all tests.  */
162   return true;
163 }
164 
165 /* Combine LOCUS and DATA to a combined adhoc loc.  */
166 
167 source_location
168 get_combined_adhoc_loc (struct line_maps *set,
169 			source_location locus,
170 			source_range src_range,
171 			void *data)
172 {
173   struct location_adhoc_data lb;
174   struct location_adhoc_data **slot;
175 
176   if (IS_ADHOC_LOC (locus))
177     locus
178       = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
179   if (locus == 0 && data == NULL)
180     return 0;
181 
182   /* Any ordinary locations ought to be "pure" at this point: no
183      compressed ranges.  */
184   linemap_assert (locus < RESERVED_LOCATION_COUNT
185 		  || locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
186 		  || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
187 		  || pure_location_p (set, locus));
188 
189   /* Consider short-range optimization.  */
190   if (can_be_stored_compactly_p (set, locus, src_range, data))
191     {
192       /* The low bits ought to be clear.  */
193       linemap_assert (pure_location_p (set, locus));
194       const line_map *map = linemap_lookup (set, locus);
195       const line_map_ordinary *ordmap = linemap_check_ordinary (map);
196       unsigned int int_diff = src_range.m_finish - src_range.m_start;
197       unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
198       if (col_diff < (1U << ordmap->m_range_bits))
199 	{
200 	  source_location packed = locus | col_diff;
201 	  set->num_optimized_ranges++;
202 	  return packed;
203 	}
204     }
205 
206   /* We can also compactly store locations
207      when locus == start == finish (and data is NULL).  */
208   if (locus == src_range.m_start
209       && locus == src_range.m_finish
210       && !data)
211     return locus;
212 
213   if (!data)
214     set->num_unoptimized_ranges++;
215 
216   lb.locus = locus;
217   lb.src_range = src_range;
218   lb.data = data;
219   slot = (struct location_adhoc_data **)
220       htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
221   if (*slot == NULL)
222     {
223       if (set->location_adhoc_data_map.curr_loc >=
224 	  set->location_adhoc_data_map.allocated)
225 	{
226 	  char *orig_data = (char *) set->location_adhoc_data_map.data;
227 	  long long offset;
228 	  /* Cast away extern "C" from the type of xrealloc.  */
229 	  line_map_realloc reallocator = (set->reallocator
230 					  ? set->reallocator
231 					  : (line_map_realloc) xrealloc);
232 
233 	  if (set->location_adhoc_data_map.allocated == 0)
234 	    set->location_adhoc_data_map.allocated = 128;
235 	  else
236 	    set->location_adhoc_data_map.allocated *= 2;
237 	  set->location_adhoc_data_map.data = (struct location_adhoc_data *)
238 	      reallocator (set->location_adhoc_data_map.data,
239 			   set->location_adhoc_data_map.allocated
240 			   * sizeof (struct location_adhoc_data));
241 	  offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
242 	  if (set->location_adhoc_data_map.allocated > 128)
243 	    htab_traverse (set->location_adhoc_data_map.htab,
244 			   location_adhoc_data_update, &offset);
245 	}
246       *slot = set->location_adhoc_data_map.data
247 	      + set->location_adhoc_data_map.curr_loc;
248       set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
249 	= lb;
250     }
251   return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
252 }
253 
254 /* Return the data for the adhoc loc.  */
255 
256 void *
257 get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
258 {
259   linemap_assert (IS_ADHOC_LOC (loc));
260   return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
261 }
262 
263 /* Return the location for the adhoc loc.  */
264 
265 source_location
266 get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
267 {
268   linemap_assert (IS_ADHOC_LOC (loc));
269   return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
270 }
271 
272 /* Return the source_range for adhoc location LOC.  */
273 
274 static source_range
275 get_range_from_adhoc_loc (struct line_maps *set, source_location loc)
276 {
277   linemap_assert (IS_ADHOC_LOC (loc));
278   return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].src_range;
279 }
280 
281 /* Get the source_range of location LOC, either from the ad-hoc
282    lookaside table, or embedded inside LOC itself.  */
283 
284 source_range
285 get_range_from_loc (struct line_maps *set,
286 		    source_location loc)
287 {
288   if (IS_ADHOC_LOC (loc))
289     return get_range_from_adhoc_loc (set, loc);
290 
291   /* For ordinary maps, extract packed range.  */
292   if (loc >= RESERVED_LOCATION_COUNT
293       && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
294       && loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
295     {
296       const line_map *map = linemap_lookup (set, loc);
297       const line_map_ordinary *ordmap = linemap_check_ordinary (map);
298       source_range result;
299       int offset = loc & ((1 << ordmap->m_range_bits) - 1);
300       result.m_start = loc - offset;
301       result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
302       return result;
303     }
304 
305   return source_range::from_location (loc);
306 }
307 
308 /* Get whether location LOC is a "pure" location, or
309    whether it is an ad-hoc location, or embeds range information.  */
310 
311 bool
312 pure_location_p (line_maps *set, source_location loc)
313 {
314   if (IS_ADHOC_LOC (loc))
315     return false;
316 
317   const line_map *map = linemap_lookup (set, loc);
318   const line_map_ordinary *ordmap = linemap_check_ordinary (map);
319 
320   if (loc & ((1U << ordmap->m_range_bits) - 1))
321     return false;
322 
323   return true;
324 }
325 
326 /* Finalize the location_adhoc_data structure.  */
327 void
328 location_adhoc_data_fini (struct line_maps *set)
329 {
330   htab_delete (set->location_adhoc_data_map.htab);
331 }
332 
333 /* Initialize a line map set.  */
334 
335 void
336 linemap_init (struct line_maps *set,
337 	      source_location builtin_location)
338 {
339   memset (set, 0, sizeof (struct line_maps));
340   set->highest_location = RESERVED_LOCATION_COUNT - 1;
341   set->highest_line = RESERVED_LOCATION_COUNT - 1;
342   set->location_adhoc_data_map.htab =
343       htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
344   set->builtin_location = builtin_location;
345 }
346 
347 /* Check for and warn about line_maps entered but not exited.  */
348 
349 void
350 linemap_check_files_exited (struct line_maps *set)
351 {
352   const line_map_ordinary *map;
353   /* Depending upon whether we are handling preprocessed input or
354      not, this can be a user error or an ICE.  */
355   for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
356        ! MAIN_FILE_P (map);
357        map = INCLUDED_FROM (set, map))
358     fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
359 	     ORDINARY_MAP_FILE_NAME (map));
360 }
361 
362 /* Create a new line map in the line map set SET, and return it.
363    REASON is the reason of creating the map. It determines the type
364    of map created (ordinary or macro map). Note that ordinary maps and
365    macro maps are allocated in different memory location.  */
366 
367 static struct line_map *
368 new_linemap (struct line_maps *set,
369 	     enum lc_reason reason)
370 {
371   /* Depending on this variable, a macro map would be allocated in a
372      different memory location than an ordinary map.  */
373   bool macro_map_p = (reason == LC_ENTER_MACRO);
374   struct line_map *result;
375 
376   if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
377     {
378       /* We ran out of allocated line maps. Let's allocate more.  */
379       size_t alloc_size;
380 
381       /* Cast away extern "C" from the type of xrealloc.  */
382       line_map_realloc reallocator = (set->reallocator
383 				      ? set->reallocator
384 				      : (line_map_realloc) xrealloc);
385       line_map_round_alloc_size_func round_alloc_size =
386 	set->round_alloc_size;
387 
388       size_t map_size = (macro_map_p
389 			 ? sizeof (line_map_macro)
390 			 : sizeof (line_map_ordinary));
391 
392       /* We are going to execute some dance to try to reduce the
393 	 overhead of the memory allocator, in case we are using the
394 	 ggc-page.c one.
395 
396 	 The actual size of memory we are going to get back from the
397 	 allocator is the smallest power of 2 that is greater than the
398 	 size we requested.  So let's consider that size then.  */
399 
400       alloc_size =
401 	(2 * LINEMAPS_ALLOCATED (set, macro_map_p) +  256)
402 	* map_size;
403 
404       /* Get the actual size of memory that is going to be allocated
405 	 by the allocator.  */
406       alloc_size = round_alloc_size (alloc_size);
407 
408       /* Now alloc_size contains the exact memory size we would get if
409 	 we have asked for the initial alloc_size amount of memory.
410 	 Let's get back to the number of macro map that amounts
411 	 to.  */
412       LINEMAPS_ALLOCATED (set, macro_map_p) =
413 	alloc_size / map_size;
414 
415       /* And now let's really do the re-allocation.  */
416       if (macro_map_p)
417 	{
418 	  set->info_macro.maps
419 	    = (line_map_macro *) (*reallocator) (set->info_macro.maps,
420 						 (LINEMAPS_ALLOCATED (set, macro_map_p)
421 						  * map_size));
422 	  result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
423 	}
424       else
425 	{
426 	  set->info_ordinary.maps =
427 	    (line_map_ordinary *) (*reallocator) (set->info_ordinary.maps,
428 						  (LINEMAPS_ALLOCATED (set, macro_map_p)
429 						   * map_size));
430 	  result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
431 	}
432       memset (result, 0,
433 	      ((LINEMAPS_ALLOCATED (set, macro_map_p)
434 		- LINEMAPS_USED (set, macro_map_p))
435 	       * map_size));
436     }
437   else
438     {
439       if (macro_map_p)
440 	result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
441       else
442 	result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
443     }
444 
445   LINEMAPS_USED (set, macro_map_p)++;
446 
447   result->reason = reason;
448   return result;
449 }
450 
451 /* Add a mapping of logical source line to physical source file and
452    line number.
453 
454    The text pointed to by TO_FILE must have a lifetime
455    at least as long as the final call to lookup_line ().  An empty
456    TO_FILE means standard input.  If reason is LC_LEAVE, and
457    TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
458    natural values considering the file we are returning to.
459 
460    FROM_LINE should be monotonic increasing across calls to this
461    function.  A call to this function can relocate the previous set of
462    maps, so any stored line_map pointers should not be used.  */
463 
464 const struct line_map *
465 linemap_add (struct line_maps *set, enum lc_reason reason,
466 	     unsigned int sysp, const char *to_file, linenum_type to_line)
467 {
468   /* Generate a start_location above the current highest_location.
469      If possible, make the low range bits be zero.  */
470   source_location start_location;
471   if (set->highest_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
472     {
473       start_location = set->highest_location + (1 << set->default_range_bits);
474       if (set->default_range_bits)
475 	start_location &= ~((1 << set->default_range_bits) - 1);
476       linemap_assert (0 == (start_location
477 			    & ((1 << set->default_range_bits) - 1)));
478     }
479   else
480     start_location = set->highest_location + 1;
481 
482   linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
483 		    && (start_location
484 			< MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
485 
486   /* When we enter the file for the first time reason cannot be
487      LC_RENAME.  */
488   linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
489 
490   /* If we are leaving the main file, return a NULL map.  */
491   if (reason == LC_LEAVE
492       && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
493       && to_file == NULL)
494     {
495       set->depth--;
496       return NULL;
497     }
498 
499   linemap_assert (reason != LC_ENTER_MACRO);
500   line_map_ordinary *map = linemap_check_ordinary (new_linemap (set, reason));
501 
502   if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
503     to_file = "<stdin>";
504 
505   if (reason == LC_RENAME_VERBATIM)
506     reason = LC_RENAME;
507 
508   if (reason == LC_LEAVE)
509     {
510       /* When we are just leaving an "included" file, and jump to the next
511 	 location inside the "includer" right after the #include
512 	 "included", this variable points the map in use right before the
513 	 #include "included", inside the same "includer" file.  */
514       line_map_ordinary *from;
515 
516       linemap_assert (!MAIN_FILE_P (map - 1));
517       /* (MAP - 1) points to the map we are leaving. The
518 	 map from which (MAP - 1) got included should be the map
519 	 that comes right before MAP in the same file.  */
520       from = INCLUDED_FROM (set, map - 1);
521 
522       /* A TO_FILE of NULL is special - we use the natural values.  */
523       if (to_file == NULL)
524 	{
525 	  to_file = ORDINARY_MAP_FILE_NAME (from);
526 	  to_line = SOURCE_LINE (from, from[1].start_location);
527 	  sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
528 	}
529       else
530 	linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from),
531 				      to_file) == 0);
532     }
533 
534   map->sysp = sysp;
535   map->start_location = start_location;
536   map->to_file = to_file;
537   map->to_line = to_line;
538   LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
539   map->m_column_and_range_bits = 0;
540   map->m_range_bits = 0;
541   set->highest_location = start_location;
542   set->highest_line = start_location;
543   set->max_column_hint = 0;
544 
545   /* This assertion is placed after set->highest_location has
546      been updated, since the latter affects
547      linemap_location_from_macro_expansion_p, which ultimately affects
548      pure_location_p.  */
549   linemap_assert (pure_location_p (set, start_location));
550 
551   if (reason == LC_ENTER)
552     {
553       map->included_from =
554 	set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
555       set->depth++;
556       if (set->trace_includes)
557 	trace_include (set, map);
558     }
559   else if (reason == LC_RENAME)
560     map->included_from = ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
561   else if (reason == LC_LEAVE)
562     {
563       set->depth--;
564       map->included_from =
565 	ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
566     }
567 
568   return map;
569 }
570 
571 /* Returns TRUE if the line table set tracks token locations across
572    macro expansion, FALSE otherwise.  */
573 
574 bool
575 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
576 {
577   return LINEMAPS_MACRO_MAPS (set) != NULL;
578 }
579 
580 /* Create a macro map.  A macro map encodes source locations of tokens
581    that are part of a macro replacement-list, at a macro expansion
582    point.  See the extensive comments of struct line_map and struct
583    line_map_macro, in line-map.h.
584 
585    This map shall be created when the macro is expanded.  The map
586    encodes the source location of the expansion point of the macro as
587    well as the "original" source location of each token that is part
588    of the macro replacement-list.  If a macro is defined but never
589    expanded, it has no macro map.  SET is the set of maps the macro
590    map should be part of.  MACRO_NODE is the macro which the new macro
591    map should encode source locations for.  EXPANSION is the location
592    of the expansion point of MACRO. For function-like macros
593    invocations, it's best to make it point to the closing parenthesis
594    of the macro, rather than the the location of the first character
595    of the macro.  NUM_TOKENS is the number of tokens that are part of
596    the replacement-list of MACRO.
597 
598    Note that when we run out of the integer space available for source
599    locations, this function returns NULL.  In that case, callers of
600    this function cannot encode {line,column} pairs into locations of
601    macro tokens anymore.  */
602 
603 const line_map_macro *
604 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
605 		     source_location expansion, unsigned int num_tokens)
606 {
607   line_map_macro *map;
608   source_location start_location;
609   /* Cast away extern "C" from the type of xrealloc.  */
610   line_map_realloc reallocator = (set->reallocator
611 				  ? set->reallocator
612 				  : (line_map_realloc) xrealloc);
613 
614   start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
615 
616   if (start_location <= set->highest_line
617       || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
618     /* We ran out of macro map space.   */
619     return NULL;
620 
621   map = linemap_check_macro (new_linemap (set, LC_ENTER_MACRO));
622 
623   map->start_location = start_location;
624   map->macro = macro_node;
625   map->n_tokens = num_tokens;
626   map->macro_locations
627     = (source_location*) reallocator (NULL,
628 				      2 * num_tokens
629 				      * sizeof (source_location));
630   map->expansion = expansion;
631   memset (MACRO_MAP_LOCATIONS (map), 0,
632 	  num_tokens * sizeof (source_location));
633 
634   LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
635 
636   return map;
637 }
638 
639 /* Create and return a virtual location for a token that is part of a
640    macro expansion-list at a macro expansion point.  See the comment
641    inside struct line_map_macro to see what an expansion-list exactly
642    is.
643 
644    A call to this function must come after a call to
645    linemap_enter_macro.
646 
647    MAP is the map into which the source location is created.  TOKEN_NO
648    is the index of the token in the macro replacement-list, starting
649    at number 0.
650 
651    ORIG_LOC is the location of the token outside of this macro
652    expansion.  If the token comes originally from the macro
653    definition, it is the locus in the macro definition; otherwise it
654    is a location in the context of the caller of this macro expansion
655    (which is a virtual location or a source location if the caller is
656    itself a macro expansion or not).
657 
658    ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
659    either of the token itself or of a macro parameter that it
660    replaces.  */
661 
662 source_location
663 linemap_add_macro_token (const line_map_macro *map,
664 			 unsigned int token_no,
665 			 source_location orig_loc,
666 			 source_location orig_parm_replacement_loc)
667 {
668   source_location result;
669 
670   linemap_assert (linemap_macro_expansion_map_p (map));
671   linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
672 
673   MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
674   MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
675 
676   result = MAP_START_LOCATION (map) + token_no;
677   return result;
678 }
679 
680 /* Return a source_location for the start (i.e. column==0) of
681    (physical) line TO_LINE in the current source file (as in the
682    most recent linemap_add).   MAX_COLUMN_HINT is the highest column
683    number we expect to use in this line (but it does not change
684    the highest_location).  */
685 
686 source_location
687 linemap_line_start (struct line_maps *set, linenum_type to_line,
688 		    unsigned int max_column_hint)
689 {
690   line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
691   source_location highest = set->highest_location;
692   source_location r;
693   linenum_type last_line =
694     SOURCE_LINE (map, set->highest_line);
695   int line_delta = to_line - last_line;
696   bool add_map = false;
697   linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
698   int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
699 
700   if (line_delta < 0
701       || (line_delta > 10
702 	  && line_delta * map->m_column_and_range_bits > 1000)
703       || (max_column_hint >= (1U << effective_column_bits))
704       || (max_column_hint <= 80 && effective_column_bits >= 10)
705       || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
706 	  && map->m_range_bits > 0)
707       || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
708 	  && (set->max_column_hint || highest >= LINE_MAP_MAX_SOURCE_LOCATION)))
709     add_map = true;
710   else
711     max_column_hint = set->max_column_hint;
712   if (add_map)
713     {
714       int column_bits;
715       int range_bits;
716       if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
717 	  || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
718 	{
719 	  /* If the column number is ridiculous or we've allocated a huge
720 	     number of source_locations, give up on column numbers
721 	     (and on packed ranges).  */
722 	  max_column_hint = 0;
723 	  column_bits = 0;
724 	  range_bits = 0;
725 	  if (highest > LINE_MAP_MAX_SOURCE_LOCATION)
726 	    return 0;
727 	}
728       else
729 	{
730 	  column_bits = 7;
731 	  if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
732 	    range_bits = set->default_range_bits;
733 	  else
734 	    range_bits = 0;
735 	  while (max_column_hint >= (1U << column_bits))
736 	    column_bits++;
737 	  max_column_hint = 1U << column_bits;
738 	  column_bits += range_bits;
739 	}
740       /* Allocate the new line_map.  However, if the current map only has a
741 	 single line we can sometimes just increase its column_bits instead. */
742       if (line_delta < 0
743 	  || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
744 	  || SOURCE_COLUMN (map, highest) >= (1U << column_bits)
745 	  || range_bits < map->m_range_bits)
746 	map = linemap_check_ordinary
747 	        (const_cast <line_map *>
748 		  (linemap_add (set, LC_RENAME,
749 				ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
750 				ORDINARY_MAP_FILE_NAME (map),
751 				to_line)));
752       map->m_column_and_range_bits = column_bits;
753       map->m_range_bits = range_bits;
754       r = (MAP_START_LOCATION (map)
755 	   + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
756 	      << column_bits));
757     }
758   else
759     r = set->highest_line + (line_delta << map->m_column_and_range_bits);
760 
761   /* Locations of ordinary tokens are always lower than locations of
762      macro tokens.  */
763   if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
764     return 0;
765 
766   set->highest_line = r;
767   if (r > set->highest_location)
768     set->highest_location = r;
769   set->max_column_hint = max_column_hint;
770 
771   /* At this point, we expect one of:
772      (a) the normal case: a "pure" location with 0 range bits, or
773      (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
774         columns anymore (or ranges), or
775      (c) we're in a region with a column hint exceeding
776         LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
777 	with column_bits == 0.  */
778   linemap_assert (pure_location_p (set, r)
779 		  || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
780 		  || map->m_column_and_range_bits == 0);
781   linemap_assert (SOURCE_LINE (map, r) == to_line);
782   return r;
783 }
784 
785 /* Encode and return a source_location from a column number. The
786    source line considered is the last source line used to call
787    linemap_line_start, i.e, the last source line which a location was
788    encoded from.  */
789 
790 source_location
791 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
792 {
793   source_location r = set->highest_line;
794 
795   linemap_assert
796     (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
797 
798   if (to_column >= set->max_column_hint)
799     {
800       if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
801 	  || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
802 	{
803 	  /* Running low on source_locations - disable column numbers.  */
804 	  return r;
805 	}
806       else
807 	{
808 	  line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
809 	  r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
810 	}
811     }
812   line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
813   r = r + (to_column << map->m_range_bits);
814   if (r >= set->highest_location)
815     set->highest_location = r;
816   return r;
817 }
818 
819 /* Encode and return a source location from a given line and
820    column.  */
821 
822 source_location
823 linemap_position_for_line_and_column (line_maps *set,
824 				      const line_map_ordinary *ord_map,
825 				      linenum_type line,
826 				      unsigned column)
827 {
828   linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
829 
830   source_location r = MAP_START_LOCATION (ord_map);
831   r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
832 	<< ord_map->m_column_and_range_bits);
833   if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
834     r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
835 	  << ord_map->m_range_bits);
836   source_location upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
837   if (r >= upper_limit)
838     r = upper_limit - 1;
839   if (r > set->highest_location)
840     set->highest_location = r;
841   return r;
842 }
843 
844 /* Encode and return a source_location starting from location LOC and
845    shifting it by COLUMN_OFFSET columns.  This function does not support
846    virtual locations.  */
847 
848 source_location
849 linemap_position_for_loc_and_offset (struct line_maps *set,
850 				     source_location loc,
851 				     unsigned int column_offset)
852 {
853   const line_map_ordinary * map = NULL;
854 
855   if (IS_ADHOC_LOC (loc))
856     loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
857 
858   /* This function does not support virtual locations yet.  */
859   if (linemap_assert_fails
860       (!linemap_location_from_macro_expansion_p (set, loc)))
861     return loc;
862 
863   if (column_offset == 0
864       /* Adding an offset to a reserved location (like
865 	 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
866 	 sense.  So let's leave the location intact in that case.  */
867       || loc < RESERVED_LOCATION_COUNT)
868     return loc;
869 
870   /* We find the real location and shift it.  */
871   loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
872   /* The new location (loc + offset) should be higher than the first
873      location encoded by MAP.  This can fail if the line information
874      is messed up because of line directives (see PR66415).  */
875   if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
876     return loc;
877 
878   linenum_type line = SOURCE_LINE (map, loc);
879   unsigned int column = SOURCE_COLUMN (map, loc);
880 
881   /* If MAP is not the last line map of its set, then the new location
882      (loc + offset) should be less than the first location encoded by
883      the next line map of the set.  Otherwise, we try to encode the
884      location in the next map.  */
885   while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
886 	 && (loc + (column_offset << map->m_range_bits)
887 	     >= MAP_START_LOCATION (&map[1])))
888     {
889       map = &map[1];
890       /* If the next map starts in a higher line, we cannot encode the
891 	 location there.  */
892       if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
893 	return loc;
894     }
895 
896   column += column_offset;
897   if (linemap_assert_fails (column < (1u << map->m_column_and_range_bits)))
898     return loc;
899 
900   source_location r =
901     linemap_position_for_line_and_column (set, map, line, column);
902   if (linemap_assert_fails (r <= set->highest_location)
903       || linemap_assert_fails (map == linemap_lookup (set, r)))
904     return loc;
905 
906   return r;
907 }
908 
909 /* Given a virtual source location yielded by a map (either an
910    ordinary or a macro map), returns that map.  */
911 
912 const struct line_map*
913 linemap_lookup (struct line_maps *set, source_location line)
914 {
915   if (IS_ADHOC_LOC (line))
916     line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
917   if (linemap_location_from_macro_expansion_p (set, line))
918     return linemap_macro_map_lookup (set, line);
919   return linemap_ordinary_map_lookup (set, line);
920 }
921 
922 /* Given a source location yielded by an ordinary map, returns that
923    map.  Since the set is built chronologically, the logical lines are
924    monotonic increasing, and so the list is sorted and we can use a
925    binary search.  */
926 
927 static const line_map_ordinary *
928 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
929 {
930   unsigned int md, mn, mx;
931   const line_map_ordinary *cached, *result;
932 
933   if (IS_ADHOC_LOC (line))
934     line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
935 
936   if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
937     return NULL;
938 
939   mn = LINEMAPS_ORDINARY_CACHE (set);
940   mx = LINEMAPS_ORDINARY_USED (set);
941 
942   cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
943   /* We should get a segfault if no line_maps have been added yet.  */
944   if (line >= MAP_START_LOCATION (cached))
945     {
946       if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
947 	return cached;
948     }
949   else
950     {
951       mx = mn;
952       mn = 0;
953     }
954 
955   while (mx - mn > 1)
956     {
957       md = (mn + mx) / 2;
958       if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
959 	mx = md;
960       else
961 	mn = md;
962     }
963 
964   LINEMAPS_ORDINARY_CACHE (set) = mn;
965   result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
966   linemap_assert (line >= MAP_START_LOCATION (result));
967   return result;
968 }
969 
970 /* Given a source location yielded by a macro map, returns that map.
971    Since the set is built chronologically, the logical lines are
972    monotonic decreasing, and so the list is sorted and we can use a
973    binary search.  */
974 
975 static const line_map_macro *
976 linemap_macro_map_lookup (struct line_maps *set, source_location line)
977 {
978   unsigned int md, mn, mx;
979   const struct line_map_macro *cached, *result;
980 
981   if (IS_ADHOC_LOC (line))
982     line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
983 
984   linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
985 
986   if (set ==  NULL)
987     return NULL;
988 
989   mn = LINEMAPS_MACRO_CACHE (set);
990   mx = LINEMAPS_MACRO_USED (set);
991   cached = LINEMAPS_MACRO_MAP_AT (set, mn);
992 
993   if (line >= MAP_START_LOCATION (cached))
994     {
995       if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
996 	return cached;
997       mx = mn - 1;
998       mn = 0;
999     }
1000 
1001   while (mn < mx)
1002     {
1003       md = (mx + mn) / 2;
1004       if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
1005 	mn = md + 1;
1006       else
1007 	mx = md;
1008     }
1009 
1010   LINEMAPS_MACRO_CACHE (set) = mx;
1011   result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
1012   linemap_assert (MAP_START_LOCATION (result) <= line);
1013 
1014   return result;
1015 }
1016 
1017 /* Return TRUE if MAP encodes locations coming from a macro
1018    replacement-list at macro expansion point.  */
1019 
1020 bool
1021 linemap_macro_expansion_map_p (const struct line_map *map)
1022 {
1023   if (!map)
1024     return false;
1025   return (map->reason == LC_ENTER_MACRO);
1026 }
1027 
1028 /* If LOCATION is the locus of a token in a replacement-list of a
1029    macro expansion return the location of the macro expansion point.
1030 
1031    Read the comments of struct line_map and struct line_map_macro in
1032    line-map.h to understand what a macro expansion point is.  */
1033 
1034 static source_location
1035 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
1036 				    source_location location ATTRIBUTE_UNUSED)
1037 {
1038   linemap_assert (linemap_macro_expansion_map_p (map)
1039 		  && location >= MAP_START_LOCATION (map));
1040 
1041   /* Make sure LOCATION is correct.  */
1042   linemap_assert ((location - MAP_START_LOCATION (map))
1043 		  <  MACRO_MAP_NUM_MACRO_TOKENS (map));
1044 
1045   return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1046 }
1047 
1048 /* LOCATION is the source location of a token that belongs to a macro
1049    replacement-list as part of the macro expansion denoted by MAP.
1050 
1051    Return the location of the token at the definition point of the
1052    macro.  */
1053 
1054 static source_location
1055 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
1056 				    source_location location)
1057 {
1058   unsigned token_no;
1059 
1060   linemap_assert (linemap_macro_expansion_map_p (map)
1061 		  && location >= MAP_START_LOCATION (map));
1062   linemap_assert (location >= RESERVED_LOCATION_COUNT);
1063 
1064   token_no = location - MAP_START_LOCATION (map);
1065   linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1066 
1067   location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1068 
1069   return location;
1070 }
1071 
1072 /* If LOCATION is the locus of a token that is an argument of a
1073    function-like macro M and appears in the expansion of M, return the
1074    locus of that argument in the context of the caller of M.
1075 
1076    In other words, this returns the xI location presented in the
1077    comments of line_map_macro above.  */
1078 source_location
1079 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1080 					      const line_map_macro* map,
1081 					      source_location location)
1082 {
1083   unsigned token_no;
1084 
1085   if (IS_ADHOC_LOC (location))
1086     location = get_location_from_adhoc_loc (set, location);
1087 
1088   linemap_assert (linemap_macro_expansion_map_p (map)
1089 		  && location >= MAP_START_LOCATION (map));
1090   linemap_assert (location >= RESERVED_LOCATION_COUNT);
1091   linemap_assert (!IS_ADHOC_LOC (location));
1092 
1093   token_no = location - MAP_START_LOCATION (map);
1094   linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1095 
1096   location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1097 
1098   return location;
1099 }
1100 
1101 /* Return the source line number corresponding to source location
1102    LOCATION.  SET is the line map set LOCATION comes from.  If
1103    LOCATION is the source location of token that is part of the
1104    replacement-list of a macro expansion return the line number of the
1105    macro expansion point.  */
1106 
1107 int
1108 linemap_get_expansion_line (struct line_maps *set,
1109 			    source_location location)
1110 {
1111   const line_map_ordinary *map = NULL;
1112 
1113   if (IS_ADHOC_LOC (location))
1114     location = set->location_adhoc_data_map.data[location
1115 						 & MAX_SOURCE_LOCATION].locus;
1116 
1117   if (location < RESERVED_LOCATION_COUNT)
1118     return 0;
1119 
1120   location =
1121     linemap_macro_loc_to_exp_point (set, location, &map);
1122 
1123   return SOURCE_LINE (map, location);
1124 }
1125 
1126 /* Return the path of the file corresponding to source code location
1127    LOCATION.
1128 
1129    If LOCATION is the source location of token that is part of the
1130    replacement-list of a macro expansion return the file path of the
1131    macro expansion point.
1132 
1133    SET is the line map set LOCATION comes from.  */
1134 
1135 const char*
1136 linemap_get_expansion_filename (struct line_maps *set,
1137 				source_location location)
1138 {
1139   const struct line_map_ordinary *map = NULL;
1140 
1141   if (IS_ADHOC_LOC (location))
1142     location = set->location_adhoc_data_map.data[location
1143 						 & MAX_SOURCE_LOCATION].locus;
1144 
1145   if (location < RESERVED_LOCATION_COUNT)
1146     return NULL;
1147 
1148   location =
1149     linemap_macro_loc_to_exp_point (set, location, &map);
1150 
1151   return LINEMAP_FILE (map);
1152 }
1153 
1154 /* Return the name of the macro associated to MACRO_MAP.  */
1155 
1156 const char*
1157 linemap_map_get_macro_name (const line_map_macro *macro_map)
1158 {
1159   linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1160   return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1161 }
1162 
1163 /* Return a positive value if LOCATION is the locus of a token that is
1164    located in a system header, O otherwise. It returns 1 if LOCATION
1165    is the locus of a token that is located in a system header, and 2
1166    if LOCATION is the locus of a token located in a C system header
1167    that therefore needs to be extern "C" protected in C++.
1168 
1169    Note that this function returns 1 if LOCATION belongs to a token
1170    that is part of a macro replacement-list defined in a system
1171    header, but expanded in a non-system file.  */
1172 
1173 int
1174 linemap_location_in_system_header_p (struct line_maps *set,
1175 				     source_location location)
1176 {
1177   const struct line_map *map = NULL;
1178 
1179   if (IS_ADHOC_LOC (location))
1180     location = set->location_adhoc_data_map.data[location
1181 						 & MAX_SOURCE_LOCATION].locus;
1182 
1183   if (location < RESERVED_LOCATION_COUNT)
1184     return false;
1185 
1186   /* Let's look at where the token for LOCATION comes from.  */
1187   while (true)
1188     {
1189       map = linemap_lookup (set, location);
1190       if (map != NULL)
1191 	{
1192 	  if (!linemap_macro_expansion_map_p (map))
1193 	    /* It's a normal token.  */
1194 	    return LINEMAP_SYSP (linemap_check_ordinary (map));
1195 	  else
1196 	    {
1197 	      const line_map_macro *macro_map = linemap_check_macro (map);
1198 
1199 	      /* It's a token resulting from a macro expansion.  */
1200 	      source_location loc =
1201 		linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
1202 	      if (loc < RESERVED_LOCATION_COUNT)
1203 		/* This token might come from a built-in macro.  Let's
1204 		   look at where that macro got expanded.  */
1205 		location = linemap_macro_map_loc_to_exp_point (macro_map, location);
1206 	      else
1207 		location = loc;
1208 	    }
1209 	}
1210       else
1211 	break;
1212     }
1213   return false;
1214 }
1215 
1216 /* Return TRUE if LOCATION is a source code location of a token coming
1217    from a macro replacement-list at a macro expansion point, FALSE
1218    otherwise.  */
1219 
1220 bool
1221 linemap_location_from_macro_expansion_p (const struct line_maps *set,
1222 					 source_location location)
1223 {
1224   if (IS_ADHOC_LOC (location))
1225     location = set->location_adhoc_data_map.data[location
1226 						 & MAX_SOURCE_LOCATION].locus;
1227 
1228   linemap_assert (location <= MAX_SOURCE_LOCATION
1229 		  && (set->highest_location
1230 		      < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
1231   if (set == NULL)
1232     return false;
1233   return (location > set->highest_location);
1234 }
1235 
1236 /* Given two virtual locations *LOC0 and *LOC1, return the first
1237    common macro map in their macro expansion histories.  Return NULL
1238    if no common macro was found.  *LOC0 (resp. *LOC1) is set to the
1239    virtual location of the token inside the resulting macro.  */
1240 
1241 static const struct line_map*
1242 first_map_in_common_1 (struct line_maps *set,
1243 		       source_location *loc0,
1244 		       source_location *loc1)
1245 {
1246   source_location l0 = *loc0, l1 = *loc1;
1247   const struct line_map *map0 = linemap_lookup (set, l0),
1248     *map1 = linemap_lookup (set, l1);
1249 
1250   while (linemap_macro_expansion_map_p (map0)
1251 	 && linemap_macro_expansion_map_p (map1)
1252 	 && (map0 != map1))
1253     {
1254       if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1255 	{
1256 	  l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1257 						   l0);
1258 	  map0 = linemap_lookup (set, l0);
1259 	}
1260       else
1261 	{
1262 	  l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1263 						   l1);
1264 	  map1 = linemap_lookup (set, l1);
1265 	}
1266     }
1267 
1268   if (map0 == map1)
1269     {
1270       *loc0 = l0;
1271       *loc1 = l1;
1272       return map0;
1273     }
1274   return NULL;
1275 }
1276 
1277 /* Given two virtual locations LOC0 and LOC1, return the first common
1278    macro map in their macro expansion histories.  Return NULL if no
1279    common macro was found.  *RES_LOC0 (resp. *RES_LOC1) is set to the
1280    virtual location of the token inside the resulting macro, upon
1281    return of a non-NULL result.  */
1282 
1283 static const struct line_map*
1284 first_map_in_common (struct line_maps *set,
1285 		     source_location loc0,
1286 		     source_location loc1,
1287 		     source_location  *res_loc0,
1288 		     source_location  *res_loc1)
1289 {
1290   *res_loc0 = loc0;
1291   *res_loc1 = loc1;
1292 
1293   return first_map_in_common_1 (set, res_loc0, res_loc1);
1294 }
1295 
1296 /* Return a positive value if PRE denotes the location of a token that
1297    comes before the token of POST, 0 if PRE denotes the location of
1298    the same token as the token for POST, and a negative value
1299    otherwise.  */
1300 
1301 int
1302 linemap_compare_locations (struct line_maps *set,
1303 			   source_location  pre,
1304 			   source_location post)
1305 {
1306   bool pre_virtual_p, post_virtual_p;
1307   source_location l0 = pre, l1 = post;
1308 
1309   if (IS_ADHOC_LOC (l0))
1310     l0 = get_location_from_adhoc_loc (set, l0);
1311   if (IS_ADHOC_LOC (l1))
1312     l1 = get_location_from_adhoc_loc (set, l1);
1313 
1314   if (l0 == l1)
1315     return 0;
1316 
1317   if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1318     l0 = linemap_resolve_location (set, l0,
1319 				   LRK_MACRO_EXPANSION_POINT,
1320 				   NULL);
1321 
1322   if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1323     l1 = linemap_resolve_location (set, l1,
1324 				   LRK_MACRO_EXPANSION_POINT,
1325 				   NULL);
1326 
1327   if (l0 == l1
1328       && pre_virtual_p
1329       && post_virtual_p)
1330     {
1331       /* So pre and post represent two tokens that are present in a
1332 	 same macro expansion.  Let's see if the token for pre was
1333 	 before the token for post in that expansion.  */
1334       unsigned i0, i1;
1335       const struct line_map *map =
1336 	first_map_in_common (set, pre, post, &l0, &l1);
1337 
1338       if (map == NULL)
1339 	/* This should not be possible.  */
1340 	abort ();
1341 
1342       i0 = l0 - MAP_START_LOCATION (map);
1343       i1 = l1 - MAP_START_LOCATION (map);
1344       return i1 - i0;
1345     }
1346 
1347   if (IS_ADHOC_LOC (l0))
1348     l0 = get_location_from_adhoc_loc (set, l0);
1349   if (IS_ADHOC_LOC (l1))
1350     l1 = get_location_from_adhoc_loc (set, l1);
1351 
1352   return l1 - l0;
1353 }
1354 
1355 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
1356 
1357 static void
1358 trace_include (const struct line_maps *set, const line_map_ordinary *map)
1359 {
1360   unsigned int i = set->depth;
1361 
1362   while (--i)
1363     putc ('.', stderr);
1364 
1365   fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1366 }
1367 
1368 /* Return the spelling location of the token wherever it comes from,
1369    whether part of a macro definition or not.
1370 
1371    This is a subroutine for linemap_resolve_location.  */
1372 
1373 static source_location
1374 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1375 				     source_location location,
1376 				     const line_map_ordinary **original_map)
1377 {
1378   struct line_map *map;
1379   linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1380 
1381   while (true)
1382     {
1383       map = const_cast <line_map *> (linemap_lookup (set, location));
1384       if (!linemap_macro_expansion_map_p (map))
1385 	break;
1386 
1387       location
1388 	= linemap_macro_map_loc_unwind_toward_spelling
1389 	    (set, linemap_check_macro (map),
1390 	     location);
1391     }
1392 
1393   if (original_map)
1394     *original_map = linemap_check_ordinary (map);
1395   return location;
1396 }
1397 
1398 /* If LOCATION is the source location of a token that belongs to a
1399    macro replacement-list -- as part of a macro expansion -- then
1400    return the location of the token at the definition point of the
1401    macro.  Otherwise, return LOCATION.  SET is the set of maps
1402    location come from.  ORIGINAL_MAP is an output parm. If non NULL,
1403    the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1404    returned location comes from.
1405 
1406    This is a subroutine of linemap_resolve_location.  */
1407 
1408 static source_location
1409 linemap_macro_loc_to_def_point (struct line_maps *set,
1410 				source_location location,
1411 				const line_map_ordinary **original_map)
1412 {
1413   struct line_map *map;
1414 
1415   if (IS_ADHOC_LOC (location))
1416     location = set->location_adhoc_data_map.data[location
1417 						 & MAX_SOURCE_LOCATION].locus;
1418 
1419   linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1420 
1421   while (true)
1422     {
1423       map = const_cast <line_map *> (linemap_lookup (set, location));
1424       if (!linemap_macro_expansion_map_p (map))
1425 	break;
1426 
1427       location =
1428 	linemap_macro_map_loc_to_def_point (linemap_check_macro (map),
1429 					    location);
1430     }
1431 
1432   if (original_map)
1433     *original_map = linemap_check_ordinary (map);
1434   return location;
1435 }
1436 
1437 /* If LOCATION is the source location of a token that belongs to a
1438    macro replacement-list -- at a macro expansion point -- then return
1439    the location of the topmost expansion point of the macro.  We say
1440    topmost because if we are in the context of a nested macro
1441    expansion, the function returns the source location of the first
1442    macro expansion that triggered the nested expansions.
1443 
1444    Otherwise, return LOCATION.  SET is the set of maps location come
1445    from.  ORIGINAL_MAP is an output parm. If non NULL, the function
1446    sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1447    location comes from.
1448 
1449    This is a subroutine of linemap_resolve_location.  */
1450 
1451 static source_location
1452 linemap_macro_loc_to_exp_point (struct line_maps *set,
1453 				source_location location,
1454 				const line_map_ordinary **original_map)
1455 {
1456   struct line_map *map;
1457 
1458   if (IS_ADHOC_LOC (location))
1459     location = set->location_adhoc_data_map.data[location
1460 						 & MAX_SOURCE_LOCATION].locus;
1461 
1462   linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1463 
1464   while (true)
1465     {
1466       map = const_cast <line_map *> (linemap_lookup (set, location));
1467       if (!linemap_macro_expansion_map_p (map))
1468 	break;
1469       location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1470 						     location);
1471     }
1472 
1473   if (original_map)
1474     *original_map = linemap_check_ordinary (map);
1475   return location;
1476 }
1477 
1478 /* Resolve a virtual location into either a spelling location, an
1479    expansion point location or a token argument replacement point
1480    location.  Return the map that encodes the virtual location as well
1481    as the resolved location.
1482 
1483    If LOC is *NOT* the location of a token resulting from the
1484    expansion of a macro, then the parameter LRK (which stands for
1485    Location Resolution Kind) is ignored and the resulting location
1486    just equals the one given in argument.
1487 
1488    Now if LOC *IS* the location of a token resulting from the
1489    expansion of a macro, this is what happens.
1490 
1491    * If LRK is set to LRK_MACRO_EXPANSION_POINT
1492    -------------------------------
1493 
1494    The virtual location is resolved to the first macro expansion point
1495    that led to this macro expansion.
1496 
1497    * If LRK is set to LRK_SPELLING_LOCATION
1498    -------------------------------------
1499 
1500    The virtual location is resolved to the locus where the token has
1501    been spelled in the source.   This can follow through all the macro
1502    expansions that led to the token.
1503 
1504    * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1505    --------------------------------------
1506 
1507    The virtual location is resolved to the locus of the token in the
1508    context of the macro definition.
1509 
1510    If LOC is the locus of a token that is an argument of a
1511    function-like macro [replacing a parameter in the replacement list
1512    of the macro] the virtual location is resolved to the locus of the
1513    parameter that is replaced, in the context of the definition of the
1514    macro.
1515 
1516    If LOC is the locus of a token that is not an argument of a
1517    function-like macro, then the function behaves as if LRK was set to
1518    LRK_SPELLING_LOCATION.
1519 
1520    If MAP is not NULL, *MAP is set to the map encoding the
1521    returned location.  Note that if the returned location wasn't originally
1522    encoded by a map, then *MAP is set to NULL.  This can happen if LOC
1523    resolves to a location reserved for the client code, like
1524    UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC.  */
1525 
1526 source_location
1527 linemap_resolve_location (struct line_maps *set,
1528 			  source_location loc,
1529 			  enum location_resolution_kind lrk,
1530 			  const line_map_ordinary **map)
1531 {
1532   source_location locus = loc;
1533   if (IS_ADHOC_LOC (loc))
1534     locus = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1535 
1536   if (locus < RESERVED_LOCATION_COUNT)
1537     {
1538       /* A reserved location wasn't encoded in a map.  Let's return a
1539 	 NULL map here, just like what linemap_ordinary_map_lookup
1540 	 does.  */
1541       if (map)
1542 	*map = NULL;
1543       return loc;
1544     }
1545 
1546   switch (lrk)
1547     {
1548     case LRK_MACRO_EXPANSION_POINT:
1549       loc = linemap_macro_loc_to_exp_point (set, loc, map);
1550       break;
1551     case LRK_SPELLING_LOCATION:
1552       loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1553       break;
1554     case LRK_MACRO_DEFINITION_LOCATION:
1555       loc = linemap_macro_loc_to_def_point (set, loc, map);
1556       break;
1557     default:
1558       abort ();
1559     }
1560   return loc;
1561 }
1562 
1563 /*
1564    Suppose that LOC is the virtual location of a token T coming from
1565    the expansion of a macro M.  This function then steps up to get the
1566    location L of the point where M got expanded.  If L is a spelling
1567    location inside a macro expansion M', then this function returns
1568    the locus of the point where M' was expanded.  Said otherwise, this
1569    function returns the location of T in the context that triggered
1570    the expansion of M.
1571 
1572    *LOC_MAP must be set to the map of LOC.  This function then sets it
1573    to the map of the returned location.  */
1574 
1575 source_location
1576 linemap_unwind_toward_expansion (struct line_maps *set,
1577 				 source_location loc,
1578 				 const struct line_map **map)
1579 {
1580   source_location resolved_location;
1581   const line_map_macro *macro_map = linemap_check_macro (*map);
1582   const struct line_map *resolved_map;
1583 
1584   if (IS_ADHOC_LOC (loc))
1585     loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1586 
1587   resolved_location =
1588     linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
1589   resolved_map = linemap_lookup (set, resolved_location);
1590 
1591   if (!linemap_macro_expansion_map_p (resolved_map))
1592     {
1593       resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
1594       resolved_map = linemap_lookup (set, resolved_location);
1595     }
1596 
1597   *map = resolved_map;
1598   return resolved_location;
1599 }
1600 
1601 /* If LOC is the virtual location of a token coming from the expansion
1602    of a macro M and if its spelling location is reserved (e.g, a
1603    location for a built-in token), then this function unwinds (using
1604    linemap_unwind_toward_expansion) the location until a location that
1605    is not reserved and is not in a system header is reached.  In other
1606    words, this unwinds the reserved location until a location that is
1607    in real source code is reached.
1608 
1609    Otherwise, if the spelling location for LOC is not reserved or if
1610    LOC doesn't come from the expansion of a macro, the function
1611    returns LOC as is and *MAP is not touched.
1612 
1613    *MAP is set to the map of the returned location if the later is
1614    different from LOC.  */
1615 source_location
1616 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1617 					  source_location loc,
1618 					  const struct line_map **map)
1619 {
1620   source_location resolved_loc;
1621   const struct line_map *map0 = NULL;
1622   const line_map_ordinary *map1 = NULL;
1623 
1624   if (IS_ADHOC_LOC (loc))
1625     loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1626 
1627   map0 = linemap_lookup (set, loc);
1628   if (!linemap_macro_expansion_map_p (map0))
1629     return loc;
1630 
1631   resolved_loc = linemap_resolve_location (set, loc,
1632 					   LRK_SPELLING_LOCATION,
1633 					   &map1);
1634 
1635   if (resolved_loc >= RESERVED_LOCATION_COUNT
1636       && !LINEMAP_SYSP (map1))
1637     return loc;
1638 
1639   while (linemap_macro_expansion_map_p (map0)
1640 	 && (resolved_loc < RESERVED_LOCATION_COUNT
1641 	     || LINEMAP_SYSP (map1)))
1642     {
1643       loc = linemap_unwind_toward_expansion (set, loc, &map0);
1644       resolved_loc = linemap_resolve_location (set, loc,
1645 					       LRK_SPELLING_LOCATION,
1646 					       &map1);
1647     }
1648 
1649   if (map != NULL)
1650     *map = map0;
1651   return loc;
1652 }
1653 
1654 /* Expand source code location LOC and return a user readable source
1655    code location.  LOC must be a spelling (non-virtual) location.  If
1656    it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1657    location is returned.  */
1658 
1659 expanded_location
1660 linemap_expand_location (struct line_maps *set,
1661 			 const struct line_map *map,
1662 			 source_location loc)
1663 
1664 {
1665   expanded_location xloc;
1666 
1667   memset (&xloc, 0, sizeof (xloc));
1668   if (IS_ADHOC_LOC (loc))
1669     {
1670       xloc.data
1671 	= set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1672       loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1673     }
1674 
1675   if (loc < RESERVED_LOCATION_COUNT)
1676     /* The location for this token wasn't generated from a line map.
1677        It was probably a location for a builtin token, chosen by some
1678        client code.  Let's not try to expand the location in that
1679        case.  */;
1680   else if (map == NULL)
1681     /* We shouldn't be getting a NULL map with a location that is not
1682        reserved by the client code.  */
1683     abort ();
1684   else
1685     {
1686       /* MAP must be an ordinary map and LOC must be non-virtual,
1687 	 encoded into this map, obviously; the accessors used on MAP
1688 	 below ensure it is ordinary.  Let's just assert the
1689 	 non-virtualness of LOC here.  */
1690       if (linemap_location_from_macro_expansion_p (set, loc))
1691 	abort ();
1692 
1693       const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1694 
1695       xloc.file = LINEMAP_FILE (ord_map);
1696       xloc.line = SOURCE_LINE (ord_map, loc);
1697       xloc.column = SOURCE_COLUMN (ord_map, loc);
1698       xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
1699     }
1700 
1701   return xloc;
1702 }
1703 
1704 
1705 /* Dump line map at index IX in line table SET to STREAM.  If STREAM
1706    is NULL, use stderr.  IS_MACRO is true if the caller wants to
1707    dump a macro map, false otherwise.  */
1708 
1709 void
1710 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1711 {
1712   const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1713       = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1714 	  "LC_ENTER_MACRO" };
1715   const char *reason;
1716   const line_map *map;
1717 
1718   if (stream == NULL)
1719     stream = stderr;
1720 
1721   if (!is_macro)
1722     map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1723   else
1724     map = LINEMAPS_MACRO_MAP_AT (set, ix);
1725 
1726   reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1727 
1728   fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1729 	   ix, (void *) map, map->start_location, reason,
1730 	   ((!is_macro
1731 	     && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1732 	    ? "yes" : "no"));
1733   if (!is_macro)
1734     {
1735       const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1736       unsigned includer_ix;
1737       const line_map_ordinary *includer_map;
1738 
1739       includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
1740       includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1741 		     ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1742 		     : NULL;
1743 
1744       fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1745 	       ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
1746       fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1747 	       includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1748     }
1749   else
1750     {
1751       const line_map_macro *macro_map = linemap_check_macro (map);
1752       fprintf (stream, "Macro: %s (%u tokens)\n",
1753 	       linemap_map_get_macro_name (macro_map),
1754 	       MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1755     }
1756 
1757   fprintf (stream, "\n");
1758 }
1759 
1760 
1761 /* Dump debugging information about source location LOC into the file
1762    stream STREAM. SET is the line map set LOC comes from.  */
1763 
1764 void
1765 linemap_dump_location (struct line_maps *set,
1766 		       source_location loc,
1767 		       FILE *stream)
1768 {
1769   const line_map_ordinary *map;
1770   source_location location;
1771   const char *path = "", *from = "";
1772   int l = -1, c = -1, s = -1, e = -1;
1773 
1774   if (IS_ADHOC_LOC (loc))
1775     loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1776 
1777   if (loc == 0)
1778     return;
1779 
1780   location =
1781     linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1782 
1783   if (map == NULL)
1784     /* Only reserved locations can be tolerated in this case.  */
1785     linemap_assert (location < RESERVED_LOCATION_COUNT);
1786   else
1787     {
1788       path = LINEMAP_FILE (map);
1789       l = SOURCE_LINE (map, location);
1790       c = SOURCE_COLUMN (map, location);
1791       s = LINEMAP_SYSP (map) != 0;
1792       e = location != loc;
1793       if (e)
1794 	from = "N/A";
1795       else
1796 	from = (INCLUDED_FROM (set, map))
1797 	  ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1798 	  : "<NULL>";
1799     }
1800 
1801   /* P: path, L: line, C: column, S: in-system-header, M: map address,
1802      E: macro expansion?, LOC: original location, R: resolved location   */
1803   fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1804 	   path, from, l, c, s, (void*)map, e, loc, location);
1805 }
1806 
1807 /* Return the highest location emitted for a given file for which
1808    there is a line map in SET.  FILE_NAME is the file name to
1809    consider.  If the function returns TRUE, *LOC is set to the highest
1810    location emitted for that file.  */
1811 
1812 bool
1813 linemap_get_file_highest_location (struct line_maps *set,
1814 				   const char *file_name,
1815 				   source_location *loc)
1816 {
1817   /* If the set is empty or no ordinary map has been created then
1818      there is no file to look for ...  */
1819   if (set == NULL || set->info_ordinary.used == 0)
1820     return false;
1821 
1822   /* Now look for the last ordinary map created for FILE_NAME.  */
1823   int i;
1824   for (i = set->info_ordinary.used - 1; i >= 0; --i)
1825     {
1826       const char *fname = set->info_ordinary.maps[i].to_file;
1827       if (fname && !filename_cmp (fname, file_name))
1828 	break;
1829     }
1830 
1831   if (i < 0)
1832     return false;
1833 
1834   /* The highest location for a given map is either the starting
1835      location of the next map minus one, or -- if the map is the
1836      latest one -- the highest location of the set.  */
1837   source_location result;
1838   if (i == (int) set->info_ordinary.used - 1)
1839     result = set->highest_location;
1840   else
1841     result = set->info_ordinary.maps[i + 1].start_location - 1;
1842 
1843   *loc = result;
1844   return true;
1845 }
1846 
1847 /* Compute and return statistics about the memory consumption of some
1848    parts of the line table SET.  */
1849 
1850 void
1851 linemap_get_statistics (struct line_maps *set,
1852 			struct linemap_stats *s)
1853 {
1854   long ordinary_maps_allocated_size, ordinary_maps_used_size,
1855     macro_maps_allocated_size, macro_maps_used_size,
1856     macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1857 
1858   const line_map_macro *cur_map;
1859 
1860   ordinary_maps_allocated_size =
1861     LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
1862 
1863   ordinary_maps_used_size =
1864     LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
1865 
1866   macro_maps_allocated_size =
1867     LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
1868 
1869   for (cur_map = LINEMAPS_MACRO_MAPS (set);
1870        cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1871        ++cur_map)
1872     {
1873       unsigned i;
1874 
1875       linemap_assert (linemap_macro_expansion_map_p (cur_map));
1876 
1877       macro_maps_locations_size +=
1878 	2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1879 
1880       for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1881 	{
1882 	  if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1883 	      MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1884 	    duplicated_macro_maps_locations_size +=
1885 	      sizeof (source_location);
1886 	}
1887     }
1888 
1889   macro_maps_used_size =
1890     LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
1891 
1892   s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1893   s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1894   s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1895   s->ordinary_maps_used_size = ordinary_maps_used_size;
1896   s->num_expanded_macros = num_expanded_macros_counter;
1897   s->num_macro_tokens = num_macro_tokens_counter;
1898   s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1899   s->macro_maps_allocated_size = macro_maps_allocated_size;
1900   s->macro_maps_locations_size = macro_maps_locations_size;
1901   s->macro_maps_used_size = macro_maps_used_size;
1902   s->duplicated_macro_maps_locations_size =
1903     duplicated_macro_maps_locations_size;
1904   s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1905 			 * sizeof (struct location_adhoc_data));
1906   s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
1907 }
1908 
1909 
1910 /* Dump line table SET to STREAM.  If STREAM is NULL, stderr is used.
1911    NUM_ORDINARY specifies how many ordinary maps to dump.  NUM_MACRO
1912    specifies how many macro maps to dump.  */
1913 
1914 void
1915 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1916 		 unsigned int num_macro)
1917 {
1918   unsigned int i;
1919 
1920   if (set == NULL)
1921     return;
1922 
1923   if (stream == NULL)
1924     stream = stderr;
1925 
1926   fprintf (stream, "# of ordinary maps:  %d\n", LINEMAPS_ORDINARY_USED (set));
1927   fprintf (stream, "# of macro maps:     %d\n", LINEMAPS_MACRO_USED (set));
1928   fprintf (stream, "Include stack depth: %d\n", set->depth);
1929   fprintf (stream, "Highest location:    %u\n", set->highest_location);
1930 
1931   if (num_ordinary)
1932     {
1933       fprintf (stream, "\nOrdinary line maps\n");
1934       for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1935 	linemap_dump (stream, set, i, false);
1936       fprintf (stream, "\n");
1937     }
1938 
1939   if (num_macro)
1940     {
1941       fprintf (stream, "\nMacro line maps\n");
1942       for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1943 	linemap_dump (stream, set, i, true);
1944       fprintf (stream, "\n");
1945     }
1946 }
1947 
1948 /* struct source_range.  */
1949 
1950 /* Is there any part of this range on the given line?  */
1951 
1952 bool
1953 source_range::intersects_line_p (const char *file, int line) const
1954 {
1955   expanded_location exploc_start
1956     = linemap_client_expand_location_to_spelling_point (m_start);
1957   if (file != exploc_start.file)
1958     return false;
1959   if (line < exploc_start.line)
1960       return false;
1961   expanded_location exploc_finish
1962     = linemap_client_expand_location_to_spelling_point (m_finish);
1963   if (file != exploc_finish.file)
1964     return false;
1965   if (line > exploc_finish.line)
1966       return false;
1967   return true;
1968 }
1969 
1970 /* class rich_location.  */
1971 
1972 /* Construct a rich_location with location LOC as its initial range.  */
1973 
1974 rich_location::rich_location (line_maps */*set*/, source_location loc) :
1975   m_num_ranges (0),
1976   m_column_override (0),
1977   m_have_expanded_location (false),
1978   m_num_fixit_hints (0)
1979 {
1980   add_range (loc, true);
1981 }
1982 
1983 /* The destructor for class rich_location.  */
1984 
1985 rich_location::~rich_location ()
1986 {
1987   for (unsigned int i = 0; i < m_num_fixit_hints; i++)
1988     delete m_fixit_hints[i];
1989 }
1990 
1991 /* Get location IDX within this rich_location.  */
1992 
1993 source_location
1994 rich_location::get_loc (unsigned int idx) const
1995 {
1996   linemap_assert (idx < m_num_ranges);
1997   return m_ranges[idx].m_loc;
1998 }
1999 
2000 /* Expand location IDX within this rich_location.  */
2001 /* Get an expanded_location for this rich_location's primary
2002    location.  */
2003 
2004 expanded_location
2005 rich_location::get_expanded_location (unsigned int idx)
2006 {
2007   if (idx == 0)
2008    {
2009      /* Cache the expansion of the primary location.  */
2010      if (!m_have_expanded_location)
2011        {
2012 	  m_expanded_location
2013 	    = linemap_client_expand_location_to_spelling_point (get_loc (0));
2014 	  if (m_column_override)
2015 	    m_expanded_location.column = m_column_override;
2016 	  m_have_expanded_location = true;
2017        }
2018 
2019      return m_expanded_location;
2020    }
2021   else
2022     return linemap_client_expand_location_to_spelling_point (get_loc (idx));
2023 }
2024 
2025 /* Set the column of the primary location, with 0 meaning
2026    "don't override it".  */
2027 
2028 void
2029 rich_location::override_column (int column)
2030 {
2031   m_column_override = column;
2032   m_have_expanded_location = false;
2033 }
2034 
2035 /* Add the given range.  */
2036 
2037 void
2038 rich_location::add_range (source_location loc, bool show_caret_p)
2039 {
2040   linemap_assert (m_num_ranges < MAX_RANGES);
2041 
2042   location_range *range = &m_ranges[m_num_ranges++];
2043   range->m_loc = loc;
2044   range->m_show_caret_p = show_caret_p;
2045 }
2046 
2047 /* Add or overwrite the location given by IDX, setting its location to LOC,
2048    and setting its "should my caret be printed" flag to SHOW_CARET_P.
2049 
2050    It must either overwrite an existing location, or add one *exactly* on
2051    the end of the array.
2052 
2053    This is primarily for use by gcc when implementing diagnostic format
2054    decoders e.g.
2055    - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2056      (which writes the source location of a tree back into location 0 of
2057      the rich_location), and
2058    - the "%C" and "%L" format codes in the Fortran frontend.  */
2059 
2060 void
2061 rich_location::set_range (line_maps * /*set*/, unsigned int idx,
2062 			  source_location loc, bool show_caret_p)
2063 {
2064   linemap_assert (idx < MAX_RANGES);
2065 
2066   /* We can either overwrite an existing range, or add one exactly
2067      on the end of the array.  */
2068   linemap_assert (idx <= m_num_ranges);
2069 
2070   location_range *locrange = &m_ranges[idx];
2071   locrange->m_loc = loc;
2072   locrange->m_show_caret_p = show_caret_p;
2073 
2074   /* Are we adding a range onto the end?  */
2075   if (idx == m_num_ranges)
2076     m_num_ranges = idx + 1;
2077 
2078   if (idx == 0)
2079     /* Mark any cached value here as dirty.  */
2080     m_have_expanded_location = false;
2081 }
2082 
2083 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2084    at WHERE.  */
2085 
2086 void
2087 rich_location::add_fixit_insert (source_location where,
2088 				 const char *new_content)
2089 {
2090   linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2091   m_fixit_hints[m_num_fixit_hints++]
2092     = new fixit_insert (where, new_content);
2093 }
2094 
2095 /* Add a fixit-hint, suggesting removal of the content at
2096    SRC_RANGE.  */
2097 
2098 void
2099 rich_location::add_fixit_remove (source_range src_range)
2100 {
2101   linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2102   m_fixit_hints[m_num_fixit_hints++] = new fixit_remove (src_range);
2103 }
2104 
2105 /* Add a fixit-hint, suggesting replacement of the content at
2106    SRC_RANGE with NEW_CONTENT.  */
2107 
2108 void
2109 rich_location::add_fixit_replace (source_range src_range,
2110 				  const char *new_content)
2111 {
2112   linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2113   m_fixit_hints[m_num_fixit_hints++]
2114     = new fixit_replace (src_range, new_content);
2115 }
2116 
2117 /* class fixit_insert.  */
2118 
2119 fixit_insert::fixit_insert (source_location where,
2120 			    const char *new_content)
2121 : m_where (where),
2122   m_bytes (xstrdup (new_content)),
2123   m_len (strlen (new_content))
2124 {
2125 }
2126 
2127 fixit_insert::~fixit_insert ()
2128 {
2129   free (m_bytes);
2130 }
2131 
2132 /* Implementation of fixit_hint::affects_line_p for fixit_insert.  */
2133 
2134 bool
2135 fixit_insert::affects_line_p (const char *file, int line)
2136 {
2137   expanded_location exploc
2138     = linemap_client_expand_location_to_spelling_point (m_where);
2139   if (file == exploc.file)
2140     if (line == exploc.line)
2141       return true;
2142   return false;
2143 }
2144 
2145 /* class fixit_remove.  */
2146 
2147 fixit_remove::fixit_remove (source_range src_range)
2148 : m_src_range (src_range)
2149 {
2150 }
2151 
2152 /* Implementation of fixit_hint::affects_line_p for fixit_remove.  */
2153 
2154 bool
2155 fixit_remove::affects_line_p (const char *file, int line)
2156 {
2157   return m_src_range.intersects_line_p (file, line);
2158 }
2159 
2160 /* class fixit_replace.  */
2161 
2162 fixit_replace::fixit_replace (source_range src_range,
2163 			      const char *new_content)
2164 : m_src_range (src_range),
2165   m_bytes (xstrdup (new_content)),
2166   m_len (strlen (new_content))
2167 {
2168 }
2169 
2170 fixit_replace::~fixit_replace ()
2171 {
2172   free (m_bytes);
2173 }
2174 
2175 /* Implementation of fixit_hint::affects_line_p for fixit_replace.  */
2176 
2177 bool
2178 fixit_replace::affects_line_p (const char *file, int line)
2179 {
2180   return m_src_range.intersects_line_p (file, line);
2181 }
2182