]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/input.c
Fix selftest::temp_source_file ctor
[thirdparty/gcc.git] / gcc / input.c
1 /* Data and functions related to line maps and input files.
2 Copyright (C) 2004-2016 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "intl.h"
24 #include "diagnostic-core.h"
25 #include "selftest.h"
26 #include "cpplib.h"
27
28 /* This is a cache used by get_next_line to store the content of a
29 file to be searched for file lines. */
30 struct fcache
31 {
32 /* These are information used to store a line boundary. */
33 struct line_info
34 {
35 /* The line number. It starts from 1. */
36 size_t line_num;
37
38 /* The position (byte count) of the beginning of the line,
39 relative to the file data pointer. This starts at zero. */
40 size_t start_pos;
41
42 /* The position (byte count) of the last byte of the line. This
43 normally points to the '\n' character, or to one byte after the
44 last byte of the file, if the file doesn't contain a '\n'
45 character. */
46 size_t end_pos;
47
48 line_info (size_t l, size_t s, size_t e)
49 : line_num (l), start_pos (s), end_pos (e)
50 {}
51
52 line_info ()
53 :line_num (0), start_pos (0), end_pos (0)
54 {}
55 };
56
57 /* The number of time this file has been accessed. This is used
58 to designate which file cache to evict from the cache
59 array. */
60 unsigned use_count;
61
62 const char *file_path;
63
64 FILE *fp;
65
66 /* This points to the content of the file that we've read so
67 far. */
68 char *data;
69
70 /* The size of the DATA array above.*/
71 size_t size;
72
73 /* The number of bytes read from the underlying file so far. This
74 must be less (or equal) than SIZE above. */
75 size_t nb_read;
76
77 /* The index of the beginning of the current line. */
78 size_t line_start_idx;
79
80 /* The number of the previous line read. This starts at 1. Zero
81 means we've read no line so far. */
82 size_t line_num;
83
84 /* This is the total number of lines of the current file. At the
85 moment, we try to get this information from the line map
86 subsystem. Note that this is just a hint. When using the C++
87 front-end, this hint is correct because the input file is then
88 completely tokenized before parsing starts; so the line map knows
89 the number of lines before compilation really starts. For e.g,
90 the C front-end, it can happen that we start emitting diagnostics
91 before the line map has seen the end of the file. */
92 size_t total_lines;
93
94 /* This is a record of the beginning and end of the lines we've seen
95 while reading the file. This is useful to avoid walking the data
96 from the beginning when we are asked to read a line that is
97 before LINE_START_IDX above. Note that the maximum size of this
98 record is fcache_line_record_size, so that the memory consumption
99 doesn't explode. We thus scale total_lines down to
100 fcache_line_record_size. */
101 vec<line_info, va_heap> line_record;
102
103 fcache ();
104 ~fcache ();
105 };
106
107 /* Current position in real source file. */
108
109 location_t input_location = UNKNOWN_LOCATION;
110
111 struct line_maps *line_table;
112
113 static fcache *fcache_tab;
114 static const size_t fcache_tab_size = 16;
115 static const size_t fcache_buffer_size = 4 * 1024;
116 static const size_t fcache_line_record_size = 100;
117
118 /* Expand the source location LOC into a human readable location. If
119 LOC resolves to a builtin location, the file name of the readable
120 location is set to the string "<built-in>". If EXPANSION_POINT_P is
121 TRUE and LOC is virtual, then it is resolved to the expansion
122 point of the involved macro. Otherwise, it is resolved to the
123 spelling location of the token.
124
125 When resolving to the spelling location of the token, if the
126 resulting location is for a built-in location (that is, it has no
127 associated line/column) in the context of a macro expansion, the
128 returned location is the first one (while unwinding the macro
129 location towards its expansion point) that is in real source
130 code. */
131
132 static expanded_location
133 expand_location_1 (source_location loc,
134 bool expansion_point_p)
135 {
136 expanded_location xloc;
137 const line_map_ordinary *map;
138 enum location_resolution_kind lrk = LRK_MACRO_EXPANSION_POINT;
139 tree block = NULL;
140
141 if (IS_ADHOC_LOC (loc))
142 {
143 block = LOCATION_BLOCK (loc);
144 loc = LOCATION_LOCUS (loc);
145 }
146
147 memset (&xloc, 0, sizeof (xloc));
148
149 if (loc >= RESERVED_LOCATION_COUNT)
150 {
151 if (!expansion_point_p)
152 {
153 /* We want to resolve LOC to its spelling location.
154
155 But if that spelling location is a reserved location that
156 appears in the context of a macro expansion (like for a
157 location for a built-in token), let's consider the first
158 location (toward the expansion point) that is not reserved;
159 that is, the first location that is in real source code. */
160 loc = linemap_unwind_to_first_non_reserved_loc (line_table,
161 loc, NULL);
162 lrk = LRK_SPELLING_LOCATION;
163 }
164 loc = linemap_resolve_location (line_table, loc,
165 lrk, &map);
166 xloc = linemap_expand_location (line_table, map, loc);
167 }
168
169 xloc.data = block;
170 if (loc <= BUILTINS_LOCATION)
171 xloc.file = loc == UNKNOWN_LOCATION ? NULL : _("<built-in>");
172
173 return xloc;
174 }
175
176 /* Initialize the set of cache used for files accessed by caret
177 diagnostic. */
178
179 static void
180 diagnostic_file_cache_init (void)
181 {
182 if (fcache_tab == NULL)
183 fcache_tab = new fcache[fcache_tab_size];
184 }
185
186 /* Free the resources used by the set of cache used for files accessed
187 by caret diagnostic. */
188
189 void
190 diagnostic_file_cache_fini (void)
191 {
192 if (fcache_tab)
193 {
194 delete [] (fcache_tab);
195 fcache_tab = NULL;
196 }
197 }
198
199 /* Return the total lines number that have been read so far by the
200 line map (in the preprocessor) so far. For languages like C++ that
201 entirely preprocess the input file before starting to parse, this
202 equals the actual number of lines of the file. */
203
204 static size_t
205 total_lines_num (const char *file_path)
206 {
207 size_t r = 0;
208 source_location l = 0;
209 if (linemap_get_file_highest_location (line_table, file_path, &l))
210 {
211 gcc_assert (l >= RESERVED_LOCATION_COUNT);
212 expanded_location xloc = expand_location (l);
213 r = xloc.line;
214 }
215 return r;
216 }
217
218 /* Lookup the cache used for the content of a given file accessed by
219 caret diagnostic. Return the found cached file, or NULL if no
220 cached file was found. */
221
222 static fcache*
223 lookup_file_in_cache_tab (const char *file_path)
224 {
225 if (file_path == NULL)
226 return NULL;
227
228 diagnostic_file_cache_init ();
229
230 /* This will contain the found cached file. */
231 fcache *r = NULL;
232 for (unsigned i = 0; i < fcache_tab_size; ++i)
233 {
234 fcache *c = &fcache_tab[i];
235 if (c->file_path && !strcmp (c->file_path, file_path))
236 {
237 ++c->use_count;
238 r = c;
239 }
240 }
241
242 if (r)
243 ++r->use_count;
244
245 return r;
246 }
247
248 /* Return the file cache that has been less used, recently, or the
249 first empty one. If HIGHEST_USE_COUNT is non-null,
250 *HIGHEST_USE_COUNT is set to the highest use count of the entries
251 in the cache table. */
252
253 static fcache*
254 evicted_cache_tab_entry (unsigned *highest_use_count)
255 {
256 diagnostic_file_cache_init ();
257
258 fcache *to_evict = &fcache_tab[0];
259 unsigned huc = to_evict->use_count;
260 for (unsigned i = 1; i < fcache_tab_size; ++i)
261 {
262 fcache *c = &fcache_tab[i];
263 bool c_is_empty = (c->file_path == NULL);
264
265 if (c->use_count < to_evict->use_count
266 || (to_evict->file_path && c_is_empty))
267 /* We evict C because it's either an entry with a lower use
268 count or one that is empty. */
269 to_evict = c;
270
271 if (huc < c->use_count)
272 huc = c->use_count;
273
274 if (c_is_empty)
275 /* We've reached the end of the cache; subsequent elements are
276 all empty. */
277 break;
278 }
279
280 if (highest_use_count)
281 *highest_use_count = huc;
282
283 return to_evict;
284 }
285
286 /* Create the cache used for the content of a given file to be
287 accessed by caret diagnostic. This cache is added to an array of
288 cache and can be retrieved by lookup_file_in_cache_tab. This
289 function returns the created cache. Note that only the last
290 fcache_tab_size files are cached. */
291
292 static fcache*
293 add_file_to_cache_tab (const char *file_path)
294 {
295
296 FILE *fp = fopen (file_path, "r");
297 if (fp == NULL)
298 return NULL;
299
300 unsigned highest_use_count = 0;
301 fcache *r = evicted_cache_tab_entry (&highest_use_count);
302 r->file_path = file_path;
303 if (r->fp)
304 fclose (r->fp);
305 r->fp = fp;
306 r->nb_read = 0;
307 r->line_start_idx = 0;
308 r->line_num = 0;
309 r->line_record.truncate (0);
310 /* Ensure that this cache entry doesn't get evicted next time
311 add_file_to_cache_tab is called. */
312 r->use_count = ++highest_use_count;
313 r->total_lines = total_lines_num (file_path);
314
315 return r;
316 }
317
318 /* Lookup the cache used for the content of a given file accessed by
319 caret diagnostic. If no cached file was found, create a new cache
320 for this file, add it to the array of cached file and return
321 it. */
322
323 static fcache*
324 lookup_or_add_file_to_cache_tab (const char *file_path)
325 {
326 fcache *r = lookup_file_in_cache_tab (file_path);
327 if (r == NULL)
328 r = add_file_to_cache_tab (file_path);
329 return r;
330 }
331
332 /* Default constructor for a cache of file used by caret
333 diagnostic. */
334
335 fcache::fcache ()
336 : use_count (0), file_path (NULL), fp (NULL), data (0),
337 size (0), nb_read (0), line_start_idx (0), line_num (0),
338 total_lines (0)
339 {
340 line_record.create (0);
341 }
342
343 /* Destructor for a cache of file used by caret diagnostic. */
344
345 fcache::~fcache ()
346 {
347 if (fp)
348 {
349 fclose (fp);
350 fp = NULL;
351 }
352 if (data)
353 {
354 XDELETEVEC (data);
355 data = 0;
356 }
357 line_record.release ();
358 }
359
360 /* Returns TRUE iff the cache would need to be filled with data coming
361 from the file. That is, either the cache is empty or full or the
362 current line is empty. Note that if the cache is full, it would
363 need to be extended and filled again. */
364
365 static bool
366 needs_read (fcache *c)
367 {
368 return (c->nb_read == 0
369 || c->nb_read == c->size
370 || (c->line_start_idx >= c->nb_read - 1));
371 }
372
373 /* Return TRUE iff the cache is full and thus needs to be
374 extended. */
375
376 static bool
377 needs_grow (fcache *c)
378 {
379 return c->nb_read == c->size;
380 }
381
382 /* Grow the cache if it needs to be extended. */
383
384 static void
385 maybe_grow (fcache *c)
386 {
387 if (!needs_grow (c))
388 return;
389
390 size_t size = c->size == 0 ? fcache_buffer_size : c->size * 2;
391 c->data = XRESIZEVEC (char, c->data, size + 1);
392 c->size = size;
393 }
394
395 /* Read more data into the cache. Extends the cache if need be.
396 Returns TRUE iff new data could be read. */
397
398 static bool
399 read_data (fcache *c)
400 {
401 if (feof (c->fp) || ferror (c->fp))
402 return false;
403
404 maybe_grow (c);
405
406 char * from = c->data + c->nb_read;
407 size_t to_read = c->size - c->nb_read;
408 size_t nb_read = fread (from, 1, to_read, c->fp);
409
410 if (ferror (c->fp))
411 return false;
412
413 c->nb_read += nb_read;
414 return !!nb_read;
415 }
416
417 /* Read new data iff the cache needs to be filled with more data
418 coming from the file FP. Return TRUE iff the cache was filled with
419 mode data. */
420
421 static bool
422 maybe_read_data (fcache *c)
423 {
424 if (!needs_read (c))
425 return false;
426 return read_data (c);
427 }
428
429 /* Read a new line from file FP, using C as a cache for the data
430 coming from the file. Upon successful completion, *LINE is set to
431 the beginning of the line found. Space for that line has been
432 allocated in the cache thus *LINE has the same life time as C.
433 *LINE_LEN is set to the length of the line. Note that the line
434 does not contain any terminal delimiter. This function returns
435 true if some data was read or process from the cache, false
436 otherwise. Note that subsequent calls to get_next_line return the
437 next lines of the file and might overwrite the content of
438 *LINE. */
439
440 static bool
441 get_next_line (fcache *c, char **line, ssize_t *line_len)
442 {
443 /* Fill the cache with data to process. */
444 maybe_read_data (c);
445
446 size_t remaining_size = c->nb_read - c->line_start_idx;
447 if (remaining_size == 0)
448 /* There is no more data to process. */
449 return false;
450
451 char *line_start = c->data + c->line_start_idx;
452
453 char *next_line_start = NULL;
454 size_t len = 0;
455 char *line_end = (char *) memchr (line_start, '\n', remaining_size);
456 if (line_end == NULL)
457 {
458 /* We haven't found the end-of-line delimiter in the cache.
459 Fill the cache with more data from the file and look for the
460 '\n'. */
461 while (maybe_read_data (c))
462 {
463 line_start = c->data + c->line_start_idx;
464 remaining_size = c->nb_read - c->line_start_idx;
465 line_end = (char *) memchr (line_start, '\n', remaining_size);
466 if (line_end != NULL)
467 {
468 next_line_start = line_end + 1;
469 break;
470 }
471 }
472 if (line_end == NULL)
473 /* We've loadded all the file into the cache and still no
474 '\n'. Let's say the line ends up at one byte passed the
475 end of the file. This is to stay consistent with the case
476 of when the line ends up with a '\n' and line_end points to
477 that terminal '\n'. That consistency is useful below in
478 the len calculation. */
479 line_end = c->data + c->nb_read ;
480 }
481 else
482 next_line_start = line_end + 1;
483
484 if (ferror (c->fp))
485 return -1;
486
487 /* At this point, we've found the end of the of line. It either
488 points to the '\n' or to one byte after the last byte of the
489 file. */
490 gcc_assert (line_end != NULL);
491
492 len = line_end - line_start;
493
494 if (c->line_start_idx < c->nb_read)
495 *line = line_start;
496
497 ++c->line_num;
498
499 /* Before we update our line record, make sure the hint about the
500 total number of lines of the file is correct. If it's not, then
501 we give up recording line boundaries from now on. */
502 bool update_line_record = true;
503 if (c->line_num > c->total_lines)
504 update_line_record = false;
505
506 /* Now update our line record so that re-reading lines from the
507 before c->line_start_idx is faster. */
508 if (update_line_record
509 && c->line_record.length () < fcache_line_record_size)
510 {
511 /* If the file lines fits in the line record, we just record all
512 its lines ...*/
513 if (c->total_lines <= fcache_line_record_size
514 && c->line_num > c->line_record.length ())
515 c->line_record.safe_push (fcache::line_info (c->line_num,
516 c->line_start_idx,
517 line_end - c->data));
518 else if (c->total_lines > fcache_line_record_size)
519 {
520 /* ... otherwise, we just scale total_lines down to
521 (fcache_line_record_size lines. */
522 size_t n = (c->line_num * fcache_line_record_size) / c->total_lines;
523 if (c->line_record.length () == 0
524 || n >= c->line_record.length ())
525 c->line_record.safe_push (fcache::line_info (c->line_num,
526 c->line_start_idx,
527 line_end - c->data));
528 }
529 }
530
531 /* Update c->line_start_idx so that it points to the next line to be
532 read. */
533 if (next_line_start)
534 c->line_start_idx = next_line_start - c->data;
535 else
536 /* We didn't find any terminal '\n'. Let's consider that the end
537 of line is the end of the data in the cache. The next
538 invocation of get_next_line will either read more data from the
539 underlying file or return false early because we've reached the
540 end of the file. */
541 c->line_start_idx = c->nb_read;
542
543 *line_len = len;
544
545 return true;
546 }
547
548 /* Reads the next line from FILE into *LINE. If *LINE is too small
549 (or NULL) it is allocated (or extended) to have enough space to
550 containe the line. *LINE_LENGTH must contain the size of the
551 initial*LINE buffer. It's then updated by this function to the
552 actual length of the returned line. Note that the returned line
553 can contain several zero bytes. Also note that the returned string
554 is allocated in static storage that is going to be re-used by
555 subsequent invocations of read_line. */
556
557 static bool
558 read_next_line (fcache *cache, char ** line, ssize_t *line_len)
559 {
560 char *l = NULL;
561 ssize_t len = 0;
562
563 if (!get_next_line (cache, &l, &len))
564 return false;
565
566 if (*line == NULL)
567 *line = XNEWVEC (char, len);
568 else
569 if (*line_len < len)
570 *line = XRESIZEVEC (char, *line, len);
571
572 memcpy (*line, l, len);
573 *line_len = len;
574
575 return true;
576 }
577
578 /* Consume the next bytes coming from the cache (or from its
579 underlying file if there are remaining unread bytes in the file)
580 until we reach the next end-of-line (or end-of-file). There is no
581 copying from the cache involved. Return TRUE upon successful
582 completion. */
583
584 static bool
585 goto_next_line (fcache *cache)
586 {
587 char *l;
588 ssize_t len;
589
590 return get_next_line (cache, &l, &len);
591 }
592
593 /* Read an arbitrary line number LINE_NUM from the file cached in C.
594 The line is copied into *LINE. *LINE_LEN must have been set to the
595 length of *LINE. If *LINE is too small (or NULL) it's extended (or
596 allocated) and *LINE_LEN is adjusted accordingly. *LINE ends up
597 with a terminal zero byte and can contain additional zero bytes.
598 This function returns bool if a line was read. */
599
600 static bool
601 read_line_num (fcache *c, size_t line_num,
602 char ** line, ssize_t *line_len)
603 {
604 gcc_assert (line_num > 0);
605
606 if (line_num <= c->line_num)
607 {
608 /* We've been asked to read lines that are before c->line_num.
609 So lets use our line record (if it's not empty) to try to
610 avoid re-reading the file from the beginning again. */
611
612 if (c->line_record.is_empty ())
613 {
614 c->line_start_idx = 0;
615 c->line_num = 0;
616 }
617 else
618 {
619 fcache::line_info *i = NULL;
620 if (c->total_lines <= fcache_line_record_size)
621 {
622 /* In languages where the input file is not totally
623 preprocessed up front, the c->total_lines hint
624 can be smaller than the number of lines of the
625 file. In that case, only the first
626 c->total_lines have been recorded.
627
628 Otherwise, the first c->total_lines we've read have
629 their start/end recorded here. */
630 i = (line_num <= c->total_lines)
631 ? &c->line_record[line_num - 1]
632 : &c->line_record[c->total_lines - 1];
633 gcc_assert (i->line_num <= line_num);
634 }
635 else
636 {
637 /* So the file had more lines than our line record
638 size. Thus the number of lines we've recorded has
639 been scaled down to fcache_line_reacord_size. Let's
640 pick the start/end of the recorded line that is
641 closest to line_num. */
642 size_t n = (line_num <= c->total_lines)
643 ? line_num * fcache_line_record_size / c->total_lines
644 : c ->line_record.length () - 1;
645 if (n < c->line_record.length ())
646 {
647 i = &c->line_record[n];
648 gcc_assert (i->line_num <= line_num);
649 }
650 }
651
652 if (i && i->line_num == line_num)
653 {
654 /* We have the start/end of the line. Let's just copy
655 it again and we are done. */
656 ssize_t len = i->end_pos - i->start_pos + 1;
657 if (*line_len < len)
658 *line = XRESIZEVEC (char, *line, len);
659 memmove (*line, c->data + i->start_pos, len);
660 (*line)[len - 1] = '\0';
661 *line_len = --len;
662 return true;
663 }
664
665 if (i)
666 {
667 c->line_start_idx = i->start_pos;
668 c->line_num = i->line_num - 1;
669 }
670 else
671 {
672 c->line_start_idx = 0;
673 c->line_num = 0;
674 }
675 }
676 }
677
678 /* Let's walk from line c->line_num up to line_num - 1, without
679 copying any line. */
680 while (c->line_num < line_num - 1)
681 if (!goto_next_line (c))
682 return false;
683
684 /* The line we want is the next one. Let's read and copy it back to
685 the caller. */
686 return read_next_line (c, line, line_len);
687 }
688
689 /* Return the physical source line that corresponds to FILE_PATH/LINE in a
690 buffer that is statically allocated. The newline is replaced by
691 the null character. Note that the line can contain several null
692 characters, so LINE_LEN, if non-null, points to the actual length
693 of the line. */
694
695 const char *
696 location_get_source_line (const char *file_path, int line,
697 int *line_len)
698 {
699 static char *buffer;
700 static ssize_t len;
701
702 if (line == 0)
703 return NULL;
704
705 fcache *c = lookup_or_add_file_to_cache_tab (file_path);
706 if (c == NULL)
707 return NULL;
708
709 bool read = read_line_num (c, line, &buffer, &len);
710
711 if (read && line_len)
712 *line_len = len;
713
714 return read ? buffer : NULL;
715 }
716
717 /* Test if the location originates from the spelling location of a
718 builtin-tokens. That is, return TRUE if LOC is a (possibly
719 virtual) location of a built-in token that appears in the expansion
720 list of a macro. Please note that this function also works on
721 tokens that result from built-in tokens. For instance, the
722 function would return true if passed a token "4" that is the result
723 of the expansion of the built-in __LINE__ macro. */
724 bool
725 is_location_from_builtin_token (source_location loc)
726 {
727 const line_map_ordinary *map = NULL;
728 loc = linemap_resolve_location (line_table, loc,
729 LRK_SPELLING_LOCATION, &map);
730 return loc == BUILTINS_LOCATION;
731 }
732
733 /* Expand the source location LOC into a human readable location. If
734 LOC is virtual, it resolves to the expansion point of the involved
735 macro. If LOC resolves to a builtin location, the file name of the
736 readable location is set to the string "<built-in>". */
737
738 expanded_location
739 expand_location (source_location loc)
740 {
741 return expand_location_1 (loc, /*expansion_point_p=*/true);
742 }
743
744 /* Expand the source location LOC into a human readable location. If
745 LOC is virtual, it resolves to the expansion location of the
746 relevant macro. If LOC resolves to a builtin location, the file
747 name of the readable location is set to the string
748 "<built-in>". */
749
750 expanded_location
751 expand_location_to_spelling_point (source_location loc)
752 {
753 return expand_location_1 (loc, /*expansion_point_p=*/false);
754 }
755
756 /* The rich_location class within libcpp requires a way to expand
757 source_location instances, and relies on the client code
758 providing a symbol named
759 linemap_client_expand_location_to_spelling_point
760 to do this.
761
762 This is the implementation for libcommon.a (all host binaries),
763 which simply calls into expand_location_to_spelling_point. */
764
765 expanded_location
766 linemap_client_expand_location_to_spelling_point (source_location loc)
767 {
768 return expand_location_to_spelling_point (loc);
769 }
770
771
772 /* If LOCATION is in a system header and if it is a virtual location for
773 a token coming from the expansion of a macro, unwind it to the
774 location of the expansion point of the macro. Otherwise, just return
775 LOCATION.
776
777 This is used for instance when we want to emit diagnostics about a
778 token that may be located in a macro that is itself defined in a
779 system header, for example, for the NULL macro. In such a case, if
780 LOCATION were passed directly to diagnostic functions such as
781 warning_at, the diagnostic would be suppressed (unless
782 -Wsystem-headers). */
783
784 source_location
785 expansion_point_location_if_in_system_header (source_location location)
786 {
787 if (in_system_header_at (location))
788 location = linemap_resolve_location (line_table, location,
789 LRK_MACRO_EXPANSION_POINT,
790 NULL);
791 return location;
792 }
793
794 /* If LOCATION is a virtual location for a token coming from the expansion
795 of a macro, unwind to the location of the expansion point of the macro. */
796
797 source_location
798 expansion_point_location (source_location location)
799 {
800 return linemap_resolve_location (line_table, location,
801 LRK_MACRO_EXPANSION_POINT, NULL);
802 }
803
804 #define ONE_K 1024
805 #define ONE_M (ONE_K * ONE_K)
806
807 /* Display a number as an integer multiple of either:
808 - 1024, if said integer is >= to 10 K (in base 2)
809 - 1024 * 1024, if said integer is >= 10 M in (base 2)
810 */
811 #define SCALE(x) ((unsigned long) ((x) < 10 * ONE_K \
812 ? (x) \
813 : ((x) < 10 * ONE_M \
814 ? (x) / ONE_K \
815 : (x) / ONE_M)))
816
817 /* For a given integer, display either:
818 - the character 'k', if the number is higher than 10 K (in base 2)
819 but strictly lower than 10 M (in base 2)
820 - the character 'M' if the number is higher than 10 M (in base2)
821 - the charcter ' ' if the number is strictly lower than 10 K */
822 #define STAT_LABEL(x) ((x) < 10 * ONE_K ? ' ' : ((x) < 10 * ONE_M ? 'k' : 'M'))
823
824 /* Display an integer amount as multiple of 1K or 1M (in base 2).
825 Display the correct unit (either k, M, or ' ') after the amout, as
826 well. */
827 #define FORMAT_AMOUNT(size) SCALE (size), STAT_LABEL (size)
828
829 /* Dump statistics to stderr about the memory usage of the line_table
830 set of line maps. This also displays some statistics about macro
831 expansion. */
832
833 void
834 dump_line_table_statistics (void)
835 {
836 struct linemap_stats s;
837 long total_used_map_size,
838 macro_maps_size,
839 total_allocated_map_size;
840
841 memset (&s, 0, sizeof (s));
842
843 linemap_get_statistics (line_table, &s);
844
845 macro_maps_size = s.macro_maps_used_size
846 + s.macro_maps_locations_size;
847
848 total_allocated_map_size = s.ordinary_maps_allocated_size
849 + s.macro_maps_allocated_size
850 + s.macro_maps_locations_size;
851
852 total_used_map_size = s.ordinary_maps_used_size
853 + s.macro_maps_used_size
854 + s.macro_maps_locations_size;
855
856 fprintf (stderr, "Number of expanded macros: %5ld\n",
857 s.num_expanded_macros);
858 if (s.num_expanded_macros != 0)
859 fprintf (stderr, "Average number of tokens per macro expansion: %5ld\n",
860 s.num_macro_tokens / s.num_expanded_macros);
861 fprintf (stderr,
862 "\nLine Table allocations during the "
863 "compilation process\n");
864 fprintf (stderr, "Number of ordinary maps used: %5ld%c\n",
865 SCALE (s.num_ordinary_maps_used),
866 STAT_LABEL (s.num_ordinary_maps_used));
867 fprintf (stderr, "Ordinary map used size: %5ld%c\n",
868 SCALE (s.ordinary_maps_used_size),
869 STAT_LABEL (s.ordinary_maps_used_size));
870 fprintf (stderr, "Number of ordinary maps allocated: %5ld%c\n",
871 SCALE (s.num_ordinary_maps_allocated),
872 STAT_LABEL (s.num_ordinary_maps_allocated));
873 fprintf (stderr, "Ordinary maps allocated size: %5ld%c\n",
874 SCALE (s.ordinary_maps_allocated_size),
875 STAT_LABEL (s.ordinary_maps_allocated_size));
876 fprintf (stderr, "Number of macro maps used: %5ld%c\n",
877 SCALE (s.num_macro_maps_used),
878 STAT_LABEL (s.num_macro_maps_used));
879 fprintf (stderr, "Macro maps used size: %5ld%c\n",
880 SCALE (s.macro_maps_used_size),
881 STAT_LABEL (s.macro_maps_used_size));
882 fprintf (stderr, "Macro maps locations size: %5ld%c\n",
883 SCALE (s.macro_maps_locations_size),
884 STAT_LABEL (s.macro_maps_locations_size));
885 fprintf (stderr, "Macro maps size: %5ld%c\n",
886 SCALE (macro_maps_size),
887 STAT_LABEL (macro_maps_size));
888 fprintf (stderr, "Duplicated maps locations size: %5ld%c\n",
889 SCALE (s.duplicated_macro_maps_locations_size),
890 STAT_LABEL (s.duplicated_macro_maps_locations_size));
891 fprintf (stderr, "Total allocated maps size: %5ld%c\n",
892 SCALE (total_allocated_map_size),
893 STAT_LABEL (total_allocated_map_size));
894 fprintf (stderr, "Total used maps size: %5ld%c\n",
895 SCALE (total_used_map_size),
896 STAT_LABEL (total_used_map_size));
897 fprintf (stderr, "Ad-hoc table size: %5ld%c\n",
898 SCALE (s.adhoc_table_size),
899 STAT_LABEL (s.adhoc_table_size));
900 fprintf (stderr, "Ad-hoc table entries used: %5ld\n",
901 s.adhoc_table_entries_used);
902 fprintf (stderr, "optimized_ranges: %i\n",
903 line_table->num_optimized_ranges);
904 fprintf (stderr, "unoptimized_ranges: %i\n",
905 line_table->num_unoptimized_ranges);
906
907 fprintf (stderr, "\n");
908 }
909
910 /* Get location one beyond the final location in ordinary map IDX. */
911
912 static source_location
913 get_end_location (struct line_maps *set, unsigned int idx)
914 {
915 if (idx == LINEMAPS_ORDINARY_USED (set) - 1)
916 return set->highest_location;
917
918 struct line_map *next_map = LINEMAPS_ORDINARY_MAP_AT (set, idx + 1);
919 return MAP_START_LOCATION (next_map);
920 }
921
922 /* Helper function for write_digit_row. */
923
924 static void
925 write_digit (FILE *stream, int digit)
926 {
927 fputc ('0' + (digit % 10), stream);
928 }
929
930 /* Helper function for dump_location_info.
931 Write a row of numbers to STREAM, numbering a source line,
932 giving the units, tens, hundreds etc of the column number. */
933
934 static void
935 write_digit_row (FILE *stream, int indent,
936 const line_map_ordinary *map,
937 source_location loc, int max_col, int divisor)
938 {
939 fprintf (stream, "%*c", indent, ' ');
940 fprintf (stream, "|");
941 for (int column = 1; column < max_col; column++)
942 {
943 source_location column_loc = loc + (column << map->m_range_bits);
944 write_digit (stream, column_loc / divisor);
945 }
946 fprintf (stream, "\n");
947 }
948
949 /* Write a half-closed (START) / half-open (END) interval of
950 source_location to STREAM. */
951
952 static void
953 dump_location_range (FILE *stream,
954 source_location start, source_location end)
955 {
956 fprintf (stream,
957 " source_location interval: %u <= loc < %u\n",
958 start, end);
959 }
960
961 /* Write a labelled description of a half-closed (START) / half-open (END)
962 interval of source_location to STREAM. */
963
964 static void
965 dump_labelled_location_range (FILE *stream,
966 const char *name,
967 source_location start, source_location end)
968 {
969 fprintf (stream, "%s\n", name);
970 dump_location_range (stream, start, end);
971 fprintf (stream, "\n");
972 }
973
974 /* Write a visualization of the locations in the line_table to STREAM. */
975
976 void
977 dump_location_info (FILE *stream)
978 {
979 /* Visualize the reserved locations. */
980 dump_labelled_location_range (stream, "RESERVED LOCATIONS",
981 0, RESERVED_LOCATION_COUNT);
982
983 /* Visualize the ordinary line_map instances, rendering the sources. */
984 for (unsigned int idx = 0; idx < LINEMAPS_ORDINARY_USED (line_table); idx++)
985 {
986 source_location end_location = get_end_location (line_table, idx);
987 /* half-closed: doesn't include this one. */
988
989 const line_map_ordinary *map
990 = LINEMAPS_ORDINARY_MAP_AT (line_table, idx);
991 fprintf (stream, "ORDINARY MAP: %i\n", idx);
992 dump_location_range (stream,
993 MAP_START_LOCATION (map), end_location);
994 fprintf (stream, " file: %s\n", ORDINARY_MAP_FILE_NAME (map));
995 fprintf (stream, " starting at line: %i\n",
996 ORDINARY_MAP_STARTING_LINE_NUMBER (map));
997 fprintf (stream, " column and range bits: %i\n",
998 map->m_column_and_range_bits);
999 fprintf (stream, " column bits: %i\n",
1000 map->m_column_and_range_bits - map->m_range_bits);
1001 fprintf (stream, " range bits: %i\n",
1002 map->m_range_bits);
1003
1004 /* Render the span of source lines that this "map" covers. */
1005 for (source_location loc = MAP_START_LOCATION (map);
1006 loc < end_location;
1007 loc += (1 << map->m_range_bits) )
1008 {
1009 gcc_assert (pure_location_p (line_table, loc) );
1010
1011 expanded_location exploc
1012 = linemap_expand_location (line_table, map, loc);
1013
1014 if (0 == exploc.column)
1015 {
1016 /* Beginning of a new source line: draw the line. */
1017
1018 int line_size;
1019 const char *line_text = location_get_source_line (exploc.file,
1020 exploc.line,
1021 &line_size);
1022 if (!line_text)
1023 break;
1024 fprintf (stream,
1025 "%s:%3i|loc:%5i|%.*s\n",
1026 exploc.file, exploc.line,
1027 loc,
1028 line_size, line_text);
1029
1030 /* "loc" is at column 0, which means "the whole line".
1031 Render the locations *within* the line, by underlining
1032 it, showing the source_location numeric values
1033 at each column. */
1034 int max_col = (1 << map->m_column_and_range_bits) - 1;
1035 if (max_col > line_size)
1036 max_col = line_size + 1;
1037
1038 int indent = 14 + strlen (exploc.file);
1039
1040 /* Thousands. */
1041 if (end_location > 999)
1042 write_digit_row (stream, indent, map, loc, max_col, 1000);
1043
1044 /* Hundreds. */
1045 if (end_location > 99)
1046 write_digit_row (stream, indent, map, loc, max_col, 100);
1047
1048 /* Tens. */
1049 write_digit_row (stream, indent, map, loc, max_col, 10);
1050
1051 /* Units. */
1052 write_digit_row (stream, indent, map, loc, max_col, 1);
1053 }
1054 }
1055 fprintf (stream, "\n");
1056 }
1057
1058 /* Visualize unallocated values. */
1059 dump_labelled_location_range (stream, "UNALLOCATED LOCATIONS",
1060 line_table->highest_location,
1061 LINEMAPS_MACRO_LOWEST_LOCATION (line_table));
1062
1063 /* Visualize the macro line_map instances, rendering the sources. */
1064 for (unsigned int i = 0; i < LINEMAPS_MACRO_USED (line_table); i++)
1065 {
1066 /* Each macro map that is allocated owns source_location values
1067 that are *lower* that the one before them.
1068 Hence it's meaningful to view them either in order of ascending
1069 source locations, or in order of ascending macro map index. */
1070 const bool ascending_source_locations = true;
1071 unsigned int idx = (ascending_source_locations
1072 ? (LINEMAPS_MACRO_USED (line_table) - (i + 1))
1073 : i);
1074 const line_map_macro *map = LINEMAPS_MACRO_MAP_AT (line_table, idx);
1075 fprintf (stream, "MACRO %i: %s (%u tokens)\n",
1076 idx,
1077 linemap_map_get_macro_name (map),
1078 MACRO_MAP_NUM_MACRO_TOKENS (map));
1079 dump_location_range (stream,
1080 map->start_location,
1081 (map->start_location
1082 + MACRO_MAP_NUM_MACRO_TOKENS (map)));
1083 inform (MACRO_MAP_EXPANSION_POINT_LOCATION (map),
1084 "expansion point is location %i",
1085 MACRO_MAP_EXPANSION_POINT_LOCATION (map));
1086 fprintf (stream, " map->start_location: %u\n",
1087 map->start_location);
1088
1089 fprintf (stream, " macro_locations:\n");
1090 for (unsigned int i = 0; i < MACRO_MAP_NUM_MACRO_TOKENS (map); i++)
1091 {
1092 source_location x = MACRO_MAP_LOCATIONS (map)[2 * i];
1093 source_location y = MACRO_MAP_LOCATIONS (map)[(2 * i) + 1];
1094
1095 /* linemap_add_macro_token encodes token numbers in an expansion
1096 by putting them after MAP_START_LOCATION. */
1097
1098 /* I'm typically seeing 4 uninitialized entries at the end of
1099 0xafafafaf.
1100 This appears to be due to macro.c:replace_args
1101 adding 2 extra args for padding tokens; presumably there may
1102 be a leading and/or trailing padding token injected,
1103 each for 2 more location slots.
1104 This would explain there being up to 4 source_locations slots
1105 that may be uninitialized. */
1106
1107 fprintf (stream, " %u: %u, %u\n",
1108 i,
1109 x,
1110 y);
1111 if (x == y)
1112 {
1113 if (x < MAP_START_LOCATION (map))
1114 inform (x, "token %u has x-location == y-location == %u", i, x);
1115 else
1116 fprintf (stream,
1117 "x-location == y-location == %u encodes token # %u\n",
1118 x, x - MAP_START_LOCATION (map));
1119 }
1120 else
1121 {
1122 inform (x, "token %u has x-location == %u", i, x);
1123 inform (x, "token %u has y-location == %u", i, y);
1124 }
1125 }
1126 fprintf (stream, "\n");
1127 }
1128
1129 /* It appears that MAX_SOURCE_LOCATION itself is never assigned to a
1130 macro map, presumably due to an off-by-one error somewhere
1131 between the logic in linemap_enter_macro and
1132 LINEMAPS_MACRO_LOWEST_LOCATION. */
1133 dump_labelled_location_range (stream, "MAX_SOURCE_LOCATION",
1134 MAX_SOURCE_LOCATION,
1135 MAX_SOURCE_LOCATION + 1);
1136
1137 /* Visualize ad-hoc values. */
1138 dump_labelled_location_range (stream, "AD-HOC LOCATIONS",
1139 MAX_SOURCE_LOCATION + 1, UINT_MAX);
1140 }
1141
1142 #if CHECKING_P
1143
1144 namespace selftest {
1145
1146 /* Selftests of location handling. */
1147
1148 /* A class for writing out a temporary sourcefile for use in selftests
1149 of input handling. */
1150
1151 class temp_source_file
1152 {
1153 public:
1154 temp_source_file (const location &loc, const char *suffix,
1155 const char *content);
1156 ~temp_source_file ();
1157
1158 const char *get_filename () const { return m_filename; }
1159
1160 private:
1161 char *m_filename;
1162 };
1163
1164 /* Constructor. Create a tempfile using SUFFIX, and write CONTENT to
1165 it. Abort if anything goes wrong, using LOC as the effective
1166 location in the problem report. */
1167
1168 temp_source_file::temp_source_file (const location &loc, const char *suffix,
1169 const char *content)
1170 {
1171 m_filename = make_temp_file (suffix);
1172 ASSERT_NE (m_filename, NULL);
1173
1174 FILE *out = fopen (m_filename, "w");
1175 if (!out)
1176 ::selftest::fail_formatted (loc, "unable to open tempfile: %s",
1177 m_filename);
1178 fprintf (out, "%s", content);
1179 fclose (out);
1180 }
1181
1182 /* Destructor. Delete the tempfile. */
1183
1184 temp_source_file::~temp_source_file ()
1185 {
1186 unlink (m_filename);
1187 free (m_filename);
1188 }
1189
1190 /* Helper function for verifying location data: when location_t
1191 values are > LINE_MAP_MAX_LOCATION_WITH_COLS, they are treated
1192 as having column 0. */
1193
1194 static bool
1195 should_have_column_data_p (location_t loc)
1196 {
1197 if (IS_ADHOC_LOC (loc))
1198 loc = get_location_from_adhoc_loc (line_table, loc);
1199 if (loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
1200 return false;
1201 return true;
1202 }
1203
1204 /* Selftest for should_have_column_data_p. */
1205
1206 static void
1207 test_should_have_column_data_p ()
1208 {
1209 ASSERT_TRUE (should_have_column_data_p (RESERVED_LOCATION_COUNT));
1210 ASSERT_TRUE
1211 (should_have_column_data_p (LINE_MAP_MAX_LOCATION_WITH_COLS));
1212 ASSERT_FALSE
1213 (should_have_column_data_p (LINE_MAP_MAX_LOCATION_WITH_COLS + 1));
1214 }
1215
1216 /* Verify the result of LOCATION_FILE/LOCATION_LINE/LOCATION_COLUMN
1217 on LOC. */
1218
1219 static void
1220 assert_loceq (const char *exp_filename, int exp_linenum, int exp_colnum,
1221 location_t loc)
1222 {
1223 ASSERT_STREQ (exp_filename, LOCATION_FILE (loc));
1224 ASSERT_EQ (exp_linenum, LOCATION_LINE (loc));
1225 /* If location_t values are sufficiently high, then column numbers
1226 will be unavailable and LOCATION_COLUMN (loc) will be 0.
1227 When close to the threshold, column numbers *may* be present: if
1228 the final linemap before the threshold contains a line that straddles
1229 the threshold, locations in that line have column information. */
1230 if (should_have_column_data_p (loc))
1231 ASSERT_EQ (exp_colnum, LOCATION_COLUMN (loc));
1232 }
1233
1234 /* Various selftests in this file involve constructing a line table
1235 and one or more line maps within it.
1236
1237 For maximum test coverage we want to run these tests with a variety
1238 of situations:
1239 - line_table->default_range_bits: some frontends use a non-zero value
1240 and others use zero
1241 - the fallback modes within line-map.c: there are various threshold
1242 values for source_location/location_t beyond line-map.c changes
1243 behavior (disabling of the range-packing optimization, disabling
1244 of column-tracking). We can exercise these by starting the line_table
1245 at interesting values at or near these thresholds.
1246
1247 The following struct describes a particular case within our test
1248 matrix. */
1249
1250 struct line_table_case
1251 {
1252 line_table_case (int default_range_bits, int base_location)
1253 : m_default_range_bits (default_range_bits),
1254 m_base_location (base_location)
1255 {}
1256
1257 int m_default_range_bits;
1258 int m_base_location;
1259 };
1260
1261 /* A class for overriding the global "line_table" within a selftest,
1262 restoring its value afterwards. */
1263
1264 class temp_line_table
1265 {
1266 public:
1267 temp_line_table (const line_table_case &);
1268 ~temp_line_table ();
1269
1270 private:
1271 line_maps *m_old_line_table;
1272 };
1273
1274 /* Constructor. Store the old value of line_table, and create a new
1275 one, using the sitation described in CASE_. */
1276
1277 temp_line_table::temp_line_table (const line_table_case &case_)
1278 : m_old_line_table (line_table)
1279 {
1280 line_table = ggc_alloc<line_maps> ();
1281 linemap_init (line_table, BUILTINS_LOCATION);
1282 line_table->reallocator = m_old_line_table->reallocator;
1283 line_table->round_alloc_size = m_old_line_table->round_alloc_size;
1284 line_table->default_range_bits = case_.m_default_range_bits;
1285 if (case_.m_base_location)
1286 {
1287 line_table->highest_location = case_.m_base_location;
1288 line_table->highest_line = case_.m_base_location;
1289 }
1290 }
1291
1292 /* Destructor. Restore the old value of line_table. */
1293
1294 temp_line_table::~temp_line_table ()
1295 {
1296 line_table = m_old_line_table;
1297 }
1298
1299 /* Verify basic operation of ordinary linemaps. */
1300
1301 static void
1302 test_accessing_ordinary_linemaps (const line_table_case &case_)
1303 {
1304 temp_line_table tmp_lt (case_);
1305
1306 /* Build a simple linemap describing some locations. */
1307 linemap_add (line_table, LC_ENTER, false, "foo.c", 0);
1308
1309 linemap_line_start (line_table, 1, 100);
1310 location_t loc_a = linemap_position_for_column (line_table, 1);
1311 location_t loc_b = linemap_position_for_column (line_table, 23);
1312
1313 linemap_line_start (line_table, 2, 100);
1314 location_t loc_c = linemap_position_for_column (line_table, 1);
1315 location_t loc_d = linemap_position_for_column (line_table, 17);
1316
1317 /* Example of a very long line. */
1318 linemap_line_start (line_table, 3, 2000);
1319 location_t loc_e = linemap_position_for_column (line_table, 700);
1320
1321 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1322
1323 /* Multiple files. */
1324 linemap_add (line_table, LC_ENTER, false, "bar.c", 0);
1325 linemap_line_start (line_table, 1, 200);
1326 location_t loc_f = linemap_position_for_column (line_table, 150);
1327 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1328
1329 /* Verify that we can recover the location info. */
1330 assert_loceq ("foo.c", 1, 1, loc_a);
1331 assert_loceq ("foo.c", 1, 23, loc_b);
1332 assert_loceq ("foo.c", 2, 1, loc_c);
1333 assert_loceq ("foo.c", 2, 17, loc_d);
1334 assert_loceq ("foo.c", 3, 700, loc_e);
1335 assert_loceq ("bar.c", 1, 150, loc_f);
1336
1337 ASSERT_FALSE (is_location_from_builtin_token (loc_a));
1338 }
1339
1340 /* Verify various properties of UNKNOWN_LOCATION. */
1341
1342 static void
1343 test_unknown_location ()
1344 {
1345 ASSERT_EQ (NULL, LOCATION_FILE (UNKNOWN_LOCATION));
1346 ASSERT_EQ (0, LOCATION_LINE (UNKNOWN_LOCATION));
1347 ASSERT_EQ (0, LOCATION_COLUMN (UNKNOWN_LOCATION));
1348 }
1349
1350 /* Verify various properties of BUILTINS_LOCATION. */
1351
1352 static void
1353 test_builtins ()
1354 {
1355 assert_loceq (_("<built-in>"), 0, 0, BUILTINS_LOCATION);
1356 ASSERT_PRED1 (is_location_from_builtin_token, BUILTINS_LOCATION);
1357 }
1358
1359 /* Verify reading of input files (e.g. for caret-based diagnostics). */
1360
1361 static void
1362 test_reading_source_line ()
1363 {
1364 /* Create a tempfile and write some text to it. */
1365 temp_source_file tmp (SELFTEST_LOCATION, ".txt",
1366 "01234567890123456789\n"
1367 "This is the test text\n"
1368 "This is the 3rd line\n");
1369
1370 /* Read back a specific line from the tempfile. */
1371 int line_size;
1372 const char *source_line = location_get_source_line (tmp.get_filename (),
1373 2, &line_size);
1374 ASSERT_TRUE (source_line != NULL);
1375 ASSERT_EQ (21, line_size);
1376 if (!strncmp ("This is the test text",
1377 source_line, line_size))
1378 ::selftest::pass (SELFTEST_LOCATION,
1379 "source_line matched expected value");
1380 else
1381 ::selftest::fail (SELFTEST_LOCATION,
1382 "source_line did not match expected value");
1383
1384 }
1385
1386 /* Tests of lexing. */
1387
1388 /* Verify that token TOK from PARSER has cpp_token_as_text
1389 equal to EXPECTED_TEXT. */
1390
1391 #define ASSERT_TOKEN_AS_TEXT_EQ(PARSER, TOK, EXPECTED_TEXT) \
1392 SELFTEST_BEGIN_STMT \
1393 unsigned char *actual_txt = cpp_token_as_text ((PARSER), (TOK)); \
1394 ASSERT_STREQ ((EXPECTED_TEXT), (const char *)actual_txt); \
1395 SELFTEST_END_STMT
1396
1397 /* Verify that TOK's src_loc is within EXP_FILENAME at EXP_LINENUM,
1398 and ranges from EXP_START_COL to EXP_FINISH_COL.
1399 Use LOC as the effective location of the selftest. */
1400
1401 static void
1402 assert_token_loc_eq (const location &loc,
1403 const cpp_token *tok,
1404 const char *exp_filename, int exp_linenum,
1405 int exp_start_col, int exp_finish_col)
1406 {
1407 location_t tok_loc = tok->src_loc;
1408 ASSERT_STREQ_AT (loc, exp_filename, LOCATION_FILE (tok_loc));
1409 ASSERT_EQ_AT (loc, exp_linenum, LOCATION_LINE (tok_loc));
1410
1411 /* If location_t values are sufficiently high, then column numbers
1412 will be unavailable. */
1413 if (!should_have_column_data_p (tok_loc))
1414 return;
1415
1416 ASSERT_EQ_AT (loc, exp_start_col, LOCATION_COLUMN (tok_loc));
1417 source_range tok_range = get_range_from_loc (line_table, tok_loc);
1418 ASSERT_EQ_AT (loc, exp_start_col, LOCATION_COLUMN (tok_range.m_start));
1419 ASSERT_EQ_AT (loc, exp_finish_col, LOCATION_COLUMN (tok_range.m_finish));
1420 }
1421
1422 /* Use assert_token_loc_eq to verify the TOK->src_loc, using
1423 SELFTEST_LOCATION as the effective location of the selftest. */
1424
1425 #define ASSERT_TOKEN_LOC_EQ(TOK, EXP_FILENAME, EXP_LINENUM, \
1426 EXP_START_COL, EXP_FINISH_COL) \
1427 assert_token_loc_eq (SELFTEST_LOCATION, (TOK), (EXP_FILENAME), \
1428 (EXP_LINENUM), (EXP_START_COL), (EXP_FINISH_COL))
1429
1430 /* Test of lexing a file using libcpp, verifying tokens and their
1431 location information. */
1432
1433 static void
1434 test_lexer (const line_table_case &case_)
1435 {
1436 /* Create a tempfile and write some text to it. */
1437 const char *content =
1438 /*00000000011111111112222222222333333.3333444444444.455555555556
1439 12345678901234567890123456789012345.6789012345678.901234567890. */
1440 ("test_name /* c-style comment */\n"
1441 " \"test literal\"\n"
1442 " // test c++-style comment\n"
1443 " 42\n");
1444 temp_source_file tmp (SELFTEST_LOCATION, ".txt", content);
1445
1446 temp_line_table tmp_lt (case_);
1447
1448 cpp_reader *parser = cpp_create_reader (CLK_GNUC89, NULL, line_table);
1449
1450 const char *fname = cpp_read_main_file (parser, tmp.get_filename ());
1451 ASSERT_NE (fname, NULL);
1452
1453 /* Verify that we get the expected tokens back, with the correct
1454 location information. */
1455
1456 location_t loc;
1457 const cpp_token *tok;
1458 tok = cpp_get_token_with_location (parser, &loc);
1459 ASSERT_NE (tok, NULL);
1460 ASSERT_EQ (tok->type, CPP_NAME);
1461 ASSERT_TOKEN_AS_TEXT_EQ (parser, tok, "test_name");
1462 ASSERT_TOKEN_LOC_EQ (tok, tmp.get_filename (), 1, 1, 9);
1463
1464 tok = cpp_get_token_with_location (parser, &loc);
1465 ASSERT_NE (tok, NULL);
1466 ASSERT_EQ (tok->type, CPP_STRING);
1467 ASSERT_TOKEN_AS_TEXT_EQ (parser, tok, "\"test literal\"");
1468 ASSERT_TOKEN_LOC_EQ (tok, tmp.get_filename (), 2, 35, 48);
1469
1470 tok = cpp_get_token_with_location (parser, &loc);
1471 ASSERT_NE (tok, NULL);
1472 ASSERT_EQ (tok->type, CPP_NUMBER);
1473 ASSERT_TOKEN_AS_TEXT_EQ (parser, tok, "42");
1474 ASSERT_TOKEN_LOC_EQ (tok, tmp.get_filename (), 4, 4, 5);
1475
1476 tok = cpp_get_token_with_location (parser, &loc);
1477 ASSERT_NE (tok, NULL);
1478 ASSERT_EQ (tok->type, CPP_EOF);
1479
1480 cpp_finish (parser, NULL);
1481 cpp_destroy (parser);
1482 }
1483
1484 /* A table of interesting location_t values, giving one axis of our test
1485 matrix. */
1486
1487 static const location_t boundary_locations[] = {
1488 /* Zero means "don't override the default values for a new line_table". */
1489 0,
1490
1491 /* An arbitrary non-zero value that isn't close to one of
1492 the boundary values below. */
1493 0x10000,
1494
1495 /* Values near LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES. */
1496 LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES - 0x100,
1497 LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES - 1,
1498 LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES,
1499 LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES + 1,
1500 LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES + 0x100,
1501
1502 /* Values near LINE_MAP_MAX_LOCATION_WITH_COLS. */
1503 LINE_MAP_MAX_LOCATION_WITH_COLS - 0x100,
1504 LINE_MAP_MAX_LOCATION_WITH_COLS - 1,
1505 LINE_MAP_MAX_LOCATION_WITH_COLS,
1506 LINE_MAP_MAX_LOCATION_WITH_COLS + 1,
1507 LINE_MAP_MAX_LOCATION_WITH_COLS + 0x100,
1508 };
1509
1510 /* Run all of the selftests within this file. */
1511
1512 void
1513 input_c_tests ()
1514 {
1515 test_should_have_column_data_p ();
1516 test_unknown_location ();
1517 test_builtins ();
1518
1519 /* As noted above in the description of struct line_table_case,
1520 we want to explore a test matrix of interesting line_table
1521 situations, running various selftests for each case within the
1522 matrix. */
1523
1524 /* Run all tests with:
1525 (a) line_table->default_range_bits == 0, and
1526 (b) line_table->default_range_bits == 5. */
1527 int num_cases_tested = 0;
1528 for (int default_range_bits = 0; default_range_bits <= 5;
1529 default_range_bits += 5)
1530 {
1531 /* ...and use each of the "interesting" location values as
1532 the starting location within line_table. */
1533 const int num_boundary_locations
1534 = sizeof (boundary_locations) / sizeof (boundary_locations[0]);
1535 for (int loc_idx = 0; loc_idx < num_boundary_locations; loc_idx++)
1536 {
1537 line_table_case c (default_range_bits, boundary_locations[loc_idx]);
1538
1539 /* Run all tests for the given case within the test matrix. */
1540 test_accessing_ordinary_linemaps (c);
1541 test_lexer (c);
1542
1543 num_cases_tested++;
1544 }
1545 }
1546
1547 /* Verify that we fully covered the test matrix. */
1548 ASSERT_EQ (num_cases_tested, 2 * 12);
1549
1550 test_reading_source_line ();
1551 }
1552
1553 } // namespace selftest
1554
1555 #endif /* CHECKING_P */