Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 1 | /* Map logical line numbers to (source file, line number) pairs. |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2 | Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009, 2010, 2011 |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 3 | Free Software Foundation, Inc. |
| 4 | |
| 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 |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 7 | Free Software Foundation; either version 3, or (at your option) any |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 8 | later version. |
| 9 | |
| 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. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 16 | along with this program; see the file COPYING3. If not see |
| 17 | <http://www.gnu.org/licenses/>. |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 18 | |
| 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! */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | #include "system.h" |
| 25 | #include "line-map.h" |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 26 | #include "cpplib.h" |
| 27 | #include "internal.h" |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 28 | |
Neil Booth | e2b9853 | 2003-06-20 05:51:13 +0000 | [diff] [blame] | 29 | static void trace_include (const struct line_maps *, const struct line_map *); |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 30 | static const struct line_map * linemap_ordinary_map_lookup (struct line_maps *, |
| 31 | source_location); |
| 32 | static const struct line_map* linemap_macro_map_lookup (struct line_maps *, |
| 33 | source_location); |
| 34 | static source_location linemap_macro_map_loc_to_def_point |
| 35 | (const struct line_map*, source_location); |
| 36 | static source_location linemap_macro_map_loc_unwind_toward_spelling |
| 37 | (const struct line_map*, source_location); |
| 38 | static source_location linemap_macro_map_loc_to_exp_point |
| 39 | (const struct line_map*, source_location); |
| 40 | static source_location linemap_macro_loc_to_spelling_point |
| 41 | (struct line_maps *, source_location, const struct line_map **); |
| 42 | static source_location linemap_macro_loc_to_def_point (struct line_maps *, |
| 43 | source_location, |
| 44 | const struct line_map **); |
| 45 | static source_location linemap_macro_loc_to_exp_point (struct line_maps *, |
| 46 | source_location, |
| 47 | const struct line_map **); |
Neil Booth | 5993019 | 2001-08-21 21:17:48 +0000 | [diff] [blame] | 48 | |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame] | 49 | /* Counters defined in macro.c. */ |
| 50 | extern unsigned num_expanded_macros_counter; |
| 51 | extern unsigned num_macro_tokens_counter; |
| 52 | |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 53 | /* Initialize a line map set. */ |
| 54 | |
| 55 | void |
Zack Weinberg | a2f7be9 | 2003-07-22 16:24:53 +0000 | [diff] [blame] | 56 | linemap_init (struct line_maps *set) |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 57 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 58 | memset (set, 0, sizeof (struct line_maps)); |
Jakub Jelinek | 96c169e | 2009-10-14 12:04:22 +0200 | [diff] [blame] | 59 | set->highest_location = RESERVED_LOCATION_COUNT - 1; |
| 60 | set->highest_line = RESERVED_LOCATION_COUNT - 1; |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Kazu Hirata | 9ac9746 | 2004-02-16 14:20:10 +0000 | [diff] [blame] | 63 | /* Check for and warn about line_maps entered but not exited. */ |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 64 | |
| 65 | void |
| 66 | linemap_check_files_exited (struct line_maps *set) |
| 67 | { |
| 68 | struct line_map *map; |
| 69 | /* Depending upon whether we are handling preprocessed input or |
| 70 | not, this can be a user error or an ICE. */ |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 71 | for (map = LINEMAPS_LAST_ORDINARY_MAP (set); |
| 72 | ! MAIN_FILE_P (map); |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 73 | map = INCLUDED_FROM (set, map)) |
| 74 | fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n", |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 75 | ORDINARY_MAP_FILE_NAME (map)); |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 76 | } |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 77 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 78 | /* Create a new line map in the line map set SET, and return it. |
| 79 | REASON is the reason of creating the map. It determines the type |
| 80 | of map created (ordinary or macro map). Note that ordinary maps and |
| 81 | macro maps are allocated in different memory location. */ |
| 82 | |
| 83 | static struct line_map * |
| 84 | new_linemap (struct line_maps *set, |
| 85 | enum lc_reason reason) |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 86 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 87 | /* Depending on this variable, a macro map would be allocated in a |
| 88 | different memory location than an ordinary map. */ |
| 89 | bool macro_map_p = (reason == LC_ENTER_MACRO); |
| 90 | struct line_map *result; |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 91 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 92 | if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p)) |
| 93 | { |
| 94 | /* We ran out of allocated line maps. Let's allocate more. */ |
Tom Tromey | b9bd6f7 | 2011-10-17 10:00:07 +0000 | [diff] [blame] | 95 | unsigned alloc_size; |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 96 | |
| 97 | line_map_realloc reallocator |
| 98 | = set->reallocator ? set->reallocator : xrealloc; |
Tom Tromey | b9bd6f7 | 2011-10-17 10:00:07 +0000 | [diff] [blame] | 99 | line_map_round_alloc_size_func round_alloc_size = |
| 100 | set->round_alloc_size; |
| 101 | |
| 102 | /* We are going to execute some dance to try to reduce the |
| 103 | overhead of the memory allocator, in case we are using the |
| 104 | ggc-page.c one. |
| 105 | |
| 106 | The actual size of memory we are going to get back from the |
| 107 | allocator is the smallest power of 2 that is greater than the |
| 108 | size we requested. So let's consider that size then. */ |
| 109 | |
| 110 | alloc_size = |
| 111 | (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256) |
| 112 | * sizeof (struct line_map); |
| 113 | |
| 114 | /* Get the actual size of memory that is going to be allocated |
| 115 | by the allocator. */ |
| 116 | alloc_size = round_alloc_size (alloc_size); |
| 117 | |
| 118 | /* Now alloc_size contains the exact memory size we would get if |
| 119 | we have asked for the initial alloc_size amount of memory. |
| 120 | Let's get back to the number of macro map that amounts |
| 121 | to. */ |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 122 | LINEMAPS_ALLOCATED (set, macro_map_p) = |
Tom Tromey | b9bd6f7 | 2011-10-17 10:00:07 +0000 | [diff] [blame] | 123 | alloc_size / (sizeof (struct line_map)); |
| 124 | |
| 125 | /* And now let's really do the re-allocation. */ |
| 126 | LINEMAPS_MAPS (set, macro_map_p) = |
| 127 | (struct line_map *) (*reallocator) |
| 128 | (LINEMAPS_MAPS (set, macro_map_p), |
| 129 | (LINEMAPS_ALLOCATED (set, macro_map_p) |
| 130 | * sizeof (struct line_map))); |
| 131 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 132 | result = |
| 133 | &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)]; |
| 134 | memset (result, 0, |
| 135 | ((LINEMAPS_ALLOCATED (set, macro_map_p) |
| 136 | - LINEMAPS_USED (set, macro_map_p)) |
| 137 | * sizeof (struct line_map))); |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 138 | } |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 139 | else |
| 140 | result = |
| 141 | &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)]; |
| 142 | |
| 143 | LINEMAPS_USED (set, macro_map_p)++; |
| 144 | |
| 145 | result->reason = reason; |
| 146 | return result; |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* Add a mapping of logical source line to physical source file and |
Geoffrey Keating | 9074464 | 2003-07-30 06:43:33 +0000 | [diff] [blame] | 150 | line number. |
| 151 | |
| 152 | The text pointed to by TO_FILE must have a lifetime |
| 153 | at least as long as the final call to lookup_line (). An empty |
| 154 | TO_FILE means standard input. If reason is LC_LEAVE, and |
| 155 | TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their |
| 156 | natural values considering the file we are returning to. |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 157 | |
| 158 | FROM_LINE should be monotonic increasing across calls to this |
Geoffrey Keating | 9074464 | 2003-07-30 06:43:33 +0000 | [diff] [blame] | 159 | function. A call to this function can relocate the previous set of |
| 160 | maps, so any stored line_map pointers should not be used. */ |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 161 | |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 162 | const struct line_map * |
Zack Weinberg | a2f7be9 | 2003-07-22 16:24:53 +0000 | [diff] [blame] | 163 | linemap_add (struct line_maps *set, enum lc_reason reason, |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 164 | unsigned int sysp, const char *to_file, linenum_type to_line) |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 165 | { |
| 166 | struct line_map *map; |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 167 | source_location start_location = set->highest_location + 1; |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 168 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 169 | linemap_assert (!(LINEMAPS_ORDINARY_USED (set) |
| 170 | && (start_location |
| 171 | < MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set))))); |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 172 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 173 | /* When we enter the file for the first time reason cannot be |
| 174 | LC_RENAME. */ |
| 175 | linemap_assert (!(set->depth == 0 && reason == LC_RENAME)); |
| 176 | |
| 177 | /* If we are leaving the main file, return a NULL map. */ |
| 178 | if (reason == LC_LEAVE |
| 179 | && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set)) |
| 180 | && to_file == NULL) |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 181 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 182 | set->depth--; |
| 183 | return NULL; |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 186 | map = new_linemap (set, reason); |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 187 | |
Joseph Myers | c7f9c0b | 2009-04-18 18:36:28 +0100 | [diff] [blame] | 188 | if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM) |
Geoffrey Keating | 9074464 | 2003-07-30 06:43:33 +0000 | [diff] [blame] | 189 | to_file = "<stdin>"; |
| 190 | |
Joseph Myers | c7f9c0b | 2009-04-18 18:36:28 +0100 | [diff] [blame] | 191 | if (reason == LC_RENAME_VERBATIM) |
| 192 | reason = LC_RENAME; |
| 193 | |
Dodji Seketeli | 892a371 | 2011-08-28 20:14:46 +0000 | [diff] [blame] | 194 | if (reason == LC_LEAVE) |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 195 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 196 | /* When we are just leaving an "included" file, and jump to the next |
| 197 | location inside the "includer" right after the #include |
| 198 | "included", this variable points the map in use right before the |
| 199 | #include "included", inside the same "includer" file. */ |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 200 | struct line_map *from; |
| 201 | bool error; |
| 202 | |
| 203 | if (MAIN_FILE_P (map - 1)) |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 204 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 205 | /* So this _should_ means we are leaving the main file -- |
| 206 | effectively ending the compilation unit. But to_file not |
| 207 | being NULL means the caller thinks we are leaving to |
| 208 | another file. This is an erroneous behaviour but we'll |
| 209 | try to recover from it. Let's pretend we are not leaving |
| 210 | the main file. */ |
Per Bothner | b314702 | 2003-07-22 23:11:34 +0000 | [diff] [blame] | 211 | error = true; |
| 212 | reason = LC_RENAME; |
| 213 | from = map - 1; |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 214 | } |
| 215 | else |
| 216 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 217 | /* (MAP - 1) points to the map we are leaving. The |
| 218 | map from which (MAP - 1) got included should be the map |
| 219 | that comes right before MAP in the same file. */ |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 220 | from = INCLUDED_FROM (set, map - 1); |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 221 | error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from), |
| 222 | to_file); |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* Depending upon whether we are handling preprocessed input or |
| 226 | not, this can be a user error or an ICE. */ |
| 227 | if (error) |
| 228 | fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n", |
| 229 | to_file); |
| 230 | |
| 231 | /* A TO_FILE of NULL is special - we use the natural values. */ |
| 232 | if (error || to_file == NULL) |
| 233 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 234 | to_file = ORDINARY_MAP_FILE_NAME (from); |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 235 | to_line = SOURCE_LINE (from, from[1].start_location); |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 236 | sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from); |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 240 | linemap_assert (reason != LC_ENTER_MACRO); |
| 241 | ORDINARY_MAP_IN_SYSTEM_HEADER_P (map) = sysp; |
| 242 | MAP_START_LOCATION (map) = start_location; |
| 243 | ORDINARY_MAP_FILE_NAME (map) = to_file; |
| 244 | ORDINARY_MAP_STARTING_LINE_NUMBER (map) = to_line; |
| 245 | LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1; |
| 246 | ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = 0; |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 247 | set->highest_location = start_location; |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 248 | set->highest_line = start_location; |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 249 | set->max_column_hint = 0; |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 250 | |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 251 | if (reason == LC_ENTER) |
Neil Booth | d8693c6 | 2001-08-21 23:05:12 +0000 | [diff] [blame] | 252 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 253 | ORDINARY_MAP_INCLUDER_FILE_INDEX (map) = |
| 254 | set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2); |
Neil Booth | d8693c6 | 2001-08-21 23:05:12 +0000 | [diff] [blame] | 255 | set->depth++; |
Neil Booth | d8693c6 | 2001-08-21 23:05:12 +0000 | [diff] [blame] | 256 | if (set->trace_includes) |
| 257 | trace_include (set, map); |
| 258 | } |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 259 | else if (reason == LC_RENAME) |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 260 | ORDINARY_MAP_INCLUDER_FILE_INDEX (map) = |
| 261 | ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]); |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 262 | else if (reason == LC_LEAVE) |
Neil Booth | d8693c6 | 2001-08-21 23:05:12 +0000 | [diff] [blame] | 263 | { |
| 264 | set->depth--; |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 265 | ORDINARY_MAP_INCLUDER_FILE_INDEX (map) = |
| 266 | ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1)); |
Neil Booth | d8693c6 | 2001-08-21 23:05:12 +0000 | [diff] [blame] | 267 | } |
Neil Booth | 5993019 | 2001-08-21 21:17:48 +0000 | [diff] [blame] | 268 | |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 269 | return map; |
| 270 | } |
| 271 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 272 | /* Returns TRUE if the line table set tracks token locations accross |
| 273 | macro expansion, FALSE otherwise. */ |
| 274 | |
| 275 | bool |
| 276 | linemap_tracks_macro_expansion_locs_p (struct line_maps *set) |
| 277 | { |
| 278 | return LINEMAPS_MACRO_MAPS (set) != NULL; |
| 279 | } |
| 280 | |
| 281 | /* Create a macro map. A macro map encodes source locations of tokens |
| 282 | that are part of a macro replacement-list, at a macro expansion |
| 283 | point. See the extensive comments of struct line_map and struct |
| 284 | line_map_macro, in line-map.h. |
| 285 | |
| 286 | This map shall be created when the macro is expanded. The map |
| 287 | encodes the source location of the expansion point of the macro as |
| 288 | well as the "original" source location of each token that is part |
| 289 | of the macro replacement-list. If a macro is defined but never |
| 290 | expanded, it has no macro map. SET is the set of maps the macro |
| 291 | map should be part of. MACRO_NODE is the macro which the new macro |
| 292 | map should encode source locations for. EXPANSION is the location |
| 293 | of the expansion point of MACRO. For function-like macros |
| 294 | invocations, it's best to make it point to the closing parenthesis |
| 295 | of the macro, rather than the the location of the first character |
| 296 | of the macro. NUM_TOKENS is the number of tokens that are part of |
| 297 | the replacement-list of MACRO. |
| 298 | |
| 299 | Note that when we run out of the integer space available for source |
| 300 | locations, this function returns NULL. In that case, callers of |
| 301 | this function cannot encode {line,column} pairs into locations of |
| 302 | macro tokens anymore. */ |
| 303 | |
| 304 | const struct line_map * |
| 305 | linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node, |
| 306 | source_location expansion, unsigned int num_tokens) |
| 307 | { |
| 308 | struct line_map *map; |
| 309 | source_location start_location; |
| 310 | line_map_realloc reallocator |
| 311 | = set->reallocator ? set->reallocator : xrealloc; |
| 312 | |
| 313 | start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens; |
| 314 | |
| 315 | if (start_location <= set->highest_line |
| 316 | || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set)) |
| 317 | /* We ran out of macro map space. */ |
| 318 | return NULL; |
| 319 | |
| 320 | map = new_linemap (set, LC_ENTER_MACRO); |
| 321 | |
| 322 | MAP_START_LOCATION (map) = start_location; |
| 323 | MACRO_MAP_MACRO (map) = macro_node; |
| 324 | MACRO_MAP_NUM_MACRO_TOKENS (map) = num_tokens; |
| 325 | MACRO_MAP_LOCATIONS (map) |
| 326 | = (source_location*) reallocator (NULL, |
| 327 | 2 * num_tokens |
| 328 | * sizeof (source_location)); |
| 329 | MACRO_MAP_EXPANSION_POINT_LOCATION (map) = expansion; |
| 330 | memset (MACRO_MAP_LOCATIONS (map), 0, |
| 331 | num_tokens * sizeof (source_location)); |
| 332 | |
| 333 | LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1; |
| 334 | set->max_column_hint = 0; |
| 335 | |
| 336 | return map; |
| 337 | } |
| 338 | |
| 339 | /* Create and return a virtual location for a token that is part of a |
| 340 | macro expansion-list at a macro expansion point. See the comment |
| 341 | inside struct line_map_macro to see what an expansion-list exactly |
| 342 | is. |
| 343 | |
| 344 | A call to this function must come after a call to |
| 345 | linemap_enter_macro. |
| 346 | |
| 347 | MAP is the map into which the source location is created. TOKEN_NO |
| 348 | is the index of the token in the macro replacement-list, starting |
| 349 | at number 0. |
| 350 | |
| 351 | ORIG_LOC is the location of the token outside of this macro |
| 352 | expansion. If the token comes originally from the macro |
| 353 | definition, it is the locus in the macro definition; otherwise it |
| 354 | is a location in the context of the caller of this macro expansion |
| 355 | (which is a virtual location or a source location if the caller is |
| 356 | itself a macro expansion or not). |
| 357 | |
| 358 | MACRO_DEFINITION_LOC is the location in the macro definition, |
| 359 | either of the token itself or of a macro parameter that it |
| 360 | replaces. */ |
| 361 | |
| 362 | source_location |
| 363 | linemap_add_macro_token (const struct line_map *map, |
| 364 | unsigned int token_no, |
| 365 | source_location orig_loc, |
| 366 | source_location orig_parm_replacement_loc) |
| 367 | { |
| 368 | source_location result; |
| 369 | |
| 370 | linemap_assert (linemap_macro_expansion_map_p (map)); |
| 371 | linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map)); |
| 372 | |
| 373 | MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc; |
| 374 | MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc; |
| 375 | |
| 376 | result = MAP_START_LOCATION (map) + token_no; |
| 377 | return result; |
| 378 | } |
| 379 | |
| 380 | /* Return a source_location for the start (i.e. column==0) of |
| 381 | (physical) line TO_LINE in the current source file (as in the |
| 382 | most recent linemap_add). MAX_COLUMN_HINT is the highest column |
| 383 | number we expect to use in this line (but it does not change |
| 384 | the highest_location). */ |
| 385 | |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 386 | source_location |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 387 | linemap_line_start (struct line_maps *set, linenum_type to_line, |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 388 | unsigned int max_column_hint) |
| 389 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 390 | struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set); |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 391 | source_location highest = set->highest_location; |
| 392 | source_location r; |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 393 | linenum_type last_line = |
| 394 | SOURCE_LINE (map, set->highest_line); |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 395 | int line_delta = to_line - last_line; |
| 396 | bool add_map = false; |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 397 | |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 398 | if (line_delta < 0 |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 399 | || (line_delta > 10 |
| 400 | && line_delta * ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) > 1000) |
| 401 | || (max_column_hint >= (1U << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))) |
| 402 | || (max_column_hint <= 80 |
| 403 | && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10)) |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 404 | { |
| 405 | add_map = true; |
| 406 | } |
| 407 | else |
| 408 | max_column_hint = set->max_column_hint; |
| 409 | if (add_map) |
| 410 | { |
| 411 | int column_bits; |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 412 | if (max_column_hint > 100000 || highest > 0xC0000000) |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 413 | { |
Per Bothner | c1fc504 | 2005-04-21 10:08:27 -0700 | [diff] [blame] | 414 | /* If the column number is ridiculous or we've allocated a huge |
| 415 | number of source_locations, give up on column numbers. */ |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 416 | max_column_hint = 0; |
| 417 | if (highest >0xF0000000) |
| 418 | return 0; |
| 419 | column_bits = 0; |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | column_bits = 7; |
| 424 | while (max_column_hint >= (1U << column_bits)) |
| 425 | column_bits++; |
| 426 | max_column_hint = 1U << column_bits; |
| 427 | } |
Per Bothner | c1fc504 | 2005-04-21 10:08:27 -0700 | [diff] [blame] | 428 | /* Allocate the new line_map. However, if the current map only has a |
| 429 | single line we can sometimes just increase its column_bits instead. */ |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 430 | if (line_delta < 0 |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 431 | || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map) |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 432 | || SOURCE_COLUMN (map, highest) >= (1U << column_bits)) |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 433 | map = (struct line_map *) linemap_add (set, LC_RENAME, |
| 434 | ORDINARY_MAP_IN_SYSTEM_HEADER_P |
| 435 | (map), |
| 436 | ORDINARY_MAP_FILE_NAME (map), |
| 437 | to_line); |
| 438 | ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = column_bits; |
| 439 | r = (MAP_START_LOCATION (map) |
| 440 | + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map)) |
| 441 | << column_bits)); |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 442 | } |
| 443 | else |
| 444 | r = highest - SOURCE_COLUMN (map, highest) |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 445 | + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)); |
| 446 | |
| 447 | /* Locations of ordinary tokens are always lower than locations of |
| 448 | macro tokens. */ |
| 449 | if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set)) |
| 450 | return 0; |
| 451 | |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 452 | set->highest_line = r; |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 453 | if (r > set->highest_location) |
| 454 | set->highest_location = r; |
| 455 | set->max_column_hint = max_column_hint; |
| 456 | return r; |
| 457 | } |
| 458 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 459 | /* Encode and return a source_location from a column number. The |
| 460 | source line considered is the last source line used to call |
| 461 | linemap_line_start, i.e, the last source line which a location was |
| 462 | encoded from. */ |
| 463 | |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 464 | source_location |
| 465 | linemap_position_for_column (struct line_maps *set, unsigned int to_column) |
| 466 | { |
| 467 | source_location r = set->highest_line; |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 468 | |
| 469 | linemap_assert |
| 470 | (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set))); |
| 471 | |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 472 | if (to_column >= set->max_column_hint) |
| 473 | { |
| 474 | if (r >= 0xC000000 || to_column > 100000) |
| 475 | { |
| 476 | /* Running low on source_locations - disable column numbers. */ |
| 477 | return r; |
| 478 | } |
| 479 | else |
| 480 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 481 | struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set); |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 482 | r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50); |
| 483 | } |
| 484 | } |
| 485 | r = r + to_column; |
| 486 | if (r >= set->highest_location) |
| 487 | set->highest_location = r; |
| 488 | return r; |
| 489 | } |
| 490 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 491 | /* Encode and return a source location from a given line and |
| 492 | column. */ |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 493 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 494 | source_location |
| 495 | linemap_position_for_line_and_column (struct line_map *map, |
| 496 | linenum_type line, |
| 497 | unsigned column) |
| 498 | { |
| 499 | linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (map) <= line); |
| 500 | |
| 501 | return (MAP_START_LOCATION (map) |
| 502 | + ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (map)) |
| 503 | << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) |
| 504 | + (column & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1))); |
| 505 | } |
| 506 | |
| 507 | /* Given a virtual source location yielded by a map (either an |
| 508 | ordinary or a macro map), returns that map. */ |
| 509 | |
| 510 | const struct line_map* |
Per Bothner | 7d40b45 | 2003-12-05 12:52:39 -0800 | [diff] [blame] | 511 | linemap_lookup (struct line_maps *set, source_location line) |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 512 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 513 | if (linemap_location_from_macro_expansion_p (set, line)) |
| 514 | return linemap_macro_map_lookup (set, line); |
| 515 | return linemap_ordinary_map_lookup (set, line); |
| 516 | } |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 517 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 518 | /* Given a source location yielded by an ordinary map, returns that |
| 519 | map. Since the set is built chronologically, the logical lines are |
| 520 | monotonic increasing, and so the list is sorted and we can use a |
| 521 | binary search. */ |
| 522 | |
| 523 | static const struct line_map * |
| 524 | linemap_ordinary_map_lookup (struct line_maps *set, source_location line) |
| 525 | { |
| 526 | unsigned int md, mn, mx; |
| 527 | const struct line_map *cached, *result; |
| 528 | |
| 529 | if (set == NULL || line < RESERVED_LOCATION_COUNT) |
| 530 | return NULL; |
| 531 | |
| 532 | mn = LINEMAPS_ORDINARY_CACHE (set); |
| 533 | mx = LINEMAPS_ORDINARY_USED (set); |
Per Bothner | 9132fbb | 2004-01-20 05:17:48 +0000 | [diff] [blame] | 534 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 535 | cached = LINEMAPS_ORDINARY_MAP_AT (set, mn); |
Kazu Hirata | 9ac9746 | 2004-02-16 14:20:10 +0000 | [diff] [blame] | 536 | /* We should get a segfault if no line_maps have been added yet. */ |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 537 | if (line >= MAP_START_LOCATION (cached)) |
Per Bothner | 9132fbb | 2004-01-20 05:17:48 +0000 | [diff] [blame] | 538 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 539 | if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1])) |
Per Bothner | 9132fbb | 2004-01-20 05:17:48 +0000 | [diff] [blame] | 540 | return cached; |
| 541 | } |
| 542 | else |
| 543 | { |
| 544 | mx = mn; |
| 545 | mn = 0; |
| 546 | } |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 547 | |
| 548 | while (mx - mn > 1) |
| 549 | { |
| 550 | md = (mn + mx) / 2; |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 551 | if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line) |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 552 | mx = md; |
| 553 | else |
| 554 | mn = md; |
| 555 | } |
| 556 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 557 | LINEMAPS_ORDINARY_CACHE (set) = mn; |
| 558 | result = LINEMAPS_ORDINARY_MAP_AT (set, mn); |
| 559 | linemap_assert (line >= MAP_START_LOCATION (result)); |
| 560 | return result; |
| 561 | } |
| 562 | |
| 563 | /* Given a source location yielded by a macro map, returns that map. |
| 564 | Since the set is built chronologically, the logical lines are |
| 565 | monotonic decreasing, and so the list is sorted and we can use a |
| 566 | binary search. */ |
| 567 | |
| 568 | static const struct line_map* |
| 569 | linemap_macro_map_lookup (struct line_maps *set, source_location line) |
| 570 | { |
| 571 | unsigned int md, mn, mx; |
| 572 | const struct line_map *cached, *result; |
| 573 | |
| 574 | linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set)); |
| 575 | |
| 576 | if (set == NULL) |
| 577 | return NULL; |
| 578 | |
| 579 | mn = LINEMAPS_MACRO_CACHE (set); |
| 580 | mx = LINEMAPS_MACRO_USED (set); |
| 581 | cached = LINEMAPS_MACRO_MAP_AT (set, mn); |
| 582 | |
| 583 | if (line >= MAP_START_LOCATION (cached)) |
| 584 | { |
| 585 | if (mn == 0 || line < MAP_START_LOCATION (&cached[-1])) |
| 586 | return cached; |
| 587 | mx = mn - 1; |
| 588 | mn = 0; |
| 589 | } |
| 590 | |
Dodji Seketeli | 3bb0c8d | 2011-10-25 08:59:07 +0000 | [diff] [blame] | 591 | while (mn < mx) |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 592 | { |
| 593 | md = (mx + mn) / 2; |
| 594 | if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line) |
Dodji Seketeli | 3bb0c8d | 2011-10-25 08:59:07 +0000 | [diff] [blame] | 595 | mn = md + 1; |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 596 | else |
| 597 | mx = md; |
Dodji Seketeli | 3bb0c8d | 2011-10-25 08:59:07 +0000 | [diff] [blame] | 598 | } |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 599 | |
| 600 | LINEMAPS_MACRO_CACHE (set) = mx; |
| 601 | result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set)); |
| 602 | linemap_assert (MAP_START_LOCATION (result) <= line); |
| 603 | |
| 604 | return result; |
| 605 | } |
| 606 | |
| 607 | /* Return TRUE if MAP encodes locations coming from a macro |
| 608 | replacement-list at macro expansion point. */ |
| 609 | |
| 610 | bool |
| 611 | linemap_macro_expansion_map_p (const struct line_map *map) |
| 612 | { |
| 613 | if (!map) |
| 614 | return false; |
| 615 | return (map->reason == LC_ENTER_MACRO); |
| 616 | } |
| 617 | |
| 618 | /* If LOCATION is the locus of a token in a replacement-list of a |
| 619 | macro expansion return the location of the macro expansion point. |
| 620 | |
| 621 | Read the comments of struct line_map and struct line_map_macro in |
| 622 | line-map.h to understand what a macro expansion point is. */ |
| 623 | |
Dodji Seketeli | 411f92de | 2011-10-17 14:01:37 +0000 | [diff] [blame] | 624 | static source_location |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 625 | linemap_macro_map_loc_to_exp_point (const struct line_map *map, |
Dodji Seketeli | 411f92de | 2011-10-17 14:01:37 +0000 | [diff] [blame] | 626 | source_location location ATTRIBUTE_UNUSED) |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 627 | { |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 628 | linemap_assert (linemap_macro_expansion_map_p (map) |
| 629 | && location >= MAP_START_LOCATION (map)); |
| 630 | |
| 631 | /* Make sure LOCATION is correct. */ |
Dodji Seketeli | 411f92de | 2011-10-17 14:01:37 +0000 | [diff] [blame] | 632 | linemap_assert ((location - MAP_START_LOCATION (map)) |
| 633 | < MACRO_MAP_NUM_MACRO_TOKENS (map)); |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 634 | |
| 635 | return MACRO_MAP_EXPANSION_POINT_LOCATION (map); |
| 636 | } |
| 637 | |
| 638 | /* If LOCATION is the source location of a token that belongs to a |
| 639 | macro replacement-list -- as part of a macro expansion -- then |
| 640 | return the location of the token at the definition point of the |
| 641 | macro. Otherwise, return LOCATION. SET is the set of maps |
| 642 | location come from. ORIGINAL_MAP is an output parm. If non NULL, |
| 643 | the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the |
| 644 | returned location comes from. */ |
| 645 | |
| 646 | source_location |
| 647 | linemap_macro_map_loc_to_def_point (const struct line_map *map, |
| 648 | source_location location) |
| 649 | { |
| 650 | unsigned token_no; |
| 651 | |
| 652 | linemap_assert (linemap_macro_expansion_map_p (map) |
| 653 | && location >= MAP_START_LOCATION (map)); |
| 654 | linemap_assert (location >= RESERVED_LOCATION_COUNT); |
| 655 | |
| 656 | token_no = location - MAP_START_LOCATION (map); |
| 657 | linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map)); |
| 658 | |
| 659 | location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1]; |
| 660 | |
| 661 | return location; |
| 662 | } |
| 663 | |
| 664 | /* If LOCATION is the locus of a token that is an argument of a |
| 665 | function-like macro M and appears in the expansion of M, return the |
| 666 | locus of that argument in the context of the caller of M. |
| 667 | |
| 668 | In other words, this returns the xI location presented in the |
| 669 | comments of line_map_macro above. */ |
| 670 | source_location |
| 671 | linemap_macro_map_loc_unwind_toward_spelling (const struct line_map* map, |
| 672 | source_location location) |
| 673 | { |
| 674 | unsigned token_no; |
| 675 | |
| 676 | linemap_assert (linemap_macro_expansion_map_p (map) |
| 677 | && location >= MAP_START_LOCATION (map)); |
| 678 | linemap_assert (location >= RESERVED_LOCATION_COUNT); |
| 679 | |
| 680 | token_no = location - MAP_START_LOCATION (map); |
| 681 | linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map)); |
| 682 | |
| 683 | location = MACRO_MAP_LOCATIONS (map)[2 * token_no]; |
| 684 | |
| 685 | return location; |
| 686 | } |
| 687 | |
| 688 | /* Return the source line number corresponding to source location |
| 689 | LOCATION. SET is the line map set LOCATION comes from. If |
| 690 | LOCATION is the source location of token that is part of the |
| 691 | replacement-list of a macro expansion return the line number of the |
| 692 | macro expansion point. */ |
| 693 | |
| 694 | int |
| 695 | linemap_get_expansion_line (struct line_maps *set, |
| 696 | source_location location) |
| 697 | { |
| 698 | const struct line_map *map = NULL; |
| 699 | |
| 700 | if (location < RESERVED_LOCATION_COUNT) |
| 701 | return 0; |
| 702 | |
| 703 | location = |
| 704 | linemap_macro_loc_to_exp_point (set, location, &map); |
| 705 | |
| 706 | return SOURCE_LINE (map, location); |
| 707 | } |
| 708 | |
| 709 | /* Return the path of the file corresponding to source code location |
| 710 | LOCATION. |
| 711 | |
| 712 | If LOCATION is the source location of token that is part of the |
| 713 | replacement-list of a macro expansion return the file path of the |
| 714 | macro expansion point. |
| 715 | |
| 716 | SET is the line map set LOCATION comes from. */ |
| 717 | |
| 718 | const char* |
| 719 | linemap_get_expansion_filename (struct line_maps *set, |
| 720 | source_location location) |
| 721 | { |
| 722 | const struct line_map *map = NULL; |
| 723 | |
| 724 | if (location < RESERVED_LOCATION_COUNT) |
| 725 | return NULL; |
| 726 | |
| 727 | location = |
| 728 | linemap_macro_loc_to_exp_point (set, location, &map); |
| 729 | |
| 730 | return LINEMAP_FILE (map); |
| 731 | } |
| 732 | |
| 733 | /* Return the name of the macro associated to MACRO_MAP. */ |
| 734 | |
| 735 | const char* |
| 736 | linemap_map_get_macro_name (const struct line_map* macro_map) |
| 737 | { |
| 738 | linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map)); |
| 739 | return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map)); |
| 740 | } |
| 741 | |
| 742 | /* Return a positive value if LOCATION is the locus of a token that is |
| 743 | located in a system header, O otherwise. It returns 1 if LOCATION |
| 744 | is the locus of a token that is located in a system header, and 2 |
| 745 | if LOCATION is the locus of a token located in a C system header |
| 746 | that therefore needs to be extern "C" protected in C++. |
| 747 | |
| 748 | Note that this function returns 1 if LOCATION belongs to a token |
| 749 | that is part of a macro replacement-list defined in a system |
| 750 | header, but expanded in a non-system file. */ |
| 751 | |
| 752 | int |
| 753 | linemap_location_in_system_header_p (struct line_maps *set, |
| 754 | source_location location) |
| 755 | { |
| 756 | const struct line_map *map = NULL; |
| 757 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 758 | location = |
| 759 | linemap_resolve_location (set, location, LRK_SPELLING_LOCATION, &map); |
| 760 | |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 761 | if (location < RESERVED_LOCATION_COUNT) |
| 762 | return false; |
| 763 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 764 | return LINEMAP_SYSP (map); |
| 765 | } |
| 766 | |
| 767 | /* Return TRUE if LOCATION is a source code location of a token coming |
| 768 | from a macro replacement-list at a macro expansion point, FALSE |
| 769 | otherwise. */ |
| 770 | |
| 771 | bool |
| 772 | linemap_location_from_macro_expansion_p (struct line_maps *set, |
| 773 | source_location location) |
| 774 | { |
| 775 | linemap_assert (location <= MAX_SOURCE_LOCATION |
| 776 | && (set->highest_location |
| 777 | < LINEMAPS_MACRO_LOWEST_LOCATION (set))); |
| 778 | if (set == NULL) |
| 779 | return false; |
| 780 | return (location > set->highest_location); |
| 781 | } |
| 782 | |
| 783 | /* Given two virtual locations *LOC0 and *LOC1, return the first |
| 784 | common macro map in their macro expansion histories. Return NULL |
| 785 | if no common macro was found. *LOC0 (resp. *LOC1) is set to the |
| 786 | virtual location of the token inside the resulting macro. */ |
| 787 | |
| 788 | static const struct line_map* |
| 789 | first_map_in_common_1 (struct line_maps *set, |
| 790 | source_location *loc0, |
| 791 | source_location *loc1) |
| 792 | { |
| 793 | source_location l0 = *loc0, l1 = *loc1; |
| 794 | const struct line_map *map0 = linemap_lookup (set, l0), |
| 795 | *map1 = linemap_lookup (set, l1); |
| 796 | |
| 797 | while (linemap_macro_expansion_map_p (map0) |
| 798 | && linemap_macro_expansion_map_p (map1) |
| 799 | && (map0 != map1)) |
| 800 | { |
| 801 | if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1)) |
| 802 | { |
| 803 | l0 = linemap_macro_map_loc_to_exp_point (map0, l0); |
| 804 | map0 = linemap_lookup (set, l0); |
| 805 | } |
| 806 | else |
| 807 | { |
| 808 | l1 = linemap_macro_map_loc_to_exp_point (map1, l1); |
| 809 | map1 = linemap_lookup (set, l1); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | if (map0 == map1) |
| 814 | { |
| 815 | *loc0 = l0; |
| 816 | *loc1 = l1; |
| 817 | return map0; |
| 818 | } |
| 819 | return NULL; |
| 820 | } |
| 821 | |
| 822 | /* Given two virtual locations LOC0 and LOC1, return the first common |
| 823 | macro map in their macro expansion histories. Return NULL if no |
| 824 | common macro was found. *RES_LOC0 (resp. *RES_LOC1) is set to the |
| 825 | virtual location of the token inside the resulting macro, upon |
| 826 | return of a non-NULL result. */ |
| 827 | |
| 828 | static const struct line_map* |
| 829 | first_map_in_common (struct line_maps *set, |
| 830 | source_location loc0, |
| 831 | source_location loc1, |
| 832 | source_location *res_loc0, |
| 833 | source_location *res_loc1) |
| 834 | { |
| 835 | *res_loc0 = loc0; |
| 836 | *res_loc1 = loc1; |
| 837 | |
| 838 | return first_map_in_common_1 (set, res_loc0, res_loc1); |
| 839 | } |
| 840 | |
| 841 | /* Return a positive value if PRE denotes the location of a token that |
| 842 | comes before the token of POST, 0 if PRE denotes the location of |
| 843 | the same token as the token for POST, and a negative value |
| 844 | otherwise. */ |
| 845 | |
| 846 | int |
| 847 | linemap_compare_locations (struct line_maps *set, |
| 848 | source_location pre, |
| 849 | source_location post) |
| 850 | { |
| 851 | bool pre_virtual_p, post_virtual_p; |
| 852 | source_location l0 = pre, l1 = post; |
| 853 | |
| 854 | if (l0 == l1) |
| 855 | return 0; |
| 856 | |
| 857 | if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0))) |
| 858 | l0 = linemap_resolve_location (set, l0, |
| 859 | LRK_MACRO_EXPANSION_POINT, |
| 860 | NULL); |
| 861 | |
| 862 | if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1))) |
| 863 | l1 = linemap_resolve_location (set, l1, |
| 864 | LRK_MACRO_EXPANSION_POINT, |
| 865 | NULL); |
| 866 | |
| 867 | if (l0 == l1 |
| 868 | && pre_virtual_p |
| 869 | && post_virtual_p) |
| 870 | { |
| 871 | /* So pre and post represent two tokens that are present in a |
| 872 | same macro expansion. Let's see if the token for pre was |
| 873 | before the token for post in that expansion. */ |
| 874 | unsigned i0, i1; |
| 875 | const struct line_map *map = |
| 876 | first_map_in_common (set, pre, post, &l0, &l1); |
| 877 | |
| 878 | if (map == NULL) |
| 879 | /* This should not be possible. */ |
| 880 | abort (); |
| 881 | |
| 882 | i0 = l0 - MAP_START_LOCATION (map); |
| 883 | i1 = l1 - MAP_START_LOCATION (map); |
| 884 | return i1 - i0; |
| 885 | } |
| 886 | |
| 887 | return l1 - l0; |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 888 | } |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 889 | |
Neil Booth | 5993019 | 2001-08-21 21:17:48 +0000 | [diff] [blame] | 890 | /* Print an include trace, for e.g. the -H option of the preprocessor. */ |
| 891 | |
| 892 | static void |
Neil Booth | e2b9853 | 2003-06-20 05:51:13 +0000 | [diff] [blame] | 893 | trace_include (const struct line_maps *set, const struct line_map *map) |
Neil Booth | 5993019 | 2001-08-21 21:17:48 +0000 | [diff] [blame] | 894 | { |
Neil Booth | d8693c6 | 2001-08-21 23:05:12 +0000 | [diff] [blame] | 895 | unsigned int i = set->depth; |
Neil Booth | 5993019 | 2001-08-21 21:17:48 +0000 | [diff] [blame] | 896 | |
Neil Booth | d8693c6 | 2001-08-21 23:05:12 +0000 | [diff] [blame] | 897 | while (--i) |
Neil Booth | 5993019 | 2001-08-21 21:17:48 +0000 | [diff] [blame] | 898 | putc ('.', stderr); |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 899 | |
| 900 | fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map)); |
| 901 | } |
| 902 | |
| 903 | /* Return the spelling location of the token wherever it comes from, |
| 904 | whether part of a macro definition or not. |
| 905 | |
| 906 | This is a subroutine for linemap_resolve_location. */ |
| 907 | |
| 908 | static source_location |
| 909 | linemap_macro_loc_to_spelling_point (struct line_maps *set, |
| 910 | source_location location, |
| 911 | const struct line_map **original_map) |
| 912 | { |
| 913 | struct line_map *map; |
| 914 | |
| 915 | linemap_assert (set && location >= RESERVED_LOCATION_COUNT); |
| 916 | |
| 917 | while (true) |
| 918 | { |
| 919 | map = (struct line_map*) linemap_lookup (set, location); |
| 920 | if (!linemap_macro_expansion_map_p (map)) |
| 921 | break; |
| 922 | |
| 923 | location = |
| 924 | linemap_macro_map_loc_unwind_toward_spelling (map, location); |
| 925 | } |
| 926 | |
| 927 | if (original_map) |
| 928 | *original_map = map; |
| 929 | return location; |
| 930 | } |
| 931 | |
| 932 | /* If LOCATION is the source location of a token that belongs to a |
| 933 | macro replacement-list -- as part of a macro expansion -- then |
| 934 | return the location of the token at the definition point of the |
| 935 | macro. Otherwise, return LOCATION. SET is the set of maps |
| 936 | location come from. ORIGINAL_MAP is an output parm. If non NULL, |
| 937 | the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the |
| 938 | returned location comes from. |
| 939 | |
| 940 | This is a subroutine of linemap_resolve_location. */ |
| 941 | |
| 942 | static source_location |
| 943 | linemap_macro_loc_to_def_point (struct line_maps *set, |
| 944 | source_location location, |
| 945 | const struct line_map **original_map) |
| 946 | { |
| 947 | struct line_map *map; |
| 948 | |
| 949 | linemap_assert (set && location >= RESERVED_LOCATION_COUNT); |
| 950 | |
| 951 | while (true) |
| 952 | { |
| 953 | map = (struct line_map*) linemap_lookup (set, location); |
| 954 | if (!linemap_macro_expansion_map_p (map)) |
| 955 | break; |
| 956 | |
| 957 | location = |
| 958 | linemap_macro_map_loc_to_def_point (map, location); |
| 959 | } |
| 960 | |
| 961 | if (original_map) |
| 962 | *original_map = map; |
| 963 | return location; |
| 964 | } |
| 965 | |
| 966 | /* If LOCATION is the source location of a token that belongs to a |
| 967 | macro replacement-list -- at a macro expansion point -- then return |
| 968 | the location of the topmost expansion point of the macro. We say |
| 969 | topmost because if we are in the context of a nested macro |
| 970 | expansion, the function returns the source location of the first |
| 971 | macro expansion that triggered the nested expansions. |
| 972 | |
| 973 | Otherwise, return LOCATION. SET is the set of maps location come |
| 974 | from. ORIGINAL_MAP is an output parm. If non NULL, the function |
| 975 | sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned |
| 976 | location comes from. |
| 977 | |
| 978 | This is a subroutine of linemap_resolve_location. */ |
| 979 | |
| 980 | static source_location |
| 981 | linemap_macro_loc_to_exp_point (struct line_maps *set, |
| 982 | source_location location, |
| 983 | const struct line_map **original_map) |
| 984 | { |
| 985 | struct line_map *map; |
| 986 | |
| 987 | linemap_assert (set && location >= RESERVED_LOCATION_COUNT); |
| 988 | |
| 989 | while (true) |
| 990 | { |
| 991 | map = (struct line_map*) linemap_lookup (set, location); |
| 992 | if (!linemap_macro_expansion_map_p (map)) |
| 993 | break; |
| 994 | location = linemap_macro_map_loc_to_exp_point (map, location); |
| 995 | } |
| 996 | |
| 997 | if (original_map) |
| 998 | *original_map = map; |
| 999 | return location; |
| 1000 | } |
| 1001 | |
| 1002 | /* Resolve a virtual location into either a spelling location, an |
| 1003 | expansion point location or a token argument replacement point |
| 1004 | location. Return the map that encodes the virtual location as well |
| 1005 | as the resolved location. |
| 1006 | |
| 1007 | If LOC is *NOT* the location of a token resulting from the |
| 1008 | expansion of a macro, then the parameter LRK (which stands for |
| 1009 | Location Resolution Kind) is ignored and the resulting location |
| 1010 | just equals the one given in argument. |
| 1011 | |
| 1012 | Now if LOC *IS* the location of a token resulting from the |
| 1013 | expansion of a macro, this is what happens. |
| 1014 | |
| 1015 | * If LRK is set to LRK_MACRO_EXPANSION_POINT |
| 1016 | ------------------------------- |
| 1017 | |
Manuel López-Ibáñez | 1d72e96 | 2012-04-26 07:14:01 +0000 | [diff] [blame] | 1018 | The virtual location is resolved to the first macro expansion point |
| 1019 | that led to this macro expansion. |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1020 | |
| 1021 | * If LRK is set to LRK_SPELLING_LOCATION |
| 1022 | ------------------------------------- |
| 1023 | |
Manuel López-Ibáñez | 1d72e96 | 2012-04-26 07:14:01 +0000 | [diff] [blame] | 1024 | The virtual location is resolved to the locus where the token has |
| 1025 | been spelled in the source. This can follow through all the macro |
| 1026 | expansions that led to the token. |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1027 | |
Manuel López-Ibáñez | 1d72e96 | 2012-04-26 07:14:01 +0000 | [diff] [blame] | 1028 | * If LRK is set to LRK_MACRO_DEFINITION_LOCATION |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1029 | -------------------------------------- |
| 1030 | |
Manuel López-Ibáñez | 1d72e96 | 2012-04-26 07:14:01 +0000 | [diff] [blame] | 1031 | The virtual location is resolved to the locus of the token in the |
| 1032 | context of the macro definition. |
| 1033 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1034 | If LOC is the locus of a token that is an argument of a |
| 1035 | function-like macro [replacing a parameter in the replacement list |
| 1036 | of the macro] the virtual location is resolved to the locus of the |
| 1037 | parameter that is replaced, in the context of the definition of the |
| 1038 | macro. |
| 1039 | |
| 1040 | If LOC is the locus of a token that is not an argument of a |
| 1041 | function-like macro, then the function behaves as if LRK was set to |
| 1042 | LRK_SPELLING_LOCATION. |
| 1043 | |
Manuel López-Ibáñez | 1d72e96 | 2012-04-26 07:14:01 +0000 | [diff] [blame] | 1044 | If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the |
| 1045 | returned location. Note that if the returned location wasn't originally |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1046 | encoded by a map, the *MAP is set to NULL. This can happen if LOC |
| 1047 | resolves to a location reserved for the client code, like |
| 1048 | UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */ |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1049 | |
| 1050 | source_location |
| 1051 | linemap_resolve_location (struct line_maps *set, |
| 1052 | source_location loc, |
| 1053 | enum location_resolution_kind lrk, |
| 1054 | const struct line_map **map) |
| 1055 | { |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1056 | if (loc < RESERVED_LOCATION_COUNT) |
| 1057 | { |
| 1058 | /* A reserved location wasn't encoded in a map. Let's return a |
| 1059 | NULL map here, just like what linemap_ordinary_map_lookup |
| 1060 | does. */ |
| 1061 | if (map) |
| 1062 | *map = NULL; |
| 1063 | return loc; |
| 1064 | } |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1065 | |
| 1066 | switch (lrk) |
| 1067 | { |
| 1068 | case LRK_MACRO_EXPANSION_POINT: |
| 1069 | loc = linemap_macro_loc_to_exp_point (set, loc, map); |
| 1070 | break; |
| 1071 | case LRK_SPELLING_LOCATION: |
| 1072 | loc = linemap_macro_loc_to_spelling_point (set, loc, map); |
| 1073 | break; |
| 1074 | case LRK_MACRO_DEFINITION_LOCATION: |
| 1075 | loc = linemap_macro_loc_to_def_point (set, loc, map); |
| 1076 | break; |
| 1077 | default: |
| 1078 | abort (); |
| 1079 | } |
| 1080 | return loc; |
| 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | Suppose that LOC is the virtual location of a token T coming from |
| 1085 | the expansion of a macro M. This function then steps up to get the |
| 1086 | location L of the point where M got expanded. If L is a spelling |
| 1087 | location inside a macro expansion M', then this function returns |
| 1088 | the locus of the point where M' was expanded. Said otherwise, this |
| 1089 | function returns the location of T in the context that triggered |
| 1090 | the expansion of M. |
| 1091 | |
| 1092 | *LOC_MAP must be set to the map of LOC. This function then sets it |
| 1093 | to the map of the returned location. */ |
| 1094 | |
| 1095 | source_location |
| 1096 | linemap_unwind_toward_expansion (struct line_maps *set, |
| 1097 | source_location loc, |
| 1098 | const struct line_map **map) |
| 1099 | { |
| 1100 | source_location resolved_location; |
| 1101 | const struct line_map *resolved_map; |
| 1102 | |
| 1103 | resolved_location = |
| 1104 | linemap_macro_map_loc_unwind_toward_spelling (*map, loc); |
| 1105 | resolved_map = linemap_lookup (set, resolved_location); |
| 1106 | |
| 1107 | if (!linemap_macro_expansion_map_p (resolved_map)) |
| 1108 | { |
| 1109 | resolved_location = linemap_macro_map_loc_to_exp_point (*map, loc); |
| 1110 | resolved_map = linemap_lookup (set, resolved_location); |
| 1111 | } |
| 1112 | |
| 1113 | *map = resolved_map; |
| 1114 | return resolved_location; |
| 1115 | } |
| 1116 | |
Dodji Seketeli | c4ca1a0 | 2012-04-30 11:42:12 +0000 | [diff] [blame^] | 1117 | /* If LOC is the virtual location of a token coming from the expansion |
| 1118 | of a macro M and if its spelling location is reserved (e.g, a |
| 1119 | location for a built-in token), then this function unwinds (using |
| 1120 | linemap_unwind_toward_expansion) the location until a location that |
| 1121 | is not reserved and is not in a sytem header is reached. In other |
| 1122 | words, this unwinds the reserved location until a location that is |
| 1123 | in real source code is reached. |
| 1124 | |
| 1125 | Otherwise, if the spelling location for LOC is not reserved or if |
| 1126 | LOC doesn't come from the expansion of a macro, the function |
| 1127 | returns LOC as is and *MAP is not touched. |
| 1128 | |
| 1129 | *MAP is set to the map of the returned location if the later is |
| 1130 | different from LOC. */ |
| 1131 | source_location |
| 1132 | linemap_unwind_to_first_non_reserved_loc (struct line_maps *set, |
| 1133 | source_location loc, |
| 1134 | const struct line_map **map) |
| 1135 | { |
| 1136 | source_location resolved_loc; |
| 1137 | const struct line_map *map0 = NULL, *map1 = NULL; |
| 1138 | |
| 1139 | map0 = linemap_lookup (set, loc); |
| 1140 | if (!linemap_macro_expansion_map_p (map0)) |
| 1141 | return loc; |
| 1142 | |
| 1143 | resolved_loc = linemap_resolve_location (set, loc, |
| 1144 | LRK_SPELLING_LOCATION, |
| 1145 | &map1); |
| 1146 | |
| 1147 | if (resolved_loc >= RESERVED_LOCATION_COUNT |
| 1148 | && !LINEMAP_SYSP (map1)) |
| 1149 | return loc; |
| 1150 | |
| 1151 | while (linemap_macro_expansion_map_p (map0) |
| 1152 | && (resolved_loc < RESERVED_LOCATION_COUNT |
| 1153 | || LINEMAP_SYSP (map1))) |
| 1154 | { |
| 1155 | loc = linemap_unwind_toward_expansion (set, loc, &map0); |
| 1156 | resolved_loc = linemap_resolve_location (set, loc, |
| 1157 | LRK_SPELLING_LOCATION, |
| 1158 | &map1); |
| 1159 | } |
| 1160 | |
| 1161 | if (map != NULL) |
| 1162 | *map = map0; |
| 1163 | return loc; |
| 1164 | } |
| 1165 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1166 | /* Expand source code location LOC and return a user readable source |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1167 | code location. LOC must be a spelling (non-virtual) location. If |
| 1168 | it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source |
| 1169 | location is returned. */ |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1170 | |
| 1171 | expanded_location |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1172 | linemap_expand_location (struct line_maps *set, |
| 1173 | const struct line_map *map, |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1174 | source_location loc) |
| 1175 | |
| 1176 | { |
| 1177 | expanded_location xloc; |
| 1178 | |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1179 | memset (&xloc, 0, sizeof (xloc)); |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1180 | |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1181 | if (loc < RESERVED_LOCATION_COUNT) |
| 1182 | /* The location for this token wasn't generated from a line map. |
| 1183 | It was probably a location for a builtin token, chosen by some |
| 1184 | client code. Let's not try to expand the location in that |
| 1185 | case. */; |
| 1186 | else if (map == NULL) |
| 1187 | /* We shouldn't be getting a NULL map with a location that is not |
| 1188 | reserved by the client code. */ |
| 1189 | abort (); |
| 1190 | else |
| 1191 | { |
| 1192 | /* MAP must be an ordinary map and LOC must be non-virtual, |
| 1193 | encoded into this map, obviously; the accessors used on MAP |
| 1194 | below ensure it is ordinary. Let's just assert the |
| 1195 | non-virtualness of LOC here. */ |
| 1196 | if (linemap_location_from_macro_expansion_p (set, loc)) |
| 1197 | abort (); |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1198 | |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1199 | xloc.file = LINEMAP_FILE (map); |
| 1200 | xloc.line = SOURCE_LINE (map, loc); |
| 1201 | xloc.column = SOURCE_COLUMN (map, loc); |
| 1202 | xloc.sysp = LINEMAP_SYSP (map) != 0; |
| 1203 | } |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1204 | |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 1205 | return xloc; |
Neil Booth | 5993019 | 2001-08-21 21:17:48 +0000 | [diff] [blame] | 1206 | } |
Tom Tromey | 847e697 | 2011-10-17 09:59:40 +0000 | [diff] [blame] | 1207 | |
Diego Novillo | 8dcf72a | 2011-11-22 11:13:00 -0500 | [diff] [blame] | 1208 | |
| 1209 | /* Dump line map at index IX in line table SET to STREAM. If STREAM |
| 1210 | is NULL, use stderr. IS_MACRO is true if the caller wants to |
| 1211 | dump a macro map, false otherwise. */ |
| 1212 | |
| 1213 | void |
| 1214 | linemap_dump (FILE *stream, struct line_maps *set, unsigned ix, bool is_macro) |
| 1215 | { |
| 1216 | const char *lc_reasons_v[LC_ENTER_MACRO + 1] |
| 1217 | = { "LC_ENTER", "LC_LEAVE", "LC_RENAME", "LC_RENAME_VERBATIM", |
| 1218 | "LC_ENTER_MACRO" }; |
| 1219 | const char *reason; |
| 1220 | struct line_map *map; |
| 1221 | |
| 1222 | if (stream == NULL) |
| 1223 | stream = stderr; |
| 1224 | |
| 1225 | if (!is_macro) |
| 1226 | map = LINEMAPS_ORDINARY_MAP_AT (set, ix); |
| 1227 | else |
| 1228 | map = LINEMAPS_MACRO_MAP_AT (set, ix); |
| 1229 | |
| 1230 | reason = (map->reason <= LC_ENTER_MACRO) ? lc_reasons_v[map->reason] : "???"; |
| 1231 | |
| 1232 | fprintf (stream, "Map #%u [%p] - LOC: %u - REASON: %s - SYSP: %s\n", |
| 1233 | ix, (void *) map, map->start_location, reason, |
| 1234 | (!is_macro && ORDINARY_MAP_IN_SYSTEM_HEADER_P (map)) ? "yes" : "no"); |
| 1235 | if (!is_macro) |
| 1236 | { |
| 1237 | unsigned includer_ix; |
| 1238 | struct line_map *includer_map; |
| 1239 | |
| 1240 | includer_ix = ORDINARY_MAP_INCLUDER_FILE_INDEX (map); |
| 1241 | includer_map = includer_ix < LINEMAPS_ORDINARY_USED (set) |
| 1242 | ? LINEMAPS_ORDINARY_MAP_AT (set, includer_ix) |
| 1243 | : NULL; |
| 1244 | |
| 1245 | fprintf (stream, "File: %s:%d\n", ORDINARY_MAP_FILE_NAME (map), |
| 1246 | ORDINARY_MAP_STARTING_LINE_NUMBER (map)); |
| 1247 | fprintf (stream, "Included from: [%d] %s\n", includer_ix, |
| 1248 | includer_map ? ORDINARY_MAP_FILE_NAME (includer_map) : "None"); |
| 1249 | } |
| 1250 | else |
| 1251 | fprintf (stream, "Macro: %s (%u tokens)\n", |
| 1252 | linemap_map_get_macro_name (map), |
| 1253 | MACRO_MAP_NUM_MACRO_TOKENS (map)); |
| 1254 | |
| 1255 | fprintf (stream, "\n"); |
| 1256 | } |
| 1257 | |
| 1258 | |
Tom Tromey | 847e697 | 2011-10-17 09:59:40 +0000 | [diff] [blame] | 1259 | /* Dump debugging information about source location LOC into the file |
| 1260 | stream STREAM. SET is the line map set LOC comes from. */ |
| 1261 | |
| 1262 | void |
| 1263 | linemap_dump_location (struct line_maps *set, |
| 1264 | source_location loc, |
| 1265 | FILE *stream) |
| 1266 | { |
| 1267 | const struct line_map *map; |
| 1268 | source_location location; |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1269 | const char *path = "", *from = ""; |
| 1270 | int l = -1, c = -1, s = -1, e = -1; |
Tom Tromey | 847e697 | 2011-10-17 09:59:40 +0000 | [diff] [blame] | 1271 | |
| 1272 | if (loc == 0) |
| 1273 | return; |
| 1274 | |
| 1275 | location = |
| 1276 | linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map); |
Tom Tromey | 847e697 | 2011-10-17 09:59:40 +0000 | [diff] [blame] | 1277 | |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1278 | if (map == NULL) |
| 1279 | /* Only reserved locations can be tolerated in this case. */ |
| 1280 | linemap_assert (location < RESERVED_LOCATION_COUNT); |
Tom Tromey | 847e697 | 2011-10-17 09:59:40 +0000 | [diff] [blame] | 1281 | else |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1282 | { |
| 1283 | path = LINEMAP_FILE (map); |
| 1284 | l = SOURCE_LINE (map, location); |
| 1285 | c = SOURCE_COLUMN (map, location); |
| 1286 | s = LINEMAP_SYSP (map) != 0; |
| 1287 | e = location != loc; |
| 1288 | if (e) |
| 1289 | from = "N/A"; |
| 1290 | else |
| 1291 | from = (INCLUDED_FROM (set, map)) |
| 1292 | ? LINEMAP_FILE (INCLUDED_FROM (set, map)) |
| 1293 | : "<NULL>"; |
| 1294 | } |
Tom Tromey | 847e697 | 2011-10-17 09:59:40 +0000 | [diff] [blame] | 1295 | |
| 1296 | /* P: path, L: line, C: column, S: in-system-header, M: map address, |
Dodji Seketeli | 84756fd4 | 2011-10-25 08:58:54 +0000 | [diff] [blame] | 1297 | E: macro expansion?, LOC: original location, R: resolved location */ |
| 1298 | fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d,R:%d}", |
| 1299 | path, from, l, c, s, (void*)map, e, loc, location); |
Tom Tromey | 847e697 | 2011-10-17 09:59:40 +0000 | [diff] [blame] | 1300 | } |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame] | 1301 | |
| 1302 | /* Compute and return statistics about the memory consumption of some |
| 1303 | parts of the line table SET. */ |
| 1304 | |
| 1305 | void |
| 1306 | linemap_get_statistics (struct line_maps *set, |
| 1307 | struct linemap_stats *s) |
| 1308 | { |
Dodji Seketeli | d17687f | 2011-10-18 08:44:49 +0000 | [diff] [blame] | 1309 | long ordinary_maps_allocated_size, ordinary_maps_used_size, |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame] | 1310 | macro_maps_allocated_size, macro_maps_used_size, |
| 1311 | macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0; |
| 1312 | |
| 1313 | struct line_map *cur_map; |
| 1314 | |
| 1315 | ordinary_maps_allocated_size = |
| 1316 | LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map); |
| 1317 | |
| 1318 | ordinary_maps_used_size = |
| 1319 | LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map); |
| 1320 | |
| 1321 | macro_maps_allocated_size = |
| 1322 | LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map); |
| 1323 | |
| 1324 | for (cur_map = LINEMAPS_MACRO_MAPS (set); |
| 1325 | cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set); |
| 1326 | ++cur_map) |
| 1327 | { |
| 1328 | unsigned i; |
| 1329 | |
| 1330 | linemap_assert (linemap_macro_expansion_map_p (cur_map)); |
| 1331 | |
| 1332 | macro_maps_locations_size += |
| 1333 | 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location); |
| 1334 | |
| 1335 | for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2) |
| 1336 | { |
| 1337 | if (MACRO_MAP_LOCATIONS (cur_map)[i] == |
| 1338 | MACRO_MAP_LOCATIONS (cur_map)[i + 1]) |
| 1339 | duplicated_macro_maps_locations_size += |
| 1340 | sizeof (source_location); |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | macro_maps_used_size = |
| 1345 | LINEMAPS_MACRO_USED (set) * sizeof (struct line_map); |
| 1346 | |
| 1347 | s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set); |
| 1348 | s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set); |
| 1349 | s->ordinary_maps_allocated_size = ordinary_maps_allocated_size; |
| 1350 | s->ordinary_maps_used_size = ordinary_maps_used_size; |
| 1351 | s->num_expanded_macros = num_expanded_macros_counter; |
| 1352 | s->num_macro_tokens = num_macro_tokens_counter; |
| 1353 | s->num_macro_maps_used = LINEMAPS_MACRO_USED (set); |
| 1354 | s->macro_maps_allocated_size = macro_maps_allocated_size; |
| 1355 | s->macro_maps_locations_size = macro_maps_locations_size; |
| 1356 | s->macro_maps_used_size = macro_maps_used_size; |
| 1357 | s->duplicated_macro_maps_locations_size = |
| 1358 | duplicated_macro_maps_locations_size; |
| 1359 | } |
Diego Novillo | 8dcf72a | 2011-11-22 11:13:00 -0500 | [diff] [blame] | 1360 | |
| 1361 | |
| 1362 | /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used. |
| 1363 | NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO |
| 1364 | specifies how many macro maps to dump. */ |
| 1365 | |
| 1366 | void |
| 1367 | line_table_dump (FILE *stream, struct line_maps *set, unsigned int num_ordinary, |
| 1368 | unsigned int num_macro) |
| 1369 | { |
| 1370 | unsigned int i; |
| 1371 | |
| 1372 | if (set == NULL) |
| 1373 | return; |
| 1374 | |
| 1375 | if (stream == NULL) |
| 1376 | stream = stderr; |
| 1377 | |
| 1378 | fprintf (stream, "# of ordinary maps: %d\n", LINEMAPS_ORDINARY_USED (set)); |
| 1379 | fprintf (stream, "# of macro maps: %d\n", LINEMAPS_MACRO_USED (set)); |
| 1380 | fprintf (stream, "Include stack depth: %d\n", set->depth); |
| 1381 | fprintf (stream, "Highest location: %u\n", set->highest_location); |
| 1382 | |
| 1383 | if (num_ordinary) |
| 1384 | { |
| 1385 | fprintf (stream, "\nOrdinary line maps\n"); |
| 1386 | for (i = 0; i < num_ordinary && i < LINEMAPS_ORDINARY_USED (set); i++) |
| 1387 | linemap_dump (stream, set, i, false); |
| 1388 | fprintf (stream, "\n"); |
| 1389 | } |
| 1390 | |
| 1391 | if (num_macro) |
| 1392 | { |
| 1393 | fprintf (stream, "\nMacro line maps\n"); |
| 1394 | for (i = 0; i < num_macro && i < LINEMAPS_MACRO_USED (set); i++) |
| 1395 | linemap_dump (stream, set, i, true); |
| 1396 | fprintf (stream, "\n"); |
| 1397 | } |
| 1398 | } |