Home | History | Annotate | Download | only in include
      1 /* Map logical line numbers to (source file, line number) pairs.
      2    Copyright (C) 2001-2013 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 /* A logical line/column number, i.e. an "index" into a line_map.  */
     50 typedef unsigned int source_location;
     51 
     52 /* Memory allocation function typedef.  Works like xrealloc.  */
     53 typedef void *(*line_map_realloc) (void *, size_t);
     54 
     55 /* Memory allocator function that returns the actual allocated size,
     56    for a given requested allocation.  */
     57 typedef size_t (*line_map_round_alloc_size_func) (size_t);
     58 
     59 /* An ordinary line map encodes physical source locations. Those
     60    physical source locations are called "spelling locations".
     61 
     62    Physical source file TO_FILE at line TO_LINE at column 0 is represented
     63    by the logical START_LOCATION.  TO_LINE+L at column C is represented by
     64    START_LOCATION+(L*(1<<column_bits))+C, as long as C<(1<<column_bits),
     65    and the result_location is less than the next line_map's start_location.
     66    (The top line is line 1 and the leftmost column is column 1; line/column 0
     67    means "entire file/line" or "unknown line/column" or "not applicable".)
     68 
     69    The highest possible source location is MAX_SOURCE_LOCATION.  */
     70 struct GTY(()) line_map_ordinary {
     71   const char *to_file;
     72   linenum_type to_line;
     73 
     74   /* An index into the set that gives the line mapping at whose end
     75      the current one was included.  File(s) at the bottom of the
     76      include stack have this set to -1.  */
     77   int included_from;
     78 
     79   /* SYSP is one for a system header, two for a C system header file
     80      that therefore needs to be extern "C" protected in C++, and zero
     81      otherwise.  This field isn't really needed now that it's in
     82      cpp_buffer.  */
     83   unsigned char sysp;
     84 
     85   /* Number of the low-order source_location bits used for a column number.  */
     86   unsigned int column_bits : 8;
     87 };
     88 
     89 /* This is the highest possible source location encoded within an
     90    ordinary or macro map.  */
     91 #define MAX_SOURCE_LOCATION 0x7FFFFFFF
     92 
     93 struct cpp_hashnode;
     94 
     95 /* A macro line map encodes location of tokens coming from a macro
     96    expansion.
     97 
     98    Please note that this struct line_map_macro is a field of struct
     99    line_map below, go read the comments of struct line_map below and
    100    then come back here.
    101 
    102    The offset from START_LOCATION is used to index into
    103    MACRO_LOCATIONS; this holds the original location of the token.  */
    104 struct GTY(()) line_map_macro {
    105   /* The cpp macro which expansion gave birth to this macro map.  */
    106   struct cpp_hashnode * GTY ((nested_ptr (union tree_node,
    107 				   "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
    108 				   "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
    109     macro;
    110 
    111   /* The number of tokens inside the replacement-list of MACRO.  */
    112   unsigned int n_tokens;
    113 
    114   /* This array of location is actually an array of pairs of
    115      locations. The elements inside it thus look like:
    116 
    117            x0,y0, x1,y1, x2,y2, ...., xn,yn.
    118 
    119      where n == n_tokens;
    120 
    121      Remember that these xI,yI are collected when libcpp is about to
    122      expand a given macro.
    123 
    124      yI is the location in the macro definition, either of the token
    125      itself or of a macro parameter that it replaces.
    126 
    127      Imagine this:
    128 
    129 	#define PLUS(A, B) A + B  <--- #1
    130 
    131 	int a = PLUS (1,2); <--- #2
    132 
    133      There is a macro map for the expansion of PLUS in #2.  PLUS is
    134      expanded into its expansion-list.  The expansion-list is the
    135      replacement-list of PLUS where the macro parameters are replaced
    136      with their arguments.  So the replacement-list of PLUS is made of
    137      the tokens:
    138 
    139         A, +, B
    140 
    141      and the expansion-list is made of the tokens:
    142 
    143         1, +, 2
    144 
    145      Let's consider the case of token "+".  Its y1 [yI for I == 1] is
    146      its spelling location in #1.
    147 
    148      y0 (thus for token "1") is the spelling location of A in #1.
    149 
    150      And y2 (of token "2") is the spelling location of B in #1.
    151 
    152      When the token is /not/ an argument for a macro, xI is the same
    153      location as yI.  Otherwise, xI is the location of the token
    154      outside this macro expansion.  If this macro was expanded from
    155      another macro expansion, xI is a virtual location representing
    156      the token in that macro expansion; otherwise, it is the spelling
    157      location of the token.
    158 
    159      Note that a virtual location is a location returned by
    160      linemap_add_macro_token.  It encodes the relevant locations (x,y
    161      pairs) of that token across the macro expansions from which it
    162      (the token) might come from.
    163 
    164      In the example above x1 (for token "+") is going to be the same
    165      as y1.  x0 is the spelling location for the argument token "1",
    166      and x2 is the spelling location for the argument token "2".  */
    167   source_location * GTY((atomic)) macro_locations;
    168 
    169   /* This is the location of the expansion point of the current macro
    170      map.  It's the location of the macro name.  That location is held
    171      by the map that was current right before the current one. It
    172      could have been either a macro or an ordinary map, depending on
    173      if we are in a nested expansion context not.  */
    174   source_location expansion;
    175 };
    176 
    177 /* A line_map encodes a sequence of locations.
    178    There are two kinds of maps. Ordinary maps and macro expansion
    179    maps, a.k.a macro maps.
    180 
    181    A macro map encodes source locations of tokens that are part of a
    182    macro replacement-list, at a macro expansion point. E.g, in:
    183 
    184             #define PLUS(A,B) A + B
    185 
    186    No macro map is going to be created there, because we are not at a
    187    macro expansion point. We are at a macro /definition/ point. So the
    188    locations of the tokens of the macro replacement-list (i.e, A + B)
    189    will be locations in an ordinary map, not a macro map.
    190 
    191    On the other hand, if we later do:
    192 
    193         int a = PLUS (1,2);
    194 
    195    The invocation of PLUS here is a macro expansion. So we are at a
    196    macro expansion point. The preprocessor expands PLUS (1,2) and
    197    replaces it with the tokens of its replacement-list: 1 + 2. A macro
    198    map is going to be created to hold (or rather to map, haha ...) the
    199    locations of the tokens 1, + and 2. The macro map also records the
    200    location of the expansion point of PLUS. That location is mapped in
    201    the map that is active right before the location of the invocation
    202    of PLUS.  */
    203 struct GTY(()) line_map {
    204   source_location start_location;
    205 
    206   /* The reason for creation of this line map.  */
    207   ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
    208 
    209   union map_u {
    210     struct line_map_ordinary GTY((tag ("0"))) ordinary;
    211     struct line_map_macro GTY((tag ("1"))) macro;
    212   } GTY((desc ("%1.reason == LC_ENTER_MACRO"))) d;
    213 };
    214 
    215 #define MAP_START_LOCATION(MAP) (MAP)->start_location
    216 
    217 #define ORDINARY_MAP_FILE_NAME(MAP) \
    218   linemap_check_ordinary (MAP)->d.ordinary.to_file
    219 
    220 #define ORDINARY_MAP_STARTING_LINE_NUMBER(MAP) \
    221   linemap_check_ordinary (MAP)->d.ordinary.to_line
    222 
    223 #define ORDINARY_MAP_INCLUDER_FILE_INDEX(MAP) \
    224   linemap_check_ordinary (MAP)->d.ordinary.included_from
    225 
    226 #define ORDINARY_MAP_IN_SYSTEM_HEADER_P(MAP) \
    227   linemap_check_ordinary (MAP)->d.ordinary.sysp
    228 
    229 #define ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(MAP) \
    230   linemap_check_ordinary (MAP)->d.ordinary.column_bits
    231 
    232 #define MACRO_MAP_MACRO(MAP) (MAP)->d.macro.macro
    233 
    234 #define MACRO_MAP_NUM_MACRO_TOKENS(MAP) (MAP)->d.macro.n_tokens
    235 
    236 #define MACRO_MAP_LOCATIONS(MAP) (MAP)->d.macro.macro_locations
    237 
    238 #define MACRO_MAP_EXPANSION_POINT_LOCATION(MAP) (MAP)->d.macro.expansion
    239 
    240 /* The abstraction of a set of location maps. There can be several
    241    types of location maps. This abstraction contains the attributes
    242    that are independent from the type of the map.  */
    243 struct GTY(()) maps_info {
    244   /* This array contains the different line maps.
    245      A line map is created for the following events:
    246        - when a new preprocessing unit start.
    247        - when a preprocessing unit ends.
    248        - when a macro expansion occurs.  */
    249   struct line_map * GTY ((length ("%h.used"))) maps;
    250 
    251   /* The total number of allocated maps.  */
    252   unsigned int allocated;
    253 
    254   /* The number of elements used in maps. This number is smaller
    255      or equal to ALLOCATED.  */
    256   unsigned int used;
    257 
    258   unsigned int cache;
    259 };
    260 
    261 /* Data structure to associate an arbitrary data to a source location.  */
    262 struct GTY(()) location_adhoc_data {
    263   source_location locus;
    264   void * GTY((skip)) data;
    265 };
    266 
    267 struct htab;
    268 
    269 /* The following data structure encodes a location with some adhoc data
    270    and maps it to a new unsigned integer (called an adhoc location)
    271    that replaces the original location to represent the mapping.
    272 
    273    The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
    274    highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
    275    the original location. Once identified as the adhoc_loc, the lower 31
    276    bits of the integer is used to index the location_adhoc_data array,
    277    in which the locus and associated data is stored.  */
    278 
    279 struct GTY(()) location_adhoc_data_map {
    280   struct htab * GTY((skip)) htab;
    281   source_location curr_loc;
    282   unsigned int allocated;
    283   struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
    284 };
    285 
    286 /* A set of chronological line_map structures.  */
    287 struct GTY(()) line_maps {
    288 
    289   struct maps_info info_ordinary;
    290 
    291   struct maps_info info_macro;
    292 
    293   /* Depth of the include stack, including the current file.  */
    294   unsigned int depth;
    295 
    296   /* If true, prints an include trace a la -H.  */
    297   bool trace_includes;
    298 
    299   /* Highest source_location "given out".  */
    300   source_location highest_location;
    301 
    302   /* Start of line of highest source_location "given out".  */
    303   source_location highest_line;
    304 
    305   /* The maximum column number we can quickly allocate.  Higher numbers
    306      may require allocating a new line_map.  */
    307   unsigned int max_column_hint;
    308 
    309   /* If non-null, the allocator to use when resizing 'maps'.  If null,
    310      xrealloc is used.  */
    311   line_map_realloc reallocator;
    312 
    313   /* The allocators' function used to know the actual size it
    314      allocated, for a certain allocation size requested.  */
    315   line_map_round_alloc_size_func round_alloc_size;
    316 
    317   struct location_adhoc_data_map location_adhoc_data_map;
    318 };
    319 
    320 /* Returns the pointer to the memory region where information about
    321    maps are stored in the line table SET. MACRO_MAP_P is a flag
    322    telling if we want macro or ordinary maps.  */
    323 #define LINEMAPS_MAP_INFO(SET, MACRO_MAP_P)				\
    324   ((MACRO_MAP_P)							\
    325    ? &((SET)->info_macro)						\
    326    : &((SET)->info_ordinary))
    327 
    328 /* Returns the pointer to the memory region where maps are stored in
    329    the line table SET. MAP_KIND shall be TRUE if we are interested in
    330    macro maps false otherwise.  */
    331 #define LINEMAPS_MAPS(SET, MAP_KIND) \
    332   (LINEMAPS_MAP_INFO (SET, MAP_KIND))->maps
    333 
    334 /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
    335    if we are interested in macro maps, FALSE otherwise.  */
    336 #define LINEMAPS_ALLOCATED(SET, MAP_KIND) \
    337   (LINEMAPS_MAP_INFO (SET, MAP_KIND))->allocated
    338 
    339 /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
    340    we are interested in macro maps, FALSE otherwise.*/
    341 #define LINEMAPS_USED(SET, MAP_KIND) \
    342   (LINEMAPS_MAP_INFO (SET, MAP_KIND))->used
    343 
    344 /* Returns the index of the last map that was looked up with
    345    linemap_lookup. MAP_KIND shall be TRUE if we are interested in
    346    macro maps, FALSE otherwise.  */
    347 #define LINEMAPS_CACHE(SET, MAP_KIND) \
    348   (LINEMAPS_MAP_INFO (SET, MAP_KIND))->cache
    349 
    350 /* Return the map at a given index.  */
    351 #define LINEMAPS_MAP_AT(SET, MAP_KIND, INDEX)	\
    352   (&((LINEMAPS_MAPS (SET, MAP_KIND))[(INDEX)]))
    353 
    354 /* Returns the last map used in the line table SET. MAP_KIND
    355    shall be TRUE if we are interested in macro maps, FALSE
    356    otherwise.*/
    357 #define LINEMAPS_LAST_MAP(SET, MAP_KIND) \
    358   LINEMAPS_MAP_AT (SET, MAP_KIND, (LINEMAPS_USED (SET, MAP_KIND) - 1))
    359 
    360 /* Returns the last map that was allocated in the line table SET.
    361    MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
    362    otherwise.*/
    363 #define LINEMAPS_LAST_ALLOCATED_MAP(SET, MAP_KIND) \
    364   LINEMAPS_MAP_AT (SET, MAP_KIND, LINEMAPS_ALLOCATED (SET, MAP_KIND) - 1)
    365 
    366 /* Returns a pointer to the memory region where ordinary maps are
    367    allocated in the line table SET.  */
    368 #define LINEMAPS_ORDINARY_MAPS(SET) \
    369   LINEMAPS_MAPS (SET, false)
    370 
    371 /* Returns the INDEXth ordinary map.  */
    372 #define LINEMAPS_ORDINARY_MAP_AT(SET, INDEX)	\
    373   LINEMAPS_MAP_AT (SET, false, INDEX)
    374 
    375 /* Return the number of ordinary maps allocated in the line table
    376    SET.  */
    377 #define LINEMAPS_ORDINARY_ALLOCATED(SET) \
    378   LINEMAPS_ALLOCATED(SET, false)
    379 
    380 /* Return the number of ordinary maps used in the line table SET.  */
    381 #define LINEMAPS_ORDINARY_USED(SET) \
    382   LINEMAPS_USED(SET, false)
    383 
    384 /* Return the index of the last ordinary map that was looked up with
    385    linemap_lookup.  */
    386 #define LINEMAPS_ORDINARY_CACHE(SET) \
    387   LINEMAPS_CACHE(SET, false)
    388 
    389 /* Returns a pointer to the last ordinary map used in the line table
    390    SET.  */
    391 #define LINEMAPS_LAST_ORDINARY_MAP(SET) \
    392   LINEMAPS_LAST_MAP(SET, false)
    393 
    394 /* Returns a pointer to the last ordinary map allocated the line table
    395    SET.  */
    396 #define LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP(SET) \
    397   LINEMAPS_LAST_ALLOCATED_MAP(SET, false)
    398 
    399 /* Returns a pointer to the beginning of the region where macro maps
    400    are allcoated.  */
    401 #define LINEMAPS_MACRO_MAPS(SET) \
    402   LINEMAPS_MAPS(SET, true)
    403 
    404 /* Returns the INDEXth macro map.  */
    405 #define LINEMAPS_MACRO_MAP_AT(SET, INDEX)	\
    406   LINEMAPS_MAP_AT (SET, true, INDEX)
    407 
    408 /* Returns the number of macro maps that were allocated in the line
    409    table SET.  */
    410 #define LINEMAPS_MACRO_ALLOCATED(SET) \
    411   LINEMAPS_ALLOCATED(SET, true)
    412 
    413 /* Returns the number of macro maps used in the line table SET.  */
    414 #define LINEMAPS_MACRO_USED(SET) \
    415   LINEMAPS_USED(SET, true)
    416 
    417 /* Returns the index of the last macro map looked up with
    418    linemap_lookup.  */
    419 #define LINEMAPS_MACRO_CACHE(SET) \
    420   LINEMAPS_CACHE(SET, true)
    421 
    422 /* Returns the lowest location [of a token resulting from macro
    423    expansion] encoded in this line table.  */
    424 #define LINEMAPS_MACRO_LOWEST_LOCATION(SET)			\
    425   (LINEMAPS_MACRO_USED (set)					\
    426    ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))		\
    427    : MAX_SOURCE_LOCATION)
    428 
    429 /* Returns the last macro map used in the line table SET.  */
    430 #define LINEMAPS_LAST_MACRO_MAP(SET) \
    431   LINEMAPS_LAST_MAP (SET, true)
    432 
    433 /* Returns the last macro map allocated in the line table SET.  */
    434 #define LINEMAPS_LAST_ALLOCATED_MACRO_MAP(SET) \
    435   LINEMAPS_LAST_ALLOCATED_MAP (SET, true)
    436 
    437 extern void location_adhoc_data_fini (struct line_maps *);
    438 extern source_location get_combined_adhoc_loc (struct line_maps *,
    439 					       source_location, void *);
    440 extern void *get_data_from_adhoc_loc (struct line_maps *, source_location);
    441 extern source_location get_location_from_adhoc_loc (struct line_maps *,
    442 						    source_location);
    443 
    444 #define IS_ADHOC_LOC(LOC) (((LOC) & MAX_SOURCE_LOCATION) != (LOC))
    445 #define COMBINE_LOCATION_DATA(SET, LOC, BLOCK) \
    446   get_combined_adhoc_loc ((SET), (LOC), (BLOCK))
    447 
    448 extern void rebuild_location_adhoc_htab (struct line_maps *);
    449 
    450 /* Initialize a line map set.  */
    451 extern void linemap_init (struct line_maps *);
    452 
    453 /* Check for and warn about line_maps entered but not exited.  */
    454 
    455 extern void linemap_check_files_exited (struct line_maps *);
    456 
    457 /* Return a source_location for the start (i.e. column==0) of
    458    (physical) line TO_LINE in the current source file (as in the
    459    most recent linemap_add).   MAX_COLUMN_HINT is the highest column
    460    number we expect to use in this line (but it does not change
    461    the highest_location).  */
    462 
    463 extern source_location linemap_line_start
    464 (struct line_maps *set, linenum_type to_line,  unsigned int max_column_hint);
    465 
    466 /* Add a mapping of logical source line to physical source file and
    467    line number. This function creates an "ordinary map", which is a
    468    map that records locations of tokens that are not part of macro
    469    replacement-lists present at a macro expansion point.
    470 
    471    The text pointed to by TO_FILE must have a lifetime
    472    at least as long as the lifetime of SET.  An empty
    473    TO_FILE means standard input.  If reason is LC_LEAVE, and
    474    TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
    475    natural values considering the file we are returning to.
    476 
    477    A call to this function can relocate the previous set of
    478    maps, so any stored line_map pointers should not be used.  */
    479 extern const struct line_map *linemap_add
    480   (struct line_maps *, enum lc_reason, unsigned int sysp,
    481    const char *to_file, linenum_type to_line);
    482 
    483 /* Given a logical source location, returns the map which the
    484    corresponding (source file, line, column) triplet can be deduced
    485    from. Since the set is built chronologically, the logical lines are
    486    monotonic increasing, and so the list is sorted and we can use a
    487    binary search. If no line map have been allocated yet, this
    488    function returns NULL.  */
    489 extern const struct line_map *linemap_lookup
    490   (struct line_maps *, source_location);
    491 
    492 /* Returns TRUE if the line table set tracks token locations across
    493    macro expansion, FALSE otherwise.  */
    494 bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
    495 
    496 /* Return TRUE if MAP encodes locations coming from a macro
    497    replacement-list at macro expansion point.  */
    498 bool linemap_macro_expansion_map_p (const struct line_map *);
    499 
    500 /* Return the name of the macro associated to MACRO_MAP.  */
    501 const char* linemap_map_get_macro_name (const struct line_map*);
    502 
    503 /* Return a positive value if LOCATION is the locus of a token that is
    504    located in a system header, O otherwise. It returns 1 if LOCATION
    505    is the locus of a token that is located in a system header, and 2
    506    if LOCATION is the locus of a token located in a C system header
    507    that therefore needs to be extern "C" protected in C++.
    508 
    509    Note that this function returns 1 if LOCATION belongs to a token
    510    that is part of a macro replacement-list defined in a system
    511    header, but expanded in a non-system file.  */
    512 int linemap_location_in_system_header_p (struct line_maps *,
    513 					 source_location);
    514 
    515 /* Return TRUE if LOCATION is a source code location of a token coming
    516    from a macro replacement-list at a macro expansion point, FALSE
    517    otherwise.  */
    518 bool linemap_location_from_macro_expansion_p (struct line_maps *,
    519 					      source_location);
    520 
    521 /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
    522    be reserved for libcpp user as special values, no token from libcpp
    523    will contain any of those locations.  */
    524 #define RESERVED_LOCATION_COUNT	2
    525 
    526 /* Converts a map and a source_location to source line.  */
    527 #define SOURCE_LINE(MAP, LOC)						\
    528   (((((LOC) - linemap_check_ordinary (MAP)->start_location)		\
    529      >> (MAP)->d.ordinary.column_bits) + (MAP)->d.ordinary.to_line))
    530 
    531 /* Convert a map and source_location to source column number.  */
    532 #define SOURCE_COLUMN(MAP, LOC)						\
    533   ((((LOC) - linemap_check_ordinary (MAP)->start_location)		\
    534     & ((1 << (MAP)->d.ordinary.column_bits) - 1)))
    535 
    536 /* Returns the last source line number within an ordinary map.  This
    537    is the (last) line of the #include, or other directive, that caused
    538    a map change.  */
    539 #define LAST_SOURCE_LINE(MAP) \
    540   SOURCE_LINE (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
    541 
    542 /* Return the last column number within an ordinary map.  */
    543 #define LAST_SOURCE_COLUMN(MAP) \
    544   SOURCE_COLUMN (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
    545 
    546 /* Return the location of the last source line within an ordinary
    547    map.  */
    548 #define LAST_SOURCE_LINE_LOCATION(MAP)					\
    549   ((((linemap_check_ordinary (MAP)[1].start_location - 1		\
    550       - (MAP)->start_location)						\
    551      & ~((1 << (MAP)->d.ordinary.column_bits) - 1))			\
    552     + (MAP)->start_location))
    553 
    554 /* Returns the map a given map was included from, or NULL if the map
    555    belongs to the main file, i.e, a file that wasn't included by
    556    another one.  */
    557 #define INCLUDED_FROM(SET, MAP)						\
    558   ((linemap_check_ordinary (MAP)->d.ordinary.included_from == -1)	\
    559    ? NULL								\
    560    : (&LINEMAPS_ORDINARY_MAPS (SET)[(MAP)->d.ordinary.included_from]))
    561 
    562 /* Nonzero if the map is at the bottom of the include stack.  */
    563 #define MAIN_FILE_P(MAP)						\
    564   ((linemap_check_ordinary (MAP)->d.ordinary.included_from < 0))
    565 
    566 #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
    567 
    568 /* Assertion macro to be used in line-map code.  */
    569 #define linemap_assert(EXPR)			\
    570   do {						\
    571     if (! (EXPR))				\
    572       abort ();					\
    573   } while (0)
    574 
    575 /* Assert that MAP encodes locations of tokens that are not part of
    576    the replacement-list of a macro expansion.  */
    577 #define linemap_check_ordinary(LINE_MAP) __extension__		\
    578   ({linemap_assert (!linemap_macro_expansion_map_p (LINE_MAP)); \
    579     (LINE_MAP);})
    580 #else
    581 #define linemap_assert(EXPR)
    582 #define linemap_check_ordinary(LINE_MAP) (LINE_MAP)
    583 #endif
    584 
    585 /* Encode and return a source_location from a column number. The
    586    source line considered is the last source line used to call
    587    linemap_line_start, i.e, the last source line which a location was
    588    encoded from.  */
    589 extern source_location
    590 linemap_position_for_column (struct line_maps *, unsigned int);
    591 
    592 /* Encode and return a source location from a given line and
    593    column.  */
    594 source_location linemap_position_for_line_and_column (struct line_map *,
    595 						      linenum_type,
    596 						      unsigned int);
    597 /* Return the file this map is for.  */
    598 #define LINEMAP_FILE(MAP)					\
    599   (linemap_check_ordinary (MAP)->d.ordinary.to_file)
    600 
    601 /* Return the line number this map started encoding location from.  */
    602 #define LINEMAP_LINE(MAP)					\
    603   (linemap_check_ordinary (MAP)->d.ordinary.to_line)
    604 
    605 /* Return a positive value if map encodes locations from a system
    606    header, 0 otherwise. Returns 1 if MAP encodes locations in a
    607    system header and 2 if it encodes locations in a C system header
    608    that therefore needs to be extern "C" protected in C++.  */
    609 #define LINEMAP_SYSP(MAP)					\
    610   (linemap_check_ordinary (MAP)->d.ordinary.sysp)
    611 
    612 /* Return a positive value if PRE denotes the location of a token that
    613    comes before the token of POST, 0 if PRE denotes the location of
    614    the same token as the token for POST, and a negative value
    615    otherwise.  */
    616 int linemap_compare_locations (struct line_maps *set,
    617 			       source_location   pre,
    618 			       source_location   post);
    619 
    620 /* Return TRUE if LOC_A denotes the location a token that comes
    621    topogically before the token denoted by location LOC_B, or if they
    622    are equal.  */
    623 #define linemap_location_before_p(SET, LOC_A, LOC_B)	\
    624   (linemap_compare_locations ((SET), (LOC_A), (LOC_B)) >= 0)
    625 
    626 typedef struct
    627 {
    628   /* The name of the source file involved.  */
    629   const char *file;
    630 
    631   /* The line-location in the source file.  */
    632   int line;
    633 
    634   int column;
    635 
    636   void *data;
    637 
    638   /* In a system header?. */
    639   bool sysp;
    640 } expanded_location;
    641 
    642 /* This is enum is used by the function linemap_resolve_location
    643    below.  The meaning of the values is explained in the comment of
    644    that function.  */
    645 enum location_resolution_kind
    646 {
    647   LRK_MACRO_EXPANSION_POINT,
    648   LRK_SPELLING_LOCATION,
    649   LRK_MACRO_DEFINITION_LOCATION
    650 };
    651 
    652 /* Resolve a virtual location into either a spelling location, an
    653    expansion point location or a token argument replacement point
    654    location.  Return the map that encodes the virtual location as well
    655    as the resolved location.
    656 
    657    If LOC is *NOT* the location of a token resulting from the
    658    expansion of a macro, then the parameter LRK (which stands for
    659    Location Resolution Kind) is ignored and the resulting location
    660    just equals the one given in argument.
    661 
    662    Now if LOC *IS* the location of a token resulting from the
    663    expansion of a macro, this is what happens.
    664 
    665    * If LRK is set to LRK_MACRO_EXPANSION_POINT
    666    -------------------------------
    667 
    668    The virtual location is resolved to the first macro expansion point
    669    that led to this macro expansion.
    670 
    671    * If LRK is set to LRK_SPELLING_LOCATION
    672    -------------------------------------
    673 
    674    The virtual location is resolved to the locus where the token has
    675    been spelled in the source.   This can follow through all the macro
    676    expansions that led to the token.
    677 
    678    * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
    679    --------------------------------------
    680 
    681    The virtual location is resolved to the locus of the token in the
    682    context of the macro definition.
    683 
    684    If LOC is the locus of a token that is an argument of a
    685    function-like macro [replacing a parameter in the replacement list
    686    of the macro] the virtual location is resolved to the locus of the
    687    parameter that is replaced, in the context of the definition of the
    688    macro.
    689 
    690    If LOC is the locus of a token that is not an argument of a
    691    function-like macro, then the function behaves as if LRK was set to
    692    LRK_SPELLING_LOCATION.
    693 
    694    If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
    695    returned location.  Note that if the returned location wasn't originally
    696    encoded by a map, the *MAP is set to NULL.  This can happen if LOC
    697    resolves to a location reserved for the client code, like
    698    UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC.  */
    699 
    700 source_location linemap_resolve_location (struct line_maps *,
    701 					  source_location loc,
    702 					  enum location_resolution_kind lrk,
    703 					  const struct line_map **loc_map);
    704 
    705 /* Suppose that LOC is the virtual location of a token coming from the
    706    expansion of a macro M.  This function then steps up to get the
    707    location L of the point where M got expanded.  If L is a spelling
    708    location inside a macro expansion M', then this function returns
    709    the point where M' was expanded.  LOC_MAP is an output parameter.
    710    When non-NULL, *LOC_MAP is set to the map of the returned
    711    location.  */
    712 source_location linemap_unwind_toward_expansion (struct line_maps *,
    713 						 source_location loc,
    714 						 const struct line_map **loc_map);
    715 
    716 /* If LOC is the virtual location of a token coming from the expansion
    717    of a macro M and if its spelling location is reserved (e.g, a
    718    location for a built-in token), then this function unwinds (using
    719    linemap_unwind_toward_expansion) the location until a location that
    720    is not reserved and is not in a system header is reached.  In other
    721    words, this unwinds the reserved location until a location that is
    722    in real source code is reached.
    723 
    724    Otherwise, if the spelling location for LOC is not reserved or if
    725    LOC doesn't come from the expansion of a macro, the function
    726    returns LOC as is and *MAP is not touched.
    727 
    728    *MAP is set to the map of the returned location if the later is
    729    different from LOC.  */
    730 source_location linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
    731 							  source_location loc,
    732 							  const struct line_map **map);
    733 
    734 /* Expand source code location LOC and return a user readable source
    735    code location.  LOC must be a spelling (non-virtual) location.  If
    736    it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
    737    location is returned.  */
    738 expanded_location linemap_expand_location (struct line_maps *,
    739 					   const struct line_map *,
    740 					   source_location loc);
    741 
    742 /* Statistics about maps allocation and usage as returned by
    743    linemap_get_statistics.  */
    744 struct linemap_stats
    745 {
    746   long num_ordinary_maps_allocated;
    747   long num_ordinary_maps_used;
    748   long ordinary_maps_allocated_size;
    749   long ordinary_maps_used_size;
    750   long num_expanded_macros;
    751   long num_macro_tokens;
    752   long num_macro_maps_used;
    753   long macro_maps_allocated_size;
    754   long macro_maps_used_size;
    755   long macro_maps_locations_size;
    756   long duplicated_macro_maps_locations_size;
    757 };
    758 
    759 /* Compute and return statistics about the memory consumption of some
    760    parts of the line table SET.  */
    761 void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
    762 
    763 /* Dump debugging information about source location LOC into the file
    764    stream STREAM. SET is the line map set LOC comes from.  */
    765 void linemap_dump_location (struct line_maps *, source_location, FILE *);
    766 
    767 /* Dump line map at index IX in line table SET to STREAM.  If STREAM
    768    is NULL, use stderr.  IS_MACRO is true if the caller wants to
    769    dump a macro map, false otherwise.  */
    770 void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
    771 
    772 /* Dump line table SET to STREAM.  If STREAM is NULL, stderr is used.
    773    NUM_ORDINARY specifies how many ordinary maps to dump.  NUM_MACRO
    774    specifies how many macro maps to dump.  */
    775 void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
    776 
    777 #endif /* !LIBCPP_LINE_MAP_H  */
    778