OSDN Git Service

2004-05-24 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / line-map.c
1 /* Map logical line numbers to (source file, line number) pairs.
2    Copyright (C) 2001, 2003, 2004
3    Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19  In other words, you are welcome to use, share and improve this program.
20  You are forbidden to forbid anyone else to use, share and improve
21  what you give them.   Help stamp out software-hoarding!  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "line-map.h"
26 #include "intl.h"
27
28 static void trace_include (const struct line_maps *, const struct line_map *);
29
30 /* Initialize a line map set.  */
31
32 void
33 linemap_init (struct line_maps *set)
34 {
35   set->maps = NULL;
36   set->allocated = 0;
37   set->used = 0;
38   set->last_listed = -1;
39   set->trace_includes = false;
40   set->depth = 0;
41   set->cache = 0;
42   set->highest_location = 0;
43   set->highest_line = 0;
44   set->max_column_hint = 0;
45 }
46
47 /* Check for and warn about line_maps entered but not exited.  */
48
49 void
50 linemap_check_files_exited (struct line_maps *set)
51 {
52   struct line_map *map;
53   /* Depending upon whether we are handling preprocessed input or
54      not, this can be a user error or an ICE.  */
55   for (map = &set->maps[set->used - 1]; ! MAIN_FILE_P (map);
56        map = INCLUDED_FROM (set, map))
57     fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
58              map->to_file);
59 }
60  
61 /* Free a line map set.  */
62
63 void
64 linemap_free (struct line_maps *set)
65 {
66   if (set->maps)
67     {
68       linemap_check_files_exited (set);
69
70       free (set->maps);
71     }
72 }
73
74 /* Add a mapping of logical source line to physical source file and
75    line number.
76
77    The text pointed to by TO_FILE must have a lifetime
78    at least as long as the final call to lookup_line ().  An empty
79    TO_FILE means standard input.  If reason is LC_LEAVE, and
80    TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
81    natural values considering the file we are returning to.
82
83    FROM_LINE should be monotonic increasing across calls to this
84    function.  A call to this function can relocate the previous set of
85    A call to this function can relocate the previous set of
86    maps, so any stored line_map pointers should not be used.  */
87
88 const struct line_map *
89 linemap_add (struct line_maps *set, enum lc_reason reason,
90              unsigned int sysp, const char *to_file, unsigned int to_line)
91 {
92   struct line_map *map;
93   source_location start_location = set->highest_location + 1;
94
95   if (set->used && start_location < set->maps[set->used - 1].start_location)
96     abort ();
97
98   if (set->used == set->allocated)
99     {
100       set->allocated = 2 * set->allocated + 256;
101       set->maps = xrealloc (set->maps, set->allocated * sizeof (struct line_map));
102     }
103
104   map = &set->maps[set->used];
105
106   if (to_file && *to_file == '\0')
107     to_file = "<stdin>";
108
109   /* If we don't keep our line maps consistent, we can easily
110      segfault.  Don't rely on the client to do it for us.  */
111   if (set->depth == 0)
112     reason = LC_ENTER;
113   else if (reason == LC_LEAVE)
114     {
115       struct line_map *from;
116       bool error;
117
118       if (MAIN_FILE_P (map - 1))
119         {
120           if (to_file == NULL)
121             {
122               set->depth--;
123               return NULL;
124             }
125           error = true;
126           reason = LC_RENAME;
127           from = map - 1;
128         }
129       else
130         {
131           from = INCLUDED_FROM (set, map - 1);
132           error = to_file && strcmp (from->to_file, to_file);
133         }
134
135       /* Depending upon whether we are handling preprocessed input or
136          not, this can be a user error or an ICE.  */
137       if (error)
138         fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
139                  to_file);
140
141       /* A TO_FILE of NULL is special - we use the natural values.  */
142       if (error || to_file == NULL)
143         {
144           to_file = from->to_file;
145           to_line = SOURCE_LINE (from, from[1].start_location);
146           sysp = from->sysp;
147         }
148     }
149
150   map->reason = reason;
151   map->sysp = sysp;
152   map->start_location = start_location;
153   map->to_file = to_file;
154   map->to_line = to_line;
155   set->cache = set->used++;
156   map->column_bits = 0;
157   set->highest_location = start_location;
158   set->highest_line = start_location;
159   set->max_column_hint = 0;
160
161   if (reason == LC_ENTER)
162     {
163       map->included_from = set->depth == 0 ? -1 : (int) (set->used - 2);
164       set->depth++;
165       if (set->trace_includes)
166         trace_include (set, map);
167     }
168   else if (reason == LC_RENAME)
169     map->included_from = map[-1].included_from;
170   else if (reason == LC_LEAVE)
171     {
172       set->depth--;
173       map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
174     }
175
176   return map;
177 }
178
179 source_location
180 linemap_line_start (struct line_maps *set, unsigned int to_line,
181                     unsigned int max_column_hint)
182 {
183   struct line_map *map = &set->maps[set->used - 1];
184   source_location highest = set->highest_location;
185   source_location r;
186   unsigned int last_line = SOURCE_LINE (map, set->highest_line);
187   int line_delta = to_line - last_line;
188   bool add_map = false;
189   if (line_delta < 0
190       || (line_delta > 10 && line_delta * map->column_bits > 1000)
191       || (max_column_hint >= (1U << map->column_bits))
192       || (max_column_hint <= 80 && map->column_bits >= 10))
193     {
194       add_map = true;
195     }
196   else
197     max_column_hint = set->max_column_hint;
198   if (add_map)
199     {
200       int column_bits;
201       if (max_column_hint > 100000 || highest > 0xC0000000)
202         {
203           max_column_hint = 0;
204           if (highest >0xF0000000)
205             return 0;
206           column_bits = 0;
207         }
208       else
209         {
210           column_bits = 7;
211           while (max_column_hint >= (1U << column_bits))
212             column_bits++;
213           max_column_hint = 1U << column_bits;
214         }
215       if (line_delta < 0
216           || last_line != map->to_line
217           || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
218         map = (struct line_map*) linemap_add (set, LC_RENAME, map->sysp,
219                                       map->to_file, to_line);
220       map->column_bits = column_bits;
221       r = map->start_location;
222     }
223   else
224     r = highest - SOURCE_COLUMN (map, highest)
225       + (line_delta << map->column_bits);
226   set->highest_line = r;
227   if (r > set->highest_location)
228     set->highest_location = r;
229   set->max_column_hint = max_column_hint;
230   return r;
231 }
232
233 source_location
234 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
235 {
236   source_location r = set->highest_line;
237   if (to_column >= set->max_column_hint)
238     {
239       if (r >= 0xC000000 || to_column > 100000)
240         {
241           /* Running low on source_locations - disable column numbers.  */
242           return r;
243         }
244       else
245         {
246           struct line_map *map = &set->maps[set->used - 1];
247           r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
248         }
249     }
250   r = r + to_column;
251   if (r >= set->highest_location)
252     set->highest_location = r;
253   return r;
254 }
255
256 /* Given a logical line, returns the map from which the corresponding
257    (source file, line) pair can be deduced.  Since the set is built
258    chronologically, the logical lines are monotonic increasing, and so
259    the list is sorted and we can use a binary search.  */
260
261 const struct line_map *
262 linemap_lookup (struct line_maps *set, source_location line)
263 {
264   unsigned int md, mn, mx;
265   const struct line_map *cached;
266
267   mn = set->cache;
268   mx = set->used;
269   
270   cached = &set->maps[mn];
271   /* We should get a segfault if no line_maps have been added yet.  */
272   if (line >= cached->start_location)
273     {
274       if (mn + 1 == mx || line < cached[1].start_location)
275         return cached;
276     }
277   else
278     {
279       mx = mn;
280       mn = 0;
281     }
282
283   while (mx - mn > 1)
284     {
285       md = (mn + mx) / 2;
286       if (set->maps[md].start_location > line)
287         mx = md;
288       else
289         mn = md;
290     }
291
292   set->cache = mn;
293   return &set->maps[mn];
294 }
295
296 /* Print the file names and line numbers of the #include commands
297    which led to the map MAP, if any, to stderr.  Nothing is output if
298    the most recently listed stack is the same as the current one.  */
299
300 void
301 linemap_print_containing_files (struct line_maps *set,
302                                 const struct line_map *map)
303 {
304   if (MAIN_FILE_P (map) || set->last_listed == map->included_from)
305     return;
306
307   set->last_listed = map->included_from;
308   map = INCLUDED_FROM (set, map);
309
310   fprintf (stderr,  _("In file included from %s:%u"),
311            map->to_file, LAST_SOURCE_LINE (map));
312
313   while (! MAIN_FILE_P (map))
314     {
315       map = INCLUDED_FROM (set, map);
316       /* Translators note: this message is used in conjunction
317          with "In file included from %s:%ld" and some other
318          tricks.  We want something like this:
319
320          | In file included from sys/select.h:123,
321          |                  from sys/types.h:234,
322          |                  from userfile.c:31:
323          | bits/select.h:45: <error message here>
324
325          with all the "from"s lined up.
326          The trailing comma is at the beginning of this message,
327          and the trailing colon is not translated.  */
328       fprintf (stderr, _(",\n                 from %s:%u"),
329                map->to_file, LAST_SOURCE_LINE (map));
330     }
331
332   fputs (":\n", stderr);
333 }
334
335 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
336
337 static void
338 trace_include (const struct line_maps *set, const struct line_map *map)
339 {
340   unsigned int i = set->depth;
341
342   while (--i)
343     putc ('.', stderr);
344   fprintf (stderr, " %s\n", map->to_file);
345 }