* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / cpppch.c
blob602711a427ed97c6ccf84473d44433a5e945a82b
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 comp_hashnodes PARAMS ((const void *, const void *));
34 static int collect_ht_nodes PARAMS ((cpp_reader *, cpp_hashnode *, void *));
35 static int write_defs PARAMS ((cpp_reader *, cpp_hashnode *, void *));
36 static int save_macros PARAMS ((cpp_reader *, cpp_hashnode *, void *));
38 /* This structure represents a macro definition on disk. */
39 struct macrodef_struct
41 unsigned int definition_length;
42 unsigned short name_length;
43 unsigned short flags;
46 /* This is how we write out a macro definition.
47 Suitable for being called by cpp_forall_identifiers. */
49 static int
50 write_macdef (pfile, hn, file_p)
51 cpp_reader *pfile;
52 cpp_hashnode *hn;
53 void *file_p;
55 FILE *f = (FILE *) file_p;
56 switch (hn->type)
58 case NT_VOID:
59 if (! (hn->flags & NODE_POISONED))
60 return 1;
62 case NT_MACRO:
63 if ((hn->flags & NODE_BUILTIN))
64 return 1;
67 struct macrodef_struct s;
68 const unsigned char *defn;
70 s.name_length = NODE_LEN (hn);
71 s.flags = hn->flags & NODE_POISONED;
73 if (hn->type == NT_MACRO)
75 defn = cpp_macro_definition (pfile, hn);
76 s.definition_length = ustrlen (defn);
78 else
80 defn = NODE_NAME (hn);
81 s.definition_length = s.name_length;
84 if (fwrite (&s, sizeof (s), 1, f) != 1
85 || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
87 cpp_errno (pfile, DL_ERROR, "while writing precompiled header");
88 return 0;
91 return 1;
93 case NT_ASSERTION:
94 /* Not currently implemented. */
95 return 1;
97 default:
98 abort ();
102 /* This structure records the names of the defined macros.
103 It's also used as a callback structure for size_initial_idents
104 and save_idents. */
106 struct cpp_savedstate
108 /* A hash table of the defined identifiers. */
109 htab_t definedhash;
110 /* The size of the definitions of those identifiers (the size of
111 'definedstrs'). */
112 size_t hashsize;
113 /* Number of definitions */
114 size_t n_defs;
115 /* Array of definitions. In cpp_write_pch_deps it is used for sorting. */
116 cpp_hashnode **defs;
117 /* Space for the next definition. Definitions are null-terminated
118 strings. */
119 unsigned char *definedstrs;
122 /* Save this identifier into the state: put it in the hash table,
123 put the definition in 'definedstrs'. */
125 static int
126 save_idents (pfile, hn, ss_p)
127 cpp_reader *pfile ATTRIBUTE_UNUSED;
128 cpp_hashnode *hn;
129 void *ss_p;
131 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
133 if (hn->type != NT_VOID)
135 struct cpp_string news;
136 void **slot;
138 news.len = NODE_LEN (hn);
139 news.text= NODE_NAME (hn);
140 slot = htab_find_slot (ss->definedhash, &news, INSERT);
141 if (*slot == NULL)
143 struct cpp_string *sp;
144 unsigned char *text;
146 sp = xmalloc (sizeof (struct cpp_string));
147 *slot = sp;
149 sp->len = NODE_LEN (hn);
150 sp->text = text = xmalloc (NODE_LEN (hn));
151 memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
155 return 1;
158 /* Hash some memory in a generic way. */
160 static hashval_t
161 hashmem (p_p, sz)
162 const void *p_p;
163 size_t sz;
165 const unsigned char *p = (const unsigned char *)p_p;
166 size_t i;
167 hashval_t h;
169 h = 0;
170 for (i = 0; i < sz; i++)
171 h = h * 67 - (*p++ - 113);
172 return h;
175 /* Hash a cpp string for the hashtable machinery. */
177 static hashval_t
178 cpp_string_hash (a_p)
179 const void *a_p;
181 const struct cpp_string *a = (const struct cpp_string *) a_p;
182 return hashmem (a->text, a->len);
185 /* Compare two cpp strings for the hashtable machinery. */
187 static int
188 cpp_string_eq (a_p, b_p)
189 const void *a_p;
190 const void *b_p;
192 const struct cpp_string *a = (const struct cpp_string *) a_p;
193 const struct cpp_string *b = (const struct cpp_string *) b_p;
194 return (a->len == b->len
195 && memcmp (a->text, b->text, a->len) == 0);
198 /* Save the current definitions of the cpp_reader for dependency
199 checking purposes. When writing a precompiled header, this should
200 be called at the same point in the compilation as cpp_valid_state
201 would be called when reading the precompiled header back in. */
204 cpp_save_state (r, f)
205 cpp_reader *r;
206 FILE *f;
208 /* Save the list of non-void identifiers for the dependency checking. */
209 r->savedstate = xmalloc (sizeof (struct cpp_savedstate));
210 r->savedstate->definedhash = htab_create (100, cpp_string_hash,
211 cpp_string_eq, NULL);
212 cpp_forall_identifiers (r, save_idents, r->savedstate);
214 /* Write out the list of defined identifiers. */
215 cpp_forall_identifiers (r, write_macdef, f);
217 return 0;
220 /* Calculate the 'hashsize' field of the saved state. */
222 static int
223 count_defs (pfile, hn, ss_p)
224 cpp_reader *pfile ATTRIBUTE_UNUSED;
225 cpp_hashnode *hn;
226 void *ss_p;
228 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
230 switch (hn->type)
232 case NT_MACRO:
233 if (hn->flags & NODE_BUILTIN)
234 return 1;
236 /* else fall through. */
238 case NT_VOID:
240 struct cpp_string news;
241 void **slot;
243 news.len = NODE_LEN (hn);
244 news.text = NODE_NAME (hn);
245 slot = htab_find (ss->definedhash, &news);
246 if (slot == NULL)
248 ss->hashsize += NODE_LEN (hn) + 1;
249 ss->n_defs += 1;
252 return 1;
254 case NT_ASSERTION:
255 /* Not currently implemented. */
256 return 1;
258 default:
259 abort ();
263 /* Collect the identifiers into the state's string table. */
264 static int
265 write_defs (pfile, hn, ss_p)
266 cpp_reader *pfile ATTRIBUTE_UNUSED;
267 cpp_hashnode *hn;
268 void *ss_p;
270 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
272 switch (hn->type)
274 case NT_MACRO:
275 if (hn->flags & NODE_BUILTIN)
276 return 1;
278 /* else fall through. */
280 case NT_VOID:
282 struct cpp_string news;
283 void **slot;
285 news.len = NODE_LEN (hn);
286 news.text = NODE_NAME (hn);
287 slot = htab_find (ss->definedhash, &news);
288 if (slot == NULL)
290 ss->defs[ss->n_defs] = hn;
291 ss->n_defs += 1;
294 return 1;
296 case NT_ASSERTION:
297 /* Not currently implemented. */
298 return 1;
300 default:
301 abort ();
305 /* Comparison function for qsort. The arguments point to pointers of
306 type ht_hashnode *. */
307 static int
308 comp_hashnodes (px, py)
309 const void *px;
310 const void *py;
312 cpp_hashnode *x = *(cpp_hashnode **) px;
313 cpp_hashnode *y = *(cpp_hashnode **) py;
314 return ustrcmp (NODE_NAME (x), NODE_NAME (y));
317 /* Write out the remainder of the dependency information. This should be
318 called after the PCH is ready to be saved. */
321 cpp_write_pch_deps (r, f)
322 cpp_reader *r;
323 FILE *f;
325 struct macrodef_struct z;
326 struct cpp_savedstate *const ss = r->savedstate;
327 unsigned char *definedstrs;
328 size_t i;
330 /* Collect the list of identifiers which have been seen and
331 weren't defined to anything previously. */
332 ss->hashsize = 0;
333 ss->n_defs = 0;
334 cpp_forall_identifiers (r, count_defs, ss);
336 ss->defs = xmalloc (ss->n_defs * sizeof (cpp_hashnode *));
337 ss->n_defs = 0;
338 cpp_forall_identifiers (r, write_defs, ss);
340 /* Sort the list, copy it into a buffer, and write it out. */
341 qsort (ss->defs, ss->n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
342 definedstrs = ss->definedstrs = xmalloc (ss->hashsize);
343 for (i = 0; i < ss->n_defs; ++i)
345 size_t len = NODE_LEN (ss->defs[i]);
346 memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
347 definedstrs += len + 1;
350 memset (&z, 0, sizeof (z));
351 z.definition_length = ss->hashsize;
352 if (fwrite (&z, sizeof (z), 1, f) != 1
353 || fwrite (ss->definedstrs, ss->hashsize, 1, f) != 1)
355 cpp_errno (r, DL_ERROR, "while writing precompiled header");
356 return -1;
358 free (ss->definedstrs);
360 /* Free the saved state. */
361 free (ss);
362 r->savedstate = NULL;
363 return 0;
366 /* Write out the definitions of the preprocessor, in a form suitable for
367 cpp_read_state. */
370 cpp_write_pch_state (r, f)
371 cpp_reader *r;
372 FILE *f;
374 struct macrodef_struct z;
376 /* Write out the list of defined identifiers. */
377 cpp_forall_identifiers (r, write_macdef, f);
378 memset (&z, 0, sizeof (z));
379 if (fwrite (&z, sizeof (z), 1, f) != 1)
381 cpp_errno (r, DL_ERROR, "while writing precompiled header");
382 return -1;
385 if (!r->deps)
386 r->deps = deps_init ();
388 if (deps_save (r->deps, f) != 0)
390 cpp_errno (r, DL_ERROR, "while writing precompiled header");
391 return -1;
394 return 0;
398 /* Data structure to transform hash table nodes into a sorted list */
400 struct ht_node_list
402 /* Array of nodes */
403 cpp_hashnode **defs;
404 /* Number of nodes in the array */
405 size_t n_defs;
406 /* Size of the allocated array */
407 size_t asize;
410 /* Callback for collecting identifiers from hash table */
412 static int
413 collect_ht_nodes (pfile, hn, nl_p)
414 cpp_reader *pfile ATTRIBUTE_UNUSED;
415 cpp_hashnode *hn;
416 void *nl_p;
418 struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
420 if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
422 if (nl->n_defs == nl->asize)
424 nl->asize *= 2;
425 nl->defs = xrealloc (nl->defs, nl->asize * sizeof (cpp_hashnode *));
428 nl->defs[nl->n_defs] = hn;
429 ++nl->n_defs;
431 return 1;
435 /* Return nonzero if FD is a precompiled header which is consistent
436 with the preprocessor's current definitions. It will be consistent
437 when:
439 - anything that was defined just before the PCH was generated
440 is defined the same way now; and
441 - anything that was not defined then, but is defined now, was not
442 used by the PCH.
444 NAME is used to print warnings if `warn_invalid_pch' is set in the
445 reader's flags.
449 cpp_valid_state (r, name, fd)
450 cpp_reader *r;
451 const char *name;
452 int fd;
454 struct macrodef_struct m;
455 size_t namebufsz = 256;
456 unsigned char *namebuf = xmalloc (namebufsz);
457 unsigned char *undeftab = NULL;
458 struct ht_node_list nl = { 0, 0, 0 };
459 unsigned char *first, *last;
460 unsigned int i;
462 /* Read in the list of identifiers that must be defined
463 Check that they are defined in the same way. */
464 for (;;)
466 cpp_hashnode *h;
467 const unsigned char *newdefn;
469 if (read (fd, &m, sizeof (m)) != sizeof (m))
470 goto error;
472 if (m.name_length == 0)
473 break;
475 if (m.definition_length > namebufsz)
477 free (namebuf);
478 namebufsz = m.definition_length + 256;
479 namebuf = xmalloc (namebufsz);
482 if ((size_t)read (fd, namebuf, m.definition_length)
483 != m.definition_length)
484 goto error;
486 h = cpp_lookup (r, namebuf, m.name_length);
487 if (m.flags & NODE_POISONED
488 || h->type != NT_MACRO
489 || h->flags & NODE_POISONED)
491 if (CPP_OPTION (r, warn_invalid_pch))
492 cpp_error (r, DL_WARNING_SYSHDR,
493 "%s: not used because `%.*s' not defined",
494 name, m.name_length, namebuf);
495 goto fail;
498 newdefn = cpp_macro_definition (r, h);
500 if (m.definition_length != ustrlen (newdefn)
501 || memcmp (namebuf, newdefn, m.definition_length) != 0)
503 if (CPP_OPTION (r, warn_invalid_pch))
504 cpp_error (r, DL_WARNING_SYSHDR,
505 "%s: not used because `%.*s' defined as `%s' not `%.*s'",
506 name, m.name_length, namebuf, newdefn + m.name_length,
507 m.definition_length - m.name_length,
508 namebuf + m.name_length);
509 goto fail;
512 free (namebuf);
513 namebuf = NULL;
515 /* Read in the list of identifiers that must not be defined.
516 Check that they really aren't. */
517 undeftab = xmalloc (m.definition_length);
518 if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
519 goto error;
521 /* Collect identifiers from the current hash table. */
522 nl.n_defs = 0;
523 nl.asize = 10;
524 nl.defs = xmalloc (nl.asize * sizeof (cpp_hashnode *));
525 cpp_forall_identifiers (r, &collect_ht_nodes, &nl);
526 qsort (nl.defs, nl.n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
528 /* Loop through nl.defs and undeftab, both of which are sorted lists.
529 There should be no matches. */
530 first = undeftab;
531 last = undeftab + m.definition_length;
532 i = 0;
534 while (first < last && i < nl.n_defs)
536 int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
538 if (cmp < 0)
539 first += ustrlen (first) + 1;
540 else if (cmp > 0)
541 ++i;
542 else
543 goto fail;
546 free(nl.defs);
547 free (undeftab);
549 /* We win! */
550 return 0;
552 error:
553 cpp_errno (r, DL_ERROR, "while reading precompiled header");
554 return -1;
556 fail:
557 if (namebuf != NULL)
558 free (namebuf);
559 if (undeftab != NULL)
560 free (undeftab);
561 if (nl.defs != NULL)
562 free (nl.defs);
563 return 1;
566 /* Save all the existing macros and assertions.
567 This code assumes that there might be hundreds, but not thousands of
568 existing definitions. */
570 struct save_macro_item {
571 struct save_macro_item *next;
572 struct cpp_hashnode macs[64];
575 struct save_macro_data
577 struct save_macro_item *macros;
578 size_t count;
579 char **saved_pragmas;
582 /* Save the definition of a single macro, so that it will persist across
583 a PCH restore. */
585 static int
586 save_macros (r, h, data_p)
587 cpp_reader *r ATTRIBUTE_UNUSED;
588 cpp_hashnode *h;
589 void *data_p;
591 struct save_macro_data *data = (struct save_macro_data *)data_p;
592 if (h->type != NT_VOID
593 && (h->flags & NODE_BUILTIN) == 0)
595 cpp_hashnode *save;
596 if (data->count == ARRAY_SIZE (data->macros->macs))
598 struct save_macro_item *d = data->macros;
599 data->macros = xmalloc (sizeof (struct save_macro_item));
600 data->macros->next = d;
601 data->count = 0;
603 save = data->macros->macs + data->count;
604 data->count++;
605 memcpy (save, h, sizeof (struct cpp_hashnode));
606 HT_STR (&save->ident) = xmemdup (HT_STR (HT_NODE (save)),
607 HT_LEN (HT_NODE (save)),
608 HT_LEN (HT_NODE (save)) + 1);
610 return 1;
613 /* Prepare to restore the state, by saving the currently-defined
614 macros in 'data'. */
616 void
617 cpp_prepare_state (r, data)
618 cpp_reader *r;
619 struct save_macro_data **data;
621 struct save_macro_data *d = xmalloc (sizeof (struct save_macro_data));
623 d->macros = NULL;
624 d->count = ARRAY_SIZE (d->macros->macs);
625 cpp_forall_identifiers (r, save_macros, d);
626 d->saved_pragmas = _cpp_save_pragma_names (r);
627 *data = d;
630 /* Given a precompiled header that was previously determined to be valid,
631 apply all its definitions (and undefinitions) to the current state.
632 DEPNAME is passed to deps_restore. */
635 cpp_read_state (r, name, f, data)
636 cpp_reader *r;
637 const char *name;
638 FILE *f;
639 struct save_macro_data *data;
641 struct macrodef_struct m;
642 size_t defnlen = 256;
643 unsigned char *defn = xmalloc (defnlen);
644 struct lexer_state old_state;
645 struct save_macro_item *d;
646 size_t i, mac_count;
647 int saved_line = r->line;
649 /* Restore spec_nodes, which will be full of references to the old
650 hashtable entries and so will now be invalid. */
652 struct spec_nodes *s = &r->spec_nodes;
653 s->n_defined = cpp_lookup (r, DSC("defined"));
654 s->n_true = cpp_lookup (r, DSC("true"));
655 s->n_false = cpp_lookup (r, DSC("false"));
656 s->n__VA_ARGS__ = cpp_lookup (r, DSC("__VA_ARGS__"));
659 /* Run through the carefully-saved macros, insert them. */
660 d = data->macros;
661 mac_count = data->count;
662 while (d)
664 struct save_macro_item *nextd;
665 for (i = 0; i < mac_count; i++)
667 cpp_hashnode *h;
669 h = cpp_lookup (r, HT_STR (HT_NODE (&d->macs[i])),
670 HT_LEN (HT_NODE (&d->macs[i])));
671 h->type = d->macs[i].type;
672 h->flags = d->macs[i].flags;
673 h->value = d->macs[i].value;
674 free ((void *)HT_STR (HT_NODE (&d->macs[i])));
676 nextd = d->next;
677 free (d);
678 d = nextd;
679 mac_count = ARRAY_SIZE (d->macs);
682 _cpp_restore_pragma_names (r, data->saved_pragmas);
684 free (data);
686 old_state = r->state;
688 r->state.in_directive = 1;
689 r->state.prevent_expansion = 1;
690 r->state.angled_headers = 0;
692 /* Read in the identifiers that must be defined. */
693 for (;;)
695 cpp_hashnode *h;
697 if (fread (&m, sizeof (m), 1, f) != 1)
698 goto error;
700 if (m.name_length == 0)
701 break;
703 if (defnlen < m.definition_length + 1)
705 defnlen = m.definition_length + 256;
706 defn = xrealloc (defn, defnlen);
709 if (fread (defn, 1, m.definition_length, f) != m.definition_length)
710 goto error;
711 defn[m.definition_length] = '\n';
713 h = cpp_lookup (r, defn, m.name_length);
715 if (h->type == NT_MACRO)
716 _cpp_free_definition (h);
717 if (m.flags & NODE_POISONED)
718 h->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
719 else if (m.name_length != m.definition_length)
721 if (cpp_push_buffer (r, defn + m.name_length,
722 m.definition_length - m.name_length,
723 true, 1) != NULL)
725 _cpp_clean_line (r);
726 if (!_cpp_create_definition (r, h))
727 abort ();
728 _cpp_pop_buffer (r);
730 else
731 abort ();
735 r->state = old_state;
736 r->line = saved_line;
737 free (defn);
738 defn = NULL;
740 if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
741 != 0)
742 goto error;
744 return 0;
746 error:
747 cpp_errno (r, DL_ERROR, "while reading precompiled header");
748 return -1;