line-map.c 53 KB

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