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