136ac495dSmrg /* Part of CPP library. (Precompiled header reading/writing.)
2*8feb0f0bSmrg Copyright (C) 2000-2020 Free Software Foundation, Inc.
336ac495dSmrg
436ac495dSmrg This program is free software; you can redistribute it and/or modify it
536ac495dSmrg under the terms of the GNU General Public License as published by the
636ac495dSmrg Free Software Foundation; either version 3, or (at your option) any
736ac495dSmrg later version.
836ac495dSmrg
936ac495dSmrg This program is distributed in the hope that it will be useful,
1036ac495dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
1136ac495dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1236ac495dSmrg GNU General Public License for more details.
1336ac495dSmrg
1436ac495dSmrg You should have received a copy of the GNU General Public License
1536ac495dSmrg along with this program; see the file COPYING3. If not see
1636ac495dSmrg <http://www.gnu.org/licenses/>. */
1736ac495dSmrg
1836ac495dSmrg #include "config.h"
1936ac495dSmrg #include "system.h"
2036ac495dSmrg #include "cpplib.h"
2136ac495dSmrg #include "internal.h"
2236ac495dSmrg #include "hashtab.h"
2336ac495dSmrg #include "mkdeps.h"
2436ac495dSmrg
2536ac495dSmrg static int write_macdef (cpp_reader *, cpp_hashnode *, void *);
2636ac495dSmrg static int save_idents (cpp_reader *, cpp_hashnode *, void *);
2736ac495dSmrg static hashval_t hashmem (const void *, size_t);
2836ac495dSmrg static hashval_t cpp_string_hash (const void *);
2936ac495dSmrg static int cpp_string_eq (const void *, const void *);
3036ac495dSmrg static int count_defs (cpp_reader *, cpp_hashnode *, void *);
3136ac495dSmrg static int comp_hashnodes (const void *, const void *);
3236ac495dSmrg static int collect_ht_nodes (cpp_reader *, cpp_hashnode *, void *);
3336ac495dSmrg static int write_defs (cpp_reader *, cpp_hashnode *, void *);
3436ac495dSmrg static int save_macros (cpp_reader *, cpp_hashnode *, void *);
3536ac495dSmrg static int _cpp_save_pushed_macros (cpp_reader *, FILE *);
3636ac495dSmrg static int _cpp_restore_pushed_macros (cpp_reader *, FILE *);
3736ac495dSmrg
3836ac495dSmrg /* This structure represents a macro definition on disk. */
3936ac495dSmrg struct macrodef_struct
4036ac495dSmrg {
4136ac495dSmrg unsigned int definition_length;
4236ac495dSmrg unsigned short name_length;
4336ac495dSmrg unsigned short flags;
4436ac495dSmrg };
4536ac495dSmrg
4636ac495dSmrg /* This is how we write out a macro definition.
4736ac495dSmrg Suitable for being called by cpp_forall_identifiers. */
4836ac495dSmrg
4936ac495dSmrg static int
write_macdef(cpp_reader * pfile,cpp_hashnode * hn,void * file_p)5036ac495dSmrg write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
5136ac495dSmrg {
5236ac495dSmrg FILE *f = (FILE *) file_p;
53c0a68be4Smrg bool is_void = false;
5436ac495dSmrg switch (hn->type)
5536ac495dSmrg {
5636ac495dSmrg case NT_VOID:
5736ac495dSmrg if (! (hn->flags & NODE_POISONED))
5836ac495dSmrg return 1;
59c0a68be4Smrg is_void = true;
60c0a68be4Smrg goto poisoned;
6136ac495dSmrg
62c0a68be4Smrg case NT_BUILTIN_MACRO:
6336ac495dSmrg return 1;
6436ac495dSmrg
65c0a68be4Smrg case NT_USER_MACRO:
66c0a68be4Smrg if (hn->value.macro->kind != cmk_assert)
6736ac495dSmrg {
68c0a68be4Smrg poisoned:
6936ac495dSmrg struct macrodef_struct s;
7036ac495dSmrg const unsigned char *defn;
7136ac495dSmrg
7236ac495dSmrg s.name_length = NODE_LEN (hn);
7336ac495dSmrg s.flags = hn->flags & NODE_POISONED;
7436ac495dSmrg
75c0a68be4Smrg if (is_void)
7636ac495dSmrg {
7736ac495dSmrg defn = NODE_NAME (hn);
7836ac495dSmrg s.definition_length = s.name_length;
7936ac495dSmrg }
80c0a68be4Smrg else
81c0a68be4Smrg {
82c0a68be4Smrg defn = cpp_macro_definition (pfile, hn);
83c0a68be4Smrg s.definition_length = ustrlen (defn);
84c0a68be4Smrg }
8536ac495dSmrg
8636ac495dSmrg if (fwrite (&s, sizeof (s), 1, f) != 1
8736ac495dSmrg || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
8836ac495dSmrg {
8936ac495dSmrg cpp_errno (pfile, CPP_DL_ERROR,
9036ac495dSmrg "while writing precompiled header");
9136ac495dSmrg return 0;
9236ac495dSmrg }
9336ac495dSmrg }
9436ac495dSmrg return 1;
9536ac495dSmrg
9636ac495dSmrg default:
9736ac495dSmrg abort ();
9836ac495dSmrg }
9936ac495dSmrg }
10036ac495dSmrg
10136ac495dSmrg /* This structure records the names of the defined macros.
10236ac495dSmrg It's also used as a callback structure for size_initial_idents
10336ac495dSmrg and save_idents. */
10436ac495dSmrg
10536ac495dSmrg struct cpp_savedstate
10636ac495dSmrg {
10736ac495dSmrg /* A hash table of the defined identifiers. */
10836ac495dSmrg htab_t definedhash;
10936ac495dSmrg /* The size of the definitions of those identifiers (the size of
11036ac495dSmrg 'definedstrs'). */
11136ac495dSmrg size_t hashsize;
11236ac495dSmrg /* Number of definitions */
11336ac495dSmrg size_t n_defs;
11436ac495dSmrg /* Array of definitions. In cpp_write_pch_deps it is used for sorting. */
11536ac495dSmrg cpp_hashnode **defs;
11636ac495dSmrg /* Space for the next definition. Definitions are null-terminated
11736ac495dSmrg strings. */
11836ac495dSmrg unsigned char *definedstrs;
11936ac495dSmrg };
12036ac495dSmrg
12136ac495dSmrg /* Save this identifier into the state: put it in the hash table,
12236ac495dSmrg put the definition in 'definedstrs'. */
12336ac495dSmrg
12436ac495dSmrg static int
save_idents(cpp_reader * pfile ATTRIBUTE_UNUSED,cpp_hashnode * hn,void * ss_p)12536ac495dSmrg save_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
12636ac495dSmrg {
12736ac495dSmrg struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
12836ac495dSmrg
12936ac495dSmrg if (hn->type != NT_VOID)
13036ac495dSmrg {
13136ac495dSmrg struct cpp_string news;
13236ac495dSmrg void **slot;
13336ac495dSmrg
13436ac495dSmrg news.len = NODE_LEN (hn);
13536ac495dSmrg news.text= NODE_NAME (hn);
13636ac495dSmrg slot = htab_find_slot (ss->definedhash, &news, INSERT);
13736ac495dSmrg if (*slot == NULL)
13836ac495dSmrg {
13936ac495dSmrg struct cpp_string *sp;
14036ac495dSmrg unsigned char *text;
14136ac495dSmrg
14236ac495dSmrg sp = XNEW (struct cpp_string);
14336ac495dSmrg *slot = sp;
14436ac495dSmrg
14536ac495dSmrg sp->len = NODE_LEN (hn);
14636ac495dSmrg sp->text = text = XNEWVEC (unsigned char, NODE_LEN (hn));
14736ac495dSmrg memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
14836ac495dSmrg }
14936ac495dSmrg }
15036ac495dSmrg
15136ac495dSmrg return 1;
15236ac495dSmrg }
15336ac495dSmrg
15436ac495dSmrg /* Hash some memory in a generic way. */
15536ac495dSmrg
15636ac495dSmrg static hashval_t
hashmem(const void * p_p,size_t sz)15736ac495dSmrg hashmem (const void *p_p, size_t sz)
15836ac495dSmrg {
15936ac495dSmrg const unsigned char *p = (const unsigned char *)p_p;
16036ac495dSmrg size_t i;
16136ac495dSmrg hashval_t h;
16236ac495dSmrg
16336ac495dSmrg h = 0;
16436ac495dSmrg for (i = 0; i < sz; i++)
16536ac495dSmrg h = h * 67 - (*p++ - 113);
16636ac495dSmrg return h;
16736ac495dSmrg }
16836ac495dSmrg
16936ac495dSmrg /* Hash a cpp string for the hashtable machinery. */
17036ac495dSmrg
17136ac495dSmrg static hashval_t
cpp_string_hash(const void * a_p)17236ac495dSmrg cpp_string_hash (const void *a_p)
17336ac495dSmrg {
17436ac495dSmrg const struct cpp_string *a = (const struct cpp_string *) a_p;
17536ac495dSmrg return hashmem (a->text, a->len);
17636ac495dSmrg }
17736ac495dSmrg
17836ac495dSmrg /* Compare two cpp strings for the hashtable machinery. */
17936ac495dSmrg
18036ac495dSmrg static int
cpp_string_eq(const void * a_p,const void * b_p)18136ac495dSmrg cpp_string_eq (const void *a_p, const void *b_p)
18236ac495dSmrg {
18336ac495dSmrg const struct cpp_string *a = (const struct cpp_string *) a_p;
18436ac495dSmrg const struct cpp_string *b = (const struct cpp_string *) b_p;
18536ac495dSmrg return (a->len == b->len
18636ac495dSmrg && memcmp (a->text, b->text, a->len) == 0);
18736ac495dSmrg }
18836ac495dSmrg
18936ac495dSmrg /* Free memory associated with cpp_string. */
19036ac495dSmrg
19136ac495dSmrg static void
cpp_string_free(void * a_p)19236ac495dSmrg cpp_string_free (void *a_p)
19336ac495dSmrg {
19436ac495dSmrg struct cpp_string *a = (struct cpp_string *) a_p;
19536ac495dSmrg free ((void *) a->text);
19636ac495dSmrg free (a);
19736ac495dSmrg }
19836ac495dSmrg
19936ac495dSmrg /* Save the current definitions of the cpp_reader for dependency
20036ac495dSmrg checking purposes. When writing a precompiled header, this should
20136ac495dSmrg be called at the same point in the compilation as cpp_valid_state
20236ac495dSmrg would be called when reading the precompiled header back in. */
20336ac495dSmrg
20436ac495dSmrg int
cpp_save_state(cpp_reader * r,FILE * f)20536ac495dSmrg cpp_save_state (cpp_reader *r, FILE *f)
20636ac495dSmrg {
20736ac495dSmrg /* Save the list of non-void identifiers for the dependency checking. */
20836ac495dSmrg r->savedstate = XNEW (struct cpp_savedstate);
20936ac495dSmrg r->savedstate->definedhash = htab_create (100, cpp_string_hash,
21036ac495dSmrg cpp_string_eq, cpp_string_free);
21136ac495dSmrg cpp_forall_identifiers (r, save_idents, r->savedstate);
21236ac495dSmrg
21336ac495dSmrg /* Write out the list of defined identifiers. */
21436ac495dSmrg cpp_forall_identifiers (r, write_macdef, f);
21536ac495dSmrg
21636ac495dSmrg return 0;
21736ac495dSmrg }
21836ac495dSmrg
21936ac495dSmrg /* Calculate the 'hashsize' field of the saved state. */
22036ac495dSmrg
22136ac495dSmrg static int
count_defs(cpp_reader * pfile ATTRIBUTE_UNUSED,cpp_hashnode * hn,void * ss_p)22236ac495dSmrg count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
22336ac495dSmrg {
22436ac495dSmrg struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
22536ac495dSmrg
22636ac495dSmrg switch (hn->type)
22736ac495dSmrg {
228c0a68be4Smrg case NT_BUILTIN_MACRO:
229c0a68be4Smrg return 1;
230c0a68be4Smrg
231c0a68be4Smrg case NT_USER_MACRO:
232c0a68be4Smrg if (hn->value.macro->kind == cmk_assert)
23336ac495dSmrg return 1;
23436ac495dSmrg
23536ac495dSmrg /* fall through. */
23636ac495dSmrg
23736ac495dSmrg case NT_VOID:
23836ac495dSmrg {
23936ac495dSmrg struct cpp_string news;
24036ac495dSmrg void **slot;
24136ac495dSmrg
24236ac495dSmrg news.len = NODE_LEN (hn);
24336ac495dSmrg news.text = NODE_NAME (hn);
24436ac495dSmrg slot = (void **) htab_find (ss->definedhash, &news);
24536ac495dSmrg if (slot == NULL)
24636ac495dSmrg {
24736ac495dSmrg ss->hashsize += NODE_LEN (hn) + 1;
24836ac495dSmrg ss->n_defs += 1;
24936ac495dSmrg }
25036ac495dSmrg }
25136ac495dSmrg return 1;
25236ac495dSmrg
25336ac495dSmrg default:
25436ac495dSmrg abort ();
25536ac495dSmrg }
25636ac495dSmrg }
25736ac495dSmrg
25836ac495dSmrg /* Collect the identifiers into the state's string table. */
25936ac495dSmrg static int
write_defs(cpp_reader * pfile ATTRIBUTE_UNUSED,cpp_hashnode * hn,void * ss_p)26036ac495dSmrg write_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
26136ac495dSmrg {
26236ac495dSmrg struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
26336ac495dSmrg
26436ac495dSmrg switch (hn->type)
26536ac495dSmrg {
266c0a68be4Smrg case NT_BUILTIN_MACRO:
267c0a68be4Smrg return 1;
268c0a68be4Smrg
269c0a68be4Smrg case NT_USER_MACRO:
270c0a68be4Smrg if (hn->value.macro->kind == cmk_assert)
27136ac495dSmrg return 1;
27236ac495dSmrg
27336ac495dSmrg /* fall through. */
27436ac495dSmrg
27536ac495dSmrg case NT_VOID:
27636ac495dSmrg {
27736ac495dSmrg struct cpp_string news;
27836ac495dSmrg void **slot;
27936ac495dSmrg
28036ac495dSmrg news.len = NODE_LEN (hn);
28136ac495dSmrg news.text = NODE_NAME (hn);
28236ac495dSmrg slot = (void **) htab_find (ss->definedhash, &news);
28336ac495dSmrg if (slot == NULL)
28436ac495dSmrg {
28536ac495dSmrg ss->defs[ss->n_defs] = hn;
28636ac495dSmrg ss->n_defs += 1;
28736ac495dSmrg }
28836ac495dSmrg }
28936ac495dSmrg return 1;
29036ac495dSmrg
29136ac495dSmrg default:
29236ac495dSmrg abort ();
29336ac495dSmrg }
29436ac495dSmrg }
29536ac495dSmrg
29636ac495dSmrg /* Comparison function for qsort. The arguments point to pointers of
29736ac495dSmrg type ht_hashnode *. */
29836ac495dSmrg static int
comp_hashnodes(const void * px,const void * py)29936ac495dSmrg comp_hashnodes (const void *px, const void *py)
30036ac495dSmrg {
30136ac495dSmrg cpp_hashnode *x = *(cpp_hashnode **) px;
30236ac495dSmrg cpp_hashnode *y = *(cpp_hashnode **) py;
30336ac495dSmrg return ustrcmp (NODE_NAME (x), NODE_NAME (y));
30436ac495dSmrg }
30536ac495dSmrg
30636ac495dSmrg /* Write out the remainder of the dependency information. This should be
30736ac495dSmrg called after the PCH is ready to be saved. */
30836ac495dSmrg
30936ac495dSmrg int
cpp_write_pch_deps(cpp_reader * r,FILE * f)31036ac495dSmrg cpp_write_pch_deps (cpp_reader *r, FILE *f)
31136ac495dSmrg {
31236ac495dSmrg struct macrodef_struct z;
31336ac495dSmrg struct cpp_savedstate *const ss = r->savedstate;
31436ac495dSmrg unsigned char *definedstrs;
31536ac495dSmrg size_t i;
31636ac495dSmrg
31736ac495dSmrg /* Collect the list of identifiers which have been seen and
31836ac495dSmrg weren't defined to anything previously. */
31936ac495dSmrg ss->hashsize = 0;
32036ac495dSmrg ss->n_defs = 0;
32136ac495dSmrg cpp_forall_identifiers (r, count_defs, ss);
32236ac495dSmrg
32336ac495dSmrg ss->defs = XNEWVEC (cpp_hashnode *, ss->n_defs);
32436ac495dSmrg ss->n_defs = 0;
32536ac495dSmrg cpp_forall_identifiers (r, write_defs, ss);
32636ac495dSmrg
32736ac495dSmrg /* Sort the list, copy it into a buffer, and write it out. */
32836ac495dSmrg qsort (ss->defs, ss->n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
32936ac495dSmrg definedstrs = ss->definedstrs = XNEWVEC (unsigned char, ss->hashsize);
33036ac495dSmrg for (i = 0; i < ss->n_defs; ++i)
33136ac495dSmrg {
33236ac495dSmrg size_t len = NODE_LEN (ss->defs[i]);
33336ac495dSmrg memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
33436ac495dSmrg definedstrs += len + 1;
33536ac495dSmrg }
33636ac495dSmrg
33736ac495dSmrg memset (&z, 0, sizeof (z));
33836ac495dSmrg z.definition_length = ss->hashsize;
33936ac495dSmrg if (fwrite (&z, sizeof (z), 1, f) != 1
34036ac495dSmrg || fwrite (ss->definedstrs, ss->hashsize, 1, f) != 1)
34136ac495dSmrg {
34236ac495dSmrg cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
34336ac495dSmrg return -1;
34436ac495dSmrg }
34536ac495dSmrg free (ss->definedstrs);
34636ac495dSmrg free (ss->defs);
34736ac495dSmrg htab_delete (ss->definedhash);
34836ac495dSmrg
34936ac495dSmrg /* Free the saved state. */
35036ac495dSmrg free (ss);
35136ac495dSmrg r->savedstate = NULL;
35236ac495dSmrg
35336ac495dSmrg /* Save the next value of __COUNTER__. */
35436ac495dSmrg if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
35536ac495dSmrg {
35636ac495dSmrg cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
35736ac495dSmrg return -1;
35836ac495dSmrg }
35936ac495dSmrg
36036ac495dSmrg return 0;
36136ac495dSmrg }
36236ac495dSmrg
36336ac495dSmrg /* Write out the definitions of the preprocessor, in a form suitable for
36436ac495dSmrg cpp_read_state. */
36536ac495dSmrg
36636ac495dSmrg int
cpp_write_pch_state(cpp_reader * r,FILE * f)36736ac495dSmrg cpp_write_pch_state (cpp_reader *r, FILE *f)
36836ac495dSmrg {
36936ac495dSmrg if (!r->deps)
37036ac495dSmrg r->deps = deps_init ();
37136ac495dSmrg
37236ac495dSmrg if (deps_save (r->deps, f) != 0)
37336ac495dSmrg {
37436ac495dSmrg cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
37536ac495dSmrg return -1;
37636ac495dSmrg }
37736ac495dSmrg
37836ac495dSmrg if (! _cpp_save_file_entries (r, f))
37936ac495dSmrg {
38036ac495dSmrg cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
38136ac495dSmrg return -1;
38236ac495dSmrg }
38336ac495dSmrg
38436ac495dSmrg /* Save the next __COUNTER__ value. When we include a precompiled header,
38536ac495dSmrg we need to start at the offset we would have if the header had been
38636ac495dSmrg included normally. */
38736ac495dSmrg if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
38836ac495dSmrg {
38936ac495dSmrg cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
39036ac495dSmrg return -1;
39136ac495dSmrg }
39236ac495dSmrg
39336ac495dSmrg /* Write saved macros. */
39436ac495dSmrg if (! _cpp_save_pushed_macros (r, f))
39536ac495dSmrg {
39636ac495dSmrg cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
39736ac495dSmrg return -1;
39836ac495dSmrg }
39936ac495dSmrg
40036ac495dSmrg return 0;
40136ac495dSmrg }
40236ac495dSmrg
40336ac495dSmrg static int
_cpp_restore_pushed_macros(cpp_reader * r,FILE * f)40436ac495dSmrg _cpp_restore_pushed_macros (cpp_reader *r, FILE *f)
40536ac495dSmrg {
40636ac495dSmrg size_t count_saved = 0;
40736ac495dSmrg size_t i;
40836ac495dSmrg struct def_pragma_macro *p;
40936ac495dSmrg size_t nlen;
41036ac495dSmrg uchar *defn;
41136ac495dSmrg size_t defnlen;
41236ac495dSmrg
41336ac495dSmrg if (fread (&count_saved, sizeof (count_saved), 1, f) != 1)
41436ac495dSmrg return 0;
41536ac495dSmrg if (! count_saved)
41636ac495dSmrg return 1;
41736ac495dSmrg for (i = 0; i < count_saved; i++)
41836ac495dSmrg {
41936ac495dSmrg if (fread (&nlen, sizeof (nlen), 1, f) != 1)
42036ac495dSmrg return 0;
42136ac495dSmrg p = XNEW (struct def_pragma_macro);
42236ac495dSmrg memset (p, 0, sizeof (struct def_pragma_macro));
42336ac495dSmrg p->name = XNEWVAR (char, nlen + 1);
42436ac495dSmrg p->name[nlen] = 0;
42536ac495dSmrg if (fread (p->name, nlen, 1, f) != 1)
42636ac495dSmrg return 0;
42736ac495dSmrg if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
42836ac495dSmrg return 0;
42936ac495dSmrg if (defnlen == 0)
43036ac495dSmrg p->is_undef = 1;
43136ac495dSmrg else
43236ac495dSmrg {
43336ac495dSmrg defn = XNEWVEC (uchar, defnlen + 1);
43436ac495dSmrg defn[defnlen] = 0;
43536ac495dSmrg
43636ac495dSmrg if (fread (defn, defnlen, 1, f) != 1)
43736ac495dSmrg return 0;
43836ac495dSmrg
43936ac495dSmrg p->definition = defn;
440c0a68be4Smrg if (fread (&(p->line), sizeof (location_t), 1, f) != 1)
44136ac495dSmrg return 0;
44236ac495dSmrg defnlen = 0;
44336ac495dSmrg if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
44436ac495dSmrg return 0;
44536ac495dSmrg p->syshdr = ((defnlen & 1) != 0 ? 1 : 0);
44636ac495dSmrg p->used = ((defnlen & 2) != 0 ? 1 : 0);
44736ac495dSmrg }
44836ac495dSmrg
44936ac495dSmrg p->next = r->pushed_macros;
45036ac495dSmrg r->pushed_macros = p;
45136ac495dSmrg }
45236ac495dSmrg return 1;
45336ac495dSmrg }
45436ac495dSmrg
45536ac495dSmrg static int
_cpp_save_pushed_macros(cpp_reader * r,FILE * f)45636ac495dSmrg _cpp_save_pushed_macros (cpp_reader *r, FILE *f)
45736ac495dSmrg {
45836ac495dSmrg size_t count_saved = 0;
45936ac495dSmrg size_t i;
46036ac495dSmrg struct def_pragma_macro *p,**pp;
46136ac495dSmrg size_t defnlen;
46236ac495dSmrg
46336ac495dSmrg /* Get count. */
46436ac495dSmrg p = r->pushed_macros;
46536ac495dSmrg while (p != NULL)
46636ac495dSmrg {
46736ac495dSmrg count_saved++;
46836ac495dSmrg p = p->next;
46936ac495dSmrg }
47036ac495dSmrg if (fwrite (&count_saved, sizeof (count_saved), 1, f) != 1)
47136ac495dSmrg return 0;
47236ac495dSmrg if (!count_saved)
47336ac495dSmrg return 1;
47436ac495dSmrg
47536ac495dSmrg pp = (struct def_pragma_macro **) alloca (sizeof (struct def_pragma_macro *)
47636ac495dSmrg * count_saved);
47736ac495dSmrg /* Store them in reverse order. */
47836ac495dSmrg p = r->pushed_macros;
47936ac495dSmrg i = count_saved;
48036ac495dSmrg while (p != NULL)
48136ac495dSmrg {
48236ac495dSmrg --i;
48336ac495dSmrg pp[i] = p;
48436ac495dSmrg p = p->next;
48536ac495dSmrg }
48636ac495dSmrg for (i = 0; i < count_saved; i++)
48736ac495dSmrg {
48836ac495dSmrg defnlen = strlen (pp[i]->name);
48936ac495dSmrg if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
49036ac495dSmrg || fwrite (pp[i]->name, defnlen, 1, f) != 1)
49136ac495dSmrg return 0;
49236ac495dSmrg if (pp[i]->is_undef)
49336ac495dSmrg {
49436ac495dSmrg defnlen = 0;
49536ac495dSmrg if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1)
49636ac495dSmrg return 0;
49736ac495dSmrg }
49836ac495dSmrg else
49936ac495dSmrg {
50036ac495dSmrg defnlen = ustrlen (pp[i]->definition);
50136ac495dSmrg if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
50236ac495dSmrg || fwrite (pp[i]->definition, defnlen, 1, f) != 1)
50336ac495dSmrg return 0;
504c0a68be4Smrg if (fwrite (&(pp[i]->line), sizeof (location_t), 1, f) != 1)
50536ac495dSmrg return 0;
50636ac495dSmrg defnlen = 0;
50736ac495dSmrg defnlen |= (pp[i]->syshdr != 0 ? 1 : 0);
50836ac495dSmrg defnlen |= (pp[i]->used != 0 ? 2 : 0);
50936ac495dSmrg if (fwrite (&defnlen, sizeof (defnlen), 1, f) != 1)
51036ac495dSmrg return 0;
51136ac495dSmrg }
51236ac495dSmrg }
51336ac495dSmrg return 1;
51436ac495dSmrg }
51536ac495dSmrg
51636ac495dSmrg
51736ac495dSmrg /* Data structure to transform hash table nodes into a sorted list */
51836ac495dSmrg
51936ac495dSmrg struct ht_node_list
52036ac495dSmrg {
52136ac495dSmrg /* Array of nodes */
52236ac495dSmrg cpp_hashnode **defs;
52336ac495dSmrg /* Number of nodes in the array */
52436ac495dSmrg size_t n_defs;
52536ac495dSmrg /* Size of the allocated array */
52636ac495dSmrg size_t asize;
52736ac495dSmrg };
52836ac495dSmrg
52936ac495dSmrg /* Callback for collecting identifiers from hash table */
53036ac495dSmrg
53136ac495dSmrg static int
collect_ht_nodes(cpp_reader * pfile ATTRIBUTE_UNUSED,cpp_hashnode * hn,void * nl_p)53236ac495dSmrg collect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
53336ac495dSmrg void *nl_p)
53436ac495dSmrg {
53536ac495dSmrg struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
53636ac495dSmrg
53736ac495dSmrg if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
53836ac495dSmrg {
53936ac495dSmrg if (nl->n_defs == nl->asize)
54036ac495dSmrg {
54136ac495dSmrg nl->asize *= 2;
54236ac495dSmrg nl->defs = XRESIZEVEC (cpp_hashnode *, nl->defs, nl->asize);
54336ac495dSmrg }
54436ac495dSmrg
54536ac495dSmrg nl->defs[nl->n_defs] = hn;
54636ac495dSmrg ++nl->n_defs;
54736ac495dSmrg }
54836ac495dSmrg return 1;
54936ac495dSmrg }
55036ac495dSmrg
55136ac495dSmrg
55236ac495dSmrg /* Return nonzero if FD is a precompiled header which is consistent
55336ac495dSmrg with the preprocessor's current definitions. It will be consistent
55436ac495dSmrg when:
55536ac495dSmrg
55636ac495dSmrg - anything that was defined just before the PCH was generated
55736ac495dSmrg is defined the same way now; and
55836ac495dSmrg - anything that was not defined then, but is defined now, was not
55936ac495dSmrg used by the PCH.
56036ac495dSmrg
56136ac495dSmrg NAME is used to print warnings if `warn_invalid_pch' is set in the
56236ac495dSmrg reader's flags.
56336ac495dSmrg */
56436ac495dSmrg
56536ac495dSmrg int
cpp_valid_state(cpp_reader * r,const char * name,int fd)56636ac495dSmrg cpp_valid_state (cpp_reader *r, const char *name, int fd)
56736ac495dSmrg {
56836ac495dSmrg struct macrodef_struct m;
56936ac495dSmrg size_t namebufsz = 256;
57036ac495dSmrg unsigned char *namebuf = XNEWVEC (unsigned char, namebufsz);
57136ac495dSmrg unsigned char *undeftab = NULL;
57236ac495dSmrg struct ht_node_list nl = { 0, 0, 0 };
57336ac495dSmrg unsigned char *first, *last;
57436ac495dSmrg unsigned int i;
57536ac495dSmrg unsigned int counter;
57636ac495dSmrg
57736ac495dSmrg /* Read in the list of identifiers that must be defined
57836ac495dSmrg Check that they are defined in the same way. */
57936ac495dSmrg for (;;)
58036ac495dSmrg {
58136ac495dSmrg cpp_hashnode *h;
58236ac495dSmrg const unsigned char *newdefn;
58336ac495dSmrg
58436ac495dSmrg if (read (fd, &m, sizeof (m)) != sizeof (m))
58536ac495dSmrg goto error;
58636ac495dSmrg
58736ac495dSmrg if (m.name_length == 0)
58836ac495dSmrg break;
58936ac495dSmrg
59036ac495dSmrg /* If this file is already preprocessed, there won't be any
59136ac495dSmrg macros defined, and that's OK. */
59236ac495dSmrg if (CPP_OPTION (r, preprocessed))
59336ac495dSmrg {
59436ac495dSmrg if (lseek (fd, m.definition_length, SEEK_CUR) == -1)
59536ac495dSmrg goto error;
59636ac495dSmrg continue;
59736ac495dSmrg }
59836ac495dSmrg
59936ac495dSmrg if (m.definition_length > namebufsz)
60036ac495dSmrg {
60136ac495dSmrg free (namebuf);
60236ac495dSmrg namebufsz = m.definition_length + 256;
60336ac495dSmrg namebuf = XNEWVEC (unsigned char, namebufsz);
60436ac495dSmrg }
60536ac495dSmrg
60636ac495dSmrg if ((size_t)read (fd, namebuf, m.definition_length)
60736ac495dSmrg != m.definition_length)
60836ac495dSmrg goto error;
60936ac495dSmrg
61036ac495dSmrg h = cpp_lookup (r, namebuf, m.name_length);
61136ac495dSmrg if (m.flags & NODE_POISONED
61236ac495dSmrg || h->flags & NODE_POISONED)
61336ac495dSmrg {
61436ac495dSmrg if (CPP_OPTION (r, warn_invalid_pch))
61536ac495dSmrg cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
61636ac495dSmrg "%s: not used because `%.*s' is poisoned",
61736ac495dSmrg name, m.name_length, namebuf);
61836ac495dSmrg goto fail;
61936ac495dSmrg }
62036ac495dSmrg
621c0a68be4Smrg if (h->type == NT_VOID)
62236ac495dSmrg {
62336ac495dSmrg /* It's ok if __GCC_HAVE_DWARF2_CFI_ASM becomes undefined,
62436ac495dSmrg as in, when the PCH file is created with -g and we're
62536ac495dSmrg attempting to use it without -g. Restoring the PCH file
62636ac495dSmrg is supposed to bring in this definition *and* enable the
62736ac495dSmrg generation of call frame information, so that precompiled
62836ac495dSmrg definitions that take this macro into account, to decide
62936ac495dSmrg what asm to emit, won't issue .cfi directives when the
63036ac495dSmrg compiler doesn't. */
63136ac495dSmrg if (!(h->flags & NODE_USED)
63236ac495dSmrg && m.name_length == sizeof ("__GCC_HAVE_DWARF2_CFI_ASM") - 1
63336ac495dSmrg && !memcmp (namebuf, "__GCC_HAVE_DWARF2_CFI_ASM", m.name_length))
63436ac495dSmrg continue;
63536ac495dSmrg
63636ac495dSmrg if (CPP_OPTION (r, warn_invalid_pch))
63736ac495dSmrg cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
63836ac495dSmrg "%s: not used because `%.*s' not defined",
63936ac495dSmrg name, m.name_length, namebuf);
64036ac495dSmrg goto fail;
64136ac495dSmrg }
64236ac495dSmrg
64336ac495dSmrg newdefn = cpp_macro_definition (r, h);
64436ac495dSmrg
64536ac495dSmrg if (m.definition_length != ustrlen (newdefn)
64636ac495dSmrg || memcmp (namebuf, newdefn, m.definition_length) != 0)
64736ac495dSmrg {
64836ac495dSmrg if (CPP_OPTION (r, warn_invalid_pch))
64936ac495dSmrg cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
65036ac495dSmrg "%s: not used because `%.*s' defined as `%s' not `%.*s'",
65136ac495dSmrg name, m.name_length, namebuf, newdefn + m.name_length,
65236ac495dSmrg m.definition_length - m.name_length,
65336ac495dSmrg namebuf + m.name_length);
65436ac495dSmrg goto fail;
65536ac495dSmrg }
65636ac495dSmrg }
65736ac495dSmrg free (namebuf);
65836ac495dSmrg namebuf = NULL;
65936ac495dSmrg
66036ac495dSmrg /* Read in the list of identifiers that must not be defined.
66136ac495dSmrg Check that they really aren't. */
66236ac495dSmrg undeftab = XNEWVEC (unsigned char, m.definition_length);
66336ac495dSmrg if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
66436ac495dSmrg goto error;
66536ac495dSmrg
66636ac495dSmrg /* Collect identifiers from the current hash table. */
66736ac495dSmrg nl.n_defs = 0;
66836ac495dSmrg nl.asize = 10;
66936ac495dSmrg nl.defs = XNEWVEC (cpp_hashnode *, nl.asize);
67036ac495dSmrg cpp_forall_identifiers (r, &collect_ht_nodes, &nl);
67136ac495dSmrg qsort (nl.defs, nl.n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
67236ac495dSmrg
67336ac495dSmrg /* Loop through nl.defs and undeftab, both of which are sorted lists.
67436ac495dSmrg There should be no matches. */
67536ac495dSmrg first = undeftab;
67636ac495dSmrg last = undeftab + m.definition_length;
67736ac495dSmrg i = 0;
67836ac495dSmrg
67936ac495dSmrg while (first < last && i < nl.n_defs)
68036ac495dSmrg {
68136ac495dSmrg int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
68236ac495dSmrg
68336ac495dSmrg if (cmp < 0)
68436ac495dSmrg first += ustrlen (first) + 1;
68536ac495dSmrg else if (cmp > 0)
68636ac495dSmrg ++i;
68736ac495dSmrg else
68836ac495dSmrg {
68936ac495dSmrg if (CPP_OPTION (r, warn_invalid_pch))
69036ac495dSmrg cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
69136ac495dSmrg "%s: not used because `%s' is defined",
69236ac495dSmrg name, first);
69336ac495dSmrg goto fail;
69436ac495dSmrg }
69536ac495dSmrg }
69636ac495dSmrg
69736ac495dSmrg free(nl.defs);
69836ac495dSmrg nl.defs = NULL;
69936ac495dSmrg free (undeftab);
70036ac495dSmrg undeftab = NULL;
70136ac495dSmrg
70236ac495dSmrg /* Read in the next value of __COUNTER__.
70336ac495dSmrg Check that (a) __COUNTER__ was not used in the pch or (b) __COUNTER__
70436ac495dSmrg has not been used in this translation unit. */
70536ac495dSmrg if (read (fd, &counter, sizeof (counter)) != sizeof (counter))
70636ac495dSmrg goto error;
70736ac495dSmrg if (counter && r->counter)
70836ac495dSmrg {
70936ac495dSmrg if (CPP_OPTION (r, warn_invalid_pch))
71036ac495dSmrg cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
71136ac495dSmrg "%s: not used because `__COUNTER__' is invalid",
71236ac495dSmrg name);
71336ac495dSmrg goto fail;
71436ac495dSmrg }
71536ac495dSmrg
71636ac495dSmrg /* We win! */
71736ac495dSmrg return 0;
71836ac495dSmrg
71936ac495dSmrg error:
72036ac495dSmrg cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
72136ac495dSmrg
72236ac495dSmrg fail:
72336ac495dSmrg free (namebuf);
72436ac495dSmrg free (undeftab);
72536ac495dSmrg free (nl.defs);
72636ac495dSmrg return 1;
72736ac495dSmrg }
72836ac495dSmrg
72936ac495dSmrg /* Save all the existing macros. */
73036ac495dSmrg
73136ac495dSmrg struct save_macro_data
73236ac495dSmrg {
73336ac495dSmrg uchar **defns;
73436ac495dSmrg size_t count;
73536ac495dSmrg size_t array_size;
73636ac495dSmrg char **saved_pragmas;
73736ac495dSmrg };
73836ac495dSmrg
73936ac495dSmrg /* Save the definition of a single macro, so that it will persist
74036ac495dSmrg across a PCH restore. Because macro data is in GCed memory, which
74136ac495dSmrg will be blown away by PCH, it must be temporarily copied to
74236ac495dSmrg malloced memory. (The macros will refer to identifier nodes which
74336ac495dSmrg are also GCed and so on, so the copying is done by turning them
74436ac495dSmrg into self-contained strings.) The assumption is that most macro
74536ac495dSmrg definitions will come from the PCH file, not from the compilation
74636ac495dSmrg before the PCH file is loaded, so it doesn't matter that this is
74736ac495dSmrg a little expensive.
74836ac495dSmrg
74936ac495dSmrg It would reduce the cost even further if macros defined in the PCH
75036ac495dSmrg file were not saved in this way, but this is not done (yet), except
75136ac495dSmrg for builtins, and for #assert by default. */
75236ac495dSmrg
75336ac495dSmrg static int
save_macros(cpp_reader * r,cpp_hashnode * h,void * data_p)75436ac495dSmrg save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
75536ac495dSmrg {
75636ac495dSmrg struct save_macro_data *data = (struct save_macro_data *)data_p;
75736ac495dSmrg
758c0a68be4Smrg if (cpp_user_macro_p (h))
75936ac495dSmrg {
76036ac495dSmrg if (data->count == data->array_size)
76136ac495dSmrg {
76236ac495dSmrg data->array_size *= 2;
76336ac495dSmrg data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size));
76436ac495dSmrg }
76536ac495dSmrg
76636ac495dSmrg const uchar * defn = cpp_macro_definition (r, h);
76736ac495dSmrg size_t defnlen = ustrlen (defn);
76836ac495dSmrg
769c0a68be4Smrg data->defns[data->count] = (uchar *) xmemdup (defn, defnlen, defnlen + 2);
77036ac495dSmrg data->defns[data->count][defnlen] = '\n';
77136ac495dSmrg data->count++;
77236ac495dSmrg }
773c0a68be4Smrg
77436ac495dSmrg return 1;
77536ac495dSmrg }
77636ac495dSmrg
77736ac495dSmrg /* Prepare to restore the state, by saving the currently-defined
77836ac495dSmrg macros in 'data'. */
77936ac495dSmrg
78036ac495dSmrg void
cpp_prepare_state(cpp_reader * r,struct save_macro_data ** data)78136ac495dSmrg cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
78236ac495dSmrg {
78336ac495dSmrg struct save_macro_data *d = XNEW (struct save_macro_data);
78436ac495dSmrg
78536ac495dSmrg d->array_size = 512;
78636ac495dSmrg d->defns = XNEWVEC (uchar *, d->array_size);
78736ac495dSmrg d->count = 0;
78836ac495dSmrg cpp_forall_identifiers (r, save_macros, d);
78936ac495dSmrg d->saved_pragmas = _cpp_save_pragma_names (r);
79036ac495dSmrg *data = d;
79136ac495dSmrg }
79236ac495dSmrg
79336ac495dSmrg /* Given a precompiled header that was previously determined to be valid,
79436ac495dSmrg apply all its definitions (and undefinitions) to the current state.
79536ac495dSmrg DEPNAME is passed to deps_restore. */
79636ac495dSmrg
79736ac495dSmrg int
cpp_read_state(cpp_reader * r,const char * name,FILE * f,struct save_macro_data * data)79836ac495dSmrg cpp_read_state (cpp_reader *r, const char *name, FILE *f,
79936ac495dSmrg struct save_macro_data *data)
80036ac495dSmrg {
80136ac495dSmrg size_t i;
80236ac495dSmrg struct lexer_state old_state;
80336ac495dSmrg unsigned int counter;
80436ac495dSmrg
80536ac495dSmrg /* Restore spec_nodes, which will be full of references to the old
80636ac495dSmrg hashtable entries and so will now be invalid. */
80736ac495dSmrg {
80836ac495dSmrg struct spec_nodes *s = &r->spec_nodes;
80936ac495dSmrg s->n_defined = cpp_lookup (r, DSC("defined"));
81036ac495dSmrg s->n_true = cpp_lookup (r, DSC("true"));
81136ac495dSmrg s->n_false = cpp_lookup (r, DSC("false"));
81236ac495dSmrg s->n__VA_ARGS__ = cpp_lookup (r, DSC("__VA_ARGS__"));
813a2dc1f3fSmrg s->n__VA_OPT__ = cpp_lookup (r, DSC("__VA_OPT__"));
81436ac495dSmrg }
81536ac495dSmrg
81636ac495dSmrg old_state = r->state;
81736ac495dSmrg r->state.in_directive = 1;
81836ac495dSmrg r->state.prevent_expansion = 1;
81936ac495dSmrg r->state.angled_headers = 0;
82036ac495dSmrg
82136ac495dSmrg /* Run through the carefully-saved macros, insert them. */
82236ac495dSmrg for (i = 0; i < data->count; i++)
82336ac495dSmrg {
82436ac495dSmrg cpp_hashnode *h;
82536ac495dSmrg size_t namelen;
82636ac495dSmrg uchar *defn;
82736ac495dSmrg
82836ac495dSmrg namelen = ustrcspn (data->defns[i], "( \n");
82936ac495dSmrg h = cpp_lookup (r, data->defns[i], namelen);
83036ac495dSmrg defn = data->defns[i] + namelen;
83136ac495dSmrg
83236ac495dSmrg /* The PCH file is valid, so we know that if there is a definition
83336ac495dSmrg from the PCH file it must be the same as the one we had
83436ac495dSmrg originally, and so do not need to restore it. */
83536ac495dSmrg if (h->type == NT_VOID)
83636ac495dSmrg {
83736ac495dSmrg if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
83836ac495dSmrg != NULL)
83936ac495dSmrg {
84036ac495dSmrg _cpp_clean_line (r);
84136ac495dSmrg if (!_cpp_create_definition (r, h))
84236ac495dSmrg abort ();
84336ac495dSmrg _cpp_pop_buffer (r);
84436ac495dSmrg }
84536ac495dSmrg else
84636ac495dSmrg abort ();
84736ac495dSmrg }
84836ac495dSmrg
84936ac495dSmrg free (data->defns[i]);
85036ac495dSmrg }
85136ac495dSmrg r->state = old_state;
85236ac495dSmrg
85336ac495dSmrg _cpp_restore_pragma_names (r, data->saved_pragmas);
85436ac495dSmrg
85536ac495dSmrg free (data);
85636ac495dSmrg
85736ac495dSmrg if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
85836ac495dSmrg != 0)
85936ac495dSmrg goto error;
86036ac495dSmrg
86136ac495dSmrg if (! _cpp_read_file_entries (r, f))
86236ac495dSmrg goto error;
86336ac495dSmrg
86436ac495dSmrg if (fread (&counter, sizeof (counter), 1, f) != 1)
86536ac495dSmrg goto error;
86636ac495dSmrg
86736ac495dSmrg if (!r->counter)
86836ac495dSmrg r->counter = counter;
86936ac495dSmrg
87036ac495dSmrg /* Read pushed macros. */
87136ac495dSmrg if (! _cpp_restore_pushed_macros (r, f))
87236ac495dSmrg goto error;
87336ac495dSmrg return 0;
87436ac495dSmrg
87536ac495dSmrg error:
87636ac495dSmrg cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
87736ac495dSmrg return -1;
87836ac495dSmrg }
879