input.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /* Data and functions related to line maps and input files.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "intl.h"
  19. #include "input.h"
  20. #include "vec.h"
  21. /* This is a cache used by get_next_line to store the content of a
  22. file to be searched for file lines. */
  23. struct fcache
  24. {
  25. /* These are information used to store a line boundary. */
  26. struct line_info
  27. {
  28. /* The line number. It starts from 1. */
  29. size_t line_num;
  30. /* The position (byte count) of the beginning of the line,
  31. relative to the file data pointer. This starts at zero. */
  32. size_t start_pos;
  33. /* The position (byte count) of the last byte of the line. This
  34. normally points to the '\n' character, or to one byte after the
  35. last byte of the file, if the file doesn't contain a '\n'
  36. character. */
  37. size_t end_pos;
  38. line_info (size_t l, size_t s, size_t e)
  39. : line_num (l), start_pos (s), end_pos (e)
  40. {}
  41. line_info ()
  42. :line_num (0), start_pos (0), end_pos (0)
  43. {}
  44. };
  45. /* The number of time this file has been accessed. This is used
  46. to designate which file cache to evict from the cache
  47. array. */
  48. unsigned use_count;
  49. const char *file_path;
  50. FILE *fp;
  51. /* This points to the content of the file that we've read so
  52. far. */
  53. char *data;
  54. /* The size of the DATA array above.*/
  55. size_t size;
  56. /* The number of bytes read from the underlying file so far. This
  57. must be less (or equal) than SIZE above. */
  58. size_t nb_read;
  59. /* The index of the beginning of the current line. */
  60. size_t line_start_idx;
  61. /* The number of the previous line read. This starts at 1. Zero
  62. means we've read no line so far. */
  63. size_t line_num;
  64. /* This is the total number of lines of the current file. At the
  65. moment, we try to get this information from the line map
  66. subsystem. Note that this is just a hint. When using the C++
  67. front-end, this hint is correct because the input file is then
  68. completely tokenized before parsing starts; so the line map knows
  69. the number of lines before compilation really starts. For e.g,
  70. the C front-end, it can happen that we start emitting diagnostics
  71. before the line map has seen the end of the file. */
  72. size_t total_lines;
  73. /* This is a record of the beginning and end of the lines we've seen
  74. while reading the file. This is useful to avoid walking the data
  75. from the beginning when we are asked to read a line that is
  76. before LINE_START_IDX above. Note that the maximum size of this
  77. record is fcache_line_record_size, so that the memory consumption
  78. doesn't explode. We thus scale total_lines down to
  79. fcache_line_record_size. */
  80. vec<line_info, va_heap> line_record;
  81. fcache ();
  82. ~fcache ();
  83. };
  84. /* Current position in real source file. */
  85. location_t input_location = UNKNOWN_LOCATION;
  86. struct line_maps *line_table;
  87. static fcache *fcache_tab;
  88. static const size_t fcache_tab_size = 16;
  89. static const size_t fcache_buffer_size = 4 * 1024;
  90. static const size_t fcache_line_record_size = 100;
  91. /* Expand the source location LOC into a human readable location. If
  92. LOC resolves to a builtin location, the file name of the readable
  93. location is set to the string "<built-in>". If EXPANSION_POINT_P is
  94. TRUE and LOC is virtual, then it is resolved to the expansion
  95. point of the involved macro. Otherwise, it is resolved to the
  96. spelling location of the token.
  97. When resolving to the spelling location of the token, if the
  98. resulting location is for a built-in location (that is, it has no
  99. associated line/column) in the context of a macro expansion, the
  100. returned location is the first one (while unwinding the macro
  101. location towards its expansion point) that is in real source
  102. code. */
  103. static expanded_location
  104. expand_location_1 (source_location loc,
  105. bool expansion_point_p)
  106. {
  107. expanded_location xloc;
  108. const struct line_map *map;
  109. enum location_resolution_kind lrk = LRK_MACRO_EXPANSION_POINT;
  110. tree block = NULL;
  111. if (IS_ADHOC_LOC (loc))
  112. {
  113. block = LOCATION_BLOCK (loc);
  114. loc = LOCATION_LOCUS (loc);
  115. }
  116. memset (&xloc, 0, sizeof (xloc));
  117. if (loc >= RESERVED_LOCATION_COUNT)
  118. {
  119. if (!expansion_point_p)
  120. {
  121. /* We want to resolve LOC to its spelling location.
  122. But if that spelling location is a reserved location that
  123. appears in the context of a macro expansion (like for a
  124. location for a built-in token), let's consider the first
  125. location (toward the expansion point) that is not reserved;
  126. that is, the first location that is in real source code. */
  127. loc = linemap_unwind_to_first_non_reserved_loc (line_table,
  128. loc, &map);
  129. lrk = LRK_SPELLING_LOCATION;
  130. }
  131. loc = linemap_resolve_location (line_table, loc,
  132. lrk, &map);
  133. xloc = linemap_expand_location (line_table, map, loc);
  134. }
  135. xloc.data = block;
  136. if (loc <= BUILTINS_LOCATION)
  137. xloc.file = loc == UNKNOWN_LOCATION ? NULL : _("<built-in>");
  138. return xloc;
  139. }
  140. /* Initialize the set of cache used for files accessed by caret
  141. diagnostic. */
  142. static void
  143. diagnostic_file_cache_init (void)
  144. {
  145. if (fcache_tab == NULL)
  146. fcache_tab = new fcache[fcache_tab_size];
  147. }
  148. /* Free the resources used by the set of cache used for files accessed
  149. by caret diagnostic. */
  150. void
  151. diagnostic_file_cache_fini (void)
  152. {
  153. if (fcache_tab)
  154. {
  155. delete [] (fcache_tab);
  156. fcache_tab = NULL;
  157. }
  158. }
  159. /* Return the total lines number that have been read so far by the
  160. line map (in the preprocessor) so far. For languages like C++ that
  161. entirely preprocess the input file before starting to parse, this
  162. equals the actual number of lines of the file. */
  163. static size_t
  164. total_lines_num (const char *file_path)
  165. {
  166. size_t r = 0;
  167. source_location l = 0;
  168. if (linemap_get_file_highest_location (line_table, file_path, &l))
  169. {
  170. gcc_assert (l >= RESERVED_LOCATION_COUNT);
  171. expanded_location xloc = expand_location (l);
  172. r = xloc.line;
  173. }
  174. return r;
  175. }
  176. /* Lookup the cache used for the content of a given file accessed by
  177. caret diagnostic. Return the found cached file, or NULL if no
  178. cached file was found. */
  179. static fcache*
  180. lookup_file_in_cache_tab (const char *file_path)
  181. {
  182. if (file_path == NULL)
  183. return NULL;
  184. diagnostic_file_cache_init ();
  185. /* This will contain the found cached file. */
  186. fcache *r = NULL;
  187. for (unsigned i = 0; i < fcache_tab_size; ++i)
  188. {
  189. fcache *c = &fcache_tab[i];
  190. if (c->file_path && !strcmp (c->file_path, file_path))
  191. {
  192. ++c->use_count;
  193. r = c;
  194. }
  195. }
  196. if (r)
  197. ++r->use_count;
  198. return r;
  199. }
  200. /* Return the file cache that has been less used, recently, or the
  201. first empty one. If HIGHEST_USE_COUNT is non-null,
  202. *HIGHEST_USE_COUNT is set to the highest use count of the entries
  203. in the cache table. */
  204. static fcache*
  205. evicted_cache_tab_entry (unsigned *highest_use_count)
  206. {
  207. diagnostic_file_cache_init ();
  208. fcache *to_evict = &fcache_tab[0];
  209. unsigned huc = to_evict->use_count;
  210. for (unsigned i = 1; i < fcache_tab_size; ++i)
  211. {
  212. fcache *c = &fcache_tab[i];
  213. bool c_is_empty = (c->file_path == NULL);
  214. if (c->use_count < to_evict->use_count
  215. || (to_evict->file_path && c_is_empty))
  216. /* We evict C because it's either an entry with a lower use
  217. count or one that is empty. */
  218. to_evict = c;
  219. if (huc < c->use_count)
  220. huc = c->use_count;
  221. if (c_is_empty)
  222. /* We've reached the end of the cache; subsequent elements are
  223. all empty. */
  224. break;
  225. }
  226. if (highest_use_count)
  227. *highest_use_count = huc;
  228. return to_evict;
  229. }
  230. /* Create the cache used for the content of a given file to be
  231. accessed by caret diagnostic. This cache is added to an array of
  232. cache and can be retrieved by lookup_file_in_cache_tab. This
  233. function returns the created cache. Note that only the last
  234. fcache_tab_size files are cached. */
  235. static fcache*
  236. add_file_to_cache_tab (const char *file_path)
  237. {
  238. FILE *fp = fopen (file_path, "r");
  239. if (fp == NULL)
  240. return NULL;
  241. unsigned highest_use_count = 0;
  242. fcache *r = evicted_cache_tab_entry (&highest_use_count);
  243. r->file_path = file_path;
  244. if (r->fp)
  245. fclose (r->fp);
  246. r->fp = fp;
  247. r->nb_read = 0;
  248. r->line_start_idx = 0;
  249. r->line_num = 0;
  250. r->line_record.truncate (0);
  251. /* Ensure that this cache entry doesn't get evicted next time
  252. add_file_to_cache_tab is called. */
  253. r->use_count = ++highest_use_count;
  254. r->total_lines = total_lines_num (file_path);
  255. return r;
  256. }
  257. /* Lookup the cache used for the content of a given file accessed by
  258. caret diagnostic. If no cached file was found, create a new cache
  259. for this file, add it to the array of cached file and return
  260. it. */
  261. static fcache*
  262. lookup_or_add_file_to_cache_tab (const char *file_path)
  263. {
  264. fcache *r = lookup_file_in_cache_tab (file_path);
  265. if (r == NULL)
  266. r = add_file_to_cache_tab (file_path);
  267. return r;
  268. }
  269. /* Default constructor for a cache of file used by caret
  270. diagnostic. */
  271. fcache::fcache ()
  272. : use_count (0), file_path (NULL), fp (NULL), data (0),
  273. size (0), nb_read (0), line_start_idx (0), line_num (0),
  274. total_lines (0)
  275. {
  276. line_record.create (0);
  277. }
  278. /* Destructor for a cache of file used by caret diagnostic. */
  279. fcache::~fcache ()
  280. {
  281. if (fp)
  282. {
  283. fclose (fp);
  284. fp = NULL;
  285. }
  286. if (data)
  287. {
  288. XDELETEVEC (data);
  289. data = 0;
  290. }
  291. line_record.release ();
  292. }
  293. /* Returns TRUE iff the cache would need to be filled with data coming
  294. from the file. That is, either the cache is empty or full or the
  295. current line is empty. Note that if the cache is full, it would
  296. need to be extended and filled again. */
  297. static bool
  298. needs_read (fcache *c)
  299. {
  300. return (c->nb_read == 0
  301. || c->nb_read == c->size
  302. || (c->line_start_idx >= c->nb_read - 1));
  303. }
  304. /* Return TRUE iff the cache is full and thus needs to be
  305. extended. */
  306. static bool
  307. needs_grow (fcache *c)
  308. {
  309. return c->nb_read == c->size;
  310. }
  311. /* Grow the cache if it needs to be extended. */
  312. static void
  313. maybe_grow (fcache *c)
  314. {
  315. if (!needs_grow (c))
  316. return;
  317. size_t size = c->size == 0 ? fcache_buffer_size : c->size * 2;
  318. c->data = XRESIZEVEC (char, c->data, size + 1);
  319. c->size = size;
  320. }
  321. /* Read more data into the cache. Extends the cache if need be.
  322. Returns TRUE iff new data could be read. */
  323. static bool
  324. read_data (fcache *c)
  325. {
  326. if (feof (c->fp) || ferror (c->fp))
  327. return false;
  328. maybe_grow (c);
  329. char * from = c->data + c->nb_read;
  330. size_t to_read = c->size - c->nb_read;
  331. size_t nb_read = fread (from, 1, to_read, c->fp);
  332. if (ferror (c->fp))
  333. return false;
  334. c->nb_read += nb_read;
  335. return !!nb_read;
  336. }
  337. /* Read new data iff the cache needs to be filled with more data
  338. coming from the file FP. Return TRUE iff the cache was filled with
  339. mode data. */
  340. static bool
  341. maybe_read_data (fcache *c)
  342. {
  343. if (!needs_read (c))
  344. return false;
  345. return read_data (c);
  346. }
  347. /* Read a new line from file FP, using C as a cache for the data
  348. coming from the file. Upon successful completion, *LINE is set to
  349. the beginning of the line found. Space for that line has been
  350. allocated in the cache thus *LINE has the same life time as C.
  351. *LINE_LEN is set to the length of the line. Note that the line
  352. does not contain any terminal delimiter. This function returns
  353. true if some data was read or process from the cache, false
  354. otherwise. Note that subsequent calls to get_next_line return the
  355. next lines of the file and might overwrite the content of
  356. *LINE. */
  357. static bool
  358. get_next_line (fcache *c, char **line, ssize_t *line_len)
  359. {
  360. /* Fill the cache with data to process. */
  361. maybe_read_data (c);
  362. size_t remaining_size = c->nb_read - c->line_start_idx;
  363. if (remaining_size == 0)
  364. /* There is no more data to process. */
  365. return false;
  366. char *line_start = c->data + c->line_start_idx;
  367. char *next_line_start = NULL;
  368. size_t len = 0;
  369. char *line_end = (char *) memchr (line_start, '\n', remaining_size);
  370. if (line_end == NULL)
  371. {
  372. /* We haven't found the end-of-line delimiter in the cache.
  373. Fill the cache with more data from the file and look for the
  374. '\n'. */
  375. while (maybe_read_data (c))
  376. {
  377. line_start = c->data + c->line_start_idx;
  378. remaining_size = c->nb_read - c->line_start_idx;
  379. line_end = (char *) memchr (line_start, '\n', remaining_size);
  380. if (line_end != NULL)
  381. {
  382. next_line_start = line_end + 1;
  383. break;
  384. }
  385. }
  386. if (line_end == NULL)
  387. /* We've loadded all the file into the cache and still no
  388. '\n'. Let's say the line ends up at one byte passed the
  389. end of the file. This is to stay consistent with the case
  390. of when the line ends up with a '\n' and line_end points to
  391. that terminal '\n'. That consistency is useful below in
  392. the len calculation. */
  393. line_end = c->data + c->nb_read ;
  394. }
  395. else
  396. next_line_start = line_end + 1;
  397. if (ferror (c->fp))
  398. return -1;
  399. /* At this point, we've found the end of the of line. It either
  400. points to the '\n' or to one byte after the last byte of the
  401. file. */
  402. gcc_assert (line_end != NULL);
  403. len = line_end - line_start;
  404. if (c->line_start_idx < c->nb_read)
  405. *line = line_start;
  406. ++c->line_num;
  407. /* Before we update our line record, make sure the hint about the
  408. total number of lines of the file is correct. If it's not, then
  409. we give up recording line boundaries from now on. */
  410. bool update_line_record = true;
  411. if (c->line_num > c->total_lines)
  412. update_line_record = false;
  413. /* Now update our line record so that re-reading lines from the
  414. before c->line_start_idx is faster. */
  415. if (update_line_record
  416. && c->line_record.length () < fcache_line_record_size)
  417. {
  418. /* If the file lines fits in the line record, we just record all
  419. its lines ...*/
  420. if (c->total_lines <= fcache_line_record_size
  421. && c->line_num > c->line_record.length ())
  422. c->line_record.safe_push (fcache::line_info (c->line_num,
  423. c->line_start_idx,
  424. line_end - c->data));
  425. else if (c->total_lines > fcache_line_record_size)
  426. {
  427. /* ... otherwise, we just scale total_lines down to
  428. (fcache_line_record_size lines. */
  429. size_t n = (c->line_num * fcache_line_record_size) / c->total_lines;
  430. if (c->line_record.length () == 0
  431. || n >= c->line_record.length ())
  432. c->line_record.safe_push (fcache::line_info (c->line_num,
  433. c->line_start_idx,
  434. line_end - c->data));
  435. }
  436. }
  437. /* Update c->line_start_idx so that it points to the next line to be
  438. read. */
  439. if (next_line_start)
  440. c->line_start_idx = next_line_start - c->data;
  441. else
  442. /* We didn't find any terminal '\n'. Let's consider that the end
  443. of line is the end of the data in the cache. The next
  444. invocation of get_next_line will either read more data from the
  445. underlying file or return false early because we've reached the
  446. end of the file. */
  447. c->line_start_idx = c->nb_read;
  448. *line_len = len;
  449. return true;
  450. }
  451. /* Reads the next line from FILE into *LINE. If *LINE is too small
  452. (or NULL) it is allocated (or extended) to have enough space to
  453. containe the line. *LINE_LENGTH must contain the size of the
  454. initial*LINE buffer. It's then updated by this function to the
  455. actual length of the returned line. Note that the returned line
  456. can contain several zero bytes. Also note that the returned string
  457. is allocated in static storage that is going to be re-used by
  458. subsequent invocations of read_line. */
  459. static bool
  460. read_next_line (fcache *cache, char ** line, ssize_t *line_len)
  461. {
  462. char *l = NULL;
  463. ssize_t len = 0;
  464. if (!get_next_line (cache, &l, &len))
  465. return false;
  466. if (*line == NULL)
  467. *line = XNEWVEC (char, len);
  468. else
  469. if (*line_len < len)
  470. *line = XRESIZEVEC (char, *line, len);
  471. memcpy (*line, l, len);
  472. *line_len = len;
  473. return true;
  474. }
  475. /* Consume the next bytes coming from the cache (or from its
  476. underlying file if there are remaining unread bytes in the file)
  477. until we reach the next end-of-line (or end-of-file). There is no
  478. copying from the cache involved. Return TRUE upon successful
  479. completion. */
  480. static bool
  481. goto_next_line (fcache *cache)
  482. {
  483. char *l;
  484. ssize_t len;
  485. return get_next_line (cache, &l, &len);
  486. }
  487. /* Read an arbitrary line number LINE_NUM from the file cached in C.
  488. The line is copied into *LINE. *LINE_LEN must have been set to the
  489. length of *LINE. If *LINE is too small (or NULL) it's extended (or
  490. allocated) and *LINE_LEN is adjusted accordingly. *LINE ends up
  491. with a terminal zero byte and can contain additional zero bytes.
  492. This function returns bool if a line was read. */
  493. static bool
  494. read_line_num (fcache *c, size_t line_num,
  495. char ** line, ssize_t *line_len)
  496. {
  497. gcc_assert (line_num > 0);
  498. if (line_num <= c->line_num)
  499. {
  500. /* We've been asked to read lines that are before c->line_num.
  501. So lets use our line record (if it's not empty) to try to
  502. avoid re-reading the file from the beginning again. */
  503. if (c->line_record.is_empty ())
  504. {
  505. c->line_start_idx = 0;
  506. c->line_num = 0;
  507. }
  508. else
  509. {
  510. fcache::line_info *i = NULL;
  511. if (c->total_lines <= fcache_line_record_size)
  512. {
  513. /* In languages where the input file is not totally
  514. preprocessed up front, the c->total_lines hint
  515. can be smaller than the number of lines of the
  516. file. In that case, only the first
  517. c->total_lines have been recorded.
  518. Otherwise, the first c->total_lines we've read have
  519. their start/end recorded here. */
  520. i = (line_num <= c->total_lines)
  521. ? &c->line_record[line_num - 1]
  522. : &c->line_record[c->total_lines - 1];
  523. gcc_assert (i->line_num <= line_num);
  524. }
  525. else
  526. {
  527. /* So the file had more lines than our line record
  528. size. Thus the number of lines we've recorded has
  529. been scaled down to fcache_line_reacord_size. Let's
  530. pick the start/end of the recorded line that is
  531. closest to line_num. */
  532. size_t n = (line_num <= c->total_lines)
  533. ? line_num * fcache_line_record_size / c->total_lines
  534. : c ->line_record.length () - 1;
  535. if (n < c->line_record.length ())
  536. {
  537. i = &c->line_record[n];
  538. gcc_assert (i->line_num <= line_num);
  539. }
  540. }
  541. if (i && i->line_num == line_num)
  542. {
  543. /* We have the start/end of the line. Let's just copy
  544. it again and we are done. */
  545. ssize_t len = i->end_pos - i->start_pos + 1;
  546. if (*line_len < len)
  547. *line = XRESIZEVEC (char, *line, len);
  548. memmove (*line, c->data + i->start_pos, len);
  549. (*line)[len - 1] = '\0';
  550. *line_len = --len;
  551. return true;
  552. }
  553. if (i)
  554. {
  555. c->line_start_idx = i->start_pos;
  556. c->line_num = i->line_num - 1;
  557. }
  558. else
  559. {
  560. c->line_start_idx = 0;
  561. c->line_num = 0;
  562. }
  563. }
  564. }
  565. /* Let's walk from line c->line_num up to line_num - 1, without
  566. copying any line. */
  567. while (c->line_num < line_num - 1)
  568. if (!goto_next_line (c))
  569. return false;
  570. /* The line we want is the next one. Let's read and copy it back to
  571. the caller. */
  572. return read_next_line (c, line, line_len);
  573. }
  574. /* Return the physical source line that corresponds to xloc in a
  575. buffer that is statically allocated. The newline is replaced by
  576. the null character. Note that the line can contain several null
  577. characters, so LINE_LEN, if non-null, points to the actual length
  578. of the line. */
  579. const char *
  580. location_get_source_line (expanded_location xloc,
  581. int *line_len)
  582. {
  583. static char *buffer;
  584. static ssize_t len;
  585. if (xloc.line == 0)
  586. return NULL;
  587. fcache *c = lookup_or_add_file_to_cache_tab (xloc.file);
  588. if (c == NULL)
  589. return NULL;
  590. bool read = read_line_num (c, xloc.line, &buffer, &len);
  591. if (read && line_len)
  592. *line_len = len;
  593. return read ? buffer : NULL;
  594. }
  595. /* Test if the location originates from the spelling location of a
  596. builtin-tokens. That is, return TRUE if LOC is a (possibly
  597. virtual) location of a built-in token that appears in the expansion
  598. list of a macro. Please note that this function also works on
  599. tokens that result from built-in tokens. For instance, the
  600. function would return true if passed a token "4" that is the result
  601. of the expansion of the built-in __LINE__ macro. */
  602. bool
  603. is_location_from_builtin_token (source_location loc)
  604. {
  605. const line_map *map = NULL;
  606. loc = linemap_resolve_location (line_table, loc,
  607. LRK_SPELLING_LOCATION, &map);
  608. return loc == BUILTINS_LOCATION;
  609. }
  610. /* Expand the source location LOC into a human readable location. If
  611. LOC is virtual, it resolves to the expansion point of the involved
  612. macro. If LOC resolves to a builtin location, the file name of the
  613. readable location is set to the string "<built-in>". */
  614. expanded_location
  615. expand_location (source_location loc)
  616. {
  617. return expand_location_1 (loc, /*expansion_point_p=*/true);
  618. }
  619. /* Expand the source location LOC into a human readable location. If
  620. LOC is virtual, it resolves to the expansion location of the
  621. relevant macro. If LOC resolves to a builtin location, the file
  622. name of the readable location is set to the string
  623. "<built-in>". */
  624. expanded_location
  625. expand_location_to_spelling_point (source_location loc)
  626. {
  627. return expand_location_1 (loc, /*expansion_point_p=*/false);
  628. }
  629. /* If LOCATION is in a system header and if it is a virtual location for
  630. a token coming from the expansion of a macro, unwind it to the
  631. location of the expansion point of the macro. Otherwise, just return
  632. LOCATION.
  633. This is used for instance when we want to emit diagnostics about a
  634. token that may be located in a macro that is itself defined in a
  635. system header, for example, for the NULL macro. In such a case, if
  636. LOCATION were passed directly to diagnostic functions such as
  637. warning_at, the diagnostic would be suppressed (unless
  638. -Wsystem-headers). */
  639. source_location
  640. expansion_point_location_if_in_system_header (source_location location)
  641. {
  642. if (in_system_header_at (location))
  643. location = linemap_resolve_location (line_table, location,
  644. LRK_MACRO_EXPANSION_POINT,
  645. NULL);
  646. return location;
  647. }
  648. #define ONE_K 1024
  649. #define ONE_M (ONE_K * ONE_K)
  650. /* Display a number as an integer multiple of either:
  651. - 1024, if said integer is >= to 10 K (in base 2)
  652. - 1024 * 1024, if said integer is >= 10 M in (base 2)
  653. */
  654. #define SCALE(x) ((unsigned long) ((x) < 10 * ONE_K \
  655. ? (x) \
  656. : ((x) < 10 * ONE_M \
  657. ? (x) / ONE_K \
  658. : (x) / ONE_M)))
  659. /* For a given integer, display either:
  660. - the character 'k', if the number is higher than 10 K (in base 2)
  661. but strictly lower than 10 M (in base 2)
  662. - the character 'M' if the number is higher than 10 M (in base2)
  663. - the charcter ' ' if the number is strictly lower than 10 K */
  664. #define STAT_LABEL(x) ((x) < 10 * ONE_K ? ' ' : ((x) < 10 * ONE_M ? 'k' : 'M'))
  665. /* Display an integer amount as multiple of 1K or 1M (in base 2).
  666. Display the correct unit (either k, M, or ' ') after the amout, as
  667. well. */
  668. #define FORMAT_AMOUNT(size) SCALE (size), STAT_LABEL (size)
  669. /* Dump statistics to stderr about the memory usage of the line_table
  670. set of line maps. This also displays some statistics about macro
  671. expansion. */
  672. void
  673. dump_line_table_statistics (void)
  674. {
  675. struct linemap_stats s;
  676. long total_used_map_size,
  677. macro_maps_size,
  678. total_allocated_map_size;
  679. memset (&s, 0, sizeof (s));
  680. linemap_get_statistics (line_table, &s);
  681. macro_maps_size = s.macro_maps_used_size
  682. + s.macro_maps_locations_size;
  683. total_allocated_map_size = s.ordinary_maps_allocated_size
  684. + s.macro_maps_allocated_size
  685. + s.macro_maps_locations_size;
  686. total_used_map_size = s.ordinary_maps_used_size
  687. + s.macro_maps_used_size
  688. + s.macro_maps_locations_size;
  689. fprintf (stderr, "Number of expanded macros: %5ld\n",
  690. s.num_expanded_macros);
  691. if (s.num_expanded_macros != 0)
  692. fprintf (stderr, "Average number of tokens per macro expansion: %5ld\n",
  693. s.num_macro_tokens / s.num_expanded_macros);
  694. fprintf (stderr,
  695. "\nLine Table allocations during the "
  696. "compilation process\n");
  697. fprintf (stderr, "Number of ordinary maps used: %5ld%c\n",
  698. SCALE (s.num_ordinary_maps_used),
  699. STAT_LABEL (s.num_ordinary_maps_used));
  700. fprintf (stderr, "Ordinary map used size: %5ld%c\n",
  701. SCALE (s.ordinary_maps_used_size),
  702. STAT_LABEL (s.ordinary_maps_used_size));
  703. fprintf (stderr, "Number of ordinary maps allocated: %5ld%c\n",
  704. SCALE (s.num_ordinary_maps_allocated),
  705. STAT_LABEL (s.num_ordinary_maps_allocated));
  706. fprintf (stderr, "Ordinary maps allocated size: %5ld%c\n",
  707. SCALE (s.ordinary_maps_allocated_size),
  708. STAT_LABEL (s.ordinary_maps_allocated_size));
  709. fprintf (stderr, "Number of macro maps used: %5ld%c\n",
  710. SCALE (s.num_macro_maps_used),
  711. STAT_LABEL (s.num_macro_maps_used));
  712. fprintf (stderr, "Macro maps used size: %5ld%c\n",
  713. SCALE (s.macro_maps_used_size),
  714. STAT_LABEL (s.macro_maps_used_size));
  715. fprintf (stderr, "Macro maps locations size: %5ld%c\n",
  716. SCALE (s.macro_maps_locations_size),
  717. STAT_LABEL (s.macro_maps_locations_size));
  718. fprintf (stderr, "Macro maps size: %5ld%c\n",
  719. SCALE (macro_maps_size),
  720. STAT_LABEL (macro_maps_size));
  721. fprintf (stderr, "Duplicated maps locations size: %5ld%c\n",
  722. SCALE (s.duplicated_macro_maps_locations_size),
  723. STAT_LABEL (s.duplicated_macro_maps_locations_size));
  724. fprintf (stderr, "Total allocated maps size: %5ld%c\n",
  725. SCALE (total_allocated_map_size),
  726. STAT_LABEL (total_allocated_map_size));
  727. fprintf (stderr, "Total used maps size: %5ld%c\n",
  728. SCALE (total_used_map_size),
  729. STAT_LABEL (total_used_map_size));
  730. fprintf (stderr, "\n");
  731. }