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