1 /* Map logical line numbers to (source file, line number) pairs.
2 Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009
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 3, 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; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>.
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
->trace_includes
= false;
40 set
->highest_location
= RESERVED_LOCATION_COUNT
- 1;
41 set
->highest_line
= RESERVED_LOCATION_COUNT
- 1;
42 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 maps, so any stored line_map pointers should not be used. */
86 const struct line_map
*
87 linemap_add (struct line_maps
*set
, enum lc_reason reason
,
88 unsigned int sysp
, const char *to_file
, linenum_type to_line
)
91 source_location start_location
= set
->highest_location
+ 1;
93 if (set
->used
&& start_location
< set
->maps
[set
->used
- 1].start_location
)
96 if (set
->used
== set
->allocated
)
98 line_map_realloc reallocator
99 = set
->reallocator
? set
->reallocator
: xrealloc
;
100 set
->allocated
= 2 * set
->allocated
+ 256;
102 = (struct line_map
*) (*reallocator
) (set
->maps
,
104 * sizeof (struct line_map
));
105 memset (&set
->maps
[set
->used
], 0, ((set
->allocated
- set
->used
)
106 * sizeof (struct line_map
)));
109 map
= &set
->maps
[set
->used
];
111 if (to_file
&& *to_file
== '\0' && reason
!= LC_RENAME_VERBATIM
)
114 if (reason
== LC_RENAME_VERBATIM
)
117 if (set
->depth
== 0 && reason
== LC_RENAME
)
120 if (reason
== LC_LEAVE
)
122 struct line_map
*from
;
125 if (MAIN_FILE_P (map
- 1))
138 from
= INCLUDED_FROM (set
, map
- 1);
139 error
= to_file
&& filename_cmp (from
->to_file
, to_file
);
142 /* Depending upon whether we are handling preprocessed input or
143 not, this can be a user error or an ICE. */
145 fprintf (stderr
, "line-map.c: file \"%s\" left but not entered\n",
148 /* A TO_FILE of NULL is special - we use the natural values. */
149 if (error
|| to_file
== NULL
)
151 to_file
= from
->to_file
;
152 to_line
= SOURCE_LINE (from
, from
[1].start_location
);
157 map
->reason
= reason
;
159 map
->start_location
= start_location
;
160 map
->to_file
= to_file
;
161 map
->to_line
= to_line
;
162 set
->cache
= set
->used
++;
163 map
->column_bits
= 0;
164 set
->highest_location
= start_location
;
165 set
->highest_line
= start_location
;
166 set
->max_column_hint
= 0;
168 if (reason
== LC_ENTER
)
170 map
->included_from
= set
->depth
== 0 ? -1 : (int) (set
->used
- 2);
172 if (set
->trace_includes
)
173 trace_include (set
, map
);
175 else if (reason
== LC_RENAME
)
176 map
->included_from
= map
[-1].included_from
;
177 else if (reason
== LC_LEAVE
)
180 map
->included_from
= INCLUDED_FROM (set
, map
- 1)->included_from
;
187 linemap_line_start (struct line_maps
*set
, linenum_type to_line
,
188 unsigned int max_column_hint
)
190 struct line_map
*map
= &set
->maps
[set
->used
- 1];
191 source_location highest
= set
->highest_location
;
193 linenum_type last_line
= SOURCE_LINE (map
, set
->highest_line
);
194 int line_delta
= to_line
- last_line
;
195 bool add_map
= false;
197 || (line_delta
> 10 && line_delta
* map
->column_bits
> 1000)
198 || (max_column_hint
>= (1U << map
->column_bits
))
199 || (max_column_hint
<= 80 && map
->column_bits
>= 10))
204 max_column_hint
= set
->max_column_hint
;
208 if (max_column_hint
> 100000 || highest
> 0xC0000000)
210 /* If the column number is ridiculous or we've allocated a huge
211 number of source_locations, give up on column numbers. */
213 if (highest
>0xF0000000)
220 while (max_column_hint
>= (1U << column_bits
))
222 max_column_hint
= 1U << column_bits
;
224 /* Allocate the new line_map. However, if the current map only has a
225 single line we can sometimes just increase its column_bits instead. */
227 || last_line
!= map
->to_line
228 || SOURCE_COLUMN (map
, highest
) >= (1U << column_bits
))
229 map
= (struct line_map
*) linemap_add (set
, LC_RENAME
, map
->sysp
,
230 map
->to_file
, to_line
);
231 map
->column_bits
= column_bits
;
232 r
= map
->start_location
+ ((to_line
- map
->to_line
) << column_bits
);
235 r
= highest
- SOURCE_COLUMN (map
, highest
)
236 + (line_delta
<< map
->column_bits
);
237 set
->highest_line
= r
;
238 if (r
> set
->highest_location
)
239 set
->highest_location
= r
;
240 set
->max_column_hint
= max_column_hint
;
245 linemap_position_for_column (struct line_maps
*set
, unsigned int to_column
)
247 source_location r
= set
->highest_line
;
248 if (to_column
>= set
->max_column_hint
)
250 if (r
>= 0xC000000 || to_column
> 100000)
252 /* Running low on source_locations - disable column numbers. */
257 struct line_map
*map
= &set
->maps
[set
->used
- 1];
258 r
= linemap_line_start (set
, SOURCE_LINE (map
, r
), to_column
+ 50);
262 if (r
>= set
->highest_location
)
263 set
->highest_location
= r
;
267 /* Given a logical line, returns the map from which the corresponding
268 (source file, line) pair can be deduced. Since the set is built
269 chronologically, the logical lines are monotonic increasing, and so
270 the list is sorted and we can use a binary search. */
272 const struct line_map
*
273 linemap_lookup (struct line_maps
*set
, source_location line
)
275 unsigned int md
, mn
, mx
;
276 const struct line_map
*cached
;
281 cached
= &set
->maps
[mn
];
282 /* We should get a segfault if no line_maps have been added yet. */
283 if (line
>= cached
->start_location
)
285 if (mn
+ 1 == mx
|| line
< cached
[1].start_location
)
297 if (set
->maps
[md
].start_location
> line
)
304 return &set
->maps
[mn
];
307 /* Print an include trace, for e.g. the -H option of the preprocessor. */
310 trace_include (const struct line_maps
*set
, const struct line_map
*map
)
312 unsigned int i
= set
->depth
;
316 fprintf (stderr
, " %s\n", map
->to_file
);