OSDN Git Service

* real.h (struct real_value): Add signalling.
[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 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 *));
36
37 /* This structure represents a macro definition on disk.  */
38 struct macrodef_struct 
39 {
40   unsigned int definition_length;
41   unsigned short name_length;
42   unsigned short flags;
43 };
44
45 /* This is how we write out a macro definition.  
46    Suitable for being called by cpp_forall_identifiers.  */
47
48 static int
49 write_macdef (pfile, hn, file_p)
50      cpp_reader *pfile;
51      cpp_hashnode *hn;
52      void *file_p;
53 {
54   FILE *f = (FILE *) file_p;
55   switch (hn->type)
56     {
57     case NT_VOID:
58       if (! (hn->flags & NODE_POISONED))
59         return 1;
60       
61     case NT_MACRO:
62       if ((hn->flags & NODE_BUILTIN))
63         return 1;
64
65       {
66         struct macrodef_struct s;
67         const unsigned char *defn;
68
69         s.name_length = NODE_LEN (hn);
70         s.flags = hn->flags & NODE_POISONED;
71
72         if (hn->type == NT_MACRO)
73           {
74             defn = cpp_macro_definition (pfile, hn);
75             s.definition_length = ustrlen (defn);
76           }
77         else
78           {
79             defn = NODE_NAME (hn);
80             s.definition_length = s.name_length;
81           }
82         
83         if (fwrite (&s, sizeof (s), 1, f) != 1
84             || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
85           {
86             cpp_errno (pfile, DL_ERROR, "while writing precompiled header");
87             return 0;
88           }
89       }
90       return 1;
91       
92     case NT_ASSERTION:
93       /* Not currently implemented.  */
94       return 1;
95
96     default:
97       abort ();
98     }
99 }
100
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.  */
104
105 struct cpp_savedstate
106 {
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;
115 };
116
117 /* Save this identifier into the state: put it in the hash table,
118    put the definition in 'definedstrs'.  */
119
120 static int
121 save_idents (pfile, hn, ss_p)
122      cpp_reader *pfile ATTRIBUTE_UNUSED;
123      cpp_hashnode *hn;
124      void *ss_p;
125 {
126   struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
127   
128   if (hn->type != NT_VOID)
129     {
130       struct cpp_string news;
131       void **slot;
132
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)
137         {
138           struct cpp_string *sp;
139           unsigned char *text;
140           
141           sp = xmalloc (sizeof (struct cpp_string));
142           *slot = sp;
143
144           sp->len = NODE_LEN (hn);
145           sp->text = text = xmalloc (NODE_LEN (hn));
146           memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
147         }
148     }
149
150   return 1;
151 }
152
153 /* Hash some memory in a generic way.  */
154
155 static hashval_t
156 hashmem (p_p, sz)
157      const void *p_p;
158      size_t sz;
159 {
160   const unsigned char *p = (const unsigned char *)p_p;
161   size_t i;
162   hashval_t h;
163   
164   h = 0;
165   for (i = 0; i < sz; i++)
166     h = h * 67 - (*p++ - 113);
167   return h;
168 }
169
170 /* Hash a cpp string for the hashtable machinery.  */
171
172 static hashval_t
173 cpp_string_hash (a_p)
174      const void *a_p;
175 {
176   const struct cpp_string *a = (const struct cpp_string *) a_p;
177   return hashmem (a->text, a->len);
178 }
179
180 /* Compare two cpp strings for the hashtable machinery.  */
181
182 static int
183 cpp_string_eq (a_p, b_p)
184      const void *a_p;
185      const void *b_p;
186 {
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);
191 }
192
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.  */
197
198 int
199 cpp_save_state (r, f)
200      cpp_reader *r;
201      FILE *f;
202 {
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);
208   
209   /* Write out the list of defined identifiers.  */
210   cpp_forall_identifiers (r, write_macdef, f);
211
212   return 0;
213 }
214
215 /* Calculate the 'hashsize' field of the saved state.  */
216
217 static int
218 count_defs (pfile, hn, ss_p)
219      cpp_reader *pfile ATTRIBUTE_UNUSED;
220      cpp_hashnode *hn;
221      void *ss_p;
222 {
223   struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
224   
225   switch (hn->type)
226     {
227     case NT_MACRO:
228       if (hn->flags & NODE_BUILTIN)
229         return 1;
230       
231       /* else fall through.  */
232
233     case NT_VOID:
234       {
235         struct cpp_string news;
236         void **slot;
237         
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;
243       }
244       return 1;
245
246     case NT_ASSERTION:
247       /* Not currently implemented.  */
248       return 1;
249
250     default:
251       abort ();
252     }
253 }
254
255 /* Write the identifiers into 'definedstrs' of the state.  */
256
257 static int
258 write_defs (pfile, hn, ss_p)
259      cpp_reader *pfile ATTRIBUTE_UNUSED;
260      cpp_hashnode *hn;
261      void *ss_p;
262 {
263   struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
264   
265   switch (hn->type)
266     {
267     case NT_MACRO:
268       if (hn->flags & NODE_BUILTIN)
269         return 1;
270       
271       /* else fall through.  */
272
273     case NT_VOID:
274       {
275         struct cpp_string news;
276         void **slot;
277         
278         news.len = NODE_LEN (hn);
279         news.text = NODE_NAME (hn);
280         slot = htab_find (ss->definedhash, &news);
281         if (slot == NULL)
282           {
283             memcpy (ss->definedstrs, NODE_NAME (hn), NODE_LEN (hn));
284             ss->definedstrs[NODE_LEN (hn)] = 0;
285             ss->definedstrs += NODE_LEN (hn) + 1;
286           }
287       }
288       return 1;
289
290     case NT_ASSERTION:
291       /* Not currently implemented.  */
292       return 1;
293
294     default:
295       abort ();
296     }
297 }
298
299 /* Write out the remainder of the dependency information.  This should be
300    called after the PCH is ready to be saved.  */
301
302 int
303 cpp_write_pch_deps (r, f)
304      cpp_reader *r;
305      FILE *f;
306 {
307   struct macrodef_struct z;
308   struct cpp_savedstate *const ss = r->savedstate;
309   unsigned char *definedstrs;
310   
311   ss->hashsize = 0;
312   
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)
322     {
323       cpp_errno (r, DL_ERROR, "while writing precompiled header");
324       return -1;
325     }
326   free (definedstrs);
327
328   /* Free the saved state.  */
329   free (ss);
330   r->savedstate = NULL;
331   return 0;
332 }
333
334 /* Write out the definitions of the preprocessor, in a form suitable for
335    cpp_read_state.  */
336
337 int
338 cpp_write_pch_state (r, f)
339      cpp_reader *r;
340      FILE *f;
341 {
342   struct macrodef_struct z;
343
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)
348     {
349       cpp_errno (r, DL_ERROR, "while writing precompiled header");
350       return -1;
351     }
352
353   if (!r->deps)
354     r->deps = deps_init ();
355
356   if (deps_save (r->deps, f) != 0)
357     {
358       cpp_errno (r, DL_ERROR, "while writing precompiled header");
359       return -1;
360     }
361
362   return 0;
363 }
364
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:
368
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.
373
374    NAME is used to print warnings if `warn_invalid_pch' is set in the
375    reader's flags.
376 */
377
378 int
379 cpp_valid_state (r, name, fd)
380      cpp_reader *r;
381      const char *name;
382      int fd;
383 {
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;
389   
390   /* Read in the list of identifiers that must be defined
391      Check that they are defined in the same way.  */
392   for (;;)
393     {
394       cpp_hashnode *h;
395       const unsigned char *newdefn;
396       
397       if (read (fd, &m, sizeof (m)) != sizeof (m))
398         goto error;
399       
400       if (m.name_length == 0)
401         break;
402
403       if (m.definition_length > namebufsz)
404         {
405           free (namebuf);
406           namebufsz = m.definition_length + 256;
407           namebuf = xmalloc (namebufsz);
408         }
409       
410       if ((size_t)read (fd, namebuf, m.definition_length) 
411           != m.definition_length)
412         goto error;
413       
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)
418         {
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;
424         }
425
426       newdefn = cpp_macro_definition (r, h);
427       
428       if (m.definition_length != ustrlen (newdefn)
429           || memcmp (namebuf, newdefn, m.definition_length) != 0)
430         {
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;
438         }
439     }
440   free (namebuf);
441   namebuf = NULL;
442
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; )
449     {
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)
455         {
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;
461         }
462       i += l + 1;
463     }
464   free (undeftab);
465
466   /* We win!  */
467   return 0;
468
469  error:
470   cpp_errno (r, DL_ERROR, "while reading precompiled header");
471   return -1;
472
473  fail:
474   if (namebuf != NULL)
475     free (namebuf);
476   if (undeftab != NULL)
477     free (undeftab);
478   return 1;
479 }
480
481 /* Save all the existing macros and assertions.  
482    This code assumes that there might be hundreds, but not thousands of
483    existing definitions.  */
484
485 struct save_macro_item {
486   struct save_macro_item *next;
487   struct cpp_hashnode macs[64];
488 };
489
490 struct save_macro_data 
491 {
492   struct save_macro_item *macros;
493   size_t count;
494   char **saved_pragmas;
495 };
496
497 /* Save the definition of a single macro, so that it will persist across
498    a PCH restore.  */
499
500 static int 
501 save_macros (r, h, data_p)
502      cpp_reader *r ATTRIBUTE_UNUSED;
503      cpp_hashnode *h;
504      void *data_p;
505 {
506   struct save_macro_data *data = (struct save_macro_data *)data_p;
507   if (h->type != NT_VOID
508       && (h->flags & NODE_BUILTIN) == 0)
509     {
510       cpp_hashnode *save;
511       if (data->count == ARRAY_SIZE (data->macros->macs))
512         {
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;
517         }
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);
524     }
525   return 1;
526 }
527
528 /* Prepare to restore the state, by saving the currently-defined
529    macros in 'data'.  */
530
531 void
532 cpp_prepare_state (r, data)
533      cpp_reader *r;
534      struct save_macro_data **data;
535 {
536   struct save_macro_data *d = xmalloc (sizeof (struct save_macro_data));
537   
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;
543 }
544
545 /* Erase all the existing macros and assertions.  */
546
547 static int 
548 reset_ht (r, h, unused)
549      cpp_reader *r ATTRIBUTE_UNUSED;
550      cpp_hashnode *h;
551      void *unused ATTRIBUTE_UNUSED;
552 {
553   if (h->type != NT_VOID
554       && (h->flags & NODE_BUILTIN) == 0)
555     {
556       h->type = NT_VOID;
557       memset (&h->value, 0, sizeof (h->value));
558     }
559   return 1;
560 }
561
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.  */
565
566 int
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;
572 {
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;
580
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);
585
586   /* Restore spec_nodes, which will be full of references to the old 
587      hashtable entries and so will now be invalid.  */
588   {
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__"));
594   }
595
596   /* Run through the carefully-saved macros, insert them.  */
597   d = data->macros;
598   mac_count = data->count;
599   while (d)
600     {
601       struct save_macro_item *nextd;
602       for (i = 0; i < mac_count; i++)
603         {
604           cpp_hashnode *h;
605           
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])));
612         }
613       nextd = d->next;
614       free (d);
615       d = nextd;
616       mac_count = ARRAY_SIZE (d->macs);
617     }
618
619   _cpp_restore_pragma_names (r, data->saved_pragmas);
620
621   free (data);
622
623   old_state = r->state;
624
625   r->state.in_directive = 1;
626   r->state.prevent_expansion = 1;
627   r->state.angled_headers = 0;
628
629   /* Read in the identifiers that must be defined.  */
630   for (;;)
631     {
632       cpp_hashnode *h;
633       
634       if (fread (&m, sizeof (m), 1, f) != 1)
635         goto error;
636       
637       if (m.name_length == 0)
638         break;
639
640       if (defnlen < m.definition_length + 1)
641         {
642           defnlen = m.definition_length + 256;
643           defn = xrealloc (defn, defnlen);
644         }
645
646       if (fread (defn, 1, m.definition_length, f) != m.definition_length)
647         goto error;
648       defn[m.definition_length] = '\0';
649       
650       h = cpp_lookup (r, defn, m.name_length);
651
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)
657         {
658           if (cpp_push_buffer (r, defn + m.name_length, 
659                                m.definition_length - m.name_length, 
660                                true, 1) != NULL)
661             {
662               if (!_cpp_create_definition (r, h))
663                 abort ();
664               _cpp_pop_buffer (r);
665             }
666           else
667             abort ();
668         }
669     }
670
671   r->state = old_state;
672   r->line = saved_line;
673   free (defn);
674   defn = NULL;
675
676   if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
677       != 0)
678     goto error;
679
680   return 0;
681   
682  error:
683   cpp_errno (r, DL_ERROR, "while reading precompiled header");
684   return -1;
685 }