annotate libcpp/line-map.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents: 55
diff changeset
1 /* Map (unsigned int) keys to (source file, line, column) triples.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2001-2020 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify it
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 under the terms of the GNU General Public License as published by the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 Free Software Foundation; either version 3, or (at your option) any
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 later version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 GNU General Public License for more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 along with this program; see the file COPYING3. If not see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 <http://www.gnu.org/licenses/>.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 In other words, you are welcome to use, share and improve this program.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 You are forbidden to forbid anyone else to use, share and improve
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 what you give them. Help stamp out software-hoarding! */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include "config.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 #include "system.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #include "line-map.h"
111
kono
parents: 55
diff changeset
25 #include "cpplib.h"
kono
parents: 55
diff changeset
26 #include "internal.h"
kono
parents: 55
diff changeset
27 #include "hashtab.h"
kono
parents: 55
diff changeset
28
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
29 static void trace_include (const line_maps *, const line_map_ordinary *);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
30 static const line_map_ordinary * linemap_ordinary_map_lookup (const line_maps *,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
31 location_t);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
32 static const line_map_macro* linemap_macro_map_lookup (const line_maps *,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
33 location_t);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
34 static location_t linemap_macro_map_loc_to_def_point
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
35 (const line_map_macro *, location_t);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
36 static location_t linemap_macro_map_loc_to_exp_point
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
37 (const line_map_macro *, location_t);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
38 static location_t linemap_macro_loc_to_spelling_point
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
39 (line_maps *, location_t, const line_map_ordinary **);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
40 static location_t linemap_macro_loc_to_def_point (line_maps *,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
41 location_t,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
42 const line_map_ordinary **);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
43 static location_t linemap_macro_loc_to_exp_point (line_maps *,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
44 location_t,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
45 const line_map_ordinary **);
111
kono
parents: 55
diff changeset
46
kono
parents: 55
diff changeset
47 /* Counters defined in macro.c. */
kono
parents: 55
diff changeset
48 extern unsigned num_expanded_macros_counter;
kono
parents: 55
diff changeset
49 extern unsigned num_macro_tokens_counter;
kono
parents: 55
diff changeset
50
kono
parents: 55
diff changeset
51 /* Destructor for class line_maps.
kono
parents: 55
diff changeset
52 Ensure non-GC-managed memory is released. */
kono
parents: 55
diff changeset
53
kono
parents: 55
diff changeset
54 line_maps::~line_maps ()
kono
parents: 55
diff changeset
55 {
kono
parents: 55
diff changeset
56 if (location_adhoc_data_map.htab)
kono
parents: 55
diff changeset
57 htab_delete (location_adhoc_data_map.htab);
kono
parents: 55
diff changeset
58 }
kono
parents: 55
diff changeset
59
kono
parents: 55
diff changeset
60 /* Hash function for location_adhoc_data hashtable. */
kono
parents: 55
diff changeset
61
kono
parents: 55
diff changeset
62 static hashval_t
kono
parents: 55
diff changeset
63 location_adhoc_data_hash (const void *l)
kono
parents: 55
diff changeset
64 {
kono
parents: 55
diff changeset
65 const struct location_adhoc_data *lb =
kono
parents: 55
diff changeset
66 (const struct location_adhoc_data *) l;
kono
parents: 55
diff changeset
67 return ((hashval_t) lb->locus
kono
parents: 55
diff changeset
68 + (hashval_t) lb->src_range.m_start
kono
parents: 55
diff changeset
69 + (hashval_t) lb->src_range.m_finish
kono
parents: 55
diff changeset
70 + (size_t) lb->data);
kono
parents: 55
diff changeset
71 }
kono
parents: 55
diff changeset
72
kono
parents: 55
diff changeset
73 /* Compare function for location_adhoc_data hashtable. */
kono
parents: 55
diff changeset
74
kono
parents: 55
diff changeset
75 static int
kono
parents: 55
diff changeset
76 location_adhoc_data_eq (const void *l1, const void *l2)
kono
parents: 55
diff changeset
77 {
kono
parents: 55
diff changeset
78 const struct location_adhoc_data *lb1 =
kono
parents: 55
diff changeset
79 (const struct location_adhoc_data *) l1;
kono
parents: 55
diff changeset
80 const struct location_adhoc_data *lb2 =
kono
parents: 55
diff changeset
81 (const struct location_adhoc_data *) l2;
kono
parents: 55
diff changeset
82 return (lb1->locus == lb2->locus
kono
parents: 55
diff changeset
83 && lb1->src_range.m_start == lb2->src_range.m_start
kono
parents: 55
diff changeset
84 && lb1->src_range.m_finish == lb2->src_range.m_finish
kono
parents: 55
diff changeset
85 && lb1->data == lb2->data);
kono
parents: 55
diff changeset
86 }
kono
parents: 55
diff changeset
87
kono
parents: 55
diff changeset
88 /* Update the hashtable when location_adhoc_data is reallocated. */
kono
parents: 55
diff changeset
89
kono
parents: 55
diff changeset
90 static int
kono
parents: 55
diff changeset
91 location_adhoc_data_update (void **slot, void *data)
kono
parents: 55
diff changeset
92 {
kono
parents: 55
diff changeset
93 *((char **) slot)
kono
parents: 55
diff changeset
94 = (char *) ((uintptr_t) *((char **) slot) + *((ptrdiff_t *) data));
kono
parents: 55
diff changeset
95 return 1;
kono
parents: 55
diff changeset
96 }
kono
parents: 55
diff changeset
97
kono
parents: 55
diff changeset
98 /* Rebuild the hash table from the location adhoc data. */
kono
parents: 55
diff changeset
99
kono
parents: 55
diff changeset
100 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
101 rebuild_location_adhoc_htab (line_maps *set)
111
kono
parents: 55
diff changeset
102 {
kono
parents: 55
diff changeset
103 unsigned i;
kono
parents: 55
diff changeset
104 set->location_adhoc_data_map.htab =
kono
parents: 55
diff changeset
105 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
kono
parents: 55
diff changeset
106 for (i = 0; i < set->location_adhoc_data_map.curr_loc; i++)
kono
parents: 55
diff changeset
107 htab_find_slot (set->location_adhoc_data_map.htab,
kono
parents: 55
diff changeset
108 set->location_adhoc_data_map.data + i, INSERT);
kono
parents: 55
diff changeset
109 }
kono
parents: 55
diff changeset
110
kono
parents: 55
diff changeset
111 /* Helper function for get_combined_adhoc_loc.
kono
parents: 55
diff changeset
112 Can the given LOCUS + SRC_RANGE and DATA pointer be stored compactly
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
113 within a location_t, without needing to use an ad-hoc location. */
111
kono
parents: 55
diff changeset
114
kono
parents: 55
diff changeset
115 static bool
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
116 can_be_stored_compactly_p (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
117 location_t locus,
111
kono
parents: 55
diff changeset
118 source_range src_range,
kono
parents: 55
diff changeset
119 void *data)
kono
parents: 55
diff changeset
120 {
kono
parents: 55
diff changeset
121 /* If there's an ad-hoc pointer, we can't store it directly in the
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
122 location_t, we need the lookaside. */
111
kono
parents: 55
diff changeset
123 if (data)
kono
parents: 55
diff changeset
124 return false;
kono
parents: 55
diff changeset
125
kono
parents: 55
diff changeset
126 /* We only store ranges that begin at the locus and that are sufficiently
kono
parents: 55
diff changeset
127 "sane". */
kono
parents: 55
diff changeset
128 if (src_range.m_start != locus)
kono
parents: 55
diff changeset
129 return false;
kono
parents: 55
diff changeset
130
kono
parents: 55
diff changeset
131 if (src_range.m_finish < src_range.m_start)
kono
parents: 55
diff changeset
132 return false;
kono
parents: 55
diff changeset
133
kono
parents: 55
diff changeset
134 if (src_range.m_start < RESERVED_LOCATION_COUNT)
kono
parents: 55
diff changeset
135 return false;
kono
parents: 55
diff changeset
136
kono
parents: 55
diff changeset
137 if (locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
kono
parents: 55
diff changeset
138 return false;
kono
parents: 55
diff changeset
139
kono
parents: 55
diff changeset
140 /* All 3 locations must be within ordinary maps, typically, the same
kono
parents: 55
diff changeset
141 ordinary map. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
142 location_t lowest_macro_loc = LINEMAPS_MACRO_LOWEST_LOCATION (set);
111
kono
parents: 55
diff changeset
143 if (locus >= lowest_macro_loc)
kono
parents: 55
diff changeset
144 return false;
kono
parents: 55
diff changeset
145 if (src_range.m_start >= lowest_macro_loc)
kono
parents: 55
diff changeset
146 return false;
kono
parents: 55
diff changeset
147 if (src_range.m_finish >= lowest_macro_loc)
kono
parents: 55
diff changeset
148 return false;
kono
parents: 55
diff changeset
149
kono
parents: 55
diff changeset
150 /* Passed all tests. */
kono
parents: 55
diff changeset
151 return true;
kono
parents: 55
diff changeset
152 }
kono
parents: 55
diff changeset
153
kono
parents: 55
diff changeset
154 /* Combine LOCUS and DATA to a combined adhoc loc. */
kono
parents: 55
diff changeset
155
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
156 location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
157 get_combined_adhoc_loc (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
158 location_t locus,
111
kono
parents: 55
diff changeset
159 source_range src_range,
kono
parents: 55
diff changeset
160 void *data)
kono
parents: 55
diff changeset
161 {
kono
parents: 55
diff changeset
162 struct location_adhoc_data lb;
kono
parents: 55
diff changeset
163 struct location_adhoc_data **slot;
kono
parents: 55
diff changeset
164
kono
parents: 55
diff changeset
165 if (IS_ADHOC_LOC (locus))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
166 locus = get_location_from_adhoc_loc (set, locus);
111
kono
parents: 55
diff changeset
167 if (locus == 0 && data == NULL)
kono
parents: 55
diff changeset
168 return 0;
kono
parents: 55
diff changeset
169
kono
parents: 55
diff changeset
170 /* Any ordinary locations ought to be "pure" at this point: no
kono
parents: 55
diff changeset
171 compressed ranges. */
kono
parents: 55
diff changeset
172 linemap_assert (locus < RESERVED_LOCATION_COUNT
kono
parents: 55
diff changeset
173 || locus >= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
kono
parents: 55
diff changeset
174 || locus >= LINEMAPS_MACRO_LOWEST_LOCATION (set)
kono
parents: 55
diff changeset
175 || pure_location_p (set, locus));
kono
parents: 55
diff changeset
176
kono
parents: 55
diff changeset
177 /* Consider short-range optimization. */
kono
parents: 55
diff changeset
178 if (can_be_stored_compactly_p (set, locus, src_range, data))
kono
parents: 55
diff changeset
179 {
kono
parents: 55
diff changeset
180 /* The low bits ought to be clear. */
kono
parents: 55
diff changeset
181 linemap_assert (pure_location_p (set, locus));
kono
parents: 55
diff changeset
182 const line_map *map = linemap_lookup (set, locus);
kono
parents: 55
diff changeset
183 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
kono
parents: 55
diff changeset
184 unsigned int int_diff = src_range.m_finish - src_range.m_start;
kono
parents: 55
diff changeset
185 unsigned int col_diff = (int_diff >> ordmap->m_range_bits);
kono
parents: 55
diff changeset
186 if (col_diff < (1U << ordmap->m_range_bits))
kono
parents: 55
diff changeset
187 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
188 location_t packed = locus | col_diff;
111
kono
parents: 55
diff changeset
189 set->num_optimized_ranges++;
kono
parents: 55
diff changeset
190 return packed;
kono
parents: 55
diff changeset
191 }
kono
parents: 55
diff changeset
192 }
kono
parents: 55
diff changeset
193
kono
parents: 55
diff changeset
194 /* We can also compactly store locations
kono
parents: 55
diff changeset
195 when locus == start == finish (and data is NULL). */
kono
parents: 55
diff changeset
196 if (locus == src_range.m_start
kono
parents: 55
diff changeset
197 && locus == src_range.m_finish
kono
parents: 55
diff changeset
198 && !data)
kono
parents: 55
diff changeset
199 return locus;
kono
parents: 55
diff changeset
200
kono
parents: 55
diff changeset
201 if (!data)
kono
parents: 55
diff changeset
202 set->num_unoptimized_ranges++;
kono
parents: 55
diff changeset
203
kono
parents: 55
diff changeset
204 lb.locus = locus;
kono
parents: 55
diff changeset
205 lb.src_range = src_range;
kono
parents: 55
diff changeset
206 lb.data = data;
kono
parents: 55
diff changeset
207 slot = (struct location_adhoc_data **)
kono
parents: 55
diff changeset
208 htab_find_slot (set->location_adhoc_data_map.htab, &lb, INSERT);
kono
parents: 55
diff changeset
209 if (*slot == NULL)
kono
parents: 55
diff changeset
210 {
kono
parents: 55
diff changeset
211 if (set->location_adhoc_data_map.curr_loc >=
kono
parents: 55
diff changeset
212 set->location_adhoc_data_map.allocated)
kono
parents: 55
diff changeset
213 {
kono
parents: 55
diff changeset
214 char *orig_data = (char *) set->location_adhoc_data_map.data;
kono
parents: 55
diff changeset
215 ptrdiff_t offset;
kono
parents: 55
diff changeset
216 /* Cast away extern "C" from the type of xrealloc. */
kono
parents: 55
diff changeset
217 line_map_realloc reallocator = (set->reallocator
kono
parents: 55
diff changeset
218 ? set->reallocator
kono
parents: 55
diff changeset
219 : (line_map_realloc) xrealloc);
kono
parents: 55
diff changeset
220
kono
parents: 55
diff changeset
221 if (set->location_adhoc_data_map.allocated == 0)
kono
parents: 55
diff changeset
222 set->location_adhoc_data_map.allocated = 128;
kono
parents: 55
diff changeset
223 else
kono
parents: 55
diff changeset
224 set->location_adhoc_data_map.allocated *= 2;
kono
parents: 55
diff changeset
225 set->location_adhoc_data_map.data = (struct location_adhoc_data *)
kono
parents: 55
diff changeset
226 reallocator (set->location_adhoc_data_map.data,
kono
parents: 55
diff changeset
227 set->location_adhoc_data_map.allocated
kono
parents: 55
diff changeset
228 * sizeof (struct location_adhoc_data));
kono
parents: 55
diff changeset
229 offset = (char *) (set->location_adhoc_data_map.data) - orig_data;
kono
parents: 55
diff changeset
230 if (set->location_adhoc_data_map.allocated > 128)
kono
parents: 55
diff changeset
231 htab_traverse (set->location_adhoc_data_map.htab,
kono
parents: 55
diff changeset
232 location_adhoc_data_update, &offset);
kono
parents: 55
diff changeset
233 }
kono
parents: 55
diff changeset
234 *slot = set->location_adhoc_data_map.data
kono
parents: 55
diff changeset
235 + set->location_adhoc_data_map.curr_loc;
kono
parents: 55
diff changeset
236 set->location_adhoc_data_map.data[set->location_adhoc_data_map.curr_loc++]
kono
parents: 55
diff changeset
237 = lb;
kono
parents: 55
diff changeset
238 }
kono
parents: 55
diff changeset
239 return ((*slot) - set->location_adhoc_data_map.data) | 0x80000000;
kono
parents: 55
diff changeset
240 }
kono
parents: 55
diff changeset
241
kono
parents: 55
diff changeset
242 /* Return the data for the adhoc loc. */
kono
parents: 55
diff changeset
243
kono
parents: 55
diff changeset
244 void *
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
245 get_data_from_adhoc_loc (const class line_maps *set, location_t loc)
111
kono
parents: 55
diff changeset
246 {
kono
parents: 55
diff changeset
247 linemap_assert (IS_ADHOC_LOC (loc));
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
248 return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].data;
111
kono
parents: 55
diff changeset
249 }
kono
parents: 55
diff changeset
250
kono
parents: 55
diff changeset
251 /* Return the location for the adhoc loc. */
kono
parents: 55
diff changeset
252
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
253 location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
254 get_location_from_adhoc_loc (const class line_maps *set, location_t loc)
111
kono
parents: 55
diff changeset
255 {
kono
parents: 55
diff changeset
256 linemap_assert (IS_ADHOC_LOC (loc));
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
257 return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].locus;
111
kono
parents: 55
diff changeset
258 }
kono
parents: 55
diff changeset
259
kono
parents: 55
diff changeset
260 /* Return the source_range for adhoc location LOC. */
kono
parents: 55
diff changeset
261
kono
parents: 55
diff changeset
262 static source_range
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
263 get_range_from_adhoc_loc (const class line_maps *set, location_t loc)
111
kono
parents: 55
diff changeset
264 {
kono
parents: 55
diff changeset
265 linemap_assert (IS_ADHOC_LOC (loc));
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
266 return set->location_adhoc_data_map.data[loc & MAX_LOCATION_T].src_range;
111
kono
parents: 55
diff changeset
267 }
kono
parents: 55
diff changeset
268
kono
parents: 55
diff changeset
269 /* Get the source_range of location LOC, either from the ad-hoc
kono
parents: 55
diff changeset
270 lookaside table, or embedded inside LOC itself. */
kono
parents: 55
diff changeset
271
kono
parents: 55
diff changeset
272 source_range
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
273 get_range_from_loc (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
274 location_t loc)
111
kono
parents: 55
diff changeset
275 {
kono
parents: 55
diff changeset
276 if (IS_ADHOC_LOC (loc))
kono
parents: 55
diff changeset
277 return get_range_from_adhoc_loc (set, loc);
kono
parents: 55
diff changeset
278
kono
parents: 55
diff changeset
279 /* For ordinary maps, extract packed range. */
kono
parents: 55
diff changeset
280 if (loc >= RESERVED_LOCATION_COUNT
kono
parents: 55
diff changeset
281 && loc < LINEMAPS_MACRO_LOWEST_LOCATION (set)
kono
parents: 55
diff changeset
282 && loc <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
kono
parents: 55
diff changeset
283 {
kono
parents: 55
diff changeset
284 const line_map *map = linemap_lookup (set, loc);
kono
parents: 55
diff changeset
285 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
kono
parents: 55
diff changeset
286 source_range result;
kono
parents: 55
diff changeset
287 int offset = loc & ((1 << ordmap->m_range_bits) - 1);
kono
parents: 55
diff changeset
288 result.m_start = loc - offset;
kono
parents: 55
diff changeset
289 result.m_finish = result.m_start + (offset << ordmap->m_range_bits);
kono
parents: 55
diff changeset
290 return result;
kono
parents: 55
diff changeset
291 }
kono
parents: 55
diff changeset
292
kono
parents: 55
diff changeset
293 return source_range::from_location (loc);
kono
parents: 55
diff changeset
294 }
kono
parents: 55
diff changeset
295
kono
parents: 55
diff changeset
296 /* Get whether location LOC is a "pure" location, or
kono
parents: 55
diff changeset
297 whether it is an ad-hoc location, or embeds range information. */
kono
parents: 55
diff changeset
298
kono
parents: 55
diff changeset
299 bool
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
300 pure_location_p (line_maps *set, location_t loc)
111
kono
parents: 55
diff changeset
301 {
kono
parents: 55
diff changeset
302 if (IS_ADHOC_LOC (loc))
kono
parents: 55
diff changeset
303 return false;
kono
parents: 55
diff changeset
304
kono
parents: 55
diff changeset
305 const line_map *map = linemap_lookup (set, loc);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
306 if (map == NULL)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
307 return true;
111
kono
parents: 55
diff changeset
308 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
kono
parents: 55
diff changeset
309
kono
parents: 55
diff changeset
310 if (loc & ((1U << ordmap->m_range_bits) - 1))
kono
parents: 55
diff changeset
311 return false;
kono
parents: 55
diff changeset
312
kono
parents: 55
diff changeset
313 return true;
kono
parents: 55
diff changeset
314 }
kono
parents: 55
diff changeset
315
kono
parents: 55
diff changeset
316 /* Given location LOC within SET, strip away any packed range information
kono
parents: 55
diff changeset
317 or ad-hoc information. */
kono
parents: 55
diff changeset
318
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
319 location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
320 get_pure_location (line_maps *set, location_t loc)
111
kono
parents: 55
diff changeset
321 {
kono
parents: 55
diff changeset
322 if (IS_ADHOC_LOC (loc))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
323 loc = get_location_from_adhoc_loc (set, loc);
111
kono
parents: 55
diff changeset
324
kono
parents: 55
diff changeset
325 if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
kono
parents: 55
diff changeset
326 return loc;
kono
parents: 55
diff changeset
327
kono
parents: 55
diff changeset
328 if (loc < RESERVED_LOCATION_COUNT)
kono
parents: 55
diff changeset
329 return loc;
kono
parents: 55
diff changeset
330
kono
parents: 55
diff changeset
331 const line_map *map = linemap_lookup (set, loc);
kono
parents: 55
diff changeset
332 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
kono
parents: 55
diff changeset
333
kono
parents: 55
diff changeset
334 return loc & ~((1 << ordmap->m_range_bits) - 1);
kono
parents: 55
diff changeset
335 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
336
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 /* Initialize a line map set. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
338
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
340 linemap_init (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
341 location_t builtin_location)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
343 #if __GNUC__ == 4 && __GNUC_MINOR__ == 2 && !defined (__clang__)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
344 /* PR33916, needed to fix PR82939. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
345 memset (set, 0, sizeof (line_maps));
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
346 #else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
347 new (set) line_maps();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
348 #endif
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
349 /* Set default reallocator (used for initial alloc too). */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
350 set->reallocator = xrealloc;
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
351 set->highest_location = RESERVED_LOCATION_COUNT - 1;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
352 set->highest_line = RESERVED_LOCATION_COUNT - 1;
111
kono
parents: 55
diff changeset
353 set->location_adhoc_data_map.htab =
kono
parents: 55
diff changeset
354 htab_create (100, location_adhoc_data_hash, location_adhoc_data_eq, NULL);
kono
parents: 55
diff changeset
355 set->builtin_location = builtin_location;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
356 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
357
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
358 /* Return the ordinary line map from whence MAP was included. Returns
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
359 NULL if MAP was not an include. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
360
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
361 const line_map_ordinary *
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
362 linemap_included_from_linemap (line_maps *set, const line_map_ordinary *map)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
363 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
364 return linemap_ordinary_map_lookup (set, linemap_included_from (map));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
365 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
366
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
367 /* Check for and warn about line_maps entered but not exited. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
368
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
369 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
370 linemap_check_files_exited (line_maps *set)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
371 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 /* Depending upon whether we are handling preprocessed input or
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
373 not, this can be a user error or an ICE. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
374 for (const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
111
kono
parents: 55
diff changeset
375 ! MAIN_FILE_P (map);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
376 map = linemap_included_from_linemap (set, map))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
377 fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
111
kono
parents: 55
diff changeset
378 ORDINARY_MAP_FILE_NAME (map));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 }
111
kono
parents: 55
diff changeset
380
kono
parents: 55
diff changeset
381 /* Create a new line map in the line map set SET, and return it.
kono
parents: 55
diff changeset
382 REASON is the reason of creating the map. It determines the type
kono
parents: 55
diff changeset
383 of map created (ordinary or macro map). Note that ordinary maps and
kono
parents: 55
diff changeset
384 macro maps are allocated in different memory location. */
kono
parents: 55
diff changeset
385
kono
parents: 55
diff changeset
386 static struct line_map *
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
387 new_linemap (line_maps *set, location_t start_location)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
388 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
389 bool macro_p = start_location >= LINE_MAP_MAX_LOCATION;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
390 unsigned num_maps_allocated = LINEMAPS_ALLOCATED (set, macro_p);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
391 unsigned num_maps_used = LINEMAPS_USED (set, macro_p);
111
kono
parents: 55
diff changeset
392
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
393 if (num_maps_used == num_maps_allocated)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
394 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
395 /* We need more space! */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
396 if (!num_maps_allocated)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
397 num_maps_allocated = 128;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
398 num_maps_allocated *= 2;
111
kono
parents: 55
diff changeset
399
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
400 size_t size_of_a_map;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
401 void *buffer;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
402 if (macro_p)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
403 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
404 size_of_a_map = sizeof (line_map_macro);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
405 buffer = set->info_macro.maps;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
406 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
407 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
408 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
409 size_of_a_map = sizeof (line_map_ordinary);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
410 buffer = set->info_ordinary.maps;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
411 }
111
kono
parents: 55
diff changeset
412
kono
parents: 55
diff changeset
413 /* We are going to execute some dance to try to reduce the
kono
parents: 55
diff changeset
414 overhead of the memory allocator, in case we are using the
kono
parents: 55
diff changeset
415 ggc-page.c one.
kono
parents: 55
diff changeset
416
kono
parents: 55
diff changeset
417 The actual size of memory we are going to get back from the
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
418 allocator may well be larger than what we ask for. Use this
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
419 hook to find what that size is. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
420 size_t alloc_size
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
421 = set->round_alloc_size (num_maps_allocated * size_of_a_map);
111
kono
parents: 55
diff changeset
422
kono
parents: 55
diff changeset
423 /* Now alloc_size contains the exact memory size we would get if
kono
parents: 55
diff changeset
424 we have asked for the initial alloc_size amount of memory.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
425 Let's get back to the number of map that amounts to. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
426 unsigned num_maps = alloc_size / size_of_a_map;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
427 buffer = set->reallocator (buffer, num_maps * size_of_a_map);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
428 memset ((char *)buffer + num_maps_used * size_of_a_map, 0,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
429 (num_maps - num_maps_used) * size_of_a_map);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
430 if (macro_p)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
431 set->info_macro.maps = (line_map_macro *)buffer;
111
kono
parents: 55
diff changeset
432 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
433 set->info_ordinary.maps = (line_map_ordinary *)buffer;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
434 LINEMAPS_ALLOCATED (set, macro_p) = num_maps;
111
kono
parents: 55
diff changeset
435 }
kono
parents: 55
diff changeset
436
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
437 line_map *result = (macro_p ? (line_map *)&set->info_macro.maps[num_maps_used]
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
438 : (line_map *)&set->info_ordinary.maps[num_maps_used]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
439 LINEMAPS_USED (set, macro_p)++;
111
kono
parents: 55
diff changeset
440
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
441 result->start_location = start_location;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
442
111
kono
parents: 55
diff changeset
443 return result;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
444 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
445
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
446 /* Add a mapping of logical source line to physical source file and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
447 line number.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
448
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
449 The text pointed to by TO_FILE must have a lifetime
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
450 at least as long as the final call to lookup_line (). An empty
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
451 TO_FILE means standard input. If reason is LC_LEAVE, and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
452 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
453 natural values considering the file we are returning to.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
454
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
455 FROM_LINE should be monotonic increasing across calls to this
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
456 function. A call to this function can relocate the previous set of
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
457 maps, so any stored line_map pointers should not be used. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
458
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
459 const struct line_map *
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
460 linemap_add (line_maps *set, enum lc_reason reason,
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
461 unsigned int sysp, const char *to_file, linenum_type to_line)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
462 {
111
kono
parents: 55
diff changeset
463 /* Generate a start_location above the current highest_location.
kono
parents: 55
diff changeset
464 If possible, make the low range bits be zero. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
465 location_t start_location;
111
kono
parents: 55
diff changeset
466 if (set->highest_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
467 {
111
kono
parents: 55
diff changeset
468 start_location = set->highest_location + (1 << set->default_range_bits);
kono
parents: 55
diff changeset
469 if (set->default_range_bits)
kono
parents: 55
diff changeset
470 start_location &= ~((1 << set->default_range_bits) - 1);
kono
parents: 55
diff changeset
471 linemap_assert (0 == (start_location
kono
parents: 55
diff changeset
472 & ((1 << set->default_range_bits) - 1)));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
473 }
111
kono
parents: 55
diff changeset
474 else
kono
parents: 55
diff changeset
475 start_location = set->highest_location + 1;
kono
parents: 55
diff changeset
476
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
477 linemap_assert (!LINEMAPS_ORDINARY_USED (set)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
478 || (start_location
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
479 >= MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set))));
111
kono
parents: 55
diff changeset
480
kono
parents: 55
diff changeset
481 /* When we enter the file for the first time reason cannot be
kono
parents: 55
diff changeset
482 LC_RENAME. */
kono
parents: 55
diff changeset
483 linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
kono
parents: 55
diff changeset
484
kono
parents: 55
diff changeset
485 /* If we are leaving the main file, return a NULL map. */
kono
parents: 55
diff changeset
486 if (reason == LC_LEAVE
kono
parents: 55
diff changeset
487 && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
kono
parents: 55
diff changeset
488 && to_file == NULL)
kono
parents: 55
diff changeset
489 {
kono
parents: 55
diff changeset
490 set->depth--;
kono
parents: 55
diff changeset
491 return NULL;
kono
parents: 55
diff changeset
492 }
kono
parents: 55
diff changeset
493
kono
parents: 55
diff changeset
494 linemap_assert (reason != LC_ENTER_MACRO);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
495
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
496 if (start_location >= LINE_MAP_MAX_LOCATION)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
497 /* We ran out of line map space. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
498 start_location = 0;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
499
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
500 line_map_ordinary *map
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
501 = linemap_check_ordinary (new_linemap (set, start_location));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
502 map->reason = reason;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
503
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
504 if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
505 to_file = "<stdin>";
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
506
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
507 if (reason == LC_RENAME_VERBATIM)
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
508 reason = LC_RENAME;
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
509
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
510 const line_map_ordinary *from = NULL;
111
kono
parents: 55
diff changeset
511 if (reason == LC_LEAVE)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
512 {
111
kono
parents: 55
diff changeset
513 /* When we are just leaving an "included" file, and jump to the next
kono
parents: 55
diff changeset
514 location inside the "includer" right after the #include
kono
parents: 55
diff changeset
515 "included", this variable points the map in use right before the
kono
parents: 55
diff changeset
516 #include "included", inside the same "includer" file. */
kono
parents: 55
diff changeset
517
kono
parents: 55
diff changeset
518 linemap_assert (!MAIN_FILE_P (map - 1));
kono
parents: 55
diff changeset
519 /* (MAP - 1) points to the map we are leaving. The
kono
parents: 55
diff changeset
520 map from which (MAP - 1) got included should be the map
kono
parents: 55
diff changeset
521 that comes right before MAP in the same file. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
522 from = linemap_included_from_linemap (set, map - 1);
111
kono
parents: 55
diff changeset
523
kono
parents: 55
diff changeset
524 /* A TO_FILE of NULL is special - we use the natural values. */
kono
parents: 55
diff changeset
525 if (to_file == NULL)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
526 {
111
kono
parents: 55
diff changeset
527 to_file = ORDINARY_MAP_FILE_NAME (from);
kono
parents: 55
diff changeset
528 to_line = SOURCE_LINE (from, from[1].start_location);
kono
parents: 55
diff changeset
529 sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
530 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
531 else
111
kono
parents: 55
diff changeset
532 linemap_assert (filename_cmp (ORDINARY_MAP_FILE_NAME (from),
kono
parents: 55
diff changeset
533 to_file) == 0);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
534 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
535
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
536 map->sysp = sysp;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
537 map->to_file = to_file;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
538 map->to_line = to_line;
111
kono
parents: 55
diff changeset
539 LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
kono
parents: 55
diff changeset
540 map->m_column_and_range_bits = 0;
kono
parents: 55
diff changeset
541 map->m_range_bits = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
542 set->highest_location = start_location;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
543 set->highest_line = start_location;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
544 set->max_column_hint = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
545
111
kono
parents: 55
diff changeset
546 /* This assertion is placed after set->highest_location has
kono
parents: 55
diff changeset
547 been updated, since the latter affects
kono
parents: 55
diff changeset
548 linemap_location_from_macro_expansion_p, which ultimately affects
kono
parents: 55
diff changeset
549 pure_location_p. */
kono
parents: 55
diff changeset
550 linemap_assert (pure_location_p (set, start_location));
kono
parents: 55
diff changeset
551
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
552 if (reason == LC_ENTER)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
553 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
554 if (set->depth == 0)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
555 map->included_from = 0;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
556 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
557 /* The location of the end of the just-closed map. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
558 map->included_from
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
559 = (((map[0].start_location - 1 - map[-1].start_location)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
560 & ~((1 << map[-1].m_column_and_range_bits) - 1))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
561 + map[-1].start_location);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
562 set->depth++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
563 if (set->trace_includes)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
564 trace_include (set, map);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
565 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
566 else if (reason == LC_RENAME)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
567 map->included_from = linemap_included_from (&map[-1]);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
568 else if (reason == LC_LEAVE)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
569 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
570 set->depth--;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
571 map->included_from = linemap_included_from (from);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
572 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
573
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
574 return map;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
575 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
576
111
kono
parents: 55
diff changeset
577 /* Returns TRUE if the line table set tracks token locations across
kono
parents: 55
diff changeset
578 macro expansion, FALSE otherwise. */
kono
parents: 55
diff changeset
579
kono
parents: 55
diff changeset
580 bool
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
581 linemap_tracks_macro_expansion_locs_p (line_maps *set)
111
kono
parents: 55
diff changeset
582 {
kono
parents: 55
diff changeset
583 return LINEMAPS_MACRO_MAPS (set) != NULL;
kono
parents: 55
diff changeset
584 }
kono
parents: 55
diff changeset
585
kono
parents: 55
diff changeset
586 /* Create a macro map. A macro map encodes source locations of tokens
kono
parents: 55
diff changeset
587 that are part of a macro replacement-list, at a macro expansion
kono
parents: 55
diff changeset
588 point. See the extensive comments of struct line_map and struct
kono
parents: 55
diff changeset
589 line_map_macro, in line-map.h.
kono
parents: 55
diff changeset
590
kono
parents: 55
diff changeset
591 This map shall be created when the macro is expanded. The map
kono
parents: 55
diff changeset
592 encodes the source location of the expansion point of the macro as
kono
parents: 55
diff changeset
593 well as the "original" source location of each token that is part
kono
parents: 55
diff changeset
594 of the macro replacement-list. If a macro is defined but never
kono
parents: 55
diff changeset
595 expanded, it has no macro map. SET is the set of maps the macro
kono
parents: 55
diff changeset
596 map should be part of. MACRO_NODE is the macro which the new macro
kono
parents: 55
diff changeset
597 map should encode source locations for. EXPANSION is the location
kono
parents: 55
diff changeset
598 of the expansion point of MACRO. For function-like macros
kono
parents: 55
diff changeset
599 invocations, it's best to make it point to the closing parenthesis
kono
parents: 55
diff changeset
600 of the macro, rather than the the location of the first character
kono
parents: 55
diff changeset
601 of the macro. NUM_TOKENS is the number of tokens that are part of
kono
parents: 55
diff changeset
602 the replacement-list of MACRO.
kono
parents: 55
diff changeset
603
kono
parents: 55
diff changeset
604 Note that when we run out of the integer space available for source
kono
parents: 55
diff changeset
605 locations, this function returns NULL. In that case, callers of
kono
parents: 55
diff changeset
606 this function cannot encode {line,column} pairs into locations of
kono
parents: 55
diff changeset
607 macro tokens anymore. */
kono
parents: 55
diff changeset
608
kono
parents: 55
diff changeset
609 const line_map_macro *
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
610 linemap_enter_macro (class line_maps *set, struct cpp_hashnode *macro_node,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
611 location_t expansion, unsigned int num_tokens)
111
kono
parents: 55
diff changeset
612 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
613 location_t start_location
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
614 = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
111
kono
parents: 55
diff changeset
615
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
616 if (start_location < LINE_MAP_MAX_LOCATION)
111
kono
parents: 55
diff changeset
617 /* We ran out of macro map space. */
kono
parents: 55
diff changeset
618 return NULL;
kono
parents: 55
diff changeset
619
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
620 line_map_macro *map = linemap_check_macro (new_linemap (set, start_location));
111
kono
parents: 55
diff changeset
621
kono
parents: 55
diff changeset
622 map->macro = macro_node;
kono
parents: 55
diff changeset
623 map->n_tokens = num_tokens;
kono
parents: 55
diff changeset
624 map->macro_locations
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
625 = (location_t*) set->reallocator (NULL,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
626 2 * num_tokens
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
627 * sizeof (location_t));
111
kono
parents: 55
diff changeset
628 map->expansion = expansion;
kono
parents: 55
diff changeset
629 memset (MACRO_MAP_LOCATIONS (map), 0,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
630 2 * num_tokens * sizeof (location_t));
111
kono
parents: 55
diff changeset
631
kono
parents: 55
diff changeset
632 LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
kono
parents: 55
diff changeset
633
kono
parents: 55
diff changeset
634 return map;
kono
parents: 55
diff changeset
635 }
kono
parents: 55
diff changeset
636
kono
parents: 55
diff changeset
637 /* Create and return a virtual location for a token that is part of a
kono
parents: 55
diff changeset
638 macro expansion-list at a macro expansion point. See the comment
kono
parents: 55
diff changeset
639 inside struct line_map_macro to see what an expansion-list exactly
kono
parents: 55
diff changeset
640 is.
kono
parents: 55
diff changeset
641
kono
parents: 55
diff changeset
642 A call to this function must come after a call to
kono
parents: 55
diff changeset
643 linemap_enter_macro.
kono
parents: 55
diff changeset
644
kono
parents: 55
diff changeset
645 MAP is the map into which the source location is created. TOKEN_NO
kono
parents: 55
diff changeset
646 is the index of the token in the macro replacement-list, starting
kono
parents: 55
diff changeset
647 at number 0.
kono
parents: 55
diff changeset
648
kono
parents: 55
diff changeset
649 ORIG_LOC is the location of the token outside of this macro
kono
parents: 55
diff changeset
650 expansion. If the token comes originally from the macro
kono
parents: 55
diff changeset
651 definition, it is the locus in the macro definition; otherwise it
kono
parents: 55
diff changeset
652 is a location in the context of the caller of this macro expansion
kono
parents: 55
diff changeset
653 (which is a virtual location or a source location if the caller is
kono
parents: 55
diff changeset
654 itself a macro expansion or not).
kono
parents: 55
diff changeset
655
kono
parents: 55
diff changeset
656 ORIG_PARM_REPLACEMENT_LOC is the location in the macro definition,
kono
parents: 55
diff changeset
657 either of the token itself or of a macro parameter that it
kono
parents: 55
diff changeset
658 replaces. */
kono
parents: 55
diff changeset
659
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
660 location_t
111
kono
parents: 55
diff changeset
661 linemap_add_macro_token (const line_map_macro *map,
kono
parents: 55
diff changeset
662 unsigned int token_no,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
663 location_t orig_loc,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
664 location_t orig_parm_replacement_loc)
111
kono
parents: 55
diff changeset
665 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
666 location_t result;
111
kono
parents: 55
diff changeset
667
kono
parents: 55
diff changeset
668 linemap_assert (linemap_macro_expansion_map_p (map));
kono
parents: 55
diff changeset
669 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
kono
parents: 55
diff changeset
670
kono
parents: 55
diff changeset
671 MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
kono
parents: 55
diff changeset
672 MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
kono
parents: 55
diff changeset
673
kono
parents: 55
diff changeset
674 result = MAP_START_LOCATION (map) + token_no;
kono
parents: 55
diff changeset
675 return result;
kono
parents: 55
diff changeset
676 }
kono
parents: 55
diff changeset
677
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
678 /* Return a location_t for the start (i.e. column==0) of
111
kono
parents: 55
diff changeset
679 (physical) line TO_LINE in the current source file (as in the
kono
parents: 55
diff changeset
680 most recent linemap_add). MAX_COLUMN_HINT is the highest column
kono
parents: 55
diff changeset
681 number we expect to use in this line (but it does not change
kono
parents: 55
diff changeset
682 the highest_location). */
kono
parents: 55
diff changeset
683
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
684 location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
685 linemap_line_start (line_maps *set, linenum_type to_line,
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
686 unsigned int max_column_hint)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
687 {
111
kono
parents: 55
diff changeset
688 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
689 location_t highest = set->highest_location;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
690 location_t r;
111
kono
parents: 55
diff changeset
691 linenum_type last_line =
kono
parents: 55
diff changeset
692 SOURCE_LINE (map, set->highest_line);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
693 int line_delta = to_line - last_line;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
694 bool add_map = false;
111
kono
parents: 55
diff changeset
695 linemap_assert (map->m_column_and_range_bits >= map->m_range_bits);
kono
parents: 55
diff changeset
696 int effective_column_bits = map->m_column_and_range_bits - map->m_range_bits;
kono
parents: 55
diff changeset
697
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
698 if (line_delta < 0
111
kono
parents: 55
diff changeset
699 || (line_delta > 10
kono
parents: 55
diff changeset
700 && line_delta * map->m_column_and_range_bits > 1000)
kono
parents: 55
diff changeset
701 || (max_column_hint >= (1U << effective_column_bits))
kono
parents: 55
diff changeset
702 || (max_column_hint <= 80 && effective_column_bits >= 10)
kono
parents: 55
diff changeset
703 || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
kono
parents: 55
diff changeset
704 && map->m_range_bits > 0)
kono
parents: 55
diff changeset
705 || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
706 && (set->max_column_hint || highest >= LINE_MAP_MAX_LOCATION)))
111
kono
parents: 55
diff changeset
707 add_map = true;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
708 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
709 max_column_hint = set->max_column_hint;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
710 if (add_map)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
711 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
712 int column_bits;
111
kono
parents: 55
diff changeset
713 int range_bits;
kono
parents: 55
diff changeset
714 if (max_column_hint > LINE_MAP_MAX_COLUMN_NUMBER
kono
parents: 55
diff changeset
715 || highest > LINE_MAP_MAX_LOCATION_WITH_COLS)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
716 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
717 /* If the column number is ridiculous or we've allocated a huge
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
718 number of location_ts, give up on column numbers
111
kono
parents: 55
diff changeset
719 (and on packed ranges). */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
720 max_column_hint = 1;
111
kono
parents: 55
diff changeset
721 column_bits = 0;
kono
parents: 55
diff changeset
722 range_bits = 0;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
723 if (highest >= LINE_MAP_MAX_LOCATION)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
724 goto overflowed;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
725 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
726 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
727 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
728 column_bits = 7;
111
kono
parents: 55
diff changeset
729 if (highest <= LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES)
kono
parents: 55
diff changeset
730 range_bits = set->default_range_bits;
kono
parents: 55
diff changeset
731 else
kono
parents: 55
diff changeset
732 range_bits = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
733 while (max_column_hint >= (1U << column_bits))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
734 column_bits++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
735 max_column_hint = 1U << column_bits;
111
kono
parents: 55
diff changeset
736 column_bits += range_bits;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
737 }
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
738
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
739 /* Allocate the new line_map. However, if the current map only has a
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
740 single line we can sometimes just increase its column_bits instead. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
741 if (line_delta < 0
111
kono
parents: 55
diff changeset
742 || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
kono
parents: 55
diff changeset
743 || SOURCE_COLUMN (map, highest) >= (1U << (column_bits - range_bits))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
744 || ( /* We can't reuse the map if the line offset is sufficiently
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
745 large to cause overflow when computing location_t values. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
746 (to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
747 >= (((uint64_t) 1)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
748 << (CHAR_BIT * sizeof (linenum_type) - column_bits)))
111
kono
parents: 55
diff changeset
749 || range_bits < map->m_range_bits)
kono
parents: 55
diff changeset
750 map = linemap_check_ordinary
kono
parents: 55
diff changeset
751 (const_cast <line_map *>
kono
parents: 55
diff changeset
752 (linemap_add (set, LC_RENAME,
kono
parents: 55
diff changeset
753 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
kono
parents: 55
diff changeset
754 ORDINARY_MAP_FILE_NAME (map),
kono
parents: 55
diff changeset
755 to_line)));
kono
parents: 55
diff changeset
756 map->m_column_and_range_bits = column_bits;
kono
parents: 55
diff changeset
757 map->m_range_bits = range_bits;
kono
parents: 55
diff changeset
758 r = (MAP_START_LOCATION (map)
kono
parents: 55
diff changeset
759 + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
kono
parents: 55
diff changeset
760 << column_bits));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
761 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
762 else
111
kono
parents: 55
diff changeset
763 r = set->highest_line + (line_delta << map->m_column_and_range_bits);
kono
parents: 55
diff changeset
764
kono
parents: 55
diff changeset
765 /* Locations of ordinary tokens are always lower than locations of
kono
parents: 55
diff changeset
766 macro tokens. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
767 if (r >= LINE_MAP_MAX_LOCATION)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
768 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
769 overflowed:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
770 /* Remember we overflowed. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
771 set->highest_line = set->highest_location = LINE_MAP_MAX_LOCATION - 1;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
772 /* No column numbers! */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
773 set->max_column_hint = 1;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
774 return 0;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
775 }
111
kono
parents: 55
diff changeset
776
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
777 set->highest_line = r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
778 if (r > set->highest_location)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
779 set->highest_location = r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
780 set->max_column_hint = max_column_hint;
111
kono
parents: 55
diff changeset
781
kono
parents: 55
diff changeset
782 /* At this point, we expect one of:
kono
parents: 55
diff changeset
783 (a) the normal case: a "pure" location with 0 range bits, or
kono
parents: 55
diff changeset
784 (b) we've gone past LINE_MAP_MAX_LOCATION_WITH_COLS so can't track
kono
parents: 55
diff changeset
785 columns anymore (or ranges), or
kono
parents: 55
diff changeset
786 (c) we're in a region with a column hint exceeding
kono
parents: 55
diff changeset
787 LINE_MAP_MAX_COLUMN_NUMBER, so column-tracking is off,
kono
parents: 55
diff changeset
788 with column_bits == 0. */
kono
parents: 55
diff changeset
789 linemap_assert (pure_location_p (set, r)
kono
parents: 55
diff changeset
790 || r >= LINE_MAP_MAX_LOCATION_WITH_COLS
kono
parents: 55
diff changeset
791 || map->m_column_and_range_bits == 0);
kono
parents: 55
diff changeset
792 linemap_assert (SOURCE_LINE (map, r) == to_line);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
793 return r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
794 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
795
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
796 /* Encode and return a location_t from a column number. The
111
kono
parents: 55
diff changeset
797 source line considered is the last source line used to call
kono
parents: 55
diff changeset
798 linemap_line_start, i.e, the last source line which a location was
kono
parents: 55
diff changeset
799 encoded from. */
kono
parents: 55
diff changeset
800
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
801 location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
802 linemap_position_for_column (line_maps *set, unsigned int to_column)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
803 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
804 location_t r = set->highest_line;
111
kono
parents: 55
diff changeset
805
kono
parents: 55
diff changeset
806 linemap_assert
kono
parents: 55
diff changeset
807 (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
kono
parents: 55
diff changeset
808
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
809 if (to_column >= set->max_column_hint)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
810 {
111
kono
parents: 55
diff changeset
811 if (r > LINE_MAP_MAX_LOCATION_WITH_COLS
kono
parents: 55
diff changeset
812 || to_column > LINE_MAP_MAX_COLUMN_NUMBER)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
813 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
814 /* Running low on location_ts - disable column numbers. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
815 return r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
816 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
817 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
818 {
111
kono
parents: 55
diff changeset
819 /* Otherwise, attempt to start a new line that can hold TO_COLUMN,
kono
parents: 55
diff changeset
820 with some space to spare. This may or may not lead to a new
kono
parents: 55
diff changeset
821 linemap being created. */
kono
parents: 55
diff changeset
822 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
823 r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
111
kono
parents: 55
diff changeset
824 map = LINEMAPS_LAST_ORDINARY_MAP (set);
kono
parents: 55
diff changeset
825 if (map->m_column_and_range_bits == 0)
kono
parents: 55
diff changeset
826 {
kono
parents: 55
diff changeset
827 /* ...then the linemap has column-tracking disabled,
kono
parents: 55
diff changeset
828 presumably due to exceeding either
kono
parents: 55
diff changeset
829 LINE_MAP_MAX_LOCATION_WITH_COLS (overall) or
kono
parents: 55
diff changeset
830 LINE_MAP_MAX_COLUMN_NUMBER (within this line).
kono
parents: 55
diff changeset
831 Return the start of the linemap, which encodes column 0, for
kono
parents: 55
diff changeset
832 the whole line. */
kono
parents: 55
diff changeset
833 return r;
kono
parents: 55
diff changeset
834 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
835 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
836 }
111
kono
parents: 55
diff changeset
837 line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
kono
parents: 55
diff changeset
838 r = r + (to_column << map->m_range_bits);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
839 if (r >= set->highest_location)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
840 set->highest_location = r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
841 return r;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
842 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
843
111
kono
parents: 55
diff changeset
844 /* Encode and return a source location from a given line and
kono
parents: 55
diff changeset
845 column. */
kono
parents: 55
diff changeset
846
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
847 location_t
111
kono
parents: 55
diff changeset
848 linemap_position_for_line_and_column (line_maps *set,
kono
parents: 55
diff changeset
849 const line_map_ordinary *ord_map,
kono
parents: 55
diff changeset
850 linenum_type line,
kono
parents: 55
diff changeset
851 unsigned column)
kono
parents: 55
diff changeset
852 {
kono
parents: 55
diff changeset
853 linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map) <= line);
kono
parents: 55
diff changeset
854
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
855 location_t r = MAP_START_LOCATION (ord_map);
111
kono
parents: 55
diff changeset
856 r += ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map))
kono
parents: 55
diff changeset
857 << ord_map->m_column_and_range_bits);
kono
parents: 55
diff changeset
858 if (r <= LINE_MAP_MAX_LOCATION_WITH_COLS)
kono
parents: 55
diff changeset
859 r += ((column & ((1 << ord_map->m_column_and_range_bits) - 1))
kono
parents: 55
diff changeset
860 << ord_map->m_range_bits);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
861 location_t upper_limit = LINEMAPS_MACRO_LOWEST_LOCATION (set);
111
kono
parents: 55
diff changeset
862 if (r >= upper_limit)
kono
parents: 55
diff changeset
863 r = upper_limit - 1;
kono
parents: 55
diff changeset
864 if (r > set->highest_location)
kono
parents: 55
diff changeset
865 set->highest_location = r;
kono
parents: 55
diff changeset
866 return r;
kono
parents: 55
diff changeset
867 }
kono
parents: 55
diff changeset
868
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
869 /* Encode and return a location_t starting from location LOC and
111
kono
parents: 55
diff changeset
870 shifting it by COLUMN_OFFSET columns. This function does not support
kono
parents: 55
diff changeset
871 virtual locations. */
kono
parents: 55
diff changeset
872
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
873 location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
874 linemap_position_for_loc_and_offset (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
875 location_t loc,
111
kono
parents: 55
diff changeset
876 unsigned int column_offset)
kono
parents: 55
diff changeset
877 {
kono
parents: 55
diff changeset
878 const line_map_ordinary * map = NULL;
kono
parents: 55
diff changeset
879
kono
parents: 55
diff changeset
880 if (IS_ADHOC_LOC (loc))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
881 loc = get_location_from_adhoc_loc (set, loc);
111
kono
parents: 55
diff changeset
882
kono
parents: 55
diff changeset
883 /* This function does not support virtual locations yet. */
kono
parents: 55
diff changeset
884 if (linemap_location_from_macro_expansion_p (set, loc))
kono
parents: 55
diff changeset
885 return loc;
kono
parents: 55
diff changeset
886
kono
parents: 55
diff changeset
887 if (column_offset == 0
kono
parents: 55
diff changeset
888 /* Adding an offset to a reserved location (like
kono
parents: 55
diff changeset
889 UNKNOWN_LOCATION for the C/C++ FEs) does not really make
kono
parents: 55
diff changeset
890 sense. So let's leave the location intact in that case. */
kono
parents: 55
diff changeset
891 || loc < RESERVED_LOCATION_COUNT)
kono
parents: 55
diff changeset
892 return loc;
kono
parents: 55
diff changeset
893
kono
parents: 55
diff changeset
894 /* We find the real location and shift it. */
kono
parents: 55
diff changeset
895 loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map);
kono
parents: 55
diff changeset
896 /* The new location (loc + offset) should be higher than the first
kono
parents: 55
diff changeset
897 location encoded by MAP. This can fail if the line information
kono
parents: 55
diff changeset
898 is messed up because of line directives (see PR66415). */
kono
parents: 55
diff changeset
899 if (MAP_START_LOCATION (map) >= loc + (column_offset << map->m_range_bits))
kono
parents: 55
diff changeset
900 return loc;
kono
parents: 55
diff changeset
901
kono
parents: 55
diff changeset
902 linenum_type line = SOURCE_LINE (map, loc);
kono
parents: 55
diff changeset
903 unsigned int column = SOURCE_COLUMN (map, loc);
kono
parents: 55
diff changeset
904
kono
parents: 55
diff changeset
905 /* If MAP is not the last line map of its set, then the new location
kono
parents: 55
diff changeset
906 (loc + offset) should be less than the first location encoded by
kono
parents: 55
diff changeset
907 the next line map of the set. Otherwise, we try to encode the
kono
parents: 55
diff changeset
908 location in the next map. */
kono
parents: 55
diff changeset
909 while (map != LINEMAPS_LAST_ORDINARY_MAP (set)
kono
parents: 55
diff changeset
910 && (loc + (column_offset << map->m_range_bits)
kono
parents: 55
diff changeset
911 >= MAP_START_LOCATION (&map[1])))
kono
parents: 55
diff changeset
912 {
kono
parents: 55
diff changeset
913 map = &map[1];
kono
parents: 55
diff changeset
914 /* If the next map starts in a higher line, we cannot encode the
kono
parents: 55
diff changeset
915 location there. */
kono
parents: 55
diff changeset
916 if (line < ORDINARY_MAP_STARTING_LINE_NUMBER (map))
kono
parents: 55
diff changeset
917 return loc;
kono
parents: 55
diff changeset
918 }
kono
parents: 55
diff changeset
919
kono
parents: 55
diff changeset
920 column += column_offset;
kono
parents: 55
diff changeset
921
kono
parents: 55
diff changeset
922 /* Bail out if the column is not representable within the existing
kono
parents: 55
diff changeset
923 linemap. */
kono
parents: 55
diff changeset
924 if (column >= (1u << (map->m_column_and_range_bits - map->m_range_bits)))
kono
parents: 55
diff changeset
925 return loc;
kono
parents: 55
diff changeset
926
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
927 location_t r =
111
kono
parents: 55
diff changeset
928 linemap_position_for_line_and_column (set, map, line, column);
kono
parents: 55
diff changeset
929 if (linemap_assert_fails (r <= set->highest_location)
kono
parents: 55
diff changeset
930 || linemap_assert_fails (map == linemap_lookup (set, r)))
kono
parents: 55
diff changeset
931 return loc;
kono
parents: 55
diff changeset
932
kono
parents: 55
diff changeset
933 return r;
kono
parents: 55
diff changeset
934 }
kono
parents: 55
diff changeset
935
kono
parents: 55
diff changeset
936 /* Given a virtual source location yielded by a map (either an
kono
parents: 55
diff changeset
937 ordinary or a macro map), returns that map. */
kono
parents: 55
diff changeset
938
kono
parents: 55
diff changeset
939 const struct line_map*
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
940 linemap_lookup (const line_maps *set, location_t line)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
941 {
111
kono
parents: 55
diff changeset
942 if (IS_ADHOC_LOC (line))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
943 line = get_location_from_adhoc_loc (set, line);
111
kono
parents: 55
diff changeset
944 if (linemap_location_from_macro_expansion_p (set, line))
kono
parents: 55
diff changeset
945 return linemap_macro_map_lookup (set, line);
kono
parents: 55
diff changeset
946 return linemap_ordinary_map_lookup (set, line);
kono
parents: 55
diff changeset
947 }
kono
parents: 55
diff changeset
948
kono
parents: 55
diff changeset
949 /* Given a source location yielded by an ordinary map, returns that
kono
parents: 55
diff changeset
950 map. Since the set is built chronologically, the logical lines are
kono
parents: 55
diff changeset
951 monotonic increasing, and so the list is sorted and we can use a
kono
parents: 55
diff changeset
952 binary search. */
kono
parents: 55
diff changeset
953
kono
parents: 55
diff changeset
954 static const line_map_ordinary *
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
955 linemap_ordinary_map_lookup (const line_maps *set, location_t line)
111
kono
parents: 55
diff changeset
956 {
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
957 unsigned int md, mn, mx;
111
kono
parents: 55
diff changeset
958 const line_map_ordinary *cached, *result;
kono
parents: 55
diff changeset
959
kono
parents: 55
diff changeset
960 if (IS_ADHOC_LOC (line))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
961 line = get_location_from_adhoc_loc (set, line);
111
kono
parents: 55
diff changeset
962
kono
parents: 55
diff changeset
963 if (set == NULL || line < RESERVED_LOCATION_COUNT)
kono
parents: 55
diff changeset
964 return NULL;
kono
parents: 55
diff changeset
965
kono
parents: 55
diff changeset
966 mn = LINEMAPS_ORDINARY_CACHE (set);
kono
parents: 55
diff changeset
967 mx = LINEMAPS_ORDINARY_USED (set);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
968
111
kono
parents: 55
diff changeset
969 cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
970 /* We should get a segfault if no line_maps have been added yet. */
111
kono
parents: 55
diff changeset
971 if (line >= MAP_START_LOCATION (cached))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
972 {
111
kono
parents: 55
diff changeset
973 if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
974 return cached;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
975 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
976 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
977 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
978 mx = mn;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
979 mn = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
980 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
981
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
982 while (mx - mn > 1)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
983 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
984 md = (mn + mx) / 2;
111
kono
parents: 55
diff changeset
985 if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
986 mx = md;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
987 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
988 mn = md;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
989 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
990
111
kono
parents: 55
diff changeset
991 LINEMAPS_ORDINARY_CACHE (set) = mn;
kono
parents: 55
diff changeset
992 result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
kono
parents: 55
diff changeset
993 linemap_assert (line >= MAP_START_LOCATION (result));
kono
parents: 55
diff changeset
994 return result;
kono
parents: 55
diff changeset
995 }
kono
parents: 55
diff changeset
996
kono
parents: 55
diff changeset
997 /* Given a source location yielded by a macro map, returns that map.
kono
parents: 55
diff changeset
998 Since the set is built chronologically, the logical lines are
kono
parents: 55
diff changeset
999 monotonic decreasing, and so the list is sorted and we can use a
kono
parents: 55
diff changeset
1000 binary search. */
kono
parents: 55
diff changeset
1001
kono
parents: 55
diff changeset
1002 static const line_map_macro *
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1003 linemap_macro_map_lookup (const line_maps *set, location_t line)
111
kono
parents: 55
diff changeset
1004 {
kono
parents: 55
diff changeset
1005 unsigned int md, mn, mx;
kono
parents: 55
diff changeset
1006 const struct line_map_macro *cached, *result;
kono
parents: 55
diff changeset
1007
kono
parents: 55
diff changeset
1008 if (IS_ADHOC_LOC (line))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1009 line = get_location_from_adhoc_loc (set, line);
111
kono
parents: 55
diff changeset
1010
kono
parents: 55
diff changeset
1011 linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
kono
parents: 55
diff changeset
1012
kono
parents: 55
diff changeset
1013 if (set == NULL)
kono
parents: 55
diff changeset
1014 return NULL;
kono
parents: 55
diff changeset
1015
kono
parents: 55
diff changeset
1016 mn = LINEMAPS_MACRO_CACHE (set);
kono
parents: 55
diff changeset
1017 mx = LINEMAPS_MACRO_USED (set);
kono
parents: 55
diff changeset
1018 cached = LINEMAPS_MACRO_MAP_AT (set, mn);
kono
parents: 55
diff changeset
1019
kono
parents: 55
diff changeset
1020 if (line >= MAP_START_LOCATION (cached))
kono
parents: 55
diff changeset
1021 {
kono
parents: 55
diff changeset
1022 if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
kono
parents: 55
diff changeset
1023 return cached;
kono
parents: 55
diff changeset
1024 mx = mn - 1;
kono
parents: 55
diff changeset
1025 mn = 0;
kono
parents: 55
diff changeset
1026 }
kono
parents: 55
diff changeset
1027
kono
parents: 55
diff changeset
1028 while (mn < mx)
kono
parents: 55
diff changeset
1029 {
kono
parents: 55
diff changeset
1030 md = (mx + mn) / 2;
kono
parents: 55
diff changeset
1031 if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
kono
parents: 55
diff changeset
1032 mn = md + 1;
kono
parents: 55
diff changeset
1033 else
kono
parents: 55
diff changeset
1034 mx = md;
kono
parents: 55
diff changeset
1035 }
kono
parents: 55
diff changeset
1036
kono
parents: 55
diff changeset
1037 LINEMAPS_MACRO_CACHE (set) = mx;
kono
parents: 55
diff changeset
1038 result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
kono
parents: 55
diff changeset
1039 linemap_assert (MAP_START_LOCATION (result) <= line);
kono
parents: 55
diff changeset
1040
kono
parents: 55
diff changeset
1041 return result;
kono
parents: 55
diff changeset
1042 }
kono
parents: 55
diff changeset
1043
kono
parents: 55
diff changeset
1044 /* Return TRUE if MAP encodes locations coming from a macro
kono
parents: 55
diff changeset
1045 replacement-list at macro expansion point. */
kono
parents: 55
diff changeset
1046
kono
parents: 55
diff changeset
1047 bool
kono
parents: 55
diff changeset
1048 linemap_macro_expansion_map_p (const struct line_map *map)
kono
parents: 55
diff changeset
1049 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1050 return map && !MAP_ORDINARY_P (map);
111
kono
parents: 55
diff changeset
1051 }
kono
parents: 55
diff changeset
1052
kono
parents: 55
diff changeset
1053 /* If LOCATION is the locus of a token in a replacement-list of a
kono
parents: 55
diff changeset
1054 macro expansion return the location of the macro expansion point.
kono
parents: 55
diff changeset
1055
kono
parents: 55
diff changeset
1056 Read the comments of struct line_map and struct line_map_macro in
kono
parents: 55
diff changeset
1057 line-map.h to understand what a macro expansion point is. */
kono
parents: 55
diff changeset
1058
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1059 static location_t
111
kono
parents: 55
diff changeset
1060 linemap_macro_map_loc_to_exp_point (const line_map_macro *map,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1061 location_t location ATTRIBUTE_UNUSED)
111
kono
parents: 55
diff changeset
1062 {
kono
parents: 55
diff changeset
1063 linemap_assert (linemap_macro_expansion_map_p (map)
kono
parents: 55
diff changeset
1064 && location >= MAP_START_LOCATION (map));
kono
parents: 55
diff changeset
1065
kono
parents: 55
diff changeset
1066 /* Make sure LOCATION is correct. */
kono
parents: 55
diff changeset
1067 linemap_assert ((location - MAP_START_LOCATION (map))
kono
parents: 55
diff changeset
1068 < MACRO_MAP_NUM_MACRO_TOKENS (map));
kono
parents: 55
diff changeset
1069
kono
parents: 55
diff changeset
1070 return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
kono
parents: 55
diff changeset
1071 }
kono
parents: 55
diff changeset
1072
kono
parents: 55
diff changeset
1073 /* LOCATION is the source location of a token that belongs to a macro
kono
parents: 55
diff changeset
1074 replacement-list as part of the macro expansion denoted by MAP.
kono
parents: 55
diff changeset
1075
kono
parents: 55
diff changeset
1076 Return the location of the token at the definition point of the
kono
parents: 55
diff changeset
1077 macro. */
kono
parents: 55
diff changeset
1078
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1079 static location_t
111
kono
parents: 55
diff changeset
1080 linemap_macro_map_loc_to_def_point (const line_map_macro *map,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1081 location_t location)
111
kono
parents: 55
diff changeset
1082 {
kono
parents: 55
diff changeset
1083 unsigned token_no;
kono
parents: 55
diff changeset
1084
kono
parents: 55
diff changeset
1085 linemap_assert (linemap_macro_expansion_map_p (map)
kono
parents: 55
diff changeset
1086 && location >= MAP_START_LOCATION (map));
kono
parents: 55
diff changeset
1087 linemap_assert (location >= RESERVED_LOCATION_COUNT);
kono
parents: 55
diff changeset
1088
kono
parents: 55
diff changeset
1089 token_no = location - MAP_START_LOCATION (map);
kono
parents: 55
diff changeset
1090 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
kono
parents: 55
diff changeset
1091
kono
parents: 55
diff changeset
1092 location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
kono
parents: 55
diff changeset
1093
kono
parents: 55
diff changeset
1094 return location;
kono
parents: 55
diff changeset
1095 }
kono
parents: 55
diff changeset
1096
kono
parents: 55
diff changeset
1097 /* If LOCATION is the locus of a token that is an argument of a
kono
parents: 55
diff changeset
1098 function-like macro M and appears in the expansion of M, return the
kono
parents: 55
diff changeset
1099 locus of that argument in the context of the caller of M.
kono
parents: 55
diff changeset
1100
kono
parents: 55
diff changeset
1101 In other words, this returns the xI location presented in the
kono
parents: 55
diff changeset
1102 comments of line_map_macro above. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1103 location_t
111
kono
parents: 55
diff changeset
1104 linemap_macro_map_loc_unwind_toward_spelling (line_maps *set,
kono
parents: 55
diff changeset
1105 const line_map_macro* map,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1106 location_t location)
111
kono
parents: 55
diff changeset
1107 {
kono
parents: 55
diff changeset
1108 unsigned token_no;
kono
parents: 55
diff changeset
1109
kono
parents: 55
diff changeset
1110 if (IS_ADHOC_LOC (location))
kono
parents: 55
diff changeset
1111 location = get_location_from_adhoc_loc (set, location);
kono
parents: 55
diff changeset
1112
kono
parents: 55
diff changeset
1113 linemap_assert (linemap_macro_expansion_map_p (map)
kono
parents: 55
diff changeset
1114 && location >= MAP_START_LOCATION (map));
kono
parents: 55
diff changeset
1115 linemap_assert (location >= RESERVED_LOCATION_COUNT);
kono
parents: 55
diff changeset
1116 linemap_assert (!IS_ADHOC_LOC (location));
kono
parents: 55
diff changeset
1117
kono
parents: 55
diff changeset
1118 token_no = location - MAP_START_LOCATION (map);
kono
parents: 55
diff changeset
1119 linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
kono
parents: 55
diff changeset
1120
kono
parents: 55
diff changeset
1121 location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
kono
parents: 55
diff changeset
1122
kono
parents: 55
diff changeset
1123 return location;
kono
parents: 55
diff changeset
1124 }
kono
parents: 55
diff changeset
1125
kono
parents: 55
diff changeset
1126 /* Return the source line number corresponding to source location
kono
parents: 55
diff changeset
1127 LOCATION. SET is the line map set LOCATION comes from. If
kono
parents: 55
diff changeset
1128 LOCATION is the source location of token that is part of the
kono
parents: 55
diff changeset
1129 replacement-list of a macro expansion return the line number of the
kono
parents: 55
diff changeset
1130 macro expansion point. */
kono
parents: 55
diff changeset
1131
kono
parents: 55
diff changeset
1132 int
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1133 linemap_get_expansion_line (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1134 location_t location)
111
kono
parents: 55
diff changeset
1135 {
kono
parents: 55
diff changeset
1136 const line_map_ordinary *map = NULL;
kono
parents: 55
diff changeset
1137
kono
parents: 55
diff changeset
1138 if (IS_ADHOC_LOC (location))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1139 location = get_location_from_adhoc_loc (set, location);
111
kono
parents: 55
diff changeset
1140
kono
parents: 55
diff changeset
1141 if (location < RESERVED_LOCATION_COUNT)
kono
parents: 55
diff changeset
1142 return 0;
kono
parents: 55
diff changeset
1143
kono
parents: 55
diff changeset
1144 location =
kono
parents: 55
diff changeset
1145 linemap_macro_loc_to_exp_point (set, location, &map);
kono
parents: 55
diff changeset
1146
kono
parents: 55
diff changeset
1147 return SOURCE_LINE (map, location);
kono
parents: 55
diff changeset
1148 }
kono
parents: 55
diff changeset
1149
kono
parents: 55
diff changeset
1150 /* Return the path of the file corresponding to source code location
kono
parents: 55
diff changeset
1151 LOCATION.
kono
parents: 55
diff changeset
1152
kono
parents: 55
diff changeset
1153 If LOCATION is the source location of token that is part of the
kono
parents: 55
diff changeset
1154 replacement-list of a macro expansion return the file path of the
kono
parents: 55
diff changeset
1155 macro expansion point.
kono
parents: 55
diff changeset
1156
kono
parents: 55
diff changeset
1157 SET is the line map set LOCATION comes from. */
kono
parents: 55
diff changeset
1158
kono
parents: 55
diff changeset
1159 const char*
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1160 linemap_get_expansion_filename (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1161 location_t location)
111
kono
parents: 55
diff changeset
1162 {
kono
parents: 55
diff changeset
1163 const struct line_map_ordinary *map = NULL;
kono
parents: 55
diff changeset
1164
kono
parents: 55
diff changeset
1165 if (IS_ADHOC_LOC (location))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1166 location = get_location_from_adhoc_loc (set, location);
111
kono
parents: 55
diff changeset
1167
kono
parents: 55
diff changeset
1168 if (location < RESERVED_LOCATION_COUNT)
kono
parents: 55
diff changeset
1169 return NULL;
kono
parents: 55
diff changeset
1170
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1171 linemap_macro_loc_to_exp_point (set, location, &map);
111
kono
parents: 55
diff changeset
1172
kono
parents: 55
diff changeset
1173 return LINEMAP_FILE (map);
kono
parents: 55
diff changeset
1174 }
kono
parents: 55
diff changeset
1175
kono
parents: 55
diff changeset
1176 /* Return the name of the macro associated to MACRO_MAP. */
kono
parents: 55
diff changeset
1177
kono
parents: 55
diff changeset
1178 const char*
kono
parents: 55
diff changeset
1179 linemap_map_get_macro_name (const line_map_macro *macro_map)
kono
parents: 55
diff changeset
1180 {
kono
parents: 55
diff changeset
1181 linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
kono
parents: 55
diff changeset
1182 return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
kono
parents: 55
diff changeset
1183 }
kono
parents: 55
diff changeset
1184
kono
parents: 55
diff changeset
1185 /* Return a positive value if LOCATION is the locus of a token that is
kono
parents: 55
diff changeset
1186 located in a system header, O otherwise. It returns 1 if LOCATION
kono
parents: 55
diff changeset
1187 is the locus of a token that is located in a system header, and 2
kono
parents: 55
diff changeset
1188 if LOCATION is the locus of a token located in a C system header
kono
parents: 55
diff changeset
1189 that therefore needs to be extern "C" protected in C++.
kono
parents: 55
diff changeset
1190
kono
parents: 55
diff changeset
1191 Note that this function returns 1 if LOCATION belongs to a token
kono
parents: 55
diff changeset
1192 that is part of a macro replacement-list defined in a system
kono
parents: 55
diff changeset
1193 header, but expanded in a non-system file. */
kono
parents: 55
diff changeset
1194
kono
parents: 55
diff changeset
1195 int
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1196 linemap_location_in_system_header_p (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1197 location_t location)
111
kono
parents: 55
diff changeset
1198 {
kono
parents: 55
diff changeset
1199 const struct line_map *map = NULL;
kono
parents: 55
diff changeset
1200
kono
parents: 55
diff changeset
1201 if (IS_ADHOC_LOC (location))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1202 location = get_location_from_adhoc_loc (set, location);
111
kono
parents: 55
diff changeset
1203
kono
parents: 55
diff changeset
1204 if (location < RESERVED_LOCATION_COUNT)
kono
parents: 55
diff changeset
1205 return false;
kono
parents: 55
diff changeset
1206
kono
parents: 55
diff changeset
1207 /* Let's look at where the token for LOCATION comes from. */
kono
parents: 55
diff changeset
1208 while (true)
kono
parents: 55
diff changeset
1209 {
kono
parents: 55
diff changeset
1210 map = linemap_lookup (set, location);
kono
parents: 55
diff changeset
1211 if (map != NULL)
kono
parents: 55
diff changeset
1212 {
kono
parents: 55
diff changeset
1213 if (!linemap_macro_expansion_map_p (map))
kono
parents: 55
diff changeset
1214 /* It's a normal token. */
kono
parents: 55
diff changeset
1215 return LINEMAP_SYSP (linemap_check_ordinary (map));
kono
parents: 55
diff changeset
1216 else
kono
parents: 55
diff changeset
1217 {
kono
parents: 55
diff changeset
1218 const line_map_macro *macro_map = linemap_check_macro (map);
kono
parents: 55
diff changeset
1219
kono
parents: 55
diff changeset
1220 /* It's a token resulting from a macro expansion. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1221 location_t loc =
111
kono
parents: 55
diff changeset
1222 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, location);
kono
parents: 55
diff changeset
1223 if (loc < RESERVED_LOCATION_COUNT)
kono
parents: 55
diff changeset
1224 /* This token might come from a built-in macro. Let's
kono
parents: 55
diff changeset
1225 look at where that macro got expanded. */
kono
parents: 55
diff changeset
1226 location = linemap_macro_map_loc_to_exp_point (macro_map, location);
kono
parents: 55
diff changeset
1227 else
kono
parents: 55
diff changeset
1228 location = loc;
kono
parents: 55
diff changeset
1229 }
kono
parents: 55
diff changeset
1230 }
kono
parents: 55
diff changeset
1231 else
kono
parents: 55
diff changeset
1232 break;
kono
parents: 55
diff changeset
1233 }
kono
parents: 55
diff changeset
1234 return false;
kono
parents: 55
diff changeset
1235 }
kono
parents: 55
diff changeset
1236
kono
parents: 55
diff changeset
1237 /* Return TRUE if LOCATION is a source code location of a token that is part of
kono
parents: 55
diff changeset
1238 a macro expansion, FALSE otherwise. */
kono
parents: 55
diff changeset
1239
kono
parents: 55
diff changeset
1240 bool
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1241 linemap_location_from_macro_expansion_p (const class line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1242 location_t location)
111
kono
parents: 55
diff changeset
1243 {
kono
parents: 55
diff changeset
1244 if (IS_ADHOC_LOC (location))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1245 location = get_location_from_adhoc_loc (set, location);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1246
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1247 return IS_MACRO_LOC (location);
111
kono
parents: 55
diff changeset
1248 }
kono
parents: 55
diff changeset
1249
kono
parents: 55
diff changeset
1250 /* Given two virtual locations *LOC0 and *LOC1, return the first
kono
parents: 55
diff changeset
1251 common macro map in their macro expansion histories. Return NULL
kono
parents: 55
diff changeset
1252 if no common macro was found. *LOC0 (resp. *LOC1) is set to the
kono
parents: 55
diff changeset
1253 virtual location of the token inside the resulting macro. */
kono
parents: 55
diff changeset
1254
kono
parents: 55
diff changeset
1255 static const struct line_map*
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1256 first_map_in_common_1 (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1257 location_t *loc0,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1258 location_t *loc1)
111
kono
parents: 55
diff changeset
1259 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1260 location_t l0 = *loc0, l1 = *loc1;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1261 const struct line_map *map0 = linemap_lookup (set, l0);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1262 if (IS_ADHOC_LOC (l0))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1263 l0 = get_location_from_adhoc_loc (set, l0);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1264
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1265 const struct line_map *map1 = linemap_lookup (set, l1);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1266 if (IS_ADHOC_LOC (l1))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1267 l1 = get_location_from_adhoc_loc (set, l1);
111
kono
parents: 55
diff changeset
1268
kono
parents: 55
diff changeset
1269 while (linemap_macro_expansion_map_p (map0)
kono
parents: 55
diff changeset
1270 && linemap_macro_expansion_map_p (map1)
kono
parents: 55
diff changeset
1271 && (map0 != map1))
kono
parents: 55
diff changeset
1272 {
kono
parents: 55
diff changeset
1273 if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
kono
parents: 55
diff changeset
1274 {
kono
parents: 55
diff changeset
1275 l0 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map0),
kono
parents: 55
diff changeset
1276 l0);
kono
parents: 55
diff changeset
1277 map0 = linemap_lookup (set, l0);
kono
parents: 55
diff changeset
1278 }
kono
parents: 55
diff changeset
1279 else
kono
parents: 55
diff changeset
1280 {
kono
parents: 55
diff changeset
1281 l1 = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map1),
kono
parents: 55
diff changeset
1282 l1);
kono
parents: 55
diff changeset
1283 map1 = linemap_lookup (set, l1);
kono
parents: 55
diff changeset
1284 }
kono
parents: 55
diff changeset
1285 }
kono
parents: 55
diff changeset
1286
kono
parents: 55
diff changeset
1287 if (map0 == map1)
kono
parents: 55
diff changeset
1288 {
kono
parents: 55
diff changeset
1289 *loc0 = l0;
kono
parents: 55
diff changeset
1290 *loc1 = l1;
kono
parents: 55
diff changeset
1291 return map0;
kono
parents: 55
diff changeset
1292 }
kono
parents: 55
diff changeset
1293 return NULL;
kono
parents: 55
diff changeset
1294 }
kono
parents: 55
diff changeset
1295
kono
parents: 55
diff changeset
1296 /* Given two virtual locations LOC0 and LOC1, return the first common
kono
parents: 55
diff changeset
1297 macro map in their macro expansion histories. Return NULL if no
kono
parents: 55
diff changeset
1298 common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the
kono
parents: 55
diff changeset
1299 virtual location of the token inside the resulting macro, upon
kono
parents: 55
diff changeset
1300 return of a non-NULL result. */
kono
parents: 55
diff changeset
1301
kono
parents: 55
diff changeset
1302 static const struct line_map*
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1303 first_map_in_common (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1304 location_t loc0,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1305 location_t loc1,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1306 location_t *res_loc0,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1307 location_t *res_loc1)
111
kono
parents: 55
diff changeset
1308 {
kono
parents: 55
diff changeset
1309 *res_loc0 = loc0;
kono
parents: 55
diff changeset
1310 *res_loc1 = loc1;
kono
parents: 55
diff changeset
1311
kono
parents: 55
diff changeset
1312 return first_map_in_common_1 (set, res_loc0, res_loc1);
kono
parents: 55
diff changeset
1313 }
kono
parents: 55
diff changeset
1314
kono
parents: 55
diff changeset
1315 /* Return a positive value if PRE denotes the location of a token that
kono
parents: 55
diff changeset
1316 comes before the token of POST, 0 if PRE denotes the location of
kono
parents: 55
diff changeset
1317 the same token as the token for POST, and a negative value
kono
parents: 55
diff changeset
1318 otherwise. */
kono
parents: 55
diff changeset
1319
kono
parents: 55
diff changeset
1320 int
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1321 linemap_compare_locations (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1322 location_t pre,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1323 location_t post)
111
kono
parents: 55
diff changeset
1324 {
kono
parents: 55
diff changeset
1325 bool pre_virtual_p, post_virtual_p;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1326 location_t l0 = pre, l1 = post;
111
kono
parents: 55
diff changeset
1327
kono
parents: 55
diff changeset
1328 if (IS_ADHOC_LOC (l0))
kono
parents: 55
diff changeset
1329 l0 = get_location_from_adhoc_loc (set, l0);
kono
parents: 55
diff changeset
1330 if (IS_ADHOC_LOC (l1))
kono
parents: 55
diff changeset
1331 l1 = get_location_from_adhoc_loc (set, l1);
kono
parents: 55
diff changeset
1332
kono
parents: 55
diff changeset
1333 if (l0 == l1)
kono
parents: 55
diff changeset
1334 return 0;
kono
parents: 55
diff changeset
1335
kono
parents: 55
diff changeset
1336 if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
kono
parents: 55
diff changeset
1337 l0 = linemap_resolve_location (set, l0,
kono
parents: 55
diff changeset
1338 LRK_MACRO_EXPANSION_POINT,
kono
parents: 55
diff changeset
1339 NULL);
kono
parents: 55
diff changeset
1340
kono
parents: 55
diff changeset
1341 if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
kono
parents: 55
diff changeset
1342 l1 = linemap_resolve_location (set, l1,
kono
parents: 55
diff changeset
1343 LRK_MACRO_EXPANSION_POINT,
kono
parents: 55
diff changeset
1344 NULL);
kono
parents: 55
diff changeset
1345
kono
parents: 55
diff changeset
1346 if (l0 == l1
kono
parents: 55
diff changeset
1347 && pre_virtual_p
kono
parents: 55
diff changeset
1348 && post_virtual_p)
kono
parents: 55
diff changeset
1349 {
kono
parents: 55
diff changeset
1350 /* So pre and post represent two tokens that are present in a
kono
parents: 55
diff changeset
1351 same macro expansion. Let's see if the token for pre was
kono
parents: 55
diff changeset
1352 before the token for post in that expansion. */
kono
parents: 55
diff changeset
1353 unsigned i0, i1;
kono
parents: 55
diff changeset
1354 const struct line_map *map =
kono
parents: 55
diff changeset
1355 first_map_in_common (set, pre, post, &l0, &l1);
kono
parents: 55
diff changeset
1356
kono
parents: 55
diff changeset
1357 if (map == NULL)
kono
parents: 55
diff changeset
1358 /* This should not be possible. */
kono
parents: 55
diff changeset
1359 abort ();
kono
parents: 55
diff changeset
1360
kono
parents: 55
diff changeset
1361 i0 = l0 - MAP_START_LOCATION (map);
kono
parents: 55
diff changeset
1362 i1 = l1 - MAP_START_LOCATION (map);
kono
parents: 55
diff changeset
1363 return i1 - i0;
kono
parents: 55
diff changeset
1364 }
kono
parents: 55
diff changeset
1365
kono
parents: 55
diff changeset
1366 if (IS_ADHOC_LOC (l0))
kono
parents: 55
diff changeset
1367 l0 = get_location_from_adhoc_loc (set, l0);
kono
parents: 55
diff changeset
1368 if (IS_ADHOC_LOC (l1))
kono
parents: 55
diff changeset
1369 l1 = get_location_from_adhoc_loc (set, l1);
kono
parents: 55
diff changeset
1370
kono
parents: 55
diff changeset
1371 return l1 - l0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1372 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1373
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1374 /* Print an include trace, for e.g. the -H option of the preprocessor. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1375
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1376 static void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1377 trace_include (const class line_maps *set, const line_map_ordinary *map)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1378 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1379 unsigned int i = set->depth;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1380
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1381 while (--i)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1382 putc ('.', stderr);
111
kono
parents: 55
diff changeset
1383
kono
parents: 55
diff changeset
1384 fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
kono
parents: 55
diff changeset
1385 }
kono
parents: 55
diff changeset
1386
kono
parents: 55
diff changeset
1387 /* Return the spelling location of the token wherever it comes from,
kono
parents: 55
diff changeset
1388 whether part of a macro definition or not.
kono
parents: 55
diff changeset
1389
kono
parents: 55
diff changeset
1390 This is a subroutine for linemap_resolve_location. */
kono
parents: 55
diff changeset
1391
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1392 static location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1393 linemap_macro_loc_to_spelling_point (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1394 location_t location,
111
kono
parents: 55
diff changeset
1395 const line_map_ordinary **original_map)
kono
parents: 55
diff changeset
1396 {
kono
parents: 55
diff changeset
1397 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
kono
parents: 55
diff changeset
1398
kono
parents: 55
diff changeset
1399 while (true)
kono
parents: 55
diff changeset
1400 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1401 const struct line_map *map = linemap_lookup (set, location);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1402 if (!map || MAP_ORDINARY_P (map))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1403 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1404 if (original_map)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1405 *original_map = (const line_map_ordinary *)map;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1406 break;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1407 }
111
kono
parents: 55
diff changeset
1408
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1409 location = linemap_macro_map_loc_unwind_toward_spelling
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1410 (set, linemap_check_macro (map), location);
111
kono
parents: 55
diff changeset
1411 }
kono
parents: 55
diff changeset
1412
kono
parents: 55
diff changeset
1413 return location;
kono
parents: 55
diff changeset
1414 }
kono
parents: 55
diff changeset
1415
kono
parents: 55
diff changeset
1416 /* If LOCATION is the source location of a token that belongs to a
kono
parents: 55
diff changeset
1417 macro replacement-list -- as part of a macro expansion -- then
kono
parents: 55
diff changeset
1418 return the location of the token at the definition point of the
kono
parents: 55
diff changeset
1419 macro. Otherwise, return LOCATION. SET is the set of maps
kono
parents: 55
diff changeset
1420 location come from. ORIGINAL_MAP is an output parm. If non NULL,
kono
parents: 55
diff changeset
1421 the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
kono
parents: 55
diff changeset
1422 returned location comes from.
kono
parents: 55
diff changeset
1423
kono
parents: 55
diff changeset
1424 This is a subroutine of linemap_resolve_location. */
kono
parents: 55
diff changeset
1425
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1426 static location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1427 linemap_macro_loc_to_def_point (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1428 location_t location,
111
kono
parents: 55
diff changeset
1429 const line_map_ordinary **original_map)
kono
parents: 55
diff changeset
1430 {
kono
parents: 55
diff changeset
1431 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
kono
parents: 55
diff changeset
1432
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1433 for (;;)
111
kono
parents: 55
diff changeset
1434 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1435 location_t caret_loc = location;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1436 if (IS_ADHOC_LOC (caret_loc))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1437 caret_loc = get_location_from_adhoc_loc (set, caret_loc);
111
kono
parents: 55
diff changeset
1438
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1439 const line_map *map = linemap_lookup (set, caret_loc);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1440 if (!map || MAP_ORDINARY_P (map))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1441 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1442 if (original_map)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1443 *original_map = (const line_map_ordinary *)map;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1444 break;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1445 }
111
kono
parents: 55
diff changeset
1446
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1447 location = linemap_macro_map_loc_to_def_point
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1448 (linemap_check_macro (map), caret_loc);
111
kono
parents: 55
diff changeset
1449 }
kono
parents: 55
diff changeset
1450
kono
parents: 55
diff changeset
1451 return location;
kono
parents: 55
diff changeset
1452 }
kono
parents: 55
diff changeset
1453
kono
parents: 55
diff changeset
1454 /* If LOCATION is the source location of a token that belongs to a
kono
parents: 55
diff changeset
1455 macro replacement-list -- at a macro expansion point -- then return
kono
parents: 55
diff changeset
1456 the location of the topmost expansion point of the macro. We say
kono
parents: 55
diff changeset
1457 topmost because if we are in the context of a nested macro
kono
parents: 55
diff changeset
1458 expansion, the function returns the source location of the first
kono
parents: 55
diff changeset
1459 macro expansion that triggered the nested expansions.
kono
parents: 55
diff changeset
1460
kono
parents: 55
diff changeset
1461 Otherwise, return LOCATION. SET is the set of maps location come
kono
parents: 55
diff changeset
1462 from. ORIGINAL_MAP is an output parm. If non NULL, the function
kono
parents: 55
diff changeset
1463 sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
kono
parents: 55
diff changeset
1464 location comes from.
kono
parents: 55
diff changeset
1465
kono
parents: 55
diff changeset
1466 This is a subroutine of linemap_resolve_location. */
kono
parents: 55
diff changeset
1467
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1468 static location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1469 linemap_macro_loc_to_exp_point (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1470 location_t location,
111
kono
parents: 55
diff changeset
1471 const line_map_ordinary **original_map)
kono
parents: 55
diff changeset
1472 {
kono
parents: 55
diff changeset
1473 struct line_map *map;
kono
parents: 55
diff changeset
1474
kono
parents: 55
diff changeset
1475 if (IS_ADHOC_LOC (location))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1476 location = get_location_from_adhoc_loc (set, location);
111
kono
parents: 55
diff changeset
1477
kono
parents: 55
diff changeset
1478 linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
kono
parents: 55
diff changeset
1479
kono
parents: 55
diff changeset
1480 while (true)
kono
parents: 55
diff changeset
1481 {
kono
parents: 55
diff changeset
1482 map = const_cast <line_map *> (linemap_lookup (set, location));
kono
parents: 55
diff changeset
1483 if (!linemap_macro_expansion_map_p (map))
kono
parents: 55
diff changeset
1484 break;
kono
parents: 55
diff changeset
1485 location = linemap_macro_map_loc_to_exp_point (linemap_check_macro (map),
kono
parents: 55
diff changeset
1486 location);
kono
parents: 55
diff changeset
1487 }
kono
parents: 55
diff changeset
1488
kono
parents: 55
diff changeset
1489 if (original_map)
kono
parents: 55
diff changeset
1490 *original_map = linemap_check_ordinary (map);
kono
parents: 55
diff changeset
1491 return location;
kono
parents: 55
diff changeset
1492 }
kono
parents: 55
diff changeset
1493
kono
parents: 55
diff changeset
1494 /* Resolve a virtual location into either a spelling location, an
kono
parents: 55
diff changeset
1495 expansion point location or a token argument replacement point
kono
parents: 55
diff changeset
1496 location. Return the map that encodes the virtual location as well
kono
parents: 55
diff changeset
1497 as the resolved location.
kono
parents: 55
diff changeset
1498
kono
parents: 55
diff changeset
1499 If LOC is *NOT* the location of a token resulting from the
kono
parents: 55
diff changeset
1500 expansion of a macro, then the parameter LRK (which stands for
kono
parents: 55
diff changeset
1501 Location Resolution Kind) is ignored and the resulting location
kono
parents: 55
diff changeset
1502 just equals the one given in argument.
kono
parents: 55
diff changeset
1503
kono
parents: 55
diff changeset
1504 Now if LOC *IS* the location of a token resulting from the
kono
parents: 55
diff changeset
1505 expansion of a macro, this is what happens.
kono
parents: 55
diff changeset
1506
kono
parents: 55
diff changeset
1507 * If LRK is set to LRK_MACRO_EXPANSION_POINT
kono
parents: 55
diff changeset
1508 -------------------------------
kono
parents: 55
diff changeset
1509
kono
parents: 55
diff changeset
1510 The virtual location is resolved to the first macro expansion point
kono
parents: 55
diff changeset
1511 that led to this macro expansion.
kono
parents: 55
diff changeset
1512
kono
parents: 55
diff changeset
1513 * If LRK is set to LRK_SPELLING_LOCATION
kono
parents: 55
diff changeset
1514 -------------------------------------
kono
parents: 55
diff changeset
1515
kono
parents: 55
diff changeset
1516 The virtual location is resolved to the locus where the token has
kono
parents: 55
diff changeset
1517 been spelled in the source. This can follow through all the macro
kono
parents: 55
diff changeset
1518 expansions that led to the token.
kono
parents: 55
diff changeset
1519
kono
parents: 55
diff changeset
1520 * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
kono
parents: 55
diff changeset
1521 --------------------------------------
kono
parents: 55
diff changeset
1522
kono
parents: 55
diff changeset
1523 The virtual location is resolved to the locus of the token in the
kono
parents: 55
diff changeset
1524 context of the macro definition.
kono
parents: 55
diff changeset
1525
kono
parents: 55
diff changeset
1526 If LOC is the locus of a token that is an argument of a
kono
parents: 55
diff changeset
1527 function-like macro [replacing a parameter in the replacement list
kono
parents: 55
diff changeset
1528 of the macro] the virtual location is resolved to the locus of the
kono
parents: 55
diff changeset
1529 parameter that is replaced, in the context of the definition of the
kono
parents: 55
diff changeset
1530 macro.
kono
parents: 55
diff changeset
1531
kono
parents: 55
diff changeset
1532 If LOC is the locus of a token that is not an argument of a
kono
parents: 55
diff changeset
1533 function-like macro, then the function behaves as if LRK was set to
kono
parents: 55
diff changeset
1534 LRK_SPELLING_LOCATION.
kono
parents: 55
diff changeset
1535
kono
parents: 55
diff changeset
1536 If MAP is not NULL, *MAP is set to the map encoding the
kono
parents: 55
diff changeset
1537 returned location. Note that if the returned location wasn't originally
kono
parents: 55
diff changeset
1538 encoded by a map, then *MAP is set to NULL. This can happen if LOC
kono
parents: 55
diff changeset
1539 resolves to a location reserved for the client code, like
kono
parents: 55
diff changeset
1540 UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
kono
parents: 55
diff changeset
1541
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1542 location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1543 linemap_resolve_location (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1544 location_t loc,
111
kono
parents: 55
diff changeset
1545 enum location_resolution_kind lrk,
kono
parents: 55
diff changeset
1546 const line_map_ordinary **map)
kono
parents: 55
diff changeset
1547 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1548 location_t locus = loc;
111
kono
parents: 55
diff changeset
1549 if (IS_ADHOC_LOC (loc))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1550 locus = get_location_from_adhoc_loc (set, loc);
111
kono
parents: 55
diff changeset
1551
kono
parents: 55
diff changeset
1552 if (locus < RESERVED_LOCATION_COUNT)
kono
parents: 55
diff changeset
1553 {
kono
parents: 55
diff changeset
1554 /* A reserved location wasn't encoded in a map. Let's return a
kono
parents: 55
diff changeset
1555 NULL map here, just like what linemap_ordinary_map_lookup
kono
parents: 55
diff changeset
1556 does. */
kono
parents: 55
diff changeset
1557 if (map)
kono
parents: 55
diff changeset
1558 *map = NULL;
kono
parents: 55
diff changeset
1559 return loc;
kono
parents: 55
diff changeset
1560 }
kono
parents: 55
diff changeset
1561
kono
parents: 55
diff changeset
1562 switch (lrk)
kono
parents: 55
diff changeset
1563 {
kono
parents: 55
diff changeset
1564 case LRK_MACRO_EXPANSION_POINT:
kono
parents: 55
diff changeset
1565 loc = linemap_macro_loc_to_exp_point (set, loc, map);
kono
parents: 55
diff changeset
1566 break;
kono
parents: 55
diff changeset
1567 case LRK_SPELLING_LOCATION:
kono
parents: 55
diff changeset
1568 loc = linemap_macro_loc_to_spelling_point (set, loc, map);
kono
parents: 55
diff changeset
1569 break;
kono
parents: 55
diff changeset
1570 case LRK_MACRO_DEFINITION_LOCATION:
kono
parents: 55
diff changeset
1571 loc = linemap_macro_loc_to_def_point (set, loc, map);
kono
parents: 55
diff changeset
1572 break;
kono
parents: 55
diff changeset
1573 default:
kono
parents: 55
diff changeset
1574 abort ();
kono
parents: 55
diff changeset
1575 }
kono
parents: 55
diff changeset
1576 return loc;
kono
parents: 55
diff changeset
1577 }
kono
parents: 55
diff changeset
1578
kono
parents: 55
diff changeset
1579 /* TRUE if LOCATION is a source code location of a token that is part of the
kono
parents: 55
diff changeset
1580 definition of a macro, FALSE otherwise. */
kono
parents: 55
diff changeset
1581
kono
parents: 55
diff changeset
1582 bool
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1583 linemap_location_from_macro_definition_p (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1584 location_t loc)
111
kono
parents: 55
diff changeset
1585 {
kono
parents: 55
diff changeset
1586 if (IS_ADHOC_LOC (loc))
kono
parents: 55
diff changeset
1587 loc = get_location_from_adhoc_loc (set, loc);
kono
parents: 55
diff changeset
1588
kono
parents: 55
diff changeset
1589 if (!linemap_location_from_macro_expansion_p (set, loc))
kono
parents: 55
diff changeset
1590 return false;
kono
parents: 55
diff changeset
1591
kono
parents: 55
diff changeset
1592 while (true)
kono
parents: 55
diff changeset
1593 {
kono
parents: 55
diff changeset
1594 const struct line_map_macro *map
kono
parents: 55
diff changeset
1595 = linemap_check_macro (linemap_lookup (set, loc));
kono
parents: 55
diff changeset
1596
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1597 location_t s_loc
111
kono
parents: 55
diff changeset
1598 = linemap_macro_map_loc_unwind_toward_spelling (set, map, loc);
kono
parents: 55
diff changeset
1599 if (linemap_location_from_macro_expansion_p (set, s_loc))
kono
parents: 55
diff changeset
1600 loc = s_loc;
kono
parents: 55
diff changeset
1601 else
kono
parents: 55
diff changeset
1602 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1603 location_t def_loc
111
kono
parents: 55
diff changeset
1604 = linemap_macro_map_loc_to_def_point (map, loc);
kono
parents: 55
diff changeset
1605 return s_loc == def_loc;
kono
parents: 55
diff changeset
1606 }
kono
parents: 55
diff changeset
1607 }
kono
parents: 55
diff changeset
1608 }
kono
parents: 55
diff changeset
1609
kono
parents: 55
diff changeset
1610 /*
kono
parents: 55
diff changeset
1611 Suppose that LOC is the virtual location of a token T coming from
kono
parents: 55
diff changeset
1612 the expansion of a macro M. This function then steps up to get the
kono
parents: 55
diff changeset
1613 location L of the point where M got expanded. If L is a spelling
kono
parents: 55
diff changeset
1614 location inside a macro expansion M', then this function returns
kono
parents: 55
diff changeset
1615 the locus of the point where M' was expanded. Said otherwise, this
kono
parents: 55
diff changeset
1616 function returns the location of T in the context that triggered
kono
parents: 55
diff changeset
1617 the expansion of M.
kono
parents: 55
diff changeset
1618
kono
parents: 55
diff changeset
1619 *LOC_MAP must be set to the map of LOC. This function then sets it
kono
parents: 55
diff changeset
1620 to the map of the returned location. */
kono
parents: 55
diff changeset
1621
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1622 location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1623 linemap_unwind_toward_expansion (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1624 location_t loc,
111
kono
parents: 55
diff changeset
1625 const struct line_map **map)
kono
parents: 55
diff changeset
1626 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1627 location_t resolved_location;
111
kono
parents: 55
diff changeset
1628 const line_map_macro *macro_map = linemap_check_macro (*map);
kono
parents: 55
diff changeset
1629 const struct line_map *resolved_map;
kono
parents: 55
diff changeset
1630
kono
parents: 55
diff changeset
1631 if (IS_ADHOC_LOC (loc))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1632 loc = get_location_from_adhoc_loc (set, loc);
111
kono
parents: 55
diff changeset
1633
kono
parents: 55
diff changeset
1634 resolved_location =
kono
parents: 55
diff changeset
1635 linemap_macro_map_loc_unwind_toward_spelling (set, macro_map, loc);
kono
parents: 55
diff changeset
1636 resolved_map = linemap_lookup (set, resolved_location);
kono
parents: 55
diff changeset
1637
kono
parents: 55
diff changeset
1638 if (!linemap_macro_expansion_map_p (resolved_map))
kono
parents: 55
diff changeset
1639 {
kono
parents: 55
diff changeset
1640 resolved_location = linemap_macro_map_loc_to_exp_point (macro_map, loc);
kono
parents: 55
diff changeset
1641 resolved_map = linemap_lookup (set, resolved_location);
kono
parents: 55
diff changeset
1642 }
kono
parents: 55
diff changeset
1643
kono
parents: 55
diff changeset
1644 *map = resolved_map;
kono
parents: 55
diff changeset
1645 return resolved_location;
kono
parents: 55
diff changeset
1646 }
kono
parents: 55
diff changeset
1647
kono
parents: 55
diff changeset
1648 /* If LOC is the virtual location of a token coming from the expansion
kono
parents: 55
diff changeset
1649 of a macro M and if its spelling location is reserved (e.g, a
kono
parents: 55
diff changeset
1650 location for a built-in token), then this function unwinds (using
kono
parents: 55
diff changeset
1651 linemap_unwind_toward_expansion) the location until a location that
kono
parents: 55
diff changeset
1652 is not reserved and is not in a system header is reached. In other
kono
parents: 55
diff changeset
1653 words, this unwinds the reserved location until a location that is
kono
parents: 55
diff changeset
1654 in real source code is reached.
kono
parents: 55
diff changeset
1655
kono
parents: 55
diff changeset
1656 Otherwise, if the spelling location for LOC is not reserved or if
kono
parents: 55
diff changeset
1657 LOC doesn't come from the expansion of a macro, the function
kono
parents: 55
diff changeset
1658 returns LOC as is and *MAP is not touched.
kono
parents: 55
diff changeset
1659
kono
parents: 55
diff changeset
1660 *MAP is set to the map of the returned location if the later is
kono
parents: 55
diff changeset
1661 different from LOC. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1662 location_t
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1663 linemap_unwind_to_first_non_reserved_loc (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1664 location_t loc,
111
kono
parents: 55
diff changeset
1665 const struct line_map **map)
kono
parents: 55
diff changeset
1666 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1667 location_t resolved_loc;
111
kono
parents: 55
diff changeset
1668 const struct line_map *map0 = NULL;
kono
parents: 55
diff changeset
1669 const line_map_ordinary *map1 = NULL;
kono
parents: 55
diff changeset
1670
kono
parents: 55
diff changeset
1671 if (IS_ADHOC_LOC (loc))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1672 loc = get_location_from_adhoc_loc (set, loc);
111
kono
parents: 55
diff changeset
1673
kono
parents: 55
diff changeset
1674 map0 = linemap_lookup (set, loc);
kono
parents: 55
diff changeset
1675 if (!linemap_macro_expansion_map_p (map0))
kono
parents: 55
diff changeset
1676 return loc;
kono
parents: 55
diff changeset
1677
kono
parents: 55
diff changeset
1678 resolved_loc = linemap_resolve_location (set, loc,
kono
parents: 55
diff changeset
1679 LRK_SPELLING_LOCATION,
kono
parents: 55
diff changeset
1680 &map1);
kono
parents: 55
diff changeset
1681
kono
parents: 55
diff changeset
1682 if (resolved_loc >= RESERVED_LOCATION_COUNT
kono
parents: 55
diff changeset
1683 && !LINEMAP_SYSP (map1))
kono
parents: 55
diff changeset
1684 return loc;
kono
parents: 55
diff changeset
1685
kono
parents: 55
diff changeset
1686 while (linemap_macro_expansion_map_p (map0)
kono
parents: 55
diff changeset
1687 && (resolved_loc < RESERVED_LOCATION_COUNT
kono
parents: 55
diff changeset
1688 || LINEMAP_SYSP (map1)))
kono
parents: 55
diff changeset
1689 {
kono
parents: 55
diff changeset
1690 loc = linemap_unwind_toward_expansion (set, loc, &map0);
kono
parents: 55
diff changeset
1691 resolved_loc = linemap_resolve_location (set, loc,
kono
parents: 55
diff changeset
1692 LRK_SPELLING_LOCATION,
kono
parents: 55
diff changeset
1693 &map1);
kono
parents: 55
diff changeset
1694 }
kono
parents: 55
diff changeset
1695
kono
parents: 55
diff changeset
1696 if (map != NULL)
kono
parents: 55
diff changeset
1697 *map = map0;
kono
parents: 55
diff changeset
1698 return loc;
kono
parents: 55
diff changeset
1699 }
kono
parents: 55
diff changeset
1700
kono
parents: 55
diff changeset
1701 /* Expand source code location LOC and return a user readable source
kono
parents: 55
diff changeset
1702 code location. LOC must be a spelling (non-virtual) location. If
kono
parents: 55
diff changeset
1703 it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
kono
parents: 55
diff changeset
1704 location is returned. */
kono
parents: 55
diff changeset
1705
kono
parents: 55
diff changeset
1706 expanded_location
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1707 linemap_expand_location (line_maps *set,
111
kono
parents: 55
diff changeset
1708 const struct line_map *map,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1709 location_t loc)
111
kono
parents: 55
diff changeset
1710
kono
parents: 55
diff changeset
1711 {
kono
parents: 55
diff changeset
1712 expanded_location xloc;
kono
parents: 55
diff changeset
1713
kono
parents: 55
diff changeset
1714 memset (&xloc, 0, sizeof (xloc));
kono
parents: 55
diff changeset
1715 if (IS_ADHOC_LOC (loc))
kono
parents: 55
diff changeset
1716 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1717 xloc.data = get_data_from_adhoc_loc (set, loc);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1718 loc = get_location_from_adhoc_loc (set, loc);
111
kono
parents: 55
diff changeset
1719 }
kono
parents: 55
diff changeset
1720
kono
parents: 55
diff changeset
1721 if (loc < RESERVED_LOCATION_COUNT)
kono
parents: 55
diff changeset
1722 /* The location for this token wasn't generated from a line map.
kono
parents: 55
diff changeset
1723 It was probably a location for a builtin token, chosen by some
kono
parents: 55
diff changeset
1724 client code. Let's not try to expand the location in that
kono
parents: 55
diff changeset
1725 case. */;
kono
parents: 55
diff changeset
1726 else if (map == NULL)
kono
parents: 55
diff changeset
1727 /* We shouldn't be getting a NULL map with a location that is not
kono
parents: 55
diff changeset
1728 reserved by the client code. */
kono
parents: 55
diff changeset
1729 abort ();
kono
parents: 55
diff changeset
1730 else
kono
parents: 55
diff changeset
1731 {
kono
parents: 55
diff changeset
1732 /* MAP must be an ordinary map and LOC must be non-virtual,
kono
parents: 55
diff changeset
1733 encoded into this map, obviously; the accessors used on MAP
kono
parents: 55
diff changeset
1734 below ensure it is ordinary. Let's just assert the
kono
parents: 55
diff changeset
1735 non-virtualness of LOC here. */
kono
parents: 55
diff changeset
1736 if (linemap_location_from_macro_expansion_p (set, loc))
kono
parents: 55
diff changeset
1737 abort ();
kono
parents: 55
diff changeset
1738
kono
parents: 55
diff changeset
1739 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
kono
parents: 55
diff changeset
1740
kono
parents: 55
diff changeset
1741 xloc.file = LINEMAP_FILE (ord_map);
kono
parents: 55
diff changeset
1742 xloc.line = SOURCE_LINE (ord_map, loc);
kono
parents: 55
diff changeset
1743 xloc.column = SOURCE_COLUMN (ord_map, loc);
kono
parents: 55
diff changeset
1744 xloc.sysp = LINEMAP_SYSP (ord_map) != 0;
kono
parents: 55
diff changeset
1745 }
kono
parents: 55
diff changeset
1746
kono
parents: 55
diff changeset
1747 return xloc;
kono
parents: 55
diff changeset
1748 }
kono
parents: 55
diff changeset
1749
kono
parents: 55
diff changeset
1750
kono
parents: 55
diff changeset
1751 /* Dump line map at index IX in line table SET to STREAM. If STREAM
kono
parents: 55
diff changeset
1752 is NULL, use stderr. IS_MACRO is true if the caller wants to
kono
parents: 55
diff changeset
1753 dump a macro map, false otherwise. */
kono
parents: 55
diff changeset
1754
kono
parents: 55
diff changeset
1755 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1756 linemap_dump (FILE *stream, class line_maps *set, unsigned ix, bool is_macro)
111
kono
parents: 55
diff changeset
1757 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1758 const char *const lc_reasons_v[LC_HWM]
111
kono
parents: 55
diff changeset
1759 = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM",
kono
parents: 55
diff changeset
1760 "LC_ENTER_MACRO" };
kono
parents: 55
diff changeset
1761 const line_map *map;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1762 unsigned reason;
111
kono
parents: 55
diff changeset
1763
kono
parents: 55
diff changeset
1764 if (stream == NULL)
kono
parents: 55
diff changeset
1765 stream = stderr;
kono
parents: 55
diff changeset
1766
kono
parents: 55
diff changeset
1767 if (!is_macro)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1768 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1769 map = LINEMAPS_ORDINARY_MAP_AT (set, ix);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1770 reason = linemap_check_ordinary (map)->reason;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1771 }
111
kono
parents: 55
diff changeset
1772 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1773 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1774 map = LINEMAPS_MACRO_MAP_AT (set, ix);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1775 reason = LC_ENTER_MACRO;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1776 }
111
kono
parents: 55
diff changeset
1777
kono
parents: 55
diff changeset
1778 fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n",
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1779 ix, (void *) map, map->start_location,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1780 reason < LC_HWM ? lc_reasons_v[reason] : "???",
111
kono
parents: 55
diff changeset
1781 ((!is_macro
kono
parents: 55
diff changeset
1782 && ORDINARY_MAP_IN_SYSTEM_HEADER_P (linemap_check_ordinary (map)))
kono
parents: 55
diff changeset
1783 ? "yes" : "no"));
kono
parents: 55
diff changeset
1784 if (!is_macro)
kono
parents: 55
diff changeset
1785 {
kono
parents: 55
diff changeset
1786 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1787 const line_map_ordinary *includer_map
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1788 = linemap_included_from_linemap (set, ord_map);
111
kono
parents: 55
diff changeset
1789
kono
parents: 55
diff changeset
1790 fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (ord_map),
kono
parents: 55
diff changeset
1791 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map));
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1792 fprintf (stream, "Included from: [%d] %s\n",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1793 includer_map ? int (includer_map - set->info_ordinary.maps) : -1,
111
kono
parents: 55
diff changeset
1794 includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None");
kono
parents: 55
diff changeset
1795 }
kono
parents: 55
diff changeset
1796 else
kono
parents: 55
diff changeset
1797 {
kono
parents: 55
diff changeset
1798 const line_map_macro *macro_map = linemap_check_macro (map);
kono
parents: 55
diff changeset
1799 fprintf (stream, "Macro: %s (%u tokens)\n",
kono
parents: 55
diff changeset
1800 linemap_map_get_macro_name (macro_map),
kono
parents: 55
diff changeset
1801 MACRO_MAP_NUM_MACRO_TOKENS (macro_map));
kono
parents: 55
diff changeset
1802 }
kono
parents: 55
diff changeset
1803
kono
parents: 55
diff changeset
1804 fprintf (stream, "\n");
kono
parents: 55
diff changeset
1805 }
kono
parents: 55
diff changeset
1806
kono
parents: 55
diff changeset
1807
kono
parents: 55
diff changeset
1808 /* Dump debugging information about source location LOC into the file
kono
parents: 55
diff changeset
1809 stream STREAM. SET is the line map set LOC comes from. */
kono
parents: 55
diff changeset
1810
kono
parents: 55
diff changeset
1811 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1812 linemap_dump_location (line_maps *set,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1813 location_t loc,
111
kono
parents: 55
diff changeset
1814 FILE *stream)
kono
parents: 55
diff changeset
1815 {
kono
parents: 55
diff changeset
1816 const line_map_ordinary *map;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1817 location_t location;
111
kono
parents: 55
diff changeset
1818 const char *path = "", *from = "";
kono
parents: 55
diff changeset
1819 int l = -1, c = -1, s = -1, e = -1;
kono
parents: 55
diff changeset
1820
kono
parents: 55
diff changeset
1821 if (IS_ADHOC_LOC (loc))
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1822 loc = get_location_from_adhoc_loc (set, loc);
111
kono
parents: 55
diff changeset
1823
kono
parents: 55
diff changeset
1824 if (loc == 0)
kono
parents: 55
diff changeset
1825 return;
kono
parents: 55
diff changeset
1826
kono
parents: 55
diff changeset
1827 location =
kono
parents: 55
diff changeset
1828 linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
kono
parents: 55
diff changeset
1829
kono
parents: 55
diff changeset
1830 if (map == NULL)
kono
parents: 55
diff changeset
1831 /* Only reserved locations can be tolerated in this case. */
kono
parents: 55
diff changeset
1832 linemap_assert (location < RESERVED_LOCATION_COUNT);
kono
parents: 55
diff changeset
1833 else
kono
parents: 55
diff changeset
1834 {
kono
parents: 55
diff changeset
1835 path = LINEMAP_FILE (map);
kono
parents: 55
diff changeset
1836 l = SOURCE_LINE (map, location);
kono
parents: 55
diff changeset
1837 c = SOURCE_COLUMN (map, location);
kono
parents: 55
diff changeset
1838 s = LINEMAP_SYSP (map) != 0;
kono
parents: 55
diff changeset
1839 e = location != loc;
kono
parents: 55
diff changeset
1840 if (e)
kono
parents: 55
diff changeset
1841 from = "N/A";
kono
parents: 55
diff changeset
1842 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1843 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1844 const line_map_ordinary *from_map
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1845 = linemap_included_from_linemap (set, map);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1846 from = from_map ? LINEMAP_FILE (from_map) : "<NULL>";
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1847 }
111
kono
parents: 55
diff changeset
1848 }
kono
parents: 55
diff changeset
1849
kono
parents: 55
diff changeset
1850 /* P: path, L: line, C: column, S: in-system-header, M: map address,
kono
parents: 55
diff changeset
1851 E: macro expansion?, LOC: original location, R: resolved location */
kono
parents: 55
diff changeset
1852 fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}",
kono
parents: 55
diff changeset
1853 path, from, l, c, s, (void*)map, e, loc, location);
kono
parents: 55
diff changeset
1854 }
kono
parents: 55
diff changeset
1855
kono
parents: 55
diff changeset
1856 /* Return the highest location emitted for a given file for which
kono
parents: 55
diff changeset
1857 there is a line map in SET. FILE_NAME is the file name to
kono
parents: 55
diff changeset
1858 consider. If the function returns TRUE, *LOC is set to the highest
kono
parents: 55
diff changeset
1859 location emitted for that file. */
kono
parents: 55
diff changeset
1860
kono
parents: 55
diff changeset
1861 bool
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1862 linemap_get_file_highest_location (line_maps *set,
111
kono
parents: 55
diff changeset
1863 const char *file_name,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1864 location_t *loc)
111
kono
parents: 55
diff changeset
1865 {
kono
parents: 55
diff changeset
1866 /* If the set is empty or no ordinary map has been created then
kono
parents: 55
diff changeset
1867 there is no file to look for ... */
kono
parents: 55
diff changeset
1868 if (set == NULL || set->info_ordinary.used == 0)
kono
parents: 55
diff changeset
1869 return false;
kono
parents: 55
diff changeset
1870
kono
parents: 55
diff changeset
1871 /* Now look for the last ordinary map created for FILE_NAME. */
kono
parents: 55
diff changeset
1872 int i;
kono
parents: 55
diff changeset
1873 for (i = set->info_ordinary.used - 1; i >= 0; --i)
kono
parents: 55
diff changeset
1874 {
kono
parents: 55
diff changeset
1875 const char *fname = set->info_ordinary.maps[i].to_file;
kono
parents: 55
diff changeset
1876 if (fname && !filename_cmp (fname, file_name))
kono
parents: 55
diff changeset
1877 break;
kono
parents: 55
diff changeset
1878 }
kono
parents: 55
diff changeset
1879
kono
parents: 55
diff changeset
1880 if (i < 0)
kono
parents: 55
diff changeset
1881 return false;
kono
parents: 55
diff changeset
1882
kono
parents: 55
diff changeset
1883 /* The highest location for a given map is either the starting
kono
parents: 55
diff changeset
1884 location of the next map minus one, or -- if the map is the
kono
parents: 55
diff changeset
1885 latest one -- the highest location of the set. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1886 location_t result;
111
kono
parents: 55
diff changeset
1887 if (i == (int) set->info_ordinary.used - 1)
kono
parents: 55
diff changeset
1888 result = set->highest_location;
kono
parents: 55
diff changeset
1889 else
kono
parents: 55
diff changeset
1890 result = set->info_ordinary.maps[i + 1].start_location - 1;
kono
parents: 55
diff changeset
1891
kono
parents: 55
diff changeset
1892 *loc = result;
kono
parents: 55
diff changeset
1893 return true;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1894 }
111
kono
parents: 55
diff changeset
1895
kono
parents: 55
diff changeset
1896 /* Compute and return statistics about the memory consumption of some
kono
parents: 55
diff changeset
1897 parts of the line table SET. */
kono
parents: 55
diff changeset
1898
kono
parents: 55
diff changeset
1899 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1900 linemap_get_statistics (line_maps *set,
111
kono
parents: 55
diff changeset
1901 struct linemap_stats *s)
kono
parents: 55
diff changeset
1902 {
kono
parents: 55
diff changeset
1903 long ordinary_maps_allocated_size, ordinary_maps_used_size,
kono
parents: 55
diff changeset
1904 macro_maps_allocated_size, macro_maps_used_size,
kono
parents: 55
diff changeset
1905 macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
kono
parents: 55
diff changeset
1906
kono
parents: 55
diff changeset
1907 const line_map_macro *cur_map;
kono
parents: 55
diff changeset
1908
kono
parents: 55
diff changeset
1909 ordinary_maps_allocated_size =
kono
parents: 55
diff changeset
1910 LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map_ordinary);
kono
parents: 55
diff changeset
1911
kono
parents: 55
diff changeset
1912 ordinary_maps_used_size =
kono
parents: 55
diff changeset
1913 LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map_ordinary);
kono
parents: 55
diff changeset
1914
kono
parents: 55
diff changeset
1915 macro_maps_allocated_size =
kono
parents: 55
diff changeset
1916 LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map_macro);
kono
parents: 55
diff changeset
1917
kono
parents: 55
diff changeset
1918 for (cur_map = LINEMAPS_MACRO_MAPS (set);
kono
parents: 55
diff changeset
1919 cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
kono
parents: 55
diff changeset
1920 ++cur_map)
kono
parents: 55
diff changeset
1921 {
kono
parents: 55
diff changeset
1922 unsigned i;
kono
parents: 55
diff changeset
1923
kono
parents: 55
diff changeset
1924 linemap_assert (linemap_macro_expansion_map_p (cur_map));
kono
parents: 55
diff changeset
1925
kono
parents: 55
diff changeset
1926 macro_maps_locations_size +=
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1927 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (location_t);
111
kono
parents: 55
diff changeset
1928
kono
parents: 55
diff changeset
1929 for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
kono
parents: 55
diff changeset
1930 {
kono
parents: 55
diff changeset
1931 if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
kono
parents: 55
diff changeset
1932 MACRO_MAP_LOCATIONS (cur_map)[i + 1])
kono
parents: 55
diff changeset
1933 duplicated_macro_maps_locations_size +=
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1934 sizeof (location_t);
111
kono
parents: 55
diff changeset
1935 }
kono
parents: 55
diff changeset
1936 }
kono
parents: 55
diff changeset
1937
kono
parents: 55
diff changeset
1938 macro_maps_used_size =
kono
parents: 55
diff changeset
1939 LINEMAPS_MACRO_USED (set) * sizeof (struct line_map_macro);
kono
parents: 55
diff changeset
1940
kono
parents: 55
diff changeset
1941 s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
kono
parents: 55
diff changeset
1942 s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
kono
parents: 55
diff changeset
1943 s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
kono
parents: 55
diff changeset
1944 s->ordinary_maps_used_size = ordinary_maps_used_size;
kono
parents: 55
diff changeset
1945 s->num_expanded_macros = num_expanded_macros_counter;
kono
parents: 55
diff changeset
1946 s->num_macro_tokens = num_macro_tokens_counter;
kono
parents: 55
diff changeset
1947 s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
kono
parents: 55
diff changeset
1948 s->macro_maps_allocated_size = macro_maps_allocated_size;
kono
parents: 55
diff changeset
1949 s->macro_maps_locations_size = macro_maps_locations_size;
kono
parents: 55
diff changeset
1950 s->macro_maps_used_size = macro_maps_used_size;
kono
parents: 55
diff changeset
1951 s->duplicated_macro_maps_locations_size =
kono
parents: 55
diff changeset
1952 duplicated_macro_maps_locations_size;
kono
parents: 55
diff changeset
1953 s->adhoc_table_size = (set->location_adhoc_data_map.allocated
kono
parents: 55
diff changeset
1954 * sizeof (struct location_adhoc_data));
kono
parents: 55
diff changeset
1955 s->adhoc_table_entries_used = set->location_adhoc_data_map.curr_loc;
kono
parents: 55
diff changeset
1956 }
kono
parents: 55
diff changeset
1957
kono
parents: 55
diff changeset
1958
kono
parents: 55
diff changeset
1959 /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
kono
parents: 55
diff changeset
1960 NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
kono
parents: 55
diff changeset
1961 specifies how many macro maps to dump. */
kono
parents: 55
diff changeset
1962
kono
parents: 55
diff changeset
1963 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1964 line_table_dump (FILE *stream, class line_maps *set, unsigned int num_ordinary,
111
kono
parents: 55
diff changeset
1965 unsigned int num_macro)
kono
parents: 55
diff changeset
1966 {
kono
parents: 55
diff changeset
1967 unsigned int i;
kono
parents: 55
diff changeset
1968
kono
parents: 55
diff changeset
1969 if (set == NULL)
kono
parents: 55
diff changeset
1970 return;
kono
parents: 55
diff changeset
1971
kono
parents: 55
diff changeset
1972 if (stream == NULL)
kono
parents: 55
diff changeset
1973 stream = stderr;
kono
parents: 55
diff changeset
1974
kono
parents: 55
diff changeset
1975 fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set));
kono
parents: 55
diff changeset
1976 fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set));
kono
parents: 55
diff changeset
1977 fprintf (stream, "Include stack depth: %d\n", set->depth);
kono
parents: 55
diff changeset
1978 fprintf (stream, "Highest location: %u\n", set->highest_location);
kono
parents: 55
diff changeset
1979
kono
parents: 55
diff changeset
1980 if (num_ordinary)
kono
parents: 55
diff changeset
1981 {
kono
parents: 55
diff changeset
1982 fprintf (stream, "\nOrdinary line maps\n");
kono
parents: 55
diff changeset
1983 for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++)
kono
parents: 55
diff changeset
1984 linemap_dump (stream, set, i, false);
kono
parents: 55
diff changeset
1985 fprintf (stream, "\n");
kono
parents: 55
diff changeset
1986 }
kono
parents: 55
diff changeset
1987
kono
parents: 55
diff changeset
1988 if (num_macro)
kono
parents: 55
diff changeset
1989 {
kono
parents: 55
diff changeset
1990 fprintf (stream, "\nMacro line maps\n");
kono
parents: 55
diff changeset
1991 for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++)
kono
parents: 55
diff changeset
1992 linemap_dump (stream, set, i, true);
kono
parents: 55
diff changeset
1993 fprintf (stream, "\n");
kono
parents: 55
diff changeset
1994 }
kono
parents: 55
diff changeset
1995 }
kono
parents: 55
diff changeset
1996
kono
parents: 55
diff changeset
1997 /* class rich_location. */
kono
parents: 55
diff changeset
1998
kono
parents: 55
diff changeset
1999 /* Construct a rich_location with location LOC as its initial range. */
kono
parents: 55
diff changeset
2000
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2001 rich_location::rich_location (line_maps *set, location_t loc,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2002 const range_label *label) :
111
kono
parents: 55
diff changeset
2003 m_line_table (set),
kono
parents: 55
diff changeset
2004 m_ranges (),
kono
parents: 55
diff changeset
2005 m_column_override (0),
kono
parents: 55
diff changeset
2006 m_have_expanded_location (false),
kono
parents: 55
diff changeset
2007 m_fixit_hints (),
kono
parents: 55
diff changeset
2008 m_seen_impossible_fixit (false),
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2009 m_fixits_cannot_be_auto_applied (false),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2010 m_path (NULL)
111
kono
parents: 55
diff changeset
2011 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2012 add_range (loc, SHOW_RANGE_WITH_CARET, label);
111
kono
parents: 55
diff changeset
2013 }
kono
parents: 55
diff changeset
2014
kono
parents: 55
diff changeset
2015 /* The destructor for class rich_location. */
kono
parents: 55
diff changeset
2016
kono
parents: 55
diff changeset
2017 rich_location::~rich_location ()
kono
parents: 55
diff changeset
2018 {
kono
parents: 55
diff changeset
2019 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
kono
parents: 55
diff changeset
2020 delete get_fixit_hint (i);
kono
parents: 55
diff changeset
2021 }
kono
parents: 55
diff changeset
2022
kono
parents: 55
diff changeset
2023 /* Get location IDX within this rich_location. */
kono
parents: 55
diff changeset
2024
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2025 location_t
111
kono
parents: 55
diff changeset
2026 rich_location::get_loc (unsigned int idx) const
kono
parents: 55
diff changeset
2027 {
kono
parents: 55
diff changeset
2028 const location_range *locrange = get_range (idx);
kono
parents: 55
diff changeset
2029 return locrange->m_loc;
kono
parents: 55
diff changeset
2030 }
kono
parents: 55
diff changeset
2031
kono
parents: 55
diff changeset
2032 /* Get range IDX within this rich_location. */
kono
parents: 55
diff changeset
2033
kono
parents: 55
diff changeset
2034 const location_range *
kono
parents: 55
diff changeset
2035 rich_location::get_range (unsigned int idx) const
kono
parents: 55
diff changeset
2036 {
kono
parents: 55
diff changeset
2037 return &m_ranges[idx];
kono
parents: 55
diff changeset
2038 }
kono
parents: 55
diff changeset
2039
kono
parents: 55
diff changeset
2040 /* Mutable access to range IDX within this rich_location. */
kono
parents: 55
diff changeset
2041
kono
parents: 55
diff changeset
2042 location_range *
kono
parents: 55
diff changeset
2043 rich_location::get_range (unsigned int idx)
kono
parents: 55
diff changeset
2044 {
kono
parents: 55
diff changeset
2045 return &m_ranges[idx];
kono
parents: 55
diff changeset
2046 }
kono
parents: 55
diff changeset
2047
kono
parents: 55
diff changeset
2048 /* Expand location IDX within this rich_location. */
kono
parents: 55
diff changeset
2049 /* Get an expanded_location for this rich_location's primary
kono
parents: 55
diff changeset
2050 location. */
kono
parents: 55
diff changeset
2051
kono
parents: 55
diff changeset
2052 expanded_location
kono
parents: 55
diff changeset
2053 rich_location::get_expanded_location (unsigned int idx)
kono
parents: 55
diff changeset
2054 {
kono
parents: 55
diff changeset
2055 if (idx == 0)
kono
parents: 55
diff changeset
2056 {
kono
parents: 55
diff changeset
2057 /* Cache the expansion of the primary location. */
kono
parents: 55
diff changeset
2058 if (!m_have_expanded_location)
kono
parents: 55
diff changeset
2059 {
kono
parents: 55
diff changeset
2060 m_expanded_location
kono
parents: 55
diff changeset
2061 = linemap_client_expand_location_to_spelling_point
kono
parents: 55
diff changeset
2062 (get_loc (0), LOCATION_ASPECT_CARET);
kono
parents: 55
diff changeset
2063 if (m_column_override)
kono
parents: 55
diff changeset
2064 m_expanded_location.column = m_column_override;
kono
parents: 55
diff changeset
2065 m_have_expanded_location = true;
kono
parents: 55
diff changeset
2066 }
kono
parents: 55
diff changeset
2067
kono
parents: 55
diff changeset
2068 return m_expanded_location;
kono
parents: 55
diff changeset
2069 }
kono
parents: 55
diff changeset
2070 else
kono
parents: 55
diff changeset
2071 return linemap_client_expand_location_to_spelling_point
kono
parents: 55
diff changeset
2072 (get_loc (idx), LOCATION_ASPECT_CARET);
kono
parents: 55
diff changeset
2073 }
kono
parents: 55
diff changeset
2074
kono
parents: 55
diff changeset
2075 /* Set the column of the primary location, with 0 meaning
kono
parents: 55
diff changeset
2076 "don't override it". */
kono
parents: 55
diff changeset
2077
kono
parents: 55
diff changeset
2078 void
kono
parents: 55
diff changeset
2079 rich_location::override_column (int column)
kono
parents: 55
diff changeset
2080 {
kono
parents: 55
diff changeset
2081 m_column_override = column;
kono
parents: 55
diff changeset
2082 m_have_expanded_location = false;
kono
parents: 55
diff changeset
2083 }
kono
parents: 55
diff changeset
2084
kono
parents: 55
diff changeset
2085 /* Add the given range. */
kono
parents: 55
diff changeset
2086
kono
parents: 55
diff changeset
2087 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2088 rich_location::add_range (location_t loc,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2089 enum range_display_kind range_display_kind,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2090 const range_label *label)
111
kono
parents: 55
diff changeset
2091 {
kono
parents: 55
diff changeset
2092 location_range range;
kono
parents: 55
diff changeset
2093 range.m_loc = loc;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2094 range.m_range_display_kind = range_display_kind;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2095 range.m_label = label;
111
kono
parents: 55
diff changeset
2096 m_ranges.push (range);
kono
parents: 55
diff changeset
2097 }
kono
parents: 55
diff changeset
2098
kono
parents: 55
diff changeset
2099 /* Add or overwrite the location given by IDX, setting its location to LOC,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2100 and setting its m_range_display_kind to RANGE_DISPLAY_KIND.
111
kono
parents: 55
diff changeset
2101
kono
parents: 55
diff changeset
2102 It must either overwrite an existing location, or add one *exactly* on
kono
parents: 55
diff changeset
2103 the end of the array.
kono
parents: 55
diff changeset
2104
kono
parents: 55
diff changeset
2105 This is primarily for use by gcc when implementing diagnostic format
kono
parents: 55
diff changeset
2106 decoders e.g.
kono
parents: 55
diff changeset
2107 - the "+" in the C/C++ frontends, for handling format codes like "%q+D"
kono
parents: 55
diff changeset
2108 (which writes the source location of a tree back into location 0 of
kono
parents: 55
diff changeset
2109 the rich_location), and
kono
parents: 55
diff changeset
2110 - the "%C" and "%L" format codes in the Fortran frontend. */
kono
parents: 55
diff changeset
2111
kono
parents: 55
diff changeset
2112 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2113 rich_location::set_range (unsigned int idx, location_t loc,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2114 enum range_display_kind range_display_kind)
111
kono
parents: 55
diff changeset
2115 {
kono
parents: 55
diff changeset
2116 /* We can either overwrite an existing range, or add one exactly
kono
parents: 55
diff changeset
2117 on the end of the array. */
kono
parents: 55
diff changeset
2118 linemap_assert (idx <= m_ranges.count ());
kono
parents: 55
diff changeset
2119
kono
parents: 55
diff changeset
2120 if (idx == m_ranges.count ())
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2121 add_range (loc, range_display_kind);
111
kono
parents: 55
diff changeset
2122 else
kono
parents: 55
diff changeset
2123 {
kono
parents: 55
diff changeset
2124 location_range *locrange = get_range (idx);
kono
parents: 55
diff changeset
2125 locrange->m_loc = loc;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2126 locrange->m_range_display_kind = range_display_kind;
111
kono
parents: 55
diff changeset
2127 }
kono
parents: 55
diff changeset
2128
kono
parents: 55
diff changeset
2129 if (idx == 0)
kono
parents: 55
diff changeset
2130 /* Mark any cached value here as dirty. */
kono
parents: 55
diff changeset
2131 m_have_expanded_location = false;
kono
parents: 55
diff changeset
2132 }
kono
parents: 55
diff changeset
2133
kono
parents: 55
diff changeset
2134 /* Methods for adding insertion fix-it hints. */
kono
parents: 55
diff changeset
2135
kono
parents: 55
diff changeset
2136 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
kono
parents: 55
diff changeset
2137 immediately before the primary range's start location. */
kono
parents: 55
diff changeset
2138
kono
parents: 55
diff changeset
2139 void
kono
parents: 55
diff changeset
2140 rich_location::add_fixit_insert_before (const char *new_content)
kono
parents: 55
diff changeset
2141 {
kono
parents: 55
diff changeset
2142 add_fixit_insert_before (get_loc (), new_content);
kono
parents: 55
diff changeset
2143 }
kono
parents: 55
diff changeset
2144
kono
parents: 55
diff changeset
2145 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
kono
parents: 55
diff changeset
2146 immediately before the start of WHERE. */
kono
parents: 55
diff changeset
2147
kono
parents: 55
diff changeset
2148 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2149 rich_location::add_fixit_insert_before (location_t where,
111
kono
parents: 55
diff changeset
2150 const char *new_content)
kono
parents: 55
diff changeset
2151 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2152 location_t start = get_range_from_loc (m_line_table, where).m_start;
111
kono
parents: 55
diff changeset
2153 maybe_add_fixit (start, start, new_content);
kono
parents: 55
diff changeset
2154 }
kono
parents: 55
diff changeset
2155
kono
parents: 55
diff changeset
2156 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
kono
parents: 55
diff changeset
2157 immediately after the primary range's end-point. */
kono
parents: 55
diff changeset
2158
kono
parents: 55
diff changeset
2159 void
kono
parents: 55
diff changeset
2160 rich_location::add_fixit_insert_after (const char *new_content)
kono
parents: 55
diff changeset
2161 {
kono
parents: 55
diff changeset
2162 add_fixit_insert_after (get_loc (), new_content);
kono
parents: 55
diff changeset
2163 }
kono
parents: 55
diff changeset
2164
kono
parents: 55
diff changeset
2165 /* Add a fixit-hint, suggesting insertion of NEW_CONTENT
kono
parents: 55
diff changeset
2166 immediately after the end-point of WHERE. */
kono
parents: 55
diff changeset
2167
kono
parents: 55
diff changeset
2168 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2169 rich_location::add_fixit_insert_after (location_t where,
111
kono
parents: 55
diff changeset
2170 const char *new_content)
kono
parents: 55
diff changeset
2171 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2172 location_t finish = get_range_from_loc (m_line_table, where).m_finish;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2173 location_t next_loc
111
kono
parents: 55
diff changeset
2174 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
kono
parents: 55
diff changeset
2175
kono
parents: 55
diff changeset
2176 /* linemap_position_for_loc_and_offset can fail, if so, it returns
kono
parents: 55
diff changeset
2177 its input value. */
kono
parents: 55
diff changeset
2178 if (next_loc == finish)
kono
parents: 55
diff changeset
2179 {
kono
parents: 55
diff changeset
2180 stop_supporting_fixits ();
kono
parents: 55
diff changeset
2181 return;
kono
parents: 55
diff changeset
2182 }
kono
parents: 55
diff changeset
2183
kono
parents: 55
diff changeset
2184 maybe_add_fixit (next_loc, next_loc, new_content);
kono
parents: 55
diff changeset
2185 }
kono
parents: 55
diff changeset
2186
kono
parents: 55
diff changeset
2187 /* Methods for adding removal fix-it hints. */
kono
parents: 55
diff changeset
2188
kono
parents: 55
diff changeset
2189 /* Add a fixit-hint, suggesting removal of the content covered
kono
parents: 55
diff changeset
2190 by range 0. */
kono
parents: 55
diff changeset
2191
kono
parents: 55
diff changeset
2192 void
kono
parents: 55
diff changeset
2193 rich_location::add_fixit_remove ()
kono
parents: 55
diff changeset
2194 {
kono
parents: 55
diff changeset
2195 add_fixit_remove (get_loc ());
kono
parents: 55
diff changeset
2196 }
kono
parents: 55
diff changeset
2197
kono
parents: 55
diff changeset
2198 /* Add a fixit-hint, suggesting removal of the content between
kono
parents: 55
diff changeset
2199 the start and finish of WHERE. */
kono
parents: 55
diff changeset
2200
kono
parents: 55
diff changeset
2201 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2202 rich_location::add_fixit_remove (location_t where)
111
kono
parents: 55
diff changeset
2203 {
kono
parents: 55
diff changeset
2204 source_range range = get_range_from_loc (m_line_table, where);
kono
parents: 55
diff changeset
2205 add_fixit_remove (range);
kono
parents: 55
diff changeset
2206 }
kono
parents: 55
diff changeset
2207
kono
parents: 55
diff changeset
2208 /* Add a fixit-hint, suggesting removal of the content at
kono
parents: 55
diff changeset
2209 SRC_RANGE. */
kono
parents: 55
diff changeset
2210
kono
parents: 55
diff changeset
2211 void
kono
parents: 55
diff changeset
2212 rich_location::add_fixit_remove (source_range src_range)
kono
parents: 55
diff changeset
2213 {
kono
parents: 55
diff changeset
2214 add_fixit_replace (src_range, "");
kono
parents: 55
diff changeset
2215 }
kono
parents: 55
diff changeset
2216
kono
parents: 55
diff changeset
2217 /* Add a fixit-hint, suggesting replacement of the content covered
kono
parents: 55
diff changeset
2218 by range 0 with NEW_CONTENT. */
kono
parents: 55
diff changeset
2219
kono
parents: 55
diff changeset
2220 void
kono
parents: 55
diff changeset
2221 rich_location::add_fixit_replace (const char *new_content)
kono
parents: 55
diff changeset
2222 {
kono
parents: 55
diff changeset
2223 add_fixit_replace (get_loc (), new_content);
kono
parents: 55
diff changeset
2224 }
kono
parents: 55
diff changeset
2225
kono
parents: 55
diff changeset
2226 /* Methods for adding "replace" fix-it hints. */
kono
parents: 55
diff changeset
2227
kono
parents: 55
diff changeset
2228 /* Add a fixit-hint, suggesting replacement of the content between
kono
parents: 55
diff changeset
2229 the start and finish of WHERE with NEW_CONTENT. */
kono
parents: 55
diff changeset
2230
kono
parents: 55
diff changeset
2231 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2232 rich_location::add_fixit_replace (location_t where,
111
kono
parents: 55
diff changeset
2233 const char *new_content)
kono
parents: 55
diff changeset
2234 {
kono
parents: 55
diff changeset
2235 source_range range = get_range_from_loc (m_line_table, where);
kono
parents: 55
diff changeset
2236 add_fixit_replace (range, new_content);
kono
parents: 55
diff changeset
2237 }
kono
parents: 55
diff changeset
2238
kono
parents: 55
diff changeset
2239 /* Add a fixit-hint, suggesting replacement of the content at
kono
parents: 55
diff changeset
2240 SRC_RANGE with NEW_CONTENT. */
kono
parents: 55
diff changeset
2241
kono
parents: 55
diff changeset
2242 void
kono
parents: 55
diff changeset
2243 rich_location::add_fixit_replace (source_range src_range,
kono
parents: 55
diff changeset
2244 const char *new_content)
kono
parents: 55
diff changeset
2245 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2246 location_t start = get_pure_location (m_line_table, src_range.m_start);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2247 location_t finish = get_pure_location (m_line_table, src_range.m_finish);
111
kono
parents: 55
diff changeset
2248
kono
parents: 55
diff changeset
2249 /* Fix-it hints use half-closed ranges, so attempt to offset the endpoint. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2250 location_t next_loc
111
kono
parents: 55
diff changeset
2251 = linemap_position_for_loc_and_offset (m_line_table, finish, 1);
kono
parents: 55
diff changeset
2252 /* linemap_position_for_loc_and_offset can fail, if so, it returns
kono
parents: 55
diff changeset
2253 its input value. */
kono
parents: 55
diff changeset
2254 if (next_loc == finish)
kono
parents: 55
diff changeset
2255 {
kono
parents: 55
diff changeset
2256 stop_supporting_fixits ();
kono
parents: 55
diff changeset
2257 return;
kono
parents: 55
diff changeset
2258 }
kono
parents: 55
diff changeset
2259 finish = next_loc;
kono
parents: 55
diff changeset
2260
kono
parents: 55
diff changeset
2261 maybe_add_fixit (start, finish, new_content);
kono
parents: 55
diff changeset
2262 }
kono
parents: 55
diff changeset
2263
kono
parents: 55
diff changeset
2264 /* Get the last fix-it hint within this rich_location, or NULL if none. */
kono
parents: 55
diff changeset
2265
kono
parents: 55
diff changeset
2266 fixit_hint *
kono
parents: 55
diff changeset
2267 rich_location::get_last_fixit_hint () const
kono
parents: 55
diff changeset
2268 {
kono
parents: 55
diff changeset
2269 if (m_fixit_hints.count () > 0)
kono
parents: 55
diff changeset
2270 return get_fixit_hint (m_fixit_hints.count () - 1);
kono
parents: 55
diff changeset
2271 else
kono
parents: 55
diff changeset
2272 return NULL;
kono
parents: 55
diff changeset
2273 }
kono
parents: 55
diff changeset
2274
kono
parents: 55
diff changeset
2275 /* If WHERE is an "awkward" location, then mark this rich_location as not
kono
parents: 55
diff changeset
2276 supporting fixits, purging any thay were already added, and return true.
kono
parents: 55
diff changeset
2277
kono
parents: 55
diff changeset
2278 Otherwise (the common case), return false. */
kono
parents: 55
diff changeset
2279
kono
parents: 55
diff changeset
2280 bool
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2281 rich_location::reject_impossible_fixit (location_t where)
111
kono
parents: 55
diff changeset
2282 {
kono
parents: 55
diff changeset
2283 /* Fix-its within a rich_location should either all be suggested, or
kono
parents: 55
diff changeset
2284 none of them should be suggested.
kono
parents: 55
diff changeset
2285 Once we've rejected a fixit, we reject any more, even those
kono
parents: 55
diff changeset
2286 with reasonable locations. */
kono
parents: 55
diff changeset
2287 if (m_seen_impossible_fixit)
kono
parents: 55
diff changeset
2288 return true;
kono
parents: 55
diff changeset
2289
kono
parents: 55
diff changeset
2290 if (where <= LINE_MAP_MAX_LOCATION_WITH_COLS)
kono
parents: 55
diff changeset
2291 /* WHERE is a reasonable location for a fix-it; don't reject it. */
kono
parents: 55
diff changeset
2292 return false;
kono
parents: 55
diff changeset
2293
kono
parents: 55
diff changeset
2294 /* Otherwise we have an attempt to add a fix-it with an "awkward"
kono
parents: 55
diff changeset
2295 location: either one that we can't obtain column information
kono
parents: 55
diff changeset
2296 for (within an ordinary map), or one within a macro expansion. */
kono
parents: 55
diff changeset
2297 stop_supporting_fixits ();
kono
parents: 55
diff changeset
2298 return true;
kono
parents: 55
diff changeset
2299 }
kono
parents: 55
diff changeset
2300
kono
parents: 55
diff changeset
2301 /* Mark this rich_location as not supporting fixits, purging any that were
kono
parents: 55
diff changeset
2302 already added. */
kono
parents: 55
diff changeset
2303
kono
parents: 55
diff changeset
2304 void
kono
parents: 55
diff changeset
2305 rich_location::stop_supporting_fixits ()
kono
parents: 55
diff changeset
2306 {
kono
parents: 55
diff changeset
2307 m_seen_impossible_fixit = true;
kono
parents: 55
diff changeset
2308
kono
parents: 55
diff changeset
2309 /* Purge the rich_location of any fix-its that were already added. */
kono
parents: 55
diff changeset
2310 for (unsigned int i = 0; i < m_fixit_hints.count (); i++)
kono
parents: 55
diff changeset
2311 delete get_fixit_hint (i);
kono
parents: 55
diff changeset
2312 m_fixit_hints.truncate (0);
kono
parents: 55
diff changeset
2313 }
kono
parents: 55
diff changeset
2314
kono
parents: 55
diff changeset
2315 /* Add HINT to the fix-it hints in this rich_location,
kono
parents: 55
diff changeset
2316 consolidating into the prior fixit if possible. */
kono
parents: 55
diff changeset
2317
kono
parents: 55
diff changeset
2318 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2319 rich_location::maybe_add_fixit (location_t start,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2320 location_t next_loc,
111
kono
parents: 55
diff changeset
2321 const char *new_content)
kono
parents: 55
diff changeset
2322 {
kono
parents: 55
diff changeset
2323 if (reject_impossible_fixit (start))
kono
parents: 55
diff changeset
2324 return;
kono
parents: 55
diff changeset
2325 if (reject_impossible_fixit (next_loc))
kono
parents: 55
diff changeset
2326 return;
kono
parents: 55
diff changeset
2327
kono
parents: 55
diff changeset
2328 /* Only allow fix-it hints that affect a single line in one file.
kono
parents: 55
diff changeset
2329 Compare the end-points. */
kono
parents: 55
diff changeset
2330 expanded_location exploc_start
kono
parents: 55
diff changeset
2331 = linemap_client_expand_location_to_spelling_point (start,
kono
parents: 55
diff changeset
2332 LOCATION_ASPECT_START);
kono
parents: 55
diff changeset
2333 expanded_location exploc_next_loc
kono
parents: 55
diff changeset
2334 = linemap_client_expand_location_to_spelling_point (next_loc,
kono
parents: 55
diff changeset
2335 LOCATION_ASPECT_START);
kono
parents: 55
diff changeset
2336 /* They must be within the same file... */
kono
parents: 55
diff changeset
2337 if (exploc_start.file != exploc_next_loc.file)
kono
parents: 55
diff changeset
2338 {
kono
parents: 55
diff changeset
2339 stop_supporting_fixits ();
kono
parents: 55
diff changeset
2340 return;
kono
parents: 55
diff changeset
2341 }
kono
parents: 55
diff changeset
2342 /* ...and on the same line. */
kono
parents: 55
diff changeset
2343 if (exploc_start.line != exploc_next_loc.line)
kono
parents: 55
diff changeset
2344 {
kono
parents: 55
diff changeset
2345 stop_supporting_fixits ();
kono
parents: 55
diff changeset
2346 return;
kono
parents: 55
diff changeset
2347 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2348 /* The columns must be in the correct order. This can fail if the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2349 endpoints straddle the boundary for which the linemap can represent
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2350 columns (PR c/82050). */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2351 if (exploc_start.column > exploc_next_loc.column)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2352 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2353 stop_supporting_fixits ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2354 return;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2355 }
111
kono
parents: 55
diff changeset
2356
kono
parents: 55
diff changeset
2357 const char *newline = strchr (new_content, '\n');
kono
parents: 55
diff changeset
2358 if (newline)
kono
parents: 55
diff changeset
2359 {
kono
parents: 55
diff changeset
2360 /* For now, we can only support insertion of whole lines
kono
parents: 55
diff changeset
2361 i.e. starts at start of line, and the newline is at the end of
kono
parents: 55
diff changeset
2362 the insertion point. */
kono
parents: 55
diff changeset
2363
kono
parents: 55
diff changeset
2364 /* It must be an insertion, not a replacement/deletion. */
kono
parents: 55
diff changeset
2365 if (start != next_loc)
kono
parents: 55
diff changeset
2366 {
kono
parents: 55
diff changeset
2367 stop_supporting_fixits ();
kono
parents: 55
diff changeset
2368 return;
kono
parents: 55
diff changeset
2369 }
kono
parents: 55
diff changeset
2370
kono
parents: 55
diff changeset
2371 /* The insertion must be at the start of a line. */
kono
parents: 55
diff changeset
2372 if (exploc_start.column != 1)
kono
parents: 55
diff changeset
2373 {
kono
parents: 55
diff changeset
2374 stop_supporting_fixits ();
kono
parents: 55
diff changeset
2375 return;
kono
parents: 55
diff changeset
2376 }
kono
parents: 55
diff changeset
2377
kono
parents: 55
diff changeset
2378 /* The newline must be at end of NEW_CONTENT.
kono
parents: 55
diff changeset
2379 We could eventually split up fix-its at newlines if we wanted
kono
parents: 55
diff changeset
2380 to allow more generality (e.g. to allow adding multiple lines
kono
parents: 55
diff changeset
2381 with one add_fixit call. */
kono
parents: 55
diff changeset
2382 if (newline[1] != '\0')
kono
parents: 55
diff changeset
2383 {
kono
parents: 55
diff changeset
2384 stop_supporting_fixits ();
kono
parents: 55
diff changeset
2385 return;
kono
parents: 55
diff changeset
2386 }
kono
parents: 55
diff changeset
2387 }
kono
parents: 55
diff changeset
2388
kono
parents: 55
diff changeset
2389 /* Consolidate neighboring fixits.
kono
parents: 55
diff changeset
2390 Don't consolidate into newline-insertion fixits. */
kono
parents: 55
diff changeset
2391 fixit_hint *prev = get_last_fixit_hint ();
kono
parents: 55
diff changeset
2392 if (prev && !prev->ends_with_newline_p ())
kono
parents: 55
diff changeset
2393 if (prev->maybe_append (start, next_loc, new_content))
kono
parents: 55
diff changeset
2394 return;
kono
parents: 55
diff changeset
2395
kono
parents: 55
diff changeset
2396 m_fixit_hints.push (new fixit_hint (start, next_loc, new_content));
kono
parents: 55
diff changeset
2397 }
kono
parents: 55
diff changeset
2398
kono
parents: 55
diff changeset
2399 /* class fixit_hint. */
kono
parents: 55
diff changeset
2400
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2401 fixit_hint::fixit_hint (location_t start,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2402 location_t next_loc,
111
kono
parents: 55
diff changeset
2403 const char *new_content)
kono
parents: 55
diff changeset
2404 : m_start (start),
kono
parents: 55
diff changeset
2405 m_next_loc (next_loc),
kono
parents: 55
diff changeset
2406 m_bytes (xstrdup (new_content)),
kono
parents: 55
diff changeset
2407 m_len (strlen (new_content))
kono
parents: 55
diff changeset
2408 {
kono
parents: 55
diff changeset
2409 }
kono
parents: 55
diff changeset
2410
kono
parents: 55
diff changeset
2411 /* Does this fix-it hint affect the given line? */
kono
parents: 55
diff changeset
2412
kono
parents: 55
diff changeset
2413 bool
kono
parents: 55
diff changeset
2414 fixit_hint::affects_line_p (const char *file, int line) const
kono
parents: 55
diff changeset
2415 {
kono
parents: 55
diff changeset
2416 expanded_location exploc_start
kono
parents: 55
diff changeset
2417 = linemap_client_expand_location_to_spelling_point (m_start,
kono
parents: 55
diff changeset
2418 LOCATION_ASPECT_START);
kono
parents: 55
diff changeset
2419 if (file != exploc_start.file)
kono
parents: 55
diff changeset
2420 return false;
kono
parents: 55
diff changeset
2421 if (line < exploc_start.line)
kono
parents: 55
diff changeset
2422 return false;
kono
parents: 55
diff changeset
2423 expanded_location exploc_next_loc
kono
parents: 55
diff changeset
2424 = linemap_client_expand_location_to_spelling_point (m_next_loc,
kono
parents: 55
diff changeset
2425 LOCATION_ASPECT_START);
kono
parents: 55
diff changeset
2426 if (file != exploc_next_loc.file)
kono
parents: 55
diff changeset
2427 return false;
kono
parents: 55
diff changeset
2428 if (line > exploc_next_loc.line)
kono
parents: 55
diff changeset
2429 return false;
kono
parents: 55
diff changeset
2430 return true;
kono
parents: 55
diff changeset
2431 }
kono
parents: 55
diff changeset
2432
kono
parents: 55
diff changeset
2433 /* Method for consolidating fix-it hints, for use by
kono
parents: 55
diff changeset
2434 rich_location::maybe_add_fixit.
kono
parents: 55
diff changeset
2435 If possible, merge a pending fix-it hint with the given params
kono
parents: 55
diff changeset
2436 into this one and return true.
kono
parents: 55
diff changeset
2437 Otherwise return false. */
kono
parents: 55
diff changeset
2438
kono
parents: 55
diff changeset
2439 bool
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2440 fixit_hint::maybe_append (location_t start,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2441 location_t next_loc,
111
kono
parents: 55
diff changeset
2442 const char *new_content)
kono
parents: 55
diff changeset
2443 {
kono
parents: 55
diff changeset
2444 /* For consolidation to be possible, START must be at this hint's
kono
parents: 55
diff changeset
2445 m_next_loc. */
kono
parents: 55
diff changeset
2446 if (start != m_next_loc)
kono
parents: 55
diff changeset
2447 return false;
kono
parents: 55
diff changeset
2448
kono
parents: 55
diff changeset
2449 /* If so, we have neighboring replacements; merge them. */
kono
parents: 55
diff changeset
2450 m_next_loc = next_loc;
kono
parents: 55
diff changeset
2451 size_t extra_len = strlen (new_content);
kono
parents: 55
diff changeset
2452 m_bytes = (char *)xrealloc (m_bytes, m_len + extra_len + 1);
kono
parents: 55
diff changeset
2453 memcpy (m_bytes + m_len, new_content, extra_len);
kono
parents: 55
diff changeset
2454 m_len += extra_len;
kono
parents: 55
diff changeset
2455 m_bytes[m_len] = '\0';
kono
parents: 55
diff changeset
2456 return true;
kono
parents: 55
diff changeset
2457 }
kono
parents: 55
diff changeset
2458
kono
parents: 55
diff changeset
2459 /* Return true iff this hint's content ends with a newline. */
kono
parents: 55
diff changeset
2460
kono
parents: 55
diff changeset
2461 bool
kono
parents: 55
diff changeset
2462 fixit_hint::ends_with_newline_p () const
kono
parents: 55
diff changeset
2463 {
kono
parents: 55
diff changeset
2464 if (m_len == 0)
kono
parents: 55
diff changeset
2465 return false;
kono
parents: 55
diff changeset
2466 return m_bytes[m_len - 1] == '\n';
kono
parents: 55
diff changeset
2467 }