gcc:
[official-gcc.git] / libcpp / include / line-map.h
blobe74ccbb5703e869806638f29c16c0067c2b68108
1 /* Map (unsigned int) keys to (source file, line, column) triples.
2 Copyright (C) 2001-2018 Free Software Foundation, Inc.
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.
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.
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/>.
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! */
22 #ifndef LIBCPP_LINE_MAP_H
23 #define LIBCPP_LINE_MAP_H
25 #ifndef GTY
26 #define GTY(x) /* nothing */
27 #endif
29 /* Both gcc and emacs number source *lines* starting at 1, but
30 they have differing conventions for *columns*.
32 GCC uses a 1-based convention for source columns,
33 whereas Emacs's M-x column-number-mode uses a 0-based convention.
35 For example, an error in the initial, left-hand
36 column of source line 3 is reported by GCC as:
38 some-file.c:3:1: error: ...etc...
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:
45 some-file.c 10% (3, 0)
47 i.e. "3:1:" in GCC corresponds to "(3, 0)" in Emacs. */
49 /* The type of line numbers. */
50 typedef unsigned int linenum_type;
52 /* A function for for use by qsort for comparing line numbers. */
54 inline int compare (linenum_type lhs, linenum_type rhs)
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;
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
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. */
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.
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.
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:
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)
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".
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.
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.
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.
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.
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.
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 -----------+-------------------------------+-------------------------------
163 | VVVVVVVVVVVVVVVVVVVVVVVVVVV
164 | Ordinary maps grow this way
166 | (unallocated integers)
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.
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).
176 | (unallocated integers)
178 | Macro maps grow this way
179 | ^^^^^^^^^^^^^^^^^^^^^^^^
181 -----------+-------------------------------+-------------------------------
182 | LINEMAPS_MACRO_LOWEST_LOCATION| Locations within macro maps
183 | macromap[m-1]->start_location | Start of last macro map
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 -----------+-------------------------------+-------------------------------
199 Examples of location encoding.
201 Packed ranges
202 =============
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:
207 11111111112
208 12345678901234567890
210 523 return foo + bar;
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).
218 Line 523 is offset 23 from the starting line of the ordinary line_map.
220 caret == start, and the offset of the finish fits within 5 bits, so
221 this can be stored as a packed range.
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).
239 "Pure" locations
240 ================
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
255 Unoptimized ranges
256 ==================
258 Consider encoding the location of the binary expression
259 below:
261 11111111112
262 12345678901234567890
264 523 return foo + bar;
265 ~~~~^~~~~
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.
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.
275 Stripping off the top bit gives us an index into the ad-hoc
276 lookaside table:
278 line_table->location_adhoc_data_map.data[0x3]
280 from which the caret, start and finish can be looked up,
281 encoded as "pure" locations:
283 start == ordmap->start + (23 << 12) + (11 << 5)
284 == ordmap->start + 0x17160 (as above; the "f" of "foo")
286 caret == ordmap->start + (23 << 12) + (15 << 5)
287 == ordmap->start + 0x171e0
289 finish == ordmap->start + (23 << 12) + (19 << 5)
290 == ordmap->start + 0x17260
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;
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);
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;
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;
311 /* Highest possible source location encoded within an ordinary map. */
312 const source_location LINE_MAP_MAX_LOCATION = 0x70000000;
314 /* A range of source locations.
316 Ranges are closed:
317 m_start is the first location within the range,
318 m_finish is the last location within the range.
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
324 source_location m_start;
325 source_location m_finish;
327 /* We avoid using constructors, since various structs that
328 don't yet have constructors will embed instances of
329 source_range. */
331 /* Make a source_range from a source_location. */
332 static source_range from_location (source_location loc)
334 source_range result;
335 result.m_start = loc;
336 result.m_finish = loc;
337 return result;
340 /* Make a source_range from a pair of source_location. */
341 static source_range from_locations (source_location start,
342 source_location finish)
344 source_range result;
345 result.m_start = start;
346 result.m_finish = finish;
347 return result;
351 /* Memory allocation function typedef. Works like xrealloc. */
352 typedef void *(*line_map_realloc) (void *, size_t);
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);
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.
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:
365 #define PLUS(A,B) A + B
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.
372 On the other hand, if we later do:
374 int a = PLUS (1,2);
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. */
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;
390 /* Size and alignment is (usually) 4 bytes. */
393 /* An ordinary line map encodes physical source locations. Those
394 physical source locations are called "spelling locations".
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".)
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. */
408 /* 4 bytes of integers, each 1 byte for easy extraction/insertion. */
410 /* The reason for creation of this line map. */
411 ENUM_BITFIELD (lc_reason) reason : 8;
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;
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;
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;
438 /* Pointer alignment boundary on both 32 and 64-bit systems. */
440 const char *to_file;
441 linenum_type to_line;
443 /* Location from whence this line map was included. For regular
444 #includes, this location will be the last location of a map. For
445 outermost file, this is 0. */
446 source_location included_from;
448 /* Size is 20 or 24 bytes, no padding */
451 /* This is the highest possible source location encoded within an
452 ordinary or macro map. */
453 const source_location MAX_SOURCE_LOCATION = 0x7FFFFFFF;
455 struct cpp_hashnode;
457 /* A macro line map encodes location of tokens coming from a macro
458 expansion.
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. */
465 /* The number of tokens inside the replacement-list of MACRO. */
466 unsigned int n_tokens;
468 /* Pointer alignment boundary. */
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;
477 /* This array of location is actually an array of pairs of
478 locations. The elements inside it thus look like:
480 x0,y0, x1,y1, x2,y2, ...., xn,yn.
482 where n == n_tokens;
484 Remember that these xI,yI are collected when libcpp is about to
485 expand a given macro.
487 yI is the location in the macro definition, either of the token
488 itself or of a macro parameter that it replaces.
490 Imagine this:
492 #define PLUS(A, B) A + B <--- #1
494 int a = PLUS (1,2); <--- #2
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:
502 A, +, B
504 and the expansion-list is made of the tokens:
506 1, +, 2
508 Let's consider the case of token "+". Its y1 [yI for I == 1] is
509 its spelling location in #1.
511 y0 (thus for token "1") is the spelling location of A in #1.
513 And y2 (of token "2") is the spelling location of B in #1.
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.
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.
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;
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;
539 /* Size is 20 or 32 (4 bytes padding on 64-bit). */
542 #if CHECKING_P && (GCC_VERSION >= 2007)
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)
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:
557 if (linemap_assert_fails(EXPR)) handle_error(); */
558 #define linemap_assert_fails(EXPR) __extension__ \
559 ({linemap_assert (EXPR); false;})
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
567 /* Categorize line map kinds. */
569 inline bool
570 MAP_ORDINARY_P (const line_map *map)
572 return map->start_location < LINE_MAP_MAX_LOCATION;
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 *);
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 *. */
584 inline line_map_ordinary *
585 linemap_check_ordinary (struct line_map *map)
587 linemap_assert (MAP_ORDINARY_P (map));
588 return (line_map_ordinary *)map;
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 *. */
595 inline const line_map_ordinary *
596 linemap_check_ordinary (const struct line_map *map)
598 linemap_assert (MAP_ORDINARY_P (map));
599 return (const line_map_ordinary *)map;
602 /* Assert that MAP is a macro expansion and downcast to the appropriate
603 subclass. */
605 inline line_map_macro *linemap_check_macro (line_map *map)
607 linemap_assert (!MAP_ORDINARY_P (map));
608 return (line_map_macro *)map;
611 /* Assert that MAP is a macro expansion and downcast to the appropriate
612 subclass. */
614 inline const line_map_macro *
615 linemap_check_macro (const line_map *map)
617 linemap_assert (!MAP_ORDINARY_P (map));
618 return (const line_map_macro *)map;
621 /* Read the start location of MAP. */
623 inline source_location
624 MAP_START_LOCATION (const line_map *map)
626 return map->start_location;
629 /* Get the starting line number of ordinary map MAP. */
631 inline linenum_type
632 ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map_ordinary *ord_map)
634 return ord_map->to_line;
637 /* Return a positive value if map encodes locations from a system
638 header, 0 otherwise. Returns 1 if ordinary map MAP encodes locations
639 in a system header and 2 if it encodes locations in a C system header
640 that therefore needs to be extern "C" protected in C++. */
642 inline unsigned char
643 ORDINARY_MAP_IN_SYSTEM_HEADER_P (const line_map_ordinary *ord_map)
645 return ord_map->sysp;
648 /* Get the filename of ordinary map MAP. */
650 inline const char *
651 ORDINARY_MAP_FILE_NAME (const line_map_ordinary *ord_map)
653 return ord_map->to_file;
656 /* Get the cpp macro whose expansion gave birth to macro map MAP. */
658 inline cpp_hashnode *
659 MACRO_MAP_MACRO (const line_map_macro *macro_map)
661 return macro_map->macro;
664 /* Get the number of tokens inside the replacement-list of the macro
665 that led to macro map MAP. */
667 inline unsigned int
668 MACRO_MAP_NUM_MACRO_TOKENS (const line_map_macro *macro_map)
670 return macro_map->n_tokens;
673 /* Get the array of pairs of locations within macro map MAP.
674 See the declaration of line_map_macro for more information. */
676 inline source_location *
677 MACRO_MAP_LOCATIONS (const line_map_macro *macro_map)
679 return macro_map->macro_locations;
682 /* Get the location of the expansion point of the macro map MAP. */
684 inline source_location
685 MACRO_MAP_EXPANSION_POINT_LOCATION (const line_map_macro *macro_map)
687 return macro_map->expansion;
690 /* The abstraction of a set of location maps. There can be several
691 types of location maps. This abstraction contains the attributes
692 that are independent from the type of the map.
694 Essentially this is just a vector of T_linemap_subclass,
695 which can only ever grow in size. */
697 struct GTY(()) maps_info_ordinary {
698 /* This array contains the "ordinary" line maps, for all
699 events other than macro expansion
700 (e.g. when a new preprocessing unit starts or ends). */
701 line_map_ordinary * GTY ((length ("%h.used"))) maps;
703 /* The total number of allocated maps. */
704 unsigned int allocated;
706 /* The number of elements used in maps. This number is smaller
707 or equal to ALLOCATED. */
708 unsigned int used;
710 unsigned int cache;
713 struct GTY(()) maps_info_macro {
714 /* This array contains the macro line maps.
715 A macro line map is created whenever a macro expansion occurs. */
716 line_map_macro * GTY ((length ("%h.used"))) maps;
718 /* The total number of allocated maps. */
719 unsigned int allocated;
721 /* The number of elements used in maps. This number is smaller
722 or equal to ALLOCATED. */
723 unsigned int used;
725 unsigned int cache;
728 /* Data structure to associate a source_range together with an arbitrary
729 data pointer with a source location. */
730 struct GTY(()) location_adhoc_data {
731 source_location locus;
732 source_range src_range;
733 void * GTY((skip)) data;
736 struct htab;
738 /* The following data structure encodes a location with some adhoc data
739 and maps it to a new unsigned integer (called an adhoc location)
740 that replaces the original location to represent the mapping.
742 The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
743 highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
744 the original location. Once identified as the adhoc_loc, the lower 31
745 bits of the integer is used to index the location_adhoc_data array,
746 in which the locus and associated data is stored. */
748 struct GTY(()) location_adhoc_data_map {
749 struct htab * GTY((skip)) htab;
750 source_location curr_loc;
751 unsigned int allocated;
752 struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
755 /* A set of chronological line_map structures. */
756 struct GTY(()) line_maps {
758 ~line_maps ();
760 maps_info_ordinary info_ordinary;
762 maps_info_macro info_macro;
764 /* Depth of the include stack, including the current file. */
765 unsigned int depth;
767 /* If true, prints an include trace a la -H. */
768 bool trace_includes;
770 /* Highest source_location "given out". */
771 source_location highest_location;
773 /* Start of line of highest source_location "given out". */
774 source_location highest_line;
776 /* The maximum column number we can quickly allocate. Higher numbers
777 may require allocating a new line_map. */
778 unsigned int max_column_hint;
780 /* If non-null, the allocator to use when resizing 'maps'. If null,
781 xrealloc is used. */
782 line_map_realloc reallocator;
784 /* The allocators' function used to know the actual size it
785 allocated, for a certain allocation size requested. */
786 line_map_round_alloc_size_func round_alloc_size;
788 struct location_adhoc_data_map location_adhoc_data_map;
790 /* The special location value that is used as spelling location for
791 built-in tokens. */
792 source_location builtin_location;
794 /* True if we've seen a #line or # 44 "file" directive. */
795 bool seen_line_directive;
797 /* The default value of range_bits in ordinary line maps. */
798 unsigned int default_range_bits;
800 unsigned int num_optimized_ranges;
801 unsigned int num_unoptimized_ranges;
804 /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
805 if we are interested in macro maps, FALSE otherwise. */
806 inline unsigned int
807 LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind)
809 if (map_kind)
810 return set->info_macro.allocated;
811 else
812 return set->info_ordinary.allocated;
815 /* As above, but by reference (e.g. as an lvalue). */
817 inline unsigned int &
818 LINEMAPS_ALLOCATED (line_maps *set, bool map_kind)
820 if (map_kind)
821 return set->info_macro.allocated;
822 else
823 return set->info_ordinary.allocated;
826 /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
827 we are interested in macro maps, FALSE otherwise.*/
828 inline unsigned int
829 LINEMAPS_USED (const line_maps *set, bool map_kind)
831 if (map_kind)
832 return set->info_macro.used;
833 else
834 return set->info_ordinary.used;
837 /* As above, but by reference (e.g. as an lvalue). */
839 inline unsigned int &
840 LINEMAPS_USED (line_maps *set, bool map_kind)
842 if (map_kind)
843 return set->info_macro.used;
844 else
845 return set->info_ordinary.used;
848 /* Returns the index of the last map that was looked up with
849 linemap_lookup. MAP_KIND shall be TRUE if we are interested in
850 macro maps, FALSE otherwise. */
851 inline unsigned int
852 LINEMAPS_CACHE (const line_maps *set, bool map_kind)
854 if (map_kind)
855 return set->info_macro.cache;
856 else
857 return set->info_ordinary.cache;
860 /* As above, but by reference (e.g. as an lvalue). */
862 inline unsigned int &
863 LINEMAPS_CACHE (line_maps *set, bool map_kind)
865 if (map_kind)
866 return set->info_macro.cache;
867 else
868 return set->info_ordinary.cache;
871 /* Return the map at a given index. */
872 inline line_map *
873 LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, int index)
875 if (map_kind)
876 return &set->info_macro.maps[index];
877 else
878 return &set->info_ordinary.maps[index];
881 /* Returns the last map used in the line table SET. MAP_KIND
882 shall be TRUE if we are interested in macro maps, FALSE
883 otherwise.*/
884 inline line_map *
885 LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind)
887 return LINEMAPS_MAP_AT (set, map_kind,
888 LINEMAPS_USED (set, map_kind) - 1);
891 /* Returns the last map that was allocated in the line table SET.
892 MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
893 otherwise.*/
894 inline line_map *
895 LINEMAPS_LAST_ALLOCATED_MAP (const line_maps *set, bool map_kind)
897 return LINEMAPS_MAP_AT (set, map_kind,
898 LINEMAPS_ALLOCATED (set, map_kind) - 1);
901 /* Returns a pointer to the memory region where ordinary maps are
902 allocated in the line table SET. */
903 inline line_map_ordinary *
904 LINEMAPS_ORDINARY_MAPS (const line_maps *set)
906 return set->info_ordinary.maps;
909 /* Returns the INDEXth ordinary map. */
910 inline line_map_ordinary *
911 LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, int index)
913 linemap_assert (index >= 0);
914 linemap_assert ((unsigned int)index < set->info_ordinary.used);
915 return &set->info_ordinary.maps[index];
918 /* Return the number of ordinary maps allocated in the line table
919 SET. */
920 inline unsigned int
921 LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set)
923 return LINEMAPS_ALLOCATED (set, false);
926 /* Return the number of ordinary maps used in the line table SET. */
927 inline unsigned int
928 LINEMAPS_ORDINARY_USED (const line_maps *set)
930 return LINEMAPS_USED (set, false);
933 /* Return the index of the last ordinary map that was looked up with
934 linemap_lookup. */
935 inline unsigned int
936 LINEMAPS_ORDINARY_CACHE (const line_maps *set)
938 return LINEMAPS_CACHE (set, false);
941 /* As above, but by reference (e.g. as an lvalue). */
943 inline unsigned int &
944 LINEMAPS_ORDINARY_CACHE (line_maps *set)
946 return LINEMAPS_CACHE (set, false);
949 /* Returns a pointer to the last ordinary map used in the line table
950 SET. */
951 inline line_map_ordinary *
952 LINEMAPS_LAST_ORDINARY_MAP (const line_maps *set)
954 return (line_map_ordinary *)LINEMAPS_LAST_MAP (set, false);
957 /* Returns a pointer to the last ordinary map allocated the line table
958 SET. */
959 inline line_map_ordinary *
960 LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP (const line_maps *set)
962 return (line_map_ordinary *)LINEMAPS_LAST_ALLOCATED_MAP (set, false);
965 /* Returns a pointer to the beginning of the region where macro maps
966 are allocated. */
967 inline line_map_macro *
968 LINEMAPS_MACRO_MAPS (const line_maps *set)
970 return set->info_macro.maps;
973 /* Returns the INDEXth macro map. */
974 inline line_map_macro *
975 LINEMAPS_MACRO_MAP_AT (const line_maps *set, int index)
977 linemap_assert (index >= 0);
978 linemap_assert ((unsigned int)index < set->info_macro.used);
979 return &set->info_macro.maps[index];
982 /* Returns the number of macro maps that were allocated in the line
983 table SET. */
984 inline unsigned int
985 LINEMAPS_MACRO_ALLOCATED (const line_maps *set)
987 return LINEMAPS_ALLOCATED (set, true);
990 /* Returns the number of macro maps used in the line table SET. */
991 inline unsigned int
992 LINEMAPS_MACRO_USED (const line_maps *set)
994 return LINEMAPS_USED (set, true);
997 /* Returns the index of the last macro map looked up with
998 linemap_lookup. */
999 inline unsigned int
1000 LINEMAPS_MACRO_CACHE (const line_maps *set)
1002 return LINEMAPS_CACHE (set, true);
1005 /* As above, but by reference (e.g. as an lvalue). */
1007 inline unsigned int &
1008 LINEMAPS_MACRO_CACHE (line_maps *set)
1010 return LINEMAPS_CACHE (set, true);
1013 /* Returns the last macro map used in the line table SET. */
1014 inline line_map_macro *
1015 LINEMAPS_LAST_MACRO_MAP (const line_maps *set)
1017 return (line_map_macro *)LINEMAPS_LAST_MAP (set, true);
1020 /* Returns the lowest location [of a token resulting from macro
1021 expansion] encoded in this line table. */
1022 inline source_location
1023 LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps *set)
1025 return LINEMAPS_MACRO_USED (set)
1026 ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))
1027 : MAX_SOURCE_LOCATION;
1030 /* Returns the last macro map allocated in the line table SET. */
1031 inline line_map_macro *
1032 LINEMAPS_LAST_ALLOCATED_MACRO_MAP (const line_maps *set)
1034 return (line_map_macro *)LINEMAPS_LAST_ALLOCATED_MAP (set, true);
1037 extern source_location get_combined_adhoc_loc (struct line_maps *,
1038 source_location,
1039 source_range,
1040 void *);
1041 extern void *get_data_from_adhoc_loc (struct line_maps *, source_location);
1042 extern source_location get_location_from_adhoc_loc (struct line_maps *,
1043 source_location);
1045 extern source_range get_range_from_loc (line_maps *set, source_location loc);
1047 /* Get whether location LOC is an ad-hoc location. */
1049 inline bool
1050 IS_ADHOC_LOC (source_location loc)
1052 return (loc & MAX_SOURCE_LOCATION) != loc;
1055 /* Get whether location LOC is a "pure" location, or
1056 whether it is an ad-hoc location, or embeds range information. */
1058 bool
1059 pure_location_p (line_maps *set, source_location loc);
1061 /* Given location LOC within SET, strip away any packed range information
1062 or ad-hoc information. */
1064 extern source_location get_pure_location (line_maps *set,
1065 source_location loc);
1067 /* Combine LOC and BLOCK, giving a combined adhoc location. */
1069 inline source_location
1070 COMBINE_LOCATION_DATA (struct line_maps *set,
1071 source_location loc,
1072 source_range src_range,
1073 void *block)
1075 return get_combined_adhoc_loc (set, loc, src_range, block);
1078 extern void rebuild_location_adhoc_htab (struct line_maps *);
1080 /* Initialize a line map set. SET is the line map set to initialize
1081 and BUILTIN_LOCATION is the special location value to be used as
1082 spelling location for built-in tokens. This BUILTIN_LOCATION has
1083 to be strictly less than RESERVED_LOCATION_COUNT. */
1084 extern void linemap_init (struct line_maps *set,
1085 source_location builtin_location);
1087 /* Check for and warn about line_maps entered but not exited. */
1089 extern void linemap_check_files_exited (struct line_maps *);
1091 /* Return a source_location for the start (i.e. column==0) of
1092 (physical) line TO_LINE in the current source file (as in the
1093 most recent linemap_add). MAX_COLUMN_HINT is the highest column
1094 number we expect to use in this line (but it does not change
1095 the highest_location). */
1097 extern source_location linemap_line_start
1098 (struct line_maps *set, linenum_type to_line, unsigned int max_column_hint);
1100 /* Add a mapping of logical source line to physical source file and
1101 line number. This function creates an "ordinary map", which is a
1102 map that records locations of tokens that are not part of macro
1103 replacement-lists present at a macro expansion point.
1105 The text pointed to by TO_FILE must have a lifetime
1106 at least as long as the lifetime of SET. An empty
1107 TO_FILE means standard input. If reason is LC_LEAVE, and
1108 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
1109 natural values considering the file we are returning to.
1111 A call to this function can relocate the previous set of
1112 maps, so any stored line_map pointers should not be used. */
1113 extern const struct line_map *linemap_add
1114 (struct line_maps *, enum lc_reason, unsigned int sysp,
1115 const char *to_file, linenum_type to_line);
1117 /* Given a logical source location, returns the map which the
1118 corresponding (source file, line, column) triplet can be deduced
1119 from. Since the set is built chronologically, the logical lines are
1120 monotonic increasing, and so the list is sorted and we can use a
1121 binary search. If no line map have been allocated yet, this
1122 function returns NULL. */
1123 extern const struct line_map *linemap_lookup
1124 (struct line_maps *, source_location);
1126 /* Returns TRUE if the line table set tracks token locations across
1127 macro expansion, FALSE otherwise. */
1128 bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
1130 /* Return the name of the macro associated to MACRO_MAP. */
1131 const char* linemap_map_get_macro_name (const line_map_macro *);
1133 /* Return a positive value if LOCATION is the locus of a token that is
1134 located in a system header, O otherwise. It returns 1 if LOCATION
1135 is the locus of a token that is located in a system header, and 2
1136 if LOCATION is the locus of a token located in a C system header
1137 that therefore needs to be extern "C" protected in C++.
1139 Note that this function returns 1 if LOCATION belongs to a token
1140 that is part of a macro replacement-list defined in a system
1141 header, but expanded in a non-system file. */
1142 int linemap_location_in_system_header_p (struct line_maps *,
1143 source_location);
1145 /* Return TRUE if LOCATION is a source code location of a token that is part of
1146 a macro expansion, FALSE otherwise. */
1147 bool linemap_location_from_macro_expansion_p (const struct line_maps *,
1148 source_location);
1150 /* TRUE if LOCATION is a source code location of a token that is part of the
1151 definition of a macro, FALSE otherwise. */
1152 bool linemap_location_from_macro_definition_p (struct line_maps *,
1153 source_location);
1155 /* With the precondition that LOCATION is the locus of a token that is
1156 an argument of a function-like macro MACRO_MAP and appears in the
1157 expansion of MACRO_MAP, return the locus of that argument in the
1158 context of the caller of MACRO_MAP. */
1160 extern source_location linemap_macro_map_loc_unwind_toward_spelling
1161 (line_maps *set, const line_map_macro *macro_map, source_location location);
1163 /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
1164 be reserved for libcpp user as special values, no token from libcpp
1165 will contain any of those locations. */
1166 const source_location RESERVED_LOCATION_COUNT = 2;
1168 /* Converts a map and a source_location to source line. */
1169 inline linenum_type
1170 SOURCE_LINE (const line_map_ordinary *ord_map, source_location loc)
1172 return ((loc - ord_map->start_location)
1173 >> ord_map->m_column_and_range_bits) + ord_map->to_line;
1176 /* Convert a map and source_location to source column number. */
1177 inline linenum_type
1178 SOURCE_COLUMN (const line_map_ordinary *ord_map, source_location loc)
1180 return ((loc - ord_map->start_location)
1181 & ((1 << ord_map->m_column_and_range_bits) - 1)) >> ord_map->m_range_bits;
1185 inline source_location
1186 linemap_included_from (const line_map_ordinary *ord_map)
1188 return ord_map->included_from;
1191 /* The linemap containing the included-from location of MAP. */
1192 const line_map_ordinary *linemap_included_from_linemap
1193 (line_maps *set, const line_map_ordinary *map);
1195 /* True if the map is at the bottom of the include stack. */
1197 inline bool
1198 MAIN_FILE_P (const line_map_ordinary *ord_map)
1200 return ord_map->included_from == 0;
1203 /* Encode and return a source_location from a column number. The
1204 source line considered is the last source line used to call
1205 linemap_line_start, i.e, the last source line which a location was
1206 encoded from. */
1207 extern source_location
1208 linemap_position_for_column (struct line_maps *, unsigned int);
1210 /* Encode and return a source location from a given line and
1211 column. */
1212 source_location
1213 linemap_position_for_line_and_column (line_maps *set,
1214 const line_map_ordinary *,
1215 linenum_type, unsigned int);
1217 /* Encode and return a source_location starting from location LOC and
1218 shifting it by OFFSET columns. This function does not support
1219 virtual locations. */
1220 source_location
1221 linemap_position_for_loc_and_offset (struct line_maps *set,
1222 source_location loc,
1223 unsigned int offset);
1225 /* Return the file this map is for. */
1226 inline const char *
1227 LINEMAP_FILE (const line_map_ordinary *ord_map)
1229 return ord_map->to_file;
1232 /* Return the line number this map started encoding location from. */
1233 inline linenum_type
1234 LINEMAP_LINE (const line_map_ordinary *ord_map)
1236 return ord_map->to_line;
1239 /* Return a positive value if map encodes locations from a system
1240 header, 0 otherwise. Returns 1 if MAP encodes locations in a
1241 system header and 2 if it encodes locations in a C system header
1242 that therefore needs to be extern "C" protected in C++. */
1243 inline unsigned char
1244 LINEMAP_SYSP (const line_map_ordinary *ord_map)
1246 return ord_map->sysp;
1249 /* Return a positive value if PRE denotes the location of a token that
1250 comes before the token of POST, 0 if PRE denotes the location of
1251 the same token as the token for POST, and a negative value
1252 otherwise. */
1253 int linemap_compare_locations (struct line_maps *set,
1254 source_location pre,
1255 source_location post);
1257 /* Return TRUE if LOC_A denotes the location a token that comes
1258 topogically before the token denoted by location LOC_B, or if they
1259 are equal. */
1260 inline bool
1261 linemap_location_before_p (struct line_maps *set,
1262 source_location loc_a,
1263 source_location loc_b)
1265 return linemap_compare_locations (set, loc_a, loc_b) >= 0;
1268 typedef struct
1270 /* The name of the source file involved. */
1271 const char *file;
1273 /* The line-location in the source file. */
1274 int line;
1276 int column;
1278 void *data;
1280 /* In a system header?. */
1281 bool sysp;
1282 } expanded_location;
1284 class range_label;
1286 /* A hint to diagnostic_show_locus on how to print a source range within a
1287 rich_location.
1289 Typically this is SHOW_RANGE_WITH_CARET for the 0th range, and
1290 SHOW_RANGE_WITHOUT_CARET for subsequent ranges,
1291 but the Fortran frontend uses SHOW_RANGE_WITH_CARET repeatedly for
1292 printing things like:
1294 x = x + y
1296 Error: Shapes for operands at (1) and (2) are not conformable
1298 where "1" and "2" are notionally carets. */
1300 enum range_display_kind
1302 /* Show the pertinent source line(s), the caret, and underline(s). */
1303 SHOW_RANGE_WITH_CARET,
1305 /* Show the pertinent source line(s) and underline(s), but don't
1306 show the caret (just an underline). */
1307 SHOW_RANGE_WITHOUT_CARET,
1309 /* Just show the source lines; don't show the range itself.
1310 This is for use when displaying some line-insertion fix-it hints (for
1311 showing the user context on the change, for when it doesn't make sense
1312 to highlight the first column on the next line). */
1313 SHOW_LINES_WITHOUT_RANGE
1316 /* A location within a rich_location: a caret&range, with
1317 the caret potentially flagged for display, and an optional
1318 label. */
1320 struct location_range
1322 source_location m_loc;
1324 enum range_display_kind m_range_display_kind;
1326 /* If non-NULL, the label for this range. */
1327 const range_label *m_label;
1330 /* A partially-embedded vec for use within rich_location for storing
1331 ranges and fix-it hints.
1333 Elements [0..NUM_EMBEDDED) are allocated within m_embed, after
1334 that they are within the dynamically-allocated m_extra.
1336 This allows for static allocation in the common case, whilst
1337 supporting the rarer case of an arbitrary number of elements.
1339 Dynamic allocation is not performed unless it's needed. */
1341 template <typename T, int NUM_EMBEDDED>
1342 class semi_embedded_vec
1344 public:
1345 semi_embedded_vec ();
1346 ~semi_embedded_vec ();
1348 unsigned int count () const { return m_num; }
1349 T& operator[] (int idx);
1350 const T& operator[] (int idx) const;
1352 void push (const T&);
1353 void truncate (int len);
1355 private:
1356 int m_num;
1357 T m_embedded[NUM_EMBEDDED];
1358 int m_alloc;
1359 T *m_extra;
1362 /* Constructor for semi_embedded_vec. In particular, no dynamic allocation
1363 is done. */
1365 template <typename T, int NUM_EMBEDDED>
1366 semi_embedded_vec<T, NUM_EMBEDDED>::semi_embedded_vec ()
1367 : m_num (0), m_alloc (0), m_extra (NULL)
1371 /* semi_embedded_vec's dtor. Release any dynamically-allocated memory. */
1373 template <typename T, int NUM_EMBEDDED>
1374 semi_embedded_vec<T, NUM_EMBEDDED>::~semi_embedded_vec ()
1376 XDELETEVEC (m_extra);
1379 /* Look up element IDX, mutably. */
1381 template <typename T, int NUM_EMBEDDED>
1383 semi_embedded_vec<T, NUM_EMBEDDED>::operator[] (int idx)
1385 linemap_assert (idx < m_num);
1386 if (idx < NUM_EMBEDDED)
1387 return m_embedded[idx];
1388 else
1390 linemap_assert (m_extra != NULL);
1391 return m_extra[idx - NUM_EMBEDDED];
1395 /* Look up element IDX (const). */
1397 template <typename T, int NUM_EMBEDDED>
1398 const T&
1399 semi_embedded_vec<T, NUM_EMBEDDED>::operator[] (int idx) const
1401 linemap_assert (idx < m_num);
1402 if (idx < NUM_EMBEDDED)
1403 return m_embedded[idx];
1404 else
1406 linemap_assert (m_extra != NULL);
1407 return m_extra[idx - NUM_EMBEDDED];
1411 /* Append VALUE to the end of the semi_embedded_vec. */
1413 template <typename T, int NUM_EMBEDDED>
1414 void
1415 semi_embedded_vec<T, NUM_EMBEDDED>::push (const T& value)
1417 int idx = m_num++;
1418 if (idx < NUM_EMBEDDED)
1419 m_embedded[idx] = value;
1420 else
1422 /* Offset "idx" to be an index within m_extra. */
1423 idx -= NUM_EMBEDDED;
1424 if (NULL == m_extra)
1426 linemap_assert (m_alloc == 0);
1427 m_alloc = 16;
1428 m_extra = XNEWVEC (T, m_alloc);
1430 else if (idx >= m_alloc)
1432 linemap_assert (m_alloc > 0);
1433 m_alloc *= 2;
1434 m_extra = XRESIZEVEC (T, m_extra, m_alloc);
1436 linemap_assert (m_extra);
1437 linemap_assert (idx < m_alloc);
1438 m_extra[idx] = value;
1442 /* Truncate to length LEN. No deallocation is performed. */
1444 template <typename T, int NUM_EMBEDDED>
1445 void
1446 semi_embedded_vec<T, NUM_EMBEDDED>::truncate (int len)
1448 linemap_assert (len <= m_num);
1449 m_num = len;
1452 class fixit_hint;
1454 /* A "rich" source code location, for use when printing diagnostics.
1455 A rich_location has one or more carets&ranges, where the carets
1456 are optional. These are referred to as "ranges" from here.
1457 Typically the zeroth range has a caret; other ranges sometimes
1458 have carets.
1460 The "primary" location of a rich_location is the caret of range 0,
1461 used for determining the line/column when printing diagnostic
1462 text, such as:
1464 some-file.c:3:1: error: ...etc...
1466 Additional ranges may be added to help the user identify other
1467 pertinent clauses in a diagnostic.
1469 Ranges can (optionally) be given labels via class range_label.
1471 rich_location instances are intended to be allocated on the stack
1472 when generating diagnostics, and to be short-lived.
1474 Examples of rich locations
1475 --------------------------
1477 Example A
1478 *********
1479 int i = "foo";
1481 This "rich" location is simply a single range (range 0), with
1482 caret = start = finish at the given point.
1484 Example B
1485 *********
1486 a = (foo && bar)
1487 ~~~~~^~~~~~~
1488 This rich location has a single range (range 0), with the caret
1489 at the first "&", and the start/finish at the parentheses.
1490 Compare with example C below.
1492 Example C
1493 *********
1494 a = (foo && bar)
1495 ~~~ ^~ ~~~
1496 This rich location has three ranges:
1497 - Range 0 has its caret and start location at the first "&" and
1498 end at the second "&.
1499 - Range 1 has its start and finish at the "f" and "o" of "foo";
1500 the caret is not flagged for display, but is perhaps at the "f"
1501 of "foo".
1502 - Similarly, range 2 has its start and finish at the "b" and "r" of
1503 "bar"; the caret is not flagged for display, but is perhaps at the
1504 "b" of "bar".
1505 Compare with example B above.
1507 Example D (Fortran frontend)
1508 ****************************
1509 x = x + y
1511 This rich location has range 0 at "1", and range 1 at "2".
1512 Both are flagged for caret display. Both ranges have start/finish
1513 equal to their caret point. The frontend overrides the diagnostic
1514 context's default caret character for these ranges.
1516 Example E (range labels)
1517 ************************
1518 printf ("arg0: %i arg1: %s arg2: %i",
1521 const char *
1522 100, 101, 102);
1526 This rich location has two ranges:
1527 - range 0 is at the "%s" with start = caret = "%" and finish at
1528 the "s". It has a range_label ("const char *").
1529 - range 1 has start/finish covering the "101" and is not flagged for
1530 caret printing. The caret is at the start of "101", where its
1531 range_label is printed ("int").
1533 Fix-it hints
1534 ------------
1536 Rich locations can also contain "fix-it hints", giving suggestions
1537 for the user on how to edit their code to fix a problem. These
1538 can be expressed as insertions, replacements, and removals of text.
1539 The edits by default are relative to the zeroth range within the
1540 rich_location, but optionally they can be expressed relative to
1541 other locations (using various overloaded methods of the form
1542 rich_location::add_fixit_*).
1544 For example:
1546 Example F: fix-it hint: insert_before
1547 *************************************
1548 ptr = arr[0];
1549 ^~~~~~
1551 This rich location has a single range (range 0) covering "arr[0]",
1552 with the caret at the start. The rich location has a single
1553 insertion fix-it hint, inserted before range 0, added via
1554 richloc.add_fixit_insert_before ("&");
1556 Example G: multiple fix-it hints: insert_before and insert_after
1557 ****************************************************************
1558 #define FN(ARG0, ARG1, ARG2) fn(ARG0, ARG1, ARG2)
1559 ^~~~ ^~~~ ^~~~
1560 ( ) ( ) ( )
1561 This rich location has three ranges, covering "arg0", "arg1",
1562 and "arg2", all with caret-printing enabled.
1563 The rich location has 6 insertion fix-it hints: each arg
1564 has a pair of insertion fix-it hints, suggesting wrapping
1565 them with parentheses: one a '(' inserted before,
1566 the other a ')' inserted after, added via
1567 richloc.add_fixit_insert_before (LOC, "(");
1569 richloc.add_fixit_insert_after (LOC, ")");
1571 Example H: fix-it hint: removal
1572 *******************************
1573 struct s {int i};;
1576 This rich location has a single range at the stray trailing
1577 semicolon, along with a single removal fix-it hint, covering
1578 the same range, added via:
1579 richloc.add_fixit_remove ();
1581 Example I: fix-it hint: replace
1582 *******************************
1583 c = s.colour;
1584 ^~~~~~
1585 color
1586 This rich location has a single range (range 0) covering "colour",
1587 and a single "replace" fix-it hint, covering the same range,
1588 added via
1589 richloc.add_fixit_replace ("color");
1591 Example J: fix-it hint: line insertion
1592 **************************************
1594 3 | #include <stddef.h>
1595 + |+#include <stdio.h>
1596 4 | int the_next_line;
1598 This rich location has a single range at line 4 column 1, marked
1599 with SHOW_LINES_WITHOUT_RANGE (to avoid printing a meaningless caret
1600 on the "i" of int). It has a insertion fix-it hint of the string
1601 "#include <stdio.h>\n".
1603 Adding a fix-it hint can fail: for example, attempts to insert content
1604 at the transition between two line maps may fail due to there being no
1605 source_location (aka location_t) value to express the new location.
1607 Attempts to add a fix-it hint within a macro expansion will fail.
1609 There is only limited support for newline characters in fix-it hints:
1610 only hints with newlines which insert an entire new line are permitted,
1611 inserting at the start of a line, and finishing with a newline
1612 (with no interior newline characters). Other attempts to add
1613 fix-it hints containing newline characters will fail.
1614 Similarly, attempts to delete or replace a range *affecting* multiple
1615 lines will fail.
1617 The rich_location API handles these failures gracefully, so that
1618 diagnostics can attempt to add fix-it hints without each needing
1619 extensive checking.
1621 Fix-it hints within a rich_location are "atomic": if any hints can't
1622 be applied, none of them will be (tracked by the m_seen_impossible_fixit
1623 flag), and no fix-its hints will be displayed for that rich_location.
1624 This implies that diagnostic messages need to be worded in such a way
1625 that they make sense whether or not the fix-it hints are displayed,
1626 or that richloc.seen_impossible_fixit_p () should be checked before
1627 issuing the diagnostics. */
1629 class rich_location
1631 public:
1632 /* Constructors. */
1634 /* Constructing from a location. */
1635 rich_location (line_maps *set, source_location loc,
1636 const range_label *label = NULL);
1638 /* Destructor. */
1639 ~rich_location ();
1641 /* Accessors. */
1642 source_location get_loc () const { return get_loc (0); }
1643 source_location get_loc (unsigned int idx) const;
1645 void
1646 add_range (source_location loc,
1647 enum range_display_kind range_display_kind
1648 = SHOW_RANGE_WITHOUT_CARET,
1649 const range_label *label = NULL);
1651 void
1652 set_range (unsigned int idx, source_location loc,
1653 enum range_display_kind range_display_kind);
1655 unsigned int get_num_locations () const { return m_ranges.count (); }
1657 const location_range *get_range (unsigned int idx) const;
1658 location_range *get_range (unsigned int idx);
1660 expanded_location get_expanded_location (unsigned int idx);
1662 void
1663 override_column (int column);
1665 /* Fix-it hints. */
1667 /* Methods for adding insertion fix-it hints. */
1669 /* Suggest inserting NEW_CONTENT immediately before the primary
1670 range's start. */
1671 void
1672 add_fixit_insert_before (const char *new_content);
1674 /* Suggest inserting NEW_CONTENT immediately before the start of WHERE. */
1675 void
1676 add_fixit_insert_before (source_location where,
1677 const char *new_content);
1679 /* Suggest inserting NEW_CONTENT immediately after the end of the primary
1680 range. */
1681 void
1682 add_fixit_insert_after (const char *new_content);
1684 /* Suggest inserting NEW_CONTENT immediately after the end of WHERE. */
1685 void
1686 add_fixit_insert_after (source_location where,
1687 const char *new_content);
1689 /* Methods for adding removal fix-it hints. */
1691 /* Suggest removing the content covered by range 0. */
1692 void
1693 add_fixit_remove ();
1695 /* Suggest removing the content covered between the start and finish
1696 of WHERE. */
1697 void
1698 add_fixit_remove (source_location where);
1700 /* Suggest removing the content covered by SRC_RANGE. */
1701 void
1702 add_fixit_remove (source_range src_range);
1704 /* Methods for adding "replace" fix-it hints. */
1706 /* Suggest replacing the content covered by range 0 with NEW_CONTENT. */
1707 void
1708 add_fixit_replace (const char *new_content);
1710 /* Suggest replacing the content between the start and finish of
1711 WHERE with NEW_CONTENT. */
1712 void
1713 add_fixit_replace (source_location where,
1714 const char *new_content);
1716 /* Suggest replacing the content covered by SRC_RANGE with
1717 NEW_CONTENT. */
1718 void
1719 add_fixit_replace (source_range src_range,
1720 const char *new_content);
1722 unsigned int get_num_fixit_hints () const { return m_fixit_hints.count (); }
1723 fixit_hint *get_fixit_hint (int idx) const { return m_fixit_hints[idx]; }
1724 fixit_hint *get_last_fixit_hint () const;
1725 bool seen_impossible_fixit_p () const { return m_seen_impossible_fixit; }
1727 /* Set this if the fix-it hints are not suitable to be
1728 automatically applied.
1730 For example, if you are suggesting more than one
1731 mutually exclusive solution to a problem, then
1732 it doesn't make sense to apply all of the solutions;
1733 manual intervention is required.
1735 If set, then the fix-it hints in the rich_location will
1736 be printed, but will not be added to generated patches,
1737 or affect the modified version of the file. */
1738 void fixits_cannot_be_auto_applied ()
1740 m_fixits_cannot_be_auto_applied = true;
1743 bool fixits_can_be_auto_applied_p () const
1745 return !m_fixits_cannot_be_auto_applied;
1748 private:
1749 bool reject_impossible_fixit (source_location where);
1750 void stop_supporting_fixits ();
1751 void maybe_add_fixit (source_location start,
1752 source_location next_loc,
1753 const char *new_content);
1755 public:
1756 static const int STATICALLY_ALLOCATED_RANGES = 3;
1758 protected:
1759 line_maps *m_line_table;
1760 semi_embedded_vec <location_range, STATICALLY_ALLOCATED_RANGES> m_ranges;
1762 int m_column_override;
1764 bool m_have_expanded_location;
1765 expanded_location m_expanded_location;
1767 static const int MAX_STATIC_FIXIT_HINTS = 2;
1768 semi_embedded_vec <fixit_hint *, MAX_STATIC_FIXIT_HINTS> m_fixit_hints;
1770 bool m_seen_impossible_fixit;
1771 bool m_fixits_cannot_be_auto_applied;
1774 /* A struct for the result of range_label::get_text: a NUL-terminated buffer
1775 of localized text, and a flag to determine if the caller should "free" the
1776 buffer. */
1778 struct label_text
1780 label_text ()
1781 : m_buffer (NULL), m_caller_owned (false)
1784 label_text (char *buffer, bool caller_owned)
1785 : m_buffer (buffer), m_caller_owned (caller_owned)
1788 void maybe_free ()
1790 if (m_caller_owned)
1791 free (m_buffer);
1794 char *m_buffer;
1795 bool m_caller_owned;
1798 /* Abstract base class for labelling a range within a rich_location
1799 (e.g. for labelling expressions with their type).
1801 Generating the text could require non-trivial work, so this work
1802 is delayed (via the "get_text" virtual function) until the diagnostic
1803 printing code "knows" it needs it, thus avoiding doing it e.g. for
1804 warnings that are filtered by command-line flags. This virtual
1805 function also isolates libcpp and the diagnostics subsystem from
1806 the front-end and middle-end-specific code for generating the text
1807 for the labels.
1809 Like the rich_location instances they annotate, range_label instances
1810 are intended to be allocated on the stack when generating diagnostics,
1811 and to be short-lived. */
1813 class range_label
1815 public:
1816 virtual ~range_label () {}
1818 /* Get localized text for the label. */
1819 virtual label_text get_text () const = 0;
1822 /* A fix-it hint: a suggested insertion, replacement, or deletion of text.
1823 We handle these three types of edit with one class, by representing
1824 them as replacement of a half-open range:
1825 [start, next_loc)
1826 Insertions have start == next_loc: "replace" the empty string at the
1827 start location with the new string.
1828 Deletions are replacement with the empty string.
1830 There is only limited support for newline characters in fix-it hints
1831 as noted above in the comment for class rich_location.
1832 A fixit_hint instance can have at most one newline character; if
1833 present, the newline character must be the final character of
1834 the content (preventing e.g. fix-its that split a pre-existing line). */
1836 class fixit_hint
1838 public:
1839 fixit_hint (source_location start,
1840 source_location next_loc,
1841 const char *new_content);
1842 ~fixit_hint () { free (m_bytes); }
1844 bool affects_line_p (const char *file, int line) const;
1845 source_location get_start_loc () const { return m_start; }
1846 source_location get_next_loc () const { return m_next_loc; }
1847 bool maybe_append (source_location start,
1848 source_location next_loc,
1849 const char *new_content);
1851 const char *get_string () const { return m_bytes; }
1852 size_t get_length () const { return m_len; }
1854 bool insertion_p () const { return m_start == m_next_loc; }
1856 bool ends_with_newline_p () const;
1858 private:
1859 /* We don't use source_range here since, unlike most places,
1860 this is a half-open/half-closed range:
1861 [start, next_loc)
1862 so that we can support insertion via start == next_loc. */
1863 source_location m_start;
1864 source_location m_next_loc;
1865 char *m_bytes;
1866 size_t m_len;
1870 /* This is enum is used by the function linemap_resolve_location
1871 below. The meaning of the values is explained in the comment of
1872 that function. */
1873 enum location_resolution_kind
1875 LRK_MACRO_EXPANSION_POINT,
1876 LRK_SPELLING_LOCATION,
1877 LRK_MACRO_DEFINITION_LOCATION
1880 /* Resolve a virtual location into either a spelling location, an
1881 expansion point location or a token argument replacement point
1882 location. Return the map that encodes the virtual location as well
1883 as the resolved location.
1885 If LOC is *NOT* the location of a token resulting from the
1886 expansion of a macro, then the parameter LRK (which stands for
1887 Location Resolution Kind) is ignored and the resulting location
1888 just equals the one given in argument.
1890 Now if LOC *IS* the location of a token resulting from the
1891 expansion of a macro, this is what happens.
1893 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1894 -------------------------------
1896 The virtual location is resolved to the first macro expansion point
1897 that led to this macro expansion.
1899 * If LRK is set to LRK_SPELLING_LOCATION
1900 -------------------------------------
1902 The virtual location is resolved to the locus where the token has
1903 been spelled in the source. This can follow through all the macro
1904 expansions that led to the token.
1906 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1907 --------------------------------------
1909 The virtual location is resolved to the locus of the token in the
1910 context of the macro definition.
1912 If LOC is the locus of a token that is an argument of a
1913 function-like macro [replacing a parameter in the replacement list
1914 of the macro] the virtual location is resolved to the locus of the
1915 parameter that is replaced, in the context of the definition of the
1916 macro.
1918 If LOC is the locus of a token that is not an argument of a
1919 function-like macro, then the function behaves as if LRK was set to
1920 LRK_SPELLING_LOCATION.
1922 If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1923 returned location. Note that if the returned location wasn't originally
1924 encoded by a map, the *MAP is set to NULL. This can happen if LOC
1925 resolves to a location reserved for the client code, like
1926 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1928 source_location linemap_resolve_location (struct line_maps *,
1929 source_location loc,
1930 enum location_resolution_kind lrk,
1931 const line_map_ordinary **loc_map);
1933 /* Suppose that LOC is the virtual location of a token coming from the
1934 expansion of a macro M. This function then steps up to get the
1935 location L of the point where M got expanded. If L is a spelling
1936 location inside a macro expansion M', then this function returns
1937 the point where M' was expanded. LOC_MAP is an output parameter.
1938 When non-NULL, *LOC_MAP is set to the map of the returned
1939 location. */
1940 source_location linemap_unwind_toward_expansion (struct line_maps *,
1941 source_location loc,
1942 const struct line_map **loc_map);
1944 /* If LOC is the virtual location of a token coming from the expansion
1945 of a macro M and if its spelling location is reserved (e.g, a
1946 location for a built-in token), then this function unwinds (using
1947 linemap_unwind_toward_expansion) the location until a location that
1948 is not reserved and is not in a system header is reached. In other
1949 words, this unwinds the reserved location until a location that is
1950 in real source code is reached.
1952 Otherwise, if the spelling location for LOC is not reserved or if
1953 LOC doesn't come from the expansion of a macro, the function
1954 returns LOC as is and *MAP is not touched.
1956 *MAP is set to the map of the returned location if the later is
1957 different from LOC. */
1958 source_location linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
1959 source_location loc,
1960 const struct line_map **map);
1962 /* Expand source code location LOC and return a user readable source
1963 code location. LOC must be a spelling (non-virtual) location. If
1964 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1965 location is returned. */
1966 expanded_location linemap_expand_location (struct line_maps *,
1967 const struct line_map *,
1968 source_location loc);
1970 /* Statistics about maps allocation and usage as returned by
1971 linemap_get_statistics. */
1972 struct linemap_stats
1974 long num_ordinary_maps_allocated;
1975 long num_ordinary_maps_used;
1976 long ordinary_maps_allocated_size;
1977 long ordinary_maps_used_size;
1978 long num_expanded_macros;
1979 long num_macro_tokens;
1980 long num_macro_maps_used;
1981 long macro_maps_allocated_size;
1982 long macro_maps_used_size;
1983 long macro_maps_locations_size;
1984 long duplicated_macro_maps_locations_size;
1985 long adhoc_table_size;
1986 long adhoc_table_entries_used;
1989 /* Return the highest location emitted for a given file for which
1990 there is a line map in SET. FILE_NAME is the file name to
1991 consider. If the function returns TRUE, *LOC is set to the highest
1992 location emitted for that file. */
1993 bool linemap_get_file_highest_location (struct line_maps * set,
1994 const char *file_name,
1995 source_location *loc);
1997 /* Compute and return statistics about the memory consumption of some
1998 parts of the line table SET. */
1999 void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
2001 /* Dump debugging information about source location LOC into the file
2002 stream STREAM. SET is the line map set LOC comes from. */
2003 void linemap_dump_location (struct line_maps *, source_location, FILE *);
2005 /* Dump line map at index IX in line table SET to STREAM. If STREAM
2006 is NULL, use stderr. IS_MACRO is true if the caller wants to
2007 dump a macro map, false otherwise. */
2008 void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
2010 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
2011 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
2012 specifies how many macro maps to dump. */
2013 void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
2015 /* An enum for distinguishing the various parts within a source_location. */
2017 enum location_aspect
2019 LOCATION_ASPECT_CARET,
2020 LOCATION_ASPECT_START,
2021 LOCATION_ASPECT_FINISH
2024 /* The rich_location class requires a way to expand source_location instances.
2025 We would directly use expand_location_to_spelling_point, which is
2026 implemented in gcc/input.c, but we also need to use it for rich_location
2027 within genmatch.c.
2028 Hence we require client code of libcpp to implement the following
2029 symbol. */
2030 extern expanded_location
2031 linemap_client_expand_location_to_spelling_point (source_location,
2032 enum location_aspect);
2034 #endif /* !LIBCPP_LINE_MAP_H */