1 /* Map logical line numbers to (source file, line number) pairs.
3 Free Software Foundation, Inc.
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
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.
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.
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! */
25 #include "coretypes.h"
30 static void trace_include
31 PARAMS ((const struct line_maps
*, const struct line_map
*));
33 /* Initialize a line map set. */
37 struct line_maps
*set
;
42 set
->last_listed
= -1;
43 set
->trace_includes
= false;
47 /* Free a line map set. */
51 struct line_maps
*set
;
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",
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 ().
72 FROM_LINE should be monotonic increasing across calls to this
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
;
80 unsigned int from_line
;
86 if (set
->used
&& from_line
< set
->maps
[set
->used
- 1].from_line
)
89 if (set
->used
== set
->allocated
)
91 set
->allocated
= 2 * set
->allocated
+ 256;
92 set
->maps
= (struct line_map
*)
93 xrealloc (set
->maps
, set
->allocated
* sizeof (struct line_map
));
96 map
= &set
->maps
[set
->used
++];
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. */
102 else if (reason
== LC_LEAVE
)
104 struct line_map
*from
;
107 if (MAIN_FILE_P (map
- 1))
115 from
= INCLUDED_FROM (set
, map
- 1);
116 error
= to_file
&& strcmp (from
->to_file
, to_file
);
119 /* Depending upon whether we are handling preprocessed input or
120 not, this can be a user error or an ICE. */
122 fprintf (stderr
, "line-map.c: file \"%s\" left but not entered\n",
125 /* A TO_FILE of NULL is special - we use the natural values. */
126 if (error
|| to_file
== NULL
)
128 to_file
= from
->to_file
;
129 to_line
= LAST_SOURCE_LINE (from
) + 1;
134 map
->reason
= reason
;
136 map
->from_line
= from_line
;
137 map
->to_file
= to_file
;
138 map
->to_line
= to_line
;
140 if (reason
== LC_ENTER
)
142 map
->included_from
= set
->depth
== 0 ? -1 : (int) (set
->used
- 2);
144 if (set
->trace_includes
)
145 trace_include (set
, map
);
147 else if (reason
== LC_RENAME
)
148 map
->included_from
= map
[-1].included_from
;
149 else if (reason
== LC_LEAVE
)
152 map
->included_from
= INCLUDED_FROM (set
, map
- 1)->included_from
;
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. */
163 const struct line_map
*
164 lookup_line (set
, line
)
165 struct line_maps
*set
;
168 unsigned int md
, mn
= 0, mx
= set
->used
;
176 if (set
->maps
[md
].from_line
> line
)
182 return &set
->maps
[mn
];
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. */
190 print_containing_files (set
, map
)
191 struct line_maps
*set
;
192 const struct line_map
*map
;
194 if (MAIN_FILE_P (map
) || set
->last_listed
== map
->included_from
)
197 set
->last_listed
= map
->included_from
;
198 map
= INCLUDED_FROM (set
, map
);
200 fprintf (stderr
, _("In file included from %s:%u"),
201 map
->to_file
, LAST_SOURCE_LINE (map
));
203 while (! MAIN_FILE_P (map
))
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:
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>
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
));
222 fputs (":\n", stderr
);
225 /* Print an include trace, for e.g. the -H option of the preprocessor. */
228 trace_include (set
, map
)
229 const struct line_maps
*set
;
230 const struct line_map
*map
;
232 unsigned int i
= set
->depth
;
236 fprintf (stderr
, " %s\n", map
->to_file
);