Merge trunk version 195707 into gupc branch.
[official-gcc.git] / libcpp / pch.c
blob94e5d21ab7aa6d1312649e6083fe8dbeefc43cc4
1 /* Part of CPP library. (Precompiled header reading/writing.)
2 Copyright (C) 2000-2013 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 3, or (at your option) any
7 later version.
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; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
18 #include "config.h"
19 #include "system.h"
20 #include "cpplib.h"
21 #include "internal.h"
22 #include "hashtab.h"
23 #include "mkdeps.h"
25 static int write_macdef (cpp_reader *, cpp_hashnode *, void *);
26 static int save_idents (cpp_reader *, cpp_hashnode *, void *);
27 static hashval_t hashmem (const void *, size_t);
28 static hashval_t cpp_string_hash (const void *);
29 static int cpp_string_eq (const void *, const void *);
30 static int count_defs (cpp_reader *, cpp_hashnode *, void *);
31 static int comp_hashnodes (const void *, const void *);
32 static int collect_ht_nodes (cpp_reader *, cpp_hashnode *, void *);
33 static int write_defs (cpp_reader *, cpp_hashnode *, void *);
34 static int save_macros (cpp_reader *, cpp_hashnode *, void *);
35 static int _cpp_save_pushed_macros (cpp_reader *, FILE *);
36 static int _cpp_restore_pushed_macros (cpp_reader *, FILE *);
38 /* This structure represents a macro definition on disk. */
39 struct macrodef_struct
41 unsigned int definition_length;
42 unsigned short name_length;
43 unsigned short flags;
46 /* This is how we write out a macro definition.
47 Suitable for being called by cpp_forall_identifiers. */
49 static int
50 write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
52 FILE *f = (FILE *) file_p;
53 switch (hn->type)
55 case NT_VOID:
56 if (! (hn->flags & NODE_POISONED))
57 return 1;
59 case NT_MACRO:
60 if ((hn->flags & NODE_BUILTIN)
61 && (!pfile->cb.user_builtin_macro
62 || !pfile->cb.user_builtin_macro (pfile, hn)))
63 return 1;
66 struct macrodef_struct s;
67 const unsigned char *defn;
69 s.name_length = NODE_LEN (hn);
70 s.flags = hn->flags & NODE_POISONED;
72 if (hn->type == NT_MACRO)
74 defn = cpp_macro_definition (pfile, hn);
75 s.definition_length = ustrlen (defn);
77 else
79 defn = NODE_NAME (hn);
80 s.definition_length = s.name_length;
83 if (fwrite (&s, sizeof (s), 1, f) != 1
84 || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
86 cpp_errno (pfile, CPP_DL_ERROR,
87 "while writing precompiled header");
88 return 0;
91 return 1;
93 case NT_ASSERTION:
94 /* Not currently implemented. */
95 return 1;
97 default:
98 abort ();
102 /* This structure records the names of the defined macros.
103 It's also used as a callback structure for size_initial_idents
104 and save_idents. */
106 struct cpp_savedstate
108 /* A hash table of the defined identifiers. */
109 htab_t definedhash;
110 /* The size of the definitions of those identifiers (the size of
111 'definedstrs'). */
112 size_t hashsize;
113 /* Number of definitions */
114 size_t n_defs;
115 /* Array of definitions. In cpp_write_pch_deps it is used for sorting. */
116 cpp_hashnode **defs;
117 /* Space for the next definition. Definitions are null-terminated
118 strings. */
119 unsigned char *definedstrs;
122 /* Save this identifier into the state: put it in the hash table,
123 put the definition in 'definedstrs'. */
125 static int
126 save_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
128 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
130 if (hn->type != NT_VOID)
132 struct cpp_string news;
133 void **slot;
135 news.len = NODE_LEN (hn);
136 news.text= NODE_NAME (hn);
137 slot = htab_find_slot (ss->definedhash, &news, INSERT);
138 if (*slot == NULL)
140 struct cpp_string *sp;
141 unsigned char *text;
143 sp = XNEW (struct cpp_string);
144 *slot = sp;
146 sp->len = NODE_LEN (hn);
147 sp->text = text = XNEWVEC (unsigned char, NODE_LEN (hn));
148 memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
152 return 1;
155 /* Hash some memory in a generic way. */
157 static hashval_t
158 hashmem (const void *p_p, size_t sz)
160 const unsigned char *p = (const unsigned char *)p_p;
161 size_t i;
162 hashval_t h;
164 h = 0;
165 for (i = 0; i < sz; i++)
166 h = h * 67 - (*p++ - 113);
167 return h;
170 /* Hash a cpp string for the hashtable machinery. */
172 static hashval_t
173 cpp_string_hash (const void *a_p)
175 const struct cpp_string *a = (const struct cpp_string *) a_p;
176 return hashmem (a->text, a->len);
179 /* Compare two cpp strings for the hashtable machinery. */
181 static int
182 cpp_string_eq (const void *a_p, const void *b_p)
184 const struct cpp_string *a = (const struct cpp_string *) a_p;
185 const struct cpp_string *b = (const struct cpp_string *) b_p;
186 return (a->len == b->len
187 && memcmp (a->text, b->text, a->len) == 0);
190 /* Save the current definitions of the cpp_reader for dependency
191 checking purposes. When writing a precompiled header, this should
192 be called at the same point in the compilation as cpp_valid_state
193 would be called when reading the precompiled header back in. */
196 cpp_save_state (cpp_reader *r, FILE *f)
198 /* Save the list of non-void identifiers for the dependency checking. */
199 r->savedstate = XNEW (struct cpp_savedstate);
200 r->savedstate->definedhash = htab_create (100, cpp_string_hash,
201 cpp_string_eq, NULL);
202 cpp_forall_identifiers (r, save_idents, r->savedstate);
204 /* Write out the list of defined identifiers. */
205 cpp_forall_identifiers (r, write_macdef, f);
207 return 0;
210 /* Calculate the 'hashsize' field of the saved state. */
212 static int
213 count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
215 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
217 switch (hn->type)
219 case NT_MACRO:
220 if (hn->flags & NODE_BUILTIN)
221 return 1;
223 /* else fall through. */
225 case NT_VOID:
227 struct cpp_string news;
228 void **slot;
230 news.len = NODE_LEN (hn);
231 news.text = NODE_NAME (hn);
232 slot = (void **) htab_find (ss->definedhash, &news);
233 if (slot == NULL)
235 ss->hashsize += NODE_LEN (hn) + 1;
236 ss->n_defs += 1;
239 return 1;
241 case NT_ASSERTION:
242 /* Not currently implemented. */
243 return 1;
245 default:
246 abort ();
250 /* Collect the identifiers into the state's string table. */
251 static int
252 write_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
254 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
256 switch (hn->type)
258 case NT_MACRO:
259 if (hn->flags & NODE_BUILTIN)
260 return 1;
262 /* else fall through. */
264 case NT_VOID:
266 struct cpp_string news;
267 void **slot;
269 news.len = NODE_LEN (hn);
270 news.text = NODE_NAME (hn);
271 slot = (void **) htab_find (ss->definedhash, &news);
272 if (slot == NULL)
274 ss->defs[ss->n_defs] = hn;
275 ss->n_defs += 1;
278 return 1;
280 case NT_ASSERTION:
281 /* Not currently implemented. */
282 return 1;
284 default:
285 abort ();
289 /* Comparison function for qsort. The arguments point to pointers of
290 type ht_hashnode *. */
291 static int
292 comp_hashnodes (const void *px, const void *py)
294 cpp_hashnode *x = *(cpp_hashnode **) px;
295 cpp_hashnode *y = *(cpp_hashnode **) py;
296 return ustrcmp (NODE_NAME (x), NODE_NAME (y));
299 /* Write out the remainder of the dependency information. This should be
300 called after the PCH is ready to be saved. */
303 cpp_write_pch_deps (cpp_reader *r, FILE *f)
305 struct macrodef_struct z;
306 struct cpp_savedstate *const ss = r->savedstate;
307 unsigned char *definedstrs;
308 size_t i;
310 /* Collect the list of identifiers which have been seen and
311 weren't defined to anything previously. */
312 ss->hashsize = 0;
313 ss->n_defs = 0;
314 cpp_forall_identifiers (r, count_defs, ss);
316 ss->defs = XNEWVEC (cpp_hashnode *, ss->n_defs);
317 ss->n_defs = 0;
318 cpp_forall_identifiers (r, write_defs, ss);
320 /* Sort the list, copy it into a buffer, and write it out. */
321 qsort (ss->defs, ss->n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
322 definedstrs = ss->definedstrs = XNEWVEC (unsigned char, ss->hashsize);
323 for (i = 0; i < ss->n_defs; ++i)
325 size_t len = NODE_LEN (ss->defs[i]);
326 memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
327 definedstrs += len + 1;
330 memset (&z, 0, sizeof (z));
331 z.definition_length = ss->hashsize;
332 if (fwrite (&z, sizeof (z), 1, f) != 1
333 || fwrite (ss->definedstrs, ss->hashsize, 1, f) != 1)
335 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
336 return -1;
338 free (ss->definedstrs);
340 /* Free the saved state. */
341 free (ss);
342 r->savedstate = NULL;
344 /* Save the next value of __COUNTER__. */
345 if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
347 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
348 return -1;
351 return 0;
354 /* Write out the definitions of the preprocessor, in a form suitable for
355 cpp_read_state. */
358 cpp_write_pch_state (cpp_reader *r, FILE *f)
360 if (!r->deps)
361 r->deps = deps_init ();
363 if (deps_save (r->deps, f) != 0)
365 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
366 return -1;
369 if (! _cpp_save_file_entries (r, f))
371 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
372 return -1;
375 /* Save the next __COUNTER__ value. When we include a precompiled header,
376 we need to start at the offset we would have if the header had been
377 included normally. */
378 if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
380 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
381 return -1;
384 /* Write saved macros. */
385 if (! _cpp_save_pushed_macros (r, f))
387 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
388 return -1;
391 return 0;
394 static int
395 _cpp_restore_pushed_macros (cpp_reader *r, FILE *f)
397 size_t count_saved = 0;
398 size_t i;
399 struct def_pragma_macro *p;
400 size_t nlen;
401 uchar *defn;
402 size_t defnlen;
404 if (fread (&count_saved, sizeof (count_saved), 1, f) != 1)
405 return 0;
406 if (! count_saved)
407 return 1;
408 for (i = 0; i < count_saved; i++)
410 if (fread (&nlen, sizeof (nlen), 1, f) != 1)
411 return 0;
412 p = XNEW (struct def_pragma_macro);
413 memset (p, 0, sizeof (struct def_pragma_macro));
414 p->name = XNEWVAR (char, nlen + 1);
415 p->name[nlen] = 0;
416 if (fread (p->name, nlen, 1, f) != 1)
417 return 0;
418 if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
419 return 0;
420 if (defnlen == 0)
421 p->is_undef = 1;
422 else
424 defn = XNEWVEC (uchar, defnlen + 1);
425 defn[defnlen] = 0;
427 if (fread (defn, defnlen, 1, f) != 1)
428 return 0;
430 p->definition = defn;
431 if (fread (&(p->line), sizeof (source_location), 1, f) != 1)
432 return 0;
433 defnlen = 0;
434 if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
435 return 0;
436 p->syshdr = ((defnlen & 1) != 0 ? 1 : 0);
437 p->used = ((defnlen & 2) != 0 ? 1 : 0);
440 p->next = r->pushed_macros;
441 r->pushed_macros = p;
443 return 1;
446 static int
447 _cpp_save_pushed_macros (cpp_reader *r, FILE *f)
449 size_t count_saved = 0;
450 size_t i;
451 struct def_pragma_macro *p,**pp;
452 size_t defnlen;
454 /* Get count. */
455 p = r->pushed_macros;
456 while (p != NULL)
458 count_saved++;
459 p = p->next;
461 if (fwrite (&count_saved, sizeof (count_saved), 1, f) != 1)
462 return 0;
463 if (!count_saved)
464 return 1;
466 pp = (struct def_pragma_macro **) alloca (sizeof (struct def_pragma_macro *)
467 * count_saved);
468 /* Store them in reverse order. */
469 p = r->pushed_macros;
470 i = count_saved;
471 while (p != NULL)
473 --i;
474 pp[i] = p;
475 p = p->next;
477 for (i = 0; i < count_saved; i++)
479 defnlen = strlen (pp[i]->name);
480 if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
481 || fwrite (pp[i]->name, defnlen, 1, f) != 1)
482 return 0;
483 if (pp[i]->is_undef)
485 defnlen = 0;
486 if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1)
487 return 0;
489 else
491 defnlen = ustrlen (pp[i]->definition);
492 if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
493 || fwrite (pp[i]->definition, defnlen, 1, f) != 1)
494 return 0;
495 if (fwrite (&(pp[i]->line), sizeof (source_location), 1, f) != 1)
496 return 0;
497 defnlen = 0;
498 defnlen |= (pp[i]->syshdr != 0 ? 1 : 0);
499 defnlen |= (pp[i]->used != 0 ? 2 : 0);
500 if (fwrite (&defnlen, sizeof (defnlen), 1, f) != 1)
501 return 0;
504 return 1;
508 /* Data structure to transform hash table nodes into a sorted list */
510 struct ht_node_list
512 /* Array of nodes */
513 cpp_hashnode **defs;
514 /* Number of nodes in the array */
515 size_t n_defs;
516 /* Size of the allocated array */
517 size_t asize;
520 /* Callback for collecting identifiers from hash table */
522 static int
523 collect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
524 void *nl_p)
526 struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
528 if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
530 if (nl->n_defs == nl->asize)
532 nl->asize *= 2;
533 nl->defs = XRESIZEVEC (cpp_hashnode *, nl->defs, nl->asize);
536 nl->defs[nl->n_defs] = hn;
537 ++nl->n_defs;
539 return 1;
543 /* Return nonzero if FD is a precompiled header which is consistent
544 with the preprocessor's current definitions. It will be consistent
545 when:
547 - anything that was defined just before the PCH was generated
548 is defined the same way now; and
549 - anything that was not defined then, but is defined now, was not
550 used by the PCH.
552 NAME is used to print warnings if `warn_invalid_pch' is set in the
553 reader's flags.
557 cpp_valid_state (cpp_reader *r, const char *name, int fd)
559 struct macrodef_struct m;
560 size_t namebufsz = 256;
561 unsigned char *namebuf = XNEWVEC (unsigned char, namebufsz);
562 unsigned char *undeftab = NULL;
563 struct ht_node_list nl = { 0, 0, 0 };
564 unsigned char *first, *last;
565 unsigned int i;
566 unsigned int counter;
568 /* Read in the list of identifiers that must be defined
569 Check that they are defined in the same way. */
570 for (;;)
572 cpp_hashnode *h;
573 const unsigned char *newdefn;
575 if (read (fd, &m, sizeof (m)) != sizeof (m))
576 goto error;
578 if (m.name_length == 0)
579 break;
581 /* If this file is already preprocessed, there won't be any
582 macros defined, and that's OK. */
583 if (CPP_OPTION (r, preprocessed))
585 if (lseek (fd, m.definition_length, SEEK_CUR) == -1)
586 goto error;
587 continue;
590 if (m.definition_length > namebufsz)
592 free (namebuf);
593 namebufsz = m.definition_length + 256;
594 namebuf = XNEWVEC (unsigned char, namebufsz);
597 if ((size_t)read (fd, namebuf, m.definition_length)
598 != m.definition_length)
599 goto error;
601 h = cpp_lookup (r, namebuf, m.name_length);
602 if (m.flags & NODE_POISONED
603 || h->flags & NODE_POISONED)
605 if (CPP_OPTION (r, warn_invalid_pch))
606 cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
607 "%s: not used because `%.*s' is poisoned",
608 name, m.name_length, namebuf);
609 goto fail;
612 if (h->type != NT_MACRO)
614 /* It's ok if __GCC_HAVE_DWARF2_CFI_ASM becomes undefined,
615 as in, when the PCH file is created with -g and we're
616 attempting to use it without -g. Restoring the PCH file
617 is supposed to bring in this definition *and* enable the
618 generation of call frame information, so that precompiled
619 definitions that take this macro into accout, to decide
620 what asm to emit, won't issue .cfi directives when the
621 compiler doesn't. */
622 if (!(h->flags & NODE_USED)
623 && m.name_length == sizeof ("__GCC_HAVE_DWARF2_CFI_ASM") - 1
624 && !memcmp (namebuf, "__GCC_HAVE_DWARF2_CFI_ASM", m.name_length))
625 continue;
627 if (CPP_OPTION (r, warn_invalid_pch))
628 cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
629 "%s: not used because `%.*s' not defined",
630 name, m.name_length, namebuf);
631 goto fail;
634 newdefn = cpp_macro_definition (r, h);
636 if (m.definition_length != ustrlen (newdefn)
637 || memcmp (namebuf, newdefn, m.definition_length) != 0)
639 if (CPP_OPTION (r, warn_invalid_pch))
640 cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
641 "%s: not used because `%.*s' defined as `%s' not `%.*s'",
642 name, m.name_length, namebuf, newdefn + m.name_length,
643 m.definition_length - m.name_length,
644 namebuf + m.name_length);
645 goto fail;
648 free (namebuf);
649 namebuf = NULL;
651 /* Read in the list of identifiers that must not be defined.
652 Check that they really aren't. */
653 undeftab = XNEWVEC (unsigned char, m.definition_length);
654 if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
655 goto error;
657 /* Collect identifiers from the current hash table. */
658 nl.n_defs = 0;
659 nl.asize = 10;
660 nl.defs = XNEWVEC (cpp_hashnode *, nl.asize);
661 cpp_forall_identifiers (r, &collect_ht_nodes, &nl);
662 qsort (nl.defs, nl.n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
664 /* Loop through nl.defs and undeftab, both of which are sorted lists.
665 There should be no matches. */
666 first = undeftab;
667 last = undeftab + m.definition_length;
668 i = 0;
670 while (first < last && i < nl.n_defs)
672 int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
674 if (cmp < 0)
675 first += ustrlen (first) + 1;
676 else if (cmp > 0)
677 ++i;
678 else
680 if (CPP_OPTION (r, warn_invalid_pch))
681 cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
682 "%s: not used because `%s' is defined",
683 name, first);
684 goto fail;
688 free(nl.defs);
689 nl.defs = NULL;
690 free (undeftab);
691 undeftab = NULL;
693 /* Read in the next value of __COUNTER__.
694 Check that (a) __COUNTER__ was not used in the pch or (b) __COUNTER__
695 has not been used in this translation unit. */
696 if (read (fd, &counter, sizeof (counter)) != sizeof (counter))
697 goto error;
698 if (counter && r->counter)
700 if (CPP_OPTION (r, warn_invalid_pch))
701 cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
702 "%s: not used because `__COUNTER__' is invalid",
703 name);
704 goto fail;
707 /* We win! */
708 return 0;
710 error:
711 cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
713 fail:
714 free (namebuf);
715 free (undeftab);
716 free (nl.defs);
717 return 1;
720 /* Save all the existing macros. */
722 struct save_macro_data
724 uchar **defns;
725 size_t count;
726 size_t array_size;
727 char **saved_pragmas;
730 /* Save the definition of a single macro, so that it will persist
731 across a PCH restore. Because macro data is in GCed memory, which
732 will be blown away by PCH, it must be temporarily copied to
733 malloced memory. (The macros will refer to identifier nodes which
734 are also GCed and so on, so the copying is done by turning them
735 into self-contained strings.) The assumption is that most macro
736 definitions will come from the PCH file, not from the compilation
737 before the PCH file is loaded, so it doesn't matter that this is
738 a little expensive.
740 It would reduce the cost even further if macros defined in the PCH
741 file were not saved in this way, but this is not done (yet), except
742 for builtins, and for #assert by default. */
744 static int
745 save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
747 struct save_macro_data *data = (struct save_macro_data *)data_p;
749 if ((h->flags & NODE_BUILTIN)
750 && h->type == NT_MACRO
751 && r->cb.user_builtin_macro)
752 r->cb.user_builtin_macro (r, h);
754 if (h->type != NT_VOID
755 && (h->flags & NODE_BUILTIN) == 0)
757 if (data->count == data->array_size)
759 data->array_size *= 2;
760 data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size));
763 switch (h->type)
765 case NT_ASSERTION:
766 /* Not currently implemented. */
767 return 1;
769 case NT_MACRO:
771 const uchar * defn = cpp_macro_definition (r, h);
772 size_t defnlen = ustrlen (defn);
774 data->defns[data->count] = (uchar *) xmemdup (defn, defnlen,
775 defnlen + 2);
776 data->defns[data->count][defnlen] = '\n';
778 break;
780 default:
781 abort ();
783 data->count++;
785 return 1;
788 /* Prepare to restore the state, by saving the currently-defined
789 macros in 'data'. */
791 void
792 cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
794 struct save_macro_data *d = XNEW (struct save_macro_data);
796 d->array_size = 512;
797 d->defns = XNEWVEC (uchar *, d->array_size);
798 d->count = 0;
799 cpp_forall_identifiers (r, save_macros, d);
800 d->saved_pragmas = _cpp_save_pragma_names (r);
801 *data = d;
804 /* Given a precompiled header that was previously determined to be valid,
805 apply all its definitions (and undefinitions) to the current state.
806 DEPNAME is passed to deps_restore. */
809 cpp_read_state (cpp_reader *r, const char *name, FILE *f,
810 struct save_macro_data *data)
812 size_t i;
813 struct lexer_state old_state;
814 unsigned int counter;
816 /* Restore spec_nodes, which will be full of references to the old
817 hashtable entries and so will now be invalid. */
819 struct spec_nodes *s = &r->spec_nodes;
820 s->n_defined = cpp_lookup (r, DSC("defined"));
821 s->n_true = cpp_lookup (r, DSC("true"));
822 s->n_false = cpp_lookup (r, DSC("false"));
823 s->n__VA_ARGS__ = cpp_lookup (r, DSC("__VA_ARGS__"));
826 old_state = r->state;
827 r->state.in_directive = 1;
828 r->state.prevent_expansion = 1;
829 r->state.angled_headers = 0;
831 /* Run through the carefully-saved macros, insert them. */
832 for (i = 0; i < data->count; i++)
834 cpp_hashnode *h;
835 size_t namelen;
836 uchar *defn;
838 namelen = ustrcspn (data->defns[i], "( \n");
839 h = cpp_lookup (r, data->defns[i], namelen);
840 defn = data->defns[i] + namelen;
842 /* The PCH file is valid, so we know that if there is a definition
843 from the PCH file it must be the same as the one we had
844 originally, and so do not need to restore it. */
845 if (h->type == NT_VOID)
847 if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
848 != NULL)
850 _cpp_clean_line (r);
851 if (!_cpp_create_definition (r, h))
852 abort ();
853 _cpp_pop_buffer (r);
855 else
856 abort ();
859 free (data->defns[i]);
861 r->state = old_state;
863 _cpp_restore_pragma_names (r, data->saved_pragmas);
865 free (data);
867 if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
868 != 0)
869 goto error;
871 if (! _cpp_read_file_entries (r, f))
872 goto error;
874 if (fread (&counter, sizeof (counter), 1, f) != 1)
875 goto error;
877 if (!r->counter)
878 r->counter = counter;
880 /* Read pushed macros. */
881 if (! _cpp_restore_pushed_macros (r, f))
882 goto error;
883 return 0;
885 error:
886 cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
887 return -1;