Correct the Changelog date of the previous commit.
[official-gcc.git] / libcpp / include / line-map.h
blobc9340a6eaefb062f3bcd843b37933aa9a3bfa4bc
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 /* A range of source locations.
136 Ranges are closed:
137 m_start is the first location within the range,
138 m_finish is the last location within the range.
140 We may need a more compact way to store these, but for now,
141 let's do it the simple way, as a pair. */
142 struct GTY(()) source_range
144 source_location m_start;
145 source_location m_finish;
147 /* Display this source_range instance, with MSG as a descriptive
148 comment. This issues a "note" diagnostic at the range, using
149 gcc's diagnostic machinery.
151 This is declared here, but is implemented within gcc/diagnostic.c,
152 since it makes use of gcc's diagnostic-printing machinery. This
153 is a slight layering violation, but this is sufficiently useful
154 for debugging that it's worth it.
156 This declaration would have a DEBUG_FUNCTION annotation, but that
157 is implemented in gcc/system.h and thus is not available here in
158 libcpp. */
159 void debug (const char *msg) const;
161 /* We avoid using constructors, since various structs that
162 don't yet have constructors will embed instances of
163 source_range. */
165 /* Make a source_range from a source_location. */
166 static source_range from_location (source_location loc)
168 source_range result;
169 result.m_start = loc;
170 result.m_finish = loc;
171 return result;
175 /* Memory allocation function typedef. Works like xrealloc. */
176 typedef void *(*line_map_realloc) (void *, size_t);
178 /* Memory allocator function that returns the actual allocated size,
179 for a given requested allocation. */
180 typedef size_t (*line_map_round_alloc_size_func) (size_t);
182 /* A line_map encodes a sequence of locations.
183 There are two kinds of maps. Ordinary maps and macro expansion
184 maps, a.k.a macro maps.
186 A macro map encodes source locations of tokens that are part of a
187 macro replacement-list, at a macro expansion point. E.g, in:
189 #define PLUS(A,B) A + B
191 No macro map is going to be created there, because we are not at a
192 macro expansion point. We are at a macro /definition/ point. So the
193 locations of the tokens of the macro replacement-list (i.e, A + B)
194 will be locations in an ordinary map, not a macro map.
196 On the other hand, if we later do:
198 int a = PLUS (1,2);
200 The invocation of PLUS here is a macro expansion. So we are at a
201 macro expansion point. The preprocessor expands PLUS (1,2) and
202 replaces it with the tokens of its replacement-list: 1 + 2. A macro
203 map is going to be created to hold (or rather to map, haha ...) the
204 locations of the tokens 1, + and 2. The macro map also records the
205 location of the expansion point of PLUS. That location is mapped in
206 the map that is active right before the location of the invocation
207 of PLUS. */
208 struct GTY((tag ("0"), desc ("%h.reason == LC_ENTER_MACRO ? 2 : 1"))) line_map {
209 source_location start_location;
211 /* The reason for creation of this line map. */
212 ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
215 /* An ordinary line map encodes physical source locations. Those
216 physical source locations are called "spelling locations".
218 Physical source file TO_FILE at line TO_LINE at column 0 is represented
219 by the logical START_LOCATION. TO_LINE+L at column C is represented by
220 START_LOCATION+(L*(1<<column_bits))+C, as long as C<(1<<column_bits),
221 and the result_location is less than the next line_map's start_location.
222 (The top line is line 1 and the leftmost column is column 1; line/column 0
223 means "entire file/line" or "unknown line/column" or "not applicable".)
225 The highest possible source location is MAX_SOURCE_LOCATION. */
226 struct GTY((tag ("1"))) line_map_ordinary : public line_map {
227 const char *to_file;
228 linenum_type to_line;
230 /* An index into the set that gives the line mapping at whose end
231 the current one was included. File(s) at the bottom of the
232 include stack have this set to -1. */
233 int included_from;
235 /* SYSP is one for a system header, two for a C system header file
236 that therefore needs to be extern "C" protected in C++, and zero
237 otherwise. This field isn't really needed now that it's in
238 cpp_buffer. */
239 unsigned char sysp;
241 /* Number of the low-order source_location bits used for a column number. */
242 unsigned int column_bits : 8;
245 /* This is the highest possible source location encoded within an
246 ordinary or macro map. */
247 const source_location MAX_SOURCE_LOCATION = 0x7FFFFFFF;
249 struct cpp_hashnode;
251 /* A macro line map encodes location of tokens coming from a macro
252 expansion.
254 The offset from START_LOCATION is used to index into
255 MACRO_LOCATIONS; this holds the original location of the token. */
256 struct GTY((tag ("2"))) line_map_macro : public line_map {
257 /* The cpp macro which expansion gave birth to this macro map. */
258 struct cpp_hashnode * GTY ((nested_ptr (union tree_node,
259 "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
260 "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
261 macro;
263 /* The number of tokens inside the replacement-list of MACRO. */
264 unsigned int n_tokens;
266 /* This array of location is actually an array of pairs of
267 locations. The elements inside it thus look like:
269 x0,y0, x1,y1, x2,y2, ...., xn,yn.
271 where n == n_tokens;
273 Remember that these xI,yI are collected when libcpp is about to
274 expand a given macro.
276 yI is the location in the macro definition, either of the token
277 itself or of a macro parameter that it replaces.
279 Imagine this:
281 #define PLUS(A, B) A + B <--- #1
283 int a = PLUS (1,2); <--- #2
285 There is a macro map for the expansion of PLUS in #2. PLUS is
286 expanded into its expansion-list. The expansion-list is the
287 replacement-list of PLUS where the macro parameters are replaced
288 with their arguments. So the replacement-list of PLUS is made of
289 the tokens:
291 A, +, B
293 and the expansion-list is made of the tokens:
295 1, +, 2
297 Let's consider the case of token "+". Its y1 [yI for I == 1] is
298 its spelling location in #1.
300 y0 (thus for token "1") is the spelling location of A in #1.
302 And y2 (of token "2") is the spelling location of B in #1.
304 When the token is /not/ an argument for a macro, xI is the same
305 location as yI. Otherwise, xI is the location of the token
306 outside this macro expansion. If this macro was expanded from
307 another macro expansion, xI is a virtual location representing
308 the token in that macro expansion; otherwise, it is the spelling
309 location of the token.
311 Note that a virtual location is a location returned by
312 linemap_add_macro_token. It encodes the relevant locations (x,y
313 pairs) of that token across the macro expansions from which it
314 (the token) might come from.
316 In the example above x1 (for token "+") is going to be the same
317 as y1. x0 is the spelling location for the argument token "1",
318 and x2 is the spelling location for the argument token "2". */
319 source_location * GTY((atomic)) macro_locations;
321 /* This is the location of the expansion point of the current macro
322 map. It's the location of the macro name. That location is held
323 by the map that was current right before the current one. It
324 could have been either a macro or an ordinary map, depending on
325 if we are in a nested expansion context not. */
326 source_location expansion;
329 #if CHECKING_P && (GCC_VERSION >= 2007)
331 /* Assertion macro to be used in line-map code. */
332 #define linemap_assert(EXPR) \
333 do { \
334 if (! (EXPR)) \
335 abort (); \
336 } while (0)
338 /* Assert that becomes a conditional expression when checking is disabled at
339 compilation time. Use this for conditions that should not happen but if
340 they happen, it is better to handle them gracefully rather than crash
341 randomly later.
342 Usage:
344 if (linemap_assert_fails(EXPR)) handle_error(); */
345 #define linemap_assert_fails(EXPR) __extension__ \
346 ({linemap_assert (EXPR); false;})
348 #else
349 /* Include EXPR, so that unused variable warnings do not occur. */
350 #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
351 #define linemap_assert_fails(EXPR) (! (EXPR))
352 #endif
354 /* Return TRUE if MAP encodes locations coming from a macro
355 replacement-list at macro expansion point. */
356 bool
357 linemap_macro_expansion_map_p (const struct line_map *);
359 /* Assert that MAP encodes locations of tokens that are not part of
360 the replacement-list of a macro expansion, downcasting from
361 line_map * to line_map_ordinary *. */
363 inline line_map_ordinary *
364 linemap_check_ordinary (struct line_map *map)
366 linemap_assert (!linemap_macro_expansion_map_p (map));
367 return (line_map_ordinary *)map;
370 /* Assert that MAP encodes locations of tokens that are not part of
371 the replacement-list of a macro expansion, downcasting from
372 const line_map * to const line_map_ordinary *. */
374 inline const line_map_ordinary *
375 linemap_check_ordinary (const struct line_map *map)
377 linemap_assert (!linemap_macro_expansion_map_p (map));
378 return (const line_map_ordinary *)map;
381 /* Assert that MAP is a macro expansion and downcast to the appropriate
382 subclass. */
384 inline line_map_macro *linemap_check_macro (line_map *map)
386 linemap_assert (linemap_macro_expansion_map_p (map));
387 return (line_map_macro *)map;
390 /* Assert that MAP is a macro expansion and downcast to the appropriate
391 subclass. */
393 inline const line_map_macro *
394 linemap_check_macro (const line_map *map)
396 linemap_assert (linemap_macro_expansion_map_p (map));
397 return (const line_map_macro *)map;
400 /* Read the start location of MAP. */
402 inline source_location
403 MAP_START_LOCATION (const line_map *map)
405 return map->start_location;
408 /* Get the starting line number of ordinary map MAP. */
410 inline linenum_type
411 ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map_ordinary *ord_map)
413 return ord_map->to_line;
416 /* Get the index of the ordinary map at whose end
417 ordinary map MAP was included.
419 File(s) at the bottom of the include stack have this set. */
421 inline int
422 ORDINARY_MAP_INCLUDER_FILE_INDEX (const line_map_ordinary *ord_map)
424 return ord_map->included_from;
427 /* Return a positive value if map encodes locations from a system
428 header, 0 otherwise. Returns 1 if ordinary map MAP encodes locations
429 in a system header and 2 if it encodes locations in a C system header
430 that therefore needs to be extern "C" protected in C++. */
432 inline unsigned char
433 ORDINARY_MAP_IN_SYSTEM_HEADER_P (const line_map_ordinary *ord_map)
435 return ord_map->sysp;
438 /* Get the number of the low-order source_location bits used for a
439 column number within ordinary map MAP. */
441 inline unsigned char
442 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (const line_map_ordinary *ord_map)
444 return ord_map->column_bits;
447 /* Get the filename of ordinary map MAP. */
449 inline const char *
450 ORDINARY_MAP_FILE_NAME (const line_map_ordinary *ord_map)
452 return ord_map->to_file;
455 /* Get the cpp macro whose expansion gave birth to macro map MAP. */
457 inline cpp_hashnode *
458 MACRO_MAP_MACRO (const line_map_macro *macro_map)
460 return macro_map->macro;
463 /* Get the number of tokens inside the replacement-list of the macro
464 that led to macro map MAP. */
466 inline unsigned int
467 MACRO_MAP_NUM_MACRO_TOKENS (const line_map_macro *macro_map)
469 return macro_map->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_macro *macro_map)
478 return macro_map->macro_locations;
481 /* Get the location of the expansion point of the macro map MAP. */
483 inline source_location
484 MACRO_MAP_EXPANSION_POINT_LOCATION (const line_map_macro *macro_map)
486 return macro_map->expansion;
489 /* The abstraction of a set of location maps. There can be several
490 types of location maps. This abstraction contains the attributes
491 that are independent from the type of the map.
493 Essentially this is just a vector of T_linemap_subclass,
494 which can only ever grow in size. */
496 struct GTY(()) maps_info_ordinary {
497 /* This array contains the "ordinary" line maps, for all
498 events other than macro expansion
499 (e.g. when a new preprocessing unit starts or ends). */
500 line_map_ordinary * GTY ((length ("%h.used"))) maps;
502 /* The total number of allocated maps. */
503 unsigned int allocated;
505 /* The number of elements used in maps. This number is smaller
506 or equal to ALLOCATED. */
507 unsigned int used;
509 unsigned int cache;
512 struct GTY(()) maps_info_macro {
513 /* This array contains the macro line maps.
514 A macro line map is created whenever a macro expansion occurs. */
515 line_map_macro * GTY ((length ("%h.used"))) maps;
517 /* The total number of allocated maps. */
518 unsigned int allocated;
520 /* The number of elements used in maps. This number is smaller
521 or equal to ALLOCATED. */
522 unsigned int used;
524 unsigned int cache;
527 /* Data structure to associate an arbitrary data to a source location. */
528 struct GTY(()) location_adhoc_data {
529 source_location locus;
530 void * GTY((skip)) data;
533 struct htab;
535 /* The following data structure encodes a location with some adhoc data
536 and maps it to a new unsigned integer (called an adhoc location)
537 that replaces the original location to represent the mapping.
539 The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
540 highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
541 the original location. Once identified as the adhoc_loc, the lower 31
542 bits of the integer is used to index the location_adhoc_data array,
543 in which the locus and associated data is stored. */
545 struct GTY(()) location_adhoc_data_map {
546 struct htab * GTY((skip)) htab;
547 source_location curr_loc;
548 unsigned int allocated;
549 struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
552 /* A set of chronological line_map structures. */
553 struct GTY(()) line_maps {
555 maps_info_ordinary info_ordinary;
557 maps_info_macro info_macro;
559 /* Depth of the include stack, including the current file. */
560 unsigned int depth;
562 /* If true, prints an include trace a la -H. */
563 bool trace_includes;
565 /* Highest source_location "given out". */
566 source_location highest_location;
568 /* Start of line of highest source_location "given out". */
569 source_location highest_line;
571 /* The maximum column number we can quickly allocate. Higher numbers
572 may require allocating a new line_map. */
573 unsigned int max_column_hint;
575 /* If non-null, the allocator to use when resizing 'maps'. If null,
576 xrealloc is used. */
577 line_map_realloc reallocator;
579 /* The allocators' function used to know the actual size it
580 allocated, for a certain allocation size requested. */
581 line_map_round_alloc_size_func round_alloc_size;
583 struct location_adhoc_data_map location_adhoc_data_map;
585 /* The special location value that is used as spelling location for
586 built-in tokens. */
587 source_location builtin_location;
589 /* True if we've seen a #line or # 44 "file" directive. */
590 bool seen_line_directive;
593 /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
594 if we are interested in macro maps, FALSE otherwise. */
595 inline unsigned int
596 LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind)
598 if (map_kind)
599 return set->info_macro.allocated;
600 else
601 return set->info_ordinary.allocated;
604 /* As above, but by reference (e.g. as an lvalue). */
606 inline unsigned int &
607 LINEMAPS_ALLOCATED (line_maps *set, bool map_kind)
609 if (map_kind)
610 return set->info_macro.allocated;
611 else
612 return set->info_ordinary.allocated;
615 /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
616 we are interested in macro maps, FALSE otherwise.*/
617 inline unsigned int
618 LINEMAPS_USED (const line_maps *set, bool map_kind)
620 if (map_kind)
621 return set->info_macro.used;
622 else
623 return set->info_ordinary.used;
626 /* As above, but by reference (e.g. as an lvalue). */
628 inline unsigned int &
629 LINEMAPS_USED (line_maps *set, bool map_kind)
631 if (map_kind)
632 return set->info_macro.used;
633 else
634 return set->info_ordinary.used;
637 /* Returns the index of the last map that was looked up with
638 linemap_lookup. MAP_KIND shall be TRUE if we are interested in
639 macro maps, FALSE otherwise. */
640 inline unsigned int
641 LINEMAPS_CACHE (const line_maps *set, bool map_kind)
643 if (map_kind)
644 return set->info_macro.cache;
645 else
646 return set->info_ordinary.cache;
649 /* As above, but by reference (e.g. as an lvalue). */
651 inline unsigned int &
652 LINEMAPS_CACHE (line_maps *set, bool map_kind)
654 if (map_kind)
655 return set->info_macro.cache;
656 else
657 return set->info_ordinary.cache;
660 /* Return the map at a given index. */
661 inline line_map *
662 LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, int index)
664 if (map_kind)
665 return &set->info_macro.maps[index];
666 else
667 return &set->info_ordinary.maps[index];
670 /* Returns the last map used in the line table SET. MAP_KIND
671 shall be TRUE if we are interested in macro maps, FALSE
672 otherwise.*/
673 inline line_map *
674 LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind)
676 return LINEMAPS_MAP_AT (set, map_kind,
677 LINEMAPS_USED (set, map_kind) - 1);
680 /* Returns the last map that was allocated in the line table SET.
681 MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
682 otherwise.*/
683 inline line_map *
684 LINEMAPS_LAST_ALLOCATED_MAP (const line_maps *set, bool map_kind)
686 return LINEMAPS_MAP_AT (set, map_kind,
687 LINEMAPS_ALLOCATED (set, map_kind) - 1);
690 /* Returns a pointer to the memory region where ordinary maps are
691 allocated in the line table SET. */
692 inline line_map_ordinary *
693 LINEMAPS_ORDINARY_MAPS (const line_maps *set)
695 return set->info_ordinary.maps;
698 /* Returns the INDEXth ordinary map. */
699 inline line_map_ordinary *
700 LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, int index)
702 linemap_assert (index >= 0);
703 linemap_assert ((unsigned int)index < set->info_ordinary.used);
704 return &set->info_ordinary.maps[index];
707 /* Return the number of ordinary maps allocated in the line table
708 SET. */
709 inline unsigned int
710 LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set)
712 return LINEMAPS_ALLOCATED (set, false);
715 /* Return the number of ordinary maps used in the line table SET. */
716 inline unsigned int
717 LINEMAPS_ORDINARY_USED (const line_maps *set)
719 return LINEMAPS_USED (set, false);
722 /* Return the index of the last ordinary map that was looked up with
723 linemap_lookup. */
724 inline unsigned int
725 LINEMAPS_ORDINARY_CACHE (const line_maps *set)
727 return LINEMAPS_CACHE (set, false);
730 /* As above, but by reference (e.g. as an lvalue). */
732 inline unsigned int &
733 LINEMAPS_ORDINARY_CACHE (line_maps *set)
735 return LINEMAPS_CACHE (set, false);
738 /* Returns a pointer to the last ordinary map used in the line table
739 SET. */
740 inline line_map_ordinary *
741 LINEMAPS_LAST_ORDINARY_MAP (const line_maps *set)
743 return (line_map_ordinary *)LINEMAPS_LAST_MAP (set, false);
746 /* Returns a pointer to the last ordinary map allocated the line table
747 SET. */
748 inline line_map_ordinary *
749 LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP (const line_maps *set)
751 return (line_map_ordinary *)LINEMAPS_LAST_ALLOCATED_MAP (set, false);
754 /* Returns a pointer to the beginning of the region where macro maps
755 are allcoated. */
756 inline line_map_macro *
757 LINEMAPS_MACRO_MAPS (const line_maps *set)
759 return set->info_macro.maps;
762 /* Returns the INDEXth macro map. */
763 inline line_map_macro *
764 LINEMAPS_MACRO_MAP_AT (const line_maps *set, int index)
766 linemap_assert (index >= 0);
767 linemap_assert ((unsigned int)index < set->info_macro.used);
768 return &set->info_macro.maps[index];
771 /* Returns the number of macro maps that were allocated in the line
772 table SET. */
773 inline unsigned int
774 LINEMAPS_MACRO_ALLOCATED (const line_maps *set)
776 return LINEMAPS_ALLOCATED (set, true);
779 /* Returns the number of macro maps used in the line table SET. */
780 inline unsigned int
781 LINEMAPS_MACRO_USED (const line_maps *set)
783 return LINEMAPS_USED (set, true);
786 /* Returns the index of the last macro map looked up with
787 linemap_lookup. */
788 inline unsigned int
789 LINEMAPS_MACRO_CACHE (const line_maps *set)
791 return LINEMAPS_CACHE (set, true);
794 /* As above, but by reference (e.g. as an lvalue). */
796 inline unsigned int &
797 LINEMAPS_MACRO_CACHE (line_maps *set)
799 return LINEMAPS_CACHE (set, true);
802 /* Returns the last macro map used in the line table SET. */
803 inline line_map_macro *
804 LINEMAPS_LAST_MACRO_MAP (const line_maps *set)
806 return (line_map_macro *)LINEMAPS_LAST_MAP (set, true);
809 /* Returns the lowest location [of a token resulting from macro
810 expansion] encoded in this line table. */
811 inline source_location
812 LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps *set)
814 return LINEMAPS_MACRO_USED (set)
815 ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))
816 : MAX_SOURCE_LOCATION;
819 /* Returns the last macro map allocated in the line table SET. */
820 inline line_map_macro *
821 LINEMAPS_LAST_ALLOCATED_MACRO_MAP (const line_maps *set)
823 return (line_map_macro *)LINEMAPS_LAST_ALLOCATED_MAP (set, true);
826 extern void location_adhoc_data_fini (struct line_maps *);
827 extern source_location get_combined_adhoc_loc (struct line_maps *,
828 source_location, void *);
829 extern void *get_data_from_adhoc_loc (struct line_maps *, source_location);
830 extern source_location get_location_from_adhoc_loc (struct line_maps *,
831 source_location);
833 /* Get whether location LOC is an ad-hoc location. */
835 inline bool
836 IS_ADHOC_LOC (source_location loc)
838 return (loc & MAX_SOURCE_LOCATION) != loc;
841 /* Combine LOC and BLOCK, giving a combined adhoc location. */
843 inline source_location
844 COMBINE_LOCATION_DATA (struct line_maps *set,
845 source_location loc,
846 void *block)
848 return get_combined_adhoc_loc (set, loc, block);
851 extern void rebuild_location_adhoc_htab (struct line_maps *);
853 /* Initialize a line map set. SET is the line map set to initialize
854 and BUILTIN_LOCATION is the special location value to be used as
855 spelling location for built-in tokens. This BUILTIN_LOCATION has
856 to be strictly less than RESERVED_LOCATION_COUNT. */
857 extern void linemap_init (struct line_maps *set,
858 source_location builtin_location);
860 /* Check for and warn about line_maps entered but not exited. */
862 extern void linemap_check_files_exited (struct line_maps *);
864 /* Return a source_location for the start (i.e. column==0) of
865 (physical) line TO_LINE in the current source file (as in the
866 most recent linemap_add). MAX_COLUMN_HINT is the highest column
867 number we expect to use in this line (but it does not change
868 the highest_location). */
870 extern source_location linemap_line_start
871 (struct line_maps *set, linenum_type to_line, unsigned int max_column_hint);
873 /* Add a mapping of logical source line to physical source file and
874 line number. This function creates an "ordinary map", which is a
875 map that records locations of tokens that are not part of macro
876 replacement-lists present at a macro expansion point.
878 The text pointed to by TO_FILE must have a lifetime
879 at least as long as the lifetime of SET. An empty
880 TO_FILE means standard input. If reason is LC_LEAVE, and
881 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
882 natural values considering the file we are returning to.
884 A call to this function can relocate the previous set of
885 maps, so any stored line_map pointers should not be used. */
886 extern const struct line_map *linemap_add
887 (struct line_maps *, enum lc_reason, unsigned int sysp,
888 const char *to_file, linenum_type to_line);
890 /* Given a logical source location, returns the map which the
891 corresponding (source file, line, column) triplet can be deduced
892 from. Since the set is built chronologically, the logical lines are
893 monotonic increasing, and so the list is sorted and we can use a
894 binary search. If no line map have been allocated yet, this
895 function returns NULL. */
896 extern const struct line_map *linemap_lookup
897 (struct line_maps *, source_location);
899 /* Returns TRUE if the line table set tracks token locations across
900 macro expansion, FALSE otherwise. */
901 bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
903 /* Return the name of the macro associated to MACRO_MAP. */
904 const char* linemap_map_get_macro_name (const line_map_macro *);
906 /* Return a positive value if LOCATION is the locus of a token that is
907 located in a system header, O otherwise. It returns 1 if LOCATION
908 is the locus of a token that is located in a system header, and 2
909 if LOCATION is the locus of a token located in a C system header
910 that therefore needs to be extern "C" protected in C++.
912 Note that this function returns 1 if LOCATION belongs to a token
913 that is part of a macro replacement-list defined in a system
914 header, but expanded in a non-system file. */
915 int linemap_location_in_system_header_p (struct line_maps *,
916 source_location);
918 /* Return TRUE if LOCATION is a source code location of a token coming
919 from a macro replacement-list at a macro expansion point, FALSE
920 otherwise. */
921 bool linemap_location_from_macro_expansion_p (const struct line_maps *,
922 source_location);
924 /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
925 be reserved for libcpp user as special values, no token from libcpp
926 will contain any of those locations. */
927 const source_location RESERVED_LOCATION_COUNT = 2;
929 /* Converts a map and a source_location to source line. */
930 inline linenum_type
931 SOURCE_LINE (const line_map_ordinary *ord_map, source_location loc)
933 return ((loc - ord_map->start_location)
934 >> ord_map->column_bits) + ord_map->to_line;
937 /* Convert a map and source_location to source column number. */
938 inline linenum_type
939 SOURCE_COLUMN (const line_map_ordinary *ord_map, source_location loc)
941 return ((loc - ord_map->start_location)
942 & ((1 << ord_map->column_bits) - 1));
945 /* Return the location of the last source line within an ordinary
946 map. */
947 inline source_location
948 LAST_SOURCE_LINE_LOCATION (const line_map_ordinary *map)
950 return (((map[1].start_location - 1
951 - map->start_location)
952 & ~((1 << map->column_bits) - 1))
953 + map->start_location);
956 /* Returns the last source line number within an ordinary map. This
957 is the (last) line of the #include, or other directive, that caused
958 a map change. */
959 inline linenum_type
960 LAST_SOURCE_LINE (const line_map_ordinary *map)
962 return SOURCE_LINE (map, LAST_SOURCE_LINE_LOCATION (map));
965 /* Return the last column number within an ordinary map. */
967 inline linenum_type
968 LAST_SOURCE_COLUMN (const line_map_ordinary *map)
970 return SOURCE_COLUMN (map, LAST_SOURCE_LINE_LOCATION (map));
973 /* Returns the map a given map was included from, or NULL if the map
974 belongs to the main file, i.e, a file that wasn't included by
975 another one. */
976 inline line_map_ordinary *
977 INCLUDED_FROM (struct line_maps *set, const line_map_ordinary *ord_map)
979 return ((ord_map->included_from == -1)
980 ? NULL
981 : LINEMAPS_ORDINARY_MAP_AT (set, ord_map->included_from));
984 /* True if the map is at the bottom of the include stack. */
986 inline bool
987 MAIN_FILE_P (const line_map_ordinary *ord_map)
989 return ord_map->included_from < 0;
992 /* Encode and return a source_location from a column number. The
993 source line considered is the last source line used to call
994 linemap_line_start, i.e, the last source line which a location was
995 encoded from. */
996 extern source_location
997 linemap_position_for_column (struct line_maps *, unsigned int);
999 /* Encode and return a source location from a given line and
1000 column. */
1001 source_location
1002 linemap_position_for_line_and_column (const line_map_ordinary *,
1003 linenum_type, unsigned int);
1005 /* Encode and return a source_location starting from location LOC and
1006 shifting it by OFFSET columns. This function does not support
1007 virtual locations. */
1008 source_location
1009 linemap_position_for_loc_and_offset (struct line_maps *set,
1010 source_location loc,
1011 unsigned int offset);
1013 /* Return the file this map is for. */
1014 inline const char *
1015 LINEMAP_FILE (const line_map_ordinary *ord_map)
1017 return ord_map->to_file;
1020 /* Return the line number this map started encoding location from. */
1021 inline linenum_type
1022 LINEMAP_LINE (const line_map_ordinary *ord_map)
1024 return ord_map->to_line;
1027 /* Return a positive value if map encodes locations from a system
1028 header, 0 otherwise. Returns 1 if MAP encodes locations in a
1029 system header and 2 if it encodes locations in a C system header
1030 that therefore needs to be extern "C" protected in C++. */
1031 inline unsigned char
1032 LINEMAP_SYSP (const line_map_ordinary *ord_map)
1034 return ord_map->sysp;
1037 /* Return a positive value if PRE denotes the location of a token that
1038 comes before the token of POST, 0 if PRE denotes the location of
1039 the same token as the token for POST, and a negative value
1040 otherwise. */
1041 int linemap_compare_locations (struct line_maps *set,
1042 source_location pre,
1043 source_location post);
1045 /* Return TRUE if LOC_A denotes the location a token that comes
1046 topogically before the token denoted by location LOC_B, or if they
1047 are equal. */
1048 inline bool
1049 linemap_location_before_p (struct line_maps *set,
1050 source_location loc_a,
1051 source_location loc_b)
1053 return linemap_compare_locations (set, loc_a, loc_b) >= 0;
1056 typedef struct
1058 /* The name of the source file involved. */
1059 const char *file;
1061 /* The line-location in the source file. */
1062 int line;
1064 int column;
1066 void *data;
1068 /* In a system header?. */
1069 bool sysp;
1070 } expanded_location;
1072 /* Both gcc and emacs number source *lines* starting at 1, but
1073 they have differing conventions for *columns*.
1075 GCC uses a 1-based convention for source columns,
1076 whereas Emacs's M-x column-number-mode uses a 0-based convention.
1078 For example, an error in the initial, left-hand
1079 column of source line 3 is reported by GCC as:
1081 some-file.c:3:1: error: ...etc...
1083 On navigating to the location of that error in Emacs
1084 (e.g. via "next-error"),
1085 the locus is reported in the Mode Line
1086 (assuming M-x column-number-mode) as:
1088 some-file.c 10% (3, 0)
1090 i.e. "3:1:" in GCC corresponds to "(3, 0)" in Emacs. */
1092 /* Ranges are closed
1093 m_start is the first location within the range, and
1094 m_finish is the last location within the range. */
1095 struct location_range
1097 expanded_location m_start;
1098 expanded_location m_finish;
1100 /* Should a caret be drawn for this range? Typically this is
1101 true for the 0th range, and false for subsequent ranges,
1102 but the Fortran frontend overrides this for rendering things like:
1104 x = x + y
1106 Error: Shapes for operands at (1) and (2) are not conformable
1108 where "1" and "2" are notionally carets. */
1109 bool m_show_caret_p;
1110 expanded_location m_caret;
1113 /* A "rich" source code location, for use when printing diagnostics.
1114 A rich_location has one or more ranges, each optionally with
1115 a caret. Typically the zeroth range has a caret; other ranges
1116 sometimes have carets.
1118 The "primary" location of a rich_location is the caret of range 0,
1119 used for determining the line/column when printing diagnostic
1120 text, such as:
1122 some-file.c:3:1: error: ...etc...
1124 Additional ranges may be added to help the user identify other
1125 pertinent clauses in a diagnostic.
1127 rich_location instances are intended to be allocated on the stack
1128 when generating diagnostics, and to be short-lived.
1130 Examples of rich locations
1131 --------------------------
1133 Example A
1134 *********
1135 int i = "foo";
1137 This "rich" location is simply a single range (range 0), with
1138 caret = start = finish at the given point.
1140 Example B
1141 *********
1142 a = (foo && bar)
1143 ~~~~~^~~~~~~
1144 This rich location has a single range (range 0), with the caret
1145 at the first "&", and the start/finish at the parentheses.
1146 Compare with example C below.
1148 Example C
1149 *********
1150 a = (foo && bar)
1151 ~~~ ^~ ~~~
1152 This rich location has three ranges:
1153 - Range 0 has its caret and start location at the first "&" and
1154 end at the second "&.
1155 - Range 1 has its start and finish at the "f" and "o" of "foo";
1156 the caret is not flagged for display, but is perhaps at the "f"
1157 of "foo".
1158 - Similarly, range 2 has its start and finish at the "b" and "r" of
1159 "bar"; the caret is not flagged for display, but is perhaps at the
1160 "b" of "bar".
1161 Compare with example B above.
1163 Example D (Fortran frontend)
1164 ****************************
1165 x = x + y
1167 This rich location has range 0 at "1", and range 1 at "2".
1168 Both are flagged for caret display. Both ranges have start/finish
1169 equal to their caret point. The frontend overrides the diagnostic
1170 context's default caret character for these ranges.
1172 Example E
1173 *********
1174 printf ("arg0: %i arg1: %s arg2: %i",
1176 100, 101, 102);
1178 This rich location has two ranges:
1179 - range 0 is at the "%s" with start = caret = "%" and finish at
1180 the "s".
1181 - range 1 has start/finish covering the "101" and is not flagged for
1182 caret printing; it is perhaps at the start of "101". */
1184 class rich_location
1186 public:
1187 /* Constructors. */
1189 /* Constructing from a location. */
1190 rich_location (source_location loc);
1192 /* Constructing from a source_range. */
1193 rich_location (source_range src_range);
1195 /* Accessors. */
1196 source_location get_loc () const { return m_loc; }
1198 source_location *get_loc_addr () { return &m_loc; }
1200 void
1201 add_range (source_location start, source_location finish,
1202 bool show_caret_p);
1204 void
1205 add_range (source_range src_range, bool show_caret_p);
1207 void
1208 add_range (location_range *src_range);
1210 void
1211 set_range (unsigned int idx, source_range src_range,
1212 bool show_caret_p, bool overwrite_loc_p);
1214 unsigned int get_num_locations () const { return m_num_ranges; }
1216 location_range *get_range (unsigned int idx)
1218 linemap_assert (idx < m_num_ranges);
1219 return &m_ranges[idx];
1222 expanded_location lazily_expand_location ();
1224 void
1225 override_column (int column);
1227 public:
1228 static const int MAX_RANGES = 3;
1230 protected:
1231 source_location m_loc;
1233 unsigned int m_num_ranges;
1234 location_range m_ranges[MAX_RANGES];
1236 bool m_have_expanded_location;
1237 expanded_location m_expanded_location;
1240 /* This is enum is used by the function linemap_resolve_location
1241 below. The meaning of the values is explained in the comment of
1242 that function. */
1243 enum location_resolution_kind
1245 LRK_MACRO_EXPANSION_POINT,
1246 LRK_SPELLING_LOCATION,
1247 LRK_MACRO_DEFINITION_LOCATION
1250 /* Resolve a virtual location into either a spelling location, an
1251 expansion point location or a token argument replacement point
1252 location. Return the map that encodes the virtual location as well
1253 as the resolved location.
1255 If LOC is *NOT* the location of a token resulting from the
1256 expansion of a macro, then the parameter LRK (which stands for
1257 Location Resolution Kind) is ignored and the resulting location
1258 just equals the one given in argument.
1260 Now if LOC *IS* the location of a token resulting from the
1261 expansion of a macro, this is what happens.
1263 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1264 -------------------------------
1266 The virtual location is resolved to the first macro expansion point
1267 that led to this macro expansion.
1269 * If LRK is set to LRK_SPELLING_LOCATION
1270 -------------------------------------
1272 The virtual location is resolved to the locus where the token has
1273 been spelled in the source. This can follow through all the macro
1274 expansions that led to the token.
1276 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1277 --------------------------------------
1279 The virtual location is resolved to the locus of the token in the
1280 context of the macro definition.
1282 If LOC is the locus of a token that is an argument of a
1283 function-like macro [replacing a parameter in the replacement list
1284 of the macro] the virtual location is resolved to the locus of the
1285 parameter that is replaced, in the context of the definition of the
1286 macro.
1288 If LOC is the locus of a token that is not an argument of a
1289 function-like macro, then the function behaves as if LRK was set to
1290 LRK_SPELLING_LOCATION.
1292 If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1293 returned location. Note that if the returned location wasn't originally
1294 encoded by a map, the *MAP is set to NULL. This can happen if LOC
1295 resolves to a location reserved for the client code, like
1296 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1298 source_location linemap_resolve_location (struct line_maps *,
1299 source_location loc,
1300 enum location_resolution_kind lrk,
1301 const line_map_ordinary **loc_map);
1303 /* Suppose that LOC is the virtual location of a token coming from the
1304 expansion of a macro M. This function then steps up to get the
1305 location L of the point where M got expanded. If L is a spelling
1306 location inside a macro expansion M', then this function returns
1307 the point where M' was expanded. LOC_MAP is an output parameter.
1308 When non-NULL, *LOC_MAP is set to the map of the returned
1309 location. */
1310 source_location linemap_unwind_toward_expansion (struct line_maps *,
1311 source_location loc,
1312 const struct line_map **loc_map);
1314 /* If LOC is the virtual location of a token coming from the expansion
1315 of a macro M and if its spelling location is reserved (e.g, a
1316 location for a built-in token), then this function unwinds (using
1317 linemap_unwind_toward_expansion) the location until a location that
1318 is not reserved and is not in a system header is reached. In other
1319 words, this unwinds the reserved location until a location that is
1320 in real source code is reached.
1322 Otherwise, if the spelling location for LOC is not reserved or if
1323 LOC doesn't come from the expansion of a macro, the function
1324 returns LOC as is and *MAP is not touched.
1326 *MAP is set to the map of the returned location if the later is
1327 different from LOC. */
1328 source_location linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
1329 source_location loc,
1330 const struct line_map **map);
1332 /* Expand source code location LOC and return a user readable source
1333 code location. LOC must be a spelling (non-virtual) location. If
1334 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1335 location is returned. */
1336 expanded_location linemap_expand_location (struct line_maps *,
1337 const struct line_map *,
1338 source_location loc);
1340 /* Statistics about maps allocation and usage as returned by
1341 linemap_get_statistics. */
1342 struct linemap_stats
1344 long num_ordinary_maps_allocated;
1345 long num_ordinary_maps_used;
1346 long ordinary_maps_allocated_size;
1347 long ordinary_maps_used_size;
1348 long num_expanded_macros;
1349 long num_macro_tokens;
1350 long num_macro_maps_used;
1351 long macro_maps_allocated_size;
1352 long macro_maps_used_size;
1353 long macro_maps_locations_size;
1354 long duplicated_macro_maps_locations_size;
1355 long adhoc_table_size;
1356 long adhoc_table_entries_used;
1359 /* Return the highest location emitted for a given file for which
1360 there is a line map in SET. FILE_NAME is the file name to
1361 consider. If the function returns TRUE, *LOC is set to the highest
1362 location emitted for that file. */
1363 bool linemap_get_file_highest_location (struct line_maps * set,
1364 const char *file_name,
1365 source_location *loc);
1367 /* Compute and return statistics about the memory consumption of some
1368 parts of the line table SET. */
1369 void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
1371 /* Dump debugging information about source location LOC into the file
1372 stream STREAM. SET is the line map set LOC comes from. */
1373 void linemap_dump_location (struct line_maps *, source_location, FILE *);
1375 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1376 is NULL, use stderr. IS_MACRO is true if the caller wants to
1377 dump a macro map, false otherwise. */
1378 void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
1380 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1381 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1382 specifies how many macro maps to dump. */
1383 void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
1385 /* The rich_location class requires a way to expand source_location instances.
1386 We would directly use expand_location_to_spelling_point, which is
1387 implemented in gcc/input.c, but we also need to use it for rich_location
1388 within genmatch.c.
1389 Hence we require client code of libcpp to implement the following
1390 symbol. */
1391 extern expanded_location
1392 linemap_client_expand_location_to_spelling_point (source_location );
1394 #endif /* !LIBCPP_LINE_MAP_H */