]> git.ipfire.org Git - thirdparty/gcc.git/blob - libcpp/include/line-map.h
Support location tracking for built-in macro tokens
[thirdparty/gcc.git] / libcpp / include / line-map.h
1 /* Map logical line numbers to (source file, line number) pairs.
2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.
17
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! */
21
22 #ifndef LIBCPP_LINE_MAP_H
23 #define LIBCPP_LINE_MAP_H
24
25 #ifndef GTY
26 #define GTY(x) /* nothing */
27 #endif
28
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
37 {
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. */
44 };
45
46 /* The type of line numbers. */
47 typedef unsigned int linenum_type;
48
49 /* A logical line/column number, i.e. an "index" into a line_map. */
50 typedef unsigned int source_location;
51
52 /* Memory allocation function typedef. Works like xrealloc. */
53 typedef void *(*line_map_realloc) (void *, size_t);
54
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);
58
59 /* An ordinary line map encodes physical source locations. Those
60 physical source locations are called "spelling locations".
61
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".)
68
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;
73
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;
78
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;
84
85 /* Number of the low-order source_location bits used for a column number. */
86 unsigned int column_bits : 8;
87 };
88
89 /* This is the highest possible source location encoded within an
90 ordinary or macro map. */
91 #define MAX_SOURCE_LOCATION 0x7FFFFFFF
92
93 struct cpp_hashnode;
94
95 /* A macro line map encodes location of tokens coming from a macro
96 expansion.
97
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.
101
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;
110
111 /* The number of tokens inside the replacement-list of MACRO. */
112 unsigned int n_tokens;
113
114 /* This array of location is actually an array of pairs of
115 locations. The elements inside it thus look like:
116
117 x0,y0, x1,y1, x2,y2, ...., xn,yn.
118
119 where n == n_tokens;
120
121 Remember that these xI,yI are collected when libcpp is about to
122 expand a given macro.
123
124 yI is the location in the macro definition, either of the token
125 itself or of a macro parameter that it replaces.
126
127 Imagine this:
128
129 #define PLUS(A, B) A + B <--- #1
130
131 int a = PLUS (1,2); <--- #2
132
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:
138
139 A, +, B
140
141 and the expansion-list is made of the tokens:
142
143 1, +, 2
144
145 Let's consider the case of token "+". Its y1 [yI for I == 1] is
146 its spelling location in #1.
147
148 y0 (thus for token "1") is the spelling location of A in #1.
149
150 And y2 (of token "2") is the spelling location of B in #1.
151
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.
158
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.
163
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;
168
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;
175 };
176
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.
180
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:
183
184 #define PLUS(A,B) A + B
185
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.
190
191 On the other hand, if we later do:
192
193 int a = PLUS (1,2);
194
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;
205
206 /* The reason for creation of this line map. */
207 ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
208
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;
213 };
214
215 #define MAP_START_LOCATION(MAP) (MAP)->start_location
216
217 #define ORDINARY_MAP_FILE_NAME(MAP) \
218 linemap_check_ordinary (MAP)->d.ordinary.to_file
219
220 #define ORDINARY_MAP_STARTING_LINE_NUMBER(MAP) \
221 linemap_check_ordinary (MAP)->d.ordinary.to_line
222
223 #define ORDINARY_MAP_INCLUDER_FILE_INDEX(MAP) \
224 linemap_check_ordinary (MAP)->d.ordinary.included_from
225
226 #define ORDINARY_MAP_IN_SYSTEM_HEADER_P(MAP) \
227 linemap_check_ordinary (MAP)->d.ordinary.sysp
228
229 #define ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(MAP) \
230 linemap_check_ordinary (MAP)->d.ordinary.column_bits
231
232 #define MACRO_MAP_MACRO(MAP) (MAP)->d.macro.macro
233
234 #define MACRO_MAP_NUM_MACRO_TOKENS(MAP) (MAP)->d.macro.n_tokens
235
236 #define MACRO_MAP_LOCATIONS(MAP) (MAP)->d.macro.macro_locations
237
238 #define MACRO_MAP_EXPANSION_POINT_LOCATION(MAP) (MAP)->d.macro.expansion
239
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;
250
251 /* The total number of allocated maps. */
252 unsigned int allocated;
253
254 /* The number of elements used in maps. This number is smaller
255 or equal to ALLOCATED. */
256 unsigned int used;
257
258 unsigned int cache;
259 };
260
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;
265 };
266
267 struct htab;
268
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.
272
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. */
278
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;
284 };
285
286 /* A set of chronological line_map structures. */
287 struct GTY(()) line_maps {
288
289 struct maps_info info_ordinary;
290
291 struct maps_info info_macro;
292
293 /* Depth of the include stack, including the current file. */
294 unsigned int depth;
295
296 /* If true, prints an include trace a la -H. */
297 bool trace_includes;
298
299 /* Highest source_location "given out". */
300 source_location highest_location;
301
302 /* Start of line of highest source_location "given out". */
303 source_location highest_line;
304
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;
308
309 /* If non-null, the allocator to use when resizing 'maps'. If null,
310 xrealloc is used. */
311 line_map_realloc reallocator;
312
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;
316
317 struct location_adhoc_data_map location_adhoc_data_map;
318
319 /* The special location value that is used as spelling location for
320 built-in tokens. */
321 source_location builtin_location;
322 };
323
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))
331
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
337
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
342
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
347
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
353
354 /* Return the map at a given index. */
355 #define LINEMAPS_MAP_AT(SET, MAP_KIND, INDEX) \
356 (&((LINEMAPS_MAPS (SET, MAP_KIND))[(INDEX)]))
357
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))
363
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)
369
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)
374
375 /* Returns the INDEXth ordinary map. */
376 #define LINEMAPS_ORDINARY_MAP_AT(SET, INDEX) \
377 LINEMAPS_MAP_AT (SET, false, INDEX)
378
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)
383
384 /* Return the number of ordinary maps used in the line table SET. */
385 #define LINEMAPS_ORDINARY_USED(SET) \
386 LINEMAPS_USED(SET, false)
387
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)
392
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)
397
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)
402
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)
407
408 /* Returns the INDEXth macro map. */
409 #define LINEMAPS_MACRO_MAP_AT(SET, INDEX) \
410 LINEMAPS_MAP_AT (SET, true, INDEX)
411
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)
416
417 /* Returns the number of macro maps used in the line table SET. */
418 #define LINEMAPS_MACRO_USED(SET) \
419 LINEMAPS_USED(SET, true)
420
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)
425
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)
432
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)
436
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)
440
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);
447
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))
451
452 extern void rebuild_location_adhoc_htab (struct line_maps *);
453
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);
460
461 /* Check for and warn about line_maps entered but not exited. */
462
463 extern void linemap_check_files_exited (struct line_maps *);
464
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). */
470
471 extern source_location linemap_line_start
472 (struct line_maps *set, linenum_type to_line, unsigned int max_column_hint);
473
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.
478
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.
484
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);
490
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);
499
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 *);
503
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 *);
507
508 /* Return the name of the macro associated to MACRO_MAP. */
509 const char* linemap_map_get_macro_name (const struct line_map*);
510
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++.
516
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);
522
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 (struct line_maps *,
527 source_location);
528
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
533
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))
538
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)))
543
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))
549
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))
553
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))
561
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]))
569
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))
573
574 #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
575
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)
582
583 /* Assert that MAP encodes locations of tokens that are not part of
584 the replacement-list of a macro expansion. */
585 #define linemap_check_ordinary(LINE_MAP) __extension__ \
586 ({linemap_assert (!linemap_macro_expansion_map_p (LINE_MAP)); \
587 (LINE_MAP);})
588 #else
589 #define linemap_assert(EXPR)
590 #define linemap_check_ordinary(LINE_MAP) (LINE_MAP)
591 #endif
592
593 /* Encode and return a source_location from a column number. The
594 source line considered is the last source line used to call
595 linemap_line_start, i.e, the last source line which a location was
596 encoded from. */
597 extern source_location
598 linemap_position_for_column (struct line_maps *, unsigned int);
599
600 /* Encode and return a source location from a given line and
601 column. */
602 source_location linemap_position_for_line_and_column (struct line_map *,
603 linenum_type,
604 unsigned int);
605 /* Return the file this map is for. */
606 #define LINEMAP_FILE(MAP) \
607 (linemap_check_ordinary (MAP)->d.ordinary.to_file)
608
609 /* Return the line number this map started encoding location from. */
610 #define LINEMAP_LINE(MAP) \
611 (linemap_check_ordinary (MAP)->d.ordinary.to_line)
612
613 /* Return a positive value if map encodes locations from a system
614 header, 0 otherwise. Returns 1 if MAP encodes locations in a
615 system header and 2 if it encodes locations in a C system header
616 that therefore needs to be extern "C" protected in C++. */
617 #define LINEMAP_SYSP(MAP) \
618 (linemap_check_ordinary (MAP)->d.ordinary.sysp)
619
620 /* Return a positive value if PRE denotes the location of a token that
621 comes before the token of POST, 0 if PRE denotes the location of
622 the same token as the token for POST, and a negative value
623 otherwise. */
624 int linemap_compare_locations (struct line_maps *set,
625 source_location pre,
626 source_location post);
627
628 /* Return TRUE if LOC_A denotes the location a token that comes
629 topogically before the token denoted by location LOC_B, or if they
630 are equal. */
631 #define linemap_location_before_p(SET, LOC_A, LOC_B) \
632 (linemap_compare_locations ((SET), (LOC_A), (LOC_B)) >= 0)
633
634 typedef struct
635 {
636 /* The name of the source file involved. */
637 const char *file;
638
639 /* The line-location in the source file. */
640 int line;
641
642 int column;
643
644 void *data;
645
646 /* In a system header?. */
647 bool sysp;
648 } expanded_location;
649
650 /* This is enum is used by the function linemap_resolve_location
651 below. The meaning of the values is explained in the comment of
652 that function. */
653 enum location_resolution_kind
654 {
655 LRK_MACRO_EXPANSION_POINT,
656 LRK_SPELLING_LOCATION,
657 LRK_MACRO_DEFINITION_LOCATION
658 };
659
660 /* Resolve a virtual location into either a spelling location, an
661 expansion point location or a token argument replacement point
662 location. Return the map that encodes the virtual location as well
663 as the resolved location.
664
665 If LOC is *NOT* the location of a token resulting from the
666 expansion of a macro, then the parameter LRK (which stands for
667 Location Resolution Kind) is ignored and the resulting location
668 just equals the one given in argument.
669
670 Now if LOC *IS* the location of a token resulting from the
671 expansion of a macro, this is what happens.
672
673 * If LRK is set to LRK_MACRO_EXPANSION_POINT
674 -------------------------------
675
676 The virtual location is resolved to the first macro expansion point
677 that led to this macro expansion.
678
679 * If LRK is set to LRK_SPELLING_LOCATION
680 -------------------------------------
681
682 The virtual location is resolved to the locus where the token has
683 been spelled in the source. This can follow through all the macro
684 expansions that led to the token.
685
686 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
687 --------------------------------------
688
689 The virtual location is resolved to the locus of the token in the
690 context of the macro definition.
691
692 If LOC is the locus of a token that is an argument of a
693 function-like macro [replacing a parameter in the replacement list
694 of the macro] the virtual location is resolved to the locus of the
695 parameter that is replaced, in the context of the definition of the
696 macro.
697
698 If LOC is the locus of a token that is not an argument of a
699 function-like macro, then the function behaves as if LRK was set to
700 LRK_SPELLING_LOCATION.
701
702 If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
703 returned location. Note that if the returned location wasn't originally
704 encoded by a map, the *MAP is set to NULL. This can happen if LOC
705 resolves to a location reserved for the client code, like
706 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
707
708 source_location linemap_resolve_location (struct line_maps *,
709 source_location loc,
710 enum location_resolution_kind lrk,
711 const struct line_map **loc_map);
712
713 /* Suppose that LOC is the virtual location of a token coming from the
714 expansion of a macro M. This function then steps up to get the
715 location L of the point where M got expanded. If L is a spelling
716 location inside a macro expansion M', then this function returns
717 the point where M' was expanded. LOC_MAP is an output parameter.
718 When non-NULL, *LOC_MAP is set to the map of the returned
719 location. */
720 source_location linemap_unwind_toward_expansion (struct line_maps *,
721 source_location loc,
722 const struct line_map **loc_map);
723
724 /* If LOC is the virtual location of a token coming from the expansion
725 of a macro M and if its spelling location is reserved (e.g, a
726 location for a built-in token), then this function unwinds (using
727 linemap_unwind_toward_expansion) the location until a location that
728 is not reserved and is not in a system header is reached. In other
729 words, this unwinds the reserved location until a location that is
730 in real source code is reached.
731
732 Otherwise, if the spelling location for LOC is not reserved or if
733 LOC doesn't come from the expansion of a macro, the function
734 returns LOC as is and *MAP is not touched.
735
736 *MAP is set to the map of the returned location if the later is
737 different from LOC. */
738 source_location linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
739 source_location loc,
740 const struct line_map **map);
741
742 /* Expand source code location LOC and return a user readable source
743 code location. LOC must be a spelling (non-virtual) location. If
744 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
745 location is returned. */
746 expanded_location linemap_expand_location (struct line_maps *,
747 const struct line_map *,
748 source_location loc);
749
750 /* Statistics about maps allocation and usage as returned by
751 linemap_get_statistics. */
752 struct linemap_stats
753 {
754 long num_ordinary_maps_allocated;
755 long num_ordinary_maps_used;
756 long ordinary_maps_allocated_size;
757 long ordinary_maps_used_size;
758 long num_expanded_macros;
759 long num_macro_tokens;
760 long num_macro_maps_used;
761 long macro_maps_allocated_size;
762 long macro_maps_used_size;
763 long macro_maps_locations_size;
764 long duplicated_macro_maps_locations_size;
765 };
766
767 /* Return the highest location emitted for a given file for which
768 there is a line map in SET. FILE_NAME is the file name to
769 consider. If the function returns TRUE, *LOC is set to the highest
770 location emitted for that file. */
771 bool linemap_get_file_highest_location (struct line_maps * set,
772 const char *file_name,
773 source_location *loc);
774
775 /* Compute and return statistics about the memory consumption of some
776 parts of the line table SET. */
777 void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
778
779 /* Dump debugging information about source location LOC into the file
780 stream STREAM. SET is the line map set LOC comes from. */
781 void linemap_dump_location (struct line_maps *, source_location, FILE *);
782
783 /* Dump line map at index IX in line table SET to STREAM. If STREAM
784 is NULL, use stderr. IS_MACRO is true if the caller wants to
785 dump a macro map, false otherwise. */
786 void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
787
788 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
789 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
790 specifies how many macro maps to dump. */
791 void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
792
793 #endif /* !LIBCPP_LINE_MAP_H */