| 1 | /* Map (unsigned int) keys to (source file, line, column) triples. |
| 2 | Copyright (C) 2001-2025 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, but for |
| 91 | ease-of-understanding the implementation, they are currently assigned as |
| 92 | follows in the case of 32-bit location_t: |
| 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 | |
| 295 | /* A 64-bit type to represent a location. We only use 63 of the 64 bits, so |
| 296 | that two location_t can be safely subtracted and stored in an int64_t. */ |
| 297 | typedef uint64_t location_t; |
| 298 | typedef int64_t location_diff_t; |
| 299 | |
| 300 | /* Sometimes we need a type that has the same size as location_t but that does |
| 301 | not represent a location. This typedef provides more clarity in those |
| 302 | cases. */ |
| 303 | typedef location_t line_map_uint_t; |
| 304 | |
| 305 | /* Do not track column numbers higher than this one. As a result, the |
| 306 | range of column_bits is [12, 18] (or 0 if column numbers are |
| 307 | disabled). */ |
| 308 | const unsigned int LINE_MAP_MAX_COLUMN_NUMBER = (1U << 31) - 1; |
| 309 | |
| 310 | /* Do not pack ranges if locations get higher than this. |
| 311 | If you change this, update: |
| 312 | gcc.dg/plugin/location-overflow-test-*.c. */ |
| 313 | const location_t LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES |
| 314 | = location_t (0x50000000) << 31; |
| 315 | |
| 316 | /* Do not track column numbers if locations get higher than this. |
| 317 | If you change this, update: |
| 318 | gcc.dg/plugin/location-overflow-test-*.c. */ |
| 319 | const location_t LINE_MAP_MAX_LOCATION_WITH_COLS |
| 320 | = location_t (0x60000000) << 31; |
| 321 | |
| 322 | /* Highest possible source location encoded within an ordinary map. Higher |
| 323 | values up to MAX_LOCATION_T represent macro virtual locations. */ |
| 324 | const location_t LINE_MAP_MAX_LOCATION = location_t (0x70000000) << 31; |
| 325 | |
| 326 | /* This is the highest possible source location encoded within an |
| 327 | ordinary or macro map. */ |
| 328 | const location_t MAX_LOCATION_T = location_t (-1) >> 2; |
| 329 | |
| 330 | /* This is the number of range bits suggested to enable, if range tracking is |
| 331 | desired. */ |
| 332 | const int line_map_suggested_range_bits = 7; |
| 333 | |
| 334 | /* A range of source locations. |
| 335 | |
| 336 | Ranges are closed: |
| 337 | m_start is the first location within the range, |
| 338 | m_finish is the last location within the range. |
| 339 | |
| 340 | We may need a more compact way to store these, but for now, |
| 341 | let's do it the simple way, as a pair. */ |
| 342 | struct GTY(()) source_range |
| 343 | { |
| 344 | location_t m_start; |
| 345 | location_t m_finish; |
| 346 | |
| 347 | /* We avoid using constructors, since various structs that |
| 348 | don't yet have constructors will embed instances of |
| 349 | source_range. */ |
| 350 | |
| 351 | /* Make a source_range from a location_t. */ |
| 352 | static source_range from_location (location_t loc) |
| 353 | { |
| 354 | source_range result; |
| 355 | result.m_start = loc; |
| 356 | result.m_finish = loc; |
| 357 | return result; |
| 358 | } |
| 359 | |
| 360 | /* Make a source_range from a pair of location_t. */ |
| 361 | static source_range from_locations (location_t start, |
| 362 | location_t finish) |
| 363 | { |
| 364 | source_range result; |
| 365 | result.m_start = start; |
| 366 | result.m_finish = finish; |
| 367 | return result; |
| 368 | } |
| 369 | }; |
| 370 | |
| 371 | /* Memory allocation function typedef. Works like xrealloc. */ |
| 372 | typedef void *(*line_map_realloc) (void *, size_t); |
| 373 | |
| 374 | /* Memory allocator function that returns the actual allocated size, |
| 375 | for a given requested allocation. */ |
| 376 | typedef size_t (*line_map_round_alloc_size_func) (size_t); |
| 377 | |
| 378 | /* A line_map encodes a sequence of locations. |
| 379 | There are two kinds of maps. Ordinary maps and macro expansion |
| 380 | maps, a.k.a macro maps. |
| 381 | |
| 382 | A macro map encodes source locations of tokens that are part of a |
| 383 | macro replacement-list, at a macro expansion point. E.g, in: |
| 384 | |
| 385 | #define PLUS(A,B) A + B |
| 386 | |
| 387 | No macro map is going to be created there, because we are not at a |
| 388 | macro expansion point. We are at a macro /definition/ point. So the |
| 389 | locations of the tokens of the macro replacement-list (i.e, A + B) |
| 390 | will be locations in an ordinary map, not a macro map. |
| 391 | |
| 392 | On the other hand, if we later do: |
| 393 | |
| 394 | int a = PLUS (1,2); |
| 395 | |
| 396 | The invocation of PLUS here is a macro expansion. So we are at a |
| 397 | macro expansion point. The preprocessor expands PLUS (1,2) and |
| 398 | replaces it with the tokens of its replacement-list: 1 + 2. A macro |
| 399 | map is going to be created to hold (or rather to map, haha ...) the |
| 400 | locations of the tokens 1, + and 2. The macro map also records the |
| 401 | location of the expansion point of PLUS. That location is mapped in |
| 402 | the map that is active right before the location of the invocation |
| 403 | of PLUS. */ |
| 404 | |
| 405 | /* This contains GTY mark-up to support precompiled headers. |
| 406 | line_map is an abstract class, only derived objects exist. */ |
| 407 | struct GTY((tag ("0" ), desc ("MAP_ORDINARY_P (&%h) ? 1 : 2" ))) line_map { |
| 408 | location_t start_location; |
| 409 | |
| 410 | /* Size is 8 bytes; alignment 4 or 8 depending on the arch. */ |
| 411 | }; |
| 412 | |
| 413 | /* An ordinary line map encodes physical source locations. Those |
| 414 | physical source locations are called "spelling locations". |
| 415 | |
| 416 | Physical source file TO_FILE at line TO_LINE at column 0 is represented |
| 417 | by the logical START_LOCATION. TO_LINE+L at column C is represented by |
| 418 | START_LOCATION+(L*(1<<m_column_and_range_bits))+(C*1<<m_range_bits), as |
| 419 | long as C<(1<<effective range bits), and the result_location is less than |
| 420 | the next line_map's start_location. |
| 421 | (The top line is line 1 and the leftmost column is column 1; line/column 0 |
| 422 | means "entire file/line" or "unknown line/column" or "not applicable".) |
| 423 | |
| 424 | The highest possible source location is MAX_LOCATION_T. */ |
| 425 | struct GTY((tag ("1" ))) line_map_ordinary : public line_map { |
| 426 | /* Base class is 8 bytes. */ |
| 427 | |
| 428 | /* 4 bytes of integers, each 1 byte for easy extraction/insertion. */ |
| 429 | |
| 430 | /* The reason for creation of this line map. */ |
| 431 | ENUM_BITFIELD (lc_reason) reason : 8; |
| 432 | |
| 433 | /* SYSP is one for a system header, two for a C system header file |
| 434 | that therefore needs to be extern "C" protected in C++, and zero |
| 435 | otherwise. This field isn't really needed now that it's in |
| 436 | cpp_buffer. */ |
| 437 | unsigned char sysp; |
| 438 | |
| 439 | /* Number of the low-order location_t bits used for column numbers |
| 440 | and ranges. */ |
| 441 | unsigned int m_column_and_range_bits : 8; |
| 442 | |
| 443 | /* Number of the low-order "column" bits used for storing short ranges |
| 444 | inline, rather than in the ad-hoc table. |
| 445 | MSB LSB |
| 446 | 31 0 |
| 447 | +-------------------------+-------------------------------------------+ |
| 448 | | |<---map->column_and_range_bits (e.g. 12)-->| |
| 449 | +-------------------------+-----------------------+-------------------+ |
| 450 | | | column_and_range_bits | map->range_bits | |
| 451 | | | - range_bits | | |
| 452 | +-------------------------+-----------------------+-------------------+ |
| 453 | | row bits | effective column bits | short range bits | |
| 454 | | | (e.g. 7) | (e.g. 5) | |
| 455 | +-------------------------+-----------------------+-------------------+ */ |
| 456 | unsigned int m_range_bits : 8; |
| 457 | |
| 458 | /* 32-bit int even in 64-bit mode. */ |
| 459 | linenum_type to_line; |
| 460 | |
| 461 | /* Location from whence this line map was included. For regular |
| 462 | #includes, this location will be the last location of a map. For |
| 463 | outermost file, this is 0. For modules it could be anywhere |
| 464 | within a map. */ |
| 465 | location_t included_from; |
| 466 | |
| 467 | /* Pointer alignment boundary, whether 32-bit or 64-bit mode. */ |
| 468 | const char *to_file; |
| 469 | |
| 470 | /* Size is 28 (32) bytes for 32-bit (64-bit) arch. */ |
| 471 | }; |
| 472 | |
| 473 | struct cpp_hashnode; |
| 474 | |
| 475 | /* A macro line map encodes location of tokens coming from a macro |
| 476 | expansion. |
| 477 | |
| 478 | The offset from START_LOCATION is used to index into |
| 479 | MACRO_LOCATIONS; this holds the original location of the token. */ |
| 480 | struct GTY((tag ("2" ))) line_map_macro : public line_map { |
| 481 | |
| 482 | /* Get the location of the expansion point of this macro map. */ |
| 483 | |
| 484 | location_t |
| 485 | get_expansion_point_location () const |
| 486 | { |
| 487 | return m_expansion; |
| 488 | } |
| 489 | |
| 490 | /* Base is 8 bytes. */ |
| 491 | |
| 492 | /* The number of tokens inside the replacement-list of MACRO. */ |
| 493 | unsigned int n_tokens; |
| 494 | |
| 495 | /* Pointer alignment boundary. */ |
| 496 | |
| 497 | /* The cpp macro whose expansion gave birth to this macro map. */ |
| 498 | struct cpp_hashnode * |
| 499 | GTY ((nested_ptr (union tree_node, |
| 500 | "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL" , |
| 501 | "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL" ))) |
| 502 | macro; |
| 503 | |
| 504 | /* This array of location is actually an array of pairs of |
| 505 | locations. The elements inside it thus look like: |
| 506 | |
| 507 | x0,y0, x1,y1, x2,y2, ...., xn,yn. |
| 508 | |
| 509 | where n == n_tokens; |
| 510 | |
| 511 | Remember that these xI,yI are collected when libcpp is about to |
| 512 | expand a given macro. |
| 513 | |
| 514 | yI is the location in the macro definition, either of the token |
| 515 | itself or of a macro parameter that it replaces. |
| 516 | |
| 517 | Imagine this: |
| 518 | |
| 519 | #define PLUS(A, B) A + B <--- #1 |
| 520 | |
| 521 | int a = PLUS (1,2); <--- #2 |
| 522 | |
| 523 | There is a macro map for the expansion of PLUS in #2. PLUS is |
| 524 | expanded into its expansion-list. The expansion-list is the |
| 525 | replacement-list of PLUS where the macro parameters are replaced |
| 526 | with their arguments. So the replacement-list of PLUS is made of |
| 527 | the tokens: |
| 528 | |
| 529 | A, +, B |
| 530 | |
| 531 | and the expansion-list is made of the tokens: |
| 532 | |
| 533 | 1, +, 2 |
| 534 | |
| 535 | Let's consider the case of token "+". Its y1 [yI for I == 1] is |
| 536 | its spelling location in #1. |
| 537 | |
| 538 | y0 (thus for token "1") is the spelling location of A in #1. |
| 539 | |
| 540 | And y2 (of token "2") is the spelling location of B in #1. |
| 541 | |
| 542 | When the token is /not/ an argument for a macro, xI is the same |
| 543 | location as yI. Otherwise, xI is the location of the token |
| 544 | outside this macro expansion. If this macro was expanded from |
| 545 | another macro expansion, xI is a virtual location representing |
| 546 | the token in that macro expansion; otherwise, it is the spelling |
| 547 | location of the token. |
| 548 | |
| 549 | Note that a virtual location is a location returned by |
| 550 | linemap_add_macro_token. It encodes the relevant locations (x,y |
| 551 | pairs) of that token across the macro expansions from which it |
| 552 | (the token) might come from. |
| 553 | |
| 554 | In the example above x1 (for token "+") is going to be the same |
| 555 | as y1. x0 is the spelling location for the argument token "1", |
| 556 | and x2 is the spelling location for the argument token "2". */ |
| 557 | location_t * GTY((atomic)) macro_locations; |
| 558 | |
| 559 | /* This is the location of the expansion point of the current macro |
| 560 | map. It's the location of the macro name. That location is held |
| 561 | by the map that was current right before the current one. It |
| 562 | could have been either a macro or an ordinary map, depending on |
| 563 | if we are in a nested expansion context not. */ |
| 564 | location_t m_expansion; |
| 565 | |
| 566 | /* Size is one of the following: |
| 567 | 32-bit system: 28 or 32 bytes, depending whether a uint64_t requires |
| 568 | 4- or 8-byte alignment. |
| 569 | 64-bit arch: 40 bytes. */ |
| 570 | }; |
| 571 | |
| 572 | #if CHECKING_P && (GCC_VERSION >= 2007) |
| 573 | |
| 574 | /* Assertion macro to be used in line-map code. */ |
| 575 | #define linemap_assert(EXPR) \ |
| 576 | do { \ |
| 577 | if (! (EXPR)) \ |
| 578 | abort (); \ |
| 579 | } while (0) |
| 580 | |
| 581 | /* Assert that becomes a conditional expression when checking is disabled at |
| 582 | compilation time. Use this for conditions that should not happen but if |
| 583 | they happen, it is better to handle them gracefully rather than crash |
| 584 | randomly later. |
| 585 | Usage: |
| 586 | |
| 587 | if (linemap_assert_fails(EXPR)) handle_error(); */ |
| 588 | #define linemap_assert_fails(EXPR) __extension__ \ |
| 589 | ({linemap_assert (EXPR); false;}) |
| 590 | |
| 591 | #else |
| 592 | /* Include EXPR, so that unused variable warnings do not occur. */ |
| 593 | #define linemap_assert(EXPR) ((void)(0 && (EXPR))) |
| 594 | #define linemap_assert_fails(EXPR) (! (EXPR)) |
| 595 | #endif |
| 596 | |
| 597 | /* Get whether location LOC is an ordinary location. */ |
| 598 | |
| 599 | inline bool |
| 600 | IS_ORDINARY_LOC (location_t loc) |
| 601 | { |
| 602 | return loc < LINE_MAP_MAX_LOCATION; |
| 603 | } |
| 604 | |
| 605 | /* Get whether location LOC is an ad-hoc location. */ |
| 606 | |
| 607 | inline bool |
| 608 | IS_ADHOC_LOC (location_t loc) |
| 609 | { |
| 610 | return loc > MAX_LOCATION_T; |
| 611 | } |
| 612 | |
| 613 | /* Categorize line map kinds. */ |
| 614 | |
| 615 | inline bool |
| 616 | MAP_ORDINARY_P (const line_map *map) |
| 617 | { |
| 618 | return IS_ORDINARY_LOC (loc: map->start_location); |
| 619 | } |
| 620 | |
| 621 | /* Return TRUE if MAP encodes locations coming from a macro |
| 622 | replacement-list at macro expansion point. */ |
| 623 | bool |
| 624 | linemap_macro_expansion_map_p (const line_map *); |
| 625 | |
| 626 | /* Assert that MAP encodes locations of tokens that are not part of |
| 627 | the replacement-list of a macro expansion, downcasting from |
| 628 | line_map * to line_map_ordinary *. */ |
| 629 | |
| 630 | inline line_map_ordinary * |
| 631 | linemap_check_ordinary (line_map *map) |
| 632 | { |
| 633 | linemap_assert (MAP_ORDINARY_P (map)); |
| 634 | return (line_map_ordinary *)map; |
| 635 | } |
| 636 | |
| 637 | /* Assert that MAP encodes locations of tokens that are not part of |
| 638 | the replacement-list of a macro expansion, downcasting from |
| 639 | const line_map * to const line_map_ordinary *. */ |
| 640 | |
| 641 | inline const line_map_ordinary * |
| 642 | linemap_check_ordinary (const line_map *map) |
| 643 | { |
| 644 | linemap_assert (MAP_ORDINARY_P (map)); |
| 645 | return (const line_map_ordinary *)map; |
| 646 | } |
| 647 | |
| 648 | /* Assert that MAP is a macro expansion and downcast to the appropriate |
| 649 | subclass. */ |
| 650 | |
| 651 | inline line_map_macro *linemap_check_macro (line_map *map) |
| 652 | { |
| 653 | linemap_assert (!MAP_ORDINARY_P (map)); |
| 654 | return (line_map_macro *)map; |
| 655 | } |
| 656 | |
| 657 | /* Assert that MAP is a macro expansion and downcast to the appropriate |
| 658 | subclass. */ |
| 659 | |
| 660 | inline const line_map_macro * |
| 661 | linemap_check_macro (const line_map *map) |
| 662 | { |
| 663 | linemap_assert (!MAP_ORDINARY_P (map)); |
| 664 | return (const line_map_macro *)map; |
| 665 | } |
| 666 | |
| 667 | /* Read the start location of MAP. */ |
| 668 | |
| 669 | inline location_t |
| 670 | MAP_START_LOCATION (const line_map *map) |
| 671 | { |
| 672 | return map->start_location; |
| 673 | } |
| 674 | |
| 675 | /* Get the starting line number of ordinary map MAP. */ |
| 676 | |
| 677 | inline linenum_type |
| 678 | ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map_ordinary *ord_map) |
| 679 | { |
| 680 | return ord_map->to_line; |
| 681 | } |
| 682 | |
| 683 | /* Return a positive value if map encodes locations from a system |
| 684 | header, 0 otherwise. Returns 1 if ordinary map MAP encodes locations |
| 685 | in a system header and 2 if it encodes locations in a C system header |
| 686 | that therefore needs to be extern "C" protected in C++. */ |
| 687 | |
| 688 | inline unsigned char |
| 689 | (const line_map_ordinary *ord_map) |
| 690 | { |
| 691 | return ord_map->sysp; |
| 692 | } |
| 693 | |
| 694 | /* TRUE if this line map is for a module (not a source file). */ |
| 695 | |
| 696 | inline bool |
| 697 | MAP_MODULE_P (const line_map *map) |
| 698 | { |
| 699 | return (MAP_ORDINARY_P (map) |
| 700 | && linemap_check_ordinary (map)->reason == LC_MODULE); |
| 701 | } |
| 702 | |
| 703 | /* Get the filename of ordinary map MAP. */ |
| 704 | |
| 705 | inline const char * |
| 706 | ORDINARY_MAP_FILE_NAME (const line_map_ordinary *ord_map) |
| 707 | { |
| 708 | return ord_map->to_file; |
| 709 | } |
| 710 | |
| 711 | /* Get the cpp macro whose expansion gave birth to macro map MAP. */ |
| 712 | |
| 713 | inline cpp_hashnode * |
| 714 | MACRO_MAP_MACRO (const line_map_macro *macro_map) |
| 715 | { |
| 716 | return macro_map->macro; |
| 717 | } |
| 718 | |
| 719 | /* Get the number of tokens inside the replacement-list of the macro |
| 720 | that led to macro map MAP. */ |
| 721 | |
| 722 | inline unsigned int |
| 723 | MACRO_MAP_NUM_MACRO_TOKENS (const line_map_macro *macro_map) |
| 724 | { |
| 725 | return macro_map->n_tokens; |
| 726 | } |
| 727 | |
| 728 | /* Get the array of pairs of locations within macro map MAP. |
| 729 | See the declaration of line_map_macro for more information. */ |
| 730 | |
| 731 | inline location_t * |
| 732 | MACRO_MAP_LOCATIONS (const line_map_macro *macro_map) |
| 733 | { |
| 734 | return macro_map->macro_locations; |
| 735 | } |
| 736 | |
| 737 | /* The abstraction of a set of location maps. There can be several |
| 738 | types of location maps. This abstraction contains the attributes |
| 739 | that are independent from the type of the map. |
| 740 | |
| 741 | Essentially this is just a vector of T_linemap_subclass, |
| 742 | which can only ever grow in size. */ |
| 743 | |
| 744 | struct GTY(()) maps_info_ordinary { |
| 745 | /* This array contains the "ordinary" line maps, for all |
| 746 | events other than macro expansion |
| 747 | (e.g. when a new preprocessing unit starts or ends). */ |
| 748 | line_map_ordinary * GTY ((length ("%h.used" ))) maps; |
| 749 | |
| 750 | /* The total number of allocated maps. */ |
| 751 | line_map_uint_t allocated; |
| 752 | |
| 753 | /* The number of elements used in maps. This number is smaller |
| 754 | or equal to ALLOCATED. */ |
| 755 | line_map_uint_t used; |
| 756 | |
| 757 | /* The index of the last ordinary map that was looked up with |
| 758 | linemap_lookup. */ |
| 759 | mutable line_map_uint_t m_cache; |
| 760 | }; |
| 761 | |
| 762 | struct GTY(()) maps_info_macro { |
| 763 | /* This array contains the macro line maps. |
| 764 | A macro line map is created whenever a macro expansion occurs. */ |
| 765 | line_map_macro * GTY ((length ("%h.used" ))) maps; |
| 766 | |
| 767 | /* The total number of allocated maps. */ |
| 768 | line_map_uint_t allocated; |
| 769 | |
| 770 | /* The number of elements used in maps. This number is smaller |
| 771 | or equal to ALLOCATED. */ |
| 772 | line_map_uint_t used; |
| 773 | |
| 774 | /* The index of the last macro map that was looked up with |
| 775 | linemap_lookup. */ |
| 776 | mutable line_map_uint_t m_cache; |
| 777 | }; |
| 778 | |
| 779 | /* Data structure to associate a source_range together with an arbitrary |
| 780 | data pointer with a source location. */ |
| 781 | struct GTY(()) location_adhoc_data { |
| 782 | location_t locus; |
| 783 | source_range src_range; |
| 784 | void * GTY((skip)) data; |
| 785 | unsigned discriminator; |
| 786 | }; |
| 787 | |
| 788 | struct htab; |
| 789 | |
| 790 | /* The following data structure encodes a location with some adhoc data |
| 791 | and maps it to a new unsigned integer (called an adhoc location) |
| 792 | that replaces the original location to represent the mapping. |
| 793 | |
| 794 | The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the |
| 795 | highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as |
| 796 | the original location. Once identified as the adhoc_loc, the lower 62 |
| 797 | bits of the integer is used to index the location_adhoc_data array, |
| 798 | in which the locus and associated data is stored. */ |
| 799 | |
| 800 | struct GTY(()) location_adhoc_data_map { |
| 801 | struct htab * GTY((skip)) htab; |
| 802 | location_t curr_loc; |
| 803 | line_map_uint_t allocated; |
| 804 | struct location_adhoc_data GTY((length ("%h.allocated" ))) *data; |
| 805 | }; |
| 806 | |
| 807 | /* A set of chronological line_map structures. */ |
| 808 | class GTY(()) line_maps { |
| 809 | public: |
| 810 | |
| 811 | ~line_maps (); |
| 812 | |
| 813 | bool pure_location_p (location_t loc) const; |
| 814 | location_t get_pure_location (location_t loc) const; |
| 815 | |
| 816 | source_range get_range_from_loc (location_t loc) const; |
| 817 | location_t get_start (location_t loc) const |
| 818 | { |
| 819 | return get_range_from_loc (loc).m_start; |
| 820 | } |
| 821 | location_t |
| 822 | get_finish (location_t loc) const |
| 823 | { |
| 824 | return get_range_from_loc (loc).m_finish; |
| 825 | } |
| 826 | |
| 827 | location_t make_location (location_t caret, |
| 828 | location_t start, |
| 829 | location_t finish); |
| 830 | |
| 831 | location_t |
| 832 | get_or_create_combined_loc (location_t locus, |
| 833 | source_range src_range, |
| 834 | void *data, |
| 835 | unsigned discriminator); |
| 836 | |
| 837 | private: |
| 838 | bool can_be_stored_compactly_p (location_t locus, |
| 839 | source_range src_range, |
| 840 | void *data, |
| 841 | unsigned discriminator) const; |
| 842 | source_range get_range_from_adhoc_loc (location_t loc) const; |
| 843 | |
| 844 | public: |
| 845 | maps_info_ordinary info_ordinary; |
| 846 | |
| 847 | maps_info_macro info_macro; |
| 848 | |
| 849 | /* Depth of the include stack, including the current file. */ |
| 850 | unsigned int depth; |
| 851 | |
| 852 | /* If true, prints an include trace a la -H. */ |
| 853 | bool trace_includes; |
| 854 | |
| 855 | /* True if we've seen a #line or # 44 "file" directive. */ |
| 856 | bool seen_line_directive; |
| 857 | |
| 858 | /* Highest location_t "given out". */ |
| 859 | location_t highest_location; |
| 860 | |
| 861 | /* Start of line of highest location_t "given out". */ |
| 862 | location_t highest_line; |
| 863 | |
| 864 | /* The maximum column number we can quickly allocate. Higher numbers |
| 865 | may require allocating a new line_map. */ |
| 866 | unsigned int max_column_hint; |
| 867 | |
| 868 | /* The allocator to use when resizing 'maps', defaults to xrealloc. */ |
| 869 | line_map_realloc GTY((callback)) m_reallocator; |
| 870 | |
| 871 | /* The allocators' function used to know the actual size it |
| 872 | allocated, for a certain allocation size requested. */ |
| 873 | line_map_round_alloc_size_func GTY((callback)) m_round_alloc_size; |
| 874 | |
| 875 | struct location_adhoc_data_map m_location_adhoc_data_map; |
| 876 | |
| 877 | /* The special location value that is used as spelling location for |
| 878 | built-in tokens. */ |
| 879 | location_t builtin_location; |
| 880 | |
| 881 | /* The default value of range_bits in ordinary line maps. */ |
| 882 | unsigned int default_range_bits; |
| 883 | |
| 884 | line_map_uint_t m_num_optimized_ranges; |
| 885 | line_map_uint_t m_num_unoptimized_ranges; |
| 886 | }; |
| 887 | |
| 888 | /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE |
| 889 | if we are interested in macro maps, FALSE otherwise. */ |
| 890 | inline line_map_uint_t |
| 891 | LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind) |
| 892 | { |
| 893 | if (map_kind) |
| 894 | return set->info_macro.allocated; |
| 895 | else |
| 896 | return set->info_ordinary.allocated; |
| 897 | } |
| 898 | |
| 899 | /* As above, but by reference (e.g. as an lvalue). */ |
| 900 | |
| 901 | inline line_map_uint_t & |
| 902 | LINEMAPS_ALLOCATED (line_maps *set, bool map_kind) |
| 903 | { |
| 904 | if (map_kind) |
| 905 | return set->info_macro.allocated; |
| 906 | else |
| 907 | return set->info_ordinary.allocated; |
| 908 | } |
| 909 | |
| 910 | /* Returns the number of used maps so far. MAP_KIND shall be TRUE if |
| 911 | we are interested in macro maps, FALSE otherwise.*/ |
| 912 | inline line_map_uint_t |
| 913 | LINEMAPS_USED (const line_maps *set, bool map_kind) |
| 914 | { |
| 915 | if (map_kind) |
| 916 | return set->info_macro.used; |
| 917 | else |
| 918 | return set->info_ordinary.used; |
| 919 | } |
| 920 | |
| 921 | /* As above, but by reference (e.g. as an lvalue). */ |
| 922 | |
| 923 | inline line_map_uint_t & |
| 924 | LINEMAPS_USED (line_maps *set, bool map_kind) |
| 925 | { |
| 926 | if (map_kind) |
| 927 | return set->info_macro.used; |
| 928 | else |
| 929 | return set->info_ordinary.used; |
| 930 | } |
| 931 | |
| 932 | /* Return the map at a given index. */ |
| 933 | inline line_map * |
| 934 | LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, line_map_uint_t index) |
| 935 | { |
| 936 | if (map_kind) |
| 937 | return &set->info_macro.maps[index]; |
| 938 | else |
| 939 | return &set->info_ordinary.maps[index]; |
| 940 | } |
| 941 | |
| 942 | /* Returns the last map used in the line table SET. MAP_KIND |
| 943 | shall be TRUE if we are interested in macro maps, FALSE |
| 944 | otherwise.*/ |
| 945 | inline line_map * |
| 946 | LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind) |
| 947 | { |
| 948 | linemap_assert (LINEMAPS_USED (set, map_kind)); |
| 949 | return LINEMAPS_MAP_AT (set, map_kind, |
| 950 | index: LINEMAPS_USED (set, map_kind) - 1); |
| 951 | } |
| 952 | |
| 953 | /* Returns the INDEXth ordinary map. */ |
| 954 | inline line_map_ordinary * |
| 955 | LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, line_map_uint_t index) |
| 956 | { |
| 957 | linemap_assert (index < LINEMAPS_USED (set, false)); |
| 958 | return (line_map_ordinary *)LINEMAPS_MAP_AT (set, map_kind: false, index); |
| 959 | } |
| 960 | |
| 961 | /* Return the number of ordinary maps allocated in the line table |
| 962 | SET. */ |
| 963 | inline line_map_uint_t |
| 964 | LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set) |
| 965 | { |
| 966 | return LINEMAPS_ALLOCATED (set, map_kind: false); |
| 967 | } |
| 968 | |
| 969 | /* Return the number of ordinary maps used in the line table SET. */ |
| 970 | inline line_map_uint_t |
| 971 | LINEMAPS_ORDINARY_USED (const line_maps *set) |
| 972 | { |
| 973 | return LINEMAPS_USED (set, map_kind: false); |
| 974 | } |
| 975 | |
| 976 | /* Returns a pointer to the last ordinary map used in the line table |
| 977 | SET. */ |
| 978 | inline line_map_ordinary * |
| 979 | LINEMAPS_LAST_ORDINARY_MAP (const line_maps *set) |
| 980 | { |
| 981 | return (line_map_ordinary *)LINEMAPS_LAST_MAP (set, map_kind: false); |
| 982 | } |
| 983 | |
| 984 | /* Returns the INDEXth macro map. */ |
| 985 | inline line_map_macro * |
| 986 | LINEMAPS_MACRO_MAP_AT (const line_maps *set, line_map_uint_t index) |
| 987 | { |
| 988 | linemap_assert (index < LINEMAPS_USED (set, true)); |
| 989 | return (line_map_macro *)LINEMAPS_MAP_AT (set, map_kind: true, index); |
| 990 | } |
| 991 | |
| 992 | /* Returns the number of macro maps that were allocated in the line |
| 993 | table SET. */ |
| 994 | inline line_map_uint_t |
| 995 | LINEMAPS_MACRO_ALLOCATED (const line_maps *set) |
| 996 | { |
| 997 | return LINEMAPS_ALLOCATED (set, map_kind: true); |
| 998 | } |
| 999 | |
| 1000 | /* Returns the number of macro maps used in the line table SET. */ |
| 1001 | inline line_map_uint_t |
| 1002 | LINEMAPS_MACRO_USED (const line_maps *set) |
| 1003 | { |
| 1004 | return LINEMAPS_USED (set, map_kind: true); |
| 1005 | } |
| 1006 | |
| 1007 | /* Returns the last macro map used in the line table SET. */ |
| 1008 | inline line_map_macro * |
| 1009 | LINEMAPS_LAST_MACRO_MAP (const line_maps *set) |
| 1010 | { |
| 1011 | return (line_map_macro *)LINEMAPS_LAST_MAP (set, map_kind: true); |
| 1012 | } |
| 1013 | |
| 1014 | /* Returns the lowest location [of a token resulting from macro |
| 1015 | expansion] encoded in this line table. */ |
| 1016 | inline location_t |
| 1017 | LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps *set) |
| 1018 | { |
| 1019 | return LINEMAPS_MACRO_USED (set) |
| 1020 | ? MAP_START_LOCATION (map: LINEMAPS_LAST_MACRO_MAP (set)) |
| 1021 | : MAX_LOCATION_T + 1; |
| 1022 | } |
| 1023 | |
| 1024 | extern void *get_data_from_adhoc_loc (const line_maps *, location_t); |
| 1025 | extern unsigned get_discriminator_from_adhoc_loc (const line_maps *, location_t); |
| 1026 | extern location_t get_location_from_adhoc_loc (const line_maps *, |
| 1027 | location_t); |
| 1028 | |
| 1029 | extern source_range get_range_from_loc (const line_maps *set, location_t loc); |
| 1030 | extern unsigned get_discriminator_from_loc (const line_maps *set, location_t loc); |
| 1031 | |
| 1032 | /* Get whether location LOC is a "pure" location, or |
| 1033 | whether it is an ad-hoc location, or embeds range information. */ |
| 1034 | |
| 1035 | bool |
| 1036 | pure_location_p (const line_maps *set, location_t loc); |
| 1037 | |
| 1038 | /* Given location LOC within SET, strip away any packed range information |
| 1039 | or ad-hoc information. */ |
| 1040 | |
| 1041 | extern location_t get_pure_location (const line_maps *set, location_t loc); |
| 1042 | |
| 1043 | extern void rebuild_location_adhoc_htab (class line_maps *); |
| 1044 | |
| 1045 | /* Initialize a line map set. SET is the line map set to initialize |
| 1046 | and BUILTIN_LOCATION is the special location value to be used as |
| 1047 | spelling location for built-in tokens. This BUILTIN_LOCATION has |
| 1048 | to be strictly less than RESERVED_LOCATION_COUNT. */ |
| 1049 | extern void linemap_init (class line_maps *set, |
| 1050 | location_t builtin_location); |
| 1051 | |
| 1052 | /* Check for and warn about line_maps entered but not exited. */ |
| 1053 | |
| 1054 | extern void linemap_check_files_exited (const line_maps *); |
| 1055 | |
| 1056 | /* Return a location_t for the start (i.e. column==0) of |
| 1057 | (physical) line TO_LINE in the current source file (as in the |
| 1058 | most recent linemap_add). MAX_COLUMN_HINT is the highest column |
| 1059 | number we expect to use in this line (but it does not change |
| 1060 | the highest_location). */ |
| 1061 | |
| 1062 | extern location_t linemap_line_start |
| 1063 | (class line_maps *set, linenum_type to_line, unsigned int max_column_hint); |
| 1064 | |
| 1065 | /* Allocate a raw block of line maps, zero initialized. */ |
| 1066 | extern line_map *line_map_new_raw (line_maps *, bool, line_map_uint_t); |
| 1067 | |
| 1068 | /* Add a mapping of logical source line to physical source file and |
| 1069 | line number. This function creates an "ordinary map", which is a |
| 1070 | map that records locations of tokens that are not part of macro |
| 1071 | replacement-lists present at a macro expansion point. |
| 1072 | |
| 1073 | The text pointed to by TO_FILE must have a lifetime |
| 1074 | at least as long as the lifetime of SET. An empty |
| 1075 | TO_FILE means standard input. If reason is LC_LEAVE, and |
| 1076 | TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their |
| 1077 | natural values considering the file we are returning to. |
| 1078 | |
| 1079 | A call to this function can relocate the previous set of |
| 1080 | maps, so any stored line_map pointers should not be used. */ |
| 1081 | extern const line_map *linemap_add |
| 1082 | (class line_maps *, enum lc_reason, unsigned int sysp, |
| 1083 | const char *to_file, linenum_type to_line); |
| 1084 | |
| 1085 | /* Create a macro map. A macro map encodes source locations of tokens |
| 1086 | that are part of a macro replacement-list, at a macro expansion |
| 1087 | point. See the extensive comments of struct line_map and struct |
| 1088 | line_map_macro, in line-map.h. |
| 1089 | |
| 1090 | This map shall be created when the macro is expanded. The map |
| 1091 | encodes the source location of the expansion point of the macro as |
| 1092 | well as the "original" source location of each token that is part |
| 1093 | of the macro replacement-list. If a macro is defined but never |
| 1094 | expanded, it has no macro map. SET is the set of maps the macro |
| 1095 | map should be part of. MACRO_NODE is the macro which the new macro |
| 1096 | map should encode source locations for. EXPANSION is the location |
| 1097 | of the expansion point of MACRO. For function-like macros |
| 1098 | invocations, it's best to make it point to the closing parenthesis |
| 1099 | of the macro, rather than the the location of the first character |
| 1100 | of the macro. NUM_TOKENS is the number of tokens that are part of |
| 1101 | the replacement-list of MACRO. */ |
| 1102 | const line_map_macro *linemap_enter_macro (line_maps *, cpp_hashnode *, |
| 1103 | location_t, unsigned int); |
| 1104 | |
| 1105 | /* Create a source location for a module. The creator must either do |
| 1106 | this after the TU is tokenized, or deal with saving and restoring |
| 1107 | map state. */ |
| 1108 | |
| 1109 | extern location_t linemap_module_loc |
| 1110 | (line_maps *, location_t from, const char *name); |
| 1111 | extern void linemap_module_reparent |
| 1112 | (line_maps *, location_t loc, location_t new_parent); |
| 1113 | |
| 1114 | /* Restore the linemap state such that the map at LWM-1 continues. |
| 1115 | Return start location of the new map. */ |
| 1116 | extern location_t linemap_module_restore |
| 1117 | (line_maps *, line_map_uint_t lwm); |
| 1118 | |
| 1119 | /* Given a logical source location, returns the map which the |
| 1120 | corresponding (source file, line, column) triplet can be deduced |
| 1121 | from. Since the set is built chronologically, the logical lines are |
| 1122 | monotonic increasing, and so the list is sorted and we can use a |
| 1123 | binary search. If no line map have been allocated yet, this |
| 1124 | function returns NULL. */ |
| 1125 | extern const line_map *linemap_lookup |
| 1126 | (const line_maps *, location_t); |
| 1127 | |
| 1128 | line_map_uint_t linemap_lookup_macro_index (const line_maps *, location_t); |
| 1129 | |
| 1130 | /* Returns TRUE if the line table set tracks token locations across |
| 1131 | macro expansion, FALSE otherwise. */ |
| 1132 | bool linemap_tracks_macro_expansion_locs_p (const line_maps *); |
| 1133 | |
| 1134 | /* Return the name of the macro associated to MACRO_MAP. */ |
| 1135 | const char* linemap_map_get_macro_name (const line_map_macro *); |
| 1136 | |
| 1137 | /* Return a positive value if LOCATION is the locus of a token that is |
| 1138 | located in a system header, O otherwise. It returns 1 if LOCATION |
| 1139 | is the locus of a token that is located in a system header, and 2 |
| 1140 | if LOCATION is the locus of a token located in a C system header |
| 1141 | that therefore needs to be extern "C" protected in C++. |
| 1142 | |
| 1143 | Note that this function returns 1 if LOCATION belongs to a token |
| 1144 | that is part of a macro replacement-list defined in a system |
| 1145 | header, but expanded in a non-system file. */ |
| 1146 | int (const line_maps *, |
| 1147 | location_t); |
| 1148 | |
| 1149 | /* Return TRUE if LOCATION is a source code location of a token that is part of |
| 1150 | a macro expansion, FALSE otherwise. */ |
| 1151 | bool linemap_location_from_macro_expansion_p (const line_maps *, |
| 1152 | location_t); |
| 1153 | |
| 1154 | /* TRUE if LOCATION is a source code location of a token that is part of the |
| 1155 | definition of a macro, FALSE otherwise. */ |
| 1156 | bool linemap_location_from_macro_definition_p (const line_maps *, |
| 1157 | location_t); |
| 1158 | |
| 1159 | /* With the precondition that LOCATION is the locus of a token that is |
| 1160 | an argument of a function-like macro MACRO_MAP and appears in the |
| 1161 | expansion of MACRO_MAP, return the locus of that argument in the |
| 1162 | context of the caller of MACRO_MAP. */ |
| 1163 | |
| 1164 | extern location_t |
| 1165 | linemap_macro_map_loc_unwind_toward_spelling (const line_maps *set, |
| 1166 | const line_map_macro *macro_map, |
| 1167 | location_t location); |
| 1168 | |
| 1169 | /* location_t values from 0 to RESERVED_LOCATION_COUNT-1 will |
| 1170 | be reserved for libcpp user as special values, no token from libcpp |
| 1171 | will contain any of those locations. */ |
| 1172 | const location_t RESERVED_LOCATION_COUNT = 2; |
| 1173 | |
| 1174 | /* Converts a map and a location_t to source line. */ |
| 1175 | inline linenum_type |
| 1176 | SOURCE_LINE (const line_map_ordinary *ord_map, location_t loc) |
| 1177 | { |
| 1178 | return ((loc - ord_map->start_location) |
| 1179 | >> ord_map->m_column_and_range_bits) + ord_map->to_line; |
| 1180 | } |
| 1181 | |
| 1182 | /* Convert a map and location_t to source column number. */ |
| 1183 | inline linenum_type |
| 1184 | SOURCE_COLUMN (const line_map_ordinary *ord_map, location_t loc) |
| 1185 | { |
| 1186 | return ((loc - ord_map->start_location) |
| 1187 | & ((location_t (1) << ord_map->m_column_and_range_bits) - 1)) |
| 1188 | >> ord_map->m_range_bits; |
| 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | inline location_t |
| 1193 | linemap_included_from (const line_map_ordinary *ord_map) |
| 1194 | { |
| 1195 | return ord_map->included_from; |
| 1196 | } |
| 1197 | |
| 1198 | /* The linemap containing the included-from location of MAP. */ |
| 1199 | const line_map_ordinary * |
| 1200 | linemap_included_from_linemap (const line_maps *set, |
| 1201 | const line_map_ordinary *map); |
| 1202 | |
| 1203 | /* True if the map is at the bottom of the include stack. */ |
| 1204 | |
| 1205 | inline bool |
| 1206 | MAIN_FILE_P (const line_map_ordinary *ord_map) |
| 1207 | { |
| 1208 | return ord_map->included_from == 0; |
| 1209 | } |
| 1210 | |
| 1211 | /* Encode and return a location_t from a column number. The |
| 1212 | source line considered is the last source line used to call |
| 1213 | linemap_line_start, i.e, the last source line which a location was |
| 1214 | encoded from. */ |
| 1215 | extern location_t |
| 1216 | linemap_position_for_column (class line_maps *, unsigned int); |
| 1217 | |
| 1218 | /* Encode and return a source location from a given line and |
| 1219 | column. */ |
| 1220 | location_t |
| 1221 | linemap_position_for_line_and_column (line_maps *set, |
| 1222 | const line_map_ordinary *, |
| 1223 | linenum_type, unsigned int); |
| 1224 | |
| 1225 | /* Encode and return a location_t starting from location LOC and |
| 1226 | shifting it by OFFSET columns. This function does not support |
| 1227 | virtual locations. */ |
| 1228 | location_t |
| 1229 | linemap_position_for_loc_and_offset (class line_maps *set, |
| 1230 | location_t loc, |
| 1231 | unsigned int offset); |
| 1232 | |
| 1233 | /* Return the file this map is for. */ |
| 1234 | inline const char * |
| 1235 | LINEMAP_FILE (const line_map_ordinary *ord_map) |
| 1236 | { |
| 1237 | return ord_map->to_file; |
| 1238 | } |
| 1239 | |
| 1240 | /* Return the line number this map started encoding location from. */ |
| 1241 | inline linenum_type |
| 1242 | LINEMAP_LINE (const line_map_ordinary *ord_map) |
| 1243 | { |
| 1244 | return ord_map->to_line; |
| 1245 | } |
| 1246 | |
| 1247 | /* Return a positive value if map encodes locations from a system |
| 1248 | header, 0 otherwise. Returns 1 if MAP encodes locations in a |
| 1249 | system header and 2 if it encodes locations in a C system header |
| 1250 | that therefore needs to be extern "C" protected in C++. */ |
| 1251 | inline unsigned char |
| 1252 | LINEMAP_SYSP (const line_map_ordinary *ord_map) |
| 1253 | { |
| 1254 | return ord_map->sysp; |
| 1255 | } |
| 1256 | |
| 1257 | const struct line_map *first_map_in_common (const line_maps *set, |
| 1258 | location_t loc0, |
| 1259 | location_t loc1, |
| 1260 | location_t *res_loc0, |
| 1261 | location_t *res_loc1); |
| 1262 | |
| 1263 | /* Return a positive value if PRE denotes the location of a token that |
| 1264 | comes before the token of POST, 0 if PRE denotes the location of |
| 1265 | the same token as the token for POST, and a negative value |
| 1266 | otherwise. */ |
| 1267 | int |
| 1268 | linemap_compare_locations (const line_maps *set, |
| 1269 | location_t pre, |
| 1270 | location_t post); |
| 1271 | |
| 1272 | /* Return TRUE if LOC_A denotes the location a token that comes |
| 1273 | topogically before the token denoted by location LOC_B, or if they |
| 1274 | are equal. */ |
| 1275 | inline bool |
| 1276 | linemap_location_before_p (const line_maps *set, |
| 1277 | location_t loc_a, |
| 1278 | location_t loc_b) |
| 1279 | { |
| 1280 | return linemap_compare_locations (set, pre: loc_a, post: loc_b) >= 0; |
| 1281 | } |
| 1282 | |
| 1283 | struct expanded_location |
| 1284 | { |
| 1285 | /* The name of the source file involved. */ |
| 1286 | const char *file; |
| 1287 | |
| 1288 | /* The line-location in the source file. */ |
| 1289 | int line; |
| 1290 | |
| 1291 | int column; |
| 1292 | |
| 1293 | void *data; |
| 1294 | |
| 1295 | /* In a system header?. */ |
| 1296 | bool sysp; |
| 1297 | }; |
| 1298 | |
| 1299 | extern bool |
| 1300 | operator== (const expanded_location &a, |
| 1301 | const expanded_location &b); |
| 1302 | inline bool |
| 1303 | operator!= (const expanded_location &a, |
| 1304 | const expanded_location &b) |
| 1305 | { |
| 1306 | return !(a == b); |
| 1307 | } |
| 1308 | |
| 1309 | |
| 1310 | /* This is enum is used by the function linemap_resolve_location |
| 1311 | below. The meaning of the values is explained in the comment of |
| 1312 | that function. */ |
| 1313 | enum location_resolution_kind |
| 1314 | { |
| 1315 | LRK_MACRO_EXPANSION_POINT, |
| 1316 | LRK_SPELLING_LOCATION, |
| 1317 | LRK_MACRO_DEFINITION_LOCATION |
| 1318 | }; |
| 1319 | |
| 1320 | /* Resolve a virtual location into either a spelling location, an |
| 1321 | expansion point location or a token argument replacement point |
| 1322 | location. Return the map that encodes the virtual location as well |
| 1323 | as the resolved location. |
| 1324 | |
| 1325 | If LOC is *NOT* the location of a token resulting from the |
| 1326 | expansion of a macro, then the parameter LRK (which stands for |
| 1327 | Location Resolution Kind) is ignored and the resulting location |
| 1328 | just equals the one given in argument. |
| 1329 | |
| 1330 | Now if LOC *IS* the location of a token resulting from the |
| 1331 | expansion of a macro, this is what happens. |
| 1332 | |
| 1333 | * If LRK is set to LRK_MACRO_EXPANSION_POINT |
| 1334 | ------------------------------- |
| 1335 | |
| 1336 | The virtual location is resolved to the first macro expansion point |
| 1337 | that led to this macro expansion. |
| 1338 | |
| 1339 | * If LRK is set to LRK_SPELLING_LOCATION |
| 1340 | ------------------------------------- |
| 1341 | |
| 1342 | The virtual location is resolved to the locus where the token has |
| 1343 | been spelled in the source. This can follow through all the macro |
| 1344 | expansions that led to the token. |
| 1345 | |
| 1346 | * If LRK is set to LRK_MACRO_DEFINITION_LOCATION |
| 1347 | -------------------------------------- |
| 1348 | |
| 1349 | The virtual location is resolved to the locus of the token in the |
| 1350 | context of the macro definition. |
| 1351 | |
| 1352 | If LOC is the locus of a token that is an argument of a |
| 1353 | function-like macro [replacing a parameter in the replacement list |
| 1354 | of the macro] the virtual location is resolved to the locus of the |
| 1355 | parameter that is replaced, in the context of the definition of the |
| 1356 | macro. |
| 1357 | |
| 1358 | If LOC is the locus of a token that is not an argument of a |
| 1359 | function-like macro, then the function behaves as if LRK was set to |
| 1360 | LRK_SPELLING_LOCATION. |
| 1361 | |
| 1362 | If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the |
| 1363 | returned location. Note that if the returned location wasn't originally |
| 1364 | encoded by a map, the *MAP is set to NULL. This can happen if LOC |
| 1365 | resolves to a location reserved for the client code, like |
| 1366 | UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */ |
| 1367 | |
| 1368 | location_t linemap_resolve_location (const line_maps *, |
| 1369 | location_t loc, |
| 1370 | enum location_resolution_kind lrk, |
| 1371 | const line_map_ordinary **loc_map); |
| 1372 | |
| 1373 | /* Suppose that LOC is the virtual location of a token coming from the |
| 1374 | expansion of a macro M. This function then steps up to get the |
| 1375 | location L of the point where M got expanded. If L is a spelling |
| 1376 | location inside a macro expansion M', then this function returns |
| 1377 | the point where M' was expanded. LOC_MAP is an output parameter. |
| 1378 | When non-NULL, *LOC_MAP is set to the map of the returned |
| 1379 | location. */ |
| 1380 | location_t linemap_unwind_toward_expansion (const line_maps *, |
| 1381 | location_t loc, |
| 1382 | const line_map **loc_map); |
| 1383 | |
| 1384 | /* If LOC is the virtual location of a token coming from the expansion |
| 1385 | of a macro M and if its spelling location is reserved (e.g, a |
| 1386 | location for a built-in token), then this function unwinds (using |
| 1387 | linemap_unwind_toward_expansion) the location until a location that |
| 1388 | is not reserved and is not in a system header is reached. In other |
| 1389 | words, this unwinds the reserved location until a location that is |
| 1390 | in real source code is reached. |
| 1391 | |
| 1392 | Otherwise, if the spelling location for LOC is not reserved or if |
| 1393 | LOC doesn't come from the expansion of a macro, the function |
| 1394 | returns LOC as is and *MAP is not touched. |
| 1395 | |
| 1396 | *MAP is set to the map of the returned location if the later is |
| 1397 | different from LOC. */ |
| 1398 | location_t linemap_unwind_to_first_non_reserved_loc (const line_maps *, |
| 1399 | location_t loc, |
| 1400 | const line_map **map); |
| 1401 | |
| 1402 | /* Expand source code location LOC and return a user readable source |
| 1403 | code location. LOC must be a spelling (non-virtual) location. If |
| 1404 | it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source |
| 1405 | location is returned. */ |
| 1406 | expanded_location linemap_expand_location (const line_maps *, |
| 1407 | const line_map *, |
| 1408 | location_t loc); |
| 1409 | |
| 1410 | /* Statistics about maps allocation and usage as returned by |
| 1411 | linemap_get_statistics. */ |
| 1412 | struct linemap_stats |
| 1413 | { |
| 1414 | long num_ordinary_maps_allocated; |
| 1415 | long num_ordinary_maps_used; |
| 1416 | long ordinary_maps_allocated_size; |
| 1417 | long ordinary_maps_used_size; |
| 1418 | long num_expanded_macros; |
| 1419 | long num_macro_tokens; |
| 1420 | long num_macro_maps_used; |
| 1421 | long macro_maps_allocated_size; |
| 1422 | long macro_maps_used_size; |
| 1423 | long macro_maps_locations_size; |
| 1424 | long duplicated_macro_maps_locations_size; |
| 1425 | long adhoc_table_size; |
| 1426 | long adhoc_table_entries_used; |
| 1427 | }; |
| 1428 | |
| 1429 | /* Return the highest location emitted for a given file for which |
| 1430 | there is a line map in SET. FILE_NAME is the file name to |
| 1431 | consider. If the function returns TRUE, *LOC is set to the highest |
| 1432 | location emitted for that file. */ |
| 1433 | bool linemap_get_file_highest_location (const line_maps * set, |
| 1434 | const char *file_name, |
| 1435 | location_t *loc); |
| 1436 | |
| 1437 | /* Compute and return statistics about the memory consumption of some |
| 1438 | parts of the line table SET. */ |
| 1439 | void linemap_get_statistics (const line_maps *, struct linemap_stats *); |
| 1440 | |
| 1441 | /* Dump debugging information about source location LOC into the file |
| 1442 | stream STREAM. SET is the line map set LOC comes from. */ |
| 1443 | void linemap_dump_location (const line_maps *, location_t, FILE *); |
| 1444 | |
| 1445 | /* Dump line map at index IX in line table SET to STREAM. If STREAM |
| 1446 | is NULL, use stderr. IS_MACRO is true if the caller wants to |
| 1447 | dump a macro map, false otherwise. */ |
| 1448 | void linemap_dump (FILE *, const line_maps *, line_map_uint_t, bool); |
| 1449 | |
| 1450 | /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used. |
| 1451 | NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO |
| 1452 | specifies how many macro maps to dump. */ |
| 1453 | void line_table_dump (FILE *, const line_maps *, |
| 1454 | line_map_uint_t, line_map_uint_t); |
| 1455 | |
| 1456 | /* An enum for distinguishing the various parts within a location_t. */ |
| 1457 | |
| 1458 | enum location_aspect |
| 1459 | { |
| 1460 | LOCATION_ASPECT_CARET, |
| 1461 | LOCATION_ASPECT_START, |
| 1462 | LOCATION_ASPECT_FINISH |
| 1463 | }; |
| 1464 | |
| 1465 | /* The rich_location class requires a way to expand location_t instances. |
| 1466 | We would directly use expand_location_to_spelling_point, which is |
| 1467 | implemented in gcc/input.cc, but we also need to use it for rich_location |
| 1468 | within genmatch.cc. |
| 1469 | Hence we require client code of libcpp to implement the following |
| 1470 | symbol. */ |
| 1471 | extern expanded_location |
| 1472 | linemap_client_expand_location_to_spelling_point (const line_maps *, |
| 1473 | location_t, |
| 1474 | enum location_aspect); |
| 1475 | |
| 1476 | #endif /* !LIBCPP_LINE_MAP_H */ |
| 1477 | |