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