OSDN Git Service

2003-06-12 Aldy Hernandez <aldyh@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / cpppch.c
1 /* Part of CPP library.  (Precompiled header reading/writing.)
2    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
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"
26
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 *));
37
38 /* This structure represents a macro definition on disk.  */
39 struct macrodef_struct 
40 {
41   unsigned int definition_length;
42   unsigned short name_length;
43   unsigned short flags;
44 };
45
46 /* This is how we write out a macro definition.  
47    Suitable for being called by cpp_forall_identifiers.  */
48
49 static int
50 write_macdef (pfile, hn, file_p)
51      cpp_reader *pfile;
52      cpp_hashnode *hn;
53      void *file_p;
54 {
55   FILE *f = (FILE *) file_p;
56   switch (hn->type)
57     {
58     case NT_VOID:
59       if (! (hn->flags & NODE_POISONED))
60         return 1;
61       
62     case NT_MACRO:
63       if ((hn->flags & NODE_BUILTIN))
64         return 1;
65
66       {
67         struct macrodef_struct s;
68         const unsigned char *defn;
69
70         s.name_length = NODE_LEN (hn);
71         s.flags = hn->flags & NODE_POISONED;
72
73         if (hn->type == NT_MACRO)
74           {
75             defn = cpp_macro_definition (pfile, hn);
76             s.definition_length = ustrlen (defn);
77           }
78         else
79           {
80             defn = NODE_NAME (hn);
81             s.definition_length = s.name_length;
82           }
83         
84         if (fwrite (&s, sizeof (s), 1, f) != 1
85             || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
86           {
87             cpp_errno (pfile, DL_ERROR, "while writing precompiled header");
88             return 0;
89           }
90       }
91       return 1;
92       
93     case NT_ASSERTION:
94       /* Not currently implemented.  */
95       return 1;
96
97     default:
98       abort ();
99     }
100 }
101
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.  */
105
106 struct cpp_savedstate
107 {
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;
120 };
121
122 /* Save this identifier into the state: put it in the hash table,
123    put the definition in 'definedstrs'.  */
124
125 static int
126 save_idents (pfile, hn, ss_p)
127      cpp_reader *pfile ATTRIBUTE_UNUSED;
128      cpp_hashnode *hn;
129      void *ss_p;
130 {
131   struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
132   
133   if (hn->type != NT_VOID)
134     {
135       struct cpp_string news;
136       void **slot;
137
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)
142         {
143           struct cpp_string *sp;
144           unsigned char *text;
145           
146           sp = xmalloc (sizeof (struct cpp_string));
147           *slot = sp;
148
149           sp->len = NODE_LEN (hn);
150           sp->text = text = xmalloc (NODE_LEN (hn));
151           memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
152         }
153     }
154
155   return 1;
156 }
157
158 /* Hash some memory in a generic way.  */
159
160 static hashval_t
161 hashmem (p_p, sz)
162      const void *p_p;
163      size_t sz;
164 {
165   const unsigned char *p = (const unsigned char *)p_p;
166   size_t i;
167   hashval_t h;
168   
169   h = 0;
170   for (i = 0; i < sz; i++)
171     h = h * 67 - (*p++ - 113);
172   return h;
173 }
174
175 /* Hash a cpp string for the hashtable machinery.  */
176
177 static hashval_t
178 cpp_string_hash (a_p)
179      const void *a_p;
180 {
181   const struct cpp_string *a = (const struct cpp_string *) a_p;
182   return hashmem (a->text, a->len);
183 }
184
185 /* Compare two cpp strings for the hashtable machinery.  */
186
187 static int
188 cpp_string_eq (a_p, b_p)
189      const void *a_p;
190      const void *b_p;
191 {
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);
196 }
197
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.  */
202
203 int
204 cpp_save_state (r, f)
205      cpp_reader *r;
206      FILE *f;
207 {
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);
213   
214   /* Write out the list of defined identifiers.  */
215   cpp_forall_identifiers (r, write_macdef, f);
216
217   return 0;
218 }
219
220 /* Calculate the 'hashsize' field of the saved state.  */
221
222 static int
223 count_defs (pfile, hn, ss_p)
224      cpp_reader *pfile ATTRIBUTE_UNUSED;
225      cpp_hashnode *hn;
226      void *ss_p;
227 {
228   struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
229   
230   switch (hn->type)
231     {
232     case NT_MACRO:
233       if (hn->flags & NODE_BUILTIN)
234         return 1;
235       
236       /* else fall through.  */
237
238     case NT_VOID:
239       {
240         struct cpp_string news;
241         void **slot;
242         
243         news.len = NODE_LEN (hn);
244         news.text = NODE_NAME (hn);
245         slot = htab_find (ss->definedhash, &news);
246         if (slot == NULL)
247           {
248             ss->hashsize += NODE_LEN (hn) + 1;
249             ss->n_defs += 1;
250           }
251       }
252       return 1;
253
254     case NT_ASSERTION:
255       /* Not currently implemented.  */
256       return 1;
257
258     default:
259       abort ();
260     }
261 }
262
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;
269 {
270   struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
271   
272   switch (hn->type)
273     {
274     case NT_MACRO:
275       if (hn->flags & NODE_BUILTIN)
276         return 1;
277       
278       /* else fall through.  */
279
280     case NT_VOID:
281       {
282         struct cpp_string news;
283         void **slot;
284         
285         news.len = NODE_LEN (hn);
286         news.text = NODE_NAME (hn);
287         slot = htab_find (ss->definedhash, &news);
288         if (slot == NULL)
289           {
290             ss->defs[ss->n_defs] = hn;
291             ss->n_defs += 1;
292           }
293       }
294       return 1;
295
296     case NT_ASSERTION:
297       /* Not currently implemented.  */
298       return 1;
299
300     default:
301       abort ();
302     }
303 }
304
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;
311 {
312   cpp_hashnode *x = *(cpp_hashnode **) px;
313   cpp_hashnode *y = *(cpp_hashnode **) py;
314   return ustrcmp (NODE_NAME (x), NODE_NAME (y));
315 }
316
317 /* Write out the remainder of the dependency information.  This should be
318    called after the PCH is ready to be saved.  */
319
320 int
321 cpp_write_pch_deps (r, f)
322      cpp_reader *r;
323      FILE *f;
324 {
325   struct macrodef_struct z;
326   struct cpp_savedstate *const ss = r->savedstate;
327   unsigned char *definedstrs;
328   size_t i;
329   
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);
335
336   ss->defs = xmalloc (ss->n_defs * sizeof (cpp_hashnode *));
337   ss->n_defs = 0;
338   cpp_forall_identifiers (r, write_defs, ss);
339
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)
344     {
345       size_t len = NODE_LEN (ss->defs[i]);
346       memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
347       definedstrs += len + 1;
348     }
349
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)
354     {
355       cpp_errno (r, DL_ERROR, "while writing precompiled header");
356       return -1;
357     }
358   free (ss->definedstrs);
359
360   /* Free the saved state.  */
361   free (ss);
362   r->savedstate = NULL;
363   return 0;
364 }
365
366 /* Write out the definitions of the preprocessor, in a form suitable for
367    cpp_read_state.  */
368
369 int
370 cpp_write_pch_state (r, f)
371      cpp_reader *r;
372      FILE *f;
373 {
374   struct macrodef_struct z;
375
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)
380     {
381       cpp_errno (r, DL_ERROR, "while writing precompiled header");
382       return -1;
383     }
384
385   if (!r->deps)
386     r->deps = deps_init ();
387
388   if (deps_save (r->deps, f) != 0)
389     {
390       cpp_errno (r, DL_ERROR, "while writing precompiled header");
391       return -1;
392     }
393
394   return 0;
395 }
396
397
398 /* Data structure to transform hash table nodes into a sorted list */
399
400 struct ht_node_list
401 {
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;
408 };
409
410 /* Callback for collecting identifiers from hash table */
411
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;
417 {
418   struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
419
420   if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
421     {
422       if (nl->n_defs == nl->asize)
423         {
424           nl->asize *= 2;
425           nl->defs = xrealloc (nl->defs, nl->asize * sizeof (cpp_hashnode *));
426         }
427
428       nl->defs[nl->n_defs] = hn;
429       ++nl->n_defs;
430     }
431   return 1;
432 }
433
434
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:
438
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.
443
444    NAME is used to print warnings if `warn_invalid_pch' is set in the
445    reader's flags.
446 */
447
448 int
449 cpp_valid_state (r, name, fd)
450      cpp_reader *r;
451      const char *name;
452      int fd;
453 {
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;
461   
462   /* Read in the list of identifiers that must be defined
463      Check that they are defined in the same way.  */
464   for (;;)
465     {
466       cpp_hashnode *h;
467       const unsigned char *newdefn;
468       
469       if (read (fd, &m, sizeof (m)) != sizeof (m))
470         goto error;
471       
472       if (m.name_length == 0)
473         break;
474
475       if (m.definition_length > namebufsz)
476         {
477           free (namebuf);
478           namebufsz = m.definition_length + 256;
479           namebuf = xmalloc (namebufsz);
480         }
481       
482       if ((size_t)read (fd, namebuf, m.definition_length) 
483           != m.definition_length)
484         goto error;
485       
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)
490         {
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;
496         }
497
498       newdefn = cpp_macro_definition (r, h);
499       
500       if (m.definition_length != ustrlen (newdefn)
501           || memcmp (namebuf, newdefn, m.definition_length) != 0)
502         {
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;
510         }
511     }
512   free (namebuf);
513   namebuf = NULL;
514
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;
520
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);
527  
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;
533  
534   while (first < last && i < nl.n_defs)
535     {
536       int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
537  
538       if (cmp < 0)
539         first += ustrlen (first) + 1;
540       else if (cmp > 0)
541         ++i;
542       else
543         goto fail;
544     }
545    
546   free(nl.defs);
547   free (undeftab);
548
549   /* We win!  */
550   return 0;
551
552  error:
553   cpp_errno (r, DL_ERROR, "while reading precompiled header");
554   return -1;
555
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;
564 }
565
566 /* Save all the existing macros and assertions.  
567    This code assumes that there might be hundreds, but not thousands of
568    existing definitions.  */
569
570 struct save_macro_item {
571   struct save_macro_item *next;
572   struct cpp_hashnode macs[64];
573 };
574
575 struct save_macro_data 
576 {
577   struct save_macro_item *macros;
578   size_t count;
579   char **saved_pragmas;
580 };
581
582 /* Save the definition of a single macro, so that it will persist across
583    a PCH restore.  */
584
585 static int 
586 save_macros (r, h, data_p)
587      cpp_reader *r ATTRIBUTE_UNUSED;
588      cpp_hashnode *h;
589      void *data_p;
590 {
591   struct save_macro_data *data = (struct save_macro_data *)data_p;
592   if (h->type != NT_VOID
593       && (h->flags & NODE_BUILTIN) == 0)
594     {
595       cpp_hashnode *save;
596       if (data->count == ARRAY_SIZE (data->macros->macs))
597         {
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;
602         }
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);
609     }
610   return 1;
611 }
612
613 /* Prepare to restore the state, by saving the currently-defined
614    macros in 'data'.  */
615
616 void
617 cpp_prepare_state (r, data)
618      cpp_reader *r;
619      struct save_macro_data **data;
620 {
621   struct save_macro_data *d = xmalloc (sizeof (struct save_macro_data));
622   
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;
628 }
629
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.  */
633
634 int
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;
640 {
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;
648
649   /* Restore spec_nodes, which will be full of references to the old 
650      hashtable entries and so will now be invalid.  */
651   {
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__"));
657   }
658
659   /* Run through the carefully-saved macros, insert them.  */
660   d = data->macros;
661   mac_count = data->count;
662   while (d)
663     {
664       struct save_macro_item *nextd;
665       for (i = 0; i < mac_count; i++)
666         {
667           cpp_hashnode *h;
668           
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])));
675         }
676       nextd = d->next;
677       free (d);
678       d = nextd;
679       mac_count = ARRAY_SIZE (d->macs);
680     }
681
682   _cpp_restore_pragma_names (r, data->saved_pragmas);
683
684   free (data);
685
686   old_state = r->state;
687
688   r->state.in_directive = 1;
689   r->state.prevent_expansion = 1;
690   r->state.angled_headers = 0;
691
692   /* Read in the identifiers that must be defined.  */
693   for (;;)
694     {
695       cpp_hashnode *h;
696       
697       if (fread (&m, sizeof (m), 1, f) != 1)
698         goto error;
699       
700       if (m.name_length == 0)
701         break;
702
703       if (defnlen < m.definition_length + 1)
704         {
705           defnlen = m.definition_length + 256;
706           defn = xrealloc (defn, defnlen);
707         }
708
709       if (fread (defn, 1, m.definition_length, f) != m.definition_length)
710         goto error;
711       defn[m.definition_length] = '\n';
712       
713       h = cpp_lookup (r, defn, m.name_length);
714
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)
720         {
721           if (cpp_push_buffer (r, defn + m.name_length, 
722                                m.definition_length - m.name_length, 
723                                true, 1) != NULL)
724             {
725               _cpp_clean_line (r);
726               if (!_cpp_create_definition (r, h))
727                 abort ();
728               _cpp_pop_buffer (r);
729             }
730           else
731             abort ();
732         }
733     }
734
735   r->state = old_state;
736   r->line = saved_line;
737   free (defn);
738   defn = NULL;
739
740   if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
741       != 0)
742     goto error;
743
744   return 0;
745   
746  error:
747   cpp_errno (r, DL_ERROR, "while reading precompiled header");
748   return -1;
749 }