annotate libcpp/line-map.c @ 120:f93fa5091070

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