OSDN Git Service

alphabatize irix___restrict
[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,
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, 
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, "%s: not used because `%s' is defined",
458                        name, undeftab + i);
459           goto fail;
460         }
461       i += l + 1;
462     }
463   free (undeftab);
464
465   /* We win!  */
466   return 0;
467
468  error:
469   cpp_errno (r, DL_ERROR, "while reading precompiled header");
470   return -1;
471
472  fail:
473   if (namebuf != NULL)
474     free (namebuf);
475   if (undeftab != NULL)
476     free (undeftab);
477   return 1;
478 }
479
480 /* Save all the existing macros and assertions.  
481    This code assumes that there might be hundreds, but not thousands of
482    existing definitions.  */
483
484 struct save_macro_item {
485   struct save_macro_item *next;
486   struct cpp_hashnode macs[64];
487 };
488
489 struct save_macro_data 
490 {
491   struct save_macro_item *macros;
492   size_t count;
493   char **saved_pragmas;
494 };
495
496 /* Save the definition of a single macro, so that it will persist across
497    a PCH restore.  */
498
499 static int 
500 save_macros (r, h, data_p)
501      cpp_reader *r ATTRIBUTE_UNUSED;
502      cpp_hashnode *h;
503      void *data_p;
504 {
505   struct save_macro_data *data = (struct save_macro_data *)data_p;
506   if (h->type != NT_VOID
507       && (h->flags & NODE_BUILTIN) == 0)
508     {
509       cpp_hashnode *save;
510       if (data->count == ARRAY_SIZE (data->macros->macs))
511         {
512           struct save_macro_item *d = data->macros;
513           data->macros = xmalloc (sizeof (struct save_macro_item));
514           data->macros->next = d;
515           data->count = 0;
516         }
517       save = data->macros->macs + data->count;
518       data->count++;
519       memcpy (save, h, sizeof (struct cpp_hashnode));
520       HT_STR (&save->ident) = xmemdup (HT_STR (HT_NODE (save)),
521                                        HT_LEN (HT_NODE (save)),
522                                        HT_LEN (HT_NODE (save)) + 1);
523     }
524   return 1;
525 }
526
527 /* Prepare to restore the state, by saving the currently-defined
528    macros in 'data'.  */
529
530 void
531 cpp_prepare_state (r, data)
532      cpp_reader *r;
533      struct save_macro_data **data;
534 {
535   struct save_macro_data *d = xmalloc (sizeof (struct save_macro_data));
536   
537   d->macros = NULL;
538   d->count = ARRAY_SIZE (d->macros->macs);
539   cpp_forall_identifiers (r, save_macros, d);
540   d->saved_pragmas = _cpp_save_pragma_names (r);
541   *data = d;
542 }
543
544 /* Erase all the existing macros and assertions.  */
545
546 static int 
547 reset_ht (r, h, unused)
548      cpp_reader *r ATTRIBUTE_UNUSED;
549      cpp_hashnode *h;
550      void *unused ATTRIBUTE_UNUSED;
551 {
552   if (h->type != NT_VOID
553       && (h->flags & NODE_BUILTIN) == 0)
554     {
555       h->type = NT_VOID;
556       memset (&h->value, 0, sizeof (h->value));
557     }
558   return 1;
559 }
560
561 /* Given a precompiled header that was previously determined to be valid,
562    apply all its definitions (and undefinitions) to the current state. 
563    DEPNAME is passed to deps_restore.  */
564
565 int
566 cpp_read_state (r, name, f, data)
567      cpp_reader *r;
568      const char *name;
569      FILE *f;
570      struct save_macro_data *data;
571 {
572   struct macrodef_struct m;
573   size_t defnlen = 256;
574   unsigned char *defn = xmalloc (defnlen);
575   struct lexer_state old_state;
576   struct save_macro_item *d;
577   size_t i, mac_count;
578   int saved_line = r->line;
579
580   /* Erase all the existing hashtable entries for macros.  At this
581      point, they're all from the PCH file, and their pointers won't be
582      valid.  */
583   cpp_forall_identifiers (r, reset_ht, NULL);
584
585   /* Restore spec_nodes, which will be full of references to the old 
586      hashtable entries and so will now be invalid.  */
587   {
588     struct spec_nodes *s = &r->spec_nodes;
589     s->n_defined        = cpp_lookup (r, DSC("defined"));
590     s->n_true           = cpp_lookup (r, DSC("true"));
591     s->n_false          = cpp_lookup (r, DSC("false"));
592     s->n__VA_ARGS__     = cpp_lookup (r, DSC("__VA_ARGS__"));
593   }
594
595   /* Run through the carefully-saved macros, insert them.  */
596   d = data->macros;
597   mac_count = data->count;
598   while (d)
599     {
600       struct save_macro_item *nextd;
601       for (i = 0; i < mac_count; i++)
602         {
603           cpp_hashnode *h;
604           
605           h = cpp_lookup (r, HT_STR (HT_NODE (&d->macs[i])), 
606                           HT_LEN (HT_NODE (&d->macs[i])));
607           h->type = d->macs[i].type;
608           h->flags = d->macs[i].flags;
609           h->value = d->macs[i].value;
610           free ((void *)HT_STR (HT_NODE (&d->macs[i])));
611         }
612       nextd = d->next;
613       free (d);
614       d = nextd;
615       mac_count = ARRAY_SIZE (d->macs);
616     }
617
618   _cpp_restore_pragma_names (r, data->saved_pragmas);
619
620   free (data);
621
622   old_state = r->state;
623
624   r->state.in_directive = 1;
625   r->state.prevent_expansion = 1;
626   r->state.angled_headers = 0;
627
628   /* Read in the identifiers that must be defined.  */
629   for (;;)
630     {
631       cpp_hashnode *h;
632       
633       if (fread (&m, sizeof (m), 1, f) != 1)
634         goto error;
635       
636       if (m.name_length == 0)
637         break;
638
639       if (defnlen < m.definition_length + 1)
640         {
641           defnlen = m.definition_length + 256;
642           defn = xrealloc (defn, defnlen);
643         }
644
645       if (fread (defn, 1, m.definition_length, f) != m.definition_length)
646         goto error;
647       defn[m.definition_length] = '\0';
648       
649       h = cpp_lookup (r, defn, m.name_length);
650
651       if (h->type == NT_MACRO)
652         _cpp_free_definition (h);
653       if (m.flags & NODE_POISONED)
654         h->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
655       else if (m.name_length != m.definition_length)
656         {
657           if (cpp_push_buffer (r, defn + m.name_length, 
658                                m.definition_length - m.name_length, 
659                                true, 1) != NULL)
660             {
661               if (!_cpp_create_definition (r, h))
662                 abort ();
663               _cpp_pop_buffer (r);
664             }
665           else
666             abort ();
667         }
668     }
669
670   r->state = old_state;
671   r->line = saved_line;
672   free (defn);
673   defn = NULL;
674
675   if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
676       != 0)
677     goto error;
678
679   return 0;
680   
681  error:
682   cpp_errno (r, DL_ERROR, "while reading precompiled header");
683   return -1;
684 }