Add stats on adhoc table to dump_line_table_statistics
[official-gcc.git] / libcpp / include / line-map.h
blobc8618a970e1e5f507f39a9f88da073b897c5196c
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
7 later version.
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
25 #ifndef GTY
26 #define GTY(x) /* nothing */
27 #endif
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
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. */
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
58 assigned as follows:
60 Actual | Value | Meaning
61 -----------+-------------------------------+-------------------------------
62 0x00000000 | UNKNOWN_LOCATION (gcc/input.h)| Unknown/invalid location.
63 -----------+-------------------------------+-------------------------------
64 0x00000001 | BUILTINS_LOCATION | The location for declarations
65 | (gcc/input.h) | in "<built-in>"
66 -----------+-------------------------------+-------------------------------
67 0x00000002 | RESERVED_LOCATION_COUNT | The first location to be
68 | (also | handed out, and the
69 | ordmap[0]->start_location) | first line in ordmap 0
70 -----------+-------------------------------+-------------------------------
71 | ordmap[1]->start_location | First line in ordmap 1
72 | ordmap[1]->start_location+1 | First column in that line
73 | ordmap[1]->start_location+2 | 2nd column in that line
74 | | Subsequent lines are offset by
75 | | (1 << column_bits),
76 | | e.g. 128 for 7 bits, with a
77 | | column value of 0 representing
78 | | "the whole line".
79 | ordmap[2]->start_location-1 | Final location in ordmap 1
80 -----------+-------------------------------+-------------------------------
81 | ordmap[2]->start_location | First line in ordmap 2
82 | ordmap[3]->start_location-1 | Final location in ordmap 2
83 -----------+-------------------------------+-------------------------------
84 | | (etc)
85 -----------+-------------------------------+-------------------------------
86 | ordmap[n-1]->start_location | First line in final ord map
87 | | (etc)
88 | set->highest_location - 1 | Final location in that ordmap
89 -----------+-------------------------------+-------------------------------
90 | set->highest_location | Location of the where the next
91 | | ordinary linemap would start
92 -----------+-------------------------------+-------------------------------
93 | |
94 | VVVVVVVVVVVVVVVVVVVVVVVVVVV
95 | Ordinary maps grow this way
97 | (unallocated integers)
99 0x60000000 | LINE_MAP_MAX_LOCATION_WITH_COLS
100 | Beyond this point, ordinary linemaps have 0 bits per column:
101 | each increment of the value corresponds to a new source line.
103 0x70000000 | LINE_MAP_MAX_SOURCE_LOCATION
104 | Beyond the point, we give up on ordinary maps; attempts to
105 | create locations in them lead to UNKNOWN_LOCATION (0).
107 | (unallocated integers)
109 | Macro maps grow this way
110 | ^^^^^^^^^^^^^^^^^^^^^^^^
112 -----------+-------------------------------+-------------------------------
113 | LINEMAPS_MACRO_LOWEST_LOCATION| Locations within macro maps
114 | macromap[m-1]->start_location | Start of last macro map
116 -----------+-------------------------------+-------------------------------
117 | macromap[m-2]->start_location | Start of penultimate macro map
118 -----------+-------------------------------+-------------------------------
119 | macromap[1]->start_location | Start of macro map 1
120 -----------+-------------------------------+-------------------------------
121 | macromap[0]->start_location | Start of macro map 0
122 0x7fffffff | MAX_SOURCE_LOCATION | Also used as a mask for
123 | | accessing the ad-hoc data table
124 -----------+-------------------------------+-------------------------------
125 0x80000000 | Start of ad-hoc values; the lower 31 bits are used as an index
126 ... | into the line_table->location_adhoc_data_map.data array.
127 0xffffffff | UINT_MAX |
128 -----------+-------------------------------+-------------------------------
130 To see how this works in practice, see the worked example in
131 libcpp/location-example.txt. */
132 typedef unsigned int source_location;
134 /* Memory allocation function typedef. Works like xrealloc. */
135 typedef void *(*line_map_realloc) (void *, size_t);
137 /* Memory allocator function that returns the actual allocated size,
138 for a given requested allocation. */
139 typedef size_t (*line_map_round_alloc_size_func) (size_t);
141 /* A line_map encodes a sequence of locations.
142 There are two kinds of maps. Ordinary maps and macro expansion
143 maps, a.k.a macro maps.
145 A macro map encodes source locations of tokens that are part of a
146 macro replacement-list, at a macro expansion point. E.g, in:
148 #define PLUS(A,B) A + B
150 No macro map is going to be created there, because we are not at a
151 macro expansion point. We are at a macro /definition/ point. So the
152 locations of the tokens of the macro replacement-list (i.e, A + B)
153 will be locations in an ordinary map, not a macro map.
155 On the other hand, if we later do:
157 int a = PLUS (1,2);
159 The invocation of PLUS here is a macro expansion. So we are at a
160 macro expansion point. The preprocessor expands PLUS (1,2) and
161 replaces it with the tokens of its replacement-list: 1 + 2. A macro
162 map is going to be created to hold (or rather to map, haha ...) the
163 locations of the tokens 1, + and 2. The macro map also records the
164 location of the expansion point of PLUS. That location is mapped in
165 the map that is active right before the location of the invocation
166 of PLUS. */
167 struct GTY((tag ("0"), desc ("%h.reason == LC_ENTER_MACRO ? 2 : 1"))) line_map {
168 source_location start_location;
170 /* The reason for creation of this line map. */
171 ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
174 /* An ordinary line map encodes physical source locations. Those
175 physical source locations are called "spelling locations".
177 Physical source file TO_FILE at line TO_LINE at column 0 is represented
178 by the logical START_LOCATION. TO_LINE+L at column C is represented by
179 START_LOCATION+(L*(1<<column_bits))+C, as long as C<(1<<column_bits),
180 and the result_location is less than the next line_map's start_location.
181 (The top line is line 1 and the leftmost column is column 1; line/column 0
182 means "entire file/line" or "unknown line/column" or "not applicable".)
184 The highest possible source location is MAX_SOURCE_LOCATION. */
185 struct GTY((tag ("1"))) line_map_ordinary : public line_map {
186 const char *to_file;
187 linenum_type to_line;
189 /* An index into the set that gives the line mapping at whose end
190 the current one was included. File(s) at the bottom of the
191 include stack have this set to -1. */
192 int included_from;
194 /* SYSP is one for a system header, two for a C system header file
195 that therefore needs to be extern "C" protected in C++, and zero
196 otherwise. This field isn't really needed now that it's in
197 cpp_buffer. */
198 unsigned char sysp;
200 /* Number of the low-order source_location bits used for a column number. */
201 unsigned int column_bits : 8;
204 /* This is the highest possible source location encoded within an
205 ordinary or macro map. */
206 const source_location MAX_SOURCE_LOCATION = 0x7FFFFFFF;
208 struct cpp_hashnode;
210 /* A macro line map encodes location of tokens coming from a macro
211 expansion.
213 The offset from START_LOCATION is used to index into
214 MACRO_LOCATIONS; this holds the original location of the token. */
215 struct GTY((tag ("2"))) line_map_macro : public line_map {
216 /* The cpp macro which expansion gave birth to this macro map. */
217 struct cpp_hashnode * GTY ((nested_ptr (union tree_node,
218 "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
219 "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
220 macro;
222 /* The number of tokens inside the replacement-list of MACRO. */
223 unsigned int n_tokens;
225 /* This array of location is actually an array of pairs of
226 locations. The elements inside it thus look like:
228 x0,y0, x1,y1, x2,y2, ...., xn,yn.
230 where n == n_tokens;
232 Remember that these xI,yI are collected when libcpp is about to
233 expand a given macro.
235 yI is the location in the macro definition, either of the token
236 itself or of a macro parameter that it replaces.
238 Imagine this:
240 #define PLUS(A, B) A + B <--- #1
242 int a = PLUS (1,2); <--- #2
244 There is a macro map for the expansion of PLUS in #2. PLUS is
245 expanded into its expansion-list. The expansion-list is the
246 replacement-list of PLUS where the macro parameters are replaced
247 with their arguments. So the replacement-list of PLUS is made of
248 the tokens:
250 A, +, B
252 and the expansion-list is made of the tokens:
254 1, +, 2
256 Let's consider the case of token "+". Its y1 [yI for I == 1] is
257 its spelling location in #1.
259 y0 (thus for token "1") is the spelling location of A in #1.
261 And y2 (of token "2") is the spelling location of B in #1.
263 When the token is /not/ an argument for a macro, xI is the same
264 location as yI. Otherwise, xI is the location of the token
265 outside this macro expansion. If this macro was expanded from
266 another macro expansion, xI is a virtual location representing
267 the token in that macro expansion; otherwise, it is the spelling
268 location of the token.
270 Note that a virtual location is a location returned by
271 linemap_add_macro_token. It encodes the relevant locations (x,y
272 pairs) of that token across the macro expansions from which it
273 (the token) might come from.
275 In the example above x1 (for token "+") is going to be the same
276 as y1. x0 is the spelling location for the argument token "1",
277 and x2 is the spelling location for the argument token "2". */
278 source_location * GTY((atomic)) macro_locations;
280 /* This is the location of the expansion point of the current macro
281 map. It's the location of the macro name. That location is held
282 by the map that was current right before the current one. It
283 could have been either a macro or an ordinary map, depending on
284 if we are in a nested expansion context not. */
285 source_location expansion;
288 #if CHECKING_P && (GCC_VERSION >= 2007)
290 /* Assertion macro to be used in line-map code. */
291 #define linemap_assert(EXPR) \
292 do { \
293 if (! (EXPR)) \
294 abort (); \
295 } while (0)
297 /* Assert that becomes a conditional expression when checking is disabled at
298 compilation time. Use this for conditions that should not happen but if
299 they happen, it is better to handle them gracefully rather than crash
300 randomly later.
301 Usage:
303 if (linemap_assert_fails(EXPR)) handle_error(); */
304 #define linemap_assert_fails(EXPR) __extension__ \
305 ({linemap_assert (EXPR); false;})
307 #else
308 /* Include EXPR, so that unused variable warnings do not occur. */
309 #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
310 #define linemap_assert_fails(EXPR) (! (EXPR))
311 #endif
313 /* Return TRUE if MAP encodes locations coming from a macro
314 replacement-list at macro expansion point. */
315 bool
316 linemap_macro_expansion_map_p (const struct line_map *);
318 /* Assert that MAP encodes locations of tokens that are not part of
319 the replacement-list of a macro expansion, downcasting from
320 line_map * to line_map_ordinary *. */
322 inline line_map_ordinary *
323 linemap_check_ordinary (struct line_map *map)
325 linemap_assert (!linemap_macro_expansion_map_p (map));
326 return (line_map_ordinary *)map;
329 /* Assert that MAP encodes locations of tokens that are not part of
330 the replacement-list of a macro expansion, downcasting from
331 const line_map * to const line_map_ordinary *. */
333 inline const line_map_ordinary *
334 linemap_check_ordinary (const struct line_map *map)
336 linemap_assert (!linemap_macro_expansion_map_p (map));
337 return (const line_map_ordinary *)map;
340 /* Assert that MAP is a macro expansion and downcast to the appropriate
341 subclass. */
343 inline line_map_macro *linemap_check_macro (line_map *map)
345 linemap_assert (linemap_macro_expansion_map_p (map));
346 return (line_map_macro *)map;
349 /* Assert that MAP is a macro expansion and downcast to the appropriate
350 subclass. */
352 inline const line_map_macro *
353 linemap_check_macro (const line_map *map)
355 linemap_assert (linemap_macro_expansion_map_p (map));
356 return (const line_map_macro *)map;
359 /* Read the start location of MAP. */
361 inline source_location
362 MAP_START_LOCATION (const line_map *map)
364 return map->start_location;
367 /* Get the starting line number of ordinary map MAP. */
369 inline linenum_type
370 ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map_ordinary *ord_map)
372 return ord_map->to_line;
375 /* Get the index of the ordinary map at whose end
376 ordinary map MAP was included.
378 File(s) at the bottom of the include stack have this set. */
380 inline int
381 ORDINARY_MAP_INCLUDER_FILE_INDEX (const line_map_ordinary *ord_map)
383 return ord_map->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++. */
391 inline unsigned char
392 ORDINARY_MAP_IN_SYSTEM_HEADER_P (const line_map_ordinary *ord_map)
394 return ord_map->sysp;
397 /* Get the number of the low-order source_location bits used for a
398 column number within ordinary map MAP. */
400 inline unsigned char
401 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (const line_map_ordinary *ord_map)
403 return ord_map->column_bits;
406 /* Get the filename of ordinary map MAP. */
408 inline const char *
409 ORDINARY_MAP_FILE_NAME (const line_map_ordinary *ord_map)
411 return ord_map->to_file;
414 /* Get the cpp macro whose expansion gave birth to macro map MAP. */
416 inline cpp_hashnode *
417 MACRO_MAP_MACRO (const line_map_macro *macro_map)
419 return macro_map->macro;
422 /* Get the number of tokens inside the replacement-list of the macro
423 that led to macro map MAP. */
425 inline unsigned int
426 MACRO_MAP_NUM_MACRO_TOKENS (const line_map_macro *macro_map)
428 return macro_map->n_tokens;
431 /* Get the array of pairs of locations within macro map MAP.
432 See the declaration of line_map_macro for more information. */
434 inline source_location *
435 MACRO_MAP_LOCATIONS (const line_map_macro *macro_map)
437 return macro_map->macro_locations;
440 /* Get the location of the expansion point of the macro map MAP. */
442 inline source_location
443 MACRO_MAP_EXPANSION_POINT_LOCATION (const line_map_macro *macro_map)
445 return macro_map->expansion;
448 /* The abstraction of a set of location maps. There can be several
449 types of location maps. This abstraction contains the attributes
450 that are independent from the type of the map.
452 Essentially this is just a vector of T_linemap_subclass,
453 which can only ever grow in size. */
455 struct GTY(()) maps_info_ordinary {
456 /* This array contains the "ordinary" line maps, for all
457 events other than macro expansion
458 (e.g. when a new preprocessing unit starts or ends). */
459 line_map_ordinary * GTY ((length ("%h.used"))) maps;
461 /* The total number of allocated maps. */
462 unsigned int allocated;
464 /* The number of elements used in maps. This number is smaller
465 or equal to ALLOCATED. */
466 unsigned int used;
468 unsigned int cache;
471 struct GTY(()) maps_info_macro {
472 /* This array contains the macro line maps.
473 A macro line map is created whenever a macro expansion occurs. */
474 line_map_macro * GTY ((length ("%h.used"))) maps;
476 /* The total number of allocated maps. */
477 unsigned int allocated;
479 /* The number of elements used in maps. This number is smaller
480 or equal to ALLOCATED. */
481 unsigned int used;
483 unsigned int cache;
486 /* Data structure to associate an arbitrary data to a source location. */
487 struct GTY(()) location_adhoc_data {
488 source_location locus;
489 void * GTY((skip)) data;
492 struct htab;
494 /* The following data structure encodes a location with some adhoc data
495 and maps it to a new unsigned integer (called an adhoc location)
496 that replaces the original location to represent the mapping.
498 The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
499 highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
500 the original location. Once identified as the adhoc_loc, the lower 31
501 bits of the integer is used to index the location_adhoc_data array,
502 in which the locus and associated data is stored. */
504 struct GTY(()) location_adhoc_data_map {
505 struct htab * GTY((skip)) htab;
506 source_location curr_loc;
507 unsigned int allocated;
508 struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
511 /* A set of chronological line_map structures. */
512 struct GTY(()) line_maps {
514 maps_info_ordinary info_ordinary;
516 maps_info_macro info_macro;
518 /* Depth of the include stack, including the current file. */
519 unsigned int depth;
521 /* If true, prints an include trace a la -H. */
522 bool trace_includes;
524 /* Highest source_location "given out". */
525 source_location highest_location;
527 /* Start of line of highest source_location "given out". */
528 source_location highest_line;
530 /* The maximum column number we can quickly allocate. Higher numbers
531 may require allocating a new line_map. */
532 unsigned int max_column_hint;
534 /* If non-null, the allocator to use when resizing 'maps'. If null,
535 xrealloc is used. */
536 line_map_realloc reallocator;
538 /* The allocators' function used to know the actual size it
539 allocated, for a certain allocation size requested. */
540 line_map_round_alloc_size_func round_alloc_size;
542 struct location_adhoc_data_map location_adhoc_data_map;
544 /* The special location value that is used as spelling location for
545 built-in tokens. */
546 source_location builtin_location;
548 /* True if we've seen a #line or # 44 "file" directive. */
549 bool seen_line_directive;
552 /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
553 if we are interested in macro maps, FALSE otherwise. */
554 inline unsigned int
555 LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind)
557 if (map_kind)
558 return set->info_macro.allocated;
559 else
560 return set->info_ordinary.allocated;
563 /* As above, but by reference (e.g. as an lvalue). */
565 inline unsigned int &
566 LINEMAPS_ALLOCATED (line_maps *set, bool map_kind)
568 if (map_kind)
569 return set->info_macro.allocated;
570 else
571 return set->info_ordinary.allocated;
574 /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
575 we are interested in macro maps, FALSE otherwise.*/
576 inline unsigned int
577 LINEMAPS_USED (const line_maps *set, bool map_kind)
579 if (map_kind)
580 return set->info_macro.used;
581 else
582 return set->info_ordinary.used;
585 /* As above, but by reference (e.g. as an lvalue). */
587 inline unsigned int &
588 LINEMAPS_USED (line_maps *set, bool map_kind)
590 if (map_kind)
591 return set->info_macro.used;
592 else
593 return set->info_ordinary.used;
596 /* Returns the index of the last map that was looked up with
597 linemap_lookup. MAP_KIND shall be TRUE if we are interested in
598 macro maps, FALSE otherwise. */
599 inline unsigned int
600 LINEMAPS_CACHE (const line_maps *set, bool map_kind)
602 if (map_kind)
603 return set->info_macro.cache;
604 else
605 return set->info_ordinary.cache;
608 /* As above, but by reference (e.g. as an lvalue). */
610 inline unsigned int &
611 LINEMAPS_CACHE (line_maps *set, bool map_kind)
613 if (map_kind)
614 return set->info_macro.cache;
615 else
616 return set->info_ordinary.cache;
619 /* Return the map at a given index. */
620 inline line_map *
621 LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, int index)
623 if (map_kind)
624 return &set->info_macro.maps[index];
625 else
626 return &set->info_ordinary.maps[index];
629 /* Returns the last map used in the line table SET. MAP_KIND
630 shall be TRUE if we are interested in macro maps, FALSE
631 otherwise.*/
632 inline line_map *
633 LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind)
635 return LINEMAPS_MAP_AT (set, map_kind,
636 LINEMAPS_USED (set, map_kind) - 1);
639 /* Returns the last map that was allocated in the line table SET.
640 MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
641 otherwise.*/
642 inline line_map *
643 LINEMAPS_LAST_ALLOCATED_MAP (const line_maps *set, bool map_kind)
645 return LINEMAPS_MAP_AT (set, map_kind,
646 LINEMAPS_ALLOCATED (set, map_kind) - 1);
649 /* Returns a pointer to the memory region where ordinary maps are
650 allocated in the line table SET. */
651 inline line_map_ordinary *
652 LINEMAPS_ORDINARY_MAPS (const line_maps *set)
654 return set->info_ordinary.maps;
657 /* Returns the INDEXth ordinary map. */
658 inline line_map_ordinary *
659 LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, int index)
661 linemap_assert (index >= 0);
662 linemap_assert ((unsigned int)index < set->info_ordinary.used);
663 return &set->info_ordinary.maps[index];
666 /* Return the number of ordinary maps allocated in the line table
667 SET. */
668 inline unsigned int
669 LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set)
671 return LINEMAPS_ALLOCATED (set, false);
674 /* Return the number of ordinary maps used in the line table SET. */
675 inline unsigned int
676 LINEMAPS_ORDINARY_USED (const line_maps *set)
678 return LINEMAPS_USED (set, false);
681 /* Return the index of the last ordinary map that was looked up with
682 linemap_lookup. */
683 inline unsigned int
684 LINEMAPS_ORDINARY_CACHE (const line_maps *set)
686 return LINEMAPS_CACHE (set, false);
689 /* As above, but by reference (e.g. as an lvalue). */
691 inline unsigned int &
692 LINEMAPS_ORDINARY_CACHE (line_maps *set)
694 return LINEMAPS_CACHE (set, false);
697 /* Returns a pointer to the last ordinary map used in the line table
698 SET. */
699 inline line_map_ordinary *
700 LINEMAPS_LAST_ORDINARY_MAP (const line_maps *set)
702 return (line_map_ordinary *)LINEMAPS_LAST_MAP (set, false);
705 /* Returns a pointer to the last ordinary map allocated the line table
706 SET. */
707 inline line_map_ordinary *
708 LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP (const line_maps *set)
710 return (line_map_ordinary *)LINEMAPS_LAST_ALLOCATED_MAP (set, false);
713 /* Returns a pointer to the beginning of the region where macro maps
714 are allcoated. */
715 inline line_map_macro *
716 LINEMAPS_MACRO_MAPS (const line_maps *set)
718 return set->info_macro.maps;
721 /* Returns the INDEXth macro map. */
722 inline line_map_macro *
723 LINEMAPS_MACRO_MAP_AT (const line_maps *set, int index)
725 linemap_assert (index >= 0);
726 linemap_assert ((unsigned int)index < set->info_macro.used);
727 return &set->info_macro.maps[index];
730 /* Returns the number of macro maps that were allocated in the line
731 table SET. */
732 inline unsigned int
733 LINEMAPS_MACRO_ALLOCATED (const line_maps *set)
735 return LINEMAPS_ALLOCATED (set, true);
738 /* Returns the number of macro maps used in the line table SET. */
739 inline unsigned int
740 LINEMAPS_MACRO_USED (const line_maps *set)
742 return LINEMAPS_USED (set, true);
745 /* Returns the index of the last macro map looked up with
746 linemap_lookup. */
747 inline unsigned int
748 LINEMAPS_MACRO_CACHE (const line_maps *set)
750 return LINEMAPS_CACHE (set, true);
753 /* As above, but by reference (e.g. as an lvalue). */
755 inline unsigned int &
756 LINEMAPS_MACRO_CACHE (line_maps *set)
758 return LINEMAPS_CACHE (set, true);
761 /* Returns the last macro map used in the line table SET. */
762 inline line_map_macro *
763 LINEMAPS_LAST_MACRO_MAP (const line_maps *set)
765 return (line_map_macro *)LINEMAPS_LAST_MAP (set, true);
768 /* Returns the lowest location [of a token resulting from macro
769 expansion] encoded in this line table. */
770 inline source_location
771 LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps *set)
773 return LINEMAPS_MACRO_USED (set)
774 ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))
775 : MAX_SOURCE_LOCATION;
778 /* Returns the last macro map allocated in the line table SET. */
779 inline line_map_macro *
780 LINEMAPS_LAST_ALLOCATED_MACRO_MAP (const line_maps *set)
782 return (line_map_macro *)LINEMAPS_LAST_ALLOCATED_MAP (set, true);
785 extern void location_adhoc_data_fini (struct line_maps *);
786 extern source_location get_combined_adhoc_loc (struct line_maps *,
787 source_location, void *);
788 extern void *get_data_from_adhoc_loc (struct line_maps *, source_location);
789 extern source_location get_location_from_adhoc_loc (struct line_maps *,
790 source_location);
792 /* Get whether location LOC is an ad-hoc location. */
794 inline bool
795 IS_ADHOC_LOC (source_location loc)
797 return (loc & MAX_SOURCE_LOCATION) != loc;
800 /* Combine LOC and BLOCK, giving a combined adhoc location. */
802 inline source_location
803 COMBINE_LOCATION_DATA (struct line_maps *set,
804 source_location loc,
805 void *block)
807 return get_combined_adhoc_loc (set, loc, block);
810 extern void rebuild_location_adhoc_htab (struct line_maps *);
812 /* Initialize a line map set. SET is the line map set to initialize
813 and BUILTIN_LOCATION is the special location value to be used as
814 spelling location for built-in tokens. This BUILTIN_LOCATION has
815 to be strictly less than RESERVED_LOCATION_COUNT. */
816 extern void linemap_init (struct line_maps *set,
817 source_location builtin_location);
819 /* Check for and warn about line_maps entered but not exited. */
821 extern void linemap_check_files_exited (struct line_maps *);
823 /* Return a source_location for the start (i.e. column==0) of
824 (physical) line TO_LINE in the current source file (as in the
825 most recent linemap_add). MAX_COLUMN_HINT is the highest column
826 number we expect to use in this line (but it does not change
827 the highest_location). */
829 extern source_location linemap_line_start
830 (struct line_maps *set, linenum_type to_line, unsigned int max_column_hint);
832 /* Add a mapping of logical source line to physical source file and
833 line number. This function creates an "ordinary map", which is a
834 map that records locations of tokens that are not part of macro
835 replacement-lists present at a macro expansion point.
837 The text pointed to by TO_FILE must have a lifetime
838 at least as long as the lifetime of SET. An empty
839 TO_FILE means standard input. If reason is LC_LEAVE, and
840 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
841 natural values considering the file we are returning to.
843 A call to this function can relocate the previous set of
844 maps, so any stored line_map pointers should not be used. */
845 extern const struct line_map *linemap_add
846 (struct line_maps *, enum lc_reason, unsigned int sysp,
847 const char *to_file, linenum_type to_line);
849 /* Given a logical source location, returns the map which the
850 corresponding (source file, line, column) triplet can be deduced
851 from. Since the set is built chronologically, the logical lines are
852 monotonic increasing, and so the list is sorted and we can use a
853 binary search. If no line map have been allocated yet, this
854 function returns NULL. */
855 extern const struct line_map *linemap_lookup
856 (struct line_maps *, source_location);
858 /* Returns TRUE if the line table set tracks token locations across
859 macro expansion, FALSE otherwise. */
860 bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
862 /* Return the name of the macro associated to MACRO_MAP. */
863 const char* linemap_map_get_macro_name (const line_map_macro *);
865 /* Return a positive value if LOCATION is the locus of a token that is
866 located in a system header, O otherwise. It returns 1 if LOCATION
867 is the locus of a token that is located in a system header, and 2
868 if LOCATION is the locus of a token located in a C system header
869 that therefore needs to be extern "C" protected in C++.
871 Note that this function returns 1 if LOCATION belongs to a token
872 that is part of a macro replacement-list defined in a system
873 header, but expanded in a non-system file. */
874 int linemap_location_in_system_header_p (struct line_maps *,
875 source_location);
877 /* Return TRUE if LOCATION is a source code location of a token coming
878 from a macro replacement-list at a macro expansion point, FALSE
879 otherwise. */
880 bool linemap_location_from_macro_expansion_p (const struct line_maps *,
881 source_location);
883 /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
884 be reserved for libcpp user as special values, no token from libcpp
885 will contain any of those locations. */
886 const source_location RESERVED_LOCATION_COUNT = 2;
888 /* Converts a map and a source_location to source line. */
889 inline linenum_type
890 SOURCE_LINE (const line_map_ordinary *ord_map, source_location loc)
892 return ((loc - ord_map->start_location)
893 >> ord_map->column_bits) + ord_map->to_line;
896 /* Convert a map and source_location to source column number. */
897 inline linenum_type
898 SOURCE_COLUMN (const line_map_ordinary *ord_map, source_location loc)
900 return ((loc - ord_map->start_location)
901 & ((1 << ord_map->column_bits) - 1));
904 /* Return the location of the last source line within an ordinary
905 map. */
906 inline source_location
907 LAST_SOURCE_LINE_LOCATION (const line_map_ordinary *map)
909 return (((map[1].start_location - 1
910 - map->start_location)
911 & ~((1 << map->column_bits) - 1))
912 + map->start_location);
915 /* Returns the last source line number within an ordinary map. This
916 is the (last) line of the #include, or other directive, that caused
917 a map change. */
918 inline linenum_type
919 LAST_SOURCE_LINE (const line_map_ordinary *map)
921 return SOURCE_LINE (map, LAST_SOURCE_LINE_LOCATION (map));
924 /* Return the last column number within an ordinary map. */
926 inline linenum_type
927 LAST_SOURCE_COLUMN (const line_map_ordinary *map)
929 return SOURCE_COLUMN (map, LAST_SOURCE_LINE_LOCATION (map));
932 /* Returns the map a given map was included from, or NULL if the map
933 belongs to the main file, i.e, a file that wasn't included by
934 another one. */
935 inline line_map_ordinary *
936 INCLUDED_FROM (struct line_maps *set, const line_map_ordinary *ord_map)
938 return ((ord_map->included_from == -1)
939 ? NULL
940 : LINEMAPS_ORDINARY_MAP_AT (set, ord_map->included_from));
943 /* True if the map is at the bottom of the include stack. */
945 inline bool
946 MAIN_FILE_P (const line_map_ordinary *ord_map)
948 return ord_map->included_from < 0;
951 /* Encode and return a source_location from a column number. The
952 source line considered is the last source line used to call
953 linemap_line_start, i.e, the last source line which a location was
954 encoded from. */
955 extern source_location
956 linemap_position_for_column (struct line_maps *, unsigned int);
958 /* Encode and return a source location from a given line and
959 column. */
960 source_location
961 linemap_position_for_line_and_column (const line_map_ordinary *,
962 linenum_type, unsigned int);
964 /* Encode and return a source_location starting from location LOC and
965 shifting it by OFFSET columns. This function does not support
966 virtual locations. */
967 source_location
968 linemap_position_for_loc_and_offset (struct line_maps *set,
969 source_location loc,
970 unsigned int offset);
972 /* Return the file this map is for. */
973 inline const char *
974 LINEMAP_FILE (const line_map_ordinary *ord_map)
976 return ord_map->to_file;
979 /* Return the line number this map started encoding location from. */
980 inline linenum_type
981 LINEMAP_LINE (const line_map_ordinary *ord_map)
983 return ord_map->to_line;
986 /* Return a positive value if map encodes locations from a system
987 header, 0 otherwise. Returns 1 if MAP encodes locations in a
988 system header and 2 if it encodes locations in a C system header
989 that therefore needs to be extern "C" protected in C++. */
990 inline unsigned char
991 LINEMAP_SYSP (const line_map_ordinary *ord_map)
993 return ord_map->sysp;
996 /* Return a positive value if PRE denotes the location of a token that
997 comes before the token of POST, 0 if PRE denotes the location of
998 the same token as the token for POST, and a negative value
999 otherwise. */
1000 int linemap_compare_locations (struct line_maps *set,
1001 source_location pre,
1002 source_location post);
1004 /* Return TRUE if LOC_A denotes the location a token that comes
1005 topogically before the token denoted by location LOC_B, or if they
1006 are equal. */
1007 inline bool
1008 linemap_location_before_p (struct line_maps *set,
1009 source_location loc_a,
1010 source_location loc_b)
1012 return linemap_compare_locations (set, loc_a, loc_b) >= 0;
1015 typedef struct
1017 /* The name of the source file involved. */
1018 const char *file;
1020 /* The line-location in the source file. */
1021 int line;
1023 int column;
1025 void *data;
1027 /* In a system header?. */
1028 bool sysp;
1029 } expanded_location;
1031 /* This is enum is used by the function linemap_resolve_location
1032 below. The meaning of the values is explained in the comment of
1033 that function. */
1034 enum location_resolution_kind
1036 LRK_MACRO_EXPANSION_POINT,
1037 LRK_SPELLING_LOCATION,
1038 LRK_MACRO_DEFINITION_LOCATION
1041 /* Resolve a virtual location into either a spelling location, an
1042 expansion point location or a token argument replacement point
1043 location. Return the map that encodes the virtual location as well
1044 as the resolved location.
1046 If LOC is *NOT* the location of a token resulting from the
1047 expansion of a macro, then the parameter LRK (which stands for
1048 Location Resolution Kind) is ignored and the resulting location
1049 just equals the one given in argument.
1051 Now if LOC *IS* the location of a token resulting from the
1052 expansion of a macro, this is what happens.
1054 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1055 -------------------------------
1057 The virtual location is resolved to the first macro expansion point
1058 that led to this macro expansion.
1060 * If LRK is set to LRK_SPELLING_LOCATION
1061 -------------------------------------
1063 The virtual location is resolved to the locus where the token has
1064 been spelled in the source. This can follow through all the macro
1065 expansions that led to the token.
1067 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1068 --------------------------------------
1070 The virtual location is resolved to the locus of the token in the
1071 context of the macro definition.
1073 If LOC is the locus of a token that is an argument of a
1074 function-like macro [replacing a parameter in the replacement list
1075 of the macro] the virtual location is resolved to the locus of the
1076 parameter that is replaced, in the context of the definition of the
1077 macro.
1079 If LOC is the locus of a token that is not an argument of a
1080 function-like macro, then the function behaves as if LRK was set to
1081 LRK_SPELLING_LOCATION.
1083 If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1084 returned location. Note that if the returned location wasn't originally
1085 encoded by a map, the *MAP is set to NULL. This can happen if LOC
1086 resolves to a location reserved for the client code, like
1087 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1089 source_location linemap_resolve_location (struct line_maps *,
1090 source_location loc,
1091 enum location_resolution_kind lrk,
1092 const line_map_ordinary **loc_map);
1094 /* Suppose that LOC is the virtual location of a token coming from the
1095 expansion of a macro M. This function then steps up to get the
1096 location L of the point where M got expanded. If L is a spelling
1097 location inside a macro expansion M', then this function returns
1098 the point where M' was expanded. LOC_MAP is an output parameter.
1099 When non-NULL, *LOC_MAP is set to the map of the returned
1100 location. */
1101 source_location linemap_unwind_toward_expansion (struct line_maps *,
1102 source_location loc,
1103 const struct line_map **loc_map);
1105 /* If LOC is the virtual location of a token coming from the expansion
1106 of a macro M and if its spelling location is reserved (e.g, a
1107 location for a built-in token), then this function unwinds (using
1108 linemap_unwind_toward_expansion) the location until a location that
1109 is not reserved and is not in a system header is reached. In other
1110 words, this unwinds the reserved location until a location that is
1111 in real source code is reached.
1113 Otherwise, if the spelling location for LOC is not reserved or if
1114 LOC doesn't come from the expansion of a macro, the function
1115 returns LOC as is and *MAP is not touched.
1117 *MAP is set to the map of the returned location if the later is
1118 different from LOC. */
1119 source_location linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
1120 source_location loc,
1121 const struct line_map **map);
1123 /* Expand source code location LOC and return a user readable source
1124 code location. LOC must be a spelling (non-virtual) location. If
1125 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1126 location is returned. */
1127 expanded_location linemap_expand_location (struct line_maps *,
1128 const struct line_map *,
1129 source_location loc);
1131 /* Statistics about maps allocation and usage as returned by
1132 linemap_get_statistics. */
1133 struct linemap_stats
1135 long num_ordinary_maps_allocated;
1136 long num_ordinary_maps_used;
1137 long ordinary_maps_allocated_size;
1138 long ordinary_maps_used_size;
1139 long num_expanded_macros;
1140 long num_macro_tokens;
1141 long num_macro_maps_used;
1142 long macro_maps_allocated_size;
1143 long macro_maps_used_size;
1144 long macro_maps_locations_size;
1145 long duplicated_macro_maps_locations_size;
1146 long adhoc_table_size;
1147 long adhoc_table_entries_used;
1150 /* Return the highest location emitted for a given file for which
1151 there is a line map in SET. FILE_NAME is the file name to
1152 consider. If the function returns TRUE, *LOC is set to the highest
1153 location emitted for that file. */
1154 bool linemap_get_file_highest_location (struct line_maps * set,
1155 const char *file_name,
1156 source_location *loc);
1158 /* Compute and return statistics about the memory consumption of some
1159 parts of the line table SET. */
1160 void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
1162 /* Dump debugging information about source location LOC into the file
1163 stream STREAM. SET is the line map set LOC comes from. */
1164 void linemap_dump_location (struct line_maps *, source_location, FILE *);
1166 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1167 is NULL, use stderr. IS_MACRO is true if the caller wants to
1168 dump a macro map, false otherwise. */
1169 void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
1171 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1172 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1173 specifies how many macro maps to dump. */
1174 void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
1176 #endif /* !LIBCPP_LINE_MAP_H */