]> git.ipfire.org Git - thirdparty/gcc.git/blob - libcpp/pch.c
PR preprocessor/84517 allow double-underscore macros after string literals
[thirdparty/gcc.git] / libcpp / pch.c
1 /* Part of CPP library. (Precompiled header reading/writing.)
2 Copyright (C) 2000-2018 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 /* XXX Really fallthru? */
59 /* FALLTHRU */
60
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;
66
67 {
68 struct macrodef_struct s;
69 const unsigned char *defn;
70
71 s.name_length = NODE_LEN (hn);
72 s.flags = hn->flags & NODE_POISONED;
73
74 if (hn->type == NT_MACRO)
75 {
76 defn = cpp_macro_definition (pfile, hn);
77 s.definition_length = ustrlen (defn);
78 }
79 else
80 {
81 defn = NODE_NAME (hn);
82 s.definition_length = s.name_length;
83 }
84
85 if (fwrite (&s, sizeof (s), 1, f) != 1
86 || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
87 {
88 cpp_errno (pfile, CPP_DL_ERROR,
89 "while writing precompiled header");
90 return 0;
91 }
92 }
93 return 1;
94
95 case NT_ASSERTION:
96 /* Not currently implemented. */
97 return 1;
98
99 default:
100 abort ();
101 }
102 }
103
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. */
107
108 struct cpp_savedstate
109 {
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;
122 };
123
124 /* Save this identifier into the state: put it in the hash table,
125 put the definition in 'definedstrs'. */
126
127 static int
128 save_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
129 {
130 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
131
132 if (hn->type != NT_VOID)
133 {
134 struct cpp_string news;
135 void **slot;
136
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)
141 {
142 struct cpp_string *sp;
143 unsigned char *text;
144
145 sp = XNEW (struct cpp_string);
146 *slot = sp;
147
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));
151 }
152 }
153
154 return 1;
155 }
156
157 /* Hash some memory in a generic way. */
158
159 static hashval_t
160 hashmem (const void *p_p, size_t sz)
161 {
162 const unsigned char *p = (const unsigned char *)p_p;
163 size_t i;
164 hashval_t h;
165
166 h = 0;
167 for (i = 0; i < sz; i++)
168 h = h * 67 - (*p++ - 113);
169 return h;
170 }
171
172 /* Hash a cpp string for the hashtable machinery. */
173
174 static hashval_t
175 cpp_string_hash (const void *a_p)
176 {
177 const struct cpp_string *a = (const struct cpp_string *) a_p;
178 return hashmem (a->text, a->len);
179 }
180
181 /* Compare two cpp strings for the hashtable machinery. */
182
183 static int
184 cpp_string_eq (const void *a_p, const void *b_p)
185 {
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);
190 }
191
192 /* Free memory associated with cpp_string. */
193
194 static void
195 cpp_string_free (void *a_p)
196 {
197 struct cpp_string *a = (struct cpp_string *) a_p;
198 free ((void *) a->text);
199 free (a);
200 }
201
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. */
206
207 int
208 cpp_save_state (cpp_reader *r, FILE *f)
209 {
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);
215
216 /* Write out the list of defined identifiers. */
217 cpp_forall_identifiers (r, write_macdef, f);
218
219 return 0;
220 }
221
222 /* Calculate the 'hashsize' field of the saved state. */
223
224 static int
225 count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
226 {
227 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
228
229 switch (hn->type)
230 {
231 case NT_MACRO:
232 if (hn->flags & NODE_BUILTIN)
233 return 1;
234
235 /* fall through. */
236
237 case NT_VOID:
238 {
239 struct cpp_string news;
240 void **slot;
241
242 news.len = NODE_LEN (hn);
243 news.text = NODE_NAME (hn);
244 slot = (void **) htab_find (ss->definedhash, &news);
245 if (slot == NULL)
246 {
247 ss->hashsize += NODE_LEN (hn) + 1;
248 ss->n_defs += 1;
249 }
250 }
251 return 1;
252
253 case NT_ASSERTION:
254 /* Not currently implemented. */
255 return 1;
256
257 default:
258 abort ();
259 }
260 }
261
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)
265 {
266 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
267
268 switch (hn->type)
269 {
270 case NT_MACRO:
271 if (hn->flags & NODE_BUILTIN)
272 return 1;
273
274 /* fall through. */
275
276 case NT_VOID:
277 {
278 struct cpp_string news;
279 void **slot;
280
281 news.len = NODE_LEN (hn);
282 news.text = NODE_NAME (hn);
283 slot = (void **) htab_find (ss->definedhash, &news);
284 if (slot == NULL)
285 {
286 ss->defs[ss->n_defs] = hn;
287 ss->n_defs += 1;
288 }
289 }
290 return 1;
291
292 case NT_ASSERTION:
293 /* Not currently implemented. */
294 return 1;
295
296 default:
297 abort ();
298 }
299 }
300
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)
305 {
306 cpp_hashnode *x = *(cpp_hashnode **) px;
307 cpp_hashnode *y = *(cpp_hashnode **) py;
308 return ustrcmp (NODE_NAME (x), NODE_NAME (y));
309 }
310
311 /* Write out the remainder of the dependency information. This should be
312 called after the PCH is ready to be saved. */
313
314 int
315 cpp_write_pch_deps (cpp_reader *r, FILE *f)
316 {
317 struct macrodef_struct z;
318 struct cpp_savedstate *const ss = r->savedstate;
319 unsigned char *definedstrs;
320 size_t i;
321
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);
327
328 ss->defs = XNEWVEC (cpp_hashnode *, ss->n_defs);
329 ss->n_defs = 0;
330 cpp_forall_identifiers (r, write_defs, ss);
331
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)
336 {
337 size_t len = NODE_LEN (ss->defs[i]);
338 memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
339 definedstrs += len + 1;
340 }
341
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)
346 {
347 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
348 return -1;
349 }
350 free (ss->definedstrs);
351 free (ss->defs);
352 htab_delete (ss->definedhash);
353
354 /* Free the saved state. */
355 free (ss);
356 r->savedstate = NULL;
357
358 /* Save the next value of __COUNTER__. */
359 if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
360 {
361 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
362 return -1;
363 }
364
365 return 0;
366 }
367
368 /* Write out the definitions of the preprocessor, in a form suitable for
369 cpp_read_state. */
370
371 int
372 cpp_write_pch_state (cpp_reader *r, FILE *f)
373 {
374 if (!r->deps)
375 r->deps = deps_init ();
376
377 if (deps_save (r->deps, f) != 0)
378 {
379 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
380 return -1;
381 }
382
383 if (! _cpp_save_file_entries (r, f))
384 {
385 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
386 return -1;
387 }
388
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)
393 {
394 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
395 return -1;
396 }
397
398 /* Write saved macros. */
399 if (! _cpp_save_pushed_macros (r, f))
400 {
401 cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
402 return -1;
403 }
404
405 return 0;
406 }
407
408 static int
409 _cpp_restore_pushed_macros (cpp_reader *r, FILE *f)
410 {
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;
417
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++)
423 {
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
437 {
438 defn = XNEWVEC (uchar, defnlen + 1);
439 defn[defnlen] = 0;
440
441 if (fread (defn, defnlen, 1, f) != 1)
442 return 0;
443
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);
452 }
453
454 p->next = r->pushed_macros;
455 r->pushed_macros = p;
456 }
457 return 1;
458 }
459
460 static int
461 _cpp_save_pushed_macros (cpp_reader *r, FILE *f)
462 {
463 size_t count_saved = 0;
464 size_t i;
465 struct def_pragma_macro *p,**pp;
466 size_t defnlen;
467
468 /* Get count. */
469 p = r->pushed_macros;
470 while (p != NULL)
471 {
472 count_saved++;
473 p = p->next;
474 }
475 if (fwrite (&count_saved, sizeof (count_saved), 1, f) != 1)
476 return 0;
477 if (!count_saved)
478 return 1;
479
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)
486 {
487 --i;
488 pp[i] = p;
489 p = p->next;
490 }
491 for (i = 0; i < count_saved; i++)
492 {
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)
498 {
499 defnlen = 0;
500 if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1)
501 return 0;
502 }
503 else
504 {
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;
516 }
517 }
518 return 1;
519 }
520
521
522 /* Data structure to transform hash table nodes into a sorted list */
523
524 struct ht_node_list
525 {
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;
532 };
533
534 /* Callback for collecting identifiers from hash table */
535
536 static int
537 collect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
538 void *nl_p)
539 {
540 struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
541
542 if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
543 {
544 if (nl->n_defs == nl->asize)
545 {
546 nl->asize *= 2;
547 nl->defs = XRESIZEVEC (cpp_hashnode *, nl->defs, nl->asize);
548 }
549
550 nl->defs[nl->n_defs] = hn;
551 ++nl->n_defs;
552 }
553 return 1;
554 }
555
556
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:
560
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.
565
566 NAME is used to print warnings if `warn_invalid_pch' is set in the
567 reader's flags.
568 */
569
570 int
571 cpp_valid_state (cpp_reader *r, const char *name, int fd)
572 {
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;
581
582 /* Read in the list of identifiers that must be defined
583 Check that they are defined in the same way. */
584 for (;;)
585 {
586 cpp_hashnode *h;
587 const unsigned char *newdefn;
588
589 if (read (fd, &m, sizeof (m)) != sizeof (m))
590 goto error;
591
592 if (m.name_length == 0)
593 break;
594
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))
598 {
599 if (lseek (fd, m.definition_length, SEEK_CUR) == -1)
600 goto error;
601 continue;
602 }
603
604 if (m.definition_length > namebufsz)
605 {
606 free (namebuf);
607 namebufsz = m.definition_length + 256;
608 namebuf = XNEWVEC (unsigned char, namebufsz);
609 }
610
611 if ((size_t)read (fd, namebuf, m.definition_length)
612 != m.definition_length)
613 goto error;
614
615 h = cpp_lookup (r, namebuf, m.name_length);
616 if (m.flags & NODE_POISONED
617 || h->flags & NODE_POISONED)
618 {
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;
624 }
625
626 if (h->type != NT_MACRO)
627 {
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;
640
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;
646 }
647
648 newdefn = cpp_macro_definition (r, h);
649
650 if (m.definition_length != ustrlen (newdefn)
651 || memcmp (namebuf, newdefn, m.definition_length) != 0)
652 {
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;
660 }
661 }
662 free (namebuf);
663 namebuf = NULL;
664
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;
670
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);
677
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;
683
684 while (first < last && i < nl.n_defs)
685 {
686 int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
687
688 if (cmp < 0)
689 first += ustrlen (first) + 1;
690 else if (cmp > 0)
691 ++i;
692 else
693 {
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;
699 }
700 }
701
702 free(nl.defs);
703 nl.defs = NULL;
704 free (undeftab);
705 undeftab = NULL;
706
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)
713 {
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;
719 }
720
721 /* We win! */
722 return 0;
723
724 error:
725 cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
726
727 fail:
728 free (namebuf);
729 free (undeftab);
730 free (nl.defs);
731 return 1;
732 }
733
734 /* Save all the existing macros. */
735
736 struct save_macro_data
737 {
738 uchar **defns;
739 size_t count;
740 size_t array_size;
741 char **saved_pragmas;
742 };
743
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.
753
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. */
757
758 static int
759 save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
760 {
761 struct save_macro_data *data = (struct save_macro_data *)data_p;
762
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);
767
768 if (h->type != NT_VOID
769 && (h->flags & NODE_BUILTIN) == 0)
770 {
771 if (data->count == data->array_size)
772 {
773 data->array_size *= 2;
774 data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size));
775 }
776
777 switch (h->type)
778 {
779 case NT_ASSERTION:
780 /* Not currently implemented. */
781 return 1;
782
783 case NT_MACRO:
784 {
785 const uchar * defn = cpp_macro_definition (r, h);
786 size_t defnlen = ustrlen (defn);
787
788 data->defns[data->count] = (uchar *) xmemdup (defn, defnlen,
789 defnlen + 2);
790 data->defns[data->count][defnlen] = '\n';
791 }
792 break;
793
794 default:
795 abort ();
796 }
797 data->count++;
798 }
799 return 1;
800 }
801
802 /* Prepare to restore the state, by saving the currently-defined
803 macros in 'data'. */
804
805 void
806 cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
807 {
808 struct save_macro_data *d = XNEW (struct save_macro_data);
809
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;
816 }
817
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. */
821
822 int
823 cpp_read_state (cpp_reader *r, const char *name, FILE *f,
824 struct save_macro_data *data)
825 {
826 size_t i;
827 struct lexer_state old_state;
828 unsigned int counter;
829
830 /* Restore spec_nodes, which will be full of references to the old
831 hashtable entries and so will now be invalid. */
832 {
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__"));
841 }
842
843 old_state = r->state;
844 r->state.in_directive = 1;
845 r->state.prevent_expansion = 1;
846 r->state.angled_headers = 0;
847
848 /* Run through the carefully-saved macros, insert them. */
849 for (i = 0; i < data->count; i++)
850 {
851 cpp_hashnode *h;
852 size_t namelen;
853 uchar *defn;
854
855 namelen = ustrcspn (data->defns[i], "( \n");
856 h = cpp_lookup (r, data->defns[i], namelen);
857 defn = data->defns[i] + namelen;
858
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)
863 {
864 if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
865 != NULL)
866 {
867 _cpp_clean_line (r);
868 if (!_cpp_create_definition (r, h))
869 abort ();
870 _cpp_pop_buffer (r);
871 }
872 else
873 abort ();
874 }
875
876 free (data->defns[i]);
877 }
878 r->state = old_state;
879
880 _cpp_restore_pragma_names (r, data->saved_pragmas);
881
882 free (data);
883
884 if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
885 != 0)
886 goto error;
887
888 if (! _cpp_read_file_entries (r, f))
889 goto error;
890
891 if (fread (&counter, sizeof (counter), 1, f) != 1)
892 goto error;
893
894 if (!r->counter)
895 r->counter = counter;
896
897 /* Read pushed macros. */
898 if (! _cpp_restore_pushed_macros (r, f))
899 goto error;
900 return 0;
901
902 error:
903 cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
904 return -1;
905 }