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 To see how this works in practice, see the worked example in
118 libcpp/location-example.txt. */
119 typedef unsigned int source_location
;
121 /* Memory allocation function typedef. Works like xrealloc. */
122 typedef void *(*line_map_realloc
) (void *, size_t);
124 /* Memory allocator function that returns the actual allocated size,
125 for a given requested allocation. */
126 typedef size_t (*line_map_round_alloc_size_func
) (size_t);
128 /* An ordinary line map encodes physical source locations. Those
129 physical source locations are called "spelling locations".
131 Physical source file TO_FILE at line TO_LINE at column 0 is represented
132 by the logical START_LOCATION. TO_LINE+L at column C is represented by
133 START_LOCATION+(L*(1<<column_bits))+C, as long as C<(1<<column_bits),
134 and the result_location is less than the next line_map's start_location.
135 (The top line is line 1 and the leftmost column is column 1; line/column 0
136 means "entire file/line" or "unknown line/column" or "not applicable".)
138 The highest possible source location is MAX_SOURCE_LOCATION. */
139 struct GTY(()) line_map_ordinary
{
141 linenum_type to_line
;
143 /* An index into the set that gives the line mapping at whose end
144 the current one was included. File(s) at the bottom of the
145 include stack have this set to -1. */
148 /* SYSP is one for a system header, two for a C system header file
149 that therefore needs to be extern "C" protected in C++, and zero
150 otherwise. This field isn't really needed now that it's in
154 /* Number of the low-order source_location bits used for a column number. */
155 unsigned int column_bits
: 8;
158 /* This is the highest possible source location encoded within an
159 ordinary or macro map. */
160 const source_location MAX_SOURCE_LOCATION
= 0x7FFFFFFF;
164 /* A macro line map encodes location of tokens coming from a macro
167 Please note that this struct line_map_macro is a field of struct
168 line_map below, go read the comments of struct line_map below and
171 The offset from START_LOCATION is used to index into
172 MACRO_LOCATIONS; this holds the original location of the token. */
173 struct GTY(()) line_map_macro
{
174 /* The cpp macro which expansion gave birth to this macro map. */
175 struct cpp_hashnode
* GTY ((nested_ptr (union tree_node
,
176 "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
177 "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
180 /* The number of tokens inside the replacement-list of MACRO. */
181 unsigned int n_tokens
;
183 /* This array of location is actually an array of pairs of
184 locations. The elements inside it thus look like:
186 x0,y0, x1,y1, x2,y2, ...., xn,yn.
190 Remember that these xI,yI are collected when libcpp is about to
191 expand a given macro.
193 yI is the location in the macro definition, either of the token
194 itself or of a macro parameter that it replaces.
198 #define PLUS(A, B) A + B <--- #1
200 int a = PLUS (1,2); <--- #2
202 There is a macro map for the expansion of PLUS in #2. PLUS is
203 expanded into its expansion-list. The expansion-list is the
204 replacement-list of PLUS where the macro parameters are replaced
205 with their arguments. So the replacement-list of PLUS is made of
210 and the expansion-list is made of the tokens:
214 Let's consider the case of token "+". Its y1 [yI for I == 1] is
215 its spelling location in #1.
217 y0 (thus for token "1") is the spelling location of A in #1.
219 And y2 (of token "2") is the spelling location of B in #1.
221 When the token is /not/ an argument for a macro, xI is the same
222 location as yI. Otherwise, xI is the location of the token
223 outside this macro expansion. If this macro was expanded from
224 another macro expansion, xI is a virtual location representing
225 the token in that macro expansion; otherwise, it is the spelling
226 location of the token.
228 Note that a virtual location is a location returned by
229 linemap_add_macro_token. It encodes the relevant locations (x,y
230 pairs) of that token across the macro expansions from which it
231 (the token) might come from.
233 In the example above x1 (for token "+") is going to be the same
234 as y1. x0 is the spelling location for the argument token "1",
235 and x2 is the spelling location for the argument token "2". */
236 source_location
* GTY((atomic
)) macro_locations
;
238 /* This is the location of the expansion point of the current macro
239 map. It's the location of the macro name. That location is held
240 by the map that was current right before the current one. It
241 could have been either a macro or an ordinary map, depending on
242 if we are in a nested expansion context not. */
243 source_location expansion
;
246 /* A line_map encodes a sequence of locations.
247 There are two kinds of maps. Ordinary maps and macro expansion
248 maps, a.k.a macro maps.
250 A macro map encodes source locations of tokens that are part of a
251 macro replacement-list, at a macro expansion point. E.g, in:
253 #define PLUS(A,B) A + B
255 No macro map is going to be created there, because we are not at a
256 macro expansion point. We are at a macro /definition/ point. So the
257 locations of the tokens of the macro replacement-list (i.e, A + B)
258 will be locations in an ordinary map, not a macro map.
260 On the other hand, if we later do:
264 The invocation of PLUS here is a macro expansion. So we are at a
265 macro expansion point. The preprocessor expands PLUS (1,2) and
266 replaces it with the tokens of its replacement-list: 1 + 2. A macro
267 map is going to be created to hold (or rather to map, haha ...) the
268 locations of the tokens 1, + and 2. The macro map also records the
269 location of the expansion point of PLUS. That location is mapped in
270 the map that is active right before the location of the invocation
272 struct GTY(()) line_map
{
273 source_location start_location
;
275 /* The reason for creation of this line map. */
276 ENUM_BITFIELD (lc_reason
) reason
: CHAR_BIT
;
279 struct line_map_ordinary
GTY((tag ("0"))) ordinary
;
280 struct line_map_macro
GTY((tag ("1"))) macro
;
281 } GTY((desc ("%1.reason == LC_ENTER_MACRO"))) d
;
284 #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
286 /* Assertion macro to be used in line-map code. */
287 #define linemap_assert(EXPR) \
293 /* Assert that becomes a conditional expression when checking is disabled at
294 compilation time. Use this for conditions that should not happen but if
295 they happen, it is better to handle them gracefully rather than crash
299 if (linemap_assert_fails(EXPR)) handle_error(); */
300 #define linemap_assert_fails(EXPR) __extension__ \
301 ({linemap_assert (EXPR); false;})
304 /* Include EXPR, so that unused variable warnings do not occur. */
305 #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
306 #define linemap_assert_fails(EXPR) (! (EXPR))
309 /* Return TRUE if MAP encodes locations coming from a macro
310 replacement-list at macro expansion point. */
312 linemap_macro_expansion_map_p (const struct line_map
*);
314 /* Assert that MAP encodes locations of tokens that are not part of
315 the replacement-list of a macro expansion. */
316 inline struct line_map
*
317 linemap_check_ordinary (struct line_map
*map
)
319 linemap_assert (!linemap_macro_expansion_map_p (map
));
323 /* Assert that MAP encodes locations of tokens that are not part of
324 the replacement-list of a macro expansion. */
326 inline const struct line_map
*
327 linemap_check_ordinary (const struct line_map
*map
)
329 linemap_assert (!linemap_macro_expansion_map_p (map
));
333 /* Read the start location of MAP, as an rvalue. */
335 inline source_location
336 MAP_START_LOCATION (const line_map
*map
)
338 return map
->start_location
;
341 /* Access the start location of MAP as a reference
342 (e.g. as an lvalue). */
344 inline source_location
&
345 MAP_START_LOCATION (line_map
*map
)
347 return map
->start_location
;
350 /* Get the starting line number of ordinary map MAP. */
353 ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map
*map
)
355 return linemap_check_ordinary (map
)->d
.ordinary
.to_line
;
358 /* Access the starting line number of ordinary map MAP by
359 reference (e.g. as an lvalue). */
362 ORDINARY_MAP_STARTING_LINE_NUMBER (line_map
*map
)
364 return linemap_check_ordinary (map
)->d
.ordinary
.to_line
;
367 /* Get the index of the ordinary map at whose end
368 ordinary map MAP was included.
370 File(s) at the bottom of the include stack have this set. */
373 ORDINARY_MAP_INCLUDER_FILE_INDEX (const line_map
*map
)
375 return linemap_check_ordinary (map
)->d
.ordinary
.included_from
;
378 /* As above, but by reference (e.g. as an lvalue). */
381 ORDINARY_MAP_INCLUDER_FILE_INDEX (line_map
*map
)
383 return linemap_check_ordinary (map
)->d
.ordinary
.included_from
;
386 /* Return a positive value if map encodes locations from a system
387 header, 0 otherwise. Returns 1 if ordinary map MAP encodes locations
388 in a system header and 2 if it encodes locations in a C system header
389 that therefore needs to be extern "C" protected in C++. */
392 ORDINARY_MAP_IN_SYSTEM_HEADER_P (const line_map
*map
)
394 return linemap_check_ordinary (map
)->d
.ordinary
.sysp
;
397 /* As above, but by reference (e.g. as an lvalue). */
399 inline unsigned char &
400 ORDINARY_MAP_IN_SYSTEM_HEADER_P (line_map
*map
)
402 return linemap_check_ordinary (map
)->d
.ordinary
.sysp
;
405 /* Get the number of the low-order source_location bits used for a
406 column number within ordinary map MAP. */
409 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (const line_map
*map
)
411 return linemap_check_ordinary (map
)->d
.ordinary
.column_bits
;
414 /* Set the number of the low-order source_location bits used for a
415 column number within ordinary map MAP. */
418 SET_ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (line_map
*map
, int col_bits
)
420 linemap_check_ordinary (map
)->d
.ordinary
.column_bits
= col_bits
;
423 /* Get the filename of ordinary map MAP. */
426 ORDINARY_MAP_FILE_NAME (const line_map
*map
)
428 return linemap_check_ordinary (map
)->d
.ordinary
.to_file
;
431 /* As above, but by reference (e.g. as an lvalue). */
433 inline const char * &
434 ORDINARY_MAP_FILE_NAME (line_map
*map
)
436 return linemap_check_ordinary (map
)->d
.ordinary
.to_file
;
439 /* Get the cpp macro whose expansion gave birth to macro map MAP. */
441 inline cpp_hashnode
*
442 MACRO_MAP_MACRO (const line_map
*map
)
444 return map
->d
.macro
.macro
;
447 /* As above, but by reference (e.g. as an lvalue). */
449 inline cpp_hashnode
* &
450 MACRO_MAP_MACRO (line_map
*map
)
452 return map
->d
.macro
.macro
;
455 /* Get the number of tokens inside the replacement-list of the macro
456 that led to macro map MAP. */
459 MACRO_MAP_NUM_MACRO_TOKENS (const line_map
*map
)
461 return map
->d
.macro
.n_tokens
;
464 /* As above, but by reference (e.g. as an lvalue). */
466 inline unsigned int &
467 MACRO_MAP_NUM_MACRO_TOKENS (line_map
*map
)
469 return map
->d
.macro
.n_tokens
;
472 /* Get the array of pairs of locations within macro map MAP.
473 See the declaration of line_map_macro for more information. */
475 inline source_location
*
476 MACRO_MAP_LOCATIONS (const line_map
*map
)
478 return map
->d
.macro
.macro_locations
;
481 /* As above, but by reference (e.g. as an lvalue). */
483 inline source_location
* &
484 MACRO_MAP_LOCATIONS (line_map
*map
)
486 return map
->d
.macro
.macro_locations
;
489 /* Get the location of the expansion point of the macro map MAP. */
491 inline source_location
492 MACRO_MAP_EXPANSION_POINT_LOCATION (const line_map
*map
)
494 return map
->d
.macro
.expansion
;
497 /* As above, but by reference (e.g. as an lvalue). */
499 inline source_location
&
500 MACRO_MAP_EXPANSION_POINT_LOCATION (line_map
*map
)
502 return map
->d
.macro
.expansion
;
505 /* The abstraction of a set of location maps. There can be several
506 types of location maps. This abstraction contains the attributes
507 that are independent from the type of the map. */
508 struct GTY(()) maps_info
{
509 /* This array contains the different line maps.
510 A line map is created for the following events:
511 - when a new preprocessing unit start.
512 - when a preprocessing unit ends.
513 - when a macro expansion occurs. */
514 struct line_map
* GTY ((length ("%h.used"))) maps
;
516 /* The total number of allocated maps. */
517 unsigned int allocated
;
519 /* The number of elements used in maps. This number is smaller
520 or equal to ALLOCATED. */
526 /* Data structure to associate an arbitrary data to a source location. */
527 struct GTY(()) location_adhoc_data
{
528 source_location locus
;
529 void * GTY((skip
)) data
;
534 /* The following data structure encodes a location with some adhoc data
535 and maps it to a new unsigned integer (called an adhoc location)
536 that replaces the original location to represent the mapping.
538 The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
539 highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
540 the original location. Once identified as the adhoc_loc, the lower 31
541 bits of the integer is used to index the location_adhoc_data array,
542 in which the locus and associated data is stored. */
544 struct GTY(()) location_adhoc_data_map
{
545 struct htab
* GTY((skip
)) htab
;
546 source_location curr_loc
;
547 unsigned int allocated
;
548 struct location_adhoc_data
GTY((length ("%h.allocated"))) *data
;
551 /* A set of chronological line_map structures. */
552 struct GTY(()) line_maps
{
554 struct maps_info info_ordinary
;
556 struct maps_info info_macro
;
558 /* Depth of the include stack, including the current file. */
561 /* If true, prints an include trace a la -H. */
564 /* Highest source_location "given out". */
565 source_location highest_location
;
567 /* Start of line of highest source_location "given out". */
568 source_location highest_line
;
570 /* The maximum column number we can quickly allocate. Higher numbers
571 may require allocating a new line_map. */
572 unsigned int max_column_hint
;
574 /* If non-null, the allocator to use when resizing 'maps'. If null,
576 line_map_realloc reallocator
;
578 /* The allocators' function used to know the actual size it
579 allocated, for a certain allocation size requested. */
580 line_map_round_alloc_size_func round_alloc_size
;
582 struct location_adhoc_data_map location_adhoc_data_map
;
584 /* The special location value that is used as spelling location for
586 source_location builtin_location
;
588 /* True if we've seen a #line or # 44 "file" directive. */
589 bool seen_line_directive
;
592 /* Returns the pointer to the memory region where information about
593 maps are stored in the line table SET. MACRO_MAP_P is a flag
594 telling if we want macro or ordinary maps. */
595 inline struct maps_info
*
596 LINEMAPS_MAP_INFO (line_maps
*set
, bool macro_map_p
)
600 : &(set
->info_ordinary
));
603 /* As above, but preserving constness. */
605 inline const struct maps_info
*
606 LINEMAPS_MAP_INFO (const line_maps
*set
, bool macro_map_p
)
610 : &(set
->info_ordinary
));
613 /* Returns the pointer to the memory region where maps are stored in
614 the line table SET. MAP_KIND shall be TRUE if we are interested in
615 macro maps false otherwise. */
617 LINEMAPS_MAPS (const line_maps
*set
, bool map_kind
)
619 return LINEMAPS_MAP_INFO (set
, map_kind
)->maps
;
622 /* As above, but by reference (e.g. as an lvalue). */
625 LINEMAPS_MAPS (line_maps
*set
, bool map_kind
)
627 return LINEMAPS_MAP_INFO (set
, map_kind
)->maps
;
630 /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
631 if we are interested in macro maps, FALSE otherwise. */
633 LINEMAPS_ALLOCATED (const line_maps
*set
, bool map_kind
)
635 return LINEMAPS_MAP_INFO (set
, map_kind
)->allocated
;
638 /* As above, but by reference (e.g. as an lvalue). */
640 inline unsigned int &
641 LINEMAPS_ALLOCATED (line_maps
*set
, bool map_kind
)
643 return LINEMAPS_MAP_INFO (set
, map_kind
)->allocated
;
646 /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
647 we are interested in macro maps, FALSE otherwise.*/
649 LINEMAPS_USED (const line_maps
*set
, bool map_kind
)
651 return LINEMAPS_MAP_INFO (set
, map_kind
)->used
;
654 /* As above, but by reference (e.g. as an lvalue). */
656 inline unsigned int &
657 LINEMAPS_USED (line_maps
*set
, bool map_kind
)
659 return LINEMAPS_MAP_INFO (set
, map_kind
)->used
;
662 /* Returns the index of the last map that was looked up with
663 linemap_lookup. MAP_KIND shall be TRUE if we are interested in
664 macro maps, FALSE otherwise. */
666 LINEMAPS_CACHE (const line_maps
*set
, bool map_kind
)
668 return LINEMAPS_MAP_INFO (set
, map_kind
)->cache
;
671 /* As above, but by reference (e.g. as an lvalue). */
673 inline unsigned int &
674 LINEMAPS_CACHE (line_maps
*set
, bool map_kind
)
676 return LINEMAPS_MAP_INFO (set
, map_kind
)->cache
;
679 /* Return the map at a given index. */
681 LINEMAPS_MAP_AT (const line_maps
*set
, bool map_kind
, int index
)
683 return &(LINEMAPS_MAPS (set
, map_kind
)[index
]);
686 /* Returns the last map used in the line table SET. MAP_KIND
687 shall be TRUE if we are interested in macro maps, FALSE
690 LINEMAPS_LAST_MAP (const line_maps
*set
, bool map_kind
)
692 return LINEMAPS_MAP_AT (set
, map_kind
,
693 LINEMAPS_USED (set
, map_kind
) - 1);
696 /* Returns the last map that was allocated in the line table SET.
697 MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
700 LINEMAPS_LAST_ALLOCATED_MAP (const line_maps
*set
, bool map_kind
)
702 return LINEMAPS_MAP_AT (set
, map_kind
,
703 LINEMAPS_ALLOCATED (set
, map_kind
) - 1);
706 /* Returns a pointer to the memory region where ordinary maps are
707 allocated in the line table SET. */
709 LINEMAPS_ORDINARY_MAPS (const line_maps
*set
)
711 return LINEMAPS_MAPS (set
, false);
714 /* Returns the INDEXth ordinary map. */
716 LINEMAPS_ORDINARY_MAP_AT (const line_maps
*set
, int index
)
718 return LINEMAPS_MAP_AT (set
, false, index
);
721 /* Return the number of ordinary maps allocated in the line table
724 LINEMAPS_ORDINARY_ALLOCATED (const line_maps
*set
)
726 return LINEMAPS_ALLOCATED (set
, false);
729 /* Return the number of ordinary maps used in the line table SET. */
731 LINEMAPS_ORDINARY_USED (const line_maps
*set
)
733 return LINEMAPS_USED (set
, false);
736 /* Return the index of the last ordinary map that was looked up with
739 LINEMAPS_ORDINARY_CACHE (const line_maps
*set
)
741 return LINEMAPS_CACHE (set
, false);
744 /* As above, but by reference (e.g. as an lvalue). */
746 inline unsigned int &
747 LINEMAPS_ORDINARY_CACHE (line_maps
*set
)
749 return LINEMAPS_CACHE (set
, false);
752 /* Returns a pointer to the last ordinary map used in the line table
755 LINEMAPS_LAST_ORDINARY_MAP (const line_maps
*set
)
757 return LINEMAPS_LAST_MAP (set
, false);
760 /* Returns a pointer to the last ordinary map allocated the line table
763 LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP (const line_maps
*set
)
765 return LINEMAPS_LAST_ALLOCATED_MAP (set
, false);
768 /* Returns a pointer to the beginning of the region where macro maps
771 LINEMAPS_MACRO_MAPS (const line_maps
*set
)
773 return LINEMAPS_MAPS (set
, true);
776 /* Returns the INDEXth macro map. */
778 LINEMAPS_MACRO_MAP_AT (const line_maps
*set
, int index
)
780 return LINEMAPS_MAP_AT (set
, true, index
);
783 /* Returns the number of macro maps that were allocated in the line
786 LINEMAPS_MACRO_ALLOCATED (const line_maps
*set
)
788 return LINEMAPS_ALLOCATED (set
, true);
791 /* Returns the number of macro maps used in the line table SET. */
793 LINEMAPS_MACRO_USED (const line_maps
*set
)
795 return LINEMAPS_USED (set
, true);
798 /* Returns the index of the last macro map looked up with
801 LINEMAPS_MACRO_CACHE (const line_maps
*set
)
803 return LINEMAPS_CACHE (set
, true);
806 /* As above, but by reference (e.g. as an lvalue). */
808 inline unsigned int &
809 LINEMAPS_MACRO_CACHE (line_maps
*set
)
811 return LINEMAPS_CACHE (set
, true);
814 /* Returns the last macro map used in the line table SET. */
816 LINEMAPS_LAST_MACRO_MAP (const line_maps
*set
)
818 return LINEMAPS_LAST_MAP (set
, true);
821 /* Returns the lowest location [of a token resulting from macro
822 expansion] encoded in this line table. */
823 inline source_location
824 LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps
*set
)
826 return LINEMAPS_MACRO_USED (set
)
827 ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set
))
828 : MAX_SOURCE_LOCATION
;
831 /* Returns the last macro map allocated in the line table SET. */
833 LINEMAPS_LAST_ALLOCATED_MACRO_MAP (const line_maps
*set
)
835 return LINEMAPS_LAST_ALLOCATED_MAP (set
, true);
838 extern void location_adhoc_data_fini (struct line_maps
*);
839 extern source_location
get_combined_adhoc_loc (struct line_maps
*,
840 source_location
, void *);
841 extern void *get_data_from_adhoc_loc (struct line_maps
*, source_location
);
842 extern source_location
get_location_from_adhoc_loc (struct line_maps
*,
845 /* Get whether location LOC is an ad-hoc location. */
848 IS_ADHOC_LOC (source_location loc
)
850 return (loc
& MAX_SOURCE_LOCATION
) != loc
;
853 /* Combine LOC and BLOCK, giving a combined adhoc location. */
855 inline source_location
856 COMBINE_LOCATION_DATA (struct line_maps
*set
,
860 return get_combined_adhoc_loc (set
, loc
, block
);
863 extern void rebuild_location_adhoc_htab (struct line_maps
*);
865 /* Initialize a line map set. SET is the line map set to initialize
866 and BUILTIN_LOCATION is the special location value to be used as
867 spelling location for built-in tokens. This BUILTIN_LOCATION has
868 to be strictly less than RESERVED_LOCATION_COUNT. */
869 extern void linemap_init (struct line_maps
*set
,
870 source_location builtin_location
);
872 /* Check for and warn about line_maps entered but not exited. */
874 extern void linemap_check_files_exited (struct line_maps
*);
876 /* Return a source_location for the start (i.e. column==0) of
877 (physical) line TO_LINE in the current source file (as in the
878 most recent linemap_add). MAX_COLUMN_HINT is the highest column
879 number we expect to use in this line (but it does not change
880 the highest_location). */
882 extern source_location linemap_line_start
883 (struct line_maps
*set
, linenum_type to_line
, unsigned int max_column_hint
);
885 /* Add a mapping of logical source line to physical source file and
886 line number. This function creates an "ordinary map", which is a
887 map that records locations of tokens that are not part of macro
888 replacement-lists present at a macro expansion point.
890 The text pointed to by TO_FILE must have a lifetime
891 at least as long as the lifetime of SET. An empty
892 TO_FILE means standard input. If reason is LC_LEAVE, and
893 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
894 natural values considering the file we are returning to.
896 A call to this function can relocate the previous set of
897 maps, so any stored line_map pointers should not be used. */
898 extern const struct line_map
*linemap_add
899 (struct line_maps
*, enum lc_reason
, unsigned int sysp
,
900 const char *to_file
, linenum_type to_line
);
902 /* Given a logical source location, returns the map which the
903 corresponding (source file, line, column) triplet can be deduced
904 from. Since the set is built chronologically, the logical lines are
905 monotonic increasing, and so the list is sorted and we can use a
906 binary search. If no line map have been allocated yet, this
907 function returns NULL. */
908 extern const struct line_map
*linemap_lookup
909 (struct line_maps
*, source_location
);
911 /* Returns TRUE if the line table set tracks token locations across
912 macro expansion, FALSE otherwise. */
913 bool linemap_tracks_macro_expansion_locs_p (struct line_maps
*);
915 /* Return the name of the macro associated to MACRO_MAP. */
916 const char* linemap_map_get_macro_name (const struct line_map
*);
918 /* Return a positive value if LOCATION is the locus of a token that is
919 located in a system header, O otherwise. It returns 1 if LOCATION
920 is the locus of a token that is located in a system header, and 2
921 if LOCATION is the locus of a token located in a C system header
922 that therefore needs to be extern "C" protected in C++.
924 Note that this function returns 1 if LOCATION belongs to a token
925 that is part of a macro replacement-list defined in a system
926 header, but expanded in a non-system file. */
927 int linemap_location_in_system_header_p (struct line_maps
*,
930 /* Return TRUE if LOCATION is a source code location of a token coming
931 from a macro replacement-list at a macro expansion point, FALSE
933 bool linemap_location_from_macro_expansion_p (const struct line_maps
*,
936 /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
937 be reserved for libcpp user as special values, no token from libcpp
938 will contain any of those locations. */
939 const int RESERVED_LOCATION_COUNT
= 2;
941 /* Converts a map and a source_location to source line. */
943 SOURCE_LINE (const struct line_map
*map
, source_location loc
)
945 return ((loc
- linemap_check_ordinary (map
)->start_location
)
946 >> map
->d
.ordinary
.column_bits
) + map
->d
.ordinary
.to_line
;
949 /* Convert a map and source_location to source column number. */
951 SOURCE_COLUMN (const struct line_map
*map
, source_location loc
)
953 return ((loc
- linemap_check_ordinary (map
)->start_location
)
954 & ((1 << map
->d
.ordinary
.column_bits
) - 1));
957 /* Return the location of the last source line within an ordinary
959 inline source_location
960 LAST_SOURCE_LINE_LOCATION (const struct line_map
*map
)
962 return (((linemap_check_ordinary (map
)[1].start_location
- 1
963 - map
->start_location
)
964 & ~((1 << map
->d
.ordinary
.column_bits
) - 1))
965 + map
->start_location
);
968 /* Returns the last source line number within an ordinary map. This
969 is the (last) line of the #include, or other directive, that caused
973 LAST_SOURCE_LINE (const struct line_map
*map
)
975 return SOURCE_LINE (map
, LAST_SOURCE_LINE_LOCATION (map
));
978 /* Return the last column number within an ordinary map. */
981 LAST_SOURCE_COLUMN (const struct line_map
*map
)
983 return SOURCE_COLUMN (map
, LAST_SOURCE_LINE_LOCATION (map
));
986 /* Returns the map a given map was included from, or NULL if the map
987 belongs to the main file, i.e, a file that wasn't included by
990 inline struct line_map
*
991 INCLUDED_FROM (struct line_maps
*set
, const struct line_map
*map
)
993 return ((linemap_check_ordinary (map
)->d
.ordinary
.included_from
== -1)
995 : (&LINEMAPS_ORDINARY_MAPS (set
)[(map
)->d
.ordinary
.included_from
]));
998 /* True if the map is at the bottom of the include stack. */
1001 MAIN_FILE_P (const struct line_map
*map
)
1003 return linemap_check_ordinary (map
)->d
.ordinary
.included_from
< 0;
1006 /* Encode and return a source_location from a column number. The
1007 source line considered is the last source line used to call
1008 linemap_line_start, i.e, the last source line which a location was
1010 extern source_location
1011 linemap_position_for_column (struct line_maps
*, unsigned int);
1013 /* Encode and return a source location from a given line and
1016 linemap_position_for_line_and_column (const struct line_map
*,
1017 linenum_type
, unsigned int);
1019 /* Encode and return a source_location starting from location LOC and
1020 shifting it by OFFSET columns. This function does not support
1021 virtual locations. */
1023 linemap_position_for_loc_and_offset (struct line_maps
*set
,
1024 source_location loc
,
1025 unsigned int offset
);
1027 /* Return the file this map is for. */
1029 LINEMAP_FILE (const struct line_map
*map
)
1031 return linemap_check_ordinary (map
)->d
.ordinary
.to_file
;
1034 /* Return the line number this map started encoding location from. */
1036 LINEMAP_LINE (const struct line_map
*map
)
1038 return linemap_check_ordinary (map
)->d
.ordinary
.to_line
;
1041 /* Return a positive value if map encodes locations from a system
1042 header, 0 otherwise. Returns 1 if MAP encodes locations in a
1043 system header and 2 if it encodes locations in a C system header
1044 that therefore needs to be extern "C" protected in C++. */
1045 inline unsigned char
1046 LINEMAP_SYSP (const struct line_map
*map
)
1048 return linemap_check_ordinary (map
)->d
.ordinary
.sysp
;
1051 /* Return a positive value if PRE denotes the location of a token that
1052 comes before the token of POST, 0 if PRE denotes the location of
1053 the same token as the token for POST, and a negative value
1055 int linemap_compare_locations (struct line_maps
*set
,
1056 source_location pre
,
1057 source_location post
);
1059 /* Return TRUE if LOC_A denotes the location a token that comes
1060 topogically before the token denoted by location LOC_B, or if they
1063 linemap_location_before_p (struct line_maps
*set
,
1064 source_location loc_a
,
1065 source_location loc_b
)
1067 return linemap_compare_locations (set
, loc_a
, loc_b
) >= 0;
1072 /* The name of the source file involved. */
1075 /* The line-location in the source file. */
1082 /* In a system header?. */
1084 } expanded_location
;
1086 /* This is enum is used by the function linemap_resolve_location
1087 below. The meaning of the values is explained in the comment of
1089 enum location_resolution_kind
1091 LRK_MACRO_EXPANSION_POINT
,
1092 LRK_SPELLING_LOCATION
,
1093 LRK_MACRO_DEFINITION_LOCATION
1096 /* Resolve a virtual location into either a spelling location, an
1097 expansion point location or a token argument replacement point
1098 location. Return the map that encodes the virtual location as well
1099 as the resolved location.
1101 If LOC is *NOT* the location of a token resulting from the
1102 expansion of a macro, then the parameter LRK (which stands for
1103 Location Resolution Kind) is ignored and the resulting location
1104 just equals the one given in argument.
1106 Now if LOC *IS* the location of a token resulting from the
1107 expansion of a macro, this is what happens.
1109 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1110 -------------------------------
1112 The virtual location is resolved to the first macro expansion point
1113 that led to this macro expansion.
1115 * If LRK is set to LRK_SPELLING_LOCATION
1116 -------------------------------------
1118 The virtual location is resolved to the locus where the token has
1119 been spelled in the source. This can follow through all the macro
1120 expansions that led to the token.
1122 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1123 --------------------------------------
1125 The virtual location is resolved to the locus of the token in the
1126 context of the macro definition.
1128 If LOC is the locus of a token that is an argument of a
1129 function-like macro [replacing a parameter in the replacement list
1130 of the macro] the virtual location is resolved to the locus of the
1131 parameter that is replaced, in the context of the definition of the
1134 If LOC is the locus of a token that is not an argument of a
1135 function-like macro, then the function behaves as if LRK was set to
1136 LRK_SPELLING_LOCATION.
1138 If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1139 returned location. Note that if the returned location wasn't originally
1140 encoded by a map, the *MAP is set to NULL. This can happen if LOC
1141 resolves to a location reserved for the client code, like
1142 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1144 source_location
linemap_resolve_location (struct line_maps
*,
1145 source_location loc
,
1146 enum location_resolution_kind lrk
,
1147 const struct line_map
**loc_map
);
1149 /* Suppose that LOC is the virtual location of a token coming from the
1150 expansion of a macro M. This function then steps up to get the
1151 location L of the point where M got expanded. If L is a spelling
1152 location inside a macro expansion M', then this function returns
1153 the point where M' was expanded. LOC_MAP is an output parameter.
1154 When non-NULL, *LOC_MAP is set to the map of the returned
1156 source_location
linemap_unwind_toward_expansion (struct line_maps
*,
1157 source_location loc
,
1158 const struct line_map
**loc_map
);
1160 /* If LOC is the virtual location of a token coming from the expansion
1161 of a macro M and if its spelling location is reserved (e.g, a
1162 location for a built-in token), then this function unwinds (using
1163 linemap_unwind_toward_expansion) the location until a location that
1164 is not reserved and is not in a system header is reached. In other
1165 words, this unwinds the reserved location until a location that is
1166 in real source code is reached.
1168 Otherwise, if the spelling location for LOC is not reserved or if
1169 LOC doesn't come from the expansion of a macro, the function
1170 returns LOC as is and *MAP is not touched.
1172 *MAP is set to the map of the returned location if the later is
1173 different from LOC. */
1174 source_location
linemap_unwind_to_first_non_reserved_loc (struct line_maps
*,
1175 source_location loc
,
1176 const struct line_map
**map
);
1178 /* Expand source code location LOC and return a user readable source
1179 code location. LOC must be a spelling (non-virtual) location. If
1180 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1181 location is returned. */
1182 expanded_location
linemap_expand_location (struct line_maps
*,
1183 const struct line_map
*,
1184 source_location loc
);
1186 /* Statistics about maps allocation and usage as returned by
1187 linemap_get_statistics. */
1188 struct linemap_stats
1190 long num_ordinary_maps_allocated
;
1191 long num_ordinary_maps_used
;
1192 long ordinary_maps_allocated_size
;
1193 long ordinary_maps_used_size
;
1194 long num_expanded_macros
;
1195 long num_macro_tokens
;
1196 long num_macro_maps_used
;
1197 long macro_maps_allocated_size
;
1198 long macro_maps_used_size
;
1199 long macro_maps_locations_size
;
1200 long duplicated_macro_maps_locations_size
;
1203 /* Return the highest location emitted for a given file for which
1204 there is a line map in SET. FILE_NAME is the file name to
1205 consider. If the function returns TRUE, *LOC is set to the highest
1206 location emitted for that file. */
1207 bool linemap_get_file_highest_location (struct line_maps
* set
,
1208 const char *file_name
,
1209 source_location
*loc
);
1211 /* Compute and return statistics about the memory consumption of some
1212 parts of the line table SET. */
1213 void linemap_get_statistics (struct line_maps
*, struct linemap_stats
*);
1215 /* Dump debugging information about source location LOC into the file
1216 stream STREAM. SET is the line map set LOC comes from. */
1217 void linemap_dump_location (struct line_maps
*, source_location
, FILE *);
1219 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1220 is NULL, use stderr. IS_MACRO is true if the caller wants to
1221 dump a macro map, false otherwise. */
1222 void linemap_dump (FILE *, struct line_maps
*, unsigned, bool);
1224 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1225 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1226 specifies how many macro maps to dump. */
1227 void line_table_dump (FILE *, struct line_maps
*, unsigned int, unsigned int);
1229 #endif /* !LIBCPP_LINE_MAP_H */