PR c++/84454 - ICE with pack expansion in signature.
[official-gcc.git] / libcpp / pch.c
blob997c4da5faaf1be48cd7185106141196799ab0ee
1 /* Part of CPP library. (Precompiled header reading/writing.)
2 Copyright (C) 2000-2018 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;
58 /* XXX Really fallthru? */
59 /* FALLTHRU */
61 case NT_MACRO:
62 if ((hn->flags & NODE_BUILTIN)
63 && (!pfile->cb.user_builtin_macro
64 || !pfile->cb.user_builtin_macro (pfile, hn)))
65 return 1;
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);
79 else
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, CPP_DL_ERROR,
89 "while writing precompiled header");
90 return 0;
93 return 1;
95 case NT_ASSERTION:
96 /* Not currently implemented. */
97 return 1;
99 default:
100 abort ();
104 /* This structure records the names of the defined macros.
105 It's also used as a callback structure for size_initial_idents
106 and save_idents. */
108 struct cpp_savedstate
110 /* A hash table of the defined identifiers. */
111 htab_t definedhash;
112 /* The size of the definitions of those identifiers (the size of
113 'definedstrs'). */
114 size_t hashsize;
115 /* Number of definitions */
116 size_t n_defs;
117 /* Array of definitions. In cpp_write_pch_deps it is used for sorting. */
118 cpp_hashnode **defs;
119 /* Space for the next definition. Definitions are null-terminated
120 strings. */
121 unsigned char *definedstrs;
124 /* Save this identifier into the state: put it in the hash table,
125 put the definition in 'definedstrs'. */
127 static int
128 save_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
130 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
132 if (hn->type != NT_VOID)
134 struct cpp_string news;
135 void **slot;
137 news.len = NODE_LEN (hn);
138 news.text= NODE_NAME (hn);
139 slot = htab_find_slot (ss->definedhash, &news, INSERT);
140 if (*slot == NULL)
142 struct cpp_string *sp;
143 unsigned char *text;
145 sp = XNEW (struct cpp_string);
146 *slot = sp;
148 sp->len = NODE_LEN (hn);
149 sp->text = text = XNEWVEC (unsigned char, NODE_LEN (hn));
150 memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
154 return 1;
157 /* Hash some memory in a generic way. */
159 static hashval_t
160 hashmem (const void *p_p, size_t sz)
162 const unsigned char *p = (const unsigned char *)p_p;
163 size_t i;
164 hashval_t h;
166 h = 0;
167 for (i = 0; i < sz; i++)
168 h = h * 67 - (*p++ - 113);
169 return h;
172 /* Hash a cpp string for the hashtable machinery. */
174 static hashval_t
175 cpp_string_hash (const void *a_p)
177 const struct cpp_string *a = (const struct cpp_string *) a_p;
178 return hashmem (a->text, a->len);
181 /* Compare two cpp strings for the hashtable machinery. */
183 static int
184 cpp_string_eq (const void *a_p, const void *b_p)
186 const struct cpp_string *a = (const struct cpp_string *) a_p;
187 const struct cpp_string *b = (const struct cpp_string *) b_p;
188 return (a->len == b->len
189 && memcmp (a->text, b->text, a->len) == 0);
192 /* Free memory associated with cpp_string. */
194 static void
195 cpp_string_free (void *a_p)
197 struct cpp_string *a = (struct cpp_string *) a_p;
198 free ((void *) a->text);
199 free (a);
202 /* Save the current definitions of the cpp_reader for dependency
203 checking purposes. When writing a precompiled header, this should
204 be called at the same point in the compilation as cpp_valid_state
205 would be called when reading the precompiled header back in. */
208 cpp_save_state (cpp_reader *r, FILE *f)
210 /* Save the list of non-void identifiers for the dependency checking. */
211 r->savedstate = XNEW (struct cpp_savedstate);
212 r->savedstate->definedhash = htab_create (100, cpp_string_hash,
213 cpp_string_eq, cpp_string_free);
214 cpp_forall_identifiers (r, save_idents, r->savedstate);
216 /* Write out the list of defined identifiers. */
217 cpp_forall_identifiers (r, write_macdef, f);
219 return 0;
222 /* Calculate the 'hashsize' field of the saved state. */
224 static int
225 count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
227 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
229 switch (hn->type)
231 case NT_MACRO:
232 if (hn->flags & NODE_BUILTIN)
233 return 1;
235 /* fall through. */
237 case NT_VOID:
239 struct cpp_string news;
240 void **slot;
242 news.len = NODE_LEN (hn);
243 news.text = NODE_NAME (hn);
244 slot = (void **) htab_find (ss->definedhash, &news);
245 if (slot == NULL)
247 ss->hashsize += NODE_LEN (hn) + 1;
248 ss->n_defs += 1;
251 return 1;
253 case NT_ASSERTION:
254 /* Not currently implemented. */
255 return 1;
257 default:
258 abort ();
262 /* Collect the identifiers into the state's string table. */
263 static int
264 write_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
266 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
268 switch (hn->type)
270 case NT_MACRO:
271 if (hn->flags & NODE_BUILTIN)
272 return 1;
274 /* fall through. */
276 case NT_VOID:
278 struct cpp_string news;
279 void **slot;
281 news.len = NODE_LEN (hn);
282 news.text = NODE_NAME (hn);
283 slot = (void **) htab_find (ss->definedhash, &news);
284 if (slot == NULL)
286 ss->defs[ss->n_defs] = hn;
287 ss->n_defs += 1;
290 return 1;
292 case NT_ASSERTION:
293 /* Not currently implemented. */
294 return 1;
296 default:
297 abort ();
301 /* Comparison function for qsort. The arguments point to pointers of
302 type ht_hashnode *. */
303 static int
304 comp_hashnodes (const void *px, const void *py)
306 cpp_hashnode *x = *(cpp_hashnode **) px;
307 cpp_hashnode *y = *(cpp_hashnode **) py;
308 return ustrcmp (NODE_NAME (x), NODE_NAME (y));
311 /* Write out the remainder of the dependency information. This should be
312 called after the PCH is ready to be saved. */
315 cpp_write_pch_deps (cpp_reader *r, FILE *f)
317 struct macrodef_struct z;
318 struct cpp_savedstate *const ss = r->savedstate;
319 unsigned char *definedstrs;
320 size_t i;
322 /* Collect the list of identifiers which have been seen and
323 weren't defined to anything previously. */
324 ss->hashsize = 0;
325 ss->n_defs = 0;
326 cpp_forall_identifiers (r, count_defs, ss);
328 ss->defs = XNEWVEC (cpp_hashnode *, ss->n_defs);
329 ss->n_defs = 0;
330 cpp_forall_identifiers (r, write_defs, ss);
332 /* Sort the list, copy it into a buffer, and write it out. */
333 qsort (ss->defs, ss->n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
334 definedstrs = ss->definedstrs = XNEWVEC (unsigned char, ss->hashsize);
335 for (i = 0; i < ss->n_defs; ++i)
337 size_t len = NODE_LEN (ss->defs[i]);
338 memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
339 definedstrs += len + 1;
342 memset (&z, 0, sizeof (z));
343 z.definition_length = ss->hashsize;
344 if (fwrite (&z, sizeof (z), 1, f) != 1
345 || fwrite (ss->definedstrs, ss->hashsize, 1, f) != 1)
347 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
348 return -1;
350 free (ss->definedstrs);
351 free (ss->defs);
352 htab_delete (ss->definedhash);
354 /* Free the saved state. */
355 free (ss);
356 r->savedstate = NULL;
358 /* Save the next value of __COUNTER__. */
359 if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
361 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
362 return -1;
365 return 0;
368 /* Write out the definitions of the preprocessor, in a form suitable for
369 cpp_read_state. */
372 cpp_write_pch_state (cpp_reader *r, FILE *f)
374 if (!r->deps)
375 r->deps = deps_init ();
377 if (deps_save (r->deps, f) != 0)
379 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
380 return -1;
383 if (! _cpp_save_file_entries (r, f))
385 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
386 return -1;
389 /* Save the next __COUNTER__ value. When we include a precompiled header,
390 we need to start at the offset we would have if the header had been
391 included normally. */
392 if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
394 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
395 return -1;
398 /* Write saved macros. */
399 if (! _cpp_save_pushed_macros (r, f))
401 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
402 return -1;
405 return 0;
408 static int
409 _cpp_restore_pushed_macros (cpp_reader *r, FILE *f)
411 size_t count_saved = 0;
412 size_t i;
413 struct def_pragma_macro *p;
414 size_t nlen;
415 uchar *defn;
416 size_t defnlen;
418 if (fread (&count_saved, sizeof (count_saved), 1, f) != 1)
419 return 0;
420 if (! count_saved)
421 return 1;
422 for (i = 0; i < count_saved; i++)
424 if (fread (&nlen, sizeof (nlen), 1, f) != 1)
425 return 0;
426 p = XNEW (struct def_pragma_macro);
427 memset (p, 0, sizeof (struct def_pragma_macro));
428 p->name = XNEWVAR (char, nlen + 1);
429 p->name[nlen] = 0;
430 if (fread (p->name, nlen, 1, f) != 1)
431 return 0;
432 if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
433 return 0;
434 if (defnlen == 0)
435 p->is_undef = 1;
436 else
438 defn = XNEWVEC (uchar, defnlen + 1);
439 defn[defnlen] = 0;
441 if (fread (defn, defnlen, 1, f) != 1)
442 return 0;
444 p->definition = defn;
445 if (fread (&(p->line), sizeof (source_location), 1, f) != 1)
446 return 0;
447 defnlen = 0;
448 if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
449 return 0;
450 p->syshdr = ((defnlen & 1) != 0 ? 1 : 0);
451 p->used = ((defnlen & 2) != 0 ? 1 : 0);
454 p->next = r->pushed_macros;
455 r->pushed_macros = p;
457 return 1;
460 static int
461 _cpp_save_pushed_macros (cpp_reader *r, FILE *f)
463 size_t count_saved = 0;
464 size_t i;
465 struct def_pragma_macro *p,**pp;
466 size_t defnlen;
468 /* Get count. */
469 p = r->pushed_macros;
470 while (p != NULL)
472 count_saved++;
473 p = p->next;
475 if (fwrite (&count_saved, sizeof (count_saved), 1, f) != 1)
476 return 0;
477 if (!count_saved)
478 return 1;
480 pp = (struct def_pragma_macro **) alloca (sizeof (struct def_pragma_macro *)
481 * count_saved);
482 /* Store them in reverse order. */
483 p = r->pushed_macros;
484 i = count_saved;
485 while (p != NULL)
487 --i;
488 pp[i] = p;
489 p = p->next;
491 for (i = 0; i < count_saved; i++)
493 defnlen = strlen (pp[i]->name);
494 if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
495 || fwrite (pp[i]->name, defnlen, 1, f) != 1)
496 return 0;
497 if (pp[i]->is_undef)
499 defnlen = 0;
500 if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1)
501 return 0;
503 else
505 defnlen = ustrlen (pp[i]->definition);
506 if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
507 || fwrite (pp[i]->definition, defnlen, 1, f) != 1)
508 return 0;
509 if (fwrite (&(pp[i]->line), sizeof (source_location), 1, f) != 1)
510 return 0;
511 defnlen = 0;
512 defnlen |= (pp[i]->syshdr != 0 ? 1 : 0);
513 defnlen |= (pp[i]->used != 0 ? 2 : 0);
514 if (fwrite (&defnlen, sizeof (defnlen), 1, f) != 1)
515 return 0;
518 return 1;
522 /* Data structure to transform hash table nodes into a sorted list */
524 struct ht_node_list
526 /* Array of nodes */
527 cpp_hashnode **defs;
528 /* Number of nodes in the array */
529 size_t n_defs;
530 /* Size of the allocated array */
531 size_t asize;
534 /* Callback for collecting identifiers from hash table */
536 static int
537 collect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
538 void *nl_p)
540 struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
542 if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
544 if (nl->n_defs == nl->asize)
546 nl->asize *= 2;
547 nl->defs = XRESIZEVEC (cpp_hashnode *, nl->defs, nl->asize);
550 nl->defs[nl->n_defs] = hn;
551 ++nl->n_defs;
553 return 1;
557 /* Return nonzero if FD is a precompiled header which is consistent
558 with the preprocessor's current definitions. It will be consistent
559 when:
561 - anything that was defined just before the PCH was generated
562 is defined the same way now; and
563 - anything that was not defined then, but is defined now, was not
564 used by the PCH.
566 NAME is used to print warnings if `warn_invalid_pch' is set in the
567 reader's flags.
571 cpp_valid_state (cpp_reader *r, const char *name, int fd)
573 struct macrodef_struct m;
574 size_t namebufsz = 256;
575 unsigned char *namebuf = XNEWVEC (unsigned char, namebufsz);
576 unsigned char *undeftab = NULL;
577 struct ht_node_list nl = { 0, 0, 0 };
578 unsigned char *first, *last;
579 unsigned int i;
580 unsigned int counter;
582 /* Read in the list of identifiers that must be defined
583 Check that they are defined in the same way. */
584 for (;;)
586 cpp_hashnode *h;
587 const unsigned char *newdefn;
589 if (read (fd, &m, sizeof (m)) != sizeof (m))
590 goto error;
592 if (m.name_length == 0)
593 break;
595 /* If this file is already preprocessed, there won't be any
596 macros defined, and that's OK. */
597 if (CPP_OPTION (r, preprocessed))
599 if (lseek (fd, m.definition_length, SEEK_CUR) == -1)
600 goto error;
601 continue;
604 if (m.definition_length > namebufsz)
606 free (namebuf);
607 namebufsz = m.definition_length + 256;
608 namebuf = XNEWVEC (unsigned char, namebufsz);
611 if ((size_t)read (fd, namebuf, m.definition_length)
612 != m.definition_length)
613 goto error;
615 h = cpp_lookup (r, namebuf, m.name_length);
616 if (m.flags & NODE_POISONED
617 || h->flags & NODE_POISONED)
619 if (CPP_OPTION (r, warn_invalid_pch))
620 cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
621 "%s: not used because `%.*s' is poisoned",
622 name, m.name_length, namebuf);
623 goto fail;
626 if (h->type != NT_MACRO)
628 /* It's ok if __GCC_HAVE_DWARF2_CFI_ASM becomes undefined,
629 as in, when the PCH file is created with -g and we're
630 attempting to use it without -g. Restoring the PCH file
631 is supposed to bring in this definition *and* enable the
632 generation of call frame information, so that precompiled
633 definitions that take this macro into account, to decide
634 what asm to emit, won't issue .cfi directives when the
635 compiler doesn't. */
636 if (!(h->flags & NODE_USED)
637 && m.name_length == sizeof ("__GCC_HAVE_DWARF2_CFI_ASM") - 1
638 && !memcmp (namebuf, "__GCC_HAVE_DWARF2_CFI_ASM", m.name_length))
639 continue;
641 if (CPP_OPTION (r, warn_invalid_pch))
642 cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
643 "%s: not used because `%.*s' not defined",
644 name, m.name_length, namebuf);
645 goto fail;
648 newdefn = cpp_macro_definition (r, h);
650 if (m.definition_length != ustrlen (newdefn)
651 || memcmp (namebuf, newdefn, m.definition_length) != 0)
653 if (CPP_OPTION (r, warn_invalid_pch))
654 cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
655 "%s: not used because `%.*s' defined as `%s' not `%.*s'",
656 name, m.name_length, namebuf, newdefn + m.name_length,
657 m.definition_length - m.name_length,
658 namebuf + m.name_length);
659 goto fail;
662 free (namebuf);
663 namebuf = NULL;
665 /* Read in the list of identifiers that must not be defined.
666 Check that they really aren't. */
667 undeftab = XNEWVEC (unsigned char, m.definition_length);
668 if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
669 goto error;
671 /* Collect identifiers from the current hash table. */
672 nl.n_defs = 0;
673 nl.asize = 10;
674 nl.defs = XNEWVEC (cpp_hashnode *, nl.asize);
675 cpp_forall_identifiers (r, &collect_ht_nodes, &nl);
676 qsort (nl.defs, nl.n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
678 /* Loop through nl.defs and undeftab, both of which are sorted lists.
679 There should be no matches. */
680 first = undeftab;
681 last = undeftab + m.definition_length;
682 i = 0;
684 while (first < last && i < nl.n_defs)
686 int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
688 if (cmp < 0)
689 first += ustrlen (first) + 1;
690 else if (cmp > 0)
691 ++i;
692 else
694 if (CPP_OPTION (r, warn_invalid_pch))
695 cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
696 "%s: not used because `%s' is defined",
697 name, first);
698 goto fail;
702 free(nl.defs);
703 nl.defs = NULL;
704 free (undeftab);
705 undeftab = NULL;
707 /* Read in the next value of __COUNTER__.
708 Check that (a) __COUNTER__ was not used in the pch or (b) __COUNTER__
709 has not been used in this translation unit. */
710 if (read (fd, &counter, sizeof (counter)) != sizeof (counter))
711 goto error;
712 if (counter && r->counter)
714 if (CPP_OPTION (r, warn_invalid_pch))
715 cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
716 "%s: not used because `__COUNTER__' is invalid",
717 name);
718 goto fail;
721 /* We win! */
722 return 0;
724 error:
725 cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
727 fail:
728 free (namebuf);
729 free (undeftab);
730 free (nl.defs);
731 return 1;
734 /* Save all the existing macros. */
736 struct save_macro_data
738 uchar **defns;
739 size_t count;
740 size_t array_size;
741 char **saved_pragmas;
744 /* Save the definition of a single macro, so that it will persist
745 across a PCH restore. Because macro data is in GCed memory, which
746 will be blown away by PCH, it must be temporarily copied to
747 malloced memory. (The macros will refer to identifier nodes which
748 are also GCed and so on, so the copying is done by turning them
749 into self-contained strings.) The assumption is that most macro
750 definitions will come from the PCH file, not from the compilation
751 before the PCH file is loaded, so it doesn't matter that this is
752 a little expensive.
754 It would reduce the cost even further if macros defined in the PCH
755 file were not saved in this way, but this is not done (yet), except
756 for builtins, and for #assert by default. */
758 static int
759 save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
761 struct save_macro_data *data = (struct save_macro_data *)data_p;
763 if ((h->flags & NODE_BUILTIN)
764 && h->type == NT_MACRO
765 && r->cb.user_builtin_macro)
766 r->cb.user_builtin_macro (r, h);
768 if (h->type != NT_VOID
769 && (h->flags & NODE_BUILTIN) == 0)
771 if (data->count == data->array_size)
773 data->array_size *= 2;
774 data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size));
777 switch (h->type)
779 case NT_ASSERTION:
780 /* Not currently implemented. */
781 return 1;
783 case NT_MACRO:
785 const uchar * defn = cpp_macro_definition (r, h);
786 size_t defnlen = ustrlen (defn);
788 data->defns[data->count] = (uchar *) xmemdup (defn, defnlen,
789 defnlen + 2);
790 data->defns[data->count][defnlen] = '\n';
792 break;
794 default:
795 abort ();
797 data->count++;
799 return 1;
802 /* Prepare to restore the state, by saving the currently-defined
803 macros in 'data'. */
805 void
806 cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
808 struct save_macro_data *d = XNEW (struct save_macro_data);
810 d->array_size = 512;
811 d->defns = XNEWVEC (uchar *, d->array_size);
812 d->count = 0;
813 cpp_forall_identifiers (r, save_macros, d);
814 d->saved_pragmas = _cpp_save_pragma_names (r);
815 *data = d;
818 /* Given a precompiled header that was previously determined to be valid,
819 apply all its definitions (and undefinitions) to the current state.
820 DEPNAME is passed to deps_restore. */
823 cpp_read_state (cpp_reader *r, const char *name, FILE *f,
824 struct save_macro_data *data)
826 size_t i;
827 struct lexer_state old_state;
828 unsigned int counter;
830 /* Restore spec_nodes, which will be full of references to the old
831 hashtable entries and so will now be invalid. */
833 struct spec_nodes *s = &r->spec_nodes;
834 s->n_defined = cpp_lookup (r, DSC("defined"));
835 s->n_true = cpp_lookup (r, DSC("true"));
836 s->n_false = cpp_lookup (r, DSC("false"));
837 s->n__VA_ARGS__ = cpp_lookup (r, DSC("__VA_ARGS__"));
838 s->n__VA_OPT__ = cpp_lookup (r, DSC("__VA_OPT__"));
839 s->n__has_include__ = cpp_lookup (r, DSC("__has_include__"));
840 s->n__has_include_next__ = cpp_lookup (r, DSC("__has_include_next__"));
843 old_state = r->state;
844 r->state.in_directive = 1;
845 r->state.prevent_expansion = 1;
846 r->state.angled_headers = 0;
848 /* Run through the carefully-saved macros, insert them. */
849 for (i = 0; i < data->count; i++)
851 cpp_hashnode *h;
852 size_t namelen;
853 uchar *defn;
855 namelen = ustrcspn (data->defns[i], "( \n");
856 h = cpp_lookup (r, data->defns[i], namelen);
857 defn = data->defns[i] + namelen;
859 /* The PCH file is valid, so we know that if there is a definition
860 from the PCH file it must be the same as the one we had
861 originally, and so do not need to restore it. */
862 if (h->type == NT_VOID)
864 if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
865 != NULL)
867 _cpp_clean_line (r);
868 if (!_cpp_create_definition (r, h))
869 abort ();
870 _cpp_pop_buffer (r);
872 else
873 abort ();
876 free (data->defns[i]);
878 r->state = old_state;
880 _cpp_restore_pragma_names (r, data->saved_pragmas);
882 free (data);
884 if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
885 != 0)
886 goto error;
888 if (! _cpp_read_file_entries (r, f))
889 goto error;
891 if (fread (&counter, sizeof (counter), 1, f) != 1)
892 goto error;
894 if (!r->counter)
895 r->counter = counter;
897 /* Read pushed macros. */
898 if (! _cpp_restore_pushed_macros (r, f))
899 goto error;
900 return 0;
902 error:
903 cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
904 return -1;