OSDN Git Service

Mon May 12 11:32:53 CEST 2003 Jan Hubicka <jh@suse.cz>
[pf3gnuchains/gcc-fork.git] / gcc / line-map.c
1 /* Map logical line numbers to (source file, line number) pairs.
2    Copyright (C) 2001
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 "coretypes.h"
26 #include "tm.h"
27 #include "line-map.h"
28 #include "intl.h"
29
30 static void trace_include
31   PARAMS ((const struct line_maps *, const struct line_map *));
32
33 /* Initialize a line map set.  */
34
35 void
36 init_line_maps (set)
37      struct line_maps *set;
38 {
39   set->maps = 0;
40   set->allocated = 0;
41   set->used = 0;
42   set->last_listed = -1;
43   set->trace_includes = false;
44   set->depth = 0;
45 }
46
47 /* Free a line map set.  */
48
49 void
50 free_line_maps (set)
51      struct line_maps *set;
52 {
53   if (set->maps)
54     {
55       struct line_map *map;
56
57       /* Depending upon whether we are handling preprocessed input or
58          not, this can be a user error or an ICE.  */
59       for (map = CURRENT_LINE_MAP (set); ! MAIN_FILE_P (map);
60            map = INCLUDED_FROM (set, map))
61         fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
62                  map->to_file);
63
64       free (set->maps);
65     }
66 }
67
68 /* Add a mapping of logical source line to physical source file and
69    line number.  Ther text pointed to by TO_FILE must have a lifetime
70    at least as long as the final call to lookup_line ().
71
72    FROM_LINE should be monotonic increasing across calls to this
73    function.  */
74
75 const struct line_map *
76 add_line_map (set, reason, sysp, from_line, to_file, to_line)
77      struct line_maps *set;
78      enum lc_reason reason;
79      unsigned int sysp;
80      unsigned int from_line;
81      const char *to_file;
82      unsigned int to_line;
83 {
84   struct line_map *map;
85
86   if (set->used && from_line < set->maps[set->used - 1].from_line)
87     abort ();
88
89   if (set->used == set->allocated)
90     {
91       set->allocated = 2 * set->allocated + 256;
92       set->maps = (struct line_map *)
93         xrealloc (set->maps, set->allocated * sizeof (struct line_map));
94     }
95
96   map = &set->maps[set->used++];
97
98   /* If we don't keep our line maps consistent, we can easily
99      segfault.  Don't rely on the client to do it for us.  */
100   if (set->depth == 0)
101     reason = LC_ENTER;
102   else if (reason == LC_LEAVE)
103     {
104       struct line_map *from;
105       bool error;
106
107       if (MAIN_FILE_P (map - 1))
108         {
109           set->depth--;
110           set->used--;
111           return NULL;
112         }
113       else
114         {
115           from = INCLUDED_FROM (set, map - 1);
116           error = to_file && strcmp (from->to_file, to_file);
117         }
118
119       /* Depending upon whether we are handling preprocessed input or
120          not, this can be a user error or an ICE.  */
121       if (error)
122         fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
123                  to_file);
124
125       /* A TO_FILE of NULL is special - we use the natural values.  */
126       if (error || to_file == NULL)
127         {
128           to_file = from->to_file;
129           to_line = LAST_SOURCE_LINE (from) + 1;
130           sysp = from->sysp;
131         }
132     }
133
134   map->reason = reason;
135   map->sysp = sysp;
136   map->from_line = from_line;
137   map->to_file = to_file;
138   map->to_line = to_line;
139
140   if (reason == LC_ENTER)
141     {
142       map->included_from = set->depth == 0 ? -1 : (int) (set->used - 2);
143       set->depth++;
144       if (set->trace_includes)
145         trace_include (set, map);
146     }
147   else if (reason == LC_RENAME)
148     map->included_from = map[-1].included_from;
149   else if (reason == LC_LEAVE)
150     {
151       set->depth--;
152       map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
153     }
154
155   return map;
156 }
157
158 /* Given a logical line, returns the map from which the corresponding
159    (source file, line) pair can be deduced.  Since the set is built
160    chronologically, the logical lines are monotonic increasing, and so
161    the list is sorted and we can use a binary search.  */
162
163 const struct line_map *
164 lookup_line (set, line)
165      struct line_maps *set;
166      unsigned int line;
167 {
168   unsigned int md, mn = 0, mx = set->used;
169
170   if (mx == 0)
171     abort ();
172
173   while (mx - mn > 1)
174     {
175       md = (mn + mx) / 2;
176       if (set->maps[md].from_line > line)
177         mx = md;
178       else
179         mn = md;
180     }
181
182   return &set->maps[mn];
183 }
184
185 /* Print the file names and line numbers of the #include commands
186    which led to the map MAP, if any, to stderr.  Nothing is output if
187    the most recently listed stack is the same as the current one.  */
188
189 void
190 print_containing_files (set, map)
191      struct line_maps *set;
192      const struct line_map *map;
193 {
194   if (MAIN_FILE_P (map) || set->last_listed == map->included_from)
195     return;
196
197   set->last_listed = map->included_from;
198   map = INCLUDED_FROM (set, map);
199
200   fprintf (stderr,  _("In file included from %s:%u"),
201            map->to_file, LAST_SOURCE_LINE (map));
202
203   while (! MAIN_FILE_P (map))
204     {
205       map = INCLUDED_FROM (set, map);
206       /* Translators note: this message is used in conjunction
207          with "In file included from %s:%ld" and some other
208          tricks.  We want something like this:
209
210          | In file included from sys/select.h:123,
211          |                  from sys/types.h:234,
212          |                  from userfile.c:31:
213          | bits/select.h:45: <error message here>
214
215          with all the "from"s lined up.
216          The trailing comma is at the beginning of this message,
217          and the trailing colon is not translated.  */
218       fprintf (stderr, _(",\n                 from %s:%u"),
219                map->to_file, LAST_SOURCE_LINE (map));
220     }
221
222   fputs (":\n", stderr);
223 }
224
225 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
226
227 static void
228 trace_include (set, map)
229      const struct line_maps *set;
230      const struct line_map *map;
231 {
232   unsigned int i = set->depth;
233
234   while (--i)
235     putc ('.', stderr);
236   fprintf (stderr, " %s\n", map->to_file);
237 }