OSDN Git Service

* c-lex.c (map): Make const.
[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 "line-map.h"
26 #include "intl.h"
27
28 /* Initialize a line map set.  */
29
30 void
31 init_line_maps (set)
32      struct line_maps *set;
33 {
34   set->maps = 0;
35   set->allocated = 0;
36   set->used = 0;
37   set->last_listed = -1;
38 }
39
40 /* Free a line map set.  */
41
42 void
43 free_line_maps (set)
44      struct line_maps *set;
45 {
46   if (set->maps)
47     {
48       struct line_map *map;
49
50       /* Depending upon whether we are handling preprocessed input or
51          not, this can be a user error or an ICE.  */
52       for (map = CURRENT_LINE_MAP (set); ! MAIN_FILE_P (map);
53            map = INCLUDED_FROM (set, map))
54         fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
55                  map->to_file);
56
57       free (set->maps);
58     }
59 }
60
61 /* Add a mapping of logical source line to physical source file and
62    line number.  Ther text pointed to by TO_FILE must have a lifetime
63    at least as long as the final call to lookup_line ().
64
65    FROM_LINE should be monotonic increasing across calls to this
66    function.  */
67
68 const struct line_map *
69 add_line_map (set, reason, sysp, from_line, to_file, to_line)
70      struct line_maps *set;
71      enum lc_reason reason;
72      unsigned int sysp;
73      unsigned int from_line;
74      const char *to_file;
75      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->used == 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           error = true;
103           reason = LC_RENAME;
104           from = map - 1;
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     map->included_from = set->used - 1;
135   else if (reason == LC_RENAME)
136     map->included_from = map[-1].included_from;
137   else if (reason == LC_LEAVE)
138     map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
139
140   set->used++;
141   return map;
142 }
143
144 /* Given a logical line, returns the map from which the corresponding
145    (source file, line) pair can be deduced.  Since the set is built
146    chronologically, the logical lines are monotonic increasing, and so
147    the list is sorted and we can use a binary search.  */
148
149 const struct line_map *
150 lookup_line (set, line)
151      struct line_maps *set;
152      unsigned int line;
153 {
154   unsigned int md, mn = 0, mx = set->used;
155
156   if (mx == 0)
157     abort ();
158
159   while (mx - mn > 1)
160     {
161       md = (mn + mx) / 2;
162       if (set->maps[md].from_line > line)
163         mx = md;
164       else
165         mn = md;
166     }
167
168   return &set->maps[mn];
169 }
170
171 /* Print the file names and line numbers of the #include commands
172    which led to the map MAP, if any, to stderr.  Nothing is output if
173    the most recently listed stack is the same as the current one.  */
174
175 void
176 print_containing_files (set, map)
177      struct line_maps *set;
178      const struct line_map *map;
179 {
180   if (MAIN_FILE_P (map) || set->last_listed == map->included_from)
181     return;
182
183   set->last_listed = map->included_from;
184   map = INCLUDED_FROM (set, map);
185
186   fprintf (stderr,  _("In file included from %s:%u"),
187            map->to_file, LAST_SOURCE_LINE (map));
188
189   while (! MAIN_FILE_P (map))
190     {
191       map = INCLUDED_FROM (set, map);
192       /* Translators note: this message is used in conjunction
193          with "In file included from %s:%ld" and some other
194          tricks.  We want something like this:
195
196          | In file included from sys/select.h:123,
197          |                  from sys/types.h:234,
198          |                  from userfile.c:31:
199          | bits/select.h:45: <error message here>
200
201          with all the "from"s lined up.
202          The trailing comma is at the beginning of this message,
203          and the trailing colon is not translated.  */
204       fprintf (stderr, _(",\n                 from %s:%u"),
205                map->to_file, LAST_SOURCE_LINE (map));
206     }
207
208   fputs (":\n", stderr);
209 }