1/* Map logical line numbers to (source file, line number) pairs.
2   Copyright (C) 2001-2015 Free Software Foundation, Inc.
3
4This program is free software; you can redistribute it and/or modify it
5under the terms of the GNU General Public License as published by the
6Free Software Foundation; either version 3, or (at your option) any
7later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
29static void trace_include (const struct line_maps *, const struct line_map *);
30static const struct line_map * linemap_ordinary_map_lookup (struct line_maps *,
31							    source_location);
32static const struct line_map* linemap_macro_map_lookup (struct line_maps *,
33							source_location);
34static source_location linemap_macro_map_loc_to_def_point
35(const struct line_map*, source_location);
36static source_location linemap_macro_map_loc_unwind_toward_spelling
37(const struct line_map*, source_location);
38static source_location linemap_macro_map_loc_to_exp_point
39(const struct line_map*, source_location);
40static source_location linemap_macro_loc_to_spelling_point
41(struct line_maps *, source_location, const struct line_map **);
42static source_location linemap_macro_loc_to_def_point (struct line_maps *,
43						       source_location,
44						       const struct line_map **);
45static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
46						       source_location,
47						       const struct line_map **);
48
49/* Counters defined in macro.c.  */
50extern unsigned num_expanded_macros_counter;
51extern unsigned num_macro_tokens_counter;
52
53/* Hash function for location_adhoc_data hashtable.  */
54
55static hashval_t
56location_adhoc_data_hash (const void *l)
57{
58  const struct location_adhoc_data *lb =
59      (const struct location_adhoc_data *) l;
60  return (hashval_t) lb->locus + (size_t) lb->data;
61}
62
63/* Compare function for location_adhoc_data hashtable.  */
64
65static int
66location_adhoc_data_eq (const void *l1, const void *l2)
67{
68  const struct location_adhoc_data *lb1 =
69      (const struct location_adhoc_data *) l1;
70  const struct location_adhoc_data *lb2 =
71      (const struct location_adhoc_data *) l2;
72  return lb1->locus == lb2->locus && lb1->data == lb2->data;
73}
74
75/* Update the hashtable when location_adhoc_data is reallocated.  */
76
77static int
78location_adhoc_data_update (void **slot, void *data)
79{
80  *((char **) slot) += *((long long *) data);
81  return 1;
82}
83
84/* Rebuild the hash table from the location adhoc data.  */
85
86void
87rebuild_location_adhoc_htab (struct line_maps *set)
88{
89  unsigned i;
90  set->location_adhoc_data_map.htab =
91      htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
92  for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
93    htab_find_slot (set->location_adhoc_data_map.htab,
94		    set->location_adhoc_data_map.data + i, INSERT);
95}
96
97/* Combine LOCUS and DATA to a combined adhoc loc.  */
98
99source_location
100get_combined_adhoc_loc (struct line_maps *set,
101			source_location locus, void *data)
102{
103  struct location_adhoc_data lb;
104  struct location_adhoc_data **slot;
105
106  linemap_assert (data);
107
108  if (IS_ADHOC_LOC (locus))
109    locus
110      = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
111  if (locus == 0 && data == NULL)
112    return 0;
113  lb.locus = locus;
114  lb.data = data;
115  slot = (struct location_adhoc_data **)
116      htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
117  if (*slot == NULL)
118    {
119      if (set->location_adhoc_data_map.curr_loc >=
120	  set->location_adhoc_data_map.allocated)
121	{
122	  char *orig_data = (char *) set->location_adhoc_data_map.data;
123	  long long offset;
124	  /* Cast away extern "C" from the type of xrealloc.  */
125	  line_map_realloc reallocator = (set->reallocator
126					  ? set->reallocator
127					  : (line_map_realloc) xrealloc);
128
129	  if (set->location_adhoc_data_map.allocated == 0)
130	    set->location_adhoc_data_map.allocated = 128;
131	  else
132	    set->location_adhoc_data_map.allocated *= 2;
133	  set->location_adhoc_data_map.data = (struct location_adhoc_data *)
134	      reallocator (set->location_adhoc_data_map.data,
135			   set->location_adhoc_data_map.allocated
136			   * sizeof (struct location_adhoc_data));
137	  offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
138	  if (set->location_adhoc_data_map.allocated > 128)
139	    htab_traverse (set->location_adhoc_data_map.htab,
140			   location_adhoc_data_update, &offset);
141	}
142      *slot = set->location_adhoc_data_map.data
143	      + set->location_adhoc_data_map.curr_loc;
144      set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
145	= lb;
146    }
147  return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
148}
149
150/* Return the data for the adhoc loc.  */
151
152void *
153get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
154{
155  linemap_assert (IS_ADHOC_LOC (loc));
156  return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
157}
158
159/* Return the location for the adhoc loc.  */
160
161source_location
162get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
163{
164  linemap_assert (IS_ADHOC_LOC (loc));
165  return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
166}
167
168/* Finalize the location_adhoc_data structure.  */
169void
170location_adhoc_data_fini (struct line_maps *set)
171{
172  htab_delete (set->location_adhoc_data_map.htab);
173}
174
175/* Initialize a line map set.  */
176
177void
178linemap_init (struct line_maps *set,
179	      source_location builtin_location)
180{
181  memset (set, 0, sizeof (struct line_maps));
182  set->highest_location = RESERVED_LOCATION_COUNT - 1;
183  set->highest_line = RESERVED_LOCATION_COUNT - 1;
184  set->location_adhoc_data_map.htab =
185      htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
186  set->builtin_location = builtin_location;
187}
188
189/* Check for and warn about line_maps entered but not exited.  */
190
191void
192linemap_check_files_exited (struct line_maps *set)
193{
194  struct line_map *map;
195  /* Depending upon whether we are handling preprocessed input or
196     not, this can be a user error or an ICE.  */
197  for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
198       ! MAIN_FILE_P (map);
199       map = INCLUDED_FROM (set, map))
200    fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
201	     ORDINARY_MAP_FILE_NAME (map));
202}
203
204/* Create a new line map in the line map set SET, and return it.
205   REASON is the reason of creating the map. It determines the type
206   of map created (ordinary or macro map). Note that ordinary maps and
207   macro maps are allocated in different memory location.  */
208
209static struct line_map *
210new_linemap (struct line_maps *set,
211	     enum lc_reason reason)
212{
213  /* Depending on this variable, a macro map would be allocated in a
214     different memory location than an ordinary map.  */
215  bool macro_map_p = (reason == LC_ENTER_MACRO);
216  struct line_map *result;
217
218  if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
219    {
220      /* We ran out of allocated line maps. Let's allocate more.  */
221      unsigned alloc_size;
222
223      /* Cast away extern "C" from the type of xrealloc.  */
224      line_map_realloc reallocator = (set->reallocator
225				      ? set->reallocator
226				      : (line_map_realloc) xrealloc);
227      line_map_round_alloc_size_func round_alloc_size =
228	set->round_alloc_size;
229
230      /* We are going to execute some dance to try to reduce the
231	 overhead of the memory allocator, in case we are using the
232	 ggc-page.c one.
233
234	 The actual size of memory we are going to get back from the
235	 allocator is the smallest power of 2 that is greater than the
236	 size we requested.  So let's consider that size then.  */
237
238      alloc_size =
239	(2 * LINEMAPS_ALLOCATED (set, macro_map_p) +  256)
240	* sizeof (struct line_map);
241
242      /* Get the actual size of memory that is going to be allocated
243	 by the allocator.  */
244      alloc_size = round_alloc_size (alloc_size);
245
246      /* Now alloc_size contains the exact memory size we would get if
247	 we have asked for the initial alloc_size amount of memory.
248	 Let's get back to the number of macro map that amounts
249	 to.  */
250      LINEMAPS_ALLOCATED (set, macro_map_p) =
251	alloc_size / (sizeof (struct line_map));
252
253      /* And now let's really do the re-allocation.  */
254      LINEMAPS_MAPS (set, macro_map_p) =
255	(struct line_map *) (*reallocator)
256	(LINEMAPS_MAPS (set, macro_map_p),
257	 (LINEMAPS_ALLOCATED (set, macro_map_p)
258	  * sizeof (struct line_map)));
259
260      result =
261	&LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
262      memset (result, 0,
263	      ((LINEMAPS_ALLOCATED (set, macro_map_p)
264		- LINEMAPS_USED (set, macro_map_p))
265	       * sizeof (struct line_map)));
266    }
267  else
268    result =
269      &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
270
271  LINEMAPS_USED (set, macro_map_p)++;
272
273  result->reason = reason;
274  return result;
275}
276
277/* Add a mapping of logical source line to physical source file and
278   line number.
279
280   The text pointed to by TO_FILE must have a lifetime
281   at least as long as the final call to lookup_line ().  An empty
282   TO_FILE means standard input.  If reason is LC_LEAVE, and
283   TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
284   natural values considering the file we are returning to.
285
286   FROM_LINE should be monotonic increasing across calls to this
287   function.  A call to this function can relocate the previous set of
288   maps, so any stored line_map pointers should not be used.  */
289
290const struct line_map *
291linemap_add (struct line_maps *set, enum lc_reason reason,
292	     unsigned int sysp, const char *to_file, linenum_type to_line)
293{
294  struct line_map *map;
295  source_location start_location = set->highest_location + 1;
296
297  linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
298		    && (start_location
299			< MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
300
301  /* When we enter the file for the first time reason cannot be
302     LC_RENAME.  */
303  linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
304
305  /* If we are leaving the main file, return a NULL map.  */
306  if (reason == LC_LEAVE
307      && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
308      && to_file == NULL)
309    {
310      set->depth--;
311      return NULL;
312    }
313
314  map = new_linemap (set, reason);
315
316  if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
317    to_file = "<stdin>";
318
319  if (reason == LC_RENAME_VERBATIM)
320    reason = LC_RENAME;
321
322  if (reason == LC_LEAVE)
323    {
324      /* When we are just leaving an "included" file, and jump to the next
325	 location inside the "includer" right after the #include
326	 "included", this variable points the map in use right before the
327	 #include "included", inside the same "includer" file.  */
328      struct line_map *from;
329      bool error;
330
331      if (MAIN_FILE_P (map - 1))
332	{
333	  /* So this _should_ mean we are leaving the main file --
334	     effectively ending the compilation unit. But to_file not
335	     being NULL means the caller thinks we are leaving to
336	     another file. This is an erroneous behaviour but we'll
337	     try to recover from it. Let's pretend we are not leaving
338	     the main file.  */
339	  error = true;
340          reason = LC_RENAME;
341          from = map - 1;
342	}
343      else
344	{
345	  /* (MAP - 1) points to the map we are leaving. The
346	     map from which (MAP - 1) got included should be the map
347	     that comes right before MAP in the same file.  */
348	  from = INCLUDED_FROM (set, map - 1);
349	  error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
350					   to_file);
351	}
352
353      /* Depending upon whether we are handling preprocessed input or
354	 not, this can be a user error or an ICE.  */
355      if (error)
356	fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
357		 to_file);
358
359      /* A TO_FILE of NULL is special - we use the natural values.  */
360      if (error || to_file == NULL)
361	{
362	  to_file = ORDINARY_MAP_FILE_NAME (from);
363	  to_line = SOURCE_LINE (from, from[1].start_location);
364	  sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
365	}
366    }
367
368  linemap_assert (reason != LC_ENTER_MACRO);
369  ORDINARY_MAP_IN_SYSTEM_HEADER_P (map) = sysp;
370  MAP_START_LOCATION (map) = start_location;
371  ORDINARY_MAP_FILE_NAME (map) = to_file;
372  ORDINARY_MAP_STARTING_LINE_NUMBER (map) = to_line;
373  LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
374  ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = 0;
375  set->highest_location = start_location;
376  set->highest_line = start_location;
377  set->max_column_hint = 0;
378
379  if (reason == LC_ENTER)
380    {
381      ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
382	set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
383      set->depth++;
384      if (set->trace_includes)
385	trace_include (set, map);
386    }
387  else if (reason == LC_RENAME)
388    ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
389      ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
390  else if (reason == LC_LEAVE)
391    {
392      set->depth--;
393      ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
394	ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
395    }
396
397  return map;
398}
399
400/* Returns TRUE if the line table set tracks token locations across
401   macro expansion, FALSE otherwise.  */
402
403bool
404linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
405{
406  return LINEMAPS_MACRO_MAPS (set) != NULL;
407}
408
409/* Create a macro map.  A macro map encodes source locations of tokens
410   that are part of a macro replacement-list, at a macro expansion
411   point.  See the extensive comments of struct line_map and struct
412   line_map_macro, in line-map.h.
413
414   This map shall be created when the macro is expanded.  The map
415   encodes the source location of the expansion point of the macro as
416   well as the "original" source location of each token that is part
417   of the macro replacement-list.  If a macro is defined but never
418   expanded, it has no macro map.  SET is the set of maps the macro
419   map should be part of.  MACRO_NODE is the macro which the new macro
420   map should encode source locations for.  EXPANSION is the location
421   of the expansion point of MACRO. For function-like macros
422   invocations, it's best to make it point to the closing parenthesis
423   of the macro, rather than the the location of the first character
424   of the macro.  NUM_TOKENS is the number of tokens that are part of
425   the replacement-list of MACRO.
426
427   Note that when we run out of the integer space available for source
428   locations, this function returns NULL.  In that case, callers of
429   this function cannot encode {line,column} pairs into locations of
430   macro tokens anymore.  */
431
432const struct line_map *
433linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
434		     source_location expansion, unsigned int num_tokens)
435{
436  struct line_map *map;
437  source_location start_location;
438  /* Cast away extern "C" from the type of xrealloc.  */
439  line_map_realloc reallocator = (set->reallocator
440				  ? set->reallocator
441				  : (line_map_realloc) xrealloc);
442
443  start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
444
445  if (start_location <= set->highest_line
446      || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
447    /* We ran out of macro map space.   */
448    return NULL;
449
450  map = new_linemap (set, LC_ENTER_MACRO);
451
452  MAP_START_LOCATION (map) = start_location;
453  MACRO_MAP_MACRO (map) = macro_node;
454  MACRO_MAP_NUM_MACRO_TOKENS (map) = num_tokens;
455  MACRO_MAP_LOCATIONS (map)
456    = (source_location*) reallocator (NULL,
457				      2 * num_tokens
458				      * sizeof (source_location));
459  MACRO_MAP_EXPANSION_POINT_LOCATION (map) = expansion;
460  memset (MACRO_MAP_LOCATIONS (map), 0,
461	  num_tokens * sizeof (source_location));
462
463  LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
464
465  return map;
466}
467
468/* Create and return a virtual location for a token that is part of a
469   macro expansion-list at a macro expansion point.  See the comment
470   inside struct line_map_macro to see what an expansion-list exactly
471   is.
472
473   A call to this function must come after a call to
474   linemap_enter_macro.
475
476   MAP is the map into which the source location is created.  TOKEN_NO
477   is the index of the token in the macro replacement-list, starting
478   at number 0.
479
480   ORIG_LOC is the location of the token outside of this macro
481   expansion.  If the token comes originally from the macro
482   definition, it is the locus in the macro definition; otherwise it
483   is a location in the context of the caller of this macro expansion
484   (which is a virtual location or a source location if the caller is
485   itself a macro expansion or not).
486
487   ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
488   either of the token itself or of a macro parameter that it
489   replaces.  */
490
491source_location
492linemap_add_macro_token (const struct line_map *map,
493			 unsigned int token_no,
494			 source_location orig_loc,
495			 source_location orig_parm_replacement_loc)
496{
497  source_location result;
498
499  linemap_assert (linemap_macro_expansion_map_p (map));
500  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
501
502  MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
503  MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
504
505  result = MAP_START_LOCATION (map) + token_no;
506  return result;
507}
508
509/* Return a source_location for the start (i.e. column==0) of
510   (physical) line TO_LINE in the current source file (as in the
511   most recent linemap_add).   MAX_COLUMN_HINT is the highest column
512   number we expect to use in this line (but it does not change
513   the highest_location).  */
514
515source_location
516linemap_line_start (struct line_maps *set, linenum_type to_line,
517		    unsigned int max_column_hint)
518{
519  struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
520  source_location highest = set->highest_location;
521  source_location r;
522  linenum_type last_line =
523    SOURCE_LINE (map, set->highest_line);
524  int line_delta = to_line - last_line;
525  bool add_map = false;
526
527  if (line_delta < 0
528      || (line_delta > 10
529	  && line_delta * ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) > 1000)
530      || (max_column_hint >= (1U << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)))
531      || (max_column_hint <= 80
532	  && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10)
533      || (highest > 0x60000000
534	  && (set->max_column_hint || highest > 0x70000000)))
535    add_map = true;
536  else
537    max_column_hint = set->max_column_hint;
538  if (add_map)
539    {
540      int column_bits;
541      if (max_column_hint > 100000 || highest > 0x60000000)
542	{
543	  /* If the column number is ridiculous or we've allocated a huge
544	     number of source_locations, give up on column numbers. */
545	  max_column_hint = 0;
546	  if (highest > 0x70000000)
547	    return 0;
548	  column_bits = 0;
549	}
550      else
551	{
552	  column_bits = 7;
553	  while (max_column_hint >= (1U << column_bits))
554	    column_bits++;
555	  max_column_hint = 1U << column_bits;
556	}
557      /* Allocate the new line_map.  However, if the current map only has a
558	 single line we can sometimes just increase its column_bits instead. */
559      if (line_delta < 0
560	  || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
561	  || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
562	map = (struct line_map *) linemap_add (set, LC_RENAME,
563					       ORDINARY_MAP_IN_SYSTEM_HEADER_P
564					       (map),
565					       ORDINARY_MAP_FILE_NAME (map),
566					       to_line);
567      ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = column_bits;
568      r = (MAP_START_LOCATION (map)
569	   + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
570	      << column_bits));
571    }
572  else
573    r = highest - SOURCE_COLUMN (map, highest)
574      + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
575
576  /* Locations of ordinary tokens are always lower than locations of
577     macro tokens.  */
578  if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
579    return 0;
580
581  set->highest_line = r;
582  if (r > set->highest_location)
583    set->highest_location = r;
584  set->max_column_hint = max_column_hint;
585  return r;
586}
587
588/* Encode and return a source_location from a column number. The
589   source line considered is the last source line used to call
590   linemap_line_start, i.e, the last source line which a location was
591   encoded from.  */
592
593source_location
594linemap_position_for_column (struct line_maps *set, unsigned int to_column)
595{
596  source_location r = set->highest_line;
597
598  linemap_assert
599    (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
600
601  if (to_column >= set->max_column_hint)
602    {
603      if (r >= 0xC000000 || to_column > 100000)
604	{
605	  /* Running low on source_locations - disable column numbers.  */
606	  return r;
607	}
608      else
609	{
610	  struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
611	  r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
612	}
613    }
614  r = r + to_column;
615  if (r >= set->highest_location)
616    set->highest_location = r;
617  return r;
618}
619
620/* Encode and return a source location from a given line and
621   column.  */
622
623source_location
624linemap_position_for_line_and_column (const struct line_map *map,
625				      linenum_type line,
626				      unsigned column)
627{
628  linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (map) <= line);
629
630  return (MAP_START_LOCATION (map)
631	  + ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
632	     << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
633	  + (column & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1)));
634}
635
636/* Encode and return a source_location starting from location LOC and
637   shifting it by OFFSET columns.  This function does not support
638   virtual locations.  */
639
640source_location
641linemap_position_for_loc_and_offset (struct line_maps *set,
642				     source_location loc,
643				     unsigned int offset)
644{
645  const struct line_map * map = NULL;
646
647  /* This function does not support virtual locations yet.  */
648  if (linemap_assert_fails
649      (!linemap_location_from_macro_expansion_p (set, loc)))
650    return loc;
651
652  if (offset == 0
653      /* Adding an offset to a reserved location (like
654	 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
655	 sense.  So let's leave the location intact in that case.  */
656      || loc < RESERVED_LOCATION_COUNT)
657    return loc;
658
659  /* We find the real location and shift it.  */
660  loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
661  /* The new location (loc + offset) should be higher than the first
662     location encoded by MAP.  */
663  if (linemap_assert_fails (MAP_START_LOCATION (map) < loc + offset))
664    return loc;
665
666  /* If MAP is not the last line map of its set, then the new location
667     (loc + offset) should be less than the first location encoded by
668     the next line map of the set.  */
669  if (map != LINEMAPS_LAST_ORDINARY_MAP (set))
670    if (linemap_assert_fails (loc + offset < MAP_START_LOCATION (&map[1])))
671      return loc;
672
673  offset += SOURCE_COLUMN (map, loc);
674  if (linemap_assert_fails (offset < (1u << map->d.ordinary.column_bits)))
675    return loc;
676
677  source_location r =
678    linemap_position_for_line_and_column (map,
679					  SOURCE_LINE (map, loc),
680					  offset);
681  if (linemap_assert_fails (r <= set->highest_location)
682      || linemap_assert_fails (map == linemap_lookup (set, r)))
683    return loc;
684
685  return r;
686}
687
688/* Given a virtual source location yielded by a map (either an
689   ordinary or a macro map), returns that map.  */
690
691const struct line_map*
692linemap_lookup (struct line_maps *set, source_location line)
693{
694  if (IS_ADHOC_LOC (line))
695    line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
696  if (linemap_location_from_macro_expansion_p (set, line))
697    return linemap_macro_map_lookup (set, line);
698  return linemap_ordinary_map_lookup (set, line);
699}
700
701/* Given a source location yielded by an ordinary map, returns that
702   map.  Since the set is built chronologically, the logical lines are
703   monotonic increasing, and so the list is sorted and we can use a
704   binary search.  */
705
706static const struct line_map *
707linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
708{
709  unsigned int md, mn, mx;
710  const struct line_map *cached, *result;
711
712  if (IS_ADHOC_LOC (line))
713    line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
714
715  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
716    return NULL;
717
718  mn = LINEMAPS_ORDINARY_CACHE (set);
719  mx = LINEMAPS_ORDINARY_USED (set);
720
721  cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
722  /* We should get a segfault if no line_maps have been added yet.  */
723  if (line >= MAP_START_LOCATION (cached))
724    {
725      if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
726	return cached;
727    }
728  else
729    {
730      mx = mn;
731      mn = 0;
732    }
733
734  while (mx - mn > 1)
735    {
736      md = (mn + mx) / 2;
737      if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
738	mx = md;
739      else
740	mn = md;
741    }
742
743  LINEMAPS_ORDINARY_CACHE (set) = mn;
744  result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
745  linemap_assert (line >= MAP_START_LOCATION (result));
746  return result;
747}
748
749/* Given a source location yielded by a macro map, returns that map.
750   Since the set is built chronologically, the logical lines are
751   monotonic decreasing, and so the list is sorted and we can use a
752   binary search.  */
753
754static const struct line_map*
755linemap_macro_map_lookup (struct line_maps *set, source_location line)
756{
757  unsigned int md, mn, mx;
758  const struct line_map *cached, *result;
759
760  if (IS_ADHOC_LOC (line))
761    line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
762
763  linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
764
765  if (set ==  NULL)
766    return NULL;
767
768  mn = LINEMAPS_MACRO_CACHE (set);
769  mx = LINEMAPS_MACRO_USED (set);
770  cached = LINEMAPS_MACRO_MAP_AT (set, mn);
771
772  if (line >= MAP_START_LOCATION (cached))
773    {
774      if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
775	return cached;
776      mx = mn - 1;
777      mn = 0;
778    }
779
780  while (mn < mx)
781    {
782      md = (mx + mn) / 2;
783      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
784	mn = md + 1;
785      else
786	mx = md;
787    }
788
789  LINEMAPS_MACRO_CACHE (set) = mx;
790  result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
791  linemap_assert (MAP_START_LOCATION (result) <= line);
792
793  return result;
794}
795
796/* Return TRUE if MAP encodes locations coming from a macro
797   replacement-list at macro expansion point.  */
798
799bool
800linemap_macro_expansion_map_p (const struct line_map *map)
801{
802  if (!map)
803    return false;
804  return (map->reason == LC_ENTER_MACRO);
805}
806
807/* If LOCATION is the locus of a token in a replacement-list of a
808   macro expansion return the location of the macro expansion point.
809
810   Read the comments of struct line_map and struct line_map_macro in
811   line-map.h to understand what a macro expansion point is.  */
812
813static source_location
814linemap_macro_map_loc_to_exp_point (const struct line_map *map,
815				    source_location location ATTRIBUTE_UNUSED)
816{
817  linemap_assert (linemap_macro_expansion_map_p (map)
818		  && location >= MAP_START_LOCATION (map));
819
820  /* Make sure LOCATION is correct.  */
821  linemap_assert ((location - MAP_START_LOCATION (map))
822		  <  MACRO_MAP_NUM_MACRO_TOKENS (map));
823
824  return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
825}
826
827/* LOCATION is the source location of a token that belongs to a macro
828   replacement-list as part of the macro expansion denoted by MAP.
829
830   Return the location of the token at the definition point of the
831   macro.  */
832
833static source_location
834linemap_macro_map_loc_to_def_point (const struct line_map *map,
835				    source_location location)
836{
837  unsigned token_no;
838
839  linemap_assert (linemap_macro_expansion_map_p (map)
840		  && location >= MAP_START_LOCATION (map));
841  linemap_assert (location >= RESERVED_LOCATION_COUNT);
842
843  token_no = location - MAP_START_LOCATION (map);
844  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
845
846  location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
847
848  return location;
849}
850
851/* If LOCATION is the locus of a token that is an argument of a
852   function-like macro M and appears in the expansion of M, return the
853   locus of that argument in the context of the caller of M.
854
855   In other words, this returns the xI location presented in the
856   comments of line_map_macro above.  */
857source_location
858linemap_macro_map_loc_unwind_toward_spelling (const struct line_map* map,
859					      source_location location)
860{
861  unsigned token_no;
862
863  linemap_assert (linemap_macro_expansion_map_p (map)
864		  && location >= MAP_START_LOCATION (map));
865  linemap_assert (location >= RESERVED_LOCATION_COUNT);
866
867  token_no = location - MAP_START_LOCATION (map);
868  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
869
870  location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
871
872  return location;
873}
874
875/* Return the source line number corresponding to source location
876   LOCATION.  SET is the line map set LOCATION comes from.  If
877   LOCATION is the source location of token that is part of the
878   replacement-list of a macro expansion return the line number of the
879   macro expansion point.  */
880
881int
882linemap_get_expansion_line (struct line_maps *set,
883			    source_location location)
884{
885  const struct line_map *map = NULL;
886
887  if (IS_ADHOC_LOC (location))
888    location = set->location_adhoc_data_map.data[location
889						 & MAX_SOURCE_LOCATION].locus;
890
891  if (location < RESERVED_LOCATION_COUNT)
892    return 0;
893
894  location =
895    linemap_macro_loc_to_exp_point (set, location, &map);
896
897  return SOURCE_LINE (map, location);
898}
899
900/* Return the path of the file corresponding to source code location
901   LOCATION.
902
903   If LOCATION is the source location of token that is part of the
904   replacement-list of a macro expansion return the file path of the
905   macro expansion point.
906
907   SET is the line map set LOCATION comes from.  */
908
909const char*
910linemap_get_expansion_filename (struct line_maps *set,
911				source_location location)
912{
913  const struct line_map *map = NULL;
914
915  if (IS_ADHOC_LOC (location))
916    location = set->location_adhoc_data_map.data[location
917						 & MAX_SOURCE_LOCATION].locus;
918
919  if (location < RESERVED_LOCATION_COUNT)
920    return NULL;
921
922  location =
923    linemap_macro_loc_to_exp_point (set, location, &map);
924
925  return LINEMAP_FILE (map);
926}
927
928/* Return the name of the macro associated to MACRO_MAP.  */
929
930const char*
931linemap_map_get_macro_name (const struct line_map* macro_map)
932{
933  linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
934  return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
935}
936
937/* Return a positive value if LOCATION is the locus of a token that is
938   located in a system header, O otherwise. It returns 1 if LOCATION
939   is the locus of a token that is located in a system header, and 2
940   if LOCATION is the locus of a token located in a C system header
941   that therefore needs to be extern "C" protected in C++.
942
943   Note that this function returns 1 if LOCATION belongs to a token
944   that is part of a macro replacement-list defined in a system
945   header, but expanded in a non-system file.  */
946
947int
948linemap_location_in_system_header_p (struct line_maps *set,
949				     source_location location)
950{
951  const struct line_map *map = NULL;
952
953  if (IS_ADHOC_LOC (location))
954    location = set->location_adhoc_data_map.data[location
955						 & MAX_SOURCE_LOCATION].locus;
956
957  if (location < RESERVED_LOCATION_COUNT)
958    return false;
959
960  /* Let's look at where the token for LOCATION comes from.  */
961  while (true)
962    {
963      map = linemap_lookup (set, location);
964      if (map != NULL)
965	{
966	  if (!linemap_macro_expansion_map_p (map))
967	    /* It's a normal token.  */
968	    return LINEMAP_SYSP (map);
969	  else
970	    {
971	      /* It's a token resulting from a macro expansion.  */
972	      source_location loc =
973		linemap_macro_map_loc_unwind_toward_spelling (map, location);
974	      if (loc < RESERVED_LOCATION_COUNT)
975		/* This token might come from a built-in macro.  Let's
976		   look at where that macro got expanded.  */
977		location = linemap_macro_map_loc_to_exp_point (map, location);
978	      else
979		location = loc;
980	    }
981	}
982      else
983	break;
984    }
985  return false;
986}
987
988/* Return TRUE if LOCATION is a source code location of a token coming
989   from a macro replacement-list at a macro expansion point, FALSE
990   otherwise.  */
991
992bool
993linemap_location_from_macro_expansion_p (const struct line_maps *set,
994					 source_location location)
995{
996  if (IS_ADHOC_LOC (location))
997    location = set->location_adhoc_data_map.data[location
998						 & MAX_SOURCE_LOCATION].locus;
999
1000  linemap_assert (location <= MAX_SOURCE_LOCATION
1001		  && (set->highest_location
1002		      < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
1003  if (set == NULL)
1004    return false;
1005  return (location > set->highest_location);
1006}
1007
1008/* Given two virtual locations *LOC0 and *LOC1, return the first
1009   common macro map in their macro expansion histories.  Return NULL
1010   if no common macro was found.  *LOC0 (resp. *LOC1) is set to the
1011   virtual location of the token inside the resulting macro.  */
1012
1013static const struct line_map*
1014first_map_in_common_1 (struct line_maps *set,
1015		       source_location *loc0,
1016		       source_location *loc1)
1017{
1018  source_location l0 = *loc0, l1 = *loc1;
1019  const struct line_map *map0 = linemap_lookup (set, l0),
1020    *map1 = linemap_lookup (set, l1);
1021
1022  while (linemap_macro_expansion_map_p (map0)
1023	 && linemap_macro_expansion_map_p (map1)
1024	 && (map0 != map1))
1025    {
1026      if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1027	{
1028	  l0 = linemap_macro_map_loc_to_exp_point (map0, l0);
1029	  map0 = linemap_lookup (set, l0);
1030	}
1031      else
1032	{
1033	  l1 = linemap_macro_map_loc_to_exp_point (map1, l1);
1034	  map1 = linemap_lookup (set, l1);
1035	}
1036    }
1037
1038  if (map0 == map1)
1039    {
1040      *loc0 = l0;
1041      *loc1 = l1;
1042      return map0;
1043    }
1044  return NULL;
1045}
1046
1047/* Given two virtual locations LOC0 and LOC1, return the first common
1048   macro map in their macro expansion histories.  Return NULL if no
1049   common macro was found.  *RES_LOC0 (resp. *RES_LOC1) is set to the
1050   virtual location of the token inside the resulting macro, upon
1051   return of a non-NULL result.  */
1052
1053static const struct line_map*
1054first_map_in_common (struct line_maps *set,
1055		     source_location loc0,
1056		     source_location loc1,
1057		     source_location  *res_loc0,
1058		     source_location  *res_loc1)
1059{
1060  *res_loc0 = loc0;
1061  *res_loc1 = loc1;
1062
1063  return first_map_in_common_1 (set, res_loc0, res_loc1);
1064}
1065
1066/* Return a positive value if PRE denotes the location of a token that
1067   comes before the token of POST, 0 if PRE denotes the location of
1068   the same token as the token for POST, and a negative value
1069   otherwise.  */
1070
1071int
1072linemap_compare_locations (struct line_maps *set,
1073			   source_location  pre,
1074			   source_location post)
1075{
1076  bool pre_virtual_p, post_virtual_p;
1077  source_location l0 = pre, l1 = post;
1078
1079  if (IS_ADHOC_LOC (l0))
1080    l0 = set->location_adhoc_data_map.data[l0 & MAX_SOURCE_LOCATION].locus;
1081  if (IS_ADHOC_LOC (l1))
1082    l1 = set->location_adhoc_data_map.data[l1 & MAX_SOURCE_LOCATION].locus;
1083
1084  if (l0 == l1)
1085    return 0;
1086
1087  if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1088    l0 = linemap_resolve_location (set, l0,
1089				   LRK_MACRO_EXPANSION_POINT,
1090				   NULL);
1091
1092  if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1093    l1 = linemap_resolve_location (set, l1,
1094				   LRK_MACRO_EXPANSION_POINT,
1095				   NULL);
1096
1097  if (l0 == l1
1098      && pre_virtual_p
1099      && post_virtual_p)
1100    {
1101      /* So pre and post represent two tokens that are present in a
1102	 same macro expansion.  Let's see if the token for pre was
1103	 before the token for post in that expansion.  */
1104      unsigned i0, i1;
1105      const struct line_map *map =
1106	first_map_in_common (set, pre, post, &l0, &l1);
1107
1108      if (map == NULL)
1109	/* This should not be possible.  */
1110	abort ();
1111
1112      i0 = l0 - MAP_START_LOCATION (map);
1113      i1 = l1 - MAP_START_LOCATION (map);
1114      return i1 - i0;
1115    }
1116
1117  return l1 - l0;
1118}
1119
1120/* Print an include trace, for e.g. the -H option of the preprocessor.  */
1121
1122static void
1123trace_include (const struct line_maps *set, const struct line_map *map)
1124{
1125  unsigned int i = set->depth;
1126
1127  while (--i)
1128    putc ('.', stderr);
1129
1130  fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1131}
1132
1133/* Return the spelling location of the token wherever it comes from,
1134   whether part of a macro definition or not.
1135
1136   This is a subroutine for linemap_resolve_location.  */
1137
1138static source_location
1139linemap_macro_loc_to_spelling_point (struct line_maps *set,
1140				     source_location location,
1141				     const struct line_map **original_map)
1142{
1143  struct line_map *map;
1144
1145  if (IS_ADHOC_LOC (location))
1146    location = set->location_adhoc_data_map.data[location
1147						 & MAX_SOURCE_LOCATION].locus;
1148
1149  linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1150
1151  while (true)
1152    {
1153      map = (struct line_map*) linemap_lookup (set, location);
1154      if (!linemap_macro_expansion_map_p (map))
1155	break;
1156
1157      location =
1158	linemap_macro_map_loc_unwind_toward_spelling (map, location);
1159    }
1160
1161  if (original_map)
1162    *original_map = map;
1163  return location;
1164}
1165
1166/* If LOCATION is the source location of a token that belongs to a
1167   macro replacement-list -- as part of a macro expansion -- then
1168   return the location of the token at the definition point of the
1169   macro.  Otherwise, return LOCATION.  SET is the set of maps
1170   location come from.  ORIGINAL_MAP is an output parm. If non NULL,
1171   the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1172   returned location comes from.
1173
1174   This is a subroutine of linemap_resolve_location.  */
1175
1176static source_location
1177linemap_macro_loc_to_def_point (struct line_maps *set,
1178				source_location location,
1179				const struct line_map **original_map)
1180{
1181  struct line_map *map;
1182
1183  if (IS_ADHOC_LOC (location))
1184    location = set->location_adhoc_data_map.data[location
1185						 & MAX_SOURCE_LOCATION].locus;
1186
1187  linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1188
1189  while (true)
1190    {
1191      map = (struct line_map*) linemap_lookup (set, location);
1192      if (!linemap_macro_expansion_map_p (map))
1193	break;
1194
1195      location =
1196	linemap_macro_map_loc_to_def_point (map, location);
1197    }
1198
1199  if (original_map)
1200    *original_map = map;
1201  return location;
1202}
1203
1204/* If LOCATION is the source location of a token that belongs to a
1205   macro replacement-list -- at a macro expansion point -- then return
1206   the location of the topmost expansion point of the macro.  We say
1207   topmost because if we are in the context of a nested macro
1208   expansion, the function returns the source location of the first
1209   macro expansion that triggered the nested expansions.
1210
1211   Otherwise, return LOCATION.  SET is the set of maps location come
1212   from.  ORIGINAL_MAP is an output parm. If non NULL, the function
1213   sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1214   location comes from.
1215
1216   This is a subroutine of linemap_resolve_location.  */
1217
1218static source_location
1219linemap_macro_loc_to_exp_point (struct line_maps *set,
1220				source_location location,
1221				const struct line_map **original_map)
1222{
1223  struct line_map *map;
1224
1225  if (IS_ADHOC_LOC (location))
1226    location = set->location_adhoc_data_map.data[location
1227						 & MAX_SOURCE_LOCATION].locus;
1228
1229  linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1230
1231  while (true)
1232    {
1233      map = (struct line_map*) linemap_lookup (set, location);
1234      if (!linemap_macro_expansion_map_p (map))
1235	break;
1236      location = linemap_macro_map_loc_to_exp_point (map, location);
1237    }
1238
1239  if (original_map)
1240    *original_map = map;
1241  return location;
1242}
1243
1244/* Resolve a virtual location into either a spelling location, an
1245   expansion point location or a token argument replacement point
1246   location.  Return the map that encodes the virtual location as well
1247   as the resolved location.
1248
1249   If LOC is *NOT* the location of a token resulting from the
1250   expansion of a macro, then the parameter LRK (which stands for
1251   Location Resolution Kind) is ignored and the resulting location
1252   just equals the one given in argument.
1253
1254   Now if LOC *IS* the location of a token resulting from the
1255   expansion of a macro, this is what happens.
1256
1257   * If LRK is set to LRK_MACRO_EXPANSION_POINT
1258   -------------------------------
1259
1260   The virtual location is resolved to the first macro expansion point
1261   that led to this macro expansion.
1262
1263   * If LRK is set to LRK_SPELLING_LOCATION
1264   -------------------------------------
1265
1266   The virtual location is resolved to the locus where the token has
1267   been spelled in the source.   This can follow through all the macro
1268   expansions that led to the token.
1269
1270   * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1271   --------------------------------------
1272
1273   The virtual location is resolved to the locus of the token in the
1274   context of the macro definition.
1275
1276   If LOC is the locus of a token that is an argument of a
1277   function-like macro [replacing a parameter in the replacement list
1278   of the macro] the virtual location is resolved to the locus of the
1279   parameter that is replaced, in the context of the definition of the
1280   macro.
1281
1282   If LOC is the locus of a token that is not an argument of a
1283   function-like macro, then the function behaves as if LRK was set to
1284   LRK_SPELLING_LOCATION.
1285
1286   If MAP is not NULL, *MAP is set to the map encoding the
1287   returned location.  Note that if the returned location wasn't originally
1288   encoded by a map, then *MAP is set to NULL.  This can happen if LOC
1289   resolves to a location reserved for the client code, like
1290   UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC.  */
1291
1292source_location
1293linemap_resolve_location (struct line_maps *set,
1294			  source_location loc,
1295			  enum location_resolution_kind lrk,
1296			  const struct line_map **map)
1297{
1298  if (IS_ADHOC_LOC (loc))
1299    loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1300
1301  if (loc < RESERVED_LOCATION_COUNT)
1302    {
1303      /* A reserved location wasn't encoded in a map.  Let's return a
1304	 NULL map here, just like what linemap_ordinary_map_lookup
1305	 does.  */
1306      if (map)
1307	*map = NULL;
1308      return loc;
1309    }
1310
1311  switch (lrk)
1312    {
1313    case LRK_MACRO_EXPANSION_POINT:
1314      loc = linemap_macro_loc_to_exp_point (set, loc, map);
1315      break;
1316    case LRK_SPELLING_LOCATION:
1317      loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1318      break;
1319    case LRK_MACRO_DEFINITION_LOCATION:
1320      loc = linemap_macro_loc_to_def_point (set, loc, map);
1321      break;
1322    default:
1323      abort ();
1324    }
1325  return loc;
1326}
1327
1328/*
1329   Suppose that LOC is the virtual location of a token T coming from
1330   the expansion of a macro M.  This function then steps up to get the
1331   location L of the point where M got expanded.  If L is a spelling
1332   location inside a macro expansion M', then this function returns
1333   the locus of the point where M' was expanded.  Said otherwise, this
1334   function returns the location of T in the context that triggered
1335   the expansion of M.
1336
1337   *LOC_MAP must be set to the map of LOC.  This function then sets it
1338   to the map of the returned location.  */
1339
1340source_location
1341linemap_unwind_toward_expansion (struct line_maps *set,
1342				 source_location loc,
1343				 const struct line_map **map)
1344{
1345  source_location resolved_location;
1346  const struct line_map *resolved_map;
1347
1348  if (IS_ADHOC_LOC (loc))
1349    loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1350
1351  resolved_location =
1352    linemap_macro_map_loc_unwind_toward_spelling (*map, loc);
1353  resolved_map = linemap_lookup (set, resolved_location);
1354
1355  if (!linemap_macro_expansion_map_p (resolved_map))
1356    {
1357      resolved_location = linemap_macro_map_loc_to_exp_point (*map, loc);
1358      resolved_map = linemap_lookup (set, resolved_location);
1359    }
1360
1361  *map = resolved_map;
1362  return resolved_location;
1363}
1364
1365/* If LOC is the virtual location of a token coming from the expansion
1366   of a macro M and if its spelling location is reserved (e.g, a
1367   location for a built-in token), then this function unwinds (using
1368   linemap_unwind_toward_expansion) the location until a location that
1369   is not reserved and is not in a system header is reached.  In other
1370   words, this unwinds the reserved location until a location that is
1371   in real source code is reached.
1372
1373   Otherwise, if the spelling location for LOC is not reserved or if
1374   LOC doesn't come from the expansion of a macro, the function
1375   returns LOC as is and *MAP is not touched.
1376
1377   *MAP is set to the map of the returned location if the later is
1378   different from LOC.  */
1379source_location
1380linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1381					  source_location loc,
1382					  const struct line_map **map)
1383{
1384  source_location resolved_loc;
1385  const struct line_map *map0 = NULL, *map1 = NULL;
1386
1387  if (IS_ADHOC_LOC (loc))
1388    loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1389
1390  map0 = linemap_lookup (set, loc);
1391  if (!linemap_macro_expansion_map_p (map0))
1392    return loc;
1393
1394  resolved_loc = linemap_resolve_location (set, loc,
1395					   LRK_SPELLING_LOCATION,
1396					   &map1);
1397
1398  if (resolved_loc >= RESERVED_LOCATION_COUNT
1399      && !LINEMAP_SYSP (map1))
1400    return loc;
1401
1402  while (linemap_macro_expansion_map_p (map0)
1403	 && (resolved_loc < RESERVED_LOCATION_COUNT
1404	     || LINEMAP_SYSP (map1)))
1405    {
1406      loc = linemap_unwind_toward_expansion (set, loc, &map0);
1407      resolved_loc = linemap_resolve_location (set, loc,
1408					       LRK_SPELLING_LOCATION,
1409					       &map1);
1410    }
1411
1412  if (map != NULL)
1413    *map = map0;
1414  return loc;
1415}
1416
1417/* Expand source code location LOC and return a user readable source
1418   code location.  LOC must be a spelling (non-virtual) location.  If
1419   it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1420   location is returned.  */
1421
1422expanded_location
1423linemap_expand_location (struct line_maps *set,
1424			 const struct line_map *map,
1425			 source_location loc)
1426
1427{
1428  expanded_location xloc;
1429
1430  memset (&xloc, 0, sizeof (xloc));
1431  if (IS_ADHOC_LOC (loc))
1432    {
1433      loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1434      xloc.data
1435	= set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
1436    }
1437
1438  if (loc < RESERVED_LOCATION_COUNT)
1439    /* The location for this token wasn't generated from a line map.
1440       It was probably a location for a builtin token, chosen by some
1441       client code.  Let's not try to expand the location in that
1442       case.  */;
1443  else if (map == NULL)
1444    /* We shouldn't be getting a NULL map with a location that is not
1445       reserved by the client code.  */
1446    abort ();
1447  else
1448    {
1449      /* MAP must be an ordinary map and LOC must be non-virtual,
1450	 encoded into this map, obviously; the accessors used on MAP
1451	 below ensure it is ordinary.  Let's just assert the
1452	 non-virtualness of LOC here.  */
1453      if (linemap_location_from_macro_expansion_p (set, loc))
1454	abort ();
1455
1456      xloc.file = LINEMAP_FILE (map);
1457      xloc.line = SOURCE_LINE (map, loc);
1458      xloc.column = SOURCE_COLUMN (map, loc);
1459      xloc.sysp = LINEMAP_SYSP (map) != 0;
1460    }
1461
1462  return xloc;
1463}
1464
1465
1466/* Dump line map at index IX in line table SET to STREAM.  If STREAM
1467   is NULL, use stderr.  IS_MACRO is true if the caller wants to
1468   dump a macro map, false otherwise.  */
1469
1470void
1471linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1472{
1473  const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1474      = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1475	  "LC_ENTER_MACRO" };
1476  const char *reason;
1477  struct line_map *map;
1478
1479  if (stream == NULL)
1480    stream = stderr;
1481
1482  if (!is_macro)
1483    map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1484  else
1485    map = LINEMAPS_MACRO_MAP_AT (set, ix);
1486
1487  reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1488
1489  fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1490	   ix, (void *) map, map->start_location, reason,
1491	   (!is_macro && ORDINARY_MAP_IN_SYSTEM_HEADER_P (map)) ? "yes" : "no");
1492  if (!is_macro)
1493    {
1494      unsigned includer_ix;
1495      struct line_map *includer_map;
1496
1497      includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (map);
1498      includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1499		     ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1500		     : NULL;
1501
1502      fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (map),
1503	       ORDINARY_MAP_STARTING_LINE_NUMBER (map));
1504      fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1505	       includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1506    }
1507  else
1508    fprintf (stream, "Macro: %s (%u tokens)\n",
1509	     linemap_map_get_macro_name (map),
1510	     MACRO_MAP_NUM_MACRO_TOKENS (map));
1511
1512  fprintf (stream, "\n");
1513}
1514
1515
1516/* Dump debugging information about source location LOC into the file
1517   stream STREAM. SET is the line map set LOC comes from.  */
1518
1519void
1520linemap_dump_location (struct line_maps *set,
1521		       source_location loc,
1522		       FILE *stream)
1523{
1524  const struct line_map *map;
1525  source_location location;
1526  const char *path = "", *from = "";
1527  int l = -1, c = -1, s = -1, e = -1;
1528
1529  if (IS_ADHOC_LOC (loc))
1530    loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1531
1532  if (loc == 0)
1533    return;
1534
1535  location =
1536    linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1537
1538  if (map == NULL)
1539    /* Only reserved locations can be tolerated in this case.  */
1540    linemap_assert (location < RESERVED_LOCATION_COUNT);
1541  else
1542    {
1543      path = LINEMAP_FILE (map);
1544      l = SOURCE_LINE (map, location);
1545      c = SOURCE_COLUMN (map, location);
1546      s = LINEMAP_SYSP (map) != 0;
1547      e = location != loc;
1548      if (e)
1549	from = "N/A";
1550      else
1551	from = (INCLUDED_FROM (set, map))
1552	  ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1553	  : "<NULL>";
1554    }
1555
1556  /* P: path, L: line, C: column, S: in-system-header, M: map address,
1557     E: macro expansion?, LOC: original location, R: resolved location   */
1558  fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1559	   path, from, l, c, s, (void*)map, e, loc, location);
1560}
1561
1562/* Return the highest location emitted for a given file for which
1563   there is a line map in SET.  FILE_NAME is the file name to
1564   consider.  If the function returns TRUE, *LOC is set to the highest
1565   location emitted for that file.  */
1566
1567bool
1568linemap_get_file_highest_location (struct line_maps *set,
1569				   const char *file_name,
1570				   source_location *loc)
1571{
1572  /* If the set is empty or no ordinary map has been created then
1573     there is no file to look for ...  */
1574  if (set == NULL || set->info_ordinary.used == 0)
1575    return false;
1576
1577  /* Now look for the last ordinary map created for FILE_NAME.  */
1578  int i;
1579  for (i = set->info_ordinary.used - 1; i >= 0; --i)
1580    {
1581      const char *fname = set->info_ordinary.maps[i].d.ordinary.to_file;
1582      if (fname && !filename_cmp (fname, file_name))
1583	break;
1584    }
1585
1586  if (i < 0)
1587    return false;
1588
1589  /* The highest location for a given map is either the starting
1590     location of the next map minus one, or -- if the map is the
1591     latest one -- the highest location of the set.  */
1592  source_location result;
1593  if (i == (int) set->info_ordinary.used - 1)
1594    result = set->highest_location;
1595  else
1596    result = set->info_ordinary.maps[i + 1].start_location - 1;
1597
1598  *loc = result;
1599  return true;
1600}
1601
1602/* Compute and return statistics about the memory consumption of some
1603   parts of the line table SET.  */
1604
1605void
1606linemap_get_statistics (struct line_maps *set,
1607			struct linemap_stats *s)
1608{
1609  long ordinary_maps_allocated_size, ordinary_maps_used_size,
1610    macro_maps_allocated_size, macro_maps_used_size,
1611    macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1612
1613  struct line_map *cur_map;
1614
1615  ordinary_maps_allocated_size =
1616    LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map);
1617
1618  ordinary_maps_used_size =
1619    LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map);
1620
1621  macro_maps_allocated_size =
1622    LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map);
1623
1624  for (cur_map = LINEMAPS_MACRO_MAPS (set);
1625       cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1626       ++cur_map)
1627    {
1628      unsigned i;
1629
1630      linemap_assert (linemap_macro_expansion_map_p (cur_map));
1631
1632      macro_maps_locations_size +=
1633	2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1634
1635      for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1636	{
1637	  if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1638	      MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1639	    duplicated_macro_maps_locations_size +=
1640	      sizeof (source_location);
1641	}
1642    }
1643
1644  macro_maps_used_size =
1645    LINEMAPS_MACRO_USED (set) * sizeof (struct line_map);
1646
1647  s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1648  s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1649  s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1650  s->ordinary_maps_used_size = ordinary_maps_used_size;
1651  s->num_expanded_macros = num_expanded_macros_counter;
1652  s->num_macro_tokens = num_macro_tokens_counter;
1653  s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1654  s->macro_maps_allocated_size = macro_maps_allocated_size;
1655  s->macro_maps_locations_size = macro_maps_locations_size;
1656  s->macro_maps_used_size = macro_maps_used_size;
1657  s->duplicated_macro_maps_locations_size =
1658    duplicated_macro_maps_locations_size;
1659}
1660
1661
1662/* Dump line table SET to STREAM.  If STREAM is NULL, stderr is used.
1663   NUM_ORDINARY specifies how many ordinary maps to dump.  NUM_MACRO
1664   specifies how many macro maps to dump.  */
1665
1666void
1667line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1668		 unsigned int num_macro)
1669{
1670  unsigned int i;
1671
1672  if (set == NULL)
1673    return;
1674
1675  if (stream == NULL)
1676    stream = stderr;
1677
1678  fprintf (stream, "# of ordinary maps:  %d\n", LINEMAPS_ORDINARY_USED (set));
1679  fprintf (stream, "# of macro maps:     %d\n", LINEMAPS_MACRO_USED (set));
1680  fprintf (stream, "Include stack depth: %d\n", set->depth);
1681  fprintf (stream, "Highest location:    %u\n", set->highest_location);
1682
1683  if (num_ordinary)
1684    {
1685      fprintf (stream, "\nOrdinary line maps\n");
1686      for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1687	linemap_dump (stream, set, i, false);
1688      fprintf (stream, "\n");
1689    }
1690
1691  if (num_macro)
1692    {
1693      fprintf (stream, "\nMacro line maps\n");
1694      for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1695	linemap_dump (stream, set, i, true);
1696      fprintf (stream, "\n");
1697    }
1698}
1699