forked from gcc-mirror/gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline-map.c
2457 lines (2047 loc) · 74.3 KB
/
line-map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Map (unsigned int) keys to (source file, line, column) triples.
Copyright (C) 2001-2020 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding! */
#include "config.h"
#include "system.h"
#include "line-map.h"
#include "cpplib.h"
#include "internal.h"
#include "hashtab.h"
static void trace_include (const line_maps *, const line_map_ordinary *);
static const line_map_ordinary * linemap_ordinary_map_lookup (const line_maps *,
location_t);
static const line_map_macro* linemap_macro_map_lookup (const line_maps *,
location_t);
static location_t linemap_macro_map_loc_to_def_point
(const line_map_macro *, location_t);
static location_t linemap_macro_map_loc_to_exp_point
(const line_map_macro *, location_t);
static location_t linemap_macro_loc_to_spelling_point
(line_maps *, location_t, const line_map_ordinary **);
static location_t linemap_macro_loc_to_def_point (line_maps *,
location_t,
const line_map_ordinary **);
static location_t linemap_macro_loc_to_exp_point (line_maps *,
location_t,
const line_map_ordinary **);
/* Counters defined in macro.c. */
extern unsigned num_expanded_macros_counter;
extern unsigned num_macro_tokens_counter;
/* Destructor for class line_maps.
Ensure non-GC-managed memory is released. */
line_maps::~line_maps ()
{
if (location_adhoc_data_map.htab)
htab_delete (location_adhoc_data_map.htab);
}
/* Hash function for location_adhoc_data hashtable. */
static hashval_t
location_adhoc_data_hash (const void *l)
{
const struct location_adhoc_data *lb =
(const struct location_adhoc_data *) l;
return ((hashval_t) lb->locus
+ (hashval_t) lb->src_range.m_start
+ (hashval_t) lb->src_range.m_finish
+ (size_t) lb->data);
}
/* Compare function for location_adhoc_data hashtable. */
static int
location_adhoc_data_eq (const void *l1, const void *l2)
{
const struct location_adhoc_data *lb1 =
(const struct location_adhoc_data *) l1;
const struct location_adhoc_data *lb2 =
(const struct location_adhoc_data *) l2;
return (lb1->locus == lb2->locus
&& lb1->src_range.m_start == lb2->src_range.m_start
&& lb1->src_range.m_finish == lb2->src_range.m_finish
&& lb1->data == lb2->data);
}
/* Update the hashtable when location_adhoc_data is reallocated. */
static int
location_adhoc_data_update (void **slot, void *data)
{
*((char **) slot)
= (char *) ((uintptr_t) *((char **) slot) + *((ptrdiff_t *) data));
return 1;
}
/* Rebuild the hash table from the location adhoc data. */
void
rebuild_location_adhoc_htab (line_maps *set)
{
unsigned i;
set->location_adhoc_data_map.htab =
htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
htab_find_slot (set->location_adhoc_data_map.htab,
set->location_adhoc_data_map.data + i, INSERT);
}
/* Helper function for get_combined_adhoc_loc.
Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
within a location_t, without needing to use an ad-hoc location. */
static bool
can_be_stored_compactly_p (line_maps *set,
location_t locus,
source_range src_range,
void *data)
{
/* If there's an ad-hoc pointer, we can't store it directly in the
location_t, we need the lookaside. */
if (data)
return false;
/* We only store ranges that begin at the locus and that are sufficiently
"sane". */
if (src_range.m_start != locus)
return false;
if (src_range.m_finish < src_range.m_start)
return false;
if (src_range.m_start < RESERVED_LOCATION_COUNT)
return false;
if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
return false;
/* All 3 locations must be within ordinary maps, typically, the same
ordinary map. */
location_t lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
if (locus >= lowest_macro_loc)
return false;
if (src_range.m_start >= lowest_macro_loc)
return false;
if (src_range.m_finish >= lowest_macro_loc)
return false;
/* Passed all tests. */
return true;
}
/* Combine LOCUS and DATA to a combined adhoc loc. */
location_t
get_combined_adhoc_loc (line_maps *set,
location_t locus,
source_range src_range,
void *data)
{
struct location_adhoc_data lb;
struct location_adhoc_data **slot;
if (IS_ADHOC_LOC (locus))
locus = get_location_from_adhoc_loc (set, locus);
if (locus == 0 && data == NULL)
return 0;
/* Any ordinary locations ought to be "pure" at this point: no
compressed ranges. */
linemap_assert (locus < RESERVED_LOCATION_COUNT
|| locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
|| locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
|| pure_location_p (set, locus));
/* Consider short-range optimization. */
if (can_be_stored_compactly_p (set, locus, src_range, data))
{
/* The low bits ought to be clear. */
linemap_assert (pure_location_p (set, locus));
const line_map *map = linemap_lookup (set, locus);
const line_map_ordinary *ordmap = linemap_check_ordinary (map);
unsigned int int_diff = src_range.m_finish - src_range.m_start;
unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
if (col_diff < (1U << ordmap->m_range_bits))
{
location_t packed = locus | col_diff;
set->num_optimized_ranges++;
return packed;
}
}
/* We can also compactly store locations
when locus == start == finish (and data is NULL). */
if (locus == src_range.m_start
&& locus == src_range.m_finish
&& !data)
return locus;
if (!data)
set->num_unoptimized_ranges++;
lb.locus = locus;
lb.src_range = src_range;
lb.data = data;
slot = (struct location_adhoc_data **)
htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
if (*slot == NULL)
{
if (set->location_adhoc_data_map.curr_loc >=
set->location_adhoc_data_map.allocated)
{
char *orig_data = (char *) set->location_adhoc_data_map.data;
ptrdiff_t offset;
/* Cast away extern "C" from the type of xrealloc. */
line_map_realloc reallocator = (set->reallocator
? set->reallocator
: (line_map_realloc) xrealloc);
if (set->location_adhoc_data_map.allocated == 0)
set->location_adhoc_data_map.allocated = 128;
else
set->location_adhoc_data_map.allocated *= 2;
set->location_adhoc_data_map.data = (struct location_adhoc_data *)
reallocator (set->location_adhoc_data_map.data,
set->location_adhoc_data_map.allocated
* sizeof (struct location_adhoc_data));
offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
if (set->location_adhoc_data_map.allocated > 128)
htab_traverse (set->location_adhoc_data_map.htab,
location_adhoc_data_update, &offset);
}
*slot = set->location_adhoc_data_map.data
+ set->location_adhoc_data_map.curr_loc;
set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
= lb;
}
return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
}
/* Return the data for the adhoc loc. */
void *
get_data_from_adhoc_loc (const class line_maps *set, location_t loc)
{
linemap_assert (IS_ADHOC_LOC (loc));
return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].data;
}
/* Return the location for the adhoc loc. */
location_t
get_location_from_adhoc_loc (const class line_maps *set, location_t loc)
{
linemap_assert (IS_ADHOC_LOC (loc));
return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].locus;
}
/* Return the source_range for adhoc location LOC. */
static source_range
get_range_from_adhoc_loc (const class line_maps *set, location_t loc)
{
linemap_assert (IS_ADHOC_LOC (loc));
return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].src_range;
}
/* Get the source_range of location LOC, either from the ad-hoc
lookaside table, or embedded inside LOC itself. */
source_range
get_range_from_loc (line_maps *set,
location_t loc)
{
if (IS_ADHOC_LOC (loc))
return get_range_from_adhoc_loc (set, loc);
/* For ordinary maps, extract packed range. */
if (loc >= RESERVED_LOCATION_COUNT
&& loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
&& loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
{
const line_map *map = linemap_lookup (set, loc);
const line_map_ordinary *ordmap = linemap_check_ordinary (map);
source_range result;
int offset = loc & ((1 << ordmap->m_range_bits) - 1);
result.m_start = loc - offset;
result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
return result;
}
return source_range::from_location (loc);
}
/* Get whether location LOC is a "pure" location, or
whether it is an ad-hoc location, or embeds range information. */
bool
pure_location_p (line_maps *set, location_t loc)
{
if (IS_ADHOC_LOC (loc))
return false;
const line_map *map = linemap_lookup (set, loc);
if (map == NULL)
return true;
const line_map_ordinary *ordmap = linemap_check_ordinary (map);
if (loc & ((1U << ordmap->m_range_bits) - 1))
return false;
return true;
}
/* Given location LOC within SET, strip away any packed range information
or ad-hoc information. */
location_t
get_pure_location (line_maps *set, location_t loc)
{
if (IS_ADHOC_LOC (loc))
loc = get_location_from_adhoc_loc (set, loc);
if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
return loc;
if (loc < RESERVED_LOCATION_COUNT)
return loc;
const line_map *map = linemap_lookup (set, loc);
const line_map_ordinary *ordmap = linemap_check_ordinary (map);
return loc & ~((1 << ordmap->m_range_bits) - 1);
}
/* Initialize a line map set. */
void
linemap_init (line_maps *set,
location_t builtin_location)
{
#if __GNUC__ == 4 && __GNUC_MINOR__ == 2 && !defined (__clang__)
/* PR33916, needed to fix PR82939. */
memset (set, 0, sizeof (line_maps));
#else
new (set) line_maps();
#endif
/* Set default reallocator (used for initial alloc too). */
set->reallocator = xrealloc;
set->highest_location = RESERVED_LOCATION_COUNT - 1;
set->highest_line = RESERVED_LOCATION_COUNT - 1;
set->location_adhoc_data_map.htab =
htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
set->builtin_location = builtin_location;
}
/* Return the ordinary line map from whence MAP was included. Returns
NULL if MAP was not an include. */
const line_map_ordinary *
linemap_included_from_linemap (line_maps *set, const line_map_ordinary *map)
{
return linemap_ordinary_map_lookup (set, linemap_included_from (map));
}
/* Check for and warn about line_maps entered but not exited. */
void
linemap_check_files_exited (line_maps *set)
{
/* Depending upon whether we are handling preprocessed input or
not, this can be a user error or an ICE. */
for (const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
! MAIN_FILE_P (map);
map = linemap_included_from_linemap (set, map))
fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
ORDINARY_MAP_FILE_NAME (map));
}
/* Create a new line map in the line map set SET, and return it.
REASON is the reason of creating the map. It determines the type
of map created (ordinary or macro map). Note that ordinary maps and
macro maps are allocated in different memory location. */
static struct line_map *
new_linemap (line_maps *set, location_t start_location)
{
bool macro_p = start_location >= LINE_MAP_MAX_LOCATION;
unsigned num_maps_allocated = LINEMAPS_ALLOCATED (set, macro_p);
unsigned num_maps_used = LINEMAPS_USED (set, macro_p);
if (num_maps_used == num_maps_allocated)
{
/* We need more space! */
if (!num_maps_allocated)
num_maps_allocated = 128;
num_maps_allocated *= 2;
size_t size_of_a_map;
void *buffer;
if (macro_p)
{
size_of_a_map = sizeof (line_map_macro);
buffer = set->info_macro.maps;
}
else
{
size_of_a_map = sizeof (line_map_ordinary);
buffer = set->info_ordinary.maps;
}
/* We are going to execute some dance to try to reduce the
overhead of the memory allocator, in case we are using the
ggc-page.c one.
The actual size of memory we are going to get back from the
allocator may well be larger than what we ask for. Use this
hook to find what that size is. */
size_t alloc_size
= set->round_alloc_size (num_maps_allocated * size_of_a_map);
/* Now alloc_size contains the exact memory size we would get if
we have asked for the initial alloc_size amount of memory.
Let's get back to the number of map that amounts to. */
unsigned num_maps = alloc_size / size_of_a_map;
buffer = set->reallocator (buffer, num_maps * size_of_a_map);
memset ((char *)buffer + num_maps_used * size_of_a_map, 0,
(num_maps - num_maps_used) * size_of_a_map);
if (macro_p)
set->info_macro.maps = (line_map_macro *)buffer;
else
set->info_ordinary.maps = (line_map_ordinary *)buffer;
LINEMAPS_ALLOCATED (set, macro_p) = num_maps;
}
line_map *result = (macro_p ? (line_map *)&set->info_macro.maps[num_maps_used]
: (line_map *)&set->info_ordinary.maps[num_maps_used]);
LINEMAPS_USED (set, macro_p)++;
result->start_location = start_location;
return result;
}
/* Add a mapping of logical source line to physical source file and
line number.
The text pointed to by TO_FILE must have a lifetime
at least as long as the final call to lookup_line (). An empty
TO_FILE means standard input. If reason is LC_LEAVE, and
TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
natural values considering the file we are returning to.
FROM_LINE should be monotonic increasing across calls to this
function. A call to this function can relocate the previous set of
maps, so any stored line_map pointers should not be used. */
const struct line_map *
linemap_add (line_maps *set, enum lc_reason reason,
unsigned int sysp, const char *to_file, linenum_type to_line)
{
/* Generate a start_location above the current highest_location.
If possible, make the low range bits be zero. */
location_t start_location = set->highest_location + 1;
unsigned range_bits = 0;
if (start_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
range_bits = set->default_range_bits;
start_location += (1 << range_bits) - 1;
start_location &= ~((1 << range_bits) - 1);
linemap_assert (!LINEMAPS_ORDINARY_USED (set)
|| (start_location
>= MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set))));
/* When we enter the file for the first time reason cannot be
LC_RENAME. */
linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
/* If we are leaving the main file, return a NULL map. */
if (reason == LC_LEAVE
&& MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
&& to_file == NULL)
{
set->depth--;
return NULL;
}
linemap_assert (reason != LC_ENTER_MACRO);
if (start_location >= LINE_MAP_MAX_LOCATION)
/* We ran out of line map space. */
start_location = 0;
line_map_ordinary *map
= linemap_check_ordinary (new_linemap (set, start_location));
map->reason = reason;
if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
to_file = "<stdin>";
if (reason == LC_RENAME_VERBATIM)
reason = LC_RENAME;
const line_map_ordinary *from = NULL;
if (reason == LC_LEAVE)
{
/* When we are just leaving an "included" file, and jump to the next
location inside the "includer" right after the #include
"included", this variable points the map in use right before the
#include "included", inside the same "includer" file. */
linemap_assert (!MAIN_FILE_P (map - 1));
/* (MAP - 1) points to the map we are leaving. The
map from which (MAP - 1) got included should be the map
that comes right before MAP in the same file. */
from = linemap_included_from_linemap (set, map - 1);
/* A TO_FILE of NULL is special - we use the natural values. */
if (to_file == NULL)
{
to_file = ORDINARY_MAP_FILE_NAME (from);
to_line = SOURCE_LINE (from, from[1].start_location);
sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
}
else
linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from),
to_file) == 0);
}
map->sysp = sysp;
map->to_file = to_file;
map->to_line = to_line;
LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
/* Do not store range_bits here. That's readjusted in
linemap_line_start. */
map->m_range_bits = map->m_column_and_range_bits = 0;
set->highest_location = start_location;
set->highest_line = start_location;
set->max_column_hint = 0;
/* This assertion is placed after set->highest_location has
been updated, since the latter affects
linemap_location_from_macro_expansion_p, which ultimately affects
pure_location_p. */
linemap_assert (pure_location_p (set, start_location));
if (reason == LC_ENTER)
{
if (set->depth == 0)
map->included_from = 0;
else
/* The location of the end of the just-closed map. */
map->included_from
= (((map[0].start_location - 1 - map[-1].start_location)
& ~((1 << map[-1].m_column_and_range_bits) - 1))
+ map[-1].start_location);
set->depth++;
if (set->trace_includes)
trace_include (set, map);
}
else if (reason == LC_RENAME)
map->included_from = linemap_included_from (&map[-1]);
else if (reason == LC_LEAVE)
{
set->depth--;
map->included_from = linemap_included_from (from);
}
return map;
}
/* Returns TRUE if the line table set tracks token locations across
macro expansion, FALSE otherwise. */
bool
linemap_tracks_macro_expansion_locs_p (line_maps *set)
{
return LINEMAPS_MACRO_MAPS (set) != NULL;
}
/* Create a macro map. A macro map encodes source locations of tokens
that are part of a macro replacement-list, at a macro expansion
point. See the extensive comments of struct line_map and struct
line_map_macro, in line-map.h.
This map shall be created when the macro is expanded. The map
encodes the source location of the expansion point of the macro as
well as the "original" source location of each token that is part
of the macro replacement-list. If a macro is defined but never
expanded, it has no macro map. SET is the set of maps the macro
map should be part of. MACRO_NODE is the macro which the new macro
map should encode source locations for. EXPANSION is the location
of the expansion point of MACRO. For function-like macros
invocations, it's best to make it point to the closing parenthesis
of the macro, rather than the the location of the first character
of the macro. NUM_TOKENS is the number of tokens that are part of
the replacement-list of MACRO.
Note that when we run out of the integer space available for source
locations, this function returns NULL. In that case, callers of
this function cannot encode {line,column} pairs into locations of
macro tokens anymore. */
const line_map_macro *
linemap_enter_macro (class line_maps *set, struct cpp_hashnode *macro_node,
location_t expansion, unsigned int num_tokens)
{
location_t start_location
= LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
if (start_location < LINE_MAP_MAX_LOCATION)
/* We ran out of macro map space. */
return NULL;
line_map_macro *map = linemap_check_macro (new_linemap (set, start_location));
map->macro = macro_node;
map->n_tokens = num_tokens;
map->macro_locations
= (location_t*) set->reallocator (NULL,
2 * num_tokens
* sizeof (location_t));
map->expansion = expansion;
memset (MACRO_MAP_LOCATIONS (map), 0,
2 * num_tokens * sizeof (location_t));
LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
return map;
}
/* Create and return a virtual location for a token that is part of a
macro expansion-list at a macro expansion point. See the comment
inside struct line_map_macro to see what an expansion-list exactly
is.
A call to this function must come after a call to
linemap_enter_macro.
MAP is the map into which the source location is created. TOKEN_NO
is the index of the token in the macro replacement-list, starting
at number 0.
ORIG_LOC is the location of the token outside of this macro
expansion. If the token comes originally from the macro
definition, it is the locus in the macro definition; otherwise it
is a location in the context of the caller of this macro expansion
(which is a virtual location or a source location if the caller is
itself a macro expansion or not).
ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
either of the token itself or of a macro parameter that it
replaces. */
location_t
linemap_add_macro_token (const line_map_macro *map,
unsigned int token_no,
location_t orig_loc,
location_t orig_parm_replacement_loc)
{
location_t result;
linemap_assert (linemap_macro_expansion_map_p (map));
linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
result = MAP_START_LOCATION (map) + token_no;
return result;
}
/* Return a location_t for the start (i.e. column==0) of
(physical) line TO_LINE in the current source file (as in the
most recent linemap_add). MAX_COLUMN_HINT is the highest column
number we expect to use in this line (but it does not change
the highest_location). */
location_t
linemap_line_start (line_maps *set, linenum_type to_line,
unsigned int max_column_hint)
{
line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
location_t highest = set->highest_location;
location_t r;
linenum_type last_line =
SOURCE_LINE (map, set->highest_line);
int line_delta = to_line - last_line;
bool add_map = false;
linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
if (line_delta < 0
|| (line_delta > 10
&& line_delta * map->m_column_and_range_bits > 1000)
|| (max_column_hint >= (1U << effective_column_bits))
|| (max_column_hint <= 80 && effective_column_bits >= 10)
|| (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
&& map->m_range_bits > 0)
|| (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
&& (set->max_column_hint || highest >= LINE_MAP_MAX_LOCATION)))
add_map = true;
else
max_column_hint = set->max_column_hint;
if (add_map)
{
int column_bits;
int range_bits;
if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
|| highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
{
/* If the column number is ridiculous or we've allocated a huge
number of location_ts, give up on column numbers
(and on packed ranges). */
max_column_hint = 1;
column_bits = 0;
range_bits = 0;
if (highest >= LINE_MAP_MAX_LOCATION)
goto overflowed;
}
else
{
column_bits = 7;
if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
range_bits = set->default_range_bits;
else
range_bits = 0;
while (max_column_hint >= (1U << column_bits))
column_bits++;
max_column_hint = 1U << column_bits;
column_bits += range_bits;
}
/* Allocate the new line_map. However, if the current map only has a
single line we can sometimes just increase its column_bits instead. */
if (line_delta < 0
|| last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
|| SOURCE_COLUMN (map, highest) >= (1U << (column_bits - range_bits))
|| ( /* We can't reuse the map if the line offset is sufficiently
large to cause overflow when computing location_t values. */
(to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
>= (((uint64_t) 1)
<< (CHAR_BIT * sizeof (linenum_type) - column_bits)))
|| range_bits < map->m_range_bits)
map = linemap_check_ordinary
(const_cast <line_map *>
(linemap_add (set, LC_RENAME,
ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
ORDINARY_MAP_FILE_NAME (map),
to_line)));
map->m_column_and_range_bits = column_bits;
map->m_range_bits = range_bits;
r = (MAP_START_LOCATION (map)
+ ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
<< column_bits));
}
else
r = set->highest_line + (line_delta << map->m_column_and_range_bits);
/* Locations of ordinary tokens are always lower than locations of
macro tokens. */
if (r >= LINE_MAP_MAX_LOCATION)
{
overflowed:
/* Remember we overflowed. */
set->highest_line = set->highest_location = LINE_MAP_MAX_LOCATION - 1;
/* No column numbers! */
set->max_column_hint = 1;
return 0;
}
set->highest_line = r;
if (r > set->highest_location)
set->highest_location = r;
set->max_column_hint = max_column_hint;
/* At this point, we expect one of:
(a) the normal case: a "pure" location with 0 range bits, or
(b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
columns anymore (or ranges), or
(c) we're in a region with a column hint exceeding
LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
with column_bits == 0. */
linemap_assert (pure_location_p (set, r)
|| r >= LINE_MAP_MAX_LOCATION_WITH_COLS
|| map->m_column_and_range_bits == 0);
linemap_assert (SOURCE_LINE (map, r) == to_line);
return r;
}
/* Encode and return a location_t from a column number. The
source line considered is the last source line used to call
linemap_line_start, i.e, the last source line which a location was
encoded from. */
location_t
linemap_position_for_column (line_maps *set, unsigned int to_column)
{
location_t r = set->highest_line;
linemap_assert
(!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
if (to_column >= set->max_column_hint)
{
if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
|| to_column > LINE_MAP_MAX_COLUMN_NUMBER)
{
/* Running low on location_ts - disable column numbers. */
return r;
}
else
{
/* Otherwise, attempt to start a new line that can hold TO_COLUMN,
with some space to spare. This may or may not lead to a new
linemap being created. */
line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
map = LINEMAPS_LAST_ORDINARY_MAP (set);
if (map->m_column_and_range_bits == 0)
{
/* ...then the linemap has column-tracking disabled,
presumably due to exceeding either
LINE_MAP_MAX_LOCATION_WITH_COLS (overall) or
LINE_MAP_MAX_COLUMN_NUMBER (within this line).
Return the start of the linemap, which encodes column 0, for
the whole line. */
return r;
}
}
}
line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
r = r + (to_column << map->m_range_bits);
if (r >= set->highest_location)
set->highest_location = r;
return r;
}
/* Encode and return a source location from a given line and
column. */
location_t
linemap_position_for_line_and_column (line_maps *set,
const line_map_ordinary *ord_map,
linenum_type line,
unsigned column)
{
linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
location_t r = MAP_START_LOCATION (ord_map);
r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
<< ord_map->m_column_and_range_bits);
if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
<< ord_map->m_range_bits);
location_t upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
if (r >= upper_limit)
r = upper_limit - 1;
if (r > set->highest_location)
set->highest_location = r;
return r;
}
/* Encode and return a location_t starting from location LOC and
shifting it by COLUMN_OFFSET columns. This function does not support
virtual locations. */
location_t
linemap_position_for_loc_and_offset (line_maps *set,
location_t loc,
unsigned int column_offset)
{
const line_map_ordinary * map = NULL;
if (IS_ADHOC_LOC (loc))
loc = get_location_from_adhoc_loc (set, loc);
/* This function does not support virtual locations yet. */
if (linemap_location_from_macro_expansion_p (set, loc))
return loc;
if (column_offset == 0
/* Adding an offset to a reserved location (like
UNKNOWN_LOCATION for the C/C++ FEs) does not really make
sense. So let's leave the location intact in that case. */
|| loc < RESERVED_LOCATION_COUNT)
return loc;
/* We find the real location and shift it. */
loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
/* The new location (loc + offset) should be higher than the first
location encoded by MAP. This can fail if the line information
is messed up because of line directives (see PR66415). */
if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
return loc;
linenum_type line = SOURCE_LINE (map, loc);
unsigned int column = SOURCE_COLUMN (map, loc);
/* If MAP is not the last line map of its set, then the new location
(loc + offset) should be less than the first location encoded by
the next line map of the set. Otherwise, we try to encode the
location in the next map. */
while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
&& (loc + (column_offset << map->m_range_bits)
>= MAP_START_LOCATION (&map[1])))
{
map = &map[1];
/* If the next map starts in a higher line, we cannot encode the
location there. */
if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
return loc;
}
column += column_offset;
/* Bail out if the column is not representable within the existing
linemap. */
if (column >= (1u << (map->m_column_and_range_bits - map->m_range_bits)))
return loc;
location_t r =
linemap_position_for_line_and_column (set, map, line, column);
if (linemap_assert_fails (r <= set->highest_location)
|| linemap_assert_fails (map == linemap_lookup (set, r)))
return loc;
return r;
}
/* Given a virtual source location yielded by a map (either an
ordinary or a macro map), returns that map. */
const struct line_map*
linemap_lookup (const line_maps *set, location_t line)
{
if (IS_ADHOC_LOC (line))
line = get_location_from_adhoc_loc (set, line);
if (linemap_location_from_macro_expansion_p (set, line))
return linemap_macro_map_lookup (set, line);
return linemap_ordinary_map_lookup (set, line);
}
/* Given a source location yielded by an ordinary map, returns that
map. Since the set is built chronologically, the logical lines are
monotonic increasing, and so the list is sorted and we can use a
binary search. */
static const line_map_ordinary *
linemap_ordinary_map_lookup (const line_maps *set, location_t line)
{
if (IS_ADHOC_LOC (line))
line = get_location_from_adhoc_loc (set, line);
if (set == NULL || line < RESERVED_LOCATION_COUNT)
return NULL;
unsigned mn = LINEMAPS_ORDINARY_CACHE (set);
unsigned mx = LINEMAPS_ORDINARY_USED (set);
const line_map_ordinary *cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
/* We should get a segfault if no line_maps have been added yet. */
if (line >= MAP_START_LOCATION (cached))
{
if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
return cached;
}
else
{
mx = mn;
mn = 0;
}
while (mx - mn > 1)
{
unsigned md = (mn + mx) / 2;
if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
mx = md;
else
mn = md;
}
LINEMAPS_ORDINARY_CACHE (set) = mn;
const line_map_ordinary *result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
linemap_assert (line >= MAP_START_LOCATION (result));
return result;
}
/* Given a source location yielded by a macro map, returns that map.
Since the set is built chronologically, the logical lines are
monotonic decreasing, and so the list is sorted and we can use a
binary search. */
static const line_map_macro *
linemap_macro_map_lookup (const line_maps *set, location_t line)
{
if (IS_ADHOC_LOC (line))
line = get_location_from_adhoc_loc (set, line);