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