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