pch.c 17.8 KB
Newer Older
1
/* Part of CPP library.  (Precompiled header reading/writing.)
2 3
   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
   Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
17
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18 19 20 21

#include "config.h"
#include "system.h"
#include "cpplib.h"
22
#include "internal.h"
23 24 25
#include "hashtab.h"
#include "mkdeps.h"

26 27 28 29 30 31 32 33 34 35
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 *);
36 37 38 39 40 41 42 43 44 45 46 47 48

/* This structure represents a macro definition on disk.  */
struct macrodef_struct 
{
  unsigned int definition_length;
  unsigned short name_length;
  unsigned short flags;
};

/* This is how we write out a macro definition.  
   Suitable for being called by cpp_forall_identifiers.  */

static int
49
write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
{
  FILE *f = (FILE *) file_p;
  switch (hn->type)
    {
    case NT_VOID:
      if (! (hn->flags & NODE_POISONED))
	return 1;
      
    case NT_MACRO:
      if ((hn->flags & NODE_BUILTIN))
	return 1;

      {
	struct macrodef_struct s;
	const unsigned char *defn;

	s.name_length = NODE_LEN (hn);
	s.flags = hn->flags & NODE_POISONED;

	if (hn->type == NT_MACRO)
	  {
	    defn = cpp_macro_definition (pfile, hn);
	    s.definition_length = ustrlen (defn);
	  }
	else
	  {
	    defn = NODE_NAME (hn);
	    s.definition_length = s.name_length;
	  }
	
	if (fwrite (&s, sizeof (s), 1, f) != 1
	    || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
	  {
83 84
	    cpp_errno (pfile, CPP_DL_ERROR,
		       "while writing precompiled header");
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	    return 0;
	  }
      }
      return 1;
      
    case NT_ASSERTION:
      /* Not currently implemented.  */
      return 1;

    default:
      abort ();
    }
}

/* This structure records the names of the defined macros.
   It's also used as a callback structure for size_initial_idents
   and save_idents.  */

struct cpp_savedstate
{
  /* A hash table of the defined identifiers.  */
  htab_t definedhash;
  /* The size of the definitions of those identifiers (the size of
     'definedstrs').  */
  size_t hashsize;
110 111
  /* Number of definitions */
  size_t n_defs;
112
  /* Array of definitions.  In cpp_write_pch_deps it is used for sorting.  */
113
  cpp_hashnode **defs;
114 115 116 117 118 119 120 121 122
  /* Space for the next definition.  Definitions are null-terminated
     strings.  */
  unsigned char *definedstrs;
};

/* Save this identifier into the state: put it in the hash table,
   put the definition in 'definedstrs'.  */

static int
123
save_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
  struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
  
  if (hn->type != NT_VOID)
    {
      struct cpp_string news;
      void **slot;

      news.len = NODE_LEN (hn);
      news.text= NODE_NAME (hn);
      slot = htab_find_slot (ss->definedhash, &news, INSERT);
      if (*slot == NULL)
	{
	  struct cpp_string *sp;
	  unsigned char *text;
	  
140
	  sp = XNEW (struct cpp_string);
141 142 143
	  *slot = sp;

	  sp->len = NODE_LEN (hn);
144
	  sp->text = text = XNEWVEC (unsigned char, NODE_LEN (hn));
145 146 147 148 149 150 151 152 153 154
	  memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
	}
    }

  return 1;
}

/* Hash some memory in a generic way.  */

static hashval_t
155
hashmem (const void *p_p, size_t sz)
156 157 158 159 160 161 162 163 164 165 166 167 168 169
{
  const unsigned char *p = (const unsigned char *)p_p;
  size_t i;
  hashval_t h;
  
  h = 0;
  for (i = 0; i < sz; i++)
    h = h * 67 - (*p++ - 113);
  return h;
}

/* Hash a cpp string for the hashtable machinery.  */

static hashval_t
170
cpp_string_hash (const void *a_p)
171 172 173 174 175 176 177 178
{
  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
179
cpp_string_eq (const void *a_p, const void *b_p)
180 181 182 183 184 185 186 187 188 189 190 191 192
{
  const struct cpp_string *a = (const struct cpp_string *) a_p;
  const struct cpp_string *b = (const struct cpp_string *) b_p;
  return (a->len == b->len
	  && memcmp (a->text, b->text, a->len) == 0);
}

/* Save the current definitions of the cpp_reader for dependency
   checking purposes.  When writing a precompiled header, this should
   be called at the same point in the compilation as cpp_valid_state
   would be called when reading the precompiled header back in.  */

int
193
cpp_save_state (cpp_reader *r, FILE *f)
194 195
{
  /* Save the list of non-void identifiers for the dependency checking.  */
196
  r->savedstate = XNEW (struct cpp_savedstate);
197 198 199 200 201 202 203 204 205 206 207 208 209
  r->savedstate->definedhash = htab_create (100, cpp_string_hash, 
					    cpp_string_eq, NULL);
  cpp_forall_identifiers (r, save_idents, r->savedstate);
  
  /* Write out the list of defined identifiers.  */
  cpp_forall_identifiers (r, write_macdef, f);

  return 0;
}

/* Calculate the 'hashsize' field of the saved state.  */

static int
210
count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
{
  struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
  
  switch (hn->type)
    {
    case NT_MACRO:
      if (hn->flags & NODE_BUILTIN)
	return 1;
      
      /* else fall through.  */

    case NT_VOID:
      {
	struct cpp_string news;
	void **slot;
	
	news.len = NODE_LEN (hn);
	news.text = NODE_NAME (hn);
229
	slot = (void **) htab_find (ss->definedhash, &news);
230
	if (slot == NULL)
231 232 233 234
	  {
	    ss->hashsize += NODE_LEN (hn) + 1;
	    ss->n_defs += 1;
	  }
235 236 237 238 239 240 241 242 243 244 245 246
      }
      return 1;

    case NT_ASSERTION:
      /* Not currently implemented.  */
      return 1;

    default:
      abort ();
    }
}

247
/* Collect the identifiers into the state's string table.  */
248
static int
249
write_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
{
  struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
  
  switch (hn->type)
    {
    case NT_MACRO:
      if (hn->flags & NODE_BUILTIN)
	return 1;
      
      /* else fall through.  */

    case NT_VOID:
      {
	struct cpp_string news;
	void **slot;
	
	news.len = NODE_LEN (hn);
	news.text = NODE_NAME (hn);
268
	slot = (void **) htab_find (ss->definedhash, &news);
269 270
	if (slot == NULL)
	  {
271 272
	    ss->defs[ss->n_defs] = hn;
	    ss->n_defs += 1;
273 274 275 276 277 278 279 280 281 282 283 284 285
	  }
      }
      return 1;

    case NT_ASSERTION:
      /* Not currently implemented.  */
      return 1;

    default:
      abort ();
    }
}

286 287 288
/* Comparison function for qsort.  The arguments point to pointers of
   type ht_hashnode *.  */
static int
289
comp_hashnodes (const void *px, const void *py)
290 291 292 293 294 295
{
  cpp_hashnode *x = *(cpp_hashnode **) px;
  cpp_hashnode *y = *(cpp_hashnode **) py;
  return ustrcmp (NODE_NAME (x), NODE_NAME (y));
}

296 297 298 299
/* Write out the remainder of the dependency information.  This should be
   called after the PCH is ready to be saved.  */

int
300
cpp_write_pch_deps (cpp_reader *r, FILE *f)
301 302 303 304
{
  struct macrodef_struct z;
  struct cpp_savedstate *const ss = r->savedstate;
  unsigned char *definedstrs;
305
  size_t i;
306
  
307
  /* Collect the list of identifiers which have been seen and
308
     weren't defined to anything previously.  */
309 310
  ss->hashsize = 0;
  ss->n_defs = 0;
311
  cpp_forall_identifiers (r, count_defs, ss);
312

313
  ss->defs = XNEWVEC (cpp_hashnode *, ss->n_defs);
314
  ss->n_defs = 0;
315
  cpp_forall_identifiers (r, write_defs, ss);
316

317
  /* Sort the list, copy it into a buffer, and write it out.  */
318
  qsort (ss->defs, ss->n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
319
  definedstrs = ss->definedstrs = XNEWVEC (unsigned char, ss->hashsize);
320 321 322 323 324 325 326
  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;
    }

327 328 329
  memset (&z, 0, sizeof (z));
  z.definition_length = ss->hashsize;
  if (fwrite (&z, sizeof (z), 1, f) != 1
330
      || fwrite (ss->definedstrs, ss->hashsize, 1, f) != 1)
331
    {
332
      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
333 334
      return -1;
    }
335
  free (ss->definedstrs);
336 337 338 339 340 341 342 343 344 345 346

  /* Free the saved state.  */
  free (ss);
  r->savedstate = NULL;
  return 0;
}

/* Write out the definitions of the preprocessor, in a form suitable for
   cpp_read_state.  */

int
347
cpp_write_pch_state (cpp_reader *r, FILE *f)
348 349 350 351 352 353
{
  if (!r->deps)
    r->deps = deps_init ();

  if (deps_save (r->deps, f) != 0)
    {
354
      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
355 356 357
      return -1;
    }

Geoffrey Keating committed
358 359 360 361 362 363
  if (! _cpp_save_file_entries (r, f))
    {
      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
      return -1;
    }

364 365 366
  return 0;
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

/* 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
383 384
collect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
		  void *nl_p)
385 386 387 388 389 390 391 392
{
  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;
393
          nl->defs = XRESIZEVEC (cpp_hashnode *, nl->defs, nl->asize);
394 395 396 397 398 399 400 401 402
        }

      nl->defs[nl->n_defs] = hn;
      ++nl->n_defs;
    }
  return 1;
}


403 404 405 406 407 408 409 410 411 412 413 414 415 416
/* Return nonzero if FD is a precompiled header which is consistent
   with the preprocessor's current definitions.  It will be consistent
   when:

   - anything that was defined just before the PCH was generated 
     is defined the same way now; and
   - anything that was not defined then, but is defined now, was not
     used by the PCH.

   NAME is used to print warnings if `warn_invalid_pch' is set in the
   reader's flags.
*/

int
417
cpp_valid_state (cpp_reader *r, const char *name, int fd)
418 419 420
{
  struct macrodef_struct m;
  size_t namebufsz = 256;
421
  unsigned char *namebuf = XNEWVEC (unsigned char, namebufsz);
422
  unsigned char *undeftab = NULL;
423
  struct ht_node_list nl = { 0, 0, 0 };
424
  unsigned char *first, *last;
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
  unsigned int i;
  
  /* Read in the list of identifiers that must be defined
     Check that they are defined in the same way.  */
  for (;;)
    {
      cpp_hashnode *h;
      const unsigned char *newdefn;
      
      if (read (fd, &m, sizeof (m)) != sizeof (m))
	goto error;
      
      if (m.name_length == 0)
	break;

440 441 442 443 444 445 446 447 448
      /* If this file is already preprocessed, there won't be any
	 macros defined, and that's OK.  */
      if (CPP_OPTION (r, preprocessed))
	{
	  if (lseek (fd, m.definition_length, SEEK_CUR) == -1)
	    goto error;
	  continue;
	}

449 450 451 452
      if (m.definition_length > namebufsz)
	{
	  free (namebuf);
	  namebufsz = m.definition_length + 256;
453
	  namebuf = XNEWVEC (unsigned char, namebufsz);
454
	}
455

456 457 458 459 460 461 462 463 464 465
      if ((size_t)read (fd, namebuf, m.definition_length) 
	  != m.definition_length)
	goto error;
      
      h = cpp_lookup (r, namebuf, m.name_length);
      if (m.flags & NODE_POISONED
	  || h->type != NT_MACRO
	  || h->flags & NODE_POISONED)
	{
	  if (CPP_OPTION (r, warn_invalid_pch))
466
	    cpp_error (r, CPP_DL_WARNING_SYSHDR,
467 468 469 470 471 472 473 474 475 476 477
		       "%s: not used because `%.*s' not defined",
		       name, m.name_length, namebuf);
	  goto fail;
	}

      newdefn = cpp_macro_definition (r, h);
      
      if (m.definition_length != ustrlen (newdefn)
	  || memcmp (namebuf, newdefn, m.definition_length) != 0)
	{
	  if (CPP_OPTION (r, warn_invalid_pch))
478
	    cpp_error (r, CPP_DL_WARNING_SYSHDR,
479 480 481 482 483 484 485 486 487 488 489 490
	       "%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,
		       namebuf +  m.name_length);
	  goto fail;
	}
    }
  free (namebuf);
  namebuf = NULL;

  /* Read in the list of identifiers that must not be defined.
     Check that they really aren't.  */
491
  undeftab = XNEWVEC (unsigned char, m.definition_length);
492 493
  if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
    goto error;
494 495 496 497

  /* Collect identifiers from the current hash table.  */
  nl.n_defs = 0;
  nl.asize = 10;
498
  nl.defs = XNEWVEC (cpp_hashnode *, nl.asize);
499 500 501 502
  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.
503
     There should be no matches.  */
504 505 506 507 508
  first = undeftab;
  last = undeftab + m.definition_length;
  i = 0;
 
  while (first < last && i < nl.n_defs)
509
    {
510 511 512 513 514 515 516
      int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
 
      if (cmp < 0)
 	first += ustrlen (first) + 1;
      else if (cmp > 0)
 	++i;
      else
517 518
	{
	  if (CPP_OPTION (r, warn_invalid_pch))
519
	    cpp_error (r, CPP_DL_WARNING_SYSHDR, 
520 521 522 523
		       "%s: not used because `%s' is defined",
		       name, first);
	  goto fail;
	}
524
    }
525 526
   
  free(nl.defs);
527 528 529 530 531 532
  free (undeftab);

  /* We win!  */
  return 0;

 error:
533
  cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
534 535 536 537 538 539 540
  return -1;

 fail:
  if (namebuf != NULL)
    free (namebuf);
  if (undeftab != NULL)
    free (undeftab);
541 542
  if (nl.defs != NULL)
    free (nl.defs);
543 544 545
  return 1;
}

Geoffrey Keating committed
546
/* Save all the existing macros.  */
547 548 549

struct save_macro_data 
{
Geoffrey Keating committed
550
  uchar **defns;
551
  size_t count;
Geoffrey Keating committed
552
  size_t array_size;
553 554 555
  char **saved_pragmas;
};

Geoffrey Keating committed
556 557 558 559 560 561 562 563 564 565 566 567 568
/* 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.  */
569 570

static int 
Geoffrey Keating committed
571
save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
572 573 574 575 576
{
  struct save_macro_data *data = (struct save_macro_data *)data_p;
  if (h->type != NT_VOID
      && (h->flags & NODE_BUILTIN) == 0)
    {
Geoffrey Keating committed
577 578 579
      if (data->count == data->array_size)
	{
	  data->array_size *= 2;
580
	  data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size)); 
Geoffrey Keating committed
581 582 583
	}
      
      switch (h->type)
584
	{
Geoffrey Keating committed
585 586 587 588 589 590 591 592 593
	case NT_ASSERTION:
	  /* Not currently implemented.  */
	  return 1;

	case NT_MACRO:
	  {
	    const uchar * defn = cpp_macro_definition (r, h);
	    size_t defnlen = ustrlen (defn);

594 595
	    data->defns[data->count] = (uchar *) xmemdup (defn, defnlen,
                                                          defnlen + 2);
Geoffrey Keating committed
596 597 598 599 600 601
	    data->defns[data->count][defnlen] = '\n';
	  }
	  break;
	  
	default:
	  abort ();
602 603 604 605 606 607 608 609 610 611
	}
      data->count++;
    }
  return 1;
}

/* Prepare to restore the state, by saving the currently-defined
   macros in 'data'.  */

void
612
cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
613
{
614
  struct save_macro_data *d = XNEW (struct save_macro_data);
615
  
Geoffrey Keating committed
616
  d->array_size = 512;
617
  d->defns = XNEWVEC (uchar *, d->array_size);
Geoffrey Keating committed
618
  d->count = 0;
619 620 621 622 623 624 625 626 627 628
  cpp_forall_identifiers (r, save_macros, d);
  d->saved_pragmas = _cpp_save_pragma_names (r);
  *data = d;
}

/* 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
629 630
cpp_read_state (cpp_reader *r, const char *name, FILE *f,
		struct save_macro_data *data)
631
{
632
  size_t i;
Geoffrey Keating committed
633
  struct lexer_state old_state;
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

  /* Restore spec_nodes, which will be full of references to the old 
     hashtable entries and so will now be invalid.  */
  {
    struct spec_nodes *s = &r->spec_nodes;
    s->n_defined	= cpp_lookup (r, DSC("defined"));
    s->n_true		= cpp_lookup (r, DSC("true"));
    s->n_false		= cpp_lookup (r, DSC("false"));
    s->n__VA_ARGS__     = cpp_lookup (r, DSC("__VA_ARGS__"));
  }

  old_state = r->state;
  r->state.in_directive = 1;
  r->state.prevent_expansion = 1;
  r->state.angled_headers = 0;

Geoffrey Keating committed
650 651
  /* Run through the carefully-saved macros, insert them.  */
  for (i = 0; i < data->count; i++)
652 653
    {
      cpp_hashnode *h;
Geoffrey Keating committed
654 655
      size_t namelen;
      uchar *defn;
656

657
      namelen = ustrcspn (data->defns[i], "( \n");
Geoffrey Keating committed
658 659
      h = cpp_lookup (r, data->defns[i], namelen);
      defn = data->defns[i] + namelen;
660

Geoffrey Keating committed
661 662 663 664
      /* 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)
665
	{
Geoffrey Keating committed
666
	  if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
667
	      != NULL)
668
	    {
669
	      _cpp_clean_line (r);
670 671 672 673 674 675 676 677
	      if (!_cpp_create_definition (r, h))
		abort ();
	      _cpp_pop_buffer (r);
	    }
	  else
	    abort ();
	}

Geoffrey Keating committed
678 679
      free (data->defns[i]);
    }
680
  r->state = old_state;
Geoffrey Keating committed
681 682 683 684

  _cpp_restore_pragma_names (r, data->saved_pragmas);

  free (data);
685 686 687 688 689

  if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
      != 0)
    goto error;

Geoffrey Keating committed
690 691 692
  if (! _cpp_read_file_entries (r, f))
    goto error;

693 694 695
  return 0;
  
 error:
696
  cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
697 698
  return -1;
}