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