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 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 *));
37 /* This structure represents a macro definition on disk. */
38 struct macrodef_struct
40 unsigned int definition_length
;
41 unsigned short name_length
;
45 /* This is how we write out a macro definition.
46 Suitable for being called by cpp_forall_identifiers. */
49 write_macdef (pfile
, hn
, file_p
)
54 FILE *f
= (FILE *) file_p
;
58 if (! (hn
->flags
& NODE_POISONED
))
62 if ((hn
->flags
& NODE_BUILTIN
))
66 struct macrodef_struct s
;
67 const unsigned char *defn
;
69 s
.name_length
= NODE_LEN (hn
);
70 s
.flags
= hn
->flags
& NODE_POISONED
;
72 if (hn
->type
== NT_MACRO
)
74 defn
= cpp_macro_definition (pfile
, hn
);
75 s
.definition_length
= ustrlen (defn
);
79 defn
= NODE_NAME (hn
);
80 s
.definition_length
= s
.name_length
;
83 if (fwrite (&s
, sizeof (s
), 1, f
) != 1
84 || fwrite (defn
, 1, s
.definition_length
, f
) != s
.definition_length
)
86 cpp_errno (pfile
, DL_ERROR
, "while writing precompiled header");
93 /* Not currently implemented. */
101 /* This structure records the names of the defined macros.
102 It's also used as a callback structure for size_initial_idents
105 struct cpp_savedstate
107 /* A hash table of the defined identifiers. */
109 /* The size of the definitions of those identifiers (the size of
112 /* Space for the next definition. Definitions are null-terminated
114 unsigned char *definedstrs
;
117 /* Save this identifier into the state: put it in the hash table,
118 put the definition in 'definedstrs'. */
121 save_idents (pfile
, hn
, ss_p
)
122 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
126 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
128 if (hn
->type
!= NT_VOID
)
130 struct cpp_string news
;
133 news
.len
= NODE_LEN (hn
);
134 news
.text
= NODE_NAME (hn
);
135 slot
= htab_find_slot (ss
->definedhash
, &news
, INSERT
);
138 struct cpp_string
*sp
;
141 sp
= xmalloc (sizeof (struct cpp_string
));
144 sp
->len
= NODE_LEN (hn
);
145 sp
->text
= text
= xmalloc (NODE_LEN (hn
));
146 memcpy (text
, NODE_NAME (hn
), NODE_LEN (hn
));
153 /* Hash some memory in a generic way. */
160 const unsigned char *p
= (const unsigned char *)p_p
;
165 for (i
= 0; i
< sz
; i
++)
166 h
= h
* 67 - (*p
++ - 113);
170 /* Hash a cpp string for the hashtable machinery. */
173 cpp_string_hash (a_p
)
176 const struct cpp_string
*a
= (const struct cpp_string
*) a_p
;
177 return hashmem (a
->text
, a
->len
);
180 /* Compare two cpp strings for the hashtable machinery. */
183 cpp_string_eq (a_p
, b_p
)
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);
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. */
199 cpp_save_state (r
, f
)
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
);
209 /* Write out the list of defined identifiers. */
210 cpp_forall_identifiers (r
, write_macdef
, f
);
215 /* Calculate the 'hashsize' field of the saved state. */
218 count_defs (pfile
, hn
, ss_p
)
219 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
223 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
228 if (hn
->flags
& NODE_BUILTIN
)
231 /* else fall through. */
235 struct cpp_string news
;
238 news
.len
= NODE_LEN (hn
);
239 news
.text
= NODE_NAME (hn
);
240 slot
= htab_find (ss
->definedhash
, &news
);
242 ss
->hashsize
+= NODE_LEN (hn
) + 1;
247 /* Not currently implemented. */
255 /* Write the identifiers into 'definedstrs' of the state. */
258 write_defs (pfile
, hn
, ss_p
)
259 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
263 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
268 if (hn
->flags
& NODE_BUILTIN
)
271 /* else fall through. */
275 struct cpp_string news
;
278 news
.len
= NODE_LEN (hn
);
279 news
.text
= NODE_NAME (hn
);
280 slot
= htab_find (ss
->definedhash
, &news
);
283 memcpy (ss
->definedstrs
, NODE_NAME (hn
), NODE_LEN (hn
));
284 ss
->definedstrs
[NODE_LEN (hn
)] = 0;
285 ss
->definedstrs
+= NODE_LEN (hn
) + 1;
291 /* Not currently implemented. */
299 /* Write out the remainder of the dependency information. This should be
300 called after the PCH is ready to be saved. */
303 cpp_write_pch_deps (r
, f
)
307 struct macrodef_struct z
;
308 struct cpp_savedstate
*const ss
= r
->savedstate
;
309 unsigned char *definedstrs
;
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)
323 cpp_errno (r
, DL_ERROR
, "while writing precompiled header");
328 /* Free the saved state. */
330 r
->savedstate
= NULL
;
334 /* Write out the definitions of the preprocessor, in a form suitable for
338 cpp_write_pch_state (r
, f
)
342 struct macrodef_struct z
;
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)
349 cpp_errno (r
, DL_ERROR
, "while writing precompiled header");
354 r
->deps
= deps_init ();
356 if (deps_save (r
->deps
, f
) != 0)
358 cpp_errno (r
, DL_ERROR
, "while writing precompiled header");
365 /* Return nonzero if FD is a precompiled header which is consistent
366 with the preprocessor's current definitions. It will be consistent
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
374 NAME is used to print warnings if `warn_invalid_pch' is set in the
379 cpp_valid_state (r
, name
, fd
)
384 struct macrodef_struct m
;
385 size_t namebufsz
= 256;
386 unsigned char *namebuf
= xmalloc (namebufsz
);
387 unsigned char *undeftab
= NULL
;
390 /* Read in the list of identifiers that must be defined
391 Check that they are defined in the same way. */
395 const unsigned char *newdefn
;
397 if (read (fd
, &m
, sizeof (m
)) != sizeof (m
))
400 if (m
.name_length
== 0)
403 if (m
.definition_length
> namebufsz
)
406 namebufsz
= m
.definition_length
+ 256;
407 namebuf
= xmalloc (namebufsz
);
410 if ((size_t)read (fd
, namebuf
, m
.definition_length
)
411 != m
.definition_length
)
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
)
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
);
426 newdefn
= cpp_macro_definition (r
, h
);
428 if (m
.definition_length
!= ustrlen (newdefn
)
429 || memcmp (namebuf
, newdefn
, m
.definition_length
) != 0)
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
);
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
)
448 for (i
= 0; i
< m
.definition_length
; )
450 int l
= ustrlen (undeftab
+ i
);
452 h
= cpp_lookup (r
, undeftab
+ i
, l
);
453 if (h
->type
!= NT_VOID
454 || h
->flags
& NODE_POISONED
)
456 if (CPP_OPTION (r
, warn_invalid_pch
))
457 cpp_error (r
, DL_WARNING
, "%s: not used because `%s' is defined",
469 cpp_errno (r
, DL_ERROR
, "while reading precompiled header");
475 if (undeftab
!= NULL
)
480 /* Save all the existing macros and assertions.
481 This code assumes that there might be hundreds, but not thousands of
482 existing definitions. */
484 struct save_macro_item
{
485 struct save_macro_item
*next
;
486 struct cpp_hashnode macs
[64];
489 struct save_macro_data
491 struct save_macro_item
*macros
;
493 char **saved_pragmas
;
496 /* Save the definition of a single macro, so that it will persist across
500 save_macros (r
, h
, data_p
)
501 cpp_reader
*r ATTRIBUTE_UNUSED
;
505 struct save_macro_data
*data
= (struct save_macro_data
*)data_p
;
506 if (h
->type
!= NT_VOID
507 && (h
->flags
& NODE_BUILTIN
) == 0)
510 if (data
->count
== ARRAY_SIZE (data
->macros
->macs
))
512 struct save_macro_item
*d
= data
->macros
;
513 data
->macros
= xmalloc (sizeof (struct save_macro_item
));
514 data
->macros
->next
= d
;
517 save
= data
->macros
->macs
+ 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);
527 /* Prepare to restore the state, by saving the currently-defined
531 cpp_prepare_state (r
, data
)
533 struct save_macro_data
**data
;
535 struct save_macro_data
*d
= xmalloc (sizeof (struct save_macro_data
));
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
);
544 /* Erase all the existing macros and assertions. */
547 reset_ht (r
, h
, unused
)
548 cpp_reader
*r ATTRIBUTE_UNUSED
;
550 void *unused ATTRIBUTE_UNUSED
;
552 if (h
->type
!= NT_VOID
553 && (h
->flags
& NODE_BUILTIN
) == 0)
556 memset (&h
->value
, 0, sizeof (h
->value
));
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. */
566 cpp_read_state (r
, name
, f
, data
)
570 struct save_macro_data
*data
;
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
;
578 int saved_line
= r
->line
;
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
583 cpp_forall_identifiers (r
, reset_ht
, NULL
);
585 /* Restore spec_nodes, which will be full of references to the old
586 hashtable entries and so will now be invalid. */
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__"));
595 /* Run through the carefully-saved macros, insert them. */
597 mac_count
= data
->count
;
600 struct save_macro_item
*nextd
;
601 for (i
= 0; i
< mac_count
; i
++)
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
])));
615 mac_count
= ARRAY_SIZE (d
->macs
);
618 _cpp_restore_pragma_names (r
, data
->saved_pragmas
);
622 old_state
= r
->state
;
624 r
->state
.in_directive
= 1;
625 r
->state
.prevent_expansion
= 1;
626 r
->state
.angled_headers
= 0;
628 /* Read in the identifiers that must be defined. */
633 if (fread (&m
, sizeof (m
), 1, f
) != 1)
636 if (m
.name_length
== 0)
639 if (defnlen
< m
.definition_length
+ 1)
641 defnlen
= m
.definition_length
+ 256;
642 defn
= xrealloc (defn
, defnlen
);
645 if (fread (defn
, 1, m
.definition_length
, f
) != m
.definition_length
)
647 defn
[m
.definition_length
] = '\0';
649 h
= cpp_lookup (r
, defn
, m
.name_length
);
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
)
657 if (cpp_push_buffer (r
, defn
+ m
.name_length
,
658 m
.definition_length
- m
.name_length
,
661 if (!_cpp_create_definition (r
, h
))
670 r
->state
= old_state
;
671 r
->line
= saved_line
;
675 if (deps_restore (r
->deps
, f
, CPP_OPTION (r
, restore_pch_deps
) ? name
: NULL
)
682 cpp_errno (r
, DL_ERROR
, "while reading precompiled header");