Line data Source code
1 : /* Map (unsigned int) keys to (source file, line, column) triples.
2 : Copyright (C) 2001-2026 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 : #include <utility>
26 :
27 : #ifndef GTY
28 : #define GTY(x) /* nothing */
29 : #endif
30 :
31 : /* Both gcc and emacs number source *lines* starting at 1, but
32 : they have differing conventions for *columns*.
33 :
34 : GCC uses a 1-based convention for source columns,
35 : whereas Emacs's M-x column-number-mode uses a 0-based convention.
36 :
37 : For example, an error in the initial, left-hand
38 : column of source line 3 is reported by GCC as:
39 :
40 : some-file.c:3:1: error: ...etc...
41 :
42 : On navigating to the location of that error in Emacs
43 : (e.g. via "next-error"),
44 : the locus is reported in the Mode Line
45 : (assuming M-x column-number-mode) as:
46 :
47 : some-file.c 10% (3, 0)
48 :
49 : i.e. "3:1:" in GCC corresponds to "(3, 0)" in Emacs. */
50 :
51 : /* The type of line numbers. */
52 : typedef unsigned int linenum_type;
53 :
54 : /* A type for doing arithmetic on line numbers. */
55 : typedef long long linenum_arith_t;
56 :
57 : /* A function for for use by qsort for comparing line numbers. */
58 :
59 668158 : inline int compare (linenum_type lhs, linenum_type rhs)
60 : {
61 : /* Avoid truncation issues by using linenum_arith_t for the comparison,
62 : and only consider the sign of the result. */
63 668158 : linenum_arith_t diff = (linenum_arith_t)lhs - (linenum_arith_t)rhs;
64 359086 : if (diff)
65 55738 : return diff > 0 ? 1 : -1;
66 : return 0;
67 : }
68 :
69 : /* Reason for creating a new line map with linemap_add. */
70 : enum lc_reason
71 : {
72 : LC_ENTER = 0, /* Begin #include. */
73 : LC_LEAVE, /* Return to including file. */
74 : LC_RENAME, /* Other reason for name change. */
75 : LC_RENAME_VERBATIM, /* Likewise, but "" != stdin. */
76 : LC_ENTER_MACRO, /* Begin macro expansion. */
77 : LC_MODULE, /* A (C++) Module. */
78 : /* FIXME: add support for stringize and paste. */
79 : LC_HWM /* High Water Mark. */
80 : };
81 :
82 : /* The typedef "location_t" is a key within the location database,
83 : identifying a source location or macro expansion, along with range
84 : information, and (optionally) a pointer for use by gcc.
85 :
86 : This key only has meaning in relation to a line_maps instance. Within
87 : gcc there is a single line_maps instance: "line_table", declared in
88 : gcc/input.h and defined in gcc/input.cc.
89 :
90 : The values of the keys are intended to be internal to libcpp, but for
91 : ease-of-understanding the implementation, they are currently assigned as
92 : follows in the case of 32-bit location_t:
93 :
94 : Actual | Value | Meaning
95 : -----------+-------------------------------+-------------------------------
96 : 0x00000000 | UNKNOWN_LOCATION (gcc/input.h)| Unknown/invalid location.
97 : -----------+-------------------------------+-------------------------------
98 : 0x00000001 | BUILTINS_LOCATION | The location for declarations
99 : | (gcc/input.h) | in "<built-in>"
100 : -----------+-------------------------------+-------------------------------
101 : 0x00000002 | RESERVED_LOCATION_COUNT | The first location to be
102 : | (also | handed out, and the
103 : | ordmap[0]->start_location) | first line in ordmap 0
104 : -----------+-------------------------------+-------------------------------
105 : | ordmap[1]->start_location | First line in ordmap 1
106 : | ordmap[1]->start_location+32 | First column in that line
107 : | (assuming range_bits == 5) |
108 : | ordmap[1]->start_location+64 | 2nd column in that line
109 : | ordmap[1]->start_location+4096| Second line in ordmap 1
110 : | (assuming column_bits == 12)
111 : |
112 : | Subsequent lines are offset by (1 << column_bits),
113 : | e.g. 4096 for 12 bits, with a column value of 0 representing
114 : | "the whole line".
115 : |
116 : | Within a line, the low "range_bits" (typically 5) are used for
117 : | storing short ranges, so that there's an offset of
118 : | (1 << range_bits) between individual columns within a line,
119 : | typically 32.
120 : | The low range_bits store the offset of the end point from the
121 : | start point, and the start point is found by masking away
122 : | the range bits.
123 : |
124 : | For example:
125 : | ordmap[1]->start_location+64 "2nd column in that line"
126 : | above means a caret at that location, with a range
127 : | starting and finishing at the same place (the range bits
128 : | are 0), a range of length 1.
129 : |
130 : | By contrast:
131 : | ordmap[1]->start_location+68
132 : | has range bits 0x4, meaning a caret with a range starting at
133 : | that location, but with endpoint 4 columns further on: a range
134 : | of length 5.
135 : |
136 : | Ranges that have caret != start, or have an endpoint too
137 : | far away to fit in range_bits are instead stored as ad-hoc
138 : | locations. Hence for range_bits == 5 we can compactly store
139 : | tokens of length <= 32 without needing to use the ad-hoc
140 : | table.
141 : |
142 : | This packing scheme means we effectively have
143 : | (column_bits - range_bits)
144 : | of bits for the columns, typically (12 - 5) = 7, for 128
145 : | columns; longer line widths are accomodated by starting a
146 : | new ordmap with a higher column_bits.
147 : |
148 : | ordmap[2]->start_location-1 | Final location in ordmap 1
149 : -----------+-------------------------------+-------------------------------
150 : | ordmap[2]->start_location | First line in ordmap 2
151 : | ordmap[3]->start_location-1 | Final location in ordmap 2
152 : -----------+-------------------------------+-------------------------------
153 : | | (etc)
154 : -----------+-------------------------------+-------------------------------
155 : | ordmap[n-1]->start_location | First line in final ord map
156 : | | (etc)
157 : | set->highest_location - 1 | Final location in that ordmap
158 : -----------+-------------------------------+-------------------------------
159 : | set->highest_location | Location of the where the next
160 : | | ordinary linemap would start
161 : -----------+-------------------------------+-------------------------------
162 : | |
163 : | VVVVVVVVVVVVVVVVVVVVVVVVVVV
164 : | Ordinary maps grow this way
165 : |
166 : | (unallocated integers)
167 : |
168 : 0x60000000 | LINE_MAP_MAX_LOCATION_WITH_COLS
169 : | Beyond this point, ordinary linemaps have 0 bits per column:
170 : | each increment of the value corresponds to a new source line.
171 : |
172 : 0x70000000 | LINE_MAP_MAX_LOCATION
173 : | Beyond the point, we give up on ordinary maps; attempts to
174 : | create locations in them lead to UNKNOWN_LOCATION (0).
175 : |
176 : | (unallocated integers)
177 : |
178 : | Macro maps grow this way
179 : | ^^^^^^^^^^^^^^^^^^^^^^^^
180 : | |
181 : -----------+-------------------------------+-------------------------------
182 : | LINEMAPS_MACRO_LOWEST_LOCATION| Locations within macro maps
183 : | macromap[m-1]->start_location | Start of last macro map
184 : | |
185 : -----------+-------------------------------+-------------------------------
186 : | macromap[m-2]->start_location | Start of penultimate macro map
187 : -----------+-------------------------------+-------------------------------
188 : | macromap[1]->start_location | Start of macro map 1
189 : -----------+-------------------------------+-------------------------------
190 : | macromap[0]->start_location | Start of macro map 0
191 : 0x7fffffff | MAX_LOCATION_T | Also used as a mask for
192 : | | accessing the ad-hoc data table
193 : -----------+-------------------------------+-------------------------------
194 : 0x80000000 | Start of ad-hoc values; the lower 31 bits are used as an index
195 : ... | into the line_table->location_adhoc_data_map.data array.
196 : 0xffffffff | UINT_MAX |
197 : -----------+-------------------------------+-------------------------------
198 :
199 : Examples of location encoding.
200 :
201 : Packed ranges
202 : =============
203 :
204 : Consider encoding the location of a token "foo", seen underlined here
205 : on line 523, within an ordinary line_map that starts at line 500:
206 :
207 : 11111111112
208 : 12345678901234567890
209 : 522
210 : 523 return foo + bar;
211 : ^~~
212 : 524
213 :
214 : The location's caret and start are both at line 523, column 11; the
215 : location's finish is on the same line, at column 13 (an offset of 2
216 : columns, for length 3).
217 :
218 : Line 523 is offset 23 from the starting line of the ordinary line_map.
219 :
220 : caret == start, and the offset of the finish fits within 5 bits, so
221 : this can be stored as a packed range.
222 :
223 : This is encoded as:
224 : ordmap->start
225 : + (line_offset << ordmap->m_column_and_range_bits)
226 : + (column << ordmap->m_range_bits)
227 : + (range_offset);
228 : i.e. (for line offset 23, column 11, range offset 2):
229 : ordmap->start
230 : + (23 << 12)
231 : + (11 << 5)
232 : + 2;
233 : i.e.:
234 : ordmap->start + 0x17162
235 : assuming that the line_map uses the default of 7 bits for columns and
236 : 5 bits for packed range (giving 12 bits for m_column_and_range_bits).
237 :
238 :
239 : "Pure" locations
240 : ================
241 :
242 : These are a special case of the above, where
243 : caret == start == finish
244 : They are stored as packed ranges with offset == 0.
245 : For example, the location of the "f" of "foo" could be stored
246 : as above, but with range offset 0, giving:
247 : ordmap->start
248 : + (23 << 12)
249 : + (11 << 5)
250 : + 0;
251 : i.e.:
252 : ordmap->start + 0x17160
253 :
254 :
255 : Unoptimized ranges
256 : ==================
257 :
258 : Consider encoding the location of the binary expression
259 : below:
260 :
261 : 11111111112
262 : 12345678901234567890
263 : 522
264 : 523 return foo + bar;
265 : ~~~~^~~~~
266 : 524
267 :
268 : The location's caret is at the "+", line 523 column 15, but starts
269 : earlier, at the "f" of "foo" at column 11. The finish is at the "r"
270 : of "bar" at column 19.
271 :
272 : This can't be stored as a packed range since start != caret.
273 : Hence it is stored as an ad-hoc location e.g. 0x80000003.
274 :
275 : Stripping off the top bit gives us an index into the ad-hoc
276 : lookaside table:
277 :
278 : line_table->location_adhoc_data_map.data[0x3]
279 :
280 : from which the caret, start and finish can be looked up,
281 : encoded as "pure" locations:
282 :
283 : start == ordmap->start + (23 << 12) + (11 << 5)
284 : == ordmap->start + 0x17160 (as above; the "f" of "foo")
285 :
286 : caret == ordmap->start + (23 << 12) + (15 << 5)
287 : == ordmap->start + 0x171e0
288 :
289 : finish == ordmap->start + (23 << 12) + (19 << 5)
290 : == ordmap->start + 0x17260
291 :
292 : To further see how location_t works in practice, see the
293 : worked example in libcpp/location-example.txt. */
294 :
295 : /* A 64-bit type to represent a location. We only use 63 of the 64 bits, so
296 : that two location_t can be safely subtracted and stored in an int64_t. */
297 : typedef uint64_t location_t;
298 : typedef int64_t location_diff_t;
299 :
300 : /* Sometimes we need a type that has the same size as location_t but that does
301 : not represent a location. This typedef provides more clarity in those
302 : cases. */
303 : typedef location_t line_map_uint_t;
304 :
305 : /* Do not track column numbers higher than this one. As a result, the
306 : range of column_bits is [12, 18] (or 0 if column numbers are
307 : disabled). */
308 : const unsigned int LINE_MAP_MAX_COLUMN_NUMBER = (1U << 31) - 1;
309 :
310 : /* Do not pack ranges if locations get higher than this.
311 : If you change this, update:
312 : gcc.dg/plugin/location-overflow-test-*.c. */
313 : const location_t LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
314 : = location_t (0x50000000) << 31;
315 :
316 : /* Do not track column numbers if locations get higher than this.
317 : If you change this, update:
318 : gcc.dg/plugin/location-overflow-test-*.c. */
319 : const location_t LINE_MAP_MAX_LOCATION_WITH_COLS
320 : = location_t (0x60000000) << 31;
321 :
322 : /* Highest possible source location encoded within an ordinary map. Higher
323 : values up to MAX_LOCATION_T represent macro virtual locations. */
324 : const location_t LINE_MAP_MAX_LOCATION = location_t (0x70000000) << 31;
325 :
326 : /* This is the highest possible source location encoded within an
327 : ordinary or macro map. */
328 : const location_t MAX_LOCATION_T = location_t (-1) >> 2;
329 :
330 : /* This is the number of range bits suggested to enable, if range tracking is
331 : desired. */
332 : const int line_map_suggested_range_bits = 7;
333 :
334 : /* A range of source locations.
335 :
336 : Ranges are closed:
337 : m_start is the first location within the range,
338 : m_finish is the last location within the range.
339 :
340 : We may need a more compact way to store these, but for now,
341 : let's do it the simple way, as a pair. */
342 : struct GTY(()) source_range
343 : {
344 : location_t m_start;
345 : location_t m_finish;
346 :
347 : /* We avoid using constructors, since various structs that
348 : don't yet have constructors will embed instances of
349 : source_range. */
350 :
351 : /* Make a source_range from a location_t. */
352 : static source_range from_location (location_t loc)
353 : {
354 : source_range result;
355 : result.m_start = loc;
356 : result.m_finish = loc;
357 : return result;
358 : }
359 :
360 : /* Make a source_range from a pair of location_t. */
361 1856 : static source_range from_locations (location_t start,
362 : location_t finish)
363 : {
364 1856 : source_range result;
365 1856 : result.m_start = start;
366 1856 : result.m_finish = finish;
367 1472 : return result;
368 : }
369 : };
370 :
371 : /* Memory allocation function typedef. Works like xrealloc. */
372 : typedef void *(*line_map_realloc) (void *, size_t);
373 :
374 : /* Memory allocator function that returns the actual allocated size,
375 : for a given requested allocation. */
376 : typedef size_t (*line_map_round_alloc_size_func) (size_t);
377 :
378 : /* A line_map encodes a sequence of locations.
379 : There are two kinds of maps. Ordinary maps and macro expansion
380 : maps, a.k.a macro maps.
381 :
382 : A macro map encodes source locations of tokens that are part of a
383 : macro replacement-list, at a macro expansion point. E.g, in:
384 :
385 : #define PLUS(A,B) A + B
386 :
387 : No macro map is going to be created there, because we are not at a
388 : macro expansion point. We are at a macro /definition/ point. So the
389 : locations of the tokens of the macro replacement-list (i.e, A + B)
390 : will be locations in an ordinary map, not a macro map.
391 :
392 : On the other hand, if we later do:
393 :
394 : int a = PLUS (1,2);
395 :
396 : The invocation of PLUS here is a macro expansion. So we are at a
397 : macro expansion point. The preprocessor expands PLUS (1,2) and
398 : replaces it with the tokens of its replacement-list: 1 + 2. A macro
399 : map is going to be created to hold (or rather to map, haha ...) the
400 : locations of the tokens 1, + and 2. The macro map also records the
401 : location of the expansion point of PLUS. That location is mapped in
402 : the map that is active right before the location of the invocation
403 : of PLUS. */
404 :
405 : /* This contains GTY mark-up to support precompiled headers.
406 : line_map is an abstract class, only derived objects exist. */
407 : struct GTY((tag ("0"), desc ("MAP_ORDINARY_P (&%h) ? 1 : 2"))) line_map {
408 : location_t start_location;
409 :
410 : /* Size is 8 bytes; alignment 4 or 8 depending on the arch. */
411 : };
412 :
413 : /* An ordinary line map encodes physical source locations. Those
414 : physical source locations are called "spelling locations".
415 :
416 : Physical source file TO_FILE at line TO_LINE at column 0 is represented
417 : by the logical START_LOCATION. TO_LINE+L at column C is represented by
418 : START_LOCATION+(L*(1<<m_column_and_range_bits))+(C*1<<m_range_bits), as
419 : long as C<(1<<effective range bits), and the result_location is less than
420 : the next line_map's start_location.
421 : (The top line is line 1 and the leftmost column is column 1; line/column 0
422 : means "entire file/line" or "unknown line/column" or "not applicable".)
423 :
424 : The highest possible source location is MAX_LOCATION_T. */
425 : struct GTY((tag ("1"))) line_map_ordinary : public line_map {
426 : /* Base class is 8 bytes. */
427 :
428 : /* 4 bytes of integers, each 1 byte for easy extraction/insertion. */
429 :
430 : /* The reason for creation of this line map. */
431 : ENUM_BITFIELD (lc_reason) reason : 8;
432 :
433 : /* SYSP is one for a system header, two for a C system header file
434 : that therefore needs to be extern "C" protected in C++, and zero
435 : otherwise. This field isn't really needed now that it's in
436 : cpp_buffer. */
437 : unsigned char sysp;
438 :
439 : /* Number of the low-order location_t bits used for column numbers
440 : and ranges. */
441 : unsigned int m_column_and_range_bits : 8;
442 :
443 : /* Number of the low-order "column" bits used for storing short ranges
444 : inline, rather than in the ad-hoc table.
445 : MSB LSB
446 : 31 0
447 : +-------------------------+-------------------------------------------+
448 : | |<---map->column_and_range_bits (e.g. 12)-->|
449 : +-------------------------+-----------------------+-------------------+
450 : | | column_and_range_bits | map->range_bits |
451 : | | - range_bits | |
452 : +-------------------------+-----------------------+-------------------+
453 : | row bits | effective column bits | short range bits |
454 : | | (e.g. 7) | (e.g. 5) |
455 : +-------------------------+-----------------------+-------------------+ */
456 : unsigned int m_range_bits : 8;
457 :
458 : /* 32-bit int even in 64-bit mode. */
459 : linenum_type to_line;
460 :
461 : /* Location from whence this line map was included. For regular
462 : #includes, this location will be the last location of a map. For
463 : outermost file, this is 0. For modules it could be anywhere
464 : within a map. */
465 : location_t included_from;
466 :
467 : /* Pointer alignment boundary, whether 32-bit or 64-bit mode. */
468 : const char *to_file;
469 :
470 : /* Size is 28 (32) bytes for 32-bit (64-bit) arch. */
471 : };
472 :
473 : struct cpp_hashnode;
474 :
475 : /* A macro line map encodes location of tokens coming from a macro
476 : expansion.
477 :
478 : The offset from START_LOCATION is used to index into
479 : MACRO_LOCATIONS; this holds the original location of the token. */
480 : struct GTY((tag ("2"))) line_map_macro : public line_map {
481 :
482 : /* Get the location of the expansion point of this macro map. */
483 :
484 : location_t
485 10979 : get_expansion_point_location () const
486 : {
487 10979 : return m_expansion;
488 : }
489 :
490 : /* Base is 8 bytes. */
491 :
492 : /* The number of tokens inside the replacement-list of MACRO. */
493 : unsigned int n_tokens;
494 :
495 : /* Pointer alignment boundary. */
496 :
497 : /* The cpp macro whose expansion gave birth to this macro map. */
498 : struct cpp_hashnode *
499 : GTY ((nested_ptr (union tree_node,
500 : "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
501 : "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
502 : macro;
503 :
504 : /* This array of location is actually an array of pairs of
505 : locations. The elements inside it thus look like:
506 :
507 : x0,y0, x1,y1, x2,y2, ...., xn,yn.
508 :
509 : where n == n_tokens;
510 :
511 : Remember that these xI,yI are collected when libcpp is about to
512 : expand a given macro.
513 :
514 : yI is the location in the macro definition, either of the token
515 : itself or of a macro parameter that it replaces.
516 :
517 : Imagine this:
518 :
519 : #define PLUS(A, B) A + B <--- #1
520 :
521 : int a = PLUS (1,2); <--- #2
522 :
523 : There is a macro map for the expansion of PLUS in #2. PLUS is
524 : expanded into its expansion-list. The expansion-list is the
525 : replacement-list of PLUS where the macro parameters are replaced
526 : with their arguments. So the replacement-list of PLUS is made of
527 : the tokens:
528 :
529 : A, +, B
530 :
531 : and the expansion-list is made of the tokens:
532 :
533 : 1, +, 2
534 :
535 : Let's consider the case of token "+". Its y1 [yI for I == 1] is
536 : its spelling location in #1.
537 :
538 : y0 (thus for token "1") is the spelling location of A in #1.
539 :
540 : And y2 (of token "2") is the spelling location of B in #1.
541 :
542 : When the token is /not/ an argument for a macro, xI is the same
543 : location as yI. Otherwise, xI is the location of the token
544 : outside this macro expansion. If this macro was expanded from
545 : another macro expansion, xI is a virtual location representing
546 : the token in that macro expansion; otherwise, it is the spelling
547 : location of the token.
548 :
549 : Note that a virtual location is a location returned by
550 : linemap_add_macro_token. It encodes the relevant locations (x,y
551 : pairs) of that token across the macro expansions from which it
552 : (the token) might come from.
553 :
554 : In the example above x1 (for token "+") is going to be the same
555 : as y1. x0 is the spelling location for the argument token "1",
556 : and x2 is the spelling location for the argument token "2". */
557 : location_t * GTY((atomic)) macro_locations;
558 :
559 : /* This is the location of the expansion point of the current macro
560 : map. It's the location of the macro name. That location is held
561 : by the map that was current right before the current one. It
562 : could have been either a macro or an ordinary map, depending on
563 : if we are in a nested expansion context not. */
564 : location_t m_expansion;
565 :
566 : /* Size is one of the following:
567 : 32-bit system: 28 or 32 bytes, depending whether a uint64_t requires
568 : 4- or 8-byte alignment.
569 : 64-bit arch: 40 bytes. */
570 : };
571 :
572 : #if CHECKING_P && (GCC_VERSION >= 2007)
573 :
574 : /* Assertion macro to be used in line-map code. */
575 : #define linemap_assert(EXPR) \
576 : do { \
577 : if (! (EXPR)) \
578 : abort (); \
579 : } while (0)
580 :
581 : /* Assert that becomes a conditional expression when checking is disabled at
582 : compilation time. Use this for conditions that should not happen but if
583 : they happen, it is better to handle them gracefully rather than crash
584 : randomly later.
585 : Usage:
586 :
587 : if (linemap_assert_fails(EXPR)) handle_error(); */
588 : #define linemap_assert_fails(EXPR) __extension__ \
589 : ({linemap_assert (EXPR); false;})
590 :
591 : #else
592 : /* Include EXPR, so that unused variable warnings do not occur. */
593 : #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
594 : #define linemap_assert_fails(EXPR) (! (EXPR))
595 : #endif
596 :
597 : /* Get whether location LOC is an ordinary location. */
598 :
599 : inline bool
600 : IS_ORDINARY_LOC (location_t loc)
601 : {
602 : return loc < LINE_MAP_MAX_LOCATION;
603 : }
604 :
605 : /* Get whether location LOC is an ad-hoc location. */
606 :
607 : inline bool
608 53626 : IS_ADHOC_LOC (location_t loc)
609 : {
610 53626 : return loc > MAX_LOCATION_T;
611 : }
612 :
613 : /* Categorize line map kinds. */
614 :
615 : inline bool
616 55071878 : MAP_ORDINARY_P (const line_map *map)
617 : {
618 55071878 : return IS_ORDINARY_LOC (map->start_location);
619 : }
620 :
621 : /* Return TRUE if MAP encodes locations coming from a macro
622 : replacement-list at macro expansion point. */
623 : bool
624 : linemap_macro_expansion_map_p (const line_map *);
625 :
626 : /* Assert that MAP encodes locations of tokens that are not part of
627 : the replacement-list of a macro expansion, downcasting from
628 : line_map * to line_map_ordinary *. */
629 :
630 : inline line_map_ordinary *
631 : linemap_check_ordinary (line_map *map)
632 : {
633 : linemap_assert (MAP_ORDINARY_P (map));
634 : return (line_map_ordinary *)map;
635 : }
636 :
637 : /* Assert that MAP encodes locations of tokens that are not part of
638 : the replacement-list of a macro expansion, downcasting from
639 : const line_map * to const line_map_ordinary *. */
640 :
641 : inline const line_map_ordinary *
642 40907448 : linemap_check_ordinary (const line_map *map)
643 : {
644 40907448 : linemap_assert (MAP_ORDINARY_P (map));
645 40907448 : return (const line_map_ordinary *)map;
646 : }
647 :
648 : /* Assert that MAP is a macro expansion and downcast to the appropriate
649 : subclass. */
650 :
651 4341 : inline line_map_macro *linemap_check_macro (line_map *map)
652 : {
653 4341 : linemap_assert (!MAP_ORDINARY_P (map));
654 4341 : return (line_map_macro *)map;
655 : }
656 :
657 : /* Assert that MAP is a macro expansion and downcast to the appropriate
658 : subclass. */
659 :
660 : inline const line_map_macro *
661 1181936 : linemap_check_macro (const line_map *map)
662 : {
663 1181936 : linemap_assert (!MAP_ORDINARY_P (map));
664 1181936 : return (const line_map_macro *)map;
665 : }
666 :
667 : /* Read the start location of MAP. */
668 :
669 : inline location_t
670 243262905 : MAP_START_LOCATION (const line_map *map)
671 : {
672 214379782 : return map->start_location;
673 : }
674 :
675 : /* Get the starting line number of ordinary map MAP. */
676 :
677 : inline linenum_type
678 26254 : ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map_ordinary *ord_map)
679 : {
680 26254 : return ord_map->to_line;
681 : }
682 :
683 : /* Return a positive value if map encodes locations from a system
684 : header, 0 otherwise. Returns 1 if ordinary map MAP encodes locations
685 : in a system header and 2 if it encodes locations in a C system header
686 : that therefore needs to be extern "C" protected in C++. */
687 :
688 : inline unsigned char
689 : ORDINARY_MAP_IN_SYSTEM_HEADER_P (const line_map_ordinary *ord_map)
690 : {
691 : return ord_map->sysp;
692 : }
693 :
694 : /* TRUE if this line map is for a module (not a source file). */
695 :
696 : inline bool
697 12978153 : MAP_MODULE_P (const line_map *map)
698 : {
699 12978153 : return (MAP_ORDINARY_P (map)
700 12978153 : && linemap_check_ordinary (map)->reason == LC_MODULE);
701 : }
702 :
703 : /* Get the filename of ordinary map MAP. */
704 :
705 : inline const char *
706 10389230 : ORDINARY_MAP_FILE_NAME (const line_map_ordinary *ord_map)
707 : {
708 10389230 : return ord_map->to_file;
709 : }
710 :
711 : /* Get the cpp macro whose expansion gave birth to macro map MAP. */
712 :
713 : inline cpp_hashnode *
714 : MACRO_MAP_MACRO (const line_map_macro *macro_map)
715 : {
716 : return macro_map->macro;
717 : }
718 :
719 : /* Get the number of tokens inside the replacement-list of the macro
720 : that led to macro map MAP. */
721 :
722 : inline unsigned int
723 8 : MACRO_MAP_NUM_MACRO_TOKENS (const line_map_macro *macro_map)
724 : {
725 8 : return macro_map->n_tokens;
726 : }
727 :
728 : /* Get the array of pairs of locations within macro map MAP.
729 : See the declaration of line_map_macro for more information. */
730 :
731 : inline location_t *
732 2 : MACRO_MAP_LOCATIONS (const line_map_macro *macro_map)
733 : {
734 2 : return macro_map->macro_locations;
735 : }
736 :
737 : /* The abstraction of a set of location maps. There can be several
738 : types of location maps. This abstraction contains the attributes
739 : that are independent from the type of the map.
740 :
741 : Essentially this is just a vector of T_linemap_subclass,
742 : which can only ever grow in size. */
743 :
744 : struct GTY(()) maps_info_ordinary {
745 : /* This array contains the "ordinary" line maps, for all
746 : events other than macro expansion
747 : (e.g. when a new preprocessing unit starts or ends). */
748 : line_map_ordinary * GTY ((length ("%h.used"))) maps;
749 :
750 : /* The total number of allocated maps. */
751 : line_map_uint_t allocated;
752 :
753 : /* The number of elements used in maps. This number is smaller
754 : or equal to ALLOCATED. */
755 : line_map_uint_t used;
756 :
757 : /* The index of the last ordinary map that was looked up with
758 : linemap_lookup. */
759 : mutable line_map_uint_t m_cache;
760 : };
761 :
762 : struct GTY(()) maps_info_macro {
763 : /* This array contains the macro line maps.
764 : A macro line map is created whenever a macro expansion occurs. */
765 : line_map_macro * GTY ((length ("%h.used"))) maps;
766 :
767 : /* The total number of allocated maps. */
768 : line_map_uint_t allocated;
769 :
770 : /* The number of elements used in maps. This number is smaller
771 : or equal to ALLOCATED. */
772 : line_map_uint_t used;
773 :
774 : /* The index of the last macro map that was looked up with
775 : linemap_lookup. */
776 : mutable line_map_uint_t m_cache;
777 : };
778 :
779 : /* Data structure to associate a source_range together with an arbitrary
780 : data pointer with a source location. */
781 : struct GTY(()) location_adhoc_data {
782 : location_t locus;
783 : source_range src_range;
784 : void * GTY((skip)) data;
785 : unsigned discriminator;
786 : };
787 :
788 : struct htab;
789 :
790 : /* The following data structure encodes a location with some adhoc data
791 : and maps it to a new unsigned integer (called an adhoc location)
792 : that replaces the original location to represent the mapping.
793 :
794 : The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
795 : highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
796 : the original location. Once identified as the adhoc_loc, the lower 62
797 : bits of the integer is used to index the location_adhoc_data array,
798 : in which the locus and associated data is stored. */
799 :
800 : struct GTY(()) location_adhoc_data_map {
801 : struct htab * GTY((skip)) htab;
802 : location_t curr_loc;
803 : line_map_uint_t allocated;
804 : struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
805 : };
806 :
807 : /* A set of chronological line_map structures. */
808 : class GTY(()) line_maps {
809 : public:
810 :
811 : ~line_maps ();
812 :
813 : bool pure_location_p (location_t loc) const;
814 : location_t get_pure_location (location_t loc) const;
815 :
816 : source_range get_range_from_loc (location_t loc) const;
817 2756 : location_t get_start (location_t loc) const
818 : {
819 2756 : return get_range_from_loc (loc).m_start;
820 : }
821 : location_t
822 2756 : get_finish (location_t loc) const
823 : {
824 2756 : return get_range_from_loc (loc).m_finish;
825 : }
826 :
827 : location_t make_location (location_t caret,
828 : location_t start,
829 : location_t finish);
830 :
831 : location_t
832 : get_or_create_combined_loc (location_t locus,
833 : source_range src_range,
834 : void *data,
835 : unsigned discriminator);
836 :
837 : private:
838 : bool can_be_stored_compactly_p (location_t locus,
839 : source_range src_range,
840 : void *data,
841 : unsigned discriminator) const;
842 : source_range get_range_from_adhoc_loc (location_t loc) const;
843 :
844 : public:
845 : maps_info_ordinary info_ordinary;
846 :
847 : maps_info_macro info_macro;
848 :
849 : /* Depth of the include stack, including the current file. */
850 : unsigned int depth;
851 :
852 : /* If true, prints an include trace a la -H. */
853 : bool trace_includes;
854 :
855 : /* True if we've seen a #line or # 44 "file" directive. */
856 : bool seen_line_directive;
857 :
858 : /* Highest location_t "given out". */
859 : location_t highest_location;
860 :
861 : /* Start of line of highest location_t "given out". */
862 : location_t highest_line;
863 :
864 : /* The maximum column number we can quickly allocate. Higher numbers
865 : may require allocating a new line_map. */
866 : unsigned int max_column_hint;
867 :
868 : /* The allocator to use when resizing 'maps', defaults to xrealloc. */
869 : line_map_realloc GTY((callback)) m_reallocator;
870 :
871 : /* The allocators' function used to know the actual size it
872 : allocated, for a certain allocation size requested. */
873 : line_map_round_alloc_size_func GTY((callback)) m_round_alloc_size;
874 :
875 : struct location_adhoc_data_map m_location_adhoc_data_map;
876 :
877 : /* The special location value that is used as spelling location for
878 : built-in tokens. */
879 : location_t builtin_location;
880 :
881 : /* The special location value to be used for tokens originating on the
882 : command line. This is currently only needed by the C-family front ends
883 : for PCH support; if it would be used for another purpose in the future,
884 : then other libcpp-using front ends may need to set it as well. */
885 : location_t cmdline_location;
886 :
887 : /* The default value of range_bits in ordinary line maps. */
888 : unsigned int default_range_bits;
889 :
890 : line_map_uint_t m_num_optimized_ranges;
891 : line_map_uint_t m_num_unoptimized_ranges;
892 : };
893 :
894 : /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
895 : if we are interested in macro maps, FALSE otherwise. */
896 : inline line_map_uint_t
897 : LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind)
898 : {
899 : if (map_kind)
900 : return set->info_macro.allocated;
901 : else
902 : return set->info_ordinary.allocated;
903 : }
904 :
905 : /* As above, but by reference (e.g. as an lvalue). */
906 :
907 : inline line_map_uint_t &
908 : LINEMAPS_ALLOCATED (line_maps *set, bool map_kind)
909 : {
910 : if (map_kind)
911 : return set->info_macro.allocated;
912 : else
913 : return set->info_ordinary.allocated;
914 : }
915 :
916 : /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
917 : we are interested in macro maps, FALSE otherwise.*/
918 : inline line_map_uint_t
919 58008161 : LINEMAPS_USED (const line_maps *set, bool map_kind)
920 : {
921 58008161 : if (map_kind)
922 28883123 : return set->info_macro.used;
923 : else
924 123062 : return set->info_ordinary.used;
925 : }
926 :
927 : /* As above, but by reference (e.g. as an lvalue). */
928 :
929 : inline line_map_uint_t &
930 : LINEMAPS_USED (line_maps *set, bool map_kind)
931 : {
932 : if (map_kind)
933 : return set->info_macro.used;
934 : else
935 : return set->info_ordinary.used;
936 : }
937 :
938 : /* Return the map at a given index. */
939 : inline line_map *
940 29081242 : LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, line_map_uint_t index)
941 : {
942 29081242 : if (map_kind)
943 28883123 : return &set->info_macro.maps[index];
944 : else
945 110513 : return &set->info_ordinary.maps[index];
946 : }
947 :
948 : /* Returns the last map used in the line table SET. MAP_KIND
949 : shall be TRUE if we are interested in macro maps, FALSE
950 : otherwise.*/
951 : inline line_map *
952 28981087 : LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind)
953 : {
954 57962174 : linemap_assert (LINEMAPS_USED (set, map_kind));
955 57962174 : return LINEMAPS_MAP_AT (set, map_kind,
956 28981087 : LINEMAPS_USED (set, map_kind) - 1);
957 : }
958 :
959 : /* Returns the INDEXth ordinary map. */
960 : inline line_map_ordinary *
961 100153 : LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, line_map_uint_t index)
962 : {
963 100153 : linemap_assert (index < LINEMAPS_USED (set, false));
964 100153 : return (line_map_ordinary *)LINEMAPS_MAP_AT (set, false, index);
965 : }
966 :
967 : /* Return the number of ordinary maps allocated in the line table
968 : SET. */
969 : inline line_map_uint_t
970 : LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set)
971 : {
972 : return LINEMAPS_ALLOCATED (set, false);
973 : }
974 :
975 : /* Return the number of ordinary maps used in the line table SET. */
976 : inline line_map_uint_t
977 13442 : LINEMAPS_ORDINARY_USED (const line_maps *set)
978 : {
979 13442 : return LINEMAPS_USED (set, false);
980 : }
981 :
982 : /* Returns a pointer to the last ordinary map used in the line table
983 : SET. */
984 : inline line_map_ordinary *
985 97964 : LINEMAPS_LAST_ORDINARY_MAP (const line_maps *set)
986 : {
987 97964 : return (line_map_ordinary *)LINEMAPS_LAST_MAP (set, false);
988 : }
989 :
990 : /* Returns the INDEXth macro map. */
991 : inline line_map_macro *
992 2 : LINEMAPS_MACRO_MAP_AT (const line_maps *set, line_map_uint_t index)
993 : {
994 2 : linemap_assert (index < LINEMAPS_USED (set, true));
995 2 : return (line_map_macro *)LINEMAPS_MAP_AT (set, true, index);
996 : }
997 :
998 : /* Returns the number of macro maps that were allocated in the line
999 : table SET. */
1000 : inline line_map_uint_t
1001 : LINEMAPS_MACRO_ALLOCATED (const line_maps *set)
1002 : {
1003 : return LINEMAPS_ALLOCATED (set, true);
1004 : }
1005 :
1006 : /* Returns the number of macro maps used in the line table SET. */
1007 : inline line_map_uint_t
1008 28913477 : LINEMAPS_MACRO_USED (const line_maps *set)
1009 : {
1010 28871902 : return LINEMAPS_USED (set, true);
1011 : }
1012 :
1013 : /* Returns the last macro map used in the line table SET. */
1014 : inline line_map_macro *
1015 28883123 : LINEMAPS_LAST_MACRO_MAP (const line_maps *set)
1016 : {
1017 28883123 : return (line_map_macro *)LINEMAPS_LAST_MAP (set, true);
1018 : }
1019 :
1020 : /* Returns the lowest location [of a token resulting from macro
1021 : expansion] encoded in this line table. */
1022 : inline location_t
1023 28913474 : LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps *set)
1024 : {
1025 28913474 : return LINEMAPS_MACRO_USED (set)
1026 57796597 : ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))
1027 28913474 : : MAX_LOCATION_T + 1;
1028 : }
1029 :
1030 : extern void *get_data_from_adhoc_loc (const line_maps *, location_t);
1031 : extern unsigned get_discriminator_from_adhoc_loc (const line_maps *, location_t);
1032 : extern location_t get_location_from_adhoc_loc (const line_maps *,
1033 : location_t);
1034 :
1035 : extern source_range get_range_from_loc (const line_maps *set, location_t loc);
1036 : extern unsigned get_discriminator_from_loc (const line_maps *set, location_t loc);
1037 :
1038 : /* Get whether location LOC is a "pure" location, or
1039 : whether it is an ad-hoc location, or embeds range information. */
1040 :
1041 : bool
1042 : pure_location_p (const line_maps *set, location_t loc);
1043 :
1044 : /* Given location LOC within SET, strip away any packed range information
1045 : or ad-hoc information. */
1046 :
1047 : extern location_t get_pure_location (const line_maps *set, location_t loc);
1048 :
1049 : extern void rebuild_location_adhoc_htab (class line_maps *);
1050 :
1051 : /* Initialize a line map set. SET is the line map set to initialize
1052 : and BUILTIN_LOCATION is the special location value to be used as
1053 : spelling location for built-in tokens. This BUILTIN_LOCATION has
1054 : to be strictly less than RESERVED_LOCATION_COUNT. */
1055 : extern void linemap_init (class line_maps *set,
1056 : location_t builtin_location);
1057 :
1058 : /* Check for and warn about line_maps entered but not exited. */
1059 :
1060 : extern void linemap_check_files_exited (const line_maps *);
1061 :
1062 : /* Return a location_t for the start (i.e. column==0) of
1063 : (physical) line TO_LINE in the current source file (as in the
1064 : most recent linemap_add). MAX_COLUMN_HINT is the highest column
1065 : number we expect to use in this line (but it does not change
1066 : the highest_location). */
1067 :
1068 : extern location_t linemap_line_start
1069 : (class line_maps *set, linenum_type to_line, unsigned int max_column_hint);
1070 :
1071 : /* Allocate a raw block of line maps, zero initialized. */
1072 : extern line_map *line_map_new_raw (line_maps *, bool, line_map_uint_t);
1073 :
1074 : /* Add a mapping of logical source line to physical source file and
1075 : line number. This function creates an "ordinary map", which is a
1076 : map that records locations of tokens that are not part of macro
1077 : replacement-lists present at a macro expansion point.
1078 :
1079 : The text pointed to by TO_FILE must have a lifetime
1080 : at least as long as the lifetime of SET. An empty
1081 : TO_FILE means standard input. If reason is LC_LEAVE, and
1082 : TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
1083 : natural values considering the file we are returning to.
1084 :
1085 : A call to this function can relocate the previous set of
1086 : maps, so any stored line_map pointers should not be used. */
1087 : extern const line_map *linemap_add
1088 : (class line_maps *, enum lc_reason, unsigned int sysp,
1089 : const char *to_file, linenum_type to_line);
1090 :
1091 : /* Create a macro map. A macro map encodes source locations of tokens
1092 : that are part of a macro replacement-list, at a macro expansion
1093 : point. See the extensive comments of struct line_map and struct
1094 : line_map_macro, in line-map.h.
1095 :
1096 : This map shall be created when the macro is expanded. The map
1097 : encodes the source location of the expansion point of the macro as
1098 : well as the "original" source location of each token that is part
1099 : of the macro replacement-list. If a macro is defined but never
1100 : expanded, it has no macro map. SET is the set of maps the macro
1101 : map should be part of. MACRO_NODE is the macro which the new macro
1102 : map should encode source locations for. EXPANSION is the location
1103 : of the expansion point of MACRO. For function-like macros
1104 : invocations, it's best to make it point to the closing parenthesis
1105 : of the macro, rather than the the location of the first character
1106 : of the macro. NUM_TOKENS is the number of tokens that are part of
1107 : the replacement-list of MACRO. */
1108 : const line_map_macro *linemap_enter_macro (line_maps *, cpp_hashnode *,
1109 : location_t, unsigned int);
1110 :
1111 : /* Create a source location for a module. The creator must either do
1112 : this after the TU is tokenized, or deal with saving and restoring
1113 : map state. */
1114 :
1115 : extern location_t linemap_module_loc
1116 : (line_maps *, location_t from, const char *name);
1117 : extern void linemap_module_reparent
1118 : (line_maps *, location_t loc, location_t new_parent);
1119 :
1120 : /* TRUE iff the location comes from a module import. */
1121 : extern bool linemap_location_from_module_p
1122 : (const line_maps *, location_t);
1123 :
1124 : /* Restore the linemap state such that the map at LWM-1 continues.
1125 : Return start location of the new map. */
1126 : extern location_t linemap_module_restore
1127 : (line_maps *, line_map_uint_t lwm);
1128 :
1129 : /* Given a logical source location, returns the map which the
1130 : corresponding (source file, line, column) triplet can be deduced
1131 : from. Since the set is built chronologically, the logical lines are
1132 : monotonic increasing, and so the list is sorted and we can use a
1133 : binary search. If no line map have been allocated yet, this
1134 : function returns NULL. */
1135 : extern const line_map *linemap_lookup
1136 : (const line_maps *, location_t);
1137 :
1138 : line_map_uint_t linemap_lookup_macro_index (const line_maps *, location_t);
1139 :
1140 : /* Returns TRUE if the line table set tracks token locations across
1141 : macro expansion, FALSE otherwise. */
1142 : bool linemap_tracks_macro_expansion_locs_p (const line_maps *);
1143 :
1144 : /* Return the name of the macro associated to MACRO_MAP. */
1145 : const char* linemap_map_get_macro_name (const line_map_macro *);
1146 :
1147 : /* Return a positive value if LOCATION is the locus of a token that is
1148 : located in a system header, O otherwise. It returns 1 if LOCATION
1149 : is the locus of a token that is located in a system header, and 2
1150 : if LOCATION is the locus of a token located in a C system header
1151 : that therefore needs to be extern "C" protected in C++.
1152 :
1153 : Note that this function returns 1 if LOCATION belongs to a token
1154 : that is part of a macro replacement-list defined in a system
1155 : header, but expanded in a non-system file. */
1156 : int linemap_location_in_system_header_p (const line_maps *,
1157 : location_t);
1158 :
1159 : /* Return TRUE if LOCATION is a source code location of a token that is part of
1160 : a macro expansion, FALSE otherwise. */
1161 : bool linemap_location_from_macro_expansion_p (const line_maps *,
1162 : location_t);
1163 :
1164 : /* TRUE if LOCATION is a source code location of a token that is part of the
1165 : definition of a macro, FALSE otherwise. */
1166 : bool linemap_location_from_macro_definition_p (const line_maps *,
1167 : location_t);
1168 :
1169 : /* With the precondition that LOCATION is the locus of a token that is
1170 : an argument of a function-like macro MACRO_MAP and appears in the
1171 : expansion of MACRO_MAP, return the locus of that argument in the
1172 : context of the caller of MACRO_MAP. */
1173 :
1174 : extern location_t
1175 : linemap_macro_map_loc_unwind_toward_spelling (const line_maps *set,
1176 : const line_map_macro *macro_map,
1177 : location_t location);
1178 :
1179 : /* location_t values from 0 to RESERVED_LOCATION_COUNT-1 will
1180 : be reserved for libcpp user as special values, no token from libcpp
1181 : will contain any of those locations. */
1182 : const location_t RESERVED_LOCATION_COUNT = 2;
1183 :
1184 : /* Converts a map and a location_t to source line. */
1185 : inline linenum_type
1186 10393900 : SOURCE_LINE (const line_map_ordinary *ord_map, location_t loc)
1187 : {
1188 10393900 : return ((loc - ord_map->start_location)
1189 10393900 : >> ord_map->m_column_and_range_bits) + ord_map->to_line;
1190 : }
1191 :
1192 : /* Convert a map and location_t to source column number. */
1193 : inline linenum_type
1194 1124 : SOURCE_COLUMN (const line_map_ordinary *ord_map, location_t loc)
1195 : {
1196 1124 : return ((loc - ord_map->start_location)
1197 1124 : & ((location_t (1) << ord_map->m_column_and_range_bits) - 1))
1198 1124 : >> ord_map->m_range_bits;
1199 : }
1200 :
1201 :
1202 : inline location_t
1203 127493 : linemap_included_from (const line_map_ordinary *ord_map)
1204 : {
1205 127493 : return ord_map->included_from;
1206 : }
1207 :
1208 : /* The linemap containing the included-from location of MAP. */
1209 : const line_map_ordinary *
1210 : linemap_included_from_linemap (const line_maps *set,
1211 : const line_map_ordinary *map);
1212 :
1213 : /* True if the map is at the bottom of the include stack. */
1214 :
1215 : inline bool
1216 19162831 : MAIN_FILE_P (const line_map_ordinary *ord_map)
1217 : {
1218 19162831 : return ord_map->included_from == 0;
1219 : }
1220 :
1221 : /* Encode and return a location_t from a column number. The
1222 : source line considered is the last source line used to call
1223 : linemap_line_start, i.e, the last source line which a location was
1224 : encoded from. */
1225 : extern location_t
1226 : linemap_position_for_column (class line_maps *, unsigned int);
1227 :
1228 : /* Encode and return a source location from a given line and
1229 : column. */
1230 : location_t
1231 : linemap_position_for_line_and_column (line_maps *set,
1232 : const line_map_ordinary *,
1233 : linenum_type, unsigned int);
1234 :
1235 : /* Encode and return a location_t starting from location LOC and
1236 : shifting it by OFFSET columns. This function does not support
1237 : virtual locations. */
1238 : location_t
1239 : linemap_position_for_loc_and_offset (class line_maps *set,
1240 : location_t loc,
1241 : unsigned int offset);
1242 :
1243 : /* Return the file this map is for. */
1244 : inline const char *
1245 29117897 : LINEMAP_FILE (const line_map_ordinary *ord_map)
1246 : {
1247 19701482 : return ord_map->to_file;
1248 : }
1249 :
1250 : /* Return the line number this map started encoding location from. */
1251 : inline linenum_type
1252 9416412 : LINEMAP_LINE (const line_map_ordinary *ord_map)
1253 : {
1254 9416412 : return ord_map->to_line;
1255 : }
1256 :
1257 : /* Return a positive value if map encodes locations from a system
1258 : header, 0 otherwise. Returns 1 if MAP encodes locations in a
1259 : system header and 2 if it encodes locations in a C system header
1260 : that therefore needs to be extern "C" protected in C++. */
1261 : inline unsigned char
1262 21820 : LINEMAP_SYSP (const line_map_ordinary *ord_map)
1263 : {
1264 21820 : return ord_map->sysp;
1265 : }
1266 :
1267 : const struct line_map *first_map_in_common (const line_maps *set,
1268 : location_t loc0,
1269 : location_t loc1,
1270 : location_t *res_loc0,
1271 : location_t *res_loc1);
1272 :
1273 : /* Return a positive value if PRE denotes the location of a token that
1274 : comes before the token of POST, 0 if PRE denotes the location of
1275 : the same token as the token for POST, and a negative value
1276 : otherwise. */
1277 : int
1278 : linemap_compare_locations (const line_maps *set,
1279 : location_t pre,
1280 : location_t post);
1281 :
1282 : /* Return TRUE if LOC_A denotes the location a token that comes
1283 : topogically before the token denoted by location LOC_B, or if they
1284 : are equal. */
1285 : inline bool
1286 722670432 : linemap_location_before_p (const line_maps *set,
1287 : location_t loc_a,
1288 : location_t loc_b)
1289 : {
1290 722670432 : return linemap_compare_locations (set, loc_a, loc_b) >= 0;
1291 : }
1292 :
1293 : struct expanded_location
1294 : {
1295 : /* The name of the source file involved. */
1296 : const char *file;
1297 :
1298 : /* The line-location in the source file. */
1299 : int line;
1300 :
1301 : int column;
1302 :
1303 : void *data;
1304 :
1305 : /* In a system header?. */
1306 : bool sysp;
1307 : };
1308 :
1309 : extern bool
1310 : operator== (const expanded_location &a,
1311 : const expanded_location &b);
1312 : inline bool
1313 106 : operator!= (const expanded_location &a,
1314 : const expanded_location &b)
1315 : {
1316 106 : return !(a == b);
1317 : }
1318 :
1319 :
1320 : /* This is enum is used by the function linemap_resolve_location
1321 : below. The meaning of the values is explained in the comment of
1322 : that function. */
1323 : enum location_resolution_kind
1324 : {
1325 : LRK_MACRO_EXPANSION_POINT,
1326 : LRK_SPELLING_LOCATION,
1327 : LRK_MACRO_DEFINITION_LOCATION
1328 : };
1329 :
1330 : /* Resolve a virtual location into either a spelling location, an
1331 : expansion point location or a token argument replacement point
1332 : location. Return the map that encodes the virtual location as well
1333 : as the resolved location.
1334 :
1335 : If LOC is *NOT* the location of a token resulting from the
1336 : expansion of a macro, then the parameter LRK (which stands for
1337 : Location Resolution Kind) is ignored and the resulting location
1338 : just equals the one given in argument.
1339 :
1340 : Now if LOC *IS* the location of a token resulting from the
1341 : expansion of a macro, this is what happens.
1342 :
1343 : * If LRK is set to LRK_MACRO_EXPANSION_POINT
1344 : -------------------------------
1345 :
1346 : The virtual location is resolved to the first macro expansion point
1347 : that led to this macro expansion.
1348 :
1349 : * If LRK is set to LRK_SPELLING_LOCATION
1350 : -------------------------------------
1351 :
1352 : The virtual location is resolved to the locus where the token has
1353 : been spelled in the source. This can follow through all the macro
1354 : expansions that led to the token.
1355 :
1356 : * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1357 : --------------------------------------
1358 :
1359 : The virtual location is resolved to the locus of the token in the
1360 : context of the macro definition.
1361 :
1362 : If LOC is the locus of a token that is an argument of a
1363 : function-like macro [replacing a parameter in the replacement list
1364 : of the macro] the virtual location is resolved to the locus of the
1365 : parameter that is replaced, in the context of the definition of the
1366 : macro.
1367 :
1368 : If LOC is the locus of a token that is not an argument of a
1369 : function-like macro, then the function behaves as if LRK was set to
1370 : LRK_SPELLING_LOCATION.
1371 :
1372 : If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1373 : returned location. Note that if the returned location wasn't originally
1374 : encoded by a map, the *MAP is set to NULL. This can happen if LOC
1375 : resolves to a location reserved for the client code, like
1376 : UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1377 :
1378 : location_t linemap_resolve_location (const line_maps *,
1379 : location_t loc,
1380 : enum location_resolution_kind lrk,
1381 : const line_map_ordinary **loc_map);
1382 :
1383 : /* Suppose that LOC is the virtual location of a token coming from the
1384 : expansion of a macro M. This function then steps up to get the
1385 : location L of the point where M got expanded. If L is a spelling
1386 : location inside a macro expansion M', then this function returns
1387 : the point where M' was expanded. LOC_MAP is an output parameter.
1388 : When non-NULL, *LOC_MAP is set to the map of the returned
1389 : location. */
1390 : location_t linemap_unwind_toward_expansion (const line_maps *,
1391 : location_t loc,
1392 : const line_map **loc_map);
1393 :
1394 : /* If LOC is the virtual location of a token coming from the expansion
1395 : of a macro M and if its spelling location is reserved (e.g, a
1396 : location for a built-in token), then this function unwinds (using
1397 : linemap_unwind_toward_expansion) the location until a location that
1398 : is not reserved and is not in a system header is reached. In other
1399 : words, this unwinds the reserved location until a location that is
1400 : in real source code is reached.
1401 :
1402 : Otherwise, if the spelling location for LOC is not reserved or if
1403 : LOC doesn't come from the expansion of a macro, the function
1404 : returns LOC as is and *MAP is not touched.
1405 :
1406 : *MAP is set to the map of the returned location if the later is
1407 : different from LOC. */
1408 : location_t linemap_unwind_to_first_non_reserved_loc (const line_maps *,
1409 : location_t loc,
1410 : const line_map **map);
1411 :
1412 : /* Expand source code location LOC and return a user readable source
1413 : code location. LOC must be a spelling (non-virtual) location. If
1414 : it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1415 : location is returned. */
1416 : expanded_location linemap_expand_location (const line_maps *,
1417 : const line_map *,
1418 : location_t loc);
1419 :
1420 : /* Statistics about maps allocation and usage as returned by
1421 : linemap_get_statistics. */
1422 : struct linemap_stats
1423 : {
1424 : long num_ordinary_maps_allocated;
1425 : long num_ordinary_maps_used;
1426 : long ordinary_maps_allocated_size;
1427 : long ordinary_maps_used_size;
1428 : long num_expanded_macros;
1429 : long num_macro_tokens;
1430 : long num_macro_maps_used;
1431 : long macro_maps_allocated_size;
1432 : long macro_maps_used_size;
1433 : long macro_maps_locations_size;
1434 : long duplicated_macro_maps_locations_size;
1435 : long adhoc_table_size;
1436 : long adhoc_table_entries_used;
1437 : };
1438 :
1439 : /* Return the highest location emitted for a given file for which
1440 : there is a line map in SET. FILE_NAME is the file name to
1441 : consider. If the function returns TRUE, *LOC is set to the highest
1442 : location emitted for that file. */
1443 : bool linemap_get_file_highest_location (const line_maps * set,
1444 : const char *file_name,
1445 : location_t *loc);
1446 :
1447 : /* Compute and return statistics about the memory consumption of some
1448 : parts of the line table SET. */
1449 : void linemap_get_statistics (const line_maps *, struct linemap_stats *);
1450 :
1451 : /* Dump debugging information about source location LOC into the file
1452 : stream STREAM. SET is the line map set LOC comes from. */
1453 : void linemap_dump_location (const line_maps *, location_t, FILE *);
1454 :
1455 : /* Dump line map at index IX in line table SET to STREAM. If STREAM
1456 : is NULL, use stderr. IS_MACRO is true if the caller wants to
1457 : dump a macro map, false otherwise. */
1458 : void linemap_dump (FILE *, const line_maps *, line_map_uint_t, bool);
1459 :
1460 : /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1461 : NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1462 : specifies how many macro maps to dump. */
1463 : void line_table_dump (FILE *, const line_maps *,
1464 : line_map_uint_t, line_map_uint_t);
1465 :
1466 : /* An enum for distinguishing the various parts within a location_t. */
1467 :
1468 : enum class location_aspect
1469 : {
1470 : caret,
1471 : start,
1472 : finish
1473 : };
1474 :
1475 : /* The rich_location class requires a way to expand location_t instances.
1476 : We would directly use expand_location_to_spelling_point, which is
1477 : implemented in gcc/input.cc, but we also need to use it for rich_location
1478 : within genmatch.cc.
1479 : Hence we require client code of libcpp to implement the following
1480 : symbol. */
1481 : extern expanded_location
1482 : linemap_client_expand_location_to_spelling_point (const line_maps *,
1483 : location_t,
1484 : enum location_aspect);
1485 :
1486 : #endif /* !LIBCPP_LINE_MAP_H */
|