pch.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /* Part of CPP library. (Precompiled header reading/writing.)
  2. Copyright (C) 2000-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. #include "config.h"
  15. #include "system.h"
  16. #include "cpplib.h"
  17. #include "internal.h"
  18. #include "hashtab.h"
  19. #include "mkdeps.h"
  20. static int write_macdef (cpp_reader *, cpp_hashnode *, void *);
  21. static int save_idents (cpp_reader *, cpp_hashnode *, void *);
  22. static hashval_t hashmem (const void *, size_t);
  23. static hashval_t cpp_string_hash (const void *);
  24. static int cpp_string_eq (const void *, const void *);
  25. static int count_defs (cpp_reader *, cpp_hashnode *, void *);
  26. static int comp_hashnodes (const void *, const void *);
  27. static int collect_ht_nodes (cpp_reader *, cpp_hashnode *, void *);
  28. static int write_defs (cpp_reader *, cpp_hashnode *, void *);
  29. static int save_macros (cpp_reader *, cpp_hashnode *, void *);
  30. static int _cpp_save_pushed_macros (cpp_reader *, FILE *);
  31. static int _cpp_restore_pushed_macros (cpp_reader *, FILE *);
  32. /* This structure represents a macro definition on disk. */
  33. struct macrodef_struct
  34. {
  35. unsigned int definition_length;
  36. unsigned short name_length;
  37. unsigned short flags;
  38. };
  39. /* This is how we write out a macro definition.
  40. Suitable for being called by cpp_forall_identifiers. */
  41. static int
  42. write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
  43. {
  44. FILE *f = (FILE *) file_p;
  45. switch (hn->type)
  46. {
  47. case NT_VOID:
  48. if (! (hn->flags & NODE_POISONED))
  49. return 1;
  50. case NT_MACRO:
  51. if ((hn->flags & NODE_BUILTIN)
  52. && (!pfile->cb.user_builtin_macro
  53. || !pfile->cb.user_builtin_macro (pfile, hn)))
  54. return 1;
  55. {
  56. struct macrodef_struct s;
  57. const unsigned char *defn;
  58. s.name_length = NODE_LEN (hn);
  59. s.flags = hn->flags & NODE_POISONED;
  60. if (hn->type == NT_MACRO)
  61. {
  62. defn = cpp_macro_definition (pfile, hn);
  63. s.definition_length = ustrlen (defn);
  64. }
  65. else
  66. {
  67. defn = NODE_NAME (hn);
  68. s.definition_length = s.name_length;
  69. }
  70. if (fwrite (&s, sizeof (s), 1, f) != 1
  71. || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
  72. {
  73. cpp_errno (pfile, CPP_DL_ERROR,
  74. "while writing precompiled header");
  75. return 0;
  76. }
  77. }
  78. return 1;
  79. case NT_ASSERTION:
  80. /* Not currently implemented. */
  81. return 1;
  82. default:
  83. abort ();
  84. }
  85. }
  86. /* This structure records the names of the defined macros.
  87. It's also used as a callback structure for size_initial_idents
  88. and save_idents. */
  89. struct cpp_savedstate
  90. {
  91. /* A hash table of the defined identifiers. */
  92. htab_t definedhash;
  93. /* The size of the definitions of those identifiers (the size of
  94. 'definedstrs'). */
  95. size_t hashsize;
  96. /* Number of definitions */
  97. size_t n_defs;
  98. /* Array of definitions. In cpp_write_pch_deps it is used for sorting. */
  99. cpp_hashnode **defs;
  100. /* Space for the next definition. Definitions are null-terminated
  101. strings. */
  102. unsigned char *definedstrs;
  103. };
  104. /* Save this identifier into the state: put it in the hash table,
  105. put the definition in 'definedstrs'. */
  106. static int
  107. save_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
  108. {
  109. struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
  110. if (hn->type != NT_VOID)
  111. {
  112. struct cpp_string news;
  113. void **slot;
  114. news.len = NODE_LEN (hn);
  115. news.text= NODE_NAME (hn);
  116. slot = htab_find_slot (ss->definedhash, &news, INSERT);
  117. if (*slot == NULL)
  118. {
  119. struct cpp_string *sp;
  120. unsigned char *text;
  121. sp = XNEW (struct cpp_string);
  122. *slot = sp;
  123. sp->len = NODE_LEN (hn);
  124. sp->text = text = XNEWVEC (unsigned char, NODE_LEN (hn));
  125. memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
  126. }
  127. }
  128. return 1;
  129. }
  130. /* Hash some memory in a generic way. */
  131. static hashval_t
  132. hashmem (const void *p_p, size_t sz)
  133. {
  134. const unsigned char *p = (const unsigned char *)p_p;
  135. size_t i;
  136. hashval_t h;
  137. h = 0;
  138. for (i = 0; i < sz; i++)
  139. h = h * 67 - (*p++ - 113);
  140. return h;
  141. }
  142. /* Hash a cpp string for the hashtable machinery. */
  143. static hashval_t
  144. cpp_string_hash (const void *a_p)
  145. {
  146. const struct cpp_string *a = (const struct cpp_string *) a_p;
  147. return hashmem (a->text, a->len);
  148. }
  149. /* Compare two cpp strings for the hashtable machinery. */
  150. static int
  151. cpp_string_eq (const void *a_p, const void *b_p)
  152. {
  153. const struct cpp_string *a = (const struct cpp_string *) a_p;
  154. const struct cpp_string *b = (const struct cpp_string *) b_p;
  155. return (a->len == b->len
  156. && memcmp (a->text, b->text, a->len) == 0);
  157. }
  158. /* Free memory associated with cpp_string. */
  159. static void
  160. cpp_string_free (void *a_p)
  161. {
  162. struct cpp_string *a = (struct cpp_string *) a_p;
  163. free ((void *) a->text);
  164. free (a);
  165. }
  166. /* Save the current definitions of the cpp_reader for dependency
  167. checking purposes. When writing a precompiled header, this should
  168. be called at the same point in the compilation as cpp_valid_state
  169. would be called when reading the precompiled header back in. */
  170. int
  171. cpp_save_state (cpp_reader *r, FILE *f)
  172. {
  173. /* Save the list of non-void identifiers for the dependency checking. */
  174. r->savedstate = XNEW (struct cpp_savedstate);
  175. r->savedstate->definedhash = htab_create (100, cpp_string_hash,
  176. cpp_string_eq, cpp_string_free);
  177. cpp_forall_identifiers (r, save_idents, r->savedstate);
  178. /* Write out the list of defined identifiers. */
  179. cpp_forall_identifiers (r, write_macdef, f);
  180. return 0;
  181. }
  182. /* Calculate the 'hashsize' field of the saved state. */
  183. static int
  184. count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
  185. {
  186. struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
  187. switch (hn->type)
  188. {
  189. case NT_MACRO:
  190. if (hn->flags & NODE_BUILTIN)
  191. return 1;
  192. /* else fall through. */
  193. case NT_VOID:
  194. {
  195. struct cpp_string news;
  196. void **slot;
  197. news.len = NODE_LEN (hn);
  198. news.text = NODE_NAME (hn);
  199. slot = (void **) htab_find (ss->definedhash, &news);
  200. if (slot == NULL)
  201. {
  202. ss->hashsize += NODE_LEN (hn) + 1;
  203. ss->n_defs += 1;
  204. }
  205. }
  206. return 1;
  207. case NT_ASSERTION:
  208. /* Not currently implemented. */
  209. return 1;
  210. default:
  211. abort ();
  212. }
  213. }
  214. /* Collect the identifiers into the state's string table. */
  215. static int
  216. write_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
  217. {
  218. struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
  219. switch (hn->type)
  220. {
  221. case NT_MACRO:
  222. if (hn->flags & NODE_BUILTIN)
  223. return 1;
  224. /* else fall through. */
  225. case NT_VOID:
  226. {
  227. struct cpp_string news;
  228. void **slot;
  229. news.len = NODE_LEN (hn);
  230. news.text = NODE_NAME (hn);
  231. slot = (void **) htab_find (ss->definedhash, &news);
  232. if (slot == NULL)
  233. {
  234. ss->defs[ss->n_defs] = hn;
  235. ss->n_defs += 1;
  236. }
  237. }
  238. return 1;
  239. case NT_ASSERTION:
  240. /* Not currently implemented. */
  241. return 1;
  242. default:
  243. abort ();
  244. }
  245. }
  246. /* Comparison function for qsort. The arguments point to pointers of
  247. type ht_hashnode *. */
  248. static int
  249. comp_hashnodes (const void *px, const void *py)
  250. {
  251. cpp_hashnode *x = *(cpp_hashnode **) px;
  252. cpp_hashnode *y = *(cpp_hashnode **) py;
  253. return ustrcmp (NODE_NAME (x), NODE_NAME (y));
  254. }
  255. /* Write out the remainder of the dependency information. This should be
  256. called after the PCH is ready to be saved. */
  257. int
  258. cpp_write_pch_deps (cpp_reader *r, FILE *f)
  259. {
  260. struct macrodef_struct z;
  261. struct cpp_savedstate *const ss = r->savedstate;
  262. unsigned char *definedstrs;
  263. size_t i;
  264. /* Collect the list of identifiers which have been seen and
  265. weren't defined to anything previously. */
  266. ss->hashsize = 0;
  267. ss->n_defs = 0;
  268. cpp_forall_identifiers (r, count_defs, ss);
  269. ss->defs = XNEWVEC (cpp_hashnode *, ss->n_defs);
  270. ss->n_defs = 0;
  271. cpp_forall_identifiers (r, write_defs, ss);
  272. /* Sort the list, copy it into a buffer, and write it out. */
  273. qsort (ss->defs, ss->n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
  274. definedstrs = ss->definedstrs = XNEWVEC (unsigned char, ss->hashsize);
  275. for (i = 0; i < ss->n_defs; ++i)
  276. {
  277. size_t len = NODE_LEN (ss->defs[i]);
  278. memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
  279. definedstrs += len + 1;
  280. }
  281. memset (&z, 0, sizeof (z));
  282. z.definition_length = ss->hashsize;
  283. if (fwrite (&z, sizeof (z), 1, f) != 1
  284. || fwrite (ss->definedstrs, ss->hashsize, 1, f) != 1)
  285. {
  286. cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
  287. return -1;
  288. }
  289. free (ss->definedstrs);
  290. free (ss->defs);
  291. htab_delete (ss->definedhash);
  292. /* Free the saved state. */
  293. free (ss);
  294. r->savedstate = NULL;
  295. /* Save the next value of __COUNTER__. */
  296. if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
  297. {
  298. cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
  299. return -1;
  300. }
  301. return 0;
  302. }
  303. /* Write out the definitions of the preprocessor, in a form suitable for
  304. cpp_read_state. */
  305. int
  306. cpp_write_pch_state (cpp_reader *r, FILE *f)
  307. {
  308. if (!r->deps)
  309. r->deps = deps_init ();
  310. if (deps_save (r->deps, f) != 0)
  311. {
  312. cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
  313. return -1;
  314. }
  315. if (! _cpp_save_file_entries (r, f))
  316. {
  317. cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
  318. return -1;
  319. }
  320. /* Save the next __COUNTER__ value. When we include a precompiled header,
  321. we need to start at the offset we would have if the header had been
  322. included normally. */
  323. if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
  324. {
  325. cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
  326. return -1;
  327. }
  328. /* Write saved macros. */
  329. if (! _cpp_save_pushed_macros (r, f))
  330. {
  331. cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
  332. return -1;
  333. }
  334. return 0;
  335. }
  336. static int
  337. _cpp_restore_pushed_macros (cpp_reader *r, FILE *f)
  338. {
  339. size_t count_saved = 0;
  340. size_t i;
  341. struct def_pragma_macro *p;
  342. size_t nlen;
  343. uchar *defn;
  344. size_t defnlen;
  345. if (fread (&count_saved, sizeof (count_saved), 1, f) != 1)
  346. return 0;
  347. if (! count_saved)
  348. return 1;
  349. for (i = 0; i < count_saved; i++)
  350. {
  351. if (fread (&nlen, sizeof (nlen), 1, f) != 1)
  352. return 0;
  353. p = XNEW (struct def_pragma_macro);
  354. memset (p, 0, sizeof (struct def_pragma_macro));
  355. p->name = XNEWVAR (char, nlen + 1);
  356. p->name[nlen] = 0;
  357. if (fread (p->name, nlen, 1, f) != 1)
  358. return 0;
  359. if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
  360. return 0;
  361. if (defnlen == 0)
  362. p->is_undef = 1;
  363. else
  364. {
  365. defn = XNEWVEC (uchar, defnlen + 1);
  366. defn[defnlen] = 0;
  367. if (fread (defn, defnlen, 1, f) != 1)
  368. return 0;
  369. p->definition = defn;
  370. if (fread (&(p->line), sizeof (source_location), 1, f) != 1)
  371. return 0;
  372. defnlen = 0;
  373. if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
  374. return 0;
  375. p->syshdr = ((defnlen & 1) != 0 ? 1 : 0);
  376. p->used = ((defnlen & 2) != 0 ? 1 : 0);
  377. }
  378. p->next = r->pushed_macros;
  379. r->pushed_macros = p;
  380. }
  381. return 1;
  382. }
  383. static int
  384. _cpp_save_pushed_macros (cpp_reader *r, FILE *f)
  385. {
  386. size_t count_saved = 0;
  387. size_t i;
  388. struct def_pragma_macro *p,**pp;
  389. size_t defnlen;
  390. /* Get count. */
  391. p = r->pushed_macros;
  392. while (p != NULL)
  393. {
  394. count_saved++;
  395. p = p->next;
  396. }
  397. if (fwrite (&count_saved, sizeof (count_saved), 1, f) != 1)
  398. return 0;
  399. if (!count_saved)
  400. return 1;
  401. pp = (struct def_pragma_macro **) alloca (sizeof (struct def_pragma_macro *)
  402. * count_saved);
  403. /* Store them in reverse order. */
  404. p = r->pushed_macros;
  405. i = count_saved;
  406. while (p != NULL)
  407. {
  408. --i;
  409. pp[i] = p;
  410. p = p->next;
  411. }
  412. for (i = 0; i < count_saved; i++)
  413. {
  414. defnlen = strlen (pp[i]->name);
  415. if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
  416. || fwrite (pp[i]->name, defnlen, 1, f) != 1)
  417. return 0;
  418. if (pp[i]->is_undef)
  419. {
  420. defnlen = 0;
  421. if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1)
  422. return 0;
  423. }
  424. else
  425. {
  426. defnlen = ustrlen (pp[i]->definition);
  427. if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
  428. || fwrite (pp[i]->definition, defnlen, 1, f) != 1)
  429. return 0;
  430. if (fwrite (&(pp[i]->line), sizeof (source_location), 1, f) != 1)
  431. return 0;
  432. defnlen = 0;
  433. defnlen |= (pp[i]->syshdr != 0 ? 1 : 0);
  434. defnlen |= (pp[i]->used != 0 ? 2 : 0);
  435. if (fwrite (&defnlen, sizeof (defnlen), 1, f) != 1)
  436. return 0;
  437. }
  438. }
  439. return 1;
  440. }
  441. /* Data structure to transform hash table nodes into a sorted list */
  442. struct ht_node_list
  443. {
  444. /* Array of nodes */
  445. cpp_hashnode **defs;
  446. /* Number of nodes in the array */
  447. size_t n_defs;
  448. /* Size of the allocated array */
  449. size_t asize;
  450. };
  451. /* Callback for collecting identifiers from hash table */
  452. static int
  453. collect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
  454. void *nl_p)
  455. {
  456. struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
  457. if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
  458. {
  459. if (nl->n_defs == nl->asize)
  460. {
  461. nl->asize *= 2;
  462. nl->defs = XRESIZEVEC (cpp_hashnode *, nl->defs, nl->asize);
  463. }
  464. nl->defs[nl->n_defs] = hn;
  465. ++nl->n_defs;
  466. }
  467. return 1;
  468. }
  469. /* Return nonzero if FD is a precompiled header which is consistent
  470. with the preprocessor's current definitions. It will be consistent
  471. when:
  472. - anything that was defined just before the PCH was generated
  473. is defined the same way now; and
  474. - anything that was not defined then, but is defined now, was not
  475. used by the PCH.
  476. NAME is used to print warnings if `warn_invalid_pch' is set in the
  477. reader's flags.
  478. */
  479. int
  480. cpp_valid_state (cpp_reader *r, const char *name, int fd)
  481. {
  482. struct macrodef_struct m;
  483. size_t namebufsz = 256;
  484. unsigned char *namebuf = XNEWVEC (unsigned char, namebufsz);
  485. unsigned char *undeftab = NULL;
  486. struct ht_node_list nl = { 0, 0, 0 };
  487. unsigned char *first, *last;
  488. unsigned int i;
  489. unsigned int counter;
  490. /* Read in the list of identifiers that must be defined
  491. Check that they are defined in the same way. */
  492. for (;;)
  493. {
  494. cpp_hashnode *h;
  495. const unsigned char *newdefn;
  496. if (read (fd, &m, sizeof (m)) != sizeof (m))
  497. goto error;
  498. if (m.name_length == 0)
  499. break;
  500. /* If this file is already preprocessed, there won't be any
  501. macros defined, and that's OK. */
  502. if (CPP_OPTION (r, preprocessed))
  503. {
  504. if (lseek (fd, m.definition_length, SEEK_CUR) == -1)
  505. goto error;
  506. continue;
  507. }
  508. if (m.definition_length > namebufsz)
  509. {
  510. free (namebuf);
  511. namebufsz = m.definition_length + 256;
  512. namebuf = XNEWVEC (unsigned char, namebufsz);
  513. }
  514. if ((size_t)read (fd, namebuf, m.definition_length)
  515. != m.definition_length)
  516. goto error;
  517. h = cpp_lookup (r, namebuf, m.name_length);
  518. if (m.flags & NODE_POISONED
  519. || h->flags & NODE_POISONED)
  520. {
  521. if (CPP_OPTION (r, warn_invalid_pch))
  522. cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
  523. "%s: not used because `%.*s' is poisoned",
  524. name, m.name_length, namebuf);
  525. goto fail;
  526. }
  527. if (h->type != NT_MACRO)
  528. {
  529. /* It's ok if __GCC_HAVE_DWARF2_CFI_ASM becomes undefined,
  530. as in, when the PCH file is created with -g and we're
  531. attempting to use it without -g. Restoring the PCH file
  532. is supposed to bring in this definition *and* enable the
  533. generation of call frame information, so that precompiled
  534. definitions that take this macro into accout, to decide
  535. what asm to emit, won't issue .cfi directives when the
  536. compiler doesn't. */
  537. if (!(h->flags & NODE_USED)
  538. && m.name_length == sizeof ("__GCC_HAVE_DWARF2_CFI_ASM") - 1
  539. && !memcmp (namebuf, "__GCC_HAVE_DWARF2_CFI_ASM", m.name_length))
  540. continue;
  541. if (CPP_OPTION (r, warn_invalid_pch))
  542. cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
  543. "%s: not used because `%.*s' not defined",
  544. name, m.name_length, namebuf);
  545. goto fail;
  546. }
  547. newdefn = cpp_macro_definition (r, h);
  548. if (m.definition_length != ustrlen (newdefn)
  549. || memcmp (namebuf, newdefn, m.definition_length) != 0)
  550. {
  551. if (CPP_OPTION (r, warn_invalid_pch))
  552. cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
  553. "%s: not used because `%.*s' defined as `%s' not `%.*s'",
  554. name, m.name_length, namebuf, newdefn + m.name_length,
  555. m.definition_length - m.name_length,
  556. namebuf + m.name_length);
  557. goto fail;
  558. }
  559. }
  560. free (namebuf);
  561. namebuf = NULL;
  562. /* Read in the list of identifiers that must not be defined.
  563. Check that they really aren't. */
  564. undeftab = XNEWVEC (unsigned char, m.definition_length);
  565. if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
  566. goto error;
  567. /* Collect identifiers from the current hash table. */
  568. nl.n_defs = 0;
  569. nl.asize = 10;
  570. nl.defs = XNEWVEC (cpp_hashnode *, nl.asize);
  571. cpp_forall_identifiers (r, &collect_ht_nodes, &nl);
  572. qsort (nl.defs, nl.n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
  573. /* Loop through nl.defs and undeftab, both of which are sorted lists.
  574. There should be no matches. */
  575. first = undeftab;
  576. last = undeftab + m.definition_length;
  577. i = 0;
  578. while (first < last && i < nl.n_defs)
  579. {
  580. int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
  581. if (cmp < 0)
  582. first += ustrlen (first) + 1;
  583. else if (cmp > 0)
  584. ++i;
  585. else
  586. {
  587. if (CPP_OPTION (r, warn_invalid_pch))
  588. cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
  589. "%s: not used because `%s' is defined",
  590. name, first);
  591. goto fail;
  592. }
  593. }
  594. free(nl.defs);
  595. nl.defs = NULL;
  596. free (undeftab);
  597. undeftab = NULL;
  598. /* Read in the next value of __COUNTER__.
  599. Check that (a) __COUNTER__ was not used in the pch or (b) __COUNTER__
  600. has not been used in this translation unit. */
  601. if (read (fd, &counter, sizeof (counter)) != sizeof (counter))
  602. goto error;
  603. if (counter && r->counter)
  604. {
  605. if (CPP_OPTION (r, warn_invalid_pch))
  606. cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
  607. "%s: not used because `__COUNTER__' is invalid",
  608. name);
  609. goto fail;
  610. }
  611. /* We win! */
  612. return 0;
  613. error:
  614. cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
  615. fail:
  616. free (namebuf);
  617. free (undeftab);
  618. free (nl.defs);
  619. return 1;
  620. }
  621. /* Save all the existing macros. */
  622. struct save_macro_data
  623. {
  624. uchar **defns;
  625. size_t count;
  626. size_t array_size;
  627. char **saved_pragmas;
  628. };
  629. /* Save the definition of a single macro, so that it will persist
  630. across a PCH restore. Because macro data is in GCed memory, which
  631. will be blown away by PCH, it must be temporarily copied to
  632. malloced memory. (The macros will refer to identifier nodes which
  633. are also GCed and so on, so the copying is done by turning them
  634. into self-contained strings.) The assumption is that most macro
  635. definitions will come from the PCH file, not from the compilation
  636. before the PCH file is loaded, so it doesn't matter that this is
  637. a little expensive.
  638. It would reduce the cost even further if macros defined in the PCH
  639. file were not saved in this way, but this is not done (yet), except
  640. for builtins, and for #assert by default. */
  641. static int
  642. save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
  643. {
  644. struct save_macro_data *data = (struct save_macro_data *)data_p;
  645. if ((h->flags & NODE_BUILTIN)
  646. && h->type == NT_MACRO
  647. && r->cb.user_builtin_macro)
  648. r->cb.user_builtin_macro (r, h);
  649. if (h->type != NT_VOID
  650. && (h->flags & NODE_BUILTIN) == 0)
  651. {
  652. if (data->count == data->array_size)
  653. {
  654. data->array_size *= 2;
  655. data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size));
  656. }
  657. switch (h->type)
  658. {
  659. case NT_ASSERTION:
  660. /* Not currently implemented. */
  661. return 1;
  662. case NT_MACRO:
  663. {
  664. const uchar * defn = cpp_macro_definition (r, h);
  665. size_t defnlen = ustrlen (defn);
  666. data->defns[data->count] = (uchar *) xmemdup (defn, defnlen,
  667. defnlen + 2);
  668. data->defns[data->count][defnlen] = '\n';
  669. }
  670. break;
  671. default:
  672. abort ();
  673. }
  674. data->count++;
  675. }
  676. return 1;
  677. }
  678. /* Prepare to restore the state, by saving the currently-defined
  679. macros in 'data'. */
  680. void
  681. cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
  682. {
  683. struct save_macro_data *d = XNEW (struct save_macro_data);
  684. d->array_size = 512;
  685. d->defns = XNEWVEC (uchar *, d->array_size);
  686. d->count = 0;
  687. cpp_forall_identifiers (r, save_macros, d);
  688. d->saved_pragmas = _cpp_save_pragma_names (r);
  689. *data = d;
  690. }
  691. /* Given a precompiled header that was previously determined to be valid,
  692. apply all its definitions (and undefinitions) to the current state.
  693. DEPNAME is passed to deps_restore. */
  694. int
  695. cpp_read_state (cpp_reader *r, const char *name, FILE *f,
  696. struct save_macro_data *data)
  697. {
  698. size_t i;
  699. struct lexer_state old_state;
  700. unsigned int counter;
  701. /* Restore spec_nodes, which will be full of references to the old
  702. hashtable entries and so will now be invalid. */
  703. {
  704. struct spec_nodes *s = &r->spec_nodes;
  705. s->n_defined = cpp_lookup (r, DSC("defined"));
  706. s->n_true = cpp_lookup (r, DSC("true"));
  707. s->n_false = cpp_lookup (r, DSC("false"));
  708. s->n__VA_ARGS__ = cpp_lookup (r, DSC("__VA_ARGS__"));
  709. s->n__has_include__ = cpp_lookup (r, DSC("__has_include__"));
  710. s->n__has_include_next__ = cpp_lookup (r, DSC("__has_include_next__"));
  711. }
  712. old_state = r->state;
  713. r->state.in_directive = 1;
  714. r->state.prevent_expansion = 1;
  715. r->state.angled_headers = 0;
  716. /* Run through the carefully-saved macros, insert them. */
  717. for (i = 0; i < data->count; i++)
  718. {
  719. cpp_hashnode *h;
  720. size_t namelen;
  721. uchar *defn;
  722. namelen = ustrcspn (data->defns[i], "( \n");
  723. h = cpp_lookup (r, data->defns[i], namelen);
  724. defn = data->defns[i] + namelen;
  725. /* The PCH file is valid, so we know that if there is a definition
  726. from the PCH file it must be the same as the one we had
  727. originally, and so do not need to restore it. */
  728. if (h->type == NT_VOID)
  729. {
  730. if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
  731. != NULL)
  732. {
  733. _cpp_clean_line (r);
  734. if (!_cpp_create_definition (r, h))
  735. abort ();
  736. _cpp_pop_buffer (r);
  737. }
  738. else
  739. abort ();
  740. }
  741. free (data->defns[i]);
  742. }
  743. r->state = old_state;
  744. _cpp_restore_pragma_names (r, data->saved_pragmas);
  745. free (data);
  746. if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
  747. != 0)
  748. goto error;
  749. if (! _cpp_read_file_entries (r, f))
  750. goto error;
  751. if (fread (&counter, sizeof (counter), 1, f) != 1)
  752. goto error;
  753. if (!r->counter)
  754. r->counter = counter;
  755. /* Read pushed macros. */
  756. if (! _cpp_restore_pushed_macros (r, f))
  757. goto error;
  758. return 0;
  759. error:
  760. cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
  761. return -1;
  762. }