]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcpp/line-map.c
Fix thinko in indirect_jump_optimize
[thirdparty/gcc.git] / libcpp / line-map.c
CommitLineData
78536ab7 1/* Map (unsigned int) keys to (source file, line, column) triples.
f1717362 2 Copyright (C) 2001-2016 Free Software Foundation, Inc.
38692459 3
4This program is free software; you can redistribute it and/or modify it
5under the terms of the GNU General Public License as published by the
6bc9506f 6Free Software Foundation; either version 3, or (at your option) any
38692459 7later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
6bc9506f 15along with this program; see the file COPYING3. If not see
16<http://www.gnu.org/licenses/>.
38692459 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#include "config.h"
23#include "system.h"
24#include "line-map.h"
97bfb9ef 25#include "cpplib.h"
26#include "internal.h"
5169661d 27#include "hashtab.h"
38692459 28
0d344d27 29/* Do not track column numbers higher than this one. As a result, the
a96cefb2 30 range of column_bits is [12, 18] (or 0 if column numbers are
0d344d27 31 disabled). */
a96cefb2 32const unsigned int LINE_MAP_MAX_COLUMN_NUMBER = (1U << 12);
0d344d27 33
a7ed4583 34/* Do not pack ranges if locations get higher than this.
35 If you change this, update:
36 gcc.dg/plugin/location_overflow_plugin.c
37 gcc.dg/plugin/location-overflow-test-*.c. */
38const source_location LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES = 0x50000000;
39
40/* Do not track column numbers if locations get higher than this.
41 If you change this, update:
42 gcc.dg/plugin/location_overflow_plugin.c
43 gcc.dg/plugin/location-overflow-test-*.c. */
0d344d27 44const source_location LINE_MAP_MAX_LOCATION_WITH_COLS = 0x60000000;
45
46/* Highest possible source location encoded within an ordinary or
47 macro map. */
48const source_location LINE_MAP_MAX_SOURCE_LOCATION = 0x70000000;
49
551e34da 50static void trace_include (const struct line_maps *, const line_map_ordinary *);
51static const line_map_ordinary * linemap_ordinary_map_lookup (struct line_maps *,
52 source_location);
53static const line_map_macro* linemap_macro_map_lookup (struct line_maps *,
54 source_location);
97bfb9ef 55static source_location linemap_macro_map_loc_to_def_point
551e34da 56(const line_map_macro *, source_location);
97bfb9ef 57static source_location linemap_macro_map_loc_to_exp_point
551e34da 58(const line_map_macro *, source_location);
97bfb9ef 59static source_location linemap_macro_loc_to_spelling_point
551e34da 60(struct line_maps *, source_location, const line_map_ordinary **);
97bfb9ef 61static source_location linemap_macro_loc_to_def_point (struct line_maps *,
62 source_location,
551e34da 63 const line_map_ordinary **);
97bfb9ef 64static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
65 source_location,
551e34da 66 const line_map_ordinary **);
438ac94c 67
e77b8253 68/* Counters defined in macro.c. */
69extern unsigned num_expanded_macros_counter;
70extern unsigned num_macro_tokens_counter;
71
5169661d 72/* Hash function for location_adhoc_data hashtable. */
73
74static hashval_t
75location_adhoc_data_hash (const void *l)
76{
77 const struct location_adhoc_data *lb =
78 (const struct location_adhoc_data *) l;
a96cefb2 79 return ((hashval_t) lb->locus
80 + (hashval_t) lb->src_range.m_start
81 + (hashval_t) lb->src_range.m_finish
82 + (size_t) lb->data);
5169661d 83}
84
85/* Compare function for location_adhoc_data hashtable. */
86
87static int
88location_adhoc_data_eq (const void *l1, const void *l2)
89{
90 const struct location_adhoc_data *lb1 =
91 (const struct location_adhoc_data *) l1;
92 const struct location_adhoc_data *lb2 =
93 (const struct location_adhoc_data *) l2;
a96cefb2 94 return (lb1->locus == lb2->locus
95 && lb1->src_range.m_start == lb2->src_range.m_start
96 && lb1->src_range.m_finish == lb2->src_range.m_finish
97 && lb1->data == lb2->data);
5169661d 98}
99
100/* Update the hashtable when location_adhoc_data is reallocated. */
101
102static int
103location_adhoc_data_update (void **slot, void *data)
104{
105 *((char **) slot) += *((long long *) data);
106 return 1;
107}
108
5c1946c8 109/* Rebuild the hash table from the location adhoc data. */
110
111void
112rebuild_location_adhoc_htab (struct line_maps *set)
113{
114 unsigned i;
115 set->location_adhoc_data_map.htab =
116 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
117 for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
118 htab_find_slot (set->location_adhoc_data_map.htab,
119 set->location_adhoc_data_map.data + i, INSERT);
120}
121
a96cefb2 122/* Helper function for get_combined_adhoc_loc.
123 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
124 within a source_location, without needing to use an ad-hoc location. */
125
126static bool
127can_be_stored_compactly_p (struct line_maps *set,
128 source_location locus,
129 source_range src_range,
130 void *data)
131{
132 /* If there's an ad-hoc pointer, we can't store it directly in the
133 source_location, we need the lookaside. */
134 if (data)
135 return false;
136
137 /* We only store ranges that begin at the locus and that are sufficiently
138 "sane". */
139 if (src_range.m_start != locus)
140 return false;
141
142 if (src_range.m_finish < src_range.m_start)
143 return false;
144
145 if (src_range.m_start < RESERVED_LOCATION_COUNT)
146 return false;
147
a7ed4583 148 if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
a96cefb2 149 return false;
150
151 /* All 3 locations must be within ordinary maps, typically, the same
152 ordinary map. */
153 source_location lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
154 if (locus >= lowest_macro_loc)
155 return false;
156 if (src_range.m_start >= lowest_macro_loc)
157 return false;
158 if (src_range.m_finish >= lowest_macro_loc)
159 return false;
160
161 /* Passed all tests. */
162 return true;
163}
164
5169661d 165/* Combine LOCUS and DATA to a combined adhoc loc. */
166
167source_location
168get_combined_adhoc_loc (struct line_maps *set,
a96cefb2 169 source_location locus,
170 source_range src_range,
171 void *data)
5169661d 172{
173 struct location_adhoc_data lb;
174 struct location_adhoc_data **slot;
175
5169661d 176 if (IS_ADHOC_LOC (locus))
6e5a7913 177 locus
178 = set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
5169661d 179 if (locus == 0 && data == NULL)
180 return 0;
a96cefb2 181
182 /* Any ordinary locations ought to be "pure" at this point: no
183 compressed ranges. */
184 linemap_assert (locus < RESERVED_LOCATION_COUNT
a7ed4583 185 || locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
a96cefb2 186 || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
187 || pure_location_p (set, locus));
188
189 /* Consider short-range optimization. */
190 if (can_be_stored_compactly_p (set, locus, src_range, data))
191 {
192 /* The low bits ought to be clear. */
193 linemap_assert (pure_location_p (set, locus));
194 const line_map *map = linemap_lookup (set, locus);
195 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
196 unsigned int int_diff = src_range.m_finish - src_range.m_start;
197 unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
198 if (col_diff < (1U << ordmap->m_range_bits))
199 {
200 source_location packed = locus | col_diff;
201 set->num_optimized_ranges++;
202 return packed;
203 }
204 }
205
7b4b11d3 206 /* We can also compactly store locations
a96cefb2 207 when locus == start == finish (and data is NULL). */
7b4b11d3 208 if (locus == src_range.m_start
a96cefb2 209 && locus == src_range.m_finish
210 && !data)
211 return locus;
212
213 if (!data)
214 set->num_unoptimized_ranges++;
215
5169661d 216 lb.locus = locus;
a96cefb2 217 lb.src_range = src_range;
5169661d 218 lb.data = data;
219 slot = (struct location_adhoc_data **)
220 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
221 if (*slot == NULL)
222 {
223 if (set->location_adhoc_data_map.curr_loc >=
224 set->location_adhoc_data_map.allocated)
225 {
226 char *orig_data = (char *) set->location_adhoc_data_map.data;
227 long long offset;
de2e6b8a 228 /* Cast away extern "C" from the type of xrealloc. */
229 line_map_realloc reallocator = (set->reallocator
230 ? set->reallocator
231 : (line_map_realloc) xrealloc);
5c1946c8 232
233 if (set->location_adhoc_data_map.allocated == 0)
234 set->location_adhoc_data_map.allocated = 128;
235 else
236 set->location_adhoc_data_map.allocated *= 2;
237 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
238 reallocator (set->location_adhoc_data_map.data,
239 set->location_adhoc_data_map.allocated
240 * sizeof (struct location_adhoc_data));
5169661d 241 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
5c1946c8 242 if (set->location_adhoc_data_map.allocated > 128)
243 htab_traverse (set->location_adhoc_data_map.htab,
244 location_adhoc_data_update, &offset);
5169661d 245 }
246 *slot = set->location_adhoc_data_map.data
247 + set->location_adhoc_data_map.curr_loc;
6e5a7913 248 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
249 = lb;
5169661d 250 }
251 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
252}
253
254/* Return the data for the adhoc loc. */
255
256void *
257get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
258{
259 linemap_assert (IS_ADHOC_LOC (loc));
260 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
261}
262
263/* Return the location for the adhoc loc. */
264
265source_location
266get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
267{
268 linemap_assert (IS_ADHOC_LOC (loc));
269 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
270}
271
a96cefb2 272/* Return the source_range for adhoc location LOC. */
273
274static source_range
275get_range_from_adhoc_loc (struct line_maps *set, source_location loc)
276{
277 linemap_assert (IS_ADHOC_LOC (loc));
278 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].src_range;
279}
280
281/* Get the source_range of location LOC, either from the ad-hoc
282 lookaside table, or embedded inside LOC itself. */
283
284source_range
285get_range_from_loc (struct line_maps *set,
286 source_location loc)
287{
288 if (IS_ADHOC_LOC (loc))
289 return get_range_from_adhoc_loc (set, loc);
290
291 /* For ordinary maps, extract packed range. */
292 if (loc >= RESERVED_LOCATION_COUNT
293 && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
a7ed4583 294 && loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
a96cefb2 295 {
296 const line_map *map = linemap_lookup (set, loc);
297 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
298 source_range result;
299 int offset = loc & ((1 << ordmap->m_range_bits) - 1);
300 result.m_start = loc - offset;
301 result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
302 return result;
303 }
304
305 return source_range::from_location (loc);
306}
307
308/* Get whether location LOC is a "pure" location, or
309 whether it is an ad-hoc location, or embeds range information. */
310
311bool
312pure_location_p (line_maps *set, source_location loc)
313{
314 if (IS_ADHOC_LOC (loc))
315 return false;
316
317 const line_map *map = linemap_lookup (set, loc);
318 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
319
320 if (loc & ((1U << ordmap->m_range_bits) - 1))
321 return false;
322
323 return true;
324}
325
5169661d 326/* Finalize the location_adhoc_data structure. */
327void
328location_adhoc_data_fini (struct line_maps *set)
329{
5169661d 330 htab_delete (set->location_adhoc_data_map.htab);
331}
332
38692459 333/* Initialize a line map set. */
334
335void
a4cfdfed 336linemap_init (struct line_maps *set,
337 source_location builtin_location)
38692459 338{
97bfb9ef 339 memset (set, 0, sizeof (struct line_maps));
af4d2883 340 set->highest_location = RESERVED_LOCATION_COUNT - 1;
341 set->highest_line = RESERVED_LOCATION_COUNT - 1;
5c1946c8 342 set->location_adhoc_data_map.htab =
343 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
a4cfdfed 344 set->builtin_location = builtin_location;
38692459 345}
346
7c2df241 347/* Check for and warn about line_maps entered but not exited. */
610625e3 348
349void
350linemap_check_files_exited (struct line_maps *set)
351{
551e34da 352 const line_map_ordinary *map;
610625e3 353 /* Depending upon whether we are handling preprocessed input or
354 not, this can be a user error or an ICE. */
97bfb9ef 355 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
356 ! MAIN_FILE_P (map);
610625e3 357 map = INCLUDED_FROM (set, map))
358 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
97bfb9ef 359 ORDINARY_MAP_FILE_NAME (map));
610625e3 360}
38692459 361
97bfb9ef 362/* Create a new line map in the line map set SET, and return it.
363 REASON is the reason of creating the map. It determines the type
364 of map created (ordinary or macro map). Note that ordinary maps and
365 macro maps are allocated in different memory location. */
366
367static struct line_map *
368new_linemap (struct line_maps *set,
369 enum lc_reason reason)
38692459 370{
97bfb9ef 371 /* Depending on this variable, a macro map would be allocated in a
372 different memory location than an ordinary map. */
373 bool macro_map_p = (reason == LC_ENTER_MACRO);
374 struct line_map *result;
375
376 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
bd507c05 377 {
97bfb9ef 378 /* We ran out of allocated line maps. Let's allocate more. */
1ae3520e 379 unsigned alloc_size;
f85fcf2b 380
de2e6b8a 381 /* Cast away extern "C" from the type of xrealloc. */
382 line_map_realloc reallocator = (set->reallocator
383 ? set->reallocator
384 : (line_map_realloc) xrealloc);
1ae3520e 385 line_map_round_alloc_size_func round_alloc_size =
386 set->round_alloc_size;
387
551e34da 388 size_t map_size = (macro_map_p
389 ? sizeof (line_map_macro)
390 : sizeof (line_map_ordinary));
391
1ae3520e 392 /* We are going to execute some dance to try to reduce the
393 overhead of the memory allocator, in case we are using the
394 ggc-page.c one.
395
396 The actual size of memory we are going to get back from the
397 allocator is the smallest power of 2 that is greater than the
398 size we requested. So let's consider that size then. */
399
400 alloc_size =
401 (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
551e34da 402 * map_size;
1ae3520e 403
404 /* Get the actual size of memory that is going to be allocated
405 by the allocator. */
406 alloc_size = round_alloc_size (alloc_size);
407
408 /* Now alloc_size contains the exact memory size we would get if
409 we have asked for the initial alloc_size amount of memory.
410 Let's get back to the number of macro map that amounts
411 to. */
97bfb9ef 412 LINEMAPS_ALLOCATED (set, macro_map_p) =
551e34da 413 alloc_size / map_size;
1ae3520e 414
415 /* And now let's really do the re-allocation. */
551e34da 416 if (macro_map_p)
417 {
418 set->info_macro.maps
419 = (line_map_macro *) (*reallocator) (set->info_macro.maps,
420 (LINEMAPS_ALLOCATED (set, macro_map_p)
421 * map_size));
422 result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
423 }
424 else
425 {
426 set->info_ordinary.maps =
427 (line_map_ordinary *) (*reallocator) (set->info_ordinary.maps,
428 (LINEMAPS_ALLOCATED (set, macro_map_p)
429 * map_size));
430 result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
431 }
97bfb9ef 432 memset (result, 0,
433 ((LINEMAPS_ALLOCATED (set, macro_map_p)
434 - LINEMAPS_USED (set, macro_map_p))
551e34da 435 * map_size));
bd507c05 436 }
97bfb9ef 437 else
551e34da 438 {
439 if (macro_map_p)
440 result = &set->info_macro.maps[LINEMAPS_USED (set, macro_map_p)];
441 else
442 result = &set->info_ordinary.maps[LINEMAPS_USED (set, macro_map_p)];
443 }
97bfb9ef 444
445 LINEMAPS_USED (set, macro_map_p)++;
446
447 result->reason = reason;
448 return result;
38692459 449}
450
451/* Add a mapping of logical source line to physical source file and
748b50a3 452 line number.
453
454 The text pointed to by TO_FILE must have a lifetime
455 at least as long as the final call to lookup_line (). An empty
456 TO_FILE means standard input. If reason is LC_LEAVE, and
457 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
458 natural values considering the file we are returning to.
38692459 459
460 FROM_LINE should be monotonic increasing across calls to this
748b50a3 461 function. A call to this function can relocate the previous set of
462 maps, so any stored line_map pointers should not be used. */
38692459 463
f85fcf2b 464const struct line_map *
196ce2be 465linemap_add (struct line_maps *set, enum lc_reason reason,
4999c35b 466 unsigned int sysp, const char *to_file, linenum_type to_line)
38692459 467{
a96cefb2 468 /* Generate a start_location above the current highest_location.
469 If possible, make the low range bits be zero. */
470 source_location start_location;
471 if (set->highest_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
472 {
473 start_location = set->highest_location + (1 << set->default_range_bits);
474 if (set->default_range_bits)
475 start_location &= ~((1 << set->default_range_bits) - 1);
476 linemap_assert (0 == (start_location
477 & ((1 << set->default_range_bits) - 1)));
478 }
479 else
480 start_location = set->highest_location + 1;
38692459 481
97bfb9ef 482 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
483 && (start_location
484 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
485
486 /* When we enter the file for the first time reason cannot be
487 LC_RENAME. */
488 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
38692459 489
97bfb9ef 490 /* If we are leaving the main file, return a NULL map. */
491 if (reason == LC_LEAVE
492 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
493 && to_file == NULL)
38692459 494 {
97bfb9ef 495 set->depth--;
496 return NULL;
38692459 497 }
498
551e34da 499 linemap_assert (reason != LC_ENTER_MACRO);
500 line_map_ordinary *map = linemap_check_ordinary (new_linemap (set, reason));
38692459 501
1eecdb28 502 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
748b50a3 503 to_file = "<stdin>";
504
1eecdb28 505 if (reason == LC_RENAME_VERBATIM)
506 reason = LC_RENAME;
507
1dc92c59 508 if (reason == LC_LEAVE)
bd507c05 509 {
97bfb9ef 510 /* When we are just leaving an "included" file, and jump to the next
511 location inside the "includer" right after the #include
512 "included", this variable points the map in use right before the
513 #include "included", inside the same "includer" file. */
551e34da 514 line_map_ordinary *from;
f85fcf2b 515 bool error;
516
517 if (MAIN_FILE_P (map - 1))
bd507c05 518 {
351855a5 519 /* So this _should_ mean we are leaving the main file --
97bfb9ef 520 effectively ending the compilation unit. But to_file not
521 being NULL means the caller thinks we are leaving to
522 another file. This is an erroneous behaviour but we'll
523 try to recover from it. Let's pretend we are not leaving
524 the main file. */
4d1574ae 525 error = true;
526 reason = LC_RENAME;
527 from = map - 1;
f85fcf2b 528 }
529 else
530 {
97bfb9ef 531 /* (MAP - 1) points to the map we are leaving. The
532 map from which (MAP - 1) got included should be the map
533 that comes right before MAP in the same file. */
f85fcf2b 534 from = INCLUDED_FROM (set, map - 1);
97bfb9ef 535 error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
536 to_file);
f85fcf2b 537 }
538
539 /* Depending upon whether we are handling preprocessed input or
540 not, this can be a user error or an ICE. */
541 if (error)
542 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
543 to_file);
544
545 /* A TO_FILE of NULL is special - we use the natural values. */
546 if (error || to_file == NULL)
547 {
97bfb9ef 548 to_file = ORDINARY_MAP_FILE_NAME (from);
610625e3 549 to_line = SOURCE_LINE (from, from[1].start_location);
97bfb9ef 550 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
bd507c05 551 }
552 }
553
3ce1a212 554 map->sysp = sysp;
555 map->start_location = start_location;
556 map->to_file = to_file;
557 map->to_line = to_line;
97bfb9ef 558 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
a96cefb2 559 map->m_column_and_range_bits = 0;
560 map->m_range_bits = 0;
610625e3 561 set->highest_location = start_location;
dbddc569 562 set->highest_line = start_location;
610625e3 563 set->max_column_hint = 0;
f85fcf2b 564
a96cefb2 565 /* This assertion is placed after set->highest_location has
566 been updated, since the latter affects
567 linemap_location_from_macro_expansion_p, which ultimately affects
568 pure_location_p. */
569 linemap_assert (pure_location_p (set, start_location));
570
bd507c05 571 if (reason == LC_ENTER)
4087823d 572 {
3ce1a212 573 map->included_from =
97bfb9ef 574 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
4087823d 575 set->depth++;
4087823d 576 if (set->trace_includes)
577 trace_include (set, map);
578 }
38692459 579 else if (reason == LC_RENAME)
3ce1a212 580 map->included_from = ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
38692459 581 else if (reason == LC_LEAVE)
4087823d 582 {
583 set->depth--;
3ce1a212 584 map->included_from =
97bfb9ef 585 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
4087823d 586 }
438ac94c 587
38692459 588 return map;
589}
590
e8aa7eaf 591/* Returns TRUE if the line table set tracks token locations across
97bfb9ef 592 macro expansion, FALSE otherwise. */
593
594bool
595linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
596{
597 return LINEMAPS_MACRO_MAPS (set) != NULL;
598}
599
600/* Create a macro map. A macro map encodes source locations of tokens
601 that are part of a macro replacement-list, at a macro expansion
602 point. See the extensive comments of struct line_map and struct
603 line_map_macro, in line-map.h.
604
605 This map shall be created when the macro is expanded. The map
606 encodes the source location of the expansion point of the macro as
607 well as the "original" source location of each token that is part
608 of the macro replacement-list. If a macro is defined but never
609 expanded, it has no macro map. SET is the set of maps the macro
610 map should be part of. MACRO_NODE is the macro which the new macro
611 map should encode source locations for. EXPANSION is the location
612 of the expansion point of MACRO. For function-like macros
613 invocations, it's best to make it point to the closing parenthesis
614 of the macro, rather than the the location of the first character
615 of the macro. NUM_TOKENS is the number of tokens that are part of
616 the replacement-list of MACRO.
617
618 Note that when we run out of the integer space available for source
619 locations, this function returns NULL. In that case, callers of
620 this function cannot encode {line,column} pairs into locations of
621 macro tokens anymore. */
622
551e34da 623const line_map_macro *
97bfb9ef 624linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
625 source_location expansion, unsigned int num_tokens)
626{
551e34da 627 line_map_macro *map;
97bfb9ef 628 source_location start_location;
de2e6b8a 629 /* Cast away extern "C" from the type of xrealloc. */
630 line_map_realloc reallocator = (set->reallocator
631 ? set->reallocator
632 : (line_map_realloc) xrealloc);
97bfb9ef 633
634 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
635
636 if (start_location <= set->highest_line
637 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
638 /* We ran out of macro map space. */
639 return NULL;
640
551e34da 641 map = linemap_check_macro (new_linemap (set, LC_ENTER_MACRO));
97bfb9ef 642
3ce1a212 643 map->start_location = start_location;
644 map->macro = macro_node;
645 map->n_tokens = num_tokens;
646 map->macro_locations
97bfb9ef 647 = (source_location*) reallocator (NULL,
648 2 * num_tokens
649 * sizeof (source_location));
3ce1a212 650 map->expansion = expansion;
97bfb9ef 651 memset (MACRO_MAP_LOCATIONS (map), 0,
652 num_tokens * sizeof (source_location));
653
654 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
97bfb9ef 655
656 return map;
657}
658
659/* Create and return a virtual location for a token that is part of a
660 macro expansion-list at a macro expansion point. See the comment
661 inside struct line_map_macro to see what an expansion-list exactly
662 is.
663
664 A call to this function must come after a call to
665 linemap_enter_macro.
666
667 MAP is the map into which the source location is created. TOKEN_NO
668 is the index of the token in the macro replacement-list, starting
669 at number 0.
670
671 ORIG_LOC is the location of the token outside of this macro
672 expansion. If the token comes originally from the macro
673 definition, it is the locus in the macro definition; otherwise it
674 is a location in the context of the caller of this macro expansion
675 (which is a virtual location or a source location if the caller is
676 itself a macro expansion or not).
677
1fcf9669 678 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
97bfb9ef 679 either of the token itself or of a macro parameter that it
680 replaces. */
681
682source_location
551e34da 683linemap_add_macro_token (const line_map_macro *map,
97bfb9ef 684 unsigned int token_no,
685 source_location orig_loc,
686 source_location orig_parm_replacement_loc)
687{
688 source_location result;
689
690 linemap_assert (linemap_macro_expansion_map_p (map));
691 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
692
693 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
694 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
695
696 result = MAP_START_LOCATION (map) + token_no;
697 return result;
698}
699
700/* Return a source_location for the start (i.e. column==0) of
701 (physical) line TO_LINE in the current source file (as in the
702 most recent linemap_add). MAX_COLUMN_HINT is the highest column
703 number we expect to use in this line (but it does not change
704 the highest_location). */
705
610625e3 706source_location
4999c35b 707linemap_line_start (struct line_maps *set, linenum_type to_line,
610625e3 708 unsigned int max_column_hint)
709{
551e34da 710 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
610625e3 711 source_location highest = set->highest_location;
712 source_location r;
97bfb9ef 713 linenum_type last_line =
714 SOURCE_LINE (map, set->highest_line);
610625e3 715 int line_delta = to_line - last_line;
716 bool add_map = false;
a96cefb2 717 linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
718 int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
97bfb9ef 719
610625e3 720 if (line_delta < 0
97bfb9ef 721 || (line_delta > 10
a96cefb2 722 && line_delta * map->m_column_and_range_bits > 1000)
723 || (max_column_hint >= (1U << effective_column_bits))
724 || (max_column_hint <= 80 && effective_column_bits >= 10)
a7ed4583 725 || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
726 && map->m_range_bits > 0)
0d344d27 727 || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
728 && (set->max_column_hint || highest >= LINE_MAP_MAX_SOURCE_LOCATION)))
1feb1b2d 729 add_map = true;
610625e3 730 else
731 max_column_hint = set->max_column_hint;
732 if (add_map)
733 {
734 int column_bits;
a96cefb2 735 int range_bits;
0d344d27 736 if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
737 || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
610625e3 738 {
0b7f838f 739 /* If the column number is ridiculous or we've allocated a huge
a96cefb2 740 number of source_locations, give up on column numbers
741 (and on packed ranges). */
610625e3 742 max_column_hint = 0;
610625e3 743 column_bits = 0;
a96cefb2 744 range_bits = 0;
0d344d27 745 if (highest > LINE_MAP_MAX_SOURCE_LOCATION)
746 return 0;
610625e3 747 }
748 else
749 {
750 column_bits = 7;
a7ed4583 751 if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
752 range_bits = set->default_range_bits;
753 else
754 range_bits = 0;
610625e3 755 while (max_column_hint >= (1U << column_bits))
756 column_bits++;
757 max_column_hint = 1U << column_bits;
a96cefb2 758 column_bits += range_bits;
610625e3 759 }
0b7f838f 760 /* Allocate the new line_map. However, if the current map only has a
761 single line we can sometimes just increase its column_bits instead. */
610625e3 762 if (line_delta < 0
97bfb9ef 763 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
a7ed4583 764 || SOURCE_COLUMN (map, highest) >= (1U << column_bits)
765 || range_bits < map->m_range_bits)
551e34da 766 map = linemap_check_ordinary
767 (const_cast <line_map *>
768 (linemap_add (set, LC_RENAME,
769 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
770 ORDINARY_MAP_FILE_NAME (map),
771 to_line)));
a96cefb2 772 map->m_column_and_range_bits = column_bits;
773 map->m_range_bits = range_bits;
97bfb9ef 774 r = (MAP_START_LOCATION (map)
775 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
776 << column_bits));
610625e3 777 }
778 else
a96cefb2 779 r = set->highest_line + (line_delta << map->m_column_and_range_bits);
97bfb9ef 780
781 /* Locations of ordinary tokens are always lower than locations of
782 macro tokens. */
783 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
784 return 0;
785
dbddc569 786 set->highest_line = r;
610625e3 787 if (r > set->highest_location)
788 set->highest_location = r;
789 set->max_column_hint = max_column_hint;
a96cefb2 790
791 /* At this point, we expect one of:
792 (a) the normal case: a "pure" location with 0 range bits, or
793 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
794 columns anymore (or ranges), or
795 (c) we're in a region with a column hint exceeding
796 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
797 with column_bits == 0. */
798 linemap_assert (pure_location_p (set, r)
799 || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
800 || map->m_column_and_range_bits == 0);
801 linemap_assert (SOURCE_LINE (map, r) == to_line);
610625e3 802 return r;
803}
804
97bfb9ef 805/* Encode and return a source_location from a column number. The
806 source line considered is the last source line used to call
807 linemap_line_start, i.e, the last source line which a location was
808 encoded from. */
809
dbddc569 810source_location
811linemap_position_for_column (struct line_maps *set, unsigned int to_column)
812{
813 source_location r = set->highest_line;
97bfb9ef 814
815 linemap_assert
816 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
817
dbddc569 818 if (to_column >= set->max_column_hint)
819 {
0d344d27 820 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
821 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
dbddc569 822 {
823 /* Running low on source_locations - disable column numbers. */
824 return r;
825 }
826 else
827 {
551e34da 828 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
dbddc569 829 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
830 }
831 }
a96cefb2 832 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
833 r = r + (to_column << map->m_range_bits);
dbddc569 834 if (r >= set->highest_location)
835 set->highest_location = r;
836 return r;
837}
838
97bfb9ef 839/* Encode and return a source location from a given line and
840 column. */
38692459 841
97bfb9ef 842source_location
a96cefb2 843linemap_position_for_line_and_column (line_maps *set,
844 const line_map_ordinary *ord_map,
97bfb9ef 845 linenum_type line,
846 unsigned column)
847{
551e34da 848 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
97bfb9ef 849
a96cefb2 850 source_location r = MAP_START_LOCATION (ord_map);
851 r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
852 << ord_map->m_column_and_range_bits);
853 if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
854 r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
855 << ord_map->m_range_bits);
856 source_location upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
857 if (r >= upper_limit)
858 r = upper_limit - 1;
859 if (r > set->highest_location)
860 set->highest_location = r;
861 return r;
97bfb9ef 862}
863
766928aa 864/* Encode and return a source_location starting from location LOC and
1ed1f69a 865 shifting it by COLUMN_OFFSET columns. This function does not support
766928aa 866 virtual locations. */
867
868source_location
869linemap_position_for_loc_and_offset (struct line_maps *set,
870 source_location loc,
1ed1f69a 871 unsigned int column_offset)
766928aa 872{
551e34da 873 const line_map_ordinary * map = NULL;
766928aa 874
a96cefb2 875 if (IS_ADHOC_LOC (loc))
876 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
877
766928aa 878 /* This function does not support virtual locations yet. */
324da92d 879 if (linemap_assert_fails
880 (!linemap_location_from_macro_expansion_p (set, loc)))
881 return loc;
766928aa 882
1ed1f69a 883 if (column_offset == 0
766928aa 884 /* Adding an offset to a reserved location (like
885 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
886 sense. So let's leave the location intact in that case. */
887 || loc < RESERVED_LOCATION_COUNT)
888 return loc;
889
890 /* We find the real location and shift it. */
891 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
892 /* The new location (loc + offset) should be higher than the first
3cff347a 893 location encoded by MAP. This can fail if the line information
894 is messed up because of line directives (see PR66415). */
1ed1f69a 895 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
324da92d 896 return loc;
766928aa 897
3cff347a 898 linenum_type line = SOURCE_LINE (map, loc);
899 unsigned int column = SOURCE_COLUMN (map, loc);
900
766928aa 901 /* If MAP is not the last line map of its set, then the new location
902 (loc + offset) should be less than the first location encoded by
3cff347a 903 the next line map of the set. Otherwise, we try to encode the
904 location in the next map. */
905 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
1ed1f69a 906 && (loc + (column_offset << map->m_range_bits)
907 >= MAP_START_LOCATION (&map[1])))
3cff347a 908 {
909 map = &map[1];
910 /* If the next map starts in a higher line, we cannot encode the
911 location there. */
912 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
913 return loc;
914 }
766928aa 915
1ed1f69a 916 column += column_offset;
917 if (linemap_assert_fails (column < (1u << map->m_column_and_range_bits)))
324da92d 918 return loc;
766928aa 919
920 source_location r =
1ed1f69a 921 linemap_position_for_line_and_column (set, map, line, column);
6d6bdc28 922 if (linemap_assert_fails (r <= set->highest_location)
923 || linemap_assert_fails (map == linemap_lookup (set, r)))
324da92d 924 return loc;
925
766928aa 926 return r;
927}
928
97bfb9ef 929/* Given a virtual source location yielded by a map (either an
930 ordinary or a macro map), returns that map. */
931
932const struct line_map*
fd3638df 933linemap_lookup (struct line_maps *set, source_location line)
97bfb9ef 934{
5169661d 935 if (IS_ADHOC_LOC (line))
936 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
97bfb9ef 937 if (linemap_location_from_macro_expansion_p (set, line))
938 return linemap_macro_map_lookup (set, line);
939 return linemap_ordinary_map_lookup (set, line);
940}
941
942/* Given a source location yielded by an ordinary map, returns that
943 map. Since the set is built chronologically, the logical lines are
944 monotonic increasing, and so the list is sorted and we can use a
945 binary search. */
946
551e34da 947static const line_map_ordinary *
97bfb9ef 948linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
38692459 949{
088db31b 950 unsigned int md, mn, mx;
551e34da 951 const line_map_ordinary *cached, *result;
088db31b 952
5169661d 953 if (IS_ADHOC_LOC (line))
954 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
955
97bfb9ef 956 if (set == NULL || line < RESERVED_LOCATION_COUNT)
957 return NULL;
958
959 mn = LINEMAPS_ORDINARY_CACHE (set);
960 mx = LINEMAPS_ORDINARY_USED (set);
088db31b 961
97bfb9ef 962 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
7c2df241 963 /* We should get a segfault if no line_maps have been added yet. */
97bfb9ef 964 if (line >= MAP_START_LOCATION (cached))
088db31b 965 {
97bfb9ef 966 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
088db31b 967 return cached;
968 }
969 else
970 {
971 mx = mn;
972 mn = 0;
973 }
38692459 974
975 while (mx - mn > 1)
976 {
977 md = (mn + mx) / 2;
97bfb9ef 978 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
38692459 979 mx = md;
980 else
981 mn = md;
982 }
983
97bfb9ef 984 LINEMAPS_ORDINARY_CACHE (set) = mn;
985 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
986 linemap_assert (line >= MAP_START_LOCATION (result));
987 return result;
988}
989
990/* Given a source location yielded by a macro map, returns that map.
991 Since the set is built chronologically, the logical lines are
992 monotonic decreasing, and so the list is sorted and we can use a
993 binary search. */
994
551e34da 995static const line_map_macro *
97bfb9ef 996linemap_macro_map_lookup (struct line_maps *set, source_location line)
997{
998 unsigned int md, mn, mx;
551e34da 999 const struct line_map_macro *cached, *result;
97bfb9ef 1000
5169661d 1001 if (IS_ADHOC_LOC (line))
1002 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
1003
97bfb9ef 1004 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
1005
1006 if (set == NULL)
1007 return NULL;
1008
1009 mn = LINEMAPS_MACRO_CACHE (set);
1010 mx = LINEMAPS_MACRO_USED (set);
1011 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
1012
1013 if (line >= MAP_START_LOCATION (cached))
1014 {
1015 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
1016 return cached;
1017 mx = mn - 1;
1018 mn = 0;
1019 }
1020
68928989 1021 while (mn < mx)
97bfb9ef 1022 {
1023 md = (mx + mn) / 2;
1024 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
68928989 1025 mn = md + 1;
97bfb9ef 1026 else
1027 mx = md;
68928989 1028 }
97bfb9ef 1029
1030 LINEMAPS_MACRO_CACHE (set) = mx;
1031 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
1032 linemap_assert (MAP_START_LOCATION (result) <= line);
1033
1034 return result;
1035}
1036
1037/* Return TRUE if MAP encodes locations coming from a macro
1038 replacement-list at macro expansion point. */
1039
1040bool
1041linemap_macro_expansion_map_p (const struct line_map *map)
1042{
1043 if (!map)
1044 return false;
1045 return (map->reason == LC_ENTER_MACRO);
1046}
1047
1048/* If LOCATION is the locus of a token in a replacement-list of a
1049 macro expansion return the location of the macro expansion point.
1050
1051 Read the comments of struct line_map and struct line_map_macro in
1052 line-map.h to understand what a macro expansion point is. */
1053
a67520a9 1054static source_location
551e34da 1055linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
a67520a9 1056 source_location location ATTRIBUTE_UNUSED)
97bfb9ef 1057{
97bfb9ef 1058 linemap_assert (linemap_macro_expansion_map_p (map)
1059 && location >= MAP_START_LOCATION (map));
1060
1061 /* Make sure LOCATION is correct. */
a67520a9 1062 linemap_assert ((location - MAP_START_LOCATION (map))
1063 < MACRO_MAP_NUM_MACRO_TOKENS (map));
97bfb9ef 1064
1065 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
1066}
1067
1fcf9669 1068/* LOCATION is the source location of a token that belongs to a macro
1069 replacement-list as part of the macro expansion denoted by MAP.
97bfb9ef 1070
1fcf9669 1071 Return the location of the token at the definition point of the
1072 macro. */
1073
1074static source_location
551e34da 1075linemap_macro_map_loc_to_def_point (const line_map_macro *map,
97bfb9ef 1076 source_location location)
1077{
1078 unsigned token_no;
1079
1080 linemap_assert (linemap_macro_expansion_map_p (map)
1081 && location >= MAP_START_LOCATION (map));
1082 linemap_assert (location >= RESERVED_LOCATION_COUNT);
1083
1084 token_no = location - MAP_START_LOCATION (map);
1085 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1086
1087 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
1088
1089 return location;
1090}
1091
1092/* If LOCATION is the locus of a token that is an argument of a
1093 function-like macro M and appears in the expansion of M, return the
1094 locus of that argument in the context of the caller of M.
1095
1096 In other words, this returns the xI location presented in the
1097 comments of line_map_macro above. */
1098source_location
a96cefb2 1099linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
1100 const line_map_macro* map,
97bfb9ef 1101 source_location location)
1102{
1103 unsigned token_no;
1104
a96cefb2 1105 if (IS_ADHOC_LOC (location))
1106 location = get_location_from_adhoc_loc (set, location);
1107
97bfb9ef 1108 linemap_assert (linemap_macro_expansion_map_p (map)
1109 && location >= MAP_START_LOCATION (map));
1110 linemap_assert (location >= RESERVED_LOCATION_COUNT);
a96cefb2 1111 linemap_assert (!IS_ADHOC_LOC (location));
97bfb9ef 1112
1113 token_no = location - MAP_START_LOCATION (map);
1114 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
1115
1116 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
1117
1118 return location;
1119}
1120
1121/* Return the source line number corresponding to source location
1122 LOCATION. SET is the line map set LOCATION comes from. If
1123 LOCATION is the source location of token that is part of the
1124 replacement-list of a macro expansion return the line number of the
1125 macro expansion point. */
1126
1127int
1128linemap_get_expansion_line (struct line_maps *set,
1129 source_location location)
1130{
551e34da 1131 const line_map_ordinary *map = NULL;
97bfb9ef 1132
5169661d 1133 if (IS_ADHOC_LOC (location))
6e5a7913 1134 location = set->location_adhoc_data_map.data[location
1135 & MAX_SOURCE_LOCATION].locus;
5169661d 1136
97bfb9ef 1137 if (location < RESERVED_LOCATION_COUNT)
1138 return 0;
1139
1140 location =
1141 linemap_macro_loc_to_exp_point (set, location, &map);
1142
1143 return SOURCE_LINE (map, location);
1144}
1145
1146/* Return the path of the file corresponding to source code location
1147 LOCATION.
1148
1149 If LOCATION is the source location of token that is part of the
1150 replacement-list of a macro expansion return the file path of the
1151 macro expansion point.
1152
1153 SET is the line map set LOCATION comes from. */
1154
1155const char*
1156linemap_get_expansion_filename (struct line_maps *set,
1157 source_location location)
1158{
551e34da 1159 const struct line_map_ordinary *map = NULL;
97bfb9ef 1160
5169661d 1161 if (IS_ADHOC_LOC (location))
6e5a7913 1162 location = set->location_adhoc_data_map.data[location
1163 & MAX_SOURCE_LOCATION].locus;
5169661d 1164
97bfb9ef 1165 if (location < RESERVED_LOCATION_COUNT)
1166 return NULL;
1167
1168 location =
1169 linemap_macro_loc_to_exp_point (set, location, &map);
1170
1171 return LINEMAP_FILE (map);
1172}
1173
1174/* Return the name of the macro associated to MACRO_MAP. */
1175
1176const char*
551e34da 1177linemap_map_get_macro_name (const line_map_macro *macro_map)
97bfb9ef 1178{
1179 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
1180 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
1181}
1182
1183/* Return a positive value if LOCATION is the locus of a token that is
1184 located in a system header, O otherwise. It returns 1 if LOCATION
1185 is the locus of a token that is located in a system header, and 2
1186 if LOCATION is the locus of a token located in a C system header
1187 that therefore needs to be extern "C" protected in C++.
1188
1189 Note that this function returns 1 if LOCATION belongs to a token
1190 that is part of a macro replacement-list defined in a system
1191 header, but expanded in a non-system file. */
1192
1193int
1194linemap_location_in_system_header_p (struct line_maps *set,
1195 source_location location)
1196{
1197 const struct line_map *map = NULL;
1198
5169661d 1199 if (IS_ADHOC_LOC (location))
6e5a7913 1200 location = set->location_adhoc_data_map.data[location
1201 & MAX_SOURCE_LOCATION].locus;
5169661d 1202
5ebe2143 1203 if (location < RESERVED_LOCATION_COUNT)
1204 return false;
1205
0aa42a53 1206 /* Let's look at where the token for LOCATION comes from. */
1207 while (true)
1208 {
1209 map = linemap_lookup (set, location);
1210 if (map != NULL)
1211 {
1212 if (!linemap_macro_expansion_map_p (map))
1213 /* It's a normal token. */
551e34da 1214 return LINEMAP_SYSP (linemap_check_ordinary (map));
0aa42a53 1215 else
1216 {
551e34da 1217 const line_map_macro *macro_map = linemap_check_macro (map);
1218
0aa42a53 1219 /* It's a token resulting from a macro expansion. */
1220 source_location loc =
a96cefb2 1221 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
0aa42a53 1222 if (loc < RESERVED_LOCATION_COUNT)
1223 /* This token might come from a built-in macro. Let's
1224 look at where that macro got expanded. */
551e34da 1225 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
0aa42a53 1226 else
1227 location = loc;
1228 }
1229 }
1230 else
1231 break;
1232 }
1233 return false;
97bfb9ef 1234}
1235
1236/* Return TRUE if LOCATION is a source code location of a token coming
1237 from a macro replacement-list at a macro expansion point, FALSE
1238 otherwise. */
1239
1240bool
1fcf9669 1241linemap_location_from_macro_expansion_p (const struct line_maps *set,
97bfb9ef 1242 source_location location)
1243{
5169661d 1244 if (IS_ADHOC_LOC (location))
6e5a7913 1245 location = set->location_adhoc_data_map.data[location
1246 & MAX_SOURCE_LOCATION].locus;
5169661d 1247
97bfb9ef 1248 linemap_assert (location <= MAX_SOURCE_LOCATION
1249 && (set->highest_location
1250 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
1251 if (set == NULL)
1252 return false;
1253 return (location > set->highest_location);
1254}
1255
1256/* Given two virtual locations *LOC0 and *LOC1, return the first
1257 common macro map in their macro expansion histories. Return NULL
1258 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
1259 virtual location of the token inside the resulting macro. */
1260
1261static const struct line_map*
1262first_map_in_common_1 (struct line_maps *set,
1263 source_location *loc0,
1264 source_location *loc1)
1265{
1266 source_location l0 = *loc0, l1 = *loc1;
1267 const struct line_map *map0 = linemap_lookup (set, l0),
1268 *map1 = linemap_lookup (set, l1);
1269
1270 while (linemap_macro_expansion_map_p (map0)
1271 && linemap_macro_expansion_map_p (map1)
1272 && (map0 != map1))
1273 {
1274 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
1275 {
551e34da 1276 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
1277 l0);
97bfb9ef 1278 map0 = linemap_lookup (set, l0);
1279 }
1280 else
1281 {
551e34da 1282 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
1283 l1);
97bfb9ef 1284 map1 = linemap_lookup (set, l1);
1285 }
1286 }
1287
1288 if (map0 == map1)
1289 {
1290 *loc0 = l0;
1291 *loc1 = l1;
1292 return map0;
1293 }
1294 return NULL;
1295}
1296
1297/* Given two virtual locations LOC0 and LOC1, return the first common
1298 macro map in their macro expansion histories. Return NULL if no
1299 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
1300 virtual location of the token inside the resulting macro, upon
1301 return of a non-NULL result. */
1302
1303static const struct line_map*
1304first_map_in_common (struct line_maps *set,
1305 source_location loc0,
1306 source_location loc1,
1307 source_location *res_loc0,
1308 source_location *res_loc1)
1309{
1310 *res_loc0 = loc0;
1311 *res_loc1 = loc1;
1312
1313 return first_map_in_common_1 (set, res_loc0, res_loc1);
1314}
1315
1316/* Return a positive value if PRE denotes the location of a token that
1317 comes before the token of POST, 0 if PRE denotes the location of
1318 the same token as the token for POST, and a negative value
1319 otherwise. */
1320
1321int
1322linemap_compare_locations (struct line_maps *set,
1323 source_location pre,
1324 source_location post)
1325{
1326 bool pre_virtual_p, post_virtual_p;
1327 source_location l0 = pre, l1 = post;
1328
6e5a7913 1329 if (IS_ADHOC_LOC (l0))
52609ec3 1330 l0 = get_location_from_adhoc_loc (set, l0);
6e5a7913 1331 if (IS_ADHOC_LOC (l1))
52609ec3 1332 l1 = get_location_from_adhoc_loc (set, l1);
6e5a7913 1333
97bfb9ef 1334 if (l0 == l1)
1335 return 0;
1336
1337 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1338 l0 = linemap_resolve_location (set, l0,
1339 LRK_MACRO_EXPANSION_POINT,
1340 NULL);
1341
1342 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1343 l1 = linemap_resolve_location (set, l1,
1344 LRK_MACRO_EXPANSION_POINT,
1345 NULL);
1346
1347 if (l0 == l1
1348 && pre_virtual_p
1349 && post_virtual_p)
1350 {
1351 /* So pre and post represent two tokens that are present in a
1352 same macro expansion. Let's see if the token for pre was
1353 before the token for post in that expansion. */
1354 unsigned i0, i1;
1355 const struct line_map *map =
1356 first_map_in_common (set, pre, post, &l0, &l1);
1357
1358 if (map == NULL)
1359 /* This should not be possible. */
1360 abort ();
1361
1362 i0 = l0 - MAP_START_LOCATION (map);
1363 i1 = l1 - MAP_START_LOCATION (map);
1364 return i1 - i0;
1365 }
1366
52609ec3 1367 if (IS_ADHOC_LOC (l0))
1368 l0 = get_location_from_adhoc_loc (set, l0);
1369 if (IS_ADHOC_LOC (l1))
1370 l1 = get_location_from_adhoc_loc (set, l1);
1371
97bfb9ef 1372 return l1 - l0;
38692459 1373}
bd507c05 1374
438ac94c 1375/* Print an include trace, for e.g. the -H option of the preprocessor. */
1376
1377static void
551e34da 1378trace_include (const struct line_maps *set, const line_map_ordinary *map)
438ac94c 1379{
4087823d 1380 unsigned int i = set->depth;
438ac94c 1381
4087823d 1382 while (--i)
438ac94c 1383 putc ('.', stderr);
97bfb9ef 1384
1385 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1386}
1387
1388/* Return the spelling location of the token wherever it comes from,
1389 whether part of a macro definition or not.
1390
1391 This is a subroutine for linemap_resolve_location. */
1392
1393static source_location
1394linemap_macro_loc_to_spelling_point (struct line_maps *set,
1395 source_location location,
551e34da 1396 const line_map_ordinary **original_map)
97bfb9ef 1397{
1398 struct line_map *map;
97bfb9ef 1399 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1400
1401 while (true)
1402 {
551e34da 1403 map = const_cast <line_map *> (linemap_lookup (set, location));
97bfb9ef 1404 if (!linemap_macro_expansion_map_p (map))
1405 break;
1406
551e34da 1407 location
1408 = linemap_macro_map_loc_unwind_toward_spelling
a96cefb2 1409 (set, linemap_check_macro (map),
551e34da 1410 location);
97bfb9ef 1411 }
1412
1413 if (original_map)
551e34da 1414 *original_map = linemap_check_ordinary (map);
97bfb9ef 1415 return location;
1416}
1417
1418/* If LOCATION is the source location of a token that belongs to a
1419 macro replacement-list -- as part of a macro expansion -- then
1420 return the location of the token at the definition point of the
1421 macro. Otherwise, return LOCATION. SET is the set of maps
1422 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1423 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1424 returned location comes from.
1425
1426 This is a subroutine of linemap_resolve_location. */
1427
1428static source_location
1429linemap_macro_loc_to_def_point (struct line_maps *set,
1430 source_location location,
551e34da 1431 const line_map_ordinary **original_map)
97bfb9ef 1432{
1433 struct line_map *map;
1434
5169661d 1435 if (IS_ADHOC_LOC (location))
6e5a7913 1436 location = set->location_adhoc_data_map.data[location
1437 & MAX_SOURCE_LOCATION].locus;
5169661d 1438
97bfb9ef 1439 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1440
1441 while (true)
1442 {
551e34da 1443 map = const_cast <line_map *> (linemap_lookup (set, location));
97bfb9ef 1444 if (!linemap_macro_expansion_map_p (map))
1445 break;
1446
1447 location =
551e34da 1448 linemap_macro_map_loc_to_def_point (linemap_check_macro (map),
1449 location);
97bfb9ef 1450 }
1451
1452 if (original_map)
551e34da 1453 *original_map = linemap_check_ordinary (map);
97bfb9ef 1454 return location;
1455}
1456
1457/* If LOCATION is the source location of a token that belongs to a
1458 macro replacement-list -- at a macro expansion point -- then return
1459 the location of the topmost expansion point of the macro. We say
1460 topmost because if we are in the context of a nested macro
1461 expansion, the function returns the source location of the first
1462 macro expansion that triggered the nested expansions.
1463
1464 Otherwise, return LOCATION. SET is the set of maps location come
1465 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1466 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1467 location comes from.
1468
1469 This is a subroutine of linemap_resolve_location. */
1470
1471static source_location
1472linemap_macro_loc_to_exp_point (struct line_maps *set,
1473 source_location location,
551e34da 1474 const line_map_ordinary **original_map)
97bfb9ef 1475{
1476 struct line_map *map;
1477
5169661d 1478 if (IS_ADHOC_LOC (location))
6e5a7913 1479 location = set->location_adhoc_data_map.data[location
1480 & MAX_SOURCE_LOCATION].locus;
5169661d 1481
97bfb9ef 1482 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1483
1484 while (true)
1485 {
551e34da 1486 map = const_cast <line_map *> (linemap_lookup (set, location));
97bfb9ef 1487 if (!linemap_macro_expansion_map_p (map))
1488 break;
551e34da 1489 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
1490 location);
97bfb9ef 1491 }
1492
1493 if (original_map)
551e34da 1494 *original_map = linemap_check_ordinary (map);
97bfb9ef 1495 return location;
1496}
1497
1498/* Resolve a virtual location into either a spelling location, an
1499 expansion point location or a token argument replacement point
1500 location. Return the map that encodes the virtual location as well
1501 as the resolved location.
1502
1503 If LOC is *NOT* the location of a token resulting from the
1504 expansion of a macro, then the parameter LRK (which stands for
1505 Location Resolution Kind) is ignored and the resulting location
1506 just equals the one given in argument.
1507
1508 Now if LOC *IS* the location of a token resulting from the
1509 expansion of a macro, this is what happens.
1510
1511 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1512 -------------------------------
1513
3f898bd2 1514 The virtual location is resolved to the first macro expansion point
1515 that led to this macro expansion.
97bfb9ef 1516
1517 * If LRK is set to LRK_SPELLING_LOCATION
1518 -------------------------------------
1519
3f898bd2 1520 The virtual location is resolved to the locus where the token has
1521 been spelled in the source. This can follow through all the macro
1522 expansions that led to the token.
97bfb9ef 1523
3f898bd2 1524 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
97bfb9ef 1525 --------------------------------------
1526
3f898bd2 1527 The virtual location is resolved to the locus of the token in the
1528 context of the macro definition.
1529
97bfb9ef 1530 If LOC is the locus of a token that is an argument of a
1531 function-like macro [replacing a parameter in the replacement list
1532 of the macro] the virtual location is resolved to the locus of the
1533 parameter that is replaced, in the context of the definition of the
1534 macro.
1535
1536 If LOC is the locus of a token that is not an argument of a
1537 function-like macro, then the function behaves as if LRK was set to
1538 LRK_SPELLING_LOCATION.
1539
1fcf9669 1540 If MAP is not NULL, *MAP is set to the map encoding the
3f898bd2 1541 returned location. Note that if the returned location wasn't originally
1fcf9669 1542 encoded by a map, then *MAP is set to NULL. This can happen if LOC
5ebe2143 1543 resolves to a location reserved for the client code, like
1544 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
97bfb9ef 1545
1546source_location
1547linemap_resolve_location (struct line_maps *set,
1548 source_location loc,
1549 enum location_resolution_kind lrk,
551e34da 1550 const line_map_ordinary **map)
97bfb9ef 1551{
a96cefb2 1552 source_location locus = loc;
5169661d 1553 if (IS_ADHOC_LOC (loc))
a96cefb2 1554 locus = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
5169661d 1555
a96cefb2 1556 if (locus < RESERVED_LOCATION_COUNT)
5ebe2143 1557 {
1558 /* A reserved location wasn't encoded in a map. Let's return a
1559 NULL map here, just like what linemap_ordinary_map_lookup
1560 does. */
1561 if (map)
1562 *map = NULL;
1563 return loc;
1564 }
97bfb9ef 1565
1566 switch (lrk)
1567 {
1568 case LRK_MACRO_EXPANSION_POINT:
1569 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1570 break;
1571 case LRK_SPELLING_LOCATION:
1572 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1573 break;
1574 case LRK_MACRO_DEFINITION_LOCATION:
1575 loc = linemap_macro_loc_to_def_point (set, loc, map);
1576 break;
1577 default:
1578 abort ();
1579 }
1580 return loc;
1581}
1582
1583/*
1584 Suppose that LOC is the virtual location of a token T coming from
1585 the expansion of a macro M. This function then steps up to get the
1586 location L of the point where M got expanded. If L is a spelling
1587 location inside a macro expansion M', then this function returns
1588 the locus of the point where M' was expanded. Said otherwise, this
1589 function returns the location of T in the context that triggered
1590 the expansion of M.
1591
1592 *LOC_MAP must be set to the map of LOC. This function then sets it
1593 to the map of the returned location. */
1594
1595source_location
1596linemap_unwind_toward_expansion (struct line_maps *set,
1597 source_location loc,
1598 const struct line_map **map)
1599{
1600 source_location resolved_location;
551e34da 1601 const line_map_macro *macro_map = linemap_check_macro (*map);
97bfb9ef 1602 const struct line_map *resolved_map;
1603
5169661d 1604 if (IS_ADHOC_LOC (loc))
1605 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1606
97bfb9ef 1607 resolved_location =
a96cefb2 1608 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
97bfb9ef 1609 resolved_map = linemap_lookup (set, resolved_location);
1610
1611 if (!linemap_macro_expansion_map_p (resolved_map))
1612 {
551e34da 1613 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
97bfb9ef 1614 resolved_map = linemap_lookup (set, resolved_location);
1615 }
1616
1617 *map = resolved_map;
1618 return resolved_location;
1619}
1620
bd172d61 1621/* If LOC is the virtual location of a token coming from the expansion
1622 of a macro M and if its spelling location is reserved (e.g, a
1623 location for a built-in token), then this function unwinds (using
1624 linemap_unwind_toward_expansion) the location until a location that
e8aa7eaf 1625 is not reserved and is not in a system header is reached. In other
bd172d61 1626 words, this unwinds the reserved location until a location that is
1627 in real source code is reached.
1628
1629 Otherwise, if the spelling location for LOC is not reserved or if
1630 LOC doesn't come from the expansion of a macro, the function
1631 returns LOC as is and *MAP is not touched.
1632
1633 *MAP is set to the map of the returned location if the later is
1634 different from LOC. */
1635source_location
1636linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1637 source_location loc,
1638 const struct line_map **map)
1639{
1640 source_location resolved_loc;
551e34da 1641 const struct line_map *map0 = NULL;
1642 const line_map_ordinary *map1 = NULL;
bd172d61 1643
5169661d 1644 if (IS_ADHOC_LOC (loc))
1645 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1646
bd172d61 1647 map0 = linemap_lookup (set, loc);
1648 if (!linemap_macro_expansion_map_p (map0))
1649 return loc;
1650
1651 resolved_loc = linemap_resolve_location (set, loc,
1652 LRK_SPELLING_LOCATION,
1653 &map1);
1654
1655 if (resolved_loc >= RESERVED_LOCATION_COUNT
1656 && !LINEMAP_SYSP (map1))
1657 return loc;
1658
1659 while (linemap_macro_expansion_map_p (map0)
1660 && (resolved_loc < RESERVED_LOCATION_COUNT
1661 || LINEMAP_SYSP (map1)))
1662 {
1663 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1664 resolved_loc = linemap_resolve_location (set, loc,
1665 LRK_SPELLING_LOCATION,
1666 &map1);
1667 }
1668
1669 if (map != NULL)
1670 *map = map0;
1671 return loc;
1672}
1673
97bfb9ef 1674/* Expand source code location LOC and return a user readable source
5ebe2143 1675 code location. LOC must be a spelling (non-virtual) location. If
1676 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1677 location is returned. */
97bfb9ef 1678
1679expanded_location
5ebe2143 1680linemap_expand_location (struct line_maps *set,
1681 const struct line_map *map,
97bfb9ef 1682 source_location loc)
1683
1684{
1685 expanded_location xloc;
1686
5ebe2143 1687 memset (&xloc, 0, sizeof (xloc));
5169661d 1688 if (IS_ADHOC_LOC (loc))
1689 {
6e5a7913 1690 xloc.data
1691 = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
a96cefb2 1692 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
5169661d 1693 }
5ebe2143 1694
1695 if (loc < RESERVED_LOCATION_COUNT)
1696 /* The location for this token wasn't generated from a line map.
1697 It was probably a location for a builtin token, chosen by some
1698 client code. Let's not try to expand the location in that
1699 case. */;
1700 else if (map == NULL)
1701 /* We shouldn't be getting a NULL map with a location that is not
1702 reserved by the client code. */
1703 abort ();
1704 else
1705 {
1706 /* MAP must be an ordinary map and LOC must be non-virtual,
1707 encoded into this map, obviously; the accessors used on MAP
1708 below ensure it is ordinary. Let's just assert the
1709 non-virtualness of LOC here. */
1710 if (linemap_location_from_macro_expansion_p (set, loc))
1711 abort ();
97bfb9ef 1712
551e34da 1713 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
1714
1715 xloc.file = LINEMAP_FILE (ord_map);
1716 xloc.line = SOURCE_LINE (ord_map, loc);
1717 xloc.column = SOURCE_COLUMN (ord_map, loc);
1718 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
5ebe2143 1719 }
97bfb9ef 1720
97bfb9ef 1721 return xloc;
438ac94c 1722}
62db153a 1723
b082215e 1724
1725/* Dump line map at index IX in line table SET to STREAM. If STREAM
1726 is NULL, use stderr. IS_MACRO is true if the caller wants to
1727 dump a macro map, false otherwise. */
1728
1729void
1730linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1731{
1732 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1733 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1734 "LC_ENTER_MACRO" };
1735 const char *reason;
551e34da 1736 const line_map *map;
b082215e 1737
1738 if (stream == NULL)
1739 stream = stderr;
1740
1741 if (!is_macro)
1742 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1743 else
1744 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1745
1746 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1747
1748 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1749 ix, (void *) map, map->start_location, reason,
551e34da 1750 ((!is_macro
1751 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
1752 ? "yes" : "no"));
b082215e 1753 if (!is_macro)
1754 {
551e34da 1755 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
b082215e 1756 unsigned includer_ix;
551e34da 1757 const line_map_ordinary *includer_map;
b082215e 1758
551e34da 1759 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (ord_map);
b082215e 1760 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1761 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1762 : NULL;
1763
551e34da 1764 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
1765 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
b082215e 1766 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1767 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1768 }
1769 else
551e34da 1770 {
1771 const line_map_macro *macro_map = linemap_check_macro (map);
1772 fprintf (stream, "Macro: %s (%u tokens)\n",
1773 linemap_map_get_macro_name (macro_map),
1774 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
1775 }
b082215e 1776
1777 fprintf (stream, "\n");
1778}
1779
1780
62db153a 1781/* Dump debugging information about source location LOC into the file
1782 stream STREAM. SET is the line map set LOC comes from. */
1783
1784void
1785linemap_dump_location (struct line_maps *set,
1786 source_location loc,
1787 FILE *stream)
1788{
551e34da 1789 const line_map_ordinary *map;
62db153a 1790 source_location location;
5ebe2143 1791 const char *path = "", *from = "";
1792 int l = -1, c = -1, s = -1, e = -1;
62db153a 1793
5169661d 1794 if (IS_ADHOC_LOC (loc))
1795 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1796
62db153a 1797 if (loc == 0)
1798 return;
1799
1800 location =
1801 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
62db153a 1802
5ebe2143 1803 if (map == NULL)
1804 /* Only reserved locations can be tolerated in this case. */
1805 linemap_assert (location < RESERVED_LOCATION_COUNT);
62db153a 1806 else
5ebe2143 1807 {
1808 path = LINEMAP_FILE (map);
1809 l = SOURCE_LINE (map, location);
1810 c = SOURCE_COLUMN (map, location);
1811 s = LINEMAP_SYSP (map) != 0;
1812 e = location != loc;
1813 if (e)
1814 from = "N/A";
1815 else
1816 from = (INCLUDED_FROM (set, map))
1817 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1818 : "<NULL>";
1819 }
62db153a 1820
1821 /* P: path, L: line, C: column, S: in-system-header, M: map address,
5ebe2143 1822 E: macro expansion?, LOC: original location, R: resolved location */
1823 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1824 path, from, l, c, s, (void*)map, e, loc, location);
62db153a 1825}
e77b8253 1826
ffc2c526 1827/* Return the highest location emitted for a given file for which
1828 there is a line map in SET. FILE_NAME is the file name to
1829 consider. If the function returns TRUE, *LOC is set to the highest
1830 location emitted for that file. */
1831
1832bool
1833linemap_get_file_highest_location (struct line_maps *set,
1834 const char *file_name,
1835 source_location *loc)
1836{
1837 /* If the set is empty or no ordinary map has been created then
1838 there is no file to look for ... */
1839 if (set == NULL || set->info_ordinary.used == 0)
1840 return false;
1841
1842 /* Now look for the last ordinary map created for FILE_NAME. */
1843 int i;
1844 for (i = set->info_ordinary.used - 1; i >= 0; --i)
1845 {
551e34da 1846 const char *fname = set->info_ordinary.maps[i].to_file;
ffc2c526 1847 if (fname && !filename_cmp (fname, file_name))
1848 break;
1849 }
1850
1851 if (i < 0)
1852 return false;
1853
1854 /* The highest location for a given map is either the starting
1855 location of the next map minus one, or -- if the map is the
1856 latest one -- the highest location of the set. */
1857 source_location result;
1858 if (i == (int) set->info_ordinary.used - 1)
1859 result = set->highest_location;
1860 else
1861 result = set->info_ordinary.maps[i + 1].start_location - 1;
1862
1863 *loc = result;
1864 return true;
1865}
1866
e77b8253 1867/* Compute and return statistics about the memory consumption of some
1868 parts of the line table SET. */
1869
1870void
1871linemap_get_statistics (struct line_maps *set,
1872 struct linemap_stats *s)
1873{
2a688977 1874 long ordinary_maps_allocated_size, ordinary_maps_used_size,
e77b8253 1875 macro_maps_allocated_size, macro_maps_used_size,
1876 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1877
551e34da 1878 const line_map_macro *cur_map;
e77b8253 1879
1880 ordinary_maps_allocated_size =
551e34da 1881 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
e77b8253 1882
1883 ordinary_maps_used_size =
551e34da 1884 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
e77b8253 1885
1886 macro_maps_allocated_size =
551e34da 1887 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
e77b8253 1888
1889 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1890 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1891 ++cur_map)
1892 {
1893 unsigned i;
1894
1895 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1896
1897 macro_maps_locations_size +=
1898 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1899
1900 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1901 {
1902 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1903 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1904 duplicated_macro_maps_locations_size +=
1905 sizeof (source_location);
1906 }
1907 }
1908
1909 macro_maps_used_size =
551e34da 1910 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
e77b8253 1911
1912 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1913 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1914 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1915 s->ordinary_maps_used_size = ordinary_maps_used_size;
1916 s->num_expanded_macros = num_expanded_macros_counter;
1917 s->num_macro_tokens = num_macro_tokens_counter;
1918 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1919 s->macro_maps_allocated_size = macro_maps_allocated_size;
1920 s->macro_maps_locations_size = macro_maps_locations_size;
1921 s->macro_maps_used_size = macro_maps_used_size;
1922 s->duplicated_macro_maps_locations_size =
1923 duplicated_macro_maps_locations_size;
0ffb4474 1924 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
1925 * sizeof (struct location_adhoc_data));
1926 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
e77b8253 1927}
b082215e 1928
1929
1930/* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1931 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1932 specifies how many macro maps to dump. */
1933
1934void
1935line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1936 unsigned int num_macro)
1937{
1938 unsigned int i;
1939
1940 if (set == NULL)
1941 return;
1942
1943 if (stream == NULL)
1944 stream = stderr;
1945
1946 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1947 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1948 fprintf (stream, "Include stack depth: %d\n", set->depth);
1949 fprintf (stream, "Highest location: %u\n", set->highest_location);
1950
1951 if (num_ordinary)
1952 {
1953 fprintf (stream, "\nOrdinary line maps\n");
1954 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1955 linemap_dump (stream, set, i, false);
1956 fprintf (stream, "\n");
1957 }
1958
1959 if (num_macro)
1960 {
1961 fprintf (stream, "\nMacro line maps\n");
1962 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1963 linemap_dump (stream, set, i, true);
1964 fprintf (stream, "\n");
1965 }
1966}
f0479000 1967
734caf84 1968/* struct source_range. */
1969
1970/* Is there any part of this range on the given line? */
1971
1972bool
1973source_range::intersects_line_p (const char *file, int line) const
1974{
1975 expanded_location exploc_start
1976 = linemap_client_expand_location_to_spelling_point (m_start);
1977 if (file != exploc_start.file)
1978 return false;
1979 if (line < exploc_start.line)
1980 return false;
1981 expanded_location exploc_finish
1982 = linemap_client_expand_location_to_spelling_point (m_finish);
1983 if (file != exploc_finish.file)
1984 return false;
1985 if (line > exploc_finish.line)
1986 return false;
1987 return true;
1988}
1989
f0479000 1990/* class rich_location. */
1991
1992/* Construct a rich_location with location LOC as its initial range. */
1993
83108969 1994rich_location::rich_location (line_maps */*set*/, source_location loc) :
f0479000 1995 m_num_ranges (0),
83108969 1996 m_column_override (0),
734caf84 1997 m_have_expanded_location (false),
1998 m_num_fixit_hints (0)
f0479000 1999{
83108969 2000 add_range (loc, true);
f0479000 2001}
2002
734caf84 2003/* The destructor for class rich_location. */
2004
2005rich_location::~rich_location ()
2006{
2007 for (unsigned int i = 0; i < m_num_fixit_hints; i++)
2008 delete m_fixit_hints[i];
2009}
2010
83108969 2011/* Get location IDX within this rich_location. */
2012
2013source_location
2014rich_location::get_loc (unsigned int idx) const
2015{
2016 linemap_assert (idx < m_num_ranges);
2017 return m_ranges[idx].m_loc;
2018}
2019
2020/* Expand location IDX within this rich_location. */
f0479000 2021/* Get an expanded_location for this rich_location's primary
2022 location. */
2023
2024expanded_location
83108969 2025rich_location::get_expanded_location (unsigned int idx)
f0479000 2026{
83108969 2027 if (idx == 0)
2028 {
2029 /* Cache the expansion of the primary location. */
2030 if (!m_have_expanded_location)
2031 {
2032 m_expanded_location
2033 = linemap_client_expand_location_to_spelling_point (get_loc (0));
2034 if (m_column_override)
2035 m_expanded_location.column = m_column_override;
2036 m_have_expanded_location = true;
2037 }
2038
2039 return m_expanded_location;
2040 }
2041 else
2042 return linemap_client_expand_location_to_spelling_point (get_loc (idx));
f0479000 2043}
2044
83108969 2045/* Set the column of the primary location, with 0 meaning
2046 "don't override it". */
f0479000 2047
2048void
2049rich_location::override_column (int column)
2050{
83108969 2051 m_column_override = column;
2052 m_have_expanded_location = false;
f0479000 2053}
2054
2055/* Add the given range. */
2056
2057void
83108969 2058rich_location::add_range (source_location loc, bool show_caret_p)
f0479000 2059{
2060 linemap_assert (m_num_ranges < MAX_RANGES);
2061
2062 location_range *range = &m_ranges[m_num_ranges++];
83108969 2063 range->m_loc = loc;
f0479000 2064 range->m_show_caret_p = show_caret_p;
2065}
2066
d0f713f4 2067/* Add or overwrite the location given by IDX, setting its location to LOC,
2068 and setting its "should my caret be printed" flag to SHOW_CARET_P.
f0479000 2069
d0f713f4 2070 It must either overwrite an existing location, or add one *exactly* on
2071 the end of the array.
f0479000 2072
d0f713f4 2073 This is primarily for use by gcc when implementing diagnostic format
2074 decoders e.g.
2075 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
2076 (which writes the source location of a tree back into location 0 of
2077 the rich_location), and
2078 - the "%C" and "%L" format codes in the Fortran frontend. */
f0479000 2079
2080void
83108969 2081rich_location::set_range (line_maps * /*set*/, unsigned int idx,
d0f713f4 2082 source_location loc, bool show_caret_p)
f0479000 2083{
2084 linemap_assert (idx < MAX_RANGES);
2085
2086 /* We can either overwrite an existing range, or add one exactly
2087 on the end of the array. */
2088 linemap_assert (idx <= m_num_ranges);
2089
2090 location_range *locrange = &m_ranges[idx];
83108969 2091 locrange->m_loc = loc;
f0479000 2092 locrange->m_show_caret_p = show_caret_p;
f0479000 2093
2094 /* Are we adding a range onto the end? */
2095 if (idx == m_num_ranges)
2096 m_num_ranges = idx + 1;
2097
d0f713f4 2098 if (idx == 0)
83108969 2099 /* Mark any cached value here as dirty. */
2100 m_have_expanded_location = false;
f0479000 2101}
734caf84 2102
2103/* Add a fixit-hint, suggesting insertion of NEW_CONTENT
2104 at WHERE. */
2105
2106void
2107rich_location::add_fixit_insert (source_location where,
2108 const char *new_content)
2109{
2110 linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2111 m_fixit_hints[m_num_fixit_hints++]
2112 = new fixit_insert (where, new_content);
2113}
2114
2115/* Add a fixit-hint, suggesting removal of the content at
2116 SRC_RANGE. */
2117
2118void
2119rich_location::add_fixit_remove (source_range src_range)
2120{
2121 linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2122 m_fixit_hints[m_num_fixit_hints++] = new fixit_remove (src_range);
2123}
2124
2125/* Add a fixit-hint, suggesting replacement of the content at
2126 SRC_RANGE with NEW_CONTENT. */
2127
2128void
2129rich_location::add_fixit_replace (source_range src_range,
2130 const char *new_content)
2131{
2132 linemap_assert (m_num_fixit_hints < MAX_FIXIT_HINTS);
2133 m_fixit_hints[m_num_fixit_hints++]
2134 = new fixit_replace (src_range, new_content);
2135}
2136
2137/* class fixit_insert. */
2138
2139fixit_insert::fixit_insert (source_location where,
2140 const char *new_content)
2141: m_where (where),
2142 m_bytes (xstrdup (new_content)),
2143 m_len (strlen (new_content))
2144{
2145}
2146
2147fixit_insert::~fixit_insert ()
2148{
2149 free (m_bytes);
2150}
2151
2152/* Implementation of fixit_hint::affects_line_p for fixit_insert. */
2153
2154bool
2155fixit_insert::affects_line_p (const char *file, int line)
2156{
2157 expanded_location exploc
2158 = linemap_client_expand_location_to_spelling_point (m_where);
2159 if (file == exploc.file)
2160 if (line == exploc.line)
2161 return true;
2162 return false;
2163}
2164
2165/* class fixit_remove. */
2166
2167fixit_remove::fixit_remove (source_range src_range)
2168: m_src_range (src_range)
2169{
2170}
2171
2172/* Implementation of fixit_hint::affects_line_p for fixit_remove. */
2173
2174bool
2175fixit_remove::affects_line_p (const char *file, int line)
2176{
2177 return m_src_range.intersects_line_p (file, line);
2178}
2179
2180/* class fixit_replace. */
2181
2182fixit_replace::fixit_replace (source_range src_range,
2183 const char *new_content)
2184: m_src_range (src_range),
2185 m_bytes (xstrdup (new_content)),
2186 m_len (strlen (new_content))
2187{
2188}
2189
2190fixit_replace::~fixit_replace ()
2191{
2192 free (m_bytes);
2193}
2194
2195/* Implementation of fixit_hint::affects_line_p for fixit_replace. */
2196
2197bool
2198fixit_replace::affects_line_p (const char *file, int line)
2199{
2200 return m_src_range.intersects_line_p (file, line);
2201}