OSDN Git Service

gccgo: Added code to dump the AST tree. The AST dump is
[pf3gnuchains/gcc-fork.git] / libcpp / line-map.c
1 /* Map logical line numbers to (source file, line number) pairs.
2    Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009
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 3, 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; see the file COPYING3.  If not see
17 <http://www.gnu.org/licenses/>.
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
27 static void trace_include (const struct line_maps *, const struct line_map *);
28
29 /* Initialize a line map set.  */
30
31 void
32 linemap_init (struct line_maps *set)
33 {
34   set->maps = NULL;
35   set->allocated = 0;
36   set->used = 0;
37   set->trace_includes = false;
38   set->depth = 0;
39   set->cache = 0;
40   set->highest_location = RESERVED_LOCATION_COUNT - 1;
41   set->highest_line = RESERVED_LOCATION_COUNT - 1;
42   set->max_column_hint = 0;
43   set->reallocator = 0;
44 }
45
46 /* Check for and warn about line_maps entered but not exited.  */
47
48 void
49 linemap_check_files_exited (struct line_maps *set)
50 {
51   struct line_map *map;
52   /* Depending upon whether we are handling preprocessed input or
53      not, this can be a user error or an ICE.  */
54   for (map = &set->maps[set->used - 1]; ! MAIN_FILE_P (map);
55        map = INCLUDED_FROM (set, map))
56     fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
57              map->to_file);
58 }
59  
60 /* Free a line map set.  */
61
62 void
63 linemap_free (struct line_maps *set)
64 {
65   if (set->maps)
66     {
67       linemap_check_files_exited (set);
68
69       free (set->maps);
70     }
71 }
72
73 /* Add a mapping of logical source line to physical source file and
74    line number.
75
76    The text pointed to by TO_FILE must have a lifetime
77    at least as long as the final call to lookup_line ().  An empty
78    TO_FILE means standard input.  If reason is LC_LEAVE, and
79    TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
80    natural values considering the file we are returning to.
81
82    FROM_LINE should be monotonic increasing across calls to this
83    function.  A call to this function can relocate the previous set of
84    maps, so any stored line_map pointers should not be used.  */
85
86 const struct line_map *
87 linemap_add (struct line_maps *set, enum lc_reason reason,
88              unsigned int sysp, const char *to_file, linenum_type to_line)
89 {
90   struct line_map *map;
91   source_location start_location = set->highest_location + 1;
92
93   if (set->used && start_location < set->maps[set->used - 1].start_location)
94     abort ();
95
96   if (set->used == set->allocated)
97     {
98       line_map_realloc reallocator
99         = set->reallocator ? set->reallocator : xrealloc;
100       set->allocated = 2 * set->allocated + 256;
101       set->maps
102         = (struct line_map *) (*reallocator) (set->maps,
103                                               set->allocated
104                                               * sizeof (struct line_map));
105       memset (&set->maps[set->used], 0, ((set->allocated - set->used)
106                                          * sizeof (struct line_map)));
107     }
108
109   map = &set->maps[set->used];
110
111   if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
112     to_file = "<stdin>";
113
114   if (reason == LC_RENAME_VERBATIM)
115     reason = LC_RENAME;
116
117   /* If we don't keep our line maps consistent, we can easily
118      segfault.  Don't rely on the client to do it for us.  */
119   if (set->depth == 0)
120     reason = LC_ENTER;
121   else if (reason == LC_LEAVE)
122     {
123       struct line_map *from;
124       bool error;
125
126       if (MAIN_FILE_P (map - 1))
127         {
128           if (to_file == NULL)
129             {
130               set->depth--;
131               return NULL;
132             }
133           error = true;
134           reason = LC_RENAME;
135           from = map - 1;
136         }
137       else
138         {
139           from = INCLUDED_FROM (set, map - 1);
140           error = to_file && filename_cmp (from->to_file, to_file);
141         }
142
143       /* Depending upon whether we are handling preprocessed input or
144          not, this can be a user error or an ICE.  */
145       if (error)
146         fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
147                  to_file);
148
149       /* A TO_FILE of NULL is special - we use the natural values.  */
150       if (error || to_file == NULL)
151         {
152           to_file = from->to_file;
153           to_line = SOURCE_LINE (from, from[1].start_location);
154           sysp = from->sysp;
155         }
156     }
157
158   map->reason = reason;
159   map->sysp = sysp;
160   map->start_location = start_location;
161   map->to_file = to_file;
162   map->to_line = to_line;
163   set->cache = set->used++;
164   map->column_bits = 0;
165   set->highest_location = start_location;
166   set->highest_line = start_location;
167   set->max_column_hint = 0;
168
169   if (reason == LC_ENTER)
170     {
171       map->included_from = set->depth == 0 ? -1 : (int) (set->used - 2);
172       set->depth++;
173       if (set->trace_includes)
174         trace_include (set, map);
175     }
176   else if (reason == LC_RENAME)
177     map->included_from = map[-1].included_from;
178   else if (reason == LC_LEAVE)
179     {
180       set->depth--;
181       map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
182     }
183
184   return map;
185 }
186
187 source_location
188 linemap_line_start (struct line_maps *set, linenum_type to_line,
189                     unsigned int max_column_hint)
190 {
191   struct line_map *map = &set->maps[set->used - 1];
192   source_location highest = set->highest_location;
193   source_location r;
194   linenum_type last_line = SOURCE_LINE (map, set->highest_line);
195   int line_delta = to_line - last_line;
196   bool add_map = false;
197   if (line_delta < 0
198       || (line_delta > 10 && line_delta * map->column_bits > 1000)
199       || (max_column_hint >= (1U << map->column_bits))
200       || (max_column_hint <= 80 && map->column_bits >= 10))
201     {
202       add_map = true;
203     }
204   else
205     max_column_hint = set->max_column_hint;
206   if (add_map)
207     {
208       int column_bits;
209       if (max_column_hint > 100000 || highest > 0xC0000000)
210         {
211           /* If the column number is ridiculous or we've allocated a huge
212              number of source_locations, give up on column numbers. */
213           max_column_hint = 0;
214           if (highest >0xF0000000)
215             return 0;
216           column_bits = 0;
217         }
218       else
219         {
220           column_bits = 7;
221           while (max_column_hint >= (1U << column_bits))
222             column_bits++;
223           max_column_hint = 1U << column_bits;
224         }
225       /* Allocate the new line_map.  However, if the current map only has a
226          single line we can sometimes just increase its column_bits instead. */
227       if (line_delta < 0
228           || last_line != map->to_line
229           || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
230         map = (struct line_map *) linemap_add (set, LC_RENAME, map->sysp,
231                                                map->to_file, to_line);
232       map->column_bits = column_bits;
233       r = map->start_location + ((to_line - map->to_line) << column_bits);
234     }
235   else
236     r = highest - SOURCE_COLUMN (map, highest)
237       + (line_delta << map->column_bits);
238   set->highest_line = r;
239   if (r > set->highest_location)
240     set->highest_location = r;
241   set->max_column_hint = max_column_hint;
242   return r;
243 }
244
245 source_location
246 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
247 {
248   source_location r = set->highest_line;
249   if (to_column >= set->max_column_hint)
250     {
251       if (r >= 0xC000000 || to_column > 100000)
252         {
253           /* Running low on source_locations - disable column numbers.  */
254           return r;
255         }
256       else
257         {
258           struct line_map *map = &set->maps[set->used - 1];
259           r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
260         }
261     }
262   r = r + to_column;
263   if (r >= set->highest_location)
264     set->highest_location = r;
265   return r;
266 }
267
268 /* Given a logical line, returns the map from which the corresponding
269    (source file, line) pair can be deduced.  Since the set is built
270    chronologically, the logical lines are monotonic increasing, and so
271    the list is sorted and we can use a binary search.  */
272
273 const struct line_map *
274 linemap_lookup (struct line_maps *set, source_location line)
275 {
276   unsigned int md, mn, mx;
277   const struct line_map *cached;
278
279   mn = set->cache;
280   mx = set->used;
281   
282   cached = &set->maps[mn];
283   /* We should get a segfault if no line_maps have been added yet.  */
284   if (line >= cached->start_location)
285     {
286       if (mn + 1 == mx || line < cached[1].start_location)
287         return cached;
288     }
289   else
290     {
291       mx = mn;
292       mn = 0;
293     }
294
295   while (mx - mn > 1)
296     {
297       md = (mn + mx) / 2;
298       if (set->maps[md].start_location > line)
299         mx = md;
300       else
301         mn = md;
302     }
303
304   set->cache = mn;
305   return &set->maps[mn];
306 }
307
308 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
309
310 static void
311 trace_include (const struct line_maps *set, const struct line_map *map)
312 {
313   unsigned int i = set->depth;
314
315   while (--i)
316     putc ('.', stderr);
317   fprintf (stderr, " %s\n", map->to_file);
318 }