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
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. */
20 #include "coretypes.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
;
46 /* This is how we write out a macro definition.
47 Suitable for being called by cpp_forall_identifiers. */
50 write_macdef (pfile
, hn
, file_p
)
55 FILE *f
= (FILE *) file_p
;
59 if (! (hn
->flags
& NODE_POISONED
))
63 if ((hn
->flags
& NODE_BUILTIN
))
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
);
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");
94 /* Not currently implemented. */
102 /* This structure records the names of the defined macros.
103 It's also used as a callback structure for size_initial_idents
106 struct cpp_savedstate
108 /* A hash table of the defined identifiers. */
110 /* The size of the definitions of those identifiers (the size of
113 /* Number of definitions */
115 /* Array of definitions. In cpp_write_pch_deps it is used for sorting. */
117 /* Space for the next definition. Definitions are null-terminated
119 unsigned char *definedstrs
;
122 /* Save this identifier into the state: put it in the hash table,
123 put the definition in 'definedstrs'. */
126 save_idents (pfile
, hn
, ss_p
)
127 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
131 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
133 if (hn
->type
!= NT_VOID
)
135 struct cpp_string news
;
138 news
.len
= NODE_LEN (hn
);
139 news
.text
= NODE_NAME (hn
);
140 slot
= htab_find_slot (ss
->definedhash
, &news
, INSERT
);
143 struct cpp_string
*sp
;
146 sp
= xmalloc (sizeof (struct cpp_string
));
149 sp
->len
= NODE_LEN (hn
);
150 sp
->text
= text
= xmalloc (NODE_LEN (hn
));
151 memcpy (text
, NODE_NAME (hn
), NODE_LEN (hn
));
158 /* Hash some memory in a generic way. */
165 const unsigned char *p
= (const unsigned char *)p_p
;
170 for (i
= 0; i
< sz
; i
++)
171 h
= h
* 67 - (*p
++ - 113);
175 /* Hash a cpp string for the hashtable machinery. */
178 cpp_string_hash (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. */
188 cpp_string_eq (a_p
, 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
)
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
);
220 /* Calculate the 'hashsize' field of the saved state. */
223 count_defs (pfile
, hn
, ss_p
)
224 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
228 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
233 if (hn
->flags
& NODE_BUILTIN
)
236 /* else fall through. */
240 struct cpp_string news
;
243 news
.len
= NODE_LEN (hn
);
244 news
.text
= NODE_NAME (hn
);
245 slot
= htab_find (ss
->definedhash
, &news
);
248 ss
->hashsize
+= NODE_LEN (hn
) + 1;
255 /* Not currently implemented. */
263 /* Collect the identifiers into the state's string table. */
265 write_defs (pfile
, hn
, ss_p
)
266 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
270 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
275 if (hn
->flags
& NODE_BUILTIN
)
278 /* else fall through. */
282 struct cpp_string news
;
285 news
.len
= NODE_LEN (hn
);
286 news
.text
= NODE_NAME (hn
);
287 slot
= htab_find (ss
->definedhash
, &news
);
290 ss
->defs
[ss
->n_defs
] = hn
;
297 /* Not currently implemented. */
305 /* Comparison function for qsort. The arguments point to pointers of
306 type ht_hashnode *. */
308 comp_hashnodes (px
, 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
)
325 struct macrodef_struct z
;
326 struct cpp_savedstate
*const ss
= r
->savedstate
;
327 unsigned char *definedstrs
;
330 /* Collect the list of identifiers which have been seen and
331 weren't defined to anything previously. */
334 cpp_forall_identifiers (r
, count_defs
, ss
);
336 ss
->defs
= xmalloc (ss
->n_defs
* sizeof (cpp_hashnode
*));
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");
358 free (ss
->definedstrs
);
360 /* Free the saved state. */
362 r
->savedstate
= NULL
;
366 /* Write out the definitions of the preprocessor, in a form suitable for
370 cpp_write_pch_state (r
, 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");
386 r
->deps
= deps_init ();
388 if (deps_save (r
->deps
, f
) != 0)
390 cpp_errno (r
, DL_ERROR
, "while writing precompiled header");
398 /* Data structure to transform hash table nodes into a sorted list */
404 /* Number of nodes in the array */
406 /* Size of the allocated array */
410 /* Callback for collecting identifiers from hash table */
413 collect_ht_nodes (pfile
, hn
, nl_p
)
414 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
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
)
425 nl
->defs
= xrealloc (nl
->defs
, nl
->asize
* sizeof (cpp_hashnode
*));
428 nl
->defs
[nl
->n_defs
] = hn
;
435 /* Return nonzero if FD is a precompiled header which is consistent
436 with the preprocessor's current definitions. It will be consistent
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
444 NAME is used to print warnings if `warn_invalid_pch' is set in the
449 cpp_valid_state (r
, name
, 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
;
462 /* Read in the list of identifiers that must be defined
463 Check that they are defined in the same way. */
467 const unsigned char *newdefn
;
469 if (read (fd
, &m
, sizeof (m
)) != sizeof (m
))
472 if (m
.name_length
== 0)
475 if (m
.definition_length
> namebufsz
)
478 namebufsz
= m
.definition_length
+ 256;
479 namebuf
= xmalloc (namebufsz
);
482 if ((size_t)read (fd
, namebuf
, m
.definition_length
)
483 != m
.definition_length
)
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
);
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
);
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
)
521 /* Collect identifiers from the current hash table. */
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. */
531 last
= undeftab
+ m
.definition_length
;
534 while (first
< last
&& i
< nl
.n_defs
)
536 int cmp
= ustrcmp (first
, NODE_NAME (nl
.defs
[i
]));
539 first
+= ustrlen (first
) + 1;
553 cpp_errno (r
, DL_ERROR
, "while reading precompiled header");
559 if (undeftab
!= NULL
)
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
;
579 char **saved_pragmas
;
582 /* Save the definition of a single macro, so that it will persist across
586 save_macros (r
, h
, data_p
)
587 cpp_reader
*r ATTRIBUTE_UNUSED
;
591 struct save_macro_data
*data
= (struct save_macro_data
*)data_p
;
592 if (h
->type
!= NT_VOID
593 && (h
->flags
& NODE_BUILTIN
) == 0)
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
;
603 save
= data
->macros
->macs
+ 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);
613 /* Prepare to restore the state, by saving the currently-defined
617 cpp_prepare_state (r
, data
)
619 struct save_macro_data
**data
;
621 struct save_macro_data
*d
= xmalloc (sizeof (struct save_macro_data
));
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
);
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
)
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
;
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. */
661 mac_count
= data
->count
;
664 struct save_macro_item
*nextd
;
665 for (i
= 0; i
< mac_count
; i
++)
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
])));
679 mac_count
= ARRAY_SIZE (d
->macs
);
682 _cpp_restore_pragma_names (r
, data
->saved_pragmas
);
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. */
697 if (fread (&m
, sizeof (m
), 1, f
) != 1)
700 if (m
.name_length
== 0)
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
)
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
,
726 if (!_cpp_create_definition (r
, h
))
735 r
->state
= old_state
;
736 r
->line
= saved_line
;
740 if (deps_restore (r
->deps
, f
, CPP_OPTION (r
, restore_pch_deps
) ? name
: NULL
)
747 cpp_errno (r
, DL_ERROR
, "while reading precompiled header");