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