Update copyright years in libcpp.
[gcc.git] / libcpp / pch.c
1 /* Part of CPP library. (Precompiled header reading/writing.)
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
17
18 #include "config.h"
19 #include "system.h"
20 #include "cpplib.h"
21 #include "internal.h"
22 #include "hashtab.h"
23 #include "mkdeps.h"
24
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 *);
37
38 /* This structure represents a macro definition on disk. */
39 struct macrodef_struct
40 {
41 unsigned int definition_length;
42 unsigned short name_length;
43 unsigned short flags;
44 };
45
46 /* This is how we write out a macro definition.
47 Suitable for being called by cpp_forall_identifiers. */
48
49 static int
50 write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
51 {
52 FILE *f = (FILE *) file_p;
53 switch (hn->type)
54 {
55 case NT_VOID:
56 if (! (hn->flags & NODE_POISONED))
57 return 1;
58
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;
64
65 {
66 struct macrodef_struct s;
67 const unsigned char *defn;
68
69 s.name_length = NODE_LEN (hn);
70 s.flags = hn->flags & NODE_POISONED;
71
72 if (hn->type == NT_MACRO)
73 {
74 defn = cpp_macro_definition (pfile, hn);
75 s.definition_length = ustrlen (defn);
76 }
77 else
78 {
79 defn = NODE_NAME (hn);
80 s.definition_length = s.name_length;
81 }
82
83 if (fwrite (&s, sizeof (s), 1, f) != 1
84 || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
85 {
86 cpp_errno (pfile, CPP_DL_ERROR,
87 "while writing precompiled header");
88 return 0;
89 }
90 }
91 return 1;
92
93 case NT_ASSERTION:
94 /* Not currently implemented. */
95 return 1;
96
97 default:
98 abort ();
99 }
100 }
101
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. */
105
106 struct cpp_savedstate
107 {
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;
120 };
121
122 /* Save this identifier into the state: put it in the hash table,
123 put the definition in 'definedstrs'. */
124
125 static int
126 save_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
127 {
128 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
129
130 if (hn->type != NT_VOID)
131 {
132 struct cpp_string news;
133 void **slot;
134
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)
139 {
140 struct cpp_string *sp;
141 unsigned char *text;
142
143 sp = XNEW (struct cpp_string);
144 *slot = sp;
145
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));
149 }
150 }
151
152 return 1;
153 }
154
155 /* Hash some memory in a generic way. */
156
157 static hashval_t
158 hashmem (const void *p_p, size_t sz)
159 {
160 const unsigned char *p = (const unsigned char *)p_p;
161 size_t i;
162 hashval_t h;
163
164 h = 0;
165 for (i = 0; i < sz; i++)
166 h = h * 67 - (*p++ - 113);
167 return h;
168 }
169
170 /* Hash a cpp string for the hashtable machinery. */
171
172 static hashval_t
173 cpp_string_hash (const void *a_p)
174 {
175 const struct cpp_string *a = (const struct cpp_string *) a_p;
176 return hashmem (a->text, a->len);
177 }
178
179 /* Compare two cpp strings for the hashtable machinery. */
180
181 static int
182 cpp_string_eq (const void *a_p, const void *b_p)
183 {
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);
188 }
189
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. */
194
195 int
196 cpp_save_state (cpp_reader *r, FILE *f)
197 {
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);
203
204 /* Write out the list of defined identifiers. */
205 cpp_forall_identifiers (r, write_macdef, f);
206
207 return 0;
208 }
209
210 /* Calculate the 'hashsize' field of the saved state. */
211
212 static int
213 count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
214 {
215 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
216
217 switch (hn->type)
218 {
219 case NT_MACRO:
220 if (hn->flags & NODE_BUILTIN)
221 return 1;
222
223 /* else fall through. */
224
225 case NT_VOID:
226 {
227 struct cpp_string news;
228 void **slot;
229
230 news.len = NODE_LEN (hn);
231 news.text = NODE_NAME (hn);
232 slot = (void **) htab_find (ss->definedhash, &news);
233 if (slot == NULL)
234 {
235 ss->hashsize += NODE_LEN (hn) + 1;
236 ss->n_defs += 1;
237 }
238 }
239 return 1;
240
241 case NT_ASSERTION:
242 /* Not currently implemented. */
243 return 1;
244
245 default:
246 abort ();
247 }
248 }
249
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)
253 {
254 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
255
256 switch (hn->type)
257 {
258 case NT_MACRO:
259 if (hn->flags & NODE_BUILTIN)
260 return 1;
261
262 /* else fall through. */
263
264 case NT_VOID:
265 {
266 struct cpp_string news;
267 void **slot;
268
269 news.len = NODE_LEN (hn);
270 news.text = NODE_NAME (hn);
271 slot = (void **) htab_find (ss->definedhash, &news);
272 if (slot == NULL)
273 {
274 ss->defs[ss->n_defs] = hn;
275 ss->n_defs += 1;
276 }
277 }
278 return 1;
279
280 case NT_ASSERTION:
281 /* Not currently implemented. */
282 return 1;
283
284 default:
285 abort ();
286 }
287 }
288
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)
293 {
294 cpp_hashnode *x = *(cpp_hashnode **) px;
295 cpp_hashnode *y = *(cpp_hashnode **) py;
296 return ustrcmp (NODE_NAME (x), NODE_NAME (y));
297 }
298
299 /* Write out the remainder of the dependency information. This should be
300 called after the PCH is ready to be saved. */
301
302 int
303 cpp_write_pch_deps (cpp_reader *r, FILE *f)
304 {
305 struct macrodef_struct z;
306 struct cpp_savedstate *const ss = r->savedstate;
307 unsigned char *definedstrs;
308 size_t i;
309
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);
315
316 ss->defs = XNEWVEC (cpp_hashnode *, ss->n_defs);
317 ss->n_defs = 0;
318 cpp_forall_identifiers (r, write_defs, ss);
319
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)
324 {
325 size_t len = NODE_LEN (ss->defs[i]);
326 memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
327 definedstrs += len + 1;
328 }
329
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)
334 {
335 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
336 return -1;
337 }
338 free (ss->definedstrs);
339
340 /* Free the saved state. */
341 free (ss);
342 r->savedstate = NULL;
343
344 /* Save the next value of __COUNTER__. */
345 if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
346 {
347 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
348 return -1;
349 }
350
351 return 0;
352 }
353
354 /* Write out the definitions of the preprocessor, in a form suitable for
355 cpp_read_state. */
356
357 int
358 cpp_write_pch_state (cpp_reader *r, FILE *f)
359 {
360 if (!r->deps)
361 r->deps = deps_init ();
362
363 if (deps_save (r->deps, f) != 0)
364 {
365 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
366 return -1;
367 }
368
369 if (! _cpp_save_file_entries (r, f))
370 {
371 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
372 return -1;
373 }
374
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)
379 {
380 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
381 return -1;
382 }
383
384 /* Write saved macros. */
385 if (! _cpp_save_pushed_macros (r, f))
386 {
387 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
388 return -1;
389 }
390
391 return 0;
392 }
393
394 static int
395 _cpp_restore_pushed_macros (cpp_reader *r, FILE *f)
396 {
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;
403
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++)
409 {
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
423 {
424 defn = XNEWVEC (uchar, defnlen + 1);
425 defn[defnlen] = 0;
426
427 if (fread (defn, defnlen, 1, f) != 1)
428 return 0;
429
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);
438 }
439
440 p->next = r->pushed_macros;
441 r->pushed_macros = p;
442 }
443 return 1;
444 }
445
446 static int
447 _cpp_save_pushed_macros (cpp_reader *r, FILE *f)
448 {
449 size_t count_saved = 0;
450 size_t i;
451 struct def_pragma_macro *p,**pp;
452 size_t defnlen;
453
454 /* Get count. */
455 p = r->pushed_macros;
456 while (p != NULL)
457 {
458 count_saved++;
459 p = p->next;
460 }
461 if (fwrite (&count_saved, sizeof (count_saved), 1, f) != 1)
462 return 0;
463 if (!count_saved)
464 return 1;
465
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)
472 {
473 --i;
474 pp[i] = p;
475 p = p->next;
476 }
477 for (i = 0; i < count_saved; i++)
478 {
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)
484 {
485 defnlen = 0;
486 if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1)
487 return 0;
488 }
489 else
490 {
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;
502 }
503 }
504 return 1;
505 }
506
507
508 /* Data structure to transform hash table nodes into a sorted list */
509
510 struct ht_node_list
511 {
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;
518 };
519
520 /* Callback for collecting identifiers from hash table */
521
522 static int
523 collect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
524 void *nl_p)
525 {
526 struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
527
528 if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
529 {
530 if (nl->n_defs == nl->asize)
531 {
532 nl->asize *= 2;
533 nl->defs = XRESIZEVEC (cpp_hashnode *, nl->defs, nl->asize);
534 }
535
536 nl->defs[nl->n_defs] = hn;
537 ++nl->n_defs;
538 }
539 return 1;
540 }
541
542
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:
546
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.
551
552 NAME is used to print warnings if `warn_invalid_pch' is set in the
553 reader's flags.
554 */
555
556 int
557 cpp_valid_state (cpp_reader *r, const char *name, int fd)
558 {
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;
567
568 /* Read in the list of identifiers that must be defined
569 Check that they are defined in the same way. */
570 for (;;)
571 {
572 cpp_hashnode *h;
573 const unsigned char *newdefn;
574
575 if (read (fd, &m, sizeof (m)) != sizeof (m))
576 goto error;
577
578 if (m.name_length == 0)
579 break;
580
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))
584 {
585 if (lseek (fd, m.definition_length, SEEK_CUR) == -1)
586 goto error;
587 continue;
588 }
589
590 if (m.definition_length > namebufsz)
591 {
592 free (namebuf);
593 namebufsz = m.definition_length + 256;
594 namebuf = XNEWVEC (unsigned char, namebufsz);
595 }
596
597 if ((size_t)read (fd, namebuf, m.definition_length)
598 != m.definition_length)
599 goto error;
600
601 h = cpp_lookup (r, namebuf, m.name_length);
602 if (m.flags & NODE_POISONED
603 || h->flags & NODE_POISONED)
604 {
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;
610 }
611
612 if (h->type != NT_MACRO)
613 {
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;
626
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;
632 }
633
634 newdefn = cpp_macro_definition (r, h);
635
636 if (m.definition_length != ustrlen (newdefn)
637 || memcmp (namebuf, newdefn, m.definition_length) != 0)
638 {
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;
646 }
647 }
648 free (namebuf);
649 namebuf = NULL;
650
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;
656
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);
663
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;
669
670 while (first < last && i < nl.n_defs)
671 {
672 int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
673
674 if (cmp < 0)
675 first += ustrlen (first) + 1;
676 else if (cmp > 0)
677 ++i;
678 else
679 {
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;
685 }
686 }
687
688 free(nl.defs);
689 nl.defs = NULL;
690 free (undeftab);
691 undeftab = NULL;
692
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)
699 {
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;
705 }
706
707 /* We win! */
708 return 0;
709
710 error:
711 cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
712
713 fail:
714 free (namebuf);
715 free (undeftab);
716 free (nl.defs);
717 return 1;
718 }
719
720 /* Save all the existing macros. */
721
722 struct save_macro_data
723 {
724 uchar **defns;
725 size_t count;
726 size_t array_size;
727 char **saved_pragmas;
728 };
729
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.
739
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. */
743
744 static int
745 save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
746 {
747 struct save_macro_data *data = (struct save_macro_data *)data_p;
748
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);
753
754 if (h->type != NT_VOID
755 && (h->flags & NODE_BUILTIN) == 0)
756 {
757 if (data->count == data->array_size)
758 {
759 data->array_size *= 2;
760 data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size));
761 }
762
763 switch (h->type)
764 {
765 case NT_ASSERTION:
766 /* Not currently implemented. */
767 return 1;
768
769 case NT_MACRO:
770 {
771 const uchar * defn = cpp_macro_definition (r, h);
772 size_t defnlen = ustrlen (defn);
773
774 data->defns[data->count] = (uchar *) xmemdup (defn, defnlen,
775 defnlen + 2);
776 data->defns[data->count][defnlen] = '\n';
777 }
778 break;
779
780 default:
781 abort ();
782 }
783 data->count++;
784 }
785 return 1;
786 }
787
788 /* Prepare to restore the state, by saving the currently-defined
789 macros in 'data'. */
790
791 void
792 cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
793 {
794 struct save_macro_data *d = XNEW (struct save_macro_data);
795
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;
802 }
803
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. */
807
808 int
809 cpp_read_state (cpp_reader *r, const char *name, FILE *f,
810 struct save_macro_data *data)
811 {
812 size_t i;
813 struct lexer_state old_state;
814 unsigned int counter;
815
816 /* Restore spec_nodes, which will be full of references to the old
817 hashtable entries and so will now be invalid. */
818 {
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__"));
824 }
825
826 old_state = r->state;
827 r->state.in_directive = 1;
828 r->state.prevent_expansion = 1;
829 r->state.angled_headers = 0;
830
831 /* Run through the carefully-saved macros, insert them. */
832 for (i = 0; i < data->count; i++)
833 {
834 cpp_hashnode *h;
835 size_t namelen;
836 uchar *defn;
837
838 namelen = ustrcspn (data->defns[i], "( \n");
839 h = cpp_lookup (r, data->defns[i], namelen);
840 defn = data->defns[i] + namelen;
841
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)
846 {
847 if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
848 != NULL)
849 {
850 _cpp_clean_line (r);
851 if (!_cpp_create_definition (r, h))
852 abort ();
853 _cpp_pop_buffer (r);
854 }
855 else
856 abort ();
857 }
858
859 free (data->defns[i]);
860 }
861 r->state = old_state;
862
863 _cpp_restore_pragma_names (r, data->saved_pragmas);
864
865 free (data);
866
867 if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
868 != 0)
869 goto error;
870
871 if (! _cpp_read_file_entries (r, f))
872 goto error;
873
874 if (fread (&counter, sizeof (counter), 1, f) != 1)
875 goto error;
876
877 if (!r->counter)
878 r->counter = counter;
879
880 /* Read pushed macros. */
881 if (! _cpp_restore_pushed_macros (r, f))
882 goto error;
883 return 0;
884
885 error:
886 cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
887 return -1;
888 }