1 /* Data and functions related to line maps and input files.
2 Copyright (C) 2004, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 /* Current position in real source file. */
29 location_t input_location
;
31 struct line_maps
*line_table
;
33 /* Expand the source location LOC into a human readable location. If
34 LOC resolves to a builtin location, the file name of the readable
35 location is set to the string "<built-in>". If EXPANSION_POINT_P is
36 TRUE and LOC is virtual, then it is resolved to the expansion
37 point of the involved macro. Otherwise, it is resolved to the
38 spelling location of the token.
40 When resolving to the spelling location of the token, if the
41 resulting location is for a built-in location (that is, it has no
42 associated line/column) in the context of a macro expansion, the
43 returned location is the first one (while unwinding the macro
44 location towards its expansion point) that is in real source
47 static expanded_location
48 expand_location_1 (source_location loc
,
49 bool expansion_point_p
)
51 expanded_location xloc
;
52 const struct line_map
*map
;
53 enum location_resolution_kind lrk
= LRK_MACRO_EXPANSION_POINT
;
55 memset (&xloc
, 0, sizeof (xloc
));
57 if (loc
>= RESERVED_LOCATION_COUNT
)
59 if (!expansion_point_p
)
61 /* We want to resolve LOC to its spelling location.
63 But if that spelling location is a reserved location that
64 appears in the context of a macro expansion (like for a
65 location for a built-in token), let's consider the first
66 location (toward the expansion point) that is not reserved;
67 that is, the first location that is in real source code. */
68 loc
= linemap_unwind_to_first_non_reserved_loc (line_table
,
70 lrk
= LRK_SPELLING_LOCATION
;
72 loc
= linemap_resolve_location (line_table
, loc
,
74 xloc
= linemap_expand_location (line_table
, map
, loc
);
77 if (loc
<= BUILTINS_LOCATION
)
78 xloc
.file
= loc
== UNKNOWN_LOCATION
? NULL
: _("<built-in>");
83 /* Reads one line from file into a static buffer. */
85 read_line (FILE *file
)
88 static size_t string_len
;
95 string
= XNEWVEC (char, string_len
);
98 while ((ptr
= fgets (string
+ pos
, string_len
- pos
, file
)))
100 size_t len
= strlen (string
+ pos
);
102 if (string
[pos
+ len
- 1] == '\n')
104 string
[pos
+ len
- 1] = 0;
108 string
= XRESIZEVEC (char, string
, string_len
* 2);
112 return pos
? string
: NULL
;
115 /* Return the physical source line that corresponds to xloc in a
116 buffer that is statically allocated. The newline is replaced by
117 the null character. */
120 location_get_source_line (expanded_location xloc
)
124 FILE *stream
= xloc
.file
? fopen (xloc
.file
, "r") : NULL
;
128 while ((buffer
= read_line (stream
)) && lines
< xloc
.line
)
135 /* Expand the source location LOC into a human readable location. If
136 LOC is virtual, it resolves to the expansion point of the involved
137 macro. If LOC resolves to a builtin location, the file name of the
138 readable location is set to the string "<built-in>". */
141 expand_location (source_location loc
)
143 return expand_location_1 (loc
, /*expansion_point_p=*/true);
146 /* Expand the source location LOC into a human readable location. If
147 LOC is virtual, it resolves to the expansion location of the
148 relevant macro. If LOC resolves to a builtin location, the file
149 name of the readable location is set to the string
153 expand_location_to_spelling_point (source_location loc
)
155 return expand_location_1 (loc
, /*expansion_piont_p=*/false);
158 /* If LOCATION is in a system header and if it's a virtual location for
159 a token coming from the expansion of a macro M, unwind it to the
160 location of the expansion point of M. Otherwise, just return
163 This is used for instance when we want to emit diagnostics about a
164 token that is located in a macro that is itself defined in a system
165 header -- e.g for the NULL macro. In that case, if LOCATION is
166 passed to diagnostics emitting functions like warning_at as is, no
167 diagnostic won't be emitted. */
170 expansion_point_location_if_in_system_header (source_location location
)
172 if (in_system_header_at (location
))
173 location
= linemap_resolve_location (line_table
, location
,
174 LRK_MACRO_EXPANSION_POINT
,
180 #define ONE_M (ONE_K * ONE_K)
182 /* Display a number as an integer multiple of either:
183 - 1024, if said integer is >= to 10 K (in base 2)
184 - 1024 * 1024, if said integer is >= 10 M in (base 2)
186 #define SCALE(x) ((unsigned long) ((x) < 10 * ONE_K \
188 : ((x) < 10 * ONE_M \
192 /* For a given integer, display either:
193 - the character 'k', if the number is higher than 10 K (in base 2)
194 but strictly lower than 10 M (in base 2)
195 - the character 'M' if the number is higher than 10 M (in base2)
196 - the charcter ' ' if the number is strictly lower than 10 K */
197 #define STAT_LABEL(x) ((x) < 10 * ONE_K ? ' ' : ((x) < 10 * ONE_M ? 'k' : 'M'))
199 /* Display an integer amount as multiple of 1K or 1M (in base 2).
200 Display the correct unit (either k, M, or ' ') after the amout, as
202 #define FORMAT_AMOUNT(size) SCALE (size), STAT_LABEL (size)
204 /* Dump statistics to stderr about the memory usage of the line_table
205 set of line maps. This also displays some statistics about macro
209 dump_line_table_statistics (void)
211 struct linemap_stats s
;
212 long total_used_map_size
,
214 total_allocated_map_size
;
216 memset (&s
, 0, sizeof (s
));
218 linemap_get_statistics (line_table
, &s
);
220 macro_maps_size
= s
.macro_maps_used_size
221 + s
.macro_maps_locations_size
;
223 total_allocated_map_size
= s
.ordinary_maps_allocated_size
224 + s
.macro_maps_allocated_size
225 + s
.macro_maps_locations_size
;
227 total_used_map_size
= s
.ordinary_maps_used_size
228 + s
.macro_maps_used_size
229 + s
.macro_maps_locations_size
;
231 fprintf (stderr
, "Number of expanded macros: %5ld\n",
232 s
.num_expanded_macros
);
233 if (s
.num_expanded_macros
!= 0)
234 fprintf (stderr
, "Average number of tokens per macro expansion: %5ld\n",
235 s
.num_macro_tokens
/ s
.num_expanded_macros
);
237 "\nLine Table allocations during the "
238 "compilation process\n");
239 fprintf (stderr
, "Number of ordinary maps used: %5ld%c\n",
240 SCALE (s
.num_ordinary_maps_used
),
241 STAT_LABEL (s
.num_ordinary_maps_used
));
242 fprintf (stderr
, "Ordinary map used size: %5ld%c\n",
243 SCALE (s
.ordinary_maps_used_size
),
244 STAT_LABEL (s
.ordinary_maps_used_size
));
245 fprintf (stderr
, "Number of ordinary maps allocated: %5ld%c\n",
246 SCALE (s
.num_ordinary_maps_allocated
),
247 STAT_LABEL (s
.num_ordinary_maps_allocated
));
248 fprintf (stderr
, "Ordinary maps allocated size: %5ld%c\n",
249 SCALE (s
.ordinary_maps_allocated_size
),
250 STAT_LABEL (s
.ordinary_maps_allocated_size
));
251 fprintf (stderr
, "Number of macro maps used: %5ld%c\n",
252 SCALE (s
.num_macro_maps_used
),
253 STAT_LABEL (s
.num_macro_maps_used
));
254 fprintf (stderr
, "Macro maps used size: %5ld%c\n",
255 SCALE (s
.macro_maps_used_size
),
256 STAT_LABEL (s
.macro_maps_used_size
));
257 fprintf (stderr
, "Macro maps locations size: %5ld%c\n",
258 SCALE (s
.macro_maps_locations_size
),
259 STAT_LABEL (s
.macro_maps_locations_size
));
260 fprintf (stderr
, "Macro maps size: %5ld%c\n",
261 SCALE (macro_maps_size
),
262 STAT_LABEL (macro_maps_size
));
263 fprintf (stderr
, "Duplicated maps locations size: %5ld%c\n",
264 SCALE (s
.duplicated_macro_maps_locations_size
),
265 STAT_LABEL (s
.duplicated_macro_maps_locations_size
));
266 fprintf (stderr
, "Total allocated maps size: %5ld%c\n",
267 SCALE (total_allocated_map_size
),
268 STAT_LABEL (total_allocated_map_size
));
269 fprintf (stderr
, "Total used maps size: %5ld%c\n",
270 SCALE (total_used_map_size
),
271 STAT_LABEL (total_used_map_size
));
272 fprintf (stderr
, "\n");