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