]> gcc.gnu.org Git - gcc.git/blob - libcpp/line-map.c
Add line map statistics to -fmem-report output
[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
29 static void trace_include (const struct line_maps *, const struct line_map *);
30 static const struct line_map * linemap_ordinary_map_lookup (struct line_maps *,
31 source_location);
32 static const struct line_map* linemap_macro_map_lookup (struct line_maps *,
33 source_location);
34 static source_location linemap_macro_map_loc_to_def_point
35 (const struct line_map*, source_location);
36 static source_location linemap_macro_map_loc_unwind_toward_spelling
37 (const struct line_map*, source_location);
38 static source_location linemap_macro_map_loc_to_exp_point
39 (const struct line_map*, source_location);
40 static source_location linemap_macro_loc_to_spelling_point
41 (struct line_maps *, source_location, const struct line_map **);
42 static source_location linemap_macro_loc_to_def_point (struct line_maps *,
43 source_location,
44 const struct line_map **);
45 static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
46 source_location,
47 const struct line_map **);
48
49 /* Counters defined in macro.c. */
50 extern unsigned num_expanded_macros_counter;
51 extern unsigned num_macro_tokens_counter;
52
53 /* Initialize a line map set. */
54
55 void
56 linemap_init (struct line_maps *set)
57 {
58 memset (set, 0, sizeof (struct line_maps));
59 set->highest_location = RESERVED_LOCATION_COUNT - 1;
60 set->highest_line = RESERVED_LOCATION_COUNT - 1;
61 }
62
63 /* Check for and warn about line_maps entered but not exited. */
64
65 void
66 linemap_check_files_exited (struct line_maps *set)
67 {
68 struct line_map *map;
69 /* Depending upon whether we are handling preprocessed input or
70 not, this can be a user error or an ICE. */
71 for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
72 ! MAIN_FILE_P (map);
73 map = INCLUDED_FROM (set, map))
74 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
75 ORDINARY_MAP_FILE_NAME (map));
76 }
77
78 /* Create a new line map in the line map set SET, and return it.
79 REASON is the reason of creating the map. It determines the type
80 of map created (ordinary or macro map). Note that ordinary maps and
81 macro maps are allocated in different memory location. */
82
83 static struct line_map *
84 new_linemap (struct line_maps *set,
85 enum lc_reason reason)
86 {
87 /* Depending on this variable, a macro map would be allocated in a
88 different memory location than an ordinary map. */
89 bool macro_map_p = (reason == LC_ENTER_MACRO);
90 struct line_map *result;
91
92 if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
93 {
94 /* We ran out of allocated line maps. Let's allocate more. */
95
96 line_map_realloc reallocator
97 = set->reallocator ? set->reallocator : xrealloc;
98 LINEMAPS_ALLOCATED (set, macro_map_p) =
99 2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256;
100 LINEMAPS_MAPS (set, macro_map_p)
101 = (struct line_map *) (*reallocator) (LINEMAPS_MAPS (set, macro_map_p),
102 LINEMAPS_ALLOCATED (set,
103 macro_map_p)
104 * sizeof (struct line_map));
105 result =
106 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
107 memset (result, 0,
108 ((LINEMAPS_ALLOCATED (set, macro_map_p)
109 - LINEMAPS_USED (set, macro_map_p))
110 * sizeof (struct line_map)));
111 }
112 else
113 result =
114 &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
115
116 LINEMAPS_USED (set, macro_map_p)++;
117
118 result->reason = reason;
119 return result;
120 }
121
122 /* Add a mapping of logical source line to physical source file and
123 line number.
124
125 The text pointed to by TO_FILE must have a lifetime
126 at least as long as the final call to lookup_line (). An empty
127 TO_FILE means standard input. If reason is LC_LEAVE, and
128 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
129 natural values considering the file we are returning to.
130
131 FROM_LINE should be monotonic increasing across calls to this
132 function. A call to this function can relocate the previous set of
133 maps, so any stored line_map pointers should not be used. */
134
135 const struct line_map *
136 linemap_add (struct line_maps *set, enum lc_reason reason,
137 unsigned int sysp, const char *to_file, linenum_type to_line)
138 {
139 struct line_map *map;
140 source_location start_location = set->highest_location + 1;
141
142 linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
143 && (start_location
144 < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
145
146 /* When we enter the file for the first time reason cannot be
147 LC_RENAME. */
148 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
149
150 /* If we are leaving the main file, return a NULL map. */
151 if (reason == LC_LEAVE
152 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
153 && to_file == NULL)
154 {
155 set->depth--;
156 return NULL;
157 }
158
159 map = new_linemap (set, reason);
160
161 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
162 to_file = "<stdin>";
163
164 if (reason == LC_RENAME_VERBATIM)
165 reason = LC_RENAME;
166
167 if (reason == LC_LEAVE)
168 {
169 /* When we are just leaving an "included" file, and jump to the next
170 location inside the "includer" right after the #include
171 "included", this variable points the map in use right before the
172 #include "included", inside the same "includer" file. */
173 struct line_map *from;
174 bool error;
175
176 if (MAIN_FILE_P (map - 1))
177 {
178 /* So this _should_ means we are leaving the main file --
179 effectively ending the compilation unit. But to_file not
180 being NULL means the caller thinks we are leaving to
181 another file. This is an erroneous behaviour but we'll
182 try to recover from it. Let's pretend we are not leaving
183 the main file. */
184 error = true;
185 reason = LC_RENAME;
186 from = map - 1;
187 }
188 else
189 {
190 /* (MAP - 1) points to the map we are leaving. The
191 map from which (MAP - 1) got included should be the map
192 that comes right before MAP in the same file. */
193 from = INCLUDED_FROM (set, map - 1);
194 error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
195 to_file);
196 }
197
198 /* Depending upon whether we are handling preprocessed input or
199 not, this can be a user error or an ICE. */
200 if (error)
201 fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
202 to_file);
203
204 /* A TO_FILE of NULL is special - we use the natural values. */
205 if (error || to_file == NULL)
206 {
207 to_file = ORDINARY_MAP_FILE_NAME (from);
208 to_line = SOURCE_LINE (from, from[1].start_location);
209 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
210 }
211 }
212
213 linemap_assert (reason != LC_ENTER_MACRO);
214 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map) = sysp;
215 MAP_START_LOCATION (map) = start_location;
216 ORDINARY_MAP_FILE_NAME (map) = to_file;
217 ORDINARY_MAP_STARTING_LINE_NUMBER (map) = to_line;
218 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
219 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = 0;
220 set->highest_location = start_location;
221 set->highest_line = start_location;
222 set->max_column_hint = 0;
223
224 if (reason == LC_ENTER)
225 {
226 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
227 set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
228 set->depth++;
229 if (set->trace_includes)
230 trace_include (set, map);
231 }
232 else if (reason == LC_RENAME)
233 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
234 ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
235 else if (reason == LC_LEAVE)
236 {
237 set->depth--;
238 ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
239 ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
240 }
241
242 return map;
243 }
244
245 /* Returns TRUE if the line table set tracks token locations accross
246 macro expansion, FALSE otherwise. */
247
248 bool
249 linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
250 {
251 return LINEMAPS_MACRO_MAPS (set) != NULL;
252 }
253
254 /* Create a macro map. A macro map encodes source locations of tokens
255 that are part of a macro replacement-list, at a macro expansion
256 point. See the extensive comments of struct line_map and struct
257 line_map_macro, in line-map.h.
258
259 This map shall be created when the macro is expanded. The map
260 encodes the source location of the expansion point of the macro as
261 well as the "original" source location of each token that is part
262 of the macro replacement-list. If a macro is defined but never
263 expanded, it has no macro map. SET is the set of maps the macro
264 map should be part of. MACRO_NODE is the macro which the new macro
265 map should encode source locations for. EXPANSION is the location
266 of the expansion point of MACRO. For function-like macros
267 invocations, it's best to make it point to the closing parenthesis
268 of the macro, rather than the the location of the first character
269 of the macro. NUM_TOKENS is the number of tokens that are part of
270 the replacement-list of MACRO.
271
272 Note that when we run out of the integer space available for source
273 locations, this function returns NULL. In that case, callers of
274 this function cannot encode {line,column} pairs into locations of
275 macro tokens anymore. */
276
277 const struct line_map *
278 linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
279 source_location expansion, unsigned int num_tokens)
280 {
281 struct line_map *map;
282 source_location start_location;
283 line_map_realloc reallocator
284 = set->reallocator ? set->reallocator : xrealloc;
285
286 start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
287
288 if (start_location <= set->highest_line
289 || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
290 /* We ran out of macro map space. */
291 return NULL;
292
293 map = new_linemap (set, LC_ENTER_MACRO);
294
295 MAP_START_LOCATION (map) = start_location;
296 MACRO_MAP_MACRO (map) = macro_node;
297 MACRO_MAP_NUM_MACRO_TOKENS (map) = num_tokens;
298 MACRO_MAP_LOCATIONS (map)
299 = (source_location*) reallocator (NULL,
300 2 * num_tokens
301 * sizeof (source_location));
302 MACRO_MAP_EXPANSION_POINT_LOCATION (map) = expansion;
303 memset (MACRO_MAP_LOCATIONS (map), 0,
304 num_tokens * sizeof (source_location));
305
306 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
307 set->max_column_hint = 0;
308
309 return map;
310 }
311
312 /* Create and return a virtual location for a token that is part of a
313 macro expansion-list at a macro expansion point. See the comment
314 inside struct line_map_macro to see what an expansion-list exactly
315 is.
316
317 A call to this function must come after a call to
318 linemap_enter_macro.
319
320 MAP is the map into which the source location is created. TOKEN_NO
321 is the index of the token in the macro replacement-list, starting
322 at number 0.
323
324 ORIG_LOC is the location of the token outside of this macro
325 expansion. If the token comes originally from the macro
326 definition, it is the locus in the macro definition; otherwise it
327 is a location in the context of the caller of this macro expansion
328 (which is a virtual location or a source location if the caller is
329 itself a macro expansion or not).
330
331 MACRO_DEFINITION_LOC is the location in the macro definition,
332 either of the token itself or of a macro parameter that it
333 replaces. */
334
335 source_location
336 linemap_add_macro_token (const struct line_map *map,
337 unsigned int token_no,
338 source_location orig_loc,
339 source_location orig_parm_replacement_loc)
340 {
341 source_location result;
342
343 linemap_assert (linemap_macro_expansion_map_p (map));
344 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
345
346 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
347 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
348
349 result = MAP_START_LOCATION (map) + token_no;
350 return result;
351 }
352
353 /* Return a source_location for the start (i.e. column==0) of
354 (physical) line TO_LINE in the current source file (as in the
355 most recent linemap_add). MAX_COLUMN_HINT is the highest column
356 number we expect to use in this line (but it does not change
357 the highest_location). */
358
359 source_location
360 linemap_line_start (struct line_maps *set, linenum_type to_line,
361 unsigned int max_column_hint)
362 {
363 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
364 source_location highest = set->highest_location;
365 source_location r;
366 linenum_type last_line =
367 SOURCE_LINE (map, set->highest_line);
368 int line_delta = to_line - last_line;
369 bool add_map = false;
370
371 if (line_delta < 0
372 || (line_delta > 10
373 && line_delta * ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) > 1000)
374 || (max_column_hint >= (1U << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)))
375 || (max_column_hint <= 80
376 && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10))
377 {
378 add_map = true;
379 }
380 else
381 max_column_hint = set->max_column_hint;
382 if (add_map)
383 {
384 int column_bits;
385 if (max_column_hint > 100000 || highest > 0xC0000000)
386 {
387 /* If the column number is ridiculous or we've allocated a huge
388 number of source_locations, give up on column numbers. */
389 max_column_hint = 0;
390 if (highest >0xF0000000)
391 return 0;
392 column_bits = 0;
393 }
394 else
395 {
396 column_bits = 7;
397 while (max_column_hint >= (1U << column_bits))
398 column_bits++;
399 max_column_hint = 1U << column_bits;
400 }
401 /* Allocate the new line_map. However, if the current map only has a
402 single line we can sometimes just increase its column_bits instead. */
403 if (line_delta < 0
404 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
405 || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
406 map = (struct line_map *) linemap_add (set, LC_RENAME,
407 ORDINARY_MAP_IN_SYSTEM_HEADER_P
408 (map),
409 ORDINARY_MAP_FILE_NAME (map),
410 to_line);
411 ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = column_bits;
412 r = (MAP_START_LOCATION (map)
413 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
414 << column_bits));
415 }
416 else
417 r = highest - SOURCE_COLUMN (map, highest)
418 + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
419
420 /* Locations of ordinary tokens are always lower than locations of
421 macro tokens. */
422 if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
423 return 0;
424
425 set->highest_line = r;
426 if (r > set->highest_location)
427 set->highest_location = r;
428 set->max_column_hint = max_column_hint;
429 return r;
430 }
431
432 /* Encode and return a source_location from a column number. The
433 source line considered is the last source line used to call
434 linemap_line_start, i.e, the last source line which a location was
435 encoded from. */
436
437 source_location
438 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
439 {
440 source_location r = set->highest_line;
441
442 linemap_assert
443 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
444
445 if (to_column >= set->max_column_hint)
446 {
447 if (r >= 0xC000000 || to_column > 100000)
448 {
449 /* Running low on source_locations - disable column numbers. */
450 return r;
451 }
452 else
453 {
454 struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
455 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
456 }
457 }
458 r = r + to_column;
459 if (r >= set->highest_location)
460 set->highest_location = r;
461 return r;
462 }
463
464 /* Encode and return a source location from a given line and
465 column. */
466
467 source_location
468 linemap_position_for_line_and_column (struct line_map *map,
469 linenum_type line,
470 unsigned column)
471 {
472 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (map) <= line);
473
474 return (MAP_START_LOCATION (map)
475 + ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
476 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
477 + (column & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1)));
478 }
479
480 /* Given a virtual source location yielded by a map (either an
481 ordinary or a macro map), returns that map. */
482
483 const struct line_map*
484 linemap_lookup (struct line_maps *set, source_location line)
485 {
486 if (linemap_location_from_macro_expansion_p (set, line))
487 return linemap_macro_map_lookup (set, line);
488 return linemap_ordinary_map_lookup (set, line);
489 }
490
491 /* Given a source location yielded by an ordinary map, returns that
492 map. Since the set is built chronologically, the logical lines are
493 monotonic increasing, and so the list is sorted and we can use a
494 binary search. */
495
496 static const struct line_map *
497 linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
498 {
499 unsigned int md, mn, mx;
500 const struct line_map *cached, *result;
501
502 if (set == NULL || line < RESERVED_LOCATION_COUNT)
503 return NULL;
504
505 mn = LINEMAPS_ORDINARY_CACHE (set);
506 mx = LINEMAPS_ORDINARY_USED (set);
507
508 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
509 /* We should get a segfault if no line_maps have been added yet. */
510 if (line >= MAP_START_LOCATION (cached))
511 {
512 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
513 return cached;
514 }
515 else
516 {
517 mx = mn;
518 mn = 0;
519 }
520
521 while (mx - mn > 1)
522 {
523 md = (mn + mx) / 2;
524 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
525 mx = md;
526 else
527 mn = md;
528 }
529
530 LINEMAPS_ORDINARY_CACHE (set) = mn;
531 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
532 linemap_assert (line >= MAP_START_LOCATION (result));
533 return result;
534 }
535
536 /* Given a source location yielded by a macro map, returns that map.
537 Since the set is built chronologically, the logical lines are
538 monotonic decreasing, and so the list is sorted and we can use a
539 binary search. */
540
541 static const struct line_map*
542 linemap_macro_map_lookup (struct line_maps *set, source_location line)
543 {
544 unsigned int md, mn, mx;
545 const struct line_map *cached, *result;
546
547 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
548
549 if (set == NULL)
550 return NULL;
551
552 mn = LINEMAPS_MACRO_CACHE (set);
553 mx = LINEMAPS_MACRO_USED (set);
554 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
555
556 if (line >= MAP_START_LOCATION (cached))
557 {
558 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
559 return cached;
560 mx = mn - 1;
561 mn = 0;
562 }
563
564 do
565 {
566 md = (mx + mn) / 2;
567 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
568 mn = md;
569 else
570 mx = md;
571 } while (mx - mn > 1);
572
573 LINEMAPS_MACRO_CACHE (set) = mx;
574 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
575 linemap_assert (MAP_START_LOCATION (result) <= line);
576
577 return result;
578 }
579
580 /* Return TRUE if MAP encodes locations coming from a macro
581 replacement-list at macro expansion point. */
582
583 bool
584 linemap_macro_expansion_map_p (const struct line_map *map)
585 {
586 if (!map)
587 return false;
588 return (map->reason == LC_ENTER_MACRO);
589 }
590
591 /* If LOCATION is the locus of a token in a replacement-list of a
592 macro expansion return the location of the macro expansion point.
593
594 Read the comments of struct line_map and struct line_map_macro in
595 line-map.h to understand what a macro expansion point is. */
596
597 source_location
598 linemap_macro_map_loc_to_exp_point (const struct line_map *map,
599 source_location location)
600 {
601 unsigned token_no;
602
603 linemap_assert (linemap_macro_expansion_map_p (map)
604 && location >= MAP_START_LOCATION (map));
605
606 /* Make sure LOCATION is correct. */
607 token_no = location - MAP_START_LOCATION (map);
608 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
609
610 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
611 }
612
613 /* If LOCATION is the source location of a token that belongs to a
614 macro replacement-list -- as part of a macro expansion -- then
615 return the location of the token at the definition point of the
616 macro. Otherwise, return LOCATION. SET is the set of maps
617 location come from. ORIGINAL_MAP is an output parm. If non NULL,
618 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
619 returned location comes from. */
620
621 source_location
622 linemap_macro_map_loc_to_def_point (const struct line_map *map,
623 source_location location)
624 {
625 unsigned token_no;
626
627 linemap_assert (linemap_macro_expansion_map_p (map)
628 && location >= MAP_START_LOCATION (map));
629 linemap_assert (location >= RESERVED_LOCATION_COUNT);
630
631 token_no = location - MAP_START_LOCATION (map);
632 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
633
634 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
635
636 return location;
637 }
638
639 /* If LOCATION is the locus of a token that is an argument of a
640 function-like macro M and appears in the expansion of M, return the
641 locus of that argument in the context of the caller of M.
642
643 In other words, this returns the xI location presented in the
644 comments of line_map_macro above. */
645 source_location
646 linemap_macro_map_loc_unwind_toward_spelling (const struct line_map* map,
647 source_location location)
648 {
649 unsigned token_no;
650
651 linemap_assert (linemap_macro_expansion_map_p (map)
652 && location >= MAP_START_LOCATION (map));
653 linemap_assert (location >= RESERVED_LOCATION_COUNT);
654
655 token_no = location - MAP_START_LOCATION (map);
656 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
657
658 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
659
660 return location;
661 }
662
663 /* Return the source line number corresponding to source location
664 LOCATION. SET is the line map set LOCATION comes from. If
665 LOCATION is the source location of token that is part of the
666 replacement-list of a macro expansion return the line number of the
667 macro expansion point. */
668
669 int
670 linemap_get_expansion_line (struct line_maps *set,
671 source_location location)
672 {
673 const struct line_map *map = NULL;
674
675 if (location < RESERVED_LOCATION_COUNT)
676 return 0;
677
678 location =
679 linemap_macro_loc_to_exp_point (set, location, &map);
680
681 return SOURCE_LINE (map, location);
682 }
683
684 /* Return the path of the file corresponding to source code location
685 LOCATION.
686
687 If LOCATION is the source location of token that is part of the
688 replacement-list of a macro expansion return the file path of the
689 macro expansion point.
690
691 SET is the line map set LOCATION comes from. */
692
693 const char*
694 linemap_get_expansion_filename (struct line_maps *set,
695 source_location location)
696 {
697 const struct line_map *map = NULL;
698
699 if (location < RESERVED_LOCATION_COUNT)
700 return NULL;
701
702 location =
703 linemap_macro_loc_to_exp_point (set, location, &map);
704
705 return LINEMAP_FILE (map);
706 }
707
708 /* Return the name of the macro associated to MACRO_MAP. */
709
710 const char*
711 linemap_map_get_macro_name (const struct line_map* macro_map)
712 {
713 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
714 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
715 }
716
717 /* Return a positive value if LOCATION is the locus of a token that is
718 located in a system header, O otherwise. It returns 1 if LOCATION
719 is the locus of a token that is located in a system header, and 2
720 if LOCATION is the locus of a token located in a C system header
721 that therefore needs to be extern "C" protected in C++.
722
723 Note that this function returns 1 if LOCATION belongs to a token
724 that is part of a macro replacement-list defined in a system
725 header, but expanded in a non-system file. */
726
727 int
728 linemap_location_in_system_header_p (struct line_maps *set,
729 source_location location)
730 {
731 const struct line_map *map = NULL;
732
733 if (location < RESERVED_LOCATION_COUNT)
734 return false;
735
736 location =
737 linemap_resolve_location (set, location, LRK_SPELLING_LOCATION, &map);
738
739 return LINEMAP_SYSP (map);
740 }
741
742 /* Return TRUE if LOCATION is a source code location of a token coming
743 from a macro replacement-list at a macro expansion point, FALSE
744 otherwise. */
745
746 bool
747 linemap_location_from_macro_expansion_p (struct line_maps *set,
748 source_location location)
749 {
750 linemap_assert (location <= MAX_SOURCE_LOCATION
751 && (set->highest_location
752 < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
753 if (set == NULL)
754 return false;
755 return (location > set->highest_location);
756 }
757
758 /* Given two virtual locations *LOC0 and *LOC1, return the first
759 common macro map in their macro expansion histories. Return NULL
760 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
761 virtual location of the token inside the resulting macro. */
762
763 static const struct line_map*
764 first_map_in_common_1 (struct line_maps *set,
765 source_location *loc0,
766 source_location *loc1)
767 {
768 source_location l0 = *loc0, l1 = *loc1;
769 const struct line_map *map0 = linemap_lookup (set, l0),
770 *map1 = linemap_lookup (set, l1);
771
772 while (linemap_macro_expansion_map_p (map0)
773 && linemap_macro_expansion_map_p (map1)
774 && (map0 != map1))
775 {
776 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
777 {
778 l0 = linemap_macro_map_loc_to_exp_point (map0, l0);
779 map0 = linemap_lookup (set, l0);
780 }
781 else
782 {
783 l1 = linemap_macro_map_loc_to_exp_point (map1, l1);
784 map1 = linemap_lookup (set, l1);
785 }
786 }
787
788 if (map0 == map1)
789 {
790 *loc0 = l0;
791 *loc1 = l1;
792 return map0;
793 }
794 return NULL;
795 }
796
797 /* Given two virtual locations LOC0 and LOC1, return the first common
798 macro map in their macro expansion histories. Return NULL if no
799 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
800 virtual location of the token inside the resulting macro, upon
801 return of a non-NULL result. */
802
803 static const struct line_map*
804 first_map_in_common (struct line_maps *set,
805 source_location loc0,
806 source_location loc1,
807 source_location *res_loc0,
808 source_location *res_loc1)
809 {
810 *res_loc0 = loc0;
811 *res_loc1 = loc1;
812
813 return first_map_in_common_1 (set, res_loc0, res_loc1);
814 }
815
816 /* Return a positive value if PRE denotes the location of a token that
817 comes before the token of POST, 0 if PRE denotes the location of
818 the same token as the token for POST, and a negative value
819 otherwise. */
820
821 int
822 linemap_compare_locations (struct line_maps *set,
823 source_location pre,
824 source_location post)
825 {
826 bool pre_virtual_p, post_virtual_p;
827 source_location l0 = pre, l1 = post;
828
829 if (l0 == l1)
830 return 0;
831
832 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
833 l0 = linemap_resolve_location (set, l0,
834 LRK_MACRO_EXPANSION_POINT,
835 NULL);
836
837 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
838 l1 = linemap_resolve_location (set, l1,
839 LRK_MACRO_EXPANSION_POINT,
840 NULL);
841
842 if (l0 == l1
843 && pre_virtual_p
844 && post_virtual_p)
845 {
846 /* So pre and post represent two tokens that are present in a
847 same macro expansion. Let's see if the token for pre was
848 before the token for post in that expansion. */
849 unsigned i0, i1;
850 const struct line_map *map =
851 first_map_in_common (set, pre, post, &l0, &l1);
852
853 if (map == NULL)
854 /* This should not be possible. */
855 abort ();
856
857 i0 = l0 - MAP_START_LOCATION (map);
858 i1 = l1 - MAP_START_LOCATION (map);
859 return i1 - i0;
860 }
861
862 return l1 - l0;
863 }
864
865 /* Print an include trace, for e.g. the -H option of the preprocessor. */
866
867 static void
868 trace_include (const struct line_maps *set, const struct line_map *map)
869 {
870 unsigned int i = set->depth;
871
872 while (--i)
873 putc ('.', stderr);
874
875 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
876 }
877
878 /* Return the spelling location of the token wherever it comes from,
879 whether part of a macro definition or not.
880
881 This is a subroutine for linemap_resolve_location. */
882
883 static source_location
884 linemap_macro_loc_to_spelling_point (struct line_maps *set,
885 source_location location,
886 const struct line_map **original_map)
887 {
888 struct line_map *map;
889
890 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
891
892 while (true)
893 {
894 map = (struct line_map*) linemap_lookup (set, location);
895 if (!linemap_macro_expansion_map_p (map))
896 break;
897
898 location =
899 linemap_macro_map_loc_unwind_toward_spelling (map, location);
900 }
901
902 if (original_map)
903 *original_map = map;
904 return location;
905 }
906
907 /* If LOCATION is the source location of a token that belongs to a
908 macro replacement-list -- as part of a macro expansion -- then
909 return the location of the token at the definition point of the
910 macro. Otherwise, return LOCATION. SET is the set of maps
911 location come from. ORIGINAL_MAP is an output parm. If non NULL,
912 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
913 returned location comes from.
914
915 This is a subroutine of linemap_resolve_location. */
916
917 static source_location
918 linemap_macro_loc_to_def_point (struct line_maps *set,
919 source_location location,
920 const struct line_map **original_map)
921 {
922 struct line_map *map;
923
924 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
925
926 while (true)
927 {
928 map = (struct line_map*) linemap_lookup (set, location);
929 if (!linemap_macro_expansion_map_p (map))
930 break;
931
932 location =
933 linemap_macro_map_loc_to_def_point (map, location);
934 }
935
936 if (original_map)
937 *original_map = map;
938 return location;
939 }
940
941 /* If LOCATION is the source location of a token that belongs to a
942 macro replacement-list -- at a macro expansion point -- then return
943 the location of the topmost expansion point of the macro. We say
944 topmost because if we are in the context of a nested macro
945 expansion, the function returns the source location of the first
946 macro expansion that triggered the nested expansions.
947
948 Otherwise, return LOCATION. SET is the set of maps location come
949 from. ORIGINAL_MAP is an output parm. If non NULL, the function
950 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
951 location comes from.
952
953 This is a subroutine of linemap_resolve_location. */
954
955 static source_location
956 linemap_macro_loc_to_exp_point (struct line_maps *set,
957 source_location location,
958 const struct line_map **original_map)
959 {
960 struct line_map *map;
961
962 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
963
964 while (true)
965 {
966 map = (struct line_map*) linemap_lookup (set, location);
967 if (!linemap_macro_expansion_map_p (map))
968 break;
969 location = linemap_macro_map_loc_to_exp_point (map, location);
970 }
971
972 if (original_map)
973 *original_map = map;
974 return location;
975 }
976
977 /* Resolve a virtual location into either a spelling location, an
978 expansion point location or a token argument replacement point
979 location. Return the map that encodes the virtual location as well
980 as the resolved location.
981
982 If LOC is *NOT* the location of a token resulting from the
983 expansion of a macro, then the parameter LRK (which stands for
984 Location Resolution Kind) is ignored and the resulting location
985 just equals the one given in argument.
986
987 Now if LOC *IS* the location of a token resulting from the
988 expansion of a macro, this is what happens.
989
990 * If LRK is set to LRK_MACRO_EXPANSION_POINT
991 -------------------------------
992
993 The virtual location is resolved to the location to the locus of
994 the expansion point of the macro.
995
996 * If LRK is set to LRK_SPELLING_LOCATION
997 -------------------------------------
998
999 The virtual location is resolved to the location to the locus where
1000 the token has been spelled in the source. This can follow through
1001 all the macro expansions that led to the token.
1002
1003 * If LRK is set to LRK_MACRO_PARM_REPLACEMENT_POINT
1004 --------------------------------------
1005
1006 If LOC is the locus of a token that is an argument of a
1007 function-like macro [replacing a parameter in the replacement list
1008 of the macro] the virtual location is resolved to the locus of the
1009 parameter that is replaced, in the context of the definition of the
1010 macro.
1011
1012 If LOC is the locus of a token that is not an argument of a
1013 function-like macro, then the function behaves as if LRK was set to
1014 LRK_SPELLING_LOCATION.
1015
1016 If MAP is non-NULL, *MAP is set to the map of the resolved
1017 location. */
1018
1019 source_location
1020 linemap_resolve_location (struct line_maps *set,
1021 source_location loc,
1022 enum location_resolution_kind lrk,
1023 const struct line_map **map)
1024 {
1025 linemap_assert (set && loc >= RESERVED_LOCATION_COUNT);
1026
1027 switch (lrk)
1028 {
1029 case LRK_MACRO_EXPANSION_POINT:
1030 loc = linemap_macro_loc_to_exp_point (set, loc, map);
1031 break;
1032 case LRK_SPELLING_LOCATION:
1033 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
1034 break;
1035 case LRK_MACRO_DEFINITION_LOCATION:
1036 loc = linemap_macro_loc_to_def_point (set, loc, map);
1037 break;
1038 default:
1039 abort ();
1040 }
1041 return loc;
1042 }
1043
1044 /*
1045 Suppose that LOC is the virtual location of a token T coming from
1046 the expansion of a macro M. This function then steps up to get the
1047 location L of the point where M got expanded. If L is a spelling
1048 location inside a macro expansion M', then this function returns
1049 the locus of the point where M' was expanded. Said otherwise, this
1050 function returns the location of T in the context that triggered
1051 the expansion of M.
1052
1053 *LOC_MAP must be set to the map of LOC. This function then sets it
1054 to the map of the returned location. */
1055
1056 source_location
1057 linemap_unwind_toward_expansion (struct line_maps *set,
1058 source_location loc,
1059 const struct line_map **map)
1060 {
1061 source_location resolved_location;
1062 const struct line_map *resolved_map;
1063
1064 resolved_location =
1065 linemap_macro_map_loc_unwind_toward_spelling (*map, loc);
1066 resolved_map = linemap_lookup (set, resolved_location);
1067
1068 if (!linemap_macro_expansion_map_p (resolved_map))
1069 {
1070 resolved_location = linemap_macro_map_loc_to_exp_point (*map, loc);
1071 resolved_map = linemap_lookup (set, resolved_location);
1072 }
1073
1074 *map = resolved_map;
1075 return resolved_location;
1076 }
1077
1078 /* Expand source code location LOC and return a user readable source
1079 code location. LOC must be a spelling (non-virtual) location. */
1080
1081 expanded_location
1082 linemap_expand_location (const struct line_map *map,
1083 source_location loc)
1084
1085 {
1086 expanded_location xloc;
1087
1088 xloc.file = LINEMAP_FILE (map);
1089 xloc.line = SOURCE_LINE (map, loc);
1090 xloc.column = SOURCE_COLUMN (map, loc);
1091 xloc.sysp = LINEMAP_SYSP (map) != 0;
1092
1093 return xloc;
1094 }
1095
1096 /* Expand source code location LOC and return a user readable source
1097 code location. LOC can be a virtual location. The LRK parameter
1098 is the same as for linemap_resolve_location. */
1099
1100 expanded_location
1101 linemap_expand_location_full (struct line_maps *set,
1102 source_location loc,
1103 enum location_resolution_kind lrk)
1104 {
1105 const struct line_map *map;
1106 expanded_location xloc;
1107
1108 loc = linemap_resolve_location (set, loc, lrk, &map);
1109 xloc = linemap_expand_location (map, loc);
1110 return xloc;
1111 }
1112
1113 /* Dump debugging information about source location LOC into the file
1114 stream STREAM. SET is the line map set LOC comes from. */
1115
1116 void
1117 linemap_dump_location (struct line_maps *set,
1118 source_location loc,
1119 FILE *stream)
1120 {
1121 const struct line_map *map;
1122 source_location location;
1123 const char *path, *from;
1124 int l,c,s,e;
1125
1126 if (loc == 0)
1127 return;
1128
1129 location =
1130 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
1131 path = LINEMAP_FILE (map);
1132
1133 l = SOURCE_LINE (map, location);
1134 c = SOURCE_COLUMN (map, location);
1135 s = LINEMAP_SYSP (map) != 0;
1136 e = location != loc;
1137
1138 if (e)
1139 from = "N/A";
1140 else
1141 from = (INCLUDED_FROM (set, map))
1142 ? LINEMAP_FILE (INCLUDED_FROM (set, map))
1143 : "<NULL>";
1144
1145 /* P: path, L: line, C: column, S: in-system-header, M: map address,
1146 E: macro expansion?. */
1147 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d}",
1148 path, from, l, c, s, (void*)map, e, loc);
1149 }
1150
1151 /* Compute and return statistics about the memory consumption of some
1152 parts of the line table SET. */
1153
1154 void
1155 linemap_get_statistics (struct line_maps *set,
1156 struct linemap_stats *s)
1157 {
1158 size_t ordinary_maps_allocated_size, ordinary_maps_used_size,
1159 macro_maps_allocated_size, macro_maps_used_size,
1160 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
1161
1162 struct line_map *cur_map;
1163
1164 ordinary_maps_allocated_size =
1165 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map);
1166
1167 ordinary_maps_used_size =
1168 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map);
1169
1170 macro_maps_allocated_size =
1171 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map);
1172
1173 for (cur_map = LINEMAPS_MACRO_MAPS (set);
1174 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
1175 ++cur_map)
1176 {
1177 unsigned i;
1178
1179 linemap_assert (linemap_macro_expansion_map_p (cur_map));
1180
1181 macro_maps_locations_size +=
1182 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
1183
1184 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
1185 {
1186 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
1187 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
1188 duplicated_macro_maps_locations_size +=
1189 sizeof (source_location);
1190 }
1191 }
1192
1193 macro_maps_used_size =
1194 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map);
1195
1196 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
1197 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
1198 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
1199 s->ordinary_maps_used_size = ordinary_maps_used_size;
1200 s->num_expanded_macros = num_expanded_macros_counter;
1201 s->num_macro_tokens = num_macro_tokens_counter;
1202 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
1203 s->macro_maps_allocated_size = macro_maps_allocated_size;
1204 s->macro_maps_locations_size = macro_maps_locations_size;
1205 s->macro_maps_used_size = macro_maps_used_size;
1206 s->duplicated_macro_maps_locations_size =
1207 duplicated_macro_maps_locations_size;
1208 }
This page took 0.094567 seconds and 6 git commands to generate.