1 /* Map (unsigned int) keys to (source file, line, column) triples.
2    Copyright (C) 2001-2016 Free Software Foundation, Inc.
3 
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING3.  If not see
16 <http://www.gnu.org/licenses/>.
17 
18  In other words, you are welcome to use, share and improve this program.
19  You are forbidden to forbid anyone else to use, share and improve
20  what you give them.   Help stamp out software-hoarding!  */
21 
22 #ifndef LIBCPP_LINE_MAP_H
23 #define LIBCPP_LINE_MAP_H
24 
25 #ifndef GTY
26 #define GTY(x) /* nothing */
27 #endif
28 
29 /* Reason for creating a new line map with linemap_add.  LC_ENTER is
30    when including a new file, e.g. a #include directive in C.
31    LC_LEAVE is when reaching a file's end.  LC_RENAME is when a file
32    name or line number changes for neither of the above reasons
33    (e.g. a #line directive in C); LC_RENAME_VERBATIM is like LC_RENAME
34    but a filename of "" is not specially interpreted as standard
35    input. LC_ENTER_MACRO is when a macro expansion is about to start.  */
36 enum lc_reason
37 {
38   LC_ENTER = 0,
39   LC_LEAVE,
40   LC_RENAME,
41   LC_RENAME_VERBATIM,
42   LC_ENTER_MACRO
43   /* FIXME: add support for stringize and paste.  */
44 };
45 
46 /* The type of line numbers.  */
47 typedef unsigned int linenum_type;
48 
49 /* The typedef "source_location" is a key within the location database,
50    identifying a source location or macro expansion, along with range
51    information, and (optionally) a pointer for use by gcc.
52 
53    This key only has meaning in relation to a line_maps instance.  Within
54    gcc there is a single line_maps instance: "line_table", declared in
55    gcc/input.h and defined in gcc/input.c.
56 
57    The values of the keys are intended to be internal to libcpp,
58    but for ease-of-understanding the implementation, they are currently
59    assigned as follows:
60 
61   Actual     | Value                         | Meaning
62   -----------+-------------------------------+-------------------------------
63   0x00000000 | UNKNOWN_LOCATION (gcc/input.h)| Unknown/invalid location.
64   -----------+-------------------------------+-------------------------------
65   0x00000001 | BUILTINS_LOCATION             | The location for declarations
66              |   (gcc/input.h)               | in "<built-in>"
67   -----------+-------------------------------+-------------------------------
68   0x00000002 | RESERVED_LOCATION_COUNT       | The first location to be
69              | (also                         | handed out, and the
70              |  ordmap[0]->start_location)   | first line in ordmap 0
71   -----------+-------------------------------+-------------------------------
72              | ordmap[1]->start_location     | First line in ordmap 1
73              | ordmap[1]->start_location+32  | First column in that line
74              |   (assuming range_bits == 5)  |
75              | ordmap[1]->start_location+64  | 2nd column in that line
76              | ordmap[1]->start_location+4096| Second line in ordmap 1
77              |   (assuming column_bits == 12)
78              |
79              |   Subsequent lines are offset by (1 << column_bits),
80              |   e.g. 4096 for 12 bits, with a column value of 0 representing
81              |   "the whole line".
82              |
83              |   Within a line, the low "range_bits" (typically 5) are used for
84              |   storing short ranges, so that there's an offset of
85              |     (1 << range_bits) between individual columns within a line,
86              |   typically 32.
87              |   The low range_bits store the offset of the end point from the
88              |   start point, and the start point is found by masking away
89              |   the range bits.
90              |
91              |   For example:
92              |      ordmap[1]->start_location+64    "2nd column in that line"
93              |   above means a caret at that location, with a range
94              |   starting and finishing at the same place (the range bits
95              |   are 0), a range of length 1.
96              |
97              |   By contrast:
98              |      ordmap[1]->start_location+68
99              |   has range bits 0x4, meaning a caret with a range starting at
100              |   that location, but with endpoint 4 columns further on: a range
101              |   of length 5.
102              |
103              |   Ranges that have caret != start, or have an endpoint too
104              |   far away to fit in range_bits are instead stored as ad-hoc
105              |   locations.  Hence for range_bits == 5 we can compactly store
106              |   tokens of length <= 32 without needing to use the ad-hoc
107              |   table.
108              |
109              |   This packing scheme means we effectively have
110              |     (column_bits - range_bits)
111              |   of bits for the columns, typically (12 - 5) = 7, for 128
112              |   columns; longer line widths are accomodated by starting a
113              |   new ordmap with a higher column_bits.
114              |
115              | ordmap[2]->start_location-1   | Final location in ordmap 1
116   -----------+-------------------------------+-------------------------------
117              | ordmap[2]->start_location     | First line in ordmap 2
118              | ordmap[3]->start_location-1   | Final location in ordmap 2
119   -----------+-------------------------------+-------------------------------
120              |                               | (etc)
121   -----------+-------------------------------+-------------------------------
122              | ordmap[n-1]->start_location   | First line in final ord map
123              |                               | (etc)
124              | set->highest_location - 1     | Final location in that ordmap
125   -----------+-------------------------------+-------------------------------
126              | set->highest_location         | Location of the where the next
127              |                               | ordinary linemap would start
128   -----------+-------------------------------+-------------------------------
129              |                               |
130              |                  VVVVVVVVVVVVVVVVVVVVVVVVVVV
131              |                  Ordinary maps grow this way
132              |
133              |                    (unallocated integers)
134              |
135   0x60000000 | LINE_MAP_MAX_LOCATION_WITH_COLS
136              |   Beyond this point, ordinary linemaps have 0 bits per column:
137              |   each increment of the value corresponds to a new source line.
138              |
139   0x70000000 | LINE_MAP_MAX_SOURCE_LOCATION
140              |   Beyond the point, we give up on ordinary maps; attempts to
141              |   create locations in them lead to UNKNOWN_LOCATION (0).
142              |
143              |                    (unallocated integers)
144              |
145              |                   Macro maps grow this way
146              |                   ^^^^^^^^^^^^^^^^^^^^^^^^
147              |                               |
148   -----------+-------------------------------+-------------------------------
149              | LINEMAPS_MACRO_LOWEST_LOCATION| Locations within macro maps
150              | macromap[m-1]->start_location | Start of last macro map
151              |                               |
152   -----------+-------------------------------+-------------------------------
153              | macromap[m-2]->start_location | Start of penultimate macro map
154   -----------+-------------------------------+-------------------------------
155              | macromap[1]->start_location   | Start of macro map 1
156   -----------+-------------------------------+-------------------------------
157              | macromap[0]->start_location   | Start of macro map 0
158   0x7fffffff | MAX_SOURCE_LOCATION           | Also used as a mask for
159              |                               | accessing the ad-hoc data table
160   -----------+-------------------------------+-------------------------------
161   0x80000000 | Start of ad-hoc values; the lower 31 bits are used as an index
162   ...        | into the line_table->location_adhoc_data_map.data array.
163   0xffffffff | UINT_MAX                      |
164   -----------+-------------------------------+-------------------------------
165 
166    Examples of location encoding.
167 
168    Packed ranges
169    =============
170 
171    Consider encoding the location of a token "foo", seen underlined here
172    on line 523, within an ordinary line_map that starts at line 500:
173 
174                  11111111112
175         12345678901234567890
176      522
177      523   return foo + bar;
178                   ^~~
179      524
180 
181    The location's caret and start are both at line 523, column 11; the
182    location's finish is on the same line, at column 13 (an offset of 2
183    columns, for length 3).
184 
185    Line 523 is offset 23 from the starting line of the ordinary line_map.
186 
187    caret == start, and the offset of the finish fits within 5 bits, so
188    this can be stored as a packed range.
189 
190    This is encoded as:
191       ordmap->start
192          + (line_offset << ordmap->m_column_and_range_bits)
193          + (column << ordmap->m_range_bits)
194          + (range_offset);
195    i.e. (for line offset 23, column 11, range offset 2):
196       ordmap->start
197          + (23 << 12)
198          + (11 << 5)
199          + 2;
200    i.e.:
201       ordmap->start + 0x17162
202    assuming that the line_map uses the default of 7 bits for columns and
203    5 bits for packed range (giving 12 bits for m_column_and_range_bits).
204 
205 
206    "Pure" locations
207    ================
208 
209    These are a special case of the above, where
210       caret == start == finish
211    They are stored as packed ranges with offset == 0.
212    For example, the location of the "f" of "foo" could be stored
213    as above, but with range offset 0, giving:
214       ordmap->start
215          + (23 << 12)
216          + (11 << 5)
217          + 0;
218    i.e.:
219       ordmap->start + 0x17160
220 
221 
222    Unoptimized ranges
223    ==================
224 
225    Consider encoding the location of the binary expression
226    below:
227 
228                  11111111112
229         12345678901234567890
230      521
231      523   return foo + bar;
232                   ~~~~^~~~~
233      523
234 
235    The location's caret is at the "+", line 523 column 15, but starts
236    earlier, at the "f" of "foo" at column 11.  The finish is at the "r"
237    of "bar" at column 19.
238 
239    This can't be stored as a packed range since start != caret.
240    Hence it is stored as an ad-hoc location e.g. 0x80000003.
241 
242    Stripping off the top bit gives us an index into the ad-hoc
243    lookaside table:
244 
245      line_table->location_adhoc_data_map.data[0x3]
246 
247    from which the caret, start and finish can be looked up,
248    encoded as "pure" locations:
249 
250      start  == ordmap->start + (23 << 12) + (11 << 5)
251             == ordmap->start + 0x17160  (as above; the "f" of "foo")
252 
253      caret  == ordmap->start + (23 << 12) + (15 << 5)
254             == ordmap->start + 0x171e0
255 
256      finish == ordmap->start + (23 << 12) + (19 << 5)
257             == ordmap->start + 0x17260
258 
259    To further see how source_location works in practice, see the
260    worked example in libcpp/location-example.txt.  */
261 typedef unsigned int source_location;
262 
263 /* A range of source locations.
264 
265    Ranges are closed:
266    m_start is the first location within the range,
267    m_finish is the last location within the range.
268 
269    We may need a more compact way to store these, but for now,
270    let's do it the simple way, as a pair.  */
271 struct GTY(()) source_range
272 {
273   source_location m_start;
274   source_location m_finish;
275 
276   /* We avoid using constructors, since various structs that
277      don't yet have constructors will embed instances of
278      source_range.  */
279 
280   /* Make a source_range from a source_location.  */
from_locationsource_range281   static source_range from_location (source_location loc)
282   {
283     source_range result;
284     result.m_start = loc;
285     result.m_finish = loc;
286     return result;
287   }
288 
289   /* Is there any part of this range on the given line?  */
290   bool intersects_line_p (const char *file, int line) const;
291 };
292 
293 /* Memory allocation function typedef.  Works like xrealloc.  */
294 typedef void *(*line_map_realloc) (void *, size_t);
295 
296 /* Memory allocator function that returns the actual allocated size,
297    for a given requested allocation.  */
298 typedef size_t (*line_map_round_alloc_size_func) (size_t);
299 
300 /* A line_map encodes a sequence of locations.
301    There are two kinds of maps. Ordinary maps and macro expansion
302    maps, a.k.a macro maps.
303 
304    A macro map encodes source locations of tokens that are part of a
305    macro replacement-list, at a macro expansion point. E.g, in:
306 
307             #define PLUS(A,B) A + B
308 
309    No macro map is going to be created there, because we are not at a
310    macro expansion point. We are at a macro /definition/ point. So the
311    locations of the tokens of the macro replacement-list (i.e, A + B)
312    will be locations in an ordinary map, not a macro map.
313 
314    On the other hand, if we later do:
315 
316         int a = PLUS (1,2);
317 
318    The invocation of PLUS here is a macro expansion. So we are at a
319    macro expansion point. The preprocessor expands PLUS (1,2) and
320    replaces it with the tokens of its replacement-list: 1 + 2. A macro
321    map is going to be created to hold (or rather to map, haha ...) the
322    locations of the tokens 1, + and 2. The macro map also records the
323    location of the expansion point of PLUS. That location is mapped in
324    the map that is active right before the location of the invocation
325    of PLUS.  */
326 struct GTY((tag ("0"), desc ("%h.reason == LC_ENTER_MACRO ? 2 : 1"))) line_map {
327   source_location start_location;
328 
329   /* The reason for creation of this line map.  */
330   ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
331 };
332 
333 /* An ordinary line map encodes physical source locations. Those
334    physical source locations are called "spelling locations".
335 
336    Physical source file TO_FILE at line TO_LINE at column 0 is represented
337    by the logical START_LOCATION.  TO_LINE+L at column C is represented by
338    START_LOCATION+(L*(1<<m_column_and_range_bits))+(C*1<<m_range_bits), as
339    long as C<(1<<effective range bits), and the result_location is less than
340    the next line_map's start_location.
341    (The top line is line 1 and the leftmost column is column 1; line/column 0
342    means "entire file/line" or "unknown line/column" or "not applicable".)
343 
344    The highest possible source location is MAX_SOURCE_LOCATION.  */
345 struct GTY((tag ("1"))) line_map_ordinary : public line_map {
346   const char *to_file;
347   linenum_type to_line;
348 
349   /* An index into the set that gives the line mapping at whose end
350      the current one was included.  File(s) at the bottom of the
351      include stack have this set to -1.  */
352   int included_from;
353 
354   /* SYSP is one for a system header, two for a C system header file
355      that therefore needs to be extern "C" protected in C++, and zero
356      otherwise.  This field isn't really needed now that it's in
357      cpp_buffer.  */
358   unsigned char sysp;
359 
360   /* Number of the low-order source_location bits used for column numbers
361      and ranges.  */
362   unsigned int m_column_and_range_bits : 8;
363 
364   /* Number of the low-order "column" bits used for storing short ranges
365      inline, rather than in the ad-hoc table.
366      MSB                                                                 LSB
367      31                                                                    0
368      +-------------------------+-------------------------------------------+
369      |                         |<---map->column_and_range_bits (e.g. 12)-->|
370      +-------------------------+-----------------------+-------------------+
371      |                         | column_and_range_bits | map->range_bits   |
372      |                         |   - range_bits        |                   |
373      +-------------------------+-----------------------+-------------------+
374      | row bits                | effective column bits | short range bits  |
375      |                         |    (e.g. 7)           |   (e.g. 5)        |
376      +-------------------------+-----------------------+-------------------+ */
377   unsigned int m_range_bits : 8;
378 };
379 
380 /* This is the highest possible source location encoded within an
381    ordinary or macro map.  */
382 const source_location MAX_SOURCE_LOCATION = 0x7FFFFFFF;
383 
384 struct cpp_hashnode;
385 
386 /* A macro line map encodes location of tokens coming from a macro
387    expansion.
388 
389    The offset from START_LOCATION is used to index into
390    MACRO_LOCATIONS; this holds the original location of the token.  */
391 struct GTY((tag ("2"))) line_map_macro : public line_map {
392   /* The cpp macro which expansion gave birth to this macro map.  */
393   struct cpp_hashnode * GTY ((nested_ptr (union tree_node,
394 				   "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
395 				   "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
396     macro;
397 
398   /* The number of tokens inside the replacement-list of MACRO.  */
399   unsigned int n_tokens;
400 
401   /* This array of location is actually an array of pairs of
402      locations. The elements inside it thus look like:
403 
404            x0,y0, x1,y1, x2,y2, ...., xn,yn.
405 
406      where n == n_tokens;
407 
408      Remember that these xI,yI are collected when libcpp is about to
409      expand a given macro.
410 
411      yI is the location in the macro definition, either of the token
412      itself or of a macro parameter that it replaces.
413 
414      Imagine this:
415 
416 	#define PLUS(A, B) A + B  <--- #1
417 
418 	int a = PLUS (1,2); <--- #2
419 
420      There is a macro map for the expansion of PLUS in #2.  PLUS is
421      expanded into its expansion-list.  The expansion-list is the
422      replacement-list of PLUS where the macro parameters are replaced
423      with their arguments.  So the replacement-list of PLUS is made of
424      the tokens:
425 
426         A, +, B
427 
428      and the expansion-list is made of the tokens:
429 
430         1, +, 2
431 
432      Let's consider the case of token "+".  Its y1 [yI for I == 1] is
433      its spelling location in #1.
434 
435      y0 (thus for token "1") is the spelling location of A in #1.
436 
437      And y2 (of token "2") is the spelling location of B in #1.
438 
439      When the token is /not/ an argument for a macro, xI is the same
440      location as yI.  Otherwise, xI is the location of the token
441      outside this macro expansion.  If this macro was expanded from
442      another macro expansion, xI is a virtual location representing
443      the token in that macro expansion; otherwise, it is the spelling
444      location of the token.
445 
446      Note that a virtual location is a location returned by
447      linemap_add_macro_token.  It encodes the relevant locations (x,y
448      pairs) of that token across the macro expansions from which it
449      (the token) might come from.
450 
451      In the example above x1 (for token "+") is going to be the same
452      as y1.  x0 is the spelling location for the argument token "1",
453      and x2 is the spelling location for the argument token "2".  */
454   source_location * GTY((atomic)) macro_locations;
455 
456   /* This is the location of the expansion point of the current macro
457      map.  It's the location of the macro name.  That location is held
458      by the map that was current right before the current one. It
459      could have been either a macro or an ordinary map, depending on
460      if we are in a nested expansion context not.  */
461   source_location expansion;
462 };
463 
464 #if CHECKING_P && (GCC_VERSION >= 2007)
465 
466 /* Assertion macro to be used in line-map code.  */
467 #define linemap_assert(EXPR)                  \
468   do {                                                \
469     if (! (EXPR))                             \
470       abort ();                                       \
471   } while (0)
472 
473 /* Assert that becomes a conditional expression when checking is disabled at
474    compilation time.  Use this for conditions that should not happen but if
475    they happen, it is better to handle them gracefully rather than crash
476    randomly later.
477    Usage:
478 
479    if (linemap_assert_fails(EXPR)) handle_error(); */
480 #define linemap_assert_fails(EXPR) __extension__ \
481   ({linemap_assert (EXPR); false;})
482 
483 #else
484 /* Include EXPR, so that unused variable warnings do not occur.  */
485 #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
486 #define linemap_assert_fails(EXPR) (! (EXPR))
487 #endif
488 
489 /* Return TRUE if MAP encodes locations coming from a macro
490    replacement-list at macro expansion point.  */
491 bool
492 linemap_macro_expansion_map_p (const struct line_map *);
493 
494 /* Assert that MAP encodes locations of tokens that are not part of
495    the replacement-list of a macro expansion, downcasting from
496    line_map * to line_map_ordinary *.  */
497 
498 inline line_map_ordinary *
linemap_check_ordinary(struct line_map * map)499 linemap_check_ordinary (struct line_map *map)
500 {
501   linemap_assert (!linemap_macro_expansion_map_p (map));
502   return (line_map_ordinary *)map;
503 }
504 
505 /* Assert that MAP encodes locations of tokens that are not part of
506    the replacement-list of a macro expansion, downcasting from
507    const line_map * to const line_map_ordinary *.  */
508 
509 inline const line_map_ordinary *
linemap_check_ordinary(const struct line_map * map)510 linemap_check_ordinary (const struct line_map *map)
511 {
512   linemap_assert (!linemap_macro_expansion_map_p (map));
513   return (const line_map_ordinary *)map;
514 }
515 
516 /* Assert that MAP is a macro expansion and downcast to the appropriate
517    subclass.  */
518 
linemap_check_macro(line_map * map)519 inline line_map_macro *linemap_check_macro (line_map *map)
520 {
521   linemap_assert (linemap_macro_expansion_map_p (map));
522   return (line_map_macro *)map;
523 }
524 
525 /* Assert that MAP is a macro expansion and downcast to the appropriate
526    subclass.  */
527 
528 inline const line_map_macro *
linemap_check_macro(const line_map * map)529 linemap_check_macro (const line_map *map)
530 {
531   linemap_assert (linemap_macro_expansion_map_p (map));
532   return (const line_map_macro *)map;
533 }
534 
535 /* Read the start location of MAP.  */
536 
537 inline source_location
MAP_START_LOCATION(const line_map * map)538 MAP_START_LOCATION (const line_map *map)
539 {
540   return map->start_location;
541 }
542 
543 /* Get the starting line number of ordinary map MAP.  */
544 
545 inline linenum_type
ORDINARY_MAP_STARTING_LINE_NUMBER(const line_map_ordinary * ord_map)546 ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map_ordinary *ord_map)
547 {
548   return ord_map->to_line;
549 }
550 
551 /* Get the index of the ordinary map at whose end
552    ordinary map MAP was included.
553 
554    File(s) at the bottom of the include stack have this set.  */
555 
556 inline int
ORDINARY_MAP_INCLUDER_FILE_INDEX(const line_map_ordinary * ord_map)557 ORDINARY_MAP_INCLUDER_FILE_INDEX (const line_map_ordinary *ord_map)
558 {
559   return ord_map->included_from;
560 }
561 
562 /* Return a positive value if map encodes locations from a system
563    header, 0 otherwise. Returns 1 if ordinary map MAP encodes locations
564    in a system header and 2 if it encodes locations in a C system header
565    that therefore needs to be extern "C" protected in C++.  */
566 
567 inline unsigned char
ORDINARY_MAP_IN_SYSTEM_HEADER_P(const line_map_ordinary * ord_map)568 ORDINARY_MAP_IN_SYSTEM_HEADER_P (const line_map_ordinary *ord_map)
569 {
570   return ord_map->sysp;
571 }
572 
573 /* Get the filename of ordinary map MAP.  */
574 
575 inline const char *
ORDINARY_MAP_FILE_NAME(const line_map_ordinary * ord_map)576 ORDINARY_MAP_FILE_NAME (const line_map_ordinary *ord_map)
577 {
578   return ord_map->to_file;
579 }
580 
581 /* Get the cpp macro whose expansion gave birth to macro map MAP.  */
582 
583 inline cpp_hashnode *
MACRO_MAP_MACRO(const line_map_macro * macro_map)584 MACRO_MAP_MACRO (const line_map_macro *macro_map)
585 {
586   return macro_map->macro;
587 }
588 
589 /* Get the number of tokens inside the replacement-list of the macro
590    that led to macro map MAP.  */
591 
592 inline unsigned int
MACRO_MAP_NUM_MACRO_TOKENS(const line_map_macro * macro_map)593 MACRO_MAP_NUM_MACRO_TOKENS (const line_map_macro *macro_map)
594 {
595   return macro_map->n_tokens;
596 }
597 
598 /* Get the array of pairs of locations within macro map MAP.
599    See the declaration of line_map_macro for more information.  */
600 
601 inline source_location *
MACRO_MAP_LOCATIONS(const line_map_macro * macro_map)602 MACRO_MAP_LOCATIONS (const line_map_macro *macro_map)
603 {
604   return macro_map->macro_locations;
605 }
606 
607 /* Get the location of the expansion point of the macro map MAP.  */
608 
609 inline source_location
MACRO_MAP_EXPANSION_POINT_LOCATION(const line_map_macro * macro_map)610 MACRO_MAP_EXPANSION_POINT_LOCATION (const line_map_macro *macro_map)
611 {
612   return macro_map->expansion;
613 }
614 
615 /* The abstraction of a set of location maps. There can be several
616    types of location maps. This abstraction contains the attributes
617    that are independent from the type of the map.
618 
619    Essentially this is just a vector of T_linemap_subclass,
620    which can only ever grow in size.  */
621 
622 struct GTY(()) maps_info_ordinary {
623   /* This array contains the "ordinary" line maps, for all
624      events other than macro expansion
625      (e.g. when a new preprocessing unit starts or ends).  */
626   line_map_ordinary * GTY ((length ("%h.used"))) maps;
627 
628   /* The total number of allocated maps.  */
629   unsigned int allocated;
630 
631   /* The number of elements used in maps. This number is smaller
632      or equal to ALLOCATED.  */
633   unsigned int used;
634 
635   unsigned int cache;
636 };
637 
638 struct GTY(()) maps_info_macro {
639   /* This array contains the macro line maps.
640      A macro line map is created whenever a macro expansion occurs.  */
641   line_map_macro * GTY ((length ("%h.used"))) maps;
642 
643   /* The total number of allocated maps.  */
644   unsigned int allocated;
645 
646   /* The number of elements used in maps. This number is smaller
647      or equal to ALLOCATED.  */
648   unsigned int used;
649 
650   unsigned int cache;
651 };
652 
653 /* Data structure to associate a source_range together with an arbitrary
654    data pointer with a source location.  */
655 struct GTY(()) location_adhoc_data {
656   source_location locus;
657   source_range src_range;
658   void * GTY((skip)) data;
659 };
660 
661 struct htab;
662 
663 /* The following data structure encodes a location with some adhoc data
664    and maps it to a new unsigned integer (called an adhoc location)
665    that replaces the original location to represent the mapping.
666 
667    The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
668    highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
669    the original location. Once identified as the adhoc_loc, the lower 31
670    bits of the integer is used to index the location_adhoc_data array,
671    in which the locus and associated data is stored.  */
672 
673 struct GTY(()) location_adhoc_data_map {
674   struct htab * GTY((skip)) htab;
675   source_location curr_loc;
676   unsigned int allocated;
677   struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
678 };
679 
680 /* A set of chronological line_map structures.  */
681 struct GTY(()) line_maps {
682 
683   maps_info_ordinary info_ordinary;
684 
685   maps_info_macro info_macro;
686 
687   /* Depth of the include stack, including the current file.  */
688   unsigned int depth;
689 
690   /* If true, prints an include trace a la -H.  */
691   bool trace_includes;
692 
693   /* Highest source_location "given out".  */
694   source_location highest_location;
695 
696   /* Start of line of highest source_location "given out".  */
697   source_location highest_line;
698 
699   /* The maximum column number we can quickly allocate.  Higher numbers
700      may require allocating a new line_map.  */
701   unsigned int max_column_hint;
702 
703   /* If non-null, the allocator to use when resizing 'maps'.  If null,
704      xrealloc is used.  */
705   line_map_realloc reallocator;
706 
707   /* The allocators' function used to know the actual size it
708      allocated, for a certain allocation size requested.  */
709   line_map_round_alloc_size_func round_alloc_size;
710 
711   struct location_adhoc_data_map location_adhoc_data_map;
712 
713   /* The special location value that is used as spelling location for
714      built-in tokens.  */
715   source_location builtin_location;
716 
717   /* True if we've seen a #line or # 44 "file" directive.  */
718   bool seen_line_directive;
719 
720   /* The default value of range_bits in ordinary line maps.  */
721   unsigned int default_range_bits;
722 
723   unsigned int num_optimized_ranges;
724   unsigned int num_unoptimized_ranges;
725 };
726 
727 /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
728    if we are interested in macro maps, FALSE otherwise.  */
729 inline unsigned int
LINEMAPS_ALLOCATED(const line_maps * set,bool map_kind)730 LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind)
731 {
732   if (map_kind)
733     return set->info_macro.allocated;
734   else
735     return set->info_ordinary.allocated;
736 }
737 
738 /* As above, but by reference (e.g. as an lvalue).  */
739 
740 inline unsigned int &
LINEMAPS_ALLOCATED(line_maps * set,bool map_kind)741 LINEMAPS_ALLOCATED (line_maps *set, bool map_kind)
742 {
743   if (map_kind)
744     return set->info_macro.allocated;
745   else
746     return set->info_ordinary.allocated;
747 }
748 
749 /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
750    we are interested in macro maps, FALSE otherwise.*/
751 inline unsigned int
LINEMAPS_USED(const line_maps * set,bool map_kind)752 LINEMAPS_USED (const line_maps *set, bool map_kind)
753 {
754   if (map_kind)
755     return set->info_macro.used;
756   else
757     return set->info_ordinary.used;
758 }
759 
760 /* As above, but by reference (e.g. as an lvalue).  */
761 
762 inline unsigned int &
LINEMAPS_USED(line_maps * set,bool map_kind)763 LINEMAPS_USED (line_maps *set, bool map_kind)
764 {
765   if (map_kind)
766     return set->info_macro.used;
767   else
768     return set->info_ordinary.used;
769 }
770 
771 /* Returns the index of the last map that was looked up with
772    linemap_lookup. MAP_KIND shall be TRUE if we are interested in
773    macro maps, FALSE otherwise.  */
774 inline unsigned int
LINEMAPS_CACHE(const line_maps * set,bool map_kind)775 LINEMAPS_CACHE (const line_maps *set, bool map_kind)
776 {
777   if (map_kind)
778     return set->info_macro.cache;
779   else
780     return set->info_ordinary.cache;
781 }
782 
783 /* As above, but by reference (e.g. as an lvalue).  */
784 
785 inline unsigned int &
LINEMAPS_CACHE(line_maps * set,bool map_kind)786 LINEMAPS_CACHE (line_maps *set, bool map_kind)
787 {
788   if (map_kind)
789     return set->info_macro.cache;
790   else
791     return set->info_ordinary.cache;
792 }
793 
794 /* Return the map at a given index.  */
795 inline line_map *
LINEMAPS_MAP_AT(const line_maps * set,bool map_kind,int index)796 LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, int index)
797 {
798   if (map_kind)
799     return &set->info_macro.maps[index];
800   else
801     return &set->info_ordinary.maps[index];
802 }
803 
804 /* Returns the last map used in the line table SET. MAP_KIND
805    shall be TRUE if we are interested in macro maps, FALSE
806    otherwise.*/
807 inline line_map *
LINEMAPS_LAST_MAP(const line_maps * set,bool map_kind)808 LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind)
809 {
810   return LINEMAPS_MAP_AT (set, map_kind,
811 			  LINEMAPS_USED (set, map_kind) - 1);
812 }
813 
814 /* Returns the last map that was allocated in the line table SET.
815    MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
816    otherwise.*/
817 inline line_map *
LINEMAPS_LAST_ALLOCATED_MAP(const line_maps * set,bool map_kind)818 LINEMAPS_LAST_ALLOCATED_MAP (const line_maps *set, bool map_kind)
819 {
820   return LINEMAPS_MAP_AT (set, map_kind,
821 			  LINEMAPS_ALLOCATED (set, map_kind) - 1);
822 }
823 
824 /* Returns a pointer to the memory region where ordinary maps are
825    allocated in the line table SET.  */
826 inline line_map_ordinary *
LINEMAPS_ORDINARY_MAPS(const line_maps * set)827 LINEMAPS_ORDINARY_MAPS (const line_maps *set)
828 {
829   return set->info_ordinary.maps;
830 }
831 
832 /* Returns the INDEXth ordinary map.  */
833 inline line_map_ordinary *
LINEMAPS_ORDINARY_MAP_AT(const line_maps * set,int index)834 LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, int index)
835 {
836   linemap_assert (index >= 0);
837   linemap_assert ((unsigned int)index < set->info_ordinary.used);
838   return &set->info_ordinary.maps[index];
839 }
840 
841 /* Return the number of ordinary maps allocated in the line table
842    SET.  */
843 inline unsigned int
LINEMAPS_ORDINARY_ALLOCATED(const line_maps * set)844 LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set)
845 {
846   return LINEMAPS_ALLOCATED (set, false);
847 }
848 
849 /* Return the number of ordinary maps used in the line table SET.  */
850 inline unsigned int
LINEMAPS_ORDINARY_USED(const line_maps * set)851 LINEMAPS_ORDINARY_USED (const line_maps *set)
852 {
853   return LINEMAPS_USED (set, false);
854 }
855 
856 /* Return the index of the last ordinary map that was looked up with
857    linemap_lookup.  */
858 inline unsigned int
LINEMAPS_ORDINARY_CACHE(const line_maps * set)859 LINEMAPS_ORDINARY_CACHE (const line_maps *set)
860 {
861   return LINEMAPS_CACHE (set, false);
862 }
863 
864 /* As above, but by reference (e.g. as an lvalue).  */
865 
866 inline unsigned int &
LINEMAPS_ORDINARY_CACHE(line_maps * set)867 LINEMAPS_ORDINARY_CACHE (line_maps *set)
868 {
869   return LINEMAPS_CACHE (set, false);
870 }
871 
872 /* Returns a pointer to the last ordinary map used in the line table
873    SET.  */
874 inline line_map_ordinary *
LINEMAPS_LAST_ORDINARY_MAP(const line_maps * set)875 LINEMAPS_LAST_ORDINARY_MAP (const line_maps *set)
876 {
877   return (line_map_ordinary *)LINEMAPS_LAST_MAP (set, false);
878 }
879 
880 /* Returns a pointer to the last ordinary map allocated the line table
881    SET.  */
882 inline line_map_ordinary *
LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP(const line_maps * set)883 LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP (const line_maps *set)
884 {
885   return (line_map_ordinary *)LINEMAPS_LAST_ALLOCATED_MAP (set, false);
886 }
887 
888 /* Returns a pointer to the beginning of the region where macro maps
889    are allcoated.  */
890 inline line_map_macro *
LINEMAPS_MACRO_MAPS(const line_maps * set)891 LINEMAPS_MACRO_MAPS (const line_maps *set)
892 {
893   return set->info_macro.maps;
894 }
895 
896 /* Returns the INDEXth macro map.  */
897 inline line_map_macro *
LINEMAPS_MACRO_MAP_AT(const line_maps * set,int index)898 LINEMAPS_MACRO_MAP_AT (const line_maps *set, int index)
899 {
900   linemap_assert (index >= 0);
901   linemap_assert ((unsigned int)index < set->info_macro.used);
902   return &set->info_macro.maps[index];
903 }
904 
905 /* Returns the number of macro maps that were allocated in the line
906    table SET.  */
907 inline unsigned int
LINEMAPS_MACRO_ALLOCATED(const line_maps * set)908 LINEMAPS_MACRO_ALLOCATED (const line_maps *set)
909 {
910   return LINEMAPS_ALLOCATED (set, true);
911 }
912 
913 /* Returns the number of macro maps used in the line table SET.  */
914 inline unsigned int
LINEMAPS_MACRO_USED(const line_maps * set)915 LINEMAPS_MACRO_USED (const line_maps *set)
916 {
917   return LINEMAPS_USED (set, true);
918 }
919 
920 /* Returns the index of the last macro map looked up with
921    linemap_lookup.  */
922 inline unsigned int
LINEMAPS_MACRO_CACHE(const line_maps * set)923 LINEMAPS_MACRO_CACHE (const line_maps *set)
924 {
925   return LINEMAPS_CACHE (set, true);
926 }
927 
928 /* As above, but by reference (e.g. as an lvalue).  */
929 
930 inline unsigned int &
LINEMAPS_MACRO_CACHE(line_maps * set)931 LINEMAPS_MACRO_CACHE (line_maps *set)
932 {
933   return LINEMAPS_CACHE (set, true);
934 }
935 
936 /* Returns the last macro map used in the line table SET.  */
937 inline line_map_macro *
LINEMAPS_LAST_MACRO_MAP(const line_maps * set)938 LINEMAPS_LAST_MACRO_MAP (const line_maps *set)
939 {
940   return (line_map_macro *)LINEMAPS_LAST_MAP (set, true);
941 }
942 
943 /* Returns the lowest location [of a token resulting from macro
944    expansion] encoded in this line table.  */
945 inline source_location
LINEMAPS_MACRO_LOWEST_LOCATION(const line_maps * set)946 LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps *set)
947 {
948   return LINEMAPS_MACRO_USED (set)
949          ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))
950          : MAX_SOURCE_LOCATION;
951 }
952 
953 /* Returns the last macro map allocated in the line table SET.  */
954 inline line_map_macro *
LINEMAPS_LAST_ALLOCATED_MACRO_MAP(const line_maps * set)955 LINEMAPS_LAST_ALLOCATED_MACRO_MAP (const line_maps *set)
956 {
957   return (line_map_macro *)LINEMAPS_LAST_ALLOCATED_MAP (set, true);
958 }
959 
960 extern void location_adhoc_data_fini (struct line_maps *);
961 extern source_location get_combined_adhoc_loc (struct line_maps *,
962 					       source_location,
963 					       source_range,
964 					       void *);
965 extern void *get_data_from_adhoc_loc (struct line_maps *, source_location);
966 extern source_location get_location_from_adhoc_loc (struct line_maps *,
967 						    source_location);
968 
969 extern source_range get_range_from_loc (line_maps *set, source_location loc);
970 
971 /* Get whether location LOC is an ad-hoc location.  */
972 
973 inline bool
IS_ADHOC_LOC(source_location loc)974 IS_ADHOC_LOC (source_location loc)
975 {
976   return (loc & MAX_SOURCE_LOCATION) != loc;
977 }
978 
979 /* Get whether location LOC is a "pure" location, or
980    whether it is an ad-hoc location, or embeds range information.  */
981 
982 bool
983 pure_location_p (line_maps *set, source_location loc);
984 
985 /* Combine LOC and BLOCK, giving a combined adhoc location.  */
986 
987 inline source_location
COMBINE_LOCATION_DATA(struct line_maps * set,source_location loc,source_range src_range,void * block)988 COMBINE_LOCATION_DATA (struct line_maps *set,
989 		       source_location loc,
990 		       source_range src_range,
991 		       void *block)
992 {
993   return get_combined_adhoc_loc (set, loc, src_range, block);
994 }
995 
996 extern void rebuild_location_adhoc_htab (struct line_maps *);
997 
998 /* Initialize a line map set.  SET is the line map set to initialize
999    and BUILTIN_LOCATION is the special location value to be used as
1000    spelling location for built-in tokens.  This BUILTIN_LOCATION has
1001    to be strictly less than RESERVED_LOCATION_COUNT.  */
1002 extern void linemap_init (struct line_maps *set,
1003 			  source_location builtin_location);
1004 
1005 /* Check for and warn about line_maps entered but not exited.  */
1006 
1007 extern void linemap_check_files_exited (struct line_maps *);
1008 
1009 /* Return a source_location for the start (i.e. column==0) of
1010    (physical) line TO_LINE in the current source file (as in the
1011    most recent linemap_add).   MAX_COLUMN_HINT is the highest column
1012    number we expect to use in this line (but it does not change
1013    the highest_location).  */
1014 
1015 extern source_location linemap_line_start
1016 (struct line_maps *set, linenum_type to_line,  unsigned int max_column_hint);
1017 
1018 /* Add a mapping of logical source line to physical source file and
1019    line number. This function creates an "ordinary map", which is a
1020    map that records locations of tokens that are not part of macro
1021    replacement-lists present at a macro expansion point.
1022 
1023    The text pointed to by TO_FILE must have a lifetime
1024    at least as long as the lifetime of SET.  An empty
1025    TO_FILE means standard input.  If reason is LC_LEAVE, and
1026    TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
1027    natural values considering the file we are returning to.
1028 
1029    A call to this function can relocate the previous set of
1030    maps, so any stored line_map pointers should not be used.  */
1031 extern const struct line_map *linemap_add
1032   (struct line_maps *, enum lc_reason, unsigned int sysp,
1033    const char *to_file, linenum_type to_line);
1034 
1035 /* Given a logical source location, returns the map which the
1036    corresponding (source file, line, column) triplet can be deduced
1037    from. Since the set is built chronologically, the logical lines are
1038    monotonic increasing, and so the list is sorted and we can use a
1039    binary search. If no line map have been allocated yet, this
1040    function returns NULL.  */
1041 extern const struct line_map *linemap_lookup
1042   (struct line_maps *, source_location);
1043 
1044 /* Returns TRUE if the line table set tracks token locations across
1045    macro expansion, FALSE otherwise.  */
1046 bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
1047 
1048 /* Return the name of the macro associated to MACRO_MAP.  */
1049 const char* linemap_map_get_macro_name (const line_map_macro *);
1050 
1051 /* Return a positive value if LOCATION is the locus of a token that is
1052    located in a system header, O otherwise. It returns 1 if LOCATION
1053    is the locus of a token that is located in a system header, and 2
1054    if LOCATION is the locus of a token located in a C system header
1055    that therefore needs to be extern "C" protected in C++.
1056 
1057    Note that this function returns 1 if LOCATION belongs to a token
1058    that is part of a macro replacement-list defined in a system
1059    header, but expanded in a non-system file.  */
1060 int linemap_location_in_system_header_p (struct line_maps *,
1061 					 source_location);
1062 
1063 /* Return TRUE if LOCATION is a source code location of a token coming
1064    from a macro replacement-list at a macro expansion point, FALSE
1065    otherwise.  */
1066 bool linemap_location_from_macro_expansion_p (const struct line_maps *,
1067 					      source_location);
1068 
1069 /* With the precondition that LOCATION is the locus of a token that is
1070    an argument of a function-like macro MACRO_MAP and appears in the
1071    expansion of MACRO_MAP, return the locus of that argument in the
1072    context of the caller of MACRO_MAP.  */
1073 
1074 extern source_location linemap_macro_map_loc_unwind_toward_spelling
1075   (line_maps *set, const line_map_macro *macro_map, source_location location);
1076 
1077 /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
1078    be reserved for libcpp user as special values, no token from libcpp
1079    will contain any of those locations.  */
1080 const source_location RESERVED_LOCATION_COUNT = 2;
1081 
1082 /* Converts a map and a source_location to source line.  */
1083 inline linenum_type
SOURCE_LINE(const line_map_ordinary * ord_map,source_location loc)1084 SOURCE_LINE (const line_map_ordinary *ord_map, source_location loc)
1085 {
1086   return ((loc - ord_map->start_location)
1087 	  >> ord_map->m_column_and_range_bits) + ord_map->to_line;
1088 }
1089 
1090 /* Convert a map and source_location to source column number.  */
1091 inline linenum_type
SOURCE_COLUMN(const line_map_ordinary * ord_map,source_location loc)1092 SOURCE_COLUMN (const line_map_ordinary *ord_map, source_location loc)
1093 {
1094   return ((loc - ord_map->start_location)
1095 	  & ((1 << ord_map->m_column_and_range_bits) - 1)) >> ord_map->m_range_bits;
1096 }
1097 
1098 /* Return the location of the last source line within an ordinary
1099    map.  */
1100 inline source_location
LAST_SOURCE_LINE_LOCATION(const line_map_ordinary * map)1101 LAST_SOURCE_LINE_LOCATION (const line_map_ordinary *map)
1102 {
1103   return (((map[1].start_location - 1
1104 	    - map->start_location)
1105 	   & ~((1 << map->m_column_and_range_bits) - 1))
1106 	  + map->start_location);
1107 }
1108 
1109 /* Returns the last source line number within an ordinary map.  This
1110    is the (last) line of the #include, or other directive, that caused
1111    a map change.  */
1112 inline linenum_type
LAST_SOURCE_LINE(const line_map_ordinary * map)1113 LAST_SOURCE_LINE (const line_map_ordinary *map)
1114 {
1115   return SOURCE_LINE (map, LAST_SOURCE_LINE_LOCATION (map));
1116 }
1117 
1118 /* Return the last column number within an ordinary map.  */
1119 
1120 inline linenum_type
LAST_SOURCE_COLUMN(const line_map_ordinary * map)1121 LAST_SOURCE_COLUMN (const line_map_ordinary *map)
1122 {
1123   return SOURCE_COLUMN (map, LAST_SOURCE_LINE_LOCATION (map));
1124 }
1125 
1126 /* Returns the map a given map was included from, or NULL if the map
1127    belongs to the main file, i.e, a file that wasn't included by
1128    another one.  */
1129 inline line_map_ordinary *
INCLUDED_FROM(struct line_maps * set,const line_map_ordinary * ord_map)1130 INCLUDED_FROM (struct line_maps *set, const line_map_ordinary *ord_map)
1131 {
1132   return ((ord_map->included_from == -1)
1133 	  ? NULL
1134 	  : LINEMAPS_ORDINARY_MAP_AT (set, ord_map->included_from));
1135 }
1136 
1137 /* True if the map is at the bottom of the include stack.  */
1138 
1139 inline bool
MAIN_FILE_P(const line_map_ordinary * ord_map)1140 MAIN_FILE_P (const line_map_ordinary *ord_map)
1141 {
1142   return ord_map->included_from < 0;
1143 }
1144 
1145 /* Encode and return a source_location from a column number. The
1146    source line considered is the last source line used to call
1147    linemap_line_start, i.e, the last source line which a location was
1148    encoded from.  */
1149 extern source_location
1150 linemap_position_for_column (struct line_maps *, unsigned int);
1151 
1152 /* Encode and return a source location from a given line and
1153    column.  */
1154 source_location
1155 linemap_position_for_line_and_column (line_maps *set,
1156 				      const line_map_ordinary *,
1157 				      linenum_type, unsigned int);
1158 
1159 /* Encode and return a source_location starting from location LOC and
1160    shifting it by OFFSET columns.  This function does not support
1161    virtual locations.  */
1162 source_location
1163 linemap_position_for_loc_and_offset (struct line_maps *set,
1164 				     source_location loc,
1165 				     unsigned int offset);
1166 
1167 /* Return the file this map is for.  */
1168 inline const char *
LINEMAP_FILE(const line_map_ordinary * ord_map)1169 LINEMAP_FILE (const line_map_ordinary *ord_map)
1170 {
1171   return ord_map->to_file;
1172 }
1173 
1174 /* Return the line number this map started encoding location from.  */
1175 inline linenum_type
LINEMAP_LINE(const line_map_ordinary * ord_map)1176 LINEMAP_LINE (const line_map_ordinary *ord_map)
1177 {
1178   return ord_map->to_line;
1179 }
1180 
1181 /* Return a positive value if map encodes locations from a system
1182    header, 0 otherwise. Returns 1 if MAP encodes locations in a
1183    system header and 2 if it encodes locations in a C system header
1184    that therefore needs to be extern "C" protected in C++.  */
1185 inline unsigned char
LINEMAP_SYSP(const line_map_ordinary * ord_map)1186 LINEMAP_SYSP (const line_map_ordinary *ord_map)
1187 {
1188   return ord_map->sysp;
1189 }
1190 
1191 /* Return a positive value if PRE denotes the location of a token that
1192    comes before the token of POST, 0 if PRE denotes the location of
1193    the same token as the token for POST, and a negative value
1194    otherwise.  */
1195 int linemap_compare_locations (struct line_maps *set,
1196 			       source_location   pre,
1197 			       source_location   post);
1198 
1199 /* Return TRUE if LOC_A denotes the location a token that comes
1200    topogically before the token denoted by location LOC_B, or if they
1201    are equal.  */
1202 inline bool
linemap_location_before_p(struct line_maps * set,source_location loc_a,source_location loc_b)1203 linemap_location_before_p (struct line_maps *set,
1204 			   source_location loc_a,
1205 			   source_location loc_b)
1206 {
1207   return linemap_compare_locations (set, loc_a, loc_b) >= 0;
1208 }
1209 
1210 typedef struct
1211 {
1212   /* The name of the source file involved.  */
1213   const char *file;
1214 
1215   /* The line-location in the source file.  */
1216   int line;
1217 
1218   int column;
1219 
1220   void *data;
1221 
1222   /* In a system header?. */
1223   bool sysp;
1224 } expanded_location;
1225 
1226 /* Both gcc and emacs number source *lines* starting at 1, but
1227    they have differing conventions for *columns*.
1228 
1229    GCC uses a 1-based convention for source columns,
1230    whereas Emacs's M-x column-number-mode uses a 0-based convention.
1231 
1232    For example, an error in the initial, left-hand
1233    column of source line 3 is reported by GCC as:
1234 
1235       some-file.c:3:1: error: ...etc...
1236 
1237    On navigating to the location of that error in Emacs
1238    (e.g. via "next-error"),
1239    the locus is reported in the Mode Line
1240    (assuming M-x column-number-mode) as:
1241 
1242      some-file.c   10%   (3, 0)
1243 
1244    i.e. "3:1:" in GCC corresponds to "(3, 0)" in Emacs.  */
1245 
1246 /* A location within a rich_location: a caret&range, with
1247    the caret potentially flagged for display.  */
1248 
1249 struct location_range
1250 {
1251   source_location m_loc;
1252 
1253   /* Should a caret be drawn for this range?  Typically this is
1254      true for the 0th range, and false for subsequent ranges,
1255      but the Fortran frontend overrides this for rendering things like:
1256 
1257        x = x + y
1258            1   2
1259        Error: Shapes for operands at (1) and (2) are not conformable
1260 
1261      where "1" and "2" are notionally carets.  */
1262   bool m_show_caret_p;
1263 };
1264 
1265 class fixit_hint;
1266   class fixit_insert;
1267   class fixit_remove;
1268   class fixit_replace;
1269 
1270 /* A "rich" source code location, for use when printing diagnostics.
1271    A rich_location has one or more carets&ranges, where the carets
1272    are optional.  These are referred to as "ranges" from here.
1273    Typically the zeroth range has a caret; other ranges sometimes
1274    have carets.
1275 
1276    The "primary" location of a rich_location is the caret of range 0,
1277    used for determining the line/column when printing diagnostic
1278    text, such as:
1279 
1280       some-file.c:3:1: error: ...etc...
1281 
1282    Additional ranges may be added to help the user identify other
1283    pertinent clauses in a diagnostic.
1284 
1285    rich_location instances are intended to be allocated on the stack
1286    when generating diagnostics, and to be short-lived.
1287 
1288    Examples of rich locations
1289    --------------------------
1290 
1291    Example A
1292    *********
1293       int i = "foo";
1294               ^
1295    This "rich" location is simply a single range (range 0), with
1296    caret = start = finish at the given point.
1297 
1298    Example B
1299    *********
1300       a = (foo && bar)
1301           ~~~~~^~~~~~~
1302    This rich location has a single range (range 0), with the caret
1303    at the first "&", and the start/finish at the parentheses.
1304    Compare with example C below.
1305 
1306    Example C
1307    *********
1308       a = (foo && bar)
1309            ~~~ ^~ ~~~
1310    This rich location has three ranges:
1311    - Range 0 has its caret and start location at the first "&" and
1312      end at the second "&.
1313    - Range 1 has its start and finish at the "f" and "o" of "foo";
1314      the caret is not flagged for display, but is perhaps at the "f"
1315      of "foo".
1316    - Similarly, range 2 has its start and finish at the "b" and "r" of
1317      "bar"; the caret is not flagged for display, but is perhaps at the
1318      "b" of "bar".
1319    Compare with example B above.
1320 
1321    Example D (Fortran frontend)
1322    ****************************
1323        x = x + y
1324            1   2
1325    This rich location has range 0 at "1", and range 1 at "2".
1326    Both are flagged for caret display.  Both ranges have start/finish
1327    equal to their caret point.  The frontend overrides the diagnostic
1328    context's default caret character for these ranges.
1329 
1330    Example E
1331    *********
1332       printf ("arg0: %i  arg1: %s arg2: %i",
1333                                ^~
1334               100, 101, 102);
1335                    ~~~
1336    This rich location has two ranges:
1337    - range 0 is at the "%s" with start = caret = "%" and finish at
1338      the "s".
1339    - range 1 has start/finish covering the "101" and is not flagged for
1340      caret printing; it is perhaps at the start of "101".  */
1341 
1342 class rich_location
1343 {
1344  public:
1345   /* Constructors.  */
1346 
1347   /* Constructing from a location.  */
1348   rich_location (line_maps *set, source_location loc);
1349 
1350   /* Constructing from a source_range.  */
1351   rich_location (source_range src_range);
1352 
1353   /* Destructor.  */
1354   ~rich_location ();
1355 
1356   /* Accessors.  */
get_loc()1357   source_location get_loc () const { return get_loc (0); }
1358   source_location get_loc (unsigned int idx) const;
1359 
1360   void
1361   add_range (source_location loc,  bool show_caret_p);
1362 
1363   void
1364   set_range (line_maps *set, unsigned int idx, source_location loc,
1365 	     bool show_caret_p);
1366 
get_num_locations()1367   unsigned int get_num_locations () const { return m_num_ranges; }
1368 
get_range(unsigned int idx)1369   location_range *get_range (unsigned int idx)
1370   {
1371     linemap_assert (idx < m_num_ranges);
1372     return &m_ranges[idx];
1373   }
1374 
1375   expanded_location get_expanded_location (unsigned int idx);
1376 
1377   void
1378   override_column (int column);
1379 
1380   /* Fix-it hints.  */
1381   void
1382   add_fixit_insert (source_location where,
1383 		    const char *new_content);
1384 
1385   void
1386   add_fixit_remove (source_range src_range);
1387 
1388   void
1389   add_fixit_replace (source_range src_range,
1390 		     const char *new_content);
1391 
get_num_fixit_hints()1392   unsigned int get_num_fixit_hints () const { return m_num_fixit_hints; }
get_fixit_hint(int idx)1393   fixit_hint *get_fixit_hint (int idx) const { return m_fixit_hints[idx]; }
1394 
1395 public:
1396   static const int MAX_RANGES = 3;
1397   static const int MAX_FIXIT_HINTS = 2;
1398 
1399 protected:
1400   unsigned int m_num_ranges;
1401   location_range m_ranges[MAX_RANGES];
1402 
1403   int m_column_override;
1404 
1405   bool m_have_expanded_location;
1406   expanded_location m_expanded_location;
1407 
1408   unsigned int m_num_fixit_hints;
1409   fixit_hint *m_fixit_hints[MAX_FIXIT_HINTS];
1410 };
1411 
1412 class fixit_hint
1413 {
1414 public:
1415   enum kind {INSERT, REMOVE, REPLACE};
1416 
~fixit_hint()1417   virtual ~fixit_hint () {}
1418 
1419   virtual enum kind get_kind () const = 0;
1420   virtual bool affects_line_p (const char *file, int line) = 0;
1421 };
1422 
1423 class fixit_insert : public fixit_hint
1424 {
1425  public:
1426   fixit_insert (source_location where,
1427 		const char *new_content);
1428   ~fixit_insert ();
get_kind()1429   enum kind get_kind () const { return INSERT; }
1430   bool affects_line_p (const char *file, int line);
1431 
get_location()1432   source_location get_location () const { return m_where; }
get_string()1433   const char *get_string () const { return m_bytes; }
get_length()1434   size_t get_length () const { return m_len; }
1435 
1436  private:
1437   source_location m_where;
1438   char *m_bytes;
1439   size_t m_len;
1440 };
1441 
1442 class fixit_remove : public fixit_hint
1443 {
1444  public:
1445   fixit_remove (source_range src_range);
~fixit_remove()1446   ~fixit_remove () {}
1447 
get_kind()1448   enum kind get_kind () const { return REMOVE; }
1449   bool affects_line_p (const char *file, int line);
1450 
get_range()1451   source_range get_range () const { return m_src_range; }
1452 
1453  private:
1454   source_range m_src_range;
1455 };
1456 
1457 class fixit_replace : public fixit_hint
1458 {
1459  public:
1460   fixit_replace (source_range src_range,
1461                  const char *new_content);
1462   ~fixit_replace ();
1463 
get_kind()1464   enum kind get_kind () const { return REPLACE; }
1465   bool affects_line_p (const char *file, int line);
1466 
get_range()1467   source_range get_range () const { return m_src_range; }
get_string()1468   const char *get_string () const { return m_bytes; }
get_length()1469   size_t get_length () const { return m_len; }
1470 
1471  private:
1472   source_range m_src_range;
1473   char *m_bytes;
1474   size_t m_len;
1475 };
1476 
1477 
1478 /* This is enum is used by the function linemap_resolve_location
1479    below.  The meaning of the values is explained in the comment of
1480    that function.  */
1481 enum location_resolution_kind
1482 {
1483   LRK_MACRO_EXPANSION_POINT,
1484   LRK_SPELLING_LOCATION,
1485   LRK_MACRO_DEFINITION_LOCATION
1486 };
1487 
1488 /* Resolve a virtual location into either a spelling location, an
1489    expansion point location or a token argument replacement point
1490    location.  Return the map that encodes the virtual location as well
1491    as the resolved location.
1492 
1493    If LOC is *NOT* the location of a token resulting from the
1494    expansion of a macro, then the parameter LRK (which stands for
1495    Location Resolution Kind) is ignored and the resulting location
1496    just equals the one given in argument.
1497 
1498    Now if LOC *IS* the location of a token resulting from the
1499    expansion of a macro, this is what happens.
1500 
1501    * If LRK is set to LRK_MACRO_EXPANSION_POINT
1502    -------------------------------
1503 
1504    The virtual location is resolved to the first macro expansion point
1505    that led to this macro expansion.
1506 
1507    * If LRK is set to LRK_SPELLING_LOCATION
1508    -------------------------------------
1509 
1510    The virtual location is resolved to the locus where the token has
1511    been spelled in the source.   This can follow through all the macro
1512    expansions that led to the token.
1513 
1514    * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1515    --------------------------------------
1516 
1517    The virtual location is resolved to the locus of the token in the
1518    context of the macro definition.
1519 
1520    If LOC is the locus of a token that is an argument of a
1521    function-like macro [replacing a parameter in the replacement list
1522    of the macro] the virtual location is resolved to the locus of the
1523    parameter that is replaced, in the context of the definition of the
1524    macro.
1525 
1526    If LOC is the locus of a token that is not an argument of a
1527    function-like macro, then the function behaves as if LRK was set to
1528    LRK_SPELLING_LOCATION.
1529 
1530    If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1531    returned location.  Note that if the returned location wasn't originally
1532    encoded by a map, the *MAP is set to NULL.  This can happen if LOC
1533    resolves to a location reserved for the client code, like
1534    UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC.  */
1535 
1536 source_location linemap_resolve_location (struct line_maps *,
1537 					  source_location loc,
1538 					  enum location_resolution_kind lrk,
1539 					  const line_map_ordinary **loc_map);
1540 
1541 /* Suppose that LOC is the virtual location of a token coming from the
1542    expansion of a macro M.  This function then steps up to get the
1543    location L of the point where M got expanded.  If L is a spelling
1544    location inside a macro expansion M', then this function returns
1545    the point where M' was expanded.  LOC_MAP is an output parameter.
1546    When non-NULL, *LOC_MAP is set to the map of the returned
1547    location.  */
1548 source_location linemap_unwind_toward_expansion (struct line_maps *,
1549 						 source_location loc,
1550 						 const struct line_map **loc_map);
1551 
1552 /* If LOC is the virtual location of a token coming from the expansion
1553    of a macro M and if its spelling location is reserved (e.g, a
1554    location for a built-in token), then this function unwinds (using
1555    linemap_unwind_toward_expansion) the location until a location that
1556    is not reserved and is not in a system header is reached.  In other
1557    words, this unwinds the reserved location until a location that is
1558    in real source code is reached.
1559 
1560    Otherwise, if the spelling location for LOC is not reserved or if
1561    LOC doesn't come from the expansion of a macro, the function
1562    returns LOC as is and *MAP is not touched.
1563 
1564    *MAP is set to the map of the returned location if the later is
1565    different from LOC.  */
1566 source_location linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
1567 							  source_location loc,
1568 							  const struct line_map **map);
1569 
1570 /* Expand source code location LOC and return a user readable source
1571    code location.  LOC must be a spelling (non-virtual) location.  If
1572    it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1573    location is returned.  */
1574 expanded_location linemap_expand_location (struct line_maps *,
1575 					   const struct line_map *,
1576 					   source_location loc);
1577 
1578 /* Statistics about maps allocation and usage as returned by
1579    linemap_get_statistics.  */
1580 struct linemap_stats
1581 {
1582   long num_ordinary_maps_allocated;
1583   long num_ordinary_maps_used;
1584   long ordinary_maps_allocated_size;
1585   long ordinary_maps_used_size;
1586   long num_expanded_macros;
1587   long num_macro_tokens;
1588   long num_macro_maps_used;
1589   long macro_maps_allocated_size;
1590   long macro_maps_used_size;
1591   long macro_maps_locations_size;
1592   long duplicated_macro_maps_locations_size;
1593   long adhoc_table_size;
1594   long adhoc_table_entries_used;
1595 };
1596 
1597 /* Return the highest location emitted for a given file for which
1598    there is a line map in SET.  FILE_NAME is the file name to
1599    consider.  If the function returns TRUE, *LOC is set to the highest
1600    location emitted for that file.  */
1601 bool linemap_get_file_highest_location (struct line_maps * set,
1602 					const char *file_name,
1603 					source_location *loc);
1604 
1605 /* Compute and return statistics about the memory consumption of some
1606    parts of the line table SET.  */
1607 void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
1608 
1609 /* Dump debugging information about source location LOC into the file
1610    stream STREAM. SET is the line map set LOC comes from.  */
1611 void linemap_dump_location (struct line_maps *, source_location, FILE *);
1612 
1613 /* Dump line map at index IX in line table SET to STREAM.  If STREAM
1614    is NULL, use stderr.  IS_MACRO is true if the caller wants to
1615    dump a macro map, false otherwise.  */
1616 void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
1617 
1618 /* Dump line table SET to STREAM.  If STREAM is NULL, stderr is used.
1619    NUM_ORDINARY specifies how many ordinary maps to dump.  NUM_MACRO
1620    specifies how many macro maps to dump.  */
1621 void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
1622 
1623 /* The rich_location class requires a way to expand source_location instances.
1624    We would directly use expand_location_to_spelling_point, which is
1625    implemented in gcc/input.c, but we also need to use it for rich_location
1626    within genmatch.c.
1627    Hence we require client code of libcpp to implement the following
1628    symbol.  */
1629 extern expanded_location
1630 linemap_client_expand_location_to_spelling_point (source_location );
1631 
1632 #endif /* !LIBCPP_LINE_MAP_H  */
1633