pch.c 23.3 KB
Newer Older
1
/* Part of CPP library.  (Precompiled header reading/writing.)
2
   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
   Free Software Foundation, Inc.
4 5 6

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
7
Free Software Foundation; either version 3, or (at your option) any
8 9 10 11 12 13 14 15
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
16 17
along with this program; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
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 *);
Kai Tietz committed
36 37
static int _cpp_save_pushed_macros (cpp_reader *, FILE *);
static int _cpp_restore_pushed_macros (cpp_reader *, FILE *);
38 39

/* This structure represents a macro definition on disk.  */
Mike Stump committed
40
struct macrodef_struct
41 42 43 44 45 46
{
  unsigned int definition_length;
  unsigned short name_length;
  unsigned short flags;
};

Mike Stump committed
47
/* This is how we write out a macro definition.
48 49 50
   Suitable for being called by cpp_forall_identifiers.  */

static int
51
write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
52 53 54 55 56 57 58
{
  FILE *f = (FILE *) file_p;
  switch (hn->type)
    {
    case NT_VOID:
      if (! (hn->flags & NODE_POISONED))
	return 1;
Mike Stump committed
59

60
    case NT_MACRO:
61 62 63
      if ((hn->flags & NODE_BUILTIN)
	  && (!pfile->cb.user_builtin_macro
	      || !pfile->cb.user_builtin_macro (pfile, hn)))
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	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;
	  }
Mike Stump committed
83

84 85 86
	if (fwrite (&s, sizeof (s), 1, f) != 1
	    || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
	  {
87 88
	    cpp_errno (pfile, CPP_DL_ERROR,
		       "while writing precompiled header");
89 90 91 92
	    return 0;
	  }
      }
      return 1;
Mike Stump committed
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    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;
114 115
  /* Number of definitions */
  size_t n_defs;
116
  /* Array of definitions.  In cpp_write_pch_deps it is used for sorting.  */
117
  cpp_hashnode **defs;
118 119 120 121 122 123 124 125 126
  /* 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
127
save_idents (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
128 129
{
  struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
Mike Stump committed
130

131 132 133 134 135 136 137 138 139 140 141 142
  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;
Mike Stump committed
143

144
	  sp = XNEW (struct cpp_string);
145 146 147
	  *slot = sp;

	  sp->len = NODE_LEN (hn);
148
	  sp->text = text = XNEWVEC (unsigned char, NODE_LEN (hn));
149 150 151 152 153 154 155 156 157 158
	  memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
	}
    }

  return 1;
}

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

static hashval_t
159
hashmem (const void *p_p, size_t sz)
160 161 162 163
{
  const unsigned char *p = (const unsigned char *)p_p;
  size_t i;
  hashval_t h;
Mike Stump committed
164

165 166 167 168 169 170 171 172 173
  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
174
cpp_string_hash (const void *a_p)
175 176 177 178 179 180 181 182
{
  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
183
cpp_string_eq (const void *a_p, const void *b_p)
184 185 186 187 188 189 190 191 192 193 194 195 196
{
  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
197
cpp_save_state (cpp_reader *r, FILE *f)
198 199
{
  /* Save the list of non-void identifiers for the dependency checking.  */
200
  r->savedstate = XNEW (struct cpp_savedstate);
Mike Stump committed
201
  r->savedstate->definedhash = htab_create (100, cpp_string_hash,
202 203
					    cpp_string_eq, NULL);
  cpp_forall_identifiers (r, save_idents, r->savedstate);
Mike Stump committed
204

205 206 207 208 209 210 211 212 213
  /* 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
214
count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
215 216
{
  struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
Mike Stump committed
217

218 219 220 221 222
  switch (hn->type)
    {
    case NT_MACRO:
      if (hn->flags & NODE_BUILTIN)
	return 1;
Mike Stump committed
223

224 225 226 227 228 229
      /* else fall through.  */

    case NT_VOID:
      {
	struct cpp_string news;
	void **slot;
Mike Stump committed
230

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

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

    default:
      abort ();
    }
}

251
/* Collect the identifiers into the state's string table.  */
252
static int
253
write_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
254 255
{
  struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
Mike Stump committed
256

257 258 259 260 261
  switch (hn->type)
    {
    case NT_MACRO:
      if (hn->flags & NODE_BUILTIN)
	return 1;
Mike Stump committed
262

263 264 265 266 267 268
      /* else fall through.  */

    case NT_VOID:
      {
	struct cpp_string news;
	void **slot;
Mike Stump committed
269

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

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

    default:
      abort ();
    }
}

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

300 301 302 303
/* Write out the remainder of the dependency information.  This should be
   called after the PCH is ready to be saved.  */

int
304
cpp_write_pch_deps (cpp_reader *r, FILE *f)
305 306 307 308
{
  struct macrodef_struct z;
  struct cpp_savedstate *const ss = r->savedstate;
  unsigned char *definedstrs;
309
  size_t i;
Mike Stump committed
310

311
  /* Collect the list of identifiers which have been seen and
312
     weren't defined to anything previously.  */
313 314
  ss->hashsize = 0;
  ss->n_defs = 0;
315
  cpp_forall_identifiers (r, count_defs, ss);
316

317
  ss->defs = XNEWVEC (cpp_hashnode *, ss->n_defs);
318
  ss->n_defs = 0;
319
  cpp_forall_identifiers (r, write_defs, ss);
320

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

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

  /* Free the saved state.  */
  free (ss);
  r->savedstate = NULL;
344 345 346 347 348 349 350 351

  /* Save the next value of __COUNTER__. */
  if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
    {
      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
      return -1;
    }

352 353 354 355 356 357 358
  return 0;
}

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

int
359
cpp_write_pch_state (cpp_reader *r, FILE *f)
360 361 362 363 364 365
{
  if (!r->deps)
    r->deps = deps_init ();

  if (deps_save (r->deps, f) != 0)
    {
366
      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
367 368 369
      return -1;
    }

Geoffrey Keating committed
370 371 372 373 374 375
  if (! _cpp_save_file_entries (r, f))
    {
      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
      return -1;
    }

376 377 378 379 380 381 382 383 384
  /* Save the next __COUNTER__ value.  When we include a precompiled header,
     we need to start at the offset we would have if the header had been
     included normally. */
  if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
    {
      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
      return -1;
    }

Kai Tietz committed
385 386 387 388 389 390 391
  /* Write saved macros.  */
  if (! _cpp_save_pushed_macros (r, f))
    {
      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
      return -1;
    }

392 393 394
  return 0;
}

Kai Tietz committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
static int
_cpp_restore_pushed_macros (cpp_reader *r, FILE *f)
{
  size_t count_saved = 0;
  size_t i;
  struct def_pragma_macro *p;
  size_t nlen;
  cpp_hashnode *h = NULL;
  cpp_macro *m;
  uchar *defn;
  size_t defnlen;

  if (fread (&count_saved, sizeof (count_saved), 1, f) != 1)
    return 0;
  if (! count_saved)
    return 1;
  for (i = 0; i < count_saved; i++)
    {
      if (fread (&nlen, sizeof (nlen), 1, f) != 1)
	return 0;
      p = XNEW (struct def_pragma_macro);
      p->name = XNEWVAR (char, nlen + 1);
      p->name[nlen] = 0;
      if (fread (p->name, nlen, 1, f) != 1)
	return 0;
      /* Save old state.  */
      m = cpp_push_definition (r, p->name);
      if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
	return 0;
      defn = XNEWVAR (uchar, defnlen + 2);
      defn[defnlen] = '\n';
      defn[defnlen + 1] = 0;

      if (fread (defn, defnlen, 1, f) != 1)
	return 0;
      cpp_pop_definition (r, p->name, NULL);
      {
	size_t namelen;
	uchar *dn;

	namelen = ustrcspn (defn, "( \n");
	h = cpp_lookup (r, defn, namelen);
	dn = defn + namelen;

	h->type = NT_VOID;
	h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
	if (cpp_push_buffer (r, dn, ustrchr (dn, '\n') - dn, true)
	    != NULL)
	  {
	    _cpp_clean_line (r);
	    if (!_cpp_create_definition (r, h))
	      abort ();
	    _cpp_pop_buffer (r);
	  }
	else
	  abort ();
      }
      p->value = cpp_push_definition (r, p->name);

      free (defn);
      p->next = r->pushed_macros;
      r->pushed_macros = p;
      /* Restore current state.  */
      cpp_pop_definition (r, p->name, m);
    }
  return 1;
}

static int
_cpp_save_pushed_macros (cpp_reader *r, FILE *f)
{
  size_t count_saved = 0;
  size_t i;
  struct def_pragma_macro *p,**pp;
  cpp_hashnode *node;
  cpp_macro *m;
  size_t defnlen;
  const uchar *defn;

  /* Get count. */
  p = r->pushed_macros;
  while (p != NULL)
    {
      count_saved++;
      p = p->next;
    }
  if (fwrite (&count_saved, sizeof (count_saved), 1, f) != 1)
    return 0;
  if (!count_saved)
    return 1;

  pp = (struct def_pragma_macro **) alloca (sizeof (struct def_pragma_macro *)
					    * count_saved);
  /* Store them in reverse order.  */
  p = r->pushed_macros;
  i = count_saved;
  while (p != NULL)
    {
      --i;
      pp[i] = p;
      p = p->next;
    }
  for (i = 0; i < count_saved; i++)
    {
      /* Save old state.  */
      m = cpp_push_definition (r, pp[i]->name);
      /* Set temporary macro name to saved state.  */
      cpp_pop_definition (r, pp[i]->name, pp[i]->value);
      node = _cpp_lex_identifier (r, pp[i]->name);
      defnlen = strlen (pp[i]->name);
      if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
	  || fwrite (pp[i]->name, defnlen, 1, f) != 1)
	return 0;
      defn = cpp_macro_definition (r, node);
      defnlen = ustrlen (defn);
      if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
	  || fwrite (defn, defnlen, 1, f) != 1)
	return 0;
      /* Restore current state.  */
      cpp_pop_definition (r, pp[i]->name, m);
    }
  return 1;
}

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

/* 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
535 536
collect_ht_nodes (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn,
		  void *nl_p)
537 538 539 540 541 542 543 544
{
  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;
545
          nl->defs = XRESIZEVEC (cpp_hashnode *, nl->defs, nl->asize);
546 547 548 549 550 551 552 553 554
        }

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


555 556 557 558
/* Return nonzero if FD is a precompiled header which is consistent
   with the preprocessor's current definitions.  It will be consistent
   when:

Mike Stump committed
559
   - anything that was defined just before the PCH was generated
560 561 562 563 564 565 566 567 568
     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
569
cpp_valid_state (cpp_reader *r, const char *name, int fd)
570 571 572
{
  struct macrodef_struct m;
  size_t namebufsz = 256;
573
  unsigned char *namebuf = XNEWVEC (unsigned char, namebufsz);
574
  unsigned char *undeftab = NULL;
575
  struct ht_node_list nl = { 0, 0, 0 };
576
  unsigned char *first, *last;
577
  unsigned int i;
578
  unsigned int counter;
Mike Stump committed
579

580 581 582 583 584 585
  /* 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;
Mike Stump committed
586

587 588
      if (read (fd, &m, sizeof (m)) != sizeof (m))
	goto error;
Mike Stump committed
589

590 591 592
      if (m.name_length == 0)
	break;

593 594 595 596 597 598 599 600 601
      /* 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;
	}

602 603 604 605
      if (m.definition_length > namebufsz)
	{
	  free (namebuf);
	  namebufsz = m.definition_length + 256;
606
	  namebuf = XNEWVEC (unsigned char, namebufsz);
607
	}
608

Mike Stump committed
609
      if ((size_t)read (fd, namebuf, m.definition_length)
610 611
	  != m.definition_length)
	goto error;
Mike Stump committed
612

613 614 615 616 617
      h = cpp_lookup (r, namebuf, m.name_length);
      if (m.flags & NODE_POISONED
	  || h->flags & NODE_POISONED)
	{
	  if (CPP_OPTION (r, warn_invalid_pch))
618 619 620
	    cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
		                "%s: not used because `%.*s' is poisoned",
		                name, m.name_length, namebuf);
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	  goto fail;
	}

      if (h->type != NT_MACRO)
	{
	  /* It's ok if __GCC_HAVE_DWARF2_CFI_ASM becomes undefined,
	     as in, when the PCH file is created with -g and we're
	     attempting to use it without -g.  Restoring the PCH file
	     is supposed to bring in this definition *and* enable the
	     generation of call frame information, so that precompiled
	     definitions that take this macro into accout, to decide
	     what asm to emit, won't issue .cfi directives when the
	     compiler doesn't.  */
	  if (!(h->flags & NODE_USED)
	      && m.name_length == sizeof ("__GCC_HAVE_DWARF2_CFI_ASM") - 1
	      && !memcmp (namebuf, "__GCC_HAVE_DWARF2_CFI_ASM", m.name_length))
	    continue;

	  if (CPP_OPTION (r, warn_invalid_pch))
640 641 642
	    cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
		                "%s: not used because `%.*s' not defined",
		                name, m.name_length, namebuf);
643 644 645 646
	  goto fail;
	}

      newdefn = cpp_macro_definition (r, h);
Mike Stump committed
647

648 649 650 651
      if (m.definition_length != ustrlen (newdefn)
	  || memcmp (namebuf, newdefn, m.definition_length) != 0)
	{
	  if (CPP_OPTION (r, warn_invalid_pch))
652
	    cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
653 654 655 656 657 658 659 660 661 662 663 664
	       "%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.  */
665
  undeftab = XNEWVEC (unsigned char, m.definition_length);
666 667
  if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
    goto error;
668 669 670 671

  /* Collect identifiers from the current hash table.  */
  nl.n_defs = 0;
  nl.asize = 10;
672
  nl.defs = XNEWVEC (cpp_hashnode *, nl.asize);
673 674
  cpp_forall_identifiers (r, &collect_ht_nodes, &nl);
  qsort (nl.defs, nl.n_defs, sizeof (cpp_hashnode *), &comp_hashnodes);
Mike Stump committed
675

676
  /* Loop through nl.defs and undeftab, both of which are sorted lists.
677
     There should be no matches.  */
678 679 680
  first = undeftab;
  last = undeftab + m.definition_length;
  i = 0;
Mike Stump committed
681

682
  while (first < last && i < nl.n_defs)
683
    {
684
      int cmp = ustrcmp (first, NODE_NAME (nl.defs[i]));
Mike Stump committed
685

686 687 688 689 690
      if (cmp < 0)
 	first += ustrlen (first) + 1;
      else if (cmp > 0)
 	++i;
      else
691 692
	{
	  if (CPP_OPTION (r, warn_invalid_pch))
693 694 695
	    cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
		                "%s: not used because `%s' is defined",
		                name, first);
696 697
	  goto fail;
	}
698
    }
Mike Stump committed
699

700
  free(nl.defs);
701
  nl.defs = NULL;
702
  free (undeftab);
703 704 705 706 707 708 709 710 711 712
  undeftab = NULL;

  /* Read in the next value of __COUNTER__.
     Check that (a) __COUNTER__ was not used in the pch or (b) __COUNTER__
     has not been used in this translation unit. */
  if (read (fd, &counter, sizeof (counter)) != sizeof (counter))
    goto error;
  if (counter && r->counter)
    {
      if (CPP_OPTION (r, warn_invalid_pch))
713 714 715
	cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
		            "%s: not used because `__COUNTER__' is invalid",
		            name);
716 717
	goto fail;
    }
718 719 720 721 722

  /* We win!  */
  return 0;

 error:
723
  cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
724 725 726 727 728 729 730
  return -1;

 fail:
  if (namebuf != NULL)
    free (namebuf);
  if (undeftab != NULL)
    free (undeftab);
731 732
  if (nl.defs != NULL)
    free (nl.defs);
733 734 735
  return 1;
}

Geoffrey Keating committed
736
/* Save all the existing macros.  */
737

Mike Stump committed
738
struct save_macro_data
739
{
Geoffrey Keating committed
740
  uchar **defns;
741
  size_t count;
Geoffrey Keating committed
742
  size_t array_size;
743 744 745
  char **saved_pragmas;
};

Geoffrey Keating committed
746 747 748 749 750 751 752 753 754 755 756 757 758
/* 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.  */
759

Mike Stump committed
760
static int
Geoffrey Keating committed
761
save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
762 763
{
  struct save_macro_data *data = (struct save_macro_data *)data_p;
764 765 766 767 768 769

  if ((h->flags & NODE_BUILTIN)
      && h->type == NT_MACRO
      && r->cb.user_builtin_macro)
    r->cb.user_builtin_macro (r, h);

770 771 772
  if (h->type != NT_VOID
      && (h->flags & NODE_BUILTIN) == 0)
    {
Geoffrey Keating committed
773 774 775
      if (data->count == data->array_size)
	{
	  data->array_size *= 2;
Mike Stump committed
776
	  data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size));
Geoffrey Keating committed
777
	}
Mike Stump committed
778

Geoffrey Keating committed
779
      switch (h->type)
780
	{
Geoffrey Keating committed
781 782 783 784 785 786 787 788 789
	case NT_ASSERTION:
	  /* Not currently implemented.  */
	  return 1;

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

790 791
	    data->defns[data->count] = (uchar *) xmemdup (defn, defnlen,
                                                          defnlen + 2);
Geoffrey Keating committed
792 793 794
	    data->defns[data->count][defnlen] = '\n';
	  }
	  break;
Mike Stump committed
795

Geoffrey Keating committed
796 797
	default:
	  abort ();
798 799 800 801 802 803 804 805 806 807
	}
      data->count++;
    }
  return 1;
}

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

void
808
cpp_prepare_state (cpp_reader *r, struct save_macro_data **data)
809
{
810
  struct save_macro_data *d = XNEW (struct save_macro_data);
Mike Stump committed
811

Geoffrey Keating committed
812
  d->array_size = 512;
813
  d->defns = XNEWVEC (uchar *, d->array_size);
Geoffrey Keating committed
814
  d->count = 0;
815 816 817 818 819 820
  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,
Mike Stump committed
821
   apply all its definitions (and undefinitions) to the current state.
822 823 824
   DEPNAME is passed to deps_restore.  */

int
825 826
cpp_read_state (cpp_reader *r, const char *name, FILE *f,
		struct save_macro_data *data)
827
{
828
  size_t i;
Geoffrey Keating committed
829
  struct lexer_state old_state;
830
  unsigned int counter;
831

Mike Stump committed
832
  /* Restore spec_nodes, which will be full of references to the old
833 834 835 836 837 838 839 840 841 842 843 844 845 846
     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
847 848
  /* Run through the carefully-saved macros, insert them.  */
  for (i = 0; i < data->count; i++)
849 850
    {
      cpp_hashnode *h;
Geoffrey Keating committed
851 852
      size_t namelen;
      uchar *defn;
853

854
      namelen = ustrcspn (data->defns[i], "( \n");
Geoffrey Keating committed
855 856
      h = cpp_lookup (r, data->defns[i], namelen);
      defn = data->defns[i] + namelen;
857

Geoffrey Keating committed
858 859 860 861
      /* 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)
862
	{
Geoffrey Keating committed
863
	  if (cpp_push_buffer (r, defn, ustrchr (defn, '\n') - defn, true)
864
	      != NULL)
865
	    {
866
	      _cpp_clean_line (r);
867 868 869 870 871 872 873 874
	      if (!_cpp_create_definition (r, h))
		abort ();
	      _cpp_pop_buffer (r);
	    }
	  else
	    abort ();
	}

Geoffrey Keating committed
875 876
      free (data->defns[i]);
    }
877
  r->state = old_state;
Geoffrey Keating committed
878 879 880 881

  _cpp_restore_pragma_names (r, data->saved_pragmas);

  free (data);
882 883 884 885 886

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

Geoffrey Keating committed
887 888 889
  if (! _cpp_read_file_entries (r, f))
    goto error;

890 891 892 893 894 895
  if (fread (&counter, sizeof (counter), 1, f) != 1)
    goto error;

  if (!r->counter)
    r->counter = counter;

Kai Tietz committed
896 897 898
  /* Read pushed macros. */
  if (! _cpp_restore_pushed_macros (r, f))
    goto error;
899
  return 0;
Mike Stump committed
900

901
 error:
902
  cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
903 904
  return -1;
}