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