1*e4b17023SJohn Marino /* Part of CPP library. (Precompiled header reading/writing.)
2*e4b17023SJohn Marino Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009, 2010
3*e4b17023SJohn Marino Free Software Foundation, Inc.
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino This program is free software; you can redistribute it and/or modify it
6*e4b17023SJohn Marino under the terms of the GNU General Public License as published by the
7*e4b17023SJohn Marino Free Software Foundation; either version 3, or (at your option) any
8*e4b17023SJohn Marino later version.
9*e4b17023SJohn Marino
10*e4b17023SJohn Marino This program is distributed in the hope that it will be useful,
11*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
12*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*e4b17023SJohn Marino GNU General Public License for more details.
14*e4b17023SJohn Marino
15*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
16*e4b17023SJohn Marino along with this program; see the file COPYING3. If not see
17*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
18*e4b17023SJohn Marino
19*e4b17023SJohn Marino #include "config.h"
20*e4b17023SJohn Marino #include "system.h"
21*e4b17023SJohn Marino #include "cpplib.h"
22*e4b17023SJohn Marino #include "internal.h"
23*e4b17023SJohn Marino #include "hashtab.h"
24*e4b17023SJohn Marino #include "mkdeps.h"
25*e4b17023SJohn Marino
26*e4b17023SJohn Marino static int write_macdef (cpp_reader *, cpp_hashnode *, void *);
27*e4b17023SJohn Marino static int save_idents (cpp_reader *, cpp_hashnode *, void *);
28*e4b17023SJohn Marino static hashval_t hashmem (const void *, size_t);
29*e4b17023SJohn Marino static hashval_t cpp_string_hash (const void *);
30*e4b17023SJohn Marino static int cpp_string_eq (const void *, const void *);
31*e4b17023SJohn Marino static int count_defs (cpp_reader *, cpp_hashnode *, void *);
32*e4b17023SJohn Marino static int comp_hashnodes (const void *, const void *);
33*e4b17023SJohn Marino static int collect_ht_nodes (cpp_reader *, cpp_hashnode *, void *);
34*e4b17023SJohn Marino static int write_defs (cpp_reader *, cpp_hashnode *, void *);
35*e4b17023SJohn Marino static int save_macros (cpp_reader *, cpp_hashnode *, void *);
36*e4b17023SJohn Marino static int _cpp_save_pushed_macros (cpp_reader *, FILE *);
37*e4b17023SJohn Marino static int _cpp_restore_pushed_macros (cpp_reader *, FILE *);
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino /* This structure represents a macro definition on disk. */
40*e4b17023SJohn Marino struct macrodef_struct
41*e4b17023SJohn Marino {
42*e4b17023SJohn Marino unsigned int definition_length;
43*e4b17023SJohn Marino unsigned short name_length;
44*e4b17023SJohn Marino unsigned short flags;
45*e4b17023SJohn Marino };
46*e4b17023SJohn Marino
47*e4b17023SJohn Marino /* This is how we write out a macro definition.
48*e4b17023SJohn Marino Suitable for being called by cpp_forall_identifiers. */
49*e4b17023SJohn Marino
50*e4b17023SJohn Marino static int
write_macdef(cpp_reader * pfile,cpp_hashnode * hn,void * file_p)51*e4b17023SJohn Marino write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
52*e4b17023SJohn Marino {
53*e4b17023SJohn Marino FILE *f = (FILE *) file_p;
54*e4b17023SJohn Marino switch (hn->type)
55*e4b17023SJohn Marino {
56*e4b17023SJohn Marino case NT_VOID:
57*e4b17023SJohn Marino if (! (hn->flags & NODE_POISONED))
58*e4b17023SJohn Marino return 1;
59*e4b17023SJohn Marino
60*e4b17023SJohn Marino case NT_MACRO:
61*e4b17023SJohn Marino if ((hn->flags & NODE_BUILTIN)
62*e4b17023SJohn Marino && (!pfile->cb.user_builtin_macro
63*e4b17023SJohn Marino || !pfile->cb.user_builtin_macro (pfile, hn)))
64*e4b17023SJohn Marino return 1;
65*e4b17023SJohn Marino
66*e4b17023SJohn Marino {
67*e4b17023SJohn Marino struct macrodef_struct s;
68*e4b17023SJohn Marino const unsigned char *defn;
69*e4b17023SJohn Marino
70*e4b17023SJohn Marino s.name_length = NODE_LEN (hn);
71*e4b17023SJohn Marino s.flags = hn->flags & NODE_POISONED;
72*e4b17023SJohn Marino
73*e4b17023SJohn Marino if (hn->type == NT_MACRO)
74*e4b17023SJohn Marino {
75*e4b17023SJohn Marino defn = cpp_macro_definition (pfile, hn);
76*e4b17023SJohn Marino s.definition_length = ustrlen (defn);
77*e4b17023SJohn Marino }
78*e4b17023SJohn Marino else
79*e4b17023SJohn Marino {
80*e4b17023SJohn Marino defn = NODE_NAME (hn);
81*e4b17023SJohn Marino s.definition_length = s.name_length;
82*e4b17023SJohn Marino }
83*e4b17023SJohn Marino
84*e4b17023SJohn Marino if (fwrite (&s, sizeof (s), 1, f) != 1
85*e4b17023SJohn Marino || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
86*e4b17023SJohn Marino {
87*e4b17023SJohn Marino cpp_errno (pfile, CPP_DL_ERROR,
88*e4b17023SJohn Marino "while writing precompiled header");
89*e4b17023SJohn Marino return 0;
90*e4b17023SJohn Marino }
91*e4b17023SJohn Marino }
92*e4b17023SJohn Marino return 1;
93*e4b17023SJohn Marino
94*e4b17023SJohn Marino case NT_ASSERTION:
95*e4b17023SJohn Marino /* Not currently implemented. */
96*e4b17023SJohn Marino return 1;
97*e4b17023SJohn Marino
98*e4b17023SJohn Marino default:
99*e4b17023SJohn Marino abort ();
100*e4b17023SJohn Marino }
101*e4b17023SJohn Marino }
102*e4b17023SJohn Marino
103*e4b17023SJohn Marino /* This structure records the names of the defined macros.
104*e4b17023SJohn Marino It's also used as a callback structure for size_initial_idents
105*e4b17023SJohn Marino and save_idents. */
106*e4b17023SJohn Marino
107*e4b17023SJohn Marino struct cpp_savedstate
108*e4b17023SJohn Marino {
109*e4b17023SJohn Marino /* A hash table of the defined identifiers. */
110*e4b17023SJohn Marino htab_t definedhash;
111*e4b17023SJohn Marino /* The size of the definitions of those identifiers (the size of
112*e4b17023SJohn Marino 'definedstrs'). */
113*e4b17023SJohn Marino size_t hashsize;
114*e4b17023SJohn Marino /* Number of definitions */
115*e4b17023SJohn Marino size_t n_defs;
116*e4b17023SJohn Marino /* Array of definitions. In cpp_write_pch_deps it is used for sorting. */
117*e4b17023SJohn Marino cpp_hashnode **defs;
118*e4b17023SJohn Marino /* Space for the next definition. Definitions are null-terminated
119*e4b17023SJohn Marino strings. */
120*e4b17023SJohn Marino unsigned char *definedstrs;
121*e4b17023SJohn Marino };
122*e4b17023SJohn Marino
123*e4b17023SJohn Marino /* Save this identifier into the state: put it in the hash table,
124*e4b17023SJohn Marino put the definition in 'definedstrs'. */
125*e4b17023SJohn Marino
126*e4b17023SJohn Marino static int
save_idents(cpp_reader * pfile ATTRIBUTE_UNUSED,cpp_hashnode * hn,void * ss_p)127*e4b17023SJohn Marino save_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
128*e4b17023SJohn Marino {
129*e4b17023SJohn Marino struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
130*e4b17023SJohn Marino
131*e4b17023SJohn Marino if (hn->type != NT_VOID)
132*e4b17023SJohn Marino {
133*e4b17023SJohn Marino struct cpp_string news;
134*e4b17023SJohn Marino void **slot;
135*e4b17023SJohn Marino
136*e4b17023SJohn Marino news.len = NODE_LEN (hn);
137*e4b17023SJohn Marino news.text= NODE_NAME (hn);
138*e4b17023SJohn Marino slot = htab_find_slot (ss->definedhash, &news, INSERT);
139*e4b17023SJohn Marino if (*slot == NULL)
140*e4b17023SJohn Marino {
141*e4b17023SJohn Marino struct cpp_string *sp;
142*e4b17023SJohn Marino unsigned char *text;
143*e4b17023SJohn Marino
144*e4b17023SJohn Marino sp = XNEW (struct cpp_string);
145*e4b17023SJohn Marino *slot = sp;
146*e4b17023SJohn Marino
147*e4b17023SJohn Marino sp->len = NODE_LEN (hn);
148*e4b17023SJohn Marino sp->text = text = XNEWVEC (unsigned char, NODE_LEN (hn));
149*e4b17023SJohn Marino memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
150*e4b17023SJohn Marino }
151*e4b17023SJohn Marino }
152*e4b17023SJohn Marino
153*e4b17023SJohn Marino return 1;
154*e4b17023SJohn Marino }
155*e4b17023SJohn Marino
156*e4b17023SJohn Marino /* Hash some memory in a generic way. */
157*e4b17023SJohn Marino
158*e4b17023SJohn Marino static hashval_t
hashmem(const void * p_p,size_t sz)159*e4b17023SJohn Marino hashmem (const void *p_p, size_t sz)
160*e4b17023SJohn Marino {
161*e4b17023SJohn Marino const unsigned char *p = (const unsigned char *)p_p;
162*e4b17023SJohn Marino size_t i;
163*e4b17023SJohn Marino hashval_t h;
164*e4b17023SJohn Marino
165*e4b17023SJohn Marino h = 0;
166*e4b17023SJohn Marino for (i = 0; i < sz; i++)
167*e4b17023SJohn Marino h = h * 67 - (*p++ - 113);
168*e4b17023SJohn Marino return h;
169*e4b17023SJohn Marino }
170*e4b17023SJohn Marino
171*e4b17023SJohn Marino /* Hash a cpp string for the hashtable machinery. */
172*e4b17023SJohn Marino
173*e4b17023SJohn Marino static hashval_t
cpp_string_hash(const void * a_p)174*e4b17023SJohn Marino cpp_string_hash (const void *a_p)
175*e4b17023SJohn Marino {
176*e4b17023SJohn Marino const struct cpp_string *a = (const struct cpp_string *) a_p;
177*e4b17023SJohn Marino return hashmem (a->text, a->len);
178*e4b17023SJohn Marino }
179*e4b17023SJohn Marino
180*e4b17023SJohn Marino /* Compare two cpp strings for the hashtable machinery. */
181*e4b17023SJohn Marino
182*e4b17023SJohn Marino static int
cpp_string_eq(const void * a_p,const void * b_p)183*e4b17023SJohn Marino cpp_string_eq (const void *a_p, const void *b_p)
184*e4b17023SJohn Marino {
185*e4b17023SJohn Marino const struct cpp_string *a = (const struct cpp_string *) a_p;
186*e4b17023SJohn Marino const struct cpp_string *b = (const struct cpp_string *) b_p;
187*e4b17023SJohn Marino return (a->len == b->len
188*e4b17023SJohn Marino && memcmp (a->text, b->text, a->len) == 0);
189*e4b17023SJohn Marino }
190*e4b17023SJohn Marino
191*e4b17023SJohn Marino /* Save the current definitions of the cpp_reader for dependency
192*e4b17023SJohn Marino checking purposes. When writing a precompiled header, this should
193*e4b17023SJohn Marino be called at the same point in the compilation as cpp_valid_state
194*e4b17023SJohn Marino would be called when reading the precompiled header back in. */
195*e4b17023SJohn Marino
196*e4b17023SJohn Marino int
cpp_save_state(cpp_reader * r,FILE * f)197*e4b17023SJohn Marino cpp_save_state (cpp_reader *r, FILE *f)
198*e4b17023SJohn Marino {
199*e4b17023SJohn Marino /* Save the list of non-void identifiers for the dependency checking. */
200*e4b17023SJohn Marino r->savedstate = XNEW (struct cpp_savedstate);
201*e4b17023SJohn Marino r->savedstate->definedhash = htab_create (100, cpp_string_hash,
202*e4b17023SJohn Marino cpp_string_eq, NULL);
203*e4b17023SJohn Marino cpp_forall_identifiers (r, save_idents, r->savedstate);
204*e4b17023SJohn Marino
205*e4b17023SJohn Marino /* Write out the list of defined identifiers. */
206*e4b17023SJohn Marino cpp_forall_identifiers (r, write_macdef, f);
207*e4b17023SJohn Marino
208*e4b17023SJohn Marino return 0;
209*e4b17023SJohn Marino }
210*e4b17023SJohn Marino
211*e4b17023SJohn Marino /* Calculate the 'hashsize' field of the saved state. */
212*e4b17023SJohn Marino
213*e4b17023SJohn Marino static int
count_defs(cpp_reader * pfile ATTRIBUTE_UNUSED,cpp_hashnode * hn,void * ss_p)214*e4b17023SJohn Marino count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
215*e4b17023SJohn Marino {
216*e4b17023SJohn Marino struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
217*e4b17023SJohn Marino
218*e4b17023SJohn Marino switch (hn->type)
219*e4b17023SJohn Marino {
220*e4b17023SJohn Marino case NT_MACRO:
221*e4b17023SJohn Marino if (hn->flags & NODE_BUILTIN)
222*e4b17023SJohn Marino return 1;
223*e4b17023SJohn Marino
224*e4b17023SJohn Marino /* else fall through. */
225*e4b17023SJohn Marino
226*e4b17023SJohn Marino case NT_VOID:
227*e4b17023SJohn Marino {
228*e4b17023SJohn Marino struct cpp_string news;
229*e4b17023SJohn Marino void **slot;
230*e4b17023SJohn Marino
231*e4b17023SJohn Marino news.len = NODE_LEN (hn);
232*e4b17023SJohn Marino news.text = NODE_NAME (hn);
233*e4b17023SJohn Marino slot = (void **) htab_find (ss->definedhash, &news);
234*e4b17023SJohn Marino if (slot == NULL)
235*e4b17023SJohn Marino {
236*e4b17023SJohn Marino ss->hashsize += NODE_LEN (hn) + 1;
237*e4b17023SJohn Marino ss->n_defs += 1;
238*e4b17023SJohn Marino }
239*e4b17023SJohn Marino }
240*e4b17023SJohn Marino return 1;
241*e4b17023SJohn Marino
242*e4b17023SJohn Marino case NT_ASSERTION:
243*e4b17023SJohn Marino /* Not currently implemented. */
244*e4b17023SJohn Marino return 1;
245*e4b17023SJohn Marino
246*e4b17023SJohn Marino default:
247*e4b17023SJohn Marino abort ();
248*e4b17023SJohn Marino }
249*e4b17023SJohn Marino }
250*e4b17023SJohn Marino
251*e4b17023SJohn Marino /* Collect the identifiers into the state's string table. */
252*e4b17023SJohn Marino static int
write_defs(cpp_reader * pfile ATTRIBUTE_UNUSED,cpp_hashnode * hn,void * ss_p)253*e4b17023SJohn Marino write_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
254*e4b17023SJohn Marino {
255*e4b17023SJohn Marino struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
256*e4b17023SJohn Marino
257*e4b17023SJohn Marino switch (hn->type)
258*e4b17023SJohn Marino {
259*e4b17023SJohn Marino case NT_MACRO:
260*e4b17023SJohn Marino if (hn->flags & NODE_BUILTIN)
261*e4b17023SJohn Marino return 1;
262*e4b17023SJohn Marino
263*e4b17023SJohn Marino /* else fall through. */
264*e4b17023SJohn Marino
265*e4b17023SJohn Marino case NT_VOID:
266*e4b17023SJohn Marino {
267*e4b17023SJohn Marino struct cpp_string news;
268*e4b17023SJohn Marino void **slot;
269*e4b17023SJohn Marino
270*e4b17023SJohn Marino news.len = NODE_LEN (hn);
271*e4b17023SJohn Marino news.text = NODE_NAME (hn);
272*e4b17023SJohn Marino slot = (void **) htab_find (ss->definedhash, &news);
273*e4b17023SJohn Marino if (slot == NULL)
274*e4b17023SJohn Marino {
275*e4b17023SJohn Marino ss->defs[ss->n_defs] = hn;
276*e4b17023SJohn Marino ss->n_defs += 1;
277*e4b17023SJohn Marino }
278*e4b17023SJohn Marino }
279*e4b17023SJohn Marino return 1;
280*e4b17023SJohn Marino
281*e4b17023SJohn Marino case NT_ASSERTION:
282*e4b17023SJohn Marino /* Not currently implemented. */
283*e4b17023SJohn Marino return 1;
284*e4b17023SJohn Marino
285*e4b17023SJohn Marino default:
286*e4b17023SJohn Marino abort ();
287*e4b17023SJohn Marino }
288*e4b17023SJohn Marino }
289*e4b17023SJohn Marino
290*e4b17023SJohn Marino /* Comparison function for qsort. The arguments point to pointers of
291*e4b17023SJohn Marino type ht_hashnode *. */
292*e4b17023SJohn Marino static int
comp_hashnodes(const void * px,const void * py)293*e4b17023SJohn Marino comp_hashnodes (const void *px, const void *py)
294*e4b17023SJohn Marino {
295*e4b17023SJohn Marino cpp_hashnode *x = *(cpp_hashnode **) px;
296*e4b17023SJohn Marino cpp_hashnode *y = *(cpp_hashnode **) py;
297*e4b17023SJohn Marino return ustrcmp (NODE_NAME (x), NODE_NAME (y));
298*e4b17023SJohn Marino }
299*e4b17023SJohn Marino
300*e4b17023SJohn Marino /* Write out the remainder of the dependency information. This should be
301*e4b17023SJohn Marino called after the PCH is ready to be saved. */
302*e4b17023SJohn Marino
303*e4b17023SJohn Marino int
cpp_write_pch_deps(cpp_reader * r,FILE * f)304*e4b17023SJohn Marino cpp_write_pch_deps (cpp_reader *r, FILE *f)
305*e4b17023SJohn Marino {
306*e4b17023SJohn Marino struct macrodef_struct z;
307*e4b17023SJohn Marino struct cpp_savedstate *const ss = r->savedstate;
308*e4b17023SJohn Marino unsigned char *definedstrs;
309*e4b17023SJohn Marino size_t i;
310*e4b17023SJohn Marino
311*e4b17023SJohn Marino /* Collect the list of identifiers which have been seen and
312*e4b17023SJohn Marino weren't defined to anything previously. */
313*e4b17023SJohn Marino ss->hashsize = 0;
314*e4b17023SJohn Marino ss->n_defs = 0;
315*e4b17023SJohn Marino cpp_forall_identifiers (r, count_defs, ss);
316*e4b17023SJohn Marino
317*e4b17023SJohn Marino ss->defs = XNEWVEC (cpp_hashnode *, ss->n_defs);
318*e4b17023SJohn Marino ss->n_defs = 0;
319*e4b17023SJohn Marino cpp_forall_identifiers (r, write_defs, ss);
320*e4b17023SJohn Marino
321*e4b17023SJohn Marino /* Sort the list, copy it into a buffer, and write it out. */
322*e4b17023SJohn Marino qsort (ss->defs, ss->n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
323*e4b17023SJohn Marino definedstrs = ss->definedstrs = XNEWVEC (unsigned char, ss->hashsize);
324*e4b17023SJohn Marino for (i = 0; i < ss->n_defs; ++i)
325*e4b17023SJohn Marino {
326*e4b17023SJohn Marino size_t len = NODE_LEN (ss->defs[i]);
327*e4b17023SJohn Marino memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
328*e4b17023SJohn Marino definedstrs += len + 1;
329*e4b17023SJohn Marino }
330*e4b17023SJohn Marino
331*e4b17023SJohn Marino memset (&z, 0, sizeof (z));
332*e4b17023SJohn Marino z.definition_length = ss->hashsize;
333*e4b17023SJohn Marino if (fwrite (&z, sizeof (z), 1, f) != 1
334*e4b17023SJohn Marino || fwrite (ss->definedstrs, ss->hashsize, 1, f) != 1)
335*e4b17023SJohn Marino {
336*e4b17023SJohn Marino cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
337*e4b17023SJohn Marino return -1;
338*e4b17023SJohn Marino }
339*e4b17023SJohn Marino free (ss->definedstrs);
340*e4b17023SJohn Marino
341*e4b17023SJohn Marino /* Free the saved state. */
342*e4b17023SJohn Marino free (ss);
343*e4b17023SJohn Marino r->savedstate = NULL;
344*e4b17023SJohn Marino
345*e4b17023SJohn Marino /* Save the next value of __COUNTER__. */
346*e4b17023SJohn Marino if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
347*e4b17023SJohn Marino {
348*e4b17023SJohn Marino cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
349*e4b17023SJohn Marino return -1;
350*e4b17023SJohn Marino }
351*e4b17023SJohn Marino
352*e4b17023SJohn Marino return 0;
353*e4b17023SJohn Marino }
354*e4b17023SJohn Marino
355*e4b17023SJohn Marino /* Write out the definitions of the preprocessor, in a form suitable for
356*e4b17023SJohn Marino cpp_read_state. */
357*e4b17023SJohn Marino
358*e4b17023SJohn Marino int
cpp_write_pch_state(cpp_reader * r,FILE * f)359*e4b17023SJohn Marino cpp_write_pch_state (cpp_reader *r, FILE *f)
360*e4b17023SJohn Marino {
361*e4b17023SJohn Marino if (!r->deps)
362*e4b17023SJohn Marino r->deps = deps_init ();
363*e4b17023SJohn Marino
364*e4b17023SJohn Marino if (deps_save (r->deps, f) != 0)
365*e4b17023SJohn Marino {
366*e4b17023SJohn Marino cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
367*e4b17023SJohn Marino return -1;
368*e4b17023SJohn Marino }
369*e4b17023SJohn Marino
370*e4b17023SJohn Marino if (! _cpp_save_file_entries (r, f))
371*e4b17023SJohn Marino {
372*e4b17023SJohn Marino cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
373*e4b17023SJohn Marino return -1;
374*e4b17023SJohn Marino }
375*e4b17023SJohn Marino
376*e4b17023SJohn Marino /* Save the next __COUNTER__ value. When we include a precompiled header,
377*e4b17023SJohn Marino we need to start at the offset we would have if the header had been
378*e4b17023SJohn Marino included normally. */
379*e4b17023SJohn Marino if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
380*e4b17023SJohn Marino {
381*e4b17023SJohn Marino cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
382*e4b17023SJohn Marino return -1;
383*e4b17023SJohn Marino }
384*e4b17023SJohn Marino
385*e4b17023SJohn Marino /* Write saved macros. */
386*e4b17023SJohn Marino if (! _cpp_save_pushed_macros (r, f))
387*e4b17023SJohn Marino {
388*e4b17023SJohn Marino cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
389*e4b17023SJohn Marino return -1;
390*e4b17023SJohn Marino }
391*e4b17023SJohn Marino
392*e4b17023SJohn Marino return 0;
393*e4b17023SJohn Marino }
394*e4b17023SJohn Marino
395*e4b17023SJohn Marino static int
_cpp_restore_pushed_macros(cpp_reader * r,FILE * f)396*e4b17023SJohn Marino _cpp_restore_pushed_macros (cpp_reader *r, FILE *f)
397*e4b17023SJohn Marino {
398*e4b17023SJohn Marino size_t count_saved = 0;
399*e4b17023SJohn Marino size_t i;
400*e4b17023SJohn Marino struct def_pragma_macro *p;
401*e4b17023SJohn Marino size_t nlen;
402*e4b17023SJohn Marino uchar *defn;
403*e4b17023SJohn Marino size_t defnlen;
404*e4b17023SJohn Marino
405*e4b17023SJohn Marino if (fread (&count_saved, sizeof (count_saved), 1, f) != 1)
406*e4b17023SJohn Marino return 0;
407*e4b17023SJohn Marino if (! count_saved)
408*e4b17023SJohn Marino return 1;
409*e4b17023SJohn Marino for (i = 0; i < count_saved; i++)
410*e4b17023SJohn Marino {
411*e4b17023SJohn Marino if (fread (&nlen, sizeof (nlen), 1, f) != 1)
412*e4b17023SJohn Marino return 0;
413*e4b17023SJohn Marino p = XNEW (struct def_pragma_macro);
414*e4b17023SJohn Marino memset (p, 0, sizeof (struct def_pragma_macro));
415*e4b17023SJohn Marino p->name = XNEWVAR (char, nlen + 1);
416*e4b17023SJohn Marino p->name[nlen] = 0;
417*e4b17023SJohn Marino if (fread (p->name, nlen, 1, f) != 1)
418*e4b17023SJohn Marino return 0;
419*e4b17023SJohn Marino if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
420*e4b17023SJohn Marino return 0;
421*e4b17023SJohn Marino if (defnlen == 0)
422*e4b17023SJohn Marino p->is_undef = 1;
423*e4b17023SJohn Marino else
424*e4b17023SJohn Marino {
425*e4b17023SJohn Marino defn = XNEWVEC (uchar, defnlen + 1);
426*e4b17023SJohn Marino defn[defnlen] = 0;
427*e4b17023SJohn Marino
428*e4b17023SJohn Marino if (fread (defn, defnlen, 1, f) != 1)
429*e4b17023SJohn Marino return 0;
430*e4b17023SJohn Marino
431*e4b17023SJohn Marino p->definition = defn;
432*e4b17023SJohn Marino if (fread (&(p->line), sizeof (source_location), 1, f) != 1)
433*e4b17023SJohn Marino return 0;
434*e4b17023SJohn Marino defnlen = 0;
435*e4b17023SJohn Marino if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
436*e4b17023SJohn Marino return 0;
437*e4b17023SJohn Marino p->syshdr = ((defnlen & 1) != 0 ? 1 : 0);
438*e4b17023SJohn Marino p->used = ((defnlen & 2) != 0 ? 1 : 0);
439*e4b17023SJohn Marino }
440*e4b17023SJohn Marino
441*e4b17023SJohn Marino p->next = r->pushed_macros;
442*e4b17023SJohn Marino r->pushed_macros = p;
443*e4b17023SJohn Marino }
444*e4b17023SJohn Marino return 1;
445*e4b17023SJohn Marino }
446*e4b17023SJohn Marino
447*e4b17023SJohn Marino static int
_cpp_save_pushed_macros(cpp_reader * r,FILE * f)448*e4b17023SJohn Marino _cpp_save_pushed_macros (cpp_reader *r, FILE *f)
449*e4b17023SJohn Marino {
450*e4b17023SJohn Marino size_t count_saved = 0;
451*e4b17023SJohn Marino size_t i;
452*e4b17023SJohn Marino struct def_pragma_macro *p,**pp;
453*e4b17023SJohn Marino size_t defnlen;
454*e4b17023SJohn Marino
455*e4b17023SJohn Marino /* Get count. */
456*e4b17023SJohn Marino p = r->pushed_macros;
457*e4b17023SJohn Marino while (p != NULL)
458*e4b17023SJohn Marino {
459*e4b17023SJohn Marino count_saved++;
460*e4b17023SJohn Marino p = p->next;
461*e4b17023SJohn Marino }
462*e4b17023SJohn Marino if (fwrite (&count_saved, sizeof (count_saved), 1, f) != 1)
463*e4b17023SJohn Marino return 0;
464*e4b17023SJohn Marino if (!count_saved)
465*e4b17023SJohn Marino return 1;
466*e4b17023SJohn Marino
467*e4b17023SJohn Marino pp = (struct def_pragma_macro **) alloca (sizeof (struct def_pragma_macro *)
468*e4b17023SJohn Marino * count_saved);
469*e4b17023SJohn Marino /* Store them in reverse order. */
470*e4b17023SJohn Marino p = r->pushed_macros;
471*e4b17023SJohn Marino i = count_saved;
472*e4b17023SJohn Marino while (p != NULL)
473*e4b17023SJohn Marino {
474*e4b17023SJohn Marino --i;
475*e4b17023SJohn Marino pp[i] = p;
476*e4b17023SJohn Marino p = p->next;
477*e4b17023SJohn Marino }
478*e4b17023SJohn Marino for (i = 0; i < count_saved; i++)
479*e4b17023SJohn Marino {
480*e4b17023SJohn Marino defnlen = strlen (pp[i]->name);
481*e4b17023SJohn Marino if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
482*e4b17023SJohn Marino || fwrite (pp[i]->name, defnlen, 1, f) != 1)
483*e4b17023SJohn Marino return 0;
484*e4b17023SJohn Marino if (pp[i]->is_undef)
485*e4b17023SJohn Marino {
486*e4b17023SJohn Marino defnlen = 0;
487*e4b17023SJohn Marino if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1)
488*e4b17023SJohn Marino return 0;
489*e4b17023SJohn Marino }
490*e4b17023SJohn Marino else
491*e4b17023SJohn Marino {
492*e4b17023SJohn Marino defnlen = ustrlen (pp[i]->definition);
493*e4b17023SJohn Marino if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
494*e4b17023SJohn Marino || fwrite (pp[i]->definition, defnlen, 1, f) != 1)
495*e4b17023SJohn Marino return 0;
496*e4b17023SJohn Marino if (fwrite (&(pp[i]->line), sizeof (source_location), 1, f) != 1)
497*e4b17023SJohn Marino return 0;
498*e4b17023SJohn Marino defnlen = 0;
499*e4b17023SJohn Marino defnlen |= (pp[i]->syshdr != 0 ? 1 : 0);
500*e4b17023SJohn Marino defnlen |= (pp[i]->used != 0 ? 2 : 0);
501*e4b17023SJohn Marino if (fwrite (&defnlen, sizeof (defnlen), 1, f) != 1)
502*e4b17023SJohn Marino return 0;
503*e4b17023SJohn Marino }
504*e4b17023SJohn Marino }
505*e4b17023SJohn Marino return 1;
506*e4b17023SJohn Marino }
507*e4b17023SJohn Marino
508*e4b17023SJohn Marino
509*e4b17023SJohn Marino /* Data structure to transform hash table nodes into a sorted list */
510*e4b17023SJohn Marino
511*e4b17023SJohn Marino struct ht_node_list
512*e4b17023SJohn Marino {
513*e4b17023SJohn Marino /* Array of nodes */
514*e4b17023SJohn Marino cpp_hashnode **defs;
515*e4b17023SJohn Marino /* Number of nodes in the array */
516*e4b17023SJohn Marino size_t n_defs;
517*e4b17023SJohn Marino /* Size of the allocated array */
518*e4b17023SJohn Marino size_t asize;
519*e4b17023SJohn Marino };
520*e4b17023SJohn Marino
521*e4b17023SJohn Marino /* Callback for collecting identifiers from hash table */
522*e4b17023SJohn Marino
523*e4b17023SJohn Marino static int
collect_ht_nodes(cpp_reader * pfile ATTRIBUTE_UNUSED,cpp_hashnode * hn,void * nl_p)524*e4b17023SJohn Marino collect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
525*e4b17023SJohn Marino void *nl_p)
526*e4b17023SJohn Marino {
527*e4b17023SJohn Marino struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
528*e4b17023SJohn Marino
529*e4b17023SJohn Marino if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
530*e4b17023SJohn Marino {
531*e4b17023SJohn Marino if (nl->n_defs == nl->asize)
532*e4b17023SJohn Marino {
533*e4b17023SJohn Marino nl->asize *= 2;
534*e4b17023SJohn Marino nl->defs = XRESIZEVEC (cpp_hashnode *, nl->defs, nl->asize);
535*e4b17023SJohn Marino }
536*e4b17023SJohn Marino
537*e4b17023SJohn Marino nl->defs[nl->n_defs] = hn;
538*e4b17023SJohn Marino ++nl->n_defs;
539*e4b17023SJohn Marino }
540*e4b17023SJohn Marino return 1;
541*e4b17023SJohn Marino }
542*e4b17023SJohn Marino
543*e4b17023SJohn Marino
544*e4b17023SJohn Marino /* Return nonzero if FD is a precompiled header which is consistent
545*e4b17023SJohn Marino with the preprocessor's current definitions. It will be consistent
546*e4b17023SJohn Marino when:
547*e4b17023SJohn Marino
548*e4b17023SJohn Marino - anything that was defined just before the PCH was generated
549*e4b17023SJohn Marino is defined the same way now; and
550*e4b17023SJohn Marino - anything that was not defined then, but is defined now, was not
551*e4b17023SJohn Marino used by the PCH.
552*e4b17023SJohn Marino
553*e4b17023SJohn Marino NAME is used to print warnings if `warn_invalid_pch' is set in the
554*e4b17023SJohn Marino reader's flags.
555*e4b17023SJohn Marino */
556*e4b17023SJohn Marino
557*e4b17023SJohn Marino int
cpp_valid_state(cpp_reader * r,const char * name,int fd)558*e4b17023SJohn Marino cpp_valid_state (cpp_reader *r, const char *name, int fd)
559*e4b17023SJohn Marino {
560*e4b17023SJohn Marino struct macrodef_struct m;
561*e4b17023SJohn Marino size_t namebufsz = 256;
562*e4b17023SJohn Marino unsigned char *namebuf = XNEWVEC (unsigned char, namebufsz);
563*e4b17023SJohn Marino unsigned char *undeftab = NULL;
564*e4b17023SJohn Marino struct ht_node_list nl = { 0, 0, 0 };
565*e4b17023SJohn Marino unsigned char *first, *last;
566*e4b17023SJohn Marino unsigned int i;
567*e4b17023SJohn Marino unsigned int counter;
568*e4b17023SJohn Marino
569*e4b17023SJohn Marino /* Read in the list of identifiers that must be defined
570*e4b17023SJohn Marino Check that they are defined in the same way. */
571*e4b17023SJohn Marino for (;;)
572*e4b17023SJohn Marino {
573*e4b17023SJohn Marino cpp_hashnode *h;
574*e4b17023SJohn Marino const unsigned char *newdefn;
575*e4b17023SJohn Marino
576*e4b17023SJohn Marino if (read (fd, &m, sizeof (m)) != sizeof (m))
577*e4b17023SJohn Marino goto error;
578*e4b17023SJohn Marino
579*e4b17023SJohn Marino if (m.name_length == 0)
580*e4b17023SJohn Marino break;
581*e4b17023SJohn Marino
582*e4b17023SJohn Marino /* If this file is already preprocessed, there won't be any
583*e4b17023SJohn Marino macros defined, and that's OK. */
584*e4b17023SJohn Marino if (CPP_OPTION (r, preprocessed))
585*e4b17023SJohn Marino {
586*e4b17023SJohn Marino if (lseek (fd, m.definition_length, SEEK_CUR) == -1)
587*e4b17023SJohn Marino goto error;
588*e4b17023SJohn Marino continue;
589*e4b17023SJohn Marino }
590*e4b17023SJohn Marino
591*e4b17023SJohn Marino if (m.definition_length > namebufsz)
592*e4b17023SJohn Marino {
593*e4b17023SJohn Marino free (namebuf);
594*e4b17023SJohn Marino namebufsz = m.definition_length + 256;
595*e4b17023SJohn Marino namebuf = XNEWVEC (unsigned char, namebufsz);
596*e4b17023SJohn Marino }
597*e4b17023SJohn Marino
598*e4b17023SJohn Marino if ((size_t)read (fd, namebuf, m.definition_length)
599*e4b17023SJohn Marino != m.definition_length)
600*e4b17023SJohn Marino goto error;
601*e4b17023SJohn Marino
602*e4b17023SJohn Marino h = cpp_lookup (r, namebuf, m.name_length);
603*e4b17023SJohn Marino if (m.flags & NODE_POISONED
604*e4b17023SJohn Marino || h->flags & NODE_POISONED)
605*e4b17023SJohn Marino {
606*e4b17023SJohn Marino if (CPP_OPTION (r, warn_invalid_pch))
607*e4b17023SJohn Marino cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
608*e4b17023SJohn Marino "%s: not used because `%.*s' is poisoned",
609*e4b17023SJohn Marino name, m.name_length, namebuf);
610*e4b17023SJohn Marino goto fail;
611*e4b17023SJohn Marino }
612*e4b17023SJohn Marino
613*e4b17023SJohn Marino if (h->type != NT_MACRO)
614*e4b17023SJohn Marino {
615*e4b17023SJohn Marino /* It's ok if __GCC_HAVE_DWARF2_CFI_ASM becomes undefined,
616*e4b17023SJohn Marino as in, when the PCH file is created with -g and we're
617*e4b17023SJohn Marino attempting to use it without -g. Restoring the PCH file
618*e4b17023SJohn Marino is supposed to bring in this definition *and* enable the
619*e4b17023SJohn Marino generation of call frame information, so that precompiled
620*e4b17023SJohn Marino definitions that take this macro into accout, to decide
621*e4b17023SJohn Marino what asm to emit, won't issue .cfi directives when the
622*e4b17023SJohn Marino compiler doesn't. */
623*e4b17023SJohn Marino if (!(h->flags & NODE_USED)
624*e4b17023SJohn Marino && m.name_length == sizeof ("__GCC_HAVE_DWARF2_CFI_ASM") - 1
625*e4b17023SJohn Marino && !memcmp (namebuf, "__GCC_HAVE_DWARF2_CFI_ASM", m.name_length))
626*e4b17023SJohn Marino continue;
627*e4b17023SJohn Marino
628*e4b17023SJohn Marino if (CPP_OPTION (r, warn_invalid_pch))
629*e4b17023SJohn Marino cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
630*e4b17023SJohn Marino "%s: not used because `%.*s' not defined",
631*e4b17023SJohn Marino name, m.name_length, namebuf);
632*e4b17023SJohn Marino goto fail;
633*e4b17023SJohn Marino }
634*e4b17023SJohn Marino
635*e4b17023SJohn Marino newdefn = cpp_macro_definition (r, h);
636*e4b17023SJohn Marino
637*e4b17023SJohn Marino if (m.definition_length != ustrlen (newdefn)
638*e4b17023SJohn Marino || memcmp (namebuf, newdefn, m.definition_length) != 0)
639*e4b17023SJohn Marino {
640*e4b17023SJohn Marino if (CPP_OPTION (r, warn_invalid_pch))
641*e4b17023SJohn Marino cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
642*e4b17023SJohn Marino "%s: not used because `%.*s' defined as `%s' not `%.*s'",
643*e4b17023SJohn Marino name, m.name_length, namebuf, newdefn + m.name_length,
644*e4b17023SJohn Marino m.definition_length - m.name_length,
645*e4b17023SJohn Marino namebuf + m.name_length);
646*e4b17023SJohn Marino goto fail;
647*e4b17023SJohn Marino }
648*e4b17023SJohn Marino }
649*e4b17023SJohn Marino free (namebuf);
650*e4b17023SJohn Marino namebuf = NULL;
651*e4b17023SJohn Marino
652*e4b17023SJohn Marino /* Read in the list of identifiers that must not be defined.
653*e4b17023SJohn Marino Check that they really aren't. */
654*e4b17023SJohn Marino undeftab = XNEWVEC (unsigned char, m.definition_length);
655*e4b17023SJohn Marino if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
656*e4b17023SJohn Marino goto error;
657*e4b17023SJohn Marino
658*e4b17023SJohn Marino /* Collect identifiers from the current hash table. */
659*e4b17023SJohn Marino nl.n_defs = 0;
660*e4b17023SJohn Marino nl.asize = 10;
661*e4b17023SJohn Marino nl.defs = XNEWVEC (cpp_hashnode *, nl.asize);
662*e4b17023SJohn Marino cpp_forall_identifiers (r, &collect_ht_nodes, &nl);
663*e4b17023SJohn Marino qsort (nl.defs, nl.n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
664*e4b17023SJohn Marino
665*e4b17023SJohn Marino /* Loop through nl.defs and undeftab, both of which are sorted lists.
666*e4b17023SJohn Marino There should be no matches. */
667*e4b17023SJohn Marino first = undeftab;
668*e4b17023SJohn Marino last = undeftab + m.definition_length;
669*e4b17023SJohn Marino i = 0;
670*e4b17023SJohn Marino
671*e4b17023SJohn Marino while (first < last && i < nl.n_defs)
672*e4b17023SJohn Marino {
673*e4b17023SJohn Marino int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
674*e4b17023SJohn Marino
675*e4b17023SJohn Marino if (cmp < 0)
676*e4b17023SJohn Marino first += ustrlen (first) + 1;
677*e4b17023SJohn Marino else if (cmp > 0)
678*e4b17023SJohn Marino ++i;
679*e4b17023SJohn Marino else
680*e4b17023SJohn Marino {
681*e4b17023SJohn Marino if (CPP_OPTION (r, warn_invalid_pch))
682*e4b17023SJohn Marino cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
683*e4b17023SJohn Marino "%s: not used because `%s' is defined",
684*e4b17023SJohn Marino name, first);
685*e4b17023SJohn Marino goto fail;
686*e4b17023SJohn Marino }
687*e4b17023SJohn Marino }
688*e4b17023SJohn Marino
689*e4b17023SJohn Marino free(nl.defs);
690*e4b17023SJohn Marino nl.defs = NULL;
691*e4b17023SJohn Marino free (undeftab);
692*e4b17023SJohn Marino undeftab = NULL;
693*e4b17023SJohn Marino
694*e4b17023SJohn Marino /* Read in the next value of __COUNTER__.
695*e4b17023SJohn Marino Check that (a) __COUNTER__ was not used in the pch or (b) __COUNTER__
696*e4b17023SJohn Marino has not been used in this translation unit. */
697*e4b17023SJohn Marino if (read (fd, &counter, sizeof (counter)) != sizeof (counter))
698*e4b17023SJohn Marino goto error;
699*e4b17023SJohn Marino if (counter && r->counter)
700*e4b17023SJohn Marino {
701*e4b17023SJohn Marino if (CPP_OPTION (r, warn_invalid_pch))
702*e4b17023SJohn Marino cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
703*e4b17023SJohn Marino "%s: not used because `__COUNTER__' is invalid",
704*e4b17023SJohn Marino name);
705*e4b17023SJohn Marino goto fail;
706*e4b17023SJohn Marino }
707*e4b17023SJohn Marino
708*e4b17023SJohn Marino /* We win! */
709*e4b17023SJohn Marino return 0;
710*e4b17023SJohn Marino
711*e4b17023SJohn Marino error:
712*e4b17023SJohn Marino cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
713*e4b17023SJohn Marino return -1;
714*e4b17023SJohn Marino
715*e4b17023SJohn Marino fail:
716*e4b17023SJohn Marino free (namebuf);
717*e4b17023SJohn Marino free (undeftab);
718*e4b17023SJohn Marino free (nl.defs);
719*e4b17023SJohn Marino return 1;
720*e4b17023SJohn Marino }
721*e4b17023SJohn Marino
722*e4b17023SJohn Marino /* Save all the existing macros. */
723*e4b17023SJohn Marino
724*e4b17023SJohn Marino struct save_macro_data
725*e4b17023SJohn Marino {
726*e4b17023SJohn Marino uchar **defns;
727*e4b17023SJohn Marino size_t count;
728*e4b17023SJohn Marino size_t array_size;
729*e4b17023SJohn Marino char **saved_pragmas;
730*e4b17023SJohn Marino };
731*e4b17023SJohn Marino
732*e4b17023SJohn Marino /* Save the definition of a single macro, so that it will persist
733*e4b17023SJohn Marino across a PCH restore. Because macro data is in GCed memory, which
734*e4b17023SJohn Marino will be blown away by PCH, it must be temporarily copied to
735*e4b17023SJohn Marino malloced memory. (The macros will refer to identifier nodes which
736*e4b17023SJohn Marino are also GCed and so on, so the copying is done by turning them
737*e4b17023SJohn Marino into self-contained strings.) The assumption is that most macro
738*e4b17023SJohn Marino definitions will come from the PCH file, not from the compilation
739*e4b17023SJohn Marino before the PCH file is loaded, so it doesn't matter that this is
740*e4b17023SJohn Marino a little expensive.
741*e4b17023SJohn Marino
742*e4b17023SJohn Marino It would reduce the cost even further if macros defined in the PCH
743*e4b17023SJohn Marino file were not saved in this way, but this is not done (yet), except
744*e4b17023SJohn Marino for builtins, and for #assert by default. */
745*e4b17023SJohn Marino
746*e4b17023SJohn Marino static int
save_macros(cpp_reader * r,cpp_hashnode * h,void * data_p)747*e4b17023SJohn Marino save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
748*e4b17023SJohn Marino {
749*e4b17023SJohn Marino struct save_macro_data *data = (struct save_macro_data *)data_p;
750*e4b17023SJohn Marino
751*e4b17023SJohn Marino if ((h->flags & NODE_BUILTIN)
752*e4b17023SJohn Marino && h->type == NT_MACRO
753*e4b17023SJohn Marino && r->cb.user_builtin_macro)
754*e4b17023SJohn Marino r->cb.user_builtin_macro (r, h);
755*e4b17023SJohn Marino
756*e4b17023SJohn Marino if (h->type != NT_VOID
757*e4b17023SJohn Marino && (h->flags & NODE_BUILTIN) == 0)
758*e4b17023SJohn Marino {
759*e4b17023SJohn Marino if (data->count == data->array_size)
760*e4b17023SJohn Marino {
761*e4b17023SJohn Marino data->array_size *= 2;
762*e4b17023SJohn Marino data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size));
763*e4b17023SJohn Marino }
764*e4b17023SJohn Marino
765*e4b17023SJohn Marino switch (h->type)
766*e4b17023SJohn Marino {
767*e4b17023SJohn Marino case NT_ASSERTION:
768*e4b17023SJohn Marino /* Not currently implemented. */
769*e4b17023SJohn Marino return 1;
770*e4b17023SJohn Marino
771*e4b17023SJohn Marino case NT_MACRO:
772*e4b17023SJohn Marino {
773*e4b17023SJohn Marino const uchar * defn = cpp_macro_definition (r, h);
774*e4b17023SJohn Marino size_t defnlen = ustrlen (defn);
775*e4b17023SJohn Marino
776*e4b17023SJohn Marino data->defns[data->count] = (uchar *) xmemdup (defn, defnlen,
777*e4b17023SJohn Marino defnlen + 2);
778*e4b17023SJohn Marino data->defns[data->count][defnlen] = '\n';
779*e4b17023SJohn Marino }
780*e4b17023SJohn Marino break;
781*e4b17023SJohn Marino
782*e4b17023SJohn Marino default:
783*e4b17023SJohn Marino abort ();
784*e4b17023SJohn Marino }
785*e4b17023SJohn Marino data->count++;
786*e4b17023SJohn Marino }
787*e4b17023SJohn Marino return 1;
788*e4b17023SJohn Marino }
789*e4b17023SJohn Marino
790*e4b17023SJohn Marino /* Prepare to restore the state, by saving the currently-defined
791*e4b17023SJohn Marino macros in 'data'. */
792*e4b17023SJohn Marino
793*e4b17023SJohn Marino void
cpp_prepare_state(cpp_reader * r,struct save_macro_data ** data)794*e4b17023SJohn Marino cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
795*e4b17023SJohn Marino {
796*e4b17023SJohn Marino struct save_macro_data *d = XNEW (struct save_macro_data);
797*e4b17023SJohn Marino
798*e4b17023SJohn Marino d->array_size = 512;
799*e4b17023SJohn Marino d->defns = XNEWVEC (uchar *, d->array_size);
800*e4b17023SJohn Marino d->count = 0;
801*e4b17023SJohn Marino cpp_forall_identifiers (r, save_macros, d);
802*e4b17023SJohn Marino d->saved_pragmas = _cpp_save_pragma_names (r);
803*e4b17023SJohn Marino *data = d;
804*e4b17023SJohn Marino }
805*e4b17023SJohn Marino
806*e4b17023SJohn Marino /* Given a precompiled header that was previously determined to be valid,
807*e4b17023SJohn Marino apply all its definitions (and undefinitions) to the current state.
808*e4b17023SJohn Marino DEPNAME is passed to deps_restore. */
809*e4b17023SJohn Marino
810*e4b17023SJohn Marino int
cpp_read_state(cpp_reader * r,const char * name,FILE * f,struct save_macro_data * data)811*e4b17023SJohn Marino cpp_read_state (cpp_reader *r, const char *name, FILE *f,
812*e4b17023SJohn Marino struct save_macro_data *data)
813*e4b17023SJohn Marino {
814*e4b17023SJohn Marino size_t i;
815*e4b17023SJohn Marino struct lexer_state old_state;
816*e4b17023SJohn Marino unsigned int counter;
817*e4b17023SJohn Marino
818*e4b17023SJohn Marino /* Restore spec_nodes, which will be full of references to the old
819*e4b17023SJohn Marino hashtable entries and so will now be invalid. */
820*e4b17023SJohn Marino {
821*e4b17023SJohn Marino struct spec_nodes *s = &r->spec_nodes;
822*e4b17023SJohn Marino s->n_defined = cpp_lookup (r, DSC("defined"));
823*e4b17023SJohn Marino s->n_true = cpp_lookup (r, DSC("true"));
824*e4b17023SJohn Marino s->n_false = cpp_lookup (r, DSC("false"));
825*e4b17023SJohn Marino s->n__VA_ARGS__ = cpp_lookup (r, DSC("__VA_ARGS__"));
826*e4b17023SJohn Marino }
827*e4b17023SJohn Marino
828*e4b17023SJohn Marino old_state = r->state;
829*e4b17023SJohn Marino r->state.in_directive = 1;
830*e4b17023SJohn Marino r->state.prevent_expansion = 1;
831*e4b17023SJohn Marino r->state.angled_headers = 0;
832*e4b17023SJohn Marino
833*e4b17023SJohn Marino /* Run through the carefully-saved macros, insert them. */
834*e4b17023SJohn Marino for (i = 0; i < data->count; i++)
835*e4b17023SJohn Marino {
836*e4b17023SJohn Marino cpp_hashnode *h;
837*e4b17023SJohn Marino size_t namelen;
838*e4b17023SJohn Marino uchar *defn;
839*e4b17023SJohn Marino
840*e4b17023SJohn Marino namelen = ustrcspn (data->defns[i], "( \n");
841*e4b17023SJohn Marino h = cpp_lookup (r, data->defns[i], namelen);
842*e4b17023SJohn Marino defn = data->defns[i] + namelen;
843*e4b17023SJohn Marino
844*e4b17023SJohn Marino /* The PCH file is valid, so we know that if there is a definition
845*e4b17023SJohn Marino from the PCH file it must be the same as the one we had
846*e4b17023SJohn Marino originally, and so do not need to restore it. */
847*e4b17023SJohn Marino if (h->type == NT_VOID)
848*e4b17023SJohn Marino {
849*e4b17023SJohn Marino if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
850*e4b17023SJohn Marino != NULL)
851*e4b17023SJohn Marino {
852*e4b17023SJohn Marino _cpp_clean_line (r);
853*e4b17023SJohn Marino if (!_cpp_create_definition (r, h))
854*e4b17023SJohn Marino abort ();
855*e4b17023SJohn Marino _cpp_pop_buffer (r);
856*e4b17023SJohn Marino }
857*e4b17023SJohn Marino else
858*e4b17023SJohn Marino abort ();
859*e4b17023SJohn Marino }
860*e4b17023SJohn Marino
861*e4b17023SJohn Marino free (data->defns[i]);
862*e4b17023SJohn Marino }
863*e4b17023SJohn Marino r->state = old_state;
864*e4b17023SJohn Marino
865*e4b17023SJohn Marino _cpp_restore_pragma_names (r, data->saved_pragmas);
866*e4b17023SJohn Marino
867*e4b17023SJohn Marino free (data);
868*e4b17023SJohn Marino
869*e4b17023SJohn Marino if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
870*e4b17023SJohn Marino != 0)
871*e4b17023SJohn Marino goto error;
872*e4b17023SJohn Marino
873*e4b17023SJohn Marino if (! _cpp_read_file_entries (r, f))
874*e4b17023SJohn Marino goto error;
875*e4b17023SJohn Marino
876*e4b17023SJohn Marino if (fread (&counter, sizeof (counter), 1, f) != 1)
877*e4b17023SJohn Marino goto error;
878*e4b17023SJohn Marino
879*e4b17023SJohn Marino if (!r->counter)
880*e4b17023SJohn Marino r->counter = counter;
881*e4b17023SJohn Marino
882*e4b17023SJohn Marino /* Read pushed macros. */
883*e4b17023SJohn Marino if (! _cpp_restore_pushed_macros (r, f))
884*e4b17023SJohn Marino goto error;
885*e4b17023SJohn Marino return 0;
886*e4b17023SJohn Marino
887*e4b17023SJohn Marino error:
888*e4b17023SJohn Marino cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
889*e4b17023SJohn Marino return -1;
890*e4b17023SJohn Marino }
891