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! */
28 static void trace_include
29 PARAMS ((const struct line_maps
*, const struct line_map
*));
31 /* Initialize a line map set. */
35 struct line_maps
*set
;
40 set
->last_listed
= -1;
41 set
->trace_includes
= false;
45 /* Free a line map set. */
49 struct line_maps
*set
;
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",
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 ().
70 FROM_LINE should be monotonic increasing across calls to this
73 const struct line_map
*
74 add_line_map (set
, reason
, sysp
, from_line
, to_file
, to_line
)
75 struct line_maps
*set
;
76 enum lc_reason reason
;
78 unsigned int from_line
;
84 if (set
->used
&& from_line
< set
->maps
[set
->used
- 1].from_line
)
87 if (set
->used
== set
->allocated
)
89 set
->allocated
= 2 * set
->allocated
+ 256;
90 set
->maps
= (struct line_map
*)
91 xrealloc (set
->maps
, set
->allocated
* sizeof (struct line_map
));
94 map
= &set
->maps
[set
->used
++];
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. */
100 else if (reason
== LC_LEAVE
)
102 struct line_map
*from
;
105 if (MAIN_FILE_P (map
- 1))
113 from
= INCLUDED_FROM (set
, map
- 1);
114 error
= to_file
&& strcmp (from
->to_file
, to_file
);
117 /* Depending upon whether we are handling preprocessed input or
118 not, this can be a user error or an ICE. */
120 fprintf (stderr
, "line-map.c: file \"%s\" left but not entered\n",
123 /* A TO_FILE of NULL is special - we use the natural values. */
124 if (error
|| to_file
== NULL
)
126 to_file
= from
->to_file
;
127 to_line
= LAST_SOURCE_LINE (from
) + 1;
132 map
->reason
= reason
;
134 map
->from_line
= from_line
;
135 map
->to_file
= to_file
;
136 map
->to_line
= to_line
;
138 if (reason
== LC_ENTER
)
141 map
->included_from
= set
->used
- 2;
142 if (set
->trace_includes
)
143 trace_include (set
, map
);
145 else if (reason
== LC_RENAME
)
146 map
->included_from
= map
[-1].included_from
;
147 else if (reason
== LC_LEAVE
)
150 map
->included_from
= INCLUDED_FROM (set
, map
- 1)->included_from
;
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. */
161 const struct line_map
*
162 lookup_line (set
, line
)
163 struct line_maps
*set
;
166 unsigned int md
, mn
= 0, mx
= set
->used
;
174 if (set
->maps
[md
].from_line
> line
)
180 return &set
->maps
[mn
];
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. */
188 print_containing_files (set
, map
)
189 struct line_maps
*set
;
190 const struct line_map
*map
;
192 if (MAIN_FILE_P (map
) || set
->last_listed
== map
->included_from
)
195 set
->last_listed
= map
->included_from
;
196 map
= INCLUDED_FROM (set
, map
);
198 fprintf (stderr
, _("In file included from %s:%u"),
199 map
->to_file
, LAST_SOURCE_LINE (map
));
201 while (! MAIN_FILE_P (map
))
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:
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>
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
));
220 fputs (":\n", stderr
);
223 /* Print an include trace, for e.g. the -H option of the preprocessor. */
226 trace_include (set
, map
)
227 const struct line_maps
*set
;
228 const struct line_map
*map
;
230 unsigned int i
= set
->depth
;
234 fprintf (stderr
, " %s\n", map
->to_file
);