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