1 /* Map (unsigned int) keys to (source file, line, column) triples.
2    Copyright (C) 2001-2019 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 #ifndef LIBCPP_LINE_MAP_H
23 #define LIBCPP_LINE_MAP_H
24 
25 #ifndef GTY
26 #define GTY(x) /* nothing */
27 #endif
28 
29 /* Both gcc and emacs number source *lines* starting at 1, but
30    they have differing conventions for *columns*.
31 
32    GCC uses a 1-based convention for source columns,
33    whereas Emacs's M-x column-number-mode uses a 0-based convention.
34 
35    For example, an error in the initial, left-hand
36    column of source line 3 is reported by GCC as:
37 
38       some-file.c:3:1: error: ...etc...
39 
40    On navigating to the location of that error in Emacs
41    (e.g. via "next-error"),
42    the locus is reported in the Mode Line
43    (assuming M-x column-number-mode) as:
44 
45      some-file.c   10%   (3, 0)
46 
47    i.e. "3:1:" in GCC corresponds to "(3, 0)" in Emacs.  */
48 
49 /* The type of line numbers.  */
50 typedef unsigned int linenum_type;
51 
52 /* A type for doing arithmetic on line numbers.  */
53 typedef long long linenum_arith_t;
54 
55 /* A function for for use by qsort for comparing line numbers.  */
56 
compare(linenum_type lhs,linenum_type rhs)57 inline int compare (linenum_type lhs, linenum_type rhs)
58 {
59   /* Avoid truncation issues by using linenum_arith_t for the comparison,
60      and only consider the sign of the result.  */
61   linenum_arith_t diff = (linenum_arith_t)lhs - (linenum_arith_t)rhs;
62   if (diff)
63     return diff > 0 ? 1 : -1;
64   return 0;
65 }
66 
67 /* Reason for creating a new line map with linemap_add.  */
68 enum lc_reason
69 {
70   LC_ENTER = 0,		/* Begin #include.  */
71   LC_LEAVE,		/* Return to including file.  */
72   LC_RENAME,		/* Other reason for name change.  */
73   LC_RENAME_VERBATIM,	/* Likewise, but "" != stdin.  */
74   LC_ENTER_MACRO,	/* Begin macro expansion.  */
75   /* FIXME: add support for stringize and paste.  */
76   LC_HWM /* High Water Mark.  */
77 };
78 
79 /* The typedef "location_t" is a key within the location database,
80    identifying a source location or macro expansion, along with range
81    information, and (optionally) a pointer for use by gcc.
82 
83    This key only has meaning in relation to a line_maps instance.  Within
84    gcc there is a single line_maps instance: "line_table", declared in
85    gcc/input.h and defined in gcc/input.c.
86 
87    The values of the keys are intended to be internal to libcpp,
88    but for ease-of-understanding the implementation, they are currently
89    assigned as follows:
90 
91   Actual     | Value                         | Meaning
92   -----------+-------------------------------+-------------------------------
93   0x00000000 | UNKNOWN_LOCATION (gcc/input.h)| Unknown/invalid location.
94   -----------+-------------------------------+-------------------------------
95   0x00000001 | BUILTINS_LOCATION             | The location for declarations
96              |   (gcc/input.h)               | in "<built-in>"
97   -----------+-------------------------------+-------------------------------
98   0x00000002 | RESERVED_LOCATION_COUNT       | The first location to be
99              | (also                         | handed out, and the
100              |  ordmap[0]->start_location)   | first line in ordmap 0
101   -----------+-------------------------------+-------------------------------
102              | ordmap[1]->start_location     | First line in ordmap 1
103              | ordmap[1]->start_location+32  | First column in that line
104              |   (assuming range_bits == 5)  |
105              | ordmap[1]->start_location+64  | 2nd column in that line
106              | ordmap[1]->start_location+4096| Second line in ordmap 1
107              |   (assuming column_bits == 12)
108              |
109              |   Subsequent lines are offset by (1 << column_bits),
110              |   e.g. 4096 for 12 bits, with a column value of 0 representing
111              |   "the whole line".
112              |
113              |   Within a line, the low "range_bits" (typically 5) are used for
114              |   storing short ranges, so that there's an offset of
115              |     (1 << range_bits) between individual columns within a line,
116              |   typically 32.
117              |   The low range_bits store the offset of the end point from the
118              |   start point, and the start point is found by masking away
119              |   the range bits.
120              |
121              |   For example:
122              |      ordmap[1]->start_location+64    "2nd column in that line"
123              |   above means a caret at that location, with a range
124              |   starting and finishing at the same place (the range bits
125              |   are 0), a range of length 1.
126              |
127              |   By contrast:
128              |      ordmap[1]->start_location+68
129              |   has range bits 0x4, meaning a caret with a range starting at
130              |   that location, but with endpoint 4 columns further on: a range
131              |   of length 5.
132              |
133              |   Ranges that have caret != start, or have an endpoint too
134              |   far away to fit in range_bits are instead stored as ad-hoc
135              |   locations.  Hence for range_bits == 5 we can compactly store
136              |   tokens of length <= 32 without needing to use the ad-hoc
137              |   table.
138              |
139              |   This packing scheme means we effectively have
140              |     (column_bits - range_bits)
141              |   of bits for the columns, typically (12 - 5) = 7, for 128
142              |   columns; longer line widths are accomodated by starting a
143              |   new ordmap with a higher column_bits.
144              |
145              | ordmap[2]->start_location-1   | Final location in ordmap 1
146   -----------+-------------------------------+-------------------------------
147              | ordmap[2]->start_location     | First line in ordmap 2
148              | ordmap[3]->start_location-1   | Final location in ordmap 2
149   -----------+-------------------------------+-------------------------------
150              |                               | (etc)
151   -----------+-------------------------------+-------------------------------
152              | ordmap[n-1]->start_location   | First line in final ord map
153              |                               | (etc)
154              | set->highest_location - 1     | Final location in that ordmap
155   -----------+-------------------------------+-------------------------------
156              | set->highest_location         | Location of the where the next
157              |                               | ordinary linemap would start
158   -----------+-------------------------------+-------------------------------
159              |                               |
160              |                  VVVVVVVVVVVVVVVVVVVVVVVVVVV
161              |                  Ordinary maps grow this way
162              |
163              |                    (unallocated integers)
164              |
165   0x60000000 | LINE_MAP_MAX_LOCATION_WITH_COLS
166              |   Beyond this point, ordinary linemaps have 0 bits per column:
167              |   each increment of the value corresponds to a new source line.
168              |
169   0x70000000 | LINE_MAP_MAX_LOCATION
170              |   Beyond the point, we give up on ordinary maps; attempts to
171              |   create locations in them lead to UNKNOWN_LOCATION (0).
172              |
173              |                    (unallocated integers)
174              |
175              |                   Macro maps grow this way
176              |                   ^^^^^^^^^^^^^^^^^^^^^^^^
177              |                               |
178   -----------+-------------------------------+-------------------------------
179              | LINEMAPS_MACRO_LOWEST_LOCATION| Locations within macro maps
180              | macromap[m-1]->start_location | Start of last macro map
181              |                               |
182   -----------+-------------------------------+-------------------------------
183              | macromap[m-2]->start_location | Start of penultimate macro map
184   -----------+-------------------------------+-------------------------------
185              | macromap[1]->start_location   | Start of macro map 1
186   -----------+-------------------------------+-------------------------------
187              | macromap[0]->start_location   | Start of macro map 0
188   0x7fffffff | MAX_LOCATION_T                | Also used as a mask for
189              |                               | accessing the ad-hoc data table
190   -----------+-------------------------------+-------------------------------
191   0x80000000 | Start of ad-hoc values; the lower 31 bits are used as an index
192   ...        | into the line_table->location_adhoc_data_map.data array.
193   0xffffffff | UINT_MAX                      |
194   -----------+-------------------------------+-------------------------------
195 
196    Examples of location encoding.
197 
198    Packed ranges
199    =============
200 
201    Consider encoding the location of a token "foo", seen underlined here
202    on line 523, within an ordinary line_map that starts at line 500:
203 
204                  11111111112
205         12345678901234567890
206      522
207      523   return foo + bar;
208                   ^~~
209      524
210 
211    The location's caret and start are both at line 523, column 11; the
212    location's finish is on the same line, at column 13 (an offset of 2
213    columns, for length 3).
214 
215    Line 523 is offset 23 from the starting line of the ordinary line_map.
216 
217    caret == start, and the offset of the finish fits within 5 bits, so
218    this can be stored as a packed range.
219 
220    This is encoded as:
221       ordmap->start
222          + (line_offset << ordmap->m_column_and_range_bits)
223          + (column << ordmap->m_range_bits)
224          + (range_offset);
225    i.e. (for line offset 23, column 11, range offset 2):
226       ordmap->start
227          + (23 << 12)
228          + (11 << 5)
229          + 2;
230    i.e.:
231       ordmap->start + 0x17162
232    assuming that the line_map uses the default of 7 bits for columns and
233    5 bits for packed range (giving 12 bits for m_column_and_range_bits).
234 
235 
236    "Pure" locations
237    ================
238 
239    These are a special case of the above, where
240       caret == start == finish
241    They are stored as packed ranges with offset == 0.
242    For example, the location of the "f" of "foo" could be stored
243    as above, but with range offset 0, giving:
244       ordmap->start
245          + (23 << 12)
246          + (11 << 5)
247          + 0;
248    i.e.:
249       ordmap->start + 0x17160
250 
251 
252    Unoptimized ranges
253    ==================
254 
255    Consider encoding the location of the binary expression
256    below:
257 
258                  11111111112
259         12345678901234567890
260      522
261      523   return foo + bar;
262                   ~~~~^~~~~
263      524
264 
265    The location's caret is at the "+", line 523 column 15, but starts
266    earlier, at the "f" of "foo" at column 11.  The finish is at the "r"
267    of "bar" at column 19.
268 
269    This can't be stored as a packed range since start != caret.
270    Hence it is stored as an ad-hoc location e.g. 0x80000003.
271 
272    Stripping off the top bit gives us an index into the ad-hoc
273    lookaside table:
274 
275      line_table->location_adhoc_data_map.data[0x3]
276 
277    from which the caret, start and finish can be looked up,
278    encoded as "pure" locations:
279 
280      start  == ordmap->start + (23 << 12) + (11 << 5)
281             == ordmap->start + 0x17160  (as above; the "f" of "foo")
282 
283      caret  == ordmap->start + (23 << 12) + (15 << 5)
284             == ordmap->start + 0x171e0
285 
286      finish == ordmap->start + (23 << 12) + (19 << 5)
287             == ordmap->start + 0x17260
288 
289    To further see how location_t works in practice, see the
290    worked example in libcpp/location-example.txt.  */
291 typedef unsigned int location_t;
292 
293 /* Do not track column numbers higher than this one.  As a result, the
294    range of column_bits is [12, 18] (or 0 if column numbers are
295    disabled).  */
296 const unsigned int LINE_MAP_MAX_COLUMN_NUMBER = (1U << 12);
297 
298 /* Do not pack ranges if locations get higher than this.
299    If you change this, update:
300      gcc.dg/plugin/location-overflow-test-*.c.  */
301 const location_t LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES = 0x50000000;
302 
303 /* Do not track column numbers if locations get higher than this.
304    If you change this, update:
305      gcc.dg/plugin/location-overflow-test-*.c.  */
306 const location_t LINE_MAP_MAX_LOCATION_WITH_COLS = 0x60000000;
307 
308 /* Highest possible source location encoded within an ordinary map.  */
309 const location_t LINE_MAP_MAX_LOCATION = 0x70000000;
310 
311 /* A range of source locations.
312 
313    Ranges are closed:
314    m_start is the first location within the range,
315    m_finish is the last location within the range.
316 
317    We may need a more compact way to store these, but for now,
318    let's do it the simple way, as a pair.  */
319 struct GTY(()) source_range
320 {
321   location_t m_start;
322   location_t m_finish;
323 
324   /* We avoid using constructors, since various structs that
325      don't yet have constructors will embed instances of
326      source_range.  */
327 
328   /* Make a source_range from a location_t.  */
from_locationsource_range329   static source_range from_location (location_t loc)
330   {
331     source_range result;
332     result.m_start = loc;
333     result.m_finish = loc;
334     return result;
335   }
336 
337   /* Make a source_range from a pair of location_t.  */
from_locationssource_range338   static source_range from_locations (location_t start,
339 				      location_t finish)
340   {
341     source_range result;
342     result.m_start = start;
343     result.m_finish = finish;
344     return result;
345   }
346 };
347 
348 /* Memory allocation function typedef.  Works like xrealloc.  */
349 typedef void *(*line_map_realloc) (void *, size_t);
350 
351 /* Memory allocator function that returns the actual allocated size,
352    for a given requested allocation.  */
353 typedef size_t (*line_map_round_alloc_size_func) (size_t);
354 
355 /* A line_map encodes a sequence of locations.
356    There are two kinds of maps. Ordinary maps and macro expansion
357    maps, a.k.a macro maps.
358 
359    A macro map encodes source locations of tokens that are part of a
360    macro replacement-list, at a macro expansion point. E.g, in:
361 
362             #define PLUS(A,B) A + B
363 
364    No macro map is going to be created there, because we are not at a
365    macro expansion point. We are at a macro /definition/ point. So the
366    locations of the tokens of the macro replacement-list (i.e, A + B)
367    will be locations in an ordinary map, not a macro map.
368 
369    On the other hand, if we later do:
370 
371         int a = PLUS (1,2);
372 
373    The invocation of PLUS here is a macro expansion. So we are at a
374    macro expansion point. The preprocessor expands PLUS (1,2) and
375    replaces it with the tokens of its replacement-list: 1 + 2. A macro
376    map is going to be created to hold (or rather to map, haha ...) the
377    locations of the tokens 1, + and 2. The macro map also records the
378    location of the expansion point of PLUS. That location is mapped in
379    the map that is active right before the location of the invocation
380    of PLUS.  */
381 
382 /* This contains GTY mark-up to support precompiled headers.
383    line_map is an abstract class, only derived objects exist.  */
384 struct GTY((tag ("0"), desc ("MAP_ORDINARY_P (&%h) ? 1 : 2"))) line_map {
385   location_t start_location;
386 
387   /* Size and alignment is (usually) 4 bytes.  */
388 };
389 
390 /* An ordinary line map encodes physical source locations. Those
391    physical source locations are called "spelling locations".
392 
393    Physical source file TO_FILE at line TO_LINE at column 0 is represented
394    by the logical START_LOCATION.  TO_LINE+L at column C is represented by
395    START_LOCATION+(L*(1<<m_column_and_range_bits))+(C*1<<m_range_bits), as
396    long as C<(1<<effective range bits), and the result_location is less than
397    the next line_map's start_location.
398    (The top line is line 1 and the leftmost column is column 1; line/column 0
399    means "entire file/line" or "unknown line/column" or "not applicable".)
400 
401    The highest possible source location is MAX_LOCATION_T.  */
402 struct GTY((tag ("1"))) line_map_ordinary : public line_map {
403   /* Base class is 4 bytes.  */
404 
405   /* 4 bytes of integers, each 1 byte for easy extraction/insertion.  */
406 
407   /* The reason for creation of this line map.  */
408   ENUM_BITFIELD (lc_reason) reason : 8;
409 
410   /* SYSP is one for a system header, two for a C system header file
411      that therefore needs to be extern "C" protected in C++, and zero
412      otherwise.  This field isn't really needed now that it's in
413      cpp_buffer.  */
414   unsigned char sysp;
415 
416   /* Number of the low-order location_t bits used for column numbers
417      and ranges.  */
418   unsigned int m_column_and_range_bits : 8;
419 
420   /* Number of the low-order "column" bits used for storing short ranges
421      inline, rather than in the ad-hoc table.
422      MSB                                                                 LSB
423      31                                                                    0
424      +-------------------------+-------------------------------------------+
425      |                         |<---map->column_and_range_bits (e.g. 12)-->|
426      +-------------------------+-----------------------+-------------------+
427      |                         | column_and_range_bits | map->range_bits   |
428      |                         |   - range_bits        |                   |
429      +-------------------------+-----------------------+-------------------+
430      | row bits                | effective column bits | short range bits  |
431      |                         |    (e.g. 7)           |   (e.g. 5)        |
432      +-------------------------+-----------------------+-------------------+ */
433   unsigned int m_range_bits : 8;
434 
435   /* Pointer alignment boundary on both 32 and 64-bit systems.  */
436 
437   const char *to_file;
438   linenum_type to_line;
439 
440   /* Location from whence this line map was included.  For regular
441      #includes, this location will be the last location of a map.  For
442      outermost file, this is 0.  */
443   location_t included_from;
444 
445   /* Size is 20 or 24 bytes, no padding  */
446 };
447 
448 /* This is the highest possible source location encoded within an
449    ordinary or macro map.  */
450 const location_t MAX_LOCATION_T = 0x7FFFFFFF;
451 
452 struct cpp_hashnode;
453 
454 /* A macro line map encodes location of tokens coming from a macro
455    expansion.
456 
457    The offset from START_LOCATION is used to index into
458    MACRO_LOCATIONS; this holds the original location of the token.  */
459 struct GTY((tag ("2"))) line_map_macro : public line_map {
460   /* Base is 4 bytes.  */
461 
462   /* The number of tokens inside the replacement-list of MACRO.  */
463   unsigned int n_tokens;
464 
465   /* Pointer alignment boundary.  */
466 
467   /* The cpp macro whose expansion gave birth to this macro map.  */
468   struct cpp_hashnode *
469     GTY ((nested_ptr (union tree_node,
470 		      "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
471 		      "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
472     macro;
473 
474   /* This array of location is actually an array of pairs of
475      locations. The elements inside it thus look like:
476 
477            x0,y0, x1,y1, x2,y2, ...., xn,yn.
478 
479      where n == n_tokens;
480 
481      Remember that these xI,yI are collected when libcpp is about to
482      expand a given macro.
483 
484      yI is the location in the macro definition, either of the token
485      itself or of a macro parameter that it replaces.
486 
487      Imagine this:
488 
489 	#define PLUS(A, B) A + B  <--- #1
490 
491 	int a = PLUS (1,2); <--- #2
492 
493      There is a macro map for the expansion of PLUS in #2.  PLUS is
494      expanded into its expansion-list.  The expansion-list is the
495      replacement-list of PLUS where the macro parameters are replaced
496      with their arguments.  So the replacement-list of PLUS is made of
497      the tokens:
498 
499         A, +, B
500 
501      and the expansion-list is made of the tokens:
502 
503         1, +, 2
504 
505      Let's consider the case of token "+".  Its y1 [yI for I == 1] is
506      its spelling location in #1.
507 
508      y0 (thus for token "1") is the spelling location of A in #1.
509 
510      And y2 (of token "2") is the spelling location of B in #1.
511 
512      When the token is /not/ an argument for a macro, xI is the same
513      location as yI.  Otherwise, xI is the location of the token
514      outside this macro expansion.  If this macro was expanded from
515      another macro expansion, xI is a virtual location representing
516      the token in that macro expansion; otherwise, it is the spelling
517      location of the token.
518 
519      Note that a virtual location is a location returned by
520      linemap_add_macro_token.  It encodes the relevant locations (x,y
521      pairs) of that token across the macro expansions from which it
522      (the token) might come from.
523 
524      In the example above x1 (for token "+") is going to be the same
525      as y1.  x0 is the spelling location for the argument token "1",
526      and x2 is the spelling location for the argument token "2".  */
527   location_t * GTY((atomic)) macro_locations;
528 
529   /* This is the location of the expansion point of the current macro
530      map.  It's the location of the macro name.  That location is held
531      by the map that was current right before the current one. It
532      could have been either a macro or an ordinary map, depending on
533      if we are in a nested expansion context not.  */
534   location_t expansion;
535 
536   /* Size is 20 or 32 (4 bytes padding on 64-bit).  */
537 };
538 
539 #if CHECKING_P && (GCC_VERSION >= 2007)
540 
541 /* Assertion macro to be used in line-map code.  */
542 #define linemap_assert(EXPR)                  \
543   do {                                                \
544     if (! (EXPR))                             \
545       abort ();                                       \
546   } while (0)
547 
548 /* Assert that becomes a conditional expression when checking is disabled at
549    compilation time.  Use this for conditions that should not happen but if
550    they happen, it is better to handle them gracefully rather than crash
551    randomly later.
552    Usage:
553 
554    if (linemap_assert_fails(EXPR)) handle_error(); */
555 #define linemap_assert_fails(EXPR) __extension__ \
556   ({linemap_assert (EXPR); false;})
557 
558 #else
559 /* Include EXPR, so that unused variable warnings do not occur.  */
560 #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
561 #define linemap_assert_fails(EXPR) (! (EXPR))
562 #endif
563 
564 /* Get whether location LOC is an ordinary location.  */
565 
566 inline bool
IS_ORDINARY_LOC(location_t loc)567 IS_ORDINARY_LOC (location_t loc)
568 {
569   return loc < LINE_MAP_MAX_LOCATION;
570 }
571 
572 /* Get whether location LOC is an ad-hoc location.  */
573 
574 inline bool
IS_ADHOC_LOC(location_t loc)575 IS_ADHOC_LOC (location_t loc)
576 {
577   return loc > MAX_LOCATION_T;
578 }
579 
580 /* Categorize line map kinds.  */
581 
582 inline bool
MAP_ORDINARY_P(const line_map * map)583 MAP_ORDINARY_P (const line_map *map)
584 {
585   return IS_ORDINARY_LOC (map->start_location);
586 }
587 
588 /* Return TRUE if MAP encodes locations coming from a macro
589    replacement-list at macro expansion point.  */
590 bool
591 linemap_macro_expansion_map_p (const struct line_map *);
592 
593 /* Assert that MAP encodes locations of tokens that are not part of
594    the replacement-list of a macro expansion, downcasting from
595    line_map * to line_map_ordinary *.  */
596 
597 inline line_map_ordinary *
linemap_check_ordinary(struct line_map * map)598 linemap_check_ordinary (struct line_map *map)
599 {
600   linemap_assert (MAP_ORDINARY_P (map));
601   return (line_map_ordinary *)map;
602 }
603 
604 /* Assert that MAP encodes locations of tokens that are not part of
605    the replacement-list of a macro expansion, downcasting from
606    const line_map * to const line_map_ordinary *.  */
607 
608 inline const line_map_ordinary *
linemap_check_ordinary(const struct line_map * map)609 linemap_check_ordinary (const struct line_map *map)
610 {
611   linemap_assert (MAP_ORDINARY_P (map));
612   return (const line_map_ordinary *)map;
613 }
614 
615 /* Assert that MAP is a macro expansion and downcast to the appropriate
616    subclass.  */
617 
linemap_check_macro(line_map * map)618 inline line_map_macro *linemap_check_macro (line_map *map)
619 {
620   linemap_assert (!MAP_ORDINARY_P (map));
621   return (line_map_macro *)map;
622 }
623 
624 /* Assert that MAP is a macro expansion and downcast to the appropriate
625    subclass.  */
626 
627 inline const line_map_macro *
linemap_check_macro(const line_map * map)628 linemap_check_macro (const line_map *map)
629 {
630   linemap_assert (!MAP_ORDINARY_P (map));
631   return (const line_map_macro *)map;
632 }
633 
634 /* Read the start location of MAP.  */
635 
636 inline location_t
MAP_START_LOCATION(const line_map * map)637 MAP_START_LOCATION (const line_map *map)
638 {
639   return map->start_location;
640 }
641 
642 /* Get the starting line number of ordinary map MAP.  */
643 
644 inline linenum_type
ORDINARY_MAP_STARTING_LINE_NUMBER(const line_map_ordinary * ord_map)645 ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map_ordinary *ord_map)
646 {
647   return ord_map->to_line;
648 }
649 
650 /* Return a positive value if map encodes locations from a system
651    header, 0 otherwise. Returns 1 if ordinary map MAP encodes locations
652    in a system header and 2 if it encodes locations in a C system header
653    that therefore needs to be extern "C" protected in C++.  */
654 
655 inline unsigned char
ORDINARY_MAP_IN_SYSTEM_HEADER_P(const line_map_ordinary * ord_map)656 ORDINARY_MAP_IN_SYSTEM_HEADER_P (const line_map_ordinary *ord_map)
657 {
658   return ord_map->sysp;
659 }
660 
661 /* Get the filename of ordinary map MAP.  */
662 
663 inline const char *
ORDINARY_MAP_FILE_NAME(const line_map_ordinary * ord_map)664 ORDINARY_MAP_FILE_NAME (const line_map_ordinary *ord_map)
665 {
666   return ord_map->to_file;
667 }
668 
669 /* Get the cpp macro whose expansion gave birth to macro map MAP.  */
670 
671 inline cpp_hashnode *
MACRO_MAP_MACRO(const line_map_macro * macro_map)672 MACRO_MAP_MACRO (const line_map_macro *macro_map)
673 {
674   return macro_map->macro;
675 }
676 
677 /* Get the number of tokens inside the replacement-list of the macro
678    that led to macro map MAP.  */
679 
680 inline unsigned int
MACRO_MAP_NUM_MACRO_TOKENS(const line_map_macro * macro_map)681 MACRO_MAP_NUM_MACRO_TOKENS (const line_map_macro *macro_map)
682 {
683   return macro_map->n_tokens;
684 }
685 
686 /* Get the array of pairs of locations within macro map MAP.
687    See the declaration of line_map_macro for more information.  */
688 
689 inline location_t *
MACRO_MAP_LOCATIONS(const line_map_macro * macro_map)690 MACRO_MAP_LOCATIONS (const line_map_macro *macro_map)
691 {
692   return macro_map->macro_locations;
693 }
694 
695 /* Get the location of the expansion point of the macro map MAP.  */
696 
697 inline location_t
MACRO_MAP_EXPANSION_POINT_LOCATION(const line_map_macro * macro_map)698 MACRO_MAP_EXPANSION_POINT_LOCATION (const line_map_macro *macro_map)
699 {
700   return macro_map->expansion;
701 }
702 
703 /* The abstraction of a set of location maps. There can be several
704    types of location maps. This abstraction contains the attributes
705    that are independent from the type of the map.
706 
707    Essentially this is just a vector of T_linemap_subclass,
708    which can only ever grow in size.  */
709 
710 struct GTY(()) maps_info_ordinary {
711   /* This array contains the "ordinary" line maps, for all
712      events other than macro expansion
713      (e.g. when a new preprocessing unit starts or ends).  */
714   line_map_ordinary * GTY ((length ("%h.used"))) maps;
715 
716   /* The total number of allocated maps.  */
717   unsigned int allocated;
718 
719   /* The number of elements used in maps. This number is smaller
720      or equal to ALLOCATED.  */
721   unsigned int used;
722 
723   unsigned int cache;
724 };
725 
726 struct GTY(()) maps_info_macro {
727   /* This array contains the macro line maps.
728      A macro line map is created whenever a macro expansion occurs.  */
729   line_map_macro * GTY ((length ("%h.used"))) maps;
730 
731   /* The total number of allocated maps.  */
732   unsigned int allocated;
733 
734   /* The number of elements used in maps. This number is smaller
735      or equal to ALLOCATED.  */
736   unsigned int used;
737 
738   unsigned int cache;
739 };
740 
741 /* Data structure to associate a source_range together with an arbitrary
742    data pointer with a source location.  */
743 struct GTY(()) location_adhoc_data {
744   location_t locus;
745   source_range src_range;
746   void * GTY((skip)) data;
747 };
748 
749 struct htab;
750 
751 /* The following data structure encodes a location with some adhoc data
752    and maps it to a new unsigned integer (called an adhoc location)
753    that replaces the original location to represent the mapping.
754 
755    The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
756    highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
757    the original location. Once identified as the adhoc_loc, the lower 31
758    bits of the integer is used to index the location_adhoc_data array,
759    in which the locus and associated data is stored.  */
760 
761 struct GTY(()) location_adhoc_data_map {
762   struct htab * GTY((skip)) htab;
763   location_t curr_loc;
764   unsigned int allocated;
765   struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
766 };
767 
768 /* A set of chronological line_map structures.  */
769 struct GTY(()) line_maps {
770 
771   ~line_maps ();
772 
773   maps_info_ordinary info_ordinary;
774 
775   maps_info_macro info_macro;
776 
777   /* Depth of the include stack, including the current file.  */
778   unsigned int depth;
779 
780   /* If true, prints an include trace a la -H.  */
781   bool trace_includes;
782 
783   /* Highest location_t "given out".  */
784   location_t highest_location;
785 
786   /* Start of line of highest location_t "given out".  */
787   location_t highest_line;
788 
789   /* The maximum column number we can quickly allocate.  Higher numbers
790      may require allocating a new line_map.  */
791   unsigned int max_column_hint;
792 
793   /* The allocator to use when resizing 'maps', defaults to xrealloc.  */
794   line_map_realloc reallocator;
795 
796   /* The allocators' function used to know the actual size it
797      allocated, for a certain allocation size requested.  */
798   line_map_round_alloc_size_func round_alloc_size;
799 
800   struct location_adhoc_data_map location_adhoc_data_map;
801 
802   /* The special location value that is used as spelling location for
803      built-in tokens.  */
804   location_t builtin_location;
805 
806   /* True if we've seen a #line or # 44 "file" directive.  */
807   bool seen_line_directive;
808 
809   /* The default value of range_bits in ordinary line maps.  */
810   unsigned int default_range_bits;
811 
812   unsigned int num_optimized_ranges;
813   unsigned int num_unoptimized_ranges;
814 };
815 
816 /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
817    if we are interested in macro maps, FALSE otherwise.  */
818 inline unsigned int
LINEMAPS_ALLOCATED(const line_maps * set,bool map_kind)819 LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind)
820 {
821   if (map_kind)
822     return set->info_macro.allocated;
823   else
824     return set->info_ordinary.allocated;
825 }
826 
827 /* As above, but by reference (e.g. as an lvalue).  */
828 
829 inline unsigned int &
LINEMAPS_ALLOCATED(line_maps * set,bool map_kind)830 LINEMAPS_ALLOCATED (line_maps *set, bool map_kind)
831 {
832   if (map_kind)
833     return set->info_macro.allocated;
834   else
835     return set->info_ordinary.allocated;
836 }
837 
838 /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
839    we are interested in macro maps, FALSE otherwise.*/
840 inline unsigned int
LINEMAPS_USED(const line_maps * set,bool map_kind)841 LINEMAPS_USED (const line_maps *set, bool map_kind)
842 {
843   if (map_kind)
844     return set->info_macro.used;
845   else
846     return set->info_ordinary.used;
847 }
848 
849 /* As above, but by reference (e.g. as an lvalue).  */
850 
851 inline unsigned int &
LINEMAPS_USED(line_maps * set,bool map_kind)852 LINEMAPS_USED (line_maps *set, bool map_kind)
853 {
854   if (map_kind)
855     return set->info_macro.used;
856   else
857     return set->info_ordinary.used;
858 }
859 
860 /* Returns the index of the last map that was looked up with
861    linemap_lookup. MAP_KIND shall be TRUE if we are interested in
862    macro maps, FALSE otherwise.  */
863 inline unsigned int
LINEMAPS_CACHE(const line_maps * set,bool map_kind)864 LINEMAPS_CACHE (const line_maps *set, bool map_kind)
865 {
866   if (map_kind)
867     return set->info_macro.cache;
868   else
869     return set->info_ordinary.cache;
870 }
871 
872 /* As above, but by reference (e.g. as an lvalue).  */
873 
874 inline unsigned int &
LINEMAPS_CACHE(line_maps * set,bool map_kind)875 LINEMAPS_CACHE (line_maps *set, bool map_kind)
876 {
877   if (map_kind)
878     return set->info_macro.cache;
879   else
880     return set->info_ordinary.cache;
881 }
882 
883 /* Return the map at a given index.  */
884 inline line_map *
LINEMAPS_MAP_AT(const line_maps * set,bool map_kind,int index)885 LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, int index)
886 {
887   if (map_kind)
888     return &set->info_macro.maps[index];
889   else
890     return &set->info_ordinary.maps[index];
891 }
892 
893 /* Returns the last map used in the line table SET. MAP_KIND
894    shall be TRUE if we are interested in macro maps, FALSE
895    otherwise.*/
896 inline line_map *
LINEMAPS_LAST_MAP(const line_maps * set,bool map_kind)897 LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind)
898 {
899   return LINEMAPS_MAP_AT (set, map_kind,
900 			  LINEMAPS_USED (set, map_kind) - 1);
901 }
902 
903 /* Returns the last map that was allocated in the line table SET.
904    MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
905    otherwise.*/
906 inline line_map *
LINEMAPS_LAST_ALLOCATED_MAP(const line_maps * set,bool map_kind)907 LINEMAPS_LAST_ALLOCATED_MAP (const line_maps *set, bool map_kind)
908 {
909   return LINEMAPS_MAP_AT (set, map_kind,
910 			  LINEMAPS_ALLOCATED (set, map_kind) - 1);
911 }
912 
913 /* Returns a pointer to the memory region where ordinary maps are
914    allocated in the line table SET.  */
915 inline line_map_ordinary *
LINEMAPS_ORDINARY_MAPS(const line_maps * set)916 LINEMAPS_ORDINARY_MAPS (const line_maps *set)
917 {
918   return set->info_ordinary.maps;
919 }
920 
921 /* Returns the INDEXth ordinary map.  */
922 inline line_map_ordinary *
LINEMAPS_ORDINARY_MAP_AT(const line_maps * set,int index)923 LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, int index)
924 {
925   linemap_assert (index >= 0);
926   linemap_assert ((unsigned int)index < set->info_ordinary.used);
927   return &set->info_ordinary.maps[index];
928 }
929 
930 /* Return the number of ordinary maps allocated in the line table
931    SET.  */
932 inline unsigned int
LINEMAPS_ORDINARY_ALLOCATED(const line_maps * set)933 LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set)
934 {
935   return LINEMAPS_ALLOCATED (set, false);
936 }
937 
938 /* Return the number of ordinary maps used in the line table SET.  */
939 inline unsigned int
LINEMAPS_ORDINARY_USED(const line_maps * set)940 LINEMAPS_ORDINARY_USED (const line_maps *set)
941 {
942   return LINEMAPS_USED (set, false);
943 }
944 
945 /* Return the index of the last ordinary map that was looked up with
946    linemap_lookup.  */
947 inline unsigned int
LINEMAPS_ORDINARY_CACHE(const line_maps * set)948 LINEMAPS_ORDINARY_CACHE (const line_maps *set)
949 {
950   return LINEMAPS_CACHE (set, false);
951 }
952 
953 /* As above, but by reference (e.g. as an lvalue).  */
954 
955 inline unsigned int &
LINEMAPS_ORDINARY_CACHE(line_maps * set)956 LINEMAPS_ORDINARY_CACHE (line_maps *set)
957 {
958   return LINEMAPS_CACHE (set, false);
959 }
960 
961 /* Returns a pointer to the last ordinary map used in the line table
962    SET.  */
963 inline line_map_ordinary *
LINEMAPS_LAST_ORDINARY_MAP(const line_maps * set)964 LINEMAPS_LAST_ORDINARY_MAP (const line_maps *set)
965 {
966   return (line_map_ordinary *)LINEMAPS_LAST_MAP (set, false);
967 }
968 
969 /* Returns a pointer to the last ordinary map allocated the line table
970    SET.  */
971 inline line_map_ordinary *
LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP(const line_maps * set)972 LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP (const line_maps *set)
973 {
974   return (line_map_ordinary *)LINEMAPS_LAST_ALLOCATED_MAP (set, false);
975 }
976 
977 /* Returns a pointer to the beginning of the region where macro maps
978    are allocated.  */
979 inline line_map_macro *
LINEMAPS_MACRO_MAPS(const line_maps * set)980 LINEMAPS_MACRO_MAPS (const line_maps *set)
981 {
982   return set->info_macro.maps;
983 }
984 
985 /* Returns the INDEXth macro map.  */
986 inline line_map_macro *
LINEMAPS_MACRO_MAP_AT(const line_maps * set,int index)987 LINEMAPS_MACRO_MAP_AT (const line_maps *set, int index)
988 {
989   linemap_assert (index >= 0);
990   linemap_assert ((unsigned int)index < set->info_macro.used);
991   return &set->info_macro.maps[index];
992 }
993 
994 /* Returns the number of macro maps that were allocated in the line
995    table SET.  */
996 inline unsigned int
LINEMAPS_MACRO_ALLOCATED(const line_maps * set)997 LINEMAPS_MACRO_ALLOCATED (const line_maps *set)
998 {
999   return LINEMAPS_ALLOCATED (set, true);
1000 }
1001 
1002 /* Returns the number of macro maps used in the line table SET.  */
1003 inline unsigned int
LINEMAPS_MACRO_USED(const line_maps * set)1004 LINEMAPS_MACRO_USED (const line_maps *set)
1005 {
1006   return LINEMAPS_USED (set, true);
1007 }
1008 
1009 /* Returns the index of the last macro map looked up with
1010    linemap_lookup.  */
1011 inline unsigned int
LINEMAPS_MACRO_CACHE(const line_maps * set)1012 LINEMAPS_MACRO_CACHE (const line_maps *set)
1013 {
1014   return LINEMAPS_CACHE (set, true);
1015 }
1016 
1017 /* As above, but by reference (e.g. as an lvalue).  */
1018 
1019 inline unsigned int &
LINEMAPS_MACRO_CACHE(line_maps * set)1020 LINEMAPS_MACRO_CACHE (line_maps *set)
1021 {
1022   return LINEMAPS_CACHE (set, true);
1023 }
1024 
1025 /* Returns the last macro map used in the line table SET.  */
1026 inline line_map_macro *
LINEMAPS_LAST_MACRO_MAP(const line_maps * set)1027 LINEMAPS_LAST_MACRO_MAP (const line_maps *set)
1028 {
1029   return (line_map_macro *)LINEMAPS_LAST_MAP (set, true);
1030 }
1031 
1032 /* Returns the lowest location [of a token resulting from macro
1033    expansion] encoded in this line table.  */
1034 inline location_t
LINEMAPS_MACRO_LOWEST_LOCATION(const line_maps * set)1035 LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps *set)
1036 {
1037   return LINEMAPS_MACRO_USED (set)
1038          ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))
1039          : MAX_LOCATION_T + 1;
1040 }
1041 
1042 /* Returns the last macro map allocated in the line table SET.  */
1043 inline line_map_macro *
LINEMAPS_LAST_ALLOCATED_MACRO_MAP(const line_maps * set)1044 LINEMAPS_LAST_ALLOCATED_MACRO_MAP (const line_maps *set)
1045 {
1046   return (line_map_macro *)LINEMAPS_LAST_ALLOCATED_MAP (set, true);
1047 }
1048 
1049 extern location_t get_combined_adhoc_loc (struct line_maps *,
1050 					       location_t,
1051 					       source_range,
1052 					       void *);
1053 extern void *get_data_from_adhoc_loc (struct line_maps *, location_t);
1054 extern location_t get_location_from_adhoc_loc (struct line_maps *,
1055 						    location_t);
1056 
1057 extern source_range get_range_from_loc (line_maps *set, location_t loc);
1058 
1059 /* Get whether location LOC is a "pure" location, or
1060    whether it is an ad-hoc location, or embeds range information.  */
1061 
1062 bool
1063 pure_location_p (line_maps *set, location_t loc);
1064 
1065 /* Given location LOC within SET, strip away any packed range information
1066    or ad-hoc information.  */
1067 
1068 extern location_t get_pure_location (line_maps *set,
1069 					  location_t loc);
1070 
1071 /* Combine LOC and BLOCK, giving a combined adhoc location.  */
1072 
1073 inline location_t
COMBINE_LOCATION_DATA(struct line_maps * set,location_t loc,source_range src_range,void * block)1074 COMBINE_LOCATION_DATA (struct line_maps *set,
1075 		       location_t loc,
1076 		       source_range src_range,
1077 		       void *block)
1078 {
1079   return get_combined_adhoc_loc (set, loc, src_range, block);
1080 }
1081 
1082 extern void rebuild_location_adhoc_htab (struct line_maps *);
1083 
1084 /* Initialize a line map set.  SET is the line map set to initialize
1085    and BUILTIN_LOCATION is the special location value to be used as
1086    spelling location for built-in tokens.  This BUILTIN_LOCATION has
1087    to be strictly less than RESERVED_LOCATION_COUNT.  */
1088 extern void linemap_init (struct line_maps *set,
1089 			  location_t builtin_location);
1090 
1091 /* Check for and warn about line_maps entered but not exited.  */
1092 
1093 extern void linemap_check_files_exited (struct line_maps *);
1094 
1095 /* Return a location_t for the start (i.e. column==0) of
1096    (physical) line TO_LINE in the current source file (as in the
1097    most recent linemap_add).   MAX_COLUMN_HINT is the highest column
1098    number we expect to use in this line (but it does not change
1099    the highest_location).  */
1100 
1101 extern location_t linemap_line_start
1102 (struct line_maps *set, linenum_type to_line,  unsigned int max_column_hint);
1103 
1104 /* Add a mapping of logical source line to physical source file and
1105    line number. This function creates an "ordinary map", which is a
1106    map that records locations of tokens that are not part of macro
1107    replacement-lists present at a macro expansion point.
1108 
1109    The text pointed to by TO_FILE must have a lifetime
1110    at least as long as the lifetime of SET.  An empty
1111    TO_FILE means standard input.  If reason is LC_LEAVE, and
1112    TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
1113    natural values considering the file we are returning to.
1114 
1115    A call to this function can relocate the previous set of
1116    maps, so any stored line_map pointers should not be used.  */
1117 extern const struct line_map *linemap_add
1118   (struct line_maps *, enum lc_reason, unsigned int sysp,
1119    const char *to_file, linenum_type to_line);
1120 
1121 /* Given a logical source location, returns the map which the
1122    corresponding (source file, line, column) triplet can be deduced
1123    from. Since the set is built chronologically, the logical lines are
1124    monotonic increasing, and so the list is sorted and we can use a
1125    binary search. If no line map have been allocated yet, this
1126    function returns NULL.  */
1127 extern const struct line_map *linemap_lookup
1128   (struct line_maps *, location_t);
1129 
1130 /* Returns TRUE if the line table set tracks token locations across
1131    macro expansion, FALSE otherwise.  */
1132 bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
1133 
1134 /* Return the name of the macro associated to MACRO_MAP.  */
1135 const char* linemap_map_get_macro_name (const line_map_macro *);
1136 
1137 /* Return a positive value if LOCATION is the locus of a token that is
1138    located in a system header, O otherwise. It returns 1 if LOCATION
1139    is the locus of a token that is located in a system header, and 2
1140    if LOCATION is the locus of a token located in a C system header
1141    that therefore needs to be extern "C" protected in C++.
1142 
1143    Note that this function returns 1 if LOCATION belongs to a token
1144    that is part of a macro replacement-list defined in a system
1145    header, but expanded in a non-system file.  */
1146 int linemap_location_in_system_header_p (struct line_maps *,
1147 					 location_t);
1148 
1149 /* Return TRUE if LOCATION is a source code location of a token that is part of
1150    a macro expansion, FALSE otherwise.  */
1151 bool linemap_location_from_macro_expansion_p (const struct line_maps *,
1152 					      location_t);
1153 
1154 /* TRUE if LOCATION is a source code location of a token that is part of the
1155    definition of a macro, FALSE otherwise.  */
1156 bool linemap_location_from_macro_definition_p (struct line_maps *,
1157 					       location_t);
1158 
1159 /* With the precondition that LOCATION is the locus of a token that is
1160    an argument of a function-like macro MACRO_MAP and appears in the
1161    expansion of MACRO_MAP, return the locus of that argument in the
1162    context of the caller of MACRO_MAP.  */
1163 
1164 extern location_t linemap_macro_map_loc_unwind_toward_spelling
1165   (line_maps *set, const line_map_macro *macro_map, location_t location);
1166 
1167 /* location_t values from 0 to RESERVED_LOCATION_COUNT-1 will
1168    be reserved for libcpp user as special values, no token from libcpp
1169    will contain any of those locations.  */
1170 const location_t RESERVED_LOCATION_COUNT = 2;
1171 
1172 /* Converts a map and a location_t to source line.  */
1173 inline linenum_type
SOURCE_LINE(const line_map_ordinary * ord_map,location_t loc)1174 SOURCE_LINE (const line_map_ordinary *ord_map, location_t loc)
1175 {
1176   return ((loc - ord_map->start_location)
1177 	  >> ord_map->m_column_and_range_bits) + ord_map->to_line;
1178 }
1179 
1180 /* Convert a map and location_t to source column number.  */
1181 inline linenum_type
SOURCE_COLUMN(const line_map_ordinary * ord_map,location_t loc)1182 SOURCE_COLUMN (const line_map_ordinary *ord_map, location_t loc)
1183 {
1184   return ((loc - ord_map->start_location)
1185 	  & ((1 << ord_map->m_column_and_range_bits) - 1)) >> ord_map->m_range_bits;
1186 }
1187 
1188 
1189 inline location_t
linemap_included_from(const line_map_ordinary * ord_map)1190 linemap_included_from (const line_map_ordinary *ord_map)
1191 {
1192   return ord_map->included_from;
1193 }
1194 
1195 /* The linemap containing the included-from location of MAP.  */
1196 const line_map_ordinary *linemap_included_from_linemap
1197   (line_maps *set, const line_map_ordinary *map);
1198 
1199 /* True if the map is at the bottom of the include stack.  */
1200 
1201 inline bool
MAIN_FILE_P(const line_map_ordinary * ord_map)1202 MAIN_FILE_P (const line_map_ordinary *ord_map)
1203 {
1204   return ord_map->included_from == 0;
1205 }
1206 
1207 /* Encode and return a location_t from a column number. The
1208    source line considered is the last source line used to call
1209    linemap_line_start, i.e, the last source line which a location was
1210    encoded from.  */
1211 extern location_t
1212 linemap_position_for_column (struct line_maps *, unsigned int);
1213 
1214 /* Encode and return a source location from a given line and
1215    column.  */
1216 location_t
1217 linemap_position_for_line_and_column (line_maps *set,
1218 				      const line_map_ordinary *,
1219 				      linenum_type, unsigned int);
1220 
1221 /* Encode and return a location_t starting from location LOC and
1222    shifting it by OFFSET columns.  This function does not support
1223    virtual locations.  */
1224 location_t
1225 linemap_position_for_loc_and_offset (struct line_maps *set,
1226 				     location_t loc,
1227 				     unsigned int offset);
1228 
1229 /* Return the file this map is for.  */
1230 inline const char *
LINEMAP_FILE(const line_map_ordinary * ord_map)1231 LINEMAP_FILE (const line_map_ordinary *ord_map)
1232 {
1233   return ord_map->to_file;
1234 }
1235 
1236 /* Return the line number this map started encoding location from.  */
1237 inline linenum_type
LINEMAP_LINE(const line_map_ordinary * ord_map)1238 LINEMAP_LINE (const line_map_ordinary *ord_map)
1239 {
1240   return ord_map->to_line;
1241 }
1242 
1243 /* Return a positive value if map encodes locations from a system
1244    header, 0 otherwise. Returns 1 if MAP encodes locations in a
1245    system header and 2 if it encodes locations in a C system header
1246    that therefore needs to be extern "C" protected in C++.  */
1247 inline unsigned char
LINEMAP_SYSP(const line_map_ordinary * ord_map)1248 LINEMAP_SYSP (const line_map_ordinary *ord_map)
1249 {
1250   return ord_map->sysp;
1251 }
1252 
1253 /* Return a positive value if PRE denotes the location of a token that
1254    comes before the token of POST, 0 if PRE denotes the location of
1255    the same token as the token for POST, and a negative value
1256    otherwise.  */
1257 int linemap_compare_locations (struct line_maps *set,
1258 			       location_t   pre,
1259 			       location_t   post);
1260 
1261 /* Return TRUE if LOC_A denotes the location a token that comes
1262    topogically before the token denoted by location LOC_B, or if they
1263    are equal.  */
1264 inline bool
linemap_location_before_p(struct line_maps * set,location_t loc_a,location_t loc_b)1265 linemap_location_before_p (struct line_maps *set,
1266 			   location_t loc_a,
1267 			   location_t loc_b)
1268 {
1269   return linemap_compare_locations (set, loc_a, loc_b) >= 0;
1270 }
1271 
1272 typedef struct
1273 {
1274   /* The name of the source file involved.  */
1275   const char *file;
1276 
1277   /* The line-location in the source file.  */
1278   int line;
1279 
1280   int column;
1281 
1282   void *data;
1283 
1284   /* In a system header?. */
1285   bool sysp;
1286 } expanded_location;
1287 
1288 class range_label;
1289 
1290 /* A hint to diagnostic_show_locus on how to print a source range within a
1291    rich_location.
1292 
1293    Typically this is SHOW_RANGE_WITH_CARET for the 0th range, and
1294    SHOW_RANGE_WITHOUT_CARET for subsequent ranges,
1295    but the Fortran frontend uses SHOW_RANGE_WITH_CARET repeatedly for
1296    printing things like:
1297 
1298        x = x + y
1299            1   2
1300        Error: Shapes for operands at (1) and (2) are not conformable
1301 
1302    where "1" and "2" are notionally carets.  */
1303 
1304 enum range_display_kind
1305 {
1306   /* Show the pertinent source line(s), the caret, and underline(s).  */
1307   SHOW_RANGE_WITH_CARET,
1308 
1309   /* Show the pertinent source line(s) and underline(s), but don't
1310      show the caret (just an underline).  */
1311   SHOW_RANGE_WITHOUT_CARET,
1312 
1313   /* Just show the source lines; don't show the range itself.
1314      This is for use when displaying some line-insertion fix-it hints (for
1315      showing the user context on the change, for when it doesn't make sense
1316      to highlight the first column on the next line).  */
1317   SHOW_LINES_WITHOUT_RANGE
1318 };
1319 
1320 /* A location within a rich_location: a caret&range, with
1321    the caret potentially flagged for display, and an optional
1322    label.  */
1323 
1324 struct location_range
1325 {
1326   location_t m_loc;
1327 
1328   enum range_display_kind m_range_display_kind;
1329 
1330   /* If non-NULL, the label for this range.  */
1331   const range_label *m_label;
1332 };
1333 
1334 /* A partially-embedded vec for use within rich_location for storing
1335    ranges and fix-it hints.
1336 
1337    Elements [0..NUM_EMBEDDED) are allocated within m_embed, after
1338    that they are within the dynamically-allocated m_extra.
1339 
1340    This allows for static allocation in the common case, whilst
1341    supporting the rarer case of an arbitrary number of elements.
1342 
1343    Dynamic allocation is not performed unless it's needed.  */
1344 
1345 template <typename T, int NUM_EMBEDDED>
1346 class semi_embedded_vec
1347 {
1348  public:
1349   semi_embedded_vec ();
1350   ~semi_embedded_vec ();
1351 
count()1352   unsigned int count () const { return m_num; }
1353   T& operator[] (int idx);
1354   const T& operator[] (int idx) const;
1355 
1356   void push (const T&);
1357   void truncate (int len);
1358 
1359  private:
1360   int m_num;
1361   T m_embedded[NUM_EMBEDDED];
1362   int m_alloc;
1363   T *m_extra;
1364 };
1365 
1366 /* Constructor for semi_embedded_vec.  In particular, no dynamic allocation
1367    is done.  */
1368 
1369 template <typename T, int NUM_EMBEDDED>
semi_embedded_vec()1370 semi_embedded_vec<T, NUM_EMBEDDED>::semi_embedded_vec ()
1371 : m_num (0), m_alloc (0), m_extra (NULL)
1372 {
1373 }
1374 
1375 /* semi_embedded_vec's dtor.  Release any dynamically-allocated memory.  */
1376 
1377 template <typename T, int NUM_EMBEDDED>
~semi_embedded_vec()1378 semi_embedded_vec<T, NUM_EMBEDDED>::~semi_embedded_vec ()
1379 {
1380   XDELETEVEC (m_extra);
1381 }
1382 
1383 /* Look up element IDX, mutably.  */
1384 
1385 template <typename T, int NUM_EMBEDDED>
1386 T&
1387 semi_embedded_vec<T, NUM_EMBEDDED>::operator[] (int idx)
1388 {
1389   linemap_assert (idx < m_num);
1390   if (idx < NUM_EMBEDDED)
1391     return m_embedded[idx];
1392   else
1393     {
1394       linemap_assert (m_extra != NULL);
1395       return m_extra[idx - NUM_EMBEDDED];
1396     }
1397 }
1398 
1399 /* Look up element IDX (const).  */
1400 
1401 template <typename T, int NUM_EMBEDDED>
1402 const T&
1403 semi_embedded_vec<T, NUM_EMBEDDED>::operator[] (int idx) const
1404 {
1405   linemap_assert (idx < m_num);
1406   if (idx < NUM_EMBEDDED)
1407     return m_embedded[idx];
1408   else
1409     {
1410       linemap_assert (m_extra != NULL);
1411       return m_extra[idx - NUM_EMBEDDED];
1412     }
1413 }
1414 
1415 /* Append VALUE to the end of the semi_embedded_vec.  */
1416 
1417 template <typename T, int NUM_EMBEDDED>
1418 void
push(const T & value)1419 semi_embedded_vec<T, NUM_EMBEDDED>::push (const T& value)
1420 {
1421   int idx = m_num++;
1422   if (idx < NUM_EMBEDDED)
1423     m_embedded[idx] = value;
1424   else
1425     {
1426       /* Offset "idx" to be an index within m_extra.  */
1427       idx -= NUM_EMBEDDED;
1428       if (NULL == m_extra)
1429 	{
1430 	  linemap_assert (m_alloc == 0);
1431 	  m_alloc = 16;
1432 	  m_extra = XNEWVEC (T, m_alloc);
1433 	}
1434       else if (idx >= m_alloc)
1435 	{
1436 	  linemap_assert (m_alloc > 0);
1437 	  m_alloc *= 2;
1438 	  m_extra = XRESIZEVEC (T, m_extra, m_alloc);
1439 	}
1440       linemap_assert (m_extra);
1441       linemap_assert (idx < m_alloc);
1442       m_extra[idx] = value;
1443     }
1444 }
1445 
1446 /* Truncate to length LEN.  No deallocation is performed.  */
1447 
1448 template <typename T, int NUM_EMBEDDED>
1449 void
truncate(int len)1450 semi_embedded_vec<T, NUM_EMBEDDED>::truncate (int len)
1451 {
1452   linemap_assert (len <= m_num);
1453   m_num = len;
1454 }
1455 
1456 class fixit_hint;
1457 
1458 /* A "rich" source code location, for use when printing diagnostics.
1459    A rich_location has one or more carets&ranges, where the carets
1460    are optional.  These are referred to as "ranges" from here.
1461    Typically the zeroth range has a caret; other ranges sometimes
1462    have carets.
1463 
1464    The "primary" location of a rich_location is the caret of range 0,
1465    used for determining the line/column when printing diagnostic
1466    text, such as:
1467 
1468       some-file.c:3:1: error: ...etc...
1469 
1470    Additional ranges may be added to help the user identify other
1471    pertinent clauses in a diagnostic.
1472 
1473    Ranges can (optionally) be given labels via class range_label.
1474 
1475    rich_location instances are intended to be allocated on the stack
1476    when generating diagnostics, and to be short-lived.
1477 
1478    Examples of rich locations
1479    --------------------------
1480 
1481    Example A
1482    *********
1483       int i = "foo";
1484               ^
1485    This "rich" location is simply a single range (range 0), with
1486    caret = start = finish at the given point.
1487 
1488    Example B
1489    *********
1490       a = (foo && bar)
1491           ~~~~~^~~~~~~
1492    This rich location has a single range (range 0), with the caret
1493    at the first "&", and the start/finish at the parentheses.
1494    Compare with example C below.
1495 
1496    Example C
1497    *********
1498       a = (foo && bar)
1499            ~~~ ^~ ~~~
1500    This rich location has three ranges:
1501    - Range 0 has its caret and start location at the first "&" and
1502      end at the second "&.
1503    - Range 1 has its start and finish at the "f" and "o" of "foo";
1504      the caret is not flagged for display, but is perhaps at the "f"
1505      of "foo".
1506    - Similarly, range 2 has its start and finish at the "b" and "r" of
1507      "bar"; the caret is not flagged for display, but is perhaps at the
1508      "b" of "bar".
1509    Compare with example B above.
1510 
1511    Example D (Fortran frontend)
1512    ****************************
1513        x = x + y
1514            1   2
1515    This rich location has range 0 at "1", and range 1 at "2".
1516    Both are flagged for caret display.  Both ranges have start/finish
1517    equal to their caret point.  The frontend overrides the diagnostic
1518    context's default caret character for these ranges.
1519 
1520    Example E (range labels)
1521    ************************
1522       printf ("arg0: %i  arg1: %s arg2: %i",
1523                                ^~
1524                                |
1525                                const char *
1526               100, 101, 102);
1527                    ~~~
1528                    |
1529                    int
1530    This rich location has two ranges:
1531    - range 0 is at the "%s" with start = caret = "%" and finish at
1532      the "s".  It has a range_label ("const char *").
1533    - range 1 has start/finish covering the "101" and is not flagged for
1534      caret printing.  The caret is at the start of "101", where its
1535      range_label is printed ("int").
1536 
1537    Fix-it hints
1538    ------------
1539 
1540    Rich locations can also contain "fix-it hints", giving suggestions
1541    for the user on how to edit their code to fix a problem.  These
1542    can be expressed as insertions, replacements, and removals of text.
1543    The edits by default are relative to the zeroth range within the
1544    rich_location, but optionally they can be expressed relative to
1545    other locations (using various overloaded methods of the form
1546    rich_location::add_fixit_*).
1547 
1548    For example:
1549 
1550    Example F: fix-it hint: insert_before
1551    *************************************
1552       ptr = arr[0];
1553 	    ^~~~~~
1554 	    &
1555    This rich location has a single range (range 0) covering "arr[0]",
1556    with the caret at the start.  The rich location has a single
1557    insertion fix-it hint, inserted before range 0, added via
1558      richloc.add_fixit_insert_before ("&");
1559 
1560    Example G: multiple fix-it hints: insert_before and insert_after
1561    ****************************************************************
1562       #define FN(ARG0, ARG1, ARG2) fn(ARG0, ARG1, ARG2)
1563 				      ^~~~  ^~~~  ^~~~
1564 				      (   ) (   ) (   )
1565    This rich location has three ranges, covering "arg0", "arg1",
1566    and "arg2", all with caret-printing enabled.
1567    The rich location has 6 insertion fix-it hints: each arg
1568    has a pair of insertion fix-it hints, suggesting wrapping
1569    them with parentheses: one a '(' inserted before,
1570    the other a ')' inserted after, added via
1571      richloc.add_fixit_insert_before (LOC, "(");
1572    and
1573      richloc.add_fixit_insert_after (LOC, ")");
1574 
1575    Example H: fix-it hint: removal
1576    *******************************
1577      struct s {int i};;
1578 		      ^
1579 		      -
1580    This rich location has a single range at the stray trailing
1581    semicolon, along with a single removal fix-it hint, covering
1582    the same range, added via:
1583      richloc.add_fixit_remove ();
1584 
1585    Example I: fix-it hint: replace
1586    *******************************
1587       c = s.colour;
1588 	    ^~~~~~
1589 	    color
1590    This rich location has a single range (range 0) covering "colour",
1591    and a single "replace" fix-it hint, covering the same range,
1592    added via
1593      richloc.add_fixit_replace ("color");
1594 
1595    Example J: fix-it hint: line insertion
1596    **************************************
1597 
1598      3 | #include <stddef.h>
1599      + |+#include <stdio.h>
1600      4 | int the_next_line;
1601 
1602    This rich location has a single range at line 4 column 1, marked
1603    with SHOW_LINES_WITHOUT_RANGE (to avoid printing a meaningless caret
1604    on the "i" of int).  It has a insertion fix-it hint of the string
1605    "#include <stdio.h>\n".
1606 
1607    Adding a fix-it hint can fail: for example, attempts to insert content
1608    at the transition between two line maps may fail due to there being no
1609    location_t value to express the new location.
1610 
1611    Attempts to add a fix-it hint within a macro expansion will fail.
1612 
1613    There is only limited support for newline characters in fix-it hints:
1614    only hints with newlines which insert an entire new line are permitted,
1615    inserting at the start of a line, and finishing with a newline
1616    (with no interior newline characters).  Other attempts to add
1617    fix-it hints containing newline characters will fail.
1618    Similarly, attempts to delete or replace a range *affecting* multiple
1619    lines will fail.
1620 
1621    The rich_location API handles these failures gracefully, so that
1622    diagnostics can attempt to add fix-it hints without each needing
1623    extensive checking.
1624 
1625    Fix-it hints within a rich_location are "atomic": if any hints can't
1626    be applied, none of them will be (tracked by the m_seen_impossible_fixit
1627    flag), and no fix-its hints will be displayed for that rich_location.
1628    This implies that diagnostic messages need to be worded in such a way
1629    that they make sense whether or not the fix-it hints are displayed,
1630    or that richloc.seen_impossible_fixit_p () should be checked before
1631    issuing the diagnostics.  */
1632 
1633 class rich_location
1634 {
1635  public:
1636   /* Constructors.  */
1637 
1638   /* Constructing from a location.  */
1639   rich_location (line_maps *set, location_t loc,
1640 		 const range_label *label = NULL);
1641 
1642   /* Destructor.  */
1643   ~rich_location ();
1644 
1645   /* Accessors.  */
get_loc()1646   location_t get_loc () const { return get_loc (0); }
1647   location_t get_loc (unsigned int idx) const;
1648 
1649   void
1650   add_range (location_t loc,
1651 	     enum range_display_kind range_display_kind
1652 	       = SHOW_RANGE_WITHOUT_CARET,
1653 	     const range_label *label = NULL);
1654 
1655   void
1656   set_range (unsigned int idx, location_t loc,
1657 	     enum range_display_kind range_display_kind);
1658 
get_num_locations()1659   unsigned int get_num_locations () const { return m_ranges.count (); }
1660 
1661   const location_range *get_range (unsigned int idx) const;
1662   location_range *get_range (unsigned int idx);
1663 
1664   expanded_location get_expanded_location (unsigned int idx);
1665 
1666   void
1667   override_column (int column);
1668 
1669   /* Fix-it hints.  */
1670 
1671   /* Methods for adding insertion fix-it hints.  */
1672 
1673   /* Suggest inserting NEW_CONTENT immediately before the primary
1674      range's start.  */
1675   void
1676   add_fixit_insert_before (const char *new_content);
1677 
1678   /* Suggest inserting NEW_CONTENT immediately before the start of WHERE.  */
1679   void
1680   add_fixit_insert_before (location_t where,
1681 			   const char *new_content);
1682 
1683   /* Suggest inserting NEW_CONTENT immediately after the end of the primary
1684      range.  */
1685   void
1686   add_fixit_insert_after (const char *new_content);
1687 
1688   /* Suggest inserting NEW_CONTENT immediately after the end of WHERE.  */
1689   void
1690   add_fixit_insert_after (location_t where,
1691 			  const char *new_content);
1692 
1693   /* Methods for adding removal fix-it hints.  */
1694 
1695   /* Suggest removing the content covered by range 0.  */
1696   void
1697   add_fixit_remove ();
1698 
1699   /* Suggest removing the content covered between the start and finish
1700      of WHERE.  */
1701   void
1702   add_fixit_remove (location_t where);
1703 
1704   /* Suggest removing the content covered by SRC_RANGE.  */
1705   void
1706   add_fixit_remove (source_range src_range);
1707 
1708   /* Methods for adding "replace" fix-it hints.  */
1709 
1710   /* Suggest replacing the content covered by range 0 with NEW_CONTENT.  */
1711   void
1712   add_fixit_replace (const char *new_content);
1713 
1714   /* Suggest replacing the content between the start and finish of
1715      WHERE with NEW_CONTENT.  */
1716   void
1717   add_fixit_replace (location_t where,
1718 		     const char *new_content);
1719 
1720   /* Suggest replacing the content covered by SRC_RANGE with
1721      NEW_CONTENT.  */
1722   void
1723   add_fixit_replace (source_range src_range,
1724 		     const char *new_content);
1725 
get_num_fixit_hints()1726   unsigned int get_num_fixit_hints () const { return m_fixit_hints.count (); }
get_fixit_hint(int idx)1727   fixit_hint *get_fixit_hint (int idx) const { return m_fixit_hints[idx]; }
1728   fixit_hint *get_last_fixit_hint () const;
seen_impossible_fixit_p()1729   bool seen_impossible_fixit_p () const { return m_seen_impossible_fixit; }
1730 
1731   /* Set this if the fix-it hints are not suitable to be
1732      automatically applied.
1733 
1734      For example, if you are suggesting more than one
1735      mutually exclusive solution to a problem, then
1736      it doesn't make sense to apply all of the solutions;
1737      manual intervention is required.
1738 
1739      If set, then the fix-it hints in the rich_location will
1740      be printed, but will not be added to generated patches,
1741      or affect the modified version of the file.  */
fixits_cannot_be_auto_applied()1742   void fixits_cannot_be_auto_applied ()
1743   {
1744     m_fixits_cannot_be_auto_applied = true;
1745   }
1746 
fixits_can_be_auto_applied_p()1747   bool fixits_can_be_auto_applied_p () const
1748   {
1749     return !m_fixits_cannot_be_auto_applied;
1750   }
1751 
1752 private:
1753   bool reject_impossible_fixit (location_t where);
1754   void stop_supporting_fixits ();
1755   void maybe_add_fixit (location_t start,
1756 			location_t next_loc,
1757 			const char *new_content);
1758 
1759 public:
1760   static const int STATICALLY_ALLOCATED_RANGES = 3;
1761 
1762 protected:
1763   line_maps *m_line_table;
1764   semi_embedded_vec <location_range, STATICALLY_ALLOCATED_RANGES> m_ranges;
1765 
1766   int m_column_override;
1767 
1768   bool m_have_expanded_location;
1769   expanded_location m_expanded_location;
1770 
1771   static const int MAX_STATIC_FIXIT_HINTS = 2;
1772   semi_embedded_vec <fixit_hint *, MAX_STATIC_FIXIT_HINTS> m_fixit_hints;
1773 
1774   bool m_seen_impossible_fixit;
1775   bool m_fixits_cannot_be_auto_applied;
1776 };
1777 
1778 /* A struct for the result of range_label::get_text: a NUL-terminated buffer
1779    of localized text, and a flag to determine if the caller should "free" the
1780    buffer.  */
1781 
1782 struct label_text
1783 {
label_textlabel_text1784   label_text ()
1785   : m_buffer (NULL), m_caller_owned (false)
1786   {}
1787 
label_textlabel_text1788   label_text (char *buffer, bool caller_owned)
1789   : m_buffer (buffer), m_caller_owned (caller_owned)
1790   {}
1791 
maybe_freelabel_text1792   void maybe_free ()
1793   {
1794     if (m_caller_owned)
1795       free (m_buffer);
1796   }
1797 
1798   char *m_buffer;
1799   bool m_caller_owned;
1800 };
1801 
1802 /* Abstract base class for labelling a range within a rich_location
1803    (e.g. for labelling expressions with their type).
1804 
1805    Generating the text could require non-trivial work, so this work
1806    is delayed (via the "get_text" virtual function) until the diagnostic
1807    printing code "knows" it needs it, thus avoiding doing it e.g. for
1808    warnings that are filtered by command-line flags.  This virtual
1809    function also isolates libcpp and the diagnostics subsystem from
1810    the front-end and middle-end-specific code for generating the text
1811    for the labels.
1812 
1813    Like the rich_location instances they annotate, range_label instances
1814    are intended to be allocated on the stack when generating diagnostics,
1815    and to be short-lived.  */
1816 
1817 class range_label
1818 {
1819  public:
~range_label()1820   virtual ~range_label () {}
1821 
1822   /* Get localized text for the label.
1823      The RANGE_IDX is provided, allowing for range_label instances to be
1824      shared by multiple ranges if need be (the "flyweight" design pattern).  */
1825   virtual label_text get_text (unsigned range_idx) const = 0;
1826 };
1827 
1828 /* A fix-it hint: a suggested insertion, replacement, or deletion of text.
1829    We handle these three types of edit with one class, by representing
1830    them as replacement of a half-open range:
1831        [start, next_loc)
1832    Insertions have start == next_loc: "replace" the empty string at the
1833    start location with the new string.
1834    Deletions are replacement with the empty string.
1835 
1836    There is only limited support for newline characters in fix-it hints
1837    as noted above in the comment for class rich_location.
1838    A fixit_hint instance can have at most one newline character; if
1839    present, the newline character must be the final character of
1840    the content (preventing e.g. fix-its that split a pre-existing line).  */
1841 
1842 class fixit_hint
1843 {
1844  public:
1845   fixit_hint (location_t start,
1846 	      location_t next_loc,
1847 	      const char *new_content);
~fixit_hint()1848   ~fixit_hint () { free (m_bytes); }
1849 
1850   bool affects_line_p (const char *file, int line) const;
get_start_loc()1851   location_t get_start_loc () const { return m_start; }
get_next_loc()1852   location_t get_next_loc () const { return m_next_loc; }
1853   bool maybe_append (location_t start,
1854 		     location_t next_loc,
1855 		     const char *new_content);
1856 
get_string()1857   const char *get_string () const { return m_bytes; }
get_length()1858   size_t get_length () const { return m_len; }
1859 
insertion_p()1860   bool insertion_p () const { return m_start == m_next_loc; }
1861 
1862   bool ends_with_newline_p () const;
1863 
1864  private:
1865   /* We don't use source_range here since, unlike most places,
1866      this is a half-open/half-closed range:
1867        [start, next_loc)
1868      so that we can support insertion via start == next_loc.  */
1869   location_t m_start;
1870   location_t m_next_loc;
1871   char *m_bytes;
1872   size_t m_len;
1873 };
1874 
1875 
1876 /* This is enum is used by the function linemap_resolve_location
1877    below.  The meaning of the values is explained in the comment of
1878    that function.  */
1879 enum location_resolution_kind
1880 {
1881   LRK_MACRO_EXPANSION_POINT,
1882   LRK_SPELLING_LOCATION,
1883   LRK_MACRO_DEFINITION_LOCATION
1884 };
1885 
1886 /* Resolve a virtual location into either a spelling location, an
1887    expansion point location or a token argument replacement point
1888    location.  Return the map that encodes the virtual location as well
1889    as the resolved location.
1890 
1891    If LOC is *NOT* the location of a token resulting from the
1892    expansion of a macro, then the parameter LRK (which stands for
1893    Location Resolution Kind) is ignored and the resulting location
1894    just equals the one given in argument.
1895 
1896    Now if LOC *IS* the location of a token resulting from the
1897    expansion of a macro, this is what happens.
1898 
1899    * If LRK is set to LRK_MACRO_EXPANSION_POINT
1900    -------------------------------
1901 
1902    The virtual location is resolved to the first macro expansion point
1903    that led to this macro expansion.
1904 
1905    * If LRK is set to LRK_SPELLING_LOCATION
1906    -------------------------------------
1907 
1908    The virtual location is resolved to the locus where the token has
1909    been spelled in the source.   This can follow through all the macro
1910    expansions that led to the token.
1911 
1912    * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1913    --------------------------------------
1914 
1915    The virtual location is resolved to the locus of the token in the
1916    context of the macro definition.
1917 
1918    If LOC is the locus of a token that is an argument of a
1919    function-like macro [replacing a parameter in the replacement list
1920    of the macro] the virtual location is resolved to the locus of the
1921    parameter that is replaced, in the context of the definition of the
1922    macro.
1923 
1924    If LOC is the locus of a token that is not an argument of a
1925    function-like macro, then the function behaves as if LRK was set to
1926    LRK_SPELLING_LOCATION.
1927 
1928    If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1929    returned location.  Note that if the returned location wasn't originally
1930    encoded by a map, the *MAP is set to NULL.  This can happen if LOC
1931    resolves to a location reserved for the client code, like
1932    UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC.  */
1933 
1934 location_t linemap_resolve_location (struct line_maps *,
1935 				     location_t loc,
1936 				     enum location_resolution_kind lrk,
1937 				     const line_map_ordinary **loc_map);
1938 
1939 /* Suppose that LOC is the virtual location of a token coming from the
1940    expansion of a macro M.  This function then steps up to get the
1941    location L of the point where M got expanded.  If L is a spelling
1942    location inside a macro expansion M', then this function returns
1943    the point where M' was expanded.  LOC_MAP is an output parameter.
1944    When non-NULL, *LOC_MAP is set to the map of the returned
1945    location.  */
1946 location_t linemap_unwind_toward_expansion (struct line_maps *,
1947 					    location_t loc,
1948 					    const struct line_map **loc_map);
1949 
1950 /* If LOC is the virtual location of a token coming from the expansion
1951    of a macro M and if its spelling location is reserved (e.g, a
1952    location for a built-in token), then this function unwinds (using
1953    linemap_unwind_toward_expansion) the location until a location that
1954    is not reserved and is not in a system header is reached.  In other
1955    words, this unwinds the reserved location until a location that is
1956    in real source code is reached.
1957 
1958    Otherwise, if the spelling location for LOC is not reserved or if
1959    LOC doesn't come from the expansion of a macro, the function
1960    returns LOC as is and *MAP is not touched.
1961 
1962    *MAP is set to the map of the returned location if the later is
1963    different from LOC.  */
1964 location_t linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
1965 						     location_t loc,
1966 						     const struct line_map **map);
1967 
1968 /* Expand source code location LOC and return a user readable source
1969    code location.  LOC must be a spelling (non-virtual) location.  If
1970    it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1971    location is returned.  */
1972 expanded_location linemap_expand_location (struct line_maps *,
1973 					   const struct line_map *,
1974 					   location_t loc);
1975 
1976 /* Statistics about maps allocation and usage as returned by
1977    linemap_get_statistics.  */
1978 struct linemap_stats
1979 {
1980   long num_ordinary_maps_allocated;
1981   long num_ordinary_maps_used;
1982   long ordinary_maps_allocated_size;
1983   long ordinary_maps_used_size;
1984   long num_expanded_macros;
1985   long num_macro_tokens;
1986   long num_macro_maps_used;
1987   long macro_maps_allocated_size;
1988   long macro_maps_used_size;
1989   long macro_maps_locations_size;
1990   long duplicated_macro_maps_locations_size;
1991   long adhoc_table_size;
1992   long adhoc_table_entries_used;
1993 };
1994 
1995 /* Return the highest location emitted for a given file for which
1996    there is a line map in SET.  FILE_NAME is the file name to
1997    consider.  If the function returns TRUE, *LOC is set to the highest
1998    location emitted for that file.  */
1999 bool linemap_get_file_highest_location (struct line_maps * set,
2000 					const char *file_name,
2001 					location_t *loc);
2002 
2003 /* Compute and return statistics about the memory consumption of some
2004    parts of the line table SET.  */
2005 void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
2006 
2007 /* Dump debugging information about source location LOC into the file
2008    stream STREAM. SET is the line map set LOC comes from.  */
2009 void linemap_dump_location (struct line_maps *, location_t, FILE *);
2010 
2011 /* Dump line map at index IX in line table SET to STREAM.  If STREAM
2012    is NULL, use stderr.  IS_MACRO is true if the caller wants to
2013    dump a macro map, false otherwise.  */
2014 void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
2015 
2016 /* Dump line table SET to STREAM.  If STREAM is NULL, stderr is used.
2017    NUM_ORDINARY specifies how many ordinary maps to dump.  NUM_MACRO
2018    specifies how many macro maps to dump.  */
2019 void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
2020 
2021 /* An enum for distinguishing the various parts within a location_t.  */
2022 
2023 enum location_aspect
2024 {
2025   LOCATION_ASPECT_CARET,
2026   LOCATION_ASPECT_START,
2027   LOCATION_ASPECT_FINISH
2028 };
2029 
2030 /* The rich_location class requires a way to expand location_t instances.
2031    We would directly use expand_location_to_spelling_point, which is
2032    implemented in gcc/input.c, but we also need to use it for rich_location
2033    within genmatch.c.
2034    Hence we require client code of libcpp to implement the following
2035    symbol.  */
2036 extern expanded_location
2037 linemap_client_expand_location_to_spelling_point (location_t,
2038 						  enum location_aspect);
2039 
2040 #endif /* !LIBCPP_LINE_MAP_H  */
2041