1 /* Part of CPP library. (Precompiled header reading/writing.)
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include "coretypes.h"
27 static int write_macdef PARAMS ((cpp_reader *, cpp_hashnode *, void *));
28 static int save_idents PARAMS ((cpp_reader *, cpp_hashnode *, void *));
29 static hashval_t hashmem PARAMS ((const void *, size_t));
30 static hashval_t cpp_string_hash PARAMS ((const void *));
31 static int cpp_string_eq PARAMS ((const void *, const void *));
32 static int count_defs PARAMS ((cpp_reader *, cpp_hashnode *, void *));
33 static int comp_hashnodes PARAMS ((const void *, const void *));
34 static int collect_ht_nodes PARAMS ((cpp_reader *, cpp_hashnode *, void *));
35 static int write_defs PARAMS ((cpp_reader *, cpp_hashnode *, void *));
36 static int save_macros PARAMS ((cpp_reader *, cpp_hashnode *, void *));
37 static int reset_ht PARAMS ((cpp_reader *, cpp_hashnode *, void *));
39 /* This structure represents a macro definition on disk. */
40 struct macrodef_struct
42 unsigned int definition_length;
43 unsigned short name_length;
47 /* This is how we write out a macro definition.
48 Suitable for being called by cpp_forall_identifiers. */
51 write_macdef (pfile, hn, file_p)
56 FILE *f = (FILE *) file_p;
60 if (! (hn->flags & NODE_POISONED))
64 if ((hn->flags & NODE_BUILTIN))
68 struct macrodef_struct s;
69 const unsigned char *defn;
71 s.name_length = NODE_LEN (hn);
72 s.flags = hn->flags & NODE_POISONED;
74 if (hn->type == NT_MACRO)
76 defn = cpp_macro_definition (pfile, hn);
77 s.definition_length = ustrlen (defn);
81 defn = NODE_NAME (hn);
82 s.definition_length = s.name_length;
85 if (fwrite (&s, sizeof (s), 1, f) != 1
86 || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
88 cpp_errno (pfile, DL_ERROR, "while writing precompiled header");
95 /* Not currently implemented. */
103 /* This structure records the names of the defined macros.
104 It's also used as a callback structure for size_initial_idents
107 struct cpp_savedstate
109 /* A hash table of the defined identifiers. */
111 /* The size of the definitions of those identifiers (the size of
114 /* Number of definitions */
116 /* Array of definitions. In cpp_write_pch_deps it is used for sorting. */
118 /* Space for the next definition. Definitions are null-terminated
120 unsigned char *definedstrs;
123 /* Save this identifier into the state: put it in the hash table,
124 put the definition in 'definedstrs'. */
127 save_idents (pfile, hn, ss_p)
128 cpp_reader *pfile ATTRIBUTE_UNUSED;
132 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
134 if (hn->type != NT_VOID)
136 struct cpp_string news;
139 news.len = NODE_LEN (hn);
140 news.text= NODE_NAME (hn);
141 slot = htab_find_slot (ss->definedhash, &news, INSERT);
144 struct cpp_string *sp;
147 sp = xmalloc (sizeof (struct cpp_string));
150 sp->len = NODE_LEN (hn);
151 sp->text = text = xmalloc (NODE_LEN (hn));
152 memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
159 /* Hash some memory in a generic way. */
166 const unsigned char *p = (const unsigned char *)p_p;
171 for (i = 0; i < sz; i++)
172 h = h * 67 - (*p++ - 113);
176 /* Hash a cpp string for the hashtable machinery. */
179 cpp_string_hash (a_p)
182 const struct cpp_string *a = (const struct cpp_string *) a_p;
183 return hashmem (a->text, a->len);
186 /* Compare two cpp strings for the hashtable machinery. */
189 cpp_string_eq (a_p, b_p)
193 const struct cpp_string *a = (const struct cpp_string *) a_p;
194 const struct cpp_string *b = (const struct cpp_string *) b_p;
195 return (a->len == b->len
196 && memcmp (a->text, b->text, a->len) == 0);
199 /* Save the current definitions of the cpp_reader for dependency
200 checking purposes. When writing a precompiled header, this should
201 be called at the same point in the compilation as cpp_valid_state
202 would be called when reading the precompiled header back in. */
205 cpp_save_state (r, f)
209 /* Save the list of non-void identifiers for the dependency checking. */
210 r->savedstate = xmalloc (sizeof (struct cpp_savedstate));
211 r->savedstate->definedhash = htab_create (100, cpp_string_hash,
212 cpp_string_eq, NULL);
213 cpp_forall_identifiers (r, save_idents, r->savedstate);
215 /* Write out the list of defined identifiers. */
216 cpp_forall_identifiers (r, write_macdef, f);
221 /* Calculate the 'hashsize' field of the saved state. */
224 count_defs (pfile, hn, ss_p)
225 cpp_reader *pfile ATTRIBUTE_UNUSED;
229 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
234 if (hn->flags & NODE_BUILTIN)
237 /* else fall through. */
241 struct cpp_string news;
244 news.len = NODE_LEN (hn);
245 news.text = NODE_NAME (hn);
246 slot = htab_find (ss->definedhash, &news);
249 ss->hashsize += NODE_LEN (hn) + 1;
256 /* Not currently implemented. */
264 /* Collect the identifiers into the state's string table. */
266 write_defs (pfile, hn, ss_p)
267 cpp_reader *pfile ATTRIBUTE_UNUSED;
271 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
276 if (hn->flags & NODE_BUILTIN)
279 /* else fall through. */
283 struct cpp_string news;
286 news.len = NODE_LEN (hn);
287 news.text = NODE_NAME (hn);
288 slot = htab_find (ss->definedhash, &news);
291 ss->defs[ss->n_defs] = hn;
298 /* Not currently implemented. */
306 /* Comparison function for qsort. The arguments point to pointers of
307 type ht_hashnode *. */
309 comp_hashnodes (px, py)
313 cpp_hashnode *x = *(cpp_hashnode **) px;
314 cpp_hashnode *y = *(cpp_hashnode **) py;
315 return ustrcmp (NODE_NAME (x), NODE_NAME (y));
318 /* Write out the remainder of the dependency information. This should be
319 called after the PCH is ready to be saved. */
322 cpp_write_pch_deps (r, f)
326 struct macrodef_struct z;
327 struct cpp_savedstate *const ss = r->savedstate;
328 unsigned char *definedstrs;
331 /* Collect the list of identifiers which have been seen and
332 weren't defined to anything previously. */
335 cpp_forall_identifiers (r, count_defs, ss);
337 ss->defs = xmalloc (ss->n_defs * sizeof (cpp_hashnode *));
339 cpp_forall_identifiers (r, write_defs, ss);
341 /* Sort the list, copy it into a buffer, and write it out. */
342 qsort (ss->defs, ss->n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
343 definedstrs = ss->definedstrs = xmalloc (ss->hashsize);
344 for (i = 0; i < ss->n_defs; ++i)
346 size_t len = NODE_LEN (ss->defs[i]);
347 memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
348 definedstrs += len + 1;
351 memset (&z, 0, sizeof (z));
352 z.definition_length = ss->hashsize;
353 if (fwrite (&z, sizeof (z), 1, f) != 1
354 || fwrite (ss->definedstrs, ss->hashsize, 1, f) != 1)
356 cpp_errno (r, DL_ERROR, "while writing precompiled header");
359 free (ss->definedstrs);
361 /* Free the saved state. */
363 r->savedstate = NULL;
367 /* Write out the definitions of the preprocessor, in a form suitable for
371 cpp_write_pch_state (r, f)
375 struct macrodef_struct z;
377 /* Write out the list of defined identifiers. */
378 cpp_forall_identifiers (r, write_macdef, f);
379 memset (&z, 0, sizeof (z));
380 if (fwrite (&z, sizeof (z), 1, f) != 1)
382 cpp_errno (r, DL_ERROR, "while writing precompiled header");
387 r->deps = deps_init ();
389 if (deps_save (r->deps, f) != 0)
391 cpp_errno (r, DL_ERROR, "while writing precompiled header");
399 /* Data structure to transform hash table nodes into a sorted list */
405 /* Number of nodes in the array */
407 /* Size of the allocated array */
411 /* Callback for collecting identifiers from hash table */
414 collect_ht_nodes (pfile, hn, nl_p)
415 cpp_reader *pfile ATTRIBUTE_UNUSED;
419 struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
421 if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
423 if (nl->n_defs == nl->asize)
426 nl->defs = xrealloc (nl->defs, nl->asize * sizeof (cpp_hashnode *));
429 nl->defs[nl->n_defs] = hn;
436 /* Return nonzero if FD is a precompiled header which is consistent
437 with the preprocessor's current definitions. It will be consistent
440 - anything that was defined just before the PCH was generated
441 is defined the same way now; and
442 - anything that was not defined then, but is defined now, was not
445 NAME is used to print warnings if `warn_invalid_pch' is set in the
450 cpp_valid_state (r, name, fd)
455 struct macrodef_struct m;
456 size_t namebufsz = 256;
457 unsigned char *namebuf = xmalloc (namebufsz);
458 unsigned char *undeftab = NULL;
459 struct ht_node_list nl;
460 unsigned char *first, *last;
463 /* Read in the list of identifiers that must be defined
464 Check that they are defined in the same way. */
468 const unsigned char *newdefn;
470 if (read (fd, &m, sizeof (m)) != sizeof (m))
473 if (m.name_length == 0)
476 if (m.definition_length > namebufsz)
479 namebufsz = m.definition_length + 256;
480 namebuf = xmalloc (namebufsz);
483 if ((size_t)read (fd, namebuf, m.definition_length)
484 != m.definition_length)
487 h = cpp_lookup (r, namebuf, m.name_length);
488 if (m.flags & NODE_POISONED
489 || h->type != NT_MACRO
490 || h->flags & NODE_POISONED)
492 if (CPP_OPTION (r, warn_invalid_pch))
493 cpp_error (r, DL_WARNING_SYSHDR,
494 "%s: not used because `%.*s' not defined",
495 name, m.name_length, namebuf);
499 newdefn = cpp_macro_definition (r, h);
501 if (m.definition_length != ustrlen (newdefn)
502 || memcmp (namebuf, newdefn, m.definition_length) != 0)
504 if (CPP_OPTION (r, warn_invalid_pch))
505 cpp_error (r, DL_WARNING_SYSHDR,
506 "%s: not used because `%.*s' defined as `%s' not `%.*s'",
507 name, m.name_length, namebuf, newdefn + m.name_length,
508 m.definition_length - m.name_length,
509 namebuf + m.name_length);
516 /* Read in the list of identifiers that must not be defined.
517 Check that they really aren't. */
518 undeftab = xmalloc (m.definition_length);
519 if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
522 /* Collect identifiers from the current hash table. */
525 nl.defs = xmalloc (nl.asize * sizeof (cpp_hashnode *));
526 cpp_forall_identifiers (r, &collect_ht_nodes, &nl);
527 qsort (nl.defs, nl.n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
529 /* Loop through nl.defs and undeftab, both of which are sorted lists.
530 There should be no matches. */
532 last = undeftab + m.definition_length;
535 while (first < last && i < nl.n_defs)
537 int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
540 first += ustrlen (first) + 1;
554 cpp_errno (r, DL_ERROR, "while reading precompiled header");
560 if (undeftab != NULL)
567 /* Save all the existing macros and assertions.
568 This code assumes that there might be hundreds, but not thousands of
569 existing definitions. */
571 struct save_macro_item {
572 struct save_macro_item *next;
573 struct cpp_hashnode macs[64];
576 struct save_macro_data
578 struct save_macro_item *macros;
580 char **saved_pragmas;
583 /* Save the definition of a single macro, so that it will persist across
587 save_macros (r, h, data_p)
588 cpp_reader *r ATTRIBUTE_UNUSED;
592 struct save_macro_data *data = (struct save_macro_data *)data_p;
593 if (h->type != NT_VOID
594 && (h->flags & NODE_BUILTIN) == 0)
597 if (data->count == ARRAY_SIZE (data->macros->macs))
599 struct save_macro_item *d = data->macros;
600 data->macros = xmalloc (sizeof (struct save_macro_item));
601 data->macros->next = d;
604 save = data->macros->macs + data->count;
606 memcpy (save, h, sizeof (struct cpp_hashnode));
607 HT_STR (&save->ident) = xmemdup (HT_STR (HT_NODE (save)),
608 HT_LEN (HT_NODE (save)),
609 HT_LEN (HT_NODE (save)) + 1);
614 /* Prepare to restore the state, by saving the currently-defined
618 cpp_prepare_state (r, data)
620 struct save_macro_data **data;
622 struct save_macro_data *d = xmalloc (sizeof (struct save_macro_data));
625 d->count = ARRAY_SIZE (d->macros->macs);
626 cpp_forall_identifiers (r, save_macros, d);
627 d->saved_pragmas = _cpp_save_pragma_names (r);
631 /* Erase all the existing macros and assertions. */
634 reset_ht (r, h, unused)
635 cpp_reader *r ATTRIBUTE_UNUSED;
637 void *unused ATTRIBUTE_UNUSED;
639 if (h->type != NT_VOID
640 && (h->flags & NODE_BUILTIN) == 0)
643 memset (&h->value, 0, sizeof (h->value));
648 /* Given a precompiled header that was previously determined to be valid,
649 apply all its definitions (and undefinitions) to the current state.
650 DEPNAME is passed to deps_restore. */
653 cpp_read_state (r, name, f, data)
657 struct save_macro_data *data;
659 struct macrodef_struct m;
660 size_t defnlen = 256;
661 unsigned char *defn = xmalloc (defnlen);
662 struct lexer_state old_state;
663 struct save_macro_item *d;
665 int saved_line = r->line;
667 /* Erase all the existing hashtable entries for macros. At this
668 point, they're all from the PCH file, and their pointers won't be
670 cpp_forall_identifiers (r, reset_ht, NULL);
672 /* Restore spec_nodes, which will be full of references to the old
673 hashtable entries and so will now be invalid. */
675 struct spec_nodes *s = &r->spec_nodes;
676 s->n_defined = cpp_lookup (r, DSC("defined"));
677 s->n_true = cpp_lookup (r, DSC("true"));
678 s->n_false = cpp_lookup (r, DSC("false"));
679 s->n__VA_ARGS__ = cpp_lookup (r, DSC("__VA_ARGS__"));
682 /* Run through the carefully-saved macros, insert them. */
684 mac_count = data->count;
687 struct save_macro_item *nextd;
688 for (i = 0; i < mac_count; i++)
692 h = cpp_lookup (r, HT_STR (HT_NODE (&d->macs[i])),
693 HT_LEN (HT_NODE (&d->macs[i])));
694 h->type = d->macs[i].type;
695 h->flags = d->macs[i].flags;
696 h->value = d->macs[i].value;
697 free ((void *)HT_STR (HT_NODE (&d->macs[i])));
702 mac_count = ARRAY_SIZE (d->macs);
705 _cpp_restore_pragma_names (r, data->saved_pragmas);
709 old_state = r->state;
711 r->state.in_directive = 1;
712 r->state.prevent_expansion = 1;
713 r->state.angled_headers = 0;
715 /* Read in the identifiers that must be defined. */
720 if (fread (&m, sizeof (m), 1, f) != 1)
723 if (m.name_length == 0)
726 if (defnlen < m.definition_length + 1)
728 defnlen = m.definition_length + 256;
729 defn = xrealloc (defn, defnlen);
732 if (fread (defn, 1, m.definition_length, f) != m.definition_length)
734 defn[m.definition_length] = '\0';
736 h = cpp_lookup (r, defn, m.name_length);
738 if (h->type == NT_MACRO)
739 _cpp_free_definition (h);
740 if (m.flags & NODE_POISONED)
741 h->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
742 else if (m.name_length != m.definition_length)
744 if (cpp_push_buffer (r, defn + m.name_length,
745 m.definition_length - m.name_length,
748 if (!_cpp_create_definition (r, h))
757 r->state = old_state;
758 r->line = saved_line;
762 if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
769 cpp_errno (r, DL_ERROR, "while reading precompiled header");