1 /* Part of CPP library. (Precompiled header reading/writing.)
2 Copyright (C) 2000-2018 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 3, 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; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
25 static int write_macdef (cpp_reader
*, cpp_hashnode
*, void *);
26 static int save_idents (cpp_reader
*, cpp_hashnode
*, void *);
27 static hashval_t
hashmem (const void *, size_t);
28 static hashval_t
cpp_string_hash (const void *);
29 static int cpp_string_eq (const void *, const void *);
30 static int count_defs (cpp_reader
*, cpp_hashnode
*, void *);
31 static int comp_hashnodes (const void *, const void *);
32 static int collect_ht_nodes (cpp_reader
*, cpp_hashnode
*, void *);
33 static int write_defs (cpp_reader
*, cpp_hashnode
*, void *);
34 static int save_macros (cpp_reader
*, cpp_hashnode
*, void *);
35 static int _cpp_save_pushed_macros (cpp_reader
*, FILE *);
36 static int _cpp_restore_pushed_macros (cpp_reader
*, FILE *);
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 (cpp_reader
*pfile
, cpp_hashnode
*hn
, void *file_p
)
52 FILE *f
= (FILE *) file_p
;
56 if (! (hn
->flags
& NODE_POISONED
))
58 /* XXX Really fallthru? */
62 if ((hn
->flags
& NODE_BUILTIN
)
63 && (!pfile
->cb
.user_builtin_macro
64 || !pfile
->cb
.user_builtin_macro (pfile
, hn
)))
68 struct macrodef_struct s
;
69 const unsigned char *defn
;
71 s
.name_length
= NODE_LEN (hn
);
72 s
.flags
= hn
->flags
& NODE_POISONED
;
74 if (hn
->type
== NT_MACRO
)
76 defn
= cpp_macro_definition (pfile
, hn
);
77 s
.definition_length
= ustrlen (defn
);
81 defn
= NODE_NAME (hn
);
82 s
.definition_length
= s
.name_length
;
85 if (fwrite (&s
, sizeof (s
), 1, f
) != 1
86 || fwrite (defn
, 1, s
.definition_length
, f
) != s
.definition_length
)
88 cpp_errno (pfile
, CPP_DL_ERROR
,
89 "while writing precompiled header");
96 /* Not currently implemented. */
104 /* This structure records the names of the defined macros.
105 It's also used as a callback structure for size_initial_idents
108 struct cpp_savedstate
110 /* A hash table of the defined identifiers. */
112 /* The size of the definitions of those identifiers (the size of
115 /* Number of definitions */
117 /* Array of definitions. In cpp_write_pch_deps it is used for sorting. */
119 /* Space for the next definition. Definitions are null-terminated
121 unsigned char *definedstrs
;
124 /* Save this identifier into the state: put it in the hash table,
125 put the definition in 'definedstrs'. */
128 save_idents (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*hn
, void *ss_p
)
130 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
132 if (hn
->type
!= NT_VOID
)
134 struct cpp_string news
;
137 news
.len
= NODE_LEN (hn
);
138 news
.text
= NODE_NAME (hn
);
139 slot
= htab_find_slot (ss
->definedhash
, &news
, INSERT
);
142 struct cpp_string
*sp
;
145 sp
= XNEW (struct cpp_string
);
148 sp
->len
= NODE_LEN (hn
);
149 sp
->text
= text
= XNEWVEC (unsigned char, NODE_LEN (hn
));
150 memcpy (text
, NODE_NAME (hn
), NODE_LEN (hn
));
157 /* Hash some memory in a generic way. */
160 hashmem (const void *p_p
, size_t sz
)
162 const unsigned char *p
= (const unsigned char *)p_p
;
167 for (i
= 0; i
< sz
; i
++)
168 h
= h
* 67 - (*p
++ - 113);
172 /* Hash a cpp string for the hashtable machinery. */
175 cpp_string_hash (const void *a_p
)
177 const struct cpp_string
*a
= (const struct cpp_string
*) a_p
;
178 return hashmem (a
->text
, a
->len
);
181 /* Compare two cpp strings for the hashtable machinery. */
184 cpp_string_eq (const void *a_p
, const void *b_p
)
186 const struct cpp_string
*a
= (const struct cpp_string
*) a_p
;
187 const struct cpp_string
*b
= (const struct cpp_string
*) b_p
;
188 return (a
->len
== b
->len
189 && memcmp (a
->text
, b
->text
, a
->len
) == 0);
192 /* Free memory associated with cpp_string. */
195 cpp_string_free (void *a_p
)
197 struct cpp_string
*a
= (struct cpp_string
*) a_p
;
198 free ((void *) a
->text
);
202 /* Save the current definitions of the cpp_reader for dependency
203 checking purposes. When writing a precompiled header, this should
204 be called at the same point in the compilation as cpp_valid_state
205 would be called when reading the precompiled header back in. */
208 cpp_save_state (cpp_reader
*r
, FILE *f
)
210 /* Save the list of non-void identifiers for the dependency checking. */
211 r
->savedstate
= XNEW (struct cpp_savedstate
);
212 r
->savedstate
->definedhash
= htab_create (100, cpp_string_hash
,
213 cpp_string_eq
, cpp_string_free
);
214 cpp_forall_identifiers (r
, save_idents
, r
->savedstate
);
216 /* Write out the list of defined identifiers. */
217 cpp_forall_identifiers (r
, write_macdef
, f
);
222 /* Calculate the 'hashsize' field of the saved state. */
225 count_defs (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*hn
, void *ss_p
)
227 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
232 if (hn
->flags
& NODE_BUILTIN
)
239 struct cpp_string news
;
242 news
.len
= NODE_LEN (hn
);
243 news
.text
= NODE_NAME (hn
);
244 slot
= (void **) htab_find (ss
->definedhash
, &news
);
247 ss
->hashsize
+= NODE_LEN (hn
) + 1;
254 /* Not currently implemented. */
262 /* Collect the identifiers into the state's string table. */
264 write_defs (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*hn
, void *ss_p
)
266 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
271 if (hn
->flags
& NODE_BUILTIN
)
278 struct cpp_string news
;
281 news
.len
= NODE_LEN (hn
);
282 news
.text
= NODE_NAME (hn
);
283 slot
= (void **) htab_find (ss
->definedhash
, &news
);
286 ss
->defs
[ss
->n_defs
] = hn
;
293 /* Not currently implemented. */
301 /* Comparison function for qsort. The arguments point to pointers of
302 type ht_hashnode *. */
304 comp_hashnodes (const void *px
, const void *py
)
306 cpp_hashnode
*x
= *(cpp_hashnode
**) px
;
307 cpp_hashnode
*y
= *(cpp_hashnode
**) py
;
308 return ustrcmp (NODE_NAME (x
), NODE_NAME (y
));
311 /* Write out the remainder of the dependency information. This should be
312 called after the PCH is ready to be saved. */
315 cpp_write_pch_deps (cpp_reader
*r
, FILE *f
)
317 struct macrodef_struct z
;
318 struct cpp_savedstate
*const ss
= r
->savedstate
;
319 unsigned char *definedstrs
;
322 /* Collect the list of identifiers which have been seen and
323 weren't defined to anything previously. */
326 cpp_forall_identifiers (r
, count_defs
, ss
);
328 ss
->defs
= XNEWVEC (cpp_hashnode
*, ss
->n_defs
);
330 cpp_forall_identifiers (r
, write_defs
, ss
);
332 /* Sort the list, copy it into a buffer, and write it out. */
333 qsort (ss
->defs
, ss
->n_defs
, sizeof (cpp_hashnode
*), &comp_hashnodes
);
334 definedstrs
= ss
->definedstrs
= XNEWVEC (unsigned char, ss
->hashsize
);
335 for (i
= 0; i
< ss
->n_defs
; ++i
)
337 size_t len
= NODE_LEN (ss
->defs
[i
]);
338 memcpy (definedstrs
, NODE_NAME (ss
->defs
[i
]), len
+ 1);
339 definedstrs
+= len
+ 1;
342 memset (&z
, 0, sizeof (z
));
343 z
.definition_length
= ss
->hashsize
;
344 if (fwrite (&z
, sizeof (z
), 1, f
) != 1
345 || fwrite (ss
->definedstrs
, ss
->hashsize
, 1, f
) != 1)
347 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
350 free (ss
->definedstrs
);
352 htab_delete (ss
->definedhash
);
354 /* Free the saved state. */
356 r
->savedstate
= NULL
;
358 /* Save the next value of __COUNTER__. */
359 if (fwrite (&r
->counter
, sizeof (r
->counter
), 1, f
) != 1)
361 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
368 /* Write out the definitions of the preprocessor, in a form suitable for
372 cpp_write_pch_state (cpp_reader
*r
, FILE *f
)
375 r
->deps
= deps_init ();
377 if (deps_save (r
->deps
, f
) != 0)
379 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
383 if (! _cpp_save_file_entries (r
, f
))
385 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
389 /* Save the next __COUNTER__ value. When we include a precompiled header,
390 we need to start at the offset we would have if the header had been
391 included normally. */
392 if (fwrite (&r
->counter
, sizeof (r
->counter
), 1, f
) != 1)
394 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
398 /* Write saved macros. */
399 if (! _cpp_save_pushed_macros (r
, f
))
401 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
409 _cpp_restore_pushed_macros (cpp_reader
*r
, FILE *f
)
411 size_t count_saved
= 0;
413 struct def_pragma_macro
*p
;
418 if (fread (&count_saved
, sizeof (count_saved
), 1, f
) != 1)
422 for (i
= 0; i
< count_saved
; i
++)
424 if (fread (&nlen
, sizeof (nlen
), 1, f
) != 1)
426 p
= XNEW (struct def_pragma_macro
);
427 memset (p
, 0, sizeof (struct def_pragma_macro
));
428 p
->name
= XNEWVAR (char, nlen
+ 1);
430 if (fread (p
->name
, nlen
, 1, f
) != 1)
432 if (fread (&defnlen
, sizeof (defnlen
), 1, f
) != 1)
438 defn
= XNEWVEC (uchar
, defnlen
+ 1);
441 if (fread (defn
, defnlen
, 1, f
) != 1)
444 p
->definition
= defn
;
445 if (fread (&(p
->line
), sizeof (source_location
), 1, f
) != 1)
448 if (fread (&defnlen
, sizeof (defnlen
), 1, f
) != 1)
450 p
->syshdr
= ((defnlen
& 1) != 0 ? 1 : 0);
451 p
->used
= ((defnlen
& 2) != 0 ? 1 : 0);
454 p
->next
= r
->pushed_macros
;
455 r
->pushed_macros
= p
;
461 _cpp_save_pushed_macros (cpp_reader
*r
, FILE *f
)
463 size_t count_saved
= 0;
465 struct def_pragma_macro
*p
,**pp
;
469 p
= r
->pushed_macros
;
475 if (fwrite (&count_saved
, sizeof (count_saved
), 1, f
) != 1)
480 pp
= (struct def_pragma_macro
**) alloca (sizeof (struct def_pragma_macro
*)
482 /* Store them in reverse order. */
483 p
= r
->pushed_macros
;
491 for (i
= 0; i
< count_saved
; i
++)
493 defnlen
= strlen (pp
[i
]->name
);
494 if (fwrite (&defnlen
, sizeof (size_t), 1, f
) != 1
495 || fwrite (pp
[i
]->name
, defnlen
, 1, f
) != 1)
500 if (fwrite (&defnlen
, sizeof (size_t), 1, f
) != 1)
505 defnlen
= ustrlen (pp
[i
]->definition
);
506 if (fwrite (&defnlen
, sizeof (size_t), 1, f
) != 1
507 || fwrite (pp
[i
]->definition
, defnlen
, 1, f
) != 1)
509 if (fwrite (&(pp
[i
]->line
), sizeof (source_location
), 1, f
) != 1)
512 defnlen
|= (pp
[i
]->syshdr
!= 0 ? 1 : 0);
513 defnlen
|= (pp
[i
]->used
!= 0 ? 2 : 0);
514 if (fwrite (&defnlen
, sizeof (defnlen
), 1, f
) != 1)
522 /* Data structure to transform hash table nodes into a sorted list */
528 /* Number of nodes in the array */
530 /* Size of the allocated array */
534 /* Callback for collecting identifiers from hash table */
537 collect_ht_nodes (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*hn
,
540 struct ht_node_list
*const nl
= (struct ht_node_list
*)nl_p
;
542 if (hn
->type
!= NT_VOID
|| hn
->flags
& NODE_POISONED
)
544 if (nl
->n_defs
== nl
->asize
)
547 nl
->defs
= XRESIZEVEC (cpp_hashnode
*, nl
->defs
, nl
->asize
);
550 nl
->defs
[nl
->n_defs
] = hn
;
557 /* Return nonzero if FD is a precompiled header which is consistent
558 with the preprocessor's current definitions. It will be consistent
561 - anything that was defined just before the PCH was generated
562 is defined the same way now; and
563 - anything that was not defined then, but is defined now, was not
566 NAME is used to print warnings if `warn_invalid_pch' is set in the
571 cpp_valid_state (cpp_reader
*r
, const char *name
, int fd
)
573 struct macrodef_struct m
;
574 size_t namebufsz
= 256;
575 unsigned char *namebuf
= XNEWVEC (unsigned char, namebufsz
);
576 unsigned char *undeftab
= NULL
;
577 struct ht_node_list nl
= { 0, 0, 0 };
578 unsigned char *first
, *last
;
580 unsigned int counter
;
582 /* Read in the list of identifiers that must be defined
583 Check that they are defined in the same way. */
587 const unsigned char *newdefn
;
589 if (read (fd
, &m
, sizeof (m
)) != sizeof (m
))
592 if (m
.name_length
== 0)
595 /* If this file is already preprocessed, there won't be any
596 macros defined, and that's OK. */
597 if (CPP_OPTION (r
, preprocessed
))
599 if (lseek (fd
, m
.definition_length
, SEEK_CUR
) == -1)
604 if (m
.definition_length
> namebufsz
)
607 namebufsz
= m
.definition_length
+ 256;
608 namebuf
= XNEWVEC (unsigned char, namebufsz
);
611 if ((size_t)read (fd
, namebuf
, m
.definition_length
)
612 != m
.definition_length
)
615 h
= cpp_lookup (r
, namebuf
, m
.name_length
);
616 if (m
.flags
& NODE_POISONED
617 || h
->flags
& NODE_POISONED
)
619 if (CPP_OPTION (r
, warn_invalid_pch
))
620 cpp_warning_syshdr (r
, CPP_W_INVALID_PCH
,
621 "%s: not used because `%.*s' is poisoned",
622 name
, m
.name_length
, namebuf
);
626 if (h
->type
!= NT_MACRO
)
628 /* It's ok if __GCC_HAVE_DWARF2_CFI_ASM becomes undefined,
629 as in, when the PCH file is created with -g and we're
630 attempting to use it without -g. Restoring the PCH file
631 is supposed to bring in this definition *and* enable the
632 generation of call frame information, so that precompiled
633 definitions that take this macro into account, to decide
634 what asm to emit, won't issue .cfi directives when the
636 if (!(h
->flags
& NODE_USED
)
637 && m
.name_length
== sizeof ("__GCC_HAVE_DWARF2_CFI_ASM") - 1
638 && !memcmp (namebuf
, "__GCC_HAVE_DWARF2_CFI_ASM", m
.name_length
))
641 if (CPP_OPTION (r
, warn_invalid_pch
))
642 cpp_warning_syshdr (r
, CPP_W_INVALID_PCH
,
643 "%s: not used because `%.*s' not defined",
644 name
, m
.name_length
, namebuf
);
648 newdefn
= cpp_macro_definition (r
, h
);
650 if (m
.definition_length
!= ustrlen (newdefn
)
651 || memcmp (namebuf
, newdefn
, m
.definition_length
) != 0)
653 if (CPP_OPTION (r
, warn_invalid_pch
))
654 cpp_warning_syshdr (r
, CPP_W_INVALID_PCH
,
655 "%s: not used because `%.*s' defined as `%s' not `%.*s'",
656 name
, m
.name_length
, namebuf
, newdefn
+ m
.name_length
,
657 m
.definition_length
- m
.name_length
,
658 namebuf
+ m
.name_length
);
665 /* Read in the list of identifiers that must not be defined.
666 Check that they really aren't. */
667 undeftab
= XNEWVEC (unsigned char, m
.definition_length
);
668 if ((size_t) read (fd
, undeftab
, m
.definition_length
) != m
.definition_length
)
671 /* Collect identifiers from the current hash table. */
674 nl
.defs
= XNEWVEC (cpp_hashnode
*, nl
.asize
);
675 cpp_forall_identifiers (r
, &collect_ht_nodes
, &nl
);
676 qsort (nl
.defs
, nl
.n_defs
, sizeof (cpp_hashnode
*), &comp_hashnodes
);
678 /* Loop through nl.defs and undeftab, both of which are sorted lists.
679 There should be no matches. */
681 last
= undeftab
+ m
.definition_length
;
684 while (first
< last
&& i
< nl
.n_defs
)
686 int cmp
= ustrcmp (first
, NODE_NAME (nl
.defs
[i
]));
689 first
+= ustrlen (first
) + 1;
694 if (CPP_OPTION (r
, warn_invalid_pch
))
695 cpp_warning_syshdr (r
, CPP_W_INVALID_PCH
,
696 "%s: not used because `%s' is defined",
707 /* Read in the next value of __COUNTER__.
708 Check that (a) __COUNTER__ was not used in the pch or (b) __COUNTER__
709 has not been used in this translation unit. */
710 if (read (fd
, &counter
, sizeof (counter
)) != sizeof (counter
))
712 if (counter
&& r
->counter
)
714 if (CPP_OPTION (r
, warn_invalid_pch
))
715 cpp_warning_syshdr (r
, CPP_W_INVALID_PCH
,
716 "%s: not used because `__COUNTER__' is invalid",
725 cpp_errno (r
, CPP_DL_ERROR
, "while reading precompiled header");
734 /* Save all the existing macros. */
736 struct save_macro_data
741 char **saved_pragmas
;
744 /* Save the definition of a single macro, so that it will persist
745 across a PCH restore. Because macro data is in GCed memory, which
746 will be blown away by PCH, it must be temporarily copied to
747 malloced memory. (The macros will refer to identifier nodes which
748 are also GCed and so on, so the copying is done by turning them
749 into self-contained strings.) The assumption is that most macro
750 definitions will come from the PCH file, not from the compilation
751 before the PCH file is loaded, so it doesn't matter that this is
754 It would reduce the cost even further if macros defined in the PCH
755 file were not saved in this way, but this is not done (yet), except
756 for builtins, and for #assert by default. */
759 save_macros (cpp_reader
*r
, cpp_hashnode
*h
, void *data_p
)
761 struct save_macro_data
*data
= (struct save_macro_data
*)data_p
;
763 if ((h
->flags
& NODE_BUILTIN
)
764 && h
->type
== NT_MACRO
765 && r
->cb
.user_builtin_macro
)
766 r
->cb
.user_builtin_macro (r
, h
);
768 if (h
->type
!= NT_VOID
769 && (h
->flags
& NODE_BUILTIN
) == 0)
771 if (data
->count
== data
->array_size
)
773 data
->array_size
*= 2;
774 data
->defns
= XRESIZEVEC (uchar
*, data
->defns
, (data
->array_size
));
780 /* Not currently implemented. */
785 const uchar
* defn
= cpp_macro_definition (r
, h
);
786 size_t defnlen
= ustrlen (defn
);
788 data
->defns
[data
->count
] = (uchar
*) xmemdup (defn
, defnlen
,
790 data
->defns
[data
->count
][defnlen
] = '\n';
802 /* Prepare to restore the state, by saving the currently-defined
806 cpp_prepare_state (cpp_reader
*r
, struct save_macro_data
**data
)
808 struct save_macro_data
*d
= XNEW (struct save_macro_data
);
811 d
->defns
= XNEWVEC (uchar
*, d
->array_size
);
813 cpp_forall_identifiers (r
, save_macros
, d
);
814 d
->saved_pragmas
= _cpp_save_pragma_names (r
);
818 /* Given a precompiled header that was previously determined to be valid,
819 apply all its definitions (and undefinitions) to the current state.
820 DEPNAME is passed to deps_restore. */
823 cpp_read_state (cpp_reader
*r
, const char *name
, FILE *f
,
824 struct save_macro_data
*data
)
827 struct lexer_state old_state
;
828 unsigned int counter
;
830 /* Restore spec_nodes, which will be full of references to the old
831 hashtable entries and so will now be invalid. */
833 struct spec_nodes
*s
= &r
->spec_nodes
;
834 s
->n_defined
= cpp_lookup (r
, DSC("defined"));
835 s
->n_true
= cpp_lookup (r
, DSC("true"));
836 s
->n_false
= cpp_lookup (r
, DSC("false"));
837 s
->n__VA_ARGS__
= cpp_lookup (r
, DSC("__VA_ARGS__"));
838 s
->n__VA_OPT__
= cpp_lookup (r
, DSC("__VA_OPT__"));
839 s
->n__has_include__
= cpp_lookup (r
, DSC("__has_include__"));
840 s
->n__has_include_next__
= cpp_lookup (r
, DSC("__has_include_next__"));
843 old_state
= r
->state
;
844 r
->state
.in_directive
= 1;
845 r
->state
.prevent_expansion
= 1;
846 r
->state
.angled_headers
= 0;
848 /* Run through the carefully-saved macros, insert them. */
849 for (i
= 0; i
< data
->count
; i
++)
855 namelen
= ustrcspn (data
->defns
[i
], "( \n");
856 h
= cpp_lookup (r
, data
->defns
[i
], namelen
);
857 defn
= data
->defns
[i
] + namelen
;
859 /* The PCH file is valid, so we know that if there is a definition
860 from the PCH file it must be the same as the one we had
861 originally, and so do not need to restore it. */
862 if (h
->type
== NT_VOID
)
864 if (cpp_push_buffer (r
, defn
, ustrchr (defn
, '\n') - defn
, true)
868 if (!_cpp_create_definition (r
, h
))
876 free (data
->defns
[i
]);
878 r
->state
= old_state
;
880 _cpp_restore_pragma_names (r
, data
->saved_pragmas
);
884 if (deps_restore (r
->deps
, f
, CPP_OPTION (r
, restore_pch_deps
) ? name
: NULL
)
888 if (! _cpp_read_file_entries (r
, f
))
891 if (fread (&counter
, sizeof (counter
), 1, f
) != 1)
895 r
->counter
= counter
;
897 /* Read pushed macros. */
898 if (! _cpp_restore_pushed_macros (r
, f
))
903 cpp_errno (r
, CPP_DL_ERROR
, "while reading precompiled header");