1169695Skan/* Part of CPP library.  (Precompiled header reading/writing.)
2169695Skan   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3169695Skan   Free Software Foundation, Inc.
4169695Skan
5169695SkanThis program is free software; you can redistribute it and/or modify it
6169695Skanunder the terms of the GNU General Public License as published by the
7169695SkanFree Software Foundation; either version 2, or (at your option) any
8169695Skanlater version.
9169695Skan
10169695SkanThis program is distributed in the hope that it will be useful,
11169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
12169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13169695SkanGNU General Public License for more details.
14169695Skan
15169695SkanYou should have received a copy of the GNU General Public License
16169695Skanalong with this program; if not, write to the Free Software
17169695SkanFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18169695Skan
19169695Skan#include "config.h"
20169695Skan#include "system.h"
21169695Skan#include "cpplib.h"
22169695Skan#include "internal.h"
23169695Skan#include "hashtab.h"
24169695Skan#include "mkdeps.h"
25169695Skan
26169695Skanstatic int write_macdef (cpp_reader *, cpp_hashnode *, void *);
27169695Skanstatic int save_idents (cpp_reader *, cpp_hashnode *, void *);
28169695Skanstatic hashval_t hashmem (const void *, size_t);
29169695Skanstatic hashval_t cpp_string_hash (const void *);
30169695Skanstatic int cpp_string_eq (const void *, const void *);
31169695Skanstatic int count_defs (cpp_reader *, cpp_hashnode *, void *);
32169695Skanstatic int comp_hashnodes (const void *, const void *);
33169695Skanstatic int collect_ht_nodes (cpp_reader *, cpp_hashnode *, void *);
34169695Skanstatic int write_defs (cpp_reader *, cpp_hashnode *, void *);
35169695Skanstatic int save_macros (cpp_reader *, cpp_hashnode *, void *);
36169695Skan
37169695Skan/* This structure represents a macro definition on disk.  */
38259890Spfgstruct macrodef_struct
39169695Skan{
40169695Skan  unsigned int definition_length;
41169695Skan  unsigned short name_length;
42169695Skan  unsigned short flags;
43169695Skan};
44169695Skan
45259890Spfg/* This is how we write out a macro definition.
46169695Skan   Suitable for being called by cpp_forall_identifiers.  */
47169695Skan
48169695Skanstatic int
49169695Skanwrite_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
50169695Skan{
51169695Skan  FILE *f = (FILE *) file_p;
52169695Skan  switch (hn->type)
53169695Skan    {
54169695Skan    case NT_VOID:
55169695Skan      if (! (hn->flags & NODE_POISONED))
56169695Skan	return 1;
57259890Spfg
58169695Skan    case NT_MACRO:
59169695Skan      if ((hn->flags & NODE_BUILTIN))
60169695Skan	return 1;
61169695Skan
62169695Skan      {
63169695Skan	struct macrodef_struct s;
64169695Skan	const unsigned char *defn;
65169695Skan
66169695Skan	s.name_length = NODE_LEN (hn);
67169695Skan	s.flags = hn->flags & NODE_POISONED;
68169695Skan
69169695Skan	if (hn->type == NT_MACRO)
70169695Skan	  {
71169695Skan	    defn = cpp_macro_definition (pfile, hn);
72169695Skan	    s.definition_length = ustrlen (defn);
73169695Skan	  }
74169695Skan	else
75169695Skan	  {
76169695Skan	    defn = NODE_NAME (hn);
77169695Skan	    s.definition_length = s.name_length;
78169695Skan	  }
79259890Spfg
80169695Skan	if (fwrite (&s, sizeof (s), 1, f) != 1
81169695Skan	    || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
82169695Skan	  {
83169695Skan	    cpp_errno (pfile, CPP_DL_ERROR,
84169695Skan		       "while writing precompiled header");
85169695Skan	    return 0;
86169695Skan	  }
87169695Skan      }
88169695Skan      return 1;
89259890Spfg
90169695Skan    case NT_ASSERTION:
91169695Skan      /* Not currently implemented.  */
92169695Skan      return 1;
93169695Skan
94169695Skan    default:
95169695Skan      abort ();
96169695Skan    }
97169695Skan}
98169695Skan
99169695Skan/* This structure records the names of the defined macros.
100169695Skan   It's also used as a callback structure for size_initial_idents
101169695Skan   and save_idents.  */
102169695Skan
103169695Skanstruct cpp_savedstate
104169695Skan{
105169695Skan  /* A hash table of the defined identifiers.  */
106169695Skan  htab_t definedhash;
107169695Skan  /* The size of the definitions of those identifiers (the size of
108169695Skan     'definedstrs').  */
109169695Skan  size_t hashsize;
110169695Skan  /* Number of definitions */
111169695Skan  size_t n_defs;
112169695Skan  /* Array of definitions.  In cpp_write_pch_deps it is used for sorting.  */
113169695Skan  cpp_hashnode **defs;
114169695Skan  /* Space for the next definition.  Definitions are null-terminated
115169695Skan     strings.  */
116169695Skan  unsigned char *definedstrs;
117169695Skan};
118169695Skan
119169695Skan/* Save this identifier into the state: put it in the hash table,
120169695Skan   put the definition in 'definedstrs'.  */
121169695Skan
122169695Skanstatic int
123169695Skansave_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
124169695Skan{
125169695Skan  struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
126259890Spfg
127169695Skan  if (hn->type != NT_VOID)
128169695Skan    {
129169695Skan      struct cpp_string news;
130169695Skan      void **slot;
131169695Skan
132169695Skan      news.len = NODE_LEN (hn);
133169695Skan      news.text= NODE_NAME (hn);
134169695Skan      slot = htab_find_slot (ss->definedhash, &news, INSERT);
135169695Skan      if (*slot == NULL)
136169695Skan	{
137169695Skan	  struct cpp_string *sp;
138169695Skan	  unsigned char *text;
139259890Spfg
140169695Skan	  sp = XNEW (struct cpp_string);
141169695Skan	  *slot = sp;
142169695Skan
143169695Skan	  sp->len = NODE_LEN (hn);
144169695Skan	  sp->text = text = XNEWVEC (unsigned char, NODE_LEN (hn));
145169695Skan	  memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
146169695Skan	}
147169695Skan    }
148169695Skan
149169695Skan  return 1;
150169695Skan}
151169695Skan
152169695Skan/* Hash some memory in a generic way.  */
153169695Skan
154169695Skanstatic hashval_t
155169695Skanhashmem (const void *p_p, size_t sz)
156169695Skan{
157169695Skan  const unsigned char *p = (const unsigned char *)p_p;
158169695Skan  size_t i;
159169695Skan  hashval_t h;
160259890Spfg
161169695Skan  h = 0;
162169695Skan  for (i = 0; i < sz; i++)
163169695Skan    h = h * 67 - (*p++ - 113);
164169695Skan  return h;
165169695Skan}
166169695Skan
167169695Skan/* Hash a cpp string for the hashtable machinery.  */
168169695Skan
169169695Skanstatic hashval_t
170169695Skancpp_string_hash (const void *a_p)
171169695Skan{
172169695Skan  const struct cpp_string *a = (const struct cpp_string *) a_p;
173169695Skan  return hashmem (a->text, a->len);
174169695Skan}
175169695Skan
176169695Skan/* Compare two cpp strings for the hashtable machinery.  */
177169695Skan
178169695Skanstatic int
179169695Skancpp_string_eq (const void *a_p, const void *b_p)
180169695Skan{
181169695Skan  const struct cpp_string *a = (const struct cpp_string *) a_p;
182169695Skan  const struct cpp_string *b = (const struct cpp_string *) b_p;
183169695Skan  return (a->len == b->len
184169695Skan	  && memcmp (a->text, b->text, a->len) == 0);
185169695Skan}
186169695Skan
187169695Skan/* Save the current definitions of the cpp_reader for dependency
188169695Skan   checking purposes.  When writing a precompiled header, this should
189169695Skan   be called at the same point in the compilation as cpp_valid_state
190169695Skan   would be called when reading the precompiled header back in.  */
191169695Skan
192169695Skanint
193169695Skancpp_save_state (cpp_reader *r, FILE *f)
194169695Skan{
195169695Skan  /* Save the list of non-void identifiers for the dependency checking.  */
196169695Skan  r->savedstate = XNEW (struct cpp_savedstate);
197259890Spfg  r->savedstate->definedhash = htab_create (100, cpp_string_hash,
198169695Skan					    cpp_string_eq, NULL);
199169695Skan  cpp_forall_identifiers (r, save_idents, r->savedstate);
200259890Spfg
201169695Skan  /* Write out the list of defined identifiers.  */
202169695Skan  cpp_forall_identifiers (r, write_macdef, f);
203169695Skan
204169695Skan  return 0;
205169695Skan}
206169695Skan
207169695Skan/* Calculate the 'hashsize' field of the saved state.  */
208169695Skan
209169695Skanstatic int
210169695Skancount_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
211169695Skan{
212169695Skan  struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
213259890Spfg
214169695Skan  switch (hn->type)
215169695Skan    {
216169695Skan    case NT_MACRO:
217169695Skan      if (hn->flags & NODE_BUILTIN)
218169695Skan	return 1;
219259890Spfg
220169695Skan      /* else fall through.  */
221169695Skan
222169695Skan    case NT_VOID:
223169695Skan      {
224169695Skan	struct cpp_string news;
225169695Skan	void **slot;
226259890Spfg
227169695Skan	news.len = NODE_LEN (hn);
228169695Skan	news.text = NODE_NAME (hn);
229169695Skan	slot = (void **) htab_find (ss->definedhash, &news);
230169695Skan	if (slot == NULL)
231169695Skan	  {
232169695Skan	    ss->hashsize += NODE_LEN (hn) + 1;
233169695Skan	    ss->n_defs += 1;
234169695Skan	  }
235169695Skan      }
236169695Skan      return 1;
237169695Skan
238169695Skan    case NT_ASSERTION:
239169695Skan      /* Not currently implemented.  */
240169695Skan      return 1;
241169695Skan
242169695Skan    default:
243169695Skan      abort ();
244169695Skan    }
245169695Skan}
246169695Skan
247169695Skan/* Collect the identifiers into the state's string table.  */
248169695Skanstatic int
249169695Skanwrite_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
250169695Skan{
251169695Skan  struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
252259890Spfg
253169695Skan  switch (hn->type)
254169695Skan    {
255169695Skan    case NT_MACRO:
256169695Skan      if (hn->flags & NODE_BUILTIN)
257169695Skan	return 1;
258259890Spfg
259169695Skan      /* else fall through.  */
260169695Skan
261169695Skan    case NT_VOID:
262169695Skan      {
263169695Skan	struct cpp_string news;
264169695Skan	void **slot;
265259890Spfg
266169695Skan	news.len = NODE_LEN (hn);
267169695Skan	news.text = NODE_NAME (hn);
268169695Skan	slot = (void **) htab_find (ss->definedhash, &news);
269169695Skan	if (slot == NULL)
270169695Skan	  {
271169695Skan	    ss->defs[ss->n_defs] = hn;
272169695Skan	    ss->n_defs += 1;
273169695Skan	  }
274169695Skan      }
275169695Skan      return 1;
276169695Skan
277169695Skan    case NT_ASSERTION:
278169695Skan      /* Not currently implemented.  */
279169695Skan      return 1;
280169695Skan
281169695Skan    default:
282169695Skan      abort ();
283169695Skan    }
284169695Skan}
285169695Skan
286169695Skan/* Comparison function for qsort.  The arguments point to pointers of
287169695Skan   type ht_hashnode *.  */
288169695Skanstatic int
289169695Skancomp_hashnodes (const void *px, const void *py)
290169695Skan{
291169695Skan  cpp_hashnode *x = *(cpp_hashnode **) px;
292169695Skan  cpp_hashnode *y = *(cpp_hashnode **) py;
293169695Skan  return ustrcmp (NODE_NAME (x), NODE_NAME (y));
294169695Skan}
295169695Skan
296169695Skan/* Write out the remainder of the dependency information.  This should be
297169695Skan   called after the PCH is ready to be saved.  */
298169695Skan
299169695Skanint
300169695Skancpp_write_pch_deps (cpp_reader *r, FILE *f)
301169695Skan{
302169695Skan  struct macrodef_struct z;
303169695Skan  struct cpp_savedstate *const ss = r->savedstate;
304169695Skan  unsigned char *definedstrs;
305169695Skan  size_t i;
306259890Spfg
307169695Skan  /* Collect the list of identifiers which have been seen and
308169695Skan     weren't defined to anything previously.  */
309169695Skan  ss->hashsize = 0;
310169695Skan  ss->n_defs = 0;
311169695Skan  cpp_forall_identifiers (r, count_defs, ss);
312169695Skan
313169695Skan  ss->defs = XNEWVEC (cpp_hashnode *, ss->n_defs);
314169695Skan  ss->n_defs = 0;
315169695Skan  cpp_forall_identifiers (r, write_defs, ss);
316169695Skan
317169695Skan  /* Sort the list, copy it into a buffer, and write it out.  */
318169695Skan  qsort (ss->defs, ss->n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
319169695Skan  definedstrs = ss->definedstrs = XNEWVEC (unsigned char, ss->hashsize);
320169695Skan  for (i = 0; i < ss->n_defs; ++i)
321169695Skan    {
322169695Skan      size_t len = NODE_LEN (ss->defs[i]);
323169695Skan      memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
324169695Skan      definedstrs += len + 1;
325169695Skan    }
326169695Skan
327169695Skan  memset (&z, 0, sizeof (z));
328169695Skan  z.definition_length = ss->hashsize;
329169695Skan  if (fwrite (&z, sizeof (z), 1, f) != 1
330169695Skan      || fwrite (ss->definedstrs, ss->hashsize, 1, f) != 1)
331169695Skan    {
332169695Skan      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
333169695Skan      return -1;
334169695Skan    }
335169695Skan  free (ss->definedstrs);
336169695Skan
337169695Skan  /* Free the saved state.  */
338169695Skan  free (ss);
339169695Skan  r->savedstate = NULL;
340169695Skan  return 0;
341169695Skan}
342169695Skan
343169695Skan/* Write out the definitions of the preprocessor, in a form suitable for
344169695Skan   cpp_read_state.  */
345169695Skan
346169695Skanint
347169695Skancpp_write_pch_state (cpp_reader *r, FILE *f)
348169695Skan{
349169695Skan  if (!r->deps)
350169695Skan    r->deps = deps_init ();
351169695Skan
352169695Skan  if (deps_save (r->deps, f) != 0)
353169695Skan    {
354169695Skan      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
355169695Skan      return -1;
356169695Skan    }
357169695Skan
358169695Skan  if (! _cpp_save_file_entries (r, f))
359169695Skan    {
360169695Skan      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
361169695Skan      return -1;
362169695Skan    }
363169695Skan
364169695Skan  return 0;
365169695Skan}
366169695Skan
367169695Skan
368169695Skan/* Data structure to transform hash table nodes into a sorted list */
369169695Skan
370169695Skanstruct ht_node_list
371169695Skan{
372169695Skan  /* Array of nodes */
373169695Skan  cpp_hashnode **defs;
374169695Skan  /* Number of nodes in the array */
375169695Skan  size_t n_defs;
376169695Skan  /* Size of the allocated array */
377169695Skan  size_t asize;
378169695Skan};
379169695Skan
380169695Skan/* Callback for collecting identifiers from hash table */
381169695Skan
382169695Skanstatic int
383169695Skancollect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
384169695Skan		  void *nl_p)
385169695Skan{
386169695Skan  struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
387169695Skan
388169695Skan  if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
389169695Skan    {
390169695Skan      if (nl->n_defs == nl->asize)
391169695Skan        {
392169695Skan          nl->asize *= 2;
393169695Skan          nl->defs = XRESIZEVEC (cpp_hashnode *, nl->defs, nl->asize);
394169695Skan        }
395169695Skan
396169695Skan      nl->defs[nl->n_defs] = hn;
397169695Skan      ++nl->n_defs;
398169695Skan    }
399169695Skan  return 1;
400169695Skan}
401169695Skan
402169695Skan
403169695Skan/* Return nonzero if FD is a precompiled header which is consistent
404169695Skan   with the preprocessor's current definitions.  It will be consistent
405169695Skan   when:
406169695Skan
407259890Spfg   - anything that was defined just before the PCH was generated
408169695Skan     is defined the same way now; and
409169695Skan   - anything that was not defined then, but is defined now, was not
410169695Skan     used by the PCH.
411169695Skan
412169695Skan   NAME is used to print warnings if `warn_invalid_pch' is set in the
413169695Skan   reader's flags.
414169695Skan*/
415169695Skan
416169695Skanint
417169695Skancpp_valid_state (cpp_reader *r, const char *name, int fd)
418169695Skan{
419169695Skan  struct macrodef_struct m;
420169695Skan  size_t namebufsz = 256;
421169695Skan  unsigned char *namebuf = XNEWVEC (unsigned char, namebufsz);
422169695Skan  unsigned char *undeftab = NULL;
423169695Skan  struct ht_node_list nl = { 0, 0, 0 };
424169695Skan  unsigned char *first, *last;
425169695Skan  unsigned int i;
426169695Skan
427169695Skan  /* Read in the list of identifiers that must be defined
428169695Skan     Check that they are defined in the same way.  */
429169695Skan  for (;;)
430169695Skan    {
431169695Skan      cpp_hashnode *h;
432169695Skan      const unsigned char *newdefn;
433259890Spfg
434169695Skan      if (read (fd, &m, sizeof (m)) != sizeof (m))
435169695Skan	goto error;
436259890Spfg
437169695Skan      if (m.name_length == 0)
438169695Skan	break;
439169695Skan
440169695Skan      /* If this file is already preprocessed, there won't be any
441169695Skan	 macros defined, and that's OK.  */
442169695Skan      if (CPP_OPTION (r, preprocessed))
443169695Skan	{
444169695Skan	  if (lseek (fd, m.definition_length, SEEK_CUR) == -1)
445169695Skan	    goto error;
446169695Skan	  continue;
447169695Skan	}
448169695Skan
449169695Skan      if (m.definition_length > namebufsz)
450169695Skan	{
451169695Skan	  free (namebuf);
452169695Skan	  namebufsz = m.definition_length + 256;
453169695Skan	  namebuf = XNEWVEC (unsigned char, namebufsz);
454169695Skan	}
455169695Skan
456259890Spfg      if ((size_t)read (fd, namebuf, m.definition_length)
457169695Skan	  != m.definition_length)
458169695Skan	goto error;
459259890Spfg
460169695Skan      h = cpp_lookup (r, namebuf, m.name_length);
461169695Skan      if (m.flags & NODE_POISONED
462169695Skan	  || h->type != NT_MACRO
463169695Skan	  || h->flags & NODE_POISONED)
464169695Skan	{
465169695Skan	  if (CPP_OPTION (r, warn_invalid_pch))
466169695Skan	    cpp_error (r, CPP_DL_WARNING_SYSHDR,
467169695Skan		       "%s: not used because `%.*s' not defined",
468169695Skan		       name, m.name_length, namebuf);
469169695Skan	  goto fail;
470169695Skan	}
471169695Skan
472169695Skan      newdefn = cpp_macro_definition (r, h);
473259890Spfg
474169695Skan      if (m.definition_length != ustrlen (newdefn)
475169695Skan	  || memcmp (namebuf, newdefn, m.definition_length) != 0)
476169695Skan	{
477169695Skan	  if (CPP_OPTION (r, warn_invalid_pch))
478169695Skan	    cpp_error (r, CPP_DL_WARNING_SYSHDR,
479169695Skan	       "%s: not used because `%.*s' defined as `%s' not `%.*s'",
480169695Skan		       name, m.name_length, namebuf, newdefn + m.name_length,
481169695Skan		       m.definition_length - m.name_length,
482169695Skan		       namebuf +  m.name_length);
483169695Skan	  goto fail;
484169695Skan	}
485169695Skan    }
486169695Skan  free (namebuf);
487169695Skan  namebuf = NULL;
488169695Skan
489169695Skan  /* Read in the list of identifiers that must not be defined.
490169695Skan     Check that they really aren't.  */
491169695Skan  undeftab = XNEWVEC (unsigned char, m.definition_length);
492169695Skan  if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
493169695Skan    goto error;
494169695Skan
495169695Skan  /* Collect identifiers from the current hash table.  */
496169695Skan  nl.n_defs = 0;
497169695Skan  nl.asize = 10;
498169695Skan  nl.defs = XNEWVEC (cpp_hashnode *, nl.asize);
499169695Skan  cpp_forall_identifiers (r, &collect_ht_nodes, &nl);
500169695Skan  qsort (nl.defs, nl.n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
501259890Spfg
502169695Skan  /* Loop through nl.defs and undeftab, both of which are sorted lists.
503169695Skan     There should be no matches.  */
504169695Skan  first = undeftab;
505169695Skan  last = undeftab + m.definition_length;
506169695Skan  i = 0;
507259890Spfg
508169695Skan  while (first < last && i < nl.n_defs)
509169695Skan    {
510169695Skan      int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
511259890Spfg
512169695Skan      if (cmp < 0)
513169695Skan 	first += ustrlen (first) + 1;
514169695Skan      else if (cmp > 0)
515169695Skan 	++i;
516169695Skan      else
517169695Skan	{
518169695Skan	  if (CPP_OPTION (r, warn_invalid_pch))
519259890Spfg	    cpp_error (r, CPP_DL_WARNING_SYSHDR,
520169695Skan		       "%s: not used because `%s' is defined",
521169695Skan		       name, first);
522169695Skan	  goto fail;
523169695Skan	}
524169695Skan    }
525259890Spfg
526169695Skan  free(nl.defs);
527169695Skan  free (undeftab);
528169695Skan
529169695Skan  /* We win!  */
530169695Skan  return 0;
531169695Skan
532169695Skan error:
533169695Skan  cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
534169695Skan  return -1;
535169695Skan
536169695Skan fail:
537169695Skan  if (namebuf != NULL)
538169695Skan    free (namebuf);
539169695Skan  if (undeftab != NULL)
540169695Skan    free (undeftab);
541169695Skan  if (nl.defs != NULL)
542169695Skan    free (nl.defs);
543169695Skan  return 1;
544169695Skan}
545169695Skan
546169695Skan/* Save all the existing macros.  */
547169695Skan
548259890Spfgstruct save_macro_data
549169695Skan{
550169695Skan  uchar **defns;
551169695Skan  size_t count;
552169695Skan  size_t array_size;
553169695Skan  char **saved_pragmas;
554169695Skan};
555169695Skan
556169695Skan/* Save the definition of a single macro, so that it will persist
557169695Skan   across a PCH restore.  Because macro data is in GCed memory, which
558169695Skan   will be blown away by PCH, it must be temporarily copied to
559169695Skan   malloced memory.  (The macros will refer to identifier nodes which
560169695Skan   are also GCed and so on, so the copying is done by turning them
561169695Skan   into self-contained strings.)  The assumption is that most macro
562169695Skan   definitions will come from the PCH file, not from the compilation
563169695Skan   before the PCH file is loaded, so it doesn't matter that this is
564169695Skan   a little expensive.
565169695Skan
566169695Skan   It would reduce the cost even further if macros defined in the PCH
567169695Skan   file were not saved in this way, but this is not done (yet), except
568169695Skan   for builtins, and for #assert by default.  */
569169695Skan
570259890Spfgstatic int
571169695Skansave_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
572169695Skan{
573169695Skan  struct save_macro_data *data = (struct save_macro_data *)data_p;
574169695Skan  if (h->type != NT_VOID
575169695Skan      && (h->flags & NODE_BUILTIN) == 0)
576169695Skan    {
577169695Skan      if (data->count == data->array_size)
578169695Skan	{
579169695Skan	  data->array_size *= 2;
580259890Spfg	  data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size));
581169695Skan	}
582259890Spfg
583169695Skan      switch (h->type)
584169695Skan	{
585169695Skan	case NT_ASSERTION:
586169695Skan	  /* Not currently implemented.  */
587169695Skan	  return 1;
588169695Skan
589169695Skan	case NT_MACRO:
590169695Skan	  {
591169695Skan	    const uchar * defn = cpp_macro_definition (r, h);
592169695Skan	    size_t defnlen = ustrlen (defn);
593169695Skan
594169695Skan	    data->defns[data->count] = (uchar *) xmemdup (defn, defnlen,
595169695Skan                                                          defnlen + 2);
596169695Skan	    data->defns[data->count][defnlen] = '\n';
597169695Skan	  }
598169695Skan	  break;
599259890Spfg
600169695Skan	default:
601169695Skan	  abort ();
602169695Skan	}
603169695Skan      data->count++;
604169695Skan    }
605169695Skan  return 1;
606169695Skan}
607169695Skan
608169695Skan/* Prepare to restore the state, by saving the currently-defined
609169695Skan   macros in 'data'.  */
610169695Skan
611169695Skanvoid
612169695Skancpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
613169695Skan{
614169695Skan  struct save_macro_data *d = XNEW (struct save_macro_data);
615259890Spfg
616169695Skan  d->array_size = 512;
617169695Skan  d->defns = XNEWVEC (uchar *, d->array_size);
618169695Skan  d->count = 0;
619169695Skan  cpp_forall_identifiers (r, save_macros, d);
620169695Skan  d->saved_pragmas = _cpp_save_pragma_names (r);
621169695Skan  *data = d;
622169695Skan}
623169695Skan
624169695Skan/* Given a precompiled header that was previously determined to be valid,
625259890Spfg   apply all its definitions (and undefinitions) to the current state.
626169695Skan   DEPNAME is passed to deps_restore.  */
627169695Skan
628169695Skanint
629169695Skancpp_read_state (cpp_reader *r, const char *name, FILE *f,
630169695Skan		struct save_macro_data *data)
631169695Skan{
632169695Skan  size_t i;
633169695Skan  struct lexer_state old_state;
634169695Skan
635259890Spfg  /* Restore spec_nodes, which will be full of references to the old
636169695Skan     hashtable entries and so will now be invalid.  */
637169695Skan  {
638169695Skan    struct spec_nodes *s = &r->spec_nodes;
639169695Skan    s->n_defined	= cpp_lookup (r, DSC("defined"));
640169695Skan    s->n_true		= cpp_lookup (r, DSC("true"));
641169695Skan    s->n_false		= cpp_lookup (r, DSC("false"));
642169695Skan    s->n__VA_ARGS__     = cpp_lookup (r, DSC("__VA_ARGS__"));
643169695Skan  }
644169695Skan
645169695Skan  old_state = r->state;
646169695Skan  r->state.in_directive = 1;
647169695Skan  r->state.prevent_expansion = 1;
648169695Skan  r->state.angled_headers = 0;
649169695Skan
650169695Skan  /* Run through the carefully-saved macros, insert them.  */
651169695Skan  for (i = 0; i < data->count; i++)
652169695Skan    {
653169695Skan      cpp_hashnode *h;
654169695Skan      size_t namelen;
655169695Skan      uchar *defn;
656169695Skan
657169695Skan      namelen = ustrcspn (data->defns[i], "( \n");
658169695Skan      h = cpp_lookup (r, data->defns[i], namelen);
659169695Skan      defn = data->defns[i] + namelen;
660169695Skan
661169695Skan      /* The PCH file is valid, so we know that if there is a definition
662169695Skan	 from the PCH file it must be the same as the one we had
663169695Skan	 originally, and so do not need to restore it.  */
664169695Skan      if (h->type == NT_VOID)
665169695Skan	{
666169695Skan	  if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
667169695Skan	      != NULL)
668169695Skan	    {
669169695Skan	      _cpp_clean_line (r);
670169695Skan	      if (!_cpp_create_definition (r, h))
671169695Skan		abort ();
672169695Skan	      _cpp_pop_buffer (r);
673169695Skan	    }
674169695Skan	  else
675169695Skan	    abort ();
676169695Skan	}
677169695Skan
678169695Skan      free (data->defns[i]);
679169695Skan    }
680169695Skan  r->state = old_state;
681169695Skan
682169695Skan  _cpp_restore_pragma_names (r, data->saved_pragmas);
683169695Skan
684169695Skan  free (data);
685169695Skan
686169695Skan  if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
687169695Skan      != 0)
688169695Skan    goto error;
689169695Skan
690169695Skan  if (! _cpp_read_file_entries (r, f))
691169695Skan    goto error;
692169695Skan
693169695Skan  return 0;
694259890Spfg
695169695Skan error:
696169695Skan  cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
697169695Skan  return -1;
698169695Skan}
699