1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
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
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.
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/>.
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! */
22 #ifndef LIBCPP_LINE_MAP_H
23 #define LIBCPP_LINE_MAP_H
26 #define GTY(x) /* nothing */
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. */
43 /* FIXME: add support for stringize and paste. */
46 /* The type of line numbers. */
47 typedef unsigned int linenum_type
;
49 /* The typedef "source_location" is a key within the location database,
50 identifying a source location or macro expansion.
52 This key only has meaning in relation to a line_maps instance. Within
53 gcc there is a single line_maps instance: "line_table", declared in
54 gcc/input.h and defined in gcc/input.c.
56 The values of the keys are intended to be internal to libcpp,
57 but for ease-of-understanding the implementation, they are currently
60 Actual | Value | Meaning
61 -----------+-------------------------------+-------------------------------
62 0x00000000 | | Reserved for use by libcpp
63 0x00000001 | RESERVED_LOCATION_COUNT - 1 | Reserved for use by libcpp
64 -----------+-------------------------------+-------------------------------
65 0x00000002 | RESERVED_LOCATION_COUNT | The first location to be
66 | (also | handed out, and the
67 | ordmap[0]->start_location) | first line in ordmap 0
68 -----------+-------------------------------+-------------------------------
69 | ordmap[1]->start_location | First line in ordmap 1
70 | ordmap[1]->start_location+1 | First column in that line
71 | ordmap[1]->start_location+2 | 2nd column in that line
72 | | Subsequent lines are offset by
73 | | (1 << column_bits),
74 | | e.g. 128 for 7 bits, with a
75 | | column value of 0 representing
77 | ordmap[2]->start_location-1 | Final location in ordmap 1
78 -----------+-------------------------------+-------------------------------
79 | ordmap[2]->start_location | First line in ordmap 2
80 | ordmap[3]->start_location-1 | Final location in ordmap 2
81 -----------+-------------------------------+-------------------------------
83 -----------+-------------------------------+-------------------------------
84 | ordmap[n-1]->start_location | First line in final ord map
86 | set->highest_location - 1 | Final location in that ordmap
87 -----------+-------------------------------+-------------------------------
88 | set->highest_location | Location of the where the next
89 | | ordinary linemap would start
90 -----------+-------------------------------+-------------------------------
92 | VVVVVVVVVVVVVVVVVVVVVVVVVVV
93 | Ordinary maps grow this way
95 | (unallocated integers)
97 | Macro maps grow this way
98 | ^^^^^^^^^^^^^^^^^^^^^^^^
100 -----------+-------------------------------+-------------------------------
101 | LINEMAPS_MACRO_LOWEST_LOCATION| Locations within macro maps
102 | macromap[m-1]->start_location | Start of last macro map
104 -----------+-------------------------------+-------------------------------
105 | macromap[m-2]->start_location | Start of penultimate macro map
106 -----------+-------------------------------+-------------------------------
107 | macromap[1]->start_location | Start of macro map 1
108 -----------+-------------------------------+-------------------------------
109 | macromap[0]->start_location | Start of macro map 0
110 0x7fffffff | MAX_SOURCE_LOCATION |
111 -----------+-------------------------------+-------------------------------
112 0x80000000 | Start of ad-hoc values |
114 0xffffffff | UINT_MAX |
115 -----------+-------------------------------+-------------------------------
117 typedef unsigned int source_location
;
119 /* Memory allocation function typedef. Works like xrealloc. */
120 typedef void *(*line_map_realloc
) (void *, size_t);
122 /* Memory allocator function that returns the actual allocated size,
123 for a given requested allocation. */
124 typedef size_t (*line_map_round_alloc_size_func
) (size_t);
126 /* An ordinary line map encodes physical source locations. Those
127 physical source locations are called "spelling locations".
129 Physical source file TO_FILE at line TO_LINE at column 0 is represented
130 by the logical START_LOCATION. TO_LINE+L at column C is represented by
131 START_LOCATION+(L*(1<<column_bits))+C, as long as C<(1<<column_bits),
132 and the result_location is less than the next line_map's start_location.
133 (The top line is line 1 and the leftmost column is column 1; line/column 0
134 means "entire file/line" or "unknown line/column" or "not applicable".)
136 The highest possible source location is MAX_SOURCE_LOCATION. */
137 struct GTY(()) line_map_ordinary
{
139 linenum_type to_line
;
141 /* An index into the set that gives the line mapping at whose end
142 the current one was included. File(s) at the bottom of the
143 include stack have this set to -1. */
146 /* SYSP is one for a system header, two for a C system header file
147 that therefore needs to be extern "C" protected in C++, and zero
148 otherwise. This field isn't really needed now that it's in
152 /* Number of the low-order source_location bits used for a column number. */
153 unsigned int column_bits
: 8;
156 /* This is the highest possible source location encoded within an
157 ordinary or macro map. */
158 #define MAX_SOURCE_LOCATION 0x7FFFFFFF
162 /* A macro line map encodes location of tokens coming from a macro
165 Please note that this struct line_map_macro is a field of struct
166 line_map below, go read the comments of struct line_map below and
169 The offset from START_LOCATION is used to index into
170 MACRO_LOCATIONS; this holds the original location of the token. */
171 struct GTY(()) line_map_macro
{
172 /* The cpp macro which expansion gave birth to this macro map. */
173 struct cpp_hashnode
* GTY ((nested_ptr (union tree_node
,
174 "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
175 "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
178 /* The number of tokens inside the replacement-list of MACRO. */
179 unsigned int n_tokens
;
181 /* This array of location is actually an array of pairs of
182 locations. The elements inside it thus look like:
184 x0,y0, x1,y1, x2,y2, ...., xn,yn.
188 Remember that these xI,yI are collected when libcpp is about to
189 expand a given macro.
191 yI is the location in the macro definition, either of the token
192 itself or of a macro parameter that it replaces.
196 #define PLUS(A, B) A + B <--- #1
198 int a = PLUS (1,2); <--- #2
200 There is a macro map for the expansion of PLUS in #2. PLUS is
201 expanded into its expansion-list. The expansion-list is the
202 replacement-list of PLUS where the macro parameters are replaced
203 with their arguments. So the replacement-list of PLUS is made of
208 and the expansion-list is made of the tokens:
212 Let's consider the case of token "+". Its y1 [yI for I == 1] is
213 its spelling location in #1.
215 y0 (thus for token "1") is the spelling location of A in #1.
217 And y2 (of token "2") is the spelling location of B in #1.
219 When the token is /not/ an argument for a macro, xI is the same
220 location as yI. Otherwise, xI is the location of the token
221 outside this macro expansion. If this macro was expanded from
222 another macro expansion, xI is a virtual location representing
223 the token in that macro expansion; otherwise, it is the spelling
224 location of the token.
226 Note that a virtual location is a location returned by
227 linemap_add_macro_token. It encodes the relevant locations (x,y
228 pairs) of that token across the macro expansions from which it
229 (the token) might come from.
231 In the example above x1 (for token "+") is going to be the same
232 as y1. x0 is the spelling location for the argument token "1",
233 and x2 is the spelling location for the argument token "2". */
234 source_location
* GTY((atomic
)) macro_locations
;
236 /* This is the location of the expansion point of the current macro
237 map. It's the location of the macro name. That location is held
238 by the map that was current right before the current one. It
239 could have been either a macro or an ordinary map, depending on
240 if we are in a nested expansion context not. */
241 source_location expansion
;
244 /* A line_map encodes a sequence of locations.
245 There are two kinds of maps. Ordinary maps and macro expansion
246 maps, a.k.a macro maps.
248 A macro map encodes source locations of tokens that are part of a
249 macro replacement-list, at a macro expansion point. E.g, in:
251 #define PLUS(A,B) A + B
253 No macro map is going to be created there, because we are not at a
254 macro expansion point. We are at a macro /definition/ point. So the
255 locations of the tokens of the macro replacement-list (i.e, A + B)
256 will be locations in an ordinary map, not a macro map.
258 On the other hand, if we later do:
262 The invocation of PLUS here is a macro expansion. So we are at a
263 macro expansion point. The preprocessor expands PLUS (1,2) and
264 replaces it with the tokens of its replacement-list: 1 + 2. A macro
265 map is going to be created to hold (or rather to map, haha ...) the
266 locations of the tokens 1, + and 2. The macro map also records the
267 location of the expansion point of PLUS. That location is mapped in
268 the map that is active right before the location of the invocation
270 struct GTY(()) line_map
{
271 source_location start_location
;
273 /* The reason for creation of this line map. */
274 ENUM_BITFIELD (lc_reason
) reason
: CHAR_BIT
;
277 struct line_map_ordinary
GTY((tag ("0"))) ordinary
;
278 struct line_map_macro
GTY((tag ("1"))) macro
;
279 } GTY((desc ("%1.reason == LC_ENTER_MACRO"))) d
;
282 #define MAP_START_LOCATION(MAP) (MAP)->start_location
284 #define ORDINARY_MAP_FILE_NAME(MAP) \
285 linemap_check_ordinary (MAP)->d.ordinary.to_file
287 #define ORDINARY_MAP_STARTING_LINE_NUMBER(MAP) \
288 linemap_check_ordinary (MAP)->d.ordinary.to_line
290 #define ORDINARY_MAP_INCLUDER_FILE_INDEX(MAP) \
291 linemap_check_ordinary (MAP)->d.ordinary.included_from
293 #define ORDINARY_MAP_IN_SYSTEM_HEADER_P(MAP) \
294 linemap_check_ordinary (MAP)->d.ordinary.sysp
296 #define ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(MAP) \
297 linemap_check_ordinary (MAP)->d.ordinary.column_bits
299 #define MACRO_MAP_MACRO(MAP) (MAP)->d.macro.macro
301 #define MACRO_MAP_NUM_MACRO_TOKENS(MAP) (MAP)->d.macro.n_tokens
303 #define MACRO_MAP_LOCATIONS(MAP) (MAP)->d.macro.macro_locations
305 #define MACRO_MAP_EXPANSION_POINT_LOCATION(MAP) (MAP)->d.macro.expansion
307 /* The abstraction of a set of location maps. There can be several
308 types of location maps. This abstraction contains the attributes
309 that are independent from the type of the map. */
310 struct GTY(()) maps_info
{
311 /* This array contains the different line maps.
312 A line map is created for the following events:
313 - when a new preprocessing unit start.
314 - when a preprocessing unit ends.
315 - when a macro expansion occurs. */
316 struct line_map
* GTY ((length ("%h.used"))) maps
;
318 /* The total number of allocated maps. */
319 unsigned int allocated
;
321 /* The number of elements used in maps. This number is smaller
322 or equal to ALLOCATED. */
328 /* Data structure to associate an arbitrary data to a source location. */
329 struct GTY(()) location_adhoc_data
{
330 source_location locus
;
331 void * GTY((skip
)) data
;
336 /* The following data structure encodes a location with some adhoc data
337 and maps it to a new unsigned integer (called an adhoc location)
338 that replaces the original location to represent the mapping.
340 The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
341 highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
342 the original location. Once identified as the adhoc_loc, the lower 31
343 bits of the integer is used to index the location_adhoc_data array,
344 in which the locus and associated data is stored. */
346 struct GTY(()) location_adhoc_data_map
{
347 struct htab
* GTY((skip
)) htab
;
348 source_location curr_loc
;
349 unsigned int allocated
;
350 struct location_adhoc_data
GTY((length ("%h.allocated"))) *data
;
353 /* A set of chronological line_map structures. */
354 struct GTY(()) line_maps
{
356 struct maps_info info_ordinary
;
358 struct maps_info info_macro
;
360 /* Depth of the include stack, including the current file. */
363 /* If true, prints an include trace a la -H. */
366 /* Highest source_location "given out". */
367 source_location highest_location
;
369 /* Start of line of highest source_location "given out". */
370 source_location highest_line
;
372 /* The maximum column number we can quickly allocate. Higher numbers
373 may require allocating a new line_map. */
374 unsigned int max_column_hint
;
376 /* If non-null, the allocator to use when resizing 'maps'. If null,
378 line_map_realloc reallocator
;
380 /* The allocators' function used to know the actual size it
381 allocated, for a certain allocation size requested. */
382 line_map_round_alloc_size_func round_alloc_size
;
384 struct location_adhoc_data_map location_adhoc_data_map
;
386 /* The special location value that is used as spelling location for
388 source_location builtin_location
;
391 /* Returns the pointer to the memory region where information about
392 maps are stored in the line table SET. MACRO_MAP_P is a flag
393 telling if we want macro or ordinary maps. */
394 #define LINEMAPS_MAP_INFO(SET, MACRO_MAP_P) \
396 ? &((SET)->info_macro) \
397 : &((SET)->info_ordinary))
399 /* Returns the pointer to the memory region where maps are stored in
400 the line table SET. MAP_KIND shall be TRUE if we are interested in
401 macro maps false otherwise. */
402 #define LINEMAPS_MAPS(SET, MAP_KIND) \
403 (LINEMAPS_MAP_INFO (SET, MAP_KIND))->maps
405 /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
406 if we are interested in macro maps, FALSE otherwise. */
407 #define LINEMAPS_ALLOCATED(SET, MAP_KIND) \
408 (LINEMAPS_MAP_INFO (SET, MAP_KIND))->allocated
410 /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
411 we are interested in macro maps, FALSE otherwise.*/
412 #define LINEMAPS_USED(SET, MAP_KIND) \
413 (LINEMAPS_MAP_INFO (SET, MAP_KIND))->used
415 /* Returns the index of the last map that was looked up with
416 linemap_lookup. MAP_KIND shall be TRUE if we are interested in
417 macro maps, FALSE otherwise. */
418 #define LINEMAPS_CACHE(SET, MAP_KIND) \
419 (LINEMAPS_MAP_INFO (SET, MAP_KIND))->cache
421 /* Return the map at a given index. */
422 #define LINEMAPS_MAP_AT(SET, MAP_KIND, INDEX) \
423 (&((LINEMAPS_MAPS (SET, MAP_KIND))[(INDEX)]))
425 /* Returns the last map used in the line table SET. MAP_KIND
426 shall be TRUE if we are interested in macro maps, FALSE
428 #define LINEMAPS_LAST_MAP(SET, MAP_KIND) \
429 LINEMAPS_MAP_AT (SET, MAP_KIND, (LINEMAPS_USED (SET, MAP_KIND) - 1))
431 /* Returns the last map that was allocated in the line table SET.
432 MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
434 #define LINEMAPS_LAST_ALLOCATED_MAP(SET, MAP_KIND) \
435 LINEMAPS_MAP_AT (SET, MAP_KIND, LINEMAPS_ALLOCATED (SET, MAP_KIND) - 1)
437 /* Returns a pointer to the memory region where ordinary maps are
438 allocated in the line table SET. */
439 #define LINEMAPS_ORDINARY_MAPS(SET) \
440 LINEMAPS_MAPS (SET, false)
442 /* Returns the INDEXth ordinary map. */
443 #define LINEMAPS_ORDINARY_MAP_AT(SET, INDEX) \
444 LINEMAPS_MAP_AT (SET, false, INDEX)
446 /* Return the number of ordinary maps allocated in the line table
448 #define LINEMAPS_ORDINARY_ALLOCATED(SET) \
449 LINEMAPS_ALLOCATED(SET, false)
451 /* Return the number of ordinary maps used in the line table SET. */
452 #define LINEMAPS_ORDINARY_USED(SET) \
453 LINEMAPS_USED(SET, false)
455 /* Return the index of the last ordinary map that was looked up with
457 #define LINEMAPS_ORDINARY_CACHE(SET) \
458 LINEMAPS_CACHE(SET, false)
460 /* Returns a pointer to the last ordinary map used in the line table
462 #define LINEMAPS_LAST_ORDINARY_MAP(SET) \
463 LINEMAPS_LAST_MAP(SET, false)
465 /* Returns a pointer to the last ordinary map allocated the line table
467 #define LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP(SET) \
468 LINEMAPS_LAST_ALLOCATED_MAP(SET, false)
470 /* Returns a pointer to the beginning of the region where macro maps
472 #define LINEMAPS_MACRO_MAPS(SET) \
473 LINEMAPS_MAPS(SET, true)
475 /* Returns the INDEXth macro map. */
476 #define LINEMAPS_MACRO_MAP_AT(SET, INDEX) \
477 LINEMAPS_MAP_AT (SET, true, INDEX)
479 /* Returns the number of macro maps that were allocated in the line
481 #define LINEMAPS_MACRO_ALLOCATED(SET) \
482 LINEMAPS_ALLOCATED(SET, true)
484 /* Returns the number of macro maps used in the line table SET. */
485 #define LINEMAPS_MACRO_USED(SET) \
486 LINEMAPS_USED(SET, true)
488 /* Returns the index of the last macro map looked up with
490 #define LINEMAPS_MACRO_CACHE(SET) \
491 LINEMAPS_CACHE(SET, true)
493 /* Returns the lowest location [of a token resulting from macro
494 expansion] encoded in this line table. */
495 #define LINEMAPS_MACRO_LOWEST_LOCATION(SET) \
496 (LINEMAPS_MACRO_USED (set) \
497 ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set)) \
498 : MAX_SOURCE_LOCATION)
500 /* Returns the last macro map used in the line table SET. */
501 #define LINEMAPS_LAST_MACRO_MAP(SET) \
502 LINEMAPS_LAST_MAP (SET, true)
504 /* Returns the last macro map allocated in the line table SET. */
505 #define LINEMAPS_LAST_ALLOCATED_MACRO_MAP(SET) \
506 LINEMAPS_LAST_ALLOCATED_MAP (SET, true)
508 extern void location_adhoc_data_fini (struct line_maps
*);
509 extern source_location
get_combined_adhoc_loc (struct line_maps
*,
510 source_location
, void *);
511 extern void *get_data_from_adhoc_loc (struct line_maps
*, source_location
);
512 extern source_location
get_location_from_adhoc_loc (struct line_maps
*,
515 #define IS_ADHOC_LOC(LOC) (((LOC) & MAX_SOURCE_LOCATION) != (LOC))
516 #define COMBINE_LOCATION_DATA(SET, LOC, BLOCK) \
517 get_combined_adhoc_loc ((SET), (LOC), (BLOCK))
519 extern void rebuild_location_adhoc_htab (struct line_maps
*);
521 /* Initialize a line map set. SET is the line map set to initialize
522 and BUILTIN_LOCATION is the special location value to be used as
523 spelling location for built-in tokens. This BUILTIN_LOCATION has
524 to be strictly less than RESERVED_LOCATION_COUNT. */
525 extern void linemap_init (struct line_maps
*set
,
526 source_location builtin_location
);
528 /* Check for and warn about line_maps entered but not exited. */
530 extern void linemap_check_files_exited (struct line_maps
*);
532 /* Return a source_location for the start (i.e. column==0) of
533 (physical) line TO_LINE in the current source file (as in the
534 most recent linemap_add). MAX_COLUMN_HINT is the highest column
535 number we expect to use in this line (but it does not change
536 the highest_location). */
538 extern source_location linemap_line_start
539 (struct line_maps
*set
, linenum_type to_line
, unsigned int max_column_hint
);
541 /* Add a mapping of logical source line to physical source file and
542 line number. This function creates an "ordinary map", which is a
543 map that records locations of tokens that are not part of macro
544 replacement-lists present at a macro expansion point.
546 The text pointed to by TO_FILE must have a lifetime
547 at least as long as the lifetime of SET. An empty
548 TO_FILE means standard input. If reason is LC_LEAVE, and
549 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
550 natural values considering the file we are returning to.
552 A call to this function can relocate the previous set of
553 maps, so any stored line_map pointers should not be used. */
554 extern const struct line_map
*linemap_add
555 (struct line_maps
*, enum lc_reason
, unsigned int sysp
,
556 const char *to_file
, linenum_type to_line
);
558 /* Given a logical source location, returns the map which the
559 corresponding (source file, line, column) triplet can be deduced
560 from. Since the set is built chronologically, the logical lines are
561 monotonic increasing, and so the list is sorted and we can use a
562 binary search. If no line map have been allocated yet, this
563 function returns NULL. */
564 extern const struct line_map
*linemap_lookup
565 (struct line_maps
*, source_location
);
567 /* Returns TRUE if the line table set tracks token locations across
568 macro expansion, FALSE otherwise. */
569 bool linemap_tracks_macro_expansion_locs_p (struct line_maps
*);
571 /* Return TRUE if MAP encodes locations coming from a macro
572 replacement-list at macro expansion point. */
573 bool linemap_macro_expansion_map_p (const struct line_map
*);
575 /* Return the name of the macro associated to MACRO_MAP. */
576 const char* linemap_map_get_macro_name (const struct line_map
*);
578 /* Return a positive value if LOCATION is the locus of a token that is
579 located in a system header, O otherwise. It returns 1 if LOCATION
580 is the locus of a token that is located in a system header, and 2
581 if LOCATION is the locus of a token located in a C system header
582 that therefore needs to be extern "C" protected in C++.
584 Note that this function returns 1 if LOCATION belongs to a token
585 that is part of a macro replacement-list defined in a system
586 header, but expanded in a non-system file. */
587 int linemap_location_in_system_header_p (struct line_maps
*,
590 /* Return TRUE if LOCATION is a source code location of a token coming
591 from a macro replacement-list at a macro expansion point, FALSE
593 bool linemap_location_from_macro_expansion_p (const struct line_maps
*,
596 /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
597 be reserved for libcpp user as special values, no token from libcpp
598 will contain any of those locations. */
599 #define RESERVED_LOCATION_COUNT 2
601 /* Converts a map and a source_location to source line. */
602 #define SOURCE_LINE(MAP, LOC) \
603 (((((LOC) - linemap_check_ordinary (MAP)->start_location) \
604 >> (MAP)->d.ordinary.column_bits) + (MAP)->d.ordinary.to_line))
606 /* Convert a map and source_location to source column number. */
607 #define SOURCE_COLUMN(MAP, LOC) \
608 ((((LOC) - linemap_check_ordinary (MAP)->start_location) \
609 & ((1 << (MAP)->d.ordinary.column_bits) - 1)))
611 /* Returns the last source line number within an ordinary map. This
612 is the (last) line of the #include, or other directive, that caused
614 #define LAST_SOURCE_LINE(MAP) \
615 SOURCE_LINE (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
617 /* Return the last column number within an ordinary map. */
618 #define LAST_SOURCE_COLUMN(MAP) \
619 SOURCE_COLUMN (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
621 /* Return the location of the last source line within an ordinary
623 #define LAST_SOURCE_LINE_LOCATION(MAP) \
624 ((((linemap_check_ordinary (MAP)[1].start_location - 1 \
625 - (MAP)->start_location) \
626 & ~((1 << (MAP)->d.ordinary.column_bits) - 1)) \
627 + (MAP)->start_location))
629 /* Returns the map a given map was included from, or NULL if the map
630 belongs to the main file, i.e, a file that wasn't included by
632 #define INCLUDED_FROM(SET, MAP) \
633 ((linemap_check_ordinary (MAP)->d.ordinary.included_from == -1) \
635 : (&LINEMAPS_ORDINARY_MAPS (SET)[(MAP)->d.ordinary.included_from]))
637 /* Nonzero if the map is at the bottom of the include stack. */
638 #define MAIN_FILE_P(MAP) \
639 ((linemap_check_ordinary (MAP)->d.ordinary.included_from < 0))
641 #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
643 /* Assertion macro to be used in line-map code. */
644 #define linemap_assert(EXPR) \
650 /* Assert that becomes a conditional expression when checking is disabled at
651 compilation time. Use this for conditions that should not happen but if
652 they happen, it is better to handle them gracefully rather than crash
656 if (linemap_assert_fails(EXPR)) handle_error(); */
657 #define linemap_assert_fails(EXPR) __extension__ \
658 ({linemap_assert (EXPR); false;})
660 /* Assert that MAP encodes locations of tokens that are not part of
661 the replacement-list of a macro expansion. */
662 #define linemap_check_ordinary(LINE_MAP) __extension__ \
663 ({linemap_assert (!linemap_macro_expansion_map_p (LINE_MAP)); \
666 /* Include EXPR, so that unused variable warnings do not occur. */
667 #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
668 #define linemap_assert_fails(EXPR) (! (EXPR))
669 #define linemap_check_ordinary(LINE_MAP) (LINE_MAP)
672 /* Encode and return a source_location from a column number. The
673 source line considered is the last source line used to call
674 linemap_line_start, i.e, the last source line which a location was
676 extern source_location
677 linemap_position_for_column (struct line_maps
*, unsigned int);
679 /* Encode and return a source location from a given line and
682 linemap_position_for_line_and_column (const struct line_map
*,
683 linenum_type
, unsigned int);
685 /* Encode and return a source_location starting from location LOC and
686 shifting it by OFFSET columns. This function does not support
687 virtual locations. */
689 linemap_position_for_loc_and_offset (struct line_maps
*set
,
691 unsigned int offset
);
693 /* Return the file this map is for. */
694 #define LINEMAP_FILE(MAP) \
695 (linemap_check_ordinary (MAP)->d.ordinary.to_file)
697 /* Return the line number this map started encoding location from. */
698 #define LINEMAP_LINE(MAP) \
699 (linemap_check_ordinary (MAP)->d.ordinary.to_line)
701 /* Return a positive value if map encodes locations from a system
702 header, 0 otherwise. Returns 1 if MAP encodes locations in a
703 system header and 2 if it encodes locations in a C system header
704 that therefore needs to be extern "C" protected in C++. */
705 #define LINEMAP_SYSP(MAP) \
706 (linemap_check_ordinary (MAP)->d.ordinary.sysp)
708 /* Return a positive value if PRE denotes the location of a token that
709 comes before the token of POST, 0 if PRE denotes the location of
710 the same token as the token for POST, and a negative value
712 int linemap_compare_locations (struct line_maps
*set
,
714 source_location post
);
716 /* Return TRUE if LOC_A denotes the location a token that comes
717 topogically before the token denoted by location LOC_B, or if they
719 #define linemap_location_before_p(SET, LOC_A, LOC_B) \
720 (linemap_compare_locations ((SET), (LOC_A), (LOC_B)) >= 0)
724 /* The name of the source file involved. */
727 /* The line-location in the source file. */
734 /* In a system header?. */
738 /* This is enum is used by the function linemap_resolve_location
739 below. The meaning of the values is explained in the comment of
741 enum location_resolution_kind
743 LRK_MACRO_EXPANSION_POINT
,
744 LRK_SPELLING_LOCATION
,
745 LRK_MACRO_DEFINITION_LOCATION
748 /* Resolve a virtual location into either a spelling location, an
749 expansion point location or a token argument replacement point
750 location. Return the map that encodes the virtual location as well
751 as the resolved location.
753 If LOC is *NOT* the location of a token resulting from the
754 expansion of a macro, then the parameter LRK (which stands for
755 Location Resolution Kind) is ignored and the resulting location
756 just equals the one given in argument.
758 Now if LOC *IS* the location of a token resulting from the
759 expansion of a macro, this is what happens.
761 * If LRK is set to LRK_MACRO_EXPANSION_POINT
762 -------------------------------
764 The virtual location is resolved to the first macro expansion point
765 that led to this macro expansion.
767 * If LRK is set to LRK_SPELLING_LOCATION
768 -------------------------------------
770 The virtual location is resolved to the locus where the token has
771 been spelled in the source. This can follow through all the macro
772 expansions that led to the token.
774 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
775 --------------------------------------
777 The virtual location is resolved to the locus of the token in the
778 context of the macro definition.
780 If LOC is the locus of a token that is an argument of a
781 function-like macro [replacing a parameter in the replacement list
782 of the macro] the virtual location is resolved to the locus of the
783 parameter that is replaced, in the context of the definition of the
786 If LOC is the locus of a token that is not an argument of a
787 function-like macro, then the function behaves as if LRK was set to
788 LRK_SPELLING_LOCATION.
790 If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
791 returned location. Note that if the returned location wasn't originally
792 encoded by a map, the *MAP is set to NULL. This can happen if LOC
793 resolves to a location reserved for the client code, like
794 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
796 source_location
linemap_resolve_location (struct line_maps
*,
798 enum location_resolution_kind lrk
,
799 const struct line_map
**loc_map
);
801 /* Suppose that LOC is the virtual location of a token coming from the
802 expansion of a macro M. This function then steps up to get the
803 location L of the point where M got expanded. If L is a spelling
804 location inside a macro expansion M', then this function returns
805 the point where M' was expanded. LOC_MAP is an output parameter.
806 When non-NULL, *LOC_MAP is set to the map of the returned
808 source_location
linemap_unwind_toward_expansion (struct line_maps
*,
810 const struct line_map
**loc_map
);
812 /* If LOC is the virtual location of a token coming from the expansion
813 of a macro M and if its spelling location is reserved (e.g, a
814 location for a built-in token), then this function unwinds (using
815 linemap_unwind_toward_expansion) the location until a location that
816 is not reserved and is not in a system header is reached. In other
817 words, this unwinds the reserved location until a location that is
818 in real source code is reached.
820 Otherwise, if the spelling location for LOC is not reserved or if
821 LOC doesn't come from the expansion of a macro, the function
822 returns LOC as is and *MAP is not touched.
824 *MAP is set to the map of the returned location if the later is
825 different from LOC. */
826 source_location
linemap_unwind_to_first_non_reserved_loc (struct line_maps
*,
828 const struct line_map
**map
);
830 /* Expand source code location LOC and return a user readable source
831 code location. LOC must be a spelling (non-virtual) location. If
832 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
833 location is returned. */
834 expanded_location
linemap_expand_location (struct line_maps
*,
835 const struct line_map
*,
836 source_location loc
);
838 /* Statistics about maps allocation and usage as returned by
839 linemap_get_statistics. */
842 long num_ordinary_maps_allocated
;
843 long num_ordinary_maps_used
;
844 long ordinary_maps_allocated_size
;
845 long ordinary_maps_used_size
;
846 long num_expanded_macros
;
847 long num_macro_tokens
;
848 long num_macro_maps_used
;
849 long macro_maps_allocated_size
;
850 long macro_maps_used_size
;
851 long macro_maps_locations_size
;
852 long duplicated_macro_maps_locations_size
;
855 /* Return the highest location emitted for a given file for which
856 there is a line map in SET. FILE_NAME is the file name to
857 consider. If the function returns TRUE, *LOC is set to the highest
858 location emitted for that file. */
859 bool linemap_get_file_highest_location (struct line_maps
* set
,
860 const char *file_name
,
861 source_location
*loc
);
863 /* Compute and return statistics about the memory consumption of some
864 parts of the line table SET. */
865 void linemap_get_statistics (struct line_maps
*, struct linemap_stats
*);
867 /* Dump debugging information about source location LOC into the file
868 stream STREAM. SET is the line map set LOC comes from. */
869 void linemap_dump_location (struct line_maps
*, source_location
, FILE *);
871 /* Dump line map at index IX in line table SET to STREAM. If STREAM
872 is NULL, use stderr. IS_MACRO is true if the caller wants to
873 dump a macro map, false otherwise. */
874 void linemap_dump (FILE *, struct line_maps
*, unsigned, bool);
876 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
877 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
878 specifies how many macro maps to dump. */
879 void line_table_dump (FILE *, struct line_maps
*, unsigned int, unsigned int);
881 #endif /* !LIBCPP_LINE_MAP_H */