1 /* Map logical line numbers to (source file, line number) pairs.
2 Copyright (C) 2001, 2003, 2004
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! */
27 static void trace_include (const struct line_maps
*, const struct line_map
*);
29 /* Initialize a line map set. */
32 linemap_init (struct line_maps
*set
)
37 set
->last_listed
= -1;
38 set
->trace_includes
= false;
41 set
->highest_location
= 0;
42 set
->highest_line
= 0;
43 set
->max_column_hint
= 0;
46 /* Check for and warn about line_maps entered but not exited. */
49 linemap_check_files_exited (struct line_maps
*set
)
52 /* Depending upon whether we are handling preprocessed input or
53 not, this can be a user error or an ICE. */
54 for (map
= &set
->maps
[set
->used
- 1]; ! MAIN_FILE_P (map
);
55 map
= INCLUDED_FROM (set
, map
))
56 fprintf (stderr
, "line-map.c: file \"%s\" entered but not left\n",
60 /* Free a line map set. */
63 linemap_free (struct line_maps
*set
)
67 linemap_check_files_exited (set
);
73 /* Add a mapping of logical source line to physical source file and
76 The text pointed to by TO_FILE must have a lifetime
77 at least as long as the final call to lookup_line (). An empty
78 TO_FILE means standard input. If reason is LC_LEAVE, and
79 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
80 natural values considering the file we are returning to.
82 FROM_LINE should be monotonic increasing across calls to this
83 function. A call to this function can relocate the previous set of
84 A call to this function can relocate the previous set of
85 maps, so any stored line_map pointers should not be used. */
87 const struct line_map
*
88 linemap_add (struct line_maps
*set
, enum lc_reason reason
,
89 unsigned int sysp
, const char *to_file
, unsigned int to_line
)
92 source_location start_location
= set
->highest_location
+ 1;
94 if (set
->used
&& start_location
< set
->maps
[set
->used
- 1].start_location
)
97 if (set
->used
== set
->allocated
)
99 set
->allocated
= 2 * set
->allocated
+ 256;
100 set
->maps
= xrealloc (set
->maps
, set
->allocated
* sizeof (struct line_map
));
103 map
= &set
->maps
[set
->used
];
105 if (to_file
&& *to_file
== '\0')
108 /* If we don't keep our line maps consistent, we can easily
109 segfault. Don't rely on the client to do it for us. */
112 else if (reason
== LC_LEAVE
)
114 struct line_map
*from
;
117 if (MAIN_FILE_P (map
- 1))
130 from
= INCLUDED_FROM (set
, map
- 1);
131 error
= to_file
&& strcmp (from
->to_file
, to_file
);
134 /* Depending upon whether we are handling preprocessed input or
135 not, this can be a user error or an ICE. */
137 fprintf (stderr
, "line-map.c: file \"%s\" left but not entered\n",
140 /* A TO_FILE of NULL is special - we use the natural values. */
141 if (error
|| to_file
== NULL
)
143 to_file
= from
->to_file
;
144 to_line
= SOURCE_LINE (from
, from
[1].start_location
);
149 map
->reason
= reason
;
151 map
->start_location
= start_location
;
152 map
->to_file
= to_file
;
153 map
->to_line
= to_line
;
154 set
->cache
= set
->used
++;
155 map
->column_bits
= 0;
156 set
->highest_location
= start_location
;
157 set
->highest_line
= start_location
;
158 set
->max_column_hint
= 0;
160 if (reason
== LC_ENTER
)
162 map
->included_from
= set
->depth
== 0 ? -1 : (int) (set
->used
- 2);
164 if (set
->trace_includes
)
165 trace_include (set
, map
);
167 else if (reason
== LC_RENAME
)
168 map
->included_from
= map
[-1].included_from
;
169 else if (reason
== LC_LEAVE
)
172 map
->included_from
= INCLUDED_FROM (set
, map
- 1)->included_from
;
179 linemap_line_start (struct line_maps
*set
, unsigned int to_line
,
180 unsigned int max_column_hint
)
182 struct line_map
*map
= &set
->maps
[set
->used
- 1];
183 source_location highest
= set
->highest_location
;
185 unsigned int last_line
= SOURCE_LINE (map
, set
->highest_line
);
186 int line_delta
= to_line
- last_line
;
187 bool add_map
= false;
189 || (line_delta
> 10 && line_delta
* map
->column_bits
> 1000)
190 || (max_column_hint
>= (1U << map
->column_bits
))
191 || (max_column_hint
<= 80 && map
->column_bits
>= 10))
196 max_column_hint
= set
->max_column_hint
;
200 if (max_column_hint
> 100000 || highest
> 0xC0000000)
203 if (highest
>0xF0000000)
210 while (max_column_hint
>= (1U << column_bits
))
212 max_column_hint
= 1U << column_bits
;
215 || last_line
!= map
->to_line
216 || SOURCE_COLUMN (map
, highest
) >= (1U << column_bits
))
217 map
= (struct line_map
*) linemap_add (set
, LC_RENAME
, map
->sysp
,
218 map
->to_file
, to_line
);
219 map
->column_bits
= column_bits
;
220 r
= map
->start_location
;
223 r
= highest
- SOURCE_COLUMN (map
, highest
)
224 + (line_delta
<< map
->column_bits
);
225 set
->highest_line
= r
;
226 if (r
> set
->highest_location
)
227 set
->highest_location
= r
;
228 set
->max_column_hint
= max_column_hint
;
233 linemap_position_for_column (struct line_maps
*set
, unsigned int to_column
)
235 source_location r
= set
->highest_line
;
236 if (to_column
>= set
->max_column_hint
)
238 if (r
>= 0xC000000 || to_column
> 100000)
240 /* Running low on source_locations - disable column numbers. */
245 struct line_map
*map
= &set
->maps
[set
->used
- 1];
246 r
= linemap_line_start (set
, SOURCE_LINE (map
, r
), to_column
+ 50);
250 if (r
>= set
->highest_location
)
251 set
->highest_location
= r
;
255 /* Given a logical line, returns the map from which the corresponding
256 (source file, line) pair can be deduced. Since the set is built
257 chronologically, the logical lines are monotonic increasing, and so
258 the list is sorted and we can use a binary search. */
260 const struct line_map
*
261 linemap_lookup (struct line_maps
*set
, source_location line
)
263 unsigned int md
, mn
, mx
;
264 const struct line_map
*cached
;
269 cached
= &set
->maps
[mn
];
270 /* We should get a segfault if no line_maps have been added yet. */
271 if (line
>= cached
->start_location
)
273 if (mn
+ 1 == mx
|| line
< cached
[1].start_location
)
285 if (set
->maps
[md
].start_location
> line
)
292 return &set
->maps
[mn
];
295 /* Print the file names and line numbers of the #include commands
296 which led to the map MAP, if any, to stderr. Nothing is output if
297 the most recently listed stack is the same as the current one. */
300 linemap_print_containing_files (struct line_maps
*set
,
301 const struct line_map
*map
)
303 if (MAIN_FILE_P (map
) || set
->last_listed
== map
->included_from
)
306 set
->last_listed
= map
->included_from
;
307 map
= INCLUDED_FROM (set
, map
);
309 fprintf (stderr
, _("In file included from %s:%u"),
310 map
->to_file
, LAST_SOURCE_LINE (map
));
312 while (! MAIN_FILE_P (map
))
314 map
= INCLUDED_FROM (set
, map
);
315 /* Translators note: this message is used in conjunction
316 with "In file included from %s:%ld" and some other
317 tricks. We want something like this:
319 | In file included from sys/select.h:123,
320 | from sys/types.h:234,
321 | from userfile.c:31:
322 | bits/select.h:45: <error message here>
324 with all the "from"s lined up.
325 The trailing comma is at the beginning of this message,
326 and the trailing colon is not translated. */
327 fprintf (stderr
, _(",\n from %s:%u"),
328 map
->to_file
, LAST_SOURCE_LINE (map
));
331 fputs (":\n", stderr
);
334 /* Print an include trace, for e.g. the -H option of the preprocessor. */
337 trace_include (const struct line_maps
*set
, const struct line_map
*map
)
339 unsigned int i
= set
->depth
;
343 fprintf (stderr
, " %s\n", map
->to_file
);