OSDN Git Service

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