summaryrefslogtreecommitdiff
path: root/libcpp/pch.c
diff options
context:
space:
mode:
authorgeoffk <geoffk@138bc75d-0d04-0410-961f-82ee72b054a4>2004-06-09 20:10:13 +0000
committergeoffk <geoffk@138bc75d-0d04-0410-961f-82ee72b054a4>2004-06-09 20:10:13 +0000
commitc39ed964c8cd9aec2fe6060830804416b0856b60 (patch)
tree1238be7fc80d74d8323d5f6c4344a6bb2be0f854 /libcpp/pch.c
parentf5efe504032463d907b2eb0f4f76a5d68ce45429 (diff)
downloadgcc-c39ed964c8cd9aec2fe6060830804416b0856b60.tar.gz
Index: gcc/ChangeLog
2004-06-09 Geoffrey Keating <geoffk@apple.com> * Makefile.in (CPPLIB_H): Put files in order of inclusion. (CPP_ID_DATA_H): New. (gtype-desc.o): Update dependencies. (GTFILES): Use CPP_ID_DATA_H. Index: gcc/testsuite/ChangeLog 2004-06-09 Geoffrey Keating <geoffk@apple.com> * gcc.dg/pch/macro-4.c: New. * gcc.dg/pch/macro-4.hs: New. Index: libcpp/ChangeLog 2004-06-09 Geoffrey Keating <geoffk@apple.com> * traditional.c (push_replacement_text): Set macro->traditional. (save_replacement_text): Likewise. * pch.c (cpp_write_pch_state): Don't write list of defined macros. (struct save_macro_item): Delete. (struct save_macro_data): Use a character array not the previous structured format. (save_macros): Save macro as text not as internal structures. (cpp_prepare_state): Update for changes to save_macro_data. (cpp_read_state): Don't read macros defined in PCH. Restore -D macros as text. * macro.c (create_iso_definition): Honour alloc_subobject. Clear traditional flag. (_cpp_create_definition): Honour alloc_subobject. * lex.c (cpp_token_val_index): New. * internal.h: Include cpp-id-data.h. (uchar): Move definition to cpp-id-data.h. (U): Likewise. (cpp_macro): Likewise. * directives.c (struct answer): Move to cpp-id-data.h. (do_assert): Honour alloc_subobject. Index: libcpp/include/ChangeLog 2004-06-09 Geoffrey Keating <geoffk@apple.com> * symtab.h (struct ht): Add field 'alloc_subobject'. * cpplib.h (struct cpp_string): Add GTY marker. (enum cpp_token_fld_kind): New. (struct cpp_token): Add GTY markers. (cpp_token_val_index): Prototype. (CPP_HASHNODE_VALUE_IDX): New. (struct cpp_hashnode): Don't skip fields of 'value' when marking. * cpp-id-data.h: New file. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@82851 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libcpp/pch.c')
-rw-r--r--libcpp/pch.c157
1 files changed, 63 insertions, 94 deletions
diff --git a/libcpp/pch.c b/libcpp/pch.c
index 51175a9a67b..a9d139a30b7 100644
--- a/libcpp/pch.c
+++ b/libcpp/pch.c
@@ -347,15 +347,6 @@ cpp_write_pch_state (cpp_reader *r, FILE *f)
{
struct macrodef_struct z;
- /* Write out the list of defined identifiers. */
- cpp_forall_identifiers (r, write_macdef, f);
- memset (&z, 0, sizeof (z));
- if (fwrite (&z, sizeof (z), 1, f) != 1)
- {
- cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
- return -1;
- }
-
if (!r->deps)
r->deps = deps_init ();
@@ -544,46 +535,64 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
return 1;
}
-/* Save all the existing macros and assertions.
- This code assumes that there might be hundreds, but not thousands of
- existing definitions. */
-
-struct save_macro_item {
- struct save_macro_item *next;
- struct cpp_hashnode macs[64];
-};
+/* Save all the existing macros. */
struct save_macro_data
{
- struct save_macro_item *macros;
+ uchar **defns;
size_t count;
+ size_t array_size;
char **saved_pragmas;
};
-/* Save the definition of a single macro, so that it will persist across
- a PCH restore. */
+/* Save the definition of a single macro, so that it will persist
+ across a PCH restore. Because macro data is in GCed memory, which
+ will be blown away by PCH, it must be temporarily copied to
+ malloced memory. (The macros will refer to identifier nodes which
+ are also GCed and so on, so the copying is done by turning them
+ into self-contained strings.) The assumption is that most macro
+ definitions will come from the PCH file, not from the compilation
+ before the PCH file is loaded, so it doesn't matter that this is
+ a little expensive.
+
+ It would reduce the cost even further if macros defined in the PCH
+ file were not saved in this way, but this is not done (yet), except
+ for builtins, and for #assert by default. */
static int
-save_macros (cpp_reader *r ATTRIBUTE_UNUSED, cpp_hashnode *h, void *data_p)
+save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
{
struct save_macro_data *data = (struct save_macro_data *)data_p;
if (h->type != NT_VOID
&& (h->flags & NODE_BUILTIN) == 0)
{
- cpp_hashnode *save;
- if (data->count == ARRAY_SIZE (data->macros->macs))
+ if (data->count == data->array_size)
+ {
+ data->array_size *= 2;
+ data->defns = xrealloc (data->defns, (data->array_size
+ * sizeof (uchar *)));
+ }
+
+ switch (h->type)
{
- struct save_macro_item *d = data->macros;
- data->macros = xmalloc (sizeof (struct save_macro_item));
- data->macros->next = d;
- data->count = 0;
+ case NT_ASSERTION:
+ /* Not currently implemented. */
+ return 1;
+
+ case NT_MACRO:
+ {
+ const uchar * defn = cpp_macro_definition (r, h);
+ size_t defnlen = ustrlen (defn);
+
+ data->defns[data->count] = xmemdup (defn, defnlen, defnlen + 2);
+ data->defns[data->count][defnlen] = '\n';
+ }
+ break;
+
+ default:
+ abort ();
}
- save = data->macros->macs + data->count;
data->count++;
- memcpy (save, h, sizeof (struct cpp_hashnode));
- HT_STR (&save->ident) = xmemdup (HT_STR (HT_NODE (save)),
- HT_LEN (HT_NODE (save)),
- HT_LEN (HT_NODE (save)) + 1);
}
return 1;
}
@@ -596,8 +605,9 @@ cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
{
struct save_macro_data *d = xmalloc (sizeof (struct save_macro_data));
- d->macros = NULL;
- d->count = ARRAY_SIZE (d->macros->macs);
+ d->array_size = 512;
+ d->defns = xmalloc (d->array_size * sizeof (d->defns[0]));
+ d->count = 0;
cpp_forall_identifiers (r, save_macros, d);
d->saved_pragmas = _cpp_save_pragma_names (r);
*data = d;
@@ -612,11 +622,9 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f,
struct save_macro_data *data)
{
struct macrodef_struct m;
- size_t defnlen = 256;
- unsigned char *defn = xmalloc (defnlen);
- struct lexer_state old_state;
struct save_macro_item *d;
size_t i, mac_count;
+ struct lexer_state old_state;
/* Restore spec_nodes, which will be full of references to the old
hashtable entries and so will now be invalid. */
@@ -628,70 +636,28 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f,
s->n__VA_ARGS__ = cpp_lookup (r, DSC("__VA_ARGS__"));
}
- /* Run through the carefully-saved macros, insert them. */
- d = data->macros;
- mac_count = data->count;
- while (d)
- {
- struct save_macro_item *nextd;
- for (i = 0; i < mac_count; i++)
- {
- cpp_hashnode *h;
-
- h = cpp_lookup (r, HT_STR (HT_NODE (&d->macs[i])),
- HT_LEN (HT_NODE (&d->macs[i])));
- h->type = d->macs[i].type;
- h->flags = d->macs[i].flags;
- h->value = d->macs[i].value;
- free ((void *)HT_STR (HT_NODE (&d->macs[i])));
- }
- nextd = d->next;
- free (d);
- d = nextd;
- mac_count = ARRAY_SIZE (d->macs);
- }
-
- _cpp_restore_pragma_names (r, data->saved_pragmas);
-
- free (data);
-
old_state = r->state;
-
r->state.in_directive = 1;
r->state.prevent_expansion = 1;
r->state.angled_headers = 0;
- /* Read in the identifiers that must be defined. */
- for (;;)
+ /* Run through the carefully-saved macros, insert them. */
+ for (i = 0; i < data->count; i++)
{
cpp_hashnode *h;
-
- if (fread (&m, sizeof (m), 1, f) != 1)
- goto error;
-
- if (m.name_length == 0)
- break;
-
- if (defnlen < m.definition_length + 1)
- {
- defnlen = m.definition_length + 256;
- defn = xrealloc (defn, defnlen);
- }
+ size_t namelen;
+ uchar *defn;
- if (fread (defn, 1, m.definition_length, f) != m.definition_length)
- goto error;
- defn[m.definition_length] = '\n';
-
- h = cpp_lookup (r, defn, m.name_length);
+ namelen = strcspn (data->defns[i], "( \n");
+ h = cpp_lookup (r, data->defns[i], namelen);
+ defn = data->defns[i] + namelen;
- if (h->type == NT_MACRO)
- _cpp_free_definition (h);
- if (m.flags & NODE_POISONED)
- h->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
- else if (m.name_length != m.definition_length)
+ /* The PCH file is valid, so we know that if there is a definition
+ from the PCH file it must be the same as the one we had
+ originally, and so do not need to restore it. */
+ if (h->type == NT_VOID)
{
- if (cpp_push_buffer (r, defn + m.name_length,
- m.definition_length - m.name_length, true)
+ if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
!= NULL)
{
_cpp_clean_line (r);
@@ -702,11 +668,14 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f,
else
abort ();
}
- }
+ free (data->defns[i]);
+ }
r->state = old_state;
- free (defn);
- defn = NULL;
+
+ _cpp_restore_pragma_names (r, data->saved_pragmas);
+
+ free (data);
if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
!= 0)