2003-03-13 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / gcc / cpppch.c
blob05ec2e36d7c9d426d0d8dcfb6c17b0ecac465514
1 /* Part of CPP library. (Precompiled header reading/writing.)
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
21 #include "cpplib.h"
22 #include "cpphash.h"
23 #include "intl.h"
24 #include "hashtab.h"
25 #include "mkdeps.h"
27 static int write_macdef PARAMS ((cpp_reader *, cpp_hashnode *, void *));
28 static int save_idents PARAMS ((cpp_reader *, cpp_hashnode *, void *));
29 static hashval_t hashmem PARAMS ((const void *, size_t));
30 static hashval_t cpp_string_hash PARAMS ((const void *));
31 static int cpp_string_eq PARAMS ((const void *, const void *));
32 static int count_defs PARAMS ((cpp_reader *, cpp_hashnode *, void *));
33 static int write_defs PARAMS ((cpp_reader *, cpp_hashnode *, void *));
34 static int save_macros PARAMS ((cpp_reader *, cpp_hashnode *, void *));
35 static int reset_ht PARAMS ((cpp_reader *, cpp_hashnode *, void *));
37 /* This structure represents a macro definition on disk. */
38 struct macrodef_struct
40 unsigned int definition_length;
41 unsigned short name_length;
42 unsigned short flags;
45 /* This is how we write out a macro definition.
46 Suitable for being called by cpp_forall_identifiers. */
48 static int
49 write_macdef (pfile, hn, file_p)
50 cpp_reader *pfile;
51 cpp_hashnode *hn;
52 void *file_p;
54 FILE *f = (FILE *) file_p;
55 switch (hn->type)
57 case NT_VOID:
58 if (! (hn->flags & NODE_POISONED))
59 return 1;
61 case NT_MACRO:
62 if ((hn->flags & NODE_BUILTIN))
63 return 1;
66 struct macrodef_struct s;
67 const unsigned char *defn;
69 s.name_length = NODE_LEN (hn);
70 s.flags = hn->flags & NODE_POISONED;
72 if (hn->type == NT_MACRO)
74 defn = cpp_macro_definition (pfile, hn);
75 s.definition_length = ustrlen (defn);
77 else
79 defn = NODE_NAME (hn);
80 s.definition_length = s.name_length;
83 if (fwrite (&s, sizeof (s), 1, f) != 1
84 || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
86 cpp_errno (pfile, DL_ERROR, "while writing precompiled header");
87 return 0;
90 return 1;
92 case NT_ASSERTION:
93 /* Not currently implemented. */
94 return 1;
96 default:
97 abort ();
101 /* This structure records the names of the defined macros.
102 It's also used as a callback structure for size_initial_idents
103 and save_idents. */
105 struct cpp_savedstate
107 /* A hash table of the defined identifiers. */
108 htab_t definedhash;
109 /* The size of the definitions of those identifiers (the size of
110 'definedstrs'). */
111 size_t hashsize;
112 /* Space for the next definition. Definitions are null-terminated
113 strings. */
114 unsigned char *definedstrs;
117 /* Save this identifier into the state: put it in the hash table,
118 put the definition in 'definedstrs'. */
120 static int
121 save_idents (pfile, hn, ss_p)
122 cpp_reader *pfile ATTRIBUTE_UNUSED;
123 cpp_hashnode *hn;
124 void *ss_p;
126 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
128 if (hn->type != NT_VOID)
130 struct cpp_string news;
131 void **slot;
133 news.len = NODE_LEN (hn);
134 news.text= NODE_NAME (hn);
135 slot = htab_find_slot (ss->definedhash, &news, INSERT);
136 if (*slot == NULL)
138 struct cpp_string *sp;
139 unsigned char *text;
141 sp = xmalloc (sizeof (struct cpp_string));
142 *slot = sp;
144 sp->len = NODE_LEN (hn);
145 sp->text = text = xmalloc (NODE_LEN (hn));
146 memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
150 return 1;
153 /* Hash some memory in a generic way. */
155 static hashval_t
156 hashmem (p_p, sz)
157 const void *p_p;
158 size_t sz;
160 const unsigned char *p = (const unsigned char *)p_p;
161 size_t i;
162 hashval_t h;
164 h = 0;
165 for (i = 0; i < sz; i++)
166 h = h * 67 - (*p++ - 113);
167 return h;
170 /* Hash a cpp string for the hashtable machinery. */
172 static hashval_t
173 cpp_string_hash (a_p)
174 const void *a_p;
176 const struct cpp_string *a = (const struct cpp_string *) a_p;
177 return hashmem (a->text, a->len);
180 /* Compare two cpp strings for the hashtable machinery. */
182 static int
183 cpp_string_eq (a_p, b_p)
184 const void *a_p;
185 const void *b_p;
187 const struct cpp_string *a = (const struct cpp_string *) a_p;
188 const struct cpp_string *b = (const struct cpp_string *) b_p;
189 return (a->len == b->len
190 && memcmp (a->text, b->text, a->len) == 0);
193 /* Save the current definitions of the cpp_reader for dependency
194 checking purposes. When writing a precompiled header, this should
195 be called at the same point in the compilation as cpp_valid_state
196 would be called when reading the precompiled header back in. */
199 cpp_save_state (r, f)
200 cpp_reader *r;
201 FILE *f;
203 /* Save the list of non-void identifiers for the dependency checking. */
204 r->savedstate = xmalloc (sizeof (struct cpp_savedstate));
205 r->savedstate->definedhash = htab_create (100, cpp_string_hash,
206 cpp_string_eq, NULL);
207 cpp_forall_identifiers (r, save_idents, r->savedstate);
209 /* Write out the list of defined identifiers. */
210 cpp_forall_identifiers (r, write_macdef, f);
212 return 0;
215 /* Calculate the 'hashsize' field of the saved state. */
217 static int
218 count_defs (pfile, hn, ss_p)
219 cpp_reader *pfile ATTRIBUTE_UNUSED;
220 cpp_hashnode *hn;
221 void *ss_p;
223 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
225 switch (hn->type)
227 case NT_MACRO:
228 if (hn->flags & NODE_BUILTIN)
229 return 1;
231 /* else fall through. */
233 case NT_VOID:
235 struct cpp_string news;
236 void **slot;
238 news.len = NODE_LEN (hn);
239 news.text = NODE_NAME (hn);
240 slot = htab_find (ss->definedhash, &news);
241 if (slot == NULL)
242 ss->hashsize += NODE_LEN (hn) + 1;
244 return 1;
246 case NT_ASSERTION:
247 /* Not currently implemented. */
248 return 1;
250 default:
251 abort ();
255 /* Write the identifiers into 'definedstrs' of the state. */
257 static int
258 write_defs (pfile, hn, ss_p)
259 cpp_reader *pfile ATTRIBUTE_UNUSED;
260 cpp_hashnode *hn;
261 void *ss_p;
263 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
265 switch (hn->type)
267 case NT_MACRO:
268 if (hn->flags & NODE_BUILTIN)
269 return 1;
271 /* else fall through. */
273 case NT_VOID:
275 struct cpp_string news;
276 void **slot;
278 news.len = NODE_LEN (hn);
279 news.text = NODE_NAME (hn);
280 slot = htab_find (ss->definedhash, &news);
281 if (slot == NULL)
283 memcpy (ss->definedstrs, NODE_NAME (hn), NODE_LEN (hn));
284 ss->definedstrs[NODE_LEN (hn)] = 0;
285 ss->definedstrs += NODE_LEN (hn) + 1;
288 return 1;
290 case NT_ASSERTION:
291 /* Not currently implemented. */
292 return 1;
294 default:
295 abort ();
299 /* Write out the remainder of the dependency information. This should be
300 called after the PCH is ready to be saved. */
303 cpp_write_pch_deps (r, f)
304 cpp_reader *r;
305 FILE *f;
307 struct macrodef_struct z;
308 struct cpp_savedstate *const ss = r->savedstate;
309 unsigned char *definedstrs;
311 ss->hashsize = 0;
313 /* Write out the list of identifiers which have been seen and
314 weren't defined to anything previously. */
315 cpp_forall_identifiers (r, count_defs, ss);
316 definedstrs = ss->definedstrs = xmalloc (ss->hashsize);
317 cpp_forall_identifiers (r, write_defs, ss);
318 memset (&z, 0, sizeof (z));
319 z.definition_length = ss->hashsize;
320 if (fwrite (&z, sizeof (z), 1, f) != 1
321 || fwrite (definedstrs, ss->hashsize, 1, f) != 1)
323 cpp_errno (r, DL_ERROR, "while writing precompiled header");
324 return -1;
326 free (definedstrs);
328 /* Free the saved state. */
329 free (ss);
330 r->savedstate = NULL;
331 return 0;
334 /* Write out the definitions of the preprocessor, in a form suitable for
335 cpp_read_state. */
338 cpp_write_pch_state (r, f)
339 cpp_reader *r;
340 FILE *f;
342 struct macrodef_struct z;
344 /* Write out the list of defined identifiers. */
345 cpp_forall_identifiers (r, write_macdef, f);
346 memset (&z, 0, sizeof (z));
347 if (fwrite (&z, sizeof (z), 1, f) != 1)
349 cpp_errno (r, DL_ERROR, "while writing precompiled header");
350 return -1;
353 if (!r->deps)
354 r->deps = deps_init ();
356 if (deps_save (r->deps, f) != 0)
358 cpp_errno (r, DL_ERROR, "while writing precompiled header");
359 return -1;
362 return 0;
365 /* Return nonzero if FD is a precompiled header which is consistent
366 with the preprocessor's current definitions. It will be consistent
367 when:
369 - anything that was defined just before the PCH was generated
370 is defined the same way now; and
371 - anything that was not defined then, but is defined now, was not
372 used by the PCH.
374 NAME is used to print warnings if `warn_invalid_pch' is set in the
375 reader's flags.
379 cpp_valid_state (r, name, fd)
380 cpp_reader *r;
381 const char *name;
382 int fd;
384 struct macrodef_struct m;
385 size_t namebufsz = 256;
386 unsigned char *namebuf = xmalloc (namebufsz);
387 unsigned char *undeftab = NULL;
388 unsigned int i;
390 /* Read in the list of identifiers that must be defined
391 Check that they are defined in the same way. */
392 for (;;)
394 cpp_hashnode *h;
395 const unsigned char *newdefn;
397 if (read (fd, &m, sizeof (m)) != sizeof (m))
398 goto error;
400 if (m.name_length == 0)
401 break;
403 if (m.definition_length > namebufsz)
405 free (namebuf);
406 namebufsz = m.definition_length + 256;
407 namebuf = xmalloc (namebufsz);
410 if ((size_t)read (fd, namebuf, m.definition_length)
411 != m.definition_length)
412 goto error;
414 h = cpp_lookup (r, namebuf, m.name_length);
415 if (m.flags & NODE_POISONED
416 || h->type != NT_MACRO
417 || h->flags & NODE_POISONED)
419 if (CPP_OPTION (r, warn_invalid_pch))
420 cpp_error (r, DL_WARNING_SYSHDR,
421 "%s: not used because `%.*s' not defined",
422 name, m.name_length, namebuf);
423 goto fail;
426 newdefn = cpp_macro_definition (r, h);
428 if (m.definition_length != ustrlen (newdefn)
429 || memcmp (namebuf, newdefn, m.definition_length) != 0)
431 if (CPP_OPTION (r, warn_invalid_pch))
432 cpp_error (r, DL_WARNING_SYSHDR,
433 "%s: not used because `%.*s' defined as `%s' not `%.*s'",
434 name, m.name_length, namebuf, newdefn + m.name_length,
435 m.definition_length - m.name_length,
436 namebuf + m.name_length);
437 goto fail;
440 free (namebuf);
441 namebuf = NULL;
443 /* Read in the list of identifiers that must not be defined.
444 Check that they really aren't. */
445 undeftab = xmalloc (m.definition_length);
446 if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
447 goto error;
448 for (i = 0; i < m.definition_length; )
450 int l = ustrlen (undeftab + i);
451 cpp_hashnode *h;
452 h = cpp_lookup (r, undeftab + i, l);
453 if (h->type != NT_VOID
454 || h->flags & NODE_POISONED)
456 if (CPP_OPTION (r, warn_invalid_pch))
457 cpp_error (r, DL_WARNING_SYSHDR,
458 "%s: not used because `%s' is defined",
459 name, undeftab + i);
460 goto fail;
462 i += l + 1;
464 free (undeftab);
466 /* We win! */
467 return 0;
469 error:
470 cpp_errno (r, DL_ERROR, "while reading precompiled header");
471 return -1;
473 fail:
474 if (namebuf != NULL)
475 free (namebuf);
476 if (undeftab != NULL)
477 free (undeftab);
478 return 1;
481 /* Save all the existing macros and assertions.
482 This code assumes that there might be hundreds, but not thousands of
483 existing definitions. */
485 struct save_macro_item {
486 struct save_macro_item *next;
487 struct cpp_hashnode macs[64];
490 struct save_macro_data
492 struct save_macro_item *macros;
493 size_t count;
494 char **saved_pragmas;
497 /* Save the definition of a single macro, so that it will persist across
498 a PCH restore. */
500 static int
501 save_macros (r, h, data_p)
502 cpp_reader *r ATTRIBUTE_UNUSED;
503 cpp_hashnode *h;
504 void *data_p;
506 struct save_macro_data *data = (struct save_macro_data *)data_p;
507 if (h->type != NT_VOID
508 && (h->flags & NODE_BUILTIN) == 0)
510 cpp_hashnode *save;
511 if (data->count == ARRAY_SIZE (data->macros->macs))
513 struct save_macro_item *d = data->macros;
514 data->macros = xmalloc (sizeof (struct save_macro_item));
515 data->macros->next = d;
516 data->count = 0;
518 save = data->macros->macs + data->count;
519 data->count++;
520 memcpy (save, h, sizeof (struct cpp_hashnode));
521 HT_STR (&save->ident) = xmemdup (HT_STR (HT_NODE (save)),
522 HT_LEN (HT_NODE (save)),
523 HT_LEN (HT_NODE (save)) + 1);
525 return 1;
528 /* Prepare to restore the state, by saving the currently-defined
529 macros in 'data'. */
531 void
532 cpp_prepare_state (r, data)
533 cpp_reader *r;
534 struct save_macro_data **data;
536 struct save_macro_data *d = xmalloc (sizeof (struct save_macro_data));
538 d->macros = NULL;
539 d->count = ARRAY_SIZE (d->macros->macs);
540 cpp_forall_identifiers (r, save_macros, d);
541 d->saved_pragmas = _cpp_save_pragma_names (r);
542 *data = d;
545 /* Erase all the existing macros and assertions. */
547 static int
548 reset_ht (r, h, unused)
549 cpp_reader *r ATTRIBUTE_UNUSED;
550 cpp_hashnode *h;
551 void *unused ATTRIBUTE_UNUSED;
553 if (h->type != NT_VOID
554 && (h->flags & NODE_BUILTIN) == 0)
556 h->type = NT_VOID;
557 memset (&h->value, 0, sizeof (h->value));
559 return 1;
562 /* Given a precompiled header that was previously determined to be valid,
563 apply all its definitions (and undefinitions) to the current state.
564 DEPNAME is passed to deps_restore. */
567 cpp_read_state (r, name, f, data)
568 cpp_reader *r;
569 const char *name;
570 FILE *f;
571 struct save_macro_data *data;
573 struct macrodef_struct m;
574 size_t defnlen = 256;
575 unsigned char *defn = xmalloc (defnlen);
576 struct lexer_state old_state;
577 struct save_macro_item *d;
578 size_t i, mac_count;
579 int saved_line = r->line;
581 /* Erase all the existing hashtable entries for macros. At this
582 point, they're all from the PCH file, and their pointers won't be
583 valid. */
584 cpp_forall_identifiers (r, reset_ht, NULL);
586 /* Restore spec_nodes, which will be full of references to the old
587 hashtable entries and so will now be invalid. */
589 struct spec_nodes *s = &r->spec_nodes;
590 s->n_defined = cpp_lookup (r, DSC("defined"));
591 s->n_true = cpp_lookup (r, DSC("true"));
592 s->n_false = cpp_lookup (r, DSC("false"));
593 s->n__VA_ARGS__ = cpp_lookup (r, DSC("__VA_ARGS__"));
596 /* Run through the carefully-saved macros, insert them. */
597 d = data->macros;
598 mac_count = data->count;
599 while (d)
601 struct save_macro_item *nextd;
602 for (i = 0; i < mac_count; i++)
604 cpp_hashnode *h;
606 h = cpp_lookup (r, HT_STR (HT_NODE (&d->macs[i])),
607 HT_LEN (HT_NODE (&d->macs[i])));
608 h->type = d->macs[i].type;
609 h->flags = d->macs[i].flags;
610 h->value = d->macs[i].value;
611 free ((void *)HT_STR (HT_NODE (&d->macs[i])));
613 nextd = d->next;
614 free (d);
615 d = nextd;
616 mac_count = ARRAY_SIZE (d->macs);
619 _cpp_restore_pragma_names (r, data->saved_pragmas);
621 free (data);
623 old_state = r->state;
625 r->state.in_directive = 1;
626 r->state.prevent_expansion = 1;
627 r->state.angled_headers = 0;
629 /* Read in the identifiers that must be defined. */
630 for (;;)
632 cpp_hashnode *h;
634 if (fread (&m, sizeof (m), 1, f) != 1)
635 goto error;
637 if (m.name_length == 0)
638 break;
640 if (defnlen < m.definition_length + 1)
642 defnlen = m.definition_length + 256;
643 defn = xrealloc (defn, defnlen);
646 if (fread (defn, 1, m.definition_length, f) != m.definition_length)
647 goto error;
648 defn[m.definition_length] = '\0';
650 h = cpp_lookup (r, defn, m.name_length);
652 if (h->type == NT_MACRO)
653 _cpp_free_definition (h);
654 if (m.flags & NODE_POISONED)
655 h->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
656 else if (m.name_length != m.definition_length)
658 if (cpp_push_buffer (r, defn + m.name_length,
659 m.definition_length - m.name_length,
660 true, 1) != NULL)
662 if (!_cpp_create_definition (r, h))
663 abort ();
664 _cpp_pop_buffer (r);
666 else
667 abort ();
671 r->state = old_state;
672 r->line = saved_line;
673 free (defn);
674 defn = NULL;
676 if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
677 != 0)
678 goto error;
680 return 0;
682 error:
683 cpp_errno (r, DL_ERROR, "while reading precompiled header");
684 return -1;