]> gcc.gnu.org Git - gcc.git/blob - libcpp/line-map.c
re PR middle-end/54645 (Many testsuite failures)
[gcc.git] / libcpp / line-map.c
1 /* Map logical line numbers to (source file, line number) pairs.
2 Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>.
18
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding! */
22
23 #include "config.h"
24 #include "system.h"
25 #include "line-map.h"
26 #include "cpplib.h"
27 #include "internal.h"
28 #include "hashtab.h"
29
30 static void trace_include (const struct line_maps *, const struct line_map *);
31 static const struct line_map * linemap_ordinary_map_lookup (struct line_maps *,
32 source_location);
33 static const struct line_map* linemap_macro_map_lookup (struct line_maps *,
34 source_location);
35 static source_location linemap_macro_map_loc_to_def_point
36 (const struct line_map*, source_location);
37 static source_location linemap_macro_map_loc_unwind_toward_spelling
38 (const struct line_map*, source_location);
39 static source_location linemap_macro_map_loc_to_exp_point
40 (const struct line_map*, source_location);
41 static source_location linemap_macro_loc_to_spelling_point
42 (struct line_maps *, source_location, const struct line_map **);
43 static source_location linemap_macro_loc_to_def_point (struct line_maps *,
44 source_location,
45 const struct line_map **);
46 static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
47 source_location,
48 const struct line_map **);
49
50 /* Counters defined in macro.c. */
51 extern unsigned num_expanded_macros_counter;
52 extern unsigned num_macro_tokens_counter;
53
54 /* Hash function for location_adhoc_data hashtable. */
55
56 static hashval_t
57 location_adhoc_data_hash (const void *l)
58 {
59 const struct location_adhoc_data *lb =
60 (const struct location_adhoc_data *) l;
61 return (hashval_t) lb->locus + (size_t) &lb->data;
62 }
63
64 /* Compare function for location_adhoc_data hashtable. */
65
66 static int
67 location_adhoc_data_eq (const void *l1, const void *l2)
68 {
69 const struct location_adhoc_data *lb1 =
70 (const struct location_adhoc_data *) l1;
71 const struct location_adhoc_data *lb2 =
72 (const struct location_adhoc_data *) l2;
73 return lb1->locus == lb2->locus && lb1->data == lb2->data;
74 }
75
76 /* Update the hashtable when location_adhoc_data is reallocated. */
77
78 static int
79 location_adhoc_data_update (void **slot, void *data)
80 {
81 *((char **) slot) += *((long long *) data);
82 return 1;
83 }
84
85 /* Rebuild the hash table from the location adhoc data. */
86
87 void
88 rebuild_location_adhoc_htab (struct line_maps *set)
89 {
90 unsigned i;
91 set->location_adhoc_data_map.htab =
92 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
93 for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
94 htab_find_slot (set->location_adhoc_data_map.htab,
95 set->location_adhoc_data_map.data + i, INSERT);
96 }
97
98 /* Combine LOCUS and DATA to a combined adhoc loc. */
99
100 source_location
101 get_combined_adhoc_loc (struct line_maps *set,
102 source_location locus, void *data)
103 {
104 struct location_adhoc_data lb;
105 struct location_adhoc_data **slot;
106
107 linemap_assert (data);
108
109 if (IS_ADHOC_LOC (locus))
110 locus =
111 set->location_adhoc_data_map.data[locus & MAX_SOURCE_LOCATION].locus;
112 if (locus == 0 && data == NULL)
113 return 0;
114 lb.locus = locus;
115 lb.data = data;
116 slot = (struct location_adhoc_data **)
117 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
118 if (*slot == NULL)
119 {
120 if (set->location_adhoc_data_map.curr_loc >=
121 set->location_adhoc_data_map.allocated)
122 {
123 char *orig_data = (char *) set->location_adhoc_data_map.data;
124 long long offset;
125 line_map_realloc reallocator
126 = set->reallocator ? set->reallocator : xrealloc;
127
128 if (set->location_adhoc_data_map.allocated == 0)
129 set->location_adhoc_data_map.allocated = 128;
130 else
131 set->location_adhoc_data_map.allocated *= 2;
132 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
133 reallocator (set->location_adhoc_data_map.data,
134 set->location_adhoc_data_map.allocated
135 * sizeof (struct location_adhoc_data));
136 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
137 if (set->location_adhoc_data_map.allocated > 128)
138 htab_traverse (set->location_adhoc_data_map.htab,
139 location_adhoc_data_update, &offset);
140 }
141 *slot = set->location_adhoc_data_map.data
142 + set->location_adhoc_data_map.curr_loc;
143 set->location_adhoc_data_map.data[
144 set->location_adhoc_data_map.curr_loc++] = lb;
145 }
146 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
147 }
148
149 /* Return the data for the adhoc loc. */
150
151 void *
152 get_data_from_adhoc_loc (struct line_maps *set, source_location loc)
153 {
154 linemap_assert (IS_ADHOC_LOC (loc));
155 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].data;
156 }
157
158 /* Return the location for the adhoc loc. */
159
160 source_location
161 get_location_from_adhoc_loc (struct line_maps *set, source_location loc)
162 {
163 linemap_assert (IS_ADHOC_LOC (loc));
164 return set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
165 }
166
167 /* Finalize the location_adhoc_data structure. */
168 void
169 location_adhoc_data_fini (struct line_maps *set)
170 {
171 htab_delete (set->location_adhoc_data_map.htab);
172 }
173
174 /* Initialize a line map set. */
175
176 void
177 linemap_init (struct line_maps *set)
178 {
179 memset (set, 0, sizeof (struct line_maps));
180 set->highest_location = RESERVED_LOCATION_COUNT - 1;
181 set->highest_line = RESERVED_LOCATION_COUNT - 1;
182 set->location_adhoc_data_map.htab =
183 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
184 }
185
186 /* Check for and warn about line_maps entered but not exited. */
187
188 void
189 linemap_check_files_exited (struct line_maps *set)
190 {
191 struct line_map *map;
192 /* Depending upon whether we are handling preprocessed input or
193 not, this can be a user error or an ICE. */
194 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
195 ! MAIN_FILE_P (map);
196 map = INCLUDED_FROM (set, map))
197 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
198 ORDINARY_MAP_FILE_NAME (map));
199 }
200
201 /* Create a new line map in the line map set SET, and return it.
202 REASON is the reason of creating the map. It determines the type
203 of map created (ordinary or macro map). Note that ordinary maps and
204 macro maps are allocated in different memory location. */
205
206 static struct line_map *
207 new_linemap (struct line_maps *set,
208 enum lc_reason reason)
209 {
210 /* Depending on this variable, a macro map would be allocated in a
211 different memory location than an ordinary map. */
212 bool macro_map_p = (reason == LC_ENTER_MACRO);
213 struct line_map *result;
214
215 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
216 {
217 /* We ran out of allocated line maps. Let's allocate more. */
218 unsigned alloc_size;
219
220 line_map_realloc reallocator
221 = set->reallocator ? set->reallocator : xrealloc;
222 line_map_round_alloc_size_func round_alloc_size =
223 set->round_alloc_size;
224
225 /* We are going to execute some dance to try to reduce the
226 overhead of the memory allocator, in case we are using the
227 ggc-page.c one.
228
229 The actual size of memory we are going to get back from the
230 allocator is the smallest power of 2 that is greater than the
231 size we requested. So let's consider that size then. */
232
233 alloc_size =
234 (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
235 * sizeof (struct line_map);
236
237 /* Get the actual size of memory that is going to be allocated
238 by the allocator. */
239 alloc_size = round_alloc_size (alloc_size);
240
241 /* Now alloc_size contains the exact memory size we would get if
242 we have asked for the initial alloc_size amount of memory.
243 Let's get back to the number of macro map that amounts
244 to. */
245 LINEMAPS_ALLOCATED (set, macro_map_p) =
246 alloc_size / (sizeof (struct line_map));
247
248 /* And now let's really do the re-allocation. */
249 LINEMAPS_MAPS (set, macro_map_p) =
250 (struct line_map *) (*reallocator)
251 (LINEMAPS_MAPS (set, macro_map_p),
252 (LINEMAPS_ALLOCATED (set, macro_map_p)
253 * sizeof (struct line_map)));
254
255 result =
256 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
257 memset (result, 0,
258 ((LINEMAPS_ALLOCATED (set, macro_map_p)
259 - LINEMAPS_USED (set, macro_map_p))
260 * sizeof (struct line_map)));
261 }
262 else
263 result =
264 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
265
266 LINEMAPS_USED (set, macro_map_p)++;
267
268 result->reason = reason;
269 return result;
270 }
271
272 /* Add a mapping of logical source line to physical source file and
273 line number.
274
275 The text pointed to by TO_FILE must have a lifetime
276 at least as long as the final call to lookup_line (). An empty
277 TO_FILE means standard input. If reason is LC_LEAVE, and
278 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
279 natural values considering the file we are returning to.
280
281 FROM_LINE should be monotonic increasing across calls to this
282 function. A call to this function can relocate the previous set of
283 maps, so any stored line_map pointers should not be used. */
284
285 const struct line_map *
286 linemap_add (struct line_maps *set, enum lc_reason reason,
287 unsigned int sysp, const char *to_file, linenum_type to_line)
288 {
289 struct line_map *map;
290 source_location start_location = set->highest_location + 1;
291
292 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
293 && (start_location
294 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
295
296 /* When we enter the file for the first time reason cannot be
297 LC_RENAME. */
298 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
299
300 /* If we are leaving the main file, return a NULL map. */
301 if (reason == LC_LEAVE
302 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
303 && to_file == NULL)
304 {
305 set->depth--;
306 return NULL;
307 }
308
309 map = new_linemap (set, reason);
310
311 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
312 to_file = "<stdin>";
313
314 if (reason == LC_RENAME_VERBATIM)
315 reason = LC_RENAME;
316
317 if (reason == LC_LEAVE)
318 {
319 /* When we are just leaving an "included" file, and jump to the next
320 location inside the "includer" right after the #include
321 "included", this variable points the map in use right before the
322 #include "included", inside the same "includer" file. */
323 struct line_map *from;
324 bool error;
325
326 if (MAIN_FILE_P (map - 1))
327 {
328 /* So this _should_ means we are leaving the main file --
329 effectively ending the compilation unit. But to_file not
330 being NULL means the caller thinks we are leaving to
331 another file. This is an erroneous behaviour but we'll
332 try to recover from it. Let's pretend we are not leaving
333 the main file. */
334 error = true;
335 reason = LC_RENAME;
336 from = map - 1;
337 }
338 else
339 {
340 /* (MAP - 1) points to the map we are leaving. The
341 map from which (MAP - 1) got included should be the map
342 that comes right before MAP in the same file. */
343 from = INCLUDED_FROM (set, map - 1);
344 error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
345 to_file);
346 }
347
348 /* Depending upon whether we are handling preprocessed input or
349 not, this can be a user error or an ICE. */
350 if (error)
351 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
352 to_file);
353
354 /* A TO_FILE of NULL is special - we use the natural values. */
355 if (error || to_file == NULL)
356 {
357 to_file = ORDINARY_MAP_FILE_NAME (from);
358 to_line = SOURCE_LINE (from, from[1].start_location);
359 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
360 }
361 }
362
363 linemap_assert (reason != LC_ENTER_MACRO);
364 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map) = sysp;
365 MAP_START_LOCATION (map) = start_location;
366 ORDINARY_MAP_FILE_NAME (map) = to_file;
367 ORDINARY_MAP_STARTING_LINE_NUMBER (map) = to_line;
368 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
369 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = 0;
370 set->highest_location = start_location;
371 set->highest_line = start_location;
372 set->max_column_hint = 0;
373
374 if (reason == LC_ENTER)
375 {
376 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
377 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
378 set->depth++;
379 if (set->trace_includes)
380 trace_include (set, map);
381 }
382 else if (reason == LC_RENAME)
383 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
384 ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
385 else if (reason == LC_LEAVE)
386 {
387 set->depth--;
388 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
389 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
390 }
391
392 return map;
393 }
394
395 /* Returns TRUE if the line table set tracks token locations across
396 macro expansion, FALSE otherwise. */
397
398 bool
399 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
400 {
401 return LINEMAPS_MACRO_MAPS (set) != NULL;
402 }
403
404 /* Create a macro map. A macro map encodes source locations of tokens
405 that are part of a macro replacement-list, at a macro expansion
406 point. See the extensive comments of struct line_map and struct
407 line_map_macro, in line-map.h.
408
409 This map shall be created when the macro is expanded. The map
410 encodes the source location of the expansion point of the macro as
411 well as the "original" source location of each token that is part
412 of the macro replacement-list. If a macro is defined but never
413 expanded, it has no macro map. SET is the set of maps the macro
414 map should be part of. MACRO_NODE is the macro which the new macro
415 map should encode source locations for. EXPANSION is the location
416 of the expansion point of MACRO. For function-like macros
417 invocations, it's best to make it point to the closing parenthesis
418 of the macro, rather than the the location of the first character
419 of the macro. NUM_TOKENS is the number of tokens that are part of
420 the replacement-list of MACRO.
421
422 Note that when we run out of the integer space available for source
423 locations, this function returns NULL. In that case, callers of
424 this function cannot encode {line,column} pairs into locations of
425 macro tokens anymore. */
426
427 const struct line_map *
428 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
429 source_location expansion, unsigned int num_tokens)
430 {
431 struct line_map *map;
432 source_location start_location;
433 line_map_realloc reallocator
434 = set->reallocator ? set->reallocator : xrealloc;
435
436 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
437
438 if (start_location <= set->highest_line
439 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
440 /* We ran out of macro map space. */
441 return NULL;
442
443 map = new_linemap (set, LC_ENTER_MACRO);
444
445 MAP_START_LOCATION (map) = start_location;
446 MACRO_MAP_MACRO (map) = macro_node;
447 MACRO_MAP_NUM_MACRO_TOKENS (map) = num_tokens;
448 MACRO_MAP_LOCATIONS (map)
449 = (source_location*) reallocator (NULL,
450 2 * num_tokens
451 * sizeof (source_location));
452 MACRO_MAP_EXPANSION_POINT_LOCATION (map) = expansion;
453 memset (MACRO_MAP_LOCATIONS (map), 0,
454 num_tokens * sizeof (source_location));
455
456 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
457
458 return map;
459 }
460
461 /* Create and return a virtual location for a token that is part of a
462 macro expansion-list at a macro expansion point. See the comment
463 inside struct line_map_macro to see what an expansion-list exactly
464 is.
465
466 A call to this function must come after a call to
467 linemap_enter_macro.
468
469 MAP is the map into which the source location is created. TOKEN_NO
470 is the index of the token in the macro replacement-list, starting
471 at number 0.
472
473 ORIG_LOC is the location of the token outside of this macro
474 expansion. If the token comes originally from the macro
475 definition, it is the locus in the macro definition; otherwise it
476 is a location in the context of the caller of this macro expansion
477 (which is a virtual location or a source location if the caller is
478 itself a macro expansion or not).
479
480 MACRO_DEFINITION_LOC is the location in the macro definition,
481 either of the token itself or of a macro parameter that it
482 replaces. */
483
484 source_location
485 linemap_add_macro_token (const struct line_map *map,
486 unsigned int token_no,
487 source_location orig_loc,
488 source_location orig_parm_replacement_loc)
489 {
490 source_location result;
491
492 linemap_assert (linemap_macro_expansion_map_p (map));
493 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
494
495 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
496 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
497
498 result = MAP_START_LOCATION (map) + token_no;
499 return result;
500 }
501
502 /* Return a source_location for the start (i.e. column==0) of
503 (physical) line TO_LINE in the current source file (as in the
504 most recent linemap_add). MAX_COLUMN_HINT is the highest column
505 number we expect to use in this line (but it does not change
506 the highest_location). */
507
508 source_location
509 linemap_line_start (struct line_maps *set, linenum_type to_line,
510 unsigned int max_column_hint)
511 {
512 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
513 source_location highest = set->highest_location;
514 source_location r;
515 linenum_type last_line =
516 SOURCE_LINE (map, set->highest_line);
517 int line_delta = to_line - last_line;
518 bool add_map = false;
519
520 if (line_delta < 0
521 || (line_delta > 10
522 && line_delta * ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) > 1000)
523 || (max_column_hint >= (1U << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)))
524 || (max_column_hint <= 80
525 && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10))
526 {
527 add_map = true;
528 }
529 else
530 max_column_hint = set->max_column_hint;
531 if (add_map)
532 {
533 int column_bits;
534 if (max_column_hint > 100000 || highest > 0x60000000)
535 {
536 /* If the column number is ridiculous or we've allocated a huge
537 number of source_locations, give up on column numbers. */
538 max_column_hint = 0;
539 if (highest >0x70000000)
540 return 0;
541 column_bits = 0;
542 }
543 else
544 {
545 column_bits = 7;
546 while (max_column_hint >= (1U << column_bits))
547 column_bits++;
548 max_column_hint = 1U << column_bits;
549 }
550 /* Allocate the new line_map. However, if the current map only has a
551 single line we can sometimes just increase its column_bits instead. */
552 if (line_delta < 0
553 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
554 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
555 map = (struct line_map *) linemap_add (set, LC_RENAME,
556 ORDINARY_MAP_IN_SYSTEM_HEADER_P
557 (map),
558 ORDINARY_MAP_FILE_NAME (map),
559 to_line);
560 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = column_bits;
561 r = (MAP_START_LOCATION (map)
562 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
563 << column_bits));
564 }
565 else
566 r = highest - SOURCE_COLUMN (map, highest)
567 + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
568
569 /* Locations of ordinary tokens are always lower than locations of
570 macro tokens. */
571 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
572 return 0;
573
574 set->highest_line = r;
575 if (r > set->highest_location)
576 set->highest_location = r;
577 set->max_column_hint = max_column_hint;
578 return r;
579 }
580
581 /* Encode and return a source_location from a column number. The
582 source line considered is the last source line used to call
583 linemap_line_start, i.e, the last source line which a location was
584 encoded from. */
585
586 source_location
587 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
588 {
589 source_location r = set->highest_line;
590
591 linemap_assert
592 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
593
594 if (to_column >= set->max_column_hint)
595 {
596 if (r >= 0xC000000 || to_column > 100000)
597 {
598 /* Running low on source_locations - disable column numbers. */
599 return r;
600 }
601 else
602 {
603 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
604 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
605 }
606 }
607 r = r + to_column;
608 if (r >= set->highest_location)
609 set->highest_location = r;
610 return r;
611 }
612
613 /* Encode and return a source location from a given line and
614 column. */
615
616 source_location
617 linemap_position_for_line_and_column (struct line_map *map,
618 linenum_type line,
619 unsigned column)
620 {
621 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (map) <= line);
622
623 return (MAP_START_LOCATION (map)
624 + ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
625 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
626 + (column & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1)));
627 }
628
629 /* Given a virtual source location yielded by a map (either an
630 ordinary or a macro map), returns that map. */
631
632 const struct line_map*
633 linemap_lookup (struct line_maps *set, source_location line)
634 {
635 if (IS_ADHOC_LOC (line))
636 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
637 if (linemap_location_from_macro_expansion_p (set, line))
638 return linemap_macro_map_lookup (set, line);
639 return linemap_ordinary_map_lookup (set, line);
640 }
641
642 /* Given a source location yielded by an ordinary map, returns that
643 map. Since the set is built chronologically, the logical lines are
644 monotonic increasing, and so the list is sorted and we can use a
645 binary search. */
646
647 static const struct line_map *
648 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
649 {
650 unsigned int md, mn, mx;
651 const struct line_map *cached, *result;
652
653 if (IS_ADHOC_LOC (line))
654 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
655
656 if (set == NULL || line < RESERVED_LOCATION_COUNT)
657 return NULL;
658
659 mn = LINEMAPS_ORDINARY_CACHE (set);
660 mx = LINEMAPS_ORDINARY_USED (set);
661
662 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
663 /* We should get a segfault if no line_maps have been added yet. */
664 if (line >= MAP_START_LOCATION (cached))
665 {
666 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
667 return cached;
668 }
669 else
670 {
671 mx = mn;
672 mn = 0;
673 }
674
675 while (mx - mn > 1)
676 {
677 md = (mn + mx) / 2;
678 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
679 mx = md;
680 else
681 mn = md;
682 }
683
684 LINEMAPS_ORDINARY_CACHE (set) = mn;
685 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
686 linemap_assert (line >= MAP_START_LOCATION (result));
687 return result;
688 }
689
690 /* Given a source location yielded by a macro map, returns that map.
691 Since the set is built chronologically, the logical lines are
692 monotonic decreasing, and so the list is sorted and we can use a
693 binary search. */
694
695 static const struct line_map*
696 linemap_macro_map_lookup (struct line_maps *set, source_location line)
697 {
698 unsigned int md, mn, mx;
699 const struct line_map *cached, *result;
700
701 if (IS_ADHOC_LOC (line))
702 line = set->location_adhoc_data_map.data[line & MAX_SOURCE_LOCATION].locus;
703
704 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
705
706 if (set == NULL)
707 return NULL;
708
709 mn = LINEMAPS_MACRO_CACHE (set);
710 mx = LINEMAPS_MACRO_USED (set);
711 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
712
713 if (line >= MAP_START_LOCATION (cached))
714 {
715 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
716 return cached;
717 mx = mn - 1;
718 mn = 0;
719 }
720
721 while (mn < mx)
722 {
723 md = (mx + mn) / 2;
724 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
725 mn = md + 1;
726 else
727 mx = md;
728 }
729
730 LINEMAPS_MACRO_CACHE (set) = mx;
731 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
732 linemap_assert (MAP_START_LOCATION (result) <= line);
733
734 return result;
735 }
736
737 /* Return TRUE if MAP encodes locations coming from a macro
738 replacement-list at macro expansion point. */
739
740 bool
741 linemap_macro_expansion_map_p (const struct line_map *map)
742 {
743 if (!map)
744 return false;
745 return (map->reason == LC_ENTER_MACRO);
746 }
747
748 /* If LOCATION is the locus of a token in a replacement-list of a
749 macro expansion return the location of the macro expansion point.
750
751 Read the comments of struct line_map and struct line_map_macro in
752 line-map.h to understand what a macro expansion point is. */
753
754 static source_location
755 linemap_macro_map_loc_to_exp_point (const struct line_map *map,
756 source_location location ATTRIBUTE_UNUSED)
757 {
758 linemap_assert (linemap_macro_expansion_map_p (map)
759 && location >= MAP_START_LOCATION (map));
760
761 /* Make sure LOCATION is correct. */
762 linemap_assert ((location - MAP_START_LOCATION (map))
763 < MACRO_MAP_NUM_MACRO_TOKENS (map));
764
765 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
766 }
767
768 /* If LOCATION is the source location of a token that belongs to a
769 macro replacement-list -- as part of a macro expansion -- then
770 return the location of the token at the definition point of the
771 macro. Otherwise, return LOCATION. SET is the set of maps
772 location come from. ORIGINAL_MAP is an output parm. If non NULL,
773 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
774 returned location comes from. */
775
776 source_location
777 linemap_macro_map_loc_to_def_point (const struct line_map *map,
778 source_location location)
779 {
780 unsigned token_no;
781
782 linemap_assert (linemap_macro_expansion_map_p (map)
783 && location >= MAP_START_LOCATION (map));
784 linemap_assert (location >= RESERVED_LOCATION_COUNT);
785
786 token_no = location - MAP_START_LOCATION (map);
787 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
788
789 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
790
791 return location;
792 }
793
794 /* If LOCATION is the locus of a token that is an argument of a
795 function-like macro M and appears in the expansion of M, return the
796 locus of that argument in the context of the caller of M.
797
798 In other words, this returns the xI location presented in the
799 comments of line_map_macro above. */
800 source_location
801 linemap_macro_map_loc_unwind_toward_spelling (const struct line_map* map,
802 source_location location)
803 {
804 unsigned token_no;
805
806 linemap_assert (linemap_macro_expansion_map_p (map)
807 && location >= MAP_START_LOCATION (map));
808 linemap_assert (location >= RESERVED_LOCATION_COUNT);
809
810 token_no = location - MAP_START_LOCATION (map);
811 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
812
813 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
814
815 return location;
816 }
817
818 /* Return the source line number corresponding to source location
819 LOCATION. SET is the line map set LOCATION comes from. If
820 LOCATION is the source location of token that is part of the
821 replacement-list of a macro expansion return the line number of the
822 macro expansion point. */
823
824 int
825 linemap_get_expansion_line (struct line_maps *set,
826 source_location location)
827 {
828 const struct line_map *map = NULL;
829
830 if (IS_ADHOC_LOC (location))
831 location = set->location_adhoc_data_map.data[
832 location & MAX_SOURCE_LOCATION].locus;
833
834 if (location < RESERVED_LOCATION_COUNT)
835 return 0;
836
837 location =
838 linemap_macro_loc_to_exp_point (set, location, &map);
839
840 return SOURCE_LINE (map, location);
841 }
842
843 /* Return the path of the file corresponding to source code location
844 LOCATION.
845
846 If LOCATION is the source location of token that is part of the
847 replacement-list of a macro expansion return the file path of the
848 macro expansion point.
849
850 SET is the line map set LOCATION comes from. */
851
852 const char*
853 linemap_get_expansion_filename (struct line_maps *set,
854 source_location location)
855 {
856 const struct line_map *map = NULL;
857
858 if (IS_ADHOC_LOC (location))
859 location = set->location_adhoc_data_map.data[
860 location & MAX_SOURCE_LOCATION].locus;
861
862 if (location < RESERVED_LOCATION_COUNT)
863 return NULL;
864
865 location =
866 linemap_macro_loc_to_exp_point (set, location, &map);
867
868 return LINEMAP_FILE (map);
869 }
870
871 /* Return the name of the macro associated to MACRO_MAP. */
872
873 const char*
874 linemap_map_get_macro_name (const struct line_map* macro_map)
875 {
876 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
877 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
878 }
879
880 /* Return a positive value if LOCATION is the locus of a token that is
881 located in a system header, O otherwise. It returns 1 if LOCATION
882 is the locus of a token that is located in a system header, and 2
883 if LOCATION is the locus of a token located in a C system header
884 that therefore needs to be extern "C" protected in C++.
885
886 Note that this function returns 1 if LOCATION belongs to a token
887 that is part of a macro replacement-list defined in a system
888 header, but expanded in a non-system file. */
889
890 int
891 linemap_location_in_system_header_p (struct line_maps *set,
892 source_location location)
893 {
894 const struct line_map *map = NULL;
895
896 if (IS_ADHOC_LOC (location))
897 location = set->location_adhoc_data_map.data[
898 location & MAX_SOURCE_LOCATION].locus;
899
900 if (location < RESERVED_LOCATION_COUNT)
901 return false;
902
903 /* Let's look at where the token for LOCATION comes from. */
904 while (true)
905 {
906 map = linemap_lookup (set, location);
907 if (map != NULL)
908 {
909 if (!linemap_macro_expansion_map_p (map))
910 /* It's a normal token. */
911 return LINEMAP_SYSP (map);
912 else
913 {
914 /* It's a token resulting from a macro expansion. */
915 source_location loc =
916 linemap_macro_map_loc_unwind_toward_spelling (map, location);
917 if (loc < RESERVED_LOCATION_COUNT)
918 /* This token might come from a built-in macro. Let's
919 look at where that macro got expanded. */
920 location = linemap_macro_map_loc_to_exp_point (map, location);
921 else
922 location = loc;
923 }
924 }
925 else
926 break;
927 }
928 return false;
929 }
930
931 /* Return TRUE if LOCATION is a source code location of a token coming
932 from a macro replacement-list at a macro expansion point, FALSE
933 otherwise. */
934
935 bool
936 linemap_location_from_macro_expansion_p (struct line_maps *set,
937 source_location location)
938 {
939 if (IS_ADHOC_LOC (location))
940 location = set->location_adhoc_data_map.data[
941 location & MAX_SOURCE_LOCATION].locus;
942
943 linemap_assert (location <= MAX_SOURCE_LOCATION
944 && (set->highest_location
945 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
946 if (set == NULL)
947 return false;
948 return (location > set->highest_location);
949 }
950
951 /* Given two virtual locations *LOC0 and *LOC1, return the first
952 common macro map in their macro expansion histories. Return NULL
953 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
954 virtual location of the token inside the resulting macro. */
955
956 static const struct line_map*
957 first_map_in_common_1 (struct line_maps *set,
958 source_location *loc0,
959 source_location *loc1)
960 {
961 source_location l0 = *loc0, l1 = *loc1;
962 const struct line_map *map0 = linemap_lookup (set, l0),
963 *map1 = linemap_lookup (set, l1);
964
965 while (linemap_macro_expansion_map_p (map0)
966 && linemap_macro_expansion_map_p (map1)
967 && (map0 != map1))
968 {
969 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
970 {
971 l0 = linemap_macro_map_loc_to_exp_point (map0, l0);
972 map0 = linemap_lookup (set, l0);
973 }
974 else
975 {
976 l1 = linemap_macro_map_loc_to_exp_point (map1, l1);
977 map1 = linemap_lookup (set, l1);
978 }
979 }
980
981 if (map0 == map1)
982 {
983 *loc0 = l0;
984 *loc1 = l1;
985 return map0;
986 }
987 return NULL;
988 }
989
990 /* Given two virtual locations LOC0 and LOC1, return the first common
991 macro map in their macro expansion histories. Return NULL if no
992 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
993 virtual location of the token inside the resulting macro, upon
994 return of a non-NULL result. */
995
996 static const struct line_map*
997 first_map_in_common (struct line_maps *set,
998 source_location loc0,
999 source_location loc1,
1000 source_location *res_loc0,
1001 source_location *res_loc1)
1002 {
1003 *res_loc0 = loc0;
1004 *res_loc1 = loc1;
1005
1006 return first_map_in_common_1 (set, res_loc0, res_loc1);
1007 }
1008
1009 /* Return a positive value if PRE denotes the location of a token that
1010 comes before the token of POST, 0 if PRE denotes the location of
1011 the same token as the token for POST, and a negative value
1012 otherwise. */
1013
1014 int
1015 linemap_compare_locations (struct line_maps *set,
1016 source_location pre,
1017 source_location post)
1018 {
1019 bool pre_virtual_p, post_virtual_p;
1020 source_location l0 = pre, l1 = post;
1021
1022 if (l0 == l1)
1023 return 0;
1024
1025 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
1026 l0 = linemap_resolve_location (set, l0,
1027 LRK_MACRO_EXPANSION_POINT,
1028 NULL);
1029
1030 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
1031 l1 = linemap_resolve_location (set, l1,
1032 LRK_MACRO_EXPANSION_POINT,
1033 NULL);
1034
1035 if (l0 == l1
1036 && pre_virtual_p
1037 && post_virtual_p)
1038 {
1039 /* So pre and post represent two tokens that are present in a
1040 same macro expansion. Let's see if the token for pre was
1041 before the token for post in that expansion. */
1042 unsigned i0, i1;
1043 const struct line_map *map =
1044 first_map_in_common (set, pre, post, &l0, &l1);
1045
1046 if (map == NULL)
1047 /* This should not be possible. */
1048 abort ();
1049
1050 i0 = l0 - MAP_START_LOCATION (map);
1051 i1 = l1 - MAP_START_LOCATION (map);
1052 return i1 - i0;
1053 }
1054
1055 return l1 - l0;
1056 }
1057
1058 /* Print an include trace, for e.g. the -H option of the preprocessor. */
1059
1060 static void
1061 trace_include (const struct line_maps *set, const struct line_map *map)
1062 {
1063 unsigned int i = set->depth;
1064
1065 while (--i)
1066 putc ('.', stderr);
1067
1068 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
1069 }
1070
1071 /* Return the spelling location of the token wherever it comes from,
1072 whether part of a macro definition or not.
1073
1074 This is a subroutine for linemap_resolve_location. */
1075
1076 static source_location
1077 linemap_macro_loc_to_spelling_point (struct line_maps *set,
1078 source_location location,
1079 const struct line_map **original_map)
1080 {
1081 struct line_map *map;
1082
1083 if (IS_ADHOC_LOC (location))
1084 location = set->location_adhoc_data_map.data[
1085 location & MAX_SOURCE_LOCATION].locus;
1086
1087 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1088
1089 while (true)
1090 {
1091 map = (struct line_map*) linemap_lookup (set, location);
1092 if (!linemap_macro_expansion_map_p (map))
1093 break;
1094
1095 location =
1096 linemap_macro_map_loc_unwind_toward_spelling (map, location);
1097 }
1098
1099 if (original_map)
1100 *original_map = map;
1101 return location;
1102 }
1103
1104 /* If LOCATION is the source location of a token that belongs to a
1105 macro replacement-list -- as part of a macro expansion -- then
1106 return the location of the token at the definition point of the
1107 macro. Otherwise, return LOCATION. SET is the set of maps
1108 location come from. ORIGINAL_MAP is an output parm. If non NULL,
1109 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
1110 returned location comes from.
1111
1112 This is a subroutine of linemap_resolve_location. */
1113
1114 static source_location
1115 linemap_macro_loc_to_def_point (struct line_maps *set,
1116 source_location location,
1117 const struct line_map **original_map)
1118 {
1119 struct line_map *map;
1120
1121 if (IS_ADHOC_LOC (location))
1122 location = set->location_adhoc_data_map.data[
1123 location & MAX_SOURCE_LOCATION].locus;
1124
1125 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1126
1127 while (true)
1128 {
1129 map = (struct line_map*) linemap_lookup (set, location);
1130 if (!linemap_macro_expansion_map_p (map))
1131 break;
1132
1133 location =
1134 linemap_macro_map_loc_to_def_point (map, location);
1135 }
1136
1137 if (original_map)
1138 *original_map = map;
1139 return location;
1140 }
1141
1142 /* If LOCATION is the source location of a token that belongs to a
1143 macro replacement-list -- at a macro expansion point -- then return
1144 the location of the topmost expansion point of the macro. We say
1145 topmost because if we are in the context of a nested macro
1146 expansion, the function returns the source location of the first
1147 macro expansion that triggered the nested expansions.
1148
1149 Otherwise, return LOCATION. SET is the set of maps location come
1150 from. ORIGINAL_MAP is an output parm. If non NULL, the function
1151 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
1152 location comes from.
1153
1154 This is a subroutine of linemap_resolve_location. */
1155
1156 static source_location
1157 linemap_macro_loc_to_exp_point (struct line_maps *set,
1158 source_location location,
1159 const struct line_map **original_map)
1160 {
1161 struct line_map *map;
1162
1163 if (IS_ADHOC_LOC (location))
1164 location = set->location_adhoc_data_map.data[
1165 location & MAX_SOURCE_LOCATION].locus;
1166
1167 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
1168
1169 while (true)
1170 {
1171 map = (struct line_map*) linemap_lookup (set, location);
1172 if (!linemap_macro_expansion_map_p (map))
1173 break;
1174 location = linemap_macro_map_loc_to_exp_point (map, location);
1175 }
1176
1177 if (original_map)
1178 *original_map = map;
1179 return location;
1180 }
1181
1182 /* Resolve a virtual location into either a spelling location, an
1183 expansion point location or a token argument replacement point
1184 location. Return the map that encodes the virtual location as well
1185 as the resolved location.
1186
1187 If LOC is *NOT* the location of a token resulting from the
1188 expansion of a macro, then the parameter LRK (which stands for
1189 Location Resolution Kind) is ignored and the resulting location
1190 just equals the one given in argument.
1191
1192 Now if LOC *IS* the location of a token resulting from the
1193 expansion of a macro, this is what happens.
1194
1195 * If LRK is set to LRK_MACRO_EXPANSION_POINT
1196 -------------------------------
1197
1198 The virtual location is resolved to the first macro expansion point
1199 that led to this macro expansion.
1200
1201 * If LRK is set to LRK_SPELLING_LOCATION
1202 -------------------------------------
1203
1204 The virtual location is resolved to the locus where the token has
1205 been spelled in the source. This can follow through all the macro
1206 expansions that led to the token.
1207
1208 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
1209 --------------------------------------
1210
1211 The virtual location is resolved to the locus of the token in the
1212 context of the macro definition.
1213
1214 If LOC is the locus of a token that is an argument of a
1215 function-like macro [replacing a parameter in the replacement list
1216 of the macro] the virtual location is resolved to the locus of the
1217 parameter that is replaced, in the context of the definition of the
1218 macro.
1219
1220 If LOC is the locus of a token that is not an argument of a
1221 function-like macro, then the function behaves as if LRK was set to
1222 LRK_SPELLING_LOCATION.
1223
1224 If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
1225 returned location. Note that if the returned location wasn't originally
1226 encoded by a map, the *MAP is set to NULL. This can happen if LOC
1227 resolves to a location reserved for the client code, like
1228 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
1229
1230 source_location
1231 linemap_resolve_location (struct line_maps *set,
1232 source_location loc,
1233 enum location_resolution_kind lrk,
1234 const struct line_map **map)
1235 {
1236 if (IS_ADHOC_LOC (loc))
1237 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1238
1239 if (loc < RESERVED_LOCATION_COUNT)
1240 {
1241 /* A reserved location wasn't encoded in a map. Let's return a
1242 NULL map here, just like what linemap_ordinary_map_lookup
1243 does. */
1244 if (map)
1245 *map = NULL;
1246 return loc;
1247 }
1248
1249 switch (lrk)
1250 {
1251 case LRK_MACRO_EXPANSION_POINT:
1252 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1253 break;
1254 case LRK_SPELLING_LOCATION:
1255 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1256 break;
1257 case LRK_MACRO_DEFINITION_LOCATION:
1258 loc = linemap_macro_loc_to_def_point (set, loc, map);
1259 break;
1260 default:
1261 abort ();
1262 }
1263 return loc;
1264 }
1265
1266 /*
1267 Suppose that LOC is the virtual location of a token T coming from
1268 the expansion of a macro M. This function then steps up to get the
1269 location L of the point where M got expanded. If L is a spelling
1270 location inside a macro expansion M', then this function returns
1271 the locus of the point where M' was expanded. Said otherwise, this
1272 function returns the location of T in the context that triggered
1273 the expansion of M.
1274
1275 *LOC_MAP must be set to the map of LOC. This function then sets it
1276 to the map of the returned location. */
1277
1278 source_location
1279 linemap_unwind_toward_expansion (struct line_maps *set,
1280 source_location loc,
1281 const struct line_map **map)
1282 {
1283 source_location resolved_location;
1284 const struct line_map *resolved_map;
1285
1286 if (IS_ADHOC_LOC (loc))
1287 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1288
1289 resolved_location =
1290 linemap_macro_map_loc_unwind_toward_spelling (*map, loc);
1291 resolved_map = linemap_lookup (set, resolved_location);
1292
1293 if (!linemap_macro_expansion_map_p (resolved_map))
1294 {
1295 resolved_location = linemap_macro_map_loc_to_exp_point (*map, loc);
1296 resolved_map = linemap_lookup (set, resolved_location);
1297 }
1298
1299 *map = resolved_map;
1300 return resolved_location;
1301 }
1302
1303 /* If LOC is the virtual location of a token coming from the expansion
1304 of a macro M and if its spelling location is reserved (e.g, a
1305 location for a built-in token), then this function unwinds (using
1306 linemap_unwind_toward_expansion) the location until a location that
1307 is not reserved and is not in a system header is reached. In other
1308 words, this unwinds the reserved location until a location that is
1309 in real source code is reached.
1310
1311 Otherwise, if the spelling location for LOC is not reserved or if
1312 LOC doesn't come from the expansion of a macro, the function
1313 returns LOC as is and *MAP is not touched.
1314
1315 *MAP is set to the map of the returned location if the later is
1316 different from LOC. */
1317 source_location
1318 linemap_unwind_to_first_non_reserved_loc (struct line_maps *set,
1319 source_location loc,
1320 const struct line_map **map)
1321 {
1322 source_location resolved_loc;
1323 const struct line_map *map0 = NULL, *map1 = NULL;
1324
1325 if (IS_ADHOC_LOC (loc))
1326 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1327
1328 map0 = linemap_lookup (set, loc);
1329 if (!linemap_macro_expansion_map_p (map0))
1330 return loc;
1331
1332 resolved_loc = linemap_resolve_location (set, loc,
1333 LRK_SPELLING_LOCATION,
1334 &map1);
1335
1336 if (resolved_loc >= RESERVED_LOCATION_COUNT
1337 && !LINEMAP_SYSP (map1))
1338 return loc;
1339
1340 while (linemap_macro_expansion_map_p (map0)
1341 && (resolved_loc < RESERVED_LOCATION_COUNT
1342 || LINEMAP_SYSP (map1)))
1343 {
1344 loc = linemap_unwind_toward_expansion (set, loc, &map0);
1345 resolved_loc = linemap_resolve_location (set, loc,
1346 LRK_SPELLING_LOCATION,
1347 &map1);
1348 }
1349
1350 if (map != NULL)
1351 *map = map0;
1352 return loc;
1353 }
1354
1355 /* Expand source code location LOC and return a user readable source
1356 code location. LOC must be a spelling (non-virtual) location. If
1357 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
1358 location is returned. */
1359
1360 expanded_location
1361 linemap_expand_location (struct line_maps *set,
1362 const struct line_map *map,
1363 source_location loc)
1364
1365 {
1366 expanded_location xloc;
1367
1368 memset (&xloc, 0, sizeof (xloc));
1369 if (IS_ADHOC_LOC (loc))
1370 {
1371 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1372 xloc.data = set->location_adhoc_data_map.data[
1373 loc & MAX_SOURCE_LOCATION].data;
1374 }
1375
1376 if (loc < RESERVED_LOCATION_COUNT)
1377 /* The location for this token wasn't generated from a line map.
1378 It was probably a location for a builtin token, chosen by some
1379 client code. Let's not try to expand the location in that
1380 case. */;
1381 else if (map == NULL)
1382 /* We shouldn't be getting a NULL map with a location that is not
1383 reserved by the client code. */
1384 abort ();
1385 else
1386 {
1387 /* MAP must be an ordinary map and LOC must be non-virtual,
1388 encoded into this map, obviously; the accessors used on MAP
1389 below ensure it is ordinary. Let's just assert the
1390 non-virtualness of LOC here. */
1391 if (linemap_location_from_macro_expansion_p (set, loc))
1392 abort ();
1393
1394 xloc.file = LINEMAP_FILE (map);
1395 xloc.line = SOURCE_LINE (map, loc);
1396 xloc.column = SOURCE_COLUMN (map, loc);
1397 xloc.sysp = LINEMAP_SYSP (map) != 0;
1398 }
1399
1400 return xloc;
1401 }
1402
1403
1404 /* Dump line map at index IX in line table SET to STREAM. If STREAM
1405 is NULL, use stderr. IS_MACRO is true if the caller wants to
1406 dump a macro map, false otherwise. */
1407
1408 void
1409 linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro)
1410 {
1411 const char *lc_reasons_v[LC_ENTER_MACRO + 1]
1412 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
1413 "LC_ENTER_MACRO" };
1414 const char *reason;
1415 struct line_map *map;
1416
1417 if (stream == NULL)
1418 stream = stderr;
1419
1420 if (!is_macro)
1421 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
1422 else
1423 map = LINEMAPS_MACRO_MAP_AT (set, ix);
1424
1425 reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???";
1426
1427 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
1428 ix, (void *) map, map->start_location, reason,
1429 (!is_macro && ORDINARY_MAP_IN_SYSTEM_HEADER_P (map)) ? "yes" : "no");
1430 if (!is_macro)
1431 {
1432 unsigned includer_ix;
1433 struct line_map *includer_map;
1434
1435 includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (map);
1436 includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set)
1437 ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix)
1438 : NULL;
1439
1440 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (map),
1441 ORDINARY_MAP_STARTING_LINE_NUMBER (map));
1442 fprintf (stream, "Included from: [%d] %s\n", includer_ix,
1443 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
1444 }
1445 else
1446 fprintf (stream, "Macro: %s (%u tokens)\n",
1447 linemap_map_get_macro_name (map),
1448 MACRO_MAP_NUM_MACRO_TOKENS (map));
1449
1450 fprintf (stream, "\n");
1451 }
1452
1453
1454 /* Dump debugging information about source location LOC into the file
1455 stream STREAM. SET is the line map set LOC comes from. */
1456
1457 void
1458 linemap_dump_location (struct line_maps *set,
1459 source_location loc,
1460 FILE *stream)
1461 {
1462 const struct line_map *map;
1463 source_location location;
1464 const char *path = "", *from = "";
1465 int l = -1, c = -1, s = -1, e = -1;
1466
1467 if (IS_ADHOC_LOC (loc))
1468 loc = set->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
1469
1470 if (loc == 0)
1471 return;
1472
1473 location =
1474 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1475
1476 if (map == NULL)
1477 /* Only reserved locations can be tolerated in this case. */
1478 linemap_assert (location < RESERVED_LOCATION_COUNT);
1479 else
1480 {
1481 path = LINEMAP_FILE (map);
1482 l = SOURCE_LINE (map, location);
1483 c = SOURCE_COLUMN (map, location);
1484 s = LINEMAP_SYSP (map) != 0;
1485 e = location != loc;
1486 if (e)
1487 from = "N/A";
1488 else
1489 from = (INCLUDED_FROM (set, map))
1490 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1491 : "<NULL>";
1492 }
1493
1494 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1495 E: macro expansion?, LOC: original location, R: resolved location */
1496 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
1497 path, from, l, c, s, (void*)map, e, loc, location);
1498 }
1499
1500 /* Compute and return statistics about the memory consumption of some
1501 parts of the line table SET. */
1502
1503 void
1504 linemap_get_statistics (struct line_maps *set,
1505 struct linemap_stats *s)
1506 {
1507 long ordinary_maps_allocated_size, ordinary_maps_used_size,
1508 macro_maps_allocated_size, macro_maps_used_size,
1509 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1510
1511 struct line_map *cur_map;
1512
1513 ordinary_maps_allocated_size =
1514 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map);
1515
1516 ordinary_maps_used_size =
1517 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map);
1518
1519 macro_maps_allocated_size =
1520 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map);
1521
1522 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1523 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1524 ++cur_map)
1525 {
1526 unsigned i;
1527
1528 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1529
1530 macro_maps_locations_size +=
1531 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1532
1533 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1534 {
1535 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1536 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1537 duplicated_macro_maps_locations_size +=
1538 sizeof (source_location);
1539 }
1540 }
1541
1542 macro_maps_used_size =
1543 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map);
1544
1545 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1546 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1547 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1548 s->ordinary_maps_used_size = ordinary_maps_used_size;
1549 s->num_expanded_macros = num_expanded_macros_counter;
1550 s->num_macro_tokens = num_macro_tokens_counter;
1551 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1552 s->macro_maps_allocated_size = macro_maps_allocated_size;
1553 s->macro_maps_locations_size = macro_maps_locations_size;
1554 s->macro_maps_used_size = macro_maps_used_size;
1555 s->duplicated_macro_maps_locations_size =
1556 duplicated_macro_maps_locations_size;
1557 }
1558
1559
1560 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
1561 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
1562 specifies how many macro maps to dump. */
1563
1564 void
1565 line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary,
1566 unsigned int num_macro)
1567 {
1568 unsigned int i;
1569
1570 if (set == NULL)
1571 return;
1572
1573 if (stream == NULL)
1574 stream = stderr;
1575
1576 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
1577 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
1578 fprintf (stream, "Include stack depth: %d\n", set->depth);
1579 fprintf (stream, "Highest location: %u\n", set->highest_location);
1580
1581 if (num_ordinary)
1582 {
1583 fprintf (stream, "\nOrdinary line maps\n");
1584 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
1585 linemap_dump (stream, set, i, false);
1586 fprintf (stream, "\n");
1587 }
1588
1589 if (num_macro)
1590 {
1591 fprintf (stream, "\nMacro line maps\n");
1592 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
1593 linemap_dump (stream, set, i, true);
1594 fprintf (stream, "\n");
1595 }
1596 }
This page took 0.167579 seconds and 6 git commands to generate.