annotate libcpp/line-map.c @ 136:4627f235cf2a

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