1 /* Part of CPP library. (Precompiled header reading/writing.)
2 Copyright (C) 2000-2013 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
))
60 if ((hn
->flags
& NODE_BUILTIN
)
61 && (!pfile
->cb
.user_builtin_macro
62 || !pfile
->cb
.user_builtin_macro (pfile
, hn
)))
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
, CPP_DL_ERROR
,
87 "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 (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*hn
, void *ss_p
)
128 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
130 if (hn
->type
!= NT_VOID
)
132 struct cpp_string news
;
135 news
.len
= NODE_LEN (hn
);
136 news
.text
= NODE_NAME (hn
);
137 slot
= htab_find_slot (ss
->definedhash
, &news
, INSERT
);
140 struct cpp_string
*sp
;
143 sp
= XNEW (struct cpp_string
);
146 sp
->len
= NODE_LEN (hn
);
147 sp
->text
= text
= XNEWVEC (unsigned char, NODE_LEN (hn
));
148 memcpy (text
, NODE_NAME (hn
), NODE_LEN (hn
));
155 /* Hash some memory in a generic way. */
158 hashmem (const void *p_p
, size_t sz
)
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 (const void *a_p
)
175 const struct cpp_string
*a
= (const struct cpp_string
*) a_p
;
176 return hashmem (a
->text
, a
->len
);
179 /* Compare two cpp strings for the hashtable machinery. */
182 cpp_string_eq (const void *a_p
, const void *b_p
)
184 const struct cpp_string
*a
= (const struct cpp_string
*) a_p
;
185 const struct cpp_string
*b
= (const struct cpp_string
*) b_p
;
186 return (a
->len
== b
->len
187 && memcmp (a
->text
, b
->text
, a
->len
) == 0);
190 /* Save the current definitions of the cpp_reader for dependency
191 checking purposes. When writing a precompiled header, this should
192 be called at the same point in the compilation as cpp_valid_state
193 would be called when reading the precompiled header back in. */
196 cpp_save_state (cpp_reader
*r
, FILE *f
)
198 /* Save the list of non-void identifiers for the dependency checking. */
199 r
->savedstate
= XNEW (struct cpp_savedstate
);
200 r
->savedstate
->definedhash
= htab_create (100, cpp_string_hash
,
201 cpp_string_eq
, NULL
);
202 cpp_forall_identifiers (r
, save_idents
, r
->savedstate
);
204 /* Write out the list of defined identifiers. */
205 cpp_forall_identifiers (r
, write_macdef
, f
);
210 /* Calculate the 'hashsize' field of the saved state. */
213 count_defs (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*hn
, void *ss_p
)
215 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
220 if (hn
->flags
& NODE_BUILTIN
)
223 /* else fall through. */
227 struct cpp_string news
;
230 news
.len
= NODE_LEN (hn
);
231 news
.text
= NODE_NAME (hn
);
232 slot
= (void **) htab_find (ss
->definedhash
, &news
);
235 ss
->hashsize
+= NODE_LEN (hn
) + 1;
242 /* Not currently implemented. */
250 /* Collect the identifiers into the state's string table. */
252 write_defs (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*hn
, void *ss_p
)
254 struct cpp_savedstate
*const ss
= (struct cpp_savedstate
*)ss_p
;
259 if (hn
->flags
& NODE_BUILTIN
)
262 /* else fall through. */
266 struct cpp_string news
;
269 news
.len
= NODE_LEN (hn
);
270 news
.text
= NODE_NAME (hn
);
271 slot
= (void **) htab_find (ss
->definedhash
, &news
);
274 ss
->defs
[ss
->n_defs
] = hn
;
281 /* Not currently implemented. */
289 /* Comparison function for qsort. The arguments point to pointers of
290 type ht_hashnode *. */
292 comp_hashnodes (const void *px
, const void *py
)
294 cpp_hashnode
*x
= *(cpp_hashnode
**) px
;
295 cpp_hashnode
*y
= *(cpp_hashnode
**) py
;
296 return ustrcmp (NODE_NAME (x
), NODE_NAME (y
));
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 (cpp_reader
*r
, FILE *f
)
305 struct macrodef_struct z
;
306 struct cpp_savedstate
*const ss
= r
->savedstate
;
307 unsigned char *definedstrs
;
310 /* Collect the list of identifiers which have been seen and
311 weren't defined to anything previously. */
314 cpp_forall_identifiers (r
, count_defs
, ss
);
316 ss
->defs
= XNEWVEC (cpp_hashnode
*, ss
->n_defs
);
318 cpp_forall_identifiers (r
, write_defs
, ss
);
320 /* Sort the list, copy it into a buffer, and write it out. */
321 qsort (ss
->defs
, ss
->n_defs
, sizeof (cpp_hashnode
*), &comp_hashnodes
);
322 definedstrs
= ss
->definedstrs
= XNEWVEC (unsigned char, ss
->hashsize
);
323 for (i
= 0; i
< ss
->n_defs
; ++i
)
325 size_t len
= NODE_LEN (ss
->defs
[i
]);
326 memcpy (definedstrs
, NODE_NAME (ss
->defs
[i
]), len
+ 1);
327 definedstrs
+= len
+ 1;
330 memset (&z
, 0, sizeof (z
));
331 z
.definition_length
= ss
->hashsize
;
332 if (fwrite (&z
, sizeof (z
), 1, f
) != 1
333 || fwrite (ss
->definedstrs
, ss
->hashsize
, 1, f
) != 1)
335 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
338 free (ss
->definedstrs
);
340 /* Free the saved state. */
342 r
->savedstate
= NULL
;
344 /* Save the next value of __COUNTER__. */
345 if (fwrite (&r
->counter
, sizeof (r
->counter
), 1, f
) != 1)
347 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
354 /* Write out the definitions of the preprocessor, in a form suitable for
358 cpp_write_pch_state (cpp_reader
*r
, FILE *f
)
361 r
->deps
= deps_init ();
363 if (deps_save (r
->deps
, f
) != 0)
365 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
369 if (! _cpp_save_file_entries (r
, f
))
371 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
375 /* Save the next __COUNTER__ value. When we include a precompiled header,
376 we need to start at the offset we would have if the header had been
377 included normally. */
378 if (fwrite (&r
->counter
, sizeof (r
->counter
), 1, f
) != 1)
380 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
384 /* Write saved macros. */
385 if (! _cpp_save_pushed_macros (r
, f
))
387 cpp_errno (r
, CPP_DL_ERROR
, "while writing precompiled header");
395 _cpp_restore_pushed_macros (cpp_reader
*r
, FILE *f
)
397 size_t count_saved
= 0;
399 struct def_pragma_macro
*p
;
404 if (fread (&count_saved
, sizeof (count_saved
), 1, f
) != 1)
408 for (i
= 0; i
< count_saved
; i
++)
410 if (fread (&nlen
, sizeof (nlen
), 1, f
) != 1)
412 p
= XNEW (struct def_pragma_macro
);
413 memset (p
, 0, sizeof (struct def_pragma_macro
));
414 p
->name
= XNEWVAR (char, nlen
+ 1);
416 if (fread (p
->name
, nlen
, 1, f
) != 1)
418 if (fread (&defnlen
, sizeof (defnlen
), 1, f
) != 1)
424 defn
= XNEWVEC (uchar
, defnlen
+ 1);
427 if (fread (defn
, defnlen
, 1, f
) != 1)
430 p
->definition
= defn
;
431 if (fread (&(p
->line
), sizeof (source_location
), 1, f
) != 1)
434 if (fread (&defnlen
, sizeof (defnlen
), 1, f
) != 1)
436 p
->syshdr
= ((defnlen
& 1) != 0 ? 1 : 0);
437 p
->used
= ((defnlen
& 2) != 0 ? 1 : 0);
440 p
->next
= r
->pushed_macros
;
441 r
->pushed_macros
= p
;
447 _cpp_save_pushed_macros (cpp_reader
*r
, FILE *f
)
449 size_t count_saved
= 0;
451 struct def_pragma_macro
*p
,**pp
;
455 p
= r
->pushed_macros
;
461 if (fwrite (&count_saved
, sizeof (count_saved
), 1, f
) != 1)
466 pp
= (struct def_pragma_macro
**) alloca (sizeof (struct def_pragma_macro
*)
468 /* Store them in reverse order. */
469 p
= r
->pushed_macros
;
477 for (i
= 0; i
< count_saved
; i
++)
479 defnlen
= strlen (pp
[i
]->name
);
480 if (fwrite (&defnlen
, sizeof (size_t), 1, f
) != 1
481 || fwrite (pp
[i
]->name
, defnlen
, 1, f
) != 1)
486 if (fwrite (&defnlen
, sizeof (size_t), 1, f
) != 1)
491 defnlen
= ustrlen (pp
[i
]->definition
);
492 if (fwrite (&defnlen
, sizeof (size_t), 1, f
) != 1
493 || fwrite (pp
[i
]->definition
, defnlen
, 1, f
) != 1)
495 if (fwrite (&(pp
[i
]->line
), sizeof (source_location
), 1, f
) != 1)
498 defnlen
|= (pp
[i
]->syshdr
!= 0 ? 1 : 0);
499 defnlen
|= (pp
[i
]->used
!= 0 ? 2 : 0);
500 if (fwrite (&defnlen
, sizeof (defnlen
), 1, f
) != 1)
508 /* Data structure to transform hash table nodes into a sorted list */
514 /* Number of nodes in the array */
516 /* Size of the allocated array */
520 /* Callback for collecting identifiers from hash table */
523 collect_ht_nodes (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*hn
,
526 struct ht_node_list
*const nl
= (struct ht_node_list
*)nl_p
;
528 if (hn
->type
!= NT_VOID
|| hn
->flags
& NODE_POISONED
)
530 if (nl
->n_defs
== nl
->asize
)
533 nl
->defs
= XRESIZEVEC (cpp_hashnode
*, nl
->defs
, nl
->asize
);
536 nl
->defs
[nl
->n_defs
] = hn
;
543 /* Return nonzero if FD is a precompiled header which is consistent
544 with the preprocessor's current definitions. It will be consistent
547 - anything that was defined just before the PCH was generated
548 is defined the same way now; and
549 - anything that was not defined then, but is defined now, was not
552 NAME is used to print warnings if `warn_invalid_pch' is set in the
557 cpp_valid_state (cpp_reader
*r
, const char *name
, int fd
)
559 struct macrodef_struct m
;
560 size_t namebufsz
= 256;
561 unsigned char *namebuf
= XNEWVEC (unsigned char, namebufsz
);
562 unsigned char *undeftab
= NULL
;
563 struct ht_node_list nl
= { 0, 0, 0 };
564 unsigned char *first
, *last
;
566 unsigned int counter
;
568 /* Read in the list of identifiers that must be defined
569 Check that they are defined in the same way. */
573 const unsigned char *newdefn
;
575 if (read (fd
, &m
, sizeof (m
)) != sizeof (m
))
578 if (m
.name_length
== 0)
581 /* If this file is already preprocessed, there won't be any
582 macros defined, and that's OK. */
583 if (CPP_OPTION (r
, preprocessed
))
585 if (lseek (fd
, m
.definition_length
, SEEK_CUR
) == -1)
590 if (m
.definition_length
> namebufsz
)
593 namebufsz
= m
.definition_length
+ 256;
594 namebuf
= XNEWVEC (unsigned char, namebufsz
);
597 if ((size_t)read (fd
, namebuf
, m
.definition_length
)
598 != m
.definition_length
)
601 h
= cpp_lookup (r
, namebuf
, m
.name_length
);
602 if (m
.flags
& NODE_POISONED
603 || h
->flags
& NODE_POISONED
)
605 if (CPP_OPTION (r
, warn_invalid_pch
))
606 cpp_warning_syshdr (r
, CPP_W_INVALID_PCH
,
607 "%s: not used because `%.*s' is poisoned",
608 name
, m
.name_length
, namebuf
);
612 if (h
->type
!= NT_MACRO
)
614 /* It's ok if __GCC_HAVE_DWARF2_CFI_ASM becomes undefined,
615 as in, when the PCH file is created with -g and we're
616 attempting to use it without -g. Restoring the PCH file
617 is supposed to bring in this definition *and* enable the
618 generation of call frame information, so that precompiled
619 definitions that take this macro into accout, to decide
620 what asm to emit, won't issue .cfi directives when the
622 if (!(h
->flags
& NODE_USED
)
623 && m
.name_length
== sizeof ("__GCC_HAVE_DWARF2_CFI_ASM") - 1
624 && !memcmp (namebuf
, "__GCC_HAVE_DWARF2_CFI_ASM", m
.name_length
))
627 if (CPP_OPTION (r
, warn_invalid_pch
))
628 cpp_warning_syshdr (r
, CPP_W_INVALID_PCH
,
629 "%s: not used because `%.*s' not defined",
630 name
, m
.name_length
, namebuf
);
634 newdefn
= cpp_macro_definition (r
, h
);
636 if (m
.definition_length
!= ustrlen (newdefn
)
637 || memcmp (namebuf
, newdefn
, m
.definition_length
) != 0)
639 if (CPP_OPTION (r
, warn_invalid_pch
))
640 cpp_warning_syshdr (r
, CPP_W_INVALID_PCH
,
641 "%s: not used because `%.*s' defined as `%s' not `%.*s'",
642 name
, m
.name_length
, namebuf
, newdefn
+ m
.name_length
,
643 m
.definition_length
- m
.name_length
,
644 namebuf
+ m
.name_length
);
651 /* Read in the list of identifiers that must not be defined.
652 Check that they really aren't. */
653 undeftab
= XNEWVEC (unsigned char, m
.definition_length
);
654 if ((size_t) read (fd
, undeftab
, m
.definition_length
) != m
.definition_length
)
657 /* Collect identifiers from the current hash table. */
660 nl
.defs
= XNEWVEC (cpp_hashnode
*, nl
.asize
);
661 cpp_forall_identifiers (r
, &collect_ht_nodes
, &nl
);
662 qsort (nl
.defs
, nl
.n_defs
, sizeof (cpp_hashnode
*), &comp_hashnodes
);
664 /* Loop through nl.defs and undeftab, both of which are sorted lists.
665 There should be no matches. */
667 last
= undeftab
+ m
.definition_length
;
670 while (first
< last
&& i
< nl
.n_defs
)
672 int cmp
= ustrcmp (first
, NODE_NAME (nl
.defs
[i
]));
675 first
+= ustrlen (first
) + 1;
680 if (CPP_OPTION (r
, warn_invalid_pch
))
681 cpp_warning_syshdr (r
, CPP_W_INVALID_PCH
,
682 "%s: not used because `%s' is defined",
693 /* Read in the next value of __COUNTER__.
694 Check that (a) __COUNTER__ was not used in the pch or (b) __COUNTER__
695 has not been used in this translation unit. */
696 if (read (fd
, &counter
, sizeof (counter
)) != sizeof (counter
))
698 if (counter
&& r
->counter
)
700 if (CPP_OPTION (r
, warn_invalid_pch
))
701 cpp_warning_syshdr (r
, CPP_W_INVALID_PCH
,
702 "%s: not used because `__COUNTER__' is invalid",
711 cpp_errno (r
, CPP_DL_ERROR
, "while reading precompiled header");
720 /* Save all the existing macros. */
722 struct save_macro_data
727 char **saved_pragmas
;
730 /* Save the definition of a single macro, so that it will persist
731 across a PCH restore. Because macro data is in GCed memory, which
732 will be blown away by PCH, it must be temporarily copied to
733 malloced memory. (The macros will refer to identifier nodes which
734 are also GCed and so on, so the copying is done by turning them
735 into self-contained strings.) The assumption is that most macro
736 definitions will come from the PCH file, not from the compilation
737 before the PCH file is loaded, so it doesn't matter that this is
740 It would reduce the cost even further if macros defined in the PCH
741 file were not saved in this way, but this is not done (yet), except
742 for builtins, and for #assert by default. */
745 save_macros (cpp_reader
*r
, cpp_hashnode
*h
, void *data_p
)
747 struct save_macro_data
*data
= (struct save_macro_data
*)data_p
;
749 if ((h
->flags
& NODE_BUILTIN
)
750 && h
->type
== NT_MACRO
751 && r
->cb
.user_builtin_macro
)
752 r
->cb
.user_builtin_macro (r
, h
);
754 if (h
->type
!= NT_VOID
755 && (h
->flags
& NODE_BUILTIN
) == 0)
757 if (data
->count
== data
->array_size
)
759 data
->array_size
*= 2;
760 data
->defns
= XRESIZEVEC (uchar
*, data
->defns
, (data
->array_size
));
766 /* Not currently implemented. */
771 const uchar
* defn
= cpp_macro_definition (r
, h
);
772 size_t defnlen
= ustrlen (defn
);
774 data
->defns
[data
->count
] = (uchar
*) xmemdup (defn
, defnlen
,
776 data
->defns
[data
->count
][defnlen
] = '\n';
788 /* Prepare to restore the state, by saving the currently-defined
792 cpp_prepare_state (cpp_reader
*r
, struct save_macro_data
**data
)
794 struct save_macro_data
*d
= XNEW (struct save_macro_data
);
797 d
->defns
= XNEWVEC (uchar
*, d
->array_size
);
799 cpp_forall_identifiers (r
, save_macros
, d
);
800 d
->saved_pragmas
= _cpp_save_pragma_names (r
);
804 /* Given a precompiled header that was previously determined to be valid,
805 apply all its definitions (and undefinitions) to the current state.
806 DEPNAME is passed to deps_restore. */
809 cpp_read_state (cpp_reader
*r
, const char *name
, FILE *f
,
810 struct save_macro_data
*data
)
813 struct lexer_state old_state
;
814 unsigned int counter
;
816 /* Restore spec_nodes, which will be full of references to the old
817 hashtable entries and so will now be invalid. */
819 struct spec_nodes
*s
= &r
->spec_nodes
;
820 s
->n_defined
= cpp_lookup (r
, DSC("defined"));
821 s
->n_true
= cpp_lookup (r
, DSC("true"));
822 s
->n_false
= cpp_lookup (r
, DSC("false"));
823 s
->n__VA_ARGS__
= cpp_lookup (r
, DSC("__VA_ARGS__"));
826 old_state
= r
->state
;
827 r
->state
.in_directive
= 1;
828 r
->state
.prevent_expansion
= 1;
829 r
->state
.angled_headers
= 0;
831 /* Run through the carefully-saved macros, insert them. */
832 for (i
= 0; i
< data
->count
; i
++)
838 namelen
= ustrcspn (data
->defns
[i
], "( \n");
839 h
= cpp_lookup (r
, data
->defns
[i
], namelen
);
840 defn
= data
->defns
[i
] + namelen
;
842 /* The PCH file is valid, so we know that if there is a definition
843 from the PCH file it must be the same as the one we had
844 originally, and so do not need to restore it. */
845 if (h
->type
== NT_VOID
)
847 if (cpp_push_buffer (r
, defn
, ustrchr (defn
, '\n') - defn
, true)
851 if (!_cpp_create_definition (r
, h
))
859 free (data
->defns
[i
]);
861 r
->state
= old_state
;
863 _cpp_restore_pragma_names (r
, data
->saved_pragmas
);
867 if (deps_restore (r
->deps
, f
, CPP_OPTION (r
, restore_pch_deps
) ? name
: NULL
)
871 if (! _cpp_read_file_entries (r
, f
))
874 if (fread (&counter
, sizeof (counter
), 1, f
) != 1)
878 r
->counter
= counter
;
880 /* Read pushed macros. */
881 if (! _cpp_restore_pushed_macros (r
, f
))
886 cpp_errno (r
, CPP_DL_ERROR
, "while reading precompiled header");