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