]> 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-2019 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 /* Both gcc and emacs number source *lines* starting at 1, but
30 they have differing conventions for *columns*.
31
32 GCC uses a 1-based convention for source columns,
33 whereas Emacs's M-x column-number-mode uses a 0-based convention.
34
35 For example, an error in the initial, left-hand
36 column of source line 3 is reported by GCC as:
37
38 some-file.c:3:1: error: ...etc...
39
40 On navigating to the location of that error in Emacs
41 (e.g. via "next-error"),
42 the locus is reported in the Mode Line
43 (assuming M-x column-number-mode) as:
44
45 some-file.c 10% (3, 0)
46
47 i.e. "3:1:" in GCC corresponds to "(3, 0)" in Emacs. */
48
49 /* The type of line numbers. */
50 typedef unsigned int linenum_type;
51
52 /* A function for for use by qsort for comparing line numbers. */
53
54 inline int compare (linenum_type lhs, linenum_type rhs)
55 {
56 /* Avoid truncation issues by using long long for the comparison,
57 and only consider the sign of the result. */
58 long long diff = (long long)lhs - (long long)rhs;
59 if (diff)
60 return diff > 0 ? 1 : -1;
61 return 0;
62 }
63
64 /* Reason for creating a new line map with linemap_add. */
65 enum lc_reason
66 {
67 LC_ENTER = 0, /* Begin #include. */
68 LC_LEAVE, /* Return to including file. */
69 LC_RENAME, /* Other reason for name change. */
70 LC_RENAME_VERBATIM, /* Likewise, but "" != stdin. */
71 LC_ENTER_MACRO, /* Begin macro expansion. */
72 /* FIXME: add support for stringize and paste. */
73 LC_HWM /* High Water Mark. */
74 };
75
76 /* The typedef "location_t" is a key within the location database,
77 identifying a source location or macro expansion, along with range
78 information, and (optionally) a pointer for use by gcc.
79
80 This key only has meaning in relation to a line_maps instance. Within
81 gcc there is a single line_maps instance: "line_table", declared in
82 gcc/input.h and defined in gcc/input.c.
83
84 The values of the keys are intended to be internal to libcpp,
85 but for ease-of-understanding the implementation, they are currently
86 assigned as follows:
87
88 Actual | Value | Meaning
89 -----------+-------------------------------+-------------------------------
90 0x00000000 | UNKNOWN_LOCATION (gcc/input.h)| Unknown/invalid location.
91 -----------+-------------------------------+-------------------------------
92 0x00000001 | BUILTINS_LOCATION | The location for declarations
93 | (gcc/input.h) | in "<built-in>"
94 -----------+-------------------------------+-------------------------------
95 0x00000002 | RESERVED_LOCATION_COUNT | The first location to be
96 | (also | handed out, and the
97 | ordmap[0]->start_location) | first line in ordmap 0
98 -----------+-------------------------------+-------------------------------
99 | ordmap[1]->start_location | First line in ordmap 1
100 | ordmap[1]->start_location+32 | First column in that line
101 | (assuming range_bits == 5) |
102 | ordmap[1]->start_location+64 | 2nd column in that line
103 | ordmap[1]->start_location+4096| Second line in ordmap 1
104 | (assuming column_bits == 12)
105 |
106 | Subsequent lines are offset by (1 << column_bits),
107 | e.g. 4096 for 12 bits, with a column value of 0 representing
108 | "the whole line".
109 |
110 | Within a line, the low "range_bits" (typically 5) are used for
111 | storing short ranges, so that there's an offset of
112 | (1 << range_bits) between individual columns within a line,
113 | typically 32.
114 | The low range_bits store the offset of the end point from the
115 | start point, and the start point is found by masking away
116 | the range bits.
117 |
118 | For example:
119 | ordmap[1]->start_location+64 "2nd column in that line"
120 | above means a caret at that location, with a range
121 | starting and finishing at the same place (the range bits
122 | are 0), a range of length 1.
123 |
124 | By contrast:
125 | ordmap[1]->start_location+68
126 | has range bits 0x4, meaning a caret with a range starting at
127 | that location, but with endpoint 4 columns further on: a range
128 | of length 5.
129 |
130 | Ranges that have caret != start, or have an endpoint too
131 | far away to fit in range_bits are instead stored as ad-hoc
132 | locations. Hence for range_bits == 5 we can compactly store
133 | tokens of length <= 32 without needing to use the ad-hoc
134 | table.
135 |
136 | This packing scheme means we effectively have
137 | (column_bits - range_bits)
138 | of bits for the columns, typically (12 - 5) = 7, for 128
139 | columns; longer line widths are accomodated by starting a
140 | new ordmap with a higher column_bits.
141 |
142 | ordmap[2]->start_location-1 | Final location in ordmap 1
143 -----------+-------------------------------+-------------------------------
144 | ordmap[2]->start_location | First line in ordmap 2
145 | ordmap[3]->start_location-1 | Final location in ordmap 2
146 -----------+-------------------------------+-------------------------------
147 | | (etc)
148 -----------+-------------------------------+-------------------------------
149 | ordmap[n-1]->start_location | First line in final ord map
150 | | (etc)
151 | set->highest_location - 1 | Final location in that ordmap
152 -----------+-------------------------------+-------------------------------
153 | set->highest_location | Location of the where the next
154 | | ordinary linemap would start
155 -----------+-------------------------------+-------------------------------
156 | |
157 | VVVVVVVVVVVVVVVVVVVVVVVVVVV
158 | Ordinary maps grow this way
159 |
160 | (unallocated integers)
161 |
162 0x60000000 | LINE_MAP_MAX_LOCATION_WITH_COLS
163 | Beyond this point, ordinary linemaps have 0 bits per column:
164 | each increment of the value corresponds to a new source line.
165 |
166 0x70000000 | LINE_MAP_MAX_LOCATION
167 | Beyond the point, we give up on ordinary maps; attempts to
168 | create locations in them lead to UNKNOWN_LOCATION (0).
169 |
170 | (unallocated integers)
171 |
172 | Macro maps grow this way
173 | ^^^^^^^^^^^^^^^^^^^^^^^^
174 | |
175 -----------+-------------------------------+-------------------------------
176 | LINEMAPS_MACRO_LOWEST_LOCATION| Locations within macro maps
177 | macromap[m-1]->start_location | Start of last macro map
178 | |
179 -----------+-------------------------------+-------------------------------
180 | macromap[m-2]->start_location | Start of penultimate macro map
181 -----------+-------------------------------+-------------------------------
182 | macromap[1]->start_location | Start of macro map 1
183 -----------+-------------------------------+-------------------------------
184 | macromap[0]->start_location | Start of macro map 0
185 0x7fffffff | MAX_LOCATION_T | Also used as a mask for
186 | | accessing the ad-hoc data table
187 -----------+-------------------------------+-------------------------------
188 0x80000000 | Start of ad-hoc values; the lower 31 bits are used as an index
189 ... | into the line_table->location_adhoc_data_map.data array.
190 0xffffffff | UINT_MAX |
191 -----------+-------------------------------+-------------------------------
192
193 Examples of location encoding.
194
195 Packed ranges
196 =============
197
198 Consider encoding the location of a token "foo", seen underlined here
199 on line 523, within an ordinary line_map that starts at line 500:
200
201 11111111112
202 12345678901234567890
203 522
204 523 return foo + bar;
205 ^~~
206 524
207
208 The location's caret and start are both at line 523, column 11; the
209 location's finish is on the same line, at column 13 (an offset of 2
210 columns, for length 3).
211
212 Line 523 is offset 23 from the starting line of the ordinary line_map.
213
214 caret == start, and the offset of the finish fits within 5 bits, so
215 this can be stored as a packed range.
216
217 This is encoded as:
218 ordmap->start
219 + (line_offset << ordmap->m_column_and_range_bits)
220 + (column << ordmap->m_range_bits)
221 + (range_offset);
222 i.e. (for line offset 23, column 11, range offset 2):
223 ordmap->start
224 + (23 << 12)
225 + (11 << 5)
226 + 2;
227 i.e.:
228 ordmap->start + 0x17162
229 assuming that the line_map uses the default of 7 bits for columns and
230 5 bits for packed range (giving 12 bits for m_column_and_range_bits).
231
232
233 "Pure" locations
234 ================
235
236 These are a special case of the above, where
237 caret == start == finish
238 They are stored as packed ranges with offset == 0.
239 For example, the location of the "f" of "foo" could be stored
240 as above, but with range offset 0, giving:
241 ordmap->start
242 + (23 << 12)
243 + (11 << 5)
244 + 0;
245 i.e.:
246 ordmap->start + 0x17160
247
248
249 Unoptimized ranges
250 ==================
251
252 Consider encoding the location of the binary expression
253 below:
254
255 11111111112
256 12345678901234567890
257 522
258 523 return foo + bar;
259 ~~~~^~~~~
260 524
261
262 The location's caret is at the "+", line 523 column 15, but starts
263 earlier, at the "f" of "foo" at column 11. The finish is at the "r"
264 of "bar" at column 19.
265
266 This can't be stored as a packed range since start != caret.
267 Hence it is stored as an ad-hoc location e.g. 0x80000003.
268
269 Stripping off the top bit gives us an index into the ad-hoc
270 lookaside table:
271
272 line_table->location_adhoc_data_map.data[0x3]
273
274 from which the caret, start and finish can be looked up,
275 encoded as "pure" locations:
276
277 start == ordmap->start + (23 << 12) + (11 << 5)
278 == ordmap->start + 0x17160 (as above; the "f" of "foo")
279
280 caret == ordmap->start + (23 << 12) + (15 << 5)
281 == ordmap->start + 0x171e0
282
283 finish == ordmap->start + (23 << 12) + (19 << 5)
284 == ordmap->start + 0x17260
285
286 To further see how location_t works in practice, see the
287 worked example in libcpp/location-example.txt. */
288 typedef unsigned int location_t;
289
290 /* Do not track column numbers higher than this one. As a result, the
291 range of column_bits is [12, 18] (or 0 if column numbers are
292 disabled). */
293 const unsigned int LINE_MAP_MAX_COLUMN_NUMBER = (1U << 12);
294
295 /* Do not pack ranges if locations get higher than this.
296 If you change this, update:
297 gcc.dg/plugin/location-overflow-test-*.c. */
298 const location_t LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES = 0x50000000;
299
300 /* Do not track column numbers if locations get higher than this.
301 If you change this, update:
302 gcc.dg/plugin/location-overflow-test-*.c. */
303 const location_t LINE_MAP_MAX_LOCATION_WITH_COLS = 0x60000000;
304
305 /* Highest possible source location encoded within an ordinary map. */
306 const location_t LINE_MAP_MAX_LOCATION = 0x70000000;
307
308 /* A range of source locations.
309
310 Ranges are closed:
311 m_start is the first location within the range,
312 m_finish is the last location within the range.
313
314 We may need a more compact way to store these, but for now,
315 let's do it the simple way, as a pair. */
316 struct GTY(()) source_range
317 {
318 location_t m_start;
319 location_t m_finish;
320
321 /* We avoid using constructors, since various structs that
322 don't yet have constructors will embed instances of
323 source_range. */
324
325 /* Make a source_range from a location_t. */
326 static source_range from_location (location_t loc)
327 {
328 source_range result;
329 result.m_start = loc;
330 result.m_finish = loc;
331 return result;
332 }
333
334 /* Make a source_range from a pair of location_t. */
335 static source_range from_locations (location_t start,
336 location_t finish)
337 {
338 source_range result;
339 result.m_start = start;
340 result.m_finish = finish;
341 return result;
342 }
343 };
344
345 /* Memory allocation function typedef. Works like xrealloc. */
346 typedef void *(*line_map_realloc) (void *, size_t);
347
348 /* Memory allocator function that returns the actual allocated size,
349 for a given requested allocation. */
350 typedef size_t (*line_map_round_alloc_size_func) (size_t);
351
352 /* A line_map encodes a sequence of locations.
353 There are two kinds of maps. Ordinary maps and macro expansion
354 maps, a.k.a macro maps.
355
356 A macro map encodes source locations of tokens that are part of a
357 macro replacement-list, at a macro expansion point. E.g, in:
358
359 #define PLUS(A,B) A + B
360
361 No macro map is going to be created there, because we are not at a
362 macro expansion point. We are at a macro /definition/ point. So the
363 locations of the tokens of the macro replacement-list (i.e, A + B)
364 will be locations in an ordinary map, not a macro map.
365
366 On the other hand, if we later do:
367
368 int a = PLUS (1,2);
369
370 The invocation of PLUS here is a macro expansion. So we are at a
371 macro expansion point. The preprocessor expands PLUS (1,2) and
372 replaces it with the tokens of its replacement-list: 1 + 2. A macro
373 map is going to be created to hold (or rather to map, haha ...) the
374 locations of the tokens 1, + and 2. The macro map also records the
375 location of the expansion point of PLUS. That location is mapped in
376 the map that is active right before the location of the invocation
377 of PLUS. */
378
379 /* This contains GTY mark-up to support precompiled headers.
380 line_map is an abstract class, only derived objects exist. */
381 struct GTY((tag ("0"), desc ("MAP_ORDINARY_P (&%h) ? 1 : 2"))) line_map {
382 location_t start_location;
383
384 /* Size and alignment is (usually) 4 bytes. */
385 };
386
387 /* An ordinary line map encodes physical source locations. Those
388 physical source locations are called "spelling locations".
389
390 Physical source file TO_FILE at line TO_LINE at column 0 is represented
391 by the logical START_LOCATION. TO_LINE+L at column C is represented by
392 START_LOCATION+(L*(1<<m_column_and_range_bits))+(C*1<<m_range_bits), as
393 long as C<(1<<effective range bits), and the result_location is less than
394 the next line_map's start_location.
395 (The top line is line 1 and the leftmost column is column 1; line/column 0
396 means "entire file/line" or "unknown line/column" or "not applicable".)
397
398 The highest possible source location is MAX_LOCATION_T. */
399 struct GTY((tag ("1"))) line_map_ordinary : public line_map {
400 /* Base class is 4 bytes. */
401
402 /* 4 bytes of integers, each 1 byte for easy extraction/insertion. */
403
404 /* The reason for creation of this line map. */
405 ENUM_BITFIELD (lc_reason) reason : 8;
406
407 /* SYSP is one for a system header, two for a C system header file
408 that therefore needs to be extern "C" protected in C++, and zero
409 otherwise. This field isn't really needed now that it's in
410 cpp_buffer. */
411 unsigned char sysp;
412
413 /* Number of the low-order location_t bits used for column numbers
414 and ranges. */
415 unsigned int m_column_and_range_bits : 8;
416
417 /* Number of the low-order "column" bits used for storing short ranges
418 inline, rather than in the ad-hoc table.
419 MSB LSB
420 31 0
421 +-------------------------+-------------------------------------------+
422 | |<---map->column_and_range_bits (e.g. 12)-->|
423 +-------------------------+-----------------------+-------------------+
424 | | column_and_range_bits | map->range_bits |
425 | | - range_bits | |
426 +-------------------------+-----------------------+-------------------+
427 | row bits | effective column bits | short range bits |
428 | | (e.g. 7) | (e.g. 5) |
429 +-------------------------+-----------------------+-------------------+ */
430 unsigned int m_range_bits : 8;
431
432 /* Pointer alignment boundary on both 32 and 64-bit systems. */
433
434 const char *to_file;
435 linenum_type to_line;
436
437 /* Location from whence this line map was included. For regular
438 #includes, this location will be the last location of a map. For
439 outermost file, this is 0. */
440 location_t included_from;
441
442 /* Size is 20 or 24 bytes, no padding */
443 };
444
445 /* This is the highest possible source location encoded within an
446 ordinary or macro map. */
447 const location_t MAX_LOCATION_T = 0x7FFFFFFF;
448
449 struct cpp_hashnode;
450
451 /* A macro line map encodes location of tokens coming from a macro
452 expansion.
453
454 The offset from START_LOCATION is used to index into
455 MACRO_LOCATIONS; this holds the original location of the token. */
456 struct GTY((tag ("2"))) line_map_macro : public line_map {
457 /* Base is 4 bytes. */
458
459 /* The number of tokens inside the replacement-list of MACRO. */
460 unsigned int n_tokens;
461
462 /* Pointer alignment boundary. */
463
464 /* The cpp macro whose expansion gave birth to this macro map. */
465 struct cpp_hashnode *
466 GTY ((nested_ptr (union tree_node,
467 "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
468 "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
469 macro;
470
471 /* This array of location is actually an array of pairs of
472 locations. The elements inside it thus look like:
473
474 x0,y0, x1,y1, x2,y2, ...., xn,yn.
475
476 where n == n_tokens;
477
478 Remember that these xI,yI are collected when libcpp is about to
479 expand a given macro.
480
481 yI is the location in the macro definition, either of the token
482 itself or of a macro parameter that it replaces.
483
484 Imagine this:
485
486 #define PLUS(A, B) A + B <--- #1
487
488 int a = PLUS (1,2); <--- #2
489
490 There is a macro map for the expansion of PLUS in #2. PLUS is
491 expanded into its expansion-list. The expansion-list is the
492 replacement-list of PLUS where the macro parameters are replaced
493 with their arguments. So the replacement-list of PLUS is made of
494 the tokens:
495
496 A, +, B
497
498 and the expansion-list is made of the tokens:
499
500 1, +, 2
501
502 Let's consider the case of token "+". Its y1 [yI for I == 1] is
503 its spelling location in #1.
504
505 y0 (thus for token "1") is the spelling location of A in #1.
506
507 And y2 (of token "2") is the spelling location of B in #1.
508
509 When the token is /not/ an argument for a macro, xI is the same
510 location as yI. Otherwise, xI is the location of the token
511 outside this macro expansion. If this macro was expanded from
512 another macro expansion, xI is a virtual location representing
513 the token in that macro expansion; otherwise, it is the spelling
514 location of the token.
515
516 Note that a virtual location is a location returned by
517 linemap_add_macro_token. It encodes the relevant locations (x,y
518 pairs) of that token across the macro expansions from which it
519 (the token) might come from.
520
521 In the example above x1 (for token "+") is going to be the same
522 as y1. x0 is the spelling location for the argument token "1",
523 and x2 is the spelling location for the argument token "2". */
524 location_t * GTY((atomic)) macro_locations;
525
526 /* This is the location of the expansion point of the current macro
527 map. It's the location of the macro name. That location is held
528 by the map that was current right before the current one. It
529 could have been either a macro or an ordinary map, depending on
530 if we are in a nested expansion context not. */
531 location_t expansion;
532
533 /* Size is 20 or 32 (4 bytes padding on 64-bit). */
534 };
535
536 #if CHECKING_P && (GCC_VERSION >= 2007)
537
538 /* Assertion macro to be used in line-map code. */
539 #define linemap_assert(EXPR) \
540 do { \
541 if (! (EXPR)) \
542 abort (); \
543 } while (0)
544
545 /* Assert that becomes a conditional expression when checking is disabled at
546 compilation time. Use this for conditions that should not happen but if
547 they happen, it is better to handle them gracefully rather than crash
548 randomly later.
549 Usage:
550
551 if (linemap_assert_fails(EXPR)) handle_error(); */
552 #define linemap_assert_fails(EXPR) __extension__ \
553 ({linemap_assert (EXPR); false;})
554
555 #else
556 /* Include EXPR, so that unused variable warnings do not occur. */
557 #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
558 #define linemap_assert_fails(EXPR) (! (EXPR))
559 #endif
560
561 /* Get whether location LOC is an ad-hoc, ordinary or macro location. */
562
563 inline bool
564 IS_ORDINARY_LOC (location_t loc)
565 {
566 return loc < LINE_MAP_MAX_LOCATION;
567 }
568
569 inline bool
570 IS_ADHOC_LOC (location_t loc)
571 {
572 return loc > MAX_LOCATION_T;
573 }
574
575 inline bool
576 IS_MACRO_LOC (location_t loc)
577 {
578 return !IS_ORDINARY_LOC (loc) && !IS_ADHOC_LOC (loc);
579 }
580
581 /* Categorize line map kinds. */
582
583 inline bool
584 MAP_ORDINARY_P (const line_map *map)
585 {
586 return IS_ORDINARY_LOC (map->start_location);
587 }
588
589 /* Return TRUE if MAP encodes locations coming from a macro
590 replacement-list at macro expansion point. */
591 bool
592 linemap_macro_expansion_map_p (const struct line_map *);
593
594 /* Assert that MAP encodes locations of tokens that are not part of
595 the replacement-list of a macro expansion, downcasting from
596 line_map * to line_map_ordinary *. */
597
598 inline line_map_ordinary *
599 linemap_check_ordinary (struct line_map *map)
600 {
601 linemap_assert (MAP_ORDINARY_P (map));
602 return (line_map_ordinary *)map;
603 }
604
605 /* Assert that MAP encodes locations of tokens that are not part of
606 the replacement-list of a macro expansion, downcasting from
607 const line_map * to const line_map_ordinary *. */
608
609 inline const line_map_ordinary *
610 linemap_check_ordinary (const struct line_map *map)
611 {
612 linemap_assert (MAP_ORDINARY_P (map));
613 return (const line_map_ordinary *)map;
614 }
615
616 /* Assert that MAP is a macro expansion and downcast to the appropriate
617 subclass. */
618
619 inline line_map_macro *linemap_check_macro (line_map *map)
620 {
621 linemap_assert (!MAP_ORDINARY_P (map));
622 return (line_map_macro *)map;
623 }
624
625 /* Assert that MAP is a macro expansion and downcast to the appropriate
626 subclass. */
627
628 inline const line_map_macro *
629 linemap_check_macro (const line_map *map)
630 {
631 linemap_assert (!MAP_ORDINARY_P (map));
632 return (const line_map_macro *)map;
633 }
634
635 /* Read the start location of MAP. */
636
637 inline location_t
638 MAP_START_LOCATION (const line_map *map)
639 {
640 return map->start_location;
641 }
642
643 /* Get the starting line number of ordinary map MAP. */
644
645 inline linenum_type
646 ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map_ordinary *ord_map)
647 {
648 return ord_map->to_line;
649 }
650
651 /* Return a positive value if map encodes locations from a system
652 header, 0 otherwise. Returns 1 if ordinary map MAP encodes locations
653 in a system header and 2 if it encodes locations in a C system header
654 that therefore needs to be extern "C" protected in C++. */
655
656 inline unsigned char
657 ORDINARY_MAP_IN_SYSTEM_HEADER_P (const line_map_ordinary *ord_map)
658 {
659 return ord_map->sysp;
660 }
661
662 /* Get the filename of ordinary map MAP. */
663
664 inline const char *
665 ORDINARY_MAP_FILE_NAME (const line_map_ordinary *ord_map)
666 {
667 return ord_map->to_file;
668 }
669
670 /* Get the cpp macro whose expansion gave birth to macro map MAP. */
671
672 inline cpp_hashnode *
673 MACRO_MAP_MACRO (const line_map_macro *macro_map)
674 {
675 return macro_map->macro;
676 }
677
678 /* Get the number of tokens inside the replacement-list of the macro
679 that led to macro map MAP. */
680
681 inline unsigned int
682 MACRO_MAP_NUM_MACRO_TOKENS (const line_map_macro *macro_map)
683 {
684 return macro_map->n_tokens;
685 }
686
687 /* Get the array of pairs of locations within macro map MAP.
688 See the declaration of line_map_macro for more information. */
689
690 inline location_t *
691 MACRO_MAP_LOCATIONS (const line_map_macro *macro_map)
692 {
693 return macro_map->macro_locations;
694 }
695
696 /* Get the location of the expansion point of the macro map MAP. */
697
698 inline location_t
699 MACRO_MAP_EXPANSION_POINT_LOCATION (const line_map_macro *macro_map)
700 {
701 return macro_map->expansion;
702 }
703
704 /* The abstraction of a set of location maps. There can be several
705 types of location maps. This abstraction contains the attributes
706 that are independent from the type of the map.
707
708 Essentially this is just a vector of T_linemap_subclass,
709 which can only ever grow in size. */
710
711 struct GTY(()) maps_info_ordinary {
712 /* This array contains the "ordinary" line maps, for all
713 events other than macro expansion
714 (e.g. when a new preprocessing unit starts or ends). */
715 line_map_ordinary * GTY ((length ("%h.used"))) maps;
716
717 /* The total number of allocated maps. */
718 unsigned int allocated;
719
720 /* The number of elements used in maps. This number is smaller
721 or equal to ALLOCATED. */
722 unsigned int used;
723
724 unsigned int cache;
725 };
726
727 struct GTY(()) maps_info_macro {
728 /* This array contains the macro line maps.
729 A macro line map is created whenever a macro expansion occurs. */
730 line_map_macro * GTY ((length ("%h.used"))) maps;
731
732 /* The total number of allocated maps. */
733 unsigned int allocated;
734
735 /* The number of elements used in maps. This number is smaller
736 or equal to ALLOCATED. */
737 unsigned int used;
738
739 unsigned int cache;
740 };
741
742 /* Data structure to associate a source_range together with an arbitrary
743 data pointer with a source location. */
744 struct GTY(()) location_adhoc_data {
745 location_t locus;
746 source_range src_range;
747 void * GTY((skip)) data;
748 };
749
750 struct htab;
751
752 /* The following data structure encodes a location with some adhoc data
753 and maps it to a new unsigned integer (called an adhoc location)
754 that replaces the original location to represent the mapping.
755
756 The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
757 highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
758 the original location. Once identified as the adhoc_loc, the lower 31
759 bits of the integer is used to index the location_adhoc_data array,
760 in which the locus and associated data is stored. */
761
762 struct GTY(()) location_adhoc_data_map {
763 struct htab * GTY((skip)) htab;
764 location_t curr_loc;
765 unsigned int allocated;
766 struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
767 };
768
769 /* A set of chronological line_map structures. */
770 struct GTY(()) line_maps {
771
772 ~line_maps ();
773
774 maps_info_ordinary info_ordinary;
775
776 maps_info_macro info_macro;
777
778 /* Depth of the include stack, including the current file. */
779 unsigned int depth;
780
781 /* If true, prints an include trace a la -H. */
782 bool trace_includes;
783
784 /* Highest location_t "given out". */
785 location_t highest_location;
786
787 /* Start of line of highest location_t "given out". */
788 location_t highest_line;
789
790 /* The maximum column number we can quickly allocate. Higher numbers
791 may require allocating a new line_map. */
792 unsigned int max_column_hint;
793
794 /* The allocator to use when resizing 'maps', defaults to xrealloc. */
795 line_map_realloc reallocator;
796
797 /* The allocators' function used to know the actual size it
798 allocated, for a certain allocation size requested. */
799 line_map_round_alloc_size_func round_alloc_size;
800
801 struct location_adhoc_data_map location_adhoc_data_map;
802
803 /* The special location value that is used as spelling location for
804 built-in tokens. */
805 location_t builtin_location;
806
807 /* True if we've seen a #line or # 44 "file" directive. */
808 bool seen_line_directive;
809
810 /* The default value of range_bits in ordinary line maps. */
811 unsigned int default_range_bits;
812
813 unsigned int num_optimized_ranges;
814 unsigned int num_unoptimized_ranges;
815 };
816
817 /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
818 if we are interested in macro maps, FALSE otherwise. */
819 inline unsigned int
820 LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind)
821 {
822 if (map_kind)
823 return set->info_macro.allocated;
824 else
825 return set->info_ordinary.allocated;
826 }
827
828 /* As above, but by reference (e.g. as an lvalue). */
829
830 inline unsigned int &
831 LINEMAPS_ALLOCATED (line_maps *set, bool map_kind)
832 {
833 if (map_kind)
834 return set->info_macro.allocated;
835 else
836 return set->info_ordinary.allocated;
837 }
838
839 /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
840 we are interested in macro maps, FALSE otherwise.*/
841 inline unsigned int
842 LINEMAPS_USED (const line_maps *set, bool map_kind)
843 {
844 if (map_kind)
845 return set->info_macro.used;
846 else
847 return set->info_ordinary.used;
848 }
849
850 /* As above, but by reference (e.g. as an lvalue). */
851
852 inline unsigned int &
853 LINEMAPS_USED (line_maps *set, bool map_kind)
854 {
855 if (map_kind)
856 return set->info_macro.used;
857 else
858 return set->info_ordinary.used;
859 }
860
861 /* Returns the index of the last map that was looked up with
862 linemap_lookup. MAP_KIND shall be TRUE if we are interested in
863 macro maps, FALSE otherwise. */
864 inline unsigned int
865 LINEMAPS_CACHE (const line_maps *set, bool map_kind)
866 {
867 if (map_kind)
868 return set->info_macro.cache;
869 else
870 return set->info_ordinary.cache;
871 }
872
873 /* As above, but by reference (e.g. as an lvalue). */
874
875 inline unsigned int &
876 LINEMAPS_CACHE (line_maps *set, bool map_kind)
877 {
878 if (map_kind)
879 return set->info_macro.cache;
880 else
881 return set->info_ordinary.cache;
882 }
883
884 /* Return the map at a given index. */
885 inline line_map *
886 LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, int index)
887 {
888 if (map_kind)
889 return &set->info_macro.maps[index];
890 else
891 return &set->info_ordinary.maps[index];
892 }
893
894 /* Returns the last map used in the line table SET. MAP_KIND
895 shall be TRUE if we are interested in macro maps, FALSE
896 otherwise.*/
897 inline line_map *
898 LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind)
899 {
900 return LINEMAPS_MAP_AT (set, map_kind,
901 LINEMAPS_USED (set, map_kind) - 1);
902 }
903
904 /* Returns the last map that was allocated in the line table SET.
905 MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
906 otherwise.*/
907 inline line_map *
908 LINEMAPS_LAST_ALLOCATED_MAP (const line_maps *set, bool map_kind)
909 {
910 return LINEMAPS_MAP_AT (set, map_kind,
911 LINEMAPS_ALLOCATED (set, map_kind) - 1);
912 }
913
914 /* Returns a pointer to the memory region where ordinary maps are
915 allocated in the line table SET. */
916 inline line_map_ordinary *
917 LINEMAPS_ORDINARY_MAPS (const line_maps *set)
918 {
919 return set->info_ordinary.maps;
920 }
921
922 /* Returns the INDEXth ordinary map. */
923 inline line_map_ordinary *
924 LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, int index)
925 {
926 linemap_assert (index >= 0);
927 linemap_assert ((unsigned int)index < set->info_ordinary.used);
928 return &set->info_ordinary.maps[index];
929 }
930
931 /* Return the number of ordinary maps allocated in the line table
932 SET. */
933 inline unsigned int
934 LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set)
935 {
936 return LINEMAPS_ALLOCATED (set, false);
937 }
938
939 /* Return the number of ordinary maps used in the line table SET. */
940 inline unsigned int
941 LINEMAPS_ORDINARY_USED (const line_maps *set)
942 {
943 return LINEMAPS_USED (set, false);
944 }
945
946 /* Return the index of the last ordinary map that was looked up with
947 linemap_lookup. */
948 inline unsigned int
949 LINEMAPS_ORDINARY_CACHE (const line_maps *set)
950 {
951 return LINEMAPS_CACHE (set, false);
952 }
953
954 /* As above, but by reference (e.g. as an lvalue). */
955
956 inline unsigned int &
957 LINEMAPS_ORDINARY_CACHE (line_maps *set)
958 {
959 return LINEMAPS_CACHE (set, false);
960 }
961
962 /* Returns a pointer to the last ordinary map used in the line table
963 SET. */
964 inline line_map_ordinary *
965 LINEMAPS_LAST_ORDINARY_MAP (const line_maps *set)
966 {
967 return (line_map_ordinary *)LINEMAPS_LAST_MAP (set, false);
968 }
969
970 /* Returns a pointer to the last ordinary map allocated the line table
971 SET. */
972 inline line_map_ordinary *
973 LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP (const line_maps *set)
974 {
975 return (line_map_ordinary *)LINEMAPS_LAST_ALLOCATED_MAP (set, false);
976 }
977
978 /* Returns a pointer to the beginning of the region where macro maps
979 are allocated. */
980 inline line_map_macro *
981 LINEMAPS_MACRO_MAPS (const line_maps *set)
982 {
983 return set->info_macro.maps;
984 }
985
986 /* Returns the INDEXth macro map. */
987 inline line_map_macro *
988 LINEMAPS_MACRO_MAP_AT (const line_maps *set, int index)
989 {
990 linemap_assert (index >= 0);
991 linemap_assert ((unsigned int)index < set->info_macro.used);
992 return &set->info_macro.maps[index];
993 }
994
995 /* Returns the number of macro maps that were allocated in the line
996 table SET. */
997 inline unsigned int
998 LINEMAPS_MACRO_ALLOCATED (const line_maps *set)
999 {
1000 return LINEMAPS_ALLOCATED (set, true);
1001 }
1002
1003 /* Returns the number of macro maps used in the line table SET. */
1004 inline unsigned int
1005 LINEMAPS_MACRO_USED (const line_maps *set)
1006 {
1007 return LINEMAPS_USED (set, true);
1008 }
1009
1010 /* Returns the index of the last macro map looked up with
1011 linemap_lookup. */
1012 inline unsigned int
1013 LINEMAPS_MACRO_CACHE (const line_maps *set)
1014 {
1015 return LINEMAPS_CACHE (set, true);
1016 }
1017
1018 /* As above, but by reference (e.g. as an lvalue). */
1019
1020 inline unsigned int &
1021 LINEMAPS_MACRO_CACHE (line_maps *set)
1022 {
1023 return LINEMAPS_CACHE (set, true);
1024 }
1025
1026 /* Returns the last macro map used in the line table SET. */
1027 inline line_map_macro *
1028 LINEMAPS_LAST_MACRO_MAP (const line_maps *set)
1029 {
1030 return (line_map_macro *)LINEMAPS_LAST_MAP (set, true);
1031 }
1032
1033 /* Returns the lowest location [of a token resulting from macro
1034 expansion] encoded in this line table. */
1035 inline location_t
1036 LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps *set)
1037 {
1038 return LINEMAPS_MACRO_USED (set)
1039 ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))
1040 : MAX_LOCATION_T + 1;
1041 }
1042
1043 /* Returns the last macro map allocated in the line table SET. */
1044 inline line_map_macro *
1045 LINEMAPS_LAST_ALLOCATED_MACRO_MAP (const line_maps *set)
1046 {
1047 return (line_map_macro *)LINEMAPS_LAST_ALLOCATED_MAP (set, true);
1048 }
1049
1050 extern location_t get_combined_adhoc_loc (struct line_maps *,
1051 location_t,
1052 source_range,
1053 void *);
1054 extern void *get_data_from_adhoc_loc (struct line_maps *, location_t);
1055 extern location_t get_location_from_adhoc_loc (struct line_maps *,
1056 location_t);
1057
1058 extern source_range get_range_from_loc (line_maps *set, location_t loc);
1059
1060 /* Get whether location LOC is a "pure" location, or
1061 whether it is an ad-hoc location, or embeds range information. */
1062
1063 bool
1064 pure_location_p (line_maps *set, location_t loc);
1065
1066 /* Given location LOC within SET, strip away any packed range information
1067 or ad-hoc information. */
1068
1069 extern location_t get_pure_location (line_maps *set,
1070 location_t loc);
1071
1072 /* Combine LOC and BLOCK, giving a combined adhoc location. */
1073
1074 inline location_t
1075 COMBINE_LOCATION_DATA (struct line_maps *set,
1076 location_t loc,
1077 source_range src_range,
1078 void *block)
1079 {
1080 return get_combined_adhoc_loc (set, loc, src_range, block);
1081 }
1082
1083 extern void rebuild_location_adhoc_htab (struct line_maps *);
1084
1085 /* Initialize a line map set. SET is the line map set to initialize
1086 and BUILTIN_LOCATION is the special location value to be used as
1087 spelling location for built-in tokens. This BUILTIN_LOCATION has
1088 to be strictly less than RESERVED_LOCATION_COUNT. */
1089 extern void linemap_init (struct line_maps *set,
1090 location_t builtin_location);
1091
1092 /* Check for and warn about line_maps entered but not exited. */
1093
1094 extern void linemap_check_files_exited (struct line_maps *);
1095
1096 /* Return a location_t for the start (i.e. column==0) of
1097 (physical) line TO_LINE in the current source file (as in the
1098 most recent linemap_add). MAX_COLUMN_HINT is the highest column
1099 number we expect to use in this line (but it does not change
1100 the highest_location). */
1101
1102 extern location_t linemap_line_start
1103 (struct line_maps *set, linenum_type to_line, unsigned int max_column_hint);
1104
1105 /* Add a mapping of logical source line to physical source file and
1106 line number. This function creates an "ordinary map", which is a
1107 map that records locations of tokens that are not part of macro
1108 replacement-lists present at a macro expansion point.
1109
1110 The text pointed to by TO_FILE must have a lifetime
1111 at least as long as the lifetime of SET. An empty
1112 TO_FILE means standard input. If reason is LC_LEAVE, and
1113 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
1114 natural values considering the file we are returning to.
1115
1116 A call to this function can relocate the previous set of
1117 maps, so any stored line_map pointers should not be used. */
1118 extern const struct line_map *linemap_add
1119 (struct line_maps *, enum lc_reason, unsigned int sysp,
1120 const char *to_file, linenum_type to_line);
1121
1122 /* Given a logical source location, returns the map which the
1123 corresponding (source file, line, column) triplet can be deduced
1124 from. Since the set is built chronologically, the logical lines are
1125 monotonic increasing, and so the list is sorted and we can use a
1126 binary search. If no line map have been allocated yet, this
1127 function returns NULL. */
1128 extern const struct line_map *linemap_lookup
1129 (struct line_maps *, location_t);
1130
1131 /* Returns TRUE if the line table set tracks token locations across
1132 macro expansion, FALSE otherwise. */
1133 bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
1134
1135 /* Return the name of the macro associated to MACRO_MAP. */
1136 const char* linemap_map_get_macro_name (const line_map_macro *);
1137
1138 /* Return a positive value if LOCATION is the locus of a token that is
1139 located in a system header, O otherwise. It returns 1 if LOCATION
1140 is the locus of a token that is located in a system header, and 2
1141 if LOCATION is the locus of a token located in a C system header
1142 that therefore needs to be extern "C" protected in C++.
1143
1144 Note that this function returns 1 if LOCATION belongs to a token
1145 that is part of a macro replacement-list defined in a system
1146 header, but expanded in a non-system file. */
1147 int linemap_location_in_system_header_p (struct line_maps *,
1148 location_t);
1149
1150 /* Return TRUE if LOCATION is a source code location of a token that is part of
1151 a macro expansion, FALSE otherwise. */
1152 bool linemap_location_from_macro_expansion_p (const struct line_maps *,
1153 location_t);
1154
1155 /* TRUE if LOCATION is a source code location of a token that is part of the
1156 definition of a macro, FALSE otherwise. */
1157 bool linemap_location_from_macro_definition_p (struct line_maps *,
1158 location_t);
1159
1160 /* With the precondition that LOCATION is the locus of a token that is
1161 an argument of a function-like macro MACRO_MAP and appears in the
1162 expansion of MACRO_MAP, return the locus of that argument in the
1163 context of the caller of MACRO_MAP. */
1164
1165 extern location_t linemap_macro_map_loc_unwind_toward_spelling
1166 (line_maps *set, const line_map_macro *macro_map, location_t location);
1167
1168 /* location_t values from 0 to RESERVED_LOCATION_COUNT-1 will
1169 be reserved for libcpp user as special values, no token from libcpp
1170 will contain any of those locations. */
1171 const location_t RESERVED_LOCATION_COUNT = 2;
1172
1173 /* Converts a map and a location_t to source line. */
1174 inline linenum_type
1175 SOURCE_LINE (const line_map_ordinary *ord_map, location_t loc)
1176 {
1177 return ((loc - ord_map->start_location)
1178 >> ord_map->m_column_and_range_bits) + ord_map->to_line;
1179 }
1180
1181 /* Convert a map and location_t to source column number. */
1182 inline linenum_type
1183 SOURCE_COLUMN (const line_map_ordinary *ord_map, location_t loc)
1184 {
1185 return ((loc - ord_map->start_location)
1186 & ((1 << ord_map->m_column_and_range_bits) - 1)) >> ord_map->m_range_bits;
1187 }
1188
1189
1190 inline location_t
1191 linemap_included_from (const line_map_ordinary *ord_map)
1192 {
1193 return ord_map->included_from;
1194 }
1195
1196 /* The linemap containing the included-from location of MAP. */
1197 const line_map_ordinary *linemap_included_from_linemap
1198 (line_maps *set, const line_map_ordinary *map);
1199
1200 /* True if the map is at the bottom of the include stack. */
1201
1202 inline bool
1203 MAIN_FILE_P (const line_map_ordinary *ord_map)
1204 {
1205 return ord_map->included_from == 0;
1206 }
1207
1208 /* Encode and return a location_t from a column number. The
1209 source line considered is the last source line used to call
1210 linemap_line_start, i.e, the last source line which a location was
1211 encoded from. */
1212 extern location_t
1213 linemap_position_for_column (struct line_maps *, unsigned int);
1214
1215 /* Encode and return a source location from a given line and
1216 column. */
1217 location_t
1218 linemap_position_for_line_and_column (line_maps *set,
1219 const line_map_ordinary *,
1220 linenum_type, unsigned int);
1221
1222 /* Encode and return a location_t starting from location LOC and
1223 shifting it by OFFSET columns. This function does not support
1224 virtual locations. */
1225 location_t
1226 linemap_position_for_loc_and_offset (struct line_maps *set,
1227 location_t loc,
1228 unsigned int offset);
1229
1230 /* Return the file this map is for. */
1231 inline const char *
1232 LINEMAP_FILE (const line_map_ordinary *ord_map)
1233 {
1234 return ord_map->to_file;
1235 }
1236
1237 /* Return the line number this map started encoding location from. */
1238 inline linenum_type
1239 LINEMAP_LINE (const line_map_ordinary *ord_map)
1240 {
1241 return ord_map->to_line;
1242 }
1243
1244 /* Return a positive value if map encodes locations from a system
1245 header, 0 otherwise. Returns 1 if MAP encodes locations in a
1246 system header and 2 if it encodes locations in a C system header
1247 that therefore needs to be extern "C" protected in C++. */
1248 inline unsigned char
1249 LINEMAP_SYSP (const line_map_ordinary *ord_map)
1250 {
1251 return ord_map->sysp;
1252 }
1253
1254 /* Return a positive value if PRE denotes the location of a token that
1255 comes before the token of POST, 0 if PRE denotes the location of
1256 the same token as the token for POST, and a negative value
1257 otherwise. */
1258 int linemap_compare_locations (struct line_maps *set,
1259 location_t pre,
1260 location_t post);
1261
1262 /* Return TRUE if LOC_A denotes the location a token that comes
1263 topogically before the token denoted by location LOC_B, or if they
1264 are equal. */
1265 inline bool
1266 linemap_location_before_p (struct line_maps *set,
1267 location_t loc_a,
1268 location_t loc_b)
1269 {
1270 return linemap_compare_locations (set, loc_a, loc_b) >= 0;
1271 }
1272
1273 typedef struct
1274 {
1275 /* The name of the source file involved. */
1276 const char *file;
1277
1278 /* The line-location in the source file. */
1279 int line;
1280
1281 int column;
1282
1283 void *data;
1284
1285 /* In a system header?. */
1286 bool sysp;
1287 } expanded_location;
1288
1289 class range_label;
1290
1291 /* A hint to diagnostic_show_locus on how to print a source range within a
1292 rich_location.
1293
1294 Typically this is SHOW_RANGE_WITH_CARET for the 0th range, and
1295 SHOW_RANGE_WITHOUT_CARET for subsequent ranges,
1296 but the Fortran frontend uses SHOW_RANGE_WITH_CARET repeatedly for
1297 printing things like:
1298
1299 x = x + y
1300 1 2
1301 Error: Shapes for operands at (1) and (2) are not conformable
1302
1303 where "1" and "2" are notionally carets. */
1304
1305 enum range_display_kind
1306 {
1307 /* Show the pertinent source line(s), the caret, and underline(s). */
1308 SHOW_RANGE_WITH_CARET,
1309
1310 /* Show the pertinent source line(s) and underline(s), but don't
1311 show the caret (just an underline). */
1312 SHOW_RANGE_WITHOUT_CARET,
1313
1314 /* Just show the source lines; don't show the range itself.
1315 This is for use when displaying some line-insertion fix-it hints (for
1316 showing the user context on the change, for when it doesn't make sense
1317 to highlight the first column on the next line). */
1318 SHOW_LINES_WITHOUT_RANGE
1319 };
1320
1321 /* A location within a rich_location: a caret&range, with
1322 the caret potentially flagged for display, and an optional
1323 label. */
1324
1325 struct location_range
1326 {
1327 location_t m_loc;
1328
1329 enum range_display_kind m_range_display_kind;
1330
1331 /* If non-NULL, the label for this range. */
1332 const range_label *m_label;
1333 };
1334
1335 /* A partially-embedded vec for use within rich_location for storing
1336 ranges and fix-it hints.
1337
1338 Elements [0..NUM_EMBEDDED) are allocated within m_embed, after
1339 that they are within the dynamically-allocated m_extra.
1340
1341 This allows for static allocation in the common case, whilst
1342 supporting the rarer case of an arbitrary number of elements.
1343
1344 Dynamic allocation is not performed unless it's needed. */
1345
1346 template <typename T, int NUM_EMBEDDED>
1347 class semi_embedded_vec
1348 {
1349 public:
1350 semi_embedded_vec ();
1351 ~semi_embedded_vec ();
1352
1353 unsigned int count () const { return m_num; }
1354 T& operator[] (int idx);
1355 const T& operator[] (int idx) const;
1356
1357 void push (const T&);
1358 void truncate (int len);
1359
1360 private:
1361 int m_num;
1362 T m_embedded[NUM_EMBEDDED];
1363 int m_alloc;
1364 T *m_extra;
1365 };
1366
1367 /* Constructor for semi_embedded_vec. In particular, no dynamic allocation
1368 is done. */
1369
1370 template <typename T, int NUM_EMBEDDED>
1371 semi_embedded_vec<T, NUM_EMBEDDED>::semi_embedded_vec ()
1372 : m_num (0), m_alloc (0), m_extra (NULL)
1373 {
1374 }
1375
1376 /* semi_embedded_vec's dtor. Release any dynamically-allocated memory. */
1377
1378 template <typename T, int NUM_EMBEDDED>
1379 semi_embedded_vec<T, NUM_EMBEDDED>::~semi_embedded_vec ()
1380 {
1381 XDELETEVEC (m_extra);
1382 }
1383
1384 /* Look up element IDX, mutably. */
1385
1386 template <typename T, int NUM_EMBEDDED>
1387 T&
1388 semi_embedded_vec<T, NUM_EMBEDDED>::operator[] (int idx)
1389 {
1390 linemap_assert (idx < m_num);
1391 if (idx < NUM_EMBEDDED)
1392 return m_embedded[idx];
1393 else
1394 {
1395 linemap_assert (m_extra != NULL);
1396 return m_extra[idx - NUM_EMBEDDED];
1397 }
1398 }
1399
1400 /* Look up element IDX (const). */
1401
1402 template <typename T, int NUM_EMBEDDED>
1403 const T&
1404 semi_embedded_vec<T, NUM_EMBEDDED>::operator[] (int idx) const
1405 {
1406 linemap_assert (idx < m_num);
1407 if (idx < NUM_EMBEDDED)
1408 return m_embedded[idx];
1409 else
1410 {
1411 linemap_assert (m_extra != NULL);
1412 return m_extra[idx - NUM_EMBEDDED];
1413 }
1414 }
1415
1416 /* Append VALUE to the end of the semi_embedded_vec. */
1417
1418 template <typename T, int NUM_EMBEDDED>
1419 void
1420 semi_embedded_vec<T, NUM_EMBEDDED>::push (const T& value)
1421 {
1422 int idx = m_num++;
1423 if (idx < NUM_EMBEDDED)
1424 m_embedded[idx] = value;
1425 else
1426 {
1427 /* Offset "idx" to be an index within m_extra. */
1428 idx -= NUM_EMBEDDED;
1429 if (NULL == m_extra)
1430 {
1431 linemap_assert (m_alloc == 0);
1432 m_alloc = 16;
1433 m_extra = XNEWVEC (T, m_alloc);
1434 }
1435 else if (idx >= m_alloc)
1436 {
1437 linemap_assert (m_alloc > 0);
1438 m_alloc *= 2;
1439 m_extra = XRESIZEVEC (T, m_extra, m_alloc);
1440 }
1441 linemap_assert (m_extra);
1442 linemap_assert (idx < m_alloc);
1443 m_extra[idx] = value;
1444 }
1445 }
1446
1447 /* Truncate to length LEN. No deallocation is performed. */
1448
1449 template <typename T, int NUM_EMBEDDED>
1450 void
1451 semi_embedded_vec<T, NUM_EMBEDDED>::truncate (int len)
1452 {
1453 linemap_assert (len <= m_num);
1454 m_num = len;
1455 }
1456
1457 class fixit_hint;
1458
1459 /* A "rich" source code location, for use when printing diagnostics.
1460 A rich_location has one or more carets&ranges, where the carets
1461 are optional. These are referred to as "ranges" from here.
1462 Typically the zeroth range has a caret; other ranges sometimes
1463 have carets.
1464
1465 The "primary" location of a rich_location is the caret of range 0,
1466 used for determining the line/column when printing diagnostic
1467 text, such as:
1468
1469 some-file.c:3:1: error: ...etc...
1470
1471 Additional ranges may be added to help the user identify other
1472 pertinent clauses in a diagnostic.
1473
1474 Ranges can (optionally) be given labels via class range_label.
1475
1476 rich_location instances are intended to be allocated on the stack
1477 when generating diagnostics, and to be short-lived.
1478
1479 Examples of rich locations
1480 --------------------------
1481
1482 Example A
1483 *********
1484 int i = "foo";
1485 ^
1486 This "rich" location is simply a single range (range 0), with
1487 caret = start = finish at the given point.
1488
1489 Example B
1490 *********
1491 a = (foo && bar)
1492 ~~~~~^~~~~~~
1493 This rich location has a single range (range 0), with the caret
1494 at the first "&", and the start/finish at the parentheses.
1495 Compare with example C below.
1496
1497 Example C
1498 *********
1499 a = (foo && bar)
1500 ~~~ ^~ ~~~
1501 This rich location has three ranges:
1502 - Range 0 has its caret and start location at the first "&" and
1503 end at the second "&.
1504 - Range 1 has its start and finish at the "f" and "o" of "foo";
1505 the caret is not flagged for display, but is perhaps at the "f"
1506 of "foo".
1507 - Similarly, range 2 has its start and finish at the "b" and "r" of
1508 "bar"; the caret is not flagged for display, but is perhaps at the
1509 "b" of "bar".
1510 Compare with example B above.
1511
1512 Example D (Fortran frontend)
1513 ****************************
1514 x = x + y
1515 1 2
1516 This rich location has range 0 at "1", and range 1 at "2".
1517 Both are flagged for caret display. Both ranges have start/finish
1518 equal to their caret point. The frontend overrides the diagnostic
1519 context's default caret character for these ranges.
1520
1521 Example E (range labels)
1522 ************************
1523 printf ("arg0: %i arg1: %s arg2: %i",
1524 ^~
1525 |
1526 const char *
1527 100, 101, 102);
1528 ~~~
1529 |
1530 int
1531 This rich location has two ranges:
1532 - range 0 is at the "%s" with start = caret = "%" and finish at
1533 the "s". It has a range_label ("const char *").
1534 - range 1 has start/finish covering the "101" and is not flagged for
1535 caret printing. The caret is at the start of "101", where its
1536 range_label is printed ("int").
1537
1538 Fix-it hints
1539 ------------
1540
1541 Rich locations can also contain "fix-it hints", giving suggestions
1542 for the user on how to edit their code to fix a problem. These
1543 can be expressed as insertions, replacements, and removals of text.
1544 The edits by default are relative to the zeroth range within the
1545 rich_location, but optionally they can be expressed relative to
1546 other locations (using various overloaded methods of the form
1547 rich_location::add_fixit_*).
1548
1549 For example:
1550
1551 Example F: fix-it hint: insert_before
1552 *************************************
1553 ptr = arr[0];
1554 ^~~~~~
1555 &
1556 This rich location has a single range (range 0) covering "arr[0]",
1557 with the caret at the start. The rich location has a single
1558 insertion fix-it hint, inserted before range 0, added via
1559 richloc.add_fixit_insert_before ("&");
1560
1561 Example G: multiple fix-it hints: insert_before and insert_after
1562 ****************************************************************
1563 #define FN(ARG0, ARG1, ARG2) fn(ARG0, ARG1, ARG2)
1564 ^~~~ ^~~~ ^~~~
1565 ( ) ( ) ( )
1566 This rich location has three ranges, covering "arg0", "arg1",
1567 and "arg2", all with caret-printing enabled.
1568 The rich location has 6 insertion fix-it hints: each arg
1569 has a pair of insertion fix-it hints, suggesting wrapping
1570 them with parentheses: one a '(' inserted before,
1571 the other a ')' inserted after, added via
1572 richloc.add_fixit_insert_before (LOC, "(");
1573 and
1574 richloc.add_fixit_insert_after (LOC, ")");
1575
1576 Example H: fix-it hint: removal
1577 *******************************
1578 struct s {int i};;
1579 ^
1580 -
1581 This rich location has a single range at the stray trailing
1582 semicolon, along with a single removal fix-it hint, covering
1583 the same range, added via:
1584 richloc.add_fixit_remove ();
1585
1586 Example I: fix-it hint: replace
1587 *******************************
1588 c = s.colour;
1589 ^~~~~~
1590 color
1591 This rich location has a single range (range 0) covering "colour",
1592 and a single "replace" fix-it hint, covering the same range,
1593 added via
1594 richloc.add_fixit_replace ("color");
1595
1596 Example J: fix-it hint: line insertion
1597 **************************************
1598
1599 3 | #include <stddef.h>
1600 + |+#include <stdio.h>
1601 4 | int the_next_line;
1602
1603 This rich location has a single range at line 4 column 1, marked
1604 with SHOW_LINES_WITHOUT_RANGE (to avoid printing a meaningless caret
1605 on the "i" of int). It has a insertion fix-it hint of the string
1606 "#include <stdio.h>\n".
1607
1608 Adding a fix-it hint can fail: for example, attempts to insert content
1609 at the transition between two line maps may fail due to there being no
1610 location_t value to express the new location.
1611
1612 Attempts to add a fix-it hint within a macro expansion will fail.
1613
1614 There is only limited support for newline characters in fix-it hints:
1615 only hints with newlines which insert an entire new line are permitted,
1616 inserting at the start of a line, and finishing with a newline
1617 (with no interior newline characters). Other attempts to add
1618 fix-it hints containing newline characters will fail.
1619 Similarly, attempts to delete or replace a range *affecting* multiple
1620 lines will fail.
1621
1622 The rich_location API handles these failures gracefully, so that
1623 diagnostics can attempt to add fix-it hints without each needing
1624 extensive checking.
1625
1626 Fix-it hints within a rich_location are "atomic": if any hints can't
1627 be applied, none of them will be (tracked by the m_seen_impossible_fixit
1628 flag), and no fix-its hints will be displayed for that rich_location.
1629 This implies that diagnostic messages need to be worded in such a way
1630 that they make sense whether or not the fix-it hints are displayed,
1631 or that richloc.seen_impossible_fixit_p () should be checked before
1632 issuing the diagnostics. */
1633
1634 class rich_location
1635 {
1636 public:
1637 /* Constructors. */
1638
1639 /* Constructing from a location. */
1640 rich_location (line_maps *set, location_t loc,
1641 const range_label *label = NULL);
1642
1643 /* Destructor. */
1644 ~rich_location ();
1645
1646 /* Accessors. */
1647 location_t get_loc () const { return get_loc (0); }
1648 location_t get_loc (unsigned int idx) const;
1649
1650 void
1651 add_range (location_t loc,
1652 enum range_display_kind range_display_kind
1653 = SHOW_RANGE_WITHOUT_CARET,
1654 const range_label *label = NULL);
1655
1656 void
1657 set_range (unsigned int idx, location_t loc,
1658 enum range_display_kind range_display_kind);
1659
1660 unsigned int get_num_locations () const { return m_ranges.count (); }
1661
1662 const location_range *get_range (unsigned int idx) const;
1663 location_range *get_range (unsigned int idx);
1664
1665 expanded_location get_expanded_location (unsigned int idx);
1666
1667 void
1668 override_column (int column);
1669
1670 /* Fix-it hints. */
1671
1672 /* Methods for adding insertion fix-it hints. */
1673
1674 /* Suggest inserting NEW_CONTENT immediately before the primary
1675 range's start. */
1676 void
1677 add_fixit_insert_before (const char *new_content);
1678
1679 /* Suggest inserting NEW_CONTENT immediately before the start of WHERE. */
1680 void
1681 add_fixit_insert_before (location_t where,
1682 const char *new_content);
1683
1684 /* Suggest inserting NEW_CONTENT immediately after the end of the primary
1685 range. */
1686 void
1687 add_fixit_insert_after (const char *new_content);
1688
1689 /* Suggest inserting NEW_CONTENT immediately after the end of WHERE. */
1690 void
1691 add_fixit_insert_after (location_t where,
1692 const char *new_content);
1693
1694 /* Methods for adding removal fix-it hints. */
1695
1696 /* Suggest removing the content covered by range 0. */
1697 void
1698 add_fixit_remove ();
1699
1700 /* Suggest removing the content covered between the start and finish
1701 of WHERE. */
1702 void
1703 add_fixit_remove (location_t where);
1704
1705 /* Suggest removing the content covered by SRC_RANGE. */
1706 void
1707 add_fixit_remove (source_range src_range);
1708
1709 /* Methods for adding "replace" fix-it hints. */
1710
1711 /* Suggest replacing the content covered by range 0 with NEW_CONTENT. */
1712 void
1713 add_fixit_replace (const char *new_content);
1714
1715 /* Suggest replacing the content between the start and finish of
1716 WHERE with NEW_CONTENT. */
1717 void
1718 add_fixit_replace (location_t where,
1719 const char *new_content);
1720
1721 /* Suggest replacing the content covered by SRC_RANGE with
1722 NEW_CONTENT. */
1723 void
1724 add_fixit_replace (source_range src_range,
1725 const char *new_content);
1726
1727 unsigned int get_num_fixit_hints () const { return m_fixit_hints.count (); }
1728 fixit_hint *get_fixit_hint (int idx) const { return m_fixit_hints[idx]; }
1729 fixit_hint *get_last_fixit_hint () const;
1730 bool seen_impossible_fixit_p () const { return m_seen_impossible_fixit; }
1731
1732 /* Set this if the fix-it hints are not suitable to be
1733 automatically applied.
1734
1735 For example, if you are suggesting more than one
1736 mutually exclusive solution to a problem, then
1737 it doesn't make sense to apply all of the solutions;
1738 manual intervention is required.
1739
1740 If set, then the fix-it hints in the rich_location will
1741 be printed, but will not be added to generated patches,
1742 or affect the modified version of the file. */
1743 void fixits_cannot_be_auto_applied ()
1744 {
1745 m_fixits_cannot_be_auto_applied = true;
1746 }
1747
1748 bool fixits_can_be_auto_applied_p () const
1749 {
1750 return !m_fixits_cannot_be_auto_applied;
1751 }
1752
1753 private:
1754 bool reject_impossible_fixit (location_t where);
1755 void stop_supporting_fixits ();
1756 void maybe_add_fixit (location_t start,
1757 location_t next_loc,
1758 const char *new_content);
1759
1760 public:
1761 static const int STATICALLY_ALLOCATED_RANGES = 3;
1762
1763 protected:
1764 line_maps *m_line_table;
1765 semi_embedded_vec <location_range, STATICALLY_ALLOCATED_RANGES> m_ranges;
1766
1767 int m_column_override;
1768
1769 bool m_have_expanded_location;
1770 expanded_location m_expanded_location;
1771
1772 static const int MAX_STATIC_FIXIT_HINTS = 2;
1773 semi_embedded_vec <fixit_hint *, MAX_STATIC_FIXIT_HINTS> m_fixit_hints;
1774
1775 bool m_seen_impossible_fixit;
1776 bool m_fixits_cannot_be_auto_applied;
1777 };
1778
1779 /* A struct for the result of range_label::get_text: a NUL-terminated buffer
1780 of localized text, and a flag to determine if the caller should "free" the
1781 buffer. */
1782
1783 struct label_text
1784 {
1785 label_text ()
1786 : m_buffer (NULL), m_caller_owned (false)
1787 {}
1788
1789 label_text (char *buffer, bool caller_owned)
1790 : m_buffer (buffer), m_caller_owned (caller_owned)
1791 {}
1792
1793 void maybe_free ()
1794 {
1795 if (m_caller_owned)
1796 free (m_buffer);
1797 }
1798
1799 char *m_buffer;
1800 bool m_caller_owned;
1801 };
1802
1803 /* Abstract base class for labelling a range within a rich_location
1804 (e.g. for labelling expressions with their type).
1805
1806 Generating the text could require non-trivial work, so this work
1807 is delayed (via the "get_text" virtual function) until the diagnostic
1808 printing code "knows" it needs it, thus avoiding doing it e.g. for
1809 warnings that are filtered by command-line flags. This virtual
1810 function also isolates libcpp and the diagnostics subsystem from
1811 the front-end and middle-end-specific code for generating the text
1812 for the labels.
1813
1814 Like the rich_location instances they annotate, range_label instances
1815 are intended to be allocated on the stack when generating diagnostics,
1816 and to be short-lived. */
1817
1818 class range_label
1819 {
1820 public:
1821 virtual ~range_label () {}
1822
1823 /* Get localized text for the label.
1824 The RANGE_IDX is provided, allowing for range_label instances to be
1825 shared by multiple ranges if need be (the "flyweight" design pattern). */
1826 virtual label_text get_text (unsigned range_idx) const = 0;
1827 };
1828
1829 /* A fix-it hint: a suggested insertion, replacement, or deletion of text.
1830 We handle these three types of edit with one class, by representing
1831 them as replacement of a half-open range:
1832 [start, next_loc)
1833 Insertions have start == next_loc: "replace" the empty string at the
1834 start location with the new string.
1835 Deletions are replacement with the empty string.
1836
1837 There is only limited support for newline characters in fix-it hints
1838 as noted above in the comment for class rich_location.
1839 A fixit_hint instance can have at most one newline character; if
1840 present, the newline character must be the final character of
1841 the content (preventing e.g. fix-its that split a pre-existing line). */
1842
1843 class fixit_hint
1844 {
1845 public:
1846 fixit_hint (location_t start,
1847 location_t next_loc,
1848 const char *new_content);
1849 ~fixit_hint () { free (m_bytes); }
1850
1851 bool affects_line_p (const char *file, int line) const;
1852 location_t get_start_loc () const { return m_start; }
1853 location_t get_next_loc () const { return m_next_loc; }
1854 bool maybe_append (location_t start,
1855 location_t next_loc,
1856 const char *new_content);
1857
1858 const char *get_string () const { return m_bytes; }
1859 size_t get_length () const { return m_len; }
1860
1861 bool insertion_p () const { return m_start == m_next_loc; }
1862
1863 bool ends_with_newline_p () const;
1864
1865 private:
1866 /* We don't use source_range here since, unlike most places,
1867 this is a half-open/half-closed range:
1868 [start, next_loc)
1869 so that we can support insertion via start == next_loc. */
1870 location_t m_start;
1871 location_t m_next_loc;
1872 char *m_bytes;
1873 size_t m_len;
1874 };
1875
1876
1877 /* This is enum is used by the function linemap_resolve_location
1878 below. The meaning of the values is explained in the comment of
1879 that function. */
1880 enum location_resolution_kind
1881 {
1882 LRK_MACRO_EXPANSION_POINT,
1883 LRK_SPELLING_LOCATION,
1884 LRK_MACRO_DEFINITION_LOCATION
1885 };
1886
1887 /* Resolve a virtual location into either a spelling location, an
1888 expansion point location or a token argument replacement point
1889 location. Return the map that encodes the virtual location as well
1890 as the resolved location.
1891
1892 If LOC is *NOT* the location of a token resulting from the
1893 expansion of a macro, then the parameter LRK (which stands for
1894 Location Resolution Kind) is ignored and the resulting location
1895 just equals the one given in argument.
1896
1897 Now if LOC *IS* the location of a token resulting from the
1898 expansion of a macro, this is what happens.
1899
1900 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1901 -------------------------------
1902
1903 The virtual location is resolved to the first macro expansion point
1904 that led to this macro expansion.
1905
1906 * If LRK is set to LRK_SPELLING_LOCATION
1907 -------------------------------------
1908
1909 The virtual location is resolved to the locus where the token has
1910 been spelled in the source. This can follow through all the macro
1911 expansions that led to the token.
1912
1913 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1914 --------------------------------------
1915
1916 The virtual location is resolved to the locus of the token in the
1917 context of the macro definition.
1918
1919 If LOC is the locus of a token that is an argument of a
1920 function-like macro [replacing a parameter in the replacement list
1921 of the macro] the virtual location is resolved to the locus of the
1922 parameter that is replaced, in the context of the definition of the
1923 macro.
1924
1925 If LOC is the locus of a token that is not an argument of a
1926 function-like macro, then the function behaves as if LRK was set to
1927 LRK_SPELLING_LOCATION.
1928
1929 If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1930 returned location. Note that if the returned location wasn't originally
1931 encoded by a map, the *MAP is set to NULL. This can happen if LOC
1932 resolves to a location reserved for the client code, like
1933 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1934
1935 location_t linemap_resolve_location (struct line_maps *,
1936 location_t loc,
1937 enum location_resolution_kind lrk,
1938 const line_map_ordinary **loc_map);
1939
1940 /* Suppose that LOC is the virtual location of a token coming from the
1941 expansion of a macro M. This function then steps up to get the
1942 location L of the point where M got expanded. If L is a spelling
1943 location inside a macro expansion M', then this function returns
1944 the point where M' was expanded. LOC_MAP is an output parameter.
1945 When non-NULL, *LOC_MAP is set to the map of the returned
1946 location. */
1947 location_t linemap_unwind_toward_expansion (struct line_maps *,
1948 location_t loc,
1949 const struct line_map **loc_map);
1950
1951 /* If LOC is the virtual location of a token coming from the expansion
1952 of a macro M and if its spelling location is reserved (e.g, a
1953 location for a built-in token), then this function unwinds (using
1954 linemap_unwind_toward_expansion) the location until a location that
1955 is not reserved and is not in a system header is reached. In other
1956 words, this unwinds the reserved location until a location that is
1957 in real source code is reached.
1958
1959 Otherwise, if the spelling location for LOC is not reserved or if
1960 LOC doesn't come from the expansion of a macro, the function
1961 returns LOC as is and *MAP is not touched.
1962
1963 *MAP is set to the map of the returned location if the later is
1964 different from LOC. */
1965 location_t linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
1966 location_t loc,
1967 const struct line_map **map);
1968
1969 /* Expand source code location LOC and return a user readable source
1970 code location. LOC must be a spelling (non-virtual) location. If
1971 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1972 location is returned. */
1973 expanded_location linemap_expand_location (struct line_maps *,
1974 const struct line_map *,
1975 location_t loc);
1976
1977 /* Statistics about maps allocation and usage as returned by
1978 linemap_get_statistics. */
1979 struct linemap_stats
1980 {
1981 long num_ordinary_maps_allocated;
1982 long num_ordinary_maps_used;
1983 long ordinary_maps_allocated_size;
1984 long ordinary_maps_used_size;
1985 long num_expanded_macros;
1986 long num_macro_tokens;
1987 long num_macro_maps_used;
1988 long macro_maps_allocated_size;
1989 long macro_maps_used_size;
1990 long macro_maps_locations_size;
1991 long duplicated_macro_maps_locations_size;
1992 long adhoc_table_size;
1993 long adhoc_table_entries_used;
1994 };
1995
1996 /* Return the highest location emitted for a given file for which
1997 there is a line map in SET. FILE_NAME is the file name to
1998 consider. If the function returns TRUE, *LOC is set to the highest
1999 location emitted for that file. */
2000 bool linemap_get_file_highest_location (struct line_maps * set,
2001 const char *file_name,
2002 location_t *loc);
2003
2004 /* Compute and return statistics about the memory consumption of some
2005 parts of the line table SET. */
2006 void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
2007
2008 /* Dump debugging information about source location LOC into the file
2009 stream STREAM. SET is the line map set LOC comes from. */
2010 void linemap_dump_location (struct line_maps *, location_t, FILE *);
2011
2012 /* Dump line map at index IX in line table SET to STREAM. If STREAM
2013 is NULL, use stderr. IS_MACRO is true if the caller wants to
2014 dump a macro map, false otherwise. */
2015 void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
2016
2017 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
2018 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
2019 specifies how many macro maps to dump. */
2020 void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
2021
2022 /* An enum for distinguishing the various parts within a location_t. */
2023
2024 enum location_aspect
2025 {
2026 LOCATION_ASPECT_CARET,
2027 LOCATION_ASPECT_START,
2028 LOCATION_ASPECT_FINISH
2029 };
2030
2031 /* The rich_location class requires a way to expand location_t instances.
2032 We would directly use expand_location_to_spelling_point, which is
2033 implemented in gcc/input.c, but we also need to use it for rich_location
2034 within genmatch.c.
2035 Hence we require client code of libcpp to implement the following
2036 symbol. */
2037 extern expanded_location
2038 linemap_client_expand_location_to_spelling_point (location_t,
2039 enum location_aspect);
2040
2041 #endif /* !LIBCPP_LINE_MAP_H */