#include "config.h"
#include "system.h"
-#include "coretypes.h"
#include "cpplib.h"
#include "cpphash.h"
#include "intl.h"
#include "hashtab.h"
#include "mkdeps.h"
-static int write_macdef PARAMS ((cpp_reader *, cpp_hashnode *, void *));
-static int save_idents PARAMS ((cpp_reader *, cpp_hashnode *, void *));
-static hashval_t hashmem PARAMS ((const void *, size_t));
-static hashval_t cpp_string_hash PARAMS ((const void *));
-static int cpp_string_eq PARAMS ((const void *, const void *));
-static int count_defs PARAMS ((cpp_reader *, cpp_hashnode *, void *));
-static int write_defs PARAMS ((cpp_reader *, cpp_hashnode *, void *));
-static int save_macros PARAMS ((cpp_reader *, cpp_hashnode *, void *));
-static int reset_ht PARAMS ((cpp_reader *, cpp_hashnode *, void *));
+static int write_macdef (cpp_reader *, cpp_hashnode *, void *);
+static int save_idents (cpp_reader *, cpp_hashnode *, void *);
+static hashval_t hashmem (const void *, size_t);
+static hashval_t cpp_string_hash (const void *);
+static int cpp_string_eq (const void *, const void *);
+static int count_defs (cpp_reader *, cpp_hashnode *, void *);
+static int comp_hashnodes (const void *, const void *);
+static int collect_ht_nodes (cpp_reader *, cpp_hashnode *, void *);
+static int write_defs (cpp_reader *, cpp_hashnode *, void *);
+static int save_macros (cpp_reader *, cpp_hashnode *, void *);
/* This structure represents a macro definition on disk. */
struct macrodef_struct
Suitable for being called by cpp_forall_identifiers. */
static int
-write_macdef (pfile, hn, file_p)
- cpp_reader *pfile;
- cpp_hashnode *hn;
- void *file_p;
+write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
{
FILE *f = (FILE *) file_p;
switch (hn->type)
/* The size of the definitions of those identifiers (the size of
'definedstrs'). */
size_t hashsize;
+ /* Number of definitions */
+ size_t n_defs;
+ /* Array of definitions. In cpp_write_pch_deps it is used for sorting. */
+ cpp_hashnode **defs;
/* Space for the next definition. Definitions are null-terminated
strings. */
unsigned char *definedstrs;
put the definition in 'definedstrs'. */
static int
-save_idents (pfile, hn, ss_p)
- cpp_reader *pfile ATTRIBUTE_UNUSED;
- cpp_hashnode *hn;
- void *ss_p;
+save_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
{
struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
/* Hash some memory in a generic way. */
static hashval_t
-hashmem (p_p, sz)
- const void *p_p;
- size_t sz;
+hashmem (const void *p_p, size_t sz)
{
const unsigned char *p = (const unsigned char *)p_p;
size_t i;
/* Hash a cpp string for the hashtable machinery. */
static hashval_t
-cpp_string_hash (a_p)
- const void *a_p;
+cpp_string_hash (const void *a_p)
{
const struct cpp_string *a = (const struct cpp_string *) a_p;
return hashmem (a->text, a->len);
/* Compare two cpp strings for the hashtable machinery. */
static int
-cpp_string_eq (a_p, b_p)
- const void *a_p;
- const void *b_p;
+cpp_string_eq (const void *a_p, const void *b_p)
{
const struct cpp_string *a = (const struct cpp_string *) a_p;
const struct cpp_string *b = (const struct cpp_string *) b_p;
would be called when reading the precompiled header back in. */
int
-cpp_save_state (r, f)
- cpp_reader *r;
- FILE *f;
+cpp_save_state (cpp_reader *r, FILE *f)
{
/* Save the list of non-void identifiers for the dependency checking. */
r->savedstate = xmalloc (sizeof (struct cpp_savedstate));
/* Calculate the 'hashsize' field of the saved state. */
static int
-count_defs (pfile, hn, ss_p)
- cpp_reader *pfile ATTRIBUTE_UNUSED;
- cpp_hashnode *hn;
- void *ss_p;
+count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
{
struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
news.text = NODE_NAME (hn);
slot = htab_find (ss->definedhash, &news);
if (slot == NULL)
- ss->hashsize += NODE_LEN (hn) + 1;
+ {
+ ss->hashsize += NODE_LEN (hn) + 1;
+ ss->n_defs += 1;
+ }
}
return 1;
}
}
-/* Write the identifiers into 'definedstrs' of the state. */
-
+/* Collect the identifiers into the state's string table. */
static int
-write_defs (pfile, hn, ss_p)
- cpp_reader *pfile ATTRIBUTE_UNUSED;
- cpp_hashnode *hn;
- void *ss_p;
+write_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
{
struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
slot = htab_find (ss->definedhash, &news);
if (slot == NULL)
{
- memcpy (ss->definedstrs, NODE_NAME (hn), NODE_LEN (hn));
- ss->definedstrs[NODE_LEN (hn)] = 0;
- ss->definedstrs += NODE_LEN (hn) + 1;
+ ss->defs[ss->n_defs] = hn;
+ ss->n_defs += 1;
}
}
return 1;
}
}
+/* Comparison function for qsort. The arguments point to pointers of
+ type ht_hashnode *. */
+static int
+comp_hashnodes (const void *px, const void *py)
+{
+ cpp_hashnode *x = *(cpp_hashnode **) px;
+ cpp_hashnode *y = *(cpp_hashnode **) py;
+ return ustrcmp (NODE_NAME (x), NODE_NAME (y));
+}
+
/* Write out the remainder of the dependency information. This should be
called after the PCH is ready to be saved. */
int
-cpp_write_pch_deps (r, f)
- cpp_reader *r;
- FILE *f;
+cpp_write_pch_deps (cpp_reader *r, FILE *f)
{
struct macrodef_struct z;
struct cpp_savedstate *const ss = r->savedstate;
unsigned char *definedstrs;
+ size_t i;
- ss->hashsize = 0;
-
- /* Write out the list of identifiers which have been seen and
+ /* Collect the list of identifiers which have been seen and
weren't defined to anything previously. */
+ ss->hashsize = 0;
+ ss->n_defs = 0;
cpp_forall_identifiers (r, count_defs, ss);
- definedstrs = ss->definedstrs = xmalloc (ss->hashsize);
+
+ ss->defs = xmalloc (ss->n_defs * sizeof (cpp_hashnode *));
+ ss->n_defs = 0;
cpp_forall_identifiers (r, write_defs, ss);
+
+ /* Sort the list, copy it into a buffer, and write it out. */
+ qsort (ss->defs, ss->n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
+ definedstrs = ss->definedstrs = xmalloc (ss->hashsize);
+ for (i = 0; i < ss->n_defs; ++i)
+ {
+ size_t len = NODE_LEN (ss->defs[i]);
+ memcpy (definedstrs, NODE_NAME (ss->defs[i]), len + 1);
+ definedstrs += len + 1;
+ }
+
memset (&z, 0, sizeof (z));
z.definition_length = ss->hashsize;
if (fwrite (&z, sizeof (z), 1, f) != 1
- || fwrite (definedstrs, ss->hashsize, 1, f) != 1)
+ || fwrite (ss->definedstrs, ss->hashsize, 1, f) != 1)
{
cpp_errno (r, DL_ERROR, "while writing precompiled header");
return -1;
}
- free (definedstrs);
+ free (ss->definedstrs);
/* Free the saved state. */
free (ss);
cpp_read_state. */
int
-cpp_write_pch_state (r, f)
- cpp_reader *r;
- FILE *f;
+cpp_write_pch_state (cpp_reader *r, FILE *f)
{
struct macrodef_struct z;
return 0;
}
+
+/* Data structure to transform hash table nodes into a sorted list */
+
+struct ht_node_list
+{
+ /* Array of nodes */
+ cpp_hashnode **defs;
+ /* Number of nodes in the array */
+ size_t n_defs;
+ /* Size of the allocated array */
+ size_t asize;
+};
+
+/* Callback for collecting identifiers from hash table */
+
+static int
+collect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
+ void *nl_p)
+{
+ struct ht_node_list *const nl = (struct ht_node_list *)nl_p;
+
+ if (hn->type != NT_VOID || hn->flags & NODE_POISONED)
+ {
+ if (nl->n_defs == nl->asize)
+ {
+ nl->asize *= 2;
+ nl->defs = xrealloc (nl->defs, nl->asize * sizeof (cpp_hashnode *));
+ }
+
+ nl->defs[nl->n_defs] = hn;
+ ++nl->n_defs;
+ }
+ return 1;
+}
+
+
/* Return nonzero if FD is a precompiled header which is consistent
with the preprocessor's current definitions. It will be consistent
when:
*/
int
-cpp_valid_state (r, name, fd)
- cpp_reader *r;
- const char *name;
- int fd;
+cpp_valid_state (cpp_reader *r, const char *name, int fd)
{
struct macrodef_struct m;
size_t namebufsz = 256;
unsigned char *namebuf = xmalloc (namebufsz);
unsigned char *undeftab = NULL;
+ struct ht_node_list nl = { 0, 0, 0 };
+ unsigned char *first, *last;
unsigned int i;
/* Read in the list of identifiers that must be defined
|| h->flags & NODE_POISONED)
{
if (CPP_OPTION (r, warn_invalid_pch))
- cpp_error (r, DL_WARNING,
+ cpp_error (r, DL_WARNING_SYSHDR,
"%s: not used because `%.*s' not defined",
name, m.name_length, namebuf);
goto fail;
|| memcmp (namebuf, newdefn, m.definition_length) != 0)
{
if (CPP_OPTION (r, warn_invalid_pch))
- cpp_error (r, DL_WARNING,
+ cpp_error (r, DL_WARNING_SYSHDR,
"%s: not used because `%.*s' defined as `%s' not `%.*s'",
name, m.name_length, namebuf, newdefn + m.name_length,
m.definition_length - m.name_length,
undeftab = xmalloc (m.definition_length);
if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
goto error;
- for (i = 0; i < m.definition_length; )
+
+ /* Collect identifiers from the current hash table. */
+ nl.n_defs = 0;
+ nl.asize = 10;
+ nl.defs = xmalloc (nl.asize * sizeof (cpp_hashnode *));
+ cpp_forall_identifiers (r, &collect_ht_nodes, &nl);
+ qsort (nl.defs, nl.n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
+
+ /* Loop through nl.defs and undeftab, both of which are sorted lists.
+ There should be no matches. */
+ first = undeftab;
+ last = undeftab + m.definition_length;
+ i = 0;
+
+ while (first < last && i < nl.n_defs)
{
- int l = ustrlen (undeftab + i);
- cpp_hashnode *h;
- h = cpp_lookup (r, undeftab + i, l);
- if (h->type != NT_VOID
- || h->flags & NODE_POISONED)
- {
- if (CPP_OPTION (r, warn_invalid_pch))
- cpp_error (r, DL_WARNING, "%s: not used because `%s' is defined",
- name, undeftab + i);
- goto fail;
- }
- i += l + 1;
+ int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
+
+ if (cmp < 0)
+ first += ustrlen (first) + 1;
+ else if (cmp > 0)
+ ++i;
+ else
+ goto fail;
}
+
+ free(nl.defs);
free (undeftab);
/* We win! */
free (namebuf);
if (undeftab != NULL)
free (undeftab);
+ if (nl.defs != NULL)
+ free (nl.defs);
return 1;
}
a PCH restore. */
static int
-save_macros (r, h, data_p)
- cpp_reader *r ATTRIBUTE_UNUSED;
- cpp_hashnode *h;
- void *data_p;
+save_macros (cpp_reader *r ATTRIBUTE_UNUSED, cpp_hashnode *h, void *data_p)
{
struct save_macro_data *data = (struct save_macro_data *)data_p;
if (h->type != NT_VOID
macros in 'data'. */
void
-cpp_prepare_state (r, data)
- cpp_reader *r;
- struct save_macro_data **data;
+cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
{
struct save_macro_data *d = xmalloc (sizeof (struct save_macro_data));
*data = d;
}
-/* Erase all the existing macros and assertions. */
-
-static int
-reset_ht (r, h, unused)
- cpp_reader *r ATTRIBUTE_UNUSED;
- cpp_hashnode *h;
- void *unused ATTRIBUTE_UNUSED;
-{
- if (h->type != NT_VOID
- && (h->flags & NODE_BUILTIN) == 0)
- {
- h->type = NT_VOID;
- memset (&h->value, 0, sizeof (h->value));
- }
- return 1;
-}
-
/* Given a precompiled header that was previously determined to be valid,
apply all its definitions (and undefinitions) to the current state.
DEPNAME is passed to deps_restore. */
int
-cpp_read_state (r, name, f, data)
- cpp_reader *r;
- const char *name;
- FILE *f;
- struct save_macro_data *data;
+cpp_read_state (cpp_reader *r, const char *name, FILE *f,
+ struct save_macro_data *data)
{
struct macrodef_struct m;
size_t defnlen = 256;
size_t i, mac_count;
int saved_line = r->line;
- /* Erase all the existing hashtable entries for macros. At this
- point, they're all from the PCH file, and their pointers won't be
- valid. */
- cpp_forall_identifiers (r, reset_ht, NULL);
-
/* Restore spec_nodes, which will be full of references to the old
hashtable entries and so will now be invalid. */
{
if (fread (defn, 1, m.definition_length, f) != m.definition_length)
goto error;
- defn[m.definition_length] = '\0';
+ defn[m.definition_length] = '\n';
h = cpp_lookup (r, defn, m.name_length);
m.definition_length - m.name_length,
true, 1) != NULL)
{
+ _cpp_clean_line (r);
if (!_cpp_create_definition (r, h))
abort ();
_cpp_pop_buffer (r);