clang-format: Enhance list of FOR_EACH macros
[official-gcc.git] / gcc / input.c
blobce84f10037108a718595ab9c23c861d8973aa9dd
1 /* Data and functions related to line maps and input files.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "intl.h"
24 #include "diagnostic-core.h"
26 /* This is a cache used by get_next_line to store the content of a
27 file to be searched for file lines. */
28 struct fcache
30 /* These are information used to store a line boundary. */
31 struct line_info
33 /* The line number. It starts from 1. */
34 size_t line_num;
36 /* The position (byte count) of the beginning of the line,
37 relative to the file data pointer. This starts at zero. */
38 size_t start_pos;
40 /* The position (byte count) of the last byte of the line. This
41 normally points to the '\n' character, or to one byte after the
42 last byte of the file, if the file doesn't contain a '\n'
43 character. */
44 size_t end_pos;
46 line_info (size_t l, size_t s, size_t e)
47 : line_num (l), start_pos (s), end_pos (e)
50 line_info ()
51 :line_num (0), start_pos (0), end_pos (0)
55 /* The number of time this file has been accessed. This is used
56 to designate which file cache to evict from the cache
57 array. */
58 unsigned use_count;
60 const char *file_path;
62 FILE *fp;
64 /* This points to the content of the file that we've read so
65 far. */
66 char *data;
68 /* The size of the DATA array above.*/
69 size_t size;
71 /* The number of bytes read from the underlying file so far. This
72 must be less (or equal) than SIZE above. */
73 size_t nb_read;
75 /* The index of the beginning of the current line. */
76 size_t line_start_idx;
78 /* The number of the previous line read. This starts at 1. Zero
79 means we've read no line so far. */
80 size_t line_num;
82 /* This is the total number of lines of the current file. At the
83 moment, we try to get this information from the line map
84 subsystem. Note that this is just a hint. When using the C++
85 front-end, this hint is correct because the input file is then
86 completely tokenized before parsing starts; so the line map knows
87 the number of lines before compilation really starts. For e.g,
88 the C front-end, it can happen that we start emitting diagnostics
89 before the line map has seen the end of the file. */
90 size_t total_lines;
92 /* This is a record of the beginning and end of the lines we've seen
93 while reading the file. This is useful to avoid walking the data
94 from the beginning when we are asked to read a line that is
95 before LINE_START_IDX above. Note that the maximum size of this
96 record is fcache_line_record_size, so that the memory consumption
97 doesn't explode. We thus scale total_lines down to
98 fcache_line_record_size. */
99 vec<line_info, va_heap> line_record;
101 fcache ();
102 ~fcache ();
105 /* Current position in real source file. */
107 location_t input_location = UNKNOWN_LOCATION;
109 struct line_maps *line_table;
111 static fcache *fcache_tab;
112 static const size_t fcache_tab_size = 16;
113 static const size_t fcache_buffer_size = 4 * 1024;
114 static const size_t fcache_line_record_size = 100;
116 /* Expand the source location LOC into a human readable location. If
117 LOC resolves to a builtin location, the file name of the readable
118 location is set to the string "<built-in>". If EXPANSION_POINT_P is
119 TRUE and LOC is virtual, then it is resolved to the expansion
120 point of the involved macro. Otherwise, it is resolved to the
121 spelling location of the token.
123 When resolving to the spelling location of the token, if the
124 resulting location is for a built-in location (that is, it has no
125 associated line/column) in the context of a macro expansion, the
126 returned location is the first one (while unwinding the macro
127 location towards its expansion point) that is in real source
128 code. */
130 static expanded_location
131 expand_location_1 (source_location loc,
132 bool expansion_point_p)
134 expanded_location xloc;
135 const line_map_ordinary *map;
136 enum location_resolution_kind lrk = LRK_MACRO_EXPANSION_POINT;
137 tree block = NULL;
139 if (IS_ADHOC_LOC (loc))
141 block = LOCATION_BLOCK (loc);
142 loc = LOCATION_LOCUS (loc);
145 memset (&xloc, 0, sizeof (xloc));
147 if (loc >= RESERVED_LOCATION_COUNT)
149 if (!expansion_point_p)
151 /* We want to resolve LOC to its spelling location.
153 But if that spelling location is a reserved location that
154 appears in the context of a macro expansion (like for a
155 location for a built-in token), let's consider the first
156 location (toward the expansion point) that is not reserved;
157 that is, the first location that is in real source code. */
158 loc = linemap_unwind_to_first_non_reserved_loc (line_table,
159 loc, NULL);
160 lrk = LRK_SPELLING_LOCATION;
162 loc = linemap_resolve_location (line_table, loc,
163 lrk, &map);
164 xloc = linemap_expand_location (line_table, map, loc);
167 xloc.data = block;
168 if (loc <= BUILTINS_LOCATION)
169 xloc.file = loc == UNKNOWN_LOCATION ? NULL : _("<built-in>");
171 return xloc;
174 /* Initialize the set of cache used for files accessed by caret
175 diagnostic. */
177 static void
178 diagnostic_file_cache_init (void)
180 if (fcache_tab == NULL)
181 fcache_tab = new fcache[fcache_tab_size];
184 /* Free the resources used by the set of cache used for files accessed
185 by caret diagnostic. */
187 void
188 diagnostic_file_cache_fini (void)
190 if (fcache_tab)
192 delete [] (fcache_tab);
193 fcache_tab = NULL;
197 /* Return the total lines number that have been read so far by the
198 line map (in the preprocessor) so far. For languages like C++ that
199 entirely preprocess the input file before starting to parse, this
200 equals the actual number of lines of the file. */
202 static size_t
203 total_lines_num (const char *file_path)
205 size_t r = 0;
206 source_location l = 0;
207 if (linemap_get_file_highest_location (line_table, file_path, &l))
209 gcc_assert (l >= RESERVED_LOCATION_COUNT);
210 expanded_location xloc = expand_location (l);
211 r = xloc.line;
213 return r;
216 /* Lookup the cache used for the content of a given file accessed by
217 caret diagnostic. Return the found cached file, or NULL if no
218 cached file was found. */
220 static fcache*
221 lookup_file_in_cache_tab (const char *file_path)
223 if (file_path == NULL)
224 return NULL;
226 diagnostic_file_cache_init ();
228 /* This will contain the found cached file. */
229 fcache *r = NULL;
230 for (unsigned i = 0; i < fcache_tab_size; ++i)
232 fcache *c = &fcache_tab[i];
233 if (c->file_path && !strcmp (c->file_path, file_path))
235 ++c->use_count;
236 r = c;
240 if (r)
241 ++r->use_count;
243 return r;
246 /* Return the file cache that has been less used, recently, or the
247 first empty one. If HIGHEST_USE_COUNT is non-null,
248 *HIGHEST_USE_COUNT is set to the highest use count of the entries
249 in the cache table. */
251 static fcache*
252 evicted_cache_tab_entry (unsigned *highest_use_count)
254 diagnostic_file_cache_init ();
256 fcache *to_evict = &fcache_tab[0];
257 unsigned huc = to_evict->use_count;
258 for (unsigned i = 1; i < fcache_tab_size; ++i)
260 fcache *c = &fcache_tab[i];
261 bool c_is_empty = (c->file_path == NULL);
263 if (c->use_count < to_evict->use_count
264 || (to_evict->file_path && c_is_empty))
265 /* We evict C because it's either an entry with a lower use
266 count or one that is empty. */
267 to_evict = c;
269 if (huc < c->use_count)
270 huc = c->use_count;
272 if (c_is_empty)
273 /* We've reached the end of the cache; subsequent elements are
274 all empty. */
275 break;
278 if (highest_use_count)
279 *highest_use_count = huc;
281 return to_evict;
284 /* Create the cache used for the content of a given file to be
285 accessed by caret diagnostic. This cache is added to an array of
286 cache and can be retrieved by lookup_file_in_cache_tab. This
287 function returns the created cache. Note that only the last
288 fcache_tab_size files are cached. */
290 static fcache*
291 add_file_to_cache_tab (const char *file_path)
294 FILE *fp = fopen (file_path, "r");
295 if (fp == NULL)
296 return NULL;
298 unsigned highest_use_count = 0;
299 fcache *r = evicted_cache_tab_entry (&highest_use_count);
300 r->file_path = file_path;
301 if (r->fp)
302 fclose (r->fp);
303 r->fp = fp;
304 r->nb_read = 0;
305 r->line_start_idx = 0;
306 r->line_num = 0;
307 r->line_record.truncate (0);
308 /* Ensure that this cache entry doesn't get evicted next time
309 add_file_to_cache_tab is called. */
310 r->use_count = ++highest_use_count;
311 r->total_lines = total_lines_num (file_path);
313 return r;
316 /* Lookup the cache used for the content of a given file accessed by
317 caret diagnostic. If no cached file was found, create a new cache
318 for this file, add it to the array of cached file and return
319 it. */
321 static fcache*
322 lookup_or_add_file_to_cache_tab (const char *file_path)
324 fcache *r = lookup_file_in_cache_tab (file_path);
325 if (r == NULL)
326 r = add_file_to_cache_tab (file_path);
327 return r;
330 /* Default constructor for a cache of file used by caret
331 diagnostic. */
333 fcache::fcache ()
334 : use_count (0), file_path (NULL), fp (NULL), data (0),
335 size (0), nb_read (0), line_start_idx (0), line_num (0),
336 total_lines (0)
338 line_record.create (0);
341 /* Destructor for a cache of file used by caret diagnostic. */
343 fcache::~fcache ()
345 if (fp)
347 fclose (fp);
348 fp = NULL;
350 if (data)
352 XDELETEVEC (data);
353 data = 0;
355 line_record.release ();
358 /* Returns TRUE iff the cache would need to be filled with data coming
359 from the file. That is, either the cache is empty or full or the
360 current line is empty. Note that if the cache is full, it would
361 need to be extended and filled again. */
363 static bool
364 needs_read (fcache *c)
366 return (c->nb_read == 0
367 || c->nb_read == c->size
368 || (c->line_start_idx >= c->nb_read - 1));
371 /* Return TRUE iff the cache is full and thus needs to be
372 extended. */
374 static bool
375 needs_grow (fcache *c)
377 return c->nb_read == c->size;
380 /* Grow the cache if it needs to be extended. */
382 static void
383 maybe_grow (fcache *c)
385 if (!needs_grow (c))
386 return;
388 size_t size = c->size == 0 ? fcache_buffer_size : c->size * 2;
389 c->data = XRESIZEVEC (char, c->data, size + 1);
390 c->size = size;
393 /* Read more data into the cache. Extends the cache if need be.
394 Returns TRUE iff new data could be read. */
396 static bool
397 read_data (fcache *c)
399 if (feof (c->fp) || ferror (c->fp))
400 return false;
402 maybe_grow (c);
404 char * from = c->data + c->nb_read;
405 size_t to_read = c->size - c->nb_read;
406 size_t nb_read = fread (from, 1, to_read, c->fp);
408 if (ferror (c->fp))
409 return false;
411 c->nb_read += nb_read;
412 return !!nb_read;
415 /* Read new data iff the cache needs to be filled with more data
416 coming from the file FP. Return TRUE iff the cache was filled with
417 mode data. */
419 static bool
420 maybe_read_data (fcache *c)
422 if (!needs_read (c))
423 return false;
424 return read_data (c);
427 /* Read a new line from file FP, using C as a cache for the data
428 coming from the file. Upon successful completion, *LINE is set to
429 the beginning of the line found. Space for that line has been
430 allocated in the cache thus *LINE has the same life time as C.
431 *LINE_LEN is set to the length of the line. Note that the line
432 does not contain any terminal delimiter. This function returns
433 true if some data was read or process from the cache, false
434 otherwise. Note that subsequent calls to get_next_line return the
435 next lines of the file and might overwrite the content of
436 *LINE. */
438 static bool
439 get_next_line (fcache *c, char **line, ssize_t *line_len)
441 /* Fill the cache with data to process. */
442 maybe_read_data (c);
444 size_t remaining_size = c->nb_read - c->line_start_idx;
445 if (remaining_size == 0)
446 /* There is no more data to process. */
447 return false;
449 char *line_start = c->data + c->line_start_idx;
451 char *next_line_start = NULL;
452 size_t len = 0;
453 char *line_end = (char *) memchr (line_start, '\n', remaining_size);
454 if (line_end == NULL)
456 /* We haven't found the end-of-line delimiter in the cache.
457 Fill the cache with more data from the file and look for the
458 '\n'. */
459 while (maybe_read_data (c))
461 line_start = c->data + c->line_start_idx;
462 remaining_size = c->nb_read - c->line_start_idx;
463 line_end = (char *) memchr (line_start, '\n', remaining_size);
464 if (line_end != NULL)
466 next_line_start = line_end + 1;
467 break;
470 if (line_end == NULL)
471 /* We've loadded all the file into the cache and still no
472 '\n'. Let's say the line ends up at one byte passed the
473 end of the file. This is to stay consistent with the case
474 of when the line ends up with a '\n' and line_end points to
475 that terminal '\n'. That consistency is useful below in
476 the len calculation. */
477 line_end = c->data + c->nb_read ;
479 else
480 next_line_start = line_end + 1;
482 if (ferror (c->fp))
483 return -1;
485 /* At this point, we've found the end of the of line. It either
486 points to the '\n' or to one byte after the last byte of the
487 file. */
488 gcc_assert (line_end != NULL);
490 len = line_end - line_start;
492 if (c->line_start_idx < c->nb_read)
493 *line = line_start;
495 ++c->line_num;
497 /* Before we update our line record, make sure the hint about the
498 total number of lines of the file is correct. If it's not, then
499 we give up recording line boundaries from now on. */
500 bool update_line_record = true;
501 if (c->line_num > c->total_lines)
502 update_line_record = false;
504 /* Now update our line record so that re-reading lines from the
505 before c->line_start_idx is faster. */
506 if (update_line_record
507 && c->line_record.length () < fcache_line_record_size)
509 /* If the file lines fits in the line record, we just record all
510 its lines ...*/
511 if (c->total_lines <= fcache_line_record_size
512 && c->line_num > c->line_record.length ())
513 c->line_record.safe_push (fcache::line_info (c->line_num,
514 c->line_start_idx,
515 line_end - c->data));
516 else if (c->total_lines > fcache_line_record_size)
518 /* ... otherwise, we just scale total_lines down to
519 (fcache_line_record_size lines. */
520 size_t n = (c->line_num * fcache_line_record_size) / c->total_lines;
521 if (c->line_record.length () == 0
522 || n >= c->line_record.length ())
523 c->line_record.safe_push (fcache::line_info (c->line_num,
524 c->line_start_idx,
525 line_end - c->data));
529 /* Update c->line_start_idx so that it points to the next line to be
530 read. */
531 if (next_line_start)
532 c->line_start_idx = next_line_start - c->data;
533 else
534 /* We didn't find any terminal '\n'. Let's consider that the end
535 of line is the end of the data in the cache. The next
536 invocation of get_next_line will either read more data from the
537 underlying file or return false early because we've reached the
538 end of the file. */
539 c->line_start_idx = c->nb_read;
541 *line_len = len;
543 return true;
546 /* Reads the next line from FILE into *LINE. If *LINE is too small
547 (or NULL) it is allocated (or extended) to have enough space to
548 containe the line. *LINE_LENGTH must contain the size of the
549 initial*LINE buffer. It's then updated by this function to the
550 actual length of the returned line. Note that the returned line
551 can contain several zero bytes. Also note that the returned string
552 is allocated in static storage that is going to be re-used by
553 subsequent invocations of read_line. */
555 static bool
556 read_next_line (fcache *cache, char ** line, ssize_t *line_len)
558 char *l = NULL;
559 ssize_t len = 0;
561 if (!get_next_line (cache, &l, &len))
562 return false;
564 if (*line == NULL)
565 *line = XNEWVEC (char, len);
566 else
567 if (*line_len < len)
568 *line = XRESIZEVEC (char, *line, len);
570 memcpy (*line, l, len);
571 *line_len = len;
573 return true;
576 /* Consume the next bytes coming from the cache (or from its
577 underlying file if there are remaining unread bytes in the file)
578 until we reach the next end-of-line (or end-of-file). There is no
579 copying from the cache involved. Return TRUE upon successful
580 completion. */
582 static bool
583 goto_next_line (fcache *cache)
585 char *l;
586 ssize_t len;
588 return get_next_line (cache, &l, &len);
591 /* Read an arbitrary line number LINE_NUM from the file cached in C.
592 The line is copied into *LINE. *LINE_LEN must have been set to the
593 length of *LINE. If *LINE is too small (or NULL) it's extended (or
594 allocated) and *LINE_LEN is adjusted accordingly. *LINE ends up
595 with a terminal zero byte and can contain additional zero bytes.
596 This function returns bool if a line was read. */
598 static bool
599 read_line_num (fcache *c, size_t line_num,
600 char ** line, ssize_t *line_len)
602 gcc_assert (line_num > 0);
604 if (line_num <= c->line_num)
606 /* We've been asked to read lines that are before c->line_num.
607 So lets use our line record (if it's not empty) to try to
608 avoid re-reading the file from the beginning again. */
610 if (c->line_record.is_empty ())
612 c->line_start_idx = 0;
613 c->line_num = 0;
615 else
617 fcache::line_info *i = NULL;
618 if (c->total_lines <= fcache_line_record_size)
620 /* In languages where the input file is not totally
621 preprocessed up front, the c->total_lines hint
622 can be smaller than the number of lines of the
623 file. In that case, only the first
624 c->total_lines have been recorded.
626 Otherwise, the first c->total_lines we've read have
627 their start/end recorded here. */
628 i = (line_num <= c->total_lines)
629 ? &c->line_record[line_num - 1]
630 : &c->line_record[c->total_lines - 1];
631 gcc_assert (i->line_num <= line_num);
633 else
635 /* So the file had more lines than our line record
636 size. Thus the number of lines we've recorded has
637 been scaled down to fcache_line_reacord_size. Let's
638 pick the start/end of the recorded line that is
639 closest to line_num. */
640 size_t n = (line_num <= c->total_lines)
641 ? line_num * fcache_line_record_size / c->total_lines
642 : c ->line_record.length () - 1;
643 if (n < c->line_record.length ())
645 i = &c->line_record[n];
646 gcc_assert (i->line_num <= line_num);
650 if (i && i->line_num == line_num)
652 /* We have the start/end of the line. Let's just copy
653 it again and we are done. */
654 ssize_t len = i->end_pos - i->start_pos + 1;
655 if (*line_len < len)
656 *line = XRESIZEVEC (char, *line, len);
657 memmove (*line, c->data + i->start_pos, len);
658 (*line)[len - 1] = '\0';
659 *line_len = --len;
660 return true;
663 if (i)
665 c->line_start_idx = i->start_pos;
666 c->line_num = i->line_num - 1;
668 else
670 c->line_start_idx = 0;
671 c->line_num = 0;
676 /* Let's walk from line c->line_num up to line_num - 1, without
677 copying any line. */
678 while (c->line_num < line_num - 1)
679 if (!goto_next_line (c))
680 return false;
682 /* The line we want is the next one. Let's read and copy it back to
683 the caller. */
684 return read_next_line (c, line, line_len);
687 /* Return the physical source line that corresponds to FILE_PATH/LINE in a
688 buffer that is statically allocated. The newline is replaced by
689 the null character. Note that the line can contain several null
690 characters, so LINE_LEN, if non-null, points to the actual length
691 of the line. */
693 const char *
694 location_get_source_line (const char *file_path, int line,
695 int *line_len)
697 static char *buffer;
698 static ssize_t len;
700 if (line == 0)
701 return NULL;
703 fcache *c = lookup_or_add_file_to_cache_tab (file_path);
704 if (c == NULL)
705 return NULL;
707 bool read = read_line_num (c, line, &buffer, &len);
709 if (read && line_len)
710 *line_len = len;
712 return read ? buffer : NULL;
715 /* Test if the location originates from the spelling location of a
716 builtin-tokens. That is, return TRUE if LOC is a (possibly
717 virtual) location of a built-in token that appears in the expansion
718 list of a macro. Please note that this function also works on
719 tokens that result from built-in tokens. For instance, the
720 function would return true if passed a token "4" that is the result
721 of the expansion of the built-in __LINE__ macro. */
722 bool
723 is_location_from_builtin_token (source_location loc)
725 const line_map_ordinary *map = NULL;
726 loc = linemap_resolve_location (line_table, loc,
727 LRK_SPELLING_LOCATION, &map);
728 return loc == BUILTINS_LOCATION;
731 /* Expand the source location LOC into a human readable location. If
732 LOC is virtual, it resolves to the expansion point of the involved
733 macro. If LOC resolves to a builtin location, the file name of the
734 readable location is set to the string "<built-in>". */
736 expanded_location
737 expand_location (source_location loc)
739 return expand_location_1 (loc, /*expansion_point_p=*/true);
742 /* Expand the source location LOC into a human readable location. If
743 LOC is virtual, it resolves to the expansion location of the
744 relevant macro. If LOC resolves to a builtin location, the file
745 name of the readable location is set to the string
746 "<built-in>". */
748 expanded_location
749 expand_location_to_spelling_point (source_location loc)
751 return expand_location_1 (loc, /*expansion_point_p=*/false);
754 /* The rich_location class within libcpp requires a way to expand
755 source_location instances, and relies on the client code
756 providing a symbol named
757 linemap_client_expand_location_to_spelling_point
758 to do this.
760 This is the implementation for libcommon.a (all host binaries),
761 which simply calls into expand_location_to_spelling_point. */
763 expanded_location
764 linemap_client_expand_location_to_spelling_point (source_location loc)
766 return expand_location_to_spelling_point (loc);
770 /* If LOCATION is in a system header and if it is a virtual location for
771 a token coming from the expansion of a macro, unwind it to the
772 location of the expansion point of the macro. Otherwise, just return
773 LOCATION.
775 This is used for instance when we want to emit diagnostics about a
776 token that may be located in a macro that is itself defined in a
777 system header, for example, for the NULL macro. In such a case, if
778 LOCATION were passed directly to diagnostic functions such as
779 warning_at, the diagnostic would be suppressed (unless
780 -Wsystem-headers). */
782 source_location
783 expansion_point_location_if_in_system_header (source_location location)
785 if (in_system_header_at (location))
786 location = linemap_resolve_location (line_table, location,
787 LRK_MACRO_EXPANSION_POINT,
788 NULL);
789 return location;
792 #define ONE_K 1024
793 #define ONE_M (ONE_K * ONE_K)
795 /* Display a number as an integer multiple of either:
796 - 1024, if said integer is >= to 10 K (in base 2)
797 - 1024 * 1024, if said integer is >= 10 M in (base 2)
799 #define SCALE(x) ((unsigned long) ((x) < 10 * ONE_K \
800 ? (x) \
801 : ((x) < 10 * ONE_M \
802 ? (x) / ONE_K \
803 : (x) / ONE_M)))
805 /* For a given integer, display either:
806 - the character 'k', if the number is higher than 10 K (in base 2)
807 but strictly lower than 10 M (in base 2)
808 - the character 'M' if the number is higher than 10 M (in base2)
809 - the charcter ' ' if the number is strictly lower than 10 K */
810 #define STAT_LABEL(x) ((x) < 10 * ONE_K ? ' ' : ((x) < 10 * ONE_M ? 'k' : 'M'))
812 /* Display an integer amount as multiple of 1K or 1M (in base 2).
813 Display the correct unit (either k, M, or ' ') after the amout, as
814 well. */
815 #define FORMAT_AMOUNT(size) SCALE (size), STAT_LABEL (size)
817 /* Dump statistics to stderr about the memory usage of the line_table
818 set of line maps. This also displays some statistics about macro
819 expansion. */
821 void
822 dump_line_table_statistics (void)
824 struct linemap_stats s;
825 long total_used_map_size,
826 macro_maps_size,
827 total_allocated_map_size;
829 memset (&s, 0, sizeof (s));
831 linemap_get_statistics (line_table, &s);
833 macro_maps_size = s.macro_maps_used_size
834 + s.macro_maps_locations_size;
836 total_allocated_map_size = s.ordinary_maps_allocated_size
837 + s.macro_maps_allocated_size
838 + s.macro_maps_locations_size;
840 total_used_map_size = s.ordinary_maps_used_size
841 + s.macro_maps_used_size
842 + s.macro_maps_locations_size;
844 fprintf (stderr, "Number of expanded macros: %5ld\n",
845 s.num_expanded_macros);
846 if (s.num_expanded_macros != 0)
847 fprintf (stderr, "Average number of tokens per macro expansion: %5ld\n",
848 s.num_macro_tokens / s.num_expanded_macros);
849 fprintf (stderr,
850 "\nLine Table allocations during the "
851 "compilation process\n");
852 fprintf (stderr, "Number of ordinary maps used: %5ld%c\n",
853 SCALE (s.num_ordinary_maps_used),
854 STAT_LABEL (s.num_ordinary_maps_used));
855 fprintf (stderr, "Ordinary map used size: %5ld%c\n",
856 SCALE (s.ordinary_maps_used_size),
857 STAT_LABEL (s.ordinary_maps_used_size));
858 fprintf (stderr, "Number of ordinary maps allocated: %5ld%c\n",
859 SCALE (s.num_ordinary_maps_allocated),
860 STAT_LABEL (s.num_ordinary_maps_allocated));
861 fprintf (stderr, "Ordinary maps allocated size: %5ld%c\n",
862 SCALE (s.ordinary_maps_allocated_size),
863 STAT_LABEL (s.ordinary_maps_allocated_size));
864 fprintf (stderr, "Number of macro maps used: %5ld%c\n",
865 SCALE (s.num_macro_maps_used),
866 STAT_LABEL (s.num_macro_maps_used));
867 fprintf (stderr, "Macro maps used size: %5ld%c\n",
868 SCALE (s.macro_maps_used_size),
869 STAT_LABEL (s.macro_maps_used_size));
870 fprintf (stderr, "Macro maps locations size: %5ld%c\n",
871 SCALE (s.macro_maps_locations_size),
872 STAT_LABEL (s.macro_maps_locations_size));
873 fprintf (stderr, "Macro maps size: %5ld%c\n",
874 SCALE (macro_maps_size),
875 STAT_LABEL (macro_maps_size));
876 fprintf (stderr, "Duplicated maps locations size: %5ld%c\n",
877 SCALE (s.duplicated_macro_maps_locations_size),
878 STAT_LABEL (s.duplicated_macro_maps_locations_size));
879 fprintf (stderr, "Total allocated maps size: %5ld%c\n",
880 SCALE (total_allocated_map_size),
881 STAT_LABEL (total_allocated_map_size));
882 fprintf (stderr, "Total used maps size: %5ld%c\n",
883 SCALE (total_used_map_size),
884 STAT_LABEL (total_used_map_size));
885 fprintf (stderr, "Ad-hoc table size: %5ld%c\n",
886 SCALE (s.adhoc_table_size),
887 STAT_LABEL (s.adhoc_table_size));
888 fprintf (stderr, "Ad-hoc table entries used: %5ld\n",
889 s.adhoc_table_entries_used);
890 fprintf (stderr, "optimized_ranges: %i\n",
891 line_table->num_optimized_ranges);
892 fprintf (stderr, "unoptimized_ranges: %i\n",
893 line_table->num_unoptimized_ranges);
895 fprintf (stderr, "\n");
898 /* Get location one beyond the final location in ordinary map IDX. */
900 static source_location
901 get_end_location (struct line_maps *set, unsigned int idx)
903 if (idx == LINEMAPS_ORDINARY_USED (set) - 1)
904 return set->highest_location;
906 struct line_map *next_map = LINEMAPS_ORDINARY_MAP_AT (set, idx + 1);
907 return MAP_START_LOCATION (next_map);
910 /* Helper function for write_digit_row. */
912 static void
913 write_digit (FILE *stream, int digit)
915 fputc ('0' + (digit % 10), stream);
918 /* Helper function for dump_location_info.
919 Write a row of numbers to STREAM, numbering a source line,
920 giving the units, tens, hundreds etc of the column number. */
922 static void
923 write_digit_row (FILE *stream, int indent,
924 const line_map_ordinary *map,
925 source_location loc, int max_col, int divisor)
927 fprintf (stream, "%*c", indent, ' ');
928 fprintf (stream, "|");
929 for (int column = 1; column < max_col; column++)
931 source_location column_loc = loc + (column << map->m_range_bits);
932 write_digit (stream, column_loc / divisor);
934 fprintf (stream, "\n");
937 /* Write a half-closed (START) / half-open (END) interval of
938 source_location to STREAM. */
940 static void
941 dump_location_range (FILE *stream,
942 source_location start, source_location end)
944 fprintf (stream,
945 " source_location interval: %u <= loc < %u\n",
946 start, end);
949 /* Write a labelled description of a half-closed (START) / half-open (END)
950 interval of source_location to STREAM. */
952 static void
953 dump_labelled_location_range (FILE *stream,
954 const char *name,
955 source_location start, source_location end)
957 fprintf (stream, "%s\n", name);
958 dump_location_range (stream, start, end);
959 fprintf (stream, "\n");
962 /* Write a visualization of the locations in the line_table to STREAM. */
964 void
965 dump_location_info (FILE *stream)
967 /* Visualize the reserved locations. */
968 dump_labelled_location_range (stream, "RESERVED LOCATIONS",
969 0, RESERVED_LOCATION_COUNT);
971 /* Visualize the ordinary line_map instances, rendering the sources. */
972 for (unsigned int idx = 0; idx < LINEMAPS_ORDINARY_USED (line_table); idx++)
974 source_location end_location = get_end_location (line_table, idx);
975 /* half-closed: doesn't include this one. */
977 const line_map_ordinary *map
978 = LINEMAPS_ORDINARY_MAP_AT (line_table, idx);
979 fprintf (stream, "ORDINARY MAP: %i\n", idx);
980 dump_location_range (stream,
981 MAP_START_LOCATION (map), end_location);
982 fprintf (stream, " file: %s\n", ORDINARY_MAP_FILE_NAME (map));
983 fprintf (stream, " starting at line: %i\n",
984 ORDINARY_MAP_STARTING_LINE_NUMBER (map));
985 fprintf (stream, " column and range bits: %i\n",
986 map->m_column_and_range_bits);
987 fprintf (stream, " column bits: %i\n",
988 map->m_column_and_range_bits - map->m_range_bits);
989 fprintf (stream, " range bits: %i\n",
990 map->m_range_bits);
992 /* Render the span of source lines that this "map" covers. */
993 for (source_location loc = MAP_START_LOCATION (map);
994 loc < end_location;
995 loc += (1 << map->m_range_bits) )
997 gcc_assert (pure_location_p (line_table, loc) );
999 expanded_location exploc
1000 = linemap_expand_location (line_table, map, loc);
1002 if (0 == exploc.column)
1004 /* Beginning of a new source line: draw the line. */
1006 int line_size;
1007 const char *line_text = location_get_source_line (exploc.file,
1008 exploc.line,
1009 &line_size);
1010 if (!line_text)
1011 break;
1012 fprintf (stream,
1013 "%s:%3i|loc:%5i|%.*s\n",
1014 exploc.file, exploc.line,
1015 loc,
1016 line_size, line_text);
1018 /* "loc" is at column 0, which means "the whole line".
1019 Render the locations *within* the line, by underlining
1020 it, showing the source_location numeric values
1021 at each column. */
1022 int max_col = (1 << map->m_column_and_range_bits) - 1;
1023 if (max_col > line_size)
1024 max_col = line_size + 1;
1026 int indent = 14 + strlen (exploc.file);
1028 /* Thousands. */
1029 if (end_location > 999)
1030 write_digit_row (stream, indent, map, loc, max_col, 1000);
1032 /* Hundreds. */
1033 if (end_location > 99)
1034 write_digit_row (stream, indent, map, loc, max_col, 100);
1036 /* Tens. */
1037 write_digit_row (stream, indent, map, loc, max_col, 10);
1039 /* Units. */
1040 write_digit_row (stream, indent, map, loc, max_col, 1);
1043 fprintf (stream, "\n");
1046 /* Visualize unallocated values. */
1047 dump_labelled_location_range (stream, "UNALLOCATED LOCATIONS",
1048 line_table->highest_location,
1049 LINEMAPS_MACRO_LOWEST_LOCATION (line_table));
1051 /* Visualize the macro line_map instances, rendering the sources. */
1052 for (unsigned int i = 0; i < LINEMAPS_MACRO_USED (line_table); i++)
1054 /* Each macro map that is allocated owns source_location values
1055 that are *lower* that the one before them.
1056 Hence it's meaningful to view them either in order of ascending
1057 source locations, or in order of ascending macro map index. */
1058 const bool ascending_source_locations = true;
1059 unsigned int idx = (ascending_source_locations
1060 ? (LINEMAPS_MACRO_USED (line_table) - (i + 1))
1061 : i);
1062 const line_map_macro *map = LINEMAPS_MACRO_MAP_AT (line_table, idx);
1063 fprintf (stream, "MACRO %i: %s (%u tokens)\n",
1064 idx,
1065 linemap_map_get_macro_name (map),
1066 MACRO_MAP_NUM_MACRO_TOKENS (map));
1067 dump_location_range (stream,
1068 map->start_location,
1069 (map->start_location
1070 + MACRO_MAP_NUM_MACRO_TOKENS (map)));
1071 inform (MACRO_MAP_EXPANSION_POINT_LOCATION (map),
1072 "expansion point is location %i",
1073 MACRO_MAP_EXPANSION_POINT_LOCATION (map));
1074 fprintf (stream, " map->start_location: %u\n",
1075 map->start_location);
1077 fprintf (stream, " macro_locations:\n");
1078 for (unsigned int i = 0; i < MACRO_MAP_NUM_MACRO_TOKENS (map); i++)
1080 source_location x = MACRO_MAP_LOCATIONS (map)[2 * i];
1081 source_location y = MACRO_MAP_LOCATIONS (map)[(2 * i) + 1];
1083 /* linemap_add_macro_token encodes token numbers in an expansion
1084 by putting them after MAP_START_LOCATION. */
1086 /* I'm typically seeing 4 uninitialized entries at the end of
1087 0xafafafaf.
1088 This appears to be due to macro.c:replace_args
1089 adding 2 extra args for padding tokens; presumably there may
1090 be a leading and/or trailing padding token injected,
1091 each for 2 more location slots.
1092 This would explain there being up to 4 source_locations slots
1093 that may be uninitialized. */
1095 fprintf (stream, " %u: %u, %u\n",
1099 if (x == y)
1101 if (x < MAP_START_LOCATION (map))
1102 inform (x, "token %u has x-location == y-location == %u", i, x);
1103 else
1104 fprintf (stream,
1105 "x-location == y-location == %u encodes token # %u\n",
1106 x, x - MAP_START_LOCATION (map));
1108 else
1110 inform (x, "token %u has x-location == %u", i, x);
1111 inform (x, "token %u has y-location == %u", i, y);
1114 fprintf (stream, "\n");
1117 /* It appears that MAX_SOURCE_LOCATION itself is never assigned to a
1118 macro map, presumably due to an off-by-one error somewhere
1119 between the logic in linemap_enter_macro and
1120 LINEMAPS_MACRO_LOWEST_LOCATION. */
1121 dump_labelled_location_range (stream, "MAX_SOURCE_LOCATION",
1122 MAX_SOURCE_LOCATION,
1123 MAX_SOURCE_LOCATION + 1);
1125 /* Visualize ad-hoc values. */
1126 dump_labelled_location_range (stream, "AD-HOC LOCATIONS",
1127 MAX_SOURCE_LOCATION + 1, UINT_MAX);