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