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